use std::any::Any;
use std::cell::Cell;
use std::rc::Rc;
use mln_gfx::frame;
use mln_util::size::Size;
pub trait TargetResource {
fn bind(&self) {}
fn finish_frame(&self, _encoder: &mut wgpu::CommandEncoder) {}
fn get_color_texture_view(&self) -> Option<wgpu::TextureView>;
fn get_color_texture_format(&self) -> Option<wgpu::TextureFormat>;
fn get_depth_stencil_texture_view(&self) -> Option<wgpu::TextureView> {
None
}
fn get_depth_stencil_texture_format(&self) -> Option<wgpu::TextureFormat> {
None
}
fn as_any(&self) -> &dyn Any;
}
impl<T: TargetResource + ?Sized> TargetResource for Rc<T> {
fn bind(&self) {
(**self).bind();
}
fn finish_frame(&self, encoder: &mut wgpu::CommandEncoder) {
(**self).finish_frame(encoder);
}
fn get_color_texture_view(&self) -> Option<wgpu::TextureView> {
(**self).get_color_texture_view()
}
fn get_color_texture_format(&self) -> Option<wgpu::TextureFormat> {
(**self).get_color_texture_format()
}
fn get_depth_stencil_texture_view(&self) -> Option<wgpu::TextureView> {
(**self).get_depth_stencil_texture_view()
}
fn get_depth_stencil_texture_format(&self) -> Option<wgpu::TextureFormat> {
(**self).get_depth_stencil_texture_format()
}
fn as_any(&self) -> &dyn Any {
(**self).as_any()
}
}
pub struct DrawTarget {
size: Cell<Size>,
resource: Box<dyn TargetResource>,
}
impl DrawTarget {
pub fn new(size: Size, resource: impl TargetResource + 'static) -> Self {
DrawTarget { size: Cell::new(size), resource: Box::new(resource) }
}
pub fn resource(&self) -> &dyn TargetResource {
&*self.resource
}
pub(crate) fn set_size(&self, size: Size) {
self.size.set(size);
}
}
impl frame::DrawTarget for DrawTarget {
fn size(&self) -> Size {
self.size.get()
}
fn as_any(&self) -> &dyn Any {
self
}
}
pub fn as_webgpu(target: &dyn frame::DrawTarget) -> Option<&DrawTarget> {
target.as_any().downcast_ref::<DrawTarget>()
}
pub fn downcast_resource<T: 'static>(target: &dyn frame::DrawTarget) -> Option<&T> {
as_webgpu(target)?.resource().as_any().downcast_ref::<T>()
}