Sign in to follow this  
BigBadBob

reading raw texture data

Recommended Posts

Hi. I have a texture created as a render target and using D3DPOOL_DEFAULT. I want to be able to read the information about each pixel in this texture. How can I do this? I have experimented a little but am rather confused about surfaces/textures and when and how you create each. From what I can gather...get the surface of the texture, copy that to a surface of a texture in system memory, call LockRect on a system mem texture then read from that?
Something like...

            CComPtr<IDirect3DTexture9> sysmemTexture = NULL;
            CComPtr<IDirect3DSurface9> sysmemTextureSurface = NULL;
            CComPtr<IDirect3DSurface9> texsurface = NULL;
            // create the new texture
            hr = m_data.m_pDev->CreateTexture(1024,
                                                1024,
                                                1,
                                                D3DUSAGE_RENDERTARGET,
                                                D3DFMT_X8R8G8B8,
                                                D3DPOOL_SYSTEMMEM,
                                                &sysmemTexture,
                                                NULL);
            // get the surface rep of the texture
            m_data.m_pTexture->GetSurfaceLevel(0, &texsurface);
            // get the surface rep of the sys mem texture
            sysmemTexture->GetSurfaceLevel(0, &sysmemTextureSurface);
            // move the texture surface data to system memory
            m_data.m_pDev->GetRenderTargetData(texsurface.p, sysmemTextureSurface.p);
            // lock the texture data
            D3DLOCKED_RECT *pRect = new D3DLOCKED_RECT;
            sysmemTexture->LockRect(0, pRect, NULL, D3DLOCK_READONLY);
            // do stuff here
            sysmemTexture->UnlockRect(0);
            delete pRect;

However when I reach the line m_data.m_pTexture->GetSurfaceLevel(0, &texsurface); it not only changes the value of the pointer in texsurface but also sets sysmemTexture to NULL :S I am rather confused and really require some help. Do I still have to allocate memory when using CComPtr or will it do it for me? If I have to do it myself, how do I go about creating my won IDirect3DTexture9 / IDirect3DSurface9? (Is what I have done above ok in theory?) Thanks very much for any help, BBB.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this