PDA

View Full Version : Conditional operator on samplers, failed



Gessos Paul
05-07-2016, 08:16 AM
In my nVidia with OpenGL 4.3, second approach works fine, but first approach fails with:

error C5208: Sampler needs to be a uniform (global or parameter to main), need to inline function or resolve conditional expressionis this a bug, or something else?
Just curiosity.


uniform sampler2DArray texRGB;
uniform sampler2DArray texRGBA;
//----------first approach
vec4 color = texture(isRGBA ? texRGBA : texRGB, coords);
//----------second approach
vec4 color;
if (isRGBA) color = texture(texRGBA, coords);
else color = texture(texRGB, coords);

Alfonse Reinheart
05-07-2016, 08:54 AM
A variable of sampler type can only be used, as part of an expression, as an argument to a function (you can use `[]` and `.length()` on arrays of samplers). The `?:` operator is not an argument to a function, so a sampler type cannot be used that way.

Also, be aware of the problems of using conditional logic with texture fetching. (https://www.opengl.org/wiki/Sampler_%28GLSL%29#Non-uniform_flow_control) So long as `inRGBA` is potentially non-uniform, then you won't get proper derivatives.

GClements
05-07-2016, 01:52 PM
vec4 color;
if (isRGBA) color = texture(texRGBA, coords);
else color = texture(texRGB, coords);

Alternatively:


vec4 color = isRGBA ? texture(texRGBA, coords) : texture(texRGB, coords);

But this has the same issue with the use of implicit derivatives in non-uniform control flow.

That can be avoided with:


vec4 color1 = texture(texRGBA, coords);
vec4 color2 = texture(texRGB, coords)
vec4 color = isRGBA ? color1 : color2;

or (probably more efficiently) with:


vec2 dx = dFdx(coords);
vec2 dy = dFdy(coords);
vec4 color = isRGBA ? textureGrad(texRGBA, coords, dx, dy) : texture(texRGB, coords, dx, dy);

Gessos Paul
05-07-2016, 05:16 PM
Oups!!!...
As a Red Book reader and beginner, I totally miss the need for this partial derivative thing.
Very helpful and interesting.
Thank you guys!

PS: I totally doesn't understand why on non-uniform flow, hardware cannot apply finite differences on neighbor fragments. (I ask just for curiosity).

Alfonse Reinheart
05-07-2016, 08:58 PM
I totally doesn't understand why on non-uniform flow, hardware cannot apply finite differences on neighbor fragments.

Because finite differences only work if the neighboring fragments are computing using the same sequence of operations. Once you're in non-uniform control flow, there is no guarantee that neighboring fragments are doing anything even remotely like one another.