support downcasting RenderResourceContext ... this should be a part of std!

This commit is contained in:
Carter Anderson 2020-04-15 15:52:29 -07:00
parent 1f5f432e6c
commit c213865cbf
4 changed files with 12 additions and 19 deletions

View file

@ -28,3 +28,4 @@ zerocopy = "0.3"
bitflags = "1.0"
# TODO: replace once_cell with std equivalent if/when this lands: https://github.com/rust-lang/rfcs/pull/2788
once_cell = "1.3.1"
downcast-rs = "1.1.1"

View file

@ -5,35 +5,24 @@ use crate::{
};
use bevy_asset::{AssetStorage, Handle, HandleUntyped};
use bevy_window::{Window, WindowId};
use std::any::Any;
use downcast_rs::{Downcast, impl_downcast};
pub struct GlobalRenderResourceContext {
pub context: Box<dyn Any + Send + Sync + 'static>,
// TODO: why doesn't this work?
// pub context: Box<dyn RenderResourceContext + Send + Sync + 'static>,
pub context: Box<dyn RenderResourceContext>,
}
impl GlobalRenderResourceContext {
pub fn new<T>(context: T) -> GlobalRenderResourceContext
where
T: RenderResourceContext + Send + Sync + 'static,
T: RenderResourceContext,
{
GlobalRenderResourceContext {
context: Box::new(context),
}
}
// pub fn render_resources_mut(&mut self) -> &dyn RenderResourceContext {
// (&mut self.context).downcast_mut::<dyn RenderResourceContext>()
// }
// pub fn downcast_mut(&self) -> &dyn RenderResourceContext {
// self.context.downcast_ref::<RenderResourceContext>()
// }
}
// TODO: Rename to RenderResources after cleaning up AssetResources rename
pub trait RenderResourceContext: Any {
pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
fn create_swap_chain(&mut self, window: &Window);
fn next_swap_chain_texture(&mut self, window_id: WindowId);
fn drop_swap_chain_texture(&mut self, window_id: WindowId);
@ -90,3 +79,5 @@ impl dyn RenderResourceContext {
self.get_asset_resource_untyped(handle.into(), index)
}
}
impl_downcast!(RenderResourceContext);

View file

@ -3,7 +3,7 @@ use crate::WgpuResources;
use bevy_asset::{AssetStorage, Handle, HandleUntyped};
use bevy_render::{
render_resource::{BufferInfo, RenderResource, ResourceInfo},
renderer_2::RenderResourceContext,
renderer_2::{RenderResourceContext},
shader::Shader,
texture::{SamplerDescriptor, TextureDescriptor},
};
@ -25,6 +25,8 @@ impl WgpuRenderResourceContext {
}
}
// impl AnyRenderResourceContext for WgpuRenderResourceContext {}
impl RenderResourceContext for WgpuRenderResourceContext {
fn create_sampler(&mut self, sampler_descriptor: &SamplerDescriptor) -> RenderResource {
self.wgpu_resources

View file

@ -12,7 +12,6 @@ use bevy_render::{
use bevy_window::{WindowCreated, WindowResized, Windows};
use legion::prelude::*;
use std::{collections::HashSet, ops::Deref, sync::Arc};
pub struct WgpuRenderer {
pub device: Arc<wgpu::Device>,
pub queue: wgpu::Queue,
@ -85,7 +84,6 @@ impl WgpuRenderer {
let world = &*world;
let resources = &*resources;
actual_thread_count += 1;
// println!("spawn {}", resource_provider_chunk.len());
let render_resource_context = render_resource_context.clone();
s.spawn(move |_| {
let mut render_context =
@ -273,6 +271,7 @@ impl WgpuRenderer {
.context
.downcast_mut::<WgpuRenderResourceContext>()
.unwrap();
let mut render_context =
WgpuRenderContext::new(self.device.clone(), render_resource_context.clone());
if let Some(command_encoder) = encoder.take() {