Skip to content

[hal metal] ray tracing acceleration structures#8071

Merged
Vecvec merged 25 commits intogfx-rs:trunkfrom
Lichtso:objc2_metal/ray_tracing_acceleration_structures
Feb 18, 2026
Merged

[hal metal] ray tracing acceleration structures#8071
Vecvec merged 25 commits intogfx-rs:trunkfrom
Lichtso:objc2_metal/ray_tracing_acceleration_structures

Conversation

@Lichtso
Copy link
Copy Markdown
Contributor

@Lichtso Lichtso commented Aug 9, 2025

Connections
Fixes: #7402
Depends on: #5641
Supersedes: #7660

Description
Implements the missing ray tracing acceleration structures in the HAL metal backend.

Testing
The examples ray_scene, ray_shadows, ray_cube_compute, ray_cube_fragment and ray_traced_triangle all work.
That is if invoked via cargo run --bin wgpu-examples ray_traced_triangle, but not via cargo xtask test ray_traced_triangle, still current CI runner is too old to catch that as it does not support hardware ray tracing.

Squash or Rebase?
Squash

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests.
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

@Lichtso Lichtso requested a review from a team as a code owner August 9, 2025 13:19
@Lichtso Lichtso force-pushed the objc2_metal/ray_tracing_acceleration_structures branch from e1218ce to edeaaba Compare August 9, 2025 13:21
Copy link
Copy Markdown
Contributor

@Vecvec Vecvec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the migrated things look good, but I'm not sure the residency sets would work in complex scenarios. Please correct me if I've gotten anything wrong.

@Lichtso
Copy link
Copy Markdown
Contributor Author

Lichtso commented Aug 10, 2025

Open problems remaining:

  • How to solve the issue with the tests not passing because the metal debug layer breaks acceleration structures if no window is present.
  • When to call residency_set.endResidency()? Possibly when the next set_bind_group() call happens.
  • What to do about a bindless usage of TLAS and its residency_set.

@Lichtso Lichtso force-pushed the objc2_metal/ray_tracing_acceleration_structures branch 2 times, most recently from 1244389 to aa6be88 Compare August 10, 2025 16:04
@Vecvec
Copy link
Copy Markdown
Contributor

Vecvec commented Aug 10, 2025

@Lichtso I think that we think of MTLResidencySets differently. I think you think of them like a encoder, where you stage commands and then commit them to run those commands. I think of them like an array of resources which get made resident when commit gets called, and requestResidency tells the driver that you are likely to try to make these resource resident, and to maybe try make them resident if it can so you don't have to block on residency commit.

@Vecvec
Copy link
Copy Markdown
Contributor

Vecvec commented Aug 10, 2025

residencysets

I've drawn an image of what I think could happen based on my understanding of residency sets. I've used green to be user space and red to be device space.

Edit: it's not very clear, but the resources in the residency set are only made resident at queue submit (when it gets committed), not when they are added to the residency set

@MarijnS95
Copy link
Copy Markdown
Contributor

MarijnS95 commented Aug 10, 2025

Yes, supposedly MTLResidencySet doesn't make a "shadow copy" whenever it is attached to anything. Instead commit() simply applies all the add/remove changes, that any command buffer to which it is attached can subsequently read to know which resources need to be made or kept resident. (Or the rest of the system can read to evict live resources when starved for memory, based on running command buffers?).

In other words it should be fine to remove live resources from a residency set, as long as those are not committed until all pending command buffers that are still referencing these resources have finished.


@Vecvec afaik the device space you have drawn isn't affected until a command buffer referencing a certain MTLResidencySet is submitted and starts running (and it'll reference the most recently committed state), or explicit residency is requested ahead of time using requestResidency() (which doesn't preclude attaching it to a queue or command buffer).

@Vecvec
Copy link
Copy Markdown
Contributor

Vecvec commented Aug 10, 2025

@Vecvec afaik the device space you have drawn isn't affected until a command buffer referencing a certain MTLResidencySet is submitted and starts running (and it'll reference the most recently committed state), or explicit residency is requested ahead of time using requestResidency() (which doesn't preclude attaching it to a queue or command buffer).

I don't think it was very clear that what is put in the residency set is not the same as what is made resident, I was meaning calling addAllocation which does happen in the encoder calls.

@Vecvec
Copy link
Copy Markdown
Contributor

Vecvec commented Sep 5, 2025

I recently also found out that residency set methods aren't thread safe, meaning that a mutex is needed around the residency set.

@Vecvec
Copy link
Copy Markdown
Contributor

Vecvec commented Oct 15, 2025

@Lichtso, just following up on this. Do you have any updates?

@Lichtso
Copy link
Copy Markdown
Contributor Author

Lichtso commented Oct 15, 2025

Haven't worked on this any further and objc2-metal is also not merged yet.
But yes, I think you are right that MTLResidencySet won't work in this way and we have to come up with a different approach or modify it somehow.

@Vecvec
Copy link
Copy Markdown
Contributor

Vecvec commented Oct 17, 2025

@Lichtso I would recommend putting the residency set in the HAL command buffer, and having a function to add to it (and possibly this could also request_residency). This allows wgpu-core to add to the set when it is in validate_acceleration_structure_actions before submit and so needs to be tracked (the unsafe function mark_acceleration_structures_built doesn't set this, so it might need to have a line stating that the BLAS must be resident on metal when being used).

@Lichtso Lichtso force-pushed the objc2_metal/ray_tracing_acceleration_structures branch from aa6be88 to 0231aa8 Compare November 27, 2025 17:15
@Lichtso
Copy link
Copy Markdown
Contributor Author

Lichtso commented Feb 9, 2026

Seems the objc2-metal PR was merged, time to rebase this then.

@Lichtso Lichtso force-pushed the objc2_metal/ray_tracing_acceleration_structures branch 5 times, most recently from 9bf31db to 5c62287 Compare February 15, 2026 17:50
@Lichtso Lichtso force-pushed the objc2_metal/ray_tracing_acceleration_structures branch from 5c62287 to 1a240b9 Compare February 15, 2026 17:58
@Lichtso Lichtso requested a review from Vecvec February 15, 2026 18:14
@Lichtso
Copy link
Copy Markdown
Contributor Author

Lichtso commented Feb 15, 2026

I would recommend putting the residency set in the HAL command buffer, and having a function to add to it

@Vecvec I think I found a workable solution. See last commit: 1a240b9

Copy link
Copy Markdown
Contributor

@Vecvec Vecvec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I have a few comments, but nothing major.

@Vecvec Vecvec merged commit 05861da into gfx-rs:trunk Feb 18, 2026
58 checks passed
@Lichtso Lichtso deleted the objc2_metal/ray_tracing_acceleration_structures branch February 18, 2026 19:41
@Lichtso Lichtso mentioned this pull request Feb 18, 2026
42 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Ray Tracing on Metal

4 participants