• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Yours3!f

bilateral gaussian blur

6 posts in this topic

Hi,

I'm trying to implement a bilateral gaussian blur. I've already implemented a simple gaussian blur (see below), but I don't know how to turn that into a bilateral one. I've read about bilateral filtering, and I know that this way the weights not only depend on the distance between the samples but between the color intensities as well. Despite this I still have no clue how to do this.

I've used this tutorial to implement the gaussian blur: [url="http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/"]http://rastergrid.co...inear-sampling/[/url]

[CODE]
#version 420 core
uniform sampler2D texture0; //color buffer
uniform vec2 direction; //vec2(1, 0) --> horizontal, vec2(0, 1) --> vertical

in cross_shader_data
{
vec2 tex_coord;
} i;

out vec4 color;

void main()
{
float weights[5] =
{
0.0702702703, 0.3162162162, 0.2270270270, 0.3162162162, 0.0702702703
};

float offsets[5] =
{
-3.2307692308, -1.3846153846, 0.0, 1.3846153846, 3.2307692308
};

vec2 tex_size = textureSize(texture0, 0);
vec2 dir = direction / tex_size;

vec3 result = vec3(0.0);

for(int c = 0; c < 5; c++)
{
result += texture(texture0, i.tex_coord + offsets[c] * dir).xyz * weights[c];
}

color = vec4(result, 1.0);
}
[/CODE]

so my question is how do I make this blur bilateral?

best regards,
Yours3!f Edited by Yours3!f
0

Share this post


Link to post
Share on other sites
In order to accomplish a bilateral blur you will need to use FBOs (Frame Buffer Objects). You essentially render your scene to an FBO and then blur that in one direction. You then render the first FBO to either another FBO or to the screen then you blur that in the other direction.

Your Shaders are going to look something like this, you will have two of them, each in a different direction.

[CODE]
uniform sampler2D color_texture;
varying vec2 vTexCoord;

const float blurSize = 1.0/512.0;

void main(void)
{
vec4 sum = vec4(0.0);
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 4.0*blursize)) * 0.05;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 4.0*blursize)) * 0.05;
gl_FragColor = sum;
}
[/CODE] Edited by ic0de
0

Share this post


Link to post
Share on other sites
[quote name='ic0de' timestamp='1347927946' post='4981068']
In order to accomplish a bilateral blur you will need to use FBOs (Frame Buffer Objects). You essentially render your scene to an FBO and then blur that in one direction. You then render the first FBO to either another FBO or to the screen then you blur that in the other direction.

Your Shaders are going to look something like this, you will have two of them, each in a different direction.

[CODE]
uniform sampler2D color_texture;
varying vec2 vTexCoord;

const float blurSize = 1.0/512.0;

void main(void)
{
vec4 sum = vec4(0.0);
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 4.0*blursize)) * 0.05;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 4.0*blursize)) * 0.05;
gl_FragColor = sum;
}
[/CODE]
[/quote]

thanks for the reply,

I'm already doing the fbos and ping-ponging between them :)
to add I'm doing the gaussian blur by 5 taps, as opposed to 9 taps in your example, but I still don't understand how did you calculate the weights:
0.16, 0.15, 0.12, 0.09, 0.05
also these need to be reduced so that I can still do blur by 5 taps...
0

Share this post


Link to post
Share on other sites
Bilateral filtering considers samples depending on two weights (often the secondary weight is more or less a boolean function).

I.e. when using a gaussin blur on a SSAO map, you can make it depending on the depth buffer, that is, only pixels which are on a similar depth level then the target pixel are considered for blurring. Therefor you need more or less two filter criteria (both are more or less functions). Here's some pseudo code with a simple threshold function for depth:

[source lang="cpp"]
// all weights should sum up to 1.0
float weight_sum = 0;

// result color
vec3 color = vec3(0);

// sample depth and color for 0...7, where 4 is the center
if(abs(depth_0-depth_4) &#60; threshold)
{
color += color_0 * weight_0;
weight_sum += weight_0;
}
if(abs(depth_1-depth_4) &#60; threshold)
{
color += color_1 * weight_1;
weight_sum += weight_1;
}
...
// consider weight sum
color *= 1/weight_sum;
[/source]

You can optimize this (ie. comparing 4 samples at once). Edited by Ashaman73
1

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1347972413' post='4981228']
Bilateral filtering considers samples depending on two weights (often the secondary weight is more or less a boolean function).

I.e. when using a gaussin blur on a SSAO map, you can make it depending on the depth buffer, that is, only pixels which are on a similar depth level then the target pixel are considered for blurring. Therefor you need more or less two filter criteria (both are more or less functions). Here's some pseudo code with a simple threshold function for depth:

[source lang="cpp"]
// all weights should sum up to 1.0
float weight_sum = 0;

// result color
vec3 color = vec3(0);

// sample depth and color for 0...7, where 4 is the center
if(abs(depth_0-depth_4) &#60; threshold)
{
color += color_0 * weight_0;
weight_sum += weight_0;
}
if(abs(depth_1-depth_4) &#60; threshold)
{
color += color_1 * weight_1;
weight_sum += weight_1;
}
...
// consider weight sum
color *= 1/weight_sum;
[/source]

You can optimize this (ie. comparing 4 samples at once).
[/quote]

thank you Ashaman73, this trick pretty much solved the problem [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] (and you're right, I'm doing SSDO blurring [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img])

this is how my shader looks like now:
[CODE]
#version 420 core

uniform sampler2D texture0; //ssdo buffer
uniform sampler2D texture1; //normal buffer
uniform sampler2D texture2; //depth buffer

uniform vec2 direction;
uniform float threshold;

in cross_shader_data
{
vec2 tex_coord;
} i;

out vec4 color;

void main()
{
float weights[4] =
{
0.0702702703, 0.3162162162, 0.3162162162, 0.0702702703
};

float offsets[4] =
{
-3.2307692308, -1.3846153846, 1.3846153846, 3.2307692308
};

vec2 tex_size = textureSize(texture0, 0);
vec2 dir = direction / tex_size;

vec3 normal_sample = texture(texture1, i.tex_coord).xyz;

if(normal_sample == vec3(0.0))
{
discard;
}

float center_depth = texture(texture2, i.tex_coord).x;
vec3 center_color = texture(texture0, i.tex_coord).xyz * 2.0 - 1.0;

vec3 result = center_color * 0.2270270270; //center weight
float weight_sum = 0.2270270270;

for(int c = 0; c < 4; c++)
{
if( abs( center_depth - texture(texture2, i.tex_coord + offsets[c] * dir).x ) < threshold )
{
result += ( texture(texture0, i.tex_coord + offsets[c] * dir).xyz * 2.0 - 1.0 ) * weights[c];
weight_sum += weights[c];
}
}

result *= 1.0 / weight_sum;

color = vec4(result * 0.5 + 0.5, 1.0);
}
[/CODE]

for the threshold I chose 0.0001, as this seemed to be "good enough".
Actually the depth buffer contains linear depth (in range [0...1]), so if I want to check for 0.1 units difference, then I should set the threshold to:
far * x = 0.1;
as far is 1000 I needed this value: 0.0001

thanks again :D
0

Share this post


Link to post
Share on other sites
Good, if you only need one shading value, not a RGB color, then you could optimize it like this:

[CODE]
vec4 sample_0_3;
sample_0_3.x = texture(texture0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.y = texture(texture0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.z = texture(texture0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.w = texture(texture0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 depth_0_3;
depth_0_3.x = texture(depth0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.y = texture(depth0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.z = texture(depth0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.w = texture(depth0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 if_replacement = step(threshold,abs( vec4(center_depth)-depth_0_3)));
result.x += dot(if_replacement.xyzw * weights_0_3.xyzw , sample_0_3.xyzw);
weight_sum+=dot(if_replacement.xyzw, weights_0_3.xyzw);
... same for 5 to 7


[/CODE] Edited by Ashaman73
0

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1348032578' post='4981552']
Good, if you only need one shading value, not a RGB color, then you could optimize it like this:

[CODE]
vec4 sample_0_3;
sample_0_3.x = texture(texture0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.y = texture(texture0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.z = texture(texture0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.w = texture(texture0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 depth_0_3;
depth_0_3.x = texture(depth0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.y = texture(depth0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.z = texture(depth0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.w = texture(depth0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 if_replacement = step(threshold,abs( vec4(center_depth)-depth_0_3)));
result.x += dot(if_replacement.xyzw * weights_0_3.xyzw , sample_0_3.xyzw);
weight_sum+=dot(if_replacement.xyzw, weights_0_3.xyzw);
... same for 5 to 7


[/CODE]
[/quote]

you're right I could do this, plus since I'm using OpenGL 4.2 I can go ahead and sample all 4 by one texture fetch using textureGatherOffsets :D
vec4 all_depth_samples = textureGatherOffsets( texture0, i.tex_coord, offsets, 0);
This could be done for the SSAO result too, but for SSDO I need RGB values...
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0