Jump to content
  • Advertisement
Sign in to follow this  
onetwothreegone

DirectX 9 SetRenderTarget

This topic is 1502 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 got my code to be able to render to a surface with device->SetRenderTarget, but I was wondering how to set it back to rendering to a window. Also is there a better way of rendering to a texture then with SetRenderTarget?

 

I have been looking around the web for a while but can't find any examples on how to set the render target back to the window.

 

Thanks,

Philip

Share this post


Link to post
Share on other sites
Advertisement

Right after you create your device, use use code like this to get a pointer to the "screen" (the default colour target and default depth target).

IDirect3DSurface9* defaultColorBuffer = 0;
IDirect3DSurface9* defaultDepthBuffer = 0;
device->GetRenderTarget( 0, &defaultColorBuffer );
device->GetDepthStencilSurface( &defaultDepthBuffer );

After rendering to a texture, you can use device->SetRenderTarget(defaultColorBuffer) to go back to rendering to the screen.

Yes, SetRenderTarget is the correct way to render to textures wink.png

 

[edit] ninja'ed by LS ph34r.png

Edited by Hodgman

Share this post


Link to post
Share on other sites

Right after you create your device, use use code like this to get a pointer to the "screen" (the default colour target and default depth target).

IDirect3DSurface9* defaultColorBuffer = 0;
IDirect3DSurface9* defaultDepthBuffer = 0;
device->GetRenderTarget( 0, &defaultColorBuffer );
device->GetDepthStencilSurface( &defaultDepthBuffer );

After rendering to a texture, you can use device->SetRenderTarget(defaultColorBuffer) to go back to rendering to the screen.

Yes, SetRenderTarget is the correct way to render to textures wink.png

 

[edit] ninja'ed by LS ph34r.png

 

And those surfaces has to be Released at the end of the program, and also released and reaquired when changing display settings?

Share this post


Link to post
Share on other sites

And those surfaces has to be Released at the end of the program, and also released and reaquired when changing display settings?

Yep!

Share this post


Link to post
Share on other sites

And those surfaces has to be Released at the end of the program, and also released and reaquired when changing display settings?

Yep!


L. Spiro


[edit]Ninja’d by Hodgman. ph34r.png [/edit]

Edited by L. Spiro

Share this post


Link to post
Share on other sites

Also is there a better way of rendering to a texture then with SetRenderTarget?

 

Since the other answers didn't deal with this part, can you say what's wrong for you about SetRenderTarget that you need a better way?  SetRenderTarget is the best way in D3D9 to accomplish render-to-texture.

 

There is however another way, but it's going to be less flexible and more bandwidth-intensive; that's to create a texture with D3DUSAGE_RENDERTARGET as before, then StretchRect from the backbuffer to it, then draw it as a fullscreen quad.

Share this post


Link to post
Share on other sites

Thank you all, I got it now. One last thing, how much longer will graphics cards be able to run DirectX 9 apps?

 

EDIT:

It appears to not be working right, looking at my code it seems I am not correctly setting the target back to the window. But it could also be that the target is not being properly set to the texture. The result is the texture is blank except for what I draw onto it. The draw area is only the size of the texture, even after I set the render target back to the window.

 

Code:

	if (t == nullptr) // Passing a nullptr means we need to render to the window.
	{
		if (renderTarget != NULL)
			renderTarget->Release();

		renderTarget = NULL;
		targetId = -1;

		device3d->SetRenderTarget(1, windowTarget);
	}
	else
	{
		if (renderTarget != NULL)
		{
			renderTarget->Release();
			renderTarget = NULL;
		}

		gameTextures[t->ID]->Texture->GetSurfaceLevel(0, &renderTarget); // Create                a surface out of the texture.

		targetId = t->ID;

		device3d->SetRenderTarget(1, renderTarget);

This is all wrapped up in a function.

Edited by onetwothreegone

Share this post


Link to post
Share on other sites

9 years, 3 months, 13 days, 2 hours, 48 minutes, 1 second, and 890 milliseconds.

Approximately.

 

 

L. Spiro ph34r.png

Edited by Hodgman
added a ninja

Share this post


Link to post
Share on other sites

9 years, 3 months, 13 days, 2 hours, 48 minutes, 1 second, and 890 milliseconds.

Approximately.

 

 

L. Spiro ph34r.png

 

Off by about an hour actually.

 

Honest answer: nowadays D3D9 is emulated via D3D10+.  Even modern versions of Windows can still run ancient D3D3 programs, and although that's not a definite indicator for the future, it should serve to give you an idea.

 

Basically there's so much software out there, particularly from the last console generation, still using D3D9 that if MS stopped supporting it in a new version of Windows, that new version would be rejected by customers.  Eventually it will die for sure, but you can confidently rely on it still being around for the foreseeable future.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!