gpt4 book ai didi

linux - 是什么导致了 glBufferSubData 中的这个 opengl 段错误?

转载 作者:太空狗 更新时间:2023-10-29 12:31:33 25 4
gpt4 key购买 nike

一段时间以来,我一直在减少这个段错误,这是我机器上的一个非常小的可重现示例(如下)。我有一种下沉的感觉,这是一个驱动程序错误,但我对 OpenGL 非常不熟悉,所以更有可能我只是做错了什么。

这是正确的 OpenGL 3.3 代码吗?无论平台和编译器如何,都应该没问题吗?

这是代码,用gcc -ggdb -lGL -lSDL2编译

#include <stdio.h>
#include "GL/gl.h"
#include "GL/glext.h"
#include "SDL2/SDL.h"

// this section is for loading OpenGL things from later versions.

typedef void (APIENTRY *GLGenVertexArrays) (GLsizei n, GLuint *arrays);
typedef void (APIENTRY *GLGenBuffers) (GLsizei n, GLuint *buffers);
typedef void (APIENTRY *GLBindVertexArray) (GLuint array);
typedef void (APIENTRY *GLBindBuffer) (GLenum target, GLuint buffer);
typedef void (APIENTRY *GLBufferData) (GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage);
typedef void (APIENTRY *GLBufferSubData) (GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data);
typedef void (APIENTRY *GLGetBufferSubData) (GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data);
typedef void (APIENTRY *GLFlush) (void);
typedef void (APIENTRY *GLFinish) (void);

GLGenVertexArrays glGenVertexArrays = NULL;
GLGenBuffers glGenBuffers = NULL;
GLBindVertexArray glBindVertexArray = NULL;
GLBindBuffer glBindBuffer = NULL;
GLBufferData glBufferData = NULL;
GLBufferSubData glBufferSubData = NULL;
GLGetBufferSubData glGetBufferSubData = NULL;

void load_gl_pointers() {
glGenVertexArrays = (GLGenVertexArrays)SDL_GL_GetProcAddress("glGenVertexArrays");
glGenBuffers = (GLGenBuffers)SDL_GL_GetProcAddress("glGenBuffers");
glBindVertexArray = (GLBindVertexArray)SDL_GL_GetProcAddress("glBindVertexArray");
glBindBuffer = (GLBindBuffer)SDL_GL_GetProcAddress("glBindBuffer");
glBufferData = (GLBufferData)SDL_GL_GetProcAddress("glBufferData");
glBufferSubData = (GLBufferSubData)SDL_GL_GetProcAddress("glBufferSubData");
glGetBufferSubData = (GLGetBufferSubData)SDL_GL_GetProcAddress("glGetBufferSubData");
}

// end OpenGL loading stuff


#define CAPACITY (1 << 8)

// return nonzero if an OpenGL error has occurred.
int opengl_checkerr(const char* const label) {
GLenum err;
switch(err = glGetError()) {
case GL_INVALID_ENUM:
printf("GL_INVALID_ENUM");
break;
case GL_INVALID_VALUE:
printf("GL_INVALID_VALUE");
break;
case GL_INVALID_OPERATION:
printf("GL_INVALID_OPERATION");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
printf("GL_INVALID_FRAMEBUFFER_OPERATION");
break;
case GL_OUT_OF_MEMORY:
printf("GL_OUT_OF_MEMORY");
break;
case GL_STACK_UNDERFLOW:
printf("GL_STACK_UNDERFLOW");
break;
case GL_STACK_OVERFLOW:
printf("GL_STACK_OVERFLOW");
break;
default: return 0;
}

printf(" %s\n", label);
return 1;
}

int main(int nargs, const char* args[]) {
printf("initializing..\n");
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_Window* const w =
SDL_CreateWindow(
"broken",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1, 1,
SDL_WINDOW_OPENGL
);

if(w == NULL) {
printf("window was null\n");
return 0;
}

SDL_GLContext context = SDL_GL_CreateContext(w);

if(context == NULL) {
printf("context was null\n");
return 0;
}

load_gl_pointers();

if(opengl_checkerr("init")) {
return 1;
}

printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR));
printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER));

float* const vs = malloc(CAPACITY * sizeof(float));
memset(vs, 0, CAPACITY * sizeof(float));

unsigned int i = 0;
while(i < 128000) {
GLuint vertex_array;
GLuint vertex_buffer;

glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);

glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);

if(opengl_checkerr("gen/binding")) {
return 1;
}

glBufferData(
GL_ARRAY_BUFFER,
CAPACITY * sizeof(float),
vs, // initialize with `vs` just to make sure it's allocated.
GL_DYNAMIC_DRAW
);

// verify that the memory is allocated by reading it back into `vs`.
glGetBufferSubData(
GL_ARRAY_BUFFER,
0,
CAPACITY * sizeof(float),
vs
);

if(opengl_checkerr("creating buffer")) {
return 1;
}

glFlush();
glFinish();

// segfault occurs here..
glBufferSubData(
GL_ARRAY_BUFFER,
0,
CAPACITY * sizeof(float),
vs
);

glFlush();
glFinish();

++i;
}

return 0;
}

当我将迭代次数从 64k 增加到 128k 时,我开始得到:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff754c859 in __memcpy_sse2_unaligned () from /usr/lib/libc.so.6
(gdb) bt
#0 0x00007ffff754c859 in __memcpy_sse2_unaligned () from /usr/lib/libc.so.6
#1 0x00007ffff2ea154d in ?? () from /usr/lib/xorg/modules/dri/i965_dri.so
#2 0x0000000000400e5c in main (nargs=1, args=0x7fffffffe8d8) at opengl-segfault.c:145

但是,我可以在没有段错误的情况下将容量增加一倍以上(将迭代次数保持在 64k)。

GL_VENDOR: Intel Open Source Technology Center
GL_RENDERER: Mesa DRI Intel(R) Haswell Mobile

最佳答案

我在调用 glGenTextures 和 glBindTexture 时遇到了非常相似的问题。我尝试调试,当我尝试逐步执行这些行时,我会得到类似以下内容的信息:

程序收到信号 SIGSEGV,段错误。
0x00007ffff26eaaa8 在 ?? () 来自/usr/lib/x86_64-linux-gnu/dri/i965_dri.so

请注意,在添加纹理之前,我可以使用 vbos 和 vaos 成功运行程序并生成良好的网格。在查看建议从 xf86-video-intel 驱动程序 切换到 xf86-video-fbdev 驱动程序的答案后,我建议不要这样做(那里关于这个问题或用户在使用集成英特尔显卡的 Linux 上面临段错误的信息真的不多。也许是一个很好的问题,可以向 Intel OpenSource 的人们提问。

我找到的解决方案是停止使用 freeglut。改用 glfw。 intel linux 图形堆栈是否真的存在一些问题,除此之外,似乎可以解决的问题是 freeglut。如果你想在你的机器上使用 glfw 最新的 opengl 核心配置文件,你只需要以下内容:

glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

设置向前兼容(虽然我看到很多帖子争论你不应该这样做)意味着 mesa 可以自由选择允许的核心上下文,将最小上下文设置为 3.0 或更高。我想 freeglut 一定是在与 mesa 的交互中出了问题,如果有人能分享一些关于这方面的信息那就太好了!

关于linux - 是什么导致了 glBufferSubData 中的这个 opengl 段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com