Remove even more wgpu commands from the main thread.

This commit is contained in:
Joshua Yanovski 2022-08-16 02:09:27 -07:00
parent d38ce95b22
commit 2301b7f47a
4 changed files with 31 additions and 23 deletions

View File

@ -171,7 +171,7 @@ impl Locals {
}
}
pub type BoundLocals = Bound<()>;
pub type BoundLocals = Arc<Bound<()>>;
pub struct TerrainLayout {
pub locals: wgpu::BindGroupLayout,
@ -189,7 +189,7 @@ impl TerrainLayout {
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
has_dynamic_offset: true,
min_binding_size: None,
},
count: None,
@ -199,7 +199,7 @@ impl TerrainLayout {
}
}
pub fn bind_locals(&self, device: &wgpu::Device, locals: &Consts<Locals>, offset: usize) -> BoundLocals {
pub fn bind_locals(&self, device: &wgpu::Device, locals: &Consts<Locals>/*, offset: usize*/) -> BoundLocals {
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.locals,
@ -207,16 +207,16 @@ impl TerrainLayout {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: locals.buf(),
offset: (offset * mem::size_of::<Locals>()) as wgpu::BufferAddress,
offset: /* (offset * mem::size_of::<Locals>()) */0 as wgpu::BufferAddress,
size: wgpu::BufferSize::new(mem::size_of::<Locals>() as u64),
})
}],
});
BoundLocals {
Arc::new(Bound {
bind_group,
with: /*locals*/(),
}
})
}
}

View File

