在以上质地GLSL位precise控制 [英] Precise control over texture bits in GLSL

查看:450
本文介绍了在以上质地GLSL位precise控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用OpenGL和GLSL八叉树遍历计划,并希望保持数据的纹理。虽然有格式的一大选择要使用的纹理数据(花车和不同大小的整数)我有一些麻烦搞清楚是否有办法有过位的更多precise控制,从而实现更高的效率和紧凑的存储。这可能是一个普遍的问题,不仅适用于OpenGL和GLSL。

I am trying to implement an octree traversal scheme using OpenGL and GLSL, and would like to keep the data in textures. While there is a big selection of formats to use for the texture data (floats and integers of different sizes) I have some trouble figuring out if there is a way to have more precise control over the bits and thus achieving greater efficiency and compact storage. This might be a general problem, not only applying to OpenGL and GLSL.

作为一个简单的玩具例子,让我们说,我有一个包含16位整数纹素。我想带code 1位两个布尔值每,1个10位的整数值,然后一个4位的整数值到这个纹理像素。是否有EN code这个技术创建纹理时,然后去code使用GLSL着色器采样纹理时这些组件?

As a simple toy example, let's say that I have a texel containing a 16 bit integer. I want to encode two booleans of 1 bit each, one 10 bit integer value and then a 4 bit integer value into this texel. Is there a technique to encode this when creating the texture, and then decode these components when sampling the texture using a GLSL shader?

编辑:看起来像我其实找位操作技巧。因为他们似乎得到支持,我以后应该多一些研发罚款。

Looks like I am in fact looking for bit manipulation techniques. Since they seem to be supported, I should be fine after some more researching.

推荐答案

整数和GLSL着色器内的位操作以来的OpenGL 3的支持(从而对DX10一流的硬件present,如果告诉你更多)。所以,你可以做此位mainulation在自己的着色器内。

Integer and bit-manipulations inside GLSL shaders are supported since OpenGL 3 (thus present on DX10 class hardware, if that tells you more). So you can just do this bit mainulation on your own inside the shader.

但随着整数工作是一回事,让他们出来的质感是另一回事。该标准的OpenGL纹理格式(你可以用来)可直接存储花车(如 GL_R16F )或正火定点值(如 GL_R16 ,有效的整数的门外汉;)),但是从他们的阅读(使用纹理 texelFetch 或其他)将净赚你漂浮值着色器,从中你不能很容易或可靠地推断出内部存储整数原位模式。

But working with integers is one thing, getting them out of the texture is another. The standard OpenGL texture formats (that you may be used to) are either storing floats directly (like GL_R16F) or normalized fixed point values (like GL_R16, effectively integers for the uninitiated ;)), but reading from them (using texture, texelFetch or whatever) will net you float values in the shader, from which you cannot that easily or reliably deduce the original bit-pattern of the internally stored integer.

让您真正需要使用的是一个整数的质感,这需要的OpenGL 3,太(或者在 GL_EXT_texture_integer 扩展,但硬件的支持,这将有可能有GL3反正)。因此,对于你的纹理,你需要使用一个实际的整数内部格式,例如像 GL_R16UI (对于单组分16位无符号整数)constrast通常的定点格式(例如像 GL_R16 对于标准化的[0,1] - 颜色用16位precision)。

So what you really need to use is an integer texture, which require OpenGL 3, too (or maybe the GL_EXT_texture_integer extension, but hardware supporting that will likely have GL3 anyway). So for your texture you need to use an actual integer internal format, like e.g. GL_R16UI (for a 1-component 16-bit unsigned integer) in constrast to the usual fixed point formats (like e.g. GL_R16 for a normalized [0,1]-color with 16 bits precision).

然后在着色器,你需要使用一个整数取样器的类型,例如像 usampler2D 一个无符号整数2D纹理(同样地 isampler ... 的签署变种)真正得到一个无符号从纹理整数 texelFetch 调用:

And then in the shader you need to use an integer sampler type, like e.g. usampler2D for an unsigned integer 2D texture (and likewise isampler... for the signed variants) to actually get an unsigned integer from your texture or texelFetch calls:

CPU:

glTexImage2D(GL_TEXTURE_2D, 0, GL_R16UI, ..., GL_R, GL_UNSIGNED_SHORT, data);

GPU:

uniform usampler2D tex;

...
uint value = texture(tex, ...).r;
bool b1 = (value&0x8000) == 0x8000, 
     b2 = (value&0x4000) == 0x4000;
uint i1 = (value>>4) & 0x3FF, 
     i2 = value & 0xF;

这篇关于在以上质地GLSL位precise控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