Jump to content
  • Advertisement
Sign in to follow this  
LevyDee

Deferred Rendering quick question

This topic is 2476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have been slowly understanding the deferred process, but I am lost on what happens after you create your GBuffer. So you have 3 textures that map your color, depth and normals, but I am failing to understand what you do with those. So for each light, you sample the GBuffer, but I don't get how you are supposed to only calculate the pixels on the screen buffer. Just a general concept of how you use the GBuffer is what I am looking for.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Have to elaborate on that. "Draw a fullscreen quad" doesn't mean anything to me. There is probably some terms ect I am not aware of.

Share this post


Link to post
Share on other sites
Quad=two triangles in a square. You draw a square with once corner at each corner of the screen, covering every pixel. The pixel shader will then be run for every pixel of the screen, and you can load your info from your GBuffer and output the correct colors to your screen

Share this post


Link to post
Share on other sites
Hi,

Each frame you do approximately the following thing (simplified version):

- Clear depth/stencil buffer
- Clear light accumulation buffer

Fill Render targets:

- Bind normal, color,material, depth buffers as render targets
- Render each mesh to the render targets (output diffuse color, normal, depth, material data etc)
- Unbind render targets

Calculate lighting :

- Bind light accumulation buffer(s) as render target, and required RT's (normal, depth) as material
- Render lights using full screen quads or light shape mesh geometry, reconstruct position, read normal from RT, calculate lighting based on these values, store the data additively to light accumulation buffer
- Unbind light accumulation buffers(s), unbind material

Combine the image:

- Set backbuffer as RT
- bind light accumulation buffers and diffuse color as material
- Draw full screen quad, for each pixel read diffuse color and accumulated light data, combine the data and output to backbuffer

- Present

This is very simplified and not optimized, but that should give you the idea of different phases in deferred rendering.

Best regards!

Share this post


Link to post
Share on other sites
Hi guys, and thank you for all the responses so far. It has been very helpful. =)

So I kind of feel like a noob because I am still having some difficulty implementing the actual shader code. So I can create my gbuffer and get the attributes I need just fine, and am now working on a spotlight effect file. I am able to get shadows, but it is completely wrong! The shadow moves when I move my camera, so somewhere, I must have a logic error somewhere.

Here is my spotlight shader. It is fairly simple, and will be spruced up once I can get the basics working correctly.


//Effect to run a spotlight pass on scene

float4x4 cameraViewProjectionInverse;

float4x4 lightView : VIEW;
float4x4 lightProjection : PROJECTION;

texture colorMap;
texture normalMap;
texture depthMap;

//Color map from GBuffer
sampler ColorMap =
sampler_state
{
Texture = <colorMap>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

//Normal map from GBuffer
sampler NormalMap =
sampler_state
{
Texture = <normalMap>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

//Depth map from GBuffer
sampler DepthMap =
sampler_state
{
Texture = <depthMap>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

void vsSpotLight(float4 iPos : POSITION0,
float2 iUV : TEXCOORD0,
out float4 oPos : POSITION0,
out float2 oUV : TEXCOORD0)
{
oPos = iPos;
oUV = iUV;
}

void psSpotLight(float2 iUV : TEXCOORD0,
out float4 oColor : COLOR0)
{
float4 mapColor = tex2D(ColorMap, iUV);
float4 mapNormal = tex2D(NormalMap, iUV);
float mapDepth = tex2D(DepthMap, iUV).x;

//Put pixel into world position
float4 pixelWorldPosition;

//Convert from UV coordinates to clip space
pixelWorldPosition.x = (iUV.x * 2.0f) - 1.0f;
pixelWorldPosition.y = ((iUV.y * 2.0f) - 1.0f) * -1;

pixelWorldPosition.z = mapDepth;
pixelWorldPosition.w = 1.0f;
pixelWorldPosition = mul(pixelWorldPosition, cameraViewProjectionInverse);
pixelWorldPosition /= pixelWorldPosition.w;

float4 pixelLightView = mul(pixelWorldPosition, lightView);
float4 pixelLightPos = mul(pixelLightView, lightProjection);

float lightDepth = pixelLightPos.z / pixelLightPos.w;

//In Shadow
if(mapDepth + 0.0001 > lightDepth)
{
oColor.xyz = mapColor.xyz * 0.5f;
oColor.w = 1.0f;
}
else
{
oColor = mapColor;
}
}

technique SpotLight
{
pass p0
{
VertexShader = compile vs_2_0 vsSpotLight();
PixelShader = compile ps_2_0 psSpotLight();

}
}


I also have a question about how I am getting the pixel back into world space. This is how the example I was looking over converted the pixel back into world space. So I am taking the UV coordinate and moving it into clip space[-1,1],[1, -1]. Then inversing by the camera view and projection matrices to get into world space. Then I am moving the pixel into light viewProj space to determine depth, so I can shade the pixel or not. Am I implementing this method correctly?

Thank you for helping!!! I am noob i know =(

Share this post


Link to post
Share on other sites
Are you trying to perform shadow mapping?

I assume that the mapDepth you have contains linear depth value of the camera view so it is used to construct the world position.

You'll need a separate per-light shadow map to perform shadow mapping and a single projection matrix to transform from world space to light space.

Best regards!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!