【发布时间】:2019-01-03 22:55:17
【问题描述】:
我想创建一个 3D 纹理,将最低(0 级)mipmap 级别设置为在着色器中生成的一些数据,然后自动填充其余的 mipmap 级别。
我按如下方式创建纹理:
//Initialize texture dimensions
width = w;
height = h;
depth = d;
//set the texture rendering target, always GL_TEXTURE3D for 3D images
target = GL_TEXTURE_3D;
//Create the texture
glGenTextures(1, &textureID);
glBindTexture(target, textureID);
glObjectLabel(GL_TEXTURE, textureID, -1, "\"3D Texture\"");
//Set the texture sampling parameters
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//Allocate VRAM storage for the texture
vector<GLbyte> empty = vector<GLbyte>(width*height*depth*(32));
glTexImage3D(target, 0, GL_RGBA8, width, height, depth, 0,
GL_RGBA, GL_FLOAT, empty.data());
然后,我使用 imageStore 在着色器中写入它(按 LOD 0 的预期工作)。
然后我尝试像这样生成 mipmap 级别:
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
glGenerateTextureMipmap(textureID);
但是,这会导致在 glGenerateTextureMipmap() 处生成分段错误。
我是在内存屏障中遗漏了一点,还是在创建纹理时没有分配足够的内存?我做错了什么?
【问题讨论】:
-
sizeof(GLfloat)在您的vector构造函数中是怎么回事?如果你想要字节不应该是vector<unsigned char>? -
你是对的,那是我的错,我已经更改它并验证它适用于当前版本。它应该是 8*4(每个组件 8 个字节的组件数量)我搞砸了,谢谢你的注意。然而,分段错误本身并没有消失
-
你确定你有
glGenerateTextureMipmap()吗?那是4.5的功能。检查该函数指针是否实际上不为 NULL。 -
@derhass 我正在运行 OpenGL 4.6(至少 glGetString() 报告是这样)。此外,我在另一部分调用了该函数,但它没有出现段错误。所以至少它不是空指针。
-
对于内存屏障本身,请参阅Which memory barrier does glGenerateMipmap require?。对于崩溃,我不知道是什么原因造成的。
标签: c++ opengl segmentation-fault textures mipmaps