21 auto root_buf = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
24 "GraphicsBatchProcessor can only process its associated RootGraphicsBuffer");
28 root_buf->cleanup_marked_buffers();
30 for (
auto& ch_buffer : root_buf->get_child_buffers()) {
34 if (ch_buffer->needs_removal()) {
38 if (!ch_buffer->has_data_for_cycle()) {
43 if (ch_buffer->needs_default_processing() && ch_buffer->get_default_processor()) {
44 ch_buffer->process_default();
47 if (
auto chain = ch_buffer->get_processing_chain()) {
48 if (ch_buffer->has_data_for_cycle()) {
49 chain->process_complete(ch_buffer);
53 auto vk_buffer = std::dynamic_pointer_cast<Buffers::VKBuffer>(ch_buffer);
54 if (vk_buffer && vk_buffer->has_render_pipeline()) {
55 for (
const auto& [
id, window] : vk_buffer->get_render_pipelines()) {
61 info.
needs_depth = vk_buffer->needs_depth_attachment();
63 root_buf->add_renderable_buffer(info);
66 "Registered buffer for rendering to window '{}'",
67 window->get_create_info().title);
71 }
catch (
const std::exception& e) {
73 "Error processing graphics buffer: {}", e.what());
80 auto root_graphics_buffer = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
81 if (!root_graphics_buffer) {
82 error<std::invalid_argument>(
85 std::source_location::current(),
86 "GraphicsBatchProcessor can only be attached to RootGraphicsBuffer");
90 error<std::runtime_error>(
93 std::source_location::current(),
94 "GraphicsBatchProcessor token incompatible with RootGraphicsBuffer requirements");
100 return std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer) !=
nullptr;
104 : m_callback(
std::move(callback))
105 , m_root_buffer(nullptr)
111 : m_callback(nullptr)
112 , m_root_buffer(nullptr)
119 auto root_graphics_buffer = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
120 if (!root_graphics_buffer) {
122 "RenderProcessor received non-RootGraphicsBuffer");
128 "RenderProcessor processing buffer that doesn't match attached root");
135 }
catch (
const std::exception& e) {
139 std::source_location::current(),
140 "RenderProcessor callback threw exception: {}",
150 auto root_graphics_buffer = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer);
151 if (!root_graphics_buffer) {
152 error<std::invalid_argument>(
155 std::source_location::current(),
156 "RenderProcessor can only be attached to RootGraphicsBuffer");
160 error<std::runtime_error>(
163 std::source_location::current(),
164 "RenderProcessor token incompatible with RootGraphicsBuffer requirements");
170 "RenderProcessor attached to RootGraphicsBuffer (has_callback: {})",
176 if (
auto root = std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer)) {
183 "RenderProcessor detached from RootGraphicsBuffer");
188 return std::dynamic_pointer_cast<RootGraphicsBuffer>(buffer) !=
nullptr;
196 "RenderProcessor callback {} (attached: {})",
203 const auto& renderable_buffers = root->get_renderable_buffers();
204 if (renderable_buffers.empty()) {
208 std::unordered_map<Core::Window*, std::vector<const RootGraphicsBuffer::RenderableBufferInfo*>> buffers_by_window;
209 for (
const auto& renderable : renderable_buffers) {
210 if (renderable.target_window && renderable.target_window->is_graphics_registered()
212 buffers_by_window[renderable.target_window.get()].push_back(&renderable);
216 if (buffers_by_window.empty()) {
217 root->clear_renderable_buffers();
222 "PresentProcessor submitting to {} windows", buffers_by_window.size());
229 if (!display_service) {
231 "DisplayService not available for dynamic rendering");
235 for (
const auto& [window_ptr, buffer_infos] : buffers_by_window) {
236 auto window = buffer_infos[0]->target_window;
238 uint64_t image_bits = display_service->acquire_next_swapchain_image(window);
239 if (image_bits == 0) {
241 "Failed to acquire swapchain image for window '{}'",
242 window->get_create_info().title);
245 vk::Image swapchain_image {
reinterpret_cast<VkImage
>(image_bits) };
248 auto primary_cmd = foundry.get_command_buffer(primary_cmd_id);
252 "Failed to create primary command buffer for window '{}'",
253 window->get_create_info().title);
254 for (
const auto* info : buffer_infos) {
255 info->buffer->clear_pipeline_commands();
261 bool window_needs_depth =
false;
262 for (
const auto* info : buffer_infos) {
263 if (info->needs_depth) {
264 window_needs_depth =
true;
269 vk::ImageView depth_view =
nullptr;
270 if (window_needs_depth) {
271 display_service->ensure_depth_attachment(window);
272 auto* view_ptr = display_service->get_depth_image_view(window);
274 depth_view = *
static_cast<vk::ImageView*
>(view_ptr);
278 flow.begin_rendering(primary_cmd_id, window, swapchain_image,
281 std::vector<vk::CommandBuffer> secondary_buffers;
282 for (
const auto* info : buffer_infos) {
283 auto secondary = foundry.get_command_buffer(info->command_buffer_id);
285 secondary_buffers.push_back(secondary);
289 if (!secondary_buffers.empty()) {
290 primary_cmd.executeCommands(secondary_buffers);
293 flow.end_rendering(primary_cmd_id, window);
295 foundry.end_commands(primary_cmd_id);
296 uint64_t primary_bits = *
reinterpret_cast<uint64_t*
>(&primary_cmd);
297 display_service->submit_and_present(window, primary_bits);
300 "Presented {} buffers to window '{}'",
301 secondary_buffers.size(), window->get_create_info().title);
303 }
catch (
const std::exception& e) {
305 "Failed to submit/present for window '{}': {}",
306 window->get_create_info().title,
310 for (
const auto* info : buffer_infos) {
311 info->buffer->clear_pipeline_commands();
315 root->clear_renderable_buffers();
319 : m_final_processor(nullptr)
335 if (batch_processor) {
355 auto it = std::remove_if(
358 [](
const std::shared_ptr<Buffer>& buf) {
359 return buf && buf->needs_removal();
364 if (removed_count > 0) {
368 "Cleaned up {} graphics buffers (remaining: {})",
392 std::vector<std::shared_ptr<VKBuffer>> filtered_buffers;
395 if (buffer && buffer->get_usage() == usage) {
396 filtered_buffers.push_back(buffer);
400 return filtered_buffers;
405 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.
const std::array< float, 4 > default_color
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.