Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Formats.hpp
1#pragma once
2
3#include <vector>
4#include <utility>
5#include <stdint.h>
6
7#include <Common/EnumTraits.hpp>
8
9namespace Grindstone::GraphicsAPI {
14 union ClearColor {
15 float float32[4];
16 int32_t int32[4];
17 uint32_t uint32[4];
18
19 ClearColor() : float32{ 0.0f, 0.0f, 0.0f, 0.0f } {};
20 ClearColor(float* v) : float32{ v[0], v[1], v[2], v[3] } {};
21 ClearColor(int32_t* v) : int32{ v[0], v[1], v[2], v[3] } {};
22 ClearColor(uint32_t* v) : uint32{ v[0], v[1], v[2], v[3] } {};
23
24 ClearColor(float r, float g, float b, float a) : float32{ r, g, b, a } {};
25 ClearColor(int32_t r, int32_t g, int32_t b, int32_t a) : int32{ r, g, b, a } {};
26 ClearColor(uint32_t r, uint32_t g, uint32_t b, uint32_t a) : uint32{ r, g, b, a } {};
27
28 ClearColor(const ClearColor& other) : uint32{ other.uint32[0], other.uint32[1] , other.uint32[2] , other.uint32[3] } {};
29
30 ClearColor& operator=(const ClearColor& other) {
31 float32[0] = other.float32[0];
32 float32[1] = other.float32[1];
33 float32[2] = other.float32[2];
34 float32[3] = other.float32[3];
35 return *this;
36 };
37
38 ~ClearColor() = default;
39 };
40
41 // Clear values for the DepthStencilImage.
43 float depth = 0.f;
44 uint32_t stencil = 0;
45 };
46
47 // A union of ClearColor and ClearDepthStencil.
48 union ClearUnion {
49 ClearColor color;
51
52 ClearUnion() : color() {}
53 ClearUnion(ClearColor value) : color(value) {}
54 ClearUnion(ClearDepthStencil value) : depth(value) {}
55
56 ClearUnion(const ClearUnion& other) : color(other.color) {}
57
58 ClearUnion& operator=(const ClearUnion& other) {
59 color = other.color;
60 return *this;
61 };
62 };
63
64 enum class LoadOp {
65 Load = 0,
66 Clear,
67 DontCare
68 };
69
70 enum class StoreOp {
71 Store = 0,
72 DontCare
73 };
74
75 enum class TextureWrapMode : uint8_t {
76 Repeat = 0,
77 ClampToEdge,
78 ClampToBorder,
79 MirroredRepeat,
80 MirroredClampToEdge
81 };
82
83 enum class TextureFilter : uint8_t {
84 Nearest = 0,
85 Linear
86 };
87
88 enum class ClearMode : uint8_t {
89 Color = 1 << 0,
90 Depth = 1 << 1,
91 Stencil = 1 << 2,
92
93 ColorAndDepth = Color | Depth,
94 All = ColorAndDepth | Stencil
95 };
96
97 enum class ImageLayout : uint8_t {
98 Undefined, // For newly created or discarded images
99 General, // For compute or unordered read/write
100 ColorAttachment, // Render target output
101 DepthWrite, // Depth write access
102 DepthRead, // Depth sampled (read-only)
103 ShaderRead, // Sampled image (color or depth)
104 TransferSrc, // Used as a copy/blit source
105 TransferDst, // Used as a copy/blit destination
106 Present, // Presented to screen
107 };
108
109 enum class MemoryUsage {
110 GPUOnly, // Device-local VRAM, inaccessible to CPU.
111 CPUToGPU, // Host-visible upload buffer (CPU writes, GPU reads)
112 GPUToCPU, // Host-visible readback buffer (GPU writes, CPU reads)
113 CPUOnly, // Host-visible, used as staging/upload/readback
114 Transient, // Transient GPU memory (tile memory / renderpass-only)
115 };
116
117 enum class ImageDimension {
118 Invalid,
119 Dimension1D,
120 Dimension2D,
121 Dimension3D
122 };
123
124 enum class ImageAspectBits : uint16_t {
125 None = 0,
126 Color = 0x00000001,
127 Depth = 0x00000002,
128 Stencil = 0x00000004,
129 Metadata = 0x00000008,
130 Plane0 = 0x00000010,
131 Plane1 = 0x00000020,
132 Plane2 = 0x00000040,
133 MemoryPlane0 = 0x00000080,
134 MemoryPlane1 = 0x00000100,
135 MemoryPlane2 = 0x00000200,
136 MemoryPlane3 = 0x00000400,
137 };
138
139 enum class PipelineStageBit : uint32_t {
140 None = 0,
141 TopOfPipe = 0x00000001,
142 DrawIndirect = 0x00000002,
143 VertexInput = 0x00000004,
144 VertexShader = 0x00000008,
145 TesslationControlShader = 0x00000010,
146 TesselationEvaluationShader = 0x00000020,
147 GeometryShader = 0x00000040,
148 FragmentShader = 0x00000080,
149 EarlyFragmentTests = 0x00000100,
150 LateFragmentTests = 0x00000200,
151 ColorAttachmentOutput = 0x00000400,
152 ComputeShader = 0x00000800,
153 Transfer = 0x00001000,
154 BottomOfPipe = 0x00002000,
155 Host = 0x00004000,
156 AllGraphics = 0x00008000,
157 AllCommands = 0x00010000,
158 TransformFeedback = 0x01000000,
159 ConditionalRendering = 0x00040000,
160 AccelerationStructureBuild = 0x02000000,
161 RayTracingShader = 0x00200000,
162 FragmentDensityProcess = 0x00800000,
163 FragmentShaderRateAttachment = 0x00400000,
164 CommandPreprocess = 0x00020000,
165 TaskShader = 0x00080000,
166 MeshShader = 0x00100000,
167 };
168
169 enum class Format {
170 Invalid,
171 R4G4_UNORM_PACK8,
172 R4G4B4A4_UNORM_PACK16,
173 B4G4R4A4_UNORM_PACK16,
174 R5G6B5_UNORM_PACK16,
175 B5G6R5_UNORM_PACK16,
176 R5G5B5A1_UNORM_PACK16,
177 B5G5R5A1_UNORM_PACK16,
178 A1R5G5B5_UNORM_PACK16,
179 R8_UNORM,
180 R8_SNORM,
181 R8_USCALED,
182 R8_SSCALED,
183 R8_UINT,
184 R8_SINT,
185 R8_SRGB,
186 R8G8_UNORM,
187 R8G8_SNORM,
188 R8G8_USCALED,
189 R8G8_SSCALED,
190 R8G8_UINT,
191 R8G8_SINT,
192 R8G8_SRGB,
193 R8G8B8_UNORM,
194 R8G8B8_SNORM,
195 R8G8B8_USCALED,
196 R8G8B8_SSCALED,
197 R8G8B8_UINT,
198 R8G8B8_SINT,
199 R8G8B8_SRGB,
200 B8G8R8_UNORM,
201 B8G8R8_SNORM,
202 B8G8R8_USCALED,
203 B8G8R8_SSCALED,
204 B8G8R8_UINT,
205 B8G8R8_SINT,
206 B8G8R8_SRGB,
207 R8G8B8A8_UNORM,
208 R8G8B8A8_SNORM,
209 R8G8B8A8_USCALED,
210 R8G8B8A8_SSCALED,
211 R8G8B8A8_UINT,
212 R8G8B8A8_SINT,
213 R8G8B8A8_SRGB,
214 B8G8R8A8_UNORM,
215 B8G8R8A8_SNORM,
216 B8G8R8A8_USCALED,
217 B8G8R8A8_SSCALED,
218 B8G8R8A8_UINT,
219 B8G8R8A8_SINT,
220 B8G8R8A8_SRGB,
221 A8B8G8R8_UNORM_PACK32,
222 A8B8G8R8_SNORM_PACK32,
223 A8B8G8R8_USCALED_PACK32,
224 A8B8G8R8_SSCALED_PACK32,
225 A8B8G8R8_UINT_PACK32,
226 A8B8G8R8_SINT_PACK32,
227 A8B8G8R8_SRGB_PACK32,
228 A2R10G10B10_UNORM_PACK32,
229 A2R10G10B10_SNORM_PACK32,
230 A2R10G10B10_USCALED_PACK32,
231 A2R10G10B10_SSCALED_PACK32,
232 A2R10G10B10_UINT_PACK32,
233 A2R10G10B10_SINT_PACK32,
234 A2B10G10R10_UNORM_PACK32,
235 A2B10G10R10_SNORM_PACK32,
236 A2B10G10R10_USCALED_PACK32,
237 A2B10G10R10_SSCALED_PACK32,
238 A2B10G10R10_UINT_PACK32,
239 A2B10G10R10_SINT_PACK32,
240 R16_UNORM,
241 R16_SNORM,
242 R16_USCALED,
243 R16_SSCALED,
244 R16_UINT,
245 R16_SINT,
246 R16_SFLOAT,
247 R16G16_UNORM,
248 R16G16_SNORM,
249 R16G16_USCALED,
250 R16G16_SSCALED,
251 R16G16_UINT,
252 R16G16_SINT,
253 R16G16_SFLOAT,
254 R16G16B16_UNORM,
255 R16G16B16_SNORM,
256 R16G16B16_USCALED,
257 R16G16B16_SSCALED,
258 R16G16B16_UINT,
259 R16G16B16_SINT,
260 R16G16B16_SFLOAT,
261 R16G16B16A16_UNORM,
262 R16G16B16A16_SNORM,
263 R16G16B16A16_USCALED,
264 R16G16B16A16_SSCALED,
265 R16G16B16A16_UINT,
266 R16G16B16A16_SINT,
267 R16G16B16A16_SFLOAT,
268 R32_UINT,
269 R32_SINT,
270 R32_SFLOAT,
271 R32G32_UINT,
272 R32G32_SINT,
273 R32G32_SFLOAT,
274 R32G32B32_UINT,
275 R32G32B32_SINT,
276 R32G32B32_SFLOAT,
277 R32G32B32A32_UINT,
278 R32G32B32A32_SINT,
279 R32G32B32A32_SFLOAT,
280 R64_UINT,
281 R64_SINT,
282 R64_SFLOAT,
283 R64G64_UINT,
284 R64G64_SINT,
285 R64G64_SFLOAT,
286 R64G64B64_UINT,
287 R64G64B64_SINT,
288 R64G64B64_SFLOAT,
289 R64G64B64A64_UINT,
290 R64G64B64A64_SINT,
291 R64G64B64A64_SFLOAT,
292 B10G11R11_UFLOAT_PACK32,
293 E5B9G9R9_UFLOAT_PACK32,
294
295 D16_UNORM,
296 X8_D24_UNORM_PACK32,
297 D32_SFLOAT,
298 S8_UINT,
299 D16_UNORM_S8_UINT,
300 D24_UNORM_S8_UINT,
301 D32_SFLOAT_S8_UINT,
302
303 BC1_RGB_UNORM_BLOCK,
304 BC1_RGB_SRGB_BLOCK,
305 BC1_RGBA_UNORM_BLOCK,
306 BC1_RGBA_SRGB_BLOCK,
307 BC2_UNORM_BLOCK,
308 BC2_SRGB_BLOCK,
309 BC3_UNORM_BLOCK,
310 BC3_SRGB_BLOCK,
311 BC4_UNORM_BLOCK,
312 BC4_SNORM_BLOCK,
313 BC5_UNORM_BLOCK,
314 BC5_SNORM_BLOCK,
315 BC6H_UFLOAT_BLOCK,
316 BC6H_SFLOAT_BLOCK,
317 BC7_UNORM_BLOCK,
318 BC7_SRGB_BLOCK,
319 ETC2_R8G8B8_UNORM_BLOCK,
320 ETC2_R8G8B8_SRGB_BLOCK,
321 ETC2_R8G8B8A1_UNORM_BLOCK,
322 ETC2_R8G8B8A1_SRGB_BLOCK,
323 ETC2_R8G8B8A8_UNORM_BLOCK,
324 ETC2_R8G8B8A8_SRGB_BLOCK,
325 EAC_R11_UNORM_BLOCK,
326 EAC_R11_SNORM_BLOCK,
327 EAC_R11G11_UNORM_BLOCK,
328 EAC_R11G11_SNORM_BLOCK,
329 ASTC_4x4_UNORM_BLOCK,
330 ASTC_4x4_SRGB_BLOCK,
331 ASTC_5x4_UNORM_BLOCK,
332 ASTC_5x4_SRGB_BLOCK,
333 ASTC_5x5_UNORM_BLOCK,
334 ASTC_5x5_SRGB_BLOCK,
335 ASTC_6x5_UNORM_BLOCK,
336 ASTC_6x5_SRGB_BLOCK,
337 ASTC_6x6_UNORM_BLOCK,
338 ASTC_6x6_SRGB_BLOCK,
339 ASTC_8x5_UNORM_BLOCK,
340 ASTC_8x5_SRGB_BLOCK,
341 ASTC_8x6_UNORM_BLOCK,
342 ASTC_8x6_SRGB_BLOCK,
343 ASTC_8x8_UNORM_BLOCK,
344 ASTC_8x8_SRGB_BLOCK,
345 ASTC_10x5_UNORM_BLOCK,
346 ASTC_10x5_SRGB_BLOCK,
347 ASTC_10x6_UNORM_BLOCK,
348 ASTC_10x6_SRGB_BLOCK,
349 ASTC_10x8_UNORM_BLOCK,
350 ASTC_10x8_SRGB_BLOCK,
351 ASTC_10x10_UNORM_BLOCK,
352 ASTC_10x10_SRGB_BLOCK,
353 ASTC_12x10_UNORM_BLOCK,
354 ASTC_12x10_SRGB_BLOCK,
355 ASTC_12x12_UNORM_BLOCK,
356 ASTC_12x12_SRGB_BLOCK,
357 G8B8G8R8_422_UNORM,
358 B8G8R8G8_422_UNORM,
359 G8_B8_R8_3PLANE_420_UNORM,
360 G8_B8R8_2PLANE_420_UNORM,
361 G8_B8_R8_3PLANE_422_UNORM,
362 G8_B8R8_2PLANE_422_UNORM,
363 G8_B8_R8_3PLANE_444_UNORM,
364 R10X6_UNORM_PACK16,
365 R10X6G10X6_UNORM_2PACK16,
366 R10X6G10X6B10X6A10X6_UNORM_4PACK16,
367 G10X6B10X6G10X6R10X6_422_UNORM_4PACK16,
368 B10X6G10X6R10X6G10X6_422_UNORM_4PACK16,
369 G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16,
370 G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16,
371 G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16,
372 G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16,
373 G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16,
374 R12X4_UNORM_PACK16,
375 R12X4G12X4_UNORM_2PACK16,
376 R12X4G12X4B12X4A12X4_UNORM_4PACK16,
377 G12X4B12X4G12X4R12X4_422_UNORM_4PACK16,
378 B12X4G12X4R12X4G12X4_422_UNORM_4PACK16,
379 G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16,
380 G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16,
381 G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16,
382 G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16,
383 G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16,
384 G16B16G16R16_422_UNORM,
385 B16G16R16G16_422_UNORM,
386 G16_B16_R16_3PLANE_420_UNORM,
387 G16_B16R16_2PLANE_420_UNORM,
388 G16_B16_R16_3PLANE_422_UNORM,
389 G16_B16R16_2PLANE_422_UNORM,
390 G16_B16_R16_3PLANE_444_UNORM,
391 G8_B8R8_2PLANE_444_UNORM,
392 G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16,
393 G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16,
394 G16_B16R16_2PLANE_444_UNORM,
395 A4R4G4B4_UNORM_PACK16,
396 A4B4G4R4_UNORM_PACK16,
397 ASTC_4x4_SFLOAT,
398 ASTC_5x4_SFLOAT,
399 ASTC_5x5_SFLOAT,
400 ASTC_6x5_SFLOAT,
401 ASTC_6x6_SFLOAT,
402 ASTC_8x5_SFLOAT,
403 ASTC_8x6_SFLOAT,
404 ASTC_8x8_SFLOAT,
405 ASTC_10x5_SFLOAT,
406 ASTC_10x6_SFLOAT,
407 ASTC_10x8_SFLOAT,
408 ASTC_10x10_SFLOAT,
409 ASTC_12x10_SFLOAT,
410 ASTC_12x12_SFLOAT,
411 PVRTC1_2BPP_UNORM,
412 PVRTC1_4BPP_UNORM,
413 PVRTC2_2BPP_UNORM,
414 PVRTC2_4BPP_UNORM,
415 PVRTC1_2BPP_SRGB,
416 PVRTC1_4BPP_SRGB,
417 PVRTC2_2BPP_SRGB,
418 PVRTC2_4BPP_SRGB,
419
420 // Limited Support:
421 R16G16_S10_5,
422 A1B5G5R5_UNORM_PACK16,
423 A8_UNORM,
424 };
425
426 enum class FormatDepthStencilType : uint8_t {
427 NotDepthStencil = 0,
428 Depth = 1 << 0,
429 Stencil = 1 << 1,
430 DepthStencil = static_cast<uint8_t>(FormatDepthStencilType::Depth) | static_cast<uint8_t>(FormatDepthStencilType::Stencil)
431 };
432
433 ImageAspectBits GetFormatAspect(Format format);
434 FormatDepthStencilType GetFormatDepthStencilType(Format format);
435 bool IsFormatCompressed(Format format);
436 uint8_t GetCompressedFormatBlockSize(Format format);
437 uint8_t GetFormatBytesPerPixel(Grindstone::GraphicsAPI::Format format);
438
439 #define SHADER_STAGE_TYPES \
440 GSExpandEntry(Vertex, 1 << 0),\
441 GSExpandEntry(TesselationEvaluation, 1 << 1),\
442 GSExpandEntry(TesselationControl, 1 << 2),\
443 GSExpandEntry(Geometry, 1 << 3),\
444 GSExpandEntry(Fragment, 1 << 4),\
445 GSExpandEntry(Task, 1 << 5),\
446 GSExpandEntry(Mesh, 1 << 6),\
447 GSExpandEntry(Compute, 1 << 7)
448
449 enum class ShaderStage : uint8_t {
450 #define GSExpandEntry(key, bit) key
451 SHADER_STAGE_TYPES,
452 #undef GSExpandEntry
453 GraphicsCount = Compute,
454 Count
455 };
456
457 constexpr uint8_t numShaderGraphicStage = static_cast<uint8_t>(ShaderStage::GraphicsCount);
458 constexpr uint8_t numShaderTotalStage = static_cast<uint8_t>(ShaderStage::Count);
459
460 enum class ShaderStageBit : uint8_t {
461 None = 0,
462#define GSExpandEntry(key, bit) key = bit
463 SHADER_STAGE_TYPES,
464#undef GSExpandEntry
465 Count = 8,
466 AllGraphics = Vertex | TesselationEvaluation | TesselationControl | Geometry | Fragment | Task | Mesh,
467 All = AllGraphics | Compute
468 };
469
470 constexpr const char* shaderStageNames[] = {
471 #define GSExpandEntry(key, bit) #key
472 SHADER_STAGE_TYPES
473 #undef GSExpandEntry
474 };
475
476 inline const char* GetShaderStageName(Grindstone::GraphicsAPI::ShaderStage stage) {
477 uint8_t index = static_cast<uint8_t>(stage);
478 if (index >= static_cast<uint8_t>(ShaderStage::Count)) {
479 return "Invalid";
480 }
481
482 return shaderStageNames[index];
483 }
484
485 enum class BindingType {
486 None,
487 Sampler,
488 CombinedImageSampler,
489 SampledImage,
490 StorageImage,
491 UniformTexelBuffer,
492 StorageTexelBuffer,
493 UniformBuffer,
494 StorageBuffer,
495 UniformBufferDynamic,
496 StorageBufferDynamic,
497 AccelerationStructure
498 };
499
500#define BLEND_OPERATIONS_LIST \
501 GSExpandEntry(None),\
502 GSExpandEntry(Add),\
503 GSExpandEntry(Subtract),\
504 GSExpandEntry(ReverseSubtract),\
505 GSExpandEntry(Minimum),\
506 GSExpandEntry(Maximum),\
507 GSExpandEntry(Zero),\
508 GSExpandEntry(Source),\
509 GSExpandEntry(Destination),\
510 GSExpandEntry(SourceOver),\
511 GSExpandEntry(DestinationOver),\
512 GSExpandEntry(SourceIn),\
513 GSExpandEntry(DestinationIn),\
514 GSExpandEntry(SourceOut),\
515 GSExpandEntry(DestinationOut),\
516 GSExpandEntry(SourceAtop),\
517 GSExpandEntry(DestinationAtop),\
518 GSExpandEntry(XOR),\
519 GSExpandEntry(Multiply),\
520 GSExpandEntry(Screen),\
521 GSExpandEntry(Overlay),\
522 GSExpandEntry(Darken),\
523 GSExpandEntry(Lighten),\
524 GSExpandEntry(ColorDodge),\
525 GSExpandEntry(ColorBurn),\
526 GSExpandEntry(HardLight),\
527 GSExpandEntry(SoftLight),\
528 GSExpandEntry(Difference),\
529 GSExpandEntry(Exclusion),\
530 GSExpandEntry(Invert),\
531 GSExpandEntry(InvertRGB),\
532 GSExpandEntry(LinearDodge),\
533 GSExpandEntry(LinearBurn),\
534 GSExpandEntry(VividLight),\
535 GSExpandEntry(LinearLight),\
536 GSExpandEntry(PinLight),\
537 GSExpandEntry(HardMix),\
538 GSExpandEntry(HSLHue),\
539 GSExpandEntry(HSLSaturation),\
540 GSExpandEntry(HSLColor),\
541 GSExpandEntry(HSLLuminosity),\
542 GSExpandEntry(Plus),\
543 GSExpandEntry(PlusClamped),\
544 GSExpandEntry(PlusClampedAlpha),\
545 GSExpandEntry(PlusDark),\
546 GSExpandEntry(Minus),\
547 GSExpandEntry(MinusClamped),\
548 GSExpandEntry(Contrast),\
549 GSExpandEntry(InvertOVG),\
550 GSExpandEntry(Red),\
551 GSExpandEntry(Green),\
552 GSExpandEntry(Blue)
553
554 enum class BlendOperation : uint8_t {
555#define GSExpandEntry(key) key
556 BLEND_OPERATIONS_LIST,
557#undef GSExpandEntry
558 Count
559 };
560
561 constexpr const char* blendOperationNames[] = {
562 #define GSExpandEntry(key) #key
563 BLEND_OPERATIONS_LIST
564 #undef GSExpandEntry
565 };
566
567 inline const char* GetBlendOperationName(Grindstone::GraphicsAPI::BlendOperation op) {
568 uint8_t index = static_cast<uint8_t>(op);
569 if (index >= static_cast<uint8_t>(BlendOperation::Count)) {
570 return "Invalid";
571 }
572
573 return blendOperationNames[index];
574 }
575
576
577#define BLEND_FACTORS_LIST \
578 GSExpandEntry(Zero),\
579 GSExpandEntry(One),\
580 GSExpandEntry(SrcColor),\
581 GSExpandEntry(OneMinusSrcColor),\
582 GSExpandEntry(DstColor),\
583 GSExpandEntry(OneMinusDstColor),\
584 GSExpandEntry(SrcAlpha),\
585 GSExpandEntry(OneMinusSrcAlpha),\
586 GSExpandEntry(DstAlpha),\
587 GSExpandEntry(OneMinusDstAlpha),\
588 GSExpandEntry(ConstantColor),\
589 GSExpandEntry(OneMinusConstantColor),\
590 GSExpandEntry(ConstantAlpha),\
591 GSExpandEntry(OneMinusConstantAlpha),\
592 GSExpandEntry(SrcAlphaSaturate),\
593 GSExpandEntry(Src1Color),\
594 GSExpandEntry(OneMinusSrc1Color),\
595 GSExpandEntry(Src1Alpha),\
596 GSExpandEntry(OneMinusSrc1Alpha)
597
598 enum class BlendFactor : uint8_t {
599 #define GSExpandEntry(key) key
600 BLEND_FACTORS_LIST,
601 #undef GSExpandEntry
602 Count
603 };
604
605 constexpr const char* blendFactorNames[] = {
606 #define GSExpandEntry(key) #key
607 BLEND_FACTORS_LIST
608 #undef GSExpandEntry
609 };
610
611 inline const char* GetBlendFactorName(Grindstone::GraphicsAPI::BlendFactor factor) {
612 uint8_t index = static_cast<uint8_t>(factor);
613 if (index >= static_cast<uint8_t>(BlendFactor::Count)) {
614 return "Invalid";
615 }
616
617 return blendFactorNames[index];
618 }
619
620 struct BlendData {
621 BlendOperation colorOperation = BlendOperation::None;
622 BlendFactor colorFactorSrc = BlendFactor::One;
623 BlendFactor colorFactorDst = BlendFactor::One;
624
625 BlendOperation alphaOperation = BlendOperation::None;
626 BlendFactor alphaFactorSrc = BlendFactor::One;
627 BlendFactor alphaFactorDst = BlendFactor::One;
628
629 bool operator==(const BlendData& o) const {
630 return colorOperation == o.colorOperation
631 && colorFactorSrc == o.colorFactorSrc
632 && colorFactorDst == o.colorFactorDst
633 && alphaOperation == o.alphaOperation
634 && alphaFactorSrc == o.alphaFactorSrc
635 && alphaFactorDst == o.alphaFactorDst;
636 }
637
638 bool operator!=(const BlendData& o) const {
639 return !(*this == o);
640 }
641
642 static BlendData NoBlending() {
643 return BlendData{
644 BlendOperation::None,
645 BlendFactor::One,
646 BlendFactor::One,
647
648 BlendOperation::None,
649 BlendFactor::One,
650 BlendFactor::One
651 };
652 };
653
654 static BlendData Additive() {
655 return BlendData{
656 BlendOperation::Add,
657 BlendFactor::One,
658 BlendFactor::One,
659
660 BlendOperation::Add,
661 BlendFactor::One,
662 BlendFactor::One
663 };
664 };
665
666 static BlendData AdditiveAlpha() {
667 return BlendData{
668 BlendOperation::Add,
669 BlendFactor::SrcAlpha,
670 BlendFactor::OneMinusSrcAlpha,
671
672 BlendOperation::Add,
673 BlendFactor::One,
674 BlendFactor::OneMinusSrcAlpha
675 };
676 };
677 };
678
679#define GEOMETRY_TYPES_LIST \
680 GSExpandEntry(Points),\
681 GSExpandEntry(Lines),\
682 GSExpandEntry(LineStrips),\
683 GSExpandEntry(LineLoops),\
684 GSExpandEntry(TriangleStrips),\
685 GSExpandEntry(TriangleFans),\
686 GSExpandEntry(Triangles),\
687 GSExpandEntry(LinesAdjacency),\
688 GSExpandEntry(TrianglesAdjacency),\
689 GSExpandEntry(TriangleStripsAdjacency),\
690 GSExpandEntry(Patches)
691
692 enum class GeometryType : uint8_t {
693 #define GSExpandEntry(key) key
694 GEOMETRY_TYPES_LIST,
695 #undef GSExpandEntry
696 Count
697 };
698
699 constexpr const char* geometryTypeNames[] = {
700 #define GSExpandEntry(key) #key
701 GEOMETRY_TYPES_LIST
702 #undef GSExpandEntry
703 };
704
705 inline const char* GetGeometryTypeName(Grindstone::GraphicsAPI::GeometryType stage) {
706 uint8_t index = static_cast<uint8_t>(stage);
707 if (index >= static_cast<uint8_t>(GeometryType::Count)) {
708 return "Invalid";
709 }
710
711 return geometryTypeNames[index];
712 }
713
714 enum class PolygonFillMode : uint8_t {
715 Point,
716 Line,
717 Fill
718 };
719
720 constexpr const char* polygonFillModeNames[] = {
721 "Point",
722 "Line",
723 "Fill"
724 };
725
726 inline const char* GetPolygonFillModeName(Grindstone::GraphicsAPI::PolygonFillMode mode) {
727 uint8_t index = static_cast<uint8_t>(mode);
728 if (index > static_cast<uint8_t>(PolygonFillMode::Fill)) {
729 return "Invalid";
730 }
731
732 return polygonFillModeNames[index];
733 }
734
735 enum class CompareOperation : uint8_t {
736 Never,
737 Less,
738 Equal,
739 LessOrEqual,
740 Greater,
741 NotEqual,
742 GreaterOrEqual,
743 Always
744 };
745
746 constexpr const char* compareOperationNames[] = {
747 #define GSExpandEntry(key, bit) #key
748 SHADER_STAGE_TYPES
749 #undef GSExpandEntry
750 };
751
752 inline const char* GetCompareOperationName(Grindstone::GraphicsAPI::CompareOperation op) {
753 uint8_t index = static_cast<uint8_t>(op);
754 if (index > static_cast<uint8_t>(CompareOperation::Always)) {
755 return "Invalid";
756 }
757
758 return compareOperationNames[index];
759 }
760
761 enum class ColorMask : uint8_t {
762 None = 0,
763 Red = 0x1,
764 Green = 0x2,
765 Blue = 0x4,
766 Alpha = 0x8,
767
768 RG = Red | Green,
769 RB = Red | Blue,
770 RA = Red | Alpha,
771 GB = Green | Blue,
772 GA = Green | Alpha,
773 BA = Blue | Alpha,
774
775 RGB = Red | Green | Blue,
776 RGA = Red | Green | Alpha,
777 RBA = Red | Blue | Alpha,
778 GBA = Green | Blue | Alpha,
779
780 RGBA = Red | Green | Blue | Alpha
781 };
782
783 constexpr const char* colorMaskNames[] = {
784 "None",
785 "R",
786 "G",
787 "RG",
788 "B",
789 "RB",
790 "GB",
791 "RGB",
792 "A",
793 "RA",
794 "GA",
795 "RGA",
796 "BA",
797 "RBA",
798 "GBA",
799 "RGBA"
800 };
801
802 inline const char* GetColorMaskName(Grindstone::GraphicsAPI::ColorMask colorMask) {
803 uint8_t index = static_cast<uint8_t>(colorMask);
804 if (index > static_cast<uint8_t>(ColorMask::RGBA)) {
805 return "Invalid";
806 }
807
808 return colorMaskNames[index];
809 }
810
811 enum class CullMode : uint8_t {
812 None = 0,
813 Front,
814 Back,
815 Both
816 };
817
818 constexpr const char* cullModeNames[] = {
819 "None",
820 "Front",
821 "Back",
822 "Both"
823 };
824
825 inline const char* GetCullModeName(Grindstone::GraphicsAPI::CullMode cullMode) {
826 uint8_t index = static_cast<uint8_t>(cullMode);
827 if (index > static_cast<uint8_t>(CullMode::Both)) {
828 return "Invalid";
829 }
830
831 return cullModeNames[index];
832 }
833
834 // This refers to semantic information about how some vertex data will be used.
835 enum class AttributeUsage {
836 Position,
837 Color,
838 TexCoord0,
839 TexCoord1,
840 TexCoord2,
841 TexCoord3,
842 Normal,
843 Tangent,
844 BlendWeights,
845 BlendIndices,
846 Other
847 };
848
849 // This refers to how the buffer should be indexed.
850 enum class VertexInputRate {
851 Vertex,
852 Instance
853 };
854
855 // A structure to define a particular kind of data in a Vertex Buffer.
857 const char* name = nullptr;
858 uint32_t bindingIndex;
859 uint32_t locationIndex = 0;
860 Format format = Format::R32_SFLOAT;
861 uint32_t byteOffset = 0;
862 AttributeUsage attributeUsage = AttributeUsage::Other;
863
864 bool operator==(const VertexAttributeDescription& o) const {
865 return bindingIndex == o.bindingIndex
866 && locationIndex == o.locationIndex
867 && format == o.format
868 && byteOffset == o.byteOffset
869 && attributeUsage == o.attributeUsage;
870 }
871
872 bool operator!=(const VertexAttributeDescription& o) const {
873 return !(*this == o);
874 }
875 };
876
877 // A structure that dictates how the Vertex Buffer data is formatted.
879 uint32_t bindingIndex;
880 uint32_t stride = 0;
881 VertexInputRate inputRate = VertexInputRate::Vertex;
882
883 bool operator==(const VertexBindingDescription& o) const {
884 return bindingIndex == o.bindingIndex
885 && stride == o.stride
886 && inputRate == o.inputRate;
887 }
888
889 bool operator!=(const VertexBindingDescription& o) const {
890 return !(*this == o);
891 }
892 };
893
895 std::vector<VertexBindingDescription> bindings;
896 std::vector<VertexAttributeDescription> attributes;
897
898 bool operator==(const VertexInputLayout& o) const {
899 if (bindings != o.bindings) {
900 return false;
901 }
902
903 if (attributes != o.attributes) {
904 return false;
905 }
906
907 return true;
908 }
909
910 bool operator!=(const VertexInputLayout& o) const {
911 return !(*this == o);
912 }
913 };
914
915 struct VertexInputLayoutBuilder {
916 std::vector<VertexBindingDescription> bindings;
917 std::vector<VertexAttributeDescription> attributes;
918
919 VertexInputLayoutBuilder() = default;
920
922 const char* name;
923 uint32_t locationIndex;
924 Format format;
925 uint32_t byteOffset;
926 AttributeUsage attributeUsage;
927 };
928
929 VertexInputLayoutBuilder& AddBinding(
931 std::initializer_list<InlineAttribute> newAttributes
932 ) {
933 bindings.emplace_back(binding);
934 for (const InlineAttribute& attrib : newAttributes) {
935 attributes.emplace_back(VertexAttributeDescription{
936 attrib.name,
937 binding.bindingIndex,
938 attrib.locationIndex,
939 attrib.format,
940 attrib.byteOffset,
941 attrib.attributeUsage
942 });
943 }
944
945 return *this;
946 }
947
948 VertexInputLayoutBuilder& AddBinding(VertexBindingDescription binding) {
949 bindings.emplace_back(binding);
950 return *this;
951 }
952
953 VertexInputLayoutBuilder& AddAttribute(VertexAttributeDescription attribute) {
954 attributes.emplace_back(attribute);
955 return *this;
956 }
957
958 VertexInputLayout Build() {
959 return {
960 bindings,
961 attributes
962 };
963 }
964 };
965}
966
967inline Grindstone::GraphicsAPI::ShaderStageBit ToShaderStageBit(const Grindstone::GraphicsAPI::ShaderStage stage) {
968 using ShaderStageType = uint8_t;
969 return static_cast<Grindstone::GraphicsAPI::ShaderStageBit>(1 << static_cast<ShaderStageType>(stage));
970}
971
972GS_ENUM_FLAGS_FUNCS(Grindstone::GraphicsAPI::ClearMode)
973GS_ENUM_FLAGS_FUNCS(Grindstone::GraphicsAPI::ImageAspectBits)
974GS_ENUM_FLAGS_FUNCS(Grindstone::GraphicsAPI::PipelineStageBit)
975GS_ENUM_FLAGS_FUNCS(Grindstone::GraphicsAPI::FormatDepthStencilType)
976GS_ENUM_FLAGS_FUNCS(Grindstone::GraphicsAPI::ShaderStageBit)
977GS_ENUM_FLAGS_FUNCS(Grindstone::GraphicsAPI::ColorMask)
978
979namespace std {
980 template<>
981 struct std::hash<Grindstone::GraphicsAPI::VertexBindingDescription> {
982 std::size_t operator()(const Grindstone::GraphicsAPI::VertexBindingDescription& binding) const noexcept {
983 size_t result = std::hash<size_t>{}(
984 static_cast<size_t>(binding.bindingIndex) << 8 |
985 static_cast<size_t>(binding.stride) << 32
986 );
987 result ^= std::hash<size_t>{}(static_cast<size_t>(binding.inputRate));
988 return result;
989 }
990 };
991
992 template<>
993 struct std::hash<Grindstone::GraphicsAPI::VertexAttributeDescription> {
994 std::size_t operator()(const Grindstone::GraphicsAPI::VertexAttributeDescription& attribute) const noexcept {
995 size_t result = std::hash<size_t>{}(
996 static_cast<size_t>(attribute.attributeUsage) |
997 static_cast<size_t>(attribute.bindingIndex) << 32
998 );
999
1000 result ^= std::hash<size_t>{}(
1001 static_cast<size_t>(attribute.byteOffset) |
1002 static_cast<size_t>(attribute.format) << 32
1003 );
1004
1005 result ^= std::hash<size_t>{}(
1006 static_cast<size_t>(attribute.locationIndex) << 32
1007 );
1008
1009 return result;
1010 }
1011 };
1012
1013 template<>
1014 struct std::hash<Grindstone::GraphicsAPI::VertexInputLayout> {
1015 std::size_t operator()(const Grindstone::GraphicsAPI::VertexInputLayout& vertexInputLayout) const noexcept {
1016 size_t result = std::hash<size_t>{}(vertexInputLayout.attributes.size()) ^ std::hash<size_t>{}(vertexInputLayout.bindings.size());
1017
1018 for (const Grindstone::GraphicsAPI::VertexBindingDescription& binding : vertexInputLayout.bindings) {
1019 result ^= std::hash<Grindstone::GraphicsAPI::VertexBindingDescription>{}(binding);
1020 }
1021
1022 for (const Grindstone::GraphicsAPI::VertexAttributeDescription& attribute : vertexInputLayout.attributes) {
1023 result ^= std::hash<Grindstone::GraphicsAPI::VertexAttributeDescription>{}(attribute);
1024 }
1025
1026 return result;
1027 }
1028 };
1029}
Definition Sampler.hpp:49
Definition Formats.hpp:620
Definition Formats.hpp:14