[oden] Input coordinates are pixel coordinates

This commit is contained in:
John Doty 2023-06-25 07:54:25 -07:00
parent e3ae371f53
commit 0a36ffdde1
4 changed files with 117 additions and 26 deletions

View file

@ -1,4 +1,5 @@
use bytemuck;
use wgpu::util::DeviceExt;
use winit::{
event::*,
event_loop::{ControlFlow, EventLoop},
@ -38,6 +39,20 @@ impl Vertex {
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct ScreenUniforms {
resolution: [f32; 2],
}
impl ScreenUniforms {
fn new(width: u32, height: u32) -> ScreenUniforms {
ScreenUniforms {
resolution: [width as f32, height as f32],
}
}
}
struct State {
surface: wgpu::Surface,
device: wgpu::Device,
@ -52,6 +67,10 @@ struct State {
diffuse_bind_group: wgpu::BindGroup,
screen_uniform: ScreenUniforms,
screen_uniform_buffer: wgpu::Buffer,
screen_uniform_bind_group: wgpu::BindGroup,
// Garbage
mouse_x: f64,
mouse_y: f64,
@ -171,6 +190,37 @@ impl State {
label: Some("diffuse_bind_group"),
});
let screen_uniform = ScreenUniforms::new(size.width, size.height);
let screen_uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Screen Uniform Buffer"),
contents: bytemuck::cast_slice(&[screen_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let screen_uniform_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
label: Some("screen_bind_group_layout"),
});
let screen_uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &screen_uniform_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: screen_uniform_buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Shader"),
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
@ -179,7 +229,10 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout],
bind_group_layouts: &[
&texture_bind_group_layout,
&screen_uniform_bind_group_layout,
],
push_constant_ranges: &[],
});
@ -242,6 +295,9 @@ impl State {
vertex_buffer,
max_vertices,
diffuse_bind_group,
screen_uniform,
screen_uniform_buffer,
screen_uniform_bind_group,
mouse_x: 0.0,
mouse_y: 0.0,
@ -265,7 +321,14 @@ impl State {
false
}
fn update(&mut self) {}
fn update(&mut self) {
self.screen_uniform = ScreenUniforms::new(self.size.width, self.size.height);
self.queue.write_buffer(
&self.screen_uniform_buffer,
0,
bytemuck::cast_slice(&[self.screen_uniform]),
);
}
fn render(&mut self, commands: Vec<GraphicsCommand>) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_texture()?;
@ -349,19 +412,19 @@ impl State {
});
vertices.push(Vertex {
position: [sc.x, sc.y + sc.h, 0.0],
tex_coords: [sc.u, sc.v],
tex_coords: [sc.u, sc.v + sc.sh],
});
vertices.push(Vertex {
position: [sc.x, sc.y + sc.h, 0.0],
tex_coords: [sc.u, sc.v],
tex_coords: [sc.u, sc.v + sc.sh],
});
vertices.push(Vertex {
position: [sc.x + sc.w, sc.y, 0.0],
tex_coords: [sc.u, sc.v],
tex_coords: [sc.u + sc.sw, sc.v],
});
vertices.push(Vertex {
position: [sc.x + sc.w, sc.y + sc.h, 0.0],
tex_coords: [sc.u, sc.v],
tex_coords: [sc.u + sc.sw, sc.v + sc.sh],
});
}
@ -375,6 +438,7 @@ impl State {
.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(&vertices));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.screen_uniform_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.draw(0..(vertices.len() as u32), 0..1);
}