@ -79,7 +79,7 @@ impl Renderer {
pub fn create_terrain_bound_locals(
&mut self,
locals: /*Arc<*/&Consts<terrain::Locals>/*>*/,
offset: usize,
/* offset: usize, */
) -> /*for<'a> Fn(&'a [terrain::Locals]) -> terrain::BoundLocals + Send + Sync*//* impl Fn() -> terrain::BoundLocals + Send + Sync */terrain::BoundLocals {
/* let device = Arc::clone(&self.device);
let immutable = Arc::clone(&self.layouts.immutable);
@ -87,7 +87,7 @@ impl Renderer {
let locals = Consts::new_mapped(&device, 1);
immutable.terrain.bind_locals(&device, locals)
} */
self.layouts.immutable.terrain.bind_locals(&self.device, locals, offset)
self.layouts.immutable.terrain.bind_locals(&self.device, locals/* , offset */)
}
pub fn create_shadow_bound_locals(&mut self, locals: &[shadow::Locals]) -> shadow::BoundLocals {

View File

@ -465,7 +465,7 @@ impl<'frame> Drawer<'frame> {
&mut self,
matrices: &[shadow::PointLightMatrix; 126],
chunks: impl Clone
+ Iterator<Item = (&'data Model<terrain::Vertex>, &'data terrain::BoundLocals)>,
+ Iterator<Item = (&'data Model<terrain::Vertex>, &'data (wgpu::DynamicOffset, terrain::BoundLocals))>,
) {
if !self.borrow.pipeline_modes.shadow.is_map() {
return;
@ -524,8 +524,8 @@ impl<'frame> Drawer<'frame> {
&data[(6 * (point_light + 1) * STRIDE + face as usize * STRIDE)
..(6 * (point_light + 1) * STRIDE + (face + 1) as usize * STRIDE)],
);
chunks.clone().for_each(|(model, locals)| {
render_pass.set_bind_group(1, &locals.bind_group, &[]);
chunks.clone().for_each(|(model, &(locals_offset, ref locals))| {
render_pass.set_bind_group(1, &locals.bind_group, &[locals_offset]);
render_pass.set_vertex_buffer(0, model.buf().slice(..));
render_pass.draw_indexed(0..model.len() as u32 / 4 * 6, 0, 0..1);
});
@ -746,9 +746,9 @@ impl<'pass_ref, 'pass: 'pass_ref> TerrainShadowDrawer<'pass_ref, 'pass> {
pub fn draw<'data: 'pass>(
&mut self,
model: &'data Model<terrain::Vertex>,
locals: &'data terrain::BoundLocals,
&(locals_offset, ref locals): &'data (wgpu::DynamicOffset, terrain::BoundLocals),
) {
self.render_pass.set_bind_group(1, &locals.bind_group, &[]);
self.render_pass.set_bind_group(1, &locals.bind_group, &[locals_offset]);
self.render_pass.set_vertex_buffer(0, model.buf().slice(..));
self.render_pass
.draw_indexed(0..model.len() as u32 / 4 * 6, 0, 0..1);
@ -924,7 +924,7 @@ impl<'pass_ref, 'pass: 'pass_ref> TerrainDrawer<'pass_ref, 'pass> {
&mut self,
model: &'data Model<terrain::Vertex>,
col_lights: &'data Arc<ColLights<terrain::Locals>>,
locals: &'data terrain::BoundLocals,
&(locals_offset, ref locals): &'data (wgpu::DynamicOffset, terrain::BoundLocals),
) {
if self.col_lights
// Check if we are still using the same atlas texture as the previous drawn
@ -937,7 +937,7 @@ impl<'pass_ref, 'pass: 'pass_ref> TerrainDrawer<'pass_ref, 'pass> {
self.col_lights = Some(col_lights);
};
self.render_pass.set_bind_group(3, &locals.bind_group, &[]);
self.render_pass.set_bind_group(3, &locals.bind_group, &[locals_offset]);
self.render_pass.set_vertex_buffer(0, model.buf().slice(..));
self.render_pass
.draw_indexed(0..model.len() as u32 / 4 * 6, 0, 0..1);
@ -975,11 +975,11 @@ pub struct SpriteDrawer<'pass_ref, 'pass: 'pass_ref> {
impl<'pass_ref, 'pass: 'pass_ref> SpriteDrawer<'pass_ref, 'pass> {
pub fn draw<'data: 'pass>(
&mut self,
terrain_locals: &'data terrain::BoundLocals,
&(terrain_locals_offset, ref terrain_locals): &'data (wgpu::DynamicOffset, terrain::BoundLocals),
instances: &'data Instances<sprite::Instance>,
) {
self.render_pass
.set_bind_group(3, &terrain_locals.bind_group, &[]);
.set_bind_group(3, &terrain_locals.bind_group, &[terrain_locals_offset]);
self.render_pass
.set_vertex_buffer(0, instances.buf().slice(..));
@ -1027,10 +1027,10 @@ impl<'pass_ref, 'pass: 'pass_ref> FluidDrawer<'pass_ref, 'pass> {
pub fn draw<'data: 'pass>(
&mut self,
model: &'data Model<fluid::Vertex>,
locals: &'data terrain::BoundLocals,
&(locals_offset, ref locals): &'data (wgpu::DynamicOffset, terrain::BoundLocals),
) {
self.render_pass.set_vertex_buffer(0, model.buf().slice(..));
self.render_pass.set_bind_group(2, &locals.bind_group, &[]);
self.render_pass.set_bind_group(2, &locals.bind_group, &[locals_offset]);
self.render_pass
.draw_indexed(0..model.len() as u32 / 4 * 6, 0, 0..1);
}
@ -1187,3 +1187,5 @@ fn set_quad_index_buffer<'a, V: super::Vertex>(
pass.set_index_buffer(slice, format);
}
}

View File

@ -92,7 +92,7 @@ pub struct TerrainChunkData {
light_map: LightMapFn,
glow_map: LightMapFn,
sprite_instances: [Instances<SpriteInstance>; SPRITE_LOD_LEVELS],
locals: pipelines::terrain::BoundLocals,
locals: (wgpu::DynamicOffset, pipelines::terrain::BoundLocals),
pub blocks_of_interest: BlocksOfInterest,
visible: Visibility,
@ -1339,7 +1339,7 @@ impl/*<V: RectRasterableVol>*/ Terrain<V> {
scene_data.state.get_delta_time() * CHUNKS_PER_SECOND + self.mesh_recv_overflow;
self.mesh_recv_overflow = recv_count.fract();
let mesh_recv = &self.mesh_recv;
let max_recv_count = self.mesh_todos_active.load(Ordering::Relaxed).min(recv_count.floor() as u64);
let max_recv_count = self.mesh_todos_active.load(Ordering::Relaxed)/*.min(recv_count.floor() as u64)*/;
let incoming_chunks =
std::iter::from_fn(|| mesh_recv.try_recv().ok())
.take(/* recv_count.floor() as usize */max_recv_count as usize);
@ -1349,6 +1349,7 @@ impl/*<V: RectRasterableVol>*/ Terrain<V> {
// be some unused slots, which is fine.
let locals = /*Arc::new(*/renderer.create_consts_mapped(max_recv_count as usize)/*)*/;
let mut locals_buffer = renderer.get_consts_mapped(&locals);
let mut locals_bound = renderer.create_terrain_bound_locals(&locals/*, locals_offset */);
let mut encoder = renderer.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Update textures."),
@ -1459,6 +1460,8 @@ impl/*<V: RectRasterableVol>*/ Terrain<V> {
depth_or_array_layers: 1,
},
);
// Drop image on background thread.
slowjob.spawn(&"TERRAIN_DROP", move || { drop(tex); });
// Update the memory mapped locals.
let locals_buffer_ =
@ -1483,7 +1486,8 @@ impl/*<V: RectRasterableVol>*/ Terrain<V> {
light_map: mesh.light_map,
glow_map: mesh.glow_map,
sprite_instances,
locals: /* mesh.locals */renderer.create_terrain_bound_locals(&locals, locals_offset),
locals: /* mesh.locals *//*renderer.create_terrain_bound_locals(&locals/*, locals_offset */)*/
((locals_offset * core::mem::size_of::<TerrainLocals>()) as wgpu::DynamicOffset, Arc::clone(&locals_bound)),
visible: Visibility {
in_range: false,
in_frustum: false,
@ -1514,6 +1518,8 @@ impl/*<V: RectRasterableVol>*/ Terrain<V> {
// Drop the memory mapping and unmap the locals.
drop(locals_buffer);
renderer.unmap_consts(&locals);
// Drop buffer on background thread.
slowjob.spawn(&"TERRAIN_DROP", move || { drop(locals); });
/* // TODO: Delay submission, don't just submit immediately out of convenience!
renderer.queue.submit(std::iter::once(encoder.finish())); */
self.command_buffers.push(encoder.finish());
@ -1829,7 +1835,7 @@ impl/*<V: RectRasterableVol>*/ Terrain<V> {
+ Iterator<
Item = (
&Model<pipelines::terrain::Vertex>,
&pipelines::terrain::BoundLocals,
&(wgpu::DynamicOffset, pipelines::terrain::BoundLocals),
),
> {
let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {