Renderer: one command buffer per frame, uploads before the first pass
wgpu-core ends its Metal command buffer at every pass: each render or compute pass costs two command buffers (the pass and its barriers), each run of copies between passes one more, and Metal creates and commits every one on its own. The frame made about 18: the section copies in their own encoder, the queue's pending writes for the HUD's fire animation, copies before and after the flatten pass, and the screen effects' and HUD's vertex uploads between later passes. At render distance 32 that cost 2-5 ms of submit per frame, mostly waiting on the command queue's lock. The frame now records after the section copies in their encoder, stages the fire animation and the HUD's glyph patches through the frame's belt instead of Queue::write_texture, and records every upload (screen effects, HUD, flatten inputs, indirect draws) before the first pass. The commands and their order within each pass are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -332,7 +332,7 @@ pub struct Renderer {
|
||||
animations: AnimatedSprites,
|
||||
uploads: upload::Uploads,
|
||||
/// Section meshes are staged through their own belt into
|
||||
/// `section_copies`, which the next submission runs ahead of the frame:
|
||||
/// `section_copies`, which the next frame records its commands after:
|
||||
/// `Queue::write_buffer` would make a staging buffer per write.
|
||||
section_uploads: upload::Uploads,
|
||||
section_copies: Option<wgpu::CommandEncoder>,
|
||||
@@ -2394,14 +2394,21 @@ impl Renderer {
|
||||
Some(f) => f.texture.clone(),
|
||||
None => self.offscreen.clone().ok_or("no offscreen target")?,
|
||||
};
|
||||
let mut encoder = self
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Horizon frame"),
|
||||
});
|
||||
// One command buffer for the frame, after the section copies staged
|
||||
// since the last one: Metal commits every command buffer wgpu-core
|
||||
// makes (one per run of copies and two per pass) on its own.
|
||||
let section_copies = self.section_copies.take();
|
||||
let has_section_copies = section_copies.is_some();
|
||||
let mut encoder = section_copies.unwrap_or_else(|| {
|
||||
self.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Horizon frame"),
|
||||
})
|
||||
});
|
||||
if self.animated_textures {
|
||||
self.animate(&mut encoder, frame.ticks);
|
||||
self.hud.animate_fire(&self.queue, frame.ticks);
|
||||
self.hud
|
||||
.animate_fire(&mut self.uploads, &mut encoder, frame.ticks);
|
||||
}
|
||||
self.update_lightmap(
|
||||
&mut encoder,
|
||||
@@ -2661,48 +2668,6 @@ impl Renderer {
|
||||
} else {
|
||||
self.draws.len()
|
||||
};
|
||||
let flat_frame = match &mut self.flat {
|
||||
Some(flat)
|
||||
if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws =>
|
||||
{
|
||||
let sections = &self.sections;
|
||||
let sorted = &self.sorted_indices;
|
||||
crate::spans::time("flatten", || {
|
||||
flat.prepare(
|
||||
&self.device,
|
||||
&mut encoder,
|
||||
&mut self.uploads,
|
||||
&self.draws,
|
||||
&self.draw_pages,
|
||||
solid_draws as usize,
|
||||
visible
|
||||
.iter()
|
||||
.filter_map(|v| sections.get(v.slot).flat_id.map(|id| (id, v.origin))),
|
||||
|page| sorted.buffer(page).clone(),
|
||||
)
|
||||
})
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
if flat_frame {
|
||||
self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.calls.len());
|
||||
} else if self.multi_draw && !self.draws.is_empty() {
|
||||
if self.draws.len() as u64 > self.indirect_capacity {
|
||||
self.indirect_capacity = (self.draws.len() as u64).next_power_of_two();
|
||||
self.indirect = self.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Section draws"),
|
||||
size: self.indirect_capacity * 20,
|
||||
usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
}
|
||||
self.uploads.buffer(
|
||||
&mut encoder,
|
||||
&self.indirect,
|
||||
0,
|
||||
bytemuck::cast_slice(&self.draws),
|
||||
);
|
||||
}
|
||||
let debug_depth = frame.debug_lines.len() as u32;
|
||||
let debug_top = frame.debug_lines_on_top.len() as u32;
|
||||
let crosshair = frame.debug_crosshair.len() as u32;
|
||||
@@ -2839,6 +2804,77 @@ impl Renderer {
|
||||
bytemuck::bytes_of(&uniform),
|
||||
);
|
||||
}
|
||||
// Every upload before the frame's first pass: a pass ends wgpu-core's
|
||||
// command buffer, so copies between passes would need one more.
|
||||
if !frame.screen.is_empty() {
|
||||
let vertices: Vec<crate::screen_effects::ScreenVertex> =
|
||||
frame.screen.iter().flat_map(|q| q.vertices).collect();
|
||||
if vertices.len() as u64 > self.screen_capacity {
|
||||
self.screen_capacity = (vertices.len() as u64).next_power_of_two();
|
||||
self.screen_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Screen effect vertices"),
|
||||
size: self.screen_capacity
|
||||
* std::mem::size_of::<crate::screen_effects::ScreenVertex>() as u64,
|
||||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
}
|
||||
self.uploads.buffer(
|
||||
&mut encoder,
|
||||
&self.screen_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&vertices),
|
||||
);
|
||||
}
|
||||
self.hud.prepare(
|
||||
&self.device,
|
||||
&mut self.uploads,
|
||||
&mut encoder,
|
||||
frame.hud,
|
||||
[self.config.width, self.config.height],
|
||||
);
|
||||
let flat_frame = match &mut self.flat {
|
||||
Some(flat)
|
||||
if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws =>
|
||||
{
|
||||
let sections = &self.sections;
|
||||
let sorted = &self.sorted_indices;
|
||||
crate::spans::time("flatten", || {
|
||||
flat.prepare(
|
||||
&self.device,
|
||||
&mut encoder,
|
||||
&mut self.uploads,
|
||||
&self.draws,
|
||||
&self.draw_pages,
|
||||
solid_draws as usize,
|
||||
visible
|
||||
.iter()
|
||||
.filter_map(|v| sections.get(v.slot).flat_id.map(|id| (id, v.origin))),
|
||||
|page| sorted.buffer(page).clone(),
|
||||
)
|
||||
})
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
if flat_frame {
|
||||
self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.calls.len());
|
||||
} else if self.multi_draw && !self.draws.is_empty() {
|
||||
if self.draws.len() as u64 > self.indirect_capacity {
|
||||
self.indirect_capacity = (self.draws.len() as u64).next_power_of_two();
|
||||
self.indirect = self.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Section draws"),
|
||||
size: self.indirect_capacity * 20,
|
||||
usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
}
|
||||
self.uploads.buffer(
|
||||
&mut encoder,
|
||||
&self.indirect,
|
||||
0,
|
||||
bytemuck::cast_slice(&self.draws),
|
||||
);
|
||||
}
|
||||
let view_target = target_texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
format: Some(self.config.format.remove_srgb_suffix()),
|
||||
..Default::default()
|
||||
@@ -3102,24 +3138,6 @@ impl Renderer {
|
||||
);
|
||||
}
|
||||
if !frame.screen.is_empty() {
|
||||
let vertices: Vec<crate::screen_effects::ScreenVertex> =
|
||||
frame.screen.iter().flat_map(|q| q.vertices).collect();
|
||||
if vertices.len() as u64 > self.screen_capacity {
|
||||
self.screen_capacity = (vertices.len() as u64).next_power_of_two();
|
||||
self.screen_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Screen effect vertices"),
|
||||
size: self.screen_capacity
|
||||
* std::mem::size_of::<crate::screen_effects::ScreenVertex>() as u64,
|
||||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
}
|
||||
self.uploads.buffer(
|
||||
&mut encoder,
|
||||
&self.screen_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&vertices),
|
||||
);
|
||||
let attachments = [Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view_target,
|
||||
resolve_target: None,
|
||||
@@ -3212,9 +3230,6 @@ impl Renderer {
|
||||
);
|
||||
}
|
||||
self.hud.draw(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
&mut self.uploads,
|
||||
&mut encoder,
|
||||
&view_target,
|
||||
&self.depth,
|
||||
@@ -3229,12 +3244,12 @@ impl Renderer {
|
||||
.take()
|
||||
.map(|path| (path, self.copy_frame(&mut encoder, &target_texture)));
|
||||
self.uploads.finish(&encoder);
|
||||
if has_section_copies {
|
||||
self.section_copy_bytes = 0;
|
||||
self.section_uploads.finish(&encoder);
|
||||
}
|
||||
let commands = crate::spans::time("encode", || encoder.finish());
|
||||
// The section copies run first: this frame draws what they write.
|
||||
let copies = self.take_section_copies();
|
||||
let submission = crate::spans::time("submit", || {
|
||||
self.queue.submit(copies.into_iter().chain([commands]))
|
||||
});
|
||||
let submission = crate::spans::time("submit", || self.queue.submit([commands]));
|
||||
if let Some(timer) = &mut self.gpu_timer {
|
||||
timer.submitted();
|
||||
timer.collect(&self.device, &mut self.gpu_times);
|
||||
|
||||
@@ -36,7 +36,9 @@ impl Uploads {
|
||||
let row = width as usize * 4;
|
||||
debug_assert_eq!(pixels.len(), row * height as usize);
|
||||
let padded = row.next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize);
|
||||
let size = NonZeroU64::new((padded * height as usize) as u64).expect("non-empty texture");
|
||||
let Some(size) = NonZeroU64::new((padded * height as usize) as u64) else {
|
||||
return;
|
||||
};
|
||||
let alignment = NonZeroU64::new(u64::from(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT))
|
||||
.expect("non-zero alignment");
|
||||
let slice = self.belt.allocate(size, alignment);
|
||||
|
||||
@@ -977,7 +977,12 @@ impl HudRenderer {
|
||||
capacity,
|
||||
})
|
||||
}
|
||||
pub fn animate_fire(&mut self, queue: &wgpu::Queue, tick: u64) {
|
||||
pub fn animate_fire(
|
||||
&mut self,
|
||||
uploads: &mut crate::gfx::upload::Uploads,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
tick: u64,
|
||||
) {
|
||||
let Some(texture) = &self.atlas_texture else {
|
||||
return;
|
||||
};
|
||||
@@ -987,7 +992,8 @@ impl HudRenderer {
|
||||
continue;
|
||||
}
|
||||
let pixels = animation.resized_pixels(frame, rect.w, rect.h);
|
||||
queue.write_texture(
|
||||
uploads.texture(
|
||||
encoder,
|
||||
wgpu::TexelCopyTextureInfo {
|
||||
texture,
|
||||
mip_level: 0,
|
||||
@@ -998,35 +1004,27 @@ impl HudRenderer {
|
||||
},
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
},
|
||||
rect.w,
|
||||
rect.h,
|
||||
&pixels,
|
||||
wgpu::TexelCopyBufferLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(rect.w * 4),
|
||||
rows_per_image: Some(rect.h),
|
||||
},
|
||||
wgpu::Extent3d {
|
||||
width: rect.w,
|
||||
height: rect.h,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
);
|
||||
*previous = frame;
|
||||
}
|
||||
}
|
||||
pub fn draw(
|
||||
/// Stage the batch's atlas patches, vertices and uniform: before the
|
||||
/// frame's first pass, so the copies share one command buffer.
|
||||
pub fn prepare(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
uploads: &mut crate::gfx::upload::Uploads,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
target: &wgpu::TextureView,
|
||||
depth: &wgpu::TextureView,
|
||||
batch: &HudBatch,
|
||||
size: [u32; 2],
|
||||
) {
|
||||
if let Some(texture) = &self.atlas_texture {
|
||||
for p in &batch.patches {
|
||||
queue.write_texture(
|
||||
uploads.texture(
|
||||
encoder,
|
||||
wgpu::TexelCopyTextureInfo {
|
||||
texture,
|
||||
mip_level: 0,
|
||||
@@ -1037,17 +1035,9 @@ impl HudRenderer {
|
||||
},
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
},
|
||||
p.width,
|
||||
p.height,
|
||||
&p.rgba,
|
||||
wgpu::TexelCopyBufferLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(p.width * 4),
|
||||
rows_per_image: None,
|
||||
},
|
||||
wgpu::Extent3d {
|
||||
width: p.width,
|
||||
height: p.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1078,6 +1068,19 @@ impl HudRenderer {
|
||||
size: [size[0] as f32, size[1] as f32, batch.scale, 0.0],
|
||||
}),
|
||||
);
|
||||
}
|
||||
/// Draw the batch `prepare` staged.
|
||||
pub fn draw(
|
||||
&self,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
target: &wgpu::TextureView,
|
||||
depth: &wgpu::TextureView,
|
||||
batch: &HudBatch,
|
||||
size: [u32; 2],
|
||||
) {
|
||||
if batch.vertices.is_empty() || size.contains(&0) {
|
||||
return;
|
||||
}
|
||||
let attachments = [Some(wgpu::RenderPassColorAttachment {
|
||||
view: target,
|
||||
resolve_target: None,
|
||||
|
||||
Reference in New Issue
Block a user