more comments and minor cleanup

This commit is contained in:
Anton-4 2020-12-12 18:11:57 +01:00
parent ea3f15f950
commit fc4d510c40
2 changed files with 12 additions and 8 deletions

View file

@ -178,22 +178,23 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
}, },
); );
//update orthographic buffer according to new window size // update orthographic buffer according to new window size
let new_uniforms = Uniforms::new(size.width, size.height); let new_uniforms = Uniforms::new(size.width, size.height);
let new_ortho_buffer = let new_ortho_buffer =
gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor { gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Ortho Uniform Buffer"), label: Some("Ortho uniform buffer"),
contents: bytemuck::cast_slice(&[new_uniforms]), contents: bytemuck::cast_slice(&[new_uniforms]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_SRC, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_SRC,
}); });
// Get a command encoder for the current frame // get a command encoder for the current frame
let mut encoder = let mut encoder =
gpu_device.create_command_encoder(&wgpu::CommandEncoderDescriptor { gpu_device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Resize"), label: Some("Resize"),
}); });
// overwrite the new buffer over the old one
encoder.copy_buffer_to_buffer( encoder.copy_buffer_to_buffer(
&new_ortho_buffer, &new_ortho_buffer,
0, 0,
@ -301,12 +302,14 @@ fn make_rect_pipeline(
) -> (wgpu::RenderPipeline, BindGroup, Buffer) { ) -> (wgpu::RenderPipeline, BindGroup, Buffer) {
let uniforms = Uniforms::new(swap_chain_descr.width, swap_chain_descr.height); let uniforms = Uniforms::new(swap_chain_descr.width, swap_chain_descr.height);
// orthographic projection is used to transfrom pixel coords to the coordinate system used by wgpu
let ortho_buffer = gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let ortho_buffer = gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Ortho Uniform Buffer"), label: Some("Ortho uniform buffer"),
contents: bytemuck::cast_slice(&[uniforms]), contents: bytemuck::cast_slice(&[uniforms]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
}); });
// bind groups consist of extra resources that are provided to the shaders
let ortho_bind_group_layout = gpu_device.create_bind_group_layout(&BindGroupLayoutDescriptor { let ortho_bind_group_layout = gpu_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[BindGroupLayoutEntry { entries: &[BindGroupLayoutEntry {
binding: 0, binding: 0,
@ -317,7 +320,7 @@ fn make_rect_pipeline(
}, },
count: None, count: None,
}], }],
label: None, label: Some("Ortho bind group layout"),
}); });
let ortho_bind_group = gpu_device.create_bind_group(&wgpu::BindGroupDescriptor { let ortho_bind_group = gpu_device.create_bind_group(&wgpu::BindGroupDescriptor {
@ -326,13 +329,13 @@ fn make_rect_pipeline(
binding: 0, binding: 0,
resource: wgpu::BindingResource::Buffer(ortho_buffer.slice(..)), resource: wgpu::BindingResource::Buffer(ortho_buffer.slice(..)),
}], }],
label: Some("ortho_bind_group"), label: Some("Ortho bind group"),
}); });
let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&ortho_bind_group_layout], bind_group_layouts: &[&ortho_bind_group_layout],
push_constant_ranges: &[], push_constant_ranges: &[],
label: Some("Rectangle Pipeline Layout"), label: Some("Rectangle pipeline layout"),
}); });
let pipeline = create_render_pipeline( let pipeline = create_render_pipeline(
&gpu_device, &gpu_device,
@ -358,7 +361,7 @@ fn create_render_pipeline(
let fs_module = device.create_shader_module(fs_src); let fs_module = device.create_shader_module(fs_src);
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"), label: Some("Render pipeline"),
layout: Some(&layout), layout: Some(&layout),
vertex_stage: wgpu::ProgrammableStageDescriptor { vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module, module: &vs_module,

View file

@ -4,6 +4,7 @@
// https://github.com/cloudhead/rgx by Alexis Sellier, licensed under the MIT license // https://github.com/cloudhead/rgx by Alexis Sellier, licensed under the MIT license
layout(set = 0, binding = 0) uniform Globals { layout(set = 0, binding = 0) uniform Globals {
// orthographic projection is used to transform pixel coords to the coordinate system used by wgpu
mat4 ortho; mat4 ortho;
} global; } global;