mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
WGSL: const to let
This commit is contained in:
committed by
Dzmitry Malyshau
parent
98252cf5d2
commit
142038c85b
@@ -123,7 +123,7 @@ pub enum Error<'a> {
|
||||
#[error("builtin {0:?} is not implemented")]
|
||||
UnimplementedBuiltin(crate::BuiltIn),
|
||||
#[error("expression {0} doesn't match its given type {1:?}")]
|
||||
ConstTypeMismatch(&'a str, Handle<crate::Type>),
|
||||
LetTypeMismatch(&'a str, Handle<crate::Type>),
|
||||
#[error("other error")]
|
||||
Other,
|
||||
}
|
||||
@@ -1999,7 +1999,7 @@ impl Parser {
|
||||
self.scopes.push(Scope::Statement);
|
||||
let mut emitter = super::Emitter::default();
|
||||
match word {
|
||||
"const" => {
|
||||
"let" => {
|
||||
emitter.start(context.expressions);
|
||||
let name = lexer.next_ident()?;
|
||||
let given_ty = if lexer.skip(Token::Separator(':')) {
|
||||
@@ -2026,7 +2026,7 @@ impl Parser {
|
||||
given_inner,
|
||||
expr_inner
|
||||
);
|
||||
return Err(Error::ConstTypeMismatch(name, ty));
|
||||
return Err(Error::LetTypeMismatch(name, ty));
|
||||
}
|
||||
}
|
||||
block.extend(emitter.finish(context.expressions));
|
||||
@@ -2584,7 +2584,7 @@ impl Parser {
|
||||
self.lookup_type.insert(name.to_owned(), ty);
|
||||
lexer.expect(Token::Separator(';'))?;
|
||||
}
|
||||
(Token::Word("const"), _) => {
|
||||
(Token::Word("let"), _) => {
|
||||
let (name, explicit_ty, _access) = self.parse_variable_ident_decl(
|
||||
lexer,
|
||||
&mut module.types,
|
||||
@@ -2611,7 +2611,7 @@ impl Parser {
|
||||
crate::ConstantInner::Composite { ty, components: _ } => ty == explicit_ty,
|
||||
};
|
||||
if !type_match {
|
||||
return Err(Error::ConstTypeMismatch(name, explicit_ty));
|
||||
return Err(Error::LetTypeMismatch(name, explicit_ty));
|
||||
}
|
||||
//TODO: check `ty` against `const_handle`.
|
||||
lexer.expect(Token::Separator(';'))?;
|
||||
|
||||
@@ -16,8 +16,8 @@ fn parse_comment() {
|
||||
|
||||
#[test]
|
||||
fn parse_types() {
|
||||
parse_str("const a : i32 = 2;").unwrap();
|
||||
assert!(parse_str("const a : x32 = 2;").is_err());
|
||||
parse_str("let a : i32 = 2;").unwrap();
|
||||
assert!(parse_str("let a : x32 = 2;").is_err());
|
||||
parse_str("var t: texture_2d<f32>;").unwrap();
|
||||
parse_str("var t: texture_cube_array<i32>;").unwrap();
|
||||
parse_str("var t: texture_multisampled_2d<u32>;").unwrap();
|
||||
@@ -30,14 +30,14 @@ fn parse_type_inference() {
|
||||
parse_str(
|
||||
"
|
||||
fn foo() {
|
||||
const a = 2u;
|
||||
const b: u32 = a;
|
||||
let a = 2u;
|
||||
let b: u32 = a;
|
||||
}",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(parse_str(
|
||||
"
|
||||
fn foo() { const c : i32 = 2.0; }",
|
||||
fn foo() { let c : i32 = 2.0; }",
|
||||
)
|
||||
.is_err());
|
||||
}
|
||||
@@ -46,7 +46,7 @@ fn parse_type_inference() {
|
||||
fn parse_type_cast() {
|
||||
parse_str(
|
||||
"
|
||||
const a : i32 = 2;
|
||||
let a : i32 = 2;
|
||||
fn main() {
|
||||
var x: f32 = f32(a);
|
||||
x = f32(i32(a + 1) / 2);
|
||||
@@ -57,8 +57,8 @@ fn parse_type_cast() {
|
||||
parse_str(
|
||||
"
|
||||
fn main() {
|
||||
const x: vec2<f32> = vec2<f32>(1.0, 2.0);
|
||||
const y: vec2<u32> = vec2<u32>(x);
|
||||
let x: vec2<f32> = vec2<f32>(1.0, 2.0);
|
||||
let y: vec2<u32> = vec2<u32>(x);
|
||||
}
|
||||
",
|
||||
)
|
||||
@@ -201,7 +201,7 @@ fn parse_texture_load() {
|
||||
"
|
||||
var t: texture_3d<u32>;
|
||||
fn foo() {
|
||||
const r: vec4<u32> = textureLoad(t, vec3<u32>(0.0, 1.0, 2.0), 1);
|
||||
let r: vec4<u32> = textureLoad(t, vec3<u32>(0.0, 1.0, 2.0), 1);
|
||||
}
|
||||
",
|
||||
)
|
||||
@@ -210,7 +210,7 @@ fn parse_texture_load() {
|
||||
"
|
||||
var t: texture_multisampled_2d_array<i32>;
|
||||
fn foo() {
|
||||
const r: vec4<i32> = textureLoad(t, vec2<i32>(10, 20), 2, 3);
|
||||
let r: vec4<i32> = textureLoad(t, vec2<i32>(10, 20), 2, 3);
|
||||
}
|
||||
",
|
||||
)
|
||||
@@ -219,7 +219,7 @@ fn parse_texture_load() {
|
||||
"
|
||||
var t: [[access(read)]] texture_storage_1d_array<r32float>;
|
||||
fn foo() {
|
||||
const r: vec4<f32> = textureLoad(t, 10, 2);
|
||||
let r: vec4<f32> = textureLoad(t, 10, 2);
|
||||
}
|
||||
",
|
||||
)
|
||||
@@ -247,8 +247,8 @@ fn parse_texture_query() {
|
||||
fn foo() {
|
||||
var dim: vec2<i32> = textureDimensions(t);
|
||||
dim = textureDimensions(t, 0);
|
||||
const layers: i32 = textureNumLayers(t);
|
||||
const samples: i32 = textureNumSamples(t);
|
||||
let layers: i32 = textureNumLayers(t);
|
||||
let samples: i32 = textureNumSamples(t);
|
||||
}
|
||||
",
|
||||
)
|
||||
@@ -259,8 +259,8 @@ fn parse_texture_query() {
|
||||
fn parse_postfix() {
|
||||
parse_str(
|
||||
"fn foo() {
|
||||
const x: f32 = vec4<f32>(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g;
|
||||
const y: f32 = fract(vec2<f32>(0.5, x)).x;
|
||||
let x: f32 = vec4<f32>(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g;
|
||||
let y: f32 = fract(vec2<f32>(0.5, x)).x;
|
||||
}",
|
||||
)
|
||||
.unwrap();
|
||||
@@ -269,9 +269,9 @@ fn parse_postfix() {
|
||||
#[test]
|
||||
fn parse_expressions() {
|
||||
parse_str("fn foo() {
|
||||
const x: f32 = select(0.0, 1.0, true);
|
||||
const y: vec2<f32> = select(vec2<f32>(1.0, 1.0), vec2<f32>(x, x), vec2<bool>(x < 0.5, x > 0.5));
|
||||
const z: bool = !(0.0 == 1.0);
|
||||
let x: f32 = select(0.0, 1.0, true);
|
||||
let y: vec2<f32> = select(vec2<f32>(1.0, 1.0), vec2<f32>(x, x), vec2<bool>(x < 0.5, x > 0.5));
|
||||
let z: bool = !(0.0 == 1.0);
|
||||
}").unwrap();
|
||||
}
|
||||
|
||||
@@ -280,8 +280,8 @@ fn parse_pointers() {
|
||||
parse_str(
|
||||
"fn foo() {
|
||||
var x: f32 = 1.0;
|
||||
const px = &x;
|
||||
const py = frexp(0.5, px);
|
||||
let px = &x;
|
||||
let py = frexp(0.5, px);
|
||||
}",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -46,13 +46,13 @@ fn invalid_integer() {
|
||||
#[test]
|
||||
fn invalid_float() {
|
||||
err!(
|
||||
"const scale: f32 = 1.1.;",
|
||||
"let scale: f32 = 1.1.;",
|
||||
@r###"
|
||||
error: expected floating-point literal, found `1.1.`
|
||||
┌─ wgsl:1:20
|
||||
┌─ wgsl:1:18
|
||||
│
|
||||
1 │ const scale: f32 = 1.1.;
|
||||
│ ^^^^ expected floating-point literal
|
||||
1 │ let scale: f32 = 1.1.;
|
||||
│ ^^^^ expected floating-point literal
|
||||
|
||||
"###
|
||||
);
|
||||
@@ -62,13 +62,13 @@ fn invalid_float() {
|
||||
#[test]
|
||||
fn invalid_scalar_width() {
|
||||
err!(
|
||||
"const scale: f32 = 1.1f1000;",
|
||||
"let scale: f32 = 1.1f1000;",
|
||||
@r###"
|
||||
error: invalid width of `1000` for literal
|
||||
┌─ wgsl:1:20
|
||||
┌─ wgsl:1:18
|
||||
│
|
||||
1 │ const scale: f32 = 1.1f1000;
|
||||
│ ^^^^^^^^ invalid width
|
||||
1 │ let scale: f32 = 1.1f1000;
|
||||
│ ^^^^^^^^ invalid width
|
||||
│
|
||||
= note: valid width is 32
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const NUM_PARTICLES: u32 = 1500u;
|
||||
let NUM_PARTICLES: u32 = 1500u;
|
||||
|
||||
struct Particle {
|
||||
pos : vec2<f32>;
|
||||
@@ -28,7 +28,7 @@ struct Particles {
|
||||
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
||||
[[stage(compute), workgroup_size(64)]]
|
||||
fn main([[builtin(global_invocation_id)]] global_invocation_id : vec3<u32>) {
|
||||
const index : u32 = global_invocation_id.x;
|
||||
let index : u32 = global_invocation_id.x;
|
||||
if (index >= NUM_PARTICLES) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ fn main(
|
||||
//TODO: https://github.com/gpuweb/gpuweb/issues/1590
|
||||
//[[builtin(workgroup_size)]] wg_size: vec3<u32>
|
||||
) {
|
||||
const dim = textureDimensions(image_src);
|
||||
const itc = dim * vec2<i32>(local_id.xy) % vec2<i32>(10, 20);
|
||||
const value = textureLoad(image_src, itc);
|
||||
let dim = textureDimensions(image_src);
|
||||
let itc = dim * vec2<i32>(local_id.xy) % vec2<i32>(10, 20);
|
||||
let value = textureLoad(image_src, itc);
|
||||
textureStore(image_dst, itc.x, value);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// vertex
|
||||
const c_scale: f32 = 1.2;
|
||||
let c_scale: f32 = 1.2;
|
||||
|
||||
struct VertexOutput {
|
||||
[[location(0)]] uv : vec2<f32>;
|
||||
@@ -20,12 +20,12 @@ fn main([[location(0)]] pos : vec2<f32>, [[location(1)]] uv : vec2<f32>) -> Vert
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn main([[location(0)]] uv : vec2<f32>) -> [[location(0)]] vec4<f32> {
|
||||
const color = textureSample(u_texture, u_sampler, uv);
|
||||
let color = textureSample(u_texture, u_sampler, uv);
|
||||
if (color.a == 0.0) {
|
||||
discard;
|
||||
}
|
||||
// forcing the expression here to be emitted in order to check the
|
||||
// uniformity of the control flow a bit more strongly.
|
||||
const premultiplied = color.a * color;
|
||||
let premultiplied = color.a * color;
|
||||
return premultiplied;
|
||||
}
|
||||
|
||||
@@ -28,21 +28,21 @@ fn fetch_shadow(light_id: u32, homogeneous_coords: vec4<f32>) -> f32 {
|
||||
if (homogeneous_coords.w <= 0.0) {
|
||||
return 1.0;
|
||||
}
|
||||
const flip_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);
|
||||
let flip_correction = vec2<f32>(0.5, -0.5);
|
||||
let proj_correction = 1.0 / homogeneous_coords.w;
|
||||
let light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2<f32>(0.5, 0.5);
|
||||
return textureSampleCompare(t_shadow, sampler_shadow, light_local, i32(light_id), homogeneous_coords.z * proj_correction);
|
||||
}
|
||||
|
||||
const c_ambient: vec3<f32> = vec3<f32>(0.05, 0.05, 0.05);
|
||||
const c_max_lights: u32 = 10u;
|
||||
let c_ambient: vec3<f32> = vec3<f32>(0.05, 0.05, 0.05);
|
||||
let c_max_lights: u32 = 10u;
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn fs_main(
|
||||
[[location(0)]] raw_normal: vec3<f32>,
|
||||
[[location(1)]] position: vec4<f32>
|
||||
) -> [[location(0)]] vec4<f32> {
|
||||
const normal: vec3<f32> = normalize(raw_normal);
|
||||
let normal: vec3<f32> = normalize(raw_normal);
|
||||
// accumulate color
|
||||
var color: vec3<f32> = c_ambient;
|
||||
var i: u32 = 0u;
|
||||
@@ -50,10 +50,10 @@ fn fs_main(
|
||||
if (i >= min(u_globals.num_lights.x, c_max_lights)) {
|
||||
break;
|
||||
}
|
||||
const light = s_lights.data[i];
|
||||
const shadow = fetch_shadow(i, light.proj * position);
|
||||
const light_dir = normalize(light.pos.xyz - position.xyz);
|
||||
const diffuse = max(0.0, dot(normal, light_dir));
|
||||
let light = s_lights.data[i];
|
||||
let shadow = fetch_shadow(i, light.proj * position);
|
||||
let light_dir = normalize(light.pos.xyz - position.xyz);
|
||||
let diffuse = max(0.0, dot(normal, light_dir));
|
||||
color = color + shadow * diffuse * light.color.xyz;
|
||||
continuing {
|
||||
i = i + 1u;
|
||||
|
||||
@@ -16,15 +16,15 @@ fn vs_main([[builtin(vertex_index)]] vertex_index: u32) -> VertexOutput {
|
||||
// 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>(
|
||||
let pos = vec4<f32>(
|
||||
f32(tmp1) * 4.0 - 1.0,
|
||||
f32(tmp2) * 4.0 - 1.0,
|
||||
0.0,
|
||||
1.0
|
||||
);
|
||||
|
||||
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;
|
||||
let inv_model_view = transpose(mat3x3<f32>(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz));
|
||||
let unprojected = r_data.proj_inv * pos;
|
||||
var out: VertexOutput;
|
||||
out.uv = inv_model_view * unprojected.xyz;
|
||||
out.position = pos;
|
||||
|
||||
Reference in New Issue
Block a user