Jump to content

  • Log In with Google      Sign In   
  • Create Account

Bloom Fail


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 L. Spiro   Crossbones+   -  Reputation: 13634

Like
0Likes
Like

Posted 10 March 2012 - 03:18 AM

I have just implemented bloom in my engine but on the OpenGL side I have these black artifacts.
Ogl0.png Ogl1.png

In Direct3D 9 they are fine.
Dx0.png 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[I], 0.0 ) ) * g_fWeights[I]);

    }

    _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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

Sponsor:

#2 ginkgo   Members   -  Reputation: 294

Like
3Likes
Like

Posted 10 March 2012 - 03:42 AM

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.

#3 L. Spiro   Crossbones+   -  Reputation: 13634

Like
0Likes
Like

Posted 10 March 2012 - 05:47 AM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 Vlad86   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 March 2012 - 06:29 AM

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).

#5 Rene Z   Members   -  Reputation: 516

Like
1Likes
Like

Posted 12 March 2012 - 07:03 AM

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.

#6 L. Spiro   Crossbones+   -  Reputation: 13634

Like
0Likes
Like

Posted 12 March 2012 - 07:43 AM

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]
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#7 patrrr   Members   -  Reputation: 1010

Like
0Likes
Like

Posted 12 March 2012 - 07:55 AM

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..?

#8 Hodgman   Moderators   -  Reputation: 30432

Like
0Likes
Like

Posted 12 March 2012 - 08:03 AM

Have you checked if the inputs to dot are nans? 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 );

  ...

}


#9 L. Spiro   Crossbones+   -  Reputation: 13634

Like
1Likes
Like

Posted 12 March 2012 - 10:45 AM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS