khora_core/renderer/traits/
render_system.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::Arc;
16
17use crate::platform::window::KhoraWindow;
18use crate::renderer::error::RenderError;
19use crate::renderer::{api::*, GraphicsDevice};
20use crate::telemetry::ResourceMonitor;
21
22/// A high-level trait representing the entire rendering subsystem.
23///
24/// This trait defines the primary interface for the engine to interact with the renderer.
25/// A concrete implementation of `RenderSystem` (likely in `khora-infra`) encapsulates
26/// all the state and logic needed to render a frame, including device management,
27/// swapchain handling, and the execution of render pipelines.
28///
29/// This can be considered the main "contract" for a `RenderAgent` to manage.
30pub trait RenderSystem: std::fmt::Debug + Send + Sync {
31    /// Initializes the rendering system with a given window.
32    ///
33    /// This method sets up the graphics device, swapchain, and any other necessary
34    /// backend resources. It should be called once at application startup.
35    ///
36    /// # Returns
37    ///
38    /// On success, it returns a `Vec` of `ResourceMonitor` trait objects that the
39    /// telemetry system can use to track GPU-specific resources like VRAM.
40    fn init(
41        &mut self,
42        window: &dyn KhoraWindow,
43    ) -> Result<Vec<Arc<dyn ResourceMonitor>>, RenderError>;
44
45    /// Notifies the rendering system that the output window has been resized.
46    ///
47    /// The implementation should handle recreating the swapchain and any other
48    /// size-dependent resources (like depth textures).
49    fn resize(&mut self, new_width: u32, new_height: u32);
50
51    /// Prepares for a new frame.
52    ///
53    /// This is typically called once per frame before `render`. It can be used
54    /// to update internal resources, like uniform buffers, based on the camera's
55    /// `ViewInfo`.
56    fn prepare_frame(&mut self, view_info: &ViewInfo);
57
58    /// Renders a single frame.
59    ///
60    /// This is the main workload function, responsible for executing all necessary
61    /// render passes to draw the provided `renderables` to the screen.
62    ///
63    /// # Arguments
64    ///
65    /// * `renderables`: A slice of `RenderObject`s representing the scene to be drawn.
66    /// * `view_info`: Contains camera and projection information for the frame.
67    /// * `settings`: Contains global rendering settings for the frame.
68    ///
69    /// # Returns
70    ///
71    /// On success, it returns `RenderStats` with performance metrics for the frame.
72    fn render(
73        &mut self,
74        renderables: &[RenderObject],
75        view_info: &ViewInfo,
76        settings: &RenderSettings,
77    ) -> Result<RenderStats, RenderError>;
78
79    /// Returns a reference to the statistics of the last successfully rendered frame.
80    fn get_last_frame_stats(&self) -> &RenderStats;
81
82    /// Checks if a specific, optional rendering feature is supported by the backend.
83    fn supports_feature(&self, feature_name: &str) -> bool;
84
85    /// Returns information about the active graphics adapter (GPU).
86    fn get_adapter_info(&self) -> Option<RendererAdapterInfo>;
87
88    /// Returns a shared, thread-safe reference to the underlying `GraphicsDevice`.
89    ///
90    /// This allows other parts of the engine (e.g., the asset system) to create
91    /// graphics resources like buffers and textures on the correct GPU device.
92    fn graphics_device(&self) -> Arc<dyn GraphicsDevice>;
93
94    /// Cleans up and releases all graphics resources.
95    /// This should be called once at application shutdown.
96    fn shutdown(&mut self);
97
98    /// Allows downcasting to a concrete `RenderSystem` type.
99    fn as_any(&self) -> &dyn std::any::Any;
100}