Recreate uniforms `BindGroup` when necessary

This commit is contained in:
Héctor Ramón Jiménez 2020-06-23 21:11:13 +02:00
parent f30a666dc8
commit ab53df8e9d
1 changed files with 30 additions and 8 deletions

View File

@ -16,6 +16,7 @@ const INDEX_BUFFER_SIZE: usize = 10_000;
pub(crate) struct Pipeline {
pipeline: wgpu::RenderPipeline,
blit: Option<msaa::Blit>,
constants_layout: wgpu::BindGroupLayout,
constants: wgpu::BindGroup,
uniforms_buffer: Buffer<Uniforms>,
vertex_buffer: Buffer<Vertex2D>,
@ -50,8 +51,10 @@ impl<T> Buffer<T> {
}
}
pub fn ensure_capacity(&mut self, device: &wgpu::Device, size: usize) {
if self.size < size {
pub fn expand(&mut self, device: &wgpu::Device, size: usize) -> bool {
let needs_resize = self.size < size;
if needs_resize {
self.raw = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: (std::mem::size_of::<T>() * size) as u64,
@ -60,6 +63,8 @@ impl<T> Buffer<T> {
self.size = size;
}
needs_resize
}
}
@ -69,7 +74,7 @@ impl Pipeline {
format: wgpu::TextureFormat,
antialiasing: Option<settings::Antialiasing>,
) -> Pipeline {
let constant_layout =
let constants_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
bindings: &[wgpu::BindGroupLayoutEntry {
@ -88,7 +93,7 @@ impl Pipeline {
let constant_bind_group =
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &constant_layout,
layout: &constants_layout,
bindings: &[wgpu::Binding {
binding: 0,
resource: wgpu::BindingResource::Buffer {
@ -100,7 +105,7 @@ impl Pipeline {
let layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&constant_layout],
bind_group_layouts: &[&constants_layout],
});
let vs = include_bytes!("shader/triangle.vert.spv");
@ -180,6 +185,7 @@ impl Pipeline {
Pipeline {
pipeline,
blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)),
constants_layout,
constants: constant_bind_group,
uniforms_buffer: constants_buffer,
vertex_buffer: Buffer::new(
@ -220,9 +226,25 @@ impl Pipeline {
// Then we ensure the current buffers are big enough, resizing if
// necessary
self.uniforms_buffer.ensure_capacity(device, meshes.len());
self.vertex_buffer.ensure_capacity(device, total_vertices);
self.index_buffer.ensure_capacity(device, total_indices);
let _ = self.vertex_buffer.expand(device, total_vertices);
let _ = self.index_buffer.expand(device, total_indices);
// If the uniforms buffer is resized, then we need to recreate its
// bind group.
if self.uniforms_buffer.expand(device, meshes.len()) {
self.constants =
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.constants_layout,
bindings: &[wgpu::Binding {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &self.uniforms_buffer.raw,
range: 0..std::mem::size_of::<Uniforms>() as u64,
},
}],
});
}
let mut uniforms: Vec<Uniforms> = Vec::with_capacity(meshes.len());
let mut offsets: Vec<(