MayaFlux 0.5.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 378 of file ShaderFoundry.cpp.

379{
380 if (!is_initialized()) {
382 "ShaderFoundry not initialized");
383 return INVALID_SHADER;
384 }
385
386 if (spec.kernel.has_value()) {
387 const std::string glsl = detail::emit_glsl_kernel(spec);
388 const std::string key = generate_source_cache_key(glsl, ShaderStage::COMPUTE);
389 auto module = compile_from_source_cached(glsl, ShaderStage::COMPUTE, key);
390 if (!module) {
392 "Failed to compile kernel shader");
393 return INVALID_SHADER;
394 }
395 const ShaderID id = m_next_shader_id++;
396 auto& state = m_shaders[id];
397 state.module = module;
398 state.filepath = key;
399 state.stage = ShaderStage::COMPUTE;
400 state.entry_point = "main";
401 m_shader_filepath_cache[key] = id;
403 "Compiled kernel shader (ID: {}, key: {})", id, key);
404 return id;
405 }
406
407 const std::string asm_text = detail::emit_spirv_asm(spec);
408 const std::string key = generate_source_cache_key(asm_text, ShaderStage::COMPUTE);
409
410 auto id_it = m_shader_filepath_cache.find(key);
411 if (id_it != m_shader_filepath_cache.end()) {
413 "Using cached generated shader: {}", key);
414 return id_it->second;
415 }
416
417 auto module = compile_from_spirv_asm(asm_text, ShaderStage::COMPUTE);
418 if (!module) {
420 "Failed to compile generated shader (key: {})", key);
421 return INVALID_SHADER;
422 }
423
424 const ShaderID id = m_next_shader_id++;
425 auto& state = m_shaders[id];
426 state.module = module;
427 state.filepath = key;
428 state.stage = ShaderStage::COMPUTE;
429 state.entry_point = "main";
430
431 m_shader_filepath_cache[key] = id;
432
434 "Compiled generated shader (ID: {}, key: {})", id, key);
435
436 return id;
437}
#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: