Skip to content

Commit aa427de

Browse files
authored
Remove unsafe acceleration structure build (#7513)
1 parent 645354a commit aa427de

File tree

23 files changed

+102
-602
lines changed

23 files changed

+102
-602
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Naga now infers the correct binding layout when a resource appears only in an as
9898
- Support BLAS compaction in wgpu. By @Vecvec in [#7285](https://github.com/gfx-rs/wgpu/pull/7285).
9999
- Removed `MaintainBase` in favor of using `PollType`. By @waywardmonkeys in [#7508](https://github.com/gfx-rs/wgpu/pull/7508).
100100
- The `destroy` functions for buffers and textures in wgpu-core are now infallible. Previously, they returned an error if called multiple times for the same object. This only affects the wgpu-core API; the wgpu API already allowed multiple `destroy` calls. By @andyleiserson in [#7686](https://github.com/gfx-rs/wgpu/pull/7686) and [#7720](https://github.com/gfx-rs/wgpu/pull/7720).
101+
- Remove `CommandEncoder::build_acceleration_structures_unsafe_tlas` in favour of `as_hal` and apply
102+
simplifications allowed by this. By @Vecvec in [#7513](https://github.com/gfx-rs/wgpu/pull/7513)
101103

102104
#### Naga
103105

docs/api-specs/ray_tracing.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ can be found with their definitions.
2121
A [`Blas`] can be created with [`Device::create_blas`].
2222
A [`Tlas`] can be created with [`Device::create_tlas`].
2323

24-
Unless one is planning on using the unsafe building API (not recommended for beginners) a [`Tlas`] should be put inside
25-
a [`TlasPackage`]. After that a reference to the [`Tlas`] can be retrieved by calling [`TlasPackage::tlas`].
26-
This reference can be placed in a bind group to be used in a shader. A reference to a [`Blas`] can
24+
The [`Tlas`] reference can be placed in a bind group to be used in a shader. A reference to a [`Blas`] can
2725
be used to create [`TlasInstance`] alongside a transformation matrix, a custom index
2826
(this can be any data that should be given to the shader on a hit) which only the first 24
2927
bits may be set, and a mask to filter hits in the shader.
@@ -86,8 +84,6 @@ fn render(/*whatever args you need to render*/) {
8684
[`Tlas`]: https://wgpu.rs/doc/wgpu/struct.Tlas.html
8785
[`Blas`]: https://wgpu.rs/doc/wgpu/struct.Blas.html
8886
[`TlasInstance`]: https://wgpu.rs/doc/wgpu/struct.TlasInstance.html
89-
[`TlasPackage`]: https://wgpu.rs/doc/wgpu/struct.TlasPackage.html
90-
[`TlasPackage::tlas`]: https://wgpu.rs/doc/wgpu/struct.TlasPackage.html#method.tlas
9187
[`Blas::prepare_compaction_async`]: https://wgpu.rs/doc/wgpu/struct.Blas.html#method.prepare_compaction_async
9288
[`Blas::ready_for_compaction`]: https://wgpu.rs/doc/wgpu/struct.Blas.html#method.ready_for_compaction
9389
[`Queue::compact_blas`]: https://wgpu.rs/doc/wgpu/struct.Queue.html#method.compact_blas

examples/features/src/ray_cube_compute/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct Example {
132132
vertex_buf: wgpu::Buffer,
133133
#[expect(dead_code)]
134134
index_buf: wgpu::Buffer,
135-
tlas_package: wgpu::TlasPackage,
135+
tlas: wgpu::Tlas,
136136
compute_pipeline: wgpu::ComputePipeline,
137137
compute_bind_group: wgpu::BindGroup,
138138
blit_pipeline: wgpu::RenderPipeline,
@@ -259,7 +259,7 @@ impl crate::framework::Example for Example {
259259
},
260260
);
261261

262-
let tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
262+
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
263263
label: None,
264264
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE,
265265
update_mode: wgpu::AccelerationStructureUpdateMode::Build,
@@ -348,13 +348,11 @@ impl crate::framework::Example for Example {
348348
],
349349
});
350350

351-
let mut tlas_package = wgpu::TlasPackage::new(tlas);
352-
353351
let dist = 3.0;
354352

355353
for x in 0..side_count {
356354
for y in 0..side_count {
357-
tlas_package[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
355+
tlas[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
358356
&blas,
359357
affine_to_rows(&Affine3A::from_rotation_translation(
360358
Quat::from_rotation_y(45.9_f32.to_radians()),
@@ -389,7 +387,7 @@ impl crate::framework::Example for Example {
389387
},
390388
]),
391389
}),
392-
iter::once(&tlas_package),
390+
iter::once(&tlas),
393391
);
394392

395393
queue.submit(Some(encoder.finish()));
@@ -401,7 +399,7 @@ impl crate::framework::Example for Example {
401399
uniform_buf,
402400
vertex_buf,
403401
index_buf,
404-
tlas_package,
402+
tlas,
405403
compute_pipeline,
406404
compute_bind_group,
407405
blit_pipeline,
@@ -427,7 +425,7 @@ impl crate::framework::Example for Example {
427425

428426
let anim_time = self.animation_timer.time();
429427

430-
self.tlas_package[0].as_mut().unwrap().transform =
428+
self.tlas[0].as_mut().unwrap().transform =
431429
affine_to_rows(&Affine3A::from_rotation_translation(
432430
Quat::from_euler(
433431
glam::EulerRot::XYZ,
@@ -445,7 +443,7 @@ impl crate::framework::Example for Example {
445443
let mut encoder =
446444
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
447445

448-
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
446+
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));
449447

450448
{
451449
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {

examples/features/src/ray_cube_fragment/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct Example {
9999
uniforms: Uniforms,
100100
uniform_buf: wgpu::Buffer,
101101
blas: wgpu::Blas,
102-
tlas_package: wgpu::TlasPackage,
102+
tlas: wgpu::Tlas,
103103
pipeline: wgpu::RenderPipeline,
104104
bind_group: wgpu::BindGroup,
105105
animation_timer: utils::AnimationTimer,
@@ -238,8 +238,6 @@ impl crate::framework::Example for Example {
238238
],
239239
});
240240

241-
let tlas_package = wgpu::TlasPackage::new(tlas);
242-
243241
let mut encoder =
244242
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
245243

@@ -260,7 +258,7 @@ impl crate::framework::Example for Example {
260258
]),
261259
}),
262260
// iter::empty(),
263-
iter::once(&tlas_package),
261+
iter::once(&tlas),
264262
);
265263

266264
queue.submit(Some(encoder.finish()));
@@ -269,7 +267,7 @@ impl crate::framework::Example for Example {
269267
uniforms,
270268
uniform_buf,
271269
blas,
272-
tlas_package,
270+
tlas,
273271
pipeline,
274272
bind_group,
275273
animation_timer: utils::AnimationTimer::default(),
@@ -309,7 +307,7 @@ impl crate::framework::Example for Example {
309307

310308
for x in 0..side_count {
311309
for y in 0..side_count {
312-
let instance = self.tlas_package.index_mut((x + y * side_count) as usize);
310+
let instance = self.tlas.index_mut((x + y * side_count) as usize);
313311

314312
let x = x as f32 / (side_count - 1) as f32;
315313
let y = y as f32 / (side_count - 1) as f32;
@@ -341,7 +339,7 @@ impl crate::framework::Example for Example {
341339
let mut encoder =
342340
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
343341

344-
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
342+
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));
345343

346344
{
347345
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {

examples/features/src/ray_cube_normals/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<F: Future<Output = Option<wgpu::Error>>> Future for ErrorFuture<F> {
121121

122122
struct Example {
123123
rt_target: wgpu::Texture,
124-
tlas_package: wgpu::TlasPackage,
124+
tlas: wgpu::Tlas,
125125
compute_pipeline: wgpu::ComputePipeline,
126126
compute_bind_group: wgpu::BindGroup,
127127
blit_pipeline: wgpu::RenderPipeline,
@@ -250,7 +250,7 @@ impl crate::framework::Example for Example {
250250
},
251251
);
252252

253-
let tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
253+
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
254254
label: None,
255255
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE
256256
| wgpu::AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN,
@@ -340,13 +340,11 @@ impl crate::framework::Example for Example {
340340
],
341341
});
342342

343-
let mut tlas_package = wgpu::TlasPackage::new(tlas);
344-
345343
let dist = 3.0;
346344

347345
for x in 0..side_count {
348346
for y in 0..side_count {
349-
tlas_package[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
347+
tlas[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
350348
&blas,
351349
affine_to_rows(&Affine3A::from_rotation_translation(
352350
Quat::from_rotation_y(45.9_f32.to_radians()),
@@ -381,14 +379,14 @@ impl crate::framework::Example for Example {
381379
},
382380
]),
383381
}),
384-
iter::once(&tlas_package),
382+
iter::once(&tlas),
385383
);
386384

387385
queue.submit(Some(encoder.finish()));
388386

389387
Example {
390388
rt_target,
391-
tlas_package,
389+
tlas,
392390
compute_pipeline,
393391
compute_bind_group,
394392
blit_pipeline,
@@ -414,7 +412,7 @@ impl crate::framework::Example for Example {
414412

415413
let anim_time = self.animation_timer.time();
416414

417-
self.tlas_package[0].as_mut().unwrap().transform =
415+
self.tlas[0].as_mut().unwrap().transform =
418416
affine_to_rows(&Affine3A::from_rotation_translation(
419417
Quat::from_euler(
420418
glam::EulerRot::XYZ,
@@ -432,7 +430,7 @@ impl crate::framework::Example for Example {
432430
let mut encoder =
433431
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
434432

435-
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
433+
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));
436434

437435
{
438436
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {

examples/features/src/ray_scene/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ fn load_scene(device: &wgpu::Device, queue: &wgpu::Queue) -> SceneComponents {
308308
struct Example {
309309
uniforms: Uniforms,
310310
uniform_buf: wgpu::Buffer,
311-
tlas_package: wgpu::TlasPackage,
311+
tlas: wgpu::Tlas,
312312
pipeline: wgpu::RenderPipeline,
313313
bind_group: wgpu::BindGroup,
314314
scene_components: SceneComponents,
@@ -370,8 +370,6 @@ impl crate::framework::Example for Example {
370370
max_instances: side_count * side_count,
371371
});
372372

373-
let tlas_package = wgpu::TlasPackage::new(tlas);
374-
375373
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
376374
label: None,
377375
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
@@ -414,7 +412,7 @@ impl crate::framework::Example for Example {
414412
},
415413
wgpu::BindGroupEntry {
416414
binding: 5,
417-
resource: tlas_package.as_binding(),
415+
resource: tlas.as_binding(),
418416
},
419417
wgpu::BindGroupEntry {
420418
binding: 1,
@@ -438,7 +436,7 @@ impl crate::framework::Example for Example {
438436
Example {
439437
uniforms,
440438
uniform_buf,
441-
tlas_package,
439+
tlas,
442440
pipeline,
443441
bind_group,
444442
scene_components,
@@ -479,7 +477,7 @@ impl crate::framework::Example for Example {
479477

480478
for x in 0..side_count {
481479
for y in 0..side_count {
482-
let instance = self.tlas_package.index_mut(x + y * side_count);
480+
let instance = self.tlas.index_mut(x + y * side_count);
483481

484482
let blas_index = (x + y)
485483
% self
@@ -521,7 +519,7 @@ impl crate::framework::Example for Example {
521519
let mut encoder =
522520
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
523521

524-
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas_package));
522+
encoder.build_acceleration_structures(iter::empty(), iter::once(&self.tlas));
525523

526524
{
527525
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {

examples/features/src/ray_shadows/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl crate::framework::Example for Example {
170170
},
171171
);
172172

173-
let tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
173+
let mut tlas = device.create_tlas(&wgpu::CreateTlasDescriptor {
174174
label: None,
175175
flags: wgpu::AccelerationStructureFlags::PREFER_FAST_TRACE,
176176
update_mode: wgpu::AccelerationStructureUpdateMode::Build,
@@ -244,9 +244,7 @@ impl crate::framework::Example for Example {
244244
cache: None,
245245
});
246246

247-
let mut tlas_package = wgpu::TlasPackage::new(tlas);
248-
249-
tlas_package[0] = Some(wgpu::TlasInstance::new(
247+
tlas[0] = Some(wgpu::TlasInstance::new(
250248
&blas,
251249
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
252250
0,
@@ -272,7 +270,7 @@ impl crate::framework::Example for Example {
272270
},
273271
]),
274272
}),
275-
iter::once(&tlas_package),
273+
iter::once(&tlas),
276274
);
277275

278276
queue.submit(Some(encoder.finish()));
@@ -287,7 +285,7 @@ impl crate::framework::Example for Example {
287285
},
288286
wgpu::BindGroupEntry {
289287
binding: 1,
290-
resource: tlas_package.as_binding(),
288+
resource: tlas.as_binding(),
291289
},
292290
],
293291
});

0 commit comments

Comments
 (0)