Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Vertex shader matrix multiplication broken?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 pontomedon   Members   -  Reputation: 273

Like
0Likes
Like

Posted 16 March 2014 - 12:31 PM

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...


Sponsor:

#2 pontomedon   Members   -  Reputation: 273

Like
2Likes
Like

Posted 16 March 2014 - 01:24 PM

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)!?

Edited by pontomedon, 16 March 2014 - 01:26 PM.


#3 JohnnyCode   Members   -  Reputation: 261

Like
0Likes
Like

Posted 16 March 2014 - 04:36 PM

consider near far plane values of projection transformation



#4 cgrant   Members   -  Reputation: 692

Like
1Likes
Like

Posted 16 March 2014 - 09:41 PM

is fragment shader precision level supported by the device ?



#5 pontomedon   Members   -  Reputation: 273

Like
0Likes
Like

Posted 17 March 2014 - 01:19 AM

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.


Edited by pontomedon, 17 March 2014 - 01:20 AM.


#6 JohnnyCode   Members   -  Reputation: 261

Like
0Likes
Like

Posted 17 March 2014 - 05:56 AM

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.



#7 pontomedon   Members   -  Reputation: 273

Like
0Likes
Like

Posted 17 March 2014 - 11:43 PM

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).



#8 JohnnyCode   Members   -  Reputation: 261

Like
-1Likes
Like

Posted 18 March 2014 - 09:21 AM

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



#9 pontomedon   Members   -  Reputation: 273

Like
0Likes
Like

Posted 19 March 2014 - 01:01 AM

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).



#10 JohnnyCode   Members   -  Reputation: 261

Like
0Likes
Like

Posted 19 March 2014 - 03:24 PM

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. 



#11 pontomedon   Members   -  Reputation: 273

Like
0Likes
Like

Posted 20 March 2014 - 01:10 AM

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. 

 

yes, a somehow broken uniform matrix is the only thing i can think of - but i have no clue how i could debug this.. any ideas? I already tried outputting constant color in the fragment shader (i.e. the most basic shader void main() {gl_FragColor = vec4(1.0);} has the same problem). I tried all i could think of in the vertex shader (i even put manual matrix multiplication there...)



#12 JohnnyCode   Members   -  Reputation: 261

Like
0Likes
Like

Posted 20 March 2014 - 07:00 AM

post the code where you set vertex shader input - attribpointers and attribute indexes. Maybe you have a dynamic factor somewhere there or are hiddenly using some default values.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS