wgpu_examples/ray_cube_normals/
mod.rs

1use std::{borrow::Cow, iter, mem};
2
3use bytemuck::{Pod, Zeroable};
4use glam::{Affine3A, Mat4, Quat, Vec3};
5use wgpu::util::DeviceExt;
6
7use crate::utils;
8use wgpu::StoreOp;
9
10// from cube
11#[repr(C)]
12#[derive(Clone, Copy, Pod, Zeroable)]
13struct Vertex {
14    _pos: [f32; 4],
15    _tex_coord: [f32; 2],
16}
17
18fn vertex(pos: [i8; 3], tc: [i8; 2]) -> Vertex {
19    Vertex {
20        _pos: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
21        _tex_coord: [tc[0] as f32, tc[1] as f32],
22    }
23}
24
25fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
26    let vertex_data = [
27        // top (0, 0, 1)
28        vertex([-1, -1, 1], [0, 0]),
29        vertex([1, -1, 1], [1, 0]),
30        vertex([1, 1, 1], [1, 1]),
31        vertex([-1, 1, 1], [0, 1]),
32        // bottom (0, 0, -1)
33        vertex([-1, 1, -1], [1, 0]),
34        vertex([1, 1, -1], [0, 0]),
35        vertex([1, -1, -1], [0, 1]),
36        vertex([-1, -1, -1], [1, 1]),
37        // right (1, 0, 0)
38        vertex([1, -1, -1], [0, 0]),
39        vertex([1, 1, -1], [1, 0]),
40        vertex([1, 1, 1], [1, 1]),
41        vertex([1, -1, 1], [0, 1]),
42        // left (-1, 0, 0)
43        vertex([-1, -1, 1], [1, 0]),
44        vertex([-1, 1, 1], [0, 0]),
45        vertex([-1, 1, -1], [0, 1]),
46        vertex([-1, -1, -1], [1, 1]),
47        // front (0, 1, 0)
48        vertex([1, 1, -1], [1, 0]),
49        vertex([-1, 1, -1], [0, 0]),
50        vertex([-1, 1, 1], [0, 1]),
51        vertex([1, 1, 1], [1, 1]),
52        // back (0, -1, 0)
53        vertex([1, -1, 1], [0, 0]),
54        vertex([-1, -1, 1], [1, 0]),
55        vertex([-1, -1, -1], [1, 1]),
56        vertex([1, -1, -1], [0, 1]),
57    ];
58
59    let index_data: &[u16] = &[
60        0, 1, 2, 2, 3, 0, // top
61        4, 5, 6, 6, 7, 4, // bottom
62        8, 9, 10, 10, 11, 8, // right
63        12, 13, 14, 14, 15, 12, // left
64        16, 17, 18, 18, 19, 16, // front
65        20, 21, 22, 22, 23, 20, // back
66    ];
67
68    (vertex_data.to_vec(), index_data.to_vec())
69}
70
71#[repr(C)]
72#[derive(Clone, Copy, Pod, Zeroable)]
73struct Uniforms {
74    view_inverse: Mat4,
75    proj_inverse: Mat4,
76}
77
78#[inline]
79fn affine_to_rows(mat: &Affine3A) -> [f32; 12] {
80    let row_0 = mat.matrix3.row(0);
81    let row_1 = mat.matrix3.row(1);
82    let row_2 = mat.matrix3.row(2);
83    let translation = mat.translation;
84    [
85        row_0.x,
86        row_0.y,
87        row_0.z,
88        translation.x,
89        row_1.x,
90        row_1.y,
91        row_1.z,
92        translation.y,
93        row_2.x,
94        row_2.y,
95        row_2.z,
96        translation.z,
97    ]
98}
99
100struct Example {
101    rt_target: wgpu::Texture,
102    tlas: wgpu::Tlas,
103    compute_pipeline: wgpu::ComputePipeline,
104    compute_bind_group: wgpu::BindGroup,
105    blit_pipeline: wgpu::RenderPipeline,
106    blit_bind_group: wgpu::BindGroup,
107    animation_timer: utils::AnimationTimer,
108}
109
110impl crate::framework::Example for Example {
111    // Don't want srgb, so normals show up better.
112    const SRGB: bool = false;
113    fn required_features() -> wgpu::Features {
114        wgpu::Features::EXPERIMENTAL_RAY_QUERY | wgpu::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN
115    }
116
117    fn required_downlevel_capabilities() -> wgpu::DownlevelCapabilities {
118        wgpu::DownlevelCapabilities {
119            flags: wgpu::DownlevelFlags::COMPUTE_SHADERS,
120            ..Default::default()
121        }
122    }
123
124    fn required_limits() -> wgpu::Limits {
125        wgpu::Limits::default().using_minimum_supported_acceleration_structure_values()
126    }
127
128    fn init(
129        config: &wgpu::SurfaceConfiguration,
130        _adapter: &wgpu::Adapter,
131        device: &wgpu::Device,
132        queue: &wgpu::Queue,
133    ) -> Self {
134        let side_count = 8;
135
136        let rt_target = device.create_texture(&wgpu::TextureDescriptor {
137            label: Some("rt_target"),
138            size: wgpu::Extent3d {
139                width: config.width,
140                height: config.height,
141                depth_or_array_layers: 1,
142            },
143            mip_level_count: 1,
144            sample_count: 1,
145            dimension: wgpu::TextureDimension::D2,
146            format: wgpu::TextureFormat::Rgba8Unorm,
147            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::STORAGE_BINDING,
148            view_formats: &[wgpu::TextureFormat::Rgba8Unorm],
149        });
150
151        let rt_view = rt_target.create_view(&wgpu::TextureViewDescriptor {
152            label: None,
153            format: Some(wgpu::TextureFormat::Rgba8Unorm),
154            dimension: Some(wgpu::TextureViewDimension::D2),
155            usage: None,
156            aspect: wgpu::TextureAspect::All,
157            base_mip_level: 0,
158            mip_level_count: None,
159            base_array_layer: 0,
160            array_layer_count: None,
161        });
162
163        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
164            label: Some("rt_sampler"),
165            address_mode_u: wgpu::AddressMode::ClampToEdge,
166            address_mode_v: wgpu::AddressMode::ClampToEdge,
167            address_mode_w: wgpu::AddressMode::ClampToEdge,
168            mag_filter: wgpu::FilterMode::Linear,
169            min_filter: wgpu::FilterMode::Linear,
170            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
171            ..Default::default()
172        });
173
174        let uniforms = {
175            let view = Mat4::look_at_rh(Vec3::new(0.0, 0.0, 2.5), Vec3::ZERO, Vec3::Y);
176            let proj = Mat4::perspective_rh(
177                59.0_f32.to_radians(),
178                config.width as f32 / config.height as f32,
179                0.001,
180                1000.0,
181            );
182
183            Uniforms {
184                view_inverse: view.inverse(),
185                proj_inverse: proj.inverse(),
186            }
187        };
188
189        let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
190            label: Some("Uniform Buffer"),
191            contents: bytemuck::cast_slice(&[uniforms]),
192            usage: wgpu::BufferUsages::UNIFORM,
193        });
194
195        let (vertex_data, index_data) = create_vertices();
196
197        let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
198            label: Some("Vertex Buffer"),
199            contents: bytemuck::cast_slice(&vertex_data),
200            usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::BLAS_INPUT,
201        });
202
203        let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
204            label: Some("Index Buffer"),
205            contents: bytemuck::cast_slice(&index_data),
206            usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::BLAS_INPUT,
207        });
208
209        let blas_geo_size_desc = wgpu::BlasTriangleGeometrySizeDescriptor {
210            vertex_format: wgpu::VertexFormat::Float32x3,
211            vertex_count: vertex_data.len() as u32,
212            index_format: Some(wgpu::IndexFormat::Uint16),
213            index_count: Some(index_data.len() as u32),
214            flags: wgpu::AccelerationStructureGeometryFlags::OPAQUE,
215        };
216
217        let blas = device.create_blas(
218            &wgpu::CreateBlasDescriptor {
219                label: None,
220                flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE
221                    | wgpu::AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN,
222                update_mode: wgpu::AccelerationStructureUpdateMode::Build,
223            },
224            wgpu::BlasGeometrySizeDescriptors::Triangles {
225                descriptors: vec![blas_geo_size_desc.clone()],
226            },
227        );
228
229        let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
230            label: None,
231            flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE
232                | wgpu::AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN,
233            update_mode: wgpu::AccelerationStructureUpdateMode::Build,
234            max_instances: side_count * side_count,
235        });
236
237        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
238            label: Some("rt_computer"),
239            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
240        });
241
242        let blit_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
243            label: Some("blit"),
244            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("blit.wgsl"))),
245        });
246
247        let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
248            label: Some("rt"),
249            layout: None,
250            module: &shader,
251            entry_point: None,
252            compilation_options: Default::default(),
253            cache: None,
254        });
255
256        let compute_bind_group_layout = compute_pipeline.get_bind_group_layout(0);
257
258        let compute_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
259            label: None,
260            layout: &compute_bind_group_layout,
261            entries: &[
262                wgpu::BindGroupEntry {
263                    binding: 0,
264                    resource: wgpu::BindingResource::TextureView(&rt_view),
265                },
266                wgpu::BindGroupEntry {
267                    binding: 1,
268                    resource: uniform_buf.as_entire_binding(),
269                },
270                wgpu::BindGroupEntry {
271                    binding: 2,
272                    resource: wgpu::BindingResource::AccelerationStructure(&tlas),
273                },
274            ],
275        });
276
277        let blit_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
278            label: Some("blit"),
279            layout: None,
280            vertex: wgpu::VertexState {
281                module: &blit_shader,
282                entry_point: Some("vs_main"),
283                compilation_options: Default::default(),
284                buffers: &[],
285            },
286            fragment: Some(wgpu::FragmentState {
287                module: &blit_shader,
288                entry_point: Some("fs_main"),
289                compilation_options: Default::default(),
290                targets: &[Some(config.format.into())],
291            }),
292            primitive: wgpu::PrimitiveState {
293                topology: wgpu::PrimitiveTopology::TriangleList,
294                ..Default::default()
295            },
296            depth_stencil: None,
297            multisample: wgpu::MultisampleState::default(),
298            multiview_mask: None,
299            cache: None,
300        });
301
302        let blit_bind_group_layout = blit_pipeline.get_bind_group_layout(0);
303
304        let blit_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
305            label: None,
306            layout: &blit_bind_group_layout,
307            entries: &[
308                wgpu::BindGroupEntry {
309                    binding: 0,
310                    resource: wgpu::BindingResource::TextureView(&rt_view),
311                },
312                wgpu::BindGroupEntry {
313                    binding: 1,
314                    resource: wgpu::BindingResource::Sampler(&sampler),
315                },
316            ],
317        });
318
319        let dist = 3.0;
320
321        for x in 0..side_count {
322            for y in 0..side_count {
323                tlas[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
324                    &blas,
325                    affine_to_rows(&Affine3A::from_rotation_translation(
326                        Quat::from_rotation_y(45.9_f32.to_radians()),
327                        Vec3 {
328                            x: x as f32 * dist,
329                            y: y as f32 * dist,
330                            z: -30.0,
331                        },
332                    )),
333                    0,
334                    0xff,
335                ));
336            }
337        }
338
339        let mut encoder =
340            device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
341
342        encoder.build_acceleration_structures(
343            iter::once(&wgpu::BlasBuildEntry {
344                blas: &blas,
345                geometry: wgpu::BlasGeometries::TriangleGeometries(vec![
346                    wgpu::BlasTriangleGeometry {
347                        size: &blas_geo_size_desc,
348                        vertex_buffer: &vertex_buf,
349                        first_vertex: 0,
350                        vertex_stride: mem::size_of::<Vertex>() as u64,
351                        index_buffer: Some(&index_buf),
352                        first_index: Some(0),
353                        transform_buffer: None,
354                        transform_buffer_offset: None,
355                    },
356                ]),
357            }),
358            iter::once(&tlas),
359        );
360
361        queue.submit(Some(encoder.finish()));
362
363        Example {
364            rt_target,
365            tlas,
366            compute_pipeline,
367            compute_bind_group,
368            blit_pipeline,
369            blit_bind_group,
370            animation_timer: utils::AnimationTimer::default(),
371        }
372    }
373
374    fn update(&mut self, _event: winit::event::WindowEvent) {
375        //empty
376    }
377
378    fn resize(
379        &mut self,
380        _config: &wgpu::SurfaceConfiguration,
381        _device: &wgpu::Device,
382        _queue: &wgpu::Queue,
383    ) {
384    }
385
386    fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
387        let anim_time = self.animation_timer.time();
388
389        self.tlas[0].as_mut().unwrap().transform =
390            affine_to_rows(&Affine3A::from_rotation_translation(
391                Quat::from_euler(
392                    glam::EulerRot::XYZ,
393                    anim_time * 0.342,
394                    anim_time * 0.254,
395                    anim_time * 0.832,
396                ),
397                Vec3 {
398                    x: 0.0,
399                    y: 0.0,
400                    z: -6.0,
401                },
402            ));
403
404        let mut encoder =
405            device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
406
407        encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));
408
409        {
410            let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
411                label: None,
412                timestamp_writes: None,
413            });
414            cpass.set_pipeline(&self.compute_pipeline);
415            cpass.set_bind_group(0, Some(&self.compute_bind_group), &[]);
416            cpass.dispatch_workgroups(self.rt_target.width() / 8, self.rt_target.height() / 8, 1);
417        }
418
419        {
420            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
421                label: None,
422                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
423                    view,
424                    depth_slice: None,
425                    resolve_target: None,
426                    ops: wgpu::Operations {
427                        load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
428                        store: StoreOp::Store,
429                    },
430                })],
431                depth_stencil_attachment: None,
432                timestamp_writes: None,
433                occlusion_query_set: None,
434                multiview_mask: None,
435            });
436
437            rpass.set_pipeline(&self.blit_pipeline);
438            rpass.set_bind_group(0, Some(&self.blit_bind_group), &[]);
439            rpass.draw(0..3, 0..1);
440        }
441
442        queue.submit(Some(encoder.finish()));
443    }
444}
445
446pub fn main() {
447    crate::framework::run::<Example>("ray-cube");
448}
449
450#[cfg(test)]
451#[wgpu_test::gpu_test]
452pub static TEST: crate::framework::ExampleTestParams = crate::framework::ExampleTestParams {
453    name: "ray_cube_normals",
454    image_path: "/examples/features/src/ray_cube_normals/screenshot.png",
455    width: 1024,
456    height: 768,
457    optional_features: wgpu::Features::default(),
458    base_test_parameters: wgpu_test::TestParameters::default().expect_fail(
459        // RADV does this fine.
460        wgpu_test::FailureCase {
461            backends: Some(wgpu::Backends::VULKAN),
462            adapter: Some("AMD"),
463            driver: Some("AMD proprietary driver"),
464            ..wgpu_test::FailureCase::default()
465        }
466        .panic("Image data mismatch"),
467    ),
468    comparisons: &[wgpu_test::ComparisonType::Mean(0.02)],
469    _phantom: std::marker::PhantomData::<Example>,
470};