Ping-ponging using DirectX, HLSL and C#

Started by
0 comments, last by Promethium 15 years, 6 months ago
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!
Advertisement
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.

This topic is closed to new replies.

Advertisement