WebGL Float modulus behaviour

后端 未结 1 566
轮回少年
轮回少年 2021-01-24 19:07

I am experiencing strange behaviours for below fragment Shader:

varying vec3 worldSpaceCoords;

uniform float rows;

void main() {

    float testNumber = 7.0;
          


        
1条回答
  •  温柔的废话
    2021-01-24 19:35

    This is likely the result of imprecise floating-point math, which adheres to rules documented in the GLSL ES Specification. The highp qualifier guarantees that the inputs and outputs of the mod operator are 32-bit IEEE 754 floating point numbers, but the calculation itself is not guaranteed to produce the same high precision. In particular, the modulus operator is defined in Chapter 8.3 as

    genType mod (genType x, genType y)

    Modulus. Returns x – y * floor (x/y).

    The division operator a/b in this expression is guaranteed to have a precision of no less than 2.5 ULP for b (see chapter 4.5.1) and is typically computed as a*(1/b) since reciprocals and multiplications are cheaper than divisions. In your case, 1/testNumber is a compile-time constant and is probably computed to a different (higher) precision than 1/rows, which needs to be computed at run-time.

    You could try using integer math instead of floating point, as the integer modulus operator a%b doesn't suffer from such precision issues.

    0 讨论(0)
提交回复
热议问题