direct3d9 - copying backbuffer to a texture

Started by
11 comments, last by mateo 17 years, 11 months ago
Would anyone happen to know how to copy a section of the backbuffer to a texture? And provide some example code to do it?
Advertisement
Hey,
I'm not 100% sure about this, but I think it should work. Create a texture and access its surface. Then get the backbuffer from your DirectX device and use UpdateSurface to copy the portion of the backbuffer surface to your texture's surface.

Something like this maybe:

...IDirect3DTexture9* texture = NULL;IDirect3DSurface9* textureSurface = NULL;IDirect3DSurface9* backBufferSurface = NULL;// Make a texture of some sizeD3DXCreateTexture(width, height, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, &texture, NULL);// Get its surfacetexture->GetSurfaceLevel(0, &textureSurface);// Get the backbufferdxDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBufferSurface);// Determine how much to copy and where to start copy it toRECT areaToCopy = { 0, 0, 640, 480 };POINT destinationPoint = { 0, 0 };// Copy the backbuffer surface to the texturedxDevice->UpdateSurface(backBufferSurface, &areaToCopy, textureSurface, &destinationPoint);...


Didn't test it. Hope it works. Good luck.
You can always set up rendering to a texture instead of copying the pixels directly.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
I don't think it's possible to only copy part of the backbuffer to a texture, but by using GetRenderTargetData() you should be able to copy the complete backbuffer to a texture that's in system memory and go from there.
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Quote:Original post by remigius
I don't think it's possible to only copy part of the backbuffer to a texture...


Change up the texture coordinates. If you want to copy the top-left quarter of the buffer, u: [0, 0.5] and v: [0, 0.5].
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
i think that IDirect3DDevice9::StretchRect() could be what you're looking for...
Yes, StretchRect will do the trick nicely.
idramire: I tried the example code, but didn't work, look in the directx sdk and noticed that UpdateSurface requires that the source surface be in system memory, I want to copy from surface-surface.

remigius: I want to be able to copy only a portion of the backbuffer.

I've tried StretchRect and it keeps returning with an invalid call error.

What are you trying to achieve by doing this? We might be able to help you more with more details.
Quote:I've tried StretchRect and it keeps returning with an invalid call error.


StretchRect probably is the only option if you only wish to copy part of the backbuffer (though you might work around that using alternative texture coordinates like mentioned above). If you get an InvalidCallException, try enabling the Direct3D debug runtime, it will tell you something that makes some more sense.

I don't have the docs around atm, but iirc you can only use StretchRect on the backbuffer to stretch to a texture that's a render target (since the backbuffer is also a render target). There are some more restrictions on the surface properties and I believe not all devices support streching and/or partial copies (making StretchRect pretty much equivalent to UpdateSurface, safe for the surface requirements. This should only be an issue for old hardware though, afaik).

Hope this helps & sorry about the misinformation in my previous post, I forgot about good ol' StretchRect :)
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!

This topic is closed to new replies.

Advertisement