Jump to content
  • Advertisement
Sign in to follow this  

Ping-ponging using DirectX, HLSL and C#

This topic is 3558 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 need to access (and process) a texture from a pixel shader with the result being rendered to a new texture, which then need to be accessed in a second shader pass. From what I have understod this is what is called "ping-ponging". I think I understand the concept quite well, and also the code within the HLSL shader. But could anyone give me a code example on how to do the swapping between textures as rendertargets and as "input" (texture that will be read from using a texture sampler) to the shader in DirectX (if possible in c#, but I can hopefully manage to understand some c++ or c code as well). In pseudo-code this would be something like this:
Set surface/texture1 as render target
Set initial texture as shader input
Begin shader effect
Begin first shader pass
Do some rendering
End first shader pass

Set surface/texture2 as render target
Set surface/texture1 as shader input
Begin second shader pass
Do some rendering
End second shader pass
End shader effect
Thanks in advance!

Share this post

Link to post
Share on other sites
You didn't specify version, so I'm gonna assume D3D 9 here.

First you create a texture, with the render target usage hint in the default pool.
Then set the surface of this texture (IDirect3DTexture9::GetSurfaceLevel) as the current render target with IDirect3DDevice9::SetRenderTarget before calling BeginScene.

Anything you render will now be rendered on your texture. After EndScene you can use the texture as any other texture in your effect. Remember to restore the back buffer as render target after you are done.

There's a bit more to it, like using compatible formats and texture sizes, but you can read about that in the DirectX SDK docs.

Edit: I've never heard of 'ping-ponging', but this technique is usually called render-to-texture. Google will give you a lot of hits.

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!