簡體   English   中英

為什么只有在SDL2中設置非零alpha大小時才會獲得sRGB幀緩沖?

[英]Why do I get an sRGB framebuffer only when I'm setting a non-zero alpha size in SDL2?

我正在嘗試按照本指南以及如何在默認幀緩沖區上啟用SRGB支持的SDL2文檔中以正確的伽馬方式呈現典型的OpenGL顏色三角形。

這是我編寫的代碼,它繪制了三角形:

#include <SDL.h>

// Header file generated with glLoadGen
#include "gl_core_3_3.h"

#include <cstdlib>

void sdl_loop(SDL_Window* window);

static const char* const vertexSource = R"(
#version 330

in vec2 position;
in vec3 color;

out vec3 vs_color;

void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
    vs_color = color;
}
)";

static const char* const fragmentSource = R"(
#version 330

in vec3 vs_color;
out vec4 fragColor;

void main()
{
    fragColor = vec4(vs_color, 1.0);
}
)";

static const float vertices[] = {
    // X    Y     R     G     B
    -0.9f, -0.9f, 1.0f, 0.0f, 0.0f,
    0.9f, -0.9f, 0.0f, 1.0f, 0.0f,
    0.0f,  0.9f, 0.0f, 0.0f, 1.0f,
};

static const unsigned short indices[] = { 0, 1, 2 };

void sdl_loop(SDL_Window* window)
{
    glClearColor(0.0f, 0.1f, 0.2f, 1.0f);

    glEnable(GL_FRAMEBUFFER_SRGB);

    auto program = glCreateProgram();
    auto vertexShader = glCreateShader(GL_VERTEX_SHADER);
    auto fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vertexShader, 1, &vertexSource, nullptr);
    glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);

    glCompileShader(vertexShader);
    glCompileShader(fragmentShader);

    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);

    glBindFragDataLocation(program, 0, "fragColor");

    glLinkProgram(program);

    glDetachShader(program, vertexShader);
    glDetachShader(program, fragmentShader);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    auto positionAttr = glGetAttribLocation(program, "position");
    auto colorAttr = glGetAttribLocation(program, "color");

    unsigned vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    unsigned vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    unsigned indexBuffer;
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    size_t stride = 5 * sizeof(float);
    size_t offset = 0;
    glEnableVertexAttribArray(positionAttr);
    glVertexAttribPointer(positionAttr, 2, GL_FLOAT, false, stride, (void*) offset);
    offset += 2 * sizeof(float);

    glEnableVertexAttribArray(colorAttr);
    glVertexAttribPointer(colorAttr, 3, GL_FLOAT, false, stride, (void*) offset);
    offset += 3 * sizeof(float);

    glUseProgram(program);

    SDL_Event evt;
    for (;;) {
        while (SDL_PollEvent(&evt)) {
            if (evt.type == SDL_QUIT) {
                goto out;
            }
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, nullptr);

        SDL_GL_SwapWindow(window);
    }

out:
    glDeleteProgram(program);
    glDeleteBuffers(1, &vertexBuffer);
    glDeleteBuffers(1, &indexBuffer);
    glDeleteVertexArrays(1, &vao);
}

int main()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                     "Couldn't initialize SDL: %s", SDL_GetError());
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
                                 "Couldn't initialize SDL", SDL_GetError(), nullptr);
        return EXIT_FAILURE;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    // Commenting this line out doesn't give you an sRGB framebuffer on Intel GPUs
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
    SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);

    auto* window = SDL_CreateWindow("Hello OpenGL 3!",
                                    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                    800, 600,
                                    SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    if (window == nullptr) {
        SDL_LogError(
            SDL_LOG_CATEGORY_APPLICATION,
            "Couldn't initialize SDL window: %s", SDL_GetError());
        SDL_ShowSimpleMessageBox(
            SDL_MESSAGEBOX_ERROR,
            "Couldn't create SDL window", SDL_GetError(), nullptr);
        return EXIT_FAILURE;
    }

    auto context = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, context);
    SDL_GL_SetSwapInterval(1);

    ogl_LoadFunctions();

    sdl_loop(window);

    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);

    SDL_Quit();

    return EXIT_SUCCESS;
}

我正在運行Ubuntu 16.04(使用HWE驅動程序),我正在使用的機器同時具有Intel HD 4000 GPU和帶有Optimus的專用nvidia GPU。 從理論上講,我應該得到一個sRGB幀緩沖區。 但是,我只能在nvidia GPU下運行或使用ApiTrace時獲得sRGB幀緩沖。 如果我在英特爾GPU下運行,如果我用SDL2指定非零的alpha大小,我只會獲得一個sRGB幀緩沖。 如果我為alpha指定零大小(或者根本不指定一個),我會得到一個線性幀緩沖。

這是我的示例在nvidia GPU(以及具有非零alpha位大小的Intel GPU)下運行時的樣子: 具有非零alpha位大小的nvidia GPU和Intel GPU

這是在具有零alpha位大小(sRGB不正確)的Intel GPU下運行時的外觀: 英特爾GPU,alpha位大小為零

這就是我用ApiTrace得到的: ApiTrace

我也跑glxinfo -t ,從我所知道的,我應該得到甚至零阿爾法位設置的sRGB支持( 編輯 :這是不是這樣的輸出。 glxinfo應該顯示s在sRGB的列上課,但沒有按” t由於某種原因或其他原因發生):

40 GLX Visuals
Vis   Vis   Visual Trans  buff lev render DB ste  r   g   b   a      s  aux dep ste  accum buffer   MS   MS         
 ID  Depth   Type  parent size el   type     reo sz  sz  sz  sz flt rgb buf th  ncl  r   g   b   a  num bufs caveats
--------------------------------------------------------------------------------------------------------------------
0x 21 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x 22 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x b7 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x b8 24 TrueColor    0     32  0  rgba   0   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x b9 24 TrueColor    0     32  0  rgba   0   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x ba 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x bb 24 TrueColor    0     24  0  rgba   0   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x bc 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x bd 24 TrueColor    0     24  0  rgba   0   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x be 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8  16  16  16  16   0   0   Slow
0x bf 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x c0 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8  16  16  16   0   0   0   Slow
0x c1 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   4   1   None
0x c2 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   8   1   None
0x c3 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   4   1   None
0x c4 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   8   1   None
0x c5 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   4   1   None
0x c6 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   8   1   None
0x c7 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   4   1   None
0x c8 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   8   1   None
0x c9 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x ca 24 DirectColor  0     32  0  rgba   0   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x cb 24 DirectColor  0     32  0  rgba   0   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x cc 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x cd 24 DirectColor  0     24  0  rgba   0   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x ce 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x cf 24 DirectColor  0     24  0  rgba   0   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x d0 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x d1 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8  16  16  16  16   0   0   Slow
0x d2 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x d3 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8  16  16  16   0   0   0   Slow
0x d4 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   4   1   None
0x d5 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   8   1   None
0x d6 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   4   1   None
0x d7 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   8   1   None
0x d8 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   4   1   None
0x d9 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   8   1   None
0x da 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   4   1   None
0x db 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   8   1   None
0x 76 32 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None

64 GLXFBConfigs:
Vis   Vis   Visual Trans  buff lev render DB ste  r   g   b   a      s  aux dep ste  accum buffer   MS   MS         
 ID  Depth   Type  parent size el   type     reo sz  sz  sz  sz flt rgb buf th  ncl  r   g   b   a  num bufs caveats
--------------------------------------------------------------------------------------------------------------------
0x 77  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0    0  0   0   0   0   0   0   0   None
0x 78  0 TrueColor    0     16  0  rgba   0   0   5   6   5   0  .   .   0    0  0   0   0   0   0   0   0   None
0x 79  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   0   0   None
0x 7a  0 TrueColor    0     16  0  rgba   0   0   5   6   5   0  .   .   0   16  0   0   0   0   0   0   0   None
0x 7b  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 7c  0 TrueColor    0     16  0  rgba   0   0   5   6   5   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 7d 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x 7e 24 TrueColor    0     32  0  rgba   0   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x 7f 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x 80 24 TrueColor    0     32  0  rgba   0   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x 81 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x 82 24 TrueColor    0     24  0  rgba   0   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x 83 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 84 24 TrueColor    0     24  0  rgba   0   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 85  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   0   0   None
0x 86  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0  16  16  16   0   0   0   Slow
0x 87 32 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x 88 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8  16  16  16  16   0   0   Slow
0x 89 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 8a 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8  16  16  16   0   0   0   Slow
0x 8b  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0    0  0   0   0   0   0   4   1   None
0x 8c  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0    0  0   0   0   0   0   8   1   None
0x 8d  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   4   1   None
0x 8e  0 TrueColor    0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   8   1   None
0x 8f 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   4   1   None
0x 90 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   8   1   None
0x 91 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   4   1   None
0x 92 24 TrueColor    0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   8   1   None
0x 93 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   4   1   None
0x 94 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   8   1   None
0x 95 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   4   1   None
0x 96 24 TrueColor    0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   8   1   None
0x 97  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0    0  0   0   0   0   0   0   0   None
0x 98  0 DirectColor  0     16  0  rgba   0   0   5   6   5   0  .   .   0    0  0   0   0   0   0   0   0   None
0x 99  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   0   0   None
0x 9a  0 DirectColor  0     16  0  rgba   0   0   5   6   5   0  .   .   0   16  0   0   0   0   0   0   0   None
0x 9b  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 9c  0 DirectColor  0     16  0  rgba   0   0   5   6   5   0  .   .   0   24  8   0   0   0   0   0   0   None
0x 9d 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x 9e 24 DirectColor  0     32  0  rgba   0   0   8   8   8   8  .   .   0    0  0   0   0   0   0   0   0   None
0x 9f 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x a0 24 DirectColor  0     32  0  rgba   0   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x a1 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x a2 24 DirectColor  0     24  0  rgba   0   0   8   8   8   0  .   .   0    0  0   0   0   0   0   0   0   None
0x a3 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x a4 24 DirectColor  0     24  0  rgba   0   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x a5  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   0   0   None
0x a6  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0  16  16  16   0   0   0   Slow
0x a7 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   0   0   None
0x a8 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8  16  16  16  16   0   0   Slow
0x a9 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   0   0   None
0x aa 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8  16  16  16   0   0   0   Slow
0x ab  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0    0  0   0   0   0   0   4   1   None
0x ac  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0    0  0   0   0   0   0   8   1   None
0x ad  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   4   1   None
0x ae  0 DirectColor  0     16  0  rgba   1   0   5   6   5   0  .   .   0   16  0   0   0   0   0   8   1   None
0x af 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   4   1   None
0x b0 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0    0  0   0   0   0   0   8   1   None
0x b1 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   4   1   None
0x b2 24 DirectColor  0     32  0  rgba   1   0   8   8   8   8  .   .   0   24  8   0   0   0   0   8   1   None
0x b3 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   4   1   None
0x b4 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0    0  0   0   0   0   0   8   1   None
0x b5 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   4   1   None
0x b6 24 DirectColor  0     24  0  rgba   1   0   8   8   8   0  .   .   0   24  8   0   0   0   0   8   1   None

為什么我在這種情況下沒有獲得sRGB幀緩沖? 我是否有可能遇到英特爾驅動程序錯誤?

顯然,罪魁禍首是英特爾Mesa驅動程序中的這個錯誤 (感謝SO用戶genpfault在freedesktop bugzilla中找到它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM