Skip to content

Remove unsafe acceleration structure build #7513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Naga now infers the correct binding layout when a resource appears only in an as
#### General

- Removed `MaintainBase` in favor of using `PollType`. By @waywardmonkeys in [#7508](https://github.com/gfx-rs/wgpu/pull/7508).
- Remove `CommandEncoder::build_acceleration_structures_unsafe_tlas` in favour of `as_hal` and apply
simplifications allowed by this. By @Vecvec in [#7513](https://github.com/gfx-rs/wgpu/pull/7513)

## v25.0.1 (2025-04-11)

Expand Down
6 changes: 1 addition & 5 deletions docs/api-specs/ray_tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ can be found with their definitions.
A [`Blas`] can be created with [`Device::create_blas`].
A [`Tlas`] can be created with [`Device::create_tlas`].

Unless one is planning on using the unsafe building API (not recommended for beginners) a [`Tlas`] should be put inside
a [`TlasPackage`]. After that a reference to the [`Tlas`] can be retrieved by calling [`TlasPackage::tlas`].
This reference can be placed in a bind group to be used in a shader. A reference to a [`Blas`] can
The [`Tlas`] reference can be placed in a bind group to be used in a shader. A reference to a [`Blas`] can
be used to create [`TlasInstance`] alongside a transformation matrix, a custom index
(this can be any data that should be given to the shader on a hit) which only the first 24
bits may be set, and a mask to filter hits in the shader.
Expand All @@ -39,8 +37,6 @@ Before a [`Tlas`] is used in a shader it must
[`Tlas`]: https://wgpu.rs/doc/wgpu/struct.Tlas.html
[`Blas`]: https://wgpu.rs/doc/wgpu/struct.Blas.html
[`TlasInstance`]: https://wgpu.rs/doc/wgpu/struct.TlasInstance.html
[`TlasPackage`]: https://wgpu.rs/doc/wgpu/struct.TlasPackage.html
[`TlasPackage::tlas`]: https://wgpu.rs/doc/wgpu/struct.TlasPackage.html#method.tlas

## `naga`'s raytracing API:

Expand Down
16 changes: 7 additions & 9 deletions examples/features/src/ray_cube_compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct Example {
vertex_buf: wgpu::Buffer,
#[expect(dead_code)]
index_buf: wgpu::Buffer,
tlas_package: wgpu::TlasPackage,
tlas: wgpu::Tlas,
compute_pipeline: wgpu::ComputePipeline,
compute_bind_group: wgpu::BindGroup,
blit_pipeline: wgpu::RenderPipeline,
Expand Down Expand Up @@ -253,7 +253,7 @@ impl crate::framework::Example for Example {
},
);

let tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
label: None,
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE,
update_mode: wgpu::AccelerationStructureUpdateMode::Build,
Expand Down Expand Up @@ -342,13 +342,11 @@ impl crate::framework::Example for Example {
],
});

let mut tlas_package = wgpu::TlasPackage::new(tlas);

let dist = 3.0;

