PDA

View Full Version : Point Frag Shader: If statement causes inconsistent color output.



CaptainSnugglebottom
02-21-2018, 11:09 AM
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:



// 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:

https://image.ibb.co/eOkOVx/maybe_Bug1.png

Camera at position 2:

https://image.ibb.co/jN8MHc/maybe_Bug2.png

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

https://preview.ibb.co/k323Vx/maybe_Bug3.png


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?

Cornix
02-21-2018, 11:40 AM
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.

CaptainSnugglebottom
02-21-2018, 01:23 PM
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.

GClements
02-21-2018, 11:12 PM
Does this work?



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.