//////////////////////////////////////////////////////////////// // // CPixelsManager::SetVolumeTexturePixels // // Copy pixels into texture // //////////////////////////////////////////////////////////////// bool CPixelsManager::SetVolumeTexturePixels ( IDirect3DVolumeTexture9* pD3DVolumeTexture, const CPixels& pixels, const RECT* pRect, uint uiSlice ) { if ( !pD3DVolumeTexture ) return false; IDirect3DVolume9* pVolume = NULL; CAutoReleaseMe < IDirect3DVolume9 > Thanks( pVolume ); UINT Level = 0; pD3DVolumeTexture->GetVolumeLevel ( Level, &pVolume ); if ( !pVolume ) return false; bool bResult = false; D3DVOLUME_DESC Desc; pVolume->GetDesc ( &Desc ); if ( Desc.Usage == 0 ) { if ( Desc.Format == D3DFMT_A8R8G8B8 || Desc.Format == D3DFMT_X8R8G8B8 || Desc.Format == D3DFMT_R5G6B5 ) { // Direct reading will work here bResult = SetVolumePixels ( pVolume, pixels, pRect, uiSlice ); } else { return false; } } return bResult; }
//////////////////////////////////////////////////////////////// // // CPixelsManager::GetTexturePixels // // Copy pixels from texture // //////////////////////////////////////////////////////////////// bool CPixelsManager::GetTexturePixels ( IDirect3DBaseTexture9* pD3DBaseTexture, CPixels& outPixels, const RECT* pRect, uint uiSurfaceIndex ) { if ( !pD3DBaseTexture ) return false; IDirect3DSurface9* pD3DSurface = NULL; CAutoReleaseMe < IDirect3DSurface9 > Thanks( pD3DSurface ); D3DRESOURCETYPE resourceType = pD3DBaseTexture->GetType (); if ( resourceType == D3DRTYPE_VOLUMETEXTURE ) { return GetVolumeTexturePixels ( (IDirect3DVolumeTexture9*)pD3DBaseTexture, outPixels, pRect, uiSurfaceIndex ); } else if ( resourceType == D3DRTYPE_CUBETEXTURE ) { D3DCUBEMAP_FACES FaceType = (D3DCUBEMAP_FACES)uiSurfaceIndex; ((IDirect3DCubeTexture9*)pD3DBaseTexture)->GetCubeMapSurface ( FaceType, 0, &pD3DSurface ); if ( !pD3DSurface ) return false; } else if ( resourceType == D3DRTYPE_TEXTURE ) { ((IDirect3DTexture9*)pD3DBaseTexture)->GetSurfaceLevel ( 0, &pD3DSurface ); if ( !pD3DSurface ) return false; } bool bResult = false; D3DSURFACE_DESC Desc; pD3DSurface->GetDesc ( &Desc ); if ( Desc.Usage == D3DUSAGE_RENDERTARGET ) { // Check we are allowed to read the render target bool bAllowScreenUpload = true; CVARS_GET ( "allow_screen_upload", bAllowScreenUpload ); if ( bAllowScreenUpload ) { // Get pixels onto offscreen surface IDirect3DSurface9* pLockableSurface = GetRTLockableSurface ( pD3DSurface ); // Then bResult = GetSurfacePixels ( pLockableSurface, outPixels, pRect ); } else { // If not allowed, return dummy data uint uiPixelsWidth = 32; uint uiPixelsHeight = 32; outPixels.SetSize ( uiPixelsWidth * uiPixelsHeight * XRGB_BYTES_PER_PIXEL + SIZEOF_PLAIN_TAIL ); memset ( outPixels.GetData (), 0xEF, outPixels.GetSize () ); bResult = SetPlainDimensions ( outPixels, uiPixelsWidth, uiPixelsHeight ); } } else if ( Desc.Usage == 0 ) { if ( Desc.Format == D3DFMT_A8R8G8B8 || Desc.Format == D3DFMT_X8R8G8B8 || Desc.Format == D3DFMT_R5G6B5 ) { // Direct reading will work here bResult = GetSurfacePixels ( pD3DSurface, outPixels, pRect ); } else { // If not a simple format, use D3DX to convert if ( !GetMatchingOffscreenSurface ( pD3DSurface, m_pTempOffscreenSurface, D3DFMT_A8R8G8B8 ) ) return false; if ( FAILED ( D3DXLoadSurfaceFromSurface ( m_pTempOffscreenSurface, NULL, NULL, pD3DSurface, NULL, NULL, D3DX_FILTER_NONE, 0 ) ) ) return false; // Then bResult = GetSurfacePixels ( m_pTempOffscreenSurface, outPixels, pRect ); } } return bResult; }
//////////////////////////////////////////////////////////////// // // CPixelsManager::SetTexturePixels // // Copy pixels into texture // //////////////////////////////////////////////////////////////// bool CPixelsManager::SetTexturePixels ( IDirect3DBaseTexture9* pD3DBaseTexture, const CPixels& pixels, const RECT* pRect, uint uiSurfaceIndex ) { if ( !pD3DBaseTexture ) return false; IDirect3DSurface9* pD3DSurface = NULL; CAutoReleaseMe < IDirect3DSurface9 > Thanks( pD3DSurface ); D3DRESOURCETYPE resourceType = pD3DBaseTexture->GetType (); if ( resourceType == D3DRTYPE_VOLUMETEXTURE ) { return SetVolumeTexturePixels ( (IDirect3DVolumeTexture9*)pD3DBaseTexture, pixels, pRect, uiSurfaceIndex ); } else if ( resourceType == D3DRTYPE_CUBETEXTURE ) { D3DCUBEMAP_FACES FaceType = (D3DCUBEMAP_FACES)uiSurfaceIndex; ((IDirect3DCubeTexture9*)pD3DBaseTexture)->GetCubeMapSurface ( FaceType, 0, &pD3DSurface ); if ( !pD3DSurface ) return false; } else if ( resourceType == D3DRTYPE_TEXTURE ) { ((IDirect3DTexture9*)pD3DBaseTexture)->GetSurfaceLevel ( 0, &pD3DSurface ); if ( !pD3DSurface ) return false; } bool bResult = false; D3DSURFACE_DESC Desc; pD3DSurface->GetDesc ( &Desc ); if ( Desc.Usage == D3DUSAGE_RENDERTARGET ) { // Get pixels onto offscreen surface IDirect3DSurface9* pLockableSurface = GetRTLockableSurface ( pD3DSurface ); // Then bResult = SetSurfacePixels ( pLockableSurface, pixels, pRect ); // And write back if ( bResult ) if ( FAILED ( D3DXLoadSurfaceFromSurface ( pD3DSurface, NULL, NULL, pLockableSurface, NULL, NULL, D3DX_FILTER_NONE, 0 ) ) ) return false; } else if ( Desc.Usage == 0 ) { if ( Desc.Format == D3DFMT_A8R8G8B8 || Desc.Format == D3DFMT_X8R8G8B8 || Desc.Format == D3DFMT_R5G6B5 ) { // Direct reading will work here bResult = SetSurfacePixels ( pD3DSurface, pixels, pRect ); } else { // If not a simple format, use D3DX to convert if ( !GetMatchingOffscreenSurface ( pD3DSurface, m_pTempOffscreenSurface, D3DFMT_A8R8G8B8 ) ) return false; if ( FAILED ( D3DXLoadSurfaceFromSurface ( m_pTempOffscreenSurface, NULL, NULL, pD3DSurface, NULL, NULL, D3DX_FILTER_NONE, 0 ) ) ) return false; // Then bResult = SetSurfacePixels ( m_pTempOffscreenSurface, pixels, pRect ); // And write back if ( bResult ) if ( FAILED ( D3DXLoadSurfaceFromSurface ( pD3DSurface, NULL, NULL, m_pTempOffscreenSurface, NULL, NULL, D3DX_FILTER_NONE, 0 ) ) ) return false; } } return bResult; }