16 const std::shared_ptr<Core::VulkanBackend>& backend,
21 "ShaderFoundry already initialized (static flag)");
27 "Cannot initialize ShaderFoundry with null backend");
33 "ShaderFoundry already initialized");
50 "ShaderFoundry initialized");
65 "Shutting down ShaderFoundry...");
67 auto& cmd_manager =
m_backend->get_command_manager();
71 if (state.is_active) {
72 cmd_manager.free_command_buffer(state.cmd);
74 if (state.timestamp_pool) {
75 device.destroyQueryPool(state.timestamp_pool);
81 device.destroyFence(state.fence);
86 device.destroySemaphore(state.semaphore);
103 "ShaderFoundry shutdown complete");
111 const std::string& filepath,
112 std::optional<ShaderStage> stage,
113 const std::string& entry_point)
117 "ShaderFoundry not initialized");
124 "Using cached shader: {}", filepath);
128 std::optional<vk::ShaderStageFlagBits> vk_stage;
129 if (stage.has_value()) {
135 if (filepath.ends_with(
".spv")) {
136 if (!shader->create_from_spirv_file(
138 vk_stage.value_or(vk::ShaderStageFlagBits::eCompute),
141 "Failed to compile SPIR-V shader: {}", filepath);
145 if (!shader->create_from_glsl_file(
146 get_device(), filepath, vk_stage, entry_point,
150 "Failed to compile GLSL shader: {}", filepath);
157 "Compiled shader: {} ({})", filepath, vk::to_string(shader->get_stage()));
162 const std::string& source,
164 const std::string& entry_point)
168 "ShaderFoundry not initialized");
175 if (!shader->create_from_glsl(
180 "Failed to compile GLSL source");
185 "Compiled shader from source ({})", vk::to_string(vk_stage));
190 const std::string& source,
192 const std::string& cache_key,
193 const std::string& entry_point)
198 "Using cached shader: {}", cache_key);
212 const std::string& spirv_path,
214 const std::string& entry_point)
218 "ShaderFoundry not initialized");
225 "Using cached SPIR-V shader: {}", spirv_path);
232 if (!shader->create_from_spirv_file(
233 get_device(), spirv_path, vk_stage, entry_point,
236 "Failed to load SPIR-V shader: {}", spirv_path);
242 "Loaded SPIR-V shader: {}", spirv_path);
248 switch (shader_source.
type) {
260 "Unknown shader source type");
266 const std::string& content,
267 std::optional<ShaderStage> stage,
268 const std::string& entry_point)
272 "ShaderFoundry not initialized");
278 std::string cache_key;
288 "Using cached shader ID for: {}", cache_key);
289 return id_it->second;
292 if (!stage.has_value()) {
295 std::filesystem::path p(content);
296 std::string stem = p.stem().string();
303 if (!stage.has_value()) {
305 "Cannot auto-detect shader stage from '{}' - must specify explicitly",
311 std::shared_ptr<Core::VKShaderModule> shader_module;
313 switch (source_type) {
325 "Cannot determine shader source type");
329 if (!shader_module) {
336 state.module = shader_module;
338 state.
stage = *stage;
344 "Shader loaded: {} (ID: {}, stage: {})",
345 cache_key,
id,
static_cast<int>(*stage));
357 namespace fs = std::filesystem;
359 fs::path path(filepath);
361 if (path.is_absolute() || fs::exists(filepath)) {
365 std::vector<std::string> search_paths = {
366 Core::SHADER_BUILD_OUTPUT_DIR,
367 Core::SHADER_INSTALL_DIR,
368 Core::SHADER_SOURCE_DIR,
373 for (
const auto& search_path : search_paths) {
374 fs::path full_path = fs::path(search_path) / filepath;
375 if (fs::exists(full_path)) {
387 if (resolved_path.has_value()) {
388 std::string ext = resolved_path->extension().string();
389 std::ranges::transform(ext, ext.begin(), ::tolower);
398 if (content.size() > 1024 || content.find(
'\n') != std::string::npos) {
407 std::hash<std::string> hasher;
408 size_t hash = hasher(source + std::to_string(
static_cast<int>(stage)));
409 return "source_" + std::to_string(hash);
429 if (!it->second.filepath.empty()) {
433 if (!it->second.filepath.empty()) {
452 const auto& reflection = it->second.module->get_reflection();
455 info.
stage = it->second.stage;
459 for (
const auto& binding : reflection.bindings) {
461 binding_info.
set = binding.set;
462 binding_info.
binding = binding.binding;
463 binding_info.
type = binding.type;
464 binding_info.
name = binding.name;
468 for (
const auto& pc : reflection.push_constants) {
470 pc_info.
offset = pc.offset;
471 pc_info.size = pc.size;
482 return it->second.
stage;
491 return it->second.entry_point;
503 std::vector<std::string> keys;
521 "Invalidated shader cache: {}", cache_key);
529 "Cleared shader cache");
546 "Updated shader compiler configuration");
576 vk::DescriptorType type,
586 vk::DescriptorBufferInfo buffer_info;
587 buffer_info.buffer = buffer;
588 buffer_info.offset = offset;
589 buffer_info.range = size;
591 vk::WriteDescriptorSet write;
592 write.dstSet = it->second.descriptor_set;
593 write.dstBinding = binding;
594 write.dstArrayElement = 0;
595 write.descriptorCount = 1;
596 write.descriptorType = type;
597 write.pBufferInfo = &buffer_info;
599 get_device().updateDescriptorSets(1, &write, 0,
nullptr);
605 vk::ImageView image_view,
607 vk::ImageLayout layout)
614 vk::DescriptorImageInfo image_info;
615 image_info.imageView = image_view;
616 image_info.sampler = sampler;
617 image_info.imageLayout = layout;
619 vk::WriteDescriptorSet write;
620 write.dstSet = it->second.descriptor_set;
621 write.dstBinding = binding;
622 write.dstArrayElement = 0;
623 write.descriptorCount = 1;
624 write.descriptorType = vk::DescriptorType::eCombinedImageSampler;
625 write.pImageInfo = &image_info;
627 get_device().updateDescriptorSets(1, &write, 0,
nullptr);
633 vk::ImageView image_view,
634 vk::ImageLayout layout)
641 vk::DescriptorImageInfo image_info;
642 image_info.imageView = image_view;
643 image_info.imageLayout = layout;
645 vk::WriteDescriptorSet write;
646 write.dstSet = it->second.descriptor_set;
647 write.dstBinding = binding;
648 write.dstArrayElement = 0;
649 write.descriptorCount = 1;
650 write.descriptorType = vk::DescriptorType::eStorageImage;
651 write.pImageInfo = &image_info;
653 get_device().updateDescriptorSets(1, &write, 0,
nullptr);
660 error<std::invalid_argument>(
663 std::source_location::current(),
664 "Invalid DescriptorSetID: {}", descriptor_set_id);
666 return it->second.descriptor_set;
675 auto& cmd_manager =
m_backend->get_command_manager();
680 state.
cmd = cmd_manager.begin_single_time_commands();
691 return it->second.cmd;
707 auto& cmd_manager =
m_backend->get_command_manager();
709 it->second.cmd.end();
711 vk::SubmitInfo submit_info;
712 submit_info.commandBufferCount = 1;
713 submit_info.pCommandBuffers = &it->second.cmd;
716 switch (it->second.type) {
728 if (queue.submit(1, &submit_info,
nullptr) != vk::Result::eSuccess) {
730 "Failed to submit command buffer");
735 cmd_manager.free_command_buffer(it->second.cmd);
737 it->second.is_active =
false;
748 cmd_it->second.cmd.end();
752 vk::FenceCreateInfo fence_info;
757 vk::SubmitInfo submit_info;
758 submit_info.commandBufferCount = 1;
759 submit_info.pCommandBuffers = &cmd_it->second.cmd;
762 switch (cmd_it->second.type) {
774 if (queue.submit(1, &submit_info, fence_state.
fence) != vk::Result::eSuccess) {
776 "Failed to submit command buffer");
780 cmd_it->second.is_active =
false;
792 cmd_it->second.cmd.end();
796 vk::SemaphoreCreateInfo semaphore_info;
800 vk::SubmitInfo submit_info;
801 submit_info.commandBufferCount = 1;
802 submit_info.pCommandBuffers = &cmd_it->second.cmd;
803 submit_info.signalSemaphoreCount = 1;
804 submit_info.pSignalSemaphores = &semaphore_state.
semaphore;
807 switch (cmd_it->second.type) {
819 if (queue.submit(1, &submit_info,
nullptr) != vk::Result::eSuccess) {
821 "Failed to submit command buffer");
825 cmd_it->second.is_active =
false;
837 if (
get_device().waitForFences(1, &it->second.fence, VK_TRUE, UINT64_MAX) != vk::Result::eSuccess) {
839 "Failed to wait for fence: {}", fence_id);
842 it->second.signaled =
true;
847 std::vector<vk::Fence> fences;
848 for (
auto fence_id : fence_ids) {
851 fences.push_back(it->second.fence);
855 if (!fences.empty()) {
856 if (
get_device().waitForFences(
static_cast<uint32_t
>(fences.size()), fences.data(), VK_TRUE, UINT64_MAX) != vk::Result::eSuccess) {
858 "Failed to wait for fences");
863 for (
auto fence_id : fence_ids) {
866 it->second.signaled =
true;
878 if (it->second.signaled) {
882 auto result =
get_device().getFenceStatus(it->second.fence);
883 it->second.signaled = (result == vk::Result::eSuccess);
884 return it->second.signaled;
890 vk::PipelineStageFlags )
904 return it->second.semaphore;
916 vk::AccessFlags src_access,
917 vk::AccessFlags dst_access,
918 vk::PipelineStageFlags src_stage,
919 vk::PipelineStageFlags dst_stage)
926 vk::BufferMemoryBarrier barrier;
927 barrier.srcAccessMask = src_access;
928 barrier.dstAccessMask = dst_access;
929 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
930 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
931 barrier.buffer = buffer;
933 barrier.size = VK_WHOLE_SIZE;
935 it->second.cmd.pipelineBarrier(
938 vk::DependencyFlags {},
947 vk::ImageLayout old_layout,
948 vk::ImageLayout new_layout,
949 vk::AccessFlags src_access,
950 vk::AccessFlags dst_access,
951 vk::PipelineStageFlags src_stage,
952 vk::PipelineStageFlags dst_stage)
959 vk::ImageMemoryBarrier barrier;
960 barrier.srcAccessMask = src_access;
961 barrier.dstAccessMask = dst_access;
962 barrier.oldLayout = old_layout;
963 barrier.newLayout = new_layout;
964 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
965 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
966 barrier.image = image;
967 barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
968 barrier.subresourceRange.baseMipLevel = 0;
969 barrier.subresourceRange.levelCount = 1;
970 barrier.subresourceRange.baseArrayLayer = 0;
971 barrier.subresourceRange.layerCount = 1;
973 it->second.cmd.pipelineBarrier(
976 vk::DependencyFlags {},
1001 if (!it->second.timestamp_pool) {
1002 vk::QueryPoolCreateInfo pool_info;
1003 pool_info.queryType = vk::QueryType::eTimestamp;
1004 pool_info.queryCount = 128;
1005 it->second.timestamp_pool =
get_device().createQueryPool(pool_info);
1008 auto query_index =
static_cast<uint32_t
>(it->second.timestamp_queries.size() * 2);
1009 it->second.timestamp_queries[label] = query_index;
1011 it->second.cmd.resetQueryPool(it->second.timestamp_pool, query_index, 2);
1012 it->second.cmd.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, it->second.timestamp_pool, query_index);
1022 auto query_it = it->second.timestamp_queries.find(label);
1023 if (query_it == it->second.timestamp_queries.end()) {
1027 uint32_t query_index = query_it->second;
1028 it->second.cmd.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, it->second.timestamp_pool, query_index + 1);
1035 return { .
label = label, .duration_ns = 0, .valid =
false };
1038 auto query_it = it->second.timestamp_queries.find(label);
1039 if (query_it == it->second.timestamp_queries.end()) {
1040 return { .label = label, .duration_ns = 0, .valid =
false };
1043 if (!it->second.timestamp_pool) {
1044 return { .label = label, .duration_ns = 0, .valid =
false };
1047 uint32_t query_index = query_it->second;
1048 uint64_t timestamps[2];
1050 auto result =
get_device().getQueryPoolResults(
1051 it->second.timestamp_pool,
1057 vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait);
1059 if (result != vk::Result::eSuccess) {
1060 return { .label = label, .duration_ns = 0, .valid =
false };
1063 auto props =
m_backend->get_context().get_physical_device().getProperties();
1064 float timestamp_period = props.limits.timestampPeriod;
1066 auto duration_ns =
static_cast<uint64_t
>((timestamps[1] - timestamps[0]) * timestamp_period);
1068 return { .label = label, .duration_ns = duration_ns, .valid =
true };
1078 auto it = foundry.m_shaders.find(shader_id);
1079 if (it != foundry.m_shaders.end()) {
1080 return it->second.module;
1093 return vk::ShaderStageFlagBits::eCompute;
1095 return vk::ShaderStageFlagBits::eVertex;
1097 return vk::ShaderStageFlagBits::eFragment;
1099 return vk::ShaderStageFlagBits::eGeometry;
1101 return vk::ShaderStageFlagBits::eTessellationControl;
1103 return vk::ShaderStageFlagBits::eTessellationEvaluation;
1105 return vk::ShaderStageFlagBits::eCompute;
1112 if (!vk_stage.has_value()) {
1113 return std::nullopt;
1116 switch (*vk_stage) {
1117 case vk::ShaderStageFlagBits::eCompute:
1119 case vk::ShaderStageFlagBits::eVertex:
1121 case vk::ShaderStageFlagBits::eFragment:
1123 case vk::ShaderStageFlagBits::eGeometry:
1125 case vk::ShaderStageFlagBits::eTessellationControl:
1127 case vk::ShaderStageFlagBits::eTessellationEvaluation:
1130 return std::nullopt;
1140 return std::make_shared<Core::VKShaderModule>();
1145 return m_backend->get_context().get_device();
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
static std::optional< vk::ShaderStageFlagBits > detect_stage_from_extension(const std::string &filepath)
Auto-detect shader stage from file extension.
vk::DescriptorSet get_descriptor_set(DescriptorSetID descriptor_set_id)
Get Vulkan descriptor set handle from DescriptorSetID.
vk::Queue get_graphics_queue() const
Get Vulkan graphics queue.
void wait_for_fences(const std::vector< FenceID > &fence_ids)
Wait for multiple fences to be signaled.
static std::optional< ShaderStage > detect_stage_from_extension(const std::string &filepath)
Auto-detect shader stage from file extension.
vk::Device get_device() const
static bool s_initialized
void shutdown()
Shutdown and cleanup.
std::atomic< uint64_t > m_next_fence_id
void wait_for_fence(FenceID fence_id)
Wait for fence to be signaled.
bool is_cached(const std::string &cache_key) const
Check if shader is cached.
std::shared_ptr< Core::VKShaderModule > compile(const ShaderSource &shader_source)
Compile shader from ShaderSource descriptor.
ShaderID reload_shader(const std::string &filepath)
Hot-reload shader (returns new ID)
std::shared_ptr< Core::VKShaderModule > hot_reload(const std::string &filepath)
Hot-reload a shader from file.
std::atomic< uint64_t > m_next_shader_id
void set_config(const ShaderCompilerConfig &config)
Update compiler configuration.
void update_descriptor_storage_image(DescriptorSetID descriptor_set_id, uint32_t binding, vk::ImageView image_view, vk::ImageLayout layout=vk::ImageLayout::eGeneral)
Update descriptor set with storage image binding.
CommandBufferID begin_commands(CommandBufferType type)
Begin recording command buffer.
vk::Semaphore get_semaphore_handle(SemaphoreID semaphore_id)
Get Vulkan fence handle from FenceID.
std::shared_ptr< Core::VKShaderModule > get_vk_shader_module(ShaderID shader_id)
DetectedSourceType
Internal enum for source type detection.
vk::Queue m_transfer_queue
bool initialize(const std::shared_ptr< Core::VulkanBackend > &backend, const ShaderCompilerConfig &config={})
Initialize shader compiler.
std::unordered_map< std::string, ShaderID > m_shader_filepath_cache
DetectedSourceType detect_source_type(const std::string &content) const
std::unordered_map< FenceID, FenceState > m_fences
std::shared_ptr< Core::VKShaderModule > create_shader_module()
void invalidate_cache(const std::string &cache_key)
Invalidate cache for specific shader.
std::atomic< uint64_t > m_next_command_id
TimestampResult get_timestamp_result(CommandBufferID cmd_id, const std::string &label)
void add_define(const std::string &name, const std::string &value="")
Add preprocessor define for shader compilation.
std::unordered_map< std::string, std::shared_ptr< Core::VKShaderModule > > m_shader_cache
static vk::ShaderStageFlagBits to_vulkan_stage(ShaderStage stage)
Convert Portal ShaderStage to Vulkan ShaderStageFlagBits.
void update_descriptor_buffer(DescriptorSetID descriptor_set_id, uint32_t binding, vk::DescriptorType type, vk::Buffer buffer, size_t offset, size_t size)
Update descriptor set with buffer binding.
vk::Queue m_graphics_queue
CommandBufferID begin_commands_with_wait(CommandBufferType type, SemaphoreID wait_semaphore, vk::PipelineStageFlags wait_stage)
Begin command buffer that waits on a semaphore.
std::optional< std::filesystem::path > resolve_shader_path(const std::string &filepath) const
std::unordered_map< DescriptorSetID, DescriptorSetState > m_descriptor_sets
std::unordered_map< CommandBufferID, CommandBufferState > m_command_buffers
ShaderCompilerConfig m_config
SemaphoreID submit_with_signal(CommandBufferID cmd_id)
Submit command buffer asynchronously, returning a semaphore.
std::shared_ptr< Core::VKDescriptorManager > m_global_descriptor_manager
vk::Queue get_compute_queue() const
Get Vulkan compute queue.
FenceID submit_async(CommandBufferID cmd_id)
Submit command buffer asynchronously, returning a fence.
void update_descriptor_image(DescriptorSetID descriptor_set_id, uint32_t binding, vk::ImageView image_view, vk::Sampler sampler, vk::ImageLayout layout=vk::ImageLayout::eShaderReadOnlyOptimal)
Update descriptor set with image binding.
std::string get_shader_entry_point(ShaderID shader_id)
Get entry point name for compiled shader.
std::shared_ptr< Core::VKShaderModule > compile_from_source_cached(const std::string &source, ShaderStage stage, const std::string &cache_key, const std::string &entry_point="main")
bool is_initialized() const
Check if compiler is initialized.
void destroy_shader(ShaderID shader_id)
Destroy shader (cleanup internal state)
void begin_timestamp(CommandBufferID cmd_id, const std::string &label="")
void image_barrier(CommandBufferID cmd_id, vk::Image image, vk::ImageLayout old_layout, vk::ImageLayout new_layout, vk::AccessFlags src_access, vk::AccessFlags dst_access, vk::PipelineStageFlags src_stage, vk::PipelineStageFlags dst_stage)
Insert image memory barrier.
static ShaderFoundry & instance()
void submit_and_wait(CommandBufferID cmd_id)
Submit command buffer and wait for completion.
vk::CommandBuffer get_command_buffer(CommandBufferID cmd_id)
Get Vulkan command buffer handle from CommandBufferID.
vk::Queue get_transfer_queue() const
Get Vulkan transfer queue.
void add_include_directory(const std::string &directory)
Add include directory for shader compilation.
std::vector< std::string > get_cached_keys() const
Get all cached shader keys.
vk::Queue m_compute_queue
void clear_cache()
Invalidate entire shader cache.
ShaderReflectionInfo get_shader_reflection(ShaderID shader_id)
Get reflection info for compiled shader.
std::shared_ptr< Core::VKShaderModule > compile_from_source(const std::string &source, ShaderStage stage, const std::string &entry_point="main")
void buffer_barrier(CommandBufferID cmd_id, vk::Buffer buffer, vk::AccessFlags src_access, vk::AccessFlags dst_access, vk::PipelineStageFlags src_stage, vk::PipelineStageFlags dst_stage)
Insert buffer memory barrier.
std::atomic< uint64_t > m_next_descriptor_set_id
std::shared_ptr< Core::VKShaderModule > compile_from_file(const std::string &filepath, std::optional< ShaderStage > stage=std::nullopt, const std::string &entry_point="main")
void end_timestamp(CommandBufferID cmd_id, const std::string &label="")
std::shared_ptr< Core::VKShaderModule > compile_from_spirv(const std::string &spirv_path, ShaderStage stage, const std::string &entry_point="main")
ShaderID 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.
std::string generate_source_cache_key(const std::string &source, ShaderStage stage) const
std::shared_ptr< Core::VulkanBackend > m_backend
bool is_fence_signaled(FenceID fence_id)
Check if fence is signaled.
std::unordered_map< SemaphoreID, SemaphoreState > m_semaphores
ShaderStage get_shader_stage(ShaderID shader_id)
Get shader stage for compiled shader.
std::unordered_map< ShaderID, ShaderState > m_shaders
DescriptorSetID allocate_descriptor_set(vk::DescriptorSetLayout layout)
Allocate descriptor set for a pipeline.
std::atomic< uint64_t > m_next_semaphore_id
@ ShaderCompilation
Shader compilation tasks (Portal::Graphics::ShaderCompiler)
@ Portal
High-level user-facing API layer.
constexpr ShaderID INVALID_SHADER
ShaderStage
User-friendly shader stage enum.
constexpr FenceID INVALID_FENCE
constexpr SemaphoreID INVALID_SEMAPHORE
constexpr CommandBufferID INVALID_COMMAND_BUFFER
Extracted descriptor binding information from shader reflection.
Extracted push constant range from shader reflection.
bool enable_reflection
Extract descriptor bindings and metadata.
std::vector< std::string > include_directories
Paths for #include resolution.
std::unordered_map< std::string, std::string > defines
Preprocessor macros.
Configuration for shader compilation.
vk::DescriptorSet descriptor_set
std::shared_ptr< Core::VKShaderModule > std::string filepath
std::optional< std::array< uint32_t, 3 > > workgroup_size
std::vector< PushConstantRangeInfo > push_constant_ranges
std::vector< DescriptorBindingInfo > descriptor_bindings
Extracted reflection information from compiled shader.
std::string content
Shader source code or SPIR-V path.
enum MayaFlux::Portal::Graphics::ShaderSource::SourceType type
@ GLSL_FILE
Path to .comp/.vert/.frag/etc.
@ GLSL_STRING
In-memory GLSL source.
@ SPIRV_FILE
Path to .spv file.
Shader source descriptor for compilation.