Skip to content

Commit

Permalink
Allowing wgpu renderer to be created separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Zhixing committed Oct 9, 2021
1 parent 43e8a15 commit 7df77dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
32 changes: 18 additions & 14 deletions pipelined/bevy_render2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,21 @@ struct ScratchRenderWorld(World);

impl Plugin for RenderPlugin {
fn build(&self, app: &mut App) {
let (instance, device, queue) =
futures_lite::future::block_on(renderer::initialize_renderer(
wgpu::util::backend_bits_from_env().unwrap_or(Backends::PRIMARY),
&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
..Default::default()
},
&wgpu::DeviceDescriptor::default(),
));
app.insert_resource(device.clone())
.insert_resource(queue.clone())
let renderer = app
.world
.remove_resource::<renderer::Renderer>()
.unwrap_or_else(|| {
futures_lite::future::block_on(renderer::initialize_renderer(
wgpu::util::backend_bits_from_env().unwrap_or(Backends::PRIMARY),
&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
..Default::default()
},
&wgpu::DeviceDescriptor::default(),
))
});
app.insert_resource(renderer.device.clone())
.insert_resource(renderer.queue.clone())
.init_resource::<ScratchRenderWorld>();
let asset_server = app.world.get_resource::<AssetServer>().unwrap().clone();

Expand All @@ -113,9 +117,9 @@ impl Plugin for RenderPlugin {
SystemStage::parallel().with_system(render_system.exclusive_system()),
)
.add_stage(RenderStage::Cleanup, SystemStage::parallel())
.insert_resource(instance)
.insert_resource(device)
.insert_resource(queue)
.insert_resource(renderer.instance)
.insert_resource(renderer.device)
.insert_resource(renderer.queue)
.insert_resource(asset_server)
.init_resource::<RenderGraph>();

Expand Down
14 changes: 12 additions & 2 deletions pipelined/bevy_render2/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ pub fn render_system(world: &mut World) {
pub type RenderQueue = Arc<Queue>;
pub type RenderInstance = Instance;

pub struct Renderer {
pub instance: RenderInstance,
pub device: RenderDevice,
pub queue: RenderQueue,
}

pub async fn initialize_renderer(
backends: Backends,
request_adapter_options: &RequestAdapterOptions<'_>,
device_descriptor: &DeviceDescriptor<'_>,
) -> (RenderInstance, RenderDevice, RenderQueue) {
) -> Renderer {
let instance = wgpu::Instance::new(backends);

let adapter = instance
Expand All @@ -72,7 +78,11 @@ pub async fn initialize_renderer(
.unwrap();
let device = Arc::new(device);
let queue = Arc::new(queue);
(instance, RenderDevice::from(device), queue)
Renderer {
instance,
device: RenderDevice::from(device),
queue,
}
}

pub struct RenderContext {
Expand Down

0 comments on commit 7df77dc

Please sign in to comment.