Skip to content

Commit

Permalink
mac/iOS: allow Metal in windows created without an explicit backend
Browse files Browse the repository at this point in the history
Fixes SDL_CreateWindowAndRenderer (and similar situations) not choosing a Metal backend. See #3991.

Passing an explicit backend into CreateWindow, eg SDL_WINDOW_OPENGL or SDL_WINDOW_METAL, will still prevent the window from being used with other backend types.
  • Loading branch information
slime73 authored and slouken committed Feb 15, 2021
1 parent 9c1871c commit 9b9d0d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/render/metal/SDL_render_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ - (void)dealloc
}

window_flags = SDL_GetWindowFlags(window);
if (!(window_flags & (SDL_WINDOW_METAL|SDL_WINDOW_OPENGL))) {
if (!(window_flags & SDL_WINDOW_METAL)) {
changed_window = SDL_TRUE;
if (SDL_RecreateWindow(window, (window_flags & ~(SDL_WINDOW_VULKAN | SDL_WINDOW_OPENGL)) | SDL_WINDOW_METAL) < 0) {
return NULL;
Expand Down
20 changes: 11 additions & 9 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ SDL_Window *
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
{
SDL_Window *window;
Uint32 graphics_flags = flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_METAL | SDL_WINDOW_VULKAN);

if (!_this) {
/* Initialize the video system if needed */
Expand Down Expand Up @@ -1480,16 +1481,16 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
return NULL;
}

/* Some platforms have OpenGL enabled by default */
/* Some platforms have certain graphics backends enabled by default */
if (!_this->is_dummy && !graphics_flags && !SDL_IsVideoContextExternal()) {
#if (SDL_VIDEO_OPENGL && __MACOSX__) || (__IPHONEOS__ && !TARGET_OS_MACCATALYST) || __ANDROID__ || __NACL__
if (!_this->is_dummy && !(flags & SDL_WINDOW_VULKAN) && !(flags & SDL_WINDOW_METAL) && !SDL_IsVideoContextExternal()) {
flags |= SDL_WINDOW_OPENGL;
}
#elif TARGET_OS_MACCATALYST
if (!_this->is_dummy && !(flags & SDL_WINDOW_VULKAN) && !(flags & SDL_WINDOW_OPENGL) && !SDL_IsVideoContextExternal()) {
#endif
#if SDL_VIDEO_METAL && (TARGET_OS_MACCATALYST || __MACOSX__ || __IPHONEOS__)
flags |= SDL_WINDOW_METAL;
}
#endif
}

if (flags & SDL_WINDOW_OPENGL) {
if (!_this->GL_CreateContext) {
SDL_SetError("OpenGL support is either not configured in SDL "
Expand All @@ -1509,7 +1510,7 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
"(%s) or platform", _this->name);
return NULL;
}
if (flags & SDL_WINDOW_OPENGL) {
if (graphics_flags & SDL_WINDOW_OPENGL) {
SDL_SetError("Vulkan and OpenGL not supported on same window");
return NULL;
}
Expand All @@ -1525,11 +1526,12 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
"(%s) or platform", _this->name);
return NULL;
}
if (flags & SDL_WINDOW_OPENGL) {
/* 'flags' may have default flags appended, don't check against that. */
if (graphics_flags & SDL_WINDOW_OPENGL) {
SDL_SetError("Metal and OpenGL not supported on same window");
return NULL;
}
if (flags & SDL_WINDOW_VULKAN) {
if (graphics_flags & SDL_WINDOW_VULKAN) {
SDL_SetError("Metal and Vulkan not supported on same window. "
"To use MoltenVK, set SDL_WINDOW_VULKAN only.");
return NULL;
Expand Down

0 comments on commit 9b9d0d4

Please sign in to comment.