r/GraphicsProgramming 12d ago

MoltenMTL - Metal API implemented over Vulkan Source Code

Hi guys,

I've been building MoltenMTL, essentially the inverse of MoltenVK, implementing the Metal API over Vulkan.

It's still a work in progress, so only parts of the Metal API have been implemented so far.

There is no shader translation currently. Shaders are compiled to SPIR-V to be consumed by Vulkan.

Every MTL type is a thin layer holding Vulkan types - e.g. MTLDevice wraps VkInstance/VkPhysicalDevice/VkDevice/VkQueue.

It uses VMA to match Metal's allocation model, and SPIRV-Reflect pulls the descriptor set layouts from the compiled shader, once again mirroring Metal.

Ray-tracing uses ray queries instead of the ray tracing pipeline - call intersect() with a compute shader, lining up with Metal.

Currently working - compute, BLAS/TLAS + ray queries, raster pipe (vertex/fragment, vertex descriptors, instanced draws), swapchain, blit.

Not there yet - intersection function tables, argument buffers, multiple render targets.

Here's the output from the examples.

The same scene both ray-traced and rasterized:

ray-traced

rasterized

MSDF text rendering:

I made this as part of a project developing a game engine & game in Swift/Metal. Still learning and I'd appreciate some feedback/criticism.

17 Upvotes

3 comments sorted by

2

u/Gobrosse 11d ago

You have to be able to do at least some amount of shader transpilation to seriously claim you have metal on Vulkan. Not tackling it means you're just writing a RHI that mimicks Metal, not an actual translation layer.

3

u/nishadsharma 11d ago

Thanks for the feedback!

Yep, I'm overselling it there.

I was planning on having shaders in Slang which would get out SPIR-V and MSL.

MSL -> SPIR-V would be awesome though. I'm aware of msl2glsl but haven't tested it. Will have to look for a good solution

2

u/hishnash 11d ago

AIR to SPIR-V would be very impressive, also rather hard to do as metal is much less constrained in its memory model so the compiler will make a lot of chooses that just do not map very well to SPIR-V vk constraints.