normal tutorial working on wgpu master

web2
Ben Hansen 2 years ago
parent bd8172d0a2
commit dad581dce9

191
Cargo.lock generated

@ -124,6 +124,15 @@ dependencies = [
"libloading",
]
[[package]]
name = "ash"
version = "0.35.2+1.2.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f6a491bcad4563b355ac2bb6e3f09d5e1c5d628710c7156e901dad0c416075e"
dependencies = [
"libloading",
]
[[package]]
name = "atty"
version = "0.2.14"
@ -415,11 +424,11 @@ dependencies = [
"glob",
"image 0.23.14",
"log",
"naga 0.8.0",
"naga 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pollster",
"rayon",
"tobj 2.0.4",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -859,12 +868,12 @@ dependencies = [
"glob",
"image 0.23.14",
"log",
"naga 0.8.0",
"naga 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pollster",
"rayon",
"thiserror",
"tobj 2.0.4",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu-subscriber",
"winit",
]
@ -970,7 +979,7 @@ dependencies = [
"log",
"pollster",
"tobj 3.2.0",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -1370,6 +1379,7 @@ checksum = "8c2352bd1d0bceb871cb9d40f24360c8133c11d7486b68b5381c1dd1a32015e3"
dependencies = [
"libc",
"libloading",
"pkg-config",
]
[[package]]
@ -1507,6 +1517,19 @@ dependencies = [
"objc",
]
[[package]]
name = "metal"
version = "0.23.1"
source = "git+https://github.com/gfx-rs/metal-rs?rev=a357159#a35715916fec38bbc08a510ecf7d115edc500c72"
dependencies = [
"bitflags",
"block",
"core-graphics-types",
"foreign-types",
"log",
"objc",
]
[[package]]
name = "mime"
version = "0.3.16"
@ -1619,6 +1642,23 @@ dependencies = [
"thiserror",
]
[[package]]
name = "naga"
version = "0.8.0"
source = "git+https://github.com/gfx-rs/naga?rev=a45b9a6#a45b9a6cc691a671aa24a32114b51c5acae02420"
dependencies = [
"bit-set",
"bitflags",
"codespan-reporting",
"hexf-parse",
"indexmap",
"log",
"num-traits",
"rustc-hash",
"spirv",
"thiserror",
]
[[package]]
name = "native-tls"
version = "0.2.8"
@ -2123,7 +2163,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu_glyph",
"winit",
]
@ -2880,7 +2920,7 @@ dependencies = [
"pollster",
"wasm-bindgen",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -2905,7 +2945,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -2930,7 +2970,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
"winit",
]
@ -2955,7 +2995,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -2981,7 +3021,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -2998,7 +3038,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3015,7 +3055,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3036,7 +3076,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3058,7 +3098,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3081,7 +3121,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3104,7 +3144,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3127,7 +3167,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3152,7 +3192,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]
@ -3539,16 +3579,36 @@ dependencies = [
"arrayvec",
"js-sys",
"log",
"naga 0.8.0",
"naga 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot",
"raw-window-handle 0.4.2",
"smallvec",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu-core",
"wgpu-hal",
"wgpu-types",
"wgpu-core 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu-hal 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu-types 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wgpu"
version = "0.12.0"
source = "git+https://github.com/gfx-rs/wgpu#d3c210160f90e5fb960025bc1ca155a8eea19052"
dependencies = [
"arrayvec",
"js-sys",
"log",
"naga 0.8.0 (git+https://github.com/gfx-rs/naga?rev=a45b9a6)",
"parking_lot",
"raw-window-handle 0.4.2",
"smallvec",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu-core 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
"wgpu-hal 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
"wgpu-types 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
]
[[package]]
@ -3564,14 +3624,36 @@ dependencies = [
"copyless",
"fxhash",
"log",
"naga 0.8.0",
"naga 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot",
"profiling",
"raw-window-handle 0.4.2",
"smallvec",
"thiserror",
"wgpu-hal 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu-types 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wgpu-core"
version = "0.12.0"
source = "git+https://github.com/gfx-rs/wgpu#d3c210160f90e5fb960025bc1ca155a8eea19052"
dependencies = [
"arrayvec",
"bitflags",
"cfg_aliases",
"codespan-reporting",
"copyless",
"fxhash",
"log",
"naga 0.8.0 (git+https://github.com/gfx-rs/naga?rev=a45b9a6)",
"parking_lot",
"profiling",
"raw-window-handle 0.4.2",
"smallvec",
"thiserror",
"wgpu-hal",
"wgpu-types",
"wgpu-hal 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
"wgpu-types 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
]
[[package]]
@ -3581,7 +3663,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1abbd51f4f3d61db9c02dd776f643437823b6fa85e8642b4bb6d278b74335fc1"
dependencies = [
"arrayvec",
"ash",
"ash 0.33.3+1.2.191",
"bit-set",
"bitflags",
"block",
"core-graphics-types",
"d3d12",
"foreign-types",
"fxhash",
"glow",
"gpu-alloc",
"gpu-descriptor",
"inplace_it",
"js-sys",
"khronos-egl",
"libloading",
"log",
"metal 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)",
"naga 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"objc",
"parking_lot",
"profiling",
"range-alloc",
"raw-window-handle 0.4.2",
"renderdoc-sys",
"thiserror",
"wasm-bindgen",
"web-sys",
"wgpu-types 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi",
]
[[package]]
name = "wgpu-hal"
version = "0.12.0"
source = "git+https://github.com/gfx-rs/wgpu#d3c210160f90e5fb960025bc1ca155a8eea19052"
dependencies = [
"arrayvec",
"ash 0.35.2+1.2.203",
"bit-set",
"bitflags",
"block",
@ -3597,8 +3716,8 @@ dependencies = [
"khronos-egl",
"libloading",
"log",
"metal",
"naga 0.8.0",
"metal 0.23.1 (git+https://github.com/gfx-rs/metal-rs?rev=a357159)",
"naga 0.8.0 (git+https://github.com/gfx-rs/naga?rev=a45b9a6)",
"objc",
"parking_lot",
"profiling",
@ -3608,7 +3727,7 @@ dependencies = [
"thiserror",
"wasm-bindgen",
"web-sys",
"wgpu-types",
"wgpu-types 0.12.0 (git+https://github.com/gfx-rs/wgpu)",
"winapi",
]
@ -3634,6 +3753,14 @@ dependencies = [
"bitflags",
]
[[package]]
name = "wgpu-types"
version = "0.12.0"
source = "git+https://github.com/gfx-rs/wgpu#d3c210160f90e5fb960025bc1ca155a8eea19052"
dependencies = [
"bitflags",
]
[[package]]
name = "wgpu_glyph"
version = "0.16.0"
@ -3643,7 +3770,7 @@ dependencies = [
"bytemuck",
"glyph_brush",
"log",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -3686,7 +3813,7 @@ dependencies = [
"pollster",
"raw-window-handle 0.3.4",
"shaderc",
"wgpu",
"wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit",
]

@ -16,7 +16,7 @@ env_logger = "0.9"
pollster = "0.2"
log = "0.4"
tobj = { version = "3.2", features = ["async"]}
wgpu = "0.12"
wgpu = { version = "0.12", git="https://github.com/gfx-rs/wgpu"}
winit = "0.26"
[dependencies.image]
@ -28,13 +28,18 @@ features = ["png", "jpeg"]
reqwest = { version = "0.11" }
console_error_panic_hook = "0.1"
console_log = "0.2"
wgpu = { version = "0.12", features = ["webgl"]}
wgpu = { version = "0.12", git="https://github.com/gfx-rs/wgpu", features = ["webgl"]}
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
'Headers',
'Request',
'RequestInit',
'RequestMode',
'Response',
]}
[build-dependencies]

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pong with WASM</title>
</head>
<body>
<div id="wasm-example"></div>
<script type="module">
import init from "./pkg/tutorial11_normals.js";
init().then(() => {
console.log("WASM Loaded");
});
</script>
<style>
body {
background-color: #444;
}
canvas {
background-color: black;
}
</style>
</body>
</html>

@ -555,6 +555,7 @@ impl State {
push_constant_ranges: &[],
});
log::info!("Render Pipeline");
let render_pipeline = {
let shader = wgpu::ShaderModuleDescriptor {
label: Some("Normal Shader"),
@ -570,6 +571,7 @@ impl State {
)
};
log::info!("Light Render Pipeline");
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
@ -749,7 +751,7 @@ pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init_with_level(log::Level::Warn).expect("Could't initialize logger");
console_log::init_with_level(log::Level::Info).expect("Could't initialize logger");
} else {
env_logger::init();
}

@ -4,26 +4,26 @@ struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
@group(0) @binding(0)
var<uniform> camera: Camera;
struct Light {
position: vec3<f32>;
color: vec3<f32>;
};
[[group(1), binding(0)]]
@group(1) @binding(0)
var<uniform> light: Light;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
@location(0) position: vec3<f32>;
};
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] color: vec3<f32>;
@builtin(position) clip_position: vec4<f32>;
@location(0) color: vec3<f32>;
};
[[stage(vertex)]]
@stage(vertex)
fn vs_main(
model: VertexInput,
) -> VertexOutput {
@ -36,7 +36,7 @@ fn vs_main(
// Fragment shader
[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
@stage(fragment)
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(in.color, 1.0);
}

@ -5,10 +5,35 @@ use wgpu::util::DeviceExt;
use crate::{model, texture};
cfg_if!{
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};
}
}
#[cfg(target_arch = "wams32")]
async fn request(file_name: &str) -> Result<Response, JsValue> {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
let uri = format!("/res/{}", file_name);
let req = Request::new_with_str_and_init(&uri, &opts)?;
let window = web_sys::window().unwrap();
let raw_res = JsFuture::from(window.fetch_with_request(&req)).await?;
let res: Response = res.dyn_into().unwrap();
Ok(res)
}
pub async fn load_string(file_name: &str) -> anyhow::Result<String> {
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
let url = format!("http://127.0.0.1:8080/learn-wgpu/{}", file_name);
let url = format!("http://127.0.0.1:8080/res/{}", file_name);
let txt = reqwest::get(&url)
.await?
.text()
@ -27,7 +52,7 @@ pub async fn load_string(file_name: &str) -> anyhow::Result<String> {
pub async fn load_binary(file_name: &str) -> anyhow::Result<Vec<u8>> {
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
let url = format!("http://127.0.0.1:8080/learn-wgpu/{}", file_name);
let url = format!("http://127.0.0.1:8080/res/{}", file_name);
let data = reqwest::get(url)
.await?
.bytes()

@ -4,42 +4,42 @@ struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
@group(1) @binding(0)
var<uniform> camera: Camera;
struct Light {
position: vec3<f32>;
color: vec3<f32>;
};
[[group(2), binding(0)]]
@group(2) @binding(0)
var<uniform> light: Light;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
[[location(1)]] tex_coords: vec2<f32>;
[[location(2)]] normal: vec3<f32>;
[[location(3)]] tangent: vec3<f32>;
[[location(4)]] bitangent: vec3<f32>;
@location(0) position: vec3<f32>;
@location(1) tex_coords: vec2<f32>;
@location(2) normal: vec3<f32>;
@location(3) tangent: vec3<f32>;
@location(4) bitangent: vec3<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>;
[[location(9)]] normal_matrix_0: vec3<f32>;
[[location(10)]] normal_matrix_1: vec3<f32>;
[[location(11)]] normal_matrix_2: 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>;
};
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] tex_coords: vec2<f32>;
[[location(1)]] tangent_position: vec3<f32>;
[[location(2)]] tangent_light_position: vec3<f32>;
[[location(3)]] tangent_view_position: vec3<f32>;
@builtin(position) clip_position: vec4<f32>;
@location(0) tex_coords: vec2<f32>;
@location(1) tangent_position: vec3<f32>;
@location(2) tangent_light_position: vec3<f32>;
@location(3) tangent_view_position: vec3<f32>;
};
[[stage(vertex)]]
@stage(vertex)
fn vs_main(
model: VertexInput,
instance: InstanceInput,
@ -79,17 +79,17 @@ fn vs_main(
// Fragment shader
[[group(0), binding(0)]]
@group(0) @binding(0)
var t_diffuse: texture_2d<f32>;
[[group(0), binding(1)]]
@group(0) @binding(1)
var s_diffuse: sampler;
[[group(0), binding(2)]]
@group(0) @binding(2)
var t_normal: texture_2d<f32>;
[[group(0), binding(3)]]
@group(0) @binding(3)
var s_normal: sampler;
[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
@stage(fragment)
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords);
let object_normal: vec4<f32> = textureSample(t_normal, s_normal, in.tex_coords);

Loading…
Cancel
Save