use std::cell::{Cell, RefCell};
use std::rc::Rc;
use mln_gfx::renderer_backend::{ContextMode, RendererBackendBase};
use mln_gfx::shader_registry::ShaderRegistry;
use mln_gfx::shaders::program_parameters::ProgramParameters;
use mln_gfx::shaders::shader_source::BuiltIn;
use crate::context::Context;
use crate::shaders::shader_group::ShaderGroup;
use crate::shaders::shader_source::shader_source;
#[derive(Default)]
pub struct BackendImpl {
instance: RefCell<Option<wgpu::Instance>>,
adapter: RefCell<Option<wgpu::Adapter>>,
device: RefCell<Option<wgpu::Device>>,
queue: RefCell<Option<wgpu::Queue>>,
depth_stencil_format: Cell<Option<wgpu::TextureFormat>>,
color_format: Cell<Option<wgpu::TextureFormat>>,
}
impl BackendImpl {
pub fn set_instance(&self, instance: Option<wgpu::Instance>) {
*self.instance.borrow_mut() = instance;
}
pub fn set_adapter(&self, adapter: Option<wgpu::Adapter>) {
*self.adapter.borrow_mut() = adapter;
}
pub fn set_device(&self, device: Option<wgpu::Device>) {
*self.device.borrow_mut() = device;
}
pub fn set_queue(&self, queue: Option<wgpu::Queue>) {
*self.queue.borrow_mut() = queue;
}
pub fn get_instance(&self) -> Option<wgpu::Instance> {
self.instance.borrow().clone()
}
pub fn get_adapter(&self) -> Option<wgpu::Adapter> {
self.adapter.borrow().clone()
}
pub fn get_device(&self) -> Option<wgpu::Device> {
self.device.borrow().clone()
}
pub fn get_queue(&self) -> Option<wgpu::Queue> {
self.queue.borrow().clone()
}
pub fn set_depth_stencil_format(&self, format: Option<wgpu::TextureFormat>) {
self.depth_stencil_format.set(format);
}
pub fn get_depth_stencil_format(&self) -> Option<wgpu::TextureFormat> {
self.depth_stencil_format.get()
}
pub fn set_color_format(&self, format: Option<wgpu::TextureFormat>) {
self.color_format.set(format);
}
pub fn get_color_format(&self) -> Option<wgpu::TextureFormat> {
self.color_format.get()
}
}
pub struct RendererBackend {
pub base: RendererBackendBase,
impl_: Rc<BackendImpl>,
}
const REGISTERED_SHADERS: &[BuiltIn] = &[
BuiltIn::BackgroundShader,
BuiltIn::BackgroundPatternShader,
BuiltIn::CircleShader,
BuiltIn::ClippingMaskProgram,
BuiltIn::CollisionBoxShader,
BuiltIn::CollisionCircleShader,
BuiltIn::DebugShader,
BuiltIn::FillShader,
BuiltIn::FillOutlineShader,
BuiltIn::FillPatternShader,
BuiltIn::FillOutlinePatternShader,
BuiltIn::FillOutlineTriangulatedShader,
BuiltIn::FillExtrusionShader,
BuiltIn::FillExtrusionPatternShader,
BuiltIn::HeatmapShader,
BuiltIn::HeatmapTextureShader,
BuiltIn::HillshadeShader,
BuiltIn::HillshadePrepareShader,
BuiltIn::ColorReliefShader,
BuiltIn::LineShader,
BuiltIn::LineGradientShader,
BuiltIn::LinePatternShader,
BuiltIn::LineSDFShader,
BuiltIn::LineGradientSDFShader,
BuiltIn::LocationIndicatorShader,
BuiltIn::LocationIndicatorTexturedShader,
BuiltIn::RasterShader,
BuiltIn::SymbolIconShader,
BuiltIn::SymbolSDFShader,
BuiltIn::SymbolTextAndIconShader,
BuiltIn::WideVectorShader,
];
impl RendererBackend {
pub fn new(context_mode: ContextMode) -> Self {
RendererBackend { base: RendererBackendBase::new(context_mode), impl_: Rc::new(BackendImpl::default()) }
}
pub fn backend_impl(&self) -> &Rc<BackendImpl> {
&self.impl_
}
pub(crate) fn reset_context(&mut self) {
if let Some(context) = self.base.reset_context()
&& let Some(context) = context.as_any().downcast_ref::<Context>()
{
context.release_cached_buffers();
}
}
pub fn create_context(&self) -> Rc<Context> {
Context::new(self.impl_.clone())
}
pub fn activate(&self) {}
pub fn deactivate(&self) {}
pub fn init_shaders(&self, registry: &ShaderRegistry, program_parameters: &ProgramParameters) {
for &shader_id in REGISTERED_SHADERS {
let Some(source) = shader_source(shader_id) else {
continue;
};
let group = Rc::new(ShaderGroup::new(shader_id, program_parameters.clone()));
if !registry.register_shader_group(group, source.name) {
panic!("Failed to register {} with shader registry!", source.name);
}
}
}
pub fn set_instance(&self, instance: Option<wgpu::Instance>) {
self.impl_.set_instance(instance);
}
pub fn set_device(&self, device: Option<wgpu::Device>) {
self.impl_.set_device(device);
}
pub fn set_queue(&self, queue: Option<wgpu::Queue>) {
self.impl_.set_queue(queue);
}
pub fn get_instance(&self) -> Option<wgpu::Instance> {
self.impl_.get_instance()
}
pub fn get_device(&self) -> Option<wgpu::Device> {
self.impl_.get_device()
}
pub fn get_queue(&self) -> Option<wgpu::Queue> {
self.impl_.get_queue()
}
pub fn set_depth_stencil_format(&self, format: Option<wgpu::TextureFormat>) {
self.impl_.set_depth_stencil_format(format);
}
pub fn get_depth_stencil_format(&self) -> Option<wgpu::TextureFormat> {
self.impl_.get_depth_stencil_format()
}
pub fn set_color_format(&self, format: Option<wgpu::TextureFormat>) {
self.impl_.set_color_format(format);
}
pub fn get_color_format(&self) -> Option<wgpu::TextureFormat> {
self.impl_.get_color_format()
}
}
impl Drop for RendererBackend {
fn drop(&mut self) {
self.reset_context();
}
}