# DX11 DX11 SSAO - Wierd Overlay

## Recommended Posts

Migi0027    4628

Hi guys!

Right now I'm facing a problem with SSAO, which is the following:

See those wierd dots or what it is, well that's my failed implementation of SSAO

And here's the output just from the ssao:

Depth and Normal Rendering:

cbuffer ConstantObjectBuffer : register (b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;

float state;
};

struct VOut
{
float4 position : SV_POSITION;
float4 depthPosition : TEXTURE0;
float4 normal : NORMAL;
};

VOut VShader(float4 position : POSITION, float4 normal : NORMAL)
{
VOut output;

position.w = 1.0f;

// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);

output.normal = normal;

// Store the position value in a second input value for depth value calculations.
output.depthPosition = output.position;

return output;
}

{
float4 color = float4(1,1,1,1);

if (state == 5 || state == 2) // 5 or 2 is depth rendering
{
float depthValue;
depthValue = input.depthPosition.z / input.depthPosition.w;
color = float4(depthValue, depthValue, depthValue, 1.0f);
}
else if (state == 6) // 6 is normal rendering
{
float3 viewSpaceNormalizedNormals = 0.5 * normalize (input.normal) + 0.5;
color = float4(viewSpaceNormalizedNormals, 1);
}

return color;
}


Full screen quad rendering (both depth and normal maps looks correct): PLEASE TAKE IN MIND THAT THIS SHADER HAS OTHER PURPOSES AND IS NEWLY BUILT, so there's mistakes...

Texture2D t_dffmap : register(t0);
Texture2D t_depthmap : register(t1);
Texture2D t_normalmap : register(t2);
Texture2D t_random : register(t3);
SamplerState ss;

cbuffer PARAMSBUFFER : register(b0)
{
float time;
float hblur;
float bloomExtract;
float bloom;
float pixelDisortion;
float pixelDisorterAmount;
float2 space;
};

cbuffer BloomBuffer : register(b1)
{
float BloomThreshold;
float BloomSaturation;
float BaseSaturation;
float BloomIntensity;
float BaseIntensity;
};

struct VS_Output
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
float2 texCoord1 : TEXCOORD1;
float2 texCoord2 : TEXCOORD2;
float2 texCoord3 : TEXCOORD3;
float2 texCoord4 : TEXCOORD4;
float2 texCoord5 : TEXCOORD5;
float2 texCoord6 : TEXCOORD6;
float2 texCoord7 : TEXCOORD7;
float2 texCoord8 : TEXCOORD8;
float2 texCoord9 : TEXCOORD9;
};

{
VS_Output Output;
Output.Tex = float2((id << 1) & 2, id & 2);
Output.Pos = float4(Output.Tex * float2(2,-2) + float2(-1,1), 0, 1);

if (hblur == 1)
{
float texelSize = 1.0f / 800;

// Create UV coordinates for the pixel and its four horizontal neighbors on either side.
Output.texCoord1 = Output.Tex + float2(texelSize * -4.0f, 0.0f);
Output.texCoord2 = Output.Tex + float2(texelSize * -3.0f, 0.0f);
Output.texCoord3 = Output.Tex + float2(texelSize * -2.0f, 0.0f);
Output.texCoord4 = Output.Tex + float2(texelSize * -1.0f, 0.0f);
Output.texCoord5 = Output.Tex + float2(texelSize *  0.0f, 0.0f);
Output.texCoord6 = Output.Tex + float2(texelSize *  1.0f, 0.0f);
Output.texCoord7 = Output.Tex + float2(texelSize *  2.0f, 0.0f);
Output.texCoord8 = Output.Tex + float2(texelSize *  3.0f, 0.0f);
Output.texCoord9 = Output.Tex + float2(texelSize *  4.0f, 0.0f);
}

return Output;
}

// Helper for modifying the saturation of a color.
{
// The constants 0.3, 0.59, and 0.11 are chosen because the
// human eye is more sensitive to green light, and less to blue.
float grey = dot(color, float3(0.3, 0.59, 0.11));

return lerp(grey, color, saturation);
}

// Ambient Occlusion Stuff --------------------------------------------------

float3 getPosition(in float2 uv)
{
return t_depthmap.Sample(ss, uv).xyz;
}

