1use crate::color_mode::ColorMode;
13use crate::command_encoder::CommandEncoder;
14use crate::context_observer::ContextObserver;
15use crate::depth_mode::DepthMode;
16use crate::drawable_builder::UniqueDrawableBuilder;
17use crate::offscreen_texture::OffscreenTexture;
18use crate::render_pass::RenderPass;
19use crate::renderer::layer_group::LayerGroupBasePtr;
20use crate::rendering_stats::RenderingStats;
21use crate::shader_registry::ShaderRegistry;
22use crate::shaders::clipping_mask_ubo::ClipUBO;
23use crate::shaders::shader_program_base::ShaderProgramBasePtr;
24use crate::texture2d::Texture2DPtr;
25use crate::types::TextureChannelDataType;
26use crate::uniform_buffer::{UniformBufferArray, UniformBufferPtr, UniqueUniformBufferArray};
27use crate::vertex_attribute::VertexAttributeArrayPtr;
28use mln_util::size::Size;
29use std::any::Any;
30use std::cell::{Ref, RefCell, RefMut};
31use std::rc::Rc;
32
33pub struct ContextBase {
35 pub maximum_vertex_binding_count: u32,
36 stats: RefCell<RenderingStats>,
37 observer: RefCell<Option<Rc<dyn ContextObserver>>>,
38}
39
40impl ContextBase {
41 pub const MINIMUM_REQUIRED_VERTEX_BINDING_COUNT: u32 = 8;
42
43 pub fn new(maximum_vertex_binding_count: u32) -> Self {
44 ContextBase {
45 maximum_vertex_binding_count,
46 stats: RefCell::new(RenderingStats::default()),
47 observer: RefCell::new(None),
48 }
49 }
50}
51
52pub trait Context: Any {
53 fn context_base(&self) -> &ContextBase;
54 fn as_any(&self) -> &dyn Any;
55
56 fn set_observer(&self, observer: Option<Rc<dyn ContextObserver>>) {
58 *self.context_base().observer.borrow_mut() = observer;
59 }
60 fn get_observer(&self) -> Option<Rc<dyn ContextObserver>> {
61 self.context_base().observer.borrow().clone()
62 }
63
64 fn begin_frame(&self);
65 fn end_frame(&self);
66 fn perform_cleanup(&self);
67 fn reduce_memory_usage(&self);
68
69 fn create_offscreen_texture(&self, size: Size, type_: TextureChannelDataType) -> Box<dyn OffscreenTexture>;
70 fn create_command_encoder(&self) -> Box<dyn CommandEncoder>;
71
72 fn rendering_stats(&self) -> RefMut<'_, RenderingStats> {
73 self.context_base().stats.borrow_mut()
74 }
75 fn thread_safe_copy_rendering_stats(&self) -> RenderingStats {
76 self.context_base().stats.borrow().clone()
77 }
78
79 fn clear_stencil_buffer(&self, value: i32);
80 fn set_dirty_state(&self);
81
82 fn create_vertex_attribute_array(&self) -> VertexAttributeArrayPtr;
83 fn create_drawable_builder(&self, name: String) -> UniqueDrawableBuilder;
84 fn create_uniform_buffer(&self, data: &[u8], persistent: bool, ssbo: bool) -> UniformBufferPtr;
85 fn create_layer_uniform_buffer_array(&self) -> UniqueUniformBufferArray;
86 fn get_generic_shader(&self, registry: &ShaderRegistry, name: &str) -> Option<ShaderProgramBasePtr>;
87 fn create_tile_layer_group(
88 &self,
89 layer_index: i32,
90 initial_capacity: usize,
91 name: String,
92 ) -> Option<LayerGroupBasePtr>;
93 fn create_layer_group(&self, layer_index: i32, initial_capacity: usize, name: String) -> Option<LayerGroupBasePtr>;
94 fn create_texture_2d(&self) -> Texture2DPtr;
95
96 fn reset_state(&self, depth_mode: DepthMode, color_mode: ColorMode);
97
98 fn emplace_or_update_uniform_buffer(&self, ptr: &mut Option<UniformBufferPtr>, data: &[u8], persistent: bool)
100 -> bool;
101
102 fn get_global_uniform_buffers(&self) -> Ref<'_, UniformBufferArray>;
103 fn mutable_global_uniform_buffers(&self) -> RefMut<'_, UniformBufferArray>;
104 fn bind_global_uniform_buffers(&self, render_pass: &dyn RenderPass);
105 fn unbind_global_uniform_buffers(&self, render_pass: &dyn RenderPass);
106
107 fn render_tile_clipping_masks(
117 &self,
118 _render_pass: &dyn RenderPass,
119 _shaders: &ShaderRegistry,
120 _tile_ubos: &[ClipUBO],
121 ) -> bool {
122 false
123 }
124}
125
126impl dyn Context {
127 pub fn emplace_or_update_uniform_buffer_pod<T: bytemuck::Pod>(
129 &self,
130 ptr: &mut Option<UniformBufferPtr>,
131 data: &T,
132 ) -> bool {
133 self.emplace_or_update_uniform_buffer(ptr, bytemuck::bytes_of(data), false)
134 }
135}