glsl-optimizer icon indicating copy to clipboard operation
glsl-optimizer copied to clipboard

precision issues on iOS

Open rraallvv opened this issue 9 years ago • 0 comments

The problem is that the precision that the optimizer sticks to the temporary variables is causing the draw call to fail sometimes, with error 0x502.

This is a snippet from the shader without optimization:

precision mediump int;
precision mediump float;
uniform float normal_mult;
varying vec3 vertex_interp;
varying vec3 normal_interp;
attribute highp vec4 vertex_attrib;
attribute vec3 normal_attrib;
uniform highp mat4 world_transform;
uniform highp mat4 camera_inverse_transform;
void main ()
{
    highp mat4 modelview = (camera_inverse_transform * world_transform);
    highp vec4 vertex_in = vertex_attrib;
    vertex_interp = (modelview * vertex_in).xyz;
    vec3 normal_in = normal_attrib;
    normal_in*=normal_mult;
    normal_interp = normalize((modelview * vec4(normal_in,0.0)).xyz);
}

Which is optimized into this:

precision mediump float;
precision mediump int;
uniform float normal_mult;
varying highp vec3 vertex_interp;
varying highp vec3 normal_interp;
attribute highp vec4 vertex_attrib;
attribute vec3 normal_attrib;
uniform highp mat4 world_transform;
uniform highp mat4 camera_inverse_transform;
void main ()
{
    highp mat4 tmpvar_1;
    tmpvar_1 = (camera_inverse_transform * world_transform);
    vertex_interp = (tmpvar_1 * vertex_attrib).xyz;
    vec4 tmpvar_2;
    tmpvar_2.w = 0.0;
    tmpvar_2.xyz = (normal_attrib * normal_mult);
    normal_interp = normalize((tmpvar_1 * tmpvar_2).xyz);
}

If I use the optimized shader as is, the call to glDrawElements fails with error 0x502 (a.k.a. invalid operation)

If I set tmpvar_2.w to a value other than zero, or change the declaration of tmpvar_2 to highp vec4 tmpvar_2; the shader works ok.

The optimized shader is causing troubles on the iPod touch 5g running iOS 7.1.2, although it's running ok on the simulator.

I have to modify programmatically the optimized shader after the optimization, so my question is whether as a rule of thumb it's ok to propagate the precision of the variable holding the result to those temporary variables involved in the calculation.

For instance since normal_interp have highp, both tmpvar_1 and tmpvar_2 should be highp too, since those are temporary variables.

rraallvv avatar Dec 27 '14 00:12 rraallvv