Use DirectX 11 for compression when possible, otherwise default to software. Make sure we don't threadbomb users systems.

This commit is contained in:
Timothy Baldridge 2021-07-04 16:25:06 -06:00
parent 24d10305a4
commit d66890d470
3 changed files with 73 additions and 1 deletions

View File

@ -14,6 +14,7 @@ namespace Wabbajack.ImageHashing
{
public class DDSImage : IDisposable
{
private static Lazy<DX11Device> DX11Device = new(() => new DX11Device());
private DDSImage(ScratchImage img, TexMetadata metadata, Extension ext)
{
_image = img;
@ -101,7 +102,7 @@ namespace Wabbajack.ImageHashing
if (CompressedTypes.Contains(newFormat))
{
var old = resized;
resized = resized.Compress(newFormat, TEX_COMPRESS_FLAGS.BC7_QUICK, 0.5f);
resized = DX11Device.Value.Compress(resized, newFormat, TEX_COMPRESS_FLAGS.BC7_QUICK, 0.5f);
old.Dispose();
}

View File

@ -0,0 +1,69 @@
using System;
using DirectXTexNet;
using Silk.NET.Core.Native;
using Silk.NET.Direct3D11;
namespace Wabbajack.ImageHashing
{
public unsafe class DX11Device
{
private readonly ID3D11Device* _device;
public DX11Device()
{
unsafe
{
var dxgi = Silk.NET.DXGI.DXGI.GetApi();
var dx11 = Silk.NET.Direct3D11.D3D11.GetApi();
D3DFeatureLevel[] levels =
{
//D3DFeatureLevel.D3DFeatureLevel100, D3DFeatureLevel.D3DFeatureLevel101,
D3DFeatureLevel.D3DFeatureLevel110
};
uint createDeviceFlags = 0;
var adapterIdx = 0;
D3DFeatureLevel fl;
ID3D11Device* device;
fixed (D3DFeatureLevel* lvls = levels)
{
var hr = dx11.CreateDevice(null, D3DDriverType.D3DDriverTypeHardware, IntPtr.Zero,
createDeviceFlags, lvls,
(uint)levels.Length,
Silk.NET.Direct3D11.D3D11.SdkVersion, &device, &fl, null);
if (FAILED(hr))
{
_device = null;
return;
}
_device = device;
}
}
}
public ScratchImage Compress(ScratchImage input, DXGI_FORMAT format, TEX_COMPRESS_FLAGS compress,
float threshold)
{
lock (this)
{
if (_device != null)
{
return input.Compress((IntPtr)_device, format, compress, threshold);
}
else
{
return input.Compress(format, compress, threshold);
}
}
}
private static bool FAILED(int x)
{
return x != 0;
}
}
}

View File

@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="DirectXTexNet" Version="1.0.2" />
<PackageReference Include="Shipwreck.Phash" Version="0.5.0" />
<PackageReference Include="Silk.NET.Direct3D11" Version="2.6.0" />
<PackageReference Include="Silk.NET.DXGI" Version="2.6.0" />
</ItemGroup>
<ItemGroup>