MayaFlux 0.1.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

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 265 of file ShaderFoundry.cpp.

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