Debian Codegraph @

third_party/rust/wgpu-hal/examples/ray-traced-triangle/shader.wgsl

raw
1 struct Uniforms {
2 view_inv: mat4x4<f32>,
3 proj_inv: mat4x4<f32>,
4 };
5 @group(0) @binding(0)
6 var<uniform> uniforms: Uniforms;
7
8 @group(0) @binding(1)
9 var output: texture_storage_2d<rgba8unorm, write>;
10
11 @group(0) @binding(2)
12 var acc_struct: acceleration_structure;
13
14 @compute @workgroup_size(8, 8)
15 fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
16 let target_size = textureDimensions(output);
17
18 let pixel_center = vec2<f32>(global_id.xy) + vec2<f32>(0.5);
19 let in_uv = pixel_center / vec2<f32>(target_size.xy);
20 let d = in_uv * 2.0 - 1.0;
21
22 let origin = (uniforms.view_inv * vec4<f32>(0.0, 0.0, 0.0, 1.0)).xyz;
23 let temp = uniforms.proj_inv * vec4<f32>(d.x, d.y, 1.0, 1.0);
24 let direction = (uniforms.view_inv * vec4<f32>(normalize(temp.xyz), 0.0)).xyz;
25
26 var rq: ray_query;
27 rayQueryInitialize(&rq, acc_struct, RayDesc(0u, 0xFFu, 0.1, 200.0, origin, direction));
28 rayQueryProceed(&rq);
29
30 var color = vec4<f32>(0.0, 0.0, 0.0, 1.0);
31 let intersection = rayQueryGetCommittedIntersection(&rq);
32 if intersection.kind != RAY_QUERY_INTERSECTION_NONE {
33 color = vec4<f32>(intersection.barycentrics, 1.0 - intersection.barycentrics.x - intersection.barycentrics.y, 1.0);
34 }
35
36 textureStore(output, global_id.xy, color);
37 }