Re-try with Software Renderer on Mac OS if NSOpenGLPFAAccelerated fails.

I found that I needed to do a special DELETE_TERMINATING_NULL, because
SET_ATTRIB(x, 0) are present elsewhere, and DELETE_ATTRIB(0) is not
the reverse of ADD_ATTRIB(0).

The DELETE_ATTRIB macro is not robust enough - there should be an
UNSET_ATTRIB(a,v).

fixes #2570
fixes #2080
This commit is contained in:
Hin-Tak Leung 2024-06-04 02:30:00 +01:00
parent b35641f4a3
commit ba3ec0ff31

View File

@ -200,6 +200,29 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
assert((size_t) index < sizeof(attribs) / sizeof(attribs[0])); \
attribs[index++] = a; \
}
// DELETE_ATTRIB checks a !=0, as SET_ATTRIB(x,0) are used, and needs
// a special UNSET_ATTRIB(x,0), instead of "DELETE_ATTRIB(x); DELETE_ATTRIB(0)"
#define DELETE_ATTRIB(a) \
{ \
assert(a != 0); \
assert(index > 0); \
int i = 0; \
while (attribs[i] != a && i < index) i++; \
if (i < index) { \
index--; \
while(i < index) { \
attribs[i] = attribs[i+1]; \
i++; \
} \
} \
}
#define DELETE_TERMINATING_NULL \
{ \
assert(index > 0); \
if (attribs[index-1] == 0) { \
index--; \
} \
}
#define SET_ATTRIB(a, v) { ADD_ATTRIB(a); ADD_ATTRIB(v); }
NSOpenGLPixelFormatAttribute attribs[40];
@ -302,20 +325,33 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
// NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
// framebuffer, so there's no need (and no way) to request it
ADD_ATTRIB(0);
#undef ADD_ATTRIB
#undef SET_ATTRIB
ADD_ATTRIB(0); // See also DELETE_TERMINATING_NULL!
window->context.nsgl.pixelFormat =
[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (window->context.nsgl.pixelFormat == nil)
{
_glfwInputError(GLFW_FORMAT_UNAVAILABLE,
"NSGL: Failed to find a suitable pixel format");
return GLFW_FALSE;
// Re-try with Software Renderer
DELETE_ATTRIB(NSOpenGLPFAAccelerated);
DELETE_TERMINATING_NULL; // Unterminate.
ADD_ATTRIB(NSOpenGLPFARendererID);
ADD_ATTRIB(kCGLRendererGenericFloatID);
ADD_ATTRIB(0); // Re-terminate.
window->context.nsgl.pixelFormat =
[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (window->context.nsgl.pixelFormat == nil)
{
_glfwInputError(GLFW_FORMAT_UNAVAILABLE,
"NSGL: Failed to find a suitable pixel format");
return GLFW_FALSE;
}
}
#undef ADD_ATTRIB
#undef DELETE_ATTRIB
#undef DELETE_TERMINATING_NULL
#undef SET_ATTRIB
NSOpenGLContext* share = nil;
if (ctxconfig->share)