finished code migration

0.10
Ben Hansen 3 years ago
parent 88df0ff823
commit fd589adbe1

23
Cargo.lock generated

@ -311,9 +311,9 @@ dependencies = [
"glob",
"image",
"log",
"naga",
"pollster",
"rayon",
"shaderc",
"tobj 2.0.4",
"wgpu",
"winit",
@ -1083,6 +1083,8 @@ dependencies = [
"log",
"num-traits",
"petgraph",
"pp-rs",
"rose_tree",
"serde",
"spirv",
"thiserror",
@ -1355,6 +1357,15 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb20dcc30536a1508e75d47dd0e399bb2fe7354dcf35cda9127f2bf1ed92e30e"
[[package]]
name = "pp-rs"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb458bb7f6e250e6eb79d5026badc10a3ebb8f9a15d1fff0f13d17c71f4d6dee"
dependencies = [
"unicode-xid",
]
[[package]]
name = "proc-macro-crate"
version = "0.1.5"
@ -1496,6 +1507,15 @@ dependencies = [
"serde",
]
[[package]]
name = "rose_tree"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcd16c61e9205949fa4f8a22096705b4c2f8b8025b2ff67ff6c86afd854039ae"
dependencies = [
"petgraph",
]
[[package]]
name = "rusttype"
version = "0.9.2"
@ -2230,6 +2250,7 @@ dependencies = [
"arrayvec",
"js-sys",
"log",
"naga",
"parking_lot",
"raw-window-handle",
"serde",

@ -98,7 +98,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -38,7 +38,7 @@ impl State {
limits: wgpu::Limits::default(),
},
// Some(&std::path::Path::new("trace")), // Trace path
None
None,
)
.await
.unwrap();
@ -79,7 +79,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -191,7 +191,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -133,7 +133,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -262,7 +262,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -205,7 +205,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -296,7 +296,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -257,7 +257,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -484,7 +484,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -465,7 +465,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -612,7 +612,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -561,7 +561,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -771,7 +771,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -561,11 +561,8 @@ impl State {
self.surface.configure(&self.device, &self.config);
self.camera.aspect = self.config.width as f32 / self.config.height as f32;
// NEW!
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -585,7 +582,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -472,11 +472,8 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
fn input(&mut self, event: &WindowEvent) -> bool {
@ -495,7 +492,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -591,11 +591,8 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -627,7 +624,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -644,11 +644,8 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -680,7 +677,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -524,11 +524,8 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -587,7 +584,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -521,11 +521,8 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -582,7 +579,9 @@ impl State {
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device

@ -14,7 +14,7 @@ image = "0.23"
log = "0.4"
rayon = "1.4"
tobj = "2.0"
wgpu = { version = "0.10", features = ["trace"] }
wgpu = { version = "0.10", features = ["trace", "spirv"] }
winit = "0.25"
[build-dependencies]
@ -22,4 +22,4 @@ anyhow = "1.0"
fs_extra = "1.2"
glob = "0.3"
rayon = "1.4"
shaderc = "0.7"
naga = { version = "0.6", features = ["glsl-in", "spv-out", "wgsl-out"]}

@ -2,42 +2,62 @@ use anyhow::*;
use fs_extra::copy_items;
use fs_extra::dir::CopyOptions;
use glob::glob;
use naga::back::wgsl;
use naga::front::glsl::Options;
use naga::front::glsl::Parser;
use rayon::prelude::*;
use std::env;
use std::fs::{read_to_string, write};
use std::path::PathBuf;
use std::{fs::read_to_string, path::PathBuf};
struct ShaderData {
src: String,
src_path: PathBuf,
spv_path: PathBuf,
kind: shaderc::ShaderKind,
}
pub fn load_shader(src_path: PathBuf) -> Result<()> {
let extension = src_path
.extension()
.context("File has no extension")?
.to_str()
.context("Extension cannot be converted to &str")?;
let kind = match extension {
"vert" => naga::ShaderStage::Vertex,
"frag" => naga::ShaderStage::Fragment,
"comp" => naga::ShaderStage::Compute,
_ => bail!("Unsupported shader: {}", src_path.display()),
};
let src = read_to_string(src_path.clone())?;
// let spv_path = src_path.with_extension(format!("{}.spv", extension));
let wgsl_path = src_path.with_extension(format!("{}.wgsl", extension));
impl ShaderData {
pub fn load(src_path: PathBuf) -> Result<Self> {
let extension = src_path
.extension()
.context("File has no extension")?
.to_str()
.context("Extension cannot be converted to &str")?;
let kind = match extension {
"vert" => shaderc::ShaderKind::Vertex,
"frag" => shaderc::ShaderKind::Fragment,
"comp" => shaderc::ShaderKind::Compute,
_ => bail!("Unsupported shader: {}", src_path.display()),
};
let mut parser = Parser::default();
let options = Options::from(kind);
let module = match parser.parse(&options, &src) {
Ok(it) => it,
Err(errors) => {
bail!(
"Failed to compile shader: {}\nErrors:\n{:#?}",
src_path.display(),
errors
);
}
};
let src = read_to_string(src_path.clone())?;
let spv_path = src_path.with_extension(format!("{}.spv", extension));
let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::empty(),
)
.validate(&module)?;
std::fs::write(wgsl_path, wgsl::write_string(&module, &info)?)?;
Ok(Self {
src,
src_path,
spv_path,
kind,
})
}
// let flags = spv::WriterFlags::DEBUG | spv::WriterFlags::ADJUST_COORDINATE_SPACE;
// let options = spv::Options {
// flags,
// ..Default::default()
// };
// let spv = spv::write_vec(&module, &info, &options)?;
// let dis = rspirv::dr::load_words(spv)
// .expect("Unable to disassemble shader")
// .disassemble();
// std::fs::write(spv_path, &spv)?;
Ok(())
}
fn main() -> Result<()> {
@ -46,45 +66,30 @@ fn main() -> Result<()> {
// Collect all shaders recursively within /src/
// UDPATED!
let mut shader_paths = Vec::new();
shader_paths.extend(glob("./src/**/*.vert")?);
shader_paths.extend(glob("./src/**/*.frag")?);
shader_paths.extend(glob("./src/**/*.comp")?);
let shader_paths = {
let mut data = Vec::new();
data.extend(glob("./src/**/*.vert")?);
data.extend(glob("./src/**/*.frag")?);
data.extend(glob("./src/**/*.comp")?);
data
};
// UPDATED!
// This is parallelized
let shaders = shader_paths
shader_paths
.into_par_iter()
.map(|glob_result| ShaderData::load(glob_result?))
.map(|glob_result| load_shader(glob_result?))
.collect::<Vec<Result<_>>>()
.into_iter()
.collect::<Result<Vec<_>>>();
let mut compiler = shaderc::Compiler::new().context("Unable to create shader compiler")?;
// This can't be parallelized. The [shaderc::Compiler] is not
// thread safe. Also, it creates a lot of resources. You could
// spawn multiple processes to handle this, but it would probably
// be better just to only compile shaders that have been changed
// recently.
for shader in shaders? {
let compiled = compiler.compile_into_spirv(
&shader.src,
shader.kind,
shader.src_path.to_str().unwrap(),
"main",
None,
)?;
write(shader.spv_path, compiled.as_binary_u8())?;
}
.collect::<Result<Vec<_>>>()?;
// This tells cargo to rerun this script if something in /res/ changes.
println!("cargo:rerun-if-changed=res/*");
let out_dir = env::var("OUT_DIR")?;
let mut copy_options = CopyOptions::new();
copy_options.overwrite = true;
let mut paths_to_copy = Vec::new();
copy_options.overwrite = true;
paths_to_copy.push("res/");
copy_items(&paths_to_copy, out_dir, &copy_options)?;

@ -0,0 +1,20 @@
struct FragmentOutput {
[[location(0)]] f_color: vec4<f32>;
};
var<private> v_color1: vec3<f32>;
var<private> f_color: vec4<f32>;
fn main1() {
let _e2: vec3<f32> = v_color1;
f_color = vec4<f32>(_e2, 1.0);
return;
}
[[stage(fragment)]]
fn main([[location(0)]] v_color: vec3<f32>) -> FragmentOutput {
v_color1 = v_color;
main1();
let _e7: vec4<f32> = f_color;
return FragmentOutput(_e7);
}

@ -0,0 +1,49 @@
[[block]]
struct Camera {
u_view_position: vec3<f32>;
u_view_proj: mat4x4<f32>;
};
[[block]]
struct Light {
u_position: vec3<f32>;
u_color: vec3<f32>;
};
struct VertexOutput {
[[location(0)]] v_color: vec3<f32>;
[[builtin(position)]] member: vec4<f32>;
};
var<private> a_position1: vec3<f32>;
var<private> v_color: vec3<f32>;
[[group(0), binding(0)]]
var<uniform> global: Camera;
[[group(1), binding(0)]]
var<uniform> global1: Light;
var<private> scale: f32 = 0.25;
var<private> gl_Position: vec4<f32>;
fn main1() {
var v_position: vec3<f32>;
let _e11: vec3<f32> = a_position1;
let _e12: f32 = scale;
let _e14: vec3<f32> = global1.u_position;
v_position = ((_e11 * _e12) + _e14);
let _e18: mat4x4<f32> = global.u_view_proj;
let _e19: vec3<f32> = v_position;
gl_Position = (_e18 * vec4<f32>(_e19, f32(1)));
let _e24: vec3<f32> = global1.u_color;
v_color = _e24;
return;
}
[[stage(vertex)]]
fn main([[location(0)]] a_position: vec3<f32>) -> VertexOutput {
a_position1 = a_position;
main1();
let _e18: vec3<f32> = v_color;
let _e20: vec4<f32> = gl_Position;
return VertexOutput(_e18, _e20);
}

@ -52,15 +52,17 @@ impl Instance {
model: (cgmath::Matrix4::from_translation(self.position)
* cgmath::Matrix4::from(self.rotation))
.into(),
normal: cgmath::Matrix3::from(self.rotation).into(),
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
#[allow(dead_code)]
struct InstanceRaw {
#[allow(dead_code)]
model: [[f32; 4]; 4],
normal: [[f32; 3]; 3],
}
impl model::Vertex for InstanceRaw {
@ -97,6 +99,21 @@ impl model::Vertex for InstanceRaw {
shader_location: 8,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 16]>() as wgpu::BufferAddress,
shader_location: 9,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 19]>() as wgpu::BufferAddress,
shader_location: 10,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 22]>() as wgpu::BufferAddress,
shader_location: 11,
format: wgpu::VertexFormat::Float32x3,
},
],
}
}
@ -357,16 +374,18 @@ impl State {
push_constant_ranges: &[],
});
println!("Creating RENDER pipeline");
let render_pipeline = create_render_pipeline(
&device,
&render_pipeline_layout,
config.format,
Some(texture::Texture::DEPTH_FORMAT),
&[model::ModelVertex::desc(), InstanceRaw::desc()],
wgpu::include_spirv!("shader.vert.spv"),
wgpu::include_spirv!("shader.frag.spv"),
wgpu::include_wgsl!("shader.vert.wgsl"),
wgpu::include_wgsl!("shader.frag.wgsl"),
);
println!("Creating LIGHT pipeline");
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
@ -380,8 +399,8 @@ impl State {
config.format,
Some(texture::Texture::DEPTH_FORMAT),
&[model::ModelVertex::desc()],
wgpu::include_spirv!("light.vert.spv"),
wgpu::include_spirv!("light.frag.spv"),
wgpu::include_wgsl!("light.vert.wgsl"),
wgpu::include_wgsl!("light.frag.wgsl"),
)
};
@ -450,11 +469,8 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(
&self.device,
&self.config,
"depth_texture",
);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -518,11 +534,14 @@ impl State {
}
fn render(&mut self) {
let frame = self.swap_chain.get_current_frame();
let frame = self.surface.get_current_frame();
match frame {
Ok(frame) => {
let frame = frame.output;
let output = frame.output;
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder =
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {

@ -232,8 +232,9 @@ pub struct ModelLoader {
impl ModelLoader {
// NEW!
pub fn new(device: &wgpu::Device) -> Self {
println!("Creating MODELLOADER pipeline");
let binder = pipeline::Binder::new(device, Some("ModelLoader Binder"));
let shader_src = wgpu::include_spirv!("model_load.comp.spv");
let shader_src = wgpu::include_wgsl!("model_load.comp.wgsl");
let pipeline = pipeline::create_compute_pipeline(
device,
&[&binder.layout],

@ -11,13 +11,13 @@ struct ModelVertex {
uint pad0; uint pad1;
};
layout(std430, set=0, binding=0) buffer SrcVertexBuffer {
layout(std430, set=0, binding=0) readonly buffer SrcVertexBuffer {
ModelVertex srcVertices[];
};
layout(std430, set=0, binding=1) buffer DstVertexBuffer {
ModelVertex dstVertices[];
};
layout(std430, set=0, binding=2) buffer IndexBuffer {
layout(std430, set=0, binding=2) readonly buffer IndexBuffer {
uint indices[];
};

@ -0,0 +1,252 @@
struct ModelVertex {
x: f32;
y: f32;
z: f32;
uv: f32;
uw: f32;
nx: f32;
ny: f32;
nz: f32;
tx: f32;
ty: f32;
tz: f32;
bx: f32;
by: f32;
bz: f32;
pad0_: u32;
pad1_: u32;
};
[[block]]
struct SrcVertexBuffer {
srcVertices: [[stride(64)]] array<ModelVertex>;
};
[[block]]
struct DstVertexBuffer {
dstVertices: [[stride(64)]] array<ModelVertex>;
};
[[block]]
struct IndexBuffer {
indices: [[stride(4)]] array<u32>;
};
[[block]]
struct ComputeInfo {
numVertices: u32;
numIndices: u32;
};
[[group(0), binding(0)]]
var<storage> global: SrcVertexBuffer;
[[group(0), binding(1)]]
var<storage, read_write> global1: DstVertexBuffer;
[[group(0), binding(2)]]
var<storage> global2: IndexBuffer;
[[group(0), binding(3)]]
var<uniform> global3: ComputeInfo;
var<private> gl_GlobalInvocationID: vec3<u32>;
fn getPos(v: ModelVertex) -> vec3<f32> {
var v1: ModelVertex;
v1 = v;
let _e12: ModelVertex = v1;
let _e14: ModelVertex = v1;
let _e16: ModelVertex = v1;
return vec3<f32>(_e12.x, _e14.y, _e16.z);
}
fn getUV(v2: ModelVertex) -> vec2<f32> {
var v3: ModelVertex;
v3 = v2;
let _e12: ModelVertex = v3;
let _e14: ModelVertex = v3;
return vec2<f32>(_e12.uv, _e14.uw);
}
fn getNormal(v4: ModelVertex) -> vec3<f32> {
var v5: ModelVertex;
v5 = v4;
let _e12: ModelVertex = v5;
let _e14: ModelVertex = v5;
let _e16: ModelVertex = v5;
return vec3<f32>(_e12.nx, _e14.ny, _e16.nz);
}
fn calcTangentBitangent(vertexIndex: u32) -> ModelVertex {
var vertexIndex1: u32;
var v6: ModelVertex;
var tangent: vec3<f32> = vec3<f32>(0.0, 0.0, 0.0);
var bitangent: vec3<f32> = vec3<f32>(0.0, 0.0, 0.0);
var trianglesIncluded: u32 = 0u;
var i: u32 = 0u;
var index0_: u32;
var index1_: u32;
var index2_: u32;
var v0_: ModelVertex;
var v1_: ModelVertex;
var v2_: ModelVertex;
var pos0_: vec3<f32>;
var pos1_: vec3<f32>;
var pos2_: vec3<f32>;
var uv0_: vec2<f32>;
var uv1_: vec2<f32>;
var uv2_: vec2<f32>;
var delta_pos1_: vec3<f32>;
var delta_pos2_: vec3<f32>;
var delta_uv1_: vec2<f32>;
var delta_uv2_: vec2<f32>;
var r: f32;
vertexIndex1 = vertexIndex;
let _e12: u32 = vertexIndex1;
let _e14: ModelVertex = global.srcVertices[_e12];
v6 = _e14;
loop {
let _e30: u32 = i;
let _e31: u32 = global3.numIndices;
if (!((_e30 < _e31))) {
break;
}
{
let _e38: u32 = i;
let _e40: u32 = global2.indices[_e38];
index0_ = _e40;
let _e42: u32 = i;
let _e47: u32 = global2.indices[(_e42 + u32(1))];
index1_ = _e47;
let _e49: u32 = i;
let _e54: u32 = global2.indices[(_e49 + u32(2))];
index2_ = _e54;
let _e56: u32 = index0_;
let _e57: u32 = vertexIndex1;
let _e59: u32 = index1_;
let _e60: u32 = vertexIndex1;
let _e63: u32 = index2_;
let _e64: u32 = vertexIndex1;
if ((((_e56 == _e57) || (_e59 == _e60)) || (_e63 == _e64))) {
{
let _e67: u32 = index0_;
let _e69: ModelVertex = global.srcVertices[_e67];
v0_ = _e69;
let _e71: u32 = index1_;
let _e73: ModelVertex = global.srcVertices[_e71];
v1_ = _e73;
let _e75: u32 = index2_;
let _e77: ModelVertex = global.srcVertices[_e75];
v2_ = _e77;
let _e80: ModelVertex = v0_;
let _e81: vec3<f32> = getPos(_e80);
pos0_ = _e81;
let _e84: ModelVertex = v1_;
let _e85: vec3<f32> = getPos(_e84);
pos1_ = _e85;
let _e88: ModelVertex = v2_;
let _e89: vec3<f32> = getPos(_e88);
pos2_ = _e89;
let _e92: ModelVertex = v0_;
let _e93: vec2<f32> = getUV(_e92);
uv0_ = _e93;
let _e96: ModelVertex = v1_;
let _e97: vec2<f32> = getUV(_e96);
uv1_ = _e97;
let _e100: ModelVertex = v2_;
let _e101: vec2<f32> = getUV(_e100);
uv2_ = _e101;
let _e103: vec3<f32> = pos1_;
let _e104: vec3<f32> = pos0_;
delta_pos1_ = (_e103 - _e104);
let _e107: vec3<f32> = pos2_;
let _e108: vec3<f32> = pos0_;
delta_pos2_ = (_e107 - _e108);
let _e111: vec2<f32> = uv1_;
let _e112: vec2<f32> = uv0_;
delta_uv1_ = (_e111 - _e112);
let _e115: vec2<f32> = uv2_;
let _e116: vec2<f32> = uv0_;
delta_uv2_ = (_e115 - _e116);
let _e120: vec2<f32> = delta_uv1_;
let _e122: vec2<f32> = delta_uv2_;
let _e125: vec2<f32> = delta_uv1_;
let _e127: vec2<f32> = delta_uv2_;
r = (1.0 / ((_e120.x * _e122.y) - (_e125.y * _e127.x)));
let _e133: vec3<f32> = tangent;
let _e134: vec3<f32> = delta_pos1_;
let _e135: vec2<f32> = delta_uv2_;
let _e138: vec3<f32> = delta_pos2_;
let _e139: vec2<f32> = delta_uv1_;
let _e143: f32 = r;
tangent = (_e133 + (((_e134 * _e135.y) - (_e138 * _e139.y)) * _e143));
let _e146: vec3<f32> = bitangent;
let _e147: vec3<f32> = delta_pos2_;
let _e148: vec2<f32> = delta_uv1_;
let _e151: vec3<f32> = delta_pos1_;
let _e152: vec2<f32> = delta_uv2_;
let _e156: f32 = r;
bitangent = (_e146 + (((_e147 * _e148.x) - (_e151 * _e152.x)) * _e156));
let _e159: u32 = trianglesIncluded;
trianglesIncluded = (_e159 + u32(1));
}
}
}
continuing {
let _e34: u32 = i;
i = (_e34 + u32(3));
}
}
let _e163: u32 = trianglesIncluded;
if ((_e163 > u32(0))) {
{
let _e167: vec3<f32> = tangent;
let _e168: u32 = trianglesIncluded;
tangent = (_e167 / vec3<f32>(f32(_e168)));
let _e172: vec3<f32> = bitangent;
let _e173: u32 = trianglesIncluded;
bitangent = (_e172 / vec3<f32>(f32(_e173)));
let _e178: vec3<f32> = tangent;
tangent = normalize(_e178);
let _e181: vec3<f32> = bitangent;
bitangent = normalize(_e181);
}
}
let _e184: vec3<f32> = tangent;
v6.tx = _e184.x;
let _e187: vec3<f32> = tangent;
v6.ty = _e187.y;
let _e190: vec3<f32> = tangent;
v6.tz = _e190.z;
let _e193: vec3<f32> = bitangent;
v6.bx = _e193.x;
let _e196: vec3<f32> = bitangent;
v6.by = _e196.y;
let _e199: vec3<f32> = bitangent;
v6.bz = _e199.z;
let _e201: ModelVertex = v6;
return _e201;
}
fn main1() {
var vertexIndex2: u32;
var result: ModelVertex;
let _e11: vec3<u32> = gl_GlobalInvocationID;
vertexIndex2 = _e11.x;
let _e15: u32 = vertexIndex2;
let _e16: ModelVertex = calcTangentBitangent(_e15);
result = _e16;
let _e18: u32 = vertexIndex2;
let _e20: ModelVertex = result;
global1.dstVertices[_e18] = _e20;
return;
}
[[stage(compute), workgroup_size(64, 1, 1)]]
fn main([[builtin(global_invocation_id)]] param: vec3<u32>) {
gl_GlobalInvocationID = param;
main1();
return;
}

@ -40,12 +40,12 @@ pub fn create_render_pipeline(
color_format: wgpu::TextureFormat,
depth_format: Option<wgpu::TextureFormat>,
vertex_layouts: &[wgpu::VertexBufferLayout],
mut vs_src: wgpu::ShaderModuleDescriptor,
mut fs_src: wgpu::ShaderModuleDescriptor,
vs_src: wgpu::ShaderModuleDescriptor,
fs_src: wgpu::ShaderModuleDescriptor,
) -> wgpu::RenderPipeline {
vs_src.flags &= wgpu::ShaderFlags::VALIDATION;
fs_src.flags &= wgpu::ShaderFlags::VALIDATION;
println!("Creating VS Module =======");
let vs_module = device.create_shader_module(&vs_src);
println!("Creating FS Module =======");
let fs_module = device.create_shader_module(&fs_src);
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {

@ -0,0 +1,109 @@
[[block]]
struct Light {
light_position: vec3<f32>;
light_color: vec3<f32>;
};
struct FragmentOutput {
[[location(0)]] f_color: vec4<f32>;
};
var<private> v_tex_coords1: vec2<f32>;
var<private> v_position1: vec3<f32>;
var<private> v_light_position1: vec3<f32>;
var<private> v_view_position1: vec3<f32>;
var<private> f_color: vec4<f32>;
[[group(0), binding(0)]]
var t_diffuse: texture_2d<f32>;
[[group(0), binding(1)]]
var s_diffuse: sampler;
[[group(0), binding(2)]]
var t_normal: texture_2d<f32>;
[[group(0), binding(3)]]
var s_normal: sampler;
[[group(2), binding(0)]]
var<uniform> global: Light;
fn main1() {
var object_color: vec4<f32>;
var object_normal: vec4<f32>;
var ambient_strength: f32 = 0.10000000149011612;
var ambient_color: vec3<f32>;
var normal: vec3<f32>;
var light_dir: vec3<f32>;
var diffuse_strength: f32;
var diffuse_color: vec3<f32>;
var view_dir: vec3<f32>;
var half_dir: vec3<f32>;
var specular_strength: f32;
var specular_color: vec3<f32>;
var result: vec3<f32>;
let _e14: vec2<f32> = v_tex_coords1;
let _e15: vec4<f32> = textureSample(t_diffuse, s_diffuse, _e14);
object_color = _e15;
let _e18: vec2<f32> = v_tex_coords1;
let _e19: vec4<f32> = textureSample(t_normal, s_normal, _e18);
object_normal = _e19;
let _e23: vec3<f32> = global.light_color;
let _e24: f32 = ambient_strength;
ambient_color = (_e23 * _e24);
let _e27: vec4<f32> = object_normal;
let _e34: vec4<f32> = object_normal;
normal = normalize(((_e34.xyz * 2.0) - vec3<f32>(1.0)));
let _e43: vec3<f32> = v_light_position1;
let _e44: vec3<f32> = v_position1;
let _e46: vec3<f32> = v_light_position1;
let _e47: vec3<f32> = v_position1;
light_dir = normalize((_e46 - _e47));
let _e53: vec3<f32> = normal;
let _e54: vec3<f32> = light_dir;
let _e59: vec3<f32> = normal;
let _e60: vec3<f32> = light_dir;
diffuse_strength = max(dot(_e59, _e60), 0.0);
let _e65: vec3<f32> = global.light_color;
let _e66: f32 = diffuse_strength;
diffuse_color = (_e65 * _e66);
let _e69: vec3<f32> = v_view_position1;
let _e70: vec3<f32> = v_position1;
let _e72: vec3<f32> = v_view_position1;
let _e73: vec3<f32> = v_position1;
view_dir = normalize((_e72 - _e73));
let _e77: vec3<f32> = view_dir;
let _e78: vec3<f32> = light_dir;
let _e80: vec3<f32> = view_dir;
let _e81: vec3<f32> = light_dir;
half_dir = normalize((_e80 + _e81));
let _e87: vec3<f32> = normal;
let _e88: vec3<f32> = half_dir;
let _e93: vec3<f32> = normal;
let _e94: vec3<f32> = half_dir;
let _e101: vec3<f32> = normal;
let _e102: vec3<f32> = half_dir;
let _e107: vec3<f32> = normal;
let _e108: vec3<f32> = half_dir;
specular_strength = pow(max(dot(_e107, _e108), 0.0), f32(32));
let _e116: f32 = specular_strength;
let _e117: vec3<f32> = global.light_color;
specular_color = (_e116 * _e117);
let _e120: vec3<f32> = ambient_color;
let _e121: vec3<f32> = diffuse_color;
let _e123: vec3<f32> = specular_color;
let _e125: vec4<f32> = object_color;
result = (((_e120 + _e121) + _e123) * _e125.xyz);
let _e129: vec3<f32> = result;
let _e130: vec4<f32> = object_color;
f_color = vec4<f32>(_e129, _e130.w);
return;
}
[[stage(fragment)]]
fn main([[location(0)]] v_tex_coords: vec2<f32>, [[location(1)]] v_position: vec3<f32>, [[location(2)]] v_light_position: vec3<f32>, [[location(3)]] v_view_position: vec3<f32>) -> FragmentOutput {
v_tex_coords1 = v_tex_coords;
v_position1 = v_position;
v_light_position1 = v_light_position;
v_view_position1 = v_view_position;
main1();
let _e31: vec4<f32> = f_color;
return FragmentOutput(_e31);
}

@ -22,6 +22,10 @@ layout(location=6) in vec4 model_matrix_1;
layout(location=7) in vec4 model_matrix_2;
layout(location=8) in vec4 model_matrix_3;
layout(location=9) in vec3 normal_matrix_0;
layout(location=10) in vec3 normal_matrix_1;
layout(location=11) in vec3 normal_matrix_2;
// NEW!
layout(set=2, binding=0) uniform Light {
vec3 light_position;
@ -37,7 +41,11 @@ void main() {
);
v_tex_coords = a_tex_coords;
mat3 normal_matrix = mat3(transpose(inverse(model_matrix)));
mat3 normal_matrix = mat3(
normal_matrix_0,
normal_matrix_1,
normal_matrix_2
);
vec3 normal = normalize(normal_matrix * a_normal);
vec3 tangent = normalize(normal_matrix * a_tangent);
vec3 bitangent = normalize(normal_matrix * a_bitangent);
@ -50,7 +58,6 @@ void main() {
));
vec4 model_space = model_matrix * vec4(a_position, 1.0);
v_position = model_space.xyz;
// NEW!
v_position = tangent_matrix * model_space.xyz;

@ -0,0 +1,124 @@
[[block]]
struct Camera {
u_view_position: vec3<f32>;
u_view_proj: mat4x4<f32>;
};
[[block]]
struct Light {
light_position: vec3<f32>;
light_color: vec3<f32>;
};
struct VertexOutput {
[[location(0)]] v_tex_coords: vec2<f32>;
[[location(1)]] v_position: vec3<f32>;
[[location(2)]] v_light_position: vec3<f32>;
[[location(3)]] v_view_position: vec3<f32>;
[[builtin(position)]] member: vec4<f32>;
};
var<private> a_position1: vec3<f32>;
var<private> a_tex_coords1: vec2<f32>;
var<private> a_normal1: vec3<f32>;
var<private> a_tangent1: vec3<f32>;
var<private> a_bitangent1: vec3<f32>;
var<private> v_tex_coords: vec2<f32>;
var<private> v_position: vec3<f32>;
var<private> v_light_position: vec3<f32>;
var<private> v_view_position: vec3<f32>;
[[group(1), binding(0)]]
var<uniform> global: Camera;
var<private> model_matrix_0_1: vec4<f32>;
var<private> model_matrix_1_1: vec4<f32>;
var<private> model_matrix_2_1: vec4<f32>;
var<private> model_matrix_3_1: vec4<f32>;
var<private> normal_matrix_0_1: vec3<f32>;
var<private> normal_matrix_1_1: vec3<f32>;
var<private> normal_matrix_2_1: vec3<f32>;
[[group(2), binding(0)]]
var<uniform> global1: Light;
var<private> gl_Position: vec4<f32>;
fn main1() {
var model_matrix: mat4x4<f32>;
var normal_matrix: mat3x3<f32>;
var normal: vec3<f32>;
var tangent: vec3<f32>;
var bitangent: vec3<f32>;
var tangent_matrix: mat3x3<f32>;
var model_space: vec4<f32>;
let _e24: vec4<f32> = model_matrix_0_1;
let _e25: vec4<f32> = model_matrix_1_1;
let _e26: vec4<f32> = model_matrix_2_1;
let _e27: vec4<f32> = model_matrix_3_1;
model_matrix = mat4x4<f32>(vec4<f32>(_e24.x, _e24.y, _e24.z, _e24.w), vec4<f32>(_e25.x, _e25.y, _e25.z, _e25.w), vec4<f32>(_e26.x, _e26.y, _e26.z, _e26.w), vec4<f32>(_e27.x, _e27.y, _e27.z, _e27.w));
let _e50: vec2<f32> = a_tex_coords1;
v_tex_coords = _e50;
let _e51: vec3<f32> = normal_matrix_0_1;
let _e52: vec3<f32> = normal_matrix_1_1;
let _e53: vec3<f32> = normal_matrix_2_1;
normal_matrix = mat3x3<f32>(vec3<f32>(_e51.x, _e51.y, _e51.z), vec3<f32>(_e52.x, _e52.y, _e52.z), vec3<f32>(_e53.x, _e53.y, _e53.z));
let _e68: mat3x3<f32> = normal_matrix;
let _e69: vec3<f32> = a_normal1;
let _e71: mat3x3<f32> = normal_matrix;
let _e72: vec3<f32> = a_normal1;
normal = normalize((_e71 * _e72));
let _e76: mat3x3<f32> = normal_matrix;
let _e77: vec3<f32> = a_tangent1;
let _e79: mat3x3<f32> = normal_matrix;
let _e80: vec3<f32> = a_tangent1;
tangent = normalize((_e79 * _e80));
let _e84: mat3x3<f32> = normal_matrix;
let _e85: vec3<f32> = a_bitangent1;
let _e87: mat3x3<f32> = normal_matrix;
let _e88: vec3<f32> = a_bitangent1;
bitangent = normalize((_e87 * _e88));
let _e92: vec3<f32> = tangent;
let _e93: vec3<f32> = bitangent;
let _e94: vec3<f32> = normal;
let _e108: vec3<f32> = tangent;
let _e109: vec3<f32> = bitangent;
let _e110: vec3<f32> = normal;
tangent_matrix = transpose(mat3x3<f32>(vec3<f32>(_e108.x, _e108.y, _e108.z), vec3<f32>(_e109.x, _e109.y, _e109.z), vec3<f32>(_e110.x, _e110.y, _e110.z)));
let _e126: mat4x4<f32> = model_matrix;
let _e127: vec3<f32> = a_position1;
model_space = (_e126 * vec4<f32>(_e127, 1.0));
let _e132: mat3x3<f32> = tangent_matrix;
let _e133: vec4<f32> = model_space;
v_position = (_e132 * _e133.xyz);
let _e136: mat3x3<f32> = tangent_matrix;
let _e137: vec3<f32> = global1.light_position;
v_light_position = (_e136 * _e137);
let _e139: mat3x3<f32> = tangent_matrix;
let _e140: vec3<f32> = global.u_view_position;
v_view_position = (_e139 * _e140);
let _e143: mat4x4<f32> = global.u_view_proj;
let _e144: vec4<f32> = model_space;
gl_Position = (_e143 * _e144);
return;
}
[[stage(vertex)]]
fn main([[location(0)]] a_position: vec3<f32>, [[location(1)]] a_tex_coords: vec2<f32>, [[location(2)]] a_normal: vec3<f32>, [[location(3)]] a_tangent: vec3<f32>, [[location(4)]] a_bitangent: vec3<f32>, [[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>, [[location(9)]] normal_matrix_0_: vec3<f32>, [[location(10)]] normal_matrix_1_: vec3<f32>, [[location(11)]] normal_matrix_2_: vec3<f32>) -> VertexOutput {
a_position1 = a_position;
a_tex_coords1 = a_tex_coords;
a_normal1 = a_normal;
a_tangent1 = a_tangent;
a_bitangent1 = a_bitangent;
model_matrix_0_1 = model_matrix_0_;
model_matrix_1_1 = model_matrix_1_;
model_matrix_2_1 = model_matrix_2_;
model_matrix_3_1 = model_matrix_3_;
normal_matrix_0_1 = normal_matrix_0_;
normal_matrix_1_1 = normal_matrix_1_;
normal_matrix_2_1 = normal_matrix_2_;
main1();
let _e65: vec2<f32> = v_tex_coords;
let _e67: vec3<f32> = v_position;
let _e69: vec3<f32> = v_light_position;
let _e71: vec3<f32> = v_view_position;
let _e73: vec4<f32> = gl_Position;
return VertexOutput(_e65, _e67, _e69, _e71, _e73);
}

@ -27,7 +27,6 @@ pub struct Display {
surface: wgpu::Surface,
pub window: Window,
pub config: wgpu::SurfaceConfiguration,
pub swap_chain: wgpu::SwapChain,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
}

@ -103,6 +103,7 @@ async fn run() {
encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
aspect: wgpu::TextureAspect::All,
texture: &render_target.texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,

@ -71,12 +71,10 @@ async fn run() {
let vs_module = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some("Vertex Shader"),
source: vs_data,
flags: wgpu::ShaderFlags::default(),
});
let fs_module = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some("Fragment Shader"),
source: fs_data,
flags: wgpu::ShaderFlags::default(),
});
let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
@ -155,7 +153,7 @@ async fn run() {
encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
aspect: wgpu::TextureAspect::All,
texture: &texture,
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},

Loading…
Cancel
Save