Jump to content

  • Log In with Google      Sign In   
  • Create Account


GLSL: Unique shader and Data Corruption!


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
12 replies to this topic

#1 Neosettler   Members   -  Reputation: 150

Like
0Likes
Like

Posted 12 January 2013 - 02:32 PM

Greetings, OpenGL Masters,

 

I've been using a unique shader for several years without problem and suddenly, between minors renderer modification and NVidia drivers upgrades, something went terribly wrong.

 

My shader is fairly complex but it can render any geometry with any material properties and light count. (I’m aware that this is not optimal but it’s very easy to maintain).

 

For instance, I do skinning like so:

 

uniform int u_DeformerCount;

in vec4 a_Weights;

in ivec4 a_Deformers;

uniform mat4 u_XFormMatrix[40]; /// Deformation matrices.

 

                if (u_DeformerCount > 0) /// Matrix deformations.

                {

                                mat4 l_deformer;

                               

                                for (int i = 0; i < 4; ++i) /// Maximum of 4 influences per vertex.

                                {

                                                l_deformer += a_Weights[i] * u_XFormMatrix[a_Deformers[i]];

 

SNIP....

                }

 

The problem:
While everything works without a glitch when u_DeformerCount > 0, there seems to be data corruption with geometries that doesn’t have the skinning condition enabled. I’m basically getting black frames frequently, like a kid playing with the light interrupter.

 

Now, I can make the problem disappeared by using u_XFormMatrix[0] instead of u_XFormMatrix[a_Deformers[i]]... I tried everything I could think of so far to fix this and I’m at the point where I could use Jedi Master’s wisdom.

 

- How could a part of the shader that is not used explicitly affect its output?

- Any known pitfall using uniform arrays?

 

PS: The major downside of a unique shader is that every uniforms needs to be set/reset every draw and I’m guessing it could be the source of my problem.



Sponsor:

#2 Nyssa   Members   -  Reputation: 426

Like
2Likes
Like

Posted 13 January 2013 - 02:10 AM

GPUs tend to compute both sides of an 'if' statement (true and false cases) then after it has done this it picks the side that matches the 'if' statement outcome. It does this because of how parallel processing is handled and doesn't really behave like 'if' statements on the CPU. So my guess would be your having errors because it's reading uninitialized data from "a_Deformers" then accessing data beyond the array bounds of "u_XFormMatrix". Try giving your data default values and see if your errors continue.

 

For example, when a mesh doesn't need skinning set all values of "a_Deformers" to zero, that way its elements are within your array bounds of "40". 


Edited by Nyssa, 13 January 2013 - 02:20 AM.


#3 Neosettler   Members   -  Reputation: 150

Like
0Likes
Like

Posted 13 January 2013 - 02:27 AM

Thx for your suggestion Nyssa,
For example, when a mesh doesn't need skinning set all values of "a_Deformers" to zero, that way its elements are within your array bounds of "40". 

Did you have in mind setting all values using glVertexAttribIPointer or in the shader some how?

EDIT: can't assign to varying in the shader. I'm guessing you meant glVertexAttribIPointer. PS: I do use glDisableVertexAttribArray a_Deformers when no skinning is needed. Still digging.

Edited by Neosettler, 13 January 2013 - 02:41 AM.


#4 Nyssa   Members   -  Reputation: 426

Like
2Likes
Like

Posted 13 January 2013 - 02:50 AM

If you were to do it from the shader then it would have to be via the preprocessor. For example:

#ifdef USE_SKINNING
if (u_DeformerCount > 0) /// Matrix deformations.
{
      mat4 l_deformer;
                               
      for (int i = 0; i < 4; ++i) /// Maximum of 4 influences per vertex.
      {
           l_deformer += a_Weights[i] * u_XFormMatrix[a_Deformers[i]];
      }
}
#endif

 

This has the bonus of removing code that is not needed for non skinned objects and may speed up your application a little.

 

Otherwise you can do it on the CPU during your programs initialization. If you have access to the raw vertex array data before it enters OpenGL then I'd simply change the values there to zero (EDIT : or remove them entirely!). Otherwise you would need to access it from OpenGL using a function like "glMapBuffer" and changing the values from there.


Edited by Nyssa, 13 January 2013 - 03:29 AM.


#5 Nyssa   Members   -  Reputation: 426

Like
0Likes
Like

Posted 13 January 2013 - 03:33 AM

Using glVertexAttribIPointer or glDisableVertexAttribArray isn't going to help on its own cause "a_Deformers" will still be uninitialized if you don't update your shader code aswell to remove the skinning portion.



#6 Neosettler   Members   -  Reputation: 150

Like
0Likes
Like

Posted 13 January 2013 - 03:54 AM

...


Edited by Neosettler, 13 January 2013 - 04:00 AM.


#7 Neosettler   Members   -  Reputation: 150

Like
0Likes
Like

Posted 13 January 2013 - 03:57 AM

Interesting, I was not aware we could do preprocessor #if. Is this a GLSL feature or you need to parse your shader and string edit it?

 

This has the bonus of removing code that is not needed for non skinned objects and may speed up your application a little.
Otherwise you can do it on the CPU during your programs initialization. If you have access to the raw vertex array data before it enters OpenGL then I'd simply change the values there to zero (EDIT : or remove them entirely!)


I agree but unless I’m missing something, while these seems to be valid solutions, it will make the unique shader concept collapse as we might alternate the skinning condition every draw.
 

Otherwise you would need to access it from OpenGL using a function like "glMapBuffer" and changing the values from there


This seems to be to most logical direction. The shader uses glBufferSubData for the vertex attributes, I’ll have to investigate glMapBuffer… this is puzzling. Thanks again for your inputs Nyssa.

 



#8 Nyssa   Members   -  Reputation: 426

Like
0Likes
Like

Posted 13 January 2013 - 04:24 AM

I believe preprocessing was available in glsl from version 1.10 (I think). EDIT : Check out the GLSL spec from page 9 here. And glBufferSubData is similar to glMapBuffer, you could use either one I guess.

 

 

I agree but unless I’m missing something, while these seems to be valid solutions, it will make the unique shader concept collapse as we might alternate the skinning condition every draw.

 

This is true. Is the idea of your unique shader to use the same shader for every object? While I understand the simplicity of that, it would result in alot of redundant calculations (such as skinning for non skinned objects). You could group skinned and non skinned objects together for rendering so there is no alternating?

 

A similar approach to what you're doing is called an Uber shader. Basically a shader with all possible combinations in it and uses alot of #defines to create the shader that's needed.


Edited by Nyssa, 13 January 2013 - 04:26 AM.


#9 mhagain   Crossbones+   -  Reputation: 6368

Like
0Likes
Like

Posted 13 January 2013 - 08:26 AM

If I'm going down that route I prefer a collection of "mini-uber" shaders.  Instead of one single great-big-whopping shader for everything, isolate out maybe 4 to 5 different types of object - something like unskinned mesh, skinned mesh, particles, 2D GUI, or whatever - then build an uber shader for each of those.  Using GLSL's compilation model you can quite easily insert snippets of common code too, so if your lighting model needs to be shared between more than one shader type, just split it off and add it to your array of GLchar * passed to glShaderSource.  That seems a bit more work on the C/C++ end of things, but you'll find it much more flexible than what you currently have.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#10 phantom   Moderators   -  Reputation: 5716

Like
2Likes
Like

Posted 13 January 2013 - 09:58 AM

GPUs tend to compute both sides of an 'if' statement (true and false cases) then after it has done this it picks the side that matches the 'if' statement outcome.

 

This depends very much on the GPU in question; anything in the last few years will have true branching support and only execute the code required by the outcome of the conditional compare the executing work group.

 

That means that if all your threads in the work group evaluate to 'true' it will ONLY take the true path, if 'false' then only the false path.

Now, if some threads are true and some are false then it will execute both paths BUT the GPU will mask out threads which aren't required to execute as such they have no impact on the running of the other threads.



#11 Neosettler   Members   -  Reputation: 150

Like
0Likes
Like

Posted 15 January 2013 - 11:37 AM

This is true. Is the idea of your unique shader to use the same shader for every object?

Yes indeed,
While I understand the simplicity of that, it would result in alot of redundant calculations

Well, I was under the impression a if statement is a if statement... apparently, the portion inside a condition that is not being use has to be considered has if it was in order for the shader to render correctly. Strangely, the bug is not constant and very hard to track. I did noticed however that it rendered correctly when removing few objects from my scene which use no textures. I must be missing something.

...digging the uber shader.

#12 Nyssa   Members   -  Reputation: 426

Like
0Likes
Like

Posted 15 January 2013 - 06:52 PM

As Phantom mentioned on newer cards your if statements would perform more like CPU if statements. But from the sounds of it your card might be a few years old so Uber shaders might be your answer. Like most good things in life there is a down side to them, they can become a bit of a nightmare to manage the larger they get.

 

Try to run your program through an app like gDEbugger or GPU PerfStudio so you can see what data OpenGL is receiving. Makes debugging a bit easier. Some objects might be overwriting uniform buffer data or something.



#13 Neosettler   Members   -  Reputation: 150

Like
0Likes
Like

Posted 25 January 2013 - 10:41 PM

very good find Nyssa with the gDEBugger, Nice tool! It turns out that my glitch was caused by the lack of calling glFinish!

case closed. thank you all for your inputs.

Cheers!




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