Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Point Frag Shader: If statement causes inconsistent color output.

  1. #1

    Point Frag Shader: If statement causes inconsistent color output.

    Hello forum,


    I was implementing some point rendering, and I've noticed strange behavior in the frag shader.

    The code is very basic, just get the vertex, render if alpha is 1, otherwise discard the fragment. The problem is with the IF statement, for whatever reason an IF statement causes the color output change between 0 and Color whenever the camera is moved. All values I input are proper, in fact, the Depth output shows that the points render properly. Also the assigned stay the same, they just change between 0 and whatever assigned color is.

    The code:

    Code :
    // shadertype=glsl
    #version 450
    layout(early_fragment_tests) in;
     
    in float	object_size;
    in vec3		object_color;
    in float	object_alpha;
    in float	object_depth;
     
    layout(location = 0) out vec4 stage2ColorMap;
     
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    void main() {	
    	float fragmentAlpha = clamp(object_alpha, 0.0, 1.0);
     
    	if (fragmentAlpha == 1.0) {
    			stage2ColorMap = vec4(object_color, 1.0);
    	} else {
    		discard;
    	}
    }

    Camera at position 1:



    Camera at position 2:



    Depth buffer output (notice how everything is shown as rendered):




    Without the IF statement the color outputs just fine.

    I could not find any info on the possible source of the issue. Is this a bug?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Sep 2013
    Posts
    186
    With floats you shouldnt be testing for exact equality. There can always be rounding errors. Just check for a reasonably small epsilon around 1.0.

  3. #3
    With floats you shouldnt be testing for exact equality. There can always be rounding errors. Just check for a reasonably small epsilon around 1.0.

    But I am not doing any operations on the value on the client side, it's literally either 1.0 or 0.0.

    Also if it was an approximation issue, the issue would be persistent regardless of camera movement, since alpha variable that I pass is constant in my case.

    Also I do clamp the value just in case. This also works just fine on the GL_TRIANGLES, which again makes me feel like it's a bug.


    At least I can work around that with GL_ALPHA_TEST, so it's not a show stopper.
    Last edited by CaptainSnugglebottom; 02-21-2018 at 01:30 PM.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,007
    Does this work?

    Code :
    void main() {	
    	float fragmentAlpha = clamp(object_alpha, 0.0, 1.0);
    	stage2ColorMap = vec4(object_color, 1.0);
    	if (fragmentAlpha < 1.0)
    		discard;
    }

    Other than that, we'd need more details: version, profile, whether multi-sampling is used, any other parameters affecting point rasterisation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •