openglglslopengl-3geometry-shader

Does mod function returns highp float in OpenGL 3.3 core?


I am trying to understand why the following code generates the below error in a geometry shader:

#version 330 core

layout (points) in;
layout (line_strip, max_vertices = 256) out;

in int gs_timestampLabel[];

...

int numberChar[10];
int number = gs_timestampLabel[0];
numberChar[digit] = mod(number, 10);

...

I have also tried:

numberChar[digit] = number - 10 * floor(number/10); // number % 10

Error:

Error compiling shader: shaders/TimestampLabels.geometry_2.glsl
Shader log: ERROR: 0:774: 'assign' :  cannot convert from 'highp float' to 'highp int'

Does mod always return float? What shall be done to get this modulus operation correctly done using integers?


Solution

  • The mod function in GLSL is always a floating-point mod. GLSL 3.30 can implicitly convert integers to floats, but it will not implicitly convert floats to ints. Hence the error.

    If you want an integer mod, do what you would do in C++: use %.