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

Loading OpenGL texture with SOIL in std::thread raises "Integer Division by Zero"

本文关键字:Division Zero by 纹理 Integer OpenGL thread std 加载 SOIL      更新时间:2023-10-16

我可以正常地在 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 中也是如此。

知道这里发生了什么吗?

这就是你的做法。请注意,正如其他人在注释中提到的,每个使用 GL 的线程都需要保持上下文最新。这意味着实际上,如果不使一个线程成为 GL 上下文的所有者,就不能在多个线程中进行 GL API 调用。因此,如果打算分离图像加载开销,建议使用单独线程中的库将图像文件加载到缓冲区中,然后使用该缓冲区在主线程中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 上进行了测试。