我有一個整數值,並且想要獲取放置在特定位置的位。我和GLSL工作的OpenGL ES 2.0如何從使用GLSL和OpenGL ES 2.0的整數中獲取位
事情是這樣的:
GetBitOnLocation(int value, int location)
{
bit myBit = value[location];
if (myBit == 0)
return 0;
if (myBit == 1)
return 1;
}
我有一個整數值,並且想要獲取放置在特定位置的位。我和GLSL工作的OpenGL ES 2.0如何從使用GLSL和OpenGL ES 2.0的整數中獲取位
事情是這樣的:
GetBitOnLocation(int value, int location)
{
bit myBit = value[location];
if (myBit == 0)
return 0;
if (myBit == 1)
return 1;
}
int GetBitOnLocation(int value, int location)
{
int myBit = value & (1 << location);
if (myBit == 0)
return 0;
else
return 1;
}
我會很驚訝,如果這工作,我所知道的GLSL ES(至少用於OpenGL ES 2.0的版本)不支持按位運算符。 –
GLSL ES 2.0不允許按位運算符。 –
但我試過了,它的工作原理。我正在使用#version 150 –
根據以前的意見,不這樣做。
ES 2硬件不需要本地支持整數;允許將它們模擬爲浮點。爲了做到這一點,直接位測試沒有被定義。
如果你絕對不顧一切,請使用mod
和step
。例如。測試整數value
的第三比特:
float bit = step(0.5, mod(float(value)/8.0, 1.0));
// bit is now 1.0 if the lowest bit was set, 0.0 otherwise
邏輯被洗牌要測試到0.5
位置的比特然後切斷它上面的所有位與所述mod
。那麼只有當你想要的位被設置時,你的值纔可以大於或等於0.5
。
一般而言,你不想這樣做; GPU在整數比特修整方面往往很弱,並且迫使GPU爲數百萬個片段執行此操作,或者從性能角度來看,成千上萬的頂點通常是一個壞主意。 – solidpixel