using a texture as a render target

Started by
8 comments, last by fboivin 15 years, 10 months ago
I'm trying to get some post processing to work, by setting up a texture as a render target, drawing to it, then using it as a texture to apply a shader to and draw it to the back buffer. This is how it should look Photobucket This is how it does look, useing the render to target method, and the exact same shaders as above. Photobucket Here how I setup the texture to be used as a render target.

result = D3DXCreateTexture((*m_d3ddev), 
		                       CGraphics->Width(), CGraphics->Height(), 
		                       D3DX_DEFAULT,       D3DUSAGE_RENDERTARGET, 
							   D3DFMT_A8R8G8B8,    D3DPOOL_DEFAULT, 
							   &texture.texture);

it's created to be the same size as the backbuffer. I then aquire the surfaces like so and set the render target to be the texure.

// save the current render target
	(*m_d3ddev)->GetRenderTarget(0, &m_prev_buffer);
	// get and set our new render target
	m_temp_texture->texture->GetSurfaceLevel(0, &m_new_buffer);
	(*m_d3ddev)->SetRenderTarget(0, m_new_buffer);
	// clear the texture before rendering to it
	(*m_d3ddev)->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0, 0, 0, 255), 1.f, 0);

// temp texture is the IDirect3DTEXTURE9 of the texture I'll be using as a
// render target

// prev_buffer is a SURFACE representing the main backbuffer
// new_buffer is the texture surface 

Am I setting the render target right, or do you need any more info?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
I don't see you actually rendering the texture?
Did you leave that code out on purpose?

The steps you need to take in this approach is the following

1: Render your normal scene
2: Save the old back buffer
3: Set the new render surface (texture)
4: Render to that surface and reset the old back buffer
5: Apply effects to texture and render that texture as a full screen quad.

I hope this helps.
Take care.
Quote:Original post by Armadon
I don't see you actually rendering the texture?
Did you leave that code out on purpose?

The steps you need to take in this approach is the following

1: Render your normal scene
2: Save the old back buffer
3: Set the new render surface (texture)
4: Render to that surface and reset the old back buffer
5: Apply effects to texture and render that texture as a full screen quad.

I hope this helps.
Take care.


I figured it would be assumed but yeah, this is basically my order of doing things...psudeo code
1: Get old rendertarget2: Set new rendertarget3: fill vertex buffer4: drawprimitives5: reset rendertarget to old one6: add the texture to my render que7: repeat steps 3 and 4 with the normal render target.


I'm new to most these techniques, so I'm shooting in the dark when looking through the docs for stuff to try out to fix it.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
you can also use ID3DXRenderToSurface class
how are you setting up your texture when you render it out? It looks like just the particles there are corrupted, not the rest of the scene, I don't know how you have the rest of your code structured, but is it possible you set everything up to render out the texture for post processing, then forget to change it back before using that same texture slot (I dont know how you are setting your textures, but it is possible that your particle texture is being set in the same place as your backbuffer texture) to render your scene the next frame?

Just throwing out a thought I had.
Are you getting any messages from the debug runtime?

I'd recommend taking a PIX capture to check the state of the device after you've set up the render target.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Do you call BeginScene()/EndScene() between setting the render target and using the texture?
Quote:Original post by Promethium
Do you call BeginScene()/EndScene() between setting the render target and using the texture?


I use the setrendertarget functions outside of the end/begin calls.

Also after drawing to the texture I reset the render target by calling.

(*m_d3ddev)->SetRenderTarget(0, m_prev_buffer);

And like said before push the texture onto my render que to be drawn like normal.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Quote:Original post by Lazerbeard
how are you setting up your texture when you render it out? It looks like just the particles there are corrupted, not the rest of the scene, I don't know how you have the rest of your code structured, but is it possible you set everything up to render out the texture for post processing, then forget to change it back before using that same texture slot (I dont know how you are setting your textures, but it is possible that your particle texture is being set in the same place as your backbuffer texture) to render your scene the next frame?

Just throwing out a thought I had.


I didn't quite understand what you were saying? Do you mean, am I switch back to the back buffer before trying to draw the texture?

OK your reply got me thinking, I was using the texture I'm rendering to, to render to it's self, I swear I gotta stop coding past 10pm I just keep making the silliest of mistakes.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
The problem must lie somewhere else because at this high level, things look just fine. As someone suggested, try turning the debug runtime on to see if you get any messages. Also, make sure you're not _using_ your texture render target when you're rendering into it (ie: you can't use and render to the same texture at once...obviously).

Time to start using those handy debugging tools! I'll say that my favorite is NVPerfhud. It shows exactly how your scene is rendered and can help you spot problems really quickly; it saved me tons of time already.

This topic is closed to new replies.

Advertisement