Advertisement

Bloom Fail

Started by March 10, 2012 09:18 AM
7 comments, last by L. Spiro 12 years, 10 months ago
I have just implemented bloom in my engine but on the OpenGL side I have these black artifacts.
[attachment=7631:Ogl0.png][attachment=7628:Ogl1.png]

In Direct3D 9 they are fine.
[attachment=7629:Dx0.png][attachment=7630:Dx1.png]


They are both using the exact same shaders, being fed the exact same data, and using the same HDR formats (16-bit float).


I know that the problem is in the blur filters, both horizontal and vertical, but I can’t see why it is happening, especially when it doesn’t happen in Direct3D 9.

Horizontal blur.


#version 130
precision mediump float;
invariant gl_Position;
float shadow2dDepth(sampler2D s,vec2 v){return texture(s,v).z;}
out vec2 LSG_VERT_OUT_PIXEL_IN_379_0/*_vOut2dTex0*/;
in vec2 _vIn2dTex0;
in vec3 _vInPos;
uniform mat4x4 g_mModelViewProjMatrix;

void main(){
gl_Position = ((g_mModelViewProjMatrix) * (vec4( _vInPos, 1.0 )));

LSG_VERT_OUT_PIXEL_IN_379_0/*_vOut2dTex0*/ = _vIn2dTex0;
}

#version 130
precision mediump float;
out vec4 lsg_vColor[8];
float shadow2dDepth(sampler2D s,vec2 v){return texture(s,v).z;}
out vec4 _vOutColor;
in vec2 LSG_VERT_OUT_PIXEL_IN_379_0/*_vIn2dTex0*/;
in vec4 LSG_VERT_OUT_PIXEL_IN_375_0/*_vInPos*/;

uniform sampler2D g_sSampler2dTex0;

uniform float g_fOffsets[9];
uniform float g_fWeights[9];

void main() {
_vOutColor = vec4( 0.0, 0.0, 0.0, 0.0 );

for ( int I = 0; I < 9; I++ ) {
_vOutColor += (texture( g_sSampler2dTex0, LSG_VERT_OUT_PIXEL_IN_379_0/*_vIn2dTex0*/ +
vec2( g_fOffsets, 0.0 ) ) * g_fWeights);

}

_vOutColor.w = 1.0;
}


I know it looks a little strange. This was generated output from my own shader language, and I added back some of the whitespace to make it more readable.

g_fOffsets has normal-looking values from -0.0049999999 to 0.0049999999.

g_fWeights:

[0x0] 0.032394581
[0x1] 0.077710561
[0x2] 0.14518245
[0x3] 0.21123920
[0x4] 0.23936538
[0x5] 0.21123920
[0x6] 0.14518245
[0x7] 0.077710561
[0x8] 0.032394581


The only difference with the vertical pass is g_fOffsets, which is normal because it is based off the height instead of the image.
Any ideas?


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Those black artifacts are often indicative of NaN Pixels which get drawn as black on most GPUs.

NaN mostly results from divisions by 0. One typical example being 0/0.

Since any operation with a NaN value results in a NaN, you then get those black block artifacts when applying a filter.

You can test this in GLSL at the end of you shader with this code snippet:

if (any(isnan(color))) {
color = vec4(1,0,0,1);
}


This will set NaN pixels to red.
Advertisement
That was helpful information.

I found out that my bright pass is writing NaN values to the texture due to a dot() with a zero-vector (black texels).
Why it doesn’t have to all black texels I have no clue, but I applied the appropriate checks and fixed it.


Thank you.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


I found out that my bright pass is writing NaN values to the texture due to a dot() with a zero-vector (black texels).


Dot has no division, only multiplications... so it should not be the cause of NaN-value. The issue must be somewhere before that.. like it already was a NaN-value vector. Possible cause exponent of the floating point is not correctly interpreted (all bits are set to 1, according to IEEE-754).
In glsl, the pow(x, y) function return value is undefined if x < 0 or if x = 0 && y = 0. I once had a situation where the specular multiplier and specular exponent of a specular map were both zero at some texels, which caused pow to return NaN. You can also check for that.
I guess I was thinking dot() was a target because the returned value is relative to the lengths of the input vectors, and as we all know, a 0-vector has to be handled as a special case in order to avoid division by 0.

However it is correct in reality that both dot() and cross() have no divides.

Except apparently in OpenGL and OpenGL ES 2 implementations.
It was the cause for the large black blocks. Simply by changing from this (bright pass, the only pass before doing a horizontal blur):


float fLuminance = dot( vAverage.rgb, vWeight );
if( fLuminance < g_fThresh ) {
vAverage = vec4( 0.0f, 0.0f, 0.0f, 1.0f );
}

to this:

float fLuminance = dot( vAverage.rgb, vWeight );
if( fLuminance < g_fThresh || isnan( fLuminance ) ) {
vAverage = vec4( 0.0f, 0.0f, 0.0f, 1.0f );
}

made the blocks go away. I rendered the result of the horizontal blue before and after this change and confirmed that without this change, the first blur has black streaks, with the change it is perfect.


Meanwhile, since a typical dot() and cross() do not have divides, Direct3D 9 was working fine.
This appears to be unnecessary extra work being done by OpenGL. There is no reason for it to generate NaN here, unless it is specifically testing for the case of a 0-length input vector.


I do still have black dots (rather than blocks) and those appear in Direct3D 9 also, but I haven’t yet been able to find the cause.
Since I can render the bright pass and blurs, and they are always perfect, I know that it is in the last stage which combines them and does the tone mapping.

Anyway I will find it eventually.


L. Spiro


[EDIT]
I was thinking about another idea I have been entertaining—that the source image has NaN values in it that I could never see before because they are black pixels and the end result is still black.
Just tested it and it is correct.
It isn’t in the pow() but it is in the main image result.
I will remove my dot() checks since they are now superfluous, and will find the actual cause of my woes soon.
[/EDIT]

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Advertisement

However it is correct in reality that both dot() and cross() have no divides.

Except apparently in OpenGL and OpenGL ES 2 implementations.
It was the cause for the large black blocks. Simply by changing from this (bright pass, the only pass before doing a horizontal blur):


I don't think that makes sense. As far as I know, the only way for dot to return NaN is if one of the components are NaN. Are you sure the input is good..?
Have you checked if the inputs to [font=courier new,courier,monospace]dot[/font] are [font=courier new,courier,monospace]nan[/font]s? e.g.if( any(isnan( vAverage.rgb )) || any(isnan( vWeight )) ) {
vAverage = vec4( 0.0f, 1.0f, 0.0f, 1.0f );
}
else
{
float fLuminance = dot( vAverage.rgb, vWeight );
...
}
For both replies, read my edit.

The dot() was not the culprit, the source image was. Something I had not considered accepting before because the results were always exactly what I had expected (a [0,0,0] input generates a [0,0,0] output regardless of any Nan’s).
I changed my checks for isnan() to those output by my main shader (which generates the image to be pre-processed), which I have used since the beginning, and that alone eliminated all errors in OpenGL.
Actually, now only Direct3D 9 has remaining problems.

However my check for isnan() comes at the very end of that shader, and is present in ALL variants. If a NaN is present, it cannot possibly be due to an NaN value being stored in the main image. So now OpenGL and OpenG ES 2 are working while Direct3D 9 is in the wrong. With my above change, only Direct3D 9 has black artifacts, and OpenGL is entirely 100% perfect, including the overall luminance calculation which was also sometimes turning full black due to NaN.

From here out I need to focus on the Direct3D side of things, but I gained enough insight from this topic that I should be able to solve the remaining issues on my own.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement