copy texture to another texture

copy texture to another texture

Post by brekeha » Sat, 20 Jan 2007 03:46:12


I have a class that has a LPDIRECT3DTEXTURE9 member. The need has
arisen for me to write a copy constructor. How can I copy one texture
to another without reloading it from file?
 
 
 

copy texture to another texture

Post by legalize+j » Sat, 20 Jan 2007 03:53:48

[Please do not mail me a copy of your followup]

"brekehan" < XXXX@XXXXX.COM > spake the secret code
< XXXX@XXXXX.COM > thusly:


Do you need to create a new texture resource whose contents are
identical to the first resource or can you share the same texture
between the two C++ objects?

If the latter, then simply AddRef() the texture interface in the copy
constructor.

If the former, you'll have to create a new resource and StretchRect all
the levels from the source resource to the destination resource.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
< http://www.yqcomputer.com/ ~legalize/book/download/index.html>

Legalize *** hood! < http://www.yqcomputer.com/ ;

 
 
 

copy texture to another texture

Post by brekeha » Sat, 20 Jan 2007 05:35:47


I was thinking about the latter, not realizing the first was an option.
I think sharing the resource is viable. Where is the resource contained
and managed? What will happen when I release a texture object and
texture object is still in use?

I'll try and find what I can in the msdn, but in the meantime, wanted
to post my reply anyway.

Here is my scenario:

A dialog parser reads though a .txt file of a bunch of 2d graphics
descriptions.
It comes across a font which is an art image of glyphs.
It creates a font object which in turn splits an image and calculates
texture coordinates.
The font object then creates the texture and holds a pointer to it
as a member
I then have a text area object which has a font as a member
To create the text area a font object is passed as an argument and
"copy constructed" to its member
Thus why I wanted to copy the texture

The idea is that the dialog parser can create a bunch of fonts and pass
in whatever font is needed for any text area it comes across. There may
be several text areas that use the same font. Since, the font contains
the texture, I wanted to copy it over in the text areas constructor.
 
 
 

copy texture to another texture

Post by legalize+j » Sat, 20 Jan 2007 07:50:37

[Please do not mail me a copy of your followup]

"brekehan" < XXXX@XXXXX.COM > spake the secret code
< XXXX@XXXXX.COM > thusly:


A COM object remains "live" as long as someone else holds a reference
to it. So if you have two objects A and B that both have a reference
to a texture object T, then the texture object T will live for as long
as A and B have a reference to it. If the constructor for A and B
calls Release on the texture interface, then the texture will be
released (assuming only A and B have references to it) when the second
object is destroyed. Note that setting a texture on a device adds a
reference to it -- the device needs the texture to stay alive as long
as its set on the device.


A copy constructor alone doesn't define the resource ownership
semantics here. For instance, I can see situations where either of
these design assumptions are valid:

1) assigning object A to object B transfers ownership of resources
from A to B

2) assigning object A to object B means ownership of resources is
shared between A and B

3) assigning object A to object B means that B gets its own
distinct copy of A's resources

You can do any of these with the texture resources in Direct3D. In
1), no AddRef or Release is done, but someone A must be signalled to
know that it doesn't "own" the resource anymore, B does. In 2), B
does an AddRef on the texture interface at the time of the copy. In
3), B does a complete level by level copy of A's texture, creating its
own texture that it owns. No AddRef/Release is performed.


If it were me, I'd lazy create font resources for this sort of
scenario. That means that you can copy around these font "objects" as
much as you want and there's no Direct3D resource consumed until you
actually use one of the fonts. Its a little more complex, but if
you're doing lots of manipulation of stuff that might *not* appear on
the screen, its a win. If you know that everything is going to appear
on the screen regardless, then you might as well do the resource
allocation up front because you'll need all those resources anyway.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
< http://www.yqcomputer.com/ ~legalize/book/download/index.html>

Legalize *** hood! < http://www.yqcomputer.com/ ;
 
 
 

copy texture to another texture

Post by brekeha » Sat, 20 Jan 2007 08:03:14

Richard [Microsoft Direct3D MVP] wrote:


An excellant post. I have learned quite a bit as a result. Thank you!

 
 
 

copy texture to another texture

Post by brekeha » Sun, 21 Jan 2007 08:29:01

Richard [Microsoft Direct3D MVP] wrote:

I got scenario 1 working for my font object, but now I am trying to use
scenario 2 for my image objects as they might manipulate the texture
after it being copied.

I looked up stretchrect and went as far as I could. I am of the proper
way to get the destination surface? I am assuming the following code is
the way to do it....but what happens when dest is a new texture and
there is no surface to get? Does the texture know when I made a new
surface and increment it's count or do I not even get access to a
surface that hasn't been made yet?

// Create a seperate copy of the texture
for(DWORD level = 0; level < m_texture->GetLevelCount(); level++)
{
LPDIRECT3DSURFACE9 source = NULL;
rhs.m_texture->GetSurfaceLevel(level, &source);

LPDIRECT3DSURFACE9 dest = NULL;
m_texture->GetSurfaceLevel(level, &dest);

if(FAILED(m_device->StretchRect(source, NULL, dest, NULL,
D3DTEXF_NONE)))
throw Error("Failed texture copy", "Image2D::Image2D(const
Image2D & rhs)", "Image2D.cpp");

source->Release();
dest->Release();
}

 
 
 

copy texture to another texture

Post by legalize+j » Sun, 21 Jan 2007 09:02:23

[Please do not mail me a copy of your followup]

"brekehan" < XXXX@XXXXX.COM > spake the secret code
< XXXX@XXXXX.COM > thusly:


You can't have a texture without its surfaces, so if you have the
texture, then you have the surfaces too. Your code looks fine to me.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
< http://www.yqcomputer.com/ ~legalize/book/download/index.html>

Legalize *** hood! < http://www.yqcomputer.com/ ;