//////////////////////////////////////////////////////////////// // // CFileTextureItem::CreateUnderlyingData // // From file // //////////////////////////////////////////////////////////////// void CFileTextureItem::CreateUnderlyingData(const SString& strFilename, bool bMipMaps, uint uiSizeX, uint uiSizeY, ERenderFormat format) { assert(!m_pD3DTexture); D3DXIMAGE_INFO imageInfo; if (FAILED(D3DXGetImageInfoFromFile(strFilename, &imageInfo))) return; D3DFORMAT D3DFormat = (D3DFORMAT)format; int iMipMaps = bMipMaps ? D3DX_DEFAULT : 1; if (uiSizeX != D3DX_DEFAULT) imageInfo.Width = uiSizeX; if (uiSizeY != D3DX_DEFAULT) imageInfo.Height = uiSizeY; m_uiSizeX = imageInfo.Width; m_uiSizeY = imageInfo.Height; m_uiSurfaceSizeX = imageInfo.Width; m_uiSurfaceSizeY = imageInfo.Height; if (imageInfo.ResourceType == D3DRTYPE_VOLUMETEXTURE) { // It's a volume texture! if (FAILED(D3DXCreateVolumeTextureFromFileEx(m_pDevice, strFilename, uiSizeX, uiSizeY, D3DX_DEFAULT, iMipMaps, 0, D3DFormat, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, (IDirect3DVolumeTexture9**)&m_pD3DTexture))) return; } else if (imageInfo.ResourceType == D3DRTYPE_CUBETEXTURE) { // It's a cubemap texture! if (FAILED(D3DXCreateCubeTextureFromFileEx(m_pDevice, strFilename, uiSizeX, iMipMaps, 0, D3DFormat, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, (IDirect3DCubeTexture9**)&m_pD3DTexture))) return; } else { // It's none of the above! // If size not specified, try to use exact image size to prevent blurring if (uiSizeX == D3DX_DEFAULT) uiSizeX = D3DX_DEFAULT_NONPOW2; if (uiSizeY == D3DX_DEFAULT) uiSizeY = D3DX_DEFAULT_NONPOW2; if (FAILED(D3DXCreateTextureFromFileEx(m_pDevice, strFilename, uiSizeX, uiSizeY, iMipMaps, 0, D3DFormat, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, (IDirect3DTexture9**)&m_pD3DTexture))) return; // Update surface size if it's a normal texture D3DSURFACE_DESC desc; ((IDirect3DTexture9*)m_pD3DTexture)->GetLevelDesc(0, &desc); m_uiSurfaceSizeX = desc.Width; m_uiSurfaceSizeY = desc.Height; } // Calc memory usage m_iMemoryKBUsed = CRenderItemManager::CalcD3DResourceMemoryKBUsage(m_pD3DTexture); }
//----------------------------------------------------------------------------- // Name: ResourceDatabase::AddTextureVolume //----------------------------------------------------------------------------- TextureVolume* ResourceDatabase::AddTextureVolume( CONST CHAR* strFilename, DWORD Width, DWORD Height, DWORD Depth, D3DFORMAT Format, DWORD Filter, DWORD MipLevels, DWORD MipFilter ) { WCHAR wszConvertedFilename[ _MAX_PATH ]; TextureVolume *pTexture = NULL; Resource *pResource = NULL; const CHAR* strFileOnly = strrchr( strFilename, '\\' ); if( strFileOnly == NULL ) { strFileOnly = strFilename; } else { strFileOnly++; } MultiByteToWideChar( CP_ACP, 0, strFileOnly, strlen( strFileOnly ) + 1, wszConvertedFilename, MAX_PATH ); _wcslwr_s( wszConvertedFilename ); pResource = FindResource( wszConvertedFilename ); if ( pResource ) { // $ERROR: report error of duplicate name assert( pResource->IsDerivedFrom( TextureVolume::TypeID ) ); return (TextureVolume*)pResource; } LPDIRECT3DVOLUMETEXTURE9 pD3DTexture; HRESULT hr = D3DXCreateVolumeTextureFromFileEx( g_pd3dDevice, strFilename, Width, Height, Depth, MipLevels, 0, Format, D3DPOOL_MANAGED,//D3DPOOL_DEFAULT, Filter, MipFilter, 0, NULL, NULL, &pD3DTexture ); if ( FAILED( hr ) ) { ATG::DebugSpew( "Could not load volume texture %s.\n", strFilename ); return NULL; } pTexture = new TextureVolume; pTexture->SetName( wszConvertedFilename ); pTexture->SetD3DTexture( pD3DTexture ); pD3DTexture->Release(); AddResource( pTexture ); return pTexture; }
IDirect3DVolumeTexture9 * TextureCache::_addVolumeTex( const char * srcFile, uint _usage ) { HRESULT hr = 0; IDirect3DVolumeTexture9 * pTexture = 0; uint pool = D3DPOOL_MANAGED; if ( _usage & D3DUSAGE_RENDERTARGET ) // The resource will be a render target. D3DUSAGE_RENDERTARGET can only be used with D3DPOOL_DEFAULT. pool = D3DPOOL_DEFAULT; uint usage = _usage ; //;| D3DUSAGE_AUTOGENMIPMAP; printf( "%s %s\n", __FUNCTION__, srcFile ); D3DXIMAGE_INFO srcInfo; uint levels = 1; hr = D3DXCreateVolumeTextureFromFileEx( device, //LPDIRECT3DDEVICE9 pDevice, srcFile, // LPCTSTR pSrcFile, 0, //UINT Width, 0, //UINT Height, 0, //UINT Depth, 0, //UINT MipLevels, usage, //DWORD Usage, D3DFMT_UNKNOWN, //D3DFORMAT Format, (D3DPOOL) pool, //D3DPOOL Pool, D3DX_DEFAULT , //DWORD Filter, D3DX_DEFAULT , //DWORD MipFilter, 0, //D3DCOLOR ColorKey, &srcInfo, //D3DXIMAGE_INFO *pSrcInfo, 0, //PALETTEENTRY *pPalette, & pTexture //LPDIRECT3DVOLUMETEXTURE9 *ppTexture ); E_TRACE(hr); if ( FAILED( hr ) ) { return 0; } return pTexture; }
bool eae6320::cTextureBuilder::Build( const std::vector<std::string>& ) { bool wereThereErrors = false; if ( !Initialize( m_path_source ) ) { wereThereErrors = true; goto OnExit; } // Get information about the source image D3DXIMAGE_INFO imageInfo_source; { if ( FAILED( D3DXGetImageInfoFromFile( m_path_source, &imageInfo_source ) ) ) { wereThereErrors = true; OutputErrorMessage( "DirectX failed to get image info for the source image", m_path_source ); goto OnExit; } } // Load the source image and do any necessary processing (compress, generate MIP maps, change resolution, etc.) { // Try to match the original image's format as closely as possible D3DFORMAT format = D3DFMT_UNKNOWN; { if ( imageInfo_source.ImageFileFormat == D3DXIFF_DDS ) { // DDS files will remain unchanged // (this could cause problems in the simplistic OpenGL texture loading code that I provide) format = imageInfo_source.Format; } else { // The decision of which DXT format to use is simplistic: // * If it can easily be determined that the source image doesn't have an alpha channel then DXT1 is used // * DXT5 is used for everything else const D3DFORMAT format_original = imageInfo_source.Format; const bool isAlphaChannelUnnecessary = ( format_original == D3DFMT_R8G8B8 ) || ( format_original == D3DFMT_X8R8G8B8 ) || ( format_original == D3DFMT_R5G6B5 ) || ( format_original == D3DFMT_X1R5G5B5 ) || ( format_original == D3DFMT_X4R4G4B4 ) || ( format_original == D3DFMT_X8B8G8R8 ); if ( isAlphaChannelUnnecessary ) { format = D3DFMT_DXT1; } else { format = D3DFMT_DXT5; } } } // The source width and height could be used, but ensuring that the dimensions are always a power-of-2 is more compatible // (and the image will probably end up taking the same amount of space anyway because of alignment issues) const unsigned int roundUpToAPowerOf2 = D3DX_DEFAULT; unsigned int requestedMipMapCount; { const bool doMipMapsExist = imageInfo_source.MipLevels > 1; if ( doMipMapsExist ) { requestedMipMapCount = D3DX_FROM_FILE; } else { // This will generate all of the potential MIP map levels (down to the smallest 1x1) requestedMipMapCount = D3DX_DEFAULT; } } const DWORD staticTexture = 0; const D3DPOOL letD3dManageMemory = D3DPOOL_MANAGED; const DWORD useDefaultFiltering = D3DX_DEFAULT; const D3DCOLOR noColorKey = 0; PALETTEENTRY* noColorPalette = NULL; switch( imageInfo_source.ResourceType ) { case D3DRTYPE_TEXTURE: { const HRESULT result = D3DXCreateTextureFromFileEx( s_direct3dDevice, m_path_source, roundUpToAPowerOf2, roundUpToAPowerOf2, requestedMipMapCount, staticTexture, format, letD3dManageMemory, useDefaultFiltering, useDefaultFiltering, noColorKey, &imageInfo_source, noColorPalette, reinterpret_cast<IDirect3DTexture9**>( &s_texture ) ); if ( FAILED( result ) ) { wereThereErrors = true; OutputErrorMessage( "DirectX failed to get image info for the source image", m_path_source ); goto OnExit; } } break; case D3DRTYPE_CUBETEXTURE: { const HRESULT result = D3DXCreateCubeTextureFromFileEx( s_direct3dDevice, m_path_source, roundUpToAPowerOf2, requestedMipMapCount, staticTexture, format, letD3dManageMemory, useDefaultFiltering, useDefaultFiltering, noColorKey, &imageInfo_source, noColorPalette, reinterpret_cast<IDirect3DCubeTexture9**>( &s_texture ) ); if ( FAILED( result ) ) { wereThereErrors = true; OutputErrorMessage( "DirectX failed to get image info for the source image", m_path_source ); goto OnExit; } } break; case D3DRTYPE_VOLUMETEXTURE: { const HRESULT result = D3DXCreateVolumeTextureFromFileEx( s_direct3dDevice, m_path_source, roundUpToAPowerOf2, roundUpToAPowerOf2, imageInfo_source.Depth, requestedMipMapCount, staticTexture, format, letD3dManageMemory, useDefaultFiltering, useDefaultFiltering, noColorKey, &imageInfo_source, noColorPalette, reinterpret_cast<IDirect3DVolumeTexture9**>( &s_texture ) ); if ( FAILED( result ) ) { wereThereErrors = true; OutputErrorMessage( "DirectX failed to get image info for the source image", m_path_source ); goto OnExit; } } break; default: { wereThereErrors = true; std::stringstream errorMessage; errorMessage << "Unsupported texture resource type " << imageInfo_source.ResourceType; OutputErrorMessage( errorMessage.str().c_str(), m_path_source ); goto OnExit; } } } // Save the texture { const D3DXIMAGE_FILEFORMAT ddsFormat = D3DXIFF_DDS; PALETTEENTRY* noColorPalette = NULL; HRESULT result = D3DXSaveTextureToFile( m_path_target, ddsFormat, s_texture, noColorPalette ); if ( FAILED( result ) ) { wereThereErrors = true; OutputErrorMessage( "DirectX failed to save the texture", m_path_target ); goto OnExit; } } OnExit: if ( !ShutDown() ) { wereThereErrors = true; } return !wereThereErrors; }
BOOL CDxtexDoc::OnOpenDocument(LPCTSTR lpszPathName) { LPDIRECT3DDEVICE9 pd3ddev = PDxtexApp()->Pd3ddev(); D3DXIMAGE_INFO imageinfo; D3DXIMAGE_INFO imageinfo2; if( FAILED( D3DXGetImageInfoFromFile( lpszPathName, &imageinfo ) ) ) { AfxMessageBox(ID_ERROR_COULDNTLOADFILE); return FALSE; } switch( imageinfo.ResourceType ) { case D3DRTYPE_TEXTURE: if( FAILED( D3DXCreateTextureFromFileEx( pd3ddev, lpszPathName, imageinfo.Width, imageinfo.Height, imageinfo.MipLevels, 0, imageinfo.Format, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, &imageinfo2, NULL, (LPDIRECT3DTEXTURE9*)&m_ptexOrig ) ) ) { AfxMessageBox(ID_ERROR_COULDNTLOADFILE); return FALSE; } m_dwWidth = imageinfo2.Width; m_dwHeight = imageinfo2.Height; m_dwDepth = 0; m_numMips = imageinfo2.MipLevels; if( imageinfo.ImageFileFormat == D3DXIFF_BMP ) { // Look for "foo_a.bmp" for alpha channel CString strPath = lpszPathName; int i = strPath.ReverseFind('.'); HRESULT hr; strPath = strPath.Left(i) + "_a.bmp"; CFileStatus status; if (CFile::GetStatus(strPath, status)) { // Make sure there's an alpha channel to load alpha image into if (FAILED(EnsureAlpha(&m_ptexOrig))) return FALSE; LPDIRECT3DSURFACE9 psurf; hr = ((LPDIRECT3DTEXTURE9)m_ptexOrig)->GetSurfaceLevel(0, &psurf); if (FAILED(hr)) return FALSE; hr = LoadAlphaIntoSurface(strPath, psurf); ReleasePpo(&psurf); if (FAILED(hr)) return FALSE; } } break; case D3DRTYPE_VOLUMETEXTURE: if( FAILED( D3DXCreateVolumeTextureFromFileEx( pd3ddev, lpszPathName, imageinfo.Width, imageinfo.Height, imageinfo.Depth, imageinfo.MipLevels, 0, imageinfo.Format, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, &imageinfo2, NULL, (LPDIRECT3DVOLUMETEXTURE9*)&m_ptexOrig ) ) ) { AfxMessageBox(ID_ERROR_COULDNTLOADFILE); return FALSE; } m_dwWidth = imageinfo2.Width; m_dwHeight = imageinfo2.Height; m_dwDepth = imageinfo2.Depth; m_numMips = imageinfo2.MipLevels; break; case D3DRTYPE_CUBETEXTURE: if( FAILED( D3DXCreateCubeTextureFromFileEx( pd3ddev, lpszPathName, imageinfo.Width, imageinfo.MipLevels, 0, imageinfo.Format, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, &imageinfo2, NULL, (LPDIRECT3DCUBETEXTURE9*)&m_ptexOrig ) ) ) { AfxMessageBox(ID_ERROR_COULDNTLOADFILE); return FALSE; } m_dwWidth = imageinfo2.Width; m_dwHeight = imageinfo2.Height; m_dwDepth = 0; m_numMips = imageinfo2.MipLevels; m_dwCubeMapFlags = DDS_CUBEMAP_ALLFACES; break; default: AfxMessageBox(ID_ERROR_COULDNTLOADFILE); return FALSE; } return TRUE; }