[Deprecated]SSAO with XEffects (Reloaded)

Announce new projects or updates of Irrlicht Engine related tools, games, and applications.
Also check the Wiki
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

[Deprecated]SSAO with XEffects (Reloaded)

Post by Halifax »

Well, as some people know on IRC, I've been working with SSAO, for about the past two or three hours. I've got a basic implementation, but it doesn't appear to be working, so I've some to the forum for some help.

Download:
Deprecated! Use the XEffects 1.1 and higher SSAO

I just wrapped my working directory, so the executable works, but you have to use your own Irrlicht library for linking.

'O' - turns on SSAO modulation
'P' - turns off

Screenshots:
Image
As you can see, there are some noticeable places that should be receiving ambient occlusion that aren't and many places that shouldn't be receiving as much as they are.

Code:

Code: Select all

removed
This code outputs the normalized ambient occlusion factor to the buffer for the blur to use, and then finally to be modulated.

Notes:
- I modified XEffects. Some of the modificiations are evident in his topic "XEffects - Reloaded", and others I got from the IRC channel. I had to modify the ScreenQuad wrapper to bind the UserMapSampler for OpenGL. The user map I needed to use is a set of random vectors.

- I modified the blur shaders to only do 3 samples, or 2, I forget.

Things to do:
Once the SSAO is corrected, I need to find a way to optimize it. And then after that I want to go into future endeavours with screen space technology such as SSGI (Screen Space Global Illumination) and deferred shading, as presented in the Leadwerks paper.

Thanks go to BlindSide for XEffects. It's a great framework!
Last edited by Halifax on Sun Mar 15, 2009 7:44 am, edited 1 time in total.
TheQuestion = 2B || !2B
fmx

Post by fmx »

whow cool!

I tried the binary and got this everytime I enabled it (pressed O):
Image

BTW I don't have the DX SDK so maybe its something todo with that.
I haven't got the time to recompile it with OGL, I'll try it tonight
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Hmm, that's wierd. I got that output sometimes with my nVidia card, but only when I had major problems in the shader that were reported. I'll take a run through my code, and see if I can get it working for you.
TheQuestion = 2B || !2B
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Excuse the double post.

@fmx: Try this:

Code: Select all

uniform sampler2D ColorMapSampler;
uniform sampler2D ScreenMapSampler;
uniform sampler2D DepthMapSampler;
uniform sampler2D UserMapSampler;

vec3 kernel[8];

void main()
{
	// random vectors around and inside the sphere
	kernel[0] = vec3( 0.072169, 0.072169, 0.072169 );
	kernel[1] = vec3(-0.144338,-0.144338,-0.144338 );
	kernel[2] = vec3(-0.216506,-0.216506, 0.216506 );
	kernel[3] = vec3(-0.288675, 0.288675,-0.288675 );
	kernel[4] = vec3(-0.360844, 0.360844, 0.360844 );
	kernel[5] = vec3( 0.433013,-0.433013,-0.433013 );
	kernel[6] = vec3( 0.505181,-0.505181, 0.505181 );
	kernel[7] = vec3( 0.577350, 0.577350,-0.577350 );

	// calculate the radius 
   	float fRadius = 2.0; 

   	// sample the depth from the texture 
   	vec4 vEncodedDepth = texture2D( DepthMapSampler, gl_TexCoord[0].st ); 
    
   	// decode the depth to 16-bit precision, and put it into view space 
   	float fFragmentDepth = vEncodedDepth.x + ( vEncodedDepth.y / 256.0 ); 
   	float fViewDepth = fFragmentDepth * 59.9; // pre-calculated near-to-far value 

	// calculate the kernel scaling vector. put the z in normalized view space like the depth buffer 
   	vec3 vKernelScale = vec3( fRadius / fViewDepth, fRadius / fViewDepth, fRadius / 59.9 ); 

   	float fOcclusion = 0.0; 

   	for( int i = 1; i < 3; i ++ ) 
   	{ 
     		vec3 vRandom = texture2D( UserMapSampler, gl_TexCoord[0].st * ( 7.0 + i ) ).xyz; 

      		//vRandom = normalize( vRandom * 2.0 - 1.0 ); 
      		vRandom = normalize( vRandom * 2.0 - 1.0 ); 

     		for( int j = 0; j < 8; ++ j ) 
      		{ 
         		// reflect the kernel about the normal 
         		vec3 vRotatedKernel = reflect( kernel[j], vRandom ) * vKernelScale; 

         		// sample the depth from the reflected kernel 
         		vec4 vEncodedSampleDepth = texture2D( DepthMapSampler, vRotatedKernel.xy + gl_TexCoord[0].st ); 

         		// decode the depth 
         		float fSampleDepth = vEncodedSampleDepth.x + ( vEncodedSampleDepth.y / 256.0 ); 

         		// compute the change in z-value 
         		float fDeltaDepth = max( fSampleDepth - fFragmentDepth, 0.0 ); 

         		// compute the range of the ambient occlusion 
         		float fRange = abs( fDeltaDepth ) / ( vKernelScale.z * 2.0 ); 

	      		fOcclusion += lerp( fDeltaDepth * 0.8, 1.0, saturate( fRange ) ); 
   		} 
	} 

	fOcclusion = fOcclusion / 16.0; 
	vec3 color = vec3( fOcclusion ); 
	color = lerp( 0.1, 0.6, saturate( color.x ) ); 
    
	vec3 baseColor = texture2D( ScreenMapSampler, gl_TexCoord[0].st ).xyz; 
	gl_FragColor = vec4( color, 1.0 ); 
}
Just delete all the code in SSAO.glsl, and put that code in its place. It seems to be a problem with ATI cards and setting arrays when you construct them.
TheQuestion = 2B || !2B
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