float3 getNormal(in float2 uv)
{
return normalize(t_normalmap.Sample(ss, uv).xyz * 2.0f - 1.0f);
}

float2 getRandom(in float2 uv)
{
return normalize(t_random.Sample(ss, float2(800, 600) * uv / 3 /*RZ*/).xy * 2.0f - 1.0f);
}

float doAmbientOcclusion(in float2 tcoord,in float2 uv, in float3 p, in float3 cnorm)
{
float3 diff = getPosition(tcoord + uv) - p;
const float3 v = normalize(diff);
const float d = length(diff)*1; // g_scale
return max(0.0,dot(cnorm,v)-0.01 /*g_bia*/)*(1.0/(1.0+d))*2/*g_int*/;
}

// End

{
float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f);

if (pixelDisortion == 1)
{
// Distortion factor
float NoiseX = pixelDisorterAmount * (time/1000) * sin(input.Tex.x * input.Tex.y+time/1000);
NoiseX=fmod(NoiseX,8) * fmod(NoiseX,4);

// Use our distortion factor to compute how much it will affect each
// texture coordinate
float DistortX = fmod(NoiseX,5);
float DistortY = fmod(NoiseX,5+0.002);

// Create our new texture coordinate based on our distortion factor
input.Tex = float2(DistortX,DistortY);
}

float4 dffMAP = t_dffmap.Sample(ss, input.Tex);

if (hblur == 1)
{
float weight0, weight1, weight2, weight3, weight4;
float normalization;

// Create the weights that each neighbor pixel will contribute to the blur.
weight0 = 1.0f;
weight1 = 0.9f;
weight2 = 0.55f;
weight3 = 0.18f;
weight4 = 0.1f;

// Create a normalized value to average the weights out a bit.
normalization = (weight0 + 2.0f * (weight1 + weight2 + weight3 + weight4));

// Normalize the weights.
weight0 = weight0 / normalization;
weight1 = weight1 / normalization;
weight2 = weight2 / normalization;
weight3 = weight3 / normalization;
weight4 = weight4 / normalization;

// Add the nine horizontal pixels to the color by the specific weight of each.
color += t_dffmap.Sample(ss, input.texCoord1) * weight4;
color += t_dffmap.Sample(ss, input.texCoord2) * weight3;
color += t_dffmap.Sample(ss, input.texCoord3) * weight2;
color += t_dffmap.Sample(ss, input.texCoord4) * weight1;
color += t_dffmap.Sample(ss, input.texCoord5) * weight0;
color += t_dffmap.Sample(ss, input.texCoord6) * weight1;
color += t_dffmap.Sample(ss, input.texCoord7) * weight2;
color += t_dffmap.Sample(ss, input.texCoord8) * weight3;
color += t_dffmap.Sample(ss, input.texCoord9) * weight4;
}
else
color = dffMAP;

if(bloom == 1)
{
// Look up the bloom and original base image colors.
float4 bloom = saturate((dffMAP - BloomThreshold) / (1 - BloomThreshold));

// Adjust color saturation and intensity.
bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
color = AdjustSaturation(color, BaseSaturation) * BaseIntensity;

// Darken down the base image in areas where there is a lot of bloom,
// to prevent things looking excessively burned-out.
color *= (1 - saturate(bloom));

// Combine the two images.
color += bloom;
}

// Apply SSAO

const float2 vec[4] = {float2(1,0),float2(-1,0),
float2(0,1),float2(0,-1)};

float3 p = getPosition(input.Tex);
float3 n = getNormal(input.Tex);
float2 rand = getRandom(input.Tex);

float ao = 0.0f;
float rad = 1/p.z; // g_s_r

//**SSAO Calculation**//
int iterations = 1;
for (int j = 0; j < iterations; ++j)
{
float2 coord2 = float2(coord1.x*0.707 - coord1.y*0.707,
coord1.x*0.707 + coord1.y*0.707);

ao += doAmbientOcclusion(input.Tex,coord1*0.25, p, n);
ao += doAmbientOcclusion(input.Tex,coord2*0.5, p, n);
ao += doAmbientOcclusion(input.Tex,coord1*0.75, p, n);
ao += doAmbientOcclusion(input.Tex,coord2, p, n);
}
ao/=(float)iterations*4.0;

