ImagePtr D3D9VideoBufferManager::CreateImage(int iWidth, int iHeight, FORMAT Format) { IDirect3DTexture9 * texture; HRESULT hr = D3DXCreateTexture(mD3D9Device, iWidth, iHeight, 1, 0, D3D9Mapping::GetD3DFormat(Format), D3DPOOL_SYSTEMMEM, &texture); D3DErrorExceptionFunction(D3DXCreateTexture, hr); IDirect3DSurface9 * surface; texture->GetSurfaceLevel(0, &surface); D3DSURFACE_DESC desc; surface->GetDesc(&desc); D3D9Image * image = new D3D9Image(); image->mWidth = desc.Width; image->mHeight = desc.Height; image->mSrcWidth = iWidth; image->mSrcHeight = iHeight; image->mFormat = D3D9Mapping::GetFormat(desc.Format); image->mMipmapLevel = texture->GetLevelCount(); image->mD3D9Texture = texture; surface->Release(); 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->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); }
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); }
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; }