MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RenderFlow.cpp
Go to the documentation of this file.
1#include "RenderFlow.hpp"
2
4
7
11
13
15
16bool RenderFlow::s_initialized = false;
17
18namespace {
19
20 /**
21 * @brief Translate semantic VertexLayout to Vulkan bindings/attributes
22 * @param layout Semantic vertex layout
23 * @return Tuple of (bindings, attributes)
24 */
25 static std::pair<
26 std::vector<Core::VertexBinding>,
27 std::vector<Core::VertexAttribute>>
28 translate_semantic_layout(const Kakshya::VertexLayout& layout)
29 {
30 using namespace MayaFlux::Portal::Graphics;
31
32 auto [vk_bindings, vk_attributes] = VertexLayoutTranslator::translate_layout(layout);
33
35 "Translated semantic vertex layout: {} bindings, {} attributes",
36 vk_bindings.size(), vk_attributes.size());
37
38 return { vk_bindings, vk_attributes };
39 }
40
41} // anonymous namespace
42
43//==============================================================================
44// Initialization
45//==============================================================================
46
48{
49 if (s_initialized) {
51 "RenderFlow already initialized (static flag)");
52 return true;
53 }
54
55 if (m_shader_foundry) {
57 "RenderFlow already initialized");
58 return true;
59 }
60
64 "ShaderFoundry must be initialized before RenderFlow");
65 return false;
66 }
67
70
71 if (!m_display_service) {
73 "DisplayService not found in BackendRegistry");
74 return false;
75 }
76
77 s_initialized = true;
78
80 "RenderFlow initialized");
81 return true;
82}
83
85{
86 if (!s_initialized) {
87 return;
88 }
89
91 "RenderFlow stopped");
92}
93
95{
96 if (!s_initialized) {
97 return;
98 }
99
101 "Shutting down RenderFlow...");
102
105 "Cannot shutdown RenderFlow: ShaderFoundry not initialized");
106 return;
107 }
108
109 auto device = m_shader_foundry->get_device();
110
111 if (!device) {
113 "Cannot shutdown RenderFlow: Vulkan device is null");
114 return;
115 }
116
118
119 m_window_associations.clear();
120
121 m_shader_foundry = nullptr;
122 m_display_service = nullptr;
123
124 s_initialized = false;
125
127 "RenderFlow shutdown complete");
128}
129
130//==============================================================================
131// Utility Conversions (Portal enums → Vulkan enums)
132//==============================================================================
133
134namespace {
135
136 vk::PrimitiveTopology to_vk_topology(PrimitiveTopology topology)
137 {
138 switch (topology) {
140 return vk::PrimitiveTopology::ePointList;
142 return vk::PrimitiveTopology::eLineList;
144 return vk::PrimitiveTopology::eLineStrip;
146 return vk::PrimitiveTopology::eTriangleList;
148 return vk::PrimitiveTopology::eTriangleStrip;
150 return vk::PrimitiveTopology::eTriangleFan;
151 default:
152 return vk::PrimitiveTopology::eTriangleList;
153 }
154 }
155
156 vk::PolygonMode to_vk_polygon_mode(PolygonMode mode)
157 {
158 switch (mode) {
160 return vk::PolygonMode::eFill;
162 return vk::PolygonMode::eLine;
164 return vk::PolygonMode::ePoint;
165 default:
166 return vk::PolygonMode::eFill;
167 }
168 }
169
170 vk::CullModeFlags to_vk_cull_mode(CullMode mode)
171 {
172 switch (mode) {
173 case CullMode::NONE:
174 return vk::CullModeFlagBits::eNone;
175 case CullMode::FRONT:
176 return vk::CullModeFlagBits::eFront;
177 case CullMode::BACK:
178 return vk::CullModeFlagBits::eBack;
180 return vk::CullModeFlagBits::eFrontAndBack;
181 default:
182 return vk::CullModeFlagBits::eBack;
183 }
184 }
185
186 vk::CompareOp to_vk_compare_op(CompareOp op)
187 {
188 switch (op) {
189 case CompareOp::NEVER:
190 return vk::CompareOp::eNever;
191 case CompareOp::LESS:
192 return vk::CompareOp::eLess;
193 case CompareOp::EQUAL:
194 return vk::CompareOp::eEqual;
196 return vk::CompareOp::eLessOrEqual;
198 return vk::CompareOp::eGreater;
200 return vk::CompareOp::eNotEqual;
202 return vk::CompareOp::eGreaterOrEqual;
204 return vk::CompareOp::eAlways;
205 default:
206 return vk::CompareOp::eLess;
207 }
208 }
209
210 vk::BlendFactor to_vk_blend_factor(BlendFactor factor)
211 {
212 switch (factor) {
214 return vk::BlendFactor::eZero;
215 case BlendFactor::ONE:
216 return vk::BlendFactor::eOne;
218 return vk::BlendFactor::eSrcColor;
220 return vk::BlendFactor::eOneMinusSrcColor;
222 return vk::BlendFactor::eDstColor;
224 return vk::BlendFactor::eOneMinusDstColor;
226 return vk::BlendFactor::eSrcAlpha;
228 return vk::BlendFactor::eOneMinusSrcAlpha;
230 return vk::BlendFactor::eDstAlpha;
232 return vk::BlendFactor::eOneMinusDstAlpha;
233 default:
234 return vk::BlendFactor::eOne;
235 }
236 }
237
238 vk::BlendOp to_vk_blend_op(BlendOp op)
239 {
240 switch (op) {
241 case BlendOp::ADD:
242 return vk::BlendOp::eAdd;
244 return vk::BlendOp::eSubtract;
246 return vk::BlendOp::eReverseSubtract;
247 case BlendOp::MIN:
248 return vk::BlendOp::eMin;
249 case BlendOp::MAX:
250 return vk::BlendOp::eMax;
251 default:
252 return vk::BlendOp::eAdd;
253 }
254 }
255} // anonymous namespace
256
257//==============================================================================
258// Pipeline Creation
259//==============================================================================
260
262 const RenderPipelineConfig& config,
263 const std::vector<vk::Format>& color_formats,
264 vk::Format depth_format)
265{
266 if (!is_initialized()) {
268 "RenderFlow not initialized");
270 }
271
272 if (config.vertex_shader == INVALID_SHADER) {
274 "Vertex shader required for graphics pipeline");
276 }
277
278 if (color_formats.empty()) {
280 "At least one color format required for dynamic rendering pipeline");
282 }
283
285
287 if (config.fragment_shader != INVALID_SHADER) {
289 }
290 if (config.geometry_shader != INVALID_SHADER) {
292 }
293 if (config.tess_control_shader != INVALID_SHADER) {
295 }
296 if (config.tess_eval_shader != INVALID_SHADER) {
298 }
299
300 if (config.semantic_vertex_layout.has_value()) {
302 "Pipeline using semantic VertexLayout ({} vertices, {} attributes)",
303 config.semantic_vertex_layout->vertex_count,
304 config.semantic_vertex_layout->attributes.size());
305
306 auto [vk_bindings, vk_attributes] = translate_semantic_layout(
307 config.semantic_vertex_layout.value());
308
309 vk_config.vertex_bindings = vk_bindings;
310 vk_config.vertex_attributes = vk_attributes;
311 vk_config.use_vertex_shader_reflection = false;
312
313 } else if (!config.vertex_bindings.empty() || !config.vertex_attributes.empty()) {
315 "Pipeline using explicit vertex config ({} bindings, {} attributes)",
316 config.vertex_bindings.size(), config.vertex_attributes.size());
317
318 for (const auto& binding : config.vertex_bindings) {
319 Core::VertexBinding vk_binding {};
320 vk_binding.binding = binding.binding;
321 vk_binding.stride = binding.stride;
322 vk_binding.input_rate = binding.per_instance ? vk::VertexInputRate::eInstance : vk::VertexInputRate::eVertex;
323 vk_config.vertex_bindings.push_back(vk_binding);
324 }
325
326 for (const auto& attr : config.vertex_attributes) {
327 Core::VertexAttribute vk_attr {};
328 vk_attr.location = attr.location;
329 vk_attr.binding = attr.binding;
330 vk_attr.format = attr.format;
331 vk_attr.offset = attr.offset;
332 vk_config.vertex_attributes.push_back(vk_attr);
333 }
334
335 vk_config.use_vertex_shader_reflection = false;
336 } else {
338 "Pipeline will use shader reflection for vertex input");
340 }
341
342 vk_config.topology = to_vk_topology(config.topology);
343 vk_config.primitive_restart_enable = false;
344
345 vk_config.polygon_mode = to_vk_polygon_mode(config.rasterization.polygon_mode);
346 vk_config.cull_mode = to_vk_cull_mode(config.rasterization.cull_mode);
347 vk_config.front_face = config.rasterization.front_face_ccw ? vk::FrontFace::eCounterClockwise : vk::FrontFace::eClockwise;
348 vk_config.line_width = config.rasterization.line_width;
350 vk_config.depth_bias_enable = config.rasterization.depth_bias;
351
354 vk_config.depth_compare_op = to_vk_compare_op(config.depth_stencil.depth_compare_op);
356
357 for (const auto& blend : config.blend_attachments) {
359 vk_blend.blend_enable = blend.blend_enable;
360 vk_blend.src_color_blend_factor = to_vk_blend_factor(blend.src_color_factor);
361 vk_blend.dst_color_blend_factor = to_vk_blend_factor(blend.dst_color_factor);
362 vk_blend.color_blend_op = to_vk_blend_op(blend.color_blend_op);
363 vk_blend.src_alpha_blend_factor = to_vk_blend_factor(blend.src_alpha_factor);
364 vk_blend.dst_alpha_blend_factor = to_vk_blend_factor(blend.dst_alpha_factor);
365 vk_blend.alpha_blend_op = to_vk_blend_op(blend.alpha_blend_op);
366 vk_config.color_blend_attachments.push_back(vk_blend);
367 }
368
369 std::vector<vk::DescriptorSetLayout> layouts;
370 for (const auto& desc_set : config.descriptor_sets) {
371 std::vector<vk::DescriptorSetLayoutBinding> bindings;
372 for (const auto& binding : desc_set) {
373 vk::DescriptorSetLayoutBinding vk_binding;
374 vk_binding.binding = binding.binding;
375 vk_binding.descriptorType = binding.type;
376 vk_binding.descriptorCount = 1;
377 vk_binding.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment;
378 bindings.push_back(vk_binding);
379 }
380
381 vk::DescriptorSetLayoutCreateInfo layout_info;
382 layout_info.bindingCount = static_cast<uint32_t>(bindings.size());
383 layout_info.pBindings = bindings.data();
384
385 auto layout = m_shader_foundry->get_device().createDescriptorSetLayout(layout_info);
386 layouts.push_back(layout);
387 }
388 vk_config.descriptor_set_layouts = layouts;
389
390 if (config.push_constant_size > 0) {
391 vk::PushConstantRange range;
392 range.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment;
393 range.offset = 0;
394 range.size = static_cast<uint32_t>(config.push_constant_size);
395 vk_config.push_constant_ranges.push_back(range);
396 }
397
398 vk_config.color_attachment_formats = color_formats;
399 vk_config.depth_attachment_format = depth_format;
400
401 vk_config.dynamic_states = {
402 vk::DynamicState::eViewport,
403 vk::DynamicState::eScissor
404 };
405
406 auto pipeline = std::make_shared<Core::VKGraphicsPipeline>();
407 if (!pipeline->create(m_shader_foundry->get_device(), vk_config)) {
409 "Failed to create VKGraphicsPipeline for dynamic rendering");
410
411 for (auto layout : layouts) {
412 m_shader_foundry->get_device().destroyDescriptorSetLayout(layout);
413 }
415 }
416
417 auto pipeline_id = m_next_pipeline_id.fetch_add(1);
418 PipelineState state;
419 state.shader_ids = { config.vertex_shader, config.fragment_shader };
420 state.pipeline = pipeline;
421 state.layouts = layouts;
422 state.layout = pipeline->get_layout();
423 m_pipelines[pipeline_id] = std::move(state);
424
426 "Dynamic rendering pipeline created (ID: {}, {} color attachments)",
427 pipeline_id, color_formats.size());
428
429 return pipeline_id;
430}
431
433{
434 auto it = m_pipelines.find(pipeline_id);
435 if (it == m_pipelines.end()) {
436 return;
437 }
438
439 auto device = m_shader_foundry->get_device();
440
441 if (it->second.pipeline) {
442 it->second.pipeline->cleanup(device);
443 }
444
445 if (it->second.layout) {
446 device.destroyPipelineLayout(it->second.layout);
447 }
448
449 for (auto layout : it->second.layouts) {
450 if (layout) {
451 device.destroyDescriptorSetLayout(layout);
452 }
453 }
454
455 m_pipelines.erase(it);
456
458 "Destroyed graphics pipeline (ID: {})", pipeline_id);
459}
460
462{
463 auto device = m_shader_foundry->get_device();
464
465 for (auto& [id, state] : m_pipelines) {
466 if (state.pipeline) {
467 state.pipeline->cleanup(device);
468 }
469
470 for (auto layout : state.layouts) {
471 if (layout) {
472 device.destroyDescriptorSetLayout(layout);
473 }
474 }
475 }
476 m_pipelines.clear();
477
479 "Cleaned up all graphics pipelines");
480}
481
482//==============================================================================
483// Dynamic Rendering
484//==============================================================================
485
487 CommandBufferID cmd_id,
488 const std::shared_ptr<Core::Window>& window,
489 vk::Image swapchain_image,
490 const std::array<float, 4>& clear_color)
491{
492 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
493 if (!cmd) {
495 "Invalid command buffer ID: {}", cmd_id);
496 return;
497 }
498
499 if (!window) {
501 "Cannot begin rendering for null window");
502 return;
503 }
504
505 if (!swapchain_image) {
507 "Cannot begin rendering with null swapchain image");
508 return;
509 }
510
511 auto it = m_window_associations.find(window);
512 if (it == m_window_associations.end()) {
514 "Window '{}' not registered for rendering. "
515 "Call register_window_for_rendering() first.",
516 window->get_create_info().title);
517 m_window_associations.emplace(window, WindowRenderAssociation { .window = window, .swapchain_image = swapchain_image });
518 } else {
519 it->second.swapchain_image = swapchain_image;
520 }
521
522 uint32_t width = 0, height = 0;
523 m_display_service->get_swapchain_extent(window, width, height);
524
525 if (width == 0 || height == 0) {
527 "Invalid swapchain extent for window '{}': {}x{}",
528 window->get_create_info().title, width, height);
529 return;
530 }
531
532 vk::ImageView image_view = get_current_image_view(window);
533 if (!image_view) {
535 "Failed to get image view for window '{}'",
536 window->get_create_info().title);
537 return;
538 }
539
540 vk::ImageMemoryBarrier pre_barrier {};
541 pre_barrier.oldLayout = vk::ImageLayout::eUndefined;
542 pre_barrier.newLayout = vk::ImageLayout::eColorAttachmentOptimal;
543 pre_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
544 pre_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
545 pre_barrier.image = swapchain_image;
546 pre_barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
547 pre_barrier.subresourceRange.baseMipLevel = 0;
548 pre_barrier.subresourceRange.levelCount = 1;
549 pre_barrier.subresourceRange.baseArrayLayer = 0;
550 pre_barrier.subresourceRange.layerCount = 1;
551 pre_barrier.srcAccessMask = vk::AccessFlagBits::eNone;
552 pre_barrier.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;
553
554 cmd.pipelineBarrier(
555 vk::PipelineStageFlagBits::eTopOfPipe,
556 vk::PipelineStageFlagBits::eColorAttachmentOutput,
557 vk::DependencyFlags {},
558 0, nullptr,
559 0, nullptr,
560 1, &pre_barrier);
561
562 vk::RenderingAttachmentInfo color_attachment {};
563 color_attachment.sType = vk::StructureType::eRenderingAttachmentInfo;
564 color_attachment.pNext = nullptr;
565 color_attachment.imageView = image_view;
566 color_attachment.imageLayout = vk::ImageLayout::eColorAttachmentOptimal;
567 color_attachment.resolveMode = vk::ResolveModeFlagBits::eNone;
568 color_attachment.resolveImageView = nullptr;
569 color_attachment.resolveImageLayout = vk::ImageLayout::eUndefined;
570 color_attachment.loadOp = vk::AttachmentLoadOp::eClear;
571 color_attachment.storeOp = vk::AttachmentStoreOp::eStore;
572 color_attachment.clearValue.color = vk::ClearColorValue(clear_color);
573
574 vk::RenderingInfo rendering_info {};
575 rendering_info.sType = vk::StructureType::eRenderingInfo;
576 rendering_info.pNext = nullptr;
577 rendering_info.flags = vk::RenderingFlagBits::eContentsSecondaryCommandBuffers;
578 rendering_info.renderArea.offset = vk::Offset2D { 0, 0 };
579 rendering_info.renderArea.extent = vk::Extent2D { width, height };
580 rendering_info.layerCount = 1;
581 rendering_info.colorAttachmentCount = 1;
582 rendering_info.pColorAttachments = &color_attachment;
583 rendering_info.pDepthAttachment = nullptr;
584 rendering_info.pStencilAttachment = nullptr;
585
586 cmd.beginRendering(rendering_info);
587
589 "Began dynamic rendering for window '{}' ({}x{})",
590 window->get_create_info().title, width, height);
591}
592
594 CommandBufferID cmd_id,
595 const std::shared_ptr<Core::Window>& window)
596{
597 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
598 if (!cmd) {
600 "Invalid command buffer ID: {}", cmd_id);
601 return;
602 }
603
604 cmd.endRendering();
605
606 auto it = m_window_associations.find(window);
607 if (it == m_window_associations.end() || !it->second.swapchain_image) {
609 "No swapchain image tracked for window '{}'",
610 window->get_create_info().title);
611 return;
612 }
613
614 vk::ImageMemoryBarrier post_barrier {};
615 post_barrier.oldLayout = vk::ImageLayout::eColorAttachmentOptimal;
616 post_barrier.newLayout = vk::ImageLayout::ePresentSrcKHR;
617 post_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
618 post_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
619 post_barrier.image = it->second.swapchain_image;
620 post_barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
621 post_barrier.subresourceRange.baseMipLevel = 0;
622 post_barrier.subresourceRange.levelCount = 1;
623 post_barrier.subresourceRange.baseArrayLayer = 0;
624 post_barrier.subresourceRange.layerCount = 1;
625 post_barrier.srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;
626 post_barrier.dstAccessMask = vk::AccessFlagBits::eNone;
627
628 cmd.pipelineBarrier(
629 vk::PipelineStageFlagBits::eColorAttachmentOutput,
630 vk::PipelineStageFlagBits::eBottomOfPipe,
631 vk::DependencyFlags {},
632 0, nullptr,
633 0, nullptr,
634 1, &post_barrier);
635
636 it->second.swapchain_image = nullptr;
637
639 "Ended dynamic rendering for window '{}'",
640 window->get_create_info().title);
641}
642
643//==============================================================================
644// Command Recording
645//==============================================================================
646
648{
649 auto pipeline_it = m_pipelines.find(pipeline_id);
650 if (pipeline_it == m_pipelines.end()) {
652 "Invalid pipeline ID: {}", pipeline_id);
653 return;
654 }
655
656 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
657 if (!cmd) {
659 "Invalid command buffer ID: {}", cmd_id);
660 return;
661 }
662
663 pipeline_it->second.pipeline->bind(cmd);
664}
665
667 CommandBufferID cmd_id,
668 const std::vector<std::shared_ptr<Buffers::VKBuffer>>& buffers,
669 uint32_t first_binding)
670{
671 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
672 if (!cmd) {
674 "Invalid command buffer ID: {}", cmd_id);
675 return;
676 }
677
678 std::vector<vk::Buffer> vk_buffers;
679 std::vector<vk::DeviceSize> offsets(buffers.size(), 0);
680
681 vk_buffers.reserve(buffers.size());
682 for (const auto& buf : buffers) {
683 vk_buffers.push_back(buf->get_buffer());
684
685 /* void* mapped = buf->get_mapped_ptr();
686 if (mapped) {
687 float* data = reinterpret_cast<float*>(mapped);
688 MF_PRINT(Journal::Component::Portal, Journal::Context::Rendering,
689 "BIND_VERTEX: All vertex data:");
690 for (int v = 0; v < 3; ++v) {
691 int offset = v * 6; // 24 bytes / 4 bytes per float = 6 floats per vertex
692 MF_PRINT(Journal::Component::Portal, Journal::Context::Rendering,
693 " Vertex {}: pos=({}, {}, {}), color=({}, {}, {})",
694 v,
695 data[offset], data[offset + 1], data[offset + 2],
696 data[offset + 3], data[offset + 4], data[offset + 5]);
697 }
698 } else {
699 MF_RT_ERROR(Journal::Component::Portal, Journal::Context::Rendering,
700 "BIND_VERTEX: Buffer not host-mapped!");
701 } */
702 }
703
704 cmd.bindVertexBuffers(first_binding, vk_buffers, offsets);
705}
706
708 CommandBufferID cmd_id,
709 const std::shared_ptr<Buffers::VKBuffer>& buffer,
710 vk::IndexType index_type)
711{
712 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
713 if (!cmd) {
715 "Invalid command buffer ID: {}", cmd_id);
716 return;
717 }
718
719 cmd.bindIndexBuffer(buffer->get_buffer(), 0, index_type);
720}
721
723 CommandBufferID cmd_id,
724 RenderPipelineID pipeline_id,
725 const std::vector<DescriptorSetID>& descriptor_sets)
726{
727 auto pipeline_it = m_pipelines.find(pipeline_id);
728 if (pipeline_it == m_pipelines.end()) {
730 "Invalid pipeline ID: {}", pipeline_id);
731 return;
732 }
733
734 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
735 if (!cmd) {
737 "Invalid command buffer ID: {}", cmd_id);
738 return;
739 }
740
741 std::vector<vk::DescriptorSet> vk_sets;
742 vk_sets.reserve(descriptor_sets.size());
743 for (auto ds_id : descriptor_sets) {
744 vk_sets.push_back(m_shader_foundry->get_descriptor_set(ds_id));
745 }
746
747 cmd.bindDescriptorSets(
748 vk::PipelineBindPoint::eGraphics,
749 pipeline_it->second.layout,
750 0,
751 vk_sets,
752 nullptr);
753}
754
756 CommandBufferID cmd_id,
757 RenderPipelineID pipeline_id,
758 const void* data,
759 size_t size)
760{
761 auto pipeline_it = m_pipelines.find(pipeline_id);
762 if (pipeline_it == m_pipelines.end()) {
764 "Invalid pipeline ID: {}", pipeline_id);
765 return;
766 }
767
768 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
769 if (!cmd) {
771 "Invalid command buffer ID: {}", cmd_id);
772 return;
773 }
774
775 cmd.pushConstants(
776 pipeline_it->second.layout,
777 vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
778 0,
779 static_cast<uint32_t>(size),
780 data);
781}
782
784 CommandBufferID cmd_id,
785 uint32_t vertex_count,
786 uint32_t instance_count,
787 uint32_t first_vertex,
788 uint32_t first_instance)
789{
790 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
791 if (!cmd) {
793 "Invalid command buffer ID: {}", cmd_id);
794 return;
795 }
796
797 cmd.draw(vertex_count, instance_count, first_vertex, first_instance);
798}
799
801 CommandBufferID cmd_id,
802 uint32_t index_count,
803 uint32_t instance_count,
804 uint32_t first_index,
805 int32_t vertex_offset,
806 uint32_t first_instance)
807{
808 auto cmd = m_shader_foundry->get_command_buffer(cmd_id);
809 if (!cmd) {
810 return;
811 }
812
813 cmd.drawIndexed(index_count, instance_count, first_index,
814 vertex_offset, first_instance);
815}
816
817//==========================================================================
818// Window Rendering Registration
819//==========================================================================
820
821void RenderFlow::register_window_for_rendering(const std::shared_ptr<Core::Window>& window)
822{
823 if (!window) {
825 "Cannot register null window");
826 return;
827 }
828
829 if (!window->is_graphics_registered()) {
831 "Window '{}' not registered with graphics backend yet.",
832 window->get_create_info().title);
833 }
834
835 if (!m_window_associations.contains(window)) {
836 WindowRenderAssociation association;
837 association.window = window;
838 m_window_associations[window] = std::move(association);
839
841 "Registered window '{}' for dynamic rendering",
842 window->get_create_info().title);
843 }
844}
845
846void RenderFlow::unregister_window(const std::shared_ptr<Core::Window>& window)
847{
848 if (!window) {
849 return;
850 }
851
852 auto it = m_window_associations.find(window);
853
854 if (it != m_window_associations.end()) {
855 m_window_associations.erase(it);
856
858 "Unregistered window '{}' from rendering",
859 window->get_create_info().title);
860 }
861}
862
863bool RenderFlow::is_window_registered(const std::shared_ptr<Core::Window>& window) const
864{
865 return window && m_window_associations.contains(window);
866}
867
868std::vector<std::shared_ptr<Core::Window>> RenderFlow::get_registered_windows() const
869{
870 std::vector<std::shared_ptr<Core::Window>> windows;
871 windows.reserve(m_window_associations.size());
872
873 for (const auto& [key, association] : m_window_associations) {
874 if (auto window = association.window.lock()) {
875 windows.push_back(window);
876 }
877 }
878
879 return windows;
880}
881
882vk::ImageView RenderFlow::get_current_image_view(const std::shared_ptr<Core::Window>& window)
883{
884 auto view_ptr = m_display_service->get_current_image_view(window);
885 if (!view_ptr) {
886 return nullptr;
887 }
888 return *static_cast<vk::ImageView*>(view_ptr);
889}
890
891//==============================================================================
892// Convenience Methods
893//==============================================================================
894
895std::vector<DescriptorSetID> RenderFlow::allocate_pipeline_descriptors(
896 RenderPipelineID pipeline_id)
897{
898 auto pipeline_it = m_pipelines.find(pipeline_id);
899 if (pipeline_it == m_pipelines.end()) {
901 "Invalid pipeline ID: {}", pipeline_id);
902 return {};
903 }
904
905 std::vector<DescriptorSetID> descriptor_set_ids;
906 for (const auto& layout : pipeline_it->second.layouts) {
907 auto ds_id = m_shader_foundry->allocate_descriptor_set(layout);
908 if (ds_id == INVALID_DESCRIPTOR_SET) {
910 "Failed to allocate descriptor set for pipeline {}", pipeline_id);
911 return {};
912 }
913 descriptor_set_ids.push_back(ds_id);
914 }
915
917 "Allocated {} descriptor sets for pipeline {}",
918 descriptor_set_ids.size(), pipeline_id);
919
920 return descriptor_set_ids;
921}
922
923} // namespace MayaFlux::Portal::Graphics
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_TRACE(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
void bind_pipeline(CommandBufferID cmd_id, RenderPipelineID pipeline)
Bind graphics pipeline.
void register_window_for_rendering(const std::shared_ptr< Core::Window > &window)
Register a window for dynamic rendering.
void push_constants(CommandBufferID cmd_id, RenderPipelineID pipeline, const void *data, size_t size)
Push constants.
std::unordered_map< std::shared_ptr< Core::Window >, WindowRenderAssociation > m_window_associations
vk::ImageView get_current_image_view(const std::shared_ptr< Core::Window > &window)
Get current image view for window.
std::vector< DescriptorSetID > allocate_pipeline_descriptors(RenderPipelineID pipeline)
Allocate descriptor sets for pipeline.
void destroy_pipeline(RenderPipelineID pipeline_id)
Destroy a graphics pipeline.
void begin_rendering(CommandBufferID cmd_id, const std::shared_ptr< Core::Window > &window, vk::Image swapchain_image, const std::array< float, 4 > &clear_color={ 0.0F, 0.0F, 0.0F, 1.0F })
Begin dynamic rendering to a window.
void bind_vertex_buffers(CommandBufferID cmd_id, const std::vector< std::shared_ptr< Buffers::VKBuffer > > &buffers, uint32_t first_binding=0)
Bind vertex buffers.
std::vector< std::shared_ptr< Core::Window > > get_registered_windows() const
Get all registered windows.
void end_rendering(CommandBufferID cmd_id, const std::shared_ptr< Core::Window > &window)
End dynamic rendering.
void draw_indexed(CommandBufferID cmd_id, uint32_t index_count, uint32_t instance_count=1, uint32_t first_index=0, int32_t vertex_offset=0, uint32_t first_instance=0)
Indexed draw command.
std::unordered_map< RenderPipelineID, PipelineState > m_pipelines
void bind_index_buffer(CommandBufferID cmd_id, const std::shared_ptr< Buffers::VKBuffer > &buffer, vk::IndexType index_type=vk::IndexType::eUint32)
Bind index buffer.
void bind_descriptor_sets(CommandBufferID cmd_id, RenderPipelineID pipeline, const std::vector< DescriptorSetID > &descriptor_sets)
Bind descriptor sets.
bool is_window_registered(const std::shared_ptr< Core::Window > &window) const
Check if a window is registered for rendering.
void draw(CommandBufferID cmd_id, uint32_t vertex_count, uint32_t instance_count=1, uint32_t first_vertex=0, uint32_t first_instance=0)
Draw command.
void unregister_window(const std::shared_ptr< Core::Window > &window)
Unregister a window from rendering.
Registry::Service::DisplayService * m_display_service
std::atomic< uint64_t > m_next_pipeline_id
RenderPipelineID create_pipeline(const RenderPipelineConfig &config, const std::vector< vk::Format > &color_formats, vk::Format depth_format=vk::Format::eUndefined)
Create graphics pipeline for dynamic rendering (no render pass object)
vk::DescriptorSet get_descriptor_set(DescriptorSetID descriptor_set_id)
Get Vulkan descriptor set handle from DescriptorSetID.
std::shared_ptr< Core::VKShaderModule > get_vk_shader_module(ShaderID shader_id)
bool is_initialized() const
Check if compiler is initialized.
vk::CommandBuffer get_command_buffer(CommandBufferID cmd_id)
Get Vulkan command buffer handle from CommandBufferID.
DescriptorSetID allocate_descriptor_set(vk::DescriptorSetLayout layout)
Allocate descriptor set for a pipeline.
static std::pair< std::vector< Core::VertexBinding >, std::vector< Core::VertexAttribute > > translate_layout(const Kakshya::VertexLayout &layout, uint32_t binding_index=0)
Translate a semantic vertex layout to Vulkan binding/attribute descriptions.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
@ Rendering
GPU rendering operations (graphics pipeline, frame rendering)
@ Portal
High-level user-facing API layer.
PolygonMode
Rasterization polygon mode.
constexpr RenderPipelineID INVALID_RENDER_PIPELINE
constexpr ShaderID INVALID_SHADER
BlendOp
Blending operation.
PrimitiveTopology
Vertex assembly primitive topology.
CompareOp
Depth/stencil comparison operation.
constexpr DescriptorSetID INVALID_DESCRIPTOR_SET
std::vector< ColorBlendAttachment > color_blend_attachments
std::vector< vk::Format > color_attachment_formats
std::shared_ptr< VKShaderModule > fragment_shader
std::vector< vk::DynamicState > dynamic_states
std::vector< vk::PushConstantRange > push_constant_ranges
std::shared_ptr< VKShaderModule > vertex_shader
std::vector< vk::DescriptorSetLayout > descriptor_set_layouts
std::vector< VertexBinding > vertex_bindings
std::vector< VertexAttribute > vertex_attributes
std::shared_ptr< VKShaderModule > geometry_shader
std::shared_ptr< VKShaderModule > tess_evaluation_shader
std::shared_ptr< VKShaderModule > tess_control_shader
Configuration for creating a graphics pipeline.
std::vector< vk::DescriptorSetLayout > layouts
std::shared_ptr< Core::VKGraphicsPipeline > pipeline
std::vector< std::vector< DescriptorBindingInfo > > descriptor_sets
std::vector< Core::VertexAttribute > vertex_attributes
std::vector< Core::VertexBinding > vertex_bindings
std::optional< Kakshya::VertexLayout > semantic_vertex_layout
std::vector< BlendAttachmentConfig > blend_attachments
Complete render pipeline configuration.
std::function< void *(const std::shared_ptr< void > &)> get_current_image_view
Get current swapchain image view for rendering.
std::function< void(const std::shared_ptr< void > &, uint32_t &, uint32_t &)> get_swapchain_extent
Get swapchain extent for a window.
Backend display and presentation service interface.