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

◆ load_shader() [2/2]

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 450\nvoid main(){}", COMPUTE); // Source

Definition at line 269 of file ShaderFoundry.cpp.

273{
274 if (!is_initialized()) {
276 "ShaderFoundry not initialized");
277 return INVALID_SHADER;
278 }
279
280 DetectedSourceType source_type = detect_source_type(content);
281
282 std::string cache_key;
283 if (source_type == DetectedSourceType::FILE_GLSL || source_type == DetectedSourceType::FILE_SPIRV) {
284 cache_key = content;
285 } else {
286 cache_key = generate_source_cache_key(content, stage.value_or(ShaderStage::COMPUTE));
287 }
288
289 auto id_it = m_shader_filepath_cache.find(cache_key);
290 if (id_it != m_shader_filepath_cache.end()) {
292 "Using cached shader ID for: {}", cache_key);
293 return id_it->second;
294 }
295
296 if (!stage.has_value()) {
297 if (source_type == DetectedSourceType::FILE_GLSL || source_type == DetectedSourceType::FILE_SPIRV) {
298 if (source_type == DetectedSourceType::FILE_SPIRV) {
299 std::filesystem::path p(content);
300 std::string stem = p.stem().string();
301 stage = detect_stage_from_extension(stem);
302 } else {
303 stage = detect_stage_from_extension(content);
304 }
305 }
306
307 if (!stage.has_value()) {
309 "Cannot auto-detect shader stage from '{}' - must specify explicitly",
310 content);
311 return INVALID_SHADER;
312 }
313 }
314
315 std::shared_ptr<Core::VKShaderModule> shader_module;
316
317 switch (source_type) {
319 shader_module = compile_from_file(content, stage, entry_point);
320 break;
322 shader_module = compile_from_spirv(content, *stage, entry_point);
323 break;
325 shader_module = compile_from_source(content, *stage, entry_point);
326 break;
327 default:
329 "Cannot determine shader source type");
330 return INVALID_SHADER;
331 }
332
333 if (!shader_module) {
334 return INVALID_SHADER;
335 }
336
338
339 ShaderState& state = m_shaders[id];
340 state.module = shader_module;
341 state.filepath = cache_key;
342 state.stage = *stage;
343 state.entry_point = entry_point;
344
345 m_shader_filepath_cache[cache_key] = id;
346
348 "Shader loaded: {} (ID: {}, stage: {})",
349 cache_key, id, static_cast<int>(*stage));
350
351 return id;
352}
#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 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: