【问题标题】:SDL, OpenGL: Segmentation faultSDL,OpenGL:分段错误
【发布时间】:2012-11-02 10:04:59
【问题描述】:

当我尝试使用 openGL 生成纹理时,此方法出现分段错误:

 void RendererGL::create_gl(SDL_Surface * surf, GLuint * tex ) {
   GLenum format;   GLint  colors_amount = surf->format->BytesPerPixel;

   if (colors_amount == 4) {
           if (surf->format->Rmask == 0x000000ff)
                   format = GL_RGBA;
           else
                   format = GL_BGRA;
   }
   else if (colors_amount == 3) {
           if (surf->format->Rmask == 0x000000ff)
                   format = GL_RGB;
           else
                   format = GL_BGR;
   }
   else {
       gCritical("Image is not truecolor");
   }
   glGenTextures( 1, tex );

   glBindTexture( GL_TEXTURE_2D, *tex );

   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

   glTexImage2D( GL_TEXTURE_2D, 0, colors_amount, surf->w, surf->h, 0, format,
                 GL_UNSIGNED_BYTE, surf->pixels ); }

我只在带有 nvidia 驱动程序的 debian 上遇到这个问题。使用开源驱动程序应用程序运行正常。 Valgrind 测试返回:

[ Info ] RendererGL::Init GL
==13033== Thread 3:
==13033== Invalid read of size 8
==13033==    at 0x51293C9: ??? (in /usr/lib/x86_64-linux-gnu/nvidia/current/libGL.so.304.64)
==13033==    by 0x419B0F: RendererGL::initGL() (RendererGL.cpp:12)
==13033==    by 0x41036C: Renderer::Renderer() (Renderer.cpp:27)
==13033==    by 0x4058EA: Renderer::getInstance() (Renderer.hpp:17)
==13033==    by 0x40B648: StandardReferences::StandardReferences() (StandardReferences.hpp:13)
==13033==    by 0x414C26: Map::Map(short**, unsigned short, unsigned int, unsigned int) (Map.cpp:8)
==13033==    by 0x4136E4: MapManager::loadMapFromFile(std::string, short) (MapManager.cpp:82)
==13033==    by 0x4138A0: MapManager::load() (MapManager.cpp:97)
==13033==    by 0x4084BA: Resource::load() (Resource.cpp:36)
==13033==    by 0x4196AF: Splash::initThread(void*) (Splash.cpp:99)
==13033==    by 0x53D0405: ??? (in /usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0.11.4)
==13033==    by 0x5413898: ??? (in /usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0.11.4)
==13033==  Address 0x658 is not stack'd, malloc'd or (recently) free'd
==13033== 
==13033== 
==13033== HEAP SUMMARY:
==13033==     in use at exit: 22,104,451 bytes in 31,729 blocks
==13033==   total heap usage: 47,499 allocs, 15,770 frees, 54,869,707 bytes allocated
==13033== 
==13033== LEAK SUMMARY:
==13033==    definitely lost: 833 bytes in 8 blocks
==13033==    indirectly lost: 1,728 bytes in 38 blocks
==13033==      possibly lost: 269,732 bytes in 99 blocks
==13033==    still reachable: 21,832,158 bytes in 31,584 blocks
==13033==         suppressed: 0 bytes in 0 blocks
==13033== Rerun with --leak-check=full to see details of leaked memory
==13033== 
==13033== For counts of detected and suppressed errors, rerun with: -v
==13033== Use --track-origins=yes to see where uninitialised values come from
==13033== ERROR SUMMARY: 12 errors from 2 contexts (suppressed: 6 from 6)

【问题讨论】:

  • glTexImage2D 的第三个参数,由变量“color_amount”提供,不采用任意值。根据规范,它必须是 glTexImage2D 参考中的格式标记之一;但是使用无效令牌不应导致段错误。
  • 尝试将其添加到主文件的开头:setenv( "MESA_DEBUG", "", 0 );。它将启用来自 Mesa/OpenGL 的调试/警告消息(它将它们写入 stdout/stderr)。我通常只是将它包装在#ifdef DEBUG 中,并在每个 GL 程序中使用它。
  • 我唯一能想到的是您的纹理可能具有非二维的幂,而您的 nvidia 卡不支持它。不过我只是猜测。
  • 感谢您的回复 :) 我的图片尺寸为:2048x1024 像素,我认为这不是问题。我在这一行中遇到 segm 失败: glGenTextures( 1, tex ); GDB 会话以此结束:程序收到信号 SIGSEGV,分段错误。 [切换到线程 0x7fffe85f2700 (LWP 3888)] 0x00007ffff7db91a9 in glGenTextures () from /usr/lib/x86_64-linux-gnu/libGL.so.1 (gdb) next 单步直到退出函数 glGenTextures,它没有行号信息. 0x00007ffff75c3e40 在?? () 来自 /usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0

标签: c++ opengl sdl sdl-image


【解决方案1】:

我打赌它是 RGB 表面而不是 RGBA 表面的段错误。

在拨打glTexImage2D() 之前尝试glPixelStorei(GL_UNPACK_ALIGNMENT, 1)​

GL_UNPACK_ALIGNMENT 默认为 4,它适用于 RGBA 数据,但会导致 OpenGL 读取 RGB 表面的 surf->pixels 的结尾。

如果surf->pitch 不为零,则必须使用GL_UNPACK_ROW_LENGTH​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多