Avoid locking to access resources when rendering. WGPU allows you to wrap all rendering resources in Arc which means we can avoid locks by directly storing data in bevy vs having the WGPU plugin store data.
Remove Arc<RwLock<HashMap<ResourceId, Resource>>>. Introduce WGPU types directly into bevy_render while also maintaining the abstraction layer. We can accomplish this by using enums instead.
Pros:
Cons:
use std::sync::Arc;
mod wgpu {
pub struct Texture {
}
}
mod other {
pub struct Texture {
}
}
pub enum Texture {
Wgpu(wgpu::Texture),
Other(other::Texture),
}
pub trait RenderContext {
fn create_texture(&self) -> Arc<Texture>;
}
pub struct WgpuRenderContext;
impl RenderContext for WgpuRenderContext {
fn create_texture(&self) -> Arc<Texture> {
Arc::new(Texture::Wgpu(wgpu::Texture { }))
}
}
pub struct OtherRenderContext;
impl RenderContext for OtherRenderContext {
fn create_texture(&self) -> Arc<Texture> {
Arc::new(Texture::Other(other::Texture { }))
}
}
fn main() {
let wgpu_render_context = WgpuRenderContext {
};
wgpu_render_context.create_texture();
let other_render_context = OtherRenderContext {
};
other_render_context.create_texture();
}
Playground Link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b184ea7b5338a23971d5bd51ba66e489
Did you forget solution B?
I was kinda hoping someone else would present a Solution B! 馃槃 You could use generics, but that gets overly complex in a lot of cases. @aclysma also suggested using box/downcast. It might be a better soultion. I think some sort of benchmark test between the two options might be a good way to move forward.
I've opened a draft PR with a partial implementation with Textures here:
https://github.com/bevyengine/bevy/pull/302
Some challenges I encountered:
You noted that performance might suffer, but actually, from my experience, performance will increase (though probably stay the same because the bottleneck here isn't calling wgpu apis). Matching on enums let's the compiler do a lot more inlining, whereas a dynamic trait call doesn't allow that.
Most helpful comment
Did you forget solution B?