Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sharing variables in fragment shader GLSL

I have tried this fragment shader.
but the result is unexpected.

void main() {
   float test;
   if(gl_FragCoord.x==300.0){
       test=1.0;
   }


   gl_FragColor = vec4(test, 1,1,1);
} 

This program makes all fragments white colored.
But it’s unexpected because shaders run asynchronously for each fragment.
Note that the colour is dependent to value of test in if block.
For example if i change it to 0 it will fill all frags with lightblue.
I had expected to just fill fragments with x=300.
Please explain how ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This code exhibits undefined behavior, since you never initialize test.

Furthermore, even if you did, the condition itself will almost certainly never be fulfilled. gl_FragCoord is not required to be an integer, so you’re doing floating-point equality. The system is not required to provide exact values, so such equality tests will almost certainly fail.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading