Add WebGPU Shading Language tokenizer, with tests

Fixes: #3883
This commit is contained in:
David Neto 2023-03-30 12:11:20 -04:00
parent 689a4b89b2
commit 3724eefaf5
5 changed files with 4475 additions and 0 deletions

View file

@ -0,0 +1,48 @@
// A fragment shader which lights textured geometry with point lights.
// Taken from the introduction of the WebGPU Shading Lnaguage Specification
// https://w3.org/TR/WGSL
// Lights from a storage buffer binding.
struct PointLight {
position : vec3f,
color : vec3f,
}
struct LightStorage {
pointCount : u32,
point : array<PointLight>,
}
@group(0) @binding(0) var<storage> lights : LightStorage;
// Texture and sampler.
@group(1) @binding(0) var baseColorSampler : sampler;
@group(1) @binding(1) var baseColorTexture : texture_2d<f32>;
// Function arguments are values from from vertex shader.
@fragment
fn fragmentMain(@location(0) worldPos : vec3f,
@location(1) normal : vec3f,
@location(2) uv : vec2f) -> @location(0) vec4f {
// Sample the base color of the surface from a texture.
let baseColor = textureSample(baseColorTexture, baseColorSampler, uv);
let N = normalize(normal);
var surfaceColor = vec3f(0);
// Loop over the scene point lights.
for (var i = 0u; i < lights.pointCount; i++) {
let worldToLight = lights.point[i].position - worldPos;
let dist = length(worldToLight);
let dir = normalize(worldToLight);
// Determine the contribution of this light to the surface color.
let radiance = lights.point[i].color * (1 / pow(dist, 2));
let nDotL = max(dot(N, dir), 0);
// Accumulate light contribution to the surface color.
surfaceColor += baseColor.rgb * radiance * nDotL;
}
// Return the accumulated surface color.
return vec4(surfaceColor, baseColor.a);
}