Jump to content
  • Advertisement
Sign in to follow this  
CC Ricers

Color pixels are being clipped at a different depth in GBuffer

This topic is 2079 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

I have a GBuffer shader in use with XNA to produce a typical output of diffuse/albedo, depth and normals. As all the vertex buffers are being batched and sent through with the same world, view and projection matrices for all render targets at once (and have no masked textures), I expected that they will all cover the same screen area for each render target. But the diffuse color component of the GBuffer appears to have the geometry cut off at a somewhat closer distance than the far clip plane of the camera.


When combined with the lighting, terrain at far distances has a transparent "trim" where you can see the lighting of the terrain, but not its color, so it shows the color of the background, skybox, etc behind it. So the depth and normal values are rendered at that distance, but not the diffuse color values. Indeed, I debug the render targets on PIX and in my program and there are less pixels being drawn to the diffuse component, as if it had its own far clipping value. I will post a screenshot of the problem later, until there here is my pixel shader GBuffer code.


struct VertexShaderOutput
   float4 Position : POSITION0;
   float2 TexCoord : TEXCOORD0;
   float3 Depth : TEXCOORD1;
   float3x3 TangentToWorld : TEXCOORD2;

struct PixelShaderOutput
   half4 Color : COLOR0;
   half4 Normal : COLOR1;
   half4 Depth : COLOR2;

// First, relevant depth output code in the vertex shader
// wvp is the product of World, View and Projection matrices

    output.Position = mul(input.Position, wvp);

    output.Depth.x = output.Position.z;
    output.Depth.y = output.Position.w;

// Complete GBuffer pixel shader

PixelShaderOutput PixelShaderGBuffer(VertexShaderOutput input)
    PixelShaderOutput output;

    // First check if this pixel is opaque
    //output Color
    output.Color = tex2D(diffuseSampler, input.TexCoord);
    clip(output.Color.a - 0.5);

    // Output the normal, in [0,1] space
    float3 normalFromMap = tex2D(normalMapSampler, input.TexCoord);

    normalFromMap = mul(normalFromMap, input.TangentToWorld);    
    output.Normal.rgb = 0.5f * (normalize(normalFromMap) + 1.0f);

    // Output specular intensity
    float4 specularAttributes = tex2D(specularSampler, input.TexCoord);
    output.Normal.a = specularAttributes.r; //specularIntensity;

    // Output Depth
    output.Depth = input.Depth.x / input.Depth.y;  

   return output;


In this shader, the only pixels that I am clipping are the diffuse color texture, if the alpha value is less than 0.5. But this probably is not setting faraway pixels from being clipped, I hope.

Share this post

Link to post
Share on other sites
It sounds like you might have alpha blending on (so the pixels with alpha > 0.5 end up being drawn, and you can still see the background through them). Turn it off for rendering to your G-buffer.
(if that's not the problem, perhaps you're using multisampling?)

Share this post

Link to post
Share on other sites

I removed the line with the alpha clipping- it was a leftover from a non-terrain shader and don't really needed it for terrain anyways. I still don't see a difference in the visuals, though. The diffuse color buffer still stops drawing at a slightly closer distance. Here is a screenshot showing the problem:



Share this post

Link to post
Share on other sites
The screenshot looks like it's after the final lighting pass. What do the individual buffers in the G buffer look like?
And you're sure alpha blending is off? Edited by phil_t

Share this post

Link to post
Share on other sites

In the individual buffers, the normal buffer and depth buffer both show the whole mountain, and in the diffuse color buffer, the mountain appears partly cut off, leaving that empty space that you see.


Right before I draw to the GBuffer, I clear the screen to black, set DepthStencilState to Default and BlendState to opaque. Then, to prepare it with default values, I fill the color buffer with black pixels, the normal buffer with gray, and depth buffer with white. Additive, not alpha, blending is used for the lighting pass, but I don't see how that would have any effect since the GBuffer is already created at that stage.


UPDATE: I just realized what it was. I was going to post a screenshot showing the individual buffers and also removed the skybox from the scene for visual clarity. After removing the skybox, I realized that the colors were being rendered correctly and nothing was clipped off prematurely. It seems that, although I put a restriction to render the skybox in its own depth range, I still needed to scale the skybox by a reasonable amount, or else it would cut into the scenery. So I changed the scaling amount from 20 to 200 and the colors aren't cut off anymore! And the skybox still appears the same size due to perspective.

Edited by CC Ricers

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!