tutorial9 wgsl attempt

This commit is contained in:
Ben Hansen 2021-05-04 15:31:58 -06:00
parent 601273ec2b
commit 8a72178353
6 changed files with 75 additions and 10 deletions

4
Cargo.lock generated
View File

@ -833,6 +833,7 @@ dependencies = [
"arrayvec", "arrayvec",
"bitflags", "bitflags",
"fxhash", "fxhash",
"gfx-auxil",
"gfx-hal", "gfx-hal",
"glow", "glow",
"js-sys", "js-sys",
@ -842,6 +843,7 @@ dependencies = [
"naga", "naga",
"parking_lot", "parking_lot",
"raw-window-handle", "raw-window-handle",
"spirv_cross",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
] ]
@ -859,6 +861,7 @@ dependencies = [
"copyless", "copyless",
"foreign-types", "foreign-types",
"fxhash", "fxhash",
"gfx-auxil",
"gfx-hal", "gfx-hal",
"log", "log",
"metal", "metal",
@ -868,6 +871,7 @@ dependencies = [
"profiling", "profiling",
"range-alloc", "range-alloc",
"raw-window-handle", "raw-window-handle",
"spirv_cross",
"storage-map", "storage-map",
] ]

View File

@ -13,7 +13,7 @@ futures = "0.3"
image = "0.23" image = "0.23"
log = "0.4" log = "0.4"
tobj = "2.0" tobj = "2.0"
wgpu = "0.8" wgpu = {version = "0.8", features = [ "cross" ]}
winit = "0.24" winit = "0.24"
[build-dependencies] [build-dependencies]

View File

@ -261,7 +261,8 @@ impl State {
features: wgpu::Features::empty(), features: wgpu::Features::empty(),
limits: wgpu::Limits::default(), limits: wgpu::Limits::default(),
}, },
Some(&std::path::Path::new("trace")), // Trace path // Some(&std::path::Path::new("trace")), // Trace path
None, // Trace path
) )
.await .await
.unwrap(); .unwrap();
@ -388,8 +389,13 @@ impl State {
) )
.unwrap(); .unwrap();
let vs_module = device.create_shader_module(&wgpu::include_spirv!("shader.vert.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 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 = let depth_texture =
texture::Texture::create_depth_texture(&device, &sc_desc, "depth_texture"); texture::Texture::create_depth_texture(&device, &sc_desc, "depth_texture");
@ -405,12 +411,12 @@ impl State {
label: Some("Render Pipeline"), label: Some("Render Pipeline"),
layout: Some(&render_pipeline_layout), layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState { vertex: wgpu::VertexState {
module: &vs_module, module: &shader,
entry_point: "main", entry_point: "main",
buffers: &[model::ModelVertex::desc(), InstanceRaw::desc()], buffers: &[model::ModelVertex::desc(), InstanceRaw::desc()],
}, },
fragment: Some(wgpu::FragmentState { fragment: Some(wgpu::FragmentState {
module: &fs_module, module: &shader,
entry_point: "main", entry_point: "main",
targets: &[wgpu::ColorTargetState { targets: &[wgpu::ColorTargetState {
format: sc_desc.format, format: sc_desc.format,

View File

@ -0,0 +1,53 @@
// Vertex shader
[[block]]
struct Uniforms {
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
[[location(1)]] tex_coords: vec2<f32>;
};
struct InstanceInput {
[[location(5)]] model_matrix_0: vec4<f32>;
[[location(6)]] model_matrix_1: vec4<f32>;
[[location(7)]] model_matrix_2: vec4<f32>;
[[location(8)]] model_matrix_3: vec4<f32>;
};
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] tex_coords: vec2<f32>;
};
[[stage(vertex)]]
fn main(
model: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
let model_matrix = mat4x4<f32>(
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<f32>(model.position, 1.0);
return out;
}
// Fragment shader
[[group(0), binding(0)]]
var t_diffuse: texture_2d<f32>;
[[group(0), binding(1)]]
var s_diffuse: sampler;
[[stage(fragment)]]
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
return textureSample(t_diffuse, s_diffuse, in.tex_coords);
}

View File

@ -13,7 +13,7 @@ futures = "0.3"
image = "0.23" image = "0.23"
log = "0.4" log = "0.4"
tobj = "2.0" tobj = "2.0"
wgpu = "0.8" wgpu = { version = "0.8", features = ["cross"]}
winit = "0.24" winit = "0.24"
[build-dependencies] [build-dependencies]

View File

@ -266,9 +266,11 @@ fn create_render_pipeline(
color_format: wgpu::TextureFormat, color_format: wgpu::TextureFormat,
depth_format: Option<wgpu::TextureFormat>, depth_format: Option<wgpu::TextureFormat>,
vertex_layouts: &[wgpu::VertexBufferLayout], vertex_layouts: &[wgpu::VertexBufferLayout],
vs_src: wgpu::ShaderModuleDescriptor, mut vs_src: wgpu::ShaderModuleDescriptor,
fs_src: wgpu::ShaderModuleDescriptor, mut fs_src: wgpu::ShaderModuleDescriptor,
) -> wgpu::RenderPipeline { ) -> 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 vs_module = device.create_shader_module(&vs_src);
let fs_module = device.create_shader_module(&fs_src); let fs_module = device.create_shader_module(&fs_src);
@ -341,7 +343,7 @@ impl State {
features: wgpu::Features::empty(), features: wgpu::Features::empty(),
limits: wgpu::Limits::default(), limits: wgpu::Limits::default(),
}, },
Some(&std::path::Path::new("trace")), // Trace path None,
) )
.await .await
.unwrap(); .unwrap();