We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2060 - SDL_GL_GetAttribute(SDL_GL_RED_SIZE) fails for core profiles
Summary: SDL_GL_GetAttribute(SDL_GL_RED_SIZE) fails for core profiles
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: 2.0.0
Hardware: All All
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords: target-2.0.4, triage-2.0.4
Depends on:
Blocks:
 
Reported: 2013-08-24 05:20 UTC by Stephan T. Lavavej
Modified: 2015-05-06 15:56 UTC (History)
2 users (show)

See Also:


Attachments
Self-contained test case. (2.33 KB, text/x-csrc)
2013-08-24 05:20 UTC, Stephan T. Lavavej
Details
Patch to fix SDL_GL_GetAttribute in Core Profile contexts (5.29 KB, patch)
2015-04-14 20:28 UTC, Alex Szpakowski
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan T. Lavavej 2013-08-24 05:20:57 UTC
Created attachment 1304 [details]
Self-contained test case.

SDL_GL_GetAttribute(SDL_GL_RED_SIZE) etc. fails for core profiles because it's using deprecated/removed OpenGL functionality. Here's a self-contained repro (I'm using mingw-w64 on Win7 x64, but the problem is universal):

C:\Temp\gcc>type meow.cpp
// g++ -DUSE_CORE_PROFILE -Wall -Wextra -mwindows meow.cpp -o meow.exe -lopengl32 -lSDL2 -limm32 -lole32 -loleaut32 -luuid -lversion -lwinmm -lmingw32 -lSDL2main

#include <stddef.h>
#include <fstream>
#include <GL/gl.h>
#include <SDL2/SDL.h>

struct Attribute {
    SDL_GLattr attr;
    int value;
    const char * name;
};

int main(int, char * []) {
    std::ofstream f("output.txt");

#ifdef USE_CORE_PROFILE
    f << "Using core profile.\n";
#else
    f << "Using compatibility profile.\n";
#endif

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        f << "SDL_Init() failed!\n";
    }

    #define ATTRIB(ATTR, VALUE) { ATTR, VALUE, #ATTR }

    const Attribute attrs[] = {
        ATTRIB(  SDL_GL_RED_SIZE, 8),
        ATTRIB(SDL_GL_GREEN_SIZE, 8),
        ATTRIB( SDL_GL_BLUE_SIZE, 8),
        ATTRIB(SDL_GL_ALPHA_SIZE, 8),
        ATTRIB(SDL_GL_DEPTH_SIZE, 0),
        ATTRIB(SDL_GL_DOUBLEBUFFER, 1),
        ATTRIB(SDL_GL_ACCELERATED_VISUAL, 1),
        ATTRIB(SDL_GL_CONTEXT_MAJOR_VERSION, 4),
        ATTRIB(SDL_GL_CONTEXT_MINOR_VERSION, 3),
#ifdef USE_CORE_PROFILE
        ATTRIB(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)
#else
        ATTRIB(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY)
#endif
    };

    const size_t N = sizeof(attrs) / sizeof(attrs[0]);

    for (size_t i = 0; i < N; ++i) {
        if (SDL_GL_SetAttribute(attrs[i].attr, attrs[i].value) != 0) {
            f << "SDL_GL_SetAttribute() failed!\n";
        }
    }

    SDL_Window * window = SDL_CreateWindow("Meow", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1200, SDL_WINDOW_OPENGL);

    if (!window) {
        f << "SDL_CreateWindow() failed!\n";
    }

    SDL_GLContext context = SDL_GL_CreateContext(window);

    if (!context) {
        f << "SDL_GL_CreateContext() failed!\n";
    }

    for (size_t i = 0; i < N; ++i) {
        int val = 0;

        if (SDL_GL_GetAttribute(attrs[i].attr, &val) != 0) {
            f << "SDL_GL_GetAttribute(" << attrs[i].name << ") failed!\n";
            continue;
        }

        if (val != attrs[i].value) {
            f << "Unexpected attribute value!\n";
        }
    }

    if (glGetError() == GL_NO_ERROR) {
        f << "Done!\n";
    } else {
        f << "glGetError() recorded failure!\n";
    }

    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

C:\Temp\gcc>g++ -Wall -Wextra -mwindows meow.cpp -o meow.exe -lopengl32 -lSDL2 -limm32 -lole32 -loleaut32 -luuid -lversion -lwinmm -lmingw32 -lSDL2main

C:\Temp\gcc>meow

C:\Temp\gcc>type output.txt
Using compatibility profile.
Done!

C:\Temp\gcc>g++ -DUSE_CORE_PROFILE -Wall -Wextra -mwindows meow.cpp -o meow.exe -lopengl32 -lSDL2 -limm32 -lole32 -loleaut32 -luuid -lversion -lwinmm -lmingw32 -lSDL2main

C:\Temp\gcc>meow

C:\Temp\gcc>type output.txt
Using core profile.
SDL_GL_GetAttribute(SDL_GL_RED_SIZE) failed!
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE) failed!
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE) failed!
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE) failed!
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE) failed!
Done!

The problem is in SDL_video.c, where SDL_GL_GetAttribute() calls glGetIntegerv() with GL_RED_BITS. As http://www.opengl.org/registry/doc/glspec43.compatibility.20130214.pdf explains in D.2.2 "Removed Features", page 790, this enum was removed:

"Context framebuffer size queries - RED_BITS, GREEN_BITS, BLUE_BITS, ALPHA_BITS, DEPTH_BITS, and STENCIL_BITS."

(STENCIL doesn't appear in my test because I'm not using it, but it is clearly also affected.)

The core-friendly way to query this information appears to be glGetFramebufferAttachmentParameteriv(), available since OpenGL 3.0: http://www.opengl.org/wiki/GLAPI/glGetFramebufferAttachmentParameter
Comment 1 Ryan C. Gordon 2015-02-19 05:22:17 UTC
Marking a large number of bugs with the "triage-2.0.4" keyword at once. Sorry if you got a lot of email from this. This is to help me sort through some bugs in regards to a 2.0.4 release. We may or may not fix this bug for 2.0.4, though!
Comment 2 Ryan C. Gordon 2015-04-07 04:57:58 UTC
(sorry if you get a lot of copies of this email, I'm marking several bugs at once)

Marking bugs for the (mostly) final 2.0.4 TODO list. This means we're hoping to resolve this bug before 2.0.4 ships if possible. In a perfect world, the open bug count with the target-2.0.4 keyword is zero when we ship.

(Note that closing a bug report as WONTFIX, INVALID or WORKSFORME might still happen.)

--ryan.
Comment 3 Alex Szpakowski 2015-04-14 20:28:10 UTC
Created attachment 2124 [details]
Patch to fix SDL_GL_GetAttribute in Core Profile contexts

Here's a patch that makes SDL_GL_GetAttribute use glGetFramebufferAttachmentParameteriv when appropriate.
Comment 4 Alex Szpakowski 2015-05-06 15:56:03 UTC
This should be fixed as of this commit: https://hg.libsdl.org/SDL/rev/a583c42c51d7