diff --git a/bevy_render/Cargo.toml b/bevy_render/Cargo.toml index 979b9f6741..14897bd1e5 100644 --- a/bevy_render/Cargo.toml +++ b/bevy_render/Cargo.toml @@ -27,4 +27,5 @@ glam = "0.8.6" 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" \ No newline at end of file +once_cell = "1.3.1" +downcast-rs = "1.1.1" \ No newline at end of file diff --git a/bevy_render/src/renderer_2/render_resource_context.rs b/bevy_render/src/renderer_2/render_resource_context.rs index 5393775e05..c2fdbe00e3 100644 --- a/bevy_render/src/renderer_2/render_resource_context.rs +++ b/bevy_render/src/renderer_2/render_resource_context.rs @@ -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, - // TODO: why doesn't this work? - // pub context: Box, + pub context: Box, } impl GlobalRenderResourceContext { pub fn new(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::() - // } - - // pub fn downcast_mut(&self) -> &dyn RenderResourceContext { - // self.context.downcast_ref::() - // } } -// 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); \ No newline at end of file diff --git a/bevy_wgpu/src/renderer_2/wgpu_render_resource_context.rs b/bevy_wgpu/src/renderer_2/wgpu_render_resource_context.rs index c8b9ac1218..4298ff00d2 100644 --- a/bevy_wgpu/src/renderer_2/wgpu_render_resource_context.rs +++ b/bevy_wgpu/src/renderer_2/wgpu_render_resource_context.rs @@ -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 diff --git a/bevy_wgpu/src/wgpu_renderer.rs b/bevy_wgpu/src/wgpu_renderer.rs index da303a30b3..0ebba8acad 100644 --- a/bevy_wgpu/src/wgpu_renderer.rs +++ b/bevy_wgpu/src/wgpu_renderer.rs @@ -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, 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::() .unwrap(); + let mut render_context = WgpuRenderContext::new(self.device.clone(), render_resource_context.clone()); if let Some(command_encoder) = encoder.take() {