Vertex shader matrix multiplication broken?

Started by
10 comments, last by JohnnyCode 10 years, 1 month ago
OpenGL ES 2.0 on Android: A very simple shader that works on a almost all devices, but doesn't on a Huawei Ascend P6:
vertex shader:

attribute vec2 a_position;
attribute vec2 a_texcoords;

uniform mat4 u_matrix;

varying mediump vec2 v_texcoords;

void main()
{
    gl_Position = u_matrix * vec4(a_position, 0.0, 1.0);
    v_texcoords = a_texcoords;
}

fragment shader:


uniform lowp sampler2D u_textureUnit;

varying mediump vec2 v_texcoords;

void main()
{
    gl_FragColor = texture2D(u_textureUnit, v_texcoords);  
}

The shader program is used to draw squares in the z=0 plane (as 4-vertex triangle strips) and a_position is given in world space. a_texcoords are standard (0,0)->(1,1) coordinates. u_matrix is set from a perspective camera, i.e. projectionmatrix*viewmatrix. when i change the distance in the view matrix (zooming in), at one point the tiles disappear, and only the clear color is shown, as if the fragment shader was never executed. when i zoom out again, i see tiles again.

Now the weird thing starts: when i (in the C++ code) transform the position to clip space and pass it to the shader as an attribute vec4 and write it unmodified to gl_Position, everything works as expected on the Huawei device. I have also compared the results of u_matrix * vec4(a_position, 0.0, 1.0) with the clip space position that i passed, and it turns out that they are *almost* equal to the clip space coordinates that i pass in from the cpu side. (in fact, i did the perspective division on both and then emitted vec4(1)-vec4(abs(difference).xyz*20.0,0.0) as a color - it was barely differentiable from white, so the difference is very small, i guess just different floating point arithmetics.)
any idea what's going on? scissor, depth and stencil tests are disabled, near and far planes of the camera are fine, backface culling is disabled. Feel free to ask for more detail...
Advertisement

To tackle this systematically, i first need to know what reasons there are for a fragment not ending up in the framebuffer - please correct me on anything i'm wrong about:

  1. primitive gets culled because it's offscreen
  2. primitive gets backface-culled
  3. fragment gets discarded because of a failed scissor test
  4. fragment gets discarded because of a failed stencil test
  5. fragment gets discarded because of a failed depth test
  6. fragment is not visible because of an alpha value of 0 if blending is enabled (which it is in my case)
  7. fragment is not written to the framebuffer because of a color write mask

did i miss out anything?

  1. should be fine because i compared the the computed clip space coordinates with my (working) clip space coordinates and they only differ by very little (not enough to push all the vertices off-screen). I'm not 100% sure about this, any thoughts about how to debug that?
  2. backface culling is disabled
  3. scissor test is disabled
  4. stencil test is disabled
  5. depth test is disabled
  6. the issue even appears if i do a gl_FragColor = vec4(1.0); as the only line in the fragment shader (the screen will then change from white to my clear color on zooming in), so i don't think this can be an issue
  7. i never do anything about the write masks in the code, and i remember having a glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE); in my init() at a point which didn't change anything. And why would it depend on on the values i put into gl_Position? On the other hand why does the clip space vector i calculate in the shader not work while the one i calculate on the cpu works fine if i checked them to be *almost* equal (see 1)!?

consider near far plane values of projection transformation

is fragment shader precision level supported by the device ?

consider near far plane values of projection transformation

I did already: but i just noticed that it must be related to the near plane: if i tilt the cam so that it doesn't look exactly into -Z, i get the artifact looking like near-plane clipping (i can zoom in to a state where only half the screen is blank). However, this happens when the cam is 1421 (meters) away from the z=0 plane, and my near plane is set to 65 (meters). I'm currently reviewing my projection matrix, but i don't get what is the specific thing about this particular GPU (and why it works if i do the transformation on the CPU)

is fragment shader precision level supported by the device ?

fragment shader uses only mediump, which is the minimum that must be supported in fragment shaders. moreover, even if i remove the varying mediump vec2 v_texcoords and just put a gl_FragColor=vec4(1.0,0.0,0.0,1.0); in the fragment shader, the bug still happens.

near and far plane values should be set to reflect the world space scale. those numbers very define zdepth distribtuion and what you observe. Near devided by far is the minimal depth step. so for example near 1.13 and far 100000.0 will observe 100000 world space units with precision 1.03 of world unit, a 1.03/10000.0 of projection space unit scale . This means you cannot observe world space values close like 0.01,0.02. You can observe a world of 0.0,1.0 world units, in this case you would set values about near=0.00001 far=1.0 and operate and obseve such scale with z depth accuracy. Setting near to zero would result in infinite accuracy, a number that possibly can't be stored, meaning that far/near number should reflect 32 bit capability in size towards the world scale.

I know, and both near and far are set appropriately - they might not be optimal regarding depth precision, but i don't use the depth test (i don't even have a depth buffer in place), so this is not an issue for me. As i said, i experience *something like* near plane clipping on a depth of ~1400 while my near plane is set to ~65. My far plane is at ~10.000, so that can't be the issue either (apart from the fact that far plane clipping would behave differently).

try to premultiply vector, not postmultiply. A stab in the dark, but nothing else comes to my mind

I must say I'm desperate enough to try anything - even something that cannot be mathematically correct - but no, that destroys it completely (nothing is drawn anymore).

if it is particulary on the device, and you do transform vector correctly on cpu, only thing I can think off is to make sure that the uniform matrix is constructed as you expect. Other possible thing is, try outputing constant color in fragment shader, you are now outputing fetched color, and if the alpha component is not 1.0, you may have alphatest enabled or something. Try recoding the vertex shader as much as you can to isolate what you can.

This topic is closed to new replies.

Advertisement