在 std::thread 中使用 SOIL 加载 OpenGL 纹理会引发"Integer Division by Zero"
Loading OpenGL texture with SOIL in std::thread raises "Integer Division by Zero"
我可以正常地在 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 = -816294792
和 w = -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 上进行了测试。
相关文章:
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- 将"模板<类型名 T>zero()"扩展/专用为可调用的"T"
- Raspberry Pi Zero W 上的 OpenCV - 浮点异常
- C++ OpenCV Randu 函数抛出'Integer division by zero'
- 为什么我的代码在"decrease to zero"问题中被时间超过了
- "ndk-build finish with non-zero exit value 2" 在编译文本仙女项目时
- Epoll zero recv() and negative(EAGAIN) send()
- 你如何在 boost::p ython 中"from __future__ import division"?
- CreateCompatibleDC(IntPtr.Zero) returns IntPtr.Zero
- QT5.10.1交叉编译为Raspberry Pi Zero W使用EGLF而不是XCB
- Windows到Raspberry Pi Zero串行问题
- C++ Zero-Initialization
- Win32 CreateThread() Arguments Zero'ed?
- 无锁"decrement if not zero"
- 当我有多态指针时使用"rule of zero"
- 为什么 std::regex_match 不支持 "zero-length assertions" ?
- 如何修复'Size of list( a class) is unknown or zero error'和"声明语法错误"?
- 我多么应该将空指针传递给c ++函数,就像C#中的IntPter.Zero一样
- 为什么在破坏函数函数中fstream Zero ernno
- 在 std::thread 中使用 SOIL 加载 OpenGL 纹理会引发"Integer Division by Zero"