static bool ConvertOptimizedTextures( DXGI_FORMAT format, size_t mipMapLevels, ff::Vector<ff::ComPtr<ff::IGraphTexture>> &textures, ff::Vector<ff::ComPtr<ff::IGraphTexture>> &finalTextures, ff::Vector<std::shared_ptr<DirectX::ScratchImage>> &alphaTextures) { for (size_t i = 0; i < textures.Size(); i++) { ff::IGraphTexture *texture = textures[i]; ff::IGraphDevice *device = texture->GetDevice(); ff::ComPtr<ff::IGraphTexture> newTexture; assertRetVal(texture->Convert(format, mipMapLevels, &newTexture), false); finalTextures.Push(newTexture); DirectX::ScratchImage scratch; assertHrRetVal(DirectX::CaptureTexture(device->Get3d(), device->GetContext(), texture->GetTexture(), scratch), false); std::shared_ptr<DirectX::ScratchImage> alphaScratch = std::make_shared<DirectX::ScratchImage>(); assertHrRetVal(DirectX::Convert(scratch.GetImages(), 1, scratch.GetMetadata(), DXGI_FORMAT_A8_UNORM, DirectX::TEX_FILTER_DEFAULT, 0, *alphaScratch), false); alphaTextures.Push(alphaScratch); } return true; }
void SaveTextureToDDSFile(ID3D11Resource* tex, LPCWSTR filename) { // from http://directxtex.codeplex.com/wikipage?title=CaptureTexture DirectX::ScratchImage image; HRESULT hr; hr = DirectX::CaptureTexture(d3dDevice, immediateContext, tex, image); if (FAILED(hr)) { return; } hr = DirectX::SaveToDDSFile(image.GetImages(), image.GetImageCount(), image.GetMetadata(), DDS_FLAGS_NONE, filename); if (FAILED(hr)) { return; } }
void convertTexture(const std::wstring& source, const std::wstring& dest, TextureFormat sourceFormat, TextureFormat destFormat, bool alpha){ DirectX::ScratchImage srcImg; DirectX::TexMetadata srcMetadata; switch (sourceFormat) { case TextureFormat::Dds: DirectX::LoadFromDDSFile(source.c_str(), 0, &srcMetadata, srcImg); break; case TextureFormat::Tga: DirectX::LoadFromTGAFile(source.c_str(), &srcMetadata, srcImg); break; default: DirectX::LoadFromWICFile(source.c_str(), 0, &srcMetadata, srcImg); break; } switch (destFormat) { case TextureFormat::Jpg: DirectX::SaveToWICFile(*srcImg.GetImage(0, 0, 0), 0, DirectX::GetWICCodec(DirectX::WICCodecs::WIC_CODEC_JPEG), dest.c_str()); break; case TextureFormat::Png: DirectX::SaveToWICFile(*srcImg.GetImage(0, 0, 0), 0, DirectX::GetWICCodec(DirectX::WICCodecs::WIC_CODEC_PNG), dest.c_str()); break; case TextureFormat::Dds: { DirectX::ScratchImage imgWithMipMaps; auto hr = DirectX::GenerateMipMaps(*srcImg.GetImage(0, 0, 0), DirectX::TEX_FILTER_FLAGS::TEX_FILTER_CUBIC, 0, imgWithMipMaps); DirectX::ScratchImage imgInBC2; //hr = DirectX::Convert(imgWithMipMaps.GetImages(), imgWithMipMaps.GetImageCount(), srcMetadata, DXGI_FORMAT_BC2_UNORM, DirectX::TEX_FILTER_FLAGS::TEX_FILTER_CUBIC, 0.5f, imgInBC2); hr = DirectX::Compress(imgWithMipMaps.GetImages(), imgWithMipMaps.GetImageCount(), imgWithMipMaps.GetMetadata(), alpha ? DXGI_FORMAT_BC3_UNORM : DXGI_FORMAT_BC1_UNORM, DirectX::TEX_COMPRESS_FLAGS::TEX_COMPRESS_DEFAULT | DirectX::TEX_COMPRESS_PARALLEL, 0.5f, imgInBC2); hr = DirectX::SaveToDDSFile(imgInBC2.GetImages(), imgInBC2.GetImageCount(), imgInBC2.GetMetadata(), 0, dest.c_str()); } break; default: break; } }
// ------------------------------------------------------------------------------------------------- bool TextureFactory::LoadResource(Resource* resource, const WCHAR * filename) { if(!FileUtils::Exists(filename)) return false; HRESULT hr = S_OK; DirectX::TexMetadata metadata; DirectX::ScratchImage sourceScratch; hr = DXUtil::LoadTexture(filename,&metadata,sourceScratch); if (Logger::IsFailureLog(hr, L"LoadTexture")) { return false; } ID3D11Texture2D* dxtex = NULL; std::wstring ext = FileUtils::GetExtensionLower(filename); // generate full mip chains for non dds file. if(ext != L".dds") { const DirectX::Image* srcImage = sourceScratch.GetImage(0, 0, 0); DirectX::ScratchImage mipScratch; hr = DirectX::GenerateMipMaps(*srcImage, DirectX::TEX_FILTER_LINEAR, 0, mipScratch, false); if (Logger::IsFailureLog(hr, L"DirectX::GenerateMipMaps")) return false; hr = DirectX::CreateTexture(m_device, mipScratch.GetImages(), mipScratch.GetImageCount(), mipScratch.GetMetadata(), (ID3D11Resource**)&dxtex); } else { hr = DirectX::CreateTexture(m_device, sourceScratch.GetImages(), sourceScratch.GetImageCount(), metadata,(ID3D11Resource**)&dxtex); } if (Logger::IsFailureLog(hr, L"DirectX::CreateTexture")) return false; ID3D11ShaderResourceView* texview = CreateTextureView(m_device, dxtex); if(!dxtex) return false; Texture* tex = (Texture*)resource; tex->Set(dxtex,texview); return true; }