19 const std::shared_ptr<VKBuffer>& buffer)
21 auto info_it = buffer_info.find(buffer);
22 if (info_it == buffer_info.end()) {
23 if (buffer->has_vertex_layout()) {
24 auto vertex_layout = buffer->get_vertex_layout();
25 if (vertex_layout.has_value()) {
26 buffer_info[buffer] = { .semantic_layout = vertex_layout.value(), .use_reflection =
false };
27 info_it = buffer_info.find(buffer);
30 if (info_it == buffer_info.end()) {
34 return &info_it->second.semantic_layout;
75 window->register_rendering_buffer(buffer);
141 const std::shared_ptr<Core::VKImage>& texture,
144 if (!texture || !texture->is_initialized() || !texture->get_image_view()) {
146 "Cannot bind null texture to binding {}", binding);
152 sampler = loom.get_default_sampler();
162 [binding](
const auto& pair) {
163 return binding >= pair.second.binding
164 && binding < pair.second.binding + pair.second.count;
168 const uint32_t cfg_set = cfg_it->second.set;
171 uint32_t array_idx = 0;
172 if (cfg_it->second.count > 1 && binding >= cfg_it->second.binding) {
173 array_idx = binding - cfg_it->second.binding;
175 foundry.update_descriptor_image(
177 cfg_it->second.binding,
178 texture->get_image_view(),
180 vk::ImageLayout::eShaderReadOnlyOptimal,
186 foundry.update_descriptor_image(
188 cfg_it->second.binding,
189 texture->get_image_view(),
191 vk::ImageLayout::eShaderReadOnlyOptimal);
198 "Bound texture to binding {}", binding);
202 const std::string& descriptor_name,
203 const std::shared_ptr<Core::VKImage>& texture,
209 "No binding configured for descriptor '{}'", descriptor_name);
213 bind_texture(binding_it->second.binding, texture, sampler);
220 "Vertex shader not loaded");
226 "Fragment shader not loaded");
232 "Target window not set");
260 if (buffer->has_vertex_layout()) {
261 auto vertex_layout = buffer->get_vertex_layout();
262 if (vertex_layout.has_value()) {
264 .semantic_layout = vertex_layout.value(),
265 .use_reflection =
false
274 "initialize_pipeline: layout not yet available, deferring");
283 auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
286 for (
const auto& binding : descriptor_bindings) {
287 unified_bindings[{ binding.set, binding.binding }] = binding;
291 auto key = std::make_pair(binding.set, binding.binding);
292 if (unified_bindings.find(key) == unified_bindings.end()) {
295 .binding = binding.binding,
296 .type = binding.type,
299 .count = binding.count
304 std::map<uint32_t, std::vector<Portal::Graphics::DescriptorBindingInfo>> bindings_by_set;
305 for (
const auto& [key, binding] : unified_bindings) {
306 bindings_by_set[binding.set].push_back(binding);
309 for (
const auto& [set_index, set_bindings] : bindings_by_set) {
313 vk::Format swapchain_format =
static_cast<vk::Format
>(
317 ? vk::Format::eD32Sfloat
318 : vk::Format::eUndefined;
320 m_pipeline_id = flow.create_pipeline(pipeline_config, { swapchain_format }, depth_format);
324 "Failed to create render pipeline");
329 buffer->set_needs_depth_attachment(
true);
342 "Cannot allocate descriptor sets without pipeline");
351 auto vt_layout = flow.get_view_transform_layout(
m_pipeline_id);
357 VKBuffer::Usage::UNIFORM,
361 foundry.update_descriptor_buffer(
364 vk::DescriptorType::eUniformBuffer,
376 if (!tex_binding.texture
377 || !tex_binding.texture->is_initialized()
378 || !tex_binding.texture->get_image_view()) {
383 [binding](
const auto& pair) {
384 return binding >= pair.second.binding
385 && binding < pair.second.binding + pair.second.count;
390 "No config for binding {}", binding);
394 const uint32_t set_index = config_it->second.set;
396 if (set_index == 0) {
399 "Engine descriptor set not allocated for set=0 texture binding {}", binding);
402 foundry.update_descriptor_image(
404 config_it->second.binding,
405 tex_binding.texture->get_image_view(),
407 vk::ImageLayout::eShaderReadOnlyOptimal,
408 binding - config_it->second.binding);
415 "Descriptor set index {} out of range", binding);
419 foundry.update_descriptor_image(
421 config_it->second.binding,
422 tex_binding.texture->get_image_view(),
424 vk::ImageLayout::eShaderReadOnlyOptimal,
425 binding - config_it->second.binding);
429 "Allocated {} descriptor sets and updated {} texture bindings",
442 "RenderProcessor: Set vertex range [offset={}, count={}]",
443 first_vertex, vertex_count);
447 const std::shared_ptr<VKBuffer>& buffer,
451 .semantic_layout = layout,
452 .use_reflection =
false
461 "Target window not set");
470 if (buffer->has_vertex_layout()) {
471 auto vertex_layout = buffer->get_vertex_layout();
472 if (vertex_layout.has_value()) {
474 .semantic_layout = vertex_layout.value(),
475 .use_reflection =
false
492 "VKBuffer has no vertex layout set. Use buffer->set_vertex_layout()");
501 vk::Format color_format =
static_cast<vk::Format
>(
505 ? vk::Format::eD32Sfloat
506 : vk::Format::eUndefined;
508 auto cmd_id = foundry.begin_secondary_commands(color_format, depth_format);
509 auto cmd = foundry.get_command_buffer(cmd_id);
515 auto cmd = foundry.get_command_buffer(cmd_id);
517 vk::Viewport viewport {
519 static_cast<float>(
height),
520 static_cast<float>(
width),
521 -
static_cast<float>(
height),
525 cmd.setViewport(0, 1, &viewport);
527 vk::Rect2D scissor { { 0, 0 }, {
width,
height } };
528 cmd.setScissor(0, 1, &scissor);
533 auto& engine_bindings = buffer->get_engine_context().ssbo_bindings;
534 for (
const auto& binding : engine_bindings) {
535 if (binding.set == 0 && binding.binding == 0) {
537 "Engine SSBO at binding=0 is reserved for ViewTransform UBO");
542 "Engine SSBO binding {} skipped: engine descriptor set not allocated", binding.binding);
545 foundry.update_descriptor_buffer(
549 binding.buffer_info.buffer,
550 binding.buffer_info.offset,
551 binding.buffer_info.range);
554 auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
555 if (!descriptor_bindings.empty()) {
556 for (
const auto& binding : descriptor_bindings) {
560 "Descriptor set index {} out of range or reserved", binding.set);
564 foundry.update_descriptor_buffer(
568 binding.buffer_info.buffer,
569 binding.buffer_info.offset,
570 binding.buffer_info.range);
595 flow.bind_descriptor_sets(
602 flow.bind_descriptor_sets(
609 if (!push_data.empty()) {
616 flow.bind_vertex_buffers(cmd_id, { buffer });
618 uint32_t draw_count = 0;
622 auto current_layout = buffer->get_vertex_layout();
623 if (!current_layout.has_value() || current_layout->vertex_count == 0) {
625 "Vertex layout has zero vertices, skipping draw");
628 draw_count = current_layout->vertex_count;
631 if (buffer->has_index_buffer()) {
632 const auto index_count =
static_cast<uint32_t
>(
633 buffer->get_index_buffer_size() /
sizeof(uint32_t));
634 flow.bind_index_buffer(cmd_id, buffer);
640 foundry.end_commands(cmd_id);
646 "Recorded secondary command buffer {} for window '{}'",
652 auto vk_buffer = std::dynamic_pointer_cast<VKBuffer>(buffer);
657 "RenderProcessor attached to VKBuffer (size: {} bytes, modality: {})",
658 vk_buffer->get_size_bytes(),
659 static_cast<int>(vk_buffer->get_modality()));
661 if (vk_buffer && vk_buffer->has_vertex_layout()) {
662 auto vertex_layout = vk_buffer->get_vertex_layout();
663 if (vertex_layout.has_value()) {
665 "RenderProcessor: Auto-injecting vertex layout "
666 "({} vertices, {} attributes)",
667 vertex_layout->vertex_count,
668 vertex_layout->attributes.size());
671 m_buffer_info[vk_buffer] = { .semantic_layout = vertex_layout.value(), .use_reflection =
false };
676 vk_buffer->set_needs_depth_attachment(
true);
727 "RenderProcessor cleanup complete");
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
#define MF_RT_DEBUG(comp, ctx,...)
void bind_texture(uint32_t binding, const std::shared_ptr< Core::VKImage > &texture, vk::Sampler sampler=nullptr)
Bind a texture to a descriptor binding point.
Portal::Graphics::DescriptorSetID m_view_transform_descriptor_set_id
void set_blend_attachment(const Portal::Graphics::BlendAttachmentConfig &config)
Set blend mode for color attachment.
Portal::Graphics::ShaderID m_fragment_shader_id
std::unordered_map< std::shared_ptr< VKBuffer >, VertexInfo > m_buffer_info
void disable_alpha_blending()
Disable blending on the color attachment, restoring opaque writes.
void set_buffer_vertex_layout(const std::shared_ptr< VKBuffer > &buffer, const Kakshya::VertexLayout &layout)
Override the vertex layout used when building the pipeline for buffer.
bool m_view_transform_active
std::optional< Portal::Graphics::BlendAttachmentConfig > m_blend_attachment
Registry::Service::DisplayService * m_display_service
uint32_t m_instance_count
Portal::Graphics::ShaderID m_geometry_shader_id
void set_tess_eval_shader(const std::string &tess_eval_path)
bool on_before_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer) override
Called before each process callback.
void enable_alpha_blending()
Enable standard alpha blending (src_alpha, one_minus_src_alpha).
std::function< Kinesis::ViewTransform()> m_view_transform_source
void set_tess_control_shader(const std::string &tess_control_path)
Portal::Graphics::RenderPipelineID m_pipeline_id
void enable_depth_test(Portal::Graphics::CompareOp compare_op=Portal::Graphics::CompareOp::LESS)
Enable depth testing for this processor's pipeline.
Portal::Graphics::ShaderID m_tess_control_shader_id
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
void initialize_descriptors(const std::shared_ptr< VKBuffer > &buffer) override
std::optional< Kinesis::ViewTransform > m_view_transform
Portal::Graphics::DepthStencilConfig m_depth_stencil
void initialize_pipeline(const std::shared_ptr< VKBuffer > &buffer) override
std::unordered_map< uint32_t, TextureBinding > m_texture_bindings
void disable_depth_test()
Disable depth testing and depth writes for this processor's pipeline.
void set_geometry_shader(const std::string &geometry_path)
void set_vertex_range(uint32_t first_vertex, uint32_t vertex_count)
Set vertex range for drawing subset of buffer.
Portal::Graphics::PrimitiveTopology m_primitive_topology
void set_view_transform(const Kinesis::ViewTransform &vt)
Set static view transform (evaluated once)
RenderProcessor(const ShaderConfig &config)
void set_fragment_shader(const std::string &fragment_path)
Portal::Graphics::PolygonMode m_polygon_mode
void execute_shader(const std::shared_ptr< VKBuffer > &buffer) override
void set_target_window(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< VKBuffer > &buffer)
void set_alpha_blending(bool enabled)
Toggle standard alpha blending.
Portal::Graphics::CullMode m_cull_mode
const Kakshya::VertexLayout * get_or_cache_vertex_layout(std::unordered_map< std::shared_ptr< VKBuffer >, VertexInfo > &buffer_info, const std::shared_ptr< VKBuffer > &buffer)
std::shared_ptr< Core::Window > m_target_window
Portal::Graphics::ShaderID m_tess_eval_shader_id
std::shared_ptr< VKBuffer > m_view_transform_ubo
void set_view_transform_source(std::function< Kinesis::ViewTransform()> fn)
Set dynamic view transform source (evaluated every frame)
std::optional< uint32_t > resolve_ds_index(uint32_t set) const
Resolve logical descriptor set index to actual index.
Portal::Graphics::ShaderID m_shader_id
virtual void on_descriptors_created()
Called after descriptor sets are created.
bool m_needs_descriptor_rebuild
size_t resolve_push_constant_size(const std::shared_ptr< VKBuffer > &buffer) const
Byte width of this processor's push constant block, extended to cover any fragment staged on the buff...
virtual void update_descriptors(const std::shared_ptr< VKBuffer > &buffer)
virtual void on_pipeline_created(Portal::Graphics::ComputePipelineID pipeline_id)
Called after pipeline is created.
virtual void on_before_descriptors_create()
Called before descriptor sets are created.
bool m_engine_owns_set_zero
Whether the engine reserves set=0 for global resources.
bool m_needs_pipeline_rebuild
std::vector< Portal::Graphics::DescriptorSetID > m_descriptor_set_ids
std::vector< uint8_t > resolve_push_constants(const std::shared_ptr< VKBuffer > &buffer) const
This processor's push constant data with buffer-staged fragments overlaid at their declared offsets.
Abstract base class for shader-based buffer processing.
void ensure_initialized(const std::shared_ptr< VKBuffer > &buffer)
void register_window_for_rendering(const std::shared_ptr< Core::Window > &window)
Register a window for dynamic rendering.
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.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
@ UNKNOWN
Unknown or undefined modality.
constexpr RenderPipelineID INVALID_RENDER_PIPELINE
MAYAFLUX_API TextureLoom & get_texture_manager()
Get the global texture manager instance.
constexpr ShaderID INVALID_SHADER
MAYAFLUX_API RenderFlow & get_render_flow()
Get the global render flow instance.
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
CompareOp
Depth/stencil comparison operation.
constexpr DescriptorSetID INVALID_DESCRIPTOR_SET
std::string shader_path
Path to shader file.
std::unordered_map< std::string, ShaderBinding > bindings
Complete description of vertex data layout in a buffer.
static BlendAttachmentConfig alpha_blend()
Create standard alpha blending configuration.
Per-attachment blend configuration.
CompareOp depth_compare_op
std::vector< std::vector< DescriptorBindingInfo > > descriptor_sets
ShaderID tess_eval_shader
Optional.
std::optional< Kakshya::VertexLayout > semantic_vertex_layout
ShaderID tess_control_shader
Optional.
bool use_vertex_shader_reflection
std::vector< BlendAttachmentConfig > blend_attachments
DepthStencilConfig depth_stencil
size_t push_constant_size
PrimitiveTopology topology
ShaderID geometry_shader
Optional.
RasterizationConfig rasterization
Complete render pipeline configuration.
std::function< int(const std::shared_ptr< void > &)> get_swapchain_format
Get actual swapchain format for a window.
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.