color *= 1.0f - ao;

return color;
}


Now what on earth am I doing wrong?

PS. I'm going to be gone for 10-11 hours from the start of this topic, but I'll come back!

##### Share on other sites
Migi0027    4628

Ok, it got a bit better now, here's my output, but is this right?

Values:

g_scale = 1;
g_intensity = 1;
g_bias = 0.001f;

## Create an account

Register a new account

• ### Similar Content

• Just a really quick question - is there any overhead to using DrawIndexedInstanced even for geometry you just render once vs using DrawIndexed? Or is the details obfuscated by the graphics driver?
I would assume no but you never know
• By isu diss
I'm trying to code Rayleigh part of Nishita's model (Display Method of the Sky Color Taking into Account Multiple Scattering). I get black screen no colors. Can anyone find the issue for me?

• By Endurion
I have a gaming framework with an renderer interface. Those support DX8, DX9 and latest, DX11. Both DX8 and DX9 use fixed function pipeline, while DX11 obviously uses shaders. I've got most of the parts working fine, as in I can switch renderers and notice almost no difference. The most advanced features are 2 directional lights with a single texture
My last problem is lighting; albeit there's documentation on the D3D lighting model I still can't get the behaviour right. My mistake shows most prominently in the dark side opposite the lights. I'm pretty sure the ambient calculation is off, but that one's supposed to be the most simple one and should be hard to get wrong.
Interestingly I've been searching high and low, and have yet to find a resource that shows how to build a HLSL shader where diffuse, ambient and specular are used together with material properties. I've got various shaders for all the variations I'm supporting. I stepped through the shader with the graphics debugger, but the calculation seems to do what I want. I'm just not sure the formula is correct.
This one should suffice though, it's doing two directional lights, texture modulated with vertex color and a normal. Maybe someone can spot one (or more mistakes). And yes, this is in the vertex shader and I'm aware lighting will be as "bad" as in fixed function; that's my goal currently.
• By Mercesa
Hey folks. So I'm having this problem in which if my camera is close to a surface, the SSAO pass suddenly spikes up to around taking 16 milliseconds.
When still looking towards the same surface, but less close. The framerate resolves itself and becomes regular again.
This happens with ANY surface of my model, I am a bit clueless in regards to what could cause this. Any ideas?
In attached image: y axis is time in ms, x axis is current frame. The dips in SSAO milliseconds are when I moved away from the surface, the peaks happen when I am very close to the surface.

Edit: So I've done some more in-depth profiling with Nvidia nsight. So these are the facts from my results
Count of command buffers goes from 4 (far away from surface) to ~20(close to surface).
The command buffer duration in % goes from around ~30% to ~99%
Sometimes the CPU duration takes up to 0.03 to 0.016 milliseconds per frame while comparatively usually it takes around 0.002 milliseconds.
I am using a vertex shader which generates my full-screen quad and afterwards I do my SSAO calculations in my pixel shader, could this be a GPU driver bug? I'm a bit lost myself. It seems there could be a CPU/GPU resource stall. But why would the amount of command buffers be variable depending on distance from a surface?

Edit n2: Any resolution above 720p starts to have this issue, and I am fairly certain my SSAO is not that performance heavy it would crap itself at a bit higher resolutions.

• In DirectX 11 we have a 24 bit integer depth + 8bit stencil format for depth-stencil resources ( DXGI_FORMAT_D24_UNORM_S8_UINT ). However, in an AMD GPU documentation for consoles I have seen they mentioned, that internally this format is implemented as a 64 bit resource with 32 bits for depth (but just truncated for 24 bits) and 32 bits for stencil (truncated to 8 bits). AMD recommends using a 32 bit floating point depth buffer instead with 8 bit stencil which is this format: DXGI_FORMAT_D32_FLOAT_S8X24_UINT.
Does anyone know why this is? What is the usual way of doing this, just follow the recommendation and use a 64 bit depthstencil? Are there performance considerations or is it just recommended to not waste memory? What about Nvidia and Intel, is using a 24 bit depthbuffer relevant on their hardware?
Cheers!

• 11
• 14
• 25
• 16
• 19