Jump to content
  • Advertisement
Sign in to follow this  
d h k

[D3D9] Using Render Target as Texture (for Deferred Rendering)

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

This morning I decided it was time for me to take my graphics framework in its current state (dare I say, 'engine'?) and finally make things interesting. After reading up on Deferred Rendering I started writing my first, well, Deferred Renderer. Fast-forward to now, a good ten hours later, and I have my G-Buffer Fill Shader working - looking at my two RGBA 16-bit floating point render targets storing diffuse color, specular intensity, view-space normals and linear depth correctly in NVidia's brilliant PerfHud tool I can't help but feel a little excited.

Enough blog-like description of how my day went, my current problem is very simple in nature but I can't quite figure it out. This is how I create my render targets that make up the G-Buffer:

GetDevice ( )->CreateRenderTarget ( window->GetWidth ( ), window->GetHeight ( ), D3DFMT_A16B16G16R16F, D3DMULTISAMPLE_NONE, 0, false, &texture, NULL );

texture is an IDirect3DSurface9 holding one of the two render targets that make up the G-Buffer in this function call.

Now, the second step of Deferred Rendering (when the render targets of the G-Buffer are filled) where I draw my lights as geometry requires a light shader that needs the render targets as input. I can't seem to find a way to pass the render targets to the shader as textures! device->SetTexture ( 0, texture ) expects an IDirect3DBaseTexture9, retrieving that from an IDirect3DSurface9 appears to be impossible (apart from very ugly manual data copying and keeping both types around).

Is there any elegant solution for my problem? I'm sure everybody who's working on a Deferred Renderer in Direct3D 9 has experienced this problem - for some reason I haven't been able to find any help though which makes me think I might have overlooked something simple.

As always, any help or feedback would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
For render target textures you call IDirect3DDevice9::CreateTexture, and pass D3DUSAGE_RENDERTARGET. Then you can call GetSurfaceLevel to get the surface for rendering.

Share this post


Link to post
Share on other sites
Works like a charm! Thanks a ton for the incredibly fast and accurate help! I do wonder why there even is a CreateRenderTarget funtion like that in the API though, it totally sent me off on the wrong track. You want render targets so you look for render target on the MSDN and so on... :)

Share this post


Link to post
Share on other sites
You should use CreateRenderTarget when you don't need access for example to depth buffer. Then you can create depth buffer as Render Target instead of Texture.

Share this post


Link to post
Share on other sites
You can't create a depth buffer with CreateRenderTarget, you do that with IDirect3DDevice9::CreateDepthStencilSurface.

I honestly don't know what the primary purpose of that method is. The only time I've ever used it is for creating render targets with MSAA, in which case you create the MSAA surface with CreateRenderTarget, create a render target texture with CreateTexture (with same format + dimensions), render to the MSAA surface, and then use StretchRect to resolve the MSAA surface to the non-MSAA texture.

If it makes you feel any better, it's much nicer in D3D10/D3D11. :P

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!