在std :: thread中使用SOIL加载OpenGL纹理会引发“Integer Division by Zero”

时间:2016-01-29 02:09:51

标签: c++ opengl std soil

我可以在SOIL / OpenGL中正常加载纹理。没有错误,一切正常:

// this is inside my texture loading code in my texture class
// that i normally use for loading textures
image = SOIL_load_OGL_texture
    (
    file,
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    NULL
    );

但是,使用相同的代码并从std :: thread调用它,在image = SOIL_load_OGL_texture行我得到未处理的异常Integer Division by Zero

void loadMe() {
    Texture* abc = new Texture("res/img/office.png");
}

void loadStuff() {
    Texture* loading = new Texture("res/img/head.png"); // < always works

    loadMe() // < always works
    std::thread textures(loadMe); // < always "integer division by zero"

这是我的Texture类中的一些相关代码:

// inside the class
private:
    GLint w, h;
    GLuint image;

// loading the texture (called by constructor if filename is given)
void Texture::loadImage(const char* file)
{

    image = SOIL_load_OGL_texture
        (
        file,
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        NULL
        );

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, image);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
    glBindTexture(GL_TEXTURE_2D, 0);

    if (image <= 0)
        std::cout << file << " failed to load!\n";
    else
        std::cout << file << " loaded.\n";

    glDisable(GL_TEXTURE_2D);
}

它在image = SOIL_load_OGL_texture完全引发了异常,当我进入调试器时,我会看到w = -816294792w = -816294792之类的内容,但我猜这只是意味着它没有设置,因为它还显示在调试器中加载其他纹理。

此外,代码的SOIL_load_OGL_texture部分可以在Texture类之外自行运行,即使在std :: thread中也是如此。

知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

这是你如何做到的。请注意,正如其他人在评论中提到的那样,每个使用GL的线程都需要保持上下文的最新状态。这意味着实际上不能在多个线程中进行GL API调用而不使一个线程成为GL上下文的所有者。因此,如果意图分离Image加载开销,建议使用单独线程中的库将图像文件加载到缓冲区中,然后将该缓冲区用于主线程中的glTexImage2D。加载图像后,可以显示虚拟纹理。

我尝试检查你所使用的平台(见上面的评论),因为我没有看到回复,我在下面假设Linux。

/* Regular GL context creation foo */
/* Regular attribute, uniform, shader creation foo */
/* Create a thread that does loading with SOIL in function SOIL_loader */
std::thread textureloader(SOIL_loader);
/* Wait for loader thread to finish, 
thus defeating the purpose of a thread. Ideally, 
only the image file read/decode should happen in separate thread */
textureloader.join();
/* Make the GL context current back again in the main thread 
for other actions */
glfwMakeContextCurrent((GLFWwindow*)window);
/* Some other foo */

==

这是加载程序线程函数:

void SOIL_loader()
{
   glfwMakeContextCurrent((GLFWwindow*)window);
    SOIL_load_OGL_texture
        (
        "./img_test.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID /* or passed ID */,
        NULL
        );
   GL_CHECK(SOIL);
}

在Ubuntu 14.04,Mesa和glfw3上进行了测试。