#### Archived

This topic is now archived and is closed to further replies.

# Getting a D3D9 texture from a D3D9 surface

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi all, Something here thats driving me up the wall, because I'm SURE that I've done the same thing under D3D8 - just now in D3D9 things have changed just a little bit and my technique no longer seems to work!! I'm trying to copy part of a surface (LPDIRECT3DSURFACE9) to an entire texture (LPDIRECT3DTEXTURE9). I've attempted using both StretchRect and UpdateSurface - neither of which succeed. I'm pretty sure that its just not matching the requirements (its pretty strict on what surfaces can be copied).. here's roughly what I've got:
pDev->CreateTexture( part->iWidth, part->iHeight, 1, 0, fmtDisp, D3DPOOL_MANAGED, &(part->pTex), NULL );

LPDIRECT3DSURFACE9 pTmpSurf;
part->pTex->GetSurfaceLevel( 0, &pTmpSurf );

RECT r;
//calculate 'r' here, to be a portion of pSurf the SAME size

//as pTmpSurf.

pDev->UpdateSurface( pSurf, &r, pTmpSurf, NULL );

SAFE_RELEASE( pTmpSurf );

the above code has been stripped off error-checking, but each call is error checked / return value analysed... the only error reported is "UpdateSurface()".. any ideas how I can do this? Jack **EDIT: Both texture and surface have the same format (no alpha channel, either 16bit or 32bit). The source surface is in _DEFAULT and the texture is in _MANAGED, but I've tried changing those to no effect. [edited by - Jollyjeffers on January 13, 2004 11:48:32 PM]

##### Share on other sites
The source surface must have been created with D3DPOOL_SYSTEMMEM.
The destination surface must have been created with D3DPOOL_DEFAULT.

(from the Summer Update docs)

I like pie.

##### Share on other sites
I'm pretty sure that UpdateSurface() won't work unless the source surface is in D3DPOOL_SYSTEMMEM and the destination surface is in D3DPOOL_DEFAULT. Plus the obvious restrictions (same size, no multisampling etc).

[edited by - TheBigJ on January 13, 2004 12:08:29 AM]

##### Share on other sites
StretchRect has its own set of limitations. For one, both surfaces have to be in D3DPOOL_DEFAULT which is why it probably failed in your case. The docs for StretchRect contain a table and details on the requirements for source and destination surfaces.

##### Share on other sites
quote:
The source surface must have been created with D3DPOOL_SYSTEMMEM.
The destination surface must have been created with D3DPOOL_DEFAULT.

that did the trick

thinking of the table in the docs... I was sure that I''d checked my stuff against that and it would work. Guess I changed one at a time, and not both at the same time serves me right for programming at 4am

thanks all,
Jack

##### Share on other sites
As an alternative, what I''ve seen work really well is using the Texture interface, get the surface level of a texture, and then you do a surface->surface copy. It''s much more flexible about Pool types.

##### Share on other sites
quote:
do a surface->surface copy

hmm, what do you mean by this exactly? is there another surface-copy beyond StretchRect / UpdateSurface ?

Jack

##### Share on other sites
Woah... Wait a minute! That would highly simplify what I''m working with right now. GetSurfaceLevel allows you to modify a SURFACE based on a texture? I''ve been sitting here messing with rectangle locking and the like to transfer surfaces to textures and so on. It''s been a nightmare.

##### Share on other sites
quote:
Woah... Wait a minute! That would highly simplify what I'm working with right now. GetSurfaceLevel allows you to modify a SURFACE based on a texture? I've been sitting here messing with rectangle locking and the like to transfer surfaces to textures and so on. It's been a nightmare.

Yeah, that's what I was talking about, sorry I wasn't being more explicit.

Aside from the simplicity of it, I'm guessing that it's also more efficiently implemented in D3DX than if you were to lock it and do it yourself.

[edited by - mbrown211 on January 14, 2004 3:25:04 PM]

##### Share on other sites
quote:
Original post by jollyjeffers
quote:
do a surface->surface copy

hmm, what do you mean by this exactly? is there another surface-copy beyond StretchRect / UpdateSurface ?

Jack

Yeah, in D3DX you can do a LoadSurfaceFromSurface
that takes in pallettes and rects (both optional) and does surface to surface copying...

##### Share on other sites
If you take a texture and get the surface level, you can make modification to a surface? Are your changes to the surface automatically made to the texture? What do you do to ditch the surface when you done? Can you just null is out or free it? And if so, can you use the LoadSurfaceFromSurface command on the surface you pulled out of the texture?

The SDK documentation for GetSurfaceLevel is VERY VERY vague... I made a post asking for clarification about it somewhere else in this forum but I didn't really get an answer.

[edited by - ShadowP13 on January 14, 2004 3:27:08 PM]

##### Share on other sites
quote:
If you take a texture and get the surface level, you can make modification to a surface? Are your changes to the surface automatically made to the texture?

yeah, you grab the surface using GetSurfaceLevel() and mess about with it as you see fit... and then SAFE_RELEASE() the surface when you're done, the texture will then reflect whatever it is you did to the surface..

quote:
in D3DX you can do a LoadSurfaceFromSurface

Unless I'm blind, the only references I can find in the SDK help to this function are for VB - and thats where I've used it before. I went for the copyrect/updatesurface route BECAUSE I couldn't find a suitable LoadSurfaceFromSurface command.

Jack

EDIT: I am blind I found the function now... wonder how many hours I've managed to waste by being this stupid?!

[edited by - Jollyjeffers on January 14, 2004 4:51:05 PM]

##### Share on other sites
quote:
If you take a texture and get the surface level, you can make modification to a surface? Are your changes to the surface automatically made to the texture? What do you do to ditch the surface when you done? Can you just null is out or free it? And if so, can you use the LoadSurfaceFromSurface command on the surface you pulled out of the texture?

The SDK documentation for GetSurfaceLevel is VERY VERY vague... I made a post asking for clarification about it somewhere else in this forum but I didn''t really get an answer.

[edited by - ShadowP13 on January 14, 2004 3:27:08 PM]

Dude, you did. You even posted that you understood.

A mipped texture can consist of several mip levels, each being a separate surface. You can make changes to each surface independently. You get access to those surfaces by calling GetSurfaceLevel(). When you''ve made changes, Release() it. That''s all there is to it. It''s just a surface.

I like pie.

##### Share on other sites
*snickers* yes, I was given several answer in my other thread, but to be honest I fealt like I was coming off like a retard so I decided to just pretend I understood and cut my losses instead of constantly posting "huh?". I didn't mean to be insulting by saying "I didn't really get an answer". By that I meant I didn't really get and answer I understood. But thanks to everyone's help I'm officially beyond this little hurdle *hurrah* thanks everyone. If you comb through the forum you can see my next stupid problem (after this I won't post anymore stupid question for at least a month, I promise)

Believe it or not, the idiot is learning. :/

[edited by - ShadowP13 on January 15, 2004 3:25:18 AM]

##### Share on other sites
Here''s the D3DXLoadSurfaceFromSurface() documentation from MSDN.