Hide Storage from Primitive for type-safety
This commit is contained in:
parent
6a1cd02b3a
commit
53a98bf7de
5 changed files with 129 additions and 45 deletions
|
|
@ -128,26 +128,25 @@ impl Primitive {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl shader::Primitive for Primitive {
|
impl shader::Primitive for Primitive {
|
||||||
fn prepare(
|
type Renderer = Pipeline;
|
||||||
|
|
||||||
|
fn initialize(
|
||||||
&self,
|
&self,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
format: wgpu::TextureFormat,
|
format: wgpu::TextureFormat,
|
||||||
storage: &mut shader::Storage,
|
) -> Pipeline {
|
||||||
|
Pipeline::new(device, queue, format)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepare(
|
||||||
|
&self,
|
||||||
|
pipeline: &mut Pipeline,
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
_bounds: &Rectangle,
|
_bounds: &Rectangle,
|
||||||
viewport: &Viewport,
|
viewport: &Viewport,
|
||||||
) {
|
) {
|
||||||
if !storage.has::<Pipeline>() {
|
|
||||||
storage.store(Pipeline::new(
|
|
||||||
device,
|
|
||||||
queue,
|
|
||||||
format,
|
|
||||||
viewport.physical_size(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let pipeline = storage.get_mut::<Pipeline>().unwrap();
|
|
||||||
|
|
||||||
// Upload data to GPU
|
// Upload data to GPU
|
||||||
pipeline.update(
|
pipeline.update(
|
||||||
device,
|
device,
|
||||||
|
|
@ -161,14 +160,11 @@ impl shader::Primitive for Primitive {
|
||||||
|
|
||||||
fn render(
|
fn render(
|
||||||
&self,
|
&self,
|
||||||
|
pipeline: &Pipeline,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
storage: &shader::Storage,
|
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
clip_bounds: &Rectangle<u32>,
|
clip_bounds: &Rectangle<u32>,
|
||||||
) {
|
) {
|
||||||
// At this point our pipeline should always be initialized
|
|
||||||
let pipeline = storage.get::<Pipeline>().unwrap();
|
|
||||||
|
|
||||||
// Render primitive
|
// Render primitive
|
||||||
pipeline.render(
|
pipeline.render(
|
||||||
target,
|
target,
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ impl Pipeline {
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
format: wgpu::TextureFormat,
|
format: wgpu::TextureFormat,
|
||||||
target_size: Size<u32>,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
//vertices of one cube
|
//vertices of one cube
|
||||||
let vertices =
|
let vertices =
|
||||||
|
|
@ -62,8 +61,8 @@ impl Pipeline {
|
||||||
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
|
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
label: Some("cubes depth texture"),
|
label: Some("cubes depth texture"),
|
||||||
size: wgpu::Extent3d {
|
size: wgpu::Extent3d {
|
||||||
width: target_size.width,
|
width: 1,
|
||||||
height: target_size.height,
|
height: 1,
|
||||||
depth_or_array_layers: 1,
|
depth_or_array_layers: 1,
|
||||||
},
|
},
|
||||||
mip_level_count: 1,
|
mip_level_count: 1,
|
||||||
|
|
@ -297,7 +296,7 @@ impl Pipeline {
|
||||||
uniforms,
|
uniforms,
|
||||||
uniform_bind_group,
|
uniform_bind_group,
|
||||||
vertices,
|
vertices,
|
||||||
depth_texture_size: target_size,
|
depth_texture_size: Size::new(1, 1),
|
||||||
depth_view,
|
depth_view,
|
||||||
depth_pipeline,
|
depth_pipeline,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -235,13 +235,13 @@ impl Layer {
|
||||||
pub fn draw_primitive(
|
pub fn draw_primitive(
|
||||||
&mut self,
|
&mut self,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
primitive: Box<dyn Primitive>,
|
primitive: impl Primitive,
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
) {
|
) {
|
||||||
let bounds = bounds * transformation;
|
let bounds = bounds * transformation;
|
||||||
|
|
||||||
self.primitives
|
self.primitives
|
||||||
.push(primitive::Instance { bounds, primitive });
|
.push(primitive::Instance::new(bounds, primitive));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush_meshes(&mut self) {
|
fn flush_meshes(&mut self) {
|
||||||
|
|
|
||||||
|
|
@ -365,10 +365,10 @@ impl Renderer {
|
||||||
|
|
||||||
for instance in &layer.primitives {
|
for instance in &layer.primitives {
|
||||||
instance.primitive.prepare(
|
instance.primitive.prepare(
|
||||||
|
&mut primitive_storage,
|
||||||
&self.engine.device,
|
&self.engine.device,
|
||||||
&self.engine.queue,
|
&self.engine.queue,
|
||||||
self.engine.format,
|
self.engine.format,
|
||||||
&mut primitive_storage,
|
|
||||||
&instance.bounds,
|
&instance.bounds,
|
||||||
viewport,
|
viewport,
|
||||||
);
|
);
|
||||||
|
|
@ -548,8 +548,8 @@ impl Renderer {
|
||||||
.and_then(Rectangle::snap)
|
.and_then(Rectangle::snap)
|
||||||
{
|
{
|
||||||
instance.primitive.render(
|
instance.primitive.render(
|
||||||
encoder,
|
|
||||||
&primitive_storage,
|
&primitive_storage,
|
||||||
|
encoder,
|
||||||
frame,
|
frame,
|
||||||
&clip_bounds,
|
&clip_bounds,
|
||||||
);
|
);
|
||||||
|
|
@ -805,7 +805,7 @@ impl graphics::geometry::Renderer for Renderer {
|
||||||
impl primitive::Renderer for Renderer {
|
impl primitive::Renderer for Renderer {
|
||||||
fn draw_primitive(&mut self, bounds: Rectangle, primitive: impl Primitive) {
|
fn draw_primitive(&mut self, bounds: Rectangle, primitive: impl Primitive) {
|
||||||
let (layer, transformation) = self.layers.current_mut();
|
let (layer, transformation) = self.layers.current_mut();
|
||||||
layer.draw_primitive(bounds, Box::new(primitive), transformation);
|
layer.draw_primitive(bounds, primitive, transformation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,32 @@ pub type Batch = Vec<Instance>;
|
||||||
|
|
||||||
/// A set of methods which allows a [`Primitive`] to be rendered.
|
/// A set of methods which allows a [`Primitive`] to be rendered.
|
||||||
pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
|
pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
|
||||||
/// Processes the [`Primitive`], allowing for GPU buffer allocation.
|
/// The shared renderer of this [`Primitive`].
|
||||||
fn prepare(
|
///
|
||||||
|
/// Normally, this will contain a bunch of [`wgpu`] state; like
|
||||||
|
/// a rendering pipeline, buffers, and textures.
|
||||||
|
///
|
||||||
|
/// All instances of this [`Primitive`] type will share the same
|
||||||
|
/// [`Renderer`].
|
||||||
|
type Renderer: MaybeSend + MaybeSync;
|
||||||
|
|
||||||
|
/// Initializes the [`Renderer`](Self::Renderer) of the [`Primitive`].
|
||||||
|
///
|
||||||
|
/// This will only be called once, when the first [`Primitive`] of this kind
|
||||||
|
/// is encountered.
|
||||||
|
fn initialize(
|
||||||
&self,
|
&self,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
format: wgpu::TextureFormat,
|
format: wgpu::TextureFormat,
|
||||||
storage: &mut Storage,
|
) -> Self::Renderer;
|
||||||
|
|
||||||
|
/// Processes the [`Primitive`], allowing for GPU buffer allocation.
|
||||||
|
fn prepare(
|
||||||
|
&self,
|
||||||
|
renderer: &mut Self::Renderer,
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
bounds: &Rectangle,
|
bounds: &Rectangle,
|
||||||
viewport: &Viewport,
|
viewport: &Viewport,
|
||||||
);
|
);
|
||||||
|
|
@ -26,21 +45,92 @@ pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
|
||||||
/// Renders the [`Primitive`].
|
/// Renders the [`Primitive`].
|
||||||
fn render(
|
fn render(
|
||||||
&self,
|
&self,
|
||||||
|
renderer: &Self::Renderer,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
|
target: &wgpu::TextureView,
|
||||||
|
clip_bounds: &Rectangle<u32>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait Stored:
|
||||||
|
Debug + MaybeSend + MaybeSync + 'static
|
||||||
|
{
|
||||||
|
fn prepare(
|
||||||
|
&self,
|
||||||
|
storage: &mut Storage,
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
|
format: wgpu::TextureFormat,
|
||||||
|
bounds: &Rectangle,
|
||||||
|
viewport: &Viewport,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn render(
|
||||||
|
&self,
|
||||||
storage: &Storage,
|
storage: &Storage,
|
||||||
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
clip_bounds: &Rectangle<u32>,
|
clip_bounds: &Rectangle<u32>,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct BlackBox<P: Primitive> {
|
||||||
|
primitive: P,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Primitive> Stored for BlackBox<P> {
|
||||||
|
fn prepare(
|
||||||
|
&self,
|
||||||
|
storage: &mut Storage,
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
|
format: wgpu::TextureFormat,
|
||||||
|
bounds: &Rectangle,
|
||||||
|
viewport: &Viewport,
|
||||||
|
) {
|
||||||
|
if !storage.has::<P>() {
|
||||||
|
storage.store::<P, _>(
|
||||||
|
self.primitive.initialize(device, queue, format),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let renderer = storage
|
||||||
|
.get_mut::<P>()
|
||||||
|
.expect("renderer should be initialized")
|
||||||
|
.downcast_mut::<P::Renderer>()
|
||||||
|
.expect("renderer should have the proper type");
|
||||||
|
|
||||||
|
self.primitive
|
||||||
|
.prepare(renderer, device, queue, bounds, viewport);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(
|
||||||
|
&self,
|
||||||
|
storage: &Storage,
|
||||||
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
|
target: &wgpu::TextureView,
|
||||||
|
clip_bounds: &Rectangle<u32>,
|
||||||
|
) {
|
||||||
|
let renderer = storage
|
||||||
|
.get::<P>()
|
||||||
|
.expect("renderer should be initialized")
|
||||||
|
.downcast_ref::<P::Renderer>()
|
||||||
|
.expect("renderer should have the proper type");
|
||||||
|
|
||||||
|
self.primitive
|
||||||
|
.render(renderer, encoder, target, clip_bounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// An instance of a specific [`Primitive`].
|
/// An instance of a specific [`Primitive`].
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
/// The bounds of the [`Instance`].
|
/// The bounds of the [`Instance`].
|
||||||
pub bounds: Rectangle,
|
pub(crate) bounds: Rectangle,
|
||||||
|
|
||||||
/// The [`Primitive`] to render.
|
/// The [`Primitive`] to render.
|
||||||
pub primitive: Box<dyn Primitive>,
|
pub(crate) primitive: Box<dyn Stored>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
|
|
@ -48,7 +138,7 @@ impl Instance {
|
||||||
pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
|
pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
|
||||||
Instance {
|
Instance {
|
||||||
bounds,
|
bounds,
|
||||||
primitive: Box::new(primitive),
|
primitive: Box::new(BlackBox { primitive }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,26 +163,25 @@ impl Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts the data `T` in to [`Storage`].
|
/// Inserts the data `T` in to [`Storage`].
|
||||||
pub fn store<T: 'static + MaybeSend + MaybeSync>(&mut self, data: T) {
|
pub fn store<T: 'static, D: Any + MaybeSend + MaybeSync>(
|
||||||
|
&mut self,
|
||||||
|
data: D,
|
||||||
|
) {
|
||||||
let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
|
let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the data with type `T` if it exists in [`Storage`].
|
/// Returns a reference to the data with type `T` if it exists in [`Storage`].
|
||||||
pub fn get<T: 'static>(&self) -> Option<&T> {
|
pub fn get<T: 'static>(&self) -> Option<&dyn Any> {
|
||||||
self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| {
|
self.pipelines
|
||||||
(pipeline.as_ref() as &dyn Any)
|
.get(&TypeId::of::<T>())
|
||||||
.downcast_ref::<T>()
|
.map(|pipeline| pipeline.as_ref() as &dyn Any)
|
||||||
.expect("Value with this type does not exist in Storage.")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the data with type `T` if it exists in [`Storage`].
|
/// Returns a mutable reference to the data with type `T` if it exists in [`Storage`].
|
||||||
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
|
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut dyn Any> {
|
||||||
self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| {
|
self.pipelines
|
||||||
(pipeline.as_mut() as &mut dyn Any)
|
.get_mut(&TypeId::of::<T>())
|
||||||
.downcast_mut::<T>()
|
.map(|pipeline| pipeline.as_mut() as &mut dyn Any)
|
||||||
.expect("Value with this type does not exist in Storage.")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue