问题
Say you have a vec3 colourIn going from a vertex shader to a frag shader, is there a way to test a value and overwrite it if you want to?
For example, set any fragment that has a blue value larger than 0.5 to the colour white?
In my Shader.frag I implemented this test:
if(colourIn.b>0.5){ //or if(greaterThan(colourIn.b,0.5))
colourIn.b=0.0;
}
It compiles and renders the scene, but I can't tell if it's worked because I'm colourblind (lol)... Have I got the theory right and implemented it correctly?
回答1:
You could directly write the conditional if you wanted, and your example should be correct, but a smarter move might be something like:
float mixValue = clamp(floor(colourIn.b * 2.0), 0.0, 1.0);
colourIn.b = mix(colourIn.b, 0.0, mixValue);
// the floor will be:
// 0.0 for [0.0 — 0.5);
// 1.0 for [0.5, 1.0)
// 2.0 1.0
//
// so the clamp will make mixValue:
// 0.0 for [0.0, 0.5]
// 1.0 for (0.5, 1.0]
//
// if you were to multiply by 1.99 then you could
// get rid of the clamp but if the input is a
// GLubyte then that'd move 128 into the low group
// instead of the high one
Which avoids the conditional and therefore any related pipeline stalls or parallelisation breakdowns.
来源:https://stackoverflow.com/questions/19844547/glsl-test-fragment-values