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,
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: attributes.shader_visibility,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true }, sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: view_dimension, view_dimension,
multisampled: false, multisampled: false,
}, },
count: None, None,
}, )
wgpu::BindGroupLayoutEntry { .with_entry(
binding: 1, attributes.shader_visibility,
visibility: attributes.shader_visibility, wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), None,
count: None, )
}, .build(context);
], let bind_group = BindGroupBuilder::new()
}); .with_layout(&bind_group_layout)
let bind_group = context .with_entry(wgpu::BindingResource::TextureView(&view))
.device .with_entry(wgpu::BindingResource::Sampler(&sampler))
.create_bind_group(&wgpu::BindGroupDescriptor { .build(context);
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,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false, has_dynamic_offset: false,
min_binding_size: None, min_binding_size: None,
}, },
count: None, None,
}], )
label: Some("camera_bind_group_layout"), .build(context);
}); let camera_bind_group = BindGroupBuilder::new()
let camera_bind_group = context .with_label("camera_bind_group")
.device .with_layout(&camera_bind_group_layout)
.create_bind_group(&wgpu::BindGroupDescriptor { .with_entry(camera_controller.get_buffer().as_entire_binding())
layout: &camera_bind_group_layout, .build(context);
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,
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::WriteOnly, access: wgpu::StorageTextureAccess::WriteOnly,
format: render_texture.attributes.format, format: render_texture.attributes.format,
view_dimension: wgpu::TextureViewDimension::D2, view_dimension: wgpu::TextureViewDimension::D2,
}, },
count: None, None,
}], )
}); .build(context);
let compute_bind_group = context let compute_bind_group = BindGroupBuilder::new()
.device .with_layout(&compute_layout)
.create_bind_group(&wgpu::BindGroupDescriptor { .with_entry(wgpu::BindingResource::TextureView(&render_texture.view))
label: None, .build(context);
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 {