SDL 2 and Metal - how to get the MTKView?

Is there a way to get the MTKView when using Metal? I saw that you can grab the underlying CAMetalLayer from SDL_cocoametalview but it looks like there is no direct support to get the MTKView? Any hints about how to do that?

A MTKView is a high level wrapper over a metal layer and some existing metal functionality. SDL doesn’t use it.

Ok then my understanding is correct but this poses a problem as I am trying to integrate with Sokol and it requires the depth stencil texture and the multisample color texture of the framebuffer and they both are exposed by the MTKView and I couldn’t find any way to get them starting from the CAMetalLayer.

You’d need to create and manage those yourself I think. Maybe sokol accepts nil values for them, since not every app will have them?

Unfortunately that is not the case. You have to provide them during the setup of any pipeline that writes to the framebuffer.

I will have to look at what SDL2 is doing when initializing the window and view when running on Cocoa (and eventually UIKit in order to support iOS, too).
I believe the easiest way would then be to throw away the SDLCocoaView and create a new MTKView from scratch and assign it to the Cocoa window.
But right now I do not know exactly how that works and I will need to look it up in Apple’s documentation and on top of that figure out what I am going to break in the SDL implementation.

Unfortunately that is not the case. You have to provide them during the setup of any pipeline that writes to the framebuffer.

Requiring non-nil textures doesn’t sound right, most games don’t use MSAA so they would have no multisample color texture to provide.

True, but depth stencil texture would be needed for anything which is not strictly 2D. Anyhow, I did try to skip passing those values (they are nil by default) and you can still setup the pipeline. Which is fine as I am working on some 2D stuff, so I can at least try to see if I can get something on the screen.

But the behavior now is that it looks like it tries to render the correct geometry using the right source texture but the result is that nothing is being drawn. Not even on offscreen textures. But that might be something else that I have been overlooking.

It seems like the pipeline is clearing the screen with any given color, but when it comes to drawing any geometry, nothing gets on screen.

I must also admit that instead of passing [MTKView currentDrawable], which is what Sokol is expecting, I am passing [CAMetalLayer nextDrawable] as the swapchain current drawable.
But that should not really be an issue.

Inspecting the Metal pipeline with Xcode shows that even the offscreen texture is not being rendered so it’s not a matter of how the swapchain has been setup.

I’ve never used Sokol, but the documentation for sokol_gfx.h specifically says the depth buffer and MSAA textures are optional.

edit: as others have said, MTK (Metal ToolKit) is just a wrapper to make some aspects of Metal easier to set up, at the cost of some flexibility. SDL doesn’t use it.

They are optional but you would need at least the depth buffer if you want to work with anything 3D. For 2D stuff you can live without that, true.