Sign in to follow this  
SaraJ

Ping-ponging using DirectX, HLSL and C#

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this