MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ShaderUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "vulkan/vulkan.hpp"
4
5#include "GraphicsUtils.hpp"
6
8
9namespace MayaFlux::Core {
10struct VertexBinding;
11struct VertexAttribute;
12}
13
15
16/**
17 * @brief Resolve the extra vk::BufferUsageFlags a BufferUsageHint requires,
18 * on top of whatever base usage the caller already applies.
19 */
20[[nodiscard]] inline vk::BufferUsageFlags to_buffer_usage_flags(BufferUsageHint hint)
21{
22 switch (hint) {
24 return vk::BufferUsageFlagBits::eIndirectBuffer;
26 return vk::BufferUsageFlagBits::eStorageBuffer;
27 default:
28 break;
29 }
30
31 vk::BufferUsageFlags flags = vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst;
32
33 switch (hint) {
35 break;
38 flags |= vk::BufferUsageFlagBits::eStorageBuffer;
39 break;
41 flags |= vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eStorageBuffer;
42 break;
44 flags |= vk::BufferUsageFlagBits::eIndexBuffer;
45 break;
47 flags |= vk::BufferUsageFlagBits::eUniformBuffer;
48 break;
50 flags |= vk::BufferUsageFlagBits::eUniformBuffer
51 | vk::BufferUsageFlagBits::eShaderDeviceAddress;
52 break;
54 flags |= vk::BufferUsageFlagBits::eStorageBuffer
55 | vk::BufferUsageFlagBits::eShaderDeviceAddress;
56 break;
58 flags |= vk::BufferUsageFlagBits::eStorageBuffer;
59 break;
60 default:
61 break;
62 }
63
64 return flags;
65}
66
67using ShaderID = uint64_t;
68using DescriptorSetID = uint64_t;
69using CommandBufferID = uint64_t;
70using FenceID = uint64_t;
71using SemaphoreID = uint64_t;
72using RenderPipelineID = uint64_t;
73using FramebufferID = uint64_t;
74
78constexpr FenceID INVALID_FENCE = 0;
82
83/**
84 * @struct ShaderCompilerConfig
85 * @brief Configuration for shader compilation
86 */
89 bool enable_debug_info = false; ///< Include debug symbols (line numbers, variable names)
90 bool enable_reflection = true; ///< Extract descriptor bindings and metadata
91 bool enable_validation = true; ///< Validate SPIR-V after compilation
92 std::vector<std::string> include_directories; ///< Paths for #include resolution
93 std::unordered_map<std::string, std::string> defines; ///< Preprocessor macros
94
96};
97
98/**
99 * @struct GpuComputeConfig
100 * @brief Plain-data description of the compute shader to dispatch.
101 *
102 * Either shader_path or shader_id must be set. If shader_id is not
103 * INVALID_SHADER it takes precedence and shader_path is ignored,
104 * allowing callers who already hold a compiled ShaderID (e.g. from
105 * ShaderFoundry::load_shader(const ShaderSpec&)) to bypass file
106 * resolution entirely.
107 */
109 std::string shader_path;
110 std::array<uint32_t, 3> workgroup_size { 256, 1, 1 };
111 size_t push_constant_size { 0 };
113};
114
115/**
116 * @struct ShaderSource
117 * @brief Shader source descriptor for compilation
118 */
120 std::string content; ///< Shader source code, SPIR-V path, or SPIR-V assembly text
122 std::string entry_point = "main";
123
124 enum class SourceType : uint8_t {
125 GLSL_STRING, ///< In-memory GLSL source
126 GLSL_FILE, ///< Path to .comp/.vert/.frag/etc
127 SPIRV_FILE, ///< Path to .spv file
128 SPIRV_ASM, ///< In-memory SPIR-V assembly text (assembled via SPIRV-Tools, no shaderc)
130
131 ShaderSource() = default;
132 ShaderSource(std::string content_, ShaderStage stage_, SourceType type_)
133 : content(std::move(content_))
134 , stage(stage_)
135 , type(type_)
136 {
137 }
138};
139
140/**
141 * @struct PushConstantBindingInfo
142 * @brief Self-describing push constant fragment staged on a VKBuffer.
143 *
144 * The push constant analogue of DescriptorBindingInfo. Where a descriptor
145 * entry carries its own set and binding, a push constant entry carries its
146 * own byte offset, so a shared list on the buffer merges into any
147 * processor's own push constant block without a per-processor key.
148 */
150 uint32_t offset {};
151 std::vector<uint8_t> data;
152 std::string name;
153};
154
155/**
156 * @enum DescriptorRole
157 * @brief Semantic descriptor type — maps to Vulkan descriptor types internally.
158 *
159 * Users declare what role a binding plays rather than which Vulkan type to use.
160 * The mapping is:
161 * UNIFORM → vk::DescriptorType::eUniformBuffer (small, frequently updated values)
162 * STORAGE → vk::DescriptorType::eStorageBuffer (large or write-capable arrays)
163 * TEXTURE → vk::DescriptorType::eCombinedImageSampler (sampled image + sampler)
164 * STORAGE_IMAGE → vk::DescriptorType::eStorageImage (compute read/write image)
165 */
166enum class DescriptorRole : uint8_t {
167 UNIFORM, ///< Small, read-only, frequently updated (scalars, param structs)
168 STORAGE, ///< Large arrays or buffers the shader may write (SSBO)
169 TEXTURE, ///< Sampled image + sampler combined
170 STORAGE_IMAGE ///< Compute-writable image
171};
172
173/**
174 * @brief Convert DescriptorRole to the corresponding vk::DescriptorType.
175 */
176[[nodiscard]] inline vk::DescriptorType to_vk_descriptor_type(DescriptorRole role)
177{
178 switch (role) {
180 return vk::DescriptorType::eUniformBuffer;
182 return vk::DescriptorType::eStorageBuffer;
184 return vk::DescriptorType::eCombinedImageSampler;
186 return vk::DescriptorType::eStorageImage;
187 }
188 return vk::DescriptorType::eStorageBuffer;
189}
190
191/**
192 * @struct DescriptorBindingConfig
193 * @brief Portal-level descriptor binding configuration
194 */
196 uint32_t set { 1 };
197 uint32_t binding {};
198 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer;
199 vk::DescriptorBufferInfo buffer_info;
200 std::string name;
201 uint32_t count { 1 };
202};
203
204/**
205 * @struct PushConstantRangeInfo
206 * @brief Extracted push constant range from shader reflection
207 */
209 uint32_t offset;
210 uint32_t size;
211};
212
213/**
214 * @struct ShaderReflectionInfo
215 * @brief Extracted reflection information from compiled shader
216 */
219 std::string entry_point;
220 std::optional<std::array<uint32_t, 3>> workgroup_size;
221 std::vector<DescriptorBindingInfo> descriptor_bindings;
222 std::vector<PushConstantRangeInfo> push_constant_ranges;
223};
224
225/**
226 * @struct RasterizationConfig
227 * @brief Rasterization state configuration
228 */
239
240/**
241 * @struct DepthStencilConfig
242 * @brief Depth and stencil test configuration
243 */
252
253/**
254 * @struct BlendAttachmentConfig
255 * @brief Per-attachment blend configuration
256 */
280
281/**
282 * @struct RenderPipelineConfig
283 * @brief Complete render pipeline configuration
284 */
286 // Shader stages
294
295 // Vertex input
296 std::vector<Core::VertexBinding> vertex_bindings;
297 std::vector<Core::VertexAttribute> vertex_attributes;
298
299 // Input assembly
301
302 // Optional semantic vertex layout
303 std::optional<Kakshya::VertexLayout> semantic_vertex_layout;
304
305 // Use reflection to auto-configure from vertex shader
307
308 // Rasterization
310
311 // Depth/stencil
313
314 // Blend
315 std::vector<BlendAttachmentConfig> blend_attachments;
316
317 // Descriptor sets (similar to compute)
318 std::vector<std::vector<DescriptorBindingInfo>> descriptor_sets;
319
320 // Push constants
322
324};
325
326} // namespace MayaFlux::Portal::Graphics
PolygonMode
Rasterization polygon mode.
constexpr RenderPipelineID INVALID_RENDER_PIPELINE
constexpr ShaderID INVALID_SHADER
ShaderStage
User-friendly shader stage enum.
constexpr FenceID INVALID_FENCE
DescriptorRole
Semantic descriptor type — maps to Vulkan descriptor types internally.
@ STORAGE_IMAGE
Compute-writable image.
@ TEXTURE
Sampled image + sampler combined.
@ STORAGE
Large arrays or buffers the shader may write (SSBO)
@ UNIFORM
Small, read-only, frequently updated (scalars, param structs)
constexpr FramebufferID INVALID_FRAMEBUFFER
BlendOp
Blending operation.
BufferUsageHint
Semantic usage hint for buffer allocation and memory properties.
@ STORAGE_BDA
Storage buffer with device address query support.
@ HOST_STORAGE
Host-visible storage buffer (eStorageBuffer + eHostVisible|eHostCoherent)
@ INDIRECT
Indirect draw/dispatch buffer (device-local)
@ UNIFORM
Uniform buffer (host-visible)
@ COMPUTE
Storage buffer for compute shaders (device-local)
@ STAGING
Host-visible staging buffer (CPU-writable, eTransferSrc|Dst)
@ UNIFORM_BDA
Uniform buffer with device address query support.
@ COMPUTE_STORAGE
Bare eStorageBuffer, no transfer flags. GpuResourceManager's host-visible/host-coherent compute buffe...
@ DEVICE
Device-local GPU-only buffer.
PrimitiveTopology
Vertex assembly primitive topology.
vk::DescriptorType to_vk_descriptor_type(DescriptorRole role)
Convert DescriptorRole to the corresponding vk::DescriptorType.
CompareOp
Depth/stencil comparison operation.
vk::BufferUsageFlags to_buffer_usage_flags(BufferUsageHint hint)
Resolve the extra vk::BufferUsageFlags a BufferUsageHint requires, on top of whatever base usage the ...
constexpr DescriptorSetID INVALID_DESCRIPTOR_SET
constexpr SemaphoreID INVALID_SEMAPHORE
constexpr CommandBufferID INVALID_COMMAND_BUFFER
static BlendAttachmentConfig alpha_blend()
Create standard alpha blending configuration.
Per-attachment blend configuration.
Depth and stencil test configuration.
Plain-data description of the compute shader to dispatch.
Self-describing push constant fragment staged on a VKBuffer.
Extracted push constant range from shader reflection.
Rasterization state configuration.
std::vector< std::vector< DescriptorBindingInfo > > descriptor_sets
std::vector< Core::VertexAttribute > vertex_attributes
std::vector< Core::VertexBinding > vertex_bindings
std::optional< Kakshya::VertexLayout > semantic_vertex_layout
std::vector< BlendAttachmentConfig > blend_attachments
Complete render pipeline configuration.
bool enable_reflection
Extract descriptor bindings and metadata.
std::vector< std::string > include_directories
Paths for #include resolution.
bool enable_debug_info
Include debug symbols (line numbers, variable names)
bool enable_validation
Validate SPIR-V after compilation.
std::unordered_map< std::string, std::string > defines
Preprocessor macros.
Configuration for shader compilation.
std::optional< std::array< uint32_t, 3 > > workgroup_size
std::vector< PushConstantRangeInfo > push_constant_ranges
std::vector< DescriptorBindingInfo > descriptor_bindings
Extracted reflection information from compiled shader.
std::string content
Shader source code, SPIR-V path, or SPIR-V assembly text.
enum MayaFlux::Portal::Graphics::ShaderSource::SourceType type
ShaderSource(std::string content_, ShaderStage stage_, SourceType type_)
@ SPIRV_ASM
In-memory SPIR-V assembly text (assembled via SPIRV-Tools, no shaderc)
Shader source descriptor for compilation.