Got model matrix to work

For now the matrix is sent as a push constant but since WebGL doesn't
support them and will likely never support them big enough for a matrix,
they will in the future be sent in a uniform buffer
This commit is contained in:
Steins7 2022-08-20 14:43:29 +02:00
parent 88078e128a
commit 1081051084
3 changed files with 26 additions and 25 deletions

View File

@ -11,7 +11,7 @@ log = "^0.4.17"
chrono = "^0.4.19" chrono = "^0.4.19"
fern = { version = "^0.6.1", features = ["colored"] } fern = { version = "^0.6.1", features = ["colored"] }
bitflags = "^1.3.2" bitflags = "^1.3.2"
cgmath = "^0.18.0" cgmath = { version = "^0.18.0", features = ["bytemuck"] }
pollster = "^0.2.5" pollster = "^0.2.5"
# surface creation # surface creation

View File

@ -26,7 +26,6 @@ pub struct WgpuRenderer {
texture_bind_group_layout: wgpu::BindGroupLayout, texture_bind_group_layout: wgpu::BindGroupLayout,
texture_render_pipeline: wgpu::RenderPipeline, texture_render_pipeline: wgpu::RenderPipeline,
shape_render_pipeline: wgpu::RenderPipeline, shape_render_pipeline: wgpu::RenderPipeline,
texture_quad: GpuMesh<TextureVertex, 4, 6>,
quad_mesh: GpuMesh<ColorVertex, 4, 6>, //TODO temporary, to be moved to shapes.rs quad_mesh: GpuMesh<ColorVertex, 4, 6>, //TODO temporary, to be moved to shapes.rs
output: Option<wgpu::SurfaceTexture>, output: Option<wgpu::SurfaceTexture>,
} }
@ -42,6 +41,8 @@ impl WgpuRenderer {
let instance = wgpu::Instance::new(wgpu::Backends::all()); let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(window) }; let surface = unsafe { instance.create_surface(window) };
//let adapters = instance.enumerate_adapters(
let adapter = instance.request_adapter( let adapter = instance.request_adapter(
&wgpu::RequestAdapterOptions { &wgpu::RequestAdapterOptions {
//for now, integrated GPU is enough, may be revised later //for now, integrated GPU is enough, may be revised later
@ -51,11 +52,14 @@ impl WgpuRenderer {
}, },
).await.unwrap(); ).await.unwrap();
let mut limits = wgpu::Limits::downlevel_defaults();
limits.max_push_constant_size = 36;
let (device, queue) = adapter.request_device( let (device, queue) = adapter.request_device(
&wgpu::DeviceDescriptor { &wgpu::DeviceDescriptor {
//using minimum requirements possible since 2D isn't very demanding anyway //using minimum requirements possible since 2D isn't very demanding anyway
features: wgpu::Features::empty(), features: wgpu::Features::PUSH_CONSTANTS,
limits: wgpu::Limits::downlevel_webgl2_defaults(), limits,
label: None, label: None,
}, },
None, // Trace path None, // Trace path
@ -97,6 +101,11 @@ impl WgpuRenderer {
label: Some("texture_bind_group_layout"), label: Some("texture_bind_group_layout"),
}); });
let mvp_push_constant = wgpu::PushConstantRange {
stages: wgpu::ShaderStages::VERTEX,
range: 0..36,
};
let texture_render_pipeline = { let texture_render_pipeline = {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("texture shader"), label: Some("texture shader"),
@ -107,7 +116,7 @@ impl WgpuRenderer {
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("texture render pipeline layout"), label: Some("texture render pipeline layout"),
bind_group_layouts: &[&texture_bind_group_layout], bind_group_layouts: &[&texture_bind_group_layout],
push_constant_ranges: &[], push_constant_ranges: &[mvp_push_constant],
}); });
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
@ -209,23 +218,6 @@ impl WgpuRenderer {
}) })
}; };
let texture_quad = {
let mesh = Mesh {
vertices: [
TextureVertex { position: [0.0, 0.0], tex_coords: [0.0, 1.0] },
TextureVertex { position: [1.0, 0.0], tex_coords: [1.0, 1.0] },
TextureVertex { position: [1.0, 1.0], tex_coords: [1.0, 0.0] },
TextureVertex { position: [0.0, 1.0], tex_coords: [0.0, 0.0] },
],
indices: [
0, 1, 2,
0, 2, 3,
],
};
GpuMesh::create(&device, mesh)
};
let quad_mesh = { let quad_mesh = {
let mesh = Mesh { let mesh = Mesh {
vertices: [ vertices: [
@ -255,7 +247,6 @@ impl WgpuRenderer {
texture_bind_group_layout, texture_bind_group_layout,
texture_render_pipeline, texture_render_pipeline,
shape_render_pipeline, shape_render_pipeline,
texture_quad,
quad_mesh, quad_mesh,
output, output,
}) })
@ -318,7 +309,14 @@ impl WgpuRenderer {
render_pass.set_index_buffer(gpu_mesh.index_buffer.slice(..), render_pass.set_index_buffer(gpu_mesh.index_buffer.slice(..),
wgpu::IndexFormat::Uint16); wgpu::IndexFormat::Uint16);
render_pass.set_bind_group(0, &texture.bind_group, &[]); render_pass.set_bind_group(0, &texture.bind_group, &[]);
render_pass.draw_indexed(0..self.texture_quad.index_number, 0, 0..1); let matrix = {
use cgmath::SquareMatrix;
cgmath::Matrix3::<f32>::from_nonuniform_scale(720.0/1280.0, 1.0)
};
render_pass.set_push_constants(wgpu::ShaderStages::VERTEX,
0,
bytemuck::bytes_of(&matrix));
render_pass.draw_indexed(0..gpu_mesh.index_number, 0, 0..1);
drop(render_pass); drop(render_pass);
self.queue.submit(std::iter::once(encoder.finish())); self.queue.submit(std::iter::once(encoder.finish()));

View File

@ -1,5 +1,7 @@
// Vertex shader // Vertex shader
var<push_constant> model_matrix: mat3x3<f32>;
struct VertexInput { struct VertexInput {
@location(0) position: vec2<f32>, @location(0) position: vec2<f32>,
@location(1) tex_coords: vec2<f32>, @location(1) tex_coords: vec2<f32>,
@ -15,7 +17,8 @@ fn vs_main(model: VertexInput) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.tex_coords = model.tex_coords; out.tex_coords = model.tex_coords;
out.clip_position = vec4<f32>(model.position, 0.0, 1.0); out.clip_position
= vec4<f32>(model_matrix * vec3<f32>(model.position, 0.0), 1.0);
return out; return out;
} }