Advertisement Jump to content
Sign in to follow this  

How to get depth texture for the motion blur

This topic is 2812 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 am following GPU gems tutorial for motion blur. I am unable to understand how to get depth buffer texture.

// Get the depth buffer value at this pixel
float zOverW = tex2D(depthTexture, texCoord);
// H is the viewport position at this pixel in the range -1 to 1.
float4 H = float4(texCoord.x * 2 - 1, (1 - texCoord.y) * 2 - 1, zOverW, 1);
// Transform by the view-projection inverse.
float4 D = mul(H, g_ViewProjectionInverseMatrix);
// Divide by w to get the world position.
float4 worldPos = D / D.w;

// Current viewport position
float4 currentPos = H;
// Use the world position, and transform by the previous view- projection matrix
float4 previousPos = mul(worldPos, g_previousViewProjectionMatrix);
// Convert to nonhomogeneous points [-1,1] by dividing by w.
previousPos /= previousPos.w;
// Use this frame's position and last frame's to compute the pixel velocity
float2 velocity = (currentPos - previousPos)/2.f;

// Get the initial color at this pixel
float4 color = tex2D(sceneSampler, texCoord);
texCoord += velocity;
for(int i = 1; i < g_numSamples; ++i, texCoord += velocity)
// Sample the color buffer along the velocity vector
float4 currentColor = tex2D(sceneSampler, texCoord);
// Add the current color to our color sum.
color += currentColor;
// Average all of the samples to get the final blur color

float4 finalColor = color / numSamples;

This is the code mentioned in the GPU gems tutorial.

Share this post

Link to post
Share on other sites
This has little to do with Math & Physics, so you sort of posted this in the wrong place :)

You get the depth buffer by either rendering to it by drawing to multiple render targets when you do your normal drawing, drawing the distance of each pixel from the camera to the depth texture target.

Or, if your hardware and software API allow it, you can access the GPU's depth buffer directly.

Tell us what technologies you are using, and maybe we can tell you more about how to do the corresponding method.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!