MGuiRendTexture* CRenderer::LoadTexture( const char_t* path, uint32* width, uint32* height ) { CTexture* texture = NULL; D3DSURFACE_DESC desc; IDirect3DTexture9* data; HRESULT hr; // I was hoping I wouldn't have to use D3DX for this :( hr = D3DXCreateTextureFromFileEx( d3dDevice, path, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &data ); if ( SUCCEEDED( hr ) ) { data->GetLevelDesc( 0, &desc ); texture = new CTexture(); texture->texture = data; texture->width = desc.Width; texture->height = desc.Height; if ( width ) *width = desc.Width; if ( height ) *height = desc.Height; } return (MGuiRendTexture*)texture; }
void MGEhud::setTexture(hud_id hud, const char *texture) { Element *e = &elements[hud]; if(e->texture) e->texture->Release(); IDirect3DTexture9 *tex = BSALoadTexture(device, texture); if(tex) { D3DSURFACE_DESC desc; tex->GetLevelDesc(0, &desc); e->w = desc.Width; e->h = desc.Height; e->texture = tex; // As the BSA cache cannot reload a texture after it is released, (it returns a pointer to the released texture) // we have to add a loose reference to keep the texture in memory and avoid a crash tex->AddRef(); e->textureFilename = texture; } else { LOG::logline("LoadHUDTexture : Cannot load texture %s", texture); e->texture = 0; e->textureFilename.clear(); } }
void LoadImage(char *buf, int sizeBuf, int &width, int &height, uint8 ** ppBuffer, bool bAlpha) { #ifdef USE_DIRECTX_RENDERER HRESULT hr; D3DSURFACE_DESC desc; IDirect3DTexture9* pTexture = NULL; D3DFORMAT d3dFormat; if (bAlpha) d3dFormat = D3DFMT_A8R8G8B8; else d3dFormat = D3DFMT_R8G8B8; // read from file hr = D3DXCreateTextureFromFileInMemoryEx( CGlobals::GetRenderDevice(), buf, sizeBuf, 0, 0, 1, 0, d3dFormat, D3DPOOL_SCRATCH, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); if( FAILED(hr) ) return; pTexture->GetLevelDesc( 0, &desc ); // set size width = desc.Width; height = desc.Height; uint8 *pBufferTemp; int nSize; if (bAlpha) nSize = width * height * 4; else nSize = width * height * 3; pBufferTemp = new uint8[nSize]; D3DLOCKED_RECT lockedRect; hr = pTexture->LockRect( 0, &lockedRect, NULL, D3DLOCK_READONLY ); if( SUCCEEDED(hr) ) { uint8 *pImagePixels = (uint8 *) lockedRect.pBits; memcpy(pBufferTemp, pImagePixels, nSize); // *ppBuffer = pBufferTemp; pTexture->UnlockRect( 0 ); } else { width = 0; height = 0; *ppBuffer = NULL; } SAFE_RELEASE( pTexture ); #endif }
void RageDisplay_D3D::UpdateTexture( unsigned uTexHandle, RageSurface* img, int xoffset, int yoffset, int width, int height ) { IDirect3DTexture9* pTex = (IDirect3DTexture9*)uTexHandle; ASSERT( pTex != NULL ); RECT rect; rect.left = xoffset; rect.top = yoffset; rect.right = width - xoffset; rect.bottom = height - yoffset; D3DLOCKED_RECT lr; pTex->LockRect( 0, &lr, &rect, 0 ); D3DSURFACE_DESC desc; pTex->GetLevelDesc(0, &desc); ASSERT( xoffset+width <= int(desc.Width) ); ASSERT( yoffset+height <= int(desc.Height) ); // // Copy bits // #if defined(XBOX) // Xbox textures need to be swizzled XGSwizzleRect( img->pixels, // pSource, img->pitch, // Pitch, NULL, // pRect, lr.pBits, // pDest, img->w, // Width, img->h, // Height, NULL, // pPoint, img->format->BytesPerPixel ); //BytesPerPixel #else int texpixfmt; for(texpixfmt = 0; texpixfmt < NUM_PIX_FORMATS; ++texpixfmt) if(D3DFORMATS[texpixfmt] == desc.Format) break; ASSERT( texpixfmt != NUM_PIX_FORMATS ); RageSurface *Texture = CreateSurfaceFromPixfmt(RagePixelFormat(texpixfmt), lr.pBits, width, height, lr.Pitch); ASSERT( Texture ); RageSurfaceUtils::Blit( img, Texture, width, height ); delete Texture; #endif pTex->UnlockRect( 0 ); }
HRESULT APIENTRY hkIDirect3DDevice9::SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture) { RSManager::setLatest(rsMan); SDLOG(6, "setTexture %d, %p\n", Stage, pTexture); if(Settings::get().getLogLevel() > 10 && pTexture) { IDirect3DTexture9 *tex; if(pTexture->QueryInterface(IID_IDirect3DTexture9, (void**)&tex) == S_OK) { D3DSURFACE_DESC desc; tex->GetLevelDesc(0, &desc); SDLOG(10, " -- size: %dx%d RT? %s\n", desc.Width, desc.Height, (desc.Usage & D3DUSAGE_RENDERTARGET) ? "true" : "false"); tex->Release(); } } return m_pD3Ddev->SetTexture(Stage, pTexture); }
TextureStorage9_2D::TextureStorage9_2D(Renderer9 *renderer, SwapChain9 *swapchain) : TextureStorage9(renderer, D3DUSAGE_RENDERTARGET) { IDirect3DTexture9 *surfaceTexture = swapchain->getOffscreenTexture(); mTexture = surfaceTexture; mMipLevels = surfaceTexture->GetLevelCount(); mInternalFormat = swapchain->getRenderTargetInternalFormat(); D3DSURFACE_DESC surfaceDesc; surfaceTexture->GetLevelDesc(0, &surfaceDesc); mTextureWidth = surfaceDesc.Width; mTextureHeight = surfaceDesc.Height; mTextureFormat = surfaceDesc.Format; mRenderTargets.resize(mMipLevels, nullptr); }
ImagePtr D3D9VideoBufferManager::LoadImage_(const TString128 & source, IMAGE_FILTER filter) { DataStreamPtr stream = ResourceManager::Instance()->OpenResource(source.c_str()); if (stream == NULL) { LOG_PRINT_FORMAT("image '%s' can't find\n", source.c_str()); return NULL; } D3DXIMAGE_INFO ImgInfo; IDirect3DTexture9 * texture; HRESULT hr = D3DXCreateTextureFromFileInMemoryEx(mD3D9Device, stream->GetData(), stream->Size(), D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_SCRATCH, D3D9Mapping::GetD3DXFilter(filter), D3DX_DEFAULT, 0, &ImgInfo, NULL, &texture); D3DErrorExceptionFunction(D3DXCreateTextureFromInMemoryEx, hr); D3DSURFACE_DESC desc; texture->GetLevelDesc(0, &desc); D3D9Image * image = new D3D9Image(); image->mWidth = desc.Width; image->mHeight = desc.Height; image->mSrcWidth = ImgInfo.Width; image->mSrcHeight = ImgInfo.Height; image->mFormat = D3D9Mapping::GetFormat(desc.Format); image->mMipmapLevel = texture->GetLevelCount(); image->mD3D9Texture = texture; return ImagePtr(image); }
TextureStorage9_2D::TextureStorage9_2D(Renderer9 *renderer, SwapChain9 *swapchain) : TextureStorage9(renderer, D3DUSAGE_RENDERTARGET) { IDirect3DTexture9 *surfaceTexture = swapchain->getOffscreenTexture(); mTexture = surfaceTexture; mMipLevels = surfaceTexture->GetLevelCount(); mInternalFormat = swapchain->GetBackBufferInternalFormat(); D3DSURFACE_DESC surfaceDesc; surfaceTexture->GetLevelDesc(0, &surfaceDesc); mTextureWidth = surfaceDesc.Width; mTextureHeight = surfaceDesc.Height; mTextureFormat = surfaceDesc.Format; mRenderTarget = NULL; initializeSerials(1, 1); }
HRESULT APIENTRY hkIDirect3DDevice9::SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture) { RSManager::setLatest(rsMan); SDLOG(6, "setTexture %d, %p\n", Stage, pTexture); if(pTexture != NULL && Settings::get().getEnableAlternativeTextureDumping()) { void *unused; if(pTexture->QueryInterface(IID_GedosatoTexture, &unused) == S_OK) { pTexture = reinterpret_cast<hkIDirect3DTexture9*>(pTexture)->m_pWrapped; SDLOG(6, " - wrapper for %p\n", pTexture); } } if(Settings::get().getLogLevel() > 10 && pTexture) { IDirect3DTexture9 *tex; if(pTexture->QueryInterface(IID_IDirect3DTexture9, (void**)&tex) == S_OK) { D3DSURFACE_DESC desc; tex->GetLevelDesc(0, &desc); SDLOG(10, " -- size: %dx%d RT? %s\n", desc.Width, desc.Height, (desc.Usage & D3DUSAGE_RENDERTARGET) ? "true" : "false"); tex->Release(); } } return m_pD3Ddev->SetTexture(Stage, pTexture); }
ImagePtr D3D9VideoBufferManager::LoadImageFromFile(const TString128 & filename, int w, int h, IMAGE_FILTER filter) { D3DXIMAGE_INFO ImgInfo; IDirect3DTexture9 * texture; HRESULT hr = D3DXCreateTextureFromFileEx(mD3D9Device, filename.c_str(), w, h, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_SCRATCH, D3D9Mapping::GetD3DXFilter(filter), D3DX_DEFAULT, 0, &ImgInfo, NULL, &texture); D3DErrorExceptionFunction(D3DXCreateTextureFromFileEx, hr); D3DSURFACE_DESC desc; texture->GetLevelDesc(0, &desc); D3D9Image * image = new D3D9Image(); image->mWidth = desc.Width; image->mHeight = desc.Height; image->mSrcWidth = ImgInfo.Width; image->mSrcHeight = ImgInfo.Height; image->mFormat = D3D9Mapping::GetFormat(desc.Format); image->mMipmapLevel = texture->GetLevelCount(); image->mD3D9Texture = texture; return ImagePtr(image); }
void ConvertImageFileToBMPBuffer(string szSrcFileName, LPBYTE * bytes, long* len) { if(!FileExists(szSrcFileName)) return; ATG::Timer m_Timer; double timeA = m_Timer.GetAbsoluteTime(); IDirect3DTexture9 * pTexture; HRESULT retVal = D3DXCreateTextureFromFileEx( CFreestyleApp::getInstance().m_pd3dDevice, szSrcFileName.c_str(), D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 1, D3DUSAGE_CPU_CACHED_MEMORY, D3DFMT_LIN_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); float timeB = m_Timer.GetAbsoluteTime(); DebugMsg("Test", "Texture Creation: %4.2f", (timeB- timeA)); if(retVal == S_OK) { timeA = m_Timer.GetAbsoluteTime(); // Get our level desc D3DSURFACE_DESC desc; pTexture->GetLevelDesc(0, &desc); // Now lock our data D3DLOCKED_RECT lock; RECT rect = {0, 0, desc.Width, desc.Height}; pTexture->LockRect(0, &lock, &rect, D3DLOCK_READONLY); //Read our data DWORD headerSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); DWORD dataLen = lock.Pitch * desc.Height; DWORD * dataBuffer = (DWORD*)malloc( dataLen + headerSize ); DWORD * address = (DWORD*)lock.pBits; // Create file our header BITMAPFILEHEADER* fHead = (BITMAPFILEHEADER*)dataBuffer; fHead->bfType = 0x424D; // "BM" fHead->bfSize = SwapDWORD(dataLen + headerSize); fHead->bfReserved1 = 0; fHead->bfReserved2 = 0; fHead->bfOffBits = SwapDWORD(headerSize); // Create our info header BITMAPINFOHEADER* iHead = (BITMAPINFOHEADER*)(fHead + 1); ZeroMemory(iHead, sizeof(BITMAPINFOHEADER)); iHead->biSize = SwapDWORD(sizeof(BITMAPINFOHEADER)); iHead->biWidth = SwapDWORD(desc.Width); iHead->biHeight = SwapDWORD(desc.Height); iHead->biPlanes = SwapWORD(1); iHead->biBitCount = SwapWORD(32); iHead->biSizeImage = SwapDWORD(dataLen); // Copy over our raw (BGRA) DWORD* rawPtr = (DWORD*)(iHead + 1); for(int y = desc.Height - 1; y >= 0; y--) { for(DWORD x = 0; x < desc.Width; x++) { DWORD cp = (y * lock.Pitch) + (x * 4); DWORD * temp = (DWORD*)(address + (cp / 4)); *rawPtr = SwapDWORD(*temp); rawPtr++; } } // Unlock our texture pTexture->UnlockRect(0); timeB = m_Timer.GetAbsoluteTime(); DebugMsg("Test", "Texture Lock Time: %4.2f", (timeB- timeA)); *len = (headerSize + dataLen); *bytes = (BYTE*)dataBuffer; }else { DebugMsg("ConvertImageInMemoryToBMPBuffer", "Conversion To BMP From Memory Failed. [%X]", retVal); } // Release our Texture if(pTexture != NULL) pTexture->Release(); }
HRESULT ConvertMemoryBufferToBMPBuffer( MemoryBuffer &image_in, MemoryBuffer &image_out) { // Check to ensure that our data has a valid length before proceeding if( image_in.GetDataLength() == 0 ) return S_FALSE; // Set up a timer class to profile our code ATG::Timer m_Timer; double timeStart, timeStop; // Begin Profiling our Texture Creation Code timeStart = m_Timer.GetAbsoluteTime(); IDirect3DTexture9 * pTexture; HRESULT retVal = D3DXCreateTextureFromFileInMemoryEx( CFreestyleApp::getInstance().m_pd3dDevice, image_in.GetData(), image_in.GetDataLength(), D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 1, D3DUSAGE_CPU_CACHED_MEMORY, D3DFMT_LIN_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); // End Profiling our Texture Creation Code timeStop = m_Timer.GetAbsoluteTime(); DebugMsg("ConvertImageToBMP", "Texture Creation took %4.2f seconds to complete", (float)(timeStop - timeStart)); // If our texture was created successfully, if(retVal = D3D_OK) { //Begin Profiling our Data Manipulation Code timeStart = m_Timer.GetAbsoluteTime(); // Get our level desc D3DSURFACE_DESC desc; pTexture->GetLevelDesc(0, &desc); // Now lock our data D3DLOCKED_RECT lock; RECT rect = {0, 0, desc.Width, desc.Height}; pTexture->LockRect(0, &lock, &rect, D3DLOCK_READONLY); //Read our data DWORD headerSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); DWORD dataLen = lock.Pitch * desc.Height; DWORD * dataBuffer = (DWORD*)malloc( dataLen + headerSize ); DWORD * address = (DWORD*)lock.pBits; // Create file our header BITMAPFILEHEADER* fHead = (BITMAPFILEHEADER*)dataBuffer; fHead->bfType = 0x424D; // "BM" fHead->bfSize = SwapDWORD(dataLen + headerSize); fHead->bfReserved1 = 0; fHead->bfReserved2 = 0; fHead->bfOffBits = SwapDWORD(headerSize); // Create our info header BITMAPINFOHEADER* iHead = (BITMAPINFOHEADER*)(fHead + 1); ZeroMemory(iHead, sizeof(BITMAPINFOHEADER)); iHead->biSize = SwapDWORD(sizeof(BITMAPINFOHEADER)); iHead->biWidth = SwapDWORD(desc.Width); iHead->biHeight = SwapDWORD(desc.Height); iHead->biPlanes = SwapWORD(1); iHead->biBitCount = SwapWORD(32); iHead->biSizeImage = SwapDWORD(dataLen); // Copy over our raw (BGRA) DWORD* rawPtr = (DWORD*)(iHead + 1); for(int y = desc.Height - 1; y >= 0; y--) { for(DWORD x = 0; x < desc.Width; x++) { DWORD cp = (y * lock.Pitch) + (x * 4); DWORD * temp = (DWORD*)(address + (cp / 4)); *rawPtr = SwapDWORD(*temp); rawPtr++; } } // Unlock our texture pTexture->UnlockRect(0); // End Profiling our Data Modification Code timeStop = m_Timer.GetAbsoluteTime(); DebugMsg("ConvertImageToBMP", "Data Modification took %4.2f seconds to complete", (float)(timeStop - timeStart)); // Begin Profiling our Memory Copy Code timeStart = m_Timer.GetAbsoluteTime(); // Copy our completed data to our new buffer image_out.Add(dataBuffer, (headerSize + dataLen)); // End Profiling our Memory Copy Code timeStop = m_Timer.GetAbsoluteTime(); DebugMsg("ConvertImageToBMP", "Image Memory Copy took %4.2f seconds to complete", (float)(timeStop - timeStart)); } else { DebugMsg("ConvertImageInMemoryToBMPBuffer", "Conversion To BMP From Memory Failed. [%X]", retVal); } return retVal; }
CTexture * CRenderManager::LoadTexture(char const * _szFileName, D3DCOLOR const _ColorKey) { if(NULL == _szFileName) { MessageBox(0, "Filename was invalid.", "LoadTexture", MB_OK); return NULL; } // Look if the texture has already been loaded list<CTexture*>::iterator IterText = m_arrTextures.begin(); for( ; IterText != m_arrTextures.end(); ++IterText) { if(0 == strcmp((*IterText)->GetName().c_str(), _szFileName)) return (*IterText); } // Look for the first available slot where the struct is NULL for(IterText = m_arrTextures.begin(); IterText != m_arrTextures.end(); ++IterText) { if(NULL == (*IterText)) { IDirect3DTexture9 * pTexture = NULL; // Set the texture if(FAILED(D3DXCreateTextureFromFileEx(m_pDevice, _szFileName, 0, 0, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, _ColorKey, 0, 0, &pTexture))) { MessageBox(0, "Failed to create texture.", "LoadTexture", MB_OK); return NULL; } CTexture * pNew = new CTexture(); std::string szName = _szFileName; pNew->SetName(szName); pNew->SetTexture(pTexture); pNew->SetColor(_ColorKey); D3DSURFACE_DESC description; pTexture->GetLevelDesc(0, &description); pNew->SetWidth(description.Width); pNew->SetHeight(description.Height); pNew->SetTexture(pTexture); (*IterText) = pNew; return (*IterText); } } IDirect3DTexture9 * pTexture = NULL; // Set the texture if(FAILED(D3DXCreateTextureFromFileEx(m_pDevice, _szFileName, 0, 0, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, _ColorKey, 0, 0, &pTexture))) { MessageBox(0, "Failed to create texture.", "LoadTexture", MB_OK); return NULL; } CTexture * pNew = new CTexture(); std::string szName = _szFileName; pNew->SetName(szName); pNew->SetTexture(pTexture); pNew->SetColor(_ColorKey); D3DSURFACE_DESC description; pTexture->GetLevelDesc(0, &description); pNew->SetWidth(description.Width); pNew->SetHeight(description.Height); pNew->SetTexture(pTexture); m_arrTextures.push_back(pNew); return pNew; }
MF_API bool MFTexture_Map(MFTexture *pTexture, int element, int mipLevel, MFLockedTexture *pLock) { int numFaces = pTexture->type == MFTexType_Cubemap ? 6 : 1; MFDebug_Assert(element < numFaces, "Array textures not supported in D3D9!"); int s = mipLevel*pTexture->numElements + (element ? element : 0); MFTextureSurface &surface = pTexture->pSurfaces[s]; DWORD lockFlags = (pTexture->createFlags & MFTCF_TypeMask) == MFTCF_Scratch ? D3DLOCK_DISCARD : 0; switch(pTexture->type) { case MFTexType_1D: { IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DSURFACE_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_RECT rect; pTex->LockRect(mipLevel, &rect, NULL, lockFlags); pLock->pData = rect.pBits; pLock->width = surface.width; pLock->height = surface.height; pLock->depth = surface.depth; pLock->strideInBytes = rect.Pitch; pLock->sliceInBytes = rect.Pitch * surface.height; break; } case MFTexType_2D: { IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DSURFACE_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_RECT rect; pTex->LockRect(mipLevel, &rect, NULL, lockFlags); pLock->pData = rect.pBits; pLock->width = surface.width; pLock->height = surface.height; pLock->depth = surface.depth; pLock->strideInBytes = rect.Pitch; pLock->sliceInBytes = rect.Pitch * surface.height; break; } case MFTexType_3D: { IDirect3DVolumeTexture9 *pTex = (IDirect3DVolumeTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DVOLUME_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height && (int)desc.Depth == surface.depth, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_BOX box; pTex->LockBox(mipLevel, &box, NULL, lockFlags); pLock->pData = box.pBits; pLock->width = surface.width; pLock->height = surface.height; pLock->depth = surface.depth; pLock->strideInBytes = box.RowPitch; pLock->sliceInBytes = box.SlicePitch; break; } case MFTexType_Cubemap: { IDirect3DCubeTexture9 *pTex = (IDirect3DCubeTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DSURFACE_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_RECT rect; pTex->LockRect(gD3DCubeFaces[element], mipLevel, &rect, NULL, lockFlags); pLock->pData = rect.pBits; pLock->width = surface.width; pLock->height = surface.height; pLock->depth = surface.depth; pLock->strideInBytes = rect.Pitch; pLock->sliceInBytes = rect.Pitch * surface.height; break; } } return true; }
MF_API bool MFTexture_Update(MFTexture *pTexture, int element, int mipLevel, const void *pData, size_t lineStride, size_t sliceStride) { int numFaces = pTexture->type == MFTexType_Cubemap ? 6 : 1; MFDebug_Assert(element < numFaces, "Array textures not supported in D3D9!"); int s = mipLevel*pTexture->numElements + (element > -1 ? element : 0); MFTextureSurface &surface = pTexture->pSurfaces[s]; DWORD lockFlags = (pTexture->createFlags & MFTCF_TypeMask) == MFTCF_Scratch ? D3DLOCK_DISCARD : 0; size_t lineBytes = (surface.bitsPerPixel * surface.width) / 8; if(lineStride == 0) lineStride = lineBytes; if(sliceStride == 0) sliceStride = lineStride * surface.width; switch(pTexture->type) { case MFTexType_1D: { IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DSURFACE_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_RECT rect; pTex->LockRect(mipLevel, &rect, NULL, lockFlags); MFCopyMemory(rect.pBits, pData, lineBytes); pTex->UnlockRect(mipLevel); break; } case MFTexType_2D: { IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DSURFACE_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_RECT rect; pTex->LockRect(mipLevel, &rect, NULL, lockFlags); const char *pSrc = (const char*)pData; char *pDest = (char*)rect.pBits; for(int i=0; i<surface.height; ++i) { MFCopyMemory(pDest, pSrc, lineBytes); pDest += rect.Pitch; pSrc += lineStride; } pTex->UnlockRect(mipLevel); break; } case MFTexType_3D: { IDirect3DVolumeTexture9 *pTex = (IDirect3DVolumeTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DVOLUME_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height && (int)desc.Depth == surface.depth, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_BOX box; pTex->LockBox(mipLevel, &box, NULL, lockFlags); MFCopyMemory(box.pBits, pData, surface.bufferLength); const char *pSrcSlice = (const char*)pData; char *pDestSlice = (char*)box.pBits; for(int d=0; d<surface.depth; ++d) { const char *pSrcLine = pSrcSlice; char *pDestLine = pDestSlice; for(int i=0; i<surface.height; ++i) { MFCopyMemory(pDestLine, pSrcLine, lineBytes); pDestLine += box.RowPitch; pSrcLine += lineStride; } pSrcSlice += sliceStride; pDestSlice += box.SlicePitch; } pTex->UnlockBox(mipLevel); break; } case MFTexType_Cubemap: { MFDebug_Assert(element != -1, "TODO: must handle setting all surfaces at once..."); IDirect3DCubeTexture9 *pTex = (IDirect3DCubeTexture9*)pTexture->pInternalData; #if defined(MF_DEBUG) D3DSURFACE_DESC desc; pTex->GetLevelDesc(mipLevel, &desc); MFDebug_Assert((int)desc.Width == surface.width && (int)desc.Height == surface.height, "D3D9 mip dimensions don't match the texture template data..."); #endif D3DLOCKED_RECT rect; pTex->LockRect(gD3DCubeFaces[element], mipLevel, &rect, NULL, lockFlags); const char *pSrc = (const char*)pData; char *pDest = (char*)rect.pBits; for(int i=0; i<surface.height; ++i) { MFCopyMemory(pDest, pSrc, lineBytes); pDest += rect.Pitch; pSrc += lineStride; } pTex->UnlockRect(gD3DCubeFaces[element], mipLevel); break; } } return true; }
HRESULT Loader::LoadElevations( float **ppImageData, int* nSize, const char * szFilename, bool swapVertical /*= true*/) { float* pImageData = NULL; CParaFile cFile; cFile.OpenAssetFile(szFilename, true, ParaTerrain::Settings::GetInstance()->GetMediaPath()); if(cFile.isEof()) return E_FAIL; int elevWidth=0; int elevHeight=0; if(strcmp(szFilename+strlen(szFilename)-4, ".raw") == 0) { /// Load from raw elevation elevWidth = (int)sqrt((float)(((int)cFile.getSize()/4))); elevHeight = elevWidth; /// just use the file buffer as the image buffer pImageData = (float*)cFile.getBuffer(); cFile.GiveupBufferOwnership(); } else { #ifdef USE_DIRECTX_RENDERER D3DSURFACE_DESC desc; IDirect3DTexture9* pTexture = NULL; HRESULT hr; /// Create a D3DFMT_X8R8G8B8 texture -- use D3DPOOL_SCRATCH to // ensure that this will succeeded independent of the device hr = D3DXCreateTextureFromFileInMemoryEx( CGlobals::GetRenderDevice(), cFile.getBuffer(), (int)cFile.getSize(), 0, 0, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_SCRATCH, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); if( FAILED(hr) ) return hr; pTexture->GetLevelDesc( 0, &desc ); elevWidth = desc.Width; elevHeight = desc.Height; // create an array of floats to store the values of the bmp pImageData = new float[elevWidth * elevHeight]; if( pImageData == NULL ) { SAFE_RELEASE( pTexture ); return E_OUTOFMEMORY; } //ZeroMemory( pImageData, elevWidth * elevHeight * sizeof(FLOAT) ); D3DLOCKED_RECT lockedRect; hr = pTexture->LockRect( 0, &lockedRect, NULL, D3DLOCK_READONLY ); float fMin=1.0f,fMax=-1.0f; if( SUCCEEDED(hr) ) { DWORD* pBuffer = (DWORD*) lockedRect.pBits; for( DWORD iY=0; iY<desc.Height; iY++ ) { pBuffer = (DWORD*)((BYTE*)lockedRect.pBits + lockedRect.Pitch * iY); for( DWORD iX=0; iX<desc.Width; iX++ ) { LinearColor color((DWORD)(*pBuffer)); float fValue = (color.r+color.g+color.b)/3.0f-0.5f; if (fValue<fMin) fMin = fValue; if (fValue>fMax) fMax = fValue; if (swapVertical) { // Invert Y, so it appears the same as the bmp pImageData[ (desc.Height-1-iY)*elevWidth + iX] = fValue; } else { pImageData[ iY*elevWidth + iX] = fValue; } pBuffer++; } } pTexture->UnlockRect( 0 ); /// Normalize all values between 0.0f and 1.0f /*float fHeight = (fMax-fMin); for( DWORD iY=0; iY<desc.Height; iY++ ) { for( DWORD iX=0; iX<desc.Width; iX++ ) { pImageData[ iY*elevWidth + iX] = (pImageData[ iY*elevWidth + iX]-fMin)/fHeight; } }*/ } SAFE_RELEASE( pTexture ); #else return E_FAIL; #endif } // TODO: re-enable this when you are ready. const bool FORCE_256_mesh = false; if(elevWidth == 129 && FORCE_256_mesh) { // make it 257. int elevWidthDest = 257; float * pImageDataDest = new float[elevWidthDest * elevWidthDest]; for (int i=0; i<elevWidthDest; i++) { for (int j=0; j<elevWidthDest; j++) { int rI = i%2; int ltX = i/2; int rJ = j%2; int ltY = j/2; if(rI == 0) { if(rJ == 0) { pImageDataDest[i*elevWidthDest+j] = pImageData[ltX*elevWidth+ltY]; } else { pImageDataDest[i*elevWidthDest+j] = (pImageData[ltX*elevWidth+ltY+1] + pImageData[ltX*elevWidth+ltY]) * 0.5f; } } else { if(rJ == 0) { pImageDataDest[i*elevWidthDest+j] = (pImageData[(ltX+1)*elevWidth+ltY] + pImageData[ltX*elevWidth+ltY])*0.5f; } else { pImageDataDest[i*elevWidthDest+j] = (pImageData[(ltX+1)*elevWidth+ltY]+pImageData[ltX*elevWidth+ltY]+pImageData[ltX*elevWidth+ltY+1]+pImageData[(ltX+1)*elevWidth+ltY+1])*0.25f; } } } } SAFE_DELETE(pImageData); pImageData = pImageDataDest; elevWidth = elevWidthDest; } *nSize = elevWidth; *ppImageData = pImageData; return S_OK; }
IDirect3DTexture9* Core::LoadTexture( IDirect3DDevice9* device, const tstring& file, u32* textureSize, bool* hasAlpha, D3DPOOL pool ) { IDirect3DTexture9* texture = NULL; bool alpha = false; if (!file.empty()) { if ( Helium::Path( file ).Exists() ) { D3DFORMAT textureFormat = D3DFMT_DXT1; D3DXIMAGE_INFO sourceInfo; LPDIRECT3DTEXTURE9 tempTexture; if ( D3DXCreateTextureFromFileEx( device, file.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, pool, D3DX_DEFAULT, // filter? D3DX_FILTER_NONE, // mip filter NULL, &sourceInfo, NULL, &tempTexture ) == D3D_OK ) { // lock the first mip level, and iterate over all pixels looking for // alpha != 0 || alpha != 255 D3DLOCKED_RECT lockedRect; HRESULT locked = tempTexture->LockRect(0, &lockedRect, NULL, D3DLOCK_READONLY); D3DSURFACE_DESC desc; tempTexture->GetLevelDesc(0, &desc); for(u32 r = 0; r < desc.Height && textureFormat == D3DFMT_DXT1; r++) { u32* pixels = (u32*) (((u8*)lockedRect.pBits) + lockedRect.Pitch * r); for(u32 c = 0; c < desc.Width; c++) { u32 masked = pixels[c] & 0xFF000000; if(masked != 0xFF000000) { alpha = true; } if(masked != 0xFF000000 && masked != 0x00000000) { textureFormat = D3DFMT_DXT5; break; } } } tempTexture->UnlockRect(0); tempTexture->Release(); } int compressionRatio; switch ( textureFormat ) { case D3DFMT_DXT1: { if (alpha) { compressionRatio = 8; } else { compressionRatio = 6; } break; } case D3DFMT_DXT5: { compressionRatio = 4; break; } default: { compressionRatio = 1; HELIUM_BREAK(); break; } } if ( D3DXCreateTextureFromFileEx( device, file.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, NULL, textureFormat, pool, D3DX_DEFAULT, D3DX_DEFAULT, NULL, &sourceInfo, NULL, &texture ) == D3D_OK ) { texture->SetAutoGenFilterType( D3DTEXF_ANISOTROPIC ); if ( textureSize ) { *textureSize = 0; for(u32 i = 0; i < texture->GetLevelCount(); i++) { D3DSURFACE_DESC desc; texture->GetLevelDesc(i, &desc); *textureSize += desc.Width * desc.Height * 4 / compressionRatio; } } } else { Log::Warning( TXT( "Unable to create texture from file '%s'\n" ), file.c_str() ); } } else { Log::Warning( TXT( "File '%s' does not exist\n" ), file.c_str() ); } } if ( hasAlpha ) { *hasAlpha = alpha; } return texture; }
void DeprecatedTextureHostSystemMemD3D9::UpdateImpl(const SurfaceDescriptor& aImage, nsIntRegion *aRegion, nsIntPoint *aOffset) { MOZ_ASSERT(aImage.type() == SurfaceDescriptor::TSurfaceDescriptorD3D9); MOZ_ASSERT(mCompositor, "Must have compositor to update."); if (!mCompositor->device()) { return; } IDirect3DTexture9* texture = reinterpret_cast<IDirect3DTexture9*>(aImage.get_SurfaceDescriptorD3D9().texture()); if (!texture) { Reset(); return; } D3DSURFACE_DESC desc; texture->GetLevelDesc(0, &desc); HRESULT hr = texture->GetLevelDesc(0, &desc); if (FAILED(hr)) { Reset(); return; } mSize.width = desc.Width; mSize.height = desc.Height; _D3DFORMAT format = desc.Format; uint32_t bpp = 0; switch (format) { case D3DFMT_X8R8G8B8: mFormat = SurfaceFormat::B8G8R8X8; bpp = 4; break; case D3DFMT_A8R8G8B8: mFormat = SurfaceFormat::B8G8R8A8; bpp = 4; break; case D3DFMT_A8: mFormat = SurfaceFormat::A8; bpp = 1; break; default: NS_ERROR("Bad image format"); } int32_t maxSize = mCompositor->GetMaxTextureSize(); if (mSize.width <= maxSize && mSize.height <= maxSize) { mIsTiled = false; mTexture = TextureToTexture(gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(), texture, mSize, format); if (!mTexture) { NS_WARNING("Could not upload texture"); Reset(); return; } } else { mIsTiled = true; uint32_t tileCount = GetRequiredTilesD3D9(mSize.width, maxSize) * GetRequiredTilesD3D9(mSize.height, maxSize); mTileTextures.resize(tileCount); for (uint32_t i = 0; i < tileCount; i++) { IntRect tileRect = GetTileRect(i); RECT d3dTileRect; d3dTileRect.left = tileRect.x; d3dTileRect.top = tileRect.y; d3dTileRect.right = tileRect.XMost(); d3dTileRect.bottom = tileRect.YMost(); D3DLOCKED_RECT lockedRect; texture->LockRect(0, &lockedRect, &d3dTileRect, 0); mTileTextures[i] = DataToTexture(gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(), reinterpret_cast<unsigned char*>(lockedRect.pBits), lockedRect.Pitch, tileRect.Size(), format, bpp); texture->UnlockRect(0); if (!mTileTextures[i]) { NS_WARNING("Could not upload texture"); Reset(); return; } } } }
bool RenderTargetD3D9Texture::getTextureData(void * pData) { if( 0 == m_pTexture || 0 == pData ) return false; IDirect3DTexture9* pSrcTexture = m_pTexture->getD3DTexture(); if( 0 == pSrcTexture ) return false; D3DSURFACE_DESC srcTexDesc; pSrcTexture->GetLevelDesc(0, &srcTexDesc); IDirect3DTexture9* pDestTex =0; IDirect3DDevice9* pD3D9Device = m_pRenderSystem->getD3D9Device()->getDevice(); HRESULT hr = pD3D9Device->CreateTexture( srcTexDesc.Width, srcTexDesc.Height, 0, 0, srcTexDesc.Format, D3DPOOL_SYSTEMMEM, &pDestTex, 0); if( FAILED(hr)) { return false; } IDirect3DSurface9* pSrcSurf =0; pSrcTexture->GetSurfaceLevel(0, &pSrcSurf); IDirect3DSurface9* pDestSurf = 0; pDestTex->GetSurfaceLevel(0, &pDestSurf); hr = pD3D9Device->GetRenderTargetData( pSrcSurf, pDestSurf); if( FAILED(hr)) { COM_SAFE_RELEASE(pSrcSurf); COM_SAFE_RELEASE(pDestSurf); COM_SAFE_RELEASE(pDestTex); return false; } else { COM_SAFE_RELEASE(pSrcSurf); } D3DSURFACE_DESC destSurfDesc; pDestSurf->GetDesc(&destSurfDesc); D3DLOCKED_RECT lockrect; pDestSurf->LockRect(&lockrect, NULL, D3DLOCK_READONLY); if( m_pTexture->hasAlpha() ) { if( destSurfDesc.Format == D3DFMT_A8B8G8R8 ) { uchar* pDesData = (uchar*)pData; for( UINT i=0; i < destSurfDesc.Height; ++i) { uchar* pSrcData = (uchar*)lockrect.pBits + i * lockrect.Pitch; for( UINT j =0; j<destSurfDesc.Width; ++j) { uint startAddr = j * 4; pDesData[0] = pSrcData[0];//red pDesData[1] = pSrcData[1];//green; pDesData[2] = pSrcData[2];//blue; pDesData[3] = pSrcData[3];//alpha pDesData +=4; pSrcData +=4; } } } else { pDestSurf->UnlockRect(); COM_SAFE_RELEASE(pDestSurf); COM_SAFE_RELEASE(pDestTex); return false; } } else { if( destSurfDesc.Format == D3DFMT_R8G8B8) { uchar* pDesData = (uchar*)pData; for( UINT i=0; i < destSurfDesc.Height; ++i) { uchar* pSrcData = (uchar*)lockrect.pBits + i * lockrect.Pitch; for( UINT j =0; j<destSurfDesc.Width; ++j) { pDesData[0] = pSrcData[2];//red pDesData[1] = pSrcData[1];//green; pDesData[2] = pSrcData[0];//blue; pDesData +=3; pSrcData +=3; } } } else if( destSurfDesc.Format == D3DFMT_X8R8G8B8) { uchar* pDesData = (uchar*)pData; for( int i= destSurfDesc.Height-1; i >=0 ; --i) { uchar* pSrcData = (uchar*)lockrect.pBits + i * lockrect.Pitch; for( UINT j =0; j<destSurfDesc.Width; ++j) { pDesData[0] = pSrcData[2];//red pDesData[1] = pSrcData[1];//green; pDesData[2] = pSrcData[0];//blue; pDesData +=3; pSrcData +=4; } } } else { pDestSurf->UnlockRect(); COM_SAFE_RELEASE(pDestSurf); COM_SAFE_RELEASE(pDestTex); return false; } } pDestSurf->UnlockRect(); COM_SAFE_RELEASE(pDestSurf); COM_SAFE_RELEASE(pDestTex); return true; }
HRESULT KG3DGraphicsEngine::ScreenShotImpl() { HRESULT hResult = E_FAIL; HRESULT hRetCode = E_FAIL; IDirect3DSurface9* pFrameSurface = NULL; IDirect3DTexture9* pTextureSys = NULL; ID3DXBuffer* pBuffer = NULL; D3DVIEWPORT9 ViewPort; RECT RectView; ASSERT(m_eShotImageType != D3DXIFF_FORCE_DWORD); hRetCode = g_pd3dDevice->GetRenderTarget(0, &pFrameSurface); KG_COM_PROCESS_ERROR(hRetCode); g_pd3dDevice->GetViewport(&ViewPort); SetRect(&RectView, ViewPort.X, ViewPort.Y, ViewPort.X + ViewPort.Width, ViewPort.Y + ViewPort.Height); if (m_eShotImageType == D3DXIFF_JPG) { D3DSURFACE_DESC SurfaceDesc; D3DLOCKED_RECT LockRect; hRetCode = D3DXSaveSurfaceToFileInMemory(&pBuffer, D3DXIFF_BMP, pFrameSurface, NULL, &RectView); KG_COM_PROCESS_ERROR(hRetCode); hRetCode = D3DXCreateTextureFromFileInMemoryEx(g_pd3dDevice, pBuffer->GetBufferPointer(), pBuffer->GetBufferSize(), ViewPort.Width, ViewPort.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTextureSys); KG_COM_PROCESS_ERROR(hRetCode); /* hRetCode = D3DXSaveSurfaceToFile(TEXT("__temp_shot.bmp"), D3DXIFF_BMP, pFrameSurface, NULL, &RectView); KG_COM_PROCESS_ERROR(hRetCode); hRetCode = D3DXCreateTextureFromFileEx(g_pd3dDevice, TEXT("__temp_shot.bmp"), ViewPort.Width, ViewPort.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTextureSys); KG_COM_PROCESS_ERROR(hRetCode); */ pTextureSys->GetLevelDesc(0, &SurfaceDesc); if (SUCCEEDED(pTextureSys->LockRect(0, &LockRect, NULL, D3DLOCK_NOSYSLOCK))) { struct _Rgba { BYTE r; BYTE g; BYTE b; BYTE a;}; struct _Rgb { BYTE b; BYTE g; BYTE r; }; _Rgb* pRgb = new _Rgb[SurfaceDesc.Height * SurfaceDesc.Width]; for (UINT v = 0; v < SurfaceDesc.Height; ++v) { _Rgba* pRgba = (_Rgba*)((BYTE*)LockRect.pBits + LockRect.Pitch * v); for (UINT u = 0; u < SurfaceDesc.Width; ++u) { pRgb[v * SurfaceDesc.Width + u].r = pRgba[u].r; pRgb[v * SurfaceDesc.Width + u].g = pRgba[u].g; pRgb[v * SurfaceDesc.Width + u].b = pRgba[u].b; } } JpegFile::RGBToJpegFile(m_szShotPath, (BYTE*)pRgb, SurfaceDesc.Width, SurfaceDesc.Height, TRUE, m_nShotImageQuty); delete[] pRgb; pTextureSys->UnlockRect(0); } else { KG_PROCESS_ERROR(FALSE); } } else { hRetCode = D3DXSaveSurfaceToFile(m_szShotPath, m_eShotImageType, pFrameSurface, NULL, &RectView); KG_COM_PROCESS_ERROR(hRetCode); } hResult = S_OK; Exit0 : SAFE_RELEASE(pBuffer); SAFE_RELEASE(pTextureSys); SAFE_RELEASE(pFrameSurface); return hResult; }