Skip to content

Commit

Permalink
use queue instead of stagingbelt for buffer writes
Browse files Browse the repository at this point in the history
  • Loading branch information
profan committed Jun 8, 2021
1 parent 437b05a commit aef85eb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 132 deletions.
20 changes: 3 additions & 17 deletions examples/clipping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let surface = unsafe { instance.create_surface(&window) };

// Initialize GPU
let (device, queue) = futures::executor::block_on(async {
let (device, mut queue) = futures::executor::block_on(async {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
Expand All @@ -31,11 +31,6 @@ fn main() -> Result<(), Box<dyn Error>> {
.expect("Request device")
});

// Create staging belt and a local pool
let mut staging_belt = wgpu::util::StagingBelt::new(1024);
let mut local_pool = futures::executor::LocalPool::new();
let local_spawner = local_pool.spawner();

// Prepare swap chain
let render_format = wgpu::TextureFormat::Bgra8UnormSrgb;
let mut size = window.inner_size();
Expand Down Expand Up @@ -139,7 +134,7 @@ fn main() -> Result<(), Box<dyn Error>> {
glyph_brush
.draw_queued(
&device,
&mut staging_belt,
&mut queue,
&mut encoder,
&frame.view,
size.width,
Expand All @@ -160,7 +155,7 @@ fn main() -> Result<(), Box<dyn Error>> {
glyph_brush
.draw_queued_with_transform_and_scissoring(
&device,
&mut staging_belt,
&mut queue,
&mut encoder,
&frame.view,
wgpu_glyph::orthographic_projection(
Expand All @@ -177,17 +172,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.expect("Draw queued");

// Submit the work!
staging_belt.finish();
queue.submit(Some(encoder.finish()));

// Recall unused staging buffers
use futures::task::SpawnExt;

local_spawner
.spawn(staging_belt.recall())
.expect("Recall staging belt");

local_pool.run_until_stalled();
}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
Expand Down
18 changes: 2 additions & 16 deletions examples/depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let surface = unsafe { instance.create_surface(&window) };

// Initialize GPU
let (device, queue) = futures::executor::block_on(async {
let (device, mut queue) = futures::executor::block_on(async {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
Expand All @@ -33,11 +33,6 @@ fn main() -> Result<(), Box<dyn Error>> {
.expect("Request device")
});

// Create staging belt and a local pool
let mut staging_belt = wgpu::util::StagingBelt::new(1024);
let mut local_pool = futures::executor::LocalPool::new();
let local_spawner = local_pool.spawner();

// Prepare swap chain and depth buffer
let mut size = window.inner_size();
let mut new_size = None;
Expand Down Expand Up @@ -158,7 +153,7 @@ fn main() -> Result<(), Box<dyn Error>> {
glyph_brush
.draw_queued(
&device,
&mut staging_belt,
&mut queue,
&mut encoder,
&frame.view,
wgpu::RenderPassDepthStencilAttachment {
Expand All @@ -178,17 +173,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.expect("Draw queued");

// Submit the work!
staging_belt.finish();
queue.submit(Some(encoder.finish()));

// Recall unused staging buffers
use futures::task::SpawnExt;

local_spawner
.spawn(staging_belt.recall())
.expect("Recall staging belt");

local_pool.run_until_stalled();
}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
Expand Down
20 changes: 3 additions & 17 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let surface = unsafe { instance.create_surface(&window) };

// Initialize GPU
let (device, queue) = futures::executor::block_on(async {
let (device, mut queue) = futures::executor::block_on(async {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
Expand All @@ -31,11 +31,6 @@ fn main() -> Result<(), Box<dyn Error>> {
.expect("Request device")
});

// Create staging belt and a local pool
let mut staging_belt = wgpu::util::StagingBelt::new(1024);
let mut local_pool = futures::executor::LocalPool::new();
let local_spawner = local_pool.spawner();

// Prepare swap chain
let render_format = wgpu::TextureFormat::Bgra8UnormSrgb;
let mut size = window.inner_size();
Expand Down Expand Up @@ -148,7 +143,7 @@ fn main() -> Result<(), Box<dyn Error>> {
glyph_brush
.draw_queued(
&device,
&mut staging_belt,
&mut queue,
&mut encoder,
&frame.view,
size.width,
Expand All @@ -157,17 +152,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.expect("Draw queued");

// Submit the work!
staging_belt.finish();
queue.submit(Some(encoder.finish()));

// Recall unused staging buffers
use futures::task::SpawnExt;

local_spawner
.spawn(staging_belt.recall())
.expect("Recall staging belt");

local_pool.run_until_stalled();

}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
Expand Down
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ where
fn process_queued(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
) {
let pipeline = &mut self.pipeline;
Expand All @@ -153,7 +153,7 @@ where

pipeline.update_cache(
device,
staging_belt,
queue,
encoder,
offset,
size,
Expand Down Expand Up @@ -201,7 +201,7 @@ where

match brush_action.unwrap() {
BrushAction::Draw(verts) => {
self.pipeline.upload(device, staging_belt, encoder, &verts);
self.pipeline.upload(device, queue, encoder, &verts);
}
BrushAction::ReDraw => {}
};
Expand Down Expand Up @@ -243,15 +243,15 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<(), F, H> {
pub fn draw_queued(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
target_width: u32,
target_height: u32,
) -> Result<(), String> {
self.draw_queued_with_transform(
device,
staging_belt,
queue,
encoder,
target,
orthographic_projection(target_width, target_height),
Expand All @@ -273,15 +273,15 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<(), F, H> {
pub fn draw_queued_with_transform(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
transform: [f32; 16],
) -> Result<(), String> {
self.process_queued(device, staging_belt, encoder);
self.process_queued(device, queue, encoder);
self.pipeline.draw(
device,
staging_belt,
queue,
encoder,
target,
transform,
Expand All @@ -306,16 +306,16 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<(), F, H> {
pub fn draw_queued_with_transform_and_scissoring(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
transform: [f32; 16],
region: Region,
) -> Result<(), String> {
self.process_queued(device, staging_belt, encoder);
self.process_queued(device, queue, encoder);
self.pipeline.draw(
device,
staging_belt,
queue,
encoder,
target,
transform,
Expand Down Expand Up @@ -363,7 +363,7 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<wgpu::DepthStencilState, F, H> {
pub fn draw_queued(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
depth_stencil_attachment: wgpu::RenderPassDepthStencilAttachment,
Expand All @@ -372,7 +372,7 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<wgpu::DepthStencilState, F, H> {
) -> Result<(), String> {
self.draw_queued_with_transform(
device,
staging_belt,
queue,
encoder,
target,
depth_stencil_attachment,
Expand All @@ -395,16 +395,16 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<wgpu::DepthStencilState, F, H> {
pub fn draw_queued_with_transform(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
depth_stencil_attachment: wgpu::RenderPassDepthStencilAttachment,
transform: [f32; 16],
) -> Result<(), String> {
self.process_queued(device, staging_belt, encoder);
self.process_queued(device, queue, encoder);
self.pipeline.draw(
device,
staging_belt,
queue,
encoder,
target,
depth_stencil_attachment,
Expand All @@ -430,18 +430,18 @@ impl<F: Font + Sync, H: BuildHasher> GlyphBrush<wgpu::DepthStencilState, F, H> {
pub fn draw_queued_with_transform_and_scissoring(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
depth_stencil_attachment: wgpu::RenderPassDepthStencilAttachment,
transform: [f32; 16],
region: Region,
) -> Result<(), String> {
self.process_queued(device, staging_belt, encoder);
self.process_queued(device, queue, encoder);

self.pipeline.draw(
device,
staging_belt,
queue,
encoder,
target,
depth_stencil_attachment,
Expand Down
Loading

0 comments on commit aef85eb

Please sign in to comment.