From 5bea8d4d20abd284a2bc9c7169f92c5a6f8592aa Mon Sep 17 00:00:00 2001 From: Doug Binks Date: Wed, 26 Dec 2018 17:07:43 +0100 Subject: [PATCH] Removed enum and only add ARB/EXT attributes if required. --- src/wgl_context.c | 163 +++++++++++++++++++++++++++------------------- 1 file changed, 96 insertions(+), 67 deletions(-) diff --git a/src/wgl_context.c b/src/wgl_context.c index 900a0cd05..02f84eae5 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -31,48 +31,12 @@ #include #include - -// Pixel format enum -typedef enum glfwEnumPixelFormatAttribs -{ - GLFW_PFA_WGL_NUMBER_PIXEL_FORMATS_ARB, - GLFW_PFA_WGL_DRAW_TO_WINDOW_ARB, - GLFW_PFA_WGL_ACCELERATION_ARB, - GLFW_PFA_WGL_SUPPORT_OPENGL_ARB, - GLFW_PFA_WGL_DOUBLE_BUFFER_ARB, - GLFW_PFA_WGL_STEREO_ARB, - GLFW_PFA_WGL_PIXEL_TYPE_ARB, - GLFW_PFA_WGL_RED_BITS_ARB, - GLFW_PFA_WGL_RED_SHIFT_ARB, - GLFW_PFA_WGL_GREEN_BITS_ARB, - GLFW_PFA_WGL_GREEN_SHIFT_ARB, - GLFW_PFA_WGL_BLUE_BITS_ARB, - GLFW_PFA_WGL_BLUE_SHIFT_ARB, - GLFW_PFA_WGL_ALPHA_BITS_ARB, - GLFW_PFA_WGL_ALPHA_SHIFT_ARB, - GLFW_PFA_WGL_ACCUM_BITS_ARB, - GLFW_PFA_WGL_ACCUM_RED_BITS_ARB, - GLFW_PFA_WGL_ACCUM_GREEN_BITS_ARB, - GLFW_PFA_WGL_ACCUM_BLUE_BITS_ARB, - GLFW_PFA_WGL_ACCUM_ALPHA_BITS_ARB, - GLFW_PFA_WGL_DEPTH_BITS_ARB, - GLFW_PFA_WGL_STENCIL_BITS_ARB, - GLFW_PFA_WGL_AUX_BUFFERS_ARB, - GLFW_PFA_WGL_SAMPLES_ARB, - GLFW_PFA_WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, - GLFW_PFA_WGL_COLORSPACE_EXT, - - GLFW_PFA_COUNT // Add values above this line. -} glfwEnumPixelFormatAttribs; - -static int glfwPixelFormatAttribs[ GLFW_PFA_COUNT ] = { - WGL_NUMBER_PIXEL_FORMATS_ARB, - WGL_DRAW_TO_WINDOW_ARB, - WGL_ACCELERATION_ARB, +// array of pixel format attributes we need, order in terms of access via getPixelFormatAttribFromArray +static const int glfwPixelFormatAttribs[] = { WGL_SUPPORT_OPENGL_ARB, - WGL_DOUBLE_BUFFER_ARB, - WGL_STEREO_ARB, + WGL_DRAW_TO_WINDOW_ARB, WGL_PIXEL_TYPE_ARB, + WGL_ACCELERATION_ARB, WGL_RED_BITS_ARB, WGL_RED_SHIFT_ARB, WGL_GREEN_BITS_ARB, @@ -81,30 +45,66 @@ static int glfwPixelFormatAttribs[ GLFW_PFA_COUNT ] = { WGL_BLUE_SHIFT_ARB, WGL_ALPHA_BITS_ARB, WGL_ALPHA_SHIFT_ARB, + WGL_DEPTH_BITS_ARB, + WGL_STENCIL_BITS_ARB, WGL_ACCUM_BITS_ARB, WGL_ACCUM_RED_BITS_ARB, WGL_ACCUM_GREEN_BITS_ARB, WGL_ACCUM_BLUE_BITS_ARB, WGL_ACCUM_ALPHA_BITS_ARB, - WGL_DEPTH_BITS_ARB, - WGL_STENCIL_BITS_ARB, WGL_AUX_BUFFERS_ARB, + WGL_STEREO_ARB, + WGL_DOUBLE_BUFFER_ARB, + // ARB EXT attribs only below this line, see initPixelFormatAttribArry WGL_SAMPLES_ARB, WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, WGL_COLORSPACE_EXT }; +// initialize array attribs with formatattribs available +// attribs array must be at least of size sizeof(glfwPixelFormatAttribs) / sizeof(glfwPixelFormatAttribs[0]) +// returns number of valid elemets in array +static int initPixelFormatAttribArry( int* attribs, int api ) +{ + const int numARBEXTAtrribs = 3; // Update this value when adding ARB EXT attribs + int numAttribs = sizeof(glfwPixelFormatAttribs) / sizeof(glfwPixelFormatAttribs[0]) - numARBEXTAtrribs; + memcpy(attribs, glfwPixelFormatAttribs, sizeof(glfwPixelFormatAttribs)); + + if (_glfw.wgl.ARB_multisample) + { + attribs[numAttribs++] = WGL_SAMPLES_ARB; + } + + if (api == GLFW_OPENGL_API) + { + if (_glfw.wgl.ARB_framebuffer_sRGB || + _glfw.wgl.EXT_framebuffer_sRGB) + { + attribs[numAttribs++] = WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB; + } + } + else + { + if (_glfw.wgl.EXT_colorspace) + { + attribs[numAttribs++] = WGL_COLORSPACE_EXT; + } + } + return numAttribs; +} + // Returns all formats in glfwPixelFormats attribute of the specified pixel format // values should be of size GLFW_PFA_COUNT and on return will hold integer values // which can be indexed using glfwEnumPixelFormatAttribs // returns 1 on success, 0 on failure -static int getPixelFormatAttribs(_GLFWwindow* window, int pixelFormat, int* values ) +static int getPixelFormatAttribs(_GLFWwindow* window, int pixelFormat, int numAttribs, int* attribs, int* values ) { assert(_glfw.wgl.ARB_pixel_format); if (!_glfw.wgl.GetPixelFormatAttribivARB(window->context.wgl.dc, pixelFormat, - 0, GLFW_PFA_COUNT, glfwPixelFormatAttribs, values)) + 0, numAttribs, + attribs, values)) { _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, "WGL: Failed to retrieve pixel format attributes"); @@ -114,6 +114,31 @@ static int getPixelFormatAttribs(_GLFWwindow* window, int pixelFormat, int* valu return 1; } +static int getPixelFormatAttribFromArray( int* attribs, int* values, int* hintPos, int attrib ) +{ + int start = *hintPos; + while ( *hintPos < sizeof(glfwPixelFormatAttribs) / sizeof(glfwPixelFormatAttribs[0]) ) + { + if ( attribs[ *hintPos ] == attrib ) + return values[ (*hintPos)++ ]; + ++(*hintPos); + } + + // hint did not work, restart search from beginning + *hintPos = 0; + while ( *hintPos < start ) + { + if ( attribs[ *hintPos ] == attrib ) + return values[ (*hintPos)++ ]; + ++(*hintPos); + } + + // error + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, + "WGL: Unknown attribute requested from attribute array"); + return 0; +} + // Returns the specified attribute of the specified pixel format // static int getPixelFormatAttrib(_GLFWwindow* window, int pixelFormat, int attrib) @@ -142,8 +167,11 @@ static int choosePixelFormat(_GLFWwindow* window, { _GLFWfbconfig* usableConfigs; const _GLFWfbconfig* closest; - int i, pixelFormat, nativeCount, usableCount; - int pixelFormatAttribs[GLFW_PFA_COUNT]; + int i, pixelFormat, nativeCount, usableCount, numAttribs, hintPos; + int pfAttribs[sizeof(glfwPixelFormatAttribs) / sizeof(glfwPixelFormatAttribs[0])]; + int pfValues[sizeof(glfwPixelFormatAttribs) / sizeof(glfwPixelFormatAttribs[0])]; + + numAttribs = initPixelFormatAttribArry(pfAttribs, ctxconfig->client); if (_glfw.wgl.ARB_pixel_format) { @@ -170,55 +198,56 @@ static int choosePixelFormat(_GLFWwindow* window, if (_glfw.wgl.ARB_pixel_format) { // Get pixel format attributes through "modern" extension - getPixelFormatAttribs(window, n, pixelFormatAttribs ); + getPixelFormatAttribs(window, n, numAttribs, pfAttribs, pfValues ); + hintPos = 0; - if (!pixelFormatAttribs[GLFW_PFA_WGL_SUPPORT_OPENGL_ARB] || - !pixelFormatAttribs[GLFW_PFA_WGL_DRAW_TO_WINDOW_ARB]) + if (!getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_SUPPORT_OPENGL_ARB) || + !getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_DRAW_TO_WINDOW_ARB)) { continue; } - if (pixelFormatAttribs[GLFW_PFA_WGL_PIXEL_TYPE_ARB] != + if (getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_PIXEL_TYPE_ARB) != WGL_TYPE_RGBA_ARB ) { continue; } - if (pixelFormatAttribs[GLFW_PFA_WGL_ACCELERATION_ARB] == + if (getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_ACCELERATION_ARB) == WGL_NO_ACCELERATION_ARB ) { continue; } - u->redBits = pixelFormatAttribs[GLFW_PFA_WGL_RED_BITS_ARB]; - u->greenBits = pixelFormatAttribs[GLFW_PFA_WGL_GREEN_BITS_ARB]; - u->blueBits = pixelFormatAttribs[GLFW_PFA_WGL_BLUE_BITS_ARB]; - u->alphaBits = pixelFormatAttribs[GLFW_PFA_WGL_ALPHA_BITS_ARB]; + u->redBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_RED_BITS_ARB); + u->greenBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_GREEN_BITS_ARB); + u->blueBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_BLUE_BITS_ARB); + u->alphaBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_ALPHA_BITS_ARB); - u->depthBits = pixelFormatAttribs[GLFW_PFA_WGL_DEPTH_BITS_ARB]; - u->stencilBits = pixelFormatAttribs[GLFW_PFA_WGL_STENCIL_BITS_ARB]; + u->depthBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_DEPTH_BITS_ARB); + u->stencilBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_STENCIL_BITS_ARB); - u->accumRedBits = pixelFormatAttribs[GLFW_PFA_WGL_ACCUM_RED_BITS_ARB]; - u->accumGreenBits = pixelFormatAttribs[GLFW_PFA_WGL_ACCUM_GREEN_BITS_ARB]; - u->accumBlueBits = pixelFormatAttribs[GLFW_PFA_WGL_ACCUM_BLUE_BITS_ARB]; - u->accumAlphaBits = pixelFormatAttribs[GLFW_PFA_WGL_ACCUM_ALPHA_BITS_ARB]; + u->accumRedBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_ACCUM_RED_BITS_ARB); + u->accumGreenBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_ACCUM_GREEN_BITS_ARB); + u->accumBlueBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_ACCUM_BLUE_BITS_ARB); + u->accumAlphaBits = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_ACCUM_ALPHA_BITS_ARB); - u->auxBuffers = pixelFormatAttribs[GLFW_PFA_WGL_AUX_BUFFERS_ARB]; + u->auxBuffers = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_AUX_BUFFERS_ARB); - if (pixelFormatAttribs[GLFW_PFA_WGL_STEREO_ARB]) + if (getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_STEREO_ARB)) u->stereo = GLFW_TRUE; - if (pixelFormatAttribs[GLFW_PFA_WGL_DOUBLE_BUFFER_ARB]) + if (getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_DOUBLE_BUFFER_ARB)) u->doublebuffer = GLFW_TRUE; if (_glfw.wgl.ARB_multisample) - u->samples = pixelFormatAttribs[GLFW_PFA_WGL_SAMPLES_ARB]; + u->samples = getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_SAMPLES_ARB); if (ctxconfig->client == GLFW_OPENGL_API) { if (_glfw.wgl.ARB_framebuffer_sRGB || _glfw.wgl.EXT_framebuffer_sRGB) { - if (pixelFormatAttribs[GLFW_PFA_WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB]) + if (getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB)) u->sRGB = GLFW_TRUE; } } @@ -226,7 +255,7 @@ static int choosePixelFormat(_GLFWwindow* window, { if (_glfw.wgl.EXT_colorspace) { - if (pixelFormatAttribs[GLFW_PFA_WGL_COLORSPACE_EXT ] == + if (getPixelFormatAttribFromArray(pfAttribs, pfValues, &hintPos, WGL_COLORSPACE_EXT) == WGL_COLORSPACE_SRGB_EXT) { u->sRGB = GLFW_TRUE;