Perform reflection on SPIR-V bytecode.
Uses SPIRV-Cross library to extract bindings, push constants, workgroup sizes, etc. Falls back to basic parsing if library unavailable.
357{
358 try {
359 spirv_cross::Compiler compiler(spirv_code);
360 spirv_cross::ShaderResources resources = compiler.get_shader_resources();
361
362 auto reflect_resources = [&](const spirv_cross::SmallVector<spirv_cross::Resource>& res_vec,
363 bool is_storage = false) {
364 for (const auto& resource : res_vec) {
365 ShaderReflection::DescriptorBinding desc;
366 desc.set = compiler.get_decoration(resource.id, spv::DecorationDescriptorSet);
367 desc.binding = compiler.get_decoration(resource.id, spv::DecorationBinding);
369 desc.name = resource.name;
370
371 const auto& type = compiler.get_type(resource.type_id);
372 desc.count = type.array.empty() ? 1 : type.array[0];
373 desc.type = spirv_to_vk_descriptor_type(type.basetype, type, is_storage);
374
376 }
377 };
378
379 reflect_resources(resources.uniform_buffers, false);
380
381 reflect_resources(resources.storage_buffers, true);
382
383 reflect_resources(resources.sampled_images, false);
384
385 reflect_resources(resources.storage_images, true);
386
387 reflect_resources(resources.separate_images, false);
388 reflect_resources(resources.separate_samplers, false);
389
393 }
394
395 for (const auto& pc_buffer : resources.push_constant_buffers) {
396 const auto& type = compiler.get_type(pc_buffer.type_id);
397
398 ShaderReflection::PushConstantRange range;
400 range.offset = 0;
401 range.size = static_cast<uint32_t>(compiler.get_declared_struct_size(type));
402
404 }
405
409 }
410
411 auto spec_constants = compiler.get_specialization_constants();
412 for (const auto& spec : spec_constants) {
413 ShaderReflection::SpecializationConstant sc;
414 sc.constant_id = spec.constant_id;
415 sc.name = compiler.get_name(spec.id);
416
417 const auto& type = compiler.get_type(compiler.get_constant(spec.id).constant_type);
418 sc.size = static_cast<uint32_t>(compiler.get_declared_struct_size(type));
419
421 }
422
425 "Reflected {} specialization constants",
427 }
428
429 if (
m_stage == vk::ShaderStageFlagBits::eCompute) {
430 auto entry_points = compiler.get_entry_points_and_stages();
431
432 for (const auto& ep : entry_points) {
433 if (ep.name ==
m_entry_point && ep.execution_model == spv::ExecutionModelGLCompute) {
434
435 std::array<uint32_t, 3> workgroup_size {
436 compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, 0),
437 compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, 1),
438 compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, 2)
439 };
440
441 if (!workgroup_size.empty() && workgroup_size.size() >= 3) {
443 workgroup_size[0],
444 workgroup_size[1],
445 workgroup_size[2]
446 };
447
449 "Compute shader workgroup size: [{}, {}, {}]",
450 workgroup_size[0], workgroup_size[1], workgroup_size[2]);
451 }
452 break;
453 }
454 }
455 }
456
457 if (
m_stage == vk::ShaderStageFlagBits::eVertex) {
458 for (const auto& input : resources.stage_inputs) {
459 uint32_t location = compiler.get_decoration(input.id, spv::DecorationLocation);
460 const auto& type = compiler.get_type(input.type_id);
461
462 vk::VertexInputAttributeDescription attr;
463 attr.location = location;
464 attr.binding = 0;
466 attr.offset = 0;
467
469 }
470
473 "Reflected {} vertex input attributes",
475 }
476 }
477
478 return true;
479
480 } catch (const spirv_cross::CompilerError& e) {
482 "SPIRV-Cross reflection failed: {}", e.what());
483 return false;
484 }
485}
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
vk::ShaderStageFlagBits m_stage
ShaderReflection m_reflection
static vk::Format spirv_type_to_vk_format(const spirv_cross::SPIRType &type)
Convert SPIRV-Cross type to Vulkan vertex attribute format.
std::string m_entry_point
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.
std::vector< SpecializationConstant > specialization_constants
std::vector< DescriptorBinding > bindings
std::vector< PushConstantRange > push_constants
std::vector< vk::VertexInputAttributeDescription > vertex_attributes
std::optional< std::array< uint32_t, 3 > > workgroup_size
local_size_x/y/z