23 auto root_buf = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
26 "GraphicsBatchProcessor can only process its associated RootGraphicsBuffer");
30 root_buf->cleanup_marked_buffers();
32 for (
auto& ch_buffer : root_buf->get_child_buffers()) {
36 if (ch_buffer->needs_removal()) {
40 if (!ch_buffer->has_data_for_cycle()) {
45 if (ch_buffer->needs_default_processing() && ch_buffer->get_default_processor()) {
46 ch_buffer->process_default();
49 if (
auto chain = ch_buffer->get_processing_chain()) {
50 if (ch_buffer->has_data_for_cycle()) {
51 chain->process_complete(ch_buffer);
55 auto vk_buffer = std::dynamic_pointer_cast<Buffers::VKBuffer>(ch_buffer);
56 if (vk_buffer && vk_buffer->has_render_pipeline()) {
57 for (
const auto& [
id, window] : vk_buffer->get_render_pipelines()) {
64 root_buf->add_renderable_buffer(info);
67 "Registered buffer for rendering to window '{}'",
68 window->get_create_info().title);
72 }
catch (
const std::exception& e) {
74 "Error processing graphics buffer: {}", e.what());
81 auto root_graphics_buffer = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
82 if (!root_graphics_buffer) {
83 error<std::invalid_argument>(
86 std::source_location::current(),
87 "GraphicsBatchProcessor can only be attached to RootGraphicsBuffer");
91 error<std::runtime_error>(
94 std::source_location::current(),
95 "GraphicsBatchProcessor token incompatible with RootGraphicsBuffer requirements");
101 return std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer) !=
nullptr;
105 : m_callback(
std::move(callback))
106 , m_root_buffer(nullptr)
112 : m_callback(nullptr)
113 , m_root_buffer(nullptr)
120 auto root_graphics_buffer = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
121 if (!root_graphics_buffer) {
123 "RenderProcessor received non-RootGraphicsBuffer");
129 "RenderProcessor processing buffer that doesn't match attached root");
136 }
catch (
const std::exception& e) {
140 std::source_location::current(),
141 "RenderProcessor callback threw exception: {}",
151 auto root_graphics_buffer = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
152 if (!root_graphics_buffer) {
153 error<std::invalid_argument>(
156 std::source_location::current(),
157 "RenderProcessor can only be attached to RootGraphicsBuffer");
161 error<std::runtime_error>(
164 std::source_location::current(),
165 "RenderProcessor token incompatible with RootGraphicsBuffer requirements");
171 "RenderProcessor attached to RootGraphicsBuffer (has_callback: {})",
177 if (
auto root = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer)) {
184 "RenderProcessor detached from RootGraphicsBuffer");
189 return std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer) !=
nullptr;
197 "RenderProcessor callback {} (attached: {})",
204 const auto& renderable_buffers = root->get_renderable_buffers();
205 if (renderable_buffers.empty()) {
207 "No renderable buffers found in fallback renderer");
211 std::unordered_map<Core::Window*, std::vector<const RootGraphicsBuffer::RenderableBufferInfo*>> buffers_by_window;
212 for (
const auto& renderable : renderable_buffers) {
213 if (renderable.target_window && renderable.target_window->is_graphics_registered()
215 buffers_by_window[renderable.target_window.get()].push_back(&renderable);
219 if (buffers_by_window.empty()) {
220 root->clear_renderable_buffers();
225 "PresentProcessor submitting to {} windows", buffers_by_window.size());
232 if (!display_service) {
234 "DisplayService not available for dynamic rendering");
238 for (
const auto& [window_ptr, buffer_infos] : buffers_by_window) {
239 auto window = buffer_infos[0]->target_window;
241 uint64_t image_bits = display_service->acquire_next_swapchain_image(window);
242 if (image_bits == 0) {
244 "Failed to acquire swapchain image for window '{}'",
245 window->get_create_info().title);
248 vk::Image swapchain_image {
reinterpret_cast<VkImage
>(image_bits) };
251 auto primary_cmd = foundry.get_command_buffer(primary_cmd_id);
255 "Failed to create primary command buffer for window '{}'",
256 window->get_create_info().title);
257 for (
const auto* info : buffer_infos) {
258 info->buffer->clear_pipeline_commands();
264 flow.begin_rendering(primary_cmd_id, window, swapchain_image);
266 std::vector<vk::CommandBuffer> secondary_buffers;
267 for (
const auto* info : buffer_infos) {
268 auto secondary = foundry.get_command_buffer(info->command_buffer_id);
270 secondary_buffers.push_back(secondary);
274 if (!secondary_buffers.empty()) {
275 primary_cmd.executeCommands(secondary_buffers);
278 flow.end_rendering(primary_cmd_id, window);
280 foundry.end_commands(primary_cmd_id);
281 uint64_t primary_bits = *
reinterpret_cast<uint64_t*
>(&primary_cmd);
282 display_service->submit_and_present(window, primary_bits);
285 "Presented {} buffers to window '{}'",
286 secondary_buffers.size(), window->get_create_info().title);
288 }
catch (
const std::exception& e) {
290 "Failed to submit/present for window '{}': {}",
291 window->get_create_info().title,
295 for (
const auto* info : buffer_infos) {
296 info->buffer->clear_pipeline_commands();
300 root->clear_renderable_buffers();
304 : m_final_processor(nullptr)
320 if (batch_processor) {
340 auto it = std::remove_if(
343 [](
const std::shared_ptr<Buffer>& buf) {
344 return buf && buf->needs_removal();
349 if (removed_count > 0) {
353 "Cleaned up {} graphics buffers (remaining: {})",
377 std::vector<std::shared_ptr<VKBuffer>> filtered_buffers;
380 if (buffer && buffer->get_usage() == usage) {
381 filtered_buffers.push_back(buffer);
385 return filtered_buffers;
390 return std::make_shared<GraphicsBatchProcessor>(shared_from_this());
#define MF_INFO(comp, ctx,...)
#define MF_RT_WARN(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
#define MF_RT_DEBUG(comp, ctx,...)
ProcessingToken m_processing_token
std::shared_ptr< RootGraphicsBuffer > m_root_buffer
Shared pointer to the root buffer this processor manages.
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks compatibility with a specific buffer type.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
Processes a buffer by coordinating child buffer operations.
GraphicsBatchProcessor(std::shared_ptr< Buffer > root_buffer)
Creates a new graphics batch processor.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when processor is attached to a buffer.
std::function< void(const std::shared_ptr< RootGraphicsBuffer > &root)> RenderCallback
Callback signature for render operations.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
Executes the render callback.
RenderCallback m_callback
User-provided render callback.
bool has_callback() const
Checks if a callback is configured.
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks compatibility with a specific buffer type.
void set_callback(RenderCallback callback)
Sets or updates the render callback.
void fallback_renderer(const std::shared_ptr< RootGraphicsBuffer > &root)
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when processor is attached to a buffer.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when processor is detached from a buffer.
PresentProcessor()
Default constructor (no callback set)
std::shared_ptr< RootGraphicsBuffer > m_root_buffer
Reference to root buffer (for validation and callbacks)
void process_pending_buffer_operations()
Process pending operations - call this at start of processing cycles.
bool has_pending_operations() const
ProcessingToken m_preferred_processing_token
Preferred processing token for this root buffer.
TokenEnforcementStrategy m_token_enforcement_strategy
Current token enforcement strategy for this root buffer.
std::vector< std::shared_ptr< VKBuffer > > m_child_buffers
Vector of tributary buffers that contribute to this root buffer.
void process_default() override
Processes this root buffer using default processing.
std::shared_ptr< BufferProcessor > get_final_processor() const
Gets the current final processor.
void cleanup_marked_buffers()
Removes buffers marked for deletion.
void set_final_processor(std::shared_ptr< BufferProcessor > processor)
Sets an optional final processor.
std::vector< std::shared_ptr< VKBuffer > > get_buffers_by_usage(VKBuffer::Usage usage) const
Gets buffers filtered by usage type.
std::shared_ptr< BufferProcessor > m_final_processor
Optional final processor (rarely used in graphics)
std::vector< std::shared_ptr< VKBuffer > > m_pending_removal
Buffers pending removal (cleaned up in next process cycle)
void initialize()
Initializes the root buffer with default processor.
bool has_buffer(const std::shared_ptr< VKBuffer > &buffer) const
Checks if a specific buffer is registered.
~RootGraphicsBuffer() override
Virtual destructor ensuring proper cleanup.
RootGraphicsBuffer()
Creates a new root graphics buffer.
std::shared_ptr< BufferProcessor > create_default_processor()
Creates the default graphics batch processor.
Root container for GPU buffer lifecycle management and batch processing.
std::shared_ptr< Buffers::BufferProcessor > get_default_processor() const override
Get the currently attached default processor.
void set_default_processor(const std::shared_ptr< BufferProcessor > &processor) override
Set the buffer's default processor.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
bool are_tokens_compatible(ProcessingToken preferred, ProcessingToken current)
Determines if two processing tokens are compatible for joint execution.
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
@ STRICT
Strictly enforces token assignment with no cross-token sharing.
@ BufferManagement
Buffer Management (Buffers::BufferManager, creating buffers)
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
@ Core
Core engine, backend, subsystems.
MAYAFLUX_API RenderFlow & get_render_flow()
Get the global render flow instance.
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
constexpr CommandBufferID INVALID_COMMAND_BUFFER
CommandBufferID command_buffer_id
std::shared_ptr< VKBuffer > buffer
std::shared_ptr< Core::Window > target_window
RenderPipelineID pipeline_id
Information about a buffer that's ready to render.
Backend display and presentation service interface.