GlistEngine
Loading...
Searching...
No Matches
gVKReflect.h
Go to the documentation of this file.
1/*
2 * gVKReflect.h
3 *
4 * Reads a compiled SPIR-V module back and reports what the shader expects from
5 * the pipeline: its vertex inputs, its push constant block and its descriptor
6 * bindings. gVKPipeline uses this instead of repeating those declarations in
7 * C++, so editing a shader under graphics/shaders is enough - the
8 * pipeline layout follows the shader on its own.
9 *
10 * This is a deliberately small reflector: it understands the subset of SPIR-V
11 * that glslc emits for the engine's 2D shaders (scalars, vectors, matrices,
12 * arrays, structs, images and samplers) rather than the whole specification.
13 * Anything it does not recognise is skipped with a warning instead of guessed
14 * at, so a shader using an unsupported construct fails loudly at pipeline
15 * creation rather than silently rendering wrong.
16 */
17
18#pragma once
19
20#ifndef CORE_GVKREFLECT_H
21#define CORE_GVKREFLECT_H
22
23#include "gVKContext.h"
24
25#ifdef GVK_VULKAN
26
27#include <cstddef>
28#include <cstdint>
29#include <string>
30#include <vector>
31
32// One descriptor a shader declares, in the shape vkCreateDescriptorSetLayout
33// wants. stages accumulates while several stages of the same pipeline are
34// reflected, so a sampler read by both the vertex and fragment shader ends up
35// with both bits set.
36struct gVKReflectedBinding {
37 uint32_t set = 0;
38 uint32_t binding = 0;
39 uint32_t count = 1;
40 VkDescriptorType type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
41 VkShaderStageFlags stages = 0;
42 // The name the shader gave the resource: the sampler's variable name, or the
43 // block's type name for a uniform buffer. Empty when the module carries no
44 // debug names, which is what glslc emits under -g0. gShader looks a texture up
45 // by this name, so a stripped module can still be drawn with - it just cannot
46 // be addressed by name.
47 std::string name;
48 // Byte footprint of a uniform or storage block, 0 for images and samplers.
49 uint32_t blocksize = 0;
50};
51
52// What a block member is made of, which is what a setter needs in order to turn
53// a packed glm value into the block's own layout. A glm::mat3 is nine floats
54// with no gaps; the same mat3 in a std140 block is three columns sixteen bytes
55// apart, so writing one over the other would corrupt two thirds of it.
56enum gVKMemberComponent {
57 GVK_MEMBER_UNKNOWN = 0,
58 GVK_MEMBER_FLOAT,
59 GVK_MEMBER_INT,
60 GVK_MEMBER_UINT,
61 GVK_MEMBER_BOOL,
62};
63
64// One addressable scalar, vector, matrix or array leaf inside a uniform block or
65// a push constant block, named the way the shader source names it. Nested
66// structs and arrays of structs are flattened with dots and indices, so
67// "lights[2].position" addresses exactly what it reads like.
68struct gVKReflectedMember {
69 std::string name;
70 uint32_t set = 0;
71 uint32_t binding = 0;
72 // Byte offset from the start of the block.
73 uint32_t offset = 0;
74 // Bytes the member occupies in the block, padding between matrix columns and
75 // array elements included.
76 uint32_t size = 0;
77 gVKMemberComponent component = GVK_MEMBER_UNKNOWN;
78 // Vector components, or rows of a matrix. 1 for a scalar.
79 uint32_t rows = 1;
80 // Matrix columns. 1 for scalars and vectors.
81 uint32_t columns = 1;
82 // Bytes between two matrix columns; 0 when the member is not a matrix.
83 uint32_t matrixstride = 0;
84 // Elements of an array, 1 when the member is not one.
85 uint32_t arraylength = 1;
86 // Bytes between two array elements; 0 when the member is not an array.
87 uint32_t arraystride = 0;
88 // Push constant members live in the push constant block rather than in a
89 // descriptor, so set and binding say nothing about them.
90 bool pushconstant = false;
91};
92
93// The merged interface of every stage of one pipeline. Reflect the vertex and
94// the fragment module into the same instance to get the whole picture: vertex
95// inputs come from the vertex stage only, while push constants and descriptors
96// are unioned across stages.
97struct gVKReflectedLayout {
98 // Which stages have been reflected into this layout so far.
99 VkShaderStageFlags stages = 0;
100 // Sorted by location, with offsets packed in that order (which is how the
101 // draw path lays its vertex structs out) and stride set to the total.
102 std::vector<VkVertexInputAttributeDescription> vertexattributes;
103 uint32_t vertexstride = 0;
104 // 0 when the pipeline uses no push constants.
105 uint32_t pushconstantsize = 0;
106 VkShaderStageFlags pushconstantstages = 0;
107 // Sorted by (set, binding).
108 std::vector<gVKReflectedBinding> bindings;
109 // Every named leaf of every uniform and push constant block, in declaration
110 // order. Empty when the modules were compiled without debug names.
111 std::vector<gVKReflectedMember> members;
112
113 // The member with this exact name, or nullptr. Names are the shader's own.
114 const gVKReflectedMember* findMember(const std::string& name) const;
115 // The member an application-facing name addresses, which is not always one the
116 // reflector stored: "kernel[3]" names an element of a member the shader
117 // declared once, so the element is resolved into `out` with its own offset.
118 // Names without an index resolve to a copy of the stored member. Returns false
119 // when the shader declares nothing by that name.
120 //
121 // Everything that answers a question about a name has to come through here,
122 // including the "does this shader have it" test - a name resolvable by one and
123 // not the other means the setter is never reached.
124 bool resolveMember(const std::string& name, gVKReflectedMember& out) const;
125 // The binding with this exact name, or nullptr.
126 const gVKReflectedBinding* findBinding(const std::string& name) const;
127
128 // Highest descriptor set index used, plus one; 0 when there are none.
129 uint32_t getSetCount() const;
130 // The bindings belonging to one set, in binding order.
131 std::vector<VkDescriptorSetLayoutBinding> getSetBindings(uint32_t set) const;
132};
133
134// Merges one SPIR-V module's interface into layout. Returns false when the
135// module is not valid SPIR-V or uses something this reflector cannot describe.
136bool gvkReflectSpirv(const uint32_t* spirv, size_t sizeBytes, gVKReflectedLayout& layout);
137
138#endif /* GVK_VULKAN */
139
140#endif /* CORE_GVKREFLECT_H */
void set(float r, float g, float b, float a=1.0f)