mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
[rs] Merge #799
799: Update naga to gfx-16 r=kvark a=kvark Makes WGSL constants much more ergonomic to write, and fixes the issues we had with the water example. Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
This commit is contained in:
@@ -26,20 +26,20 @@ webgl = ["wgc"]
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.wgc]
|
||||
package = "wgpu-core"
|
||||
git = "https://github.com/gfx-rs/wgpu"
|
||||
rev = "2f3b398e3887a336c963c43e7954f94cae42dd31"
|
||||
rev = "bb9a5a85d2bc1ee555739c646976f992a48127b5"
|
||||
features = ["raw-window-handle", "cross"]
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies.wgc]
|
||||
package = "wgpu-core"
|
||||
git = "https://github.com/gfx-rs/wgpu"
|
||||
rev = "2f3b398e3887a336c963c43e7954f94cae42dd31"
|
||||
rev = "bb9a5a85d2bc1ee555739c646976f992a48127b5"
|
||||
features = ["raw-window-handle", "cross"]
|
||||
optional = true
|
||||
|
||||
[dependencies.wgt]
|
||||
package = "wgpu-types"
|
||||
git = "https://github.com/gfx-rs/wgpu"
|
||||
rev = "2f3b398e3887a336c963c43e7954f94cae42dd31"
|
||||
rev = "bb9a5a85d2bc1ee555739c646976f992a48127b5"
|
||||
|
||||
[dependencies]
|
||||
arrayvec = "0.5"
|
||||
@@ -68,13 +68,13 @@ env_logger = "0.8"
|
||||
# used to test all the example shaders
|
||||
[dev-dependencies.naga]
|
||||
git = "https://github.com/gfx-rs/naga"
|
||||
tag = "gfx-15"
|
||||
tag = "gfx-16"
|
||||
features = ["wgsl-in"]
|
||||
|
||||
# used to generate SPIR-V for the Web target
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies.naga]
|
||||
git = "https://github.com/gfx-rs/naga"
|
||||
tag = "gfx-15"
|
||||
tag = "gfx-16"
|
||||
features = ["wgsl-in", "spv-out"]
|
||||
|
||||
[[example]]
|
||||
|
||||
@@ -4,8 +4,8 @@ fn main(
|
||||
[[location(1)]] particle_vel: vec2<f32>,
|
||||
[[location(2)]] position: vec2<f32>,
|
||||
) -> [[builtin(position)]] vec4<f32> {
|
||||
const angle: f32 = -atan2(particle_vel.x, particle_vel.y);
|
||||
const pos: vec2<f32> = vec2<f32>(
|
||||
const angle = -atan2(particle_vel.x, particle_vel.y);
|
||||
const pos = vec2<f32>(
|
||||
position.x * cos(angle) - position.y * sin(angle),
|
||||
position.x * sin(angle) + position.y * cos(angle)
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[[stage(vertex)]]
|
||||
fn vs_main([[builtin(vertex_index)]] in_vertex_index: u32) -> [[builtin(position)]] vec4<f32> {
|
||||
var x: f32 = f32(i32(in_vertex_index) - 1);
|
||||
var y: f32 = f32(i32(in_vertex_index & 1) * 2 - 1);
|
||||
const x = f32(i32(in_vertex_index) - 1);
|
||||
const y = f32(i32(in_vertex_index & 1) * 2 - 1);
|
||||
return vec4<f32>(x, y, 0.0, 1.0);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ struct VertexOutput {
|
||||
[[stage(vertex)]]
|
||||
fn vs_main([[builtin(vertex_index)]] vertex_index: u32) -> VertexOutput {
|
||||
var out: VertexOutput;
|
||||
var x: i32 = i32(vertex_index) / 2;
|
||||
var y: i32 = i32(vertex_index) & 1;
|
||||
const tc: vec2<f32> = vec2<f32>(
|
||||
const x = i32(vertex_index) / 2;
|
||||
const y = i32(vertex_index) & 1;
|
||||
const tc = vec2<f32>(
|
||||
f32(x) * 2.0,
|
||||
f32(y) * 2.0
|
||||
);
|
||||
|
||||
@@ -12,7 +12,7 @@ var r_data: Locals;
|
||||
|
||||
[[stage(vertex)]]
|
||||
fn vs_main([[builtin(vertex_index)]] vertex_index: u32) -> VertexOutput {
|
||||
var pos: vec2<f32> = vec2<f32>(
|
||||
const pos = vec2<f32>(
|
||||
100.0 * (1.0 - f32(vertex_index & 2u)),
|
||||
1000.0 * f32(vertex_index & 1u)
|
||||
);
|
||||
|
||||
@@ -32,8 +32,8 @@ fn vs_main(
|
||||
[[location(0)]] position: vec4<i32>,
|
||||
[[location(1)]] normal: vec4<i32>,
|
||||
) -> VertexOutput {
|
||||
const w: mat4x4<f32> = u_entity.world;
|
||||
const world_pos: vec4<f32> = u_entity.world * vec4<f32>(position);
|
||||
const w = u_entity.world;
|
||||
const world_pos = u_entity.world * vec4<f32>(position);
|
||||
var out: VertexOutput;
|
||||
out.world_normal = mat3x3<f32>(w.x.xyz, w.y.xyz, w.z.xyz) * vec3<f32>(normal.xyz);
|
||||
out.world_position = world_pos;
|
||||
@@ -67,10 +67,10 @@ fn fetch_shadow(light_id: u32, homogeneous_coords: vec4<f32>) -> f32 {
|
||||
return 1.0;
|
||||
}
|
||||
// compensate for the Y-flip difference between the NDC and texture coordinates
|
||||
const flip_correction: vec2<f32> = vec2<f32>(0.5, -0.5);
|
||||
const flip_correction = vec2<f32>(0.5, -0.5);
|
||||
// compute texture coordinates for shadow lookup
|
||||
const proj_correction: f32 = 1.0 / homogeneous_coords.w;
|
||||
const light_local: vec2<f32> = homogeneous_coords.xy * flip_correction * proj_correction + vec2<f32>(0.5, 0.5);
|
||||
const proj_correction = 1.0 / homogeneous_coords.w;
|
||||
const light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2<f32>(0.5, 0.5);
|
||||
// do the lookup, using HW PCF and comparison
|
||||
return textureSampleCompare(t_shadow, sampler_shadow, light_local, i32(light_id), homogeneous_coords.z * proj_correction);
|
||||
}
|
||||
@@ -80,7 +80,7 @@ const c_max_lights: u32 = 10u;
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||
const normal: vec3<f32> = normalize(in.world_normal);
|
||||
const normal = normalize(in.world_normal);
|
||||
// accumulate color
|
||||
var color: vec3<f32> = c_ambient;
|
||||
var i: u32 = 0u;
|
||||
@@ -88,12 +88,12 @@ fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||
if (i >= min(u_globals.num_lights.x, c_max_lights)) {
|
||||
break;
|
||||
}
|
||||
const light: Light = s_lights.data[i];
|
||||
const light = s_lights.data[i];
|
||||
// project into the light space
|
||||
const shadow: f32 = fetch_shadow(i, light.proj * in.world_position);
|
||||
const shadow = fetch_shadow(i, light.proj * in.world_position);
|
||||
// compute Lambertian diffuse term
|
||||
const light_dir: vec3<f32> = normalize(light.pos.xyz - in.world_position.xyz);
|
||||
const diffuse: f32 = max(0.0, dot(normal, light_dir));
|
||||
const light_dir = normalize(light.pos.xyz - in.world_position.xyz);
|
||||
const diffuse = max(0.0, dot(normal, light_dir));
|
||||
// add light contribution
|
||||
color = color + shadow * diffuse * light.color.xyz;
|
||||
continuing {
|
||||
|
||||
@@ -20,9 +20,9 @@ var r_data: Data;
|
||||
[[stage(vertex)]]
|
||||
fn vs_sky([[builtin(vertex_index)]] vertex_index: u32) -> SkyOutput {
|
||||
// hacky way to draw a large triangle
|
||||
var tmp1: i32 = i32(vertex_index) / 2;
|
||||
var tmp2: i32 = i32(vertex_index) & 1;
|
||||
const pos: vec4<f32> = vec4<f32>(
|
||||
const tmp1 = i32(vertex_index) / 2;
|
||||
const tmp2 = i32(vertex_index) & 1;
|
||||
const pos = vec4<f32>(
|
||||
f32(tmp1) * 4.0 - 1.0,
|
||||
f32(tmp2) * 4.0 - 1.0,
|
||||
1.0,
|
||||
@@ -30,8 +30,8 @@ fn vs_sky([[builtin(vertex_index)]] vertex_index: u32) -> SkyOutput {
|
||||
);
|
||||
|
||||
// transposition = inversion for this orthonormal matrix
|
||||
const inv_model_view: mat3x3<f32> = transpose(mat3x3<f32>(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz));
|
||||
const unprojected: vec4<f32> = r_data.proj_inv * pos;
|
||||
const inv_model_view = transpose(mat3x3<f32>(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz));
|
||||
const unprojected = r_data.proj_inv * pos;
|
||||
|
||||
var out: SkyOutput;
|
||||
out.uv = inv_model_view * unprojected.xyz;
|
||||
@@ -69,10 +69,10 @@ fn fs_sky(in: SkyOutput) -> [[location(0)]] vec4<f32> {
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn fs_entity(in: EntityOutput) -> [[location(0)]] vec4<f32> {
|
||||
const incident: vec3<f32> = normalize(in.view);
|
||||
const normal: vec3<f32> = normalize(in.normal);
|
||||
const reflected: vec3<f32> = incident - 2.0 * dot(normal, incident) * normal;
|
||||
const incident = normalize(in.view);
|
||||
const normal = normalize(in.normal);
|
||||
const reflected = incident - 2.0 * dot(normal, incident) * normal;
|
||||
|
||||
const reflected_color: vec4<f32> = textureSample(r_texture, r_sampler, reflected);
|
||||
const reflected_color = textureSample(r_texture, r_sampler, reflected);
|
||||
return vec4<f32>(0.1, 0.1, 0.1, 0.1) + 0.5 * reflected_color;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user