MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ShaderFoundry.cpp
Go to the documentation of this file.
1#include "ShaderFoundry.hpp"
2
10
12
14
16 const std::shared_ptr<Core::VulkanBackend>& backend,
17 const ShaderCompilerConfig& config)
18{
19 if (s_initialized) {
21 "ShaderFoundry already initialized (static flag)");
22 return true;
23 }
24
25 if (!backend) {
27 "Cannot initialize ShaderFoundry with null backend");
28 return false;
29 }
30
31 if (m_backend) {
33 "ShaderFoundry already initialized");
34 return true;
35 }
36
37 m_backend = backend;
38 m_config = config;
39
40 m_global_descriptor_manager = std::make_shared<Core::VKDescriptorManager>();
41 m_global_descriptor_manager->initialize(get_device(), 1024);
42
43 m_graphics_queue = m_backend->get_context().get_graphics_queue();
44 m_compute_queue = m_backend->get_context().get_compute_queue();
45 m_transfer_queue = m_backend->get_context().get_transfer_queue();
46
47 s_initialized = true;
48
50 "ShaderFoundry initialized");
51 return true;
52}
53
55{
56 if (!s_initialized) {
57 return;
58 }
59
60 if (!m_backend) {
61 return;
62 }
63
65 "Shutting down ShaderFoundry...");
66
67 auto& cmd_manager = m_backend->get_command_manager();
68 auto device = get_device();
69
70 for (auto& [id, state] : m_command_buffers) {
71 if (state.is_active) {
72 cmd_manager.free_command_buffer(state.cmd);
73 }
74 if (state.timestamp_pool) {
75 device.destroyQueryPool(state.timestamp_pool);
76 }
77 }
78 m_command_buffers.clear();
79
80 for (auto& [id, state] : m_fences) {
81 device.destroyFence(state.fence);
82 }
83 m_fences.clear();
84
85 for (auto& [id, state] : m_semaphores) {
86 device.destroySemaphore(state.semaphore);
87 }
88 m_semaphores.clear();
89
90 m_descriptor_sets.clear();
91
92 m_global_descriptor_manager->cleanup(device);
93 m_shader_cache.clear();
94 m_shaders.clear();
96 m_descriptor_sets.clear();
97
98 m_backend = nullptr;
99
100 s_initialized = false;
101
103 "ShaderFoundry shutdown complete");
104}
105
106//==============================================================================
107// Shader Compilation - Primary API
108//==============================================================================
109
110std::shared_ptr<Core::VKShaderModule> ShaderFoundry::compile_from_file(
111 const std::string& filepath,
112 std::optional<ShaderStage> stage,
113 const std::string& entry_point)
114{
115 if (!is_initialized()) {
117 "ShaderFoundry not initialized");
118 return nullptr;
119 }
120
121 auto it = m_shader_cache.find(filepath);
122 if (it != m_shader_cache.end()) {
124 "Using cached shader: {}", filepath);
125 return it->second;
126 }
127
128 std::optional<vk::ShaderStageFlagBits> vk_stage;
129 if (stage.has_value()) {
130 vk_stage = to_vulkan_stage(*stage);
131 }
132
133 auto shader = create_shader_module();
134
135 if (filepath.ends_with(".spv")) {
136 if (!shader->create_from_spirv_file(
137 get_device(), filepath,
138 vk_stage.value_or(vk::ShaderStageFlagBits::eCompute),
139 entry_point, m_config.enable_reflection)) {
141 "Failed to compile SPIR-V shader: {}", filepath);
142 return nullptr;
143 }
144 } else {
145 if (!shader->create_from_glsl_file(
146 get_device(), filepath, vk_stage, entry_point,
148 m_config.defines)) {
150 "Failed to compile GLSL shader: {}", filepath);
151 return nullptr;
152 }
153 }
154
155 m_shader_cache[filepath] = shader;
157 "Compiled shader: {} ({})", filepath, vk::to_string(shader->get_stage()));
158 return shader;
159}
160
161std::shared_ptr<Core::VKShaderModule> ShaderFoundry::compile_from_source(
162 const std::string& source,
163 ShaderStage stage,
164 const std::string& entry_point)
165{
166 if (!is_initialized()) {
168 "ShaderFoundry not initialized");
169 return nullptr;
170 }
171
172 auto shader = create_shader_module();
173 auto vk_stage = to_vulkan_stage(stage);
174
175 if (!shader->create_from_glsl(
176 get_device(), source, vk_stage, entry_point,
178 m_config.defines)) {
180 "Failed to compile GLSL source");
181 return nullptr;
182 }
183
185 "Compiled shader from source ({})", vk::to_string(vk_stage));
186 return shader;
187}
188
189std::shared_ptr<Core::VKShaderModule> ShaderFoundry::compile_from_source_cached(
190 const std::string& source,
191 ShaderStage stage,
192 const std::string& cache_key,
193 const std::string& entry_point)
194{
195 auto it = m_shader_cache.find(cache_key);
196 if (it != m_shader_cache.end()) {
198 "Using cached shader: {}", cache_key);
199 return it->second;
200 }
201
202 auto shader = compile_from_source(source, stage, entry_point);
203 if (!shader) {
204 return nullptr;
205 }
206
207 m_shader_cache[cache_key] = shader;
208 return shader;
209}
210
211std::shared_ptr<Core::VKShaderModule> ShaderFoundry::compile_from_spirv(
212 const std::string& spirv_path,
213 ShaderStage stage,
214 const std::string& entry_point)
215{
216 if (!is_initialized()) {
218 "ShaderFoundry not initialized");
219 return nullptr;
220 }
221
222 auto it = m_shader_cache.find(spirv_path);
223 if (it != m_shader_cache.end()) {
225 "Using cached SPIR-V shader: {}", spirv_path);
226 return it->second;
227 }
228
229 auto shader = create_shader_module();
230 auto vk_stage = to_vulkan_stage(stage);
231
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);
237 return nullptr;
238 }
239
240 m_shader_cache[spirv_path] = shader;
242 "Loaded SPIR-V shader: {}", spirv_path);
243 return shader;
244}
245
246std::shared_ptr<Core::VKShaderModule> ShaderFoundry::compile(const ShaderSource& shader_source)
247{
248 switch (shader_source.type) {
250 return compile_from_file(shader_source.content, shader_source.stage, shader_source.entry_point);
251
253 return compile_from_source(shader_source.content, shader_source.stage, shader_source.entry_point);
254
256 return compile_from_spirv(shader_source.content, shader_source.stage, shader_source.entry_point);
257
258 default:
260 "Unknown shader source type");
261 return nullptr;
262 }
263}
264
266 const std::string& content,
267 std::optional<ShaderStage> stage,
268 const std::string& entry_point)
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}
349
351{
352 return load_shader(source.content, source.stage, source.entry_point);
353}
354
355std::optional<std::filesystem::path> ShaderFoundry::resolve_shader_path(const std::string& filepath) const
356{
357 namespace fs = std::filesystem;
358
359 fs::path path(filepath);
360
361 if (path.is_absolute() || fs::exists(filepath)) {
362 return path;
363 }
364
365 std::vector<std::string> search_paths = {
366 Core::SHADER_BUILD_OUTPUT_DIR,
367 Core::SHADER_INSTALL_DIR,
368 Core::SHADER_SOURCE_DIR,
369 "./shaders",
370 "../shaders"
371 };
372
373 for (const auto& search_path : search_paths) {
374 fs::path full_path = fs::path(search_path) / filepath;
375 if (fs::exists(full_path)) {
376 return full_path;
377 }
378 }
379
380 return std::nullopt;
381}
382
384{
385 auto resolved_path = resolve_shader_path(content);
386
387 if (resolved_path.has_value()) {
388 std::string ext = resolved_path->extension().string();
389 std::ranges::transform(ext, ext.begin(), ::tolower);
390
391 if (ext == ".spv") {
393 }
394
396 }
397
398 if (content.size() > 1024 || content.find('\n') != std::string::npos) {
400 }
401
403}
404
405std::string ShaderFoundry::generate_source_cache_key(const std::string& source, ShaderStage stage) const
406{
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);
410}
411
412ShaderID ShaderFoundry::reload_shader(const std::string& filepath)
413{
414 invalidate_cache(filepath);
415
416 auto cache_it = m_shader_filepath_cache.find(filepath);
417 if (cache_it != m_shader_filepath_cache.end()) {
418 destroy_shader(cache_it->second);
419 m_shader_filepath_cache.erase(cache_it);
420 }
421
422 return load_shader(filepath);
423}
424
426{
427 auto it = m_shaders.find(shader_id);
428 if (it != m_shaders.end()) {
429 if (!it->second.filepath.empty()) {
430 m_shader_filepath_cache.erase(it->second.filepath);
431 }
432
433 if (!it->second.filepath.empty()) {
434 m_shader_cache.erase(it->second.filepath);
435 }
436
437 m_shaders.erase(it);
438 }
439}
440
441//==============================================================================
442// Shader Introspection
443//==============================================================================
444
446{
447 auto it = m_shaders.find(shader_id);
448 if (it == m_shaders.end()) {
449 return {};
450 }
451
452 const auto& reflection = it->second.module->get_reflection();
453
455 info.stage = it->second.stage;
456 info.entry_point = it->second.entry_point;
457 info.workgroup_size = reflection.workgroup_size;
458
459 for (const auto& binding : reflection.bindings) {
460 DescriptorBindingInfo binding_info;
461 binding_info.set = binding.set;
462 binding_info.binding = binding.binding;
463 binding_info.type = binding.type;
464 binding_info.name = binding.name;
465 info.descriptor_bindings.push_back(binding_info);
466 }
467
468 for (const auto& pc : reflection.push_constants) {
469 PushConstantRangeInfo pc_info {};
470 pc_info.offset = pc.offset;
471 pc_info.size = pc.size;
472 info.push_constant_ranges.push_back(pc_info);
473 }
474
475 return info;
476}
477
479{
480 auto it = m_shaders.find(shader_id);
481 if (it != m_shaders.end()) {
482 return it->second.stage;
483 }
485}
486
488{
489 auto it = m_shaders.find(shader_id);
490 if (it != m_shaders.end()) {
491 return it->second.entry_point;
492 }
493 return "main";
494}
495
496bool ShaderFoundry::is_cached(const std::string& cache_key) const
497{
498 return m_shader_cache.find(cache_key) != m_shader_cache.end();
499}
500
501std::vector<std::string> ShaderFoundry::get_cached_keys() const
502{
503 std::vector<std::string> keys;
504 keys.reserve(m_shader_cache.size());
505 for (const auto& [key, _] : m_shader_cache) {
506 keys.push_back(key);
507 }
508 return keys;
509}
510
511//==============================================================================
512// Hot-Reload Support
513//==============================================================================
514
515void ShaderFoundry::invalidate_cache(const std::string& cache_key)
516{
517 auto it = m_shader_cache.find(cache_key);
518 if (it != m_shader_cache.end()) {
519 m_shader_cache.erase(it);
521 "Invalidated shader cache: {}", cache_key);
522 }
523}
524
531
532std::shared_ptr<Core::VKShaderModule> ShaderFoundry::hot_reload(const std::string& filepath)
533{
534 invalidate_cache(filepath);
535 return compile_from_file(filepath);
536}
537
538//==============================================================================
539// Configuration
540//==============================================================================
541
543{
544 m_config = config;
546 "Updated shader compiler configuration");
547}
548
549void ShaderFoundry::add_include_directory(const std::string& directory)
550{
551 m_config.include_directories.push_back(directory);
552}
553
554void ShaderFoundry::add_define(const std::string& name, const std::string& value)
555{
556 m_config.defines[name] = value;
557}
558
559//==============================================================================
560// Descriptor Management
561//==============================================================================
562
564{
566
568 state.descriptor_set = m_global_descriptor_manager->allocate_set(get_device(), layout);
569
570 return id;
571}
572
574 DescriptorSetID descriptor_set_id,
575 uint32_t binding,
576 vk::DescriptorType type,
577 vk::Buffer buffer,
578 size_t offset,
579 size_t size)
580{
581 auto it = m_descriptor_sets.find(descriptor_set_id);
582 if (it == m_descriptor_sets.end()) {
583 return;
584 }
585
586 vk::DescriptorBufferInfo buffer_info;
587 buffer_info.buffer = buffer;
588 buffer_info.offset = offset;
589 buffer_info.range = size;
590
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;
598
599 get_device().updateDescriptorSets(1, &write, 0, nullptr);
600}
601
603 DescriptorSetID descriptor_set_id,
604 uint32_t binding,
605 vk::ImageView image_view,
606 vk::Sampler sampler,
607 vk::ImageLayout layout)
608{
609 auto it = m_descriptor_sets.find(descriptor_set_id);
610 if (it == m_descriptor_sets.end()) {
611 return;
612 }
613
614 vk::DescriptorImageInfo image_info;
615 image_info.imageView = image_view;
616 image_info.sampler = sampler;
617 image_info.imageLayout = layout;
618
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;
626
627 get_device().updateDescriptorSets(1, &write, 0, nullptr);
628}
629
631 DescriptorSetID descriptor_set_id,
632 uint32_t binding,
633 vk::ImageView image_view,
634 vk::ImageLayout layout)
635{
636 auto it = m_descriptor_sets.find(descriptor_set_id);
637 if (it == m_descriptor_sets.end()) {
638 return;
639 }
640
641 vk::DescriptorImageInfo image_info;
642 image_info.imageView = image_view;
643 image_info.imageLayout = layout;
644
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;
652
653 get_device().updateDescriptorSets(1, &write, 0, nullptr);
654}
655
656vk::DescriptorSet ShaderFoundry::get_descriptor_set(DescriptorSetID descriptor_set_id)
657{
658 auto it = m_descriptor_sets.find(descriptor_set_id);
659 if (it == m_descriptor_sets.end()) {
660 error<std::invalid_argument>(
663 std::source_location::current(),
664 "Invalid DescriptorSetID: {}", descriptor_set_id);
665 }
666 return it->second.descriptor_set;
667}
668
669//==============================================================================
670// Command Recording
671//==============================================================================
672
674{
675 auto& cmd_manager = m_backend->get_command_manager();
676
678
680 state.cmd = cmd_manager.begin_single_time_commands();
681 state.type = type;
682 state.is_active = true;
683
684 return id;
685}
686
688{
689 auto it = m_command_buffers.find(cmd_id);
690 if (it != m_command_buffers.end()) {
691 return it->second.cmd;
692 }
693 return nullptr;
694}
695
696//==============================================================================
697// Synchronization
698//==============================================================================
699
701{
702 auto it = m_command_buffers.find(cmd_id);
703 if (it == m_command_buffers.end() || !it->second.is_active) {
704 return;
705 }
706
707 auto& cmd_manager = m_backend->get_command_manager();
708
709 it->second.cmd.end();
710
711 vk::SubmitInfo submit_info;
712 submit_info.commandBufferCount = 1;
713 submit_info.pCommandBuffers = &it->second.cmd;
714
715 vk::Queue queue;
716 switch (it->second.type) {
718 queue = m_graphics_queue;
719 break;
721 queue = m_compute_queue;
722 break;
724 queue = m_transfer_queue;
725 break;
726 }
727
728 if (queue.submit(1, &submit_info, nullptr) != vk::Result::eSuccess) {
730 "Failed to submit command buffer");
731 return;
732 }
733 queue.waitIdle();
734
735 cmd_manager.free_command_buffer(it->second.cmd);
736
737 it->second.is_active = false;
738 m_command_buffers.erase(it);
739}
740
742{
743 auto cmd_it = m_command_buffers.find(cmd_id);
744 if (cmd_it == m_command_buffers.end() || !cmd_it->second.is_active) {
745 return INVALID_FENCE;
746 }
747
748 cmd_it->second.cmd.end();
749
750 FenceID fence_id = m_next_fence_id++;
751
752 vk::FenceCreateInfo fence_info;
753 FenceState& fence_state = m_fences[fence_id];
754 fence_state.fence = get_device().createFence(fence_info);
755 fence_state.signaled = false;
756
757 vk::SubmitInfo submit_info;
758 submit_info.commandBufferCount = 1;
759 submit_info.pCommandBuffers = &cmd_it->second.cmd;
760
761 vk::Queue queue;
762 switch (cmd_it->second.type) {
764 queue = m_graphics_queue;
765 break;
767 queue = m_compute_queue;
768 break;
770 queue = m_transfer_queue;
771 break;
772 }
773
774 if (queue.submit(1, &submit_info, fence_state.fence) != vk::Result::eSuccess) {
776 "Failed to submit command buffer");
777 return INVALID_FENCE;
778 }
779
780 cmd_it->second.is_active = false;
781
782 return fence_id;
783}
784
786{
787 auto cmd_it = m_command_buffers.find(cmd_id);
788 if (cmd_it == m_command_buffers.end() || !cmd_it->second.is_active) {
789 return INVALID_SEMAPHORE;
790 }
791
792 cmd_it->second.cmd.end();
793
794 SemaphoreID semaphore_id = m_next_semaphore_id++;
795
796 vk::SemaphoreCreateInfo semaphore_info;
797 SemaphoreState& semaphore_state = m_semaphores[semaphore_id];
798 semaphore_state.semaphore = get_device().createSemaphore(semaphore_info);
799
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;
805
806 vk::Queue queue;
807 switch (cmd_it->second.type) {
809 queue = m_graphics_queue;
810 break;
812 queue = m_compute_queue;
813 break;
815 queue = m_transfer_queue;
816 break;
817 }
818
819 if (queue.submit(1, &submit_info, nullptr) != vk::Result::eSuccess) {
821 "Failed to submit command buffer");
822 return INVALID_SEMAPHORE;
823 }
824
825 cmd_it->second.is_active = false;
826
827 return semaphore_id;
828}
829
831{
832 auto it = m_fences.find(fence_id);
833 if (it == m_fences.end()) {
834 return;
835 }
836
837 if (get_device().waitForFences(1, &it->second.fence, VK_TRUE, UINT64_MAX) != vk::Result::eSuccess) {
839 "Failed to wait for fence: {}", fence_id);
840 return;
841 }
842 it->second.signaled = true;
843}
844
845void ShaderFoundry::wait_for_fences(const std::vector<FenceID>& fence_ids)
846{
847 std::vector<vk::Fence> fences;
848 for (auto fence_id : fence_ids) {
849 auto it = m_fences.find(fence_id);
850 if (it != m_fences.end()) {
851 fences.push_back(it->second.fence);
852 }
853 }
854
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");
859 return;
860 }
861 }
862
863 for (auto fence_id : fence_ids) {
864 auto it = m_fences.find(fence_id);
865 if (it != m_fences.end()) {
866 it->second.signaled = true;
867 }
868 }
869}
870
872{
873 auto it = m_fences.find(fence_id);
874 if (it == m_fences.end()) {
875 return false;
876 }
877
878 if (it->second.signaled) {
879 return true;
880 }
881
882 auto result = get_device().getFenceStatus(it->second.fence);
883 it->second.signaled = (result == vk::Result::eSuccess);
884 return it->second.signaled;
885}
886
889 SemaphoreID wait_semaphore,
890 vk::PipelineStageFlags /*wait_stage*/)
891{
892 auto sem_it = m_semaphores.find(wait_semaphore);
893 if (sem_it == m_semaphores.end()) {
895 }
896
897 return begin_commands(type);
898}
899
901{
902 auto it = m_semaphores.find(semaphore_id);
903 if (it != m_semaphores.end()) {
904 return it->second.semaphore;
905 }
906 return nullptr;
907}
908
909//==============================================================================
910// Memory Barriers
911//==============================================================================
912
914 CommandBufferID cmd_id,
915 vk::Buffer buffer,
916 vk::AccessFlags src_access,
917 vk::AccessFlags dst_access,
918 vk::PipelineStageFlags src_stage,
919 vk::PipelineStageFlags dst_stage)
920{
921 auto it = m_command_buffers.find(cmd_id);
922 if (it == m_command_buffers.end()) {
923 return;
924 }
925
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;
932 barrier.offset = 0;
933 barrier.size = VK_WHOLE_SIZE;
934
935 it->second.cmd.pipelineBarrier(
936 src_stage,
937 dst_stage,
938 vk::DependencyFlags {},
939 0, nullptr,
940 1, &barrier,
941 0, nullptr);
942}
943
945 CommandBufferID cmd_id,
946 vk::Image image,
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)
953{
954 auto it = m_command_buffers.find(cmd_id);
955 if (it == m_command_buffers.end()) {
956 return;
957 }
958
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;
972
973 it->second.cmd.pipelineBarrier(
974 src_stage,
975 dst_stage,
976 vk::DependencyFlags {},
977 0, nullptr,
978 0, nullptr,
979 1, &barrier);
980}
981
982//==============================================================================
983// Queue Management
984//==============================================================================
985
989
990//==============================================================================
991// Profiling
992//==============================================================================
993
994void ShaderFoundry::begin_timestamp(CommandBufferID cmd_id, const std::string& label)
995{
996 auto it = m_command_buffers.find(cmd_id);
997 if (it == m_command_buffers.end()) {
998 return;
999 }
1000
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);
1006 }
1007
1008 auto query_index = static_cast<uint32_t>(it->second.timestamp_queries.size() * 2);
1009 it->second.timestamp_queries[label] = query_index;
1010
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);
1013}
1014
1015void ShaderFoundry::end_timestamp(CommandBufferID cmd_id, const std::string& label)
1016{
1017 auto it = m_command_buffers.find(cmd_id);
1018 if (it == m_command_buffers.end()) {
1019 return;
1020 }
1021
1022 auto query_it = it->second.timestamp_queries.find(label);
1023 if (query_it == it->second.timestamp_queries.end()) {
1024 return;
1025 }
1026
1027 uint32_t query_index = query_it->second;
1028 it->second.cmd.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, it->second.timestamp_pool, query_index + 1);
1029}
1030
1032{
1033 auto it = m_command_buffers.find(cmd_id);
1034 if (it == m_command_buffers.end()) {
1035 return { .label = label, .duration_ns = 0, .valid = false };
1036 }
1037
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 };
1041 }
1042
1043 if (!it->second.timestamp_pool) {
1044 return { .label = label, .duration_ns = 0, .valid = false };
1045 }
1046
1047 uint32_t query_index = query_it->second;
1048 uint64_t timestamps[2];
1049
1050 auto result = get_device().getQueryPoolResults(
1051 it->second.timestamp_pool,
1052 query_index,
1053 2,
1054 sizeof(timestamps),
1055 timestamps,
1056 sizeof(uint64_t),
1057 vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait);
1058
1059 if (result != vk::Result::eSuccess) {
1060 return { .label = label, .duration_ns = 0, .valid = false };
1061 }
1062
1063 auto props = m_backend->get_context().get_physical_device().getProperties();
1064 float timestamp_period = props.limits.timestampPeriod;
1065
1066 auto duration_ns = static_cast<uint64_t>((timestamps[1] - timestamps[0]) * timestamp_period);
1067
1068 return { .label = label, .duration_ns = duration_ns, .valid = true };
1069}
1070
1071//==============================================================================
1072// Internal Access
1073//==============================================================================
1074
1075std::shared_ptr<Core::VKShaderModule> ShaderFoundry::get_vk_shader_module(ShaderID shader_id)
1076{
1077 auto& foundry = ShaderFoundry::instance();
1078 auto it = foundry.m_shaders.find(shader_id);
1079 if (it != foundry.m_shaders.end()) {
1080 return it->second.module;
1081 }
1082 return nullptr;
1083}
1084
1085//==============================================================================
1086// Utilities
1087//==============================================================================
1088
1089vk::ShaderStageFlagBits ShaderFoundry::to_vulkan_stage(ShaderStage stage)
1090{
1091 switch (stage) {
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;
1104 default:
1105 return vk::ShaderStageFlagBits::eCompute;
1106 }
1107}
1108
1109std::optional<ShaderStage> ShaderFoundry::detect_stage_from_extension(const std::string& filepath)
1110{
1111 auto vk_stage = Core::VKShaderModule::detect_stage_from_extension(filepath);
1112 if (!vk_stage.has_value()) {
1113 return std::nullopt;
1114 }
1115
1116 switch (*vk_stage) {
1117 case vk::ShaderStageFlagBits::eCompute:
1118 return ShaderStage::COMPUTE;
1119 case vk::ShaderStageFlagBits::eVertex:
1120 return ShaderStage::VERTEX;
1121 case vk::ShaderStageFlagBits::eFragment:
1122 return ShaderStage::FRAGMENT;
1123 case vk::ShaderStageFlagBits::eGeometry:
1124 return ShaderStage::GEOMETRY;
1125 case vk::ShaderStageFlagBits::eTessellationControl:
1127 case vk::ShaderStageFlagBits::eTessellationEvaluation:
1129 default:
1130 return std::nullopt;
1131 }
1132}
1133
1134//==============================================================================
1135// Private Helpers
1136//==============================================================================
1137
1138std::shared_ptr<Core::VKShaderModule> ShaderFoundry::create_shader_module()
1139{
1140 return std::make_shared<Core::VKShaderModule>();
1141}
1142
1144{
1145 return m_backend->get_context().get_device();
1146}
1147
1148} // namespace MayaFlux::Portal::Graphics
#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.
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.
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.
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.
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.
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
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.
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.
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.
@ 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.
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
Shader source descriptor for compilation.