From 8a7217835348fbf22a9b26715688adf10dbc252c Mon Sep 17 00:00:00 2001 From: Ben Hansen Date: Tue, 4 May 2021 15:31:58 -0600 Subject: [PATCH] tutorial9 wgsl attempt --- Cargo.lock | 4 ++ code/beginner/tutorial9-models/Cargo.toml | 2 +- code/beginner/tutorial9-models/src/main.rs | 16 ++++-- .../beginner/tutorial9-models/src/shader.wgsl | 53 +++++++++++++++++++ .../tutorial10-lighting/Cargo.toml | 2 +- .../tutorial10-lighting/src/main.rs | 8 +-- 6 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 code/beginner/tutorial9-models/src/shader.wgsl diff --git a/Cargo.lock b/Cargo.lock index 34d7c582..6ecc8f7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,6 +833,7 @@ dependencies = [ "arrayvec", "bitflags", "fxhash", + "gfx-auxil", "gfx-hal", "glow", "js-sys", @@ -842,6 +843,7 @@ dependencies = [ "naga", "parking_lot", "raw-window-handle", + "spirv_cross", "wasm-bindgen", "web-sys", ] @@ -859,6 +861,7 @@ dependencies = [ "copyless", "foreign-types", "fxhash", + "gfx-auxil", "gfx-hal", "log", "metal", @@ -868,6 +871,7 @@ dependencies = [ "profiling", "range-alloc", "raw-window-handle", + "spirv_cross", "storage-map", ] diff --git a/code/beginner/tutorial9-models/Cargo.toml b/code/beginner/tutorial9-models/Cargo.toml index edb4d30d..344eed7d 100644 --- a/code/beginner/tutorial9-models/Cargo.toml +++ b/code/beginner/tutorial9-models/Cargo.toml @@ -13,7 +13,7 @@ futures = "0.3" image = "0.23" log = "0.4" tobj = "2.0" -wgpu = "0.8" +wgpu = {version = "0.8", features = [ "cross" ]} winit = "0.24" [build-dependencies] diff --git a/code/beginner/tutorial9-models/src/main.rs b/code/beginner/tutorial9-models/src/main.rs index ce6703b1..7d894f4b 100644 --- a/code/beginner/tutorial9-models/src/main.rs +++ b/code/beginner/tutorial9-models/src/main.rs @@ -261,7 +261,8 @@ impl State { features: wgpu::Features::empty(), limits: wgpu::Limits::default(), }, - Some(&std::path::Path::new("trace")), // Trace path + // Some(&std::path::Path::new("trace")), // Trace path + None, // Trace path ) .await .unwrap(); @@ -388,8 +389,13 @@ impl State { ) .unwrap(); - let vs_module = device.create_shader_module(&wgpu::include_spirv!("shader.vert.spv")); - let fs_module = device.create_shader_module(&wgpu::include_spirv!("shader.frag.spv")); + // let vs_module = device.create_shader_module(&wgpu::include_spirv!("shader.vert.spv")); + // let fs_module = device.create_shader_module(&wgpu::include_spirv!("shader.frag.spv")); + let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor { + label: Some("shader.wgsl"), + source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), + flags: wgpu::ShaderFlags::VALIDATION, + }); let depth_texture = texture::Texture::create_depth_texture(&device, &sc_desc, "depth_texture"); @@ -405,12 +411,12 @@ impl State { label: Some("Render Pipeline"), layout: Some(&render_pipeline_layout), vertex: wgpu::VertexState { - module: &vs_module, + module: &shader, entry_point: "main", buffers: &[model::ModelVertex::desc(), InstanceRaw::desc()], }, fragment: Some(wgpu::FragmentState { - module: &fs_module, + module: &shader, entry_point: "main", targets: &[wgpu::ColorTargetState { format: sc_desc.format, diff --git a/code/beginner/tutorial9-models/src/shader.wgsl b/code/beginner/tutorial9-models/src/shader.wgsl new file mode 100644 index 00000000..7d037a79 --- /dev/null +++ b/code/beginner/tutorial9-models/src/shader.wgsl @@ -0,0 +1,53 @@ +// Vertex shader + +[[block]] +struct Uniforms { + view_proj: mat4x4; +}; +[[group(1), binding(0)]] +var uniforms: Uniforms; + +struct VertexInput { + [[location(0)]] position: vec3; + [[location(1)]] tex_coords: vec2; +}; +struct InstanceInput { + [[location(5)]] model_matrix_0: vec4; + [[location(6)]] model_matrix_1: vec4; + [[location(7)]] model_matrix_2: vec4; + [[location(8)]] model_matrix_3: vec4; +}; + +struct VertexOutput { + [[builtin(position)]] clip_position: vec4; + [[location(0)]] tex_coords: vec2; +}; + +[[stage(vertex)]] +fn main( + model: VertexInput, + instance: InstanceInput, +) -> VertexOutput { + let model_matrix = mat4x4( + instance.model_matrix_0, + instance.model_matrix_1, + instance.model_matrix_2, + instance.model_matrix_3, + ); + var out: VertexOutput; + out.tex_coords = model.tex_coords; + out.clip_position = uniforms.view_proj * model_matrix * vec4(model.position, 1.0); + return out; +} + +// Fragment shader + +[[group(0), binding(0)]] +var t_diffuse: texture_2d; +[[group(0), binding(1)]] +var s_diffuse: sampler; + +[[stage(fragment)]] +fn main(in: VertexOutput) -> [[location(0)]] vec4 { + return textureSample(t_diffuse, s_diffuse, in.tex_coords); +} \ No newline at end of file diff --git a/code/intermediate/tutorial10-lighting/Cargo.toml b/code/intermediate/tutorial10-lighting/Cargo.toml index fe77964b..111fbc0b 100644 --- a/code/intermediate/tutorial10-lighting/Cargo.toml +++ b/code/intermediate/tutorial10-lighting/Cargo.toml @@ -13,7 +13,7 @@ futures = "0.3" image = "0.23" log = "0.4" tobj = "2.0" -wgpu = "0.8" +wgpu = { version = "0.8", features = ["cross"]} winit = "0.24" [build-dependencies] diff --git a/code/intermediate/tutorial10-lighting/src/main.rs b/code/intermediate/tutorial10-lighting/src/main.rs index 51cdfcd0..30a8f16a 100644 --- a/code/intermediate/tutorial10-lighting/src/main.rs +++ b/code/intermediate/tutorial10-lighting/src/main.rs @@ -266,9 +266,11 @@ fn create_render_pipeline( color_format: wgpu::TextureFormat, depth_format: Option, vertex_layouts: &[wgpu::VertexBufferLayout], - vs_src: wgpu::ShaderModuleDescriptor, - fs_src: wgpu::ShaderModuleDescriptor, + mut vs_src: wgpu::ShaderModuleDescriptor, + mut fs_src: wgpu::ShaderModuleDescriptor, ) -> wgpu::RenderPipeline { + vs_src.flags = wgpu::ShaderFlags::EXPERIMENTAL_TRANSLATION; + fs_src.flags = wgpu::ShaderFlags::EXPERIMENTAL_TRANSLATION; let vs_module = device.create_shader_module(&vs_src); let fs_module = device.create_shader_module(&fs_src); @@ -341,7 +343,7 @@ impl State { features: wgpu::Features::empty(), limits: wgpu::Limits::default(), }, - Some(&std::path::Path::new("trace")), // Trace path + None, ) .await .unwrap();