wgpu/tests/in/operators.wgsl

309 lines
6.1 KiB
WebGPU Shading Language
Raw Normal View History

2021-07-22 19:31:05 +00:00
//TODO: support splatting constructors for globals?
let v_f32_one = vec4<f32>(1.0, 1.0, 1.0, 1.0);
let v_f32_zero = vec4<f32>(0.0, 0.0, 0.0, 0.0);
let v_f32_half = vec4<f32>(0.5, 0.5, 0.5, 0.5);
let v_i32_one = vec4<i32>(1, 1, 1, 1);
2021-07-22 19:31:05 +00:00
fn builtins() -> vec4<f32> {
// select()
let condition = true;
let s1 = select(0, 1, condition);
let s2 = select(v_f32_zero, v_f32_one, condition);
2021-08-04 22:59:25 +00:00
let s3 = select(v_f32_one, v_f32_zero, vec4<bool>(false, false, false, false));
2021-07-22 19:31:05 +00:00
// mix()
let m1 = mix(v_f32_zero, v_f32_one, v_f32_half);
let m2 = mix(v_f32_zero, v_f32_one, 0.1);
// bitcast()
let b1 = bitcast<f32>(v_i32_one.x);
let b2 = bitcast<vec4<f32>>(v_i32_one);
2021-09-10 01:04:38 +00:00
// convert
let v_i32_zero = vec4<i32>(v_f32_zero);
2021-07-22 19:31:05 +00:00
// done
2021-09-10 01:04:38 +00:00
return vec4<f32>(vec4<i32>(s1) + v_i32_zero) + s2 + m1 + m2 + b1 + b2;
2021-07-22 19:31:05 +00:00
}
2021-04-27 05:10:02 +00:00
fn splat() -> vec4<f32> {
let a = (1.0 + vec2<f32>(2.0) - 3.0) / 4.0;
let b = vec4<i32>(5) % 2;
return a.xyxy + vec4<f32>(b);
}
2021-04-27 05:10:02 +00:00
fn bool_cast(x: vec3<f32>) -> vec3<f32> {
let y = vec3<bool>(x);
return vec3<f32>(y);
}
2021-09-03 16:19:00 +00:00
struct Foo {
a: vec4<f32>,
b: i32,
}
2021-09-03 16:19:00 +00:00
fn constructors() -> f32 {
var foo: Foo;
foo = Foo(vec4<f32>(1.0), 1);
let mat2comp = mat2x2<f32>(
1.0, 0.0,
0.0, 1.0,
);
let mat4comp = mat4x4<f32>(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
);
// zero value constructors
2022-04-26 16:40:37 +00:00
_ = bool();
_ = i32();
_ = u32();
_ = f32();
_ = vec2<u32>();
_ = mat2x2<f32>();
_ = array<Foo, 3>();
_ = Foo();
// constructors that infer their type from their parameters
2022-04-26 16:40:37 +00:00
_ = vec2(0u);
_ = mat2x2(vec2(0.), vec2(0.));
_ = array(0, 1, 2, 3);
// identity constructors
_ = bool(bool());
_ = i32(i32());
_ = u32(u32());
_ = f32(f32());
_ = vec2<u32>(vec2<u32>());
_ = mat2x3<f32>(mat2x3<f32>());
_ = vec2(vec2<u32>());
_ = mat2x3(mat2x3<f32>());
2021-09-03 16:19:00 +00:00
return foo.a.x;
}
2022-04-12 13:50:10 +00:00
fn logical() {
// unary
2022-04-26 16:40:37 +00:00
_ = !true;
_ = !vec2(true);
2022-04-12 13:50:10 +00:00
// binary
2022-04-26 16:40:37 +00:00
_ = true || false;
_ = true && false;
_ = true | false;
_ = vec3(true) | vec3(false);
_ = true & false;
_ = vec4(true) & vec4(false);
}
2022-04-12 13:50:10 +00:00
fn arithmetic() {
// unary
2022-04-12 14:33:43 +00:00
// TODO: uncomment when we get the changes from https://github.com/gfx-rs/rspirv/pull/231
2022-04-26 16:40:37 +00:00
// _ = -1;
_ = -1.0;
_ = -vec2(1);
_ = -vec2(1.0);
2022-04-12 13:50:10 +00:00
// binary
// Addition
2022-04-26 16:40:37 +00:00
_ = 2 + 1;
_ = 2u + 1u;
_ = 2.0 + 1.0;
_ = vec2(2) + vec2(1);
_ = vec3(2u) + vec3(1u);
_ = vec4(2.0) + vec4(1.0);
2022-04-12 13:50:10 +00:00
// Subtraction
2022-04-26 16:40:37 +00:00
_ = 2 - 1;
_ = 2u - 1u;
_ = 2.0 - 1.0;
_ = vec2(2) - vec2(1);
_ = vec3(2u) - vec3(1u);
_ = vec4(2.0) - vec4(1.0);
2022-04-12 13:50:10 +00:00
// Multiplication
2022-04-26 16:40:37 +00:00
_ = 2 * 1;
_ = 2u * 1u;
_ = 2.0 * 1.0;
_ = vec2(2) * vec2(1);
_ = vec3(2u) * vec3(1u);
_ = vec4(2.0) * vec4(1.0);
2022-04-12 13:50:10 +00:00
// Division
2022-04-26 16:40:37 +00:00
_ = 2 / 1;
_ = 2u / 1u;
_ = 2.0 / 1.0;
_ = vec2(2) / vec2(1);
_ = vec3(2u) / vec3(1u);
_ = vec4(2.0) / vec4(1.0);
2022-04-12 13:50:10 +00:00
// Remainder
2022-04-26 16:40:37 +00:00
_ = 2 % 1;
_ = 2u % 1u;
_ = 2.0 % 1.0;
_ = vec2(2) % vec2(1);
_ = vec3(2u) % vec3(1u);
_ = vec4(2.0) % vec4(1.0);
2022-04-12 13:50:10 +00:00
// Binary arithmetic expressions with mixed scalar and vector operands
2022-04-26 16:40:37 +00:00
_ = vec2(2) + 1;
_ = 2 + vec2(1);
_ = vec2(2u) + 1u;
_ = 2u + vec2(1u);
_ = vec2(2.0) + 1.0;
_ = 2.0 + vec2(1.0);
_ = vec2(2) - 1;
_ = 2 - vec2(1);
_ = vec2(2u) - 1u;
_ = 2u - vec2(1u);
_ = vec2(2.0) - 1.0;
_ = 2.0 - vec2(1.0);
_ = vec2(2) * 1;
_ = 2 * vec2(1);
_ = vec2(2u) * 1u;
_ = 2u * vec2(1u);
_ = vec2(2.0) * 1.0;
_ = 2.0 * vec2(1.0);
_ = vec2(2) / 1;
_ = 2 / vec2(1);
_ = vec2(2u) / 1u;
_ = 2u / vec2(1u);
_ = vec2(2.0) / 1.0;
_ = 2.0 / vec2(1.0);
_ = vec2(2) % 1;
_ = 2 % vec2(1);
_ = vec2(2u) % 1u;
_ = 2u % vec2(1u);
_ = vec2(2.0) % 1.0;
_ = 2.0 % vec2(1.0);
2022-04-12 13:50:10 +00:00
// Matrix arithmetic
2022-04-26 16:40:37 +00:00
_ = mat3x3<f32>() + mat3x3<f32>();
_ = mat3x3<f32>() - mat3x3<f32>();
2022-04-12 13:50:10 +00:00
2022-04-26 16:40:37 +00:00
_ = mat3x3<f32>() * 1.0;
_ = 2.0 * mat3x3<f32>();
2022-04-12 13:50:10 +00:00
2022-04-26 16:40:37 +00:00
_ = mat4x3<f32>() * vec4(1.0);
_ = vec3(2.0) * mat4x3<f32>();
2022-04-26 16:40:37 +00:00
_ = mat4x3<f32>() * mat3x4<f32>();
}
2022-04-12 13:50:10 +00:00
fn bit() {
// unary
2022-04-26 16:40:37 +00:00
_ = ~1;
_ = ~1u;
_ = ~vec2(1);
_ = ~vec3(1u);
2022-04-12 13:50:10 +00:00
// binary
2022-04-26 16:40:37 +00:00
_ = 2 | 1;
_ = 2u | 1u;
_ = vec2(2) | vec2(1);
_ = vec3(2u) | vec3(1u);
_ = 2 & 1;
_ = 2u & 1u;
_ = vec2(2) & vec2(1);
_ = vec3(2u) & vec3(1u);
_ = 2 ^ 1;
_ = 2u ^ 1u;
_ = vec2(2) ^ vec2(1);
_ = vec3(2u) ^ vec3(1u);
_ = 2 << 1u;
_ = 2u << 1u;
_ = vec2(2) << vec2(1u);
_ = vec3(2u) << vec3(1u);
_ = 2 >> 1u;
_ = 2u >> 1u;
_ = vec2(2) >> vec2(1u);
_ = vec3(2u) >> vec3(1u);
2022-04-12 13:50:10 +00:00
}
fn comparison() {
2022-04-26 16:40:37 +00:00
_ = 2 == 1;
_ = 2u == 1u;
_ = 2.0 == 1.0;
_ = vec2(2) == vec2(1);
_ = vec3(2u) == vec3(1u);
_ = vec4(2.0) == vec4(1.0);
_ = 2 != 1;
_ = 2u != 1u;
_ = 2.0 != 1.0;
_ = vec2(2) != vec2(1);
_ = vec3(2u) != vec3(1u);
_ = vec4(2.0) != vec4(1.0);
_ = 2 < 1;
_ = 2u < 1u;
_ = 2.0 < 1.0;
_ = vec2(2) < vec2(1);
_ = vec3(2u) < vec3(1u);
_ = vec4(2.0) < vec4(1.0);
_ = 2 <= 1;
_ = 2u <= 1u;
_ = 2.0 <= 1.0;
_ = vec2(2) <= vec2(1);
_ = vec3(2u) <= vec3(1u);
_ = vec4(2.0) <= vec4(1.0);
_ = 2 > 1;
_ = 2u > 1u;
_ = 2.0 > 1.0;
_ = vec2(2) > vec2(1);
_ = vec3(2u) > vec3(1u);
_ = vec4(2.0) > vec4(1.0);
_ = 2 >= 1;
_ = 2u >= 1u;
_ = 2.0 >= 1.0;
_ = vec2(2) >= vec2(1);
_ = vec3(2u) >= vec3(1u);
_ = vec4(2.0) >= vec4(1.0);
2021-12-15 22:30:33 +00:00
}
2022-04-12 13:50:10 +00:00
fn assignment() {
2022-01-11 15:56:41 +00:00
var a = 1;
2022-04-12 13:50:10 +00:00
2022-01-11 15:56:41 +00:00
a += 1;
a -= 1;
a *= a;
a /= a;
a %= 1;
a &= 0;
2022-04-12 13:50:10 +00:00
a |= 0;
a ^= 0;
a <<= 2u;
a >>= 1u;
a++;
a--;
var vec0: vec3<i32> = vec3<i32>();
vec0[1]++;
vec0[1]--;
2022-01-11 15:56:41 +00:00
}
@compute @workgroup_size(1)
2021-06-28 18:25:30 +00:00
fn main() {
2022-04-26 16:40:37 +00:00
_ = builtins();
_ = splat();
_ = bool_cast(v_f32_one.xyz);
_ = constructors();
2022-04-12 13:50:10 +00:00
2022-01-11 15:56:41 +00:00
logical();
2022-04-12 13:50:10 +00:00
arithmetic();
bit();
comparison();
assignment();
2021-06-28 18:25:30 +00:00
}