Use bind group and bind group layout builders

This commit is contained in:
Jarrod Doyle 2023-04-21 12:27:31 +01:00
parent 20aeaa304d
commit c6932e6646
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 64 additions and 94 deletions

View File

@ -1,6 +1,6 @@
// TODO: Support mip-mapping and multi-sampling // TODO: Support mip-mapping and multi-sampling
use super::Context; use super::{BindGroupBuilder, BindGroupLayoutBuilder, Context};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TextureAttributes { pub struct TextureAttributes {
@ -141,46 +141,27 @@ impl Texture {
wgpu::TextureDimension::D3 => wgpu::TextureViewDimension::D3, wgpu::TextureDimension::D3 => wgpu::TextureViewDimension::D3,
}; };
let bind_group_layout = let bind_group_layout = BindGroupLayoutBuilder::new()
context .with_entry(
.device attributes.shader_visibility,
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { wgpu::BindingType::Texture {
label: None, sample_type: wgpu::TextureSampleType::Float { filterable: true },
entries: &[ view_dimension,
wgpu::BindGroupLayoutEntry { multisampled: false,
binding: 0, },
visibility: attributes.shader_visibility, None,
ty: wgpu::BindingType::Texture { )
sample_type: wgpu::TextureSampleType::Float { filterable: true }, .with_entry(
view_dimension: view_dimension, attributes.shader_visibility,
multisampled: false, wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
}, None,
count: None, )
}, .build(context);
wgpu::BindGroupLayoutEntry { let bind_group = BindGroupBuilder::new()
binding: 1, .with_layout(&bind_group_layout)
visibility: attributes.shader_visibility, .with_entry(wgpu::BindingResource::TextureView(&view))
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), .with_entry(wgpu::BindingResource::Sampler(&sampler))
count: None, .build(context);
},
],
});
let bind_group = context
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&sampler),
},
],
});
Self { Self {
attributes, attributes,

View File

@ -3,7 +3,7 @@ use winit::event::WindowEvent;
use crate::{ use crate::{
camera, camera,
render::{Context, Texture, TextureBuilder}, render::{BindGroupBuilder, BindGroupLayoutBuilder, Context, Texture, TextureBuilder},
}; };
pub(crate) struct Renderer { pub(crate) struct Renderer {
@ -93,32 +93,23 @@ impl Renderer {
0.25, 0.25,
); );
let camera_bind_group_layout = let camera_bind_group_layout = BindGroupLayoutBuilder::new()
context .with_label("camera_bind_group_layout")
.device .with_entry(
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { wgpu::ShaderStages::COMPUTE,
entries: &[wgpu::BindGroupLayoutEntry { wgpu::BindingType::Buffer {
binding: 0, ty: wgpu::BufferBindingType::Uniform,
visibility: wgpu::ShaderStages::COMPUTE, has_dynamic_offset: false,
ty: wgpu::BindingType::Buffer { min_binding_size: None,
ty: wgpu::BufferBindingType::Uniform, },
has_dynamic_offset: false, None,
min_binding_size: None, )
}, .build(context);
count: None, let camera_bind_group = BindGroupBuilder::new()
}], .with_label("camera_bind_group")
label: Some("camera_bind_group_layout"), .with_layout(&camera_bind_group_layout)
}); .with_entry(camera_controller.get_buffer().as_entire_binding())
let camera_bind_group = context .build(context);
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: camera_controller.get_buffer().as_entire_binding(),
}],
label: Some("camera_bind_group"),
});
log::info!("Creating render pipeline..."); log::info!("Creating render pipeline...");
let render_pipeline = let render_pipeline =
@ -152,32 +143,21 @@ impl Renderer {
log::info!("Creating compute pipeline..."); log::info!("Creating compute pipeline...");
let cs_descriptor = wgpu::include_wgsl!("../assets/shaders/voxel_volume.wgsl"); let cs_descriptor = wgpu::include_wgsl!("../assets/shaders/voxel_volume.wgsl");
let cs = context.device.create_shader_module(cs_descriptor); let cs = context.device.create_shader_module(cs_descriptor);
let compute_layout = let compute_layout = BindGroupLayoutBuilder::new()
context .with_entry(
.device wgpu::ShaderStages::COMPUTE,
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { wgpu::BindingType::StorageTexture {
label: None, access: wgpu::StorageTextureAccess::WriteOnly,
entries: &[wgpu::BindGroupLayoutEntry { format: render_texture.attributes.format,
binding: 0, view_dimension: wgpu::TextureViewDimension::D2,
visibility: wgpu::ShaderStages::COMPUTE, },
ty: wgpu::BindingType::StorageTexture { None,
access: wgpu::StorageTextureAccess::WriteOnly, )
format: render_texture.attributes.format, .build(context);
view_dimension: wgpu::TextureViewDimension::D2, let compute_bind_group = BindGroupBuilder::new()
}, .with_layout(&compute_layout)
count: None, .with_entry(wgpu::BindingResource::TextureView(&render_texture.view))
}], .build(context);
});
let compute_bind_group = context
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &compute_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&render_texture.view),
}],
});
let compute_pipeline = let compute_pipeline =
context context
.device .device
@ -217,6 +197,7 @@ impl Renderer {
} }
} }
// pub fn render(&mut self, context: &Context, encoders: &[wgpu::CommandEncoder]) {
pub fn render(&mut self, context: &Context) { pub fn render(&mut self, context: &Context) {
let frame = context.surface.get_current_texture().unwrap(); let frame = context.surface.get_current_texture().unwrap();
let view = frame let view = frame
@ -256,6 +237,14 @@ impl Renderer {
context.queue.submit(Some(encoder.finish())); context.queue.submit(Some(encoder.finish()));
frame.present(); frame.present();
// let mut command_buffers = Vec::with_capacity(encoders.len());
// for encoder in encoders {
// command_buffers.push(encoder.finish());
// }
// context.queue.submit(command_buffers);
// frame.present();
} }
pub fn input(&mut self, event: &WindowEvent) -> bool { pub fn input(&mut self, event: &WindowEvent) -> bool {