Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
AttachmentInfo.hpp
1#pragma once
2
3#include <vector>
4#include <string>
5#include <map>
6#include <functional>
7#include <stdint.h>
8
9#include <Common/EnumTraits.hpp>
10#include <Common/Graphics/Formats.hpp>
11#include <Common/Graphics/Image.hpp>
12
13#include "RenderGraphResourceRef.hpp"
14
15namespace Grindstone::Renderer {
16 struct MetaSize {
17 uint32_t value;
18
19 enum class ImageSizeInfo : uint32_t {
20 // If this bit is disabled, the base of the image size is divided, if it is enabled, it is multiplied, by the size it is relative to.
21 Multiply = 1u << 29u,
22
23 // If neither this bit, nor SwapchainRelative is enabled, the size is absolute
24 ViewportRelative = 1u << 30u,
25
26 // If neither this bit, nor ViewportRelative is enabled, the size is absolute
27 SwapchainRelative = 1u << 31u,
28
29 MetaBits = Multiply | ViewportRelative | SwapchainRelative,
30 SizeBits = ~static_cast<uint32_t>(MetaBits)
31 };
32
33 bool operator==(const MetaSize& other) const {
34 return value == other.value;
35 }
36
37 explicit operator uint32_t() const {
38 return value;
39 }
40
41 static const MetaSize Pixels(uint32_t x) { return { .value = x }; };
42 static const MetaSize Viewport() { return { static_cast<uint32_t>(ImageSizeInfo::ViewportRelative) }; };
43 static const MetaSize Swapchain() { return { static_cast<uint32_t>(ImageSizeInfo::SwapchainRelative) }; };
44 static MetaSize MultiplyViewport(uint32_t x) { return { static_cast<uint32_t>(ImageSizeInfo::ViewportRelative) | static_cast<uint32_t>(ImageSizeInfo::Multiply) | x }; }
45 static MetaSize MultiplySwapchain(uint32_t x) { return { static_cast<uint32_t>(ImageSizeInfo::SwapchainRelative) | static_cast<uint32_t>(ImageSizeInfo::Multiply) | x }; }
46
47 static MetaSize DivideViewport(uint32_t x) { return { static_cast<uint32_t>(ImageSizeInfo::ViewportRelative) | x }; }
48 static MetaSize DivideSwapchain(uint32_t x) { return { static_cast<uint32_t>(ImageSizeInfo::SwapchainRelative) | x }; }
49
50 inline bool IsSwapchainRelative() const {
51 return value & static_cast<uint32_t>(ImageSizeInfo::SwapchainRelative);
52 }
53
54 inline bool IsViewportRelative() const {
55 return value & static_cast<uint32_t>(ImageSizeInfo::ViewportRelative);
56 }
57
58 inline bool IsMultiply() const {
59 return value & static_cast<uint32_t>(ImageSizeInfo::Multiply);
60 }
61
62 inline bool IsDivide() const {
63 return (value & static_cast<uint32_t>(ImageSizeInfo::Multiply)) == 0;
64 }
65
66 uint32_t Resolve(uint32_t viewportResolution, uint32_t swapchainResolution) const {
67 uint32_t size = value & static_cast<uint32_t>(ImageSizeInfo::SizeBits);
68 bool isAbsoluteSize = (size == 0);
69
70 if (IsViewportRelative()) {
71 if (IsMultiply()) {
72 return isAbsoluteSize
73 ? viewportResolution
74 : viewportResolution * size;
75 }
76 else {
77 return isAbsoluteSize
78 ? viewportResolution
79 : viewportResolution / size;
80 }
81 }
82 else if (IsSwapchainRelative()) {
83 if (IsMultiply()) {
84 return isAbsoluteSize
85 ? swapchainResolution
86 : swapchainResolution * size;
87 }
88 else {
89 return isAbsoluteSize
90 ? swapchainResolution
91 : swapchainResolution / size;
92 }
93 }
94 else {
95 return size;
96 }
97 }
98 };
99
100 struct MetaSize2D {
101 MetaSize x;
102 MetaSize y;
103
104 static const MetaSize2D Zero() { return { 0, 0 }; };
105
106 static const MetaSize2D Pixels(uint32_t x, uint32_t y) { return { MetaSize::Pixels(x), MetaSize::Pixels(y) }; };
107 static const MetaSize2D Viewport() { return { MetaSize::Viewport(), MetaSize::Viewport() }; };
108 static const MetaSize2D Swapchain() { return { MetaSize::Swapchain(), MetaSize::Swapchain() }; };
109
110 static MetaSize2D MultiplyViewport(uint32_t x) { return { MetaSize::MultiplyViewport(x), MetaSize::MultiplyViewport(x) }; }
111 static MetaSize2D MultiplySwapchain(uint32_t x) { return { MetaSize::MultiplySwapchain(x), MetaSize::MultiplySwapchain(x) }; }
112
113 static MetaSize2D DivideViewport(uint32_t x) { return { MetaSize::DivideViewport(x), MetaSize::DivideViewport(x) }; }
114 static MetaSize2D DivideSwapchain(uint32_t x) { return { MetaSize::DivideSwapchain(x), MetaSize::DivideSwapchain(x) }; }
115
116 bool operator==(const MetaSize2D& other) const {
117 return x == other.x && y == other.y;
118 }
119
120 Grindstone::Math::Uint2 Resolve(Grindstone::Math::Uint2 viewportResolution, Grindstone::Math::Uint2 swapchainResolution) const {
121 return { x.Resolve(viewportResolution.x, swapchainResolution.x), y.Resolve(viewportResolution.y, swapchainResolution.y) };
122 }
123
124 Grindstone::Math::Uint2 Resolve(Grindstone::Math::Uint2 swapchainResolution) const {
125 GS_ASSERT(!x.IsViewportRelative());
126 GS_ASSERT(!y.IsViewportRelative());
127 return { x.Resolve(0, swapchainResolution.x), y.Resolve(0, swapchainResolution.y) };
128 }
129 };
130
131 struct MetaRect {
132 MetaSize2D offset;
133 MetaSize2D extent;
134
135 static const MetaRect Pixels(uint32_t width, uint32_t height) {
136 return { MetaSize2D::Zero(), MetaSize2D::Pixels(width, height) };
137 }
138 static const MetaRect Pixels(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
139 return { MetaSize2D::Pixels(x, y), MetaSize2D::Pixels(width, height) };
140 }
141 static const MetaRect Viewport() { return { MetaSize2D::Zero(), MetaSize2D::Viewport() }; }
142 static const MetaRect Swapchain() { return { MetaSize2D::Zero(), MetaSize2D::Swapchain() }; }
143
144 bool operator==(const MetaRect& other) const {
145 return offset == other.offset && extent == other.extent;
146 }
147
148 Grindstone::Math::IntRect2D Resolve(Grindstone::Math::Uint2 viewportResolution, Grindstone::Math::Uint2 swapchainResolution) const {
149 return { offset.Resolve(viewportResolution, swapchainResolution), extent.Resolve(viewportResolution, swapchainResolution) };
150 }
151
152 Grindstone::Math::IntRect2D Resolve(Grindstone::Math::Uint2 swapchainResolution) const {
153 return { offset.Resolve(swapchainResolution), extent.Resolve(swapchainResolution) };
154 }
155 };
156
157 enum class RenderGraphImageUsage : uint16_t {
158 None = 0,
159 Read = 1 << 0,
160 Write = 1 << 1,
161 Sampled = 1 << 2,
162 Storage = 1 << 3,
163 ColorAttachment = 1 << 4,
164 DepthAttachment = 1 << 5,
165 StencilAttachment = 1 << 6,
166 SubpassInputAttachment = 1 << 7,
167 Transfer = 1 << 8,
168
169 SampledRead = static_cast<uint16_t>(RenderGraphImageUsage::Sampled) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
170 StorageRead = static_cast<uint16_t>(RenderGraphImageUsage::Storage) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
171 StorageWrite = static_cast<uint16_t>(RenderGraphImageUsage::Storage) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
172 StorageReadWrite = static_cast<uint16_t>(RenderGraphImageUsage::StorageRead) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
173 ColorAttachmentRead = static_cast<uint16_t>(RenderGraphImageUsage::ColorAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
174 ColorAttachmentWrite = static_cast<uint16_t>(RenderGraphImageUsage::ColorAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
175 ColorAttachmentReadWrite = static_cast<uint16_t>(RenderGraphImageUsage::ColorAttachmentRead) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
176 DepthAttachmentRead = static_cast<uint16_t>(RenderGraphImageUsage::DepthAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
177 DepthAttachmentWrite = static_cast<uint16_t>(RenderGraphImageUsage::DepthAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
178 DepthAttachmentReadWrite = static_cast<uint16_t>(RenderGraphImageUsage::DepthAttachmentRead) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
179 StencilAttachmentRead = static_cast<uint16_t>(RenderGraphImageUsage::StencilAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
180 StencilAttachmentWrite = static_cast<uint16_t>(RenderGraphImageUsage::StencilAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
181 StencilAttachmentReadWrite = static_cast<uint16_t>(RenderGraphImageUsage::StencilAttachmentRead) | static_cast<uint16_t>(RenderGraphImageUsage::Write),
182 DepthStencilAttachmentRead = static_cast<uint16_t>(RenderGraphImageUsage::DepthAttachmentRead) | static_cast<uint16_t>(RenderGraphImageUsage::StencilAttachment),
183 DepthStencilAttachmentWrite = static_cast<uint16_t>(RenderGraphImageUsage::DepthAttachmentWrite) | static_cast<uint16_t>(RenderGraphImageUsage::StencilAttachment),
184 DepthStencilAttachmentReadWrite = static_cast<uint16_t>(RenderGraphImageUsage::DepthAttachmentReadWrite) | static_cast<uint16_t>(RenderGraphImageUsage::StencilAttachment),
185 SubpassInputAttachmentRead = static_cast<uint16_t>(RenderGraphImageUsage::SubpassInputAttachment) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
186 TransferSrc = static_cast<uint16_t>(RenderGraphImageUsage::Transfer) | static_cast<uint16_t>(RenderGraphImageUsage::Read),
187 TransferDst = static_cast<uint16_t>(RenderGraphImageUsage::Transfer) | static_cast<uint16_t>(RenderGraphImageUsage::Write)
188 };
189}
190
191GS_ENUM_FLAGS_FUNCS(Grindstone::Renderer::RenderGraphImageUsage)
192
193namespace Grindstone::Renderer {
196 RenderGraphImageUsage usage;
197
199 Grindstone::GraphicsAPI::LoadOp loadOp;
201 };
202
203 AttachmentMeta attachment;
204
205 bool IsRead() const {
206 return Any(usage & RenderGraphImageUsage::Read);
207 }
208
209 bool IsWrite() const {
210 return Any(usage & RenderGraphImageUsage::Write);
211 }
212
213 bool IsTransfer() const {
214 return Any(usage & RenderGraphImageUsage::Transfer);
215 }
216
217 bool IsShaderInput() const {
218 return Any(usage & (RenderGraphImageUsage::Sampled | RenderGraphImageUsage::Storage));
219 }
220
221 bool IsAttachment() const {
222 return Any(usage & (RenderGraphImageUsage::ColorAttachment | RenderGraphImageUsage::DepthAttachment | RenderGraphImageUsage::StencilAttachment));
223 }
224 };
225
227 Grindstone::String name;
228 MetaSize2D size = MetaSize2D::Viewport();
229 uint32_t samples = 1;
230 uint32_t mipLevels = 1;
231 uint32_t depth = 1;
232 uint32_t arrayLayers = 1;
233 Grindstone::GraphicsAPI::Format format;
234
235 GraphicsAPI::ImageDimension imageDimensions = GraphicsAPI::ImageDimension::Dimension2D;
236 GraphicsAPI::MemoryUsage memoryUsage = GraphicsAPI::MemoryUsage::GPUOnly;
238
239 Grindstone::GraphicsAPI::ImageLayout externalInitialLayout;
240 Grindstone::GraphicsAPI::AccessFlags externalInitialAccessFlags;
241 Grindstone::GraphicsAPI::PipelineStageBit externalInitialPipelineStage;
242 Grindstone::GraphicsAPI::ImageLayout externalFinalLayout;
243 Grindstone::GraphicsAPI::AccessFlags externalFinalAccessFlags;
244 Grindstone::GraphicsAPI::PipelineStageBit externalFinalPipelineStage;
245 std::function<Grindstone::GraphicsAPI::Image*()> externalGetterCallback;
246
247 bool operator==(const ImageDescription& other) const {
248 return
249 size == other.size &&
250 samples == other.samples &&
251 mipLevels == other.mipLevels &&
252 arrayLayers == other.arrayLayers &&
253 depth == other.depth &&
254 format == other.format;
255 }
256 };
257
260 Grindstone::GraphicsAPI::LoadOp loadOp;
262 };
263}
264
265namespace std {
266 template<>
267 struct hash<Grindstone::Renderer::MetaSize> {
268 std::size_t operator()(const Grindstone::Renderer::MetaSize& desc) const noexcept {
269 size_t result = std::hash<size_t>{}(
270 static_cast<size_t>(desc.value)
271 );
272 }
273 };
274
275 template<>
276 struct hash<Grindstone::Renderer::MetaSize2D> {
277 std::size_t operator()(const Grindstone::Renderer::MetaSize2D& desc) const noexcept {
278 size_t result = std::hash<size_t>{}(
279 static_cast<size_t>(static_cast<uint32_t>(desc.x)) |
280 static_cast<size_t>(static_cast<uint32_t>(desc.y)) << 32
281 );
282 }
283 };
284
285 template<>
286 struct hash<Grindstone::Renderer::MetaRect> {
287 std::size_t operator()(const Grindstone::Renderer::MetaRect& desc) const noexcept {
288 size_t result =
289 std::hash<Grindstone::Renderer::MetaSize2D>{}(desc.offset) ^
290 std::hash<Grindstone::Renderer::MetaSize2D>{}(desc.extent);
291 }
292 };
293
294 template<>
295 struct hash<Grindstone::Renderer::ImageDescription> {
296 std::size_t operator()(const Grindstone::Renderer::ImageDescription& desc) const noexcept {
297 size_t result = std::hash<Grindstone::Renderer::MetaSize2D>{}(desc.size);
298
299 result ^= std::hash<size_t>{}(
300 static_cast<size_t>(desc.samples) |
301 static_cast<size_t>(desc.mipLevels) << 32
302 );
303
304 result ^= std::hash<size_t>{}(
305 static_cast<size_t>(desc.depth) |
306 static_cast<size_t>(desc.arrayLayers) << 32
307 );
308
309 result ^= std::hash<size_t>{}(
310 static_cast<size_t>(desc.format) |
311 static_cast<size_t>(desc.imageDimensions) << 32
312 );
313
314 result ^= std::hash<size_t>{}(
315 static_cast<size_t>(desc.memoryUsage) |
316 static_cast<size_t>(desc.imageUsage.GetValueUnderlying()) << 32
317 );
318
319 return result;
320 }
321 };
322}
323
Definition Bitset.hpp:331
Definition Image.hpp:49
Definition Rect.hpp:33
Definition AttachmentInfo.hpp:258
Definition AttachmentInfo.hpp:226
Definition AttachmentInfo.hpp:131
Definition AttachmentInfo.hpp:100
Definition AttachmentInfo.hpp:16
Definition AttachmentInfo.hpp:194
Definition RenderGraphResourceRef.hpp:51
Definition Formats.hpp:48