|
| 1 | +struct VertexOutput { |
| 2 | + @builtin(position) position: vec4<f32>, |
| 3 | + @location(0) tex_coords: vec2<f32>, |
| 4 | +}; |
| 5 | + |
| 6 | +@vertex |
| 7 | +fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput { |
| 8 | + var result: VertexOutput; |
| 9 | + let x = i32(vertex_index) / 2; |
| 10 | + let y = i32(vertex_index) & 1; |
| 11 | + let tc = vec2<f32>( |
| 12 | + f32(x) * 2.0, |
| 13 | + f32(y) * 2.0 |
| 14 | + ); |
| 15 | + result.position = vec4<f32>( |
| 16 | + tc.x * 2.0 - 1.0, |
| 17 | + 1.0 - tc.y * 2.0, |
| 18 | + 0.0, 1.0 |
| 19 | + ); |
| 20 | + result.tex_coords = tc; |
| 21 | + return result; |
| 22 | +} |
| 23 | + |
| 24 | +/* |
| 25 | +The contents of the RayQuery struct are roughly as follows |
| 26 | +let RAY_FLAG_NONE = 0x00u; |
| 27 | +let RAY_FLAG_OPAQUE = 0x01u; |
| 28 | +let RAY_FLAG_NO_OPAQUE = 0x02u; |
| 29 | +let RAY_FLAG_TERMINATE_ON_FIRST_HIT = 0x04u; |
| 30 | +let RAY_FLAG_SKIP_CLOSEST_HIT_SHADER = 0x08u; |
| 31 | +let RAY_FLAG_CULL_BACK_FACING = 0x10u; |
| 32 | +let RAY_FLAG_CULL_FRONT_FACING = 0x20u; |
| 33 | +let RAY_FLAG_CULL_OPAQUE = 0x40u; |
| 34 | +let RAY_FLAG_CULL_NO_OPAQUE = 0x80u; |
| 35 | +let RAY_FLAG_SKIP_TRIANGLES = 0x100u; |
| 36 | +let RAY_FLAG_SKIP_AABBS = 0x200u; |
| 37 | +
|
| 38 | +let RAY_QUERY_INTERSECTION_NONE = 0u; |
| 39 | +let RAY_QUERY_INTERSECTION_TRIANGLE = 1u; |
| 40 | +let RAY_QUERY_INTERSECTION_GENERATED = 2u; |
| 41 | +let RAY_QUERY_INTERSECTION_AABB = 3u; |
| 42 | +
|
| 43 | +struct RayDesc { |
| 44 | + flags: u32, |
| 45 | + cull_mask: u32, |
| 46 | + t_min: f32, |
| 47 | + t_max: f32, |
| 48 | + origin: vec3<f32>, |
| 49 | + dir: vec3<f32>, |
| 50 | +} |
| 51 | +
|
| 52 | +struct RayIntersection { |
| 53 | + kind: u32, |
| 54 | + t: f32, |
| 55 | + instance_custom_index: u32, |
| 56 | + instance_id: u32, |
| 57 | + sbt_record_offset: u32, |
| 58 | + geometry_index: u32, |
| 59 | + primitive_index: u32, |
| 60 | + barycentrics: vec2<f32>, |
| 61 | + front_face: bool, |
| 62 | + object_to_world: mat4x3<f32>, |
| 63 | + world_to_object: mat4x3<f32>, |
| 64 | +} |
| 65 | +*/ |
| 66 | + |
| 67 | +struct Uniforms { |
| 68 | + view_inv: mat4x4<f32>, |
| 69 | + proj_inv: mat4x4<f32>, |
| 70 | +}; |
| 71 | + |
| 72 | +struct Vertex { |
| 73 | + pos: vec3<f32>, |
| 74 | + normal: vec3<f32>, |
| 75 | + uv: vec2<f32>, |
| 76 | +}; |
| 77 | + |
| 78 | + |
| 79 | +struct Instance { |
| 80 | + first_vertex: u32, |
| 81 | + first_geometry: u32, |
| 82 | + last_geometry: u32, |
| 83 | + _pad: u32 |
| 84 | +}; |
| 85 | + |
| 86 | +struct Material{ |
| 87 | + roughness_exponent: f32, |
| 88 | + metalness: f32, |
| 89 | + specularity: f32, |
| 90 | + albedo: vec3<f32> |
| 91 | +} |
| 92 | + |
| 93 | +struct Geometry { |
| 94 | + first_index: u32, |
| 95 | + material: Material, |
| 96 | +}; |
| 97 | + |
| 98 | + |
| 99 | +@group(0) @binding(0) |
| 100 | +var<uniform> uniforms: Uniforms; |
| 101 | + |
| 102 | +@group(0) @binding(1) |
| 103 | +var<storage, read> vertices: array<Vertex>; |
| 104 | + |
| 105 | +@group(0) @binding(2) |
| 106 | +var<storage, read> indices: array<u32>; |
| 107 | + |
| 108 | +@group(0) @binding(3) |
| 109 | +var<storage, read> geometries: array<Geometry>; |
| 110 | + |
| 111 | +@group(0) @binding(4) |
| 112 | +var<storage, read> instances: array<Instance>; |
| 113 | + |
| 114 | +@group(0) @binding(5) |
| 115 | +var acc_struct: acceleration_structure; |
| 116 | + |
| 117 | +@fragment |
| 118 | +fn fs_main(vertex: VertexOutput) -> @location(0) vec4<f32> { |
| 119 | + |
| 120 | + var color = vec4<f32>(vertex.tex_coords, 0.0, 1.0); |
| 121 | + |
| 122 | + let d = vertex.tex_coords * 2.0 - 1.0; |
| 123 | + |
| 124 | + let origin = (uniforms.view_inv * vec4<f32>(0.0,0.0,0.0,1.0)).xyz; |
| 125 | + let temp = uniforms.proj_inv * vec4<f32>(d.x, d.y, 1.0, 1.0); |
| 126 | + let direction = (uniforms.view_inv * vec4<f32>(normalize(temp.xyz), 0.0)).xyz; |
| 127 | + |
| 128 | + var rq: ray_query; |
| 129 | + rayQueryInitialize(&rq, acc_struct, RayDesc(0u, 0xFFu, 0.1, 200.0, origin, direction)); |
| 130 | + rayQueryProceed(&rq); |
| 131 | + |
| 132 | + let intersection = rayQueryGetCommittedIntersection(&rq); |
| 133 | + if (intersection.kind != RAY_QUERY_INTERSECTION_NONE) { |
| 134 | + let instance = instances[intersection.instance_custom_index]; |
| 135 | + let geometry = geometries[intersection.geometry_index + instance.first_geometry]; |
| 136 | + |
| 137 | + let index_offset = geometry.first_index; |
| 138 | + let vertex_offset = instance.first_vertex; |
| 139 | + |
| 140 | + let first_index_index = intersection.primitive_index * 3u + index_offset; |
| 141 | + |
| 142 | + let v_0 = vertices[vertex_offset+indices[first_index_index+0u]]; |
| 143 | + let v_1 = vertices[vertex_offset+indices[first_index_index+1u]]; |
| 144 | + let v_2 = vertices[vertex_offset+indices[first_index_index+2u]]; |
| 145 | + |
| 146 | + let bary = vec3<f32>(1.0 - intersection.barycentrics.x - intersection.barycentrics.y, intersection.barycentrics); |
| 147 | + |
| 148 | + let pos = v_0.pos * bary.x + v_1.pos * bary.y + v_2.pos * bary.z; |
| 149 | + let normal_raw = v_0.normal * bary.x + v_1.normal * bary.y + v_2.normal * bary.z; |
| 150 | + let uv = v_0.uv * bary.x + v_1.uv * bary.y + v_2.uv * bary.z; |
| 151 | + |
| 152 | + let normal = normalize(normal_raw); |
| 153 | + |
| 154 | + let material = geometry.material; |
| 155 | + |
| 156 | + color = vec4<f32>(material.albedo, 1.0); |
| 157 | + |
| 158 | + if(intersection.instance_custom_index == 1u){ |
| 159 | + color = vec4<f32>(normal, 1.0); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return color; |
| 164 | +} |
0 commit comments