17 void append_default_include_directories(ShaderCompilerConfig& config)
19 const std::string defaults[] = {
20 Config::SHADER_SOURCE_DIR,
21 std::string(Config::SHADER_SOURCE_DIR) +
"/include",
22 Config::SHADER_BUILD_OUTPUT_DIR
25 for (
const auto& directory : defaults) {
26 if (std::ranges::find(config.include_directories, directory) == config.include_directories.end()) {
27 config.include_directories.push_back(directory);
35 const std::shared_ptr<Core::VulkanBackend>& backend,
40 "ShaderFoundry already initialized (static flag)");
46 "Cannot initialize ShaderFoundry with null backend");
52 "ShaderFoundry already initialized");
59 append_default_include_directories(
m_config);
71 "ShaderFoundry initialized");
82 "Stopping ShaderFoundry - freeing command buffers...");
91 "ShaderFoundry stopped - command buffers freed");
105 "Shutting down ShaderFoundry...");
109 "{} command buffers still exist during shutdown - freeing now",
126 "ShaderFoundry shutdown complete");
134 const std::string& filepath,
135 std::optional<ShaderStage> stage,
136 const std::string& entry_point)
140 "ShaderFoundry not initialized");
147 "Using cached shader: {}", filepath);
151 std::optional<vk::ShaderStageFlagBits> vk_stage;
152 if (stage.has_value()) {
158 if (filepath.ends_with(
".spv")) {
159 if (!shader->create_from_spirv_file(
161 vk_stage.value_or(vk::ShaderStageFlagBits::eCompute),
164 "Failed to compile SPIR-V shader: {}", filepath);
168 if (!shader->create_from_glsl_file(
169 get_device(), filepath, vk_stage, entry_point,
173 "Failed to compile GLSL shader: {}", filepath);
180 "Compiled shader: {} ({})", filepath, vk::to_string(shader->get_stage()));
185 const std::string& source,
187 const std::string& entry_point)
191 "ShaderFoundry not initialized");
198 if (!shader->create_from_glsl(
203 "Failed to compile GLSL source");
208 "Compiled shader from source ({})", vk::to_string(vk_stage));
213 const std::string& source,
215 const std::string& cache_key,
216 const std::string& entry_point)
221 "Using cached shader: {}", cache_key);
235 const std::string& spirv_path,
237 const std::string& entry_point)
241 "ShaderFoundry not initialized");
248 "Using cached SPIR-V shader: {}", spirv_path);
255 if (!shader->create_from_spirv_file(
256 get_device(), spirv_path, vk_stage, entry_point,
259 "Failed to load SPIR-V shader: {}", spirv_path);
265 "Loaded SPIR-V shader: {}", spirv_path);
270 const std::string& spirv_asm,
272 const std::string& entry_point)
275 if (!shader->create_from_spirv_asm(
279 "Failed to assemble generated SPIR-V");
287 switch (shader_source.
type) {
302 "Unknown shader source type");
308 const std::string& content,
309 std::optional<ShaderStage> stage,
310 const std::string& entry_point)
314 "ShaderFoundry not initialized");
320 std::string cache_key;
330 "Using cached shader ID for: {}", cache_key);
331 return id_it->second;
334 if (!stage.has_value()) {
337 std::filesystem::path p(content);
338 std::string stem = p.stem().string();
345 if (!stage.has_value()) {
347 "Cannot auto-detect shader stage from '{}' - must specify explicitly",
353 std::shared_ptr<Core::VKShaderModule> shader_module;
355 switch (source_type) {
367 "Cannot determine shader source type");
371 if (!shader_module) {
378 state.module = shader_module;
380 state.
stage = *stage;
386 "Shader loaded: {} (ID: {}, stage: {})",
387 cache_key,
id,
static_cast<int>(*stage));
394 return load_shader(source.content, source.stage, source.entry_point);
401 "ShaderFoundry not initialized");
405 if (spec.
kernel.has_value()) {
412 "Using cached kernel shader: {}", key);
413 return kernel_id_it->second;
416 auto module = compile_from_source_cached(glsl, ShaderStage::COMPUTE, key);
419 "Failed to compile kernel shader");
424 state.module =
module;
425 state.filepath = key;
427 state.entry_point =
"main";
430 "Compiled kernel shader (ID: {}, key: {})",
id, key);
440 "Using cached generated shader: {}", key);
441 return id_it->second;
444 auto module = compile_from_spirv_asm(asm_text, ShaderStage::COMPUTE);
447 "Failed to compile generated shader (key: {})", key);
453 state.module =
module;
454 state.filepath = key;
456 state.entry_point =
"main";
461 "Compiled generated shader (ID: {}, key: {})",
id, key);
468 namespace fs = std::filesystem;
470 fs::path path(filepath);
473 if (path.is_absolute() || fs::exists(path, ec)) {
477 std::vector<std::string> user_paths;
479#ifdef MAYAFLUX_PROJECT_SHADER_DIR
480 user_paths.emplace_back(MAYAFLUX_PROJECT_SHADER_DIR);
483 user_paths.insert(user_paths.end(),
484 {
"./shaders",
"./data/shaders",
"../shaders",
"../data/shaders",
"data/shaders" });
486 if (std::string_view(Config::SHADER_EXAMPLE_DIR).
length() > 0) {
487 user_paths.emplace_back(Config::SHADER_EXAMPLE_DIR);
490 const std::vector<std::string> library_paths = {
491 Config::SHADER_BUILD_OUTPUT_DIR,
492 Config::SHADER_INSTALL_DIR,
493 Config::SHADER_SOURCE_DIR
496 for (
const auto& search_path : user_paths) {
497 fs::path full_path = fs::path(search_path) / filepath;
498 if (!fs::exists(full_path, ec)) {
502 for (
const auto& library_path : library_paths) {
503 fs::path shadowed = fs::path(library_path) / filepath;
504 if (fs::exists(shadowed, ec) && !fs::equivalent(full_path, shadowed, ec)) {
506 "Shader '{}' at '{}' shadows library shader '{}'",
507 filepath, full_path.string(), shadowed.string());
514 for (
const auto& search_path : library_paths) {
515 fs::path full_path = fs::path(search_path) / filepath;
516 if (fs::exists(full_path, ec)) {
528 if (resolved_path.has_value()) {
529 std::string ext = resolved_path->extension().string();
530 std::ranges::transform(ext, ext.begin(), ::tolower);
539 if (content.size() > 1024 || content.find(
'\n') != std::string::npos) {
548 std::hash<std::string> hasher;
549 size_t hash = hasher(source + std::to_string(
static_cast<int>(stage)));
550 return "source_" + std::to_string(hash);
570 if (!it->second.filepath.empty()) {
574 if (it->second.module) {
592 const auto& reflection = it->second.module->get_reflection();
595 info.
stage = it->second.stage;
599 for (
const auto& binding : reflection.bindings) {
601 .binding = binding.binding,
602 .type = binding.type,
603 .name = binding.name });
606 for (
const auto& pc : reflection.push_constants) {
608 pc_info.
offset = pc.offset;
609 pc_info.size = pc.size;
618 std::vector<DescriptorBindingInfo> bindings;
625 for (
const auto& binding : it->second.module->get_active_bindings()) {
626 bindings.push_back({ .set = binding.set,
627 .binding = binding.binding,
628 .type = binding.type,
629 .name = binding.name });
639 return it->second.
stage;
648 return it->second.entry_point;
660 std::vector<std::string> keys;
674 shader_module->cleanup(
device);
680 state.module->cleanup(
device);
689 "Cleaned up shader modules");
702 "Invalidated shader cache: {}", cache_key);
710 "Cleared shader cache");
726 append_default_include_directories(
m_config);
728 "Updated shader compiler configuration");
758 vk::DescriptorType
type,
768 vk::DescriptorBufferInfo buffer_info;
769 buffer_info.buffer =
buffer;
770 buffer_info.offset =
offset;
771 buffer_info.range =
size;
773 vk::WriteDescriptorSet write;
774 write.dstSet = it->second.descriptor_set;
775 write.dstBinding = binding;
776 write.dstArrayElement = 0;
777 write.descriptorCount = 1;
778 write.descriptorType =
type;
779 write.pBufferInfo = &buffer_info;
781 get_device().updateDescriptorSets(1, &write, 0,
nullptr);
786 if (writes.empty()) {
790 std::vector<vk::DescriptorBufferInfo> buffer_infos;
791 buffer_infos.reserve(writes.size());
792 std::vector<vk::WriteDescriptorSet> descriptor_writes;
793 descriptor_writes.reserve(writes.size());
795 for (
const auto& w : writes) {
801 buffer_infos.emplace_back(w.buffer, w.offset, w.size);
803 vk::WriteDescriptorSet write;
804 write.dstSet = it->second.descriptor_set;
805 write.dstBinding = w.binding;
806 write.dstArrayElement = 0;
807 write.descriptorCount = 1;
808 write.descriptorType = w.type;
809 write.pBufferInfo = &buffer_infos.back();
810 descriptor_writes.push_back(write);
813 if (!descriptor_writes.empty()) {
815 static_cast<uint32_t
>(descriptor_writes.size()), descriptor_writes.data(), 0,
nullptr);
822 vk::ImageView image_view,
824 vk::ImageLayout layout,
825 uint32_t array_element)
832 vk::DescriptorImageInfo image_info;
833 image_info.imageView = image_view;
834 image_info.sampler = sampler;
835 image_info.imageLayout = layout;
837 vk::WriteDescriptorSet write;
838 write.dstSet = it->second.descriptor_set;
839 write.dstBinding = binding;
840 write.dstArrayElement = array_element;
841 write.descriptorCount = 1;
842 write.descriptorType = vk::DescriptorType::eCombinedImageSampler;
843 write.pImageInfo = &image_info;
845 get_device().updateDescriptorSets(1, &write, 0,
nullptr);
851 vk::ImageView image_view,
852 vk::ImageLayout layout)
859 vk::DescriptorImageInfo image_info;
860 image_info.imageView = image_view;
861 image_info.imageLayout = layout;
863 vk::WriteDescriptorSet write;
864 write.dstSet = it->second.descriptor_set;
865 write.dstBinding = binding;
866 write.dstArrayElement = 0;
867 write.descriptorCount = 1;
868 write.descriptorType = vk::DescriptorType::eStorageImage;
869 write.pImageInfo = &image_info;
871 get_device().updateDescriptorSets(1, &write, 0,
nullptr);
876 if (writes.empty()) {
880 std::vector<vk::DescriptorImageInfo> image_infos;
881 image_infos.reserve(writes.size());
882 std::vector<vk::WriteDescriptorSet> descriptor_writes;
883 descriptor_writes.reserve(writes.size());
885 for (
const auto& w : writes) {
891 vk::DescriptorImageInfo info;
892 info.imageView = w.image_view;
893 info.sampler = w.sampler;
894 info.imageLayout = w.layout;
895 image_infos.push_back(info);
897 vk::WriteDescriptorSet write;
898 write.dstSet = it->second.descriptor_set;
899 write.dstBinding = w.binding;
900 write.dstArrayElement = 0;
901 write.descriptorCount = 1;
902 write.descriptorType = w.type;
903 write.pImageInfo = &image_infos.back();
904 descriptor_writes.push_back(write);
907 if (!descriptor_writes.empty()) {
909 static_cast<uint32_t
>(descriptor_writes.size()), descriptor_writes.data(), 0,
nullptr);
917 error<std::invalid_argument>(
920 std::source_location::current(),
921 "Invalid DescriptorSetID: {}", descriptor_set_id);
923 return it->second.descriptor_set;
938 "Cleaned up descriptor resources");
947 auto& cmd_manager =
m_backend->get_command_manager();
954 ? cmd_manager.begin_single_time_commands_compute()
955 : cmd_manager.begin_single_time_commands();
964 vk::Format color_format,
965 vk::Format depth_format)
967 auto& cmd_manager =
m_backend->get_command_manager();
971 vk::CommandBuffer
cmd = cmd_manager.allocate_command_buffer(vk::CommandBufferLevel::eSecondary);
973 vk::CommandBufferInheritanceRenderingInfo inheritance_rendering;
974 inheritance_rendering.colorAttachmentCount = 1;
975 inheritance_rendering.pColorAttachmentFormats = &color_format;
976 inheritance_rendering.depthAttachmentFormat = depth_format;
977 inheritance_rendering.rasterizationSamples = vk::SampleCountFlagBits::e1;
979 vk::CommandBufferInheritanceInfo inheritance_info;
980 inheritance_info.pNext = &inheritance_rendering;
982 vk::CommandBufferBeginInfo begin_info;
983 begin_info.flags = vk::CommandBufferUsageFlagBits::eRenderPassContinue | vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
984 begin_info.pInheritanceInfo = &inheritance_info;
986 cmd.begin(begin_info);
1001 return it->second.cmd;
1013 it->second.cmd.end();
1014 it->second.is_active =
false;
1024 auto& cmd_manager =
m_backend->get_command_manager();
1029 if (state.is_active) {
1030 cmd_manager.free_command_buffer(state.cmd);
1032 if (state.timestamp_pool) {
1033 device.destroyQueryPool(state.timestamp_pool);
1039 "Freed all command buffers");
1053 auto& cmd_manager =
m_backend->get_command_manager();
1055 it->second.cmd.end();
1057 vk::SubmitInfo submit_info;
1058 submit_info.commandBufferCount = 1;
1059 submit_info.pCommandBuffers = &it->second.cmd;
1062 switch (it->second.type) {
1074 if (queue.submit(1, &submit_info,
nullptr) != vk::Result::eSuccess) {
1076 "Failed to submit command buffer");
1081 if (it->second.timestamp_pool) {
1082 for (
const auto& query : it->second.timestamp_queries) {
1086 get_device().destroyQueryPool(it->second.timestamp_pool);
1087 it->second.timestamp_pool =
nullptr;
1090 cmd_manager.free_command_buffer(it->second.cmd);
1092 it->second.is_active =
false;
1103 cmd_it->second.cmd.end();
1107 vk::FenceCreateInfo fence_info;
1111 fence_state.
cmd_id = cmd_id;
1113 vk::SubmitInfo submit_info;
1114 submit_info.commandBufferCount = 1;
1115 submit_info.pCommandBuffers = &cmd_it->second.cmd;
1118 switch (cmd_it->second.type) {
1130 if (queue.submit(1, &submit_info, fence_state.
fence) != vk::Result::eSuccess) {
1132 "Failed to submit command buffer");
1136 cmd_it->second.is_active =
false;
1148 cmd_it->second.cmd.end();
1152 vk::SemaphoreCreateInfo semaphore_info;
1156 vk::SubmitInfo submit_info;
1157 submit_info.commandBufferCount = 1;
1158 submit_info.pCommandBuffers = &cmd_it->second.cmd;
1159 submit_info.signalSemaphoreCount = 1;
1160 submit_info.pSignalSemaphores = &semaphore_state.
semaphore;
1163 switch (cmd_it->second.type) {
1175 if (queue.submit(1, &submit_info,
nullptr) != vk::Result::eSuccess) {
1177 "Failed to submit command buffer");
1181 cmd_it->second.is_active =
false;
1183 return semaphore_id;
1193 if (
get_device().waitForFences(1, &it->second.fence, VK_TRUE, UINT64_MAX) != vk::Result::eSuccess) {
1195 "Failed to wait for fence: {}", fence_id);
1198 it->second.signaled =
true;
1203 std::vector<vk::Fence> fences;
1204 for (
auto fence_id : fence_ids) {
1207 fences.push_back(it->second.fence);
1211 if (!fences.empty()) {
1212 if (
get_device().waitForFences(
static_cast<uint32_t
>(fences.size()), fences.data(), VK_TRUE, UINT64_MAX) != vk::Result::eSuccess) {
1214 "Failed to wait for fences");
1219 for (
auto fence_id : fence_ids) {
1222 it->second.signaled =
true;
1236 if (it->second.fence) {
1238 it->second.fence =
nullptr;
1247 m_backend->get_command_manager().free_command_buffer(cmd_it->second.cmd);
1260 if (it->second.signaled) {
1264 auto result =
get_device().getFenceStatus(it->second.fence);
1265 it->second.signaled = (result == vk::Result::eSuccess);
1266 return it->second.signaled;
1272 vk::PipelineStageFlags )
1286 return it->second.semaphore;
1295 for (
auto& [
id, state] :
m_fences) {
1297 device.destroyFence(state.fence);
1303 if (state.semaphore) {
1304 device.destroySemaphore(state.semaphore);
1310 "Cleaned up sync objects");
1320 vk::AccessFlags src_access,
1321 vk::AccessFlags dst_access,
1322 vk::PipelineStageFlags src_stage,
1323 vk::PipelineStageFlags dst_stage)
1330 vk::BufferMemoryBarrier barrier;
1331 barrier.srcAccessMask = src_access;
1332 barrier.dstAccessMask = dst_access;
1333 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1334 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1337 barrier.size = VK_WHOLE_SIZE;
1339 it->second.cmd.pipelineBarrier(
1342 vk::DependencyFlags {},
1351 vk::ImageLayout old_layout,
1352 vk::ImageLayout new_layout,
1353 vk::AccessFlags src_access,
1354 vk::AccessFlags dst_access,
1355 vk::PipelineStageFlags src_stage,
1356 vk::PipelineStageFlags dst_stage)
1363 vk::ImageMemoryBarrier barrier;
1364 barrier.srcAccessMask = src_access;
1365 barrier.dstAccessMask = dst_access;
1366 barrier.oldLayout = old_layout;
1367 barrier.newLayout = new_layout;
1368 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1369 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1370 barrier.image =
image;
1371 barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
1372 barrier.subresourceRange.baseMipLevel = 0;
1373 barrier.subresourceRange.levelCount = 1;
1374 barrier.subresourceRange.baseArrayLayer = 0;
1375 barrier.subresourceRange.layerCount = 1;
1377 it->second.cmd.pipelineBarrier(
1380 vk::DependencyFlags {},
1405 if (!it->second.timestamp_pool) {
1406 vk::QueryPoolCreateInfo pool_info;
1407 pool_info.queryType = vk::QueryType::eTimestamp;
1408 pool_info.queryCount = 128;
1409 it->second.timestamp_pool =
get_device().createQueryPool(pool_info);
1412 auto query_index =
static_cast<uint32_t
>(it->second.timestamp_queries.size() * 2);
1413 it->second.timestamp_queries[
label] = query_index;
1415 it->second.cmd.resetQueryPool(it->second.timestamp_pool, query_index, 2);
1416 it->second.cmd.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, it->second.timestamp_pool, query_index);
1426 auto query_it = it->second.timestamp_queries.find(
label);
1427 if (query_it == it->second.timestamp_queries.end()) {
1431 uint32_t query_index = query_it->second;
1432 it->second.cmd.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, it->second.timestamp_pool, query_index + 1);
1444 if (entry.cmd_id == cmd_id && entry.result.label ==
label) {
1445 return entry.result;
1449 return { .label =
label, .duration_ns = 0, .valid =
false };
1456 return { .label =
label, .duration_ns = 0, .valid =
false };
1459 uint32_t query_index = query_it->second;
1460 uint64_t timestamps[2];
1462 vk::QueryResultFlags flags = vk::QueryResultFlagBits::e64;
1464 flags |= vk::QueryResultFlagBits::eWait;
1467 auto result =
get_device().getQueryPoolResults(
1476 if (result != vk::Result::eSuccess) {
1477 return { .label =
label, .duration_ns = 0, .valid =
false };
1480 auto props =
m_backend->get_context().get_physical_device().getProperties();
1481 float timestamp_period = props.limits.timestampPeriod;
1483 auto duration_ns =
static_cast<uint64_t
>((timestamps[1] - timestamps[0]) * timestamp_period);
1485 return { .label =
label, .duration_ns = duration_ns, .valid =
true };
1495 auto it = foundry.m_shaders.find(shader_id);
1496 if (it != foundry.m_shaders.end()) {
1497 return it->second.module;
1510 return vk::ShaderStageFlagBits::eCompute;
1512 return vk::ShaderStageFlagBits::eVertex;
1514 return vk::ShaderStageFlagBits::eFragment;
1516 return vk::ShaderStageFlagBits::eGeometry;
1518 return vk::ShaderStageFlagBits::eTessellationControl;
1520 return vk::ShaderStageFlagBits::eTessellationEvaluation;
1522 return vk::ShaderStageFlagBits::eMeshEXT;
1524 return vk::ShaderStageFlagBits::eTaskEXT;
1526 return vk::ShaderStageFlagBits::eCompute;
1533 if (!vk_stage.has_value()) {
1534 return std::nullopt;
1537 switch (*vk_stage) {
1538 case vk::ShaderStageFlagBits::eCompute:
1540 case vk::ShaderStageFlagBits::eVertex:
1542 case vk::ShaderStageFlagBits::eFragment:
1544 case vk::ShaderStageFlagBits::eGeometry:
1546 case vk::ShaderStageFlagBits::eTessellationControl:
1548 case vk::ShaderStageFlagBits::eTessellationEvaluation:
1550 case vk::ShaderStageFlagBits::eMeshEXT:
1552 case vk::ShaderStageFlagBits::eTaskEXT:
1555 return std::nullopt;
1565 return std::make_shared<Core::VKShaderModule>();
1570 return m_backend->get_context().get_device();
1575 return m_backend->get_context().get_physical_device();
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::shared_ptr< NetworkGeometryBuffer > buffer
vk::PhysicalDevice device
vk::PhysicalDeviceType type
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.
TimestampResult resolve_timestamp(const CommandBufferState &state, const std::string &label, bool wait)
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.
CommandBufferID begin_secondary_commands(vk::Format color_format, vk::Format depth_format=vk::Format::eUndefined)
Begin recording a secondary command buffer for dynamic rendering.
void update_descriptor_buffers(std::span< const DescriptorBufferWrite > writes)
Update several buffer descriptor slots in one Vulkan call.
static std::optional< ShaderStage > detect_stage_from_extension(const std::string &filepath)
Auto-detect shader stage from file extension.
vk::Device get_device() const
Get logical device handle.
static bool s_initialized
void shutdown()
Shutdown and cleanup all ShaderFoundry resources.
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)
void cleanup_sync_objects()
std::shared_ptr< Core::VKShaderModule > hot_reload(const std::string &filepath)
Hot-reload a shader from file.
std::vector< DescriptorBindingInfo > get_active_descriptor_bindings(ShaderID shader_id)
Get the descriptor bindings a shader's entry point statically uses.
void update_descriptor_images(std::span< const DescriptorImageWrite > writes)
Update several image descriptor slots in one Vulkan call.
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
void release_fence(FenceID fence_id)
Destroy the fence and free its associated command buffer.
DetectedSourceType detect_source_type(const std::string &content) const
std::unordered_map< FenceID, FenceState > m_fences
std::shared_ptr< Core::VKShaderModule > create_shader_module()
Memory::HistoryBuffer< FinishedTimestamp > m_finished_timestamps
void cleanup_shader_modules()
void free_all_command_buffers()
Free all allocated command buffers.
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 update_descriptor_image(DescriptorSetID descriptor_set_id, uint32_t binding, vk::ImageView image_view, vk::Sampler sampler, vk::ImageLayout layout=vk::ImageLayout::eShaderReadOnlyOptimal, uint32_t array_element=0)
Update descriptor set with image binding.
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
void stop()
Stop active command recording and free command buffers.
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.
void cleanup_descriptor_resources()
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.
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 end_commands(CommandBufferID cmd_id)
End recording command buffer.
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.
vk::PhysicalDevice get_physical_device() const
Get physical device handle.
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_spirv_asm(const std::string &spirv_asm, 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")
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.
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
ShaderStage
User-friendly shader stage enum.
constexpr FenceID INVALID_FENCE
constexpr SemaphoreID INVALID_SEMAPHORE
constexpr CommandBufferID INVALID_COMMAND_BUFFER
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.
std::unordered_map< std::string, uint32_t > timestamp_queries
vk::QueryPool timestamp_pool
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, SPIR-V path, or SPIR-V assembly text.
enum MayaFlux::Portal::Graphics::ShaderSource::SourceType type
@ GLSL_FILE
Path to .comp/.vert/.frag/etc.
@ GLSL_STRING
In-memory GLSL source.
@ SPIRV_ASM
In-memory SPIR-V assembly text (assembled via SPIRV-Tools, no shaderc)
@ SPIRV_FILE
Path to .spv file.
Shader source descriptor for compilation.
std::optional< KernelSource > kernel
When set, KernelOp is ignored.
Complete declarative description of a generated compute shader.