|
| 1 | +use super::*; |
| 2 | +use crate::renderer::visibility_buffer::{ |
| 3 | + VisibilityDraw, VisibilityRecord, INVALID_DRAW_ID, VISIBILITY_FORMAT, |
| 4 | +}; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn raw_virtual_clusters_rasterize_namespaced_visibility_ids_on_the_real_gpu() { |
| 8 | + const WIDTH: u32 = 16; |
| 9 | + const HEIGHT: u32 = 16; |
| 10 | + const ROW_BYTES: u32 = 256; |
| 11 | + |
| 12 | + let Some((device, queue)) = try_traversal_device() else { |
| 13 | + eprintln!("no GPU adapter — skipping virtual visibility raster oracle"); |
| 14 | + return; |
| 15 | + }; |
| 16 | + let required = wgpu::Features::PRIMITIVE_INDEX | wgpu::Features::INDIRECT_FIRST_INSTANCE; |
| 17 | + if !device.features().contains(required) { |
| 18 | + eprintln!("adapter lacks primitive-index/indirect-first-instance — skipping oracle"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + let mut pool = GpuVirtualGeometryPool::new(&device, gpu_config(5)).unwrap(); |
| 23 | + let mesh = pool |
| 24 | + .register_mesh(&queue, hierarchy_asset(hierarchy_archive())) |
| 25 | + .unwrap(); |
| 26 | + make_hierarchy_fully_resident(&mut pool, &queue, mesh); |
| 27 | + let selector = GpuVirtualHierarchySelector::new(&device, &pool, traversal_config()).unwrap(); |
| 28 | + let emitter = GpuVirtualDrawEmitter::new(&device, &selector).unwrap(); |
| 29 | + let raster = GpuVirtualVisibilityRaster::new(&device, &pool, &selector, &emitter).unwrap(); |
| 30 | + let identity = [ |
| 31 | + [1.0, 0.0, 0.0, 0.0], |
| 32 | + [0.0, 1.0, 0.0, 0.0], |
| 33 | + [0.0, 0.0, 1.0, 0.0], |
| 34 | + [0.0, 0.0, 0.0, 1.0], |
| 35 | + ]; |
| 36 | + raster |
| 37 | + .prepare_frame( |
| 38 | + &queue, |
| 39 | + GpuVirtualVisibilityFrame::new(identity, identity).unwrap(), |
| 40 | + ) |
| 41 | + .unwrap(); |
| 42 | + |
| 43 | + let visibility = device.create_texture(&wgpu::TextureDescriptor { |
| 44 | + label: Some("virtual_visibility_oracle_ids"), |
| 45 | + size: wgpu::Extent3d { |
| 46 | + width: WIDTH, |
| 47 | + height: HEIGHT, |
| 48 | + depth_or_array_layers: 1, |
| 49 | + }, |
| 50 | + mip_level_count: 1, |
| 51 | + sample_count: 1, |
| 52 | + dimension: wgpu::TextureDimension::D2, |
| 53 | + format: VISIBILITY_FORMAT, |
| 54 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, |
| 55 | + view_formats: &[], |
| 56 | + }); |
| 57 | + let depth = device.create_texture(&wgpu::TextureDescriptor { |
| 58 | + label: Some("virtual_visibility_oracle_depth"), |
| 59 | + size: wgpu::Extent3d { |
| 60 | + width: WIDTH, |
| 61 | + height: HEIGHT, |
| 62 | + depth_or_array_layers: 1, |
| 63 | + }, |
| 64 | + mip_level_count: 1, |
| 65 | + sample_count: 1, |
| 66 | + dimension: wgpu::TextureDimension::D2, |
| 67 | + format: crate::renderer::DEPTH_FORMAT, |
| 68 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 69 | + view_formats: &[], |
| 70 | + }); |
| 71 | + let readback_source = device.create_buffer(&wgpu::BufferDescriptor { |
| 72 | + label: Some("virtual_visibility_oracle_copy"), |
| 73 | + size: u64::from(ROW_BYTES * HEIGHT), |
| 74 | + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, |
| 75 | + mapped_at_creation: false, |
| 76 | + }); |
| 77 | + let visibility_view = visibility.create_view(&Default::default()); |
| 78 | + let depth_view = depth.create_view(&Default::default()); |
| 79 | + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { |
| 80 | + label: Some("virtual_visibility_oracle_encoder"), |
| 81 | + }); |
| 82 | + selector |
| 83 | + .record( |
| 84 | + &queue, |
| 85 | + &mut encoder, |
| 86 | + &pool, |
| 87 | + &[GpuVirtualInstance::identity(mesh, 901)], |
| 88 | + traversal_view(50.0), |
| 89 | + ) |
| 90 | + .unwrap(); |
| 91 | + emitter.record(&queue, &mut encoder, &selector).unwrap(); |
| 92 | + { |
| 93 | + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { |
| 94 | + label: Some("virtual_visibility_oracle_pass"), |
| 95 | + color_attachments: &[Some(wgpu::RenderPassColorAttachment { |
| 96 | + view: &visibility_view, |
| 97 | + resolve_target: None, |
| 98 | + depth_slice: None, |
| 99 | + ops: wgpu::Operations { |
| 100 | + load: wgpu::LoadOp::Clear(wgpu::Color { |
| 101 | + r: f64::from(u32::MAX), |
| 102 | + g: f64::from(u32::MAX), |
| 103 | + b: 0.0, |
| 104 | + a: 0.0, |
| 105 | + }), |
| 106 | + store: wgpu::StoreOp::Store, |
| 107 | + }, |
| 108 | + })], |
| 109 | + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { |
| 110 | + view: &depth_view, |
| 111 | + depth_ops: Some(wgpu::Operations { |
| 112 | + load: wgpu::LoadOp::Clear(1.0), |
| 113 | + store: wgpu::StoreOp::Store, |
| 114 | + }), |
| 115 | + stencil_ops: None, |
| 116 | + }), |
| 117 | + timestamp_writes: None, |
| 118 | + occlusion_query_set: None, |
| 119 | + multiview_mask: None, |
| 120 | + }); |
| 121 | + raster.draw_fixed_for_test(&mut pass, &emitter, 4).unwrap(); |
| 122 | + } |
| 123 | + encoder.copy_texture_to_buffer( |
| 124 | + wgpu::TexelCopyTextureInfo { |
| 125 | + texture: &visibility, |
| 126 | + mip_level: 0, |
| 127 | + origin: wgpu::Origin3d::ZERO, |
| 128 | + aspect: wgpu::TextureAspect::All, |
| 129 | + }, |
| 130 | + wgpu::TexelCopyBufferInfo { |
| 131 | + buffer: &readback_source, |
| 132 | + layout: wgpu::TexelCopyBufferLayout { |
| 133 | + offset: 0, |
| 134 | + bytes_per_row: Some(ROW_BYTES), |
| 135 | + rows_per_image: Some(HEIGHT), |
| 136 | + }, |
| 137 | + }, |
| 138 | + wgpu::Extent3d { |
| 139 | + width: WIDTH, |
| 140 | + height: HEIGHT, |
| 141 | + depth_or_array_layers: 1, |
| 142 | + }, |
| 143 | + ); |
| 144 | + queue.submit(std::iter::once(encoder.finish())); |
| 145 | + |
| 146 | + let bytes = read_gpu_buffer( |
| 147 | + &device, |
| 148 | + &queue, |
| 149 | + &readback_source, |
| 150 | + u64::from(ROW_BYTES * HEIGHT), |
| 151 | + ); |
| 152 | + let mut covered = 0usize; |
| 153 | + let mut background = 0usize; |
| 154 | + for y in 0..HEIGHT { |
| 155 | + for x in 0..WIDTH { |
| 156 | + let offset = (y * ROW_BYTES + x * 8) as usize; |
| 157 | + let words: &[u32] = bytemuck::cast_slice(&bytes[offset..offset + 8]); |
| 158 | + let record = VisibilityRecord { |
| 159 | + draw_id: words[0], |
| 160 | + primitive_and_face: words[1], |
| 161 | + }; |
| 162 | + if record.draw_id == INVALID_DRAW_ID { |
| 163 | + background += 1; |
| 164 | + continue; |
| 165 | + } |
| 166 | + let Some((VisibilityDraw::Virtual(draw_index), primitive, _)) = record.decode_draw() |
| 167 | + else { |
| 168 | + panic!("virtual raster emitted a compatibility or invalid visibility ID"); |
| 169 | + }; |
| 170 | + assert!(draw_index < 4); |
| 171 | + assert_eq!(primitive, 0); |
| 172 | + covered += 1; |
| 173 | + } |
| 174 | + } |
| 175 | + assert!(covered > 0); |
| 176 | + assert!(background > 0); |
| 177 | + assert_eq!( |
| 178 | + raster.counted_submission_supported(), |
| 179 | + device |
| 180 | + .features() |
| 181 | + .contains(wgpu::Features::MULTI_DRAW_INDIRECT_COUNT) |
| 182 | + ); |
| 183 | +} |
0 commit comments