如何在GLSL 1.3和OpenGL 2.1中使用位操作

如何在GLSL 1.3和OpenGL 2.1中使用位操作,opengl,glsl,bitwise-operators,Opengl,Glsl,Bitwise Operators,我正在尝试编写一个使用许多位操作的着色器。事实上,它们从glsl 1.30开始就受到支持,但我只支持OpenGL2.1 有没有办法在我的OpenGL版本中使用位操作?所有SM3兼容(~OpenGL 2.1)硬件都支持有限的整数功能。这通常是通过模拟带浮点数的整数来完成的,不包括位操作 对于位操作,您需要GLSL 1.3或 如果您只有OpenGL 2.1的原因是您的驱动程序有些过时,那么您可能很幸运仍然有EXT_gpu_shader4(在这种情况下,更新驱动程序可能是个好主意) 如果原因是你的显卡

我正在尝试编写一个使用许多位操作的着色器。事实上,它们从glsl 1.30开始就受到支持,但我只支持OpenGL2.1

有没有办法在我的OpenGL版本中使用位操作?

所有SM3兼容(~OpenGL 2.1)硬件都支持有限的整数功能。这通常是通过模拟带浮点数的整数来完成的,不包括位操作

对于位操作,您需要GLSL 1.3或

如果您只有OpenGL 2.1的原因是您的驱动程序有些过时,那么您可能很幸运仍然有EXT_gpu_shader4(在这种情况下,更新驱动程序可能是个好主意)

如果原因是你的显卡根本不支持更好的东西,那你就倒霉了

如果您确实有EXT_gpu_shader4(检查扩展字符串),则可以添加:

#extension EXT_gpu_shader4 : require

到您的GLSL 1.2着色器,它应该可以工作。

这应该可以帮助您开始

lowp ivec4 imod4_2(lowp ivec4 x)
{
  return x - (2 * (x/2));
}

lowp ivec4 parselowbits(lowp int x)
{
  // Implement (x % y) where y is known to be the constant 2
  // by first dividing x by (8, 4, 2, 1) and then doing a mod
  // by (2, 2, 2, 2) to generate an int vector.

  lowp ivec4 numerator = ivec4(x);
  lowp ivec4 denominator = ivec4(8, 4, 2, 1);
  lowp ivec4 modNumerator = numerator / denominator;
  lowp ivec4 modResult = imod4_2(modNumerator);

  return modResult;
}

lowp ivec4 parsehighbits(lowp int x)
{
  // Implement (x % y) where y is known to be the constant 2
  // by first dividing by (8*16, 4*16, 2*16, 1*16) and then doing a mod
  // by (2, 2, 2, 2) to generate an int vector.

  lowp ivec4 numerator = ivec4(x);
  lowp ivec4 denominator = ivec4(8*16, 4*16, 2*16, 1*16);
  lowp ivec4 modNumerator = numerator / denominator;
  lowp ivec4 modResult = imod4_2(modNumerator);

  return modResult;
}
上面的函数在一个分量的高半字节和低半字节(4位)上工作,比如输入向量的.r.g。当然,您需要读取中的值并乘以255以反规范化。然后,实施和维护是很容易的:

lowp ivec4 and4(lowp ivec4 a, lowp ivec4 b)
{
 lowp ivec4 a_and_b = a * b;
 return a_and_b;
}