//=============================================================================================================================== void TextureManager::WritePNGToFile(LPCWSTR filename, ID3D11ShaderResourceView* textureSRV) { ID3D11Texture2D *textureInterface = 0; ID3D11Resource *textureResource; textureSRV->GetResource(&textureResource); textureResource->QueryInterface<ID3D11Texture2D>(&textureInterface); HRESULT result; result = SaveWICTextureToFile(mD3DSystem->GetDeviceContext(), textureInterface, GUID_ContainerFormatPng, filename); if (FAILED(result)) { ScratchImage image; result = CaptureTexture(mD3DSystem->GetDevice11(), mD3DSystem->GetDeviceContext(), textureInterface, image); if (SUCCEEDED(result)) { result = SaveToWICFile(image.GetImages(), image.GetImageCount(), WIC_FLAGS_NONE, GUID_ContainerFormatPng, filename); if (FAILED(result)) { ZShadeMessageCenter::MsgBoxError(NULL, "Failed to save PNG texture !!"); } } } SAFE_RELEASE(textureInterface); SAFE_RELEASE(textureResource); }
//========================================================================================================================= void TextureManager::WriteTGAToFile(LPCWSTR filename, ID3D11ShaderResourceView* textureSRV) { // https://directxtex.codeplex.com/wikipage?title=CaptureTexture ID3D11Texture2D *textureInterface = 0; ID3D11Resource *textureResource; textureSRV->GetResource(&textureResource); textureResource->QueryInterface<ID3D11Texture2D>(&textureInterface); HRESULT result; //ScratchImage image; ScratchImage image; result = CaptureTexture(mD3DSystem->GetDevice11(), mD3DSystem->GetDeviceContext(), textureInterface, image); if (SUCCEEDED(result)) { result = SaveToTGAFile(*image.GetImages(), filename); if (FAILED(result)) { ZShadeMessageCenter::MsgBoxError(NULL, "Failed to save TGA texture !!"); } } SAFE_RELEASE(textureInterface); SAFE_RELEASE(textureResource); }
void SaveTextureAsDDS(ID3D11Resource* texture, const wchar* filePath) { ID3D11DevicePtr device; texture->GetDevice(&device); ID3D11DeviceContextPtr context; device->GetImmediateContext(&context); ScratchImage scratchImage; DXCall(CaptureTexture(device, context, texture, scratchImage)); DXCall(SaveToDDSFile(scratchImage.GetImages(), scratchImage.GetImageCount(), scratchImage.GetMetadata(), DDS_FLAGS_FORCE_DX10_EXT, filePath)); }
void SaveTextureAsPNG(ID3D11Resource* texture, const wchar* filePath) { ID3D11DevicePtr device; texture->GetDevice(&device); ID3D11DeviceContextPtr context; device->GetImmediateContext(&context); ScratchImage scratchImage; DXCall(CaptureTexture(device, context, texture, scratchImage)); DXCall(SaveToWICFile(scratchImage.GetImages(), scratchImage.GetImageCount(), WIC_FLAGS_NONE, GetWICCodec(WIC_CODEC_PNG), filePath)); }