• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Deferred Rendering

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.

8 replies to this topic

### #1CursedTyrant  Members

Posted 22 February 2012 - 01:59 PM

So I'm having quite a bit of problem while converting my deferred rendering code (well, not mine exactly, I used Catalin Zima's tutorial) from XNA 4 to SlimDX's D3D9.

DeferredRenderer.cs:

I can post the shader files here too, but there's about 4 of them, so that's only if somebody wants me to, so as not to clutter the place.I basically copied them from the XNA example, and it being HLSL I didn't have to change it at all, since it's exactly the same both in XNA and SlimDX. It worked in XNA, so it should work here.

I'm getting this with the code above:

So the question is: what am I doing wrong and how do I fix it?

I'm using the Express edition of VS, so debugging this is quite difficult. I tried using PIX, but I'm not very experienced with it (or D3D) and it didn't really return anything too useful.

### #2gfxgangsta  Members

Posted 22 February 2012 - 03:00 PM

Do you get anything useful in the G-Buffer? Try drawing the contents of these render targets to the screen for real-time visualization:

colorRTSurface
normalRTSurface
depthRTSurface

You can also save them to disk and then view them offline. Do they have expected results?

### #3CursedTyrant  Members

Posted 22 February 2012 - 03:12 PM

They do, in fact. Here they are (depth not included, since it's mostly white, but it was the same in XNA and it worked):

colorRT

normalRT

### #4CursedTyrant  Members

Posted 22 February 2012 - 03:19 PM

Hmm, after doing this:

[source lang="csharp"]Surface.ToFile(colorRTSurface, "colorRTSurface.png", ImageFileFormat.Png);Surface.ToFile(normalRTSurface, "normalRTSurface.png", ImageFileFormat.Png);Surface.ToFile(depthRTSurface, "depthRTSurface.png", ImageFileFormat.Png);[/source]

I get some weird results. I've attached the images.

### #5gfxgangsta  Members

Posted 22 February 2012 - 03:28 PM

I assume the QuadRenderer works correctly. Shouldn't the light direction vector be normalized?

### #6CursedTyrant  Members

Posted 23 February 2012 - 03:13 PM

I believe it does. When I tried passing 0.5f instead of 1.0f to the QuadRenderer, it covered half of the screen, so that works at least.

Here's the class, but I assume it works fine:

[source lang="csharp"]using System;using System.Collections.Generic;using SlimDX;using SlimDX.Direct3D9;namespace VoxelSubmarineGameSlimDXTest.DeferredRendering{ using VertexPositionTexture = VertexStructures.VertexPositionTexture; public class QuadRenderer { private static Device device; private static VertexPositionTexture[] verts = null; private static short[] ib = null; public static void Init(Device argDevice) { device = argDevice; ib = new short[] { 0, 1, 2, 2, 3, 0 }; } public static void Render(Vector2 v1, Vector2 v2) { device.SetSamplerState(0, SamplerState.MagFilter | SamplerState.MinFilter | SamplerState.MipFilter, TextureFilter.Point); device.SetSamplerState(0, SamplerState.AddressU | SamplerState.AddressV, TextureAddress.Wrap); verts = new VertexPositionTexture[] { new VertexPositionTexture( new Vector3(v2.X, v1.Y, 1), new Vector2(1, 1)), new VertexPositionTexture( new Vector3(v1.X, v1.Y, 1), new Vector2(0, 1)), new VertexPositionTexture( new Vector3(v1.X, v2.Y, 1), new Vector2(0, 0)), new VertexPositionTexture( new Vector3(v2.X, v2.Y, 1), new Vector2(1, 0)) }; device.VertexDeclaration = VertexStructures.VertexPositionTextureDeclaration; device.VertexFormat = VertexPositionTexture.Format; device.DrawIndexedUserPrimitives<short, VertexPositionTexture>(PrimitiveType.TriangleList, 0, 4, 2, ib, Format.Index16, verts, VertexPositionTexture.SizeInBytes); } }}[/source]

As for the light direction vector - it's normalized in the shaders themselves, but I believe things which are within the 0.0 to 1.0 range don't have to be normalized, since that's basically what normalizing does?

### #7CursedTyrant  Members

Posted 23 February 2012 - 03:19 PM

Scratch that, when I changed the VertexFormat to VertexFormat.None in the QuadRenderer class, everything seems to work.

What I would like to know is why, and if you have a good link on the subject for me to read, it would be greatly appreciated.

### #8gfxgangsta  Members

Posted 23 February 2012 - 03:47 PM

Can you post the code for directionalLightEffect and combineFinalEffect?

And as for "but I believe things which are within the 0.0 to 1.0 range don't have to be normalized, since that's basically what normalizing does"...

Normalizing causes a vector to become unit length. Even if the individual XYZ are in the 0.0 to 1.0 range, that doesn't mean the vector's length == 1.

### #9CursedTyrant  Members

Posted 23 February 2012 - 04:00 PM

DirectionalLight.fx


float3 lightDirection;

float3 Color;

float3 cameraPosition;

float4x4 InvertViewProjection;

float2 halfPixel;

texture colorMap;

texture normalMap;

texture depthMap;

sampler colorSampler = sampler_state

{

Texture = (colorMap);

MagFilter = LINEAR;

MinFilter = LINEAR;

MipFilter = LINEAR;

};

sampler depthSampler = sampler_state

{

Texture = (depthMap);

MagFilter = LINEAR;

MinFilter = LINEAR;

MipFilter = LINEAR;

};

sampler normalSampler = sampler_state

{

Texture = (normalMap);

MagFilter = LINEAR;

MinFilter = LINEAR;

MipFilter = LINEAR;

};

{

float3 Position : POSITION0;

float2 TexCoord : TEXCOORD0;

};

{

float4 Position : POSITION0;

float2 TexCoord : TEXCOORD0;

};

{

output.Position = float4(input.Position, 1);

output.TexCoord = input.TexCoord - halfPixel;

return output;

}

{

// Get normal data from the normalMap

float4 normalData = tex2D(normalSampler, input.TexCoord);

// Transform normal back to [-1, 1] range

float3 normal = 2.0f * normalData.xyz - 1.0f;

// Get specular power and transform it into [0, 255] range

float specularPower = normalData.a * 255;

// Get specular intensity from the colorMap

float specularIntensity = tex2D(colorSampler, input.TexCoord).a;

float depthVal = tex2D(depthSampler, input.TexCoord).r;

float4 position;

position.x = input.TexCoord.x * 2.0f - 1.0f;

position.y = -(input.TexCoord.y * 2.0f - 1.0f);

position.z = depthVal;

position.w = 1.0f;

// Transform to world space

position = mul(position, InvertViewProjection);

position /= position.w;

// Surface-to-light vector

float3 lightVector = -normalize(lightDirection);

// Compute diffuse light

float NdL = max(0, dot(normal, lightVector));

float3 diffuseLight = NdL * Color.rgb;

// Reflection vector

float3 reflectionVector = normalize(reflect(lightVector, normal));

// Camera-to-surface vector

float3 directionToCamera = normalize(cameraPosition - position);

// Compute specular light

float specularLight = specularIntensity * pow(saturate(dot(

reflectionVector, directionToCamera)), specularPower);

float4 outColor = float4(diffuseLight, specularLight);

return saturate(outColor);

}

technique Technique1

{

pass Pass1

{

}

}



CombineFinal.fx


texture colorMap;

texture lightMap;

texture depthMap;

float2 halfPixel;

float3 cameraPosition;

float4x4 InvertViewProjection;

float4 FogColor = float4(0.0f, 0.05f, 0.1f, 0.0f);

sampler colorSampler = sampler_state

{

Texture = (colorMap);

MagFilter = LINEAR;

MinFilter = LINEAR;

Mipfilter = LINEAR;

};

sampler lightSampler = sampler_state

{

Texture = (lightMap);

MagFilter = LINEAR;

MinFilter = LINEAR;

Mipfilter = LINEAR;

};

sampler depthSampler = sampler_state

{

Texture = (depthMap);

MagFilter = LINEAR;

MinFilter = LINEAR;

Mipfilter = LINEAR;

};

{

float3 Position : POSITION0;

float2 TexCoord : TEXCOORD0;

};

{

float4 Position : POSITION0;

float2 TexCoord : TEXCOORD0;

};

{

output.Position = float4(input.Position, 1);

output.TexCoord = input.TexCoord - halfPixel;

return output;

}

{

float3 diffuseColor = tex2D(colorSampler, input.TexCoord).rgb;

float4 light = tex2D(lightSampler, input.TexCoord);

float3 diffuseLight = light.rgb;

float specularLight = light.a;

float depthVal = tex2D(depthSampler, input.TexCoord).r;

float4 position;

position.x = input.TexCoord.x * 2.0f - 1.0f;

position.y = -(input.TexCoord.y * 2.0f - 1.0f);

position.z = depthVal;

position.w = 1.0f;

// Transform to world space

position = mul(position, InvertViewProjection);

position /= position.w;

float Fog = 1.0f - (length(position - cameraPosition) / 500.0f);

Fog = clamp(Fog, 0.0f, 1.0f);

float4 Color = float4((diffuseColor * diffuseLight + diffuseColor * specularLight), 0.0f);

Color = lerp(FogColor, Color, Fog);

return saturate(Color);

}

technique Technique1

{

pass Pass1

{

}

}



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.