仅在GLSL ES 3.00及更高版本中支持逐位运算符

时间:2016-11-13 06:15:35

标签: firefox glsl bitwise-operators

我正在尝试创建一个在GLSL中获得颜色位置的函数。

 precision highp float;
 uniform sampler2D uTexture0;
 varying vec2 vTextureCoords;

int getBit(float color, int bit){
        highp int colorInt = int(color);
        return (colorInt >> bit) & 1;
}

void main(void) {
   highp vec4 texelColour = texture2D(uTexture0, vec2(vTextureCoords.s, vTextureCoords.t));

 if(getBit(texelColour.r * 255.0, 7) == 1 ){
       gl_FragColor = vec4(0.5, 0.8, 0.5, 0.8);
 }else{
       gl_FragColor = vec4(0.4, 0.1, 0.0, 0.0);
 }

}

chrome和firefox返回此错误

ERROR: 0:35: '>>' : bit-wise operator supported in GLSL ES 3.00 and above only  
ERROR: 0:35: '&' : bit-wise operator supported in GLSL ES 3.00 and above only  

我正试图在我的着色器的第一行强制使用该版本,如下所示:

#version 130

但控制台返回该版本不受支持。

有没有办法为getBit函数创建子程序? 或者在sharder片段中为实现bitwize运算符启用哪些设置?

感谢您的回复。

纪尧姆

2 个答案:

答案 0 :(得分:0)

我正在寻找一种编写自己的移位功能的解决方案

 int getBit(float num, float b){
        num = num * 255.0;
        int bit = 0;
        for(int i = 7; i >= 0; i--){
            if(num >=  pow(2.0,float(i))){
                num = num - pow(2.0,float(i));
                if(b == float(i)){
                    bit = 1;
                }
            }
        }
        return bit;
    }


 if(getBit(texelColour.r , 3.0) != 0 && getBit(texelColour.g * 255.0, 0.0) == 0 &&  getBit(texelColour.b * 255.0, 0.0) == 0){
   gl_FragColor = vec4(0.5, 0.8, 0.5, 0.8);
}else{
   gl_FragColor = vec4(0.4, 0.1, 0.0, 0.0);
}

我是glsl的新手所以它肯定会更好但是有效。

Ps:我的需要仅为1字节十进制

纪尧姆

答案 1 :(得分:0)

我认为您需要声明您正在使用着色器顶部的es3:

#version 300 es

这还需要您更改着色器其余部分的语法(不再varying,而是inout等)。