OpenGL布尔制服?

OpenGL bool uniform?

我正在尝试将布尔值发送到 OpenGL glsl 着色器。

目前我在着色器中有这个:

uniform bool foo;

我用这个来设置它:

glUniform1i(glGetUniformLocation(shader, "foo"), true);

似乎没有 glUniform1b,所以我将其设置为整数。这似乎工作正常。

这种做法有什么问题吗?它是便携式的,还是会在其他显卡/驱动程序上损坏?我目前使用的是 OpenGL 4.3。

§ 4.1 Basic Types The OpenGL Shading Language supports the following basic data types, grouped as follows:

  • bool a conditional type, taking on values of true or false
  • bvec2 a two-component Boolean vector
  • bvec3 a three-component Boolean vector
  • bvec4 a four-component Boolean vector

...

§ 4.1.2 Booleans To make conditional execution of code easier to express, the type bool is supported. There is no expectation that hardware directly supports variables of this type. (...)

至于设置:

§ 2.2.1 (...) When state values are specified using a different parameter type than the actual type of that state, data conversions are performed as follows:

  • When the type of internal state is boolean, zero integer or floating-point values are converted to FALSE and non-zero values are converted to TRUE.

glsl bool 的大小是 32 位,与 int 相同,所以是的,您可以这样设置。

不,它不会在其他 OpenGL 显卡/驱动程序上中断。