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