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

◆ load_shader() [3/3]

ShaderID MayaFlux::Portal::Graphics::ShaderFoundry::load_shader ( const std::string &  content,
std::optional< ShaderStage >  stage = std::nullopt,
const std::string &  entry_point = "main" 
)

Universal shader loader - auto-detects source type.

Parameters
contentFile path, GLSL source string, or SPIR-V path
stageOptional stage override (auto-detected if omitted)
entry_pointEntry point function name (default: "main")
Returns
ShaderID, or INVALID_SHADER on failure

Supports:

  • GLSL files: .comp, .vert, .frag, .geom, .tesc, .tese
  • SPIR-V files: .spv

Stage auto-detection: .comp → COMPUTE .vert → VERTEX .frag → FRAGMENT .geom → GEOMETRY .tesc → TESS_CONTROL .tese → TESS_EVALUATION .mesh → MESH .task → TASK

Examples: load_shader("shaders/kernel.comp"); // File load_shader("shaders/kernel.spv", COMPUTE); // SPIR-V load_shader("#version 460\nvoid main(){}", COMPUTE); // Source

Definition at line 307 of file ShaderFoundry.cpp.

311{
312 if (!is_initialized()) {
314 "ShaderFoundry not initialized");
315 return INVALID_SHADER;
316 }
317
318 DetectedSourceType source_type = detect_source_type(content);
319
320 std::string cache_key;
321 if (source_type == DetectedSourceType::FILE_GLSL || source_type == DetectedSourceType::FILE_SPIRV) {
322 cache_key = content;
323 } else {
324 cache_key = generate_source_cache_key(content, stage.value_or(ShaderStage::COMPUTE));
325 }
326
327 auto id_it = m_shader_filepath_cache.find(cache_key);
328 if (id_it != m_shader_filepath_cache.end()) {
330 "Using cached shader ID for: {}", cache_key);
331 return id_it->second;
332 }
333
334 if (!stage.has_value()) {
335 if (source_type == DetectedSourceType::FILE_GLSL || source_type == DetectedSourceType::FILE_SPIRV) {
336 if (source_type == DetectedSourceType::FILE_SPIRV) {
337 std::filesystem::path p(content);
338 std::string stem = p.stem().string();
339 stage = detect_stage_from_extension(stem);
340 } else {
341 stage = detect_stage_from_extension(content);
342 }
343 }
344
345 if (!stage.has_value()) {
347 "Cannot auto-detect shader stage from '{}' - must specify explicitly",
348 content);
349 return INVALID_SHADER;
350 }
351 }
352
353 std::shared_ptr<Core::VKShaderModule> shader_module;
354
355 switch (source_type) {
357 shader_module = compile_from_file(content, stage, entry_point);
358 break;
360 shader_module = compile_from_spirv(content, *stage, entry_point);
361 break;
363 shader_module = compile_from_source(content, *stage, entry_point);
364 break;
365 default:
367 "Cannot determine shader source type");
368 return INVALID_SHADER;
369 }
370
371 if (!shader_module) {
372 return INVALID_SHADER;
373 }
374
376
377 ShaderState& state = m_shaders[id];
378 state.module = shader_module;
379 state.filepath = cache_key;
380 state.stage = *stage;
381 state.entry_point = entry_point;
382
383 m_shader_filepath_cache[cache_key] = id;
384
386 "Shader loaded: {} (ID: {}, stage: {})",
387 cache_key, id, static_cast<int>(*stage));
388
389 return id;
390}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
static std::optional< ShaderStage > detect_stage_from_extension(const std::string &filepath)
Auto-detect shader stage from file extension.
DetectedSourceType
Internal enum for source type detection.
std::unordered_map< std::string, ShaderID > m_shader_filepath_cache
DetectedSourceType detect_source_type(const std::string &content) const
bool is_initialized() const
Check if compiler is initialized.
std::shared_ptr< Core::VKShaderModule > compile_from_source(const std::string &source, ShaderStage stage, const std::string &entry_point="main")
std::shared_ptr< Core::VKShaderModule > compile_from_file(const std::string &filepath, std::optional< ShaderStage > stage=std::nullopt, const std::string &entry_point="main")
std::shared_ptr< Core::VKShaderModule > compile_from_spirv(const std::string &spirv_path, ShaderStage stage, const std::string &entry_point="main")
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.
constexpr ShaderID INVALID_SHADER

References compile_from_file(), compile_from_source(), compile_from_spirv(), MayaFlux::Portal::Graphics::COMPUTE, detect_source_type(), detect_stage_from_extension(), MayaFlux::Portal::Graphics::ShaderFoundry::ShaderState::entry_point, FILE_GLSL, FILE_SPIRV, MayaFlux::Portal::Graphics::ShaderFoundry::ShaderState::filepath, generate_source_cache_key(), MayaFlux::Portal::Graphics::INVALID_SHADER, is_initialized(), m_next_shader_id, m_shader_filepath_cache, m_shaders, MF_DEBUG, MF_ERROR, MF_INFO, MayaFlux::Journal::Portal, MayaFlux::Journal::ShaderCompilation, SOURCE_STRING, and MayaFlux::Portal::Graphics::ShaderFoundry::ShaderState::stage.

Referenced by MayaFlux::Yantra::config_from_spec(), load_shader(), reload_shader(), and MayaFlux::Buffers::RenderProcessor::RenderProcessor().

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