for x in 0..side_count {
for y in 0..side_count {
tlas_package[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
tlas[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
&blas,
affine_to_rows(&Affine3A::from_rotation_translation(
Quat::from_rotation_y(45.9_f32.to_radians()),
Expand Down Expand Up @@ -383,7 +381,7 @@ impl crate::framework::Example for Example {
},
]),
}),
iter::once(&tlas_package),
iter::once(&tlas),
);

queue.submit(Some(encoder.finish()));
Expand All @@ -397,7 +395,7 @@ impl crate::framework::Example for Example {
uniform_buf,
vertex_buf,
index_buf,
tlas_package,
tlas,
compute_pipeline,
compute_bind_group,
blit_pipeline,
Expand All @@ -423,7 +421,7 @@ impl crate::framework::Example for Example {

let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;

self.tlas_package[0].as_mut().unwrap().transform =
self.tlas[0].as_mut().unwrap().transform =
affine_to_rows(&Affine3A::from_rotation_translation(
Quat::from_euler(
glam::EulerRot::XYZ,
Expand All @@ -441,7 +439,7 @@ impl crate::framework::Example for Example {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));

{
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
Expand Down
12 changes: 5 additions & 7 deletions examples/features/src/ray_cube_fragment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct Example {
uniforms: Uniforms,
uniform_buf: wgpu::Buffer,
blas: wgpu::Blas,
tlas_package: wgpu::TlasPackage,
tlas: wgpu::Tlas,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
start_inst: Instant,
Expand Down Expand Up @@ -233,8 +233,6 @@ impl crate::framework::Example for Example {
],
});

let tlas_package = wgpu::TlasPackage::new(tlas);

let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

Expand All @@ -255,7 +253,7 @@ impl crate::framework::Example for Example {
]),
}),
// iter::empty(),
iter::once(&tlas_package),
iter::once(&tlas),
);

queue.submit(Some(encoder.finish()));
Expand All @@ -266,7 +264,7 @@ impl crate::framework::Example for Example {
uniforms,
uniform_buf,
blas,
tlas_package,
tlas,
pipeline,
bind_group,
start_inst,
Expand Down Expand Up @@ -306,7 +304,7 @@ impl crate::framework::Example for Example {

for x in 0..side_count {
for y in 0..side_count {
let instance = self.tlas_package.index_mut((x + y * side_count) as usize);
let instance = self.tlas.index_mut((x + y * side_count) as usize);

let x = x as f32 / (side_count - 1) as f32;
let y = y as f32 / (side_count - 1) as f32;
Expand Down Expand Up @@ -338,7 +336,7 @@ impl crate::framework::Example for Example {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));

{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
Expand Down
16 changes: 7 additions & 9 deletions examples/features/src/ray_cube_normals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<F: Future<Output = Option<wgpu::Error>>> Future for ErrorFuture<F> {

struct Example {
rt_target: wgpu::Texture,
tlas_package: wgpu::TlasPackage,
tlas: wgpu::Tlas,
compute_pipeline: wgpu::ComputePipeline,
compute_bind_group: wgpu::BindGroup,
blit_pipeline: wgpu::RenderPipeline,
Expand Down Expand Up @@ -245,7 +245,7 @@ impl crate::framework::Example for Example {
},
);

let tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
label: None,
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE
| wgpu::AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN,
Expand Down Expand Up @@ -335,13 +335,11 @@ impl crate::framework::Example for Example {
],
});

let mut tlas_package = wgpu::TlasPackage::new(tlas);

let dist = 3.0;

for x in 0..side_count {
for y in 0..side_count {
tlas_package[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
tlas[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
&blas,
affine_to_rows(&Affine3A::from_rotation_translation(
Quat::from_rotation_y(45.9_f32.to_radians()),
Expand Down Expand Up @@ -376,7 +374,7 @@ impl crate::framework::Example for Example {
},
]),
}),
iter::once(&tlas_package),
iter::once(&tlas),
);

queue.submit(Some(encoder.finish()));
Expand All @@ -385,7 +383,7 @@ impl crate::framework::Example for Example {

Example {
rt_target,
tlas_package,
tlas,
compute_pipeline,
compute_bind_group,
blit_pipeline,
Expand All @@ -411,7 +409,7 @@ impl crate::framework::Example for Example {

let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;

self.tlas_package[0].as_mut().unwrap().transform =
self.tlas[0].as_mut().unwrap().transform =
affine_to_rows(&Affine3A::from_rotation_translation(
Quat::from_euler(
glam::EulerRot::XYZ,
Expand All @@ -429,7 +427,7 @@ impl crate::framework::Example for Example {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));

{
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
Expand Down
12 changes: 5 additions & 7 deletions examples/features/src/ray_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ fn load_scene(device: &wgpu::Device, queue: &wgpu::Queue) -> SceneComponents {
struct Example {
uniforms: Uniforms,
uniform_buf: wgpu::Buffer,
tlas_package: wgpu::TlasPackage,
tlas: wgpu::Tlas,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
start_inst: Instant,
Expand Down Expand Up @@ -365,8 +365,6 @@ impl crate::framework::Example for Example {
max_instances: side_count * side_count,
});

let tlas_package = wgpu::TlasPackage::new(tlas);

let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
Expand Down Expand Up @@ -409,7 +407,7 @@ impl crate::framework::Example for Example {
},
wgpu::BindGroupEntry {
binding: 5,
resource: tlas_package.as_binding(),
resource: tlas.as_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
Expand All @@ -435,7 +433,7 @@ impl crate::framework::Example for Example {
Example {
uniforms,
uniform_buf,
tlas_package,
tlas,
pipeline,
bind_group,
start_inst,
Expand Down Expand Up @@ -476,7 +474,7 @@ impl crate::framework::Example for Example {

for x in 0..side_count {
for y in 0..side_count {
let instance = self.tlas_package.index_mut(x + y * side_count);
let instance = self.tlas.index_mut(x + y * side_count);

let blas_index = (x + y)
% self
Expand Down Expand Up @@ -518,7 +516,7 @@ impl crate::framework::Example for Example {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));

{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
Expand Down
10 changes: 4 additions & 6 deletions examples/features/src/ray_shadows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl crate::framework::Example for Example {
},
);

let tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
label: None,
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE,
update_mode: wgpu::AccelerationStructureUpdateMode::Build,
Expand Down Expand Up @@ -238,9 +238,7 @@ impl crate::framework::Example for Example {
cache: None,
});

let mut tlas_package = wgpu::TlasPackage::new(tlas);

tlas_package[0] = Some(wgpu::TlasInstance::new(
tlas[0] = Some(wgpu::TlasInstance::new(
&blas,
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
0,
Expand All @@ -266,7 +264,7 @@ impl crate::framework::Example for Example {
},
]),
}),
iter::once(&tlas_package),
iter::once(&tlas),
);

queue.submit(Some(encoder.finish()));
Expand All @@ -281,7 +279,7 @@ impl crate::framework::Example for Example {
},
wgpu::BindGroupEntry {
binding: 1,
resource: tlas_package.as_binding(),
resource: tlas.as_binding(),
},
],
});
Expand Down
Loading