Skip to content

Commit

Permalink
Extract RenderDevice from world to avoid manual drop.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Aug 3, 2024
1 parent 285899e commit cbe0da6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
5 changes: 1 addition & 4 deletions examples/wgpu/wgpu_instancing/wgpu_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn special_color(index: usize) -> [f32; 3] {
}
}

fn render(app: &RenderApp, model: &Model, frame: Frame) {
fn render(app: &RenderApp, model: &Model, mut frame: Frame) {
let mut g = model.graphics.lock().unwrap();

// If the window has changed size, recreate our depth texture to match.
Expand Down Expand Up @@ -342,9 +342,6 @@ fn render(app: &RenderApp, model: &Model, frame: Frame) {
usage,
});

// Drop the device to avoid borrowing issues
drop(device);

let mut encoder = frame.command_encoder();
encoder.copy_buffer_to_buffer(&new_uniform_buffer, 0, &g.uniform_buffer, 0, uniforms_size);
let mut render_pass = wgpu::RenderPassBuilder::new()
Expand Down
3 changes: 0 additions & 3 deletions examples/wgpu/wgpu_teapot/wgpu_teapot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ fn render(app: &RenderApp, model: &Model, frame: Frame) {
usage,
});

// Avoid borrowing issues by dropping device when finished.
drop(device);

let mut encoder = frame.command_encoder();
encoder.copy_buffer_to_buffer(&new_uniform_buffer, 0, &g.uniform_buffer, 0, uniforms_size);
let mut render_pass = wgpu::RenderPassBuilder::new()
Expand Down
11 changes: 6 additions & 5 deletions nannou/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::ecs::entity::EntityHashMap;
use bevy::prelude::*;
use bevy::render::render_resource::Extent3d;
use bevy::render::renderer::RenderContext;
use bevy::render::renderer::{RenderContext, RenderDevice};
use bevy::render::view::{ExtractedView, ExtractedWindows, ViewTarget};
use bevy::render::{Extract, RenderApp};
use nannou_core::geom;
Expand Down Expand Up @@ -39,6 +39,7 @@ pub struct Frame<'a, 'w> {
view_target: &'w ViewTarget,
extracted_windows: &'w ExtractedWindows,
extracted_view: &'w ExtractedView,
render_device: &'w RenderDevice,
render_context: RefCell<&'a mut RenderContext<'w>>,
}

Expand All @@ -54,10 +55,12 @@ impl<'a, 'w> Frame<'a, 'w> {
extracted_view: &'w ExtractedView,
render_context: &'a mut RenderContext<'w>,
) -> Self {
let render_device = world.resource::<RenderDevice>();
Frame {
window_id: view_target_id,
world,
view_target,
render_device,
render_context: RefCell::new(render_context),
extracted_windows,
extracted_view,
Expand Down Expand Up @@ -115,10 +118,8 @@ impl<'a, 'w> Frame<'a, 'w> {
///
/// This refers to the same **DeviceQueuePair** as held by the window associated with this
/// frame.
pub fn device(&self) -> std::cell::Ref<wgpu::Device> {
std::cell::Ref::map(self.render_context.borrow(), |x| {
x.render_device().wgpu_device()
})
pub fn device(&self) -> &wgpu::Device {
self.render_device.wgpu_device()
}

/// The texture to which all graphics should be drawn this frame.
Expand Down

0 comments on commit cbe0da6

Please sign in to comment.