mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-27 16:49:39 +01:00
feat(core): support building custom geometry BLAS (#9290)
This commit is contained in:
1 parent
fd04edc0ac
commit
438ed2a7ce
18 files changed
+1419
-6
No files matched your search
@@ -46,6 +46,7 @@ Bottom level categories:
|
||||
|
||||
#### General
|
||||
|
||||
- BLAS support for procedural AABB geometry (`BlasGeometrySizeDescriptors::AABBs`, `BlasAabbGeometry`, and related descriptors). By @dylanblokhuis in [#9290](https://github.com/gfx-rs/wgpu/pull/9290)
|
||||
- Added "limit bucketing" functionality which can adjust adapter limits and features to match one of several pre-defined buckets. This is controlled by the new `apply_limit_buckets` member in `RequestAdapterOptions`, which is `false` by default. By @andyleiserson in [#9119](https://github.com/gfx-rs/wgpu/pull/9119).
|
||||
- Make `wgpu_types::texture::format::TextureChannel` accessible as `wgpu::TextureChannel`. By @TornaxO7 in [#9394](https://github.com/gfx-rs/wgpu/pull/9349).
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ pub mod mipmap;
|
||||
pub mod msaa_line;
|
||||
pub mod multiple_render_targets;
|
||||
pub mod multiview;
|
||||
pub mod ray_aabb_compute;
|
||||
pub mod ray_cube_compute;
|
||||
pub mod ray_cube_fragment;
|
||||
pub mod ray_cube_normals;
|
||||
@@ -56,6 +57,7 @@ fn all_tests() -> Vec<wgpu_test::GpuTestInitializer> {
|
||||
mipmap::TEST_QUERY,
|
||||
msaa_line::TEST,
|
||||
multiple_render_targets::TEST,
|
||||
ray_aabb_compute::TEST,
|
||||
ray_cube_compute::TEST,
|
||||
ray_cube_fragment::TEST,
|
||||
ray_cube_normals::TEST,
|
||||
|
||||
@@ -158,6 +158,12 @@ const EXAMPLES: &[ExampleDesc] = &[
|
||||
webgl: false, // No RODS
|
||||
webgpu: true,
|
||||
},
|
||||
ExampleDesc {
|
||||
name: "ray_aabb_compute",
|
||||
function: wgpu_examples::ray_aabb_compute::main,
|
||||
webgl: false,
|
||||
webgpu: false,
|
||||
},
|
||||
ExampleDesc {
|
||||
name: "ray_cube_compute",
|
||||
function: wgpu_examples::ray_cube_compute::main,
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# ray-aabb
|
||||
|
||||
This example ray-traces AABB geometry from an acceleration structure, then turns
|
||||
each hit into a sphere: the compute shader uses hardware ray queries for the
|
||||
AABBs, fits a sphere inside each box, and resolves it with ray-sphere
|
||||
intersection.
|
||||
|
||||
NOTE: The Metal backend does not support AABB intersections in ray queries yet,
|
||||
so this example will not work.
|
||||
|
||||
## To Run
|
||||
|
||||
```
|
||||
cargo run --bin wgpu-examples ray_aabb_compute
|
||||
```
|
||||
|
||||
## Screenshots
|
||||
|
||||

|
||||
@@ -0,0 +1,34 @@
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(0) tex_coords: vec2<f32>,
|
||||
};
|
||||
|
||||
@vertex
|
||||
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
|
||||
var result: VertexOutput;
|
||||
let x = i32(vertex_index) / 2;
|
||||
let y = i32(vertex_index) & 1;
|
||||
let tc = vec2<f32>(
|
||||
f32(x) * 2.0,
|
||||
f32(y) * 2.0
|
||||
);
|
||||
result.position = vec4<f32>(
|
||||
tc.x * 2.0 - 1.0,
|
||||
1.0 - tc.y * 2.0,
|
||||
0.0, 1.0
|
||||
);
|
||||
result.tex_coords = tc;
|
||||
return result;
|
||||
}
|
||||
|
||||
@group(0)
|
||||
@binding(0)
|
||||
var r_color: texture_2d<f32>;
|
||||
@group(0)
|
||||
@binding(1)
|
||||
var r_sampler: sampler;
|
||||
|
||||
@fragment
|
||||
fn fs_main(vertex: VertexOutput) -> @location(0) vec4<f32> {
|
||||
return textureSample(r_color, r_sampler, vertex.tex_coords);
|
||||
}
|
||||
@@ -0,0 +1,406 @@
|
||||
use std::{borrow::Cow, iter, mem};
|
||||
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use glam::{Affine3A, Mat4, Quat, Vec3};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use wgpu::StoreOp;
|
||||
|
||||
use crate::utils;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
struct GpuAabb {
|
||||
min: [f32; 3],
|
||||
max: [f32; 3],
|
||||
_pad: [f32; 2],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
struct Uniforms {
|
||||
view_inverse: Mat4,
|
||||
proj_inverse: Mat4,
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn affine_to_rows(mat: &Affine3A) -> [f32; 12] {
|
||||
let row_0 = mat.matrix3.row(0);
|
||||
let row_1 = mat.matrix3.row(1);
|
||||
let row_2 = mat.matrix3.row(2);
|
||||
let translation = mat.translation;
|
||||
[
|
||||
row_0.x,
|
||||
row_0.y,
|
||||
row_0.z,
|
||||
translation.x,
|
||||
row_1.x,
|
||||
row_1.y,
|
||||
row_1.z,
|
||||
translation.y,
|
||||
row_2.x,
|
||||
row_2.y,
|
||||
row_2.z,
|
||||
translation.z,
|
||||
]
|
||||
}
|
||||
|
||||
struct Example {
|
||||
rt_target: wgpu::Texture,
|
||||
#[expect(dead_code)]
|
||||
rt_view: wgpu::TextureView,
|
||||
#[expect(dead_code)]
|
||||
sampler: wgpu::Sampler,
|
||||
#[expect(dead_code)]
|
||||
uniform_buf: wgpu::Buffer,
|
||||
#[expect(dead_code)]
|
||||
aabb_buf: wgpu::Buffer,
|
||||
tlas: wgpu::Tlas,
|
||||
compute_pipeline: wgpu::ComputePipeline,
|
||||
compute_bind_group: wgpu::BindGroup,
|
||||
blit_pipeline: wgpu::RenderPipeline,
|
||||
blit_bind_group: wgpu::BindGroup,
|
||||
animation_timer: utils::AnimationTimer,
|
||||
}
|
||||
|
||||
impl crate::framework::Example for Example {
|
||||
fn required_features() -> wgpu::Features {
|
||||
wgpu::Features::TEXTURE_BINDING_ARRAY
|
||||
| wgpu::Features::VERTEX_WRITABLE_STORAGE
|
||||
| wgpu::Features::EXPERIMENTAL_RAY_QUERY
|
||||
}
|
||||
|
||||
fn required_downlevel_capabilities() -> wgpu::DownlevelCapabilities {
|
||||
wgpu::DownlevelCapabilities {
|
||||
flags: wgpu::DownlevelFlags::COMPUTE_SHADERS,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn required_limits() -> wgpu::Limits {
|
||||
wgpu::Limits::default().using_minimum_supported_acceleration_structure_values()
|
||||
}
|
||||
|
||||
fn init(
|
||||
config: &wgpu::SurfaceConfiguration,
|
||||
_adapter: &wgpu::Adapter,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
) -> Self {
|
||||
let aabb_data = [
|
||||
GpuAabb {
|
||||
min: [-3.5, -0.5, -0.5],
|
||||
max: [-1.5, 0.5, 0.5],
|
||||
_pad: [0.0; 2],
|
||||
},
|
||||
GpuAabb {
|
||||
min: [-0.5, -0.5, -0.5],
|
||||
max: [0.5, 0.5, 0.5],
|
||||
_pad: [0.0; 2],
|
||||
},
|
||||
GpuAabb {
|
||||
min: [1.5, -0.5, -0.5],
|
||||
max: [3.5, 0.5, 0.5],
|
||||
_pad: [0.0; 2],
|
||||
},
|
||||
];
|
||||
|
||||
let rt_target = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("rt_target"),
|
||||
size: wgpu::Extent3d {
|
||||
width: config.width,
|
||||
height: config.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8Unorm,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::STORAGE_BINDING,
|
||||
view_formats: &[wgpu::TextureFormat::Rgba8Unorm],
|
||||
});
|
||||
|
||||
let rt_view = rt_target.create_view(&wgpu::TextureViewDescriptor {
|
||||
label: None,
|
||||
format: Some(wgpu::TextureFormat::Rgba8Unorm),
|
||||
dimension: Some(wgpu::TextureViewDimension::D2),
|
||||
usage: None,
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
base_mip_level: 0,
|
||||
mip_level_count: None,
|
||||
base_array_layer: 0,
|
||||
array_layer_count: None,
|
||||
});
|
||||
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: Some("rt_sampler"),
|
||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||
mag_filter: wgpu::FilterMode::Linear,
|
||||
min_filter: wgpu::FilterMode::Linear,
|
||||
mipmap_filter: wgpu::MipmapFilterMode::Nearest,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let uniforms = {
|
||||
let view =
|
||||
Mat4::look_at_rh(Vec3::new(0.0, 0.5, 5.0), Vec3::new(0.0, 0.0, 0.0), Vec3::Y);
|
||||
let proj = Mat4::perspective_rh(
|
||||
59.0_f32.to_radians(),
|
||||
config.width as f32 / config.height as f32,
|
||||
0.001,
|
||||
1000.0,
|
||||
);
|
||||
|
||||
Uniforms {
|
||||
view_inverse: view.inverse(),
|
||||
proj_inverse: proj.inverse(),
|
||||
}
|
||||
};
|
||||
|
||||
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Uniform Buffer"),
|
||||
contents: bytemuck::cast_slice(&[uniforms]),
|
||||
usage: wgpu::BufferUsages::UNIFORM,
|
||||
});
|
||||
|
||||
let aabb_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("AABB primitives"),
|
||||
contents: bytemuck::cast_slice(&aabb_data),
|
||||
usage: wgpu::BufferUsages::BLAS_INPUT | wgpu::BufferUsages::STORAGE,
|
||||
});
|
||||
|
||||
let aabb_size_desc = wgpu::BlasAABBGeometrySizeDescriptor {
|
||||
primitive_count: aabb_data.len() as u32,
|
||||
flags: wgpu::AccelerationStructureGeometryFlags::OPAQUE,
|
||||
};
|
||||
|
||||
let blas = device.create_blas(
|
||||
&wgpu::CreateBlasDescriptor {
|
||||
label: None,
|
||||
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: wgpu::AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
wgpu::BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![aabb_size_desc.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
|
||||
label: None,
|
||||
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: wgpu::AccelerationStructureUpdateMode::Build,
|
||||
max_instances: 1,
|
||||
});
|
||||
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("ray_aabb_compute"),
|
||||
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
|
||||
});
|
||||
|
||||
let blit_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("blit"),
|
||||
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("blit.wgsl"))),
|
||||
});
|
||||
|
||||
let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("rt_aabb"),
|
||||
layout: None,
|
||||
module: &shader,
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let compute_bind_group_layout = compute_pipeline.get_bind_group_layout(0);
|
||||
|
||||
let compute_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: None,
|
||||
layout: &compute_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&rt_view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: uniform_buf.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: wgpu::BindingResource::AccelerationStructure(&tlas),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: aabb_buf.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let blit_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("blit"),
|
||||
layout: None,
|
||||
vertex: wgpu::VertexState {
|
||||
module: &blit_shader,
|
||||
entry_point: Some("vs_main"),
|
||||
compilation_options: Default::default(),
|
||||
buffers: &[],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &blit_shader,
|
||||
entry_point: Some("fs_main"),
|
||||
compilation_options: Default::default(),
|
||||
targets: &[Some(config.format.into())],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let blit_bind_group_layout = blit_pipeline.get_bind_group_layout(0);
|
||||
|
||||
let blit_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: None,
|
||||
layout: &blit_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&rt_view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
tlas[0] = Some(wgpu::TlasInstance::new(
|
||||
&blas,
|
||||
affine_to_rows(&Affine3A::from_rotation_translation(
|
||||
Quat::IDENTITY,
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
)),
|
||||
0,
|
||||
0xff,
|
||||
));
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
encoder.build_acceleration_structures(
|
||||
iter::once(&wgpu::BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: wgpu::BlasGeometries::AabbGeometries(vec![wgpu::BlasAabbGeometry {
|
||||
size: &aabb_size_desc,
|
||||
stride: mem::size_of::<GpuAabb>() as wgpu::BufferAddress,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}),
|
||||
iter::once(&tlas),
|
||||
);
|
||||
|
||||
queue.submit(Some(encoder.finish()));
|
||||
|
||||
Example {
|
||||
rt_target,
|
||||
rt_view,
|
||||
sampler,
|
||||
uniform_buf,
|
||||
aabb_buf,
|
||||
tlas,
|
||||
compute_pipeline,
|
||||
compute_bind_group,
|
||||
blit_pipeline,
|
||||
blit_bind_group,
|
||||
animation_timer: utils::AnimationTimer::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, _event: winit::event::WindowEvent) {}
|
||||
|
||||
fn resize(
|
||||
&mut self,
|
||||
_config: &wgpu::SurfaceConfiguration,
|
||||
_device: &wgpu::Device,
|
||||
_queue: &wgpu::Queue,
|
||||
) {
|
||||
}
|
||||
|
||||
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
|
||||
let anim_time = self.animation_timer.time();
|
||||
|
||||
self.tlas[0].as_mut().unwrap().transform =
|
||||
affine_to_rows(&Affine3A::from_rotation_translation(
|
||||
Quat::from_rotation_y(anim_time * 0.4),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
));
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));
|
||||
|
||||
{
|
||||
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: None,
|
||||
timestamp_writes: None,
|
||||
});
|
||||
cpass.set_pipeline(&self.compute_pipeline);
|
||||
cpass.set_bind_group(0, Some(&self.compute_bind_group), &[]);
|
||||
cpass.dispatch_workgroups(self.rt_target.width() / 8, self.rt_target.height() / 8, 1);
|
||||
}
|
||||
|
||||
{
|
||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: None,
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view,
|
||||
depth_slice: None,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
|
||||
store: StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
|
||||
rpass.set_pipeline(&self.blit_pipeline);
|
||||
rpass.set_bind_group(0, Some(&self.blit_bind_group), &[]);
|
||||
rpass.draw(0..3, 0..1);
|
||||
}
|
||||
|
||||
queue.submit(Some(encoder.finish()));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
crate::framework::run::<Example>("ray-aabb");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[wgpu_test::gpu_test]
|
||||
pub static TEST: crate::framework::ExampleTestParams = crate::framework::ExampleTestParams {
|
||||
name: "ray_aabb_compute",
|
||||
image_path: "/examples/features/src/ray_aabb_compute/screenshot.png",
|
||||
width: 1024,
|
||||
height: 768,
|
||||
optional_features: wgpu::Features::default(),
|
||||
base_test_parameters: wgpu_test::TestParameters::default()
|
||||
// Metal has no AABB intersection in ray queries yet; image compare fails.
|
||||
// https://github.com/gfx-rs/wgpu/pull/9304
|
||||
// https://github.com/gfx-rs/wgpu/issues/9100
|
||||
.expect_fail(wgpu_test::FailureCase::backend(wgpu::Backends::METAL)),
|
||||
comparisons: &[wgpu_test::ComparisonType::Mean(0.02)],
|
||||
_phantom: std::marker::PhantomData::<Example>,
|
||||
};
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
@@ -0,0 +1,98 @@
|
||||
enable wgpu_ray_query;
|
||||
|
||||
struct Uniforms {
|
||||
view_inv: mat4x4<f32>,
|
||||
proj_inv: mat4x4<f32>,
|
||||
};
|
||||
|
||||
struct GpuAabb {
|
||||
min_x: f32,
|
||||
min_y: f32,
|
||||
min_z: f32,
|
||||
max_x: f32,
|
||||
max_y: f32,
|
||||
max_z: f32,
|
||||
_pad0: f32,
|
||||
_pad1: f32,
|
||||
}
|
||||
|
||||
@group(0) @binding(0)
|
||||
var output: texture_storage_2d<rgba8unorm, write>;
|
||||
|
||||
@group(0) @binding(1)
|
||||
var<uniform> uniforms: Uniforms;
|
||||
|
||||
@group(0) @binding(2)
|
||||
var acc_struct: acceleration_structure;
|
||||
|
||||
@group(0) @binding(3)
|
||||
var<storage, read> gpu_aabbs: array<GpuAabb, 3>;
|
||||
|
||||
fn ray_sphere(origin: vec3<f32>, dir: vec3<f32>, center: vec3<f32>, radius: f32) -> f32 {
|
||||
let oc = origin - center;
|
||||
let b = dot(oc, dir);
|
||||
let c = dot(oc, oc) - radius * radius;
|
||||
let disc = b * b - c;
|
||||
if (disc < 0.0) {
|
||||
return -1.0;
|
||||
}
|
||||
let s = sqrt(disc);
|
||||
var t = -b - s;
|
||||
if (t < 0.001) {
|
||||
t = -b + s;
|
||||
}
|
||||
if (t < 0.001) {
|
||||
return -1.0;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
@compute @workgroup_size(8, 8)
|
||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
let target_size = textureDimensions(output);
|
||||
var color = vec4<f32>(vec2<f32>(global_id.xy) / vec2<f32>(target_size), 0.0, 1.0);
|
||||
|
||||
let pixel_center = vec2<f32>(global_id.xy) + vec2<f32>(0.5);
|
||||
let in_uv = pixel_center / vec2<f32>(target_size.xy);
|
||||
let d = in_uv * 2.0 - 1.0;
|
||||
|
||||
let origin = (uniforms.view_inv * vec4<f32>(0.0, 0.0, 0.0, 1.0)).xyz;
|
||||
let temp = uniforms.proj_inv * vec4<f32>(d.x, d.y, 1.0, 1.0);
|
||||
let dir = normalize((uniforms.view_inv * vec4<f32>(normalize(temp.xyz), 0.0)).xyz);
|
||||
|
||||
var rq: ray_query;
|
||||
rayQueryInitialize(&rq, acc_struct, RayDesc(0u, 0xFFu, 0.01, 200.0, origin, dir));
|
||||
|
||||
while (rayQueryProceed(&rq)) {
|
||||
let c = rayQueryGetCandidateIntersection(&rq);
|
||||
if (c.kind == RAY_QUERY_INTERSECTION_AABB) {
|
||||
let aabb = gpu_aabbs[c.primitive_index];
|
||||
let min_v = vec3<f32>(aabb.min_x, aabb.min_y, aabb.min_z);
|
||||
let max_v = vec3<f32>(aabb.max_x, aabb.max_y, aabb.max_z);
|
||||
let extent = max_v - min_v;
|
||||
let radius = min(extent.x, min(extent.y, extent.z)) * 0.45;
|
||||
let center_o = (min_v + max_v) * 0.5;
|
||||
let center_w = (c.object_to_world * vec4<f32>(center_o, 1.0)).xyz;
|
||||
let t = ray_sphere(origin, dir, center_w, radius);
|
||||
if (t > 0.0) {
|
||||
rayQueryGenerateIntersection(&rq, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let hit = rayQueryGetCommittedIntersection(&rq);
|
||||
if (hit.kind == RAY_QUERY_INTERSECTION_GENERATED) {
|
||||
let aabb = gpu_aabbs[hit.primitive_index];
|
||||
let min_v = vec3<f32>(aabb.min_x, aabb.min_y, aabb.min_z);
|
||||
let max_v = vec3<f32>(aabb.max_x, aabb.max_y, aabb.max_z);
|
||||
let extent = max_v - min_v;
|
||||
let radius = min(extent.x, min(extent.y, extent.z)) * 0.45;
|
||||
let center_o = (min_v + max_v) * 0.5;
|
||||
let center_w = (hit.object_to_world * vec4<f32>(center_o, 1.0)).xyz;
|
||||
let p = origin + dir * hit.t;
|
||||
let n = normalize(p - center_w);
|
||||
color = vec4<f32>(n * 0.5 + vec3<f32>(0.5), 1.0);
|
||||
}
|
||||
|
||||
textureStore(output, global_id.xy, color);
|
||||
}
|
||||
@@ -1305,6 +1305,13 @@ impl Player {
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
wgc::ray_tracing::OwnedBlasGeometries::AabbGeometries(geos) => {
|
||||
wgc::ray_tracing::OwnedBlasGeometries::AabbGeometries(
|
||||
geos.into_iter()
|
||||
.map(|geo| self.resolve_blas_aabb_geometry(geo))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1326,6 +1333,18 @@ impl Player {
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_blas_aabb_geometry(
|
||||
&self,
|
||||
geometry: wgc::ray_tracing::OwnedBlasAabbGeometry<PointerReferences>,
|
||||
) -> wgc::ray_tracing::OwnedBlasAabbGeometry<ArcReferences> {
|
||||
wgc::ray_tracing::OwnedBlasAabbGeometry {
|
||||
size: geometry.size,
|
||||
stride: geometry.stride,
|
||||
aabb_buffer: self.resolve_buffer_id(geometry.aabb_buffer),
|
||||
primitive_offset: geometry.primitive_offset,
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_tlas_instance(
|
||||
&self,
|
||||
instance: wgc::ray_tracing::OwnedTlasInstance<PointerReferences>,
|
||||
|
||||
@@ -0,0 +1,527 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::ray_tracing::acceleration_structure_limits;
|
||||
use wgpu::util::{BufferInitDescriptor, DeviceExt};
|
||||
use wgpu::{
|
||||
include_wgsl, AccelerationStructureFlags, AccelerationStructureGeometryFlags,
|
||||
AccelerationStructureUpdateMode, BlasAABBGeometrySizeDescriptor, BlasAabbGeometry,
|
||||
BlasBuildEntry, BlasGeometries, BlasGeometrySizeDescriptors, BlasTriangleGeometry,
|
||||
BlasTriangleGeometrySizeDescriptor, BufferAddress, BufferUsages, CommandEncoderDescriptor,
|
||||
ComputePassDescriptor, ComputePipelineDescriptor, CreateBlasDescriptor, CreateTlasDescriptor,
|
||||
TlasInstance, VertexFormat, AABB_GEOMETRY_MIN_STRIDE,
|
||||
};
|
||||
use wgpu::{BindGroupDescriptor, BindGroupEntry, BindingResource, BufferDescriptor};
|
||||
use wgpu_test::{
|
||||
fail, gpu_test, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext,
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct AabbPrimitive {
|
||||
min: [f32; 3],
|
||||
max: [f32; 3],
|
||||
}
|
||||
|
||||
pub fn all_tests(tests: &mut Vec<GpuTestInitializer>) {
|
||||
tests.extend([
|
||||
AABB_BLAS_BUILD_AND_TRACE,
|
||||
AABB_UNALIGNED_PRIMITIVE_OFFSET,
|
||||
AABB_INVALID_STRIDE,
|
||||
AABB_GEOMETRY_KIND_MISMATCH,
|
||||
AABB_INSUFFICIENT_BUFFER,
|
||||
AABB_PRIMITIVE_COUNT_EXCEEDS_CREATION,
|
||||
AABB_FLAGS_MISMATCH,
|
||||
]);
|
||||
}
|
||||
|
||||
fn aabb_size_desc(primitive_count: u32) -> BlasAABBGeometrySizeDescriptor {
|
||||
BlasAABBGeometrySizeDescriptor {
|
||||
primitive_count,
|
||||
flags: AccelerationStructureGeometryFlags::empty(),
|
||||
}
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_BLAS_BUILD_AND_TRACE: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY),
|
||||
)
|
||||
.run_sync(aabb_blas_build_and_trace);
|
||||
|
||||
fn aabb_blas_build_and_trace(ctx: TestingContext) {
|
||||
let aabb_data = AabbPrimitive {
|
||||
min: [-1.0, -1.0, 2.0],
|
||||
max: [1.0, 1.0, 4.0],
|
||||
};
|
||||
|
||||
let aabb_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: bytemuck::bytes_of(&aabb_data),
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
|
||||
let blas_size = aabb_size_desc(1);
|
||||
let blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("AABB BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![blas_size.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut tlas = ctx.device.create_tlas(&CreateTlasDescriptor {
|
||||
label: Some("TLAS"),
|
||||
max_instances: 1,
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
});
|
||||
tlas[0] = Some(TlasInstance::new(
|
||||
&blas,
|
||||
[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
|
||||
0,
|
||||
0xFF,
|
||||
));
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor {
|
||||
label: Some("build"),
|
||||
});
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &blas_size,
|
||||
stride: size_of::<AabbPrimitive>() as BufferAddress,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}],
|
||||
[&tlas],
|
||||
);
|
||||
ctx.queue.submit([encoder.finish()]);
|
||||
|
||||
let out_buf = ctx.device.create_buffer(&BufferDescriptor {
|
||||
label: None,
|
||||
size: 176,
|
||||
usage: BufferUsages::STORAGE,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let shader = ctx
|
||||
.device
|
||||
.create_shader_module(include_wgsl!("shader.wgsl"));
|
||||
let compute_pipeline = ctx
|
||||
.device
|
||||
.create_compute_pipeline(&ComputePipelineDescriptor {
|
||||
label: None,
|
||||
layout: None,
|
||||
module: &shader,
|
||||
entry_point: Some("all_of_struct"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let bind_group = ctx.device.create_bind_group(&BindGroupDescriptor {
|
||||
label: None,
|
||||
layout: &compute_pipeline.get_bind_group_layout(0),
|
||||
entries: &[
|
||||
BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: BindingResource::AccelerationStructure(&tlas),
|
||||
},
|
||||
BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: BindingResource::Buffer(out_buf.as_entire_buffer_binding()),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
{
|
||||
let mut pass = encoder.begin_compute_pass(&ComputePassDescriptor {
|
||||
label: None,
|
||||
timestamp_writes: None,
|
||||
});
|
||||
pass.set_pipeline(&compute_pipeline);
|
||||
pass.set_bind_group(0, Some(&bind_group), &[]);
|
||||
pass.dispatch_workgroups(1, 1, 1);
|
||||
}
|
||||
ctx.queue.submit([encoder.finish()]);
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_UNALIGNED_PRIMITIVE_OFFSET: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY)
|
||||
.enable_noop(),
|
||||
)
|
||||
.run_sync(aabb_unaligned_primitive_offset);
|
||||
|
||||
/// `primitive_offset` must be a multiple of 8.
|
||||
fn aabb_unaligned_primitive_offset(ctx: TestingContext) {
|
||||
let stride = size_of::<AabbPrimitive>() as BufferAddress;
|
||||
let aabb_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: &[0u8; 4 + size_of::<AabbPrimitive>()],
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
let blas_size = aabb_size_desc(1);
|
||||
let blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![blas_size.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &blas_size,
|
||||
stride,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 4,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_INVALID_STRIDE: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY)
|
||||
.enable_noop(),
|
||||
)
|
||||
.run_sync(aabb_invalid_stride);
|
||||
|
||||
/// AABB `stride` must be at least `AABB_GEOMETRY_MIN_STRIDE` and a multiple of 8.
|
||||
fn aabb_invalid_stride(ctx: TestingContext) {
|
||||
let aabb_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: bytemuck::bytes_of(&AabbPrimitive {
|
||||
min: [-1.0, -1.0, -1.0],
|
||||
max: [1.0, 1.0, 1.0],
|
||||
}),
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
let blas_size = aabb_size_desc(1);
|
||||
let blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![blas_size.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &blas_size,
|
||||
stride: AABB_GEOMETRY_MIN_STRIDE + 1,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_GEOMETRY_KIND_MISMATCH: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY)
|
||||
.enable_noop(),
|
||||
)
|
||||
.run_sync(aabb_geometry_kind_mismatch);
|
||||
|
||||
/// Triangle build entries are invalid for an AABB BLAS and vice versa.
|
||||
fn aabb_geometry_kind_mismatch(ctx: TestingContext) {
|
||||
let vertices = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: &[0; size_of::<[[f32; 3]; 3]>()],
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
let stride = size_of::<AabbPrimitive>() as BufferAddress;
|
||||
let aabb_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: bytemuck::bytes_of(&AabbPrimitive {
|
||||
min: [-1.0, -1.0, -1.0],
|
||||
max: [1.0, 1.0, 1.0],
|
||||
}),
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
|
||||
let aabb_blas_size = aabb_size_desc(1);
|
||||
let aabb_blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("AABB BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![aabb_blas_size.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let tri_size = BlasTriangleGeometrySizeDescriptor {
|
||||
vertex_format: VertexFormat::Float32x3,
|
||||
vertex_count: 3,
|
||||
index_format: None,
|
||||
index_count: None,
|
||||
flags: AccelerationStructureGeometryFlags::OPAQUE,
|
||||
};
|
||||
|
||||
// AABB BLAS built with triangle geometry
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &aabb_blas,
|
||||
geometry: BlasGeometries::TriangleGeometries(vec![BlasTriangleGeometry {
|
||||
size: &tri_size,
|
||||
vertex_buffer: &vertices,
|
||||
first_vertex: 0,
|
||||
vertex_stride: size_of::<[f32; 3]>() as BufferAddress,
|
||||
index_buffer: None,
|
||||
first_index: None,
|
||||
transform_buffer: None,
|
||||
transform_buffer_offset: None,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
|
||||
// Triangle BLAS built with AABB geometry
|
||||
let tri_blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("tri BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::Triangles {
|
||||
descriptors: vec![tri_size.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &tri_blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &aabb_blas_size,
|
||||
stride,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_INSUFFICIENT_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY)
|
||||
.enable_noop(),
|
||||
)
|
||||
.run_sync(aabb_insufficient_buffer);
|
||||
|
||||
/// The AABB buffer must cover `primitive_offset + primitive_count * stride`.
|
||||
fn aabb_insufficient_buffer(ctx: TestingContext) {
|
||||
let stride = size_of::<AabbPrimitive>() as BufferAddress;
|
||||
let small_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: &[0u8; 16],
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
let blas_size = aabb_size_desc(1);
|
||||
let blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![blas_size.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &blas_size,
|
||||
stride,
|
||||
aabb_buffer: &small_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_PRIMITIVE_COUNT_EXCEEDS_CREATION: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY)
|
||||
.enable_noop(),
|
||||
)
|
||||
.run_sync(aabb_primitive_count_exceeds_creation);
|
||||
|
||||
/// Build cannot request more AABB primitives than the BLAS was created for.
|
||||
fn aabb_primitive_count_exceeds_creation(ctx: TestingContext) {
|
||||
let stride = size_of::<AabbPrimitive>() as BufferAddress;
|
||||
let aabb_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: bytemuck::cast_slice(&[
|
||||
AabbPrimitive {
|
||||
min: [-1.0, -1.0, -1.0],
|
||||
max: [1.0, 1.0, 1.0],
|
||||
},
|
||||
AabbPrimitive {
|
||||
min: [-1.0, -1.0, -1.0],
|
||||
max: [1.0, 1.0, 1.0],
|
||||
},
|
||||
]),
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
let create_desc = aabb_size_desc(1);
|
||||
let build_desc = aabb_size_desc(2);
|
||||
|
||||
let blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![create_desc],
|
||||
},
|
||||
);
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &build_desc,
|
||||
stride,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
}
|
||||
|
||||
#[gpu_test]
|
||||
static AABB_FLAGS_MISMATCH: GpuTestConfiguration = GpuTestConfiguration::new()
|
||||
.parameters(
|
||||
TestParameters::default()
|
||||
.test_features_limits()
|
||||
.limits(acceleration_structure_limits())
|
||||
.features(wgpu::Features::EXPERIMENTAL_RAY_QUERY)
|
||||
.enable_noop(),
|
||||
)
|
||||
.run_sync(aabb_flags_mismatch);
|
||||
|
||||
/// Per-geometry flags at build time must match those at creation.
|
||||
fn aabb_flags_mismatch(ctx: TestingContext) {
|
||||
let aabb_buf = ctx.device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: bytemuck::bytes_of(&AabbPrimitive {
|
||||
min: [-1.0, -1.0, -1.0],
|
||||
max: [1.0, 1.0, 1.0],
|
||||
}),
|
||||
usage: BufferUsages::BLAS_INPUT,
|
||||
});
|
||||
let create_desc = BlasAABBGeometrySizeDescriptor {
|
||||
primitive_count: 1,
|
||||
flags: AccelerationStructureGeometryFlags::OPAQUE,
|
||||
};
|
||||
let build_desc = BlasAABBGeometrySizeDescriptor {
|
||||
primitive_count: 1,
|
||||
flags: AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION,
|
||||
};
|
||||
|
||||
let blas = ctx.device.create_blas(
|
||||
&CreateBlasDescriptor {
|
||||
label: Some("BLAS"),
|
||||
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
|
||||
update_mode: AccelerationStructureUpdateMode::Build,
|
||||
},
|
||||
BlasGeometrySizeDescriptors::AABBs {
|
||||
descriptors: vec![create_desc.clone()],
|
||||
},
|
||||
);
|
||||
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&CommandEncoderDescriptor::default());
|
||||
encoder.build_acceleration_structures(
|
||||
[&BlasBuildEntry {
|
||||
blas: &blas,
|
||||
geometry: BlasGeometries::AabbGeometries(vec![BlasAabbGeometry {
|
||||
size: &build_desc,
|
||||
stride: size_of::<AabbPrimitive>() as BufferAddress,
|
||||
aabb_buffer: &aabb_buf,
|
||||
primitive_offset: 0,
|
||||
}]),
|
||||
}],
|
||||
[],
|
||||
);
|
||||
fail(&ctx.device, || encoder.finish(), None);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ use wgpu::{
|
||||
};
|
||||
use wgpu_test::TestingContext;
|
||||
|
||||
mod as_aabb;
|
||||
mod as_build;
|
||||
mod as_create;
|
||||
mod as_use_after_free;
|
||||
@@ -19,6 +20,7 @@ mod scene;
|
||||
mod shader;
|
||||
|
||||
pub fn all_tests(tests: &mut Vec<wgpu_test::GpuTestInitializer>) {
|
||||
as_aabb::all_tests(tests);
|
||||
as_build::all_tests(tests);
|
||||
as_create::all_tests(tests);
|
||||
as_use_after_free::all_tests(tests);
|
||||
|
||||
@@ -20,9 +20,9 @@ use crate::{
|
||||
id::CommandEncoderId,
|
||||
init_tracker::MemoryInitKind,
|
||||
ray_tracing::{
|
||||
ArcBlasBuildEntry, ArcBlasGeometries, ArcBlasTriangleGeometry, ArcTlasInstance,
|
||||
ArcTlasPackage, BlasBuildEntry, BlasGeometries, BuildAccelerationStructureError,
|
||||
OwnedBlasBuildEntry, OwnedTlasPackage, TlasPackage,
|
||||
ArcBlasAabbGeometry, ArcBlasBuildEntry, ArcBlasGeometries, ArcBlasTriangleGeometry,
|
||||
ArcTlasInstance, ArcTlasPackage, BlasBuildEntry, BlasGeometries,
|
||||
BuildAccelerationStructureError, OwnedBlasBuildEntry, OwnedTlasPackage, TlasPackage,
|
||||
},
|
||||
resource::{Blas, BlasCompactState, Labeled, StagingBuffer, Tlas},
|
||||
scratch::ScratchBuffer,
|
||||
@@ -139,6 +139,19 @@ impl Global {
|
||||
.collect::<Result<_, BuildAccelerationStructureError>>()?;
|
||||
ArcBlasGeometries::TriangleGeometries(tri_geo)
|
||||
}
|
||||
BlasGeometries::AabbGeometries(aabb_geometries) => {
|
||||
let aabb_geo = aabb_geometries
|
||||
.map(|ag| {
|
||||
Ok(ArcBlasAabbGeometry {
|
||||
size: ag.size.clone(),
|
||||
stride: ag.stride,
|
||||
aabb_buffer: self.resolve_buffer_id(ag.aabb_buffer)?,
|
||||
primitive_offset: ag.primitive_offset,
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, BuildAccelerationStructureError>>()?;
|
||||
ArcBlasGeometries::AabbGeometries(aabb_geo)
|
||||
}
|
||||
};
|
||||
Ok(ArcBlasBuildEntry {
|
||||
blas: self.resolve_blas_id(blas_entry.blas_id)?,
|
||||
@@ -592,6 +605,11 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>(
|
||||
for (i, mesh) in triangle_geometries.iter().enumerate() {
|
||||
let size_desc = match &blas.sizes {
|
||||
wgt::BlasGeometrySizeDescriptors::Triangles { descriptors } => descriptors,
|
||||
_ => {
|
||||
return Err(BuildAccelerationStructureError::BlasGeometryKindMismatch(
|
||||
blas.error_ident(),
|
||||
));
|
||||
}
|
||||
};
|
||||
if i >= size_desc.len() {
|
||||
return Err(BuildAccelerationStructureError::IncompatibleBlasBuildSizes(
|
||||
@@ -888,6 +906,118 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>(
|
||||
});
|
||||
}
|
||||
}
|
||||
ArcBlasGeometries::AabbGeometries(aabb_geometries) => {
|
||||
let mut aabb_entries =
|
||||
Vec::<hal::AccelerationStructureAABBs<dyn hal::DynBuffer>>::new();
|
||||
|
||||
for (i, aabb) in aabb_geometries.iter().enumerate() {
|
||||
let size_desc = match &blas.sizes {
|
||||
wgt::BlasGeometrySizeDescriptors::AABBs { descriptors } => descriptors,
|
||||
_ => {
|
||||
return Err(BuildAccelerationStructureError::BlasGeometryKindMismatch(
|
||||
blas.error_ident(),
|
||||
));
|
||||
}
|
||||
};
|
||||
if i >= size_desc.len() {
|
||||
return Err(BuildAccelerationStructureError::IncompatibleBlasBuildSizes(
|
||||
blas.error_ident(),
|
||||
));
|
||||
}
|
||||
let size_desc = &size_desc[i];
|
||||
|
||||
if size_desc.flags != aabb.size.flags {
|
||||
return Err(BuildAccelerationStructureError::IncompatibleBlasFlags(
|
||||
blas.error_ident(),
|
||||
size_desc.flags,
|
||||
aabb.size.flags,
|
||||
));
|
||||
}
|
||||
|
||||
if size_desc.primitive_count < aabb.size.primitive_count {
|
||||
return Err(
|
||||
BuildAccelerationStructureError::IncompatibleBlasAabbPrimitiveCount(
|
||||
blas.error_ident(),
|
||||
size_desc.primitive_count,
|
||||
aabb.size.primitive_count,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if aabb.primitive_offset % 8 != 0 {
|
||||
return Err(
|
||||
BuildAccelerationStructureError::UnalignedAabbPrimitiveOffset(
|
||||
blas.error_ident(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if aabb.stride < wgt::AABB_GEOMETRY_MIN_STRIDE || aabb.stride % 8 != 0 {
|
||||
return Err(BuildAccelerationStructureError::InvalidAabbStride(
|
||||
blas.error_ident(),
|
||||
aabb.stride,
|
||||
));
|
||||
}
|
||||
|
||||
let aabb_buffer = aabb.aabb_buffer.clone();
|
||||
let aabb_pending = state.tracker.buffers.set_single(
|
||||
&aabb_buffer,
|
||||
BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,
|
||||
);
|
||||
let aabb_raw = {
|
||||
let aabb_raw = aabb.aabb_buffer.as_ref().try_raw(state.snatch_guard)?;
|
||||
let aabb_buffer = &aabb.aabb_buffer;
|
||||
aabb_buffer.check_usage(BufferUsages::BLAS_INPUT)?;
|
||||
|
||||
if let Some(barrier) = aabb_pending.map(|pending| {
|
||||
pending.into_hal(aabb_buffer.as_ref(), state.snatch_guard)
|
||||
}) {
|
||||
input_barriers.push(barrier);
|
||||
}
|
||||
|
||||
let required_end = aabb.primitive_offset as u64
|
||||
+ aabb.size.primitive_count as u64 * aabb.stride;
|
||||
if aabb_buffer.size < required_end {
|
||||
return Err(BuildAccelerationStructureError::InsufficientBufferSize(
|
||||
aabb_buffer.error_ident(),
|
||||
aabb_buffer.size,
|
||||
required_end,
|
||||
));
|
||||
}
|
||||
|
||||
state.buffer_memory_init_actions.extend(
|
||||
aabb_buffer.initialization_status.read().create_action(
|
||||
aabb_buffer,
|
||||
aabb.primitive_offset as u64..required_end,
|
||||
MemoryInitKind::NeedsInitializedMemory,
|
||||
),
|
||||
);
|
||||
aabb_raw
|
||||
};
|
||||
|
||||
aabb_entries.push(hal::AccelerationStructureAABBs {
|
||||
buffer: Some(aabb_raw),
|
||||
offset: aabb.primitive_offset,
|
||||
count: aabb.size.primitive_count,
|
||||
stride: aabb.stride,
|
||||
flags: aabb.size.flags,
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let scratch_buffer_offset = *scratch_buffer_blas_size;
|
||||
*scratch_buffer_blas_size += align_to(
|
||||
blas.size_info.build_scratch_size as u32,
|
||||
state.device.alignments.ray_tracing_scratch_buffer_alignment,
|
||||
) as u64;
|
||||
|
||||
blas_storage.push(BlasStore {
|
||||
blas: blas.clone(),
|
||||
entries: hal::AccelerationStructureEntries::AABBs(aabb_entries),
|
||||
scratch_buffer_offset,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -22,7 +22,7 @@ use crate::{
|
||||
LabelHelpers,
|
||||
};
|
||||
use hal::AccelerationStructureTriangleIndices;
|
||||
use wgt::Features;
|
||||
use wgt::{Features, AABB_GEOMETRY_MIN_STRIDE};
|
||||
|
||||
impl Device {
|
||||
pub fn create_blas(
|
||||
@@ -117,6 +117,43 @@ impl Device {
|
||||
)
|
||||
}
|
||||
}
|
||||
wgt::BlasGeometrySizeDescriptors::AABBs { descriptors } => {
|
||||
if descriptors.len() as u32 > self.limits.max_blas_geometry_count {
|
||||
return Err(CreateBlasError::TooManyGeometries(
|
||||
self.limits.max_blas_geometry_count,
|
||||
descriptors.len() as u32,
|
||||
));
|
||||
}
|
||||
|
||||
let mut entries =
|
||||
Vec::<hal::AccelerationStructureAABBs<dyn hal::DynBuffer>>::with_capacity(
|
||||
descriptors.len(),
|
||||
);
|
||||
for desc in descriptors {
|
||||
if desc.primitive_count > self.limits.max_blas_primitive_count {
|
||||
return Err(CreateBlasError::TooManyPrimitives(
|
||||
self.limits.max_blas_primitive_count,
|
||||
desc.primitive_count,
|
||||
));
|
||||
}
|
||||
|
||||
entries.push(hal::AccelerationStructureAABBs::<dyn hal::DynBuffer> {
|
||||
buffer: Some(self.zero_buffer.as_ref()),
|
||||
offset: 0,
|
||||
count: desc.primitive_count,
|
||||
stride: AABB_GEOMETRY_MIN_STRIDE,
|
||||
flags: desc.flags,
|
||||
});
|
||||
}
|
||||
unsafe {
|
||||
self.raw().get_acceleration_structure_build_sizes(
|
||||
&hal::GetAccelerationStructureBuildSizesDescriptor {
|
||||
entries: &hal::AccelerationStructureEntries::AABBs(entries),
|
||||
flags: blas_desc.flags,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let raw = unsafe {
|
||||
|
||||
@@ -362,6 +362,11 @@ impl IntoTrace for crate::ray_tracing::OwnedBlasGeometries<ArcReferences> {
|
||||
geos.into_iter().map(|g| g.into_trace()).collect(),
|
||||
)
|
||||
}
|
||||
crate::ray_tracing::OwnedBlasGeometries::AabbGeometries(geos) => {
|
||||
crate::ray_tracing::OwnedBlasGeometries::AabbGeometries(
|
||||
geos.into_iter().map(|g| g.into_trace()).collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -382,6 +387,18 @@ impl IntoTrace for crate::ray_tracing::OwnedBlasTriangleGeometry<ArcReferences>
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoTrace for crate::ray_tracing::OwnedBlasAabbGeometry<ArcReferences> {
|
||||
type Output = crate::ray_tracing::OwnedBlasAabbGeometry<PointerReferences>;
|
||||
fn into_trace(self) -> Self::Output {
|
||||
crate::ray_tracing::OwnedBlasAabbGeometry {
|
||||
size: self.size,
|
||||
stride: self.stride,
|
||||
aabb_buffer: self.aabb_buffer.into_trace(),
|
||||
primitive_offset: self.primitive_offset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoTrace for crate::ray_tracing::OwnedTlasPackage<ArcReferences> {
|
||||
type Output = crate::ray_tracing::OwnedTlasPackage<PointerReferences>;
|
||||
fn into_trace(self) -> Self::Output {
|
||||
|
||||
@@ -48,6 +48,8 @@ pub enum CreateBlasError {
|
||||
"Limit `max_blas_primitive_count` is {0}, but the BLAS had a maximum of {1} primitives"
|
||||
)]
|
||||
TooManyPrimitives(u32, u32),
|
||||
#[error("AABB geometry stride {0} is invalid (must be >= {1} and a multiple of 8)")]
|
||||
InvalidAabbStride(BufferAddress, BufferAddress),
|
||||
}
|
||||
|
||||
impl WebGpuError for CreateBlasError {
|
||||
@@ -58,7 +60,8 @@ impl WebGpuError for CreateBlasError {
|
||||
Self::MissingIndexData
|
||||
| Self::InvalidVertexFormat(..)
|
||||
| Self::TooManyGeometries(..)
|
||||
| Self::TooManyPrimitives(..) => ErrorType::Validation,
|
||||
| Self::TooManyPrimitives(..)
|
||||
| Self::InvalidAabbStride(..) => ErrorType::Validation,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,6 +184,20 @@ pub enum BuildAccelerationStructureError {
|
||||
"Tlas {0:?} dependent {1:?} is missing AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN"
|
||||
)]
|
||||
TlasDependentMissingVertexReturn(ResourceErrorIdent, ResourceErrorIdent),
|
||||
|
||||
#[error("Blas {0:?} geometry kind at creation does not match build (triangles vs AABBs)")]
|
||||
BlasGeometryKindMismatch(ResourceErrorIdent),
|
||||
|
||||
#[error(
|
||||
"Blas {0:?} build AABB primitive count is greater than creation count (creation: {1}, build: {2})"
|
||||
)]
|
||||
IncompatibleBlasAabbPrimitiveCount(ResourceErrorIdent, u32, u32),
|
||||
|
||||
#[error("Blas {0:?} AABB primitive offset must be a multiple of 8")]
|
||||
UnalignedAabbPrimitiveOffset(ResourceErrorIdent),
|
||||
|
||||
#[error("Blas {0:?} AABB stride is invalid (must be >= {1} and a multiple of 8)")]
|
||||
InvalidAabbStride(ResourceErrorIdent, BufferAddress),
|
||||
}
|
||||
|
||||
impl WebGpuError for BuildAccelerationStructureError {
|
||||
@@ -212,7 +229,11 @@ impl WebGpuError for BuildAccelerationStructureError {
|
||||
| Self::TlasInstanceCountExceeded(..)
|
||||
| Self::TransformMissing(..)
|
||||
| Self::UseTransformMissing(..)
|
||||
| Self::TlasDependentMissingVertexReturn(..) => ErrorType::Validation,
|
||||
| Self::TlasDependentMissingVertexReturn(..)
|
||||
| Self::BlasGeometryKindMismatch(..)
|
||||
| Self::IncompatibleBlasAabbPrimitiveCount(..)
|
||||
| Self::UnalignedAabbPrimitiveOffset(..)
|
||||
| Self::InvalidAabbStride(..) => ErrorType::Validation,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -255,8 +276,17 @@ pub struct BlasTriangleGeometry<'a> {
|
||||
pub transform_buffer_offset: Option<BufferAddress>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BlasAabbGeometry<'a> {
|
||||
pub size: &'a wgt::BlasAABBGeometrySizeDescriptor,
|
||||
pub stride: BufferAddress,
|
||||
pub aabb_buffer: BufferId,
|
||||
pub primitive_offset: u32,
|
||||
}
|
||||
|
||||
pub enum BlasGeometries<'a> {
|
||||
TriangleGeometries(Box<dyn Iterator<Item = BlasTriangleGeometry<'a>> + 'a>),
|
||||
AabbGeometries(Box<dyn Iterator<Item = BlasAabbGeometry<'a>> + 'a>),
|
||||
}
|
||||
|
||||
pub struct BlasBuildEntry<'a> {
|
||||
@@ -330,10 +360,23 @@ pub struct OwnedBlasTriangleGeometry<R: ReferenceType> {
|
||||
pub type ArcBlasTriangleGeometry = OwnedBlasTriangleGeometry<ArcReferences>;
|
||||
pub type TraceBlasTriangleGeometry = OwnedBlasTriangleGeometry<IdReferences>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "serde", apply(serde_object_reference_struct))]
|
||||
pub struct OwnedBlasAabbGeometry<R: ReferenceType> {
|
||||
pub size: wgt::BlasAABBGeometrySizeDescriptor,
|
||||
pub stride: BufferAddress,
|
||||
pub aabb_buffer: R::Buffer,
|
||||
pub primitive_offset: u32,
|
||||
}
|
||||
|
||||
pub type ArcBlasAabbGeometry = OwnedBlasAabbGeometry<ArcReferences>;
|
||||
pub type TraceBlasAabbGeometry = OwnedBlasAabbGeometry<IdReferences>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "serde", apply(serde_object_reference_struct))]
|
||||
pub enum OwnedBlasGeometries<R: ReferenceType> {
|
||||
TriangleGeometries(Vec<OwnedBlasTriangleGeometry<R>>),
|
||||
AabbGeometries(Vec<OwnedBlasAabbGeometry<R>>),
|
||||
}
|
||||
|
||||
pub type ArcBlasGeometries = OwnedBlasGeometries<ArcReferences>;
|
||||
|
||||
@@ -26,6 +26,21 @@ pub struct BlasTriangleGeometrySizeDescriptor {
|
||||
pub flags: AccelerationStructureGeometryFlags,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
/// Descriptor for size-defining attributes of one AABB geometry group in a bottom level acceleration structure.
|
||||
///
|
||||
/// Each primitive is one axis-aligned bounding box, typically stored as two `vec3<f32>` min/max corners.
|
||||
pub struct BlasAABBGeometrySizeDescriptor {
|
||||
/// Number of AABB primitives in this geometry.
|
||||
pub primitive_count: u32,
|
||||
/// Flags for the geometry.
|
||||
pub flags: AccelerationStructureGeometryFlags,
|
||||
}
|
||||
|
||||
/// Minimum stride for AABB geometry (24 bytes: two `vec3<f32>`).
|
||||
pub const AABB_GEOMETRY_MIN_STRIDE: crate::BufferAddress = 24;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
/// Descriptor for all size defining attributes of all geometries inside a bottom level acceleration structure.
|
||||
@@ -35,6 +50,11 @@ pub enum BlasGeometrySizeDescriptors {
|
||||
/// Descriptor for each triangle geometry.
|
||||
descriptors: Vec<BlasTriangleGeometrySizeDescriptor>,
|
||||
},
|
||||
/// AABB geometry version.
|
||||
AABBs {
|
||||
/// Descriptor for each AABB geometry.
|
||||
descriptors: Vec<BlasAABBGeometrySizeDescriptor>,
|
||||
},
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
|
||||
@@ -12,6 +12,13 @@ use crate::{Buffer, Label};
|
||||
pub type BlasTriangleGeometrySizeDescriptor = wgt::BlasTriangleGeometrySizeDescriptor;
|
||||
static_assertions::assert_impl_all!(BlasTriangleGeometrySizeDescriptor: Send, Sync);
|
||||
|
||||
/// Descriptor for the size defining attributes of an AABB geometry group in a bottom level acceleration structure.
|
||||
pub type BlasAABBGeometrySizeDescriptor = wgt::BlasAABBGeometrySizeDescriptor;
|
||||
static_assertions::assert_impl_all!(BlasAABBGeometrySizeDescriptor: Send, Sync);
|
||||
|
||||
/// Minimum stride for AABB geometry (24 bytes: two `vec3<f32>`).
|
||||
pub use wgt::AABB_GEOMETRY_MIN_STRIDE;
|
||||
|
||||
/// Descriptor for the size defining attributes, for a bottom level acceleration structure.
|
||||
pub type BlasGeometrySizeDescriptors = wgt::BlasGeometrySizeDescriptors;
|
||||
static_assertions::assert_impl_all!(BlasGeometrySizeDescriptors: Send, Sync);
|
||||
@@ -116,10 +123,29 @@ pub struct BlasTriangleGeometry<'a> {
|
||||
}
|
||||
static_assertions::assert_impl_all!(BlasTriangleGeometry<'_>: WasmNotSendSync);
|
||||
|
||||
/// Definition for an axis-aligned bounding box geometry group for a bottom level acceleration structure.
|
||||
///
|
||||
/// Buffer data must contain `size.primitive_count` primitives at `primitive_offset`, each `size.stride` bytes,
|
||||
/// with `stride` at least [`AABB_GEOMETRY_MIN_STRIDE`] and a multiple of 8.
|
||||
pub struct BlasAabbGeometry<'a> {
|
||||
/// Sub descriptor for the size defining attributes of this geometry.
|
||||
pub size: &'a BlasAABBGeometrySizeDescriptor,
|
||||
/// Stride in bytes between consecutive AABB primitives in the buffer (at least
|
||||
/// [`AABB_GEOMETRY_MIN_STRIDE`], and must be a multiple of 8).
|
||||
pub stride: wgt::BufferAddress,
|
||||
/// Buffer containing packed AABB primitives (layout determined by `size.stride`).
|
||||
pub aabb_buffer: &'a Buffer,
|
||||
/// Byte offset to the first AABB primitive (must be a multiple of 8).
|
||||
pub primitive_offset: u32,
|
||||
}
|
||||
static_assertions::assert_impl_all!(BlasAabbGeometry<'_>: WasmNotSendSync);
|
||||
|
||||
/// Contains the sets of geometry that go into a [Blas].
|
||||
pub enum BlasGeometries<'a> {
|
||||
/// Triangle geometry variant.
|
||||
TriangleGeometries(Vec<BlasTriangleGeometry<'a>>),
|
||||
/// Procedural AABB geometry variant.
|
||||
AabbGeometries(Vec<BlasAabbGeometry<'a>>),
|
||||
}
|
||||
static_assertions::assert_impl_all!(BlasGeometries<'_>: WasmNotSendSync);
|
||||
|
||||
@@ -230,6 +256,20 @@ pub struct ContextBlasTriangleGeometry<'a> {
|
||||
pub enum ContextBlasGeometries<'a> {
|
||||
/// Triangle geometries.
|
||||
TriangleGeometries(Box<dyn Iterator<Item = ContextBlasTriangleGeometry<'a>> + 'a>),
|
||||
/// AABB geometries.
|
||||
AabbGeometries(Box<dyn Iterator<Item = ContextBlasAabbGeometry<'a>> + 'a>),
|
||||
}
|
||||
|
||||
/// Context version of [BlasAabbGeometry].
|
||||
pub struct ContextBlasAabbGeometry<'a> {
|
||||
#[expect(dead_code)]
|
||||
pub(crate) size: &'a BlasAABBGeometrySizeDescriptor,
|
||||
#[expect(dead_code)]
|
||||
pub(crate) stride: wgt::BufferAddress,
|
||||
#[expect(dead_code)]
|
||||
pub(crate) aabb_buffer: &'a dispatch::DispatchBuffer,
|
||||
#[expect(dead_code)]
|
||||
pub(crate) primitive_offset: u32,
|
||||
}
|
||||
|
||||
/// Context version see [BlasBuildEntry].
|
||||
|
||||
@@ -2837,6 +2837,18 @@ impl dispatch::CommandEncoderInterface for CoreCommandEncoder {
|
||||
});
|
||||
wgc::ray_tracing::BlasGeometries::TriangleGeometries(Box::new(iter))
|
||||
}
|
||||
crate::BlasGeometries::AabbGeometries(ref aabb_geometries) => {
|
||||
let iter =
|
||||
aabb_geometries
|
||||
.iter()
|
||||
.map(|ag| wgc::ray_tracing::BlasAabbGeometry {
|
||||
aabb_buffer: ag.aabb_buffer.inner.as_core().id,
|
||||
stride: ag.stride,
|
||||
size: ag.size,
|
||||
primitive_offset: ag.primitive_offset,
|
||||
});
|
||||
wgc::ray_tracing::BlasGeometries::AabbGeometries(Box::new(iter))
|
||||
}
|
||||
};
|
||||
wgc::ray_tracing::BlasBuildEntry {
|
||||
blas_id: e.blas.inner.as_core().id,
|
||||
|
||||
Reference in new issue
Block a user