How to initialise a Metal render with SDL

Hello,

Sorry if it’s a really dumb question but I was trying to figure out how to initialise SDL with Metal support for MacOS. I already have my game working with OpenGL, I create an SDL window and GLContext and do my own render calls, I want to be able to just initialise SDL to use Metal.

Usually I use

SDL_CreateWindow("window",  SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
SDL_GL_CreateContext(window);

How can I do the same with Metal? I found a SDL_WINDOW_VULKAN but no Metal equivalent.

Thanks for your time.

I imagine you could do it by getting the NSWindow (macOS) or UIWindow (iOS) pointer from SDL via SDL_GetWindowWMinfo(), and then create your own NSView or UIView inside it for use with Metal, and then bootstrap Metal yourself from there.

I imagine you could do it by getting the NSWindow (macOS) or UIWindow
(iOS) pointer from SDL via SDL_GetWindowWMinfo(), and then create your
own NSView or UIView inside it for use with Metal, and then bootstrap
Metal yourself from there.

We don’t expose this in a public API, but I think you can duplicate
what our Metal code for the 2D render API does to set this up:

https://hg.libsdl.org/SDL/file/178b6d6b407f/src/render/metal/SDL_render_metal.m#l1390

The magic happens in Cocoa_Mtl_AddMetalView() …

https://hg.libsdl.org/SDL/file/178b6d6b407f/src/video/cocoa/SDL_cocoametalview.m#l103

–ryan.

Any particular reason there’s no public API for this? I’ve often wondered why Metal hasn’t joined OpenGL and Vulkan as first-class citizens of SDL.

No reason; just hasn’t happened so far. :slight_smile:

1 Like