I think ATI cards usually want the "vec3 kernel[8]; " inside the main function, so move it in there.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

BlindSide wrote:I think ATI cards usually want the "vec3 kernel[8]; " inside the main function, so move it in there.
Ah okay, I thought I just recalled seeing it in one of your shaders like that before (outside of the main function). That's my fault.

So are there any problems that are jumping out at you, BlindSide, with my SSAO calculations?
TheQuestion = 2B || !2B
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Yeah, quite a few but when I reduce the radious I get depth artifacts. Give me some time to look this over.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Post by Nadro »

It doesn't work on my HD 4850 with Catalyst 9.1. I don't like GLSL, I prefer Cg, but sometimes in Cg I also have problems with GLSL profiles on my Radeon.

This is screen SSAO from my engine:
Image
Image

When You improve shader for Radeons, We can will compare results. I release this SSAO version soon, with next IrrCg release.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Cool, that looks like some very good SSAO. I would definitely be interested in reading your shader code.

By the way, the buffer is blurred at a later time, I just didn't post a screenshot with the blurred buffer. Also I'm almost sure my errors are probably coming from my re-mapping process while generating the occlusion value, and afterwards for tone mapping.
TheQuestion = 2B || !2B
fmx

Post by fmx »

nadro that's looking really neat :)

halifax, your edited code gave me more errors everytime i pressed "o":

Code: Select all

Irrlicht Engine version 1.5
Professional Service Pack 2 (Build 3790)
Using renderer: OpenGL 2.1.8201
ATI MOBILITY RADEON HD 3450: ATI Technologies Inc.
OpenGL driver version is 1.2 or better.
GLSL version: 1.3
Loaded texture: C:\SSAO\SSAO\Release\bin\media\wall.bmp
GLSL shader failed to compile
Fragment shader failed to compile with the following errors:
WARNING: 0:37: implicit cast from int to floatERROR: 0:59: 'saturate' : no match
ing overloaded function found
ERROR: 0:59: 'lerp' : no matching overloaded function found
ERROR: 0:65: 'saturate' : no matching overloaded function found
ERROR: 0:65: 'lerp' : no matching overloaded function found
ERROR: 0:65: 'assign' :  cannot convert from 'const float' to '3-component vecto
r of float'
ERROR:  compilation errors.  No code generated.

Loaded texture: C:\SSAO\SSAO\Release\bin\media\SSAORandomVectors.bmp
GLSL shader failed to compile
Fragment shader failed to compile with the following errors:
WARNING: 0:37: implicit cast from int to floatERROR: 0:59: 'saturate' : no match
ing overloaded function found
ERROR: 0:59: 'lerp' : no matching overloaded function found
ERROR: 0:65: 'saturate' : no matching overloaded function found
ERROR: 0:65: 'lerp' : no matching overloaded function found
ERROR: 0:65: 'assign' :  cannot convert from 'const float' to '3-component vecto
r of float'
ERROR:  compilation errors.  No code generated.
somethings gone wrong somewhere, but I'll be happy to keep testing things until it works out :)
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Replace all 'lerp' with 'mix', and all 'saturate(x)' with 'clamp(x, 0.0, 1.0)' (Or if its a vec2 'clamp(x, vec2(0.0, 0.0), vec2(1.0, 1.0))' and so on...).
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

BlindSide wrote:Replace all 'lerp' with 'mix', and all 'saturate(x)' with 'clamp(x, 0.0, 1.0)' (Or if its a vec2 'clamp(x, vec2(0.0, 0.0), vec2(1.0, 1.0))' and so on...).
Does GLSL not like saturate and lerp or something? (I'm just trying to learn everything I can about shaders.)
TheQuestion = 2B || !2B
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

They are not official glsl functions hence kill ATI cards :twisted:
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

omaremad wrote:They are not official glsl functions hence kill ATI cards :twisted:
Ah alright, that makes sense. I really should read the GLSL Specification, but it's soooo long. :lol:
TheQuestion = 2B || !2B
9YkKsvXM
Posts: 64
Joined: Tue Mar 11, 2008 11:45 pm

Post by 9YkKsvXM »

-
Last edited by 9YkKsvXM on Mon Jun 08, 2020 1:36 am, edited 1 time in total.
Post Reply