Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing the wgpu renderer to be created externally #2743

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to remove the Renderer resource if it exists right?

I would prefer an explicit check / branch over a removal here.

Copy link
Contributor Author

@Neo-Zhixing Neo-Zhixing Aug 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll create ownership problems if we leave renderer::Renderer in App. Specifically, RenderInstance is not Clone. So, you can have RenderInstance in the app or the render subapp, but not both.

.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