我正在尝试通过以下方式以伽玛正确的方式渲染典型的 OpenGL 颜色三角形本指南 https://learnopengl.com/#!Advanced-Lighting/Gamma-Correction并查阅 SDL2 文档,了解如何在默认帧缓冲区上启用 SRGB 支持。
这是我编写的绘制三角形的代码:
#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 帧缓冲区。如果我在 Intel GPU 下运行,如果我使用 SDL2 指定非零 alpha 大小,我只会获得 sRGB 帧缓冲区。如果我为 Alpha 指定零大小(或者根本不指定大小),我将得到一个线性帧缓冲区。
This is how my example looks when running under the nvidia GPU (and the Intel GPU with a non-zero alpha bit size):
This is how it looks when running under the Intel GPU with a zero alpha bit size (sRGB incorrect):
And this is what I get with ApiTrace:
我也跑了glxinfo -t
据我所知,即使设置了零 alpha 位,我也应该获得 sRGB 支持(EDIT: 事实并非如此。的输出glxinfo
应该显示s
es 在 sRGB 列中,但由于某种原因不会发生):
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 帧缓冲区?我是否可能遇到了英特尔驱动程序错误?