MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ compile_glsl_to_spirv()

std::vector< uint32_t > MayaFlux::Core::VKShaderModule::compile_glsl_to_spirv ( const std::string &  glsl_source,
vk::ShaderStageFlagBits  stage,
const std::vector< std::string > &  include_directories,
const std::unordered_map< std::string, std::string > &  defines 
)
private

Compile GLSL to SPIR-V using shaderc.

Parameters
glsl_sourceGLSL source code
stageShader stage (affects compiler settings)
include_directoriesInclude paths
definesPreprocessor macros
Returns
SPIR-V bytecode, or empty vector on failure

Definition at line 600 of file VKShaderModule.cpp.

605{
606#ifdef MAYAFLUX_USE_SHADERC
607 shaderc::Compiler compiler;
608 shaderc::CompileOptions options;
609
610 options.SetOptimizationLevel(shaderc_optimization_level_performance);
611
612 for (const auto& dir : include_directories) {
613 // options.AddIncludeDirectory(dir);
614 }
615
616 for (const auto& [name, value] : defines) {
617 options.AddMacroDefinition(name, value);
618 }
619
620 shaderc_shader_kind shader_kind;
621 switch (stage) {
622 case vk::ShaderStageFlagBits::eVertex:
623 shader_kind = shaderc_glsl_vertex_shader;
624 break;
625 case vk::ShaderStageFlagBits::eFragment:
626 shader_kind = shaderc_glsl_fragment_shader;
627 break;
628 case vk::ShaderStageFlagBits::eCompute:
629 shader_kind = shaderc_glsl_compute_shader;
630 break;
631 case vk::ShaderStageFlagBits::eGeometry:
632 shader_kind = shaderc_glsl_geometry_shader;
633 break;
634 case vk::ShaderStageFlagBits::eTessellationControl:
635 shader_kind = shaderc_glsl_tess_control_shader;
636 break;
637 case vk::ShaderStageFlagBits::eTessellationEvaluation:
638 shader_kind = shaderc_glsl_tess_evaluation_shader;
639 break;
640 case vk::ShaderStageFlagBits::eMeshEXT:
641 shader_kind = shaderc_glsl_mesh_shader;
642 break;
643 case vk::ShaderStageFlagBits::eTaskEXT:
644 shader_kind = shaderc_glsl_task_shader;
645 break;
646 default:
648 "Unsupported shader stage for GLSL compilation: {}", vk::to_string(stage));
649 return {};
650 }
651
652 shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(
653 glsl_source,
654 shader_kind,
655 "shader.glsl",
656 options);
657
658 if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
660 "GLSL compilation failed:\n{}", result.GetErrorMessage());
661 return {};
662 }
663
664 std::vector<uint32_t> spirv(result.cbegin(), result.cend());
665
667 "Compiled GLSL ({} stage) -> {} bytes SPIR-V",
668 vk::to_string(stage), spirv.size() * 4);
669
670 return spirv;
671
672#else
673 return compile_glsl_to_spirv_external(glsl_source, stage, include_directories, defines);
674#endif
675}
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
static std::vector< uint32_t > compile_glsl_to_spirv_external(const std::string &glsl_source, vk::ShaderStageFlagBits stage, const std::vector< std::string > &include_directories={}, const std::unordered_map< std::string, std::string > &defines={})
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.

References compile_glsl_to_spirv_external(), MayaFlux::Journal::Core, MayaFlux::Journal::GraphicsBackend, MF_DEBUG, and MF_ERROR.

Referenced by create_from_glsl().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: