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

◆ load_shader() [2/3]

ShaderID MayaFlux::Portal::Graphics::ShaderFoundry::load_shader ( const ShaderSpec &  spec)

Compile and cache a compute shader from a declarative ShaderSpec.

Emits SPIR-V assembly text from the spec via detail::emit_spirv_asm(), assembles it via VKShaderModule::create_from_spirv_asm() using SPIRV-Tools, and caches the result by content hash. No shaderc or GLSL toolchain is involved.

Subsequent calls with a spec that produces identical assembly are cache hits and return the existing ShaderID at no cost.

The emitted shader is always a compute stage. Entry point is "main". Binding 0 at set 0 is never emitted; it is engine-reserved.

Parameters
specShaderSpec produced by ShaderSpec::Assemble::build().
Returns
ShaderID on success, INVALID_SHADER on compilation failure.

Definition at line 397 of file ShaderFoundry.cpp.

398{
399 if (!is_initialized()) {
401 "ShaderFoundry not initialized");
402 return INVALID_SHADER;
403 }
404
405 if (spec.kernel.has_value()) {
406 const std::string glsl = detail::emit_glsl_kernel(spec);
407 const std::string key = generate_source_cache_key(glsl, ShaderStage::COMPUTE);
408
409 auto kernel_id_it = m_shader_filepath_cache.find(key);
410 if (kernel_id_it != m_shader_filepath_cache.end()) {
412 "Using cached kernel shader: {}", key);
413 return kernel_id_it->second;
414 }
415
416 auto module = compile_from_source_cached(glsl, ShaderStage::COMPUTE, key);
417 if (!module) {
419 "Failed to compile kernel shader");
420 return INVALID_SHADER;
421 }
422 const ShaderID id = m_next_shader_id++;
423 auto& state = m_shaders[id];
424 state.module = module;
425 state.filepath = key;
426 state.stage = ShaderStage::COMPUTE;
427 state.entry_point = "main";
428 m_shader_filepath_cache[key] = id;
430 "Compiled kernel shader (ID: {}, key: {})", id, key);
431 return id;
432 }
433
434 const std::string asm_text = detail::emit_spirv_asm(spec);
435 const std::string key = generate_source_cache_key(asm_text, ShaderStage::COMPUTE);
436
437 auto id_it = m_shader_filepath_cache.find(key);
438 if (id_it != m_shader_filepath_cache.end()) {
440 "Using cached generated shader: {}", key);
441 return id_it->second;
442 }
443
444 auto module = compile_from_spirv_asm(asm_text, ShaderStage::COMPUTE);
445 if (!module) {
447 "Failed to compile generated shader (key: {})", key);
448 return INVALID_SHADER;
449 }
450
451 const ShaderID id = m_next_shader_id++;
452 auto& state = m_shaders[id];
453 state.module = module;
454 state.filepath = key;
455 state.stage = ShaderStage::COMPUTE;
456 state.entry_point = "main";
457
458 m_shader_filepath_cache[key] = id;
459
461 "Compiled generated shader (ID: {}, key: {})", id, key);
462
463 return id;
464}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::unordered_map< std::string, ShaderID > m_shader_filepath_cache
bool is_initialized() const
Check if compiler is initialized.
std::string generate_source_cache_key(const std::string &source, ShaderStage stage) const
std::unordered_map< ShaderID, ShaderState > m_shaders
@ ShaderCompilation
Shader compilation tasks (Portal::Graphics::ShaderCompiler)
@ Portal
High-level user-facing API layer.
std::string emit_glsl_kernel(const ShaderSpec &spec)
Emit a complete GLSL compute shader from spec metadata and a KernelSource body.
std::string emit_spirv_asm(const ShaderSpec &spec)
Emit complete SPIR-V assembly text for a generated compute kernel.
constexpr ShaderID INVALID_SHADER

References MayaFlux::Portal::Graphics::COMPUTE, MayaFlux::Portal::Graphics::detail::emit_glsl_kernel(), MayaFlux::Portal::Graphics::detail::emit_spirv_asm(), generate_source_cache_key(), MayaFlux::Portal::Graphics::INVALID_SHADER, is_initialized(), MayaFlux::Portal::Graphics::ShaderSpec::kernel, m_next_shader_id, m_shader_filepath_cache, m_shaders, MF_DEBUG, MF_ERROR, MF_INFO, MayaFlux::Journal::Portal, and MayaFlux::Journal::ShaderCompilation.

+ Here is the call graph for this function: