GLSL colour output is off by one as RGB - opengl

In GLSL, outputColour = vec3(0, 0, 0.5) outputs an RGB value of (0, 0, 127) instead of (0, 0, 128). Confirmed by GL.ReadPixels() and the Photoshop eyedropper tool.
Currently I'm bypassing the issue with outputColour.b += 0.001 but I sure that will come back to haunt me later.
Has any body experienced this before and what is the solution?

Whenever OpenGL is asked to convert a float to a normalized integer, the implementation is allowed to do the rounding however it likes. So if it wants to chop off the decimal, that's fine.
If you need to control the rounding for normalization, then control it directly.
vec4 NormalizeColor(in vec4 input)
{
vec4 denorm = input * 255.0;
vec4 rounded = round(denorm);
return rounded / 255.0;
}
You can replace the call to round with whatever you like.

Related

Pass array of floats to fragment shader via texture

I am trying to pass an array of floats (in my case an audio wave) to a fragment shader via texture. It works but I get some imperfections as if the value read from the 1px height texture wasn't reliable.
This happens with many combinations of bar widths and amounts.
I get the value from the texture with:
precision mediump float;
...
uniform sampler2D uDisp;
...
void main(){
...
float columnWidth = availableWidth / barsCount;
float barIndex = floor((coord.x-paddingH)/columnWidth);
float textureX = min( 1.0, (barIndex+1.0)/barsCount );
float barValue = texture2D(uDisp, vec2(textureX, 0.0)).r;
...
If instead of the value from the texture I use something else the issue doesn't seem to be there.
barValue = barIndex*0.1;
Any idea what could be the issue? Is using a texture for this purpose a bad idea?
I am using Pixi.JS as WebGL framework, so I don't have access to low level APIs.
With a gradient texture for the data and many bars the problems becomes pretty evident.
Update: Looks like the issue relates to the consistency of the value of textureX.
Trying different formulas like barIndex/(barsCount-1.0) results in less noise. Wrapping it on a min definitely adds more noise.
Turned out the issue wasn't in reading the values from the texture, but was in the drawing. Instead of using IFs I switched to step and the problem went away.
vec2 topLeft = vec2(
paddingH + (barIndex*columnWidth) + ((columnWidth-barWidthInPixels)*0.5),
top
);
vec2 bottomRight = vec2(
topLeft.x + barWidthInPixels,
bottom
);
vec2 tl = step(topLeft, coord);
vec2 br = 1.0-step(bottomRight, coord);
float blend = tl.x * tl.y * br.x * br.y;
I guess comparisons of floats through IFs are not very reliable in shaders.
Generally mediump is insufficient for texture coordinates for any non-trivial texture, so where possible use highp. This isn't always available on some older GPUs, so depending on the platform this may not solve your problem.
If you know you are doing 1:1 mapping then also use GL_NEAREST rather than GL_LINEAR, as the quantization effect will more likely hide some of the precision side-effects.
Given you probably know the number of columns and bars you can probably pre-compute some of the values on the CPU (e.g. precompute 1/columns and pass that as a uniform) at fp32 precision. Passing in small values between 0 and 1 is always much better at preserving floating point accuracy, rather than passing in big values and then dividing out.

GLSL If always returns true

I have float brightness with a dot production of 2 vectors in it. brightness should be positive most of the time, but to make sure I use max(brightness, 0.0). When I try to use brightness in my lighting system nothing shows on display. I tried to debug this with if-statement, but it seems it returns always true or something. So if(brightness > 99999) and if(brightness < 99999) or if(brightness == 12345) all discards the pixel. I am using GLSL version 430 core.
float brightness = dot(vector1, vector2);
brightness = max(brightness, 0.0);
if(brightness < -999) {
discard;
}
So in this example ^ it will discard the pixel.
You most probably have a NaN in vector1 or vector2 components (NaN => not a number, can happen if there is a division by zero during vector1 or vector2 computations).
Please see this for a way to detect NaN:
How to deal with NaN or inf in OpenGL ES 2.0 shaders
So to solve the issue you need to look at how vector1 & vector2 are computed.
In order to confirm this is a NaN issue, use constant values for vector1/vector2 like:
// force values so we are sure dot will be 1.0
vector1 = vec3(1.0,0.0,0.0);
vector2 = vec3(1.0,1.0,0.0);
float brightness = dot(vector1, vector2);
brightness = max(brightness, 0.0);
// so this should never execute since dot is 1.0
if(brightness < -999) {
discard;
}
It is totally impossible that the discard be executed with code above.
If you think this still happens, only other possible explanation would be that the code after the if test would be setting fragColor.rgb to black or fragColor.a to zero causing no pixel to display if blending is enabled.

What could cause a variable to behave as infinite and simultaneously as non-infinite?

To learn opengl, I'm creating a simple 3D graphics engine using openGL 3.3. I've recently added light attenuation over distance; this has turned all objects completely black. This was done by adding the following code to my light calculations in the fragment shader:
float distance = length(lite.position - FragPos);
float attenuation = 1.0f/(lite.constant + (lite.linear * distance) + (lite.quadratic * (distance * distance)));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
result += (ambient + diffuse + specular);
It seems safe to assume that attenuation is very small, effectively or actually 0, or negative (black). To test this I use result += vec3(attenuation);, the result of this is white objects; this then indicates that attenuation is not near 0 and instead 1.0 or larger; an additional test trying result += vec3(attenuation/500000); still produces white, which indicates that attenuation is quite large, perhaps infinite. I did some infinity and NaN checks on it. NaN checks told me it is a number, infinity checks tell me it is sometimes infinite and sometimes isn't. In fact it told me that it is both infinite and not infinite at the same time. I determined this by using the following code segment:
if(isinf(attenuation)){
result += vec3(1.0, 0.0, 0.0);
}
if(isinf(attenuation) && !isinf(attenuation)){
result += vec3(0.0, 1.0, 0.0);
}
if(!isinf(attenuation)){
result += vec3(0.0, 0.0, 1.0);
}
My objects turned purple/magenta. Were attenuation infinite, I would expect my objects to appear red; were they not infinite, I would expect them to appear blue; were they somehow both infinite and not infinite I would expect them to appear green. If I make the result += ... to be result = ..., the objects appear red. In this case, were it both infinite and not infinite, as my purple objects suggest, result would first be set to red, and then set to blue, resulting in blue objects (if somehow the green check fails).
I hope this describes the source of my confusion. My testing shows that attenuation is infinite, and that it is not infinite, AND that it is neither.
To top everything off when I use:
float attenuation = 1.0f/(1.0 + (0.0014 * distance) + (0.000007* (distance * distance)));
to determine the attenuation factor, everything works exactly as expected; however the values shown here as constants are exactly what's passed in from my openGL calls (c++):
glUniform1f(lightConstantLoc, 1.0f);
glUniform1f(lightLinearLoc, 0.0014f);
glUniform1f(lightQuadraticLoc, 0.000007f);
From there I should conclude that my data is not being delivered to my shaders correctly, however I'm confident my lite.constant etc values have been set correctly, and that distance is a reasonable value. When I single each one out as a color, the objects do turn that color.i.e.: using this
result = vec3(lite.constant, 0.0, 0.0);
my objects turn some shade of red, for lite.constant, lite.linear etc.
Searching google and stack overflow for things like "glsl isinf true and false" or "glsl variable is and isn't infinite" gives me absolutely no relevant results.
I get the feeling I'm distinctly ignorant of something happening here, or the way something works. And so I turn to you, am I missing something obvious, doing this all wrong, or is this a true mystery?
I'm not sure why your attenuation is so large, but the explanation for your is/isn't infinite issue is simple -- at least one component of attenuation is infinite, while at least one of the other components is not.
When you do if (bvec) -- testing a condition that is a boolean vector rather than a single boolean -- it acts as if you did if (any(bvec)). So it will execute the associated true branch if any of the components are true. When you have isinf(attenuation), you get a boolean vector. For example, if the red is inifinite and the others are not, you'll get (true, false, false). So !isinf(attenuation) will be (false, true, true), and the result of the && in the middle if is (false, false, false).
So it executes the first and third if (and not the second), giving you magenta.
The problem lies in code that is not shown in the question.
The crucial piece of information is that my shader supports up to 5 light sources, and iterates across all 5 of them; even when fewer than 5 light sources are provided. With this in mind changing
vec3 result = vec3(0.0);
for(int i = 0; i < 5; ++i){
Light lite = light[i];
...
to
vec3 result = vec3(0.0);
for(int i = 0; i < 1; ++i){
Light lite = light[i];
...
solves the problem, and everything now behaves perfectly. It seems that data that does not exist is neither infinite nor finite; which makes sense to some degree.

How to output fragment values greater than 1 in WebGL

Let's say I am rendering 2 samples that will be combined into a single image. The first sample contains values outside the range of a displayable pixel (in this case, greater than 1). But when subtracted by the 2nd sample, it does fall in the range.
I store the samples in framebuffer textures prior to combining them.
I want to be able to store values greater than 1, but those values are being clamped to 1. Can the GLSL fragment shader output such values? Can textures store them? If not, how else can I store them?
According to this page, it is possible:
rendering to screen requires the outputs to be of a displayable format, which is not always the case in a multipass pipeline. Sometimes the textures produced by a pass need to have a floating point format which does not translate directly to colors
But according to the specification, texture floats are clamped to the range [0,1].
The easiest way is to use floating point textures.
var gl = someCanvasElement.getContext("experimental-webgl");
var ext = gl.getExtension("OES_texture_float");
if (!ext) {
alert("no OES_texture_float");
return;
}
now you can create and render with floating point textures. The next thing to do is see if you can render to floating point textures.
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, null);
gl.texParameteri(gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_MAG_FILTER, gl.NEAREST);
var fb = gl.createFramebuffer();
gl.bindFrameBuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if (status != gl.FRAMEBUFFER_COMPLETE) {
alert("can not render to floating point textures");
return;
}
Floats are not clamped when using OES_texture_float
If the device doesn't support rendering to a floating point texture then you'd have to encode your results some other way like gil suggests
WebGL2
Note: in WebGL2 floating point textures are always available. On the other hand you still have to check for and enable OES_texture_float_linear if you want to filter floating point textures. Also in WebGL2 you need to enable EXT_color_buffer_float to render to a floating point texture (and you still need to call gl.checkFramebufferStatus since it's up to the driver which combinations of attachments are supported). And further, there's EXT_float_blend for whether or not you can have blending enabled when rendering to a floating point texture.
Fragment shaders can output values outside the [0.0, 1.0] range, but only if the format of the buffer the values are written to supports values outside that range. What is needed to enable this are render targets (renderbuffers or textures attached to an FBO) that store float values.
OpenGL ES 2.0 and lower do not require support for float format textures. OpenGL ES 3.0 and higher do. For example, in ES 3.0 you could use GL_RGBA16F for a RGBA texture with 16-bit float (aka half-float) components, and GL_RGBA32F for 32-bit float components. Both ES 3.0 and 3.1 still do not require support for using these formats as render targets, though, which is what you need for this use case.
ES 2.0 implementations can provide half-float textures by supporting the OES_texture_half_float and float textures by supporting the OES_texture_float extension. To support rendering to half-float textures, they also need EXT_color_buffer_half_float. EXT_color_buffer_float defines rendering to float textures, but is specified to be based on ES 3.0.
In summary:
ES 2.0 and higher can support rendering to 16-bit float textures by supporting both the OES_texture_half_float and EXT_color_buffer_half_float extensions.
ES 3.0 and higher can support rendering to 32-bit float textures by supporting both the OES_texture_float and EXT_color_buffer_float extensions.
If you want to use these features, you will have to test for the presence of these extensions on your device.
The key idea here is to encode a float in some unrestricted range using 2 or 4 fixed point 8 bit channels (color channels) in the range [0,1]. This method is generic and applies to WebGL or any other GL system.
Let's say you start with a float value:
float value;
Assume your machine support mediump (16 bit float), you can encode value using
2 8 bit channels:
float myNormalize(float val)
{
float min = -1.0;
float max = 1.0;
float norm = (val - min) / (max - min);
return norm;
}
vec2 encode_float_as_2bytes(float a)
{
a = myNormalize(a);
vec2 enc = vec2(1.0, 256.0);
enc *= a;
enc = fract(enc);
enc.x -= enc.y * (1.0 / 256.0);
return enc;
}
Here encode_float_as_2bytes(float a) accepts the value to be encoded. The value is first normalized to [0,1], using some bounding values (on my example my float can take values in[-1, 1]. After normalization, the value is encoded using vec2.
Now you can write the encoded value to the color buffer:
float a = compute_something(...);
gl_FragColor.xy = encode_float_as_2bytes(a);
Now when reading the encoded values (either by other shader or using glReadPixels(), you can decode the encoded float and get the value back:
float denormalize(float val)
{
float min = -1.0;
float max = 1.0;
float den = val * (max - min) + min;
return den;
}
float decode_2_bytes(vec2 a)
{
float ret;
ret = a.x * 1.0 + a.y * 1.0/256.0;
ret = denormalize(ret);
return ret;
}
Pay attention that the denormalization values have to match the normalization values (on this example -1, 1.
You can find more about float encoding here: http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/

How to debug a GLSL shader?

I need to debug a GLSL program but I don't know how to output intermediate result.
Is it possible to make some debug traces (like with printf) with GLSL ?
You can't easily communicate back to the CPU from within GLSL. Using glslDevil or other tools is your best bet.
A printf would require trying to get back to the CPU from the GPU running the GLSL code. Instead, you can try pushing ahead to the display. Instead of trying to output text, output something visually distinctive to the screen. For example you can paint something a specific color only if you reach the point of your code where you want add a printf. If you need to printf a value you can set the color according to that value.
void main(){
float bug=0.0;
vec3 tile=texture2D(colMap, coords.st).xyz;
vec4 col=vec4(tile, 1.0);
if(something) bug=1.0;
col.x+=bug;
gl_FragColor=col;
}
I have found Transform Feedback to be a useful tool for debugging vertex shaders. You can use this to capture the values of VS outputs, and read them back on the CPU side, without having to go through the rasterizer.
Here is another link to a tutorial on Transform Feedback.
GLSL Sandbox has been pretty handy to me for shaders.
Not debugging per se (which has been answered as incapable) but handy to see the changes in output quickly.
You can try this: https://github.com/msqrt/shader-printf which is an implementation called appropriately "Simple printf functionality for GLSL."
You might also want to try ShaderToy, and maybe watch a video like this one (https://youtu.be/EBrAdahFtuo) from "The Art of Code" YouTube channel where you can see some of the techniques that work well for debugging and visualising. I can strongly recommend his channel as he writes some really good stuff and he also has a knack for presenting complex ideas in novel, highly engaging and and easy to digest formats (His Mandelbrot video is a superb example of exactly that : https://youtu.be/6IWXkV82oyY)
I hope nobody minds this late reply, but the question ranks high on Google searches for GLSL debugging and much has of course changed in 9 years :-)
PS: Other alternatives could also be NVIDIA nSight and AMD ShaderAnalyzer which offer a full stepping debugger for shaders.
If you want to visualize the variations of a value across the screen, you can use a heatmap function similar to this (I wrote it in hlsl, but it is easy to adapt to glsl):
float4 HeatMapColor(float value, float minValue, float maxValue)
{
#define HEATMAP_COLORS_COUNT 6
float4 colors[HEATMAP_COLORS_COUNT] =
{
float4(0.32, 0.00, 0.32, 1.00),
float4(0.00, 0.00, 1.00, 1.00),
float4(0.00, 1.00, 0.00, 1.00),
float4(1.00, 1.00, 0.00, 1.00),
float4(1.00, 0.60, 0.00, 1.00),
float4(1.00, 0.00, 0.00, 1.00),
};
float ratio=(HEATMAP_COLORS_COUNT-1.0)*saturate((value-minValue)/(maxValue-minValue));
float indexMin=floor(ratio);
float indexMax=min(indexMin+1,HEATMAP_COLORS_COUNT-1);
return lerp(colors[indexMin], colors[indexMax], ratio-indexMin);
}
Then in your pixel shader you just output something like:
return HeatMapColor(myValue, 0.00, 50.00);
And can get an idea of how it varies across your pixels:
Of course you can use any set of colors you like.
At the bottom of this answer is an example of GLSL code which allows to output the full float value as color, encoding IEEE 754 binary32. I use it like follows (this snippet gives out yy component of modelview matrix):
vec4 xAsColor=toColor(gl_ModelViewMatrix[1][1]);
if(bool(1)) // put 0 here to get lowest byte instead of three highest
gl_FrontColor=vec4(xAsColor.rgb,1);
else
gl_FrontColor=vec4(xAsColor.a,0,0,1);
After you get this on screen, you can just take any color picker, format the color as HTML (appending 00 to the rgb value if you don't need higher precision, and doing a second pass to get the lower byte if you do), and you get the hexadecimal representation of the float as IEEE 754 binary32.
Here's the actual implementation of toColor():
const int emax=127;
// Input: x>=0
// Output: base 2 exponent of x if (x!=0 && !isnan(x) && !isinf(x))
// -emax if x==0
// emax+1 otherwise
int floorLog2(float x)
{
if(x==0.) return -emax;
// NOTE: there exist values of x, for which floor(log2(x)) will give wrong
// (off by one) result as compared to the one calculated with infinite precision.
// Thus we do it in a brute-force way.
for(int e=emax;e>=1-emax;--e)
if(x>=exp2(float(e))) return e;
// If we are here, x must be infinity or NaN
return emax+1;
}
// Input: any x
// Output: IEEE 754 biased exponent with bias=emax
int biasedExp(float x) { return emax+floorLog2(abs(x)); }
// Input: any x such that (!isnan(x) && !isinf(x))
// Output: significand AKA mantissa of x if !isnan(x) && !isinf(x)
// undefined otherwise
float significand(float x)
{
// converting int to float so that exp2(genType) gets correctly-typed value
float expo=float(floorLog2(abs(x)));
return abs(x)/exp2(expo);
}
// Input: x\in[0,1)
// N>=0
// Output: Nth byte as counted from the highest byte in the fraction
int part(float x,int N)
{
// All comments about exactness here assume that underflow and overflow don't occur
const float byteShift=256.;
// Multiplication is exact since it's just an increase of exponent by 8
for(int n=0;n<N;++n)
x*=byteShift;
// Cut higher bits away.
// $q \in [0,1) \cap \mathbb Q'.$
float q=fract(x);
// Shift and cut lower bits away. Cutting lower bits prevents potentially unexpected
// results of rounding by the GPU later in the pipeline when transforming to TrueColor
// the resulting subpixel value.
// $c \in [0,255] \cap \mathbb Z.$
// Multiplication is exact since it's just and increase of exponent by 8
float c=floor(byteShift*q);
return int(c);
}
// Input: any x acceptable to significand()
// Output: significand of x split to (8,8,8)-bit data vector
ivec3 significandAsIVec3(float x)
{
ivec3 result;
float sig=significand(x)/2.; // shift all bits to fractional part
result.x=part(sig,0);
result.y=part(sig,1);
result.z=part(sig,2);
return result;
}
// Input: any x such that !isnan(x)
// Output: IEEE 754 defined binary32 number, packed as ivec4(byte3,byte2,byte1,byte0)
ivec4 packIEEE754binary32(float x)
{
int e = biasedExp(x);
// sign to bit 7
int s = x<0. ? 128 : 0;
ivec4 binary32;
binary32.yzw=significandAsIVec3(x);
// clear the implicit integer bit of significand
if(binary32.y>=128) binary32.y-=128;
// put lowest bit of exponent into its position, replacing just cleared integer bit
binary32.y+=128*int(mod(float(e),2.));
// prepare high bits of exponent for fitting into their positions
e/=2;
// pack highest byte
binary32.x=e+s;
return binary32;
}
vec4 toColor(float x)
{
ivec4 binary32=packIEEE754binary32(x);
// Transform color components to [0,1] range.
// Division is inexact, but works reliably for all integers from 0 to 255 if
// the transformation to TrueColor by GPU uses rounding to nearest or upwards.
// The result will be multiplied by 255 back when transformed
// to TrueColor subpixel value by OpenGL.
return vec4(binary32)/255.;
}
I am sharing a fragment shader example, how i actually debug.
#version 410 core
uniform sampler2D samp;
in VS_OUT
{
vec4 color;
vec2 texcoord;
} fs_in;
out vec4 color;
void main(void)
{
vec4 sampColor;
if( texture2D(samp, fs_in.texcoord).x > 0.8f) //Check if Color contains red
sampColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); //If yes, set it to white
else
sampColor = texture2D(samp, fs_in.texcoord); //else sample from original
color = sampColor;
}
The existing answers are all good stuff, but I wanted to share one more little gem that has been valuable in debugging tricky precision issues in a GLSL shader. With very large int numbers represented as a floating point, one needs to take care to use floor(n) and floor(n + 0.5) properly to implement round() to an exact int. It is then possible to render a float value that is an exact int by the following logic to pack the byte components into R, G, and B output values.
// Break components out of 24 bit float with rounded int value
// scaledWOB = (offset >> 8) & 0xFFFF
float scaledWOB = floor(offset / 256.0);
// c2 = (scaledWOB >> 8) & 0xFF
float c2 = floor(scaledWOB / 256.0);
// c0 = offset - (scaledWOB << 8)
float c0 = offset - floor(scaledWOB * 256.0);
// c1 = scaledWOB - (c2 << 8)
float c1 = scaledWOB - floor(c2 * 256.0);
// Normalize to byte range
vec4 pix;
pix.r = c0 / 255.0;
pix.g = c1 / 255.0;
pix.b = c2 / 255.0;
pix.a = 1.0;
gl_FragColor = pix;
The GLSL Shader source code is compiled and linked by the graphics driver and executed on the GPU.
If you want to debug the shader, then you have to use graphics debugger like RenderDoc or NVIDIA Nsight.
I found a very nice github library (https://github.com/msqrt/shader-printf)
You can use the printf function in a shader file.
sue this
vec3 dd(vec3 finalColor,vec3 valueToDebug){
//debugging
finalColor.x = (v_uv.y < 0.3 && v_uv.x < 0.3) ? valueToDebug.x : finalColor.x;
finalColor.y = (v_uv.y < 0.3 && v_uv.x < 0.3) ? valueToDebug.y : finalColor.y;
finalColor.z = (v_uv.y < 0.3 && v_uv.x < 0.3) ? valueToDebug.z : finalColor.z;
return finalColor;
}
//on the main function, second argument is the value to debug
colour = dd(colour,vec3(0.0,1.0,1.));
gl_FragColor = vec4(clamp(colour * 20., 0., 1.),1.0);
Do offline rendering to a texture and evaluate the texture's data.
You can find related code by googling for "render to texture" opengl
Then use glReadPixels to read the output into an array and perform assertions on it (since looking through such a huge array in the debugger is usually not really useful).
Also you might want to disable clamping to output values that are not between 0 and 1, which is only supported for floating point textures.
I personally was bothered by the problem of properly debugging shaders for a while. There does not seem to be a good way - If anyone finds a good (and not outdated/deprecated) debugger, please let me know.