Skip to content

Implement Ray Tracing on Metal #7402

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
zmarlon opened this issue Mar 23, 2025 · 6 comments · May be fixed by #7660
Open

Implement Ray Tracing on Metal #7402

zmarlon opened this issue Mar 23, 2025 · 6 comments · May be fixed by #7660
Labels
backend: metal Issues with Metal feature: raytracing Issues with the Ray Tracing Native Feature type: enhancement New feature or request

Comments

@zmarlon
Copy link

zmarlon commented Mar 23, 2025

Raytracing is not yet implemented for Metal? Is it currently being worked on? If not, I might take a look at it. Is there an example that could be tested?

@Vecvec
Copy link
Collaborator

Vecvec commented Mar 24, 2025

See the ray_cube_compute, ray_cube_fragment examples which use the wgpu api built on top of the wgpu-hal api.

@cwfitzgerald cwfitzgerald changed the title Implenet RayTracing in Wgpu Hal for Metal Implement Ray Tracing on Metal Mar 24, 2025
@cwfitzgerald cwfitzgerald added type: enhancement New feature or request backend: metal Issues with Metal feature: raytracing Issues with the Ray Tracing Native Feature labels Apr 9, 2025
@Lichtso
Copy link
Contributor

Lichtso commented Apr 30, 2025

I implemented it for the most part: https://github.com/Lichtso/wgpu/tree/metal/ray_tracing_acceleration_structures
But it does not work yet. It can be compiled and cargo run --bin wgpu-examples ray_traced_triangle runs, but there are no intersections / hits. Probably missing a encoder.use_resource(blas); somewhere.

A challenge I encontered is that metal does not use raw GPU pointers like all other backends. Instead one has to build the BLASes first and then add a list of their handles to the TLAS descriptor to build it. Then the instances in the TLAS can reference those listed BLASes by index. This could require a rework of the AccelerationStructureInstances across all backends I think.

And then there are these other annoyances like how the flags and the barriers are handled.

@Vecvec
Copy link
Collaborator

Vecvec commented Apr 30, 2025

This could require a rework of the AccelerationStructureInstances across all backends I think

Maybe try MTLIndirectInstanceAccelerationStructureDescriptor? https://developer.apple.com/documentation/metal/mtlindirectinstanceaccelerationstructuredescriptor.

Edit: Also see https://developer.apple.com/documentation/metal/mtlindirectaccelerationstructureinstancedescriptor.
Edit 2: It seems to (unclear from docs) be possible to continue using MTLAccelerationStructureInstanceDescriptor without setting instancedAccelerationStructures by setting instanceDescriptorType to indirect and using MTLIndirectAccelerationStructureInstanceDescriptor in the buffer.

@Lichtso
Copy link
Contributor

Lichtso commented May 2, 2025

Great idea, I will give it a shot after I got something working. I fixed a few more things, the metal API validation and the msl shader validation pass, but so far there still are no intersections ever at all :(

At least I can now see the BLAS, the TLAS instantiations thereof and the shaders acceleration structure buffer binding being active in XCode: https://developer.apple.com/documentation/xcode/inspecting-acceleration-structures

Still, the actual shader produces no hits even if I make all parameters the least restrictive possible (like using a negative minimal distance, forcing opacity and full hit mask etc.).

@Vecvec
Copy link
Collaborator

Vecvec commented May 2, 2025

The only other thing I could find is that you bitshift custom_data into the mask (I don't think this would fix it but maybe try?). IIUC rather confusingly, I think that the place custom data should go is in UserID (in MTLAccelerationStructureUserIDInstanceDescriptor, or in the indirect descriptor). This isn't actually the ID of a user but a user provided ID for the instance. This has a confusing name, but is 8 bits sent to the shader and not used anywhere else which is what we need. The only other idea I have is that the rays are going is the wrong direction (e.g. 90 degrees off). Have you tried aligning the acceleration structure viewer camera to the ray direction?

@Lichtso Lichtso linked a pull request May 2, 2025 that will close this issue
5 tasks
@Lichtso
Copy link
Contributor

Lichtso commented May 2, 2025

Well, turns out it was already working. I just had Xcode configured not to run cargo build, so I was looking at an outdated build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: metal Issues with Metal feature: raytracing Issues with the Ray Tracing Native Feature type: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants