opengl - bool 纹理

标签 opengl graphics textures shader

我需要一种有效的方法来使用 openGL 从纹理中获取 bool 值。 bool 数组将是一个巨大的 3D 数组,我不能浪费空间,这意味着我不能将 bool 值编码为每个 1 个字节。

现在我的解决方案是将其编码为由整数组成的一维缓冲区纹理。在着色器中使用按位运算,我可以获取 bool 值。

我的解决方案存在两个性能消耗问题。我为 bool 值计算的索引自然是一个 3D 坐标,这意味着如果我想提高效率并利用硬件,我应该使用 3D 纹理来减少 3D -> 1D 索引转换的性能损失(我是否正确)在想这个吗?)

另一个问题,不像上面的问题那么糟糕,是从整数获取中解码 bool 值。

我注意到,如果我对 3D 纹理内部的 bool 值进行编码,我将不得不进行不同但仍然昂贵的 3D -> 1D 索引转换因为无论如何存储整数内部的位都是 1D。基本上,它就像每个纹理像素存储一个 bool 值立方体,您可以使用一维坐标对其进行索引。因此,由于还需要从 3D 纹理中解码内容,因此我似乎没有获得任何性能。

我当前的解决方案是最佳选择还是有更好的选择?我想知道这里是否可以使用任何 openGL 支持的压缩图像格式?

最佳答案

I should use a 3D texture to cut the performance penalty of the 3D -> 1D index conversion (am I correct in thinking this?)

实际上,如果您做得很聪明,那么转换可以非常有效地完成。如果您的纹理尺寸是 2 的幂,则可以归结为位移和 mask 。

I notice that if i encode the booleans inside of a 3D texture, I would have to do a different but still as costly 3D -> 1D index conversion anyway because storing bits inside of an integer is 1D anyway. Basically, it would be like each texel storing a cube of booleans, which you index with a 1D coordinate. Therefore I don't appear to be gaining any performance because of the need to decode things from the 3D texture also.

这就是我所推荐的。使用位运算即可轻松实现转换。每个方向您可以走两个单位。二→二元。你有三个维度,所以正好是 3 位。因此,要找到 8 位立方体中的特定位,您只需对 x、y 和 z 子纹理元素位置进行或运算(分别移位 0、1 和 2 到更高位),即

subbit = (x & 1) | ((y & 1) << 1) | ((z & 1) << 2);

如果您可以确保 x、y 和 z 仅为 1 或零,您可以保存 … & 1 掩码。

给定某个整数纹理坐标 t,您可以通过将 1 移至较低位来屏蔽大纹理坐标的 LSB 和要从中获取的纹素,从而获得 x、y 和 z 子纹素坐标。

ivec3 sub_texel = ivec3(texcoord.x  & 1, texcoord.y  & 1,  texcoord.z & 1);
ivec3     texel = ivec3(texcoord.x >> 1, texcoord.y >> 1, texcoord.z >> 1);

如果 texcoord 是 [0; 中的 float vec3; 1] 当然,您首先必须将其带入整数坐标。

I'm wondering if any openGL supported compressed image formats would be of use here?

不,因为有些是基于有损压缩的。

关于opengl - bool 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129551/

相关文章:

c++ - 学习正确使用 VBO

opengl - draw* 函数的直接状态访问?

java - 如何用LWJGL将图像保存到内存中?

java - 带有缓冲区策略的 OS X 上的 JFrame 禁用圆角

c# - 如何在 C# 中使用 RANSAC 过滤 OpenSURF 无效匹配

java - 加载 OpenGL 纹理的 Android 安全方法

c++ - SOIL2 文件上未解析的外部符号 - 需要 opengl 调用

javascript - 如何使用 4 个 Angular 的位置获取 3d 平面的 Angular

javascript - 如何在 Three.js 中不拉伸(stretch)地重复纹理?

c++ - 如何在 OpenGL 屏幕上同时显示图形和文字?