Simplify passing of the device (suggested by @rootdarkarchon)

This commit is contained in:
Exter-N 2025-03-12 15:18:20 +01:00
parent e5620e17e0
commit e7f7077e96

View file

@ -50,11 +50,11 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
public Task SaveAs(CombinedTexture.TextureSaveType type, bool mipMaps, bool asTex, string input, string output) public Task SaveAs(CombinedTexture.TextureSaveType type, bool mipMaps, bool asTex, string input, string output)
=> Enqueue(new SaveAsAction(this, type, uiBuilder.Device, mipMaps, asTex, input, output)); => Enqueue(new SaveAsAction(this, type, mipMaps, asTex, input, output));
public Task SaveAs(CombinedTexture.TextureSaveType type, bool mipMaps, bool asTex, BaseImage image, string path, byte[]? rgba = null, public Task SaveAs(CombinedTexture.TextureSaveType type, bool mipMaps, bool asTex, BaseImage image, string path, byte[]? rgba = null,
int width = 0, int height = 0) int width = 0, int height = 0)
=> Enqueue(new SaveAsAction(this, type, uiBuilder.Device, mipMaps, asTex, image, path, rgba, width, height)); => Enqueue(new SaveAsAction(this, type, mipMaps, asTex, image, path, rgba, width, height));
private Task Enqueue(IAction action) private Task Enqueue(IAction action)
{ {
@ -159,30 +159,27 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
private readonly string _outputPath; private readonly string _outputPath;
private readonly ImageInputData _input; private readonly ImageInputData _input;
private readonly CombinedTexture.TextureSaveType _type; private readonly CombinedTexture.TextureSaveType _type;
private readonly Device? _device;
private readonly bool _mipMaps; private readonly bool _mipMaps;
private readonly bool _asTex; private readonly bool _asTex;
public SaveAsAction(TextureManager textures, CombinedTexture.TextureSaveType type, Device? device, bool mipMaps, bool asTex, public SaveAsAction(TextureManager textures, CombinedTexture.TextureSaveType type, bool mipMaps, bool asTex, string input,
string input, string output) string output)
{ {
_textures = textures; _textures = textures;
_input = new ImageInputData(input); _input = new ImageInputData(input);
_outputPath = output; _outputPath = output;
_type = type; _type = type;
_device = device;
_mipMaps = mipMaps; _mipMaps = mipMaps;
_asTex = asTex; _asTex = asTex;
} }
public SaveAsAction(TextureManager textures, CombinedTexture.TextureSaveType type, Device? device, bool mipMaps, bool asTex, public SaveAsAction(TextureManager textures, CombinedTexture.TextureSaveType type, bool mipMaps, bool asTex, BaseImage image,
BaseImage image, string path, byte[]? rgba = null, int width = 0, int height = 0) string path, byte[]? rgba = null, int width = 0, int height = 0)
{ {
_textures = textures; _textures = textures;
_input = new ImageInputData(image, rgba, width, height); _input = new ImageInputData(image, rgba, width, height);
_outputPath = path; _outputPath = path;
_type = type; _type = type;
_device = device;
_mipMaps = mipMaps; _mipMaps = mipMaps;
_asTex = asTex; _asTex = asTex;
} }
@ -207,8 +204,8 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
rgba, width, height), rgba, width, height),
CombinedTexture.TextureSaveType.AsIs when imageTypeBehaviour is TextureType.Dds => AddMipMaps(image.AsDds!, _mipMaps), CombinedTexture.TextureSaveType.AsIs when imageTypeBehaviour is TextureType.Dds => AddMipMaps(image.AsDds!, _mipMaps),
CombinedTexture.TextureSaveType.Bitmap => ConvertToRgbaDds(image, _mipMaps, cancel, rgba, width, height), CombinedTexture.TextureSaveType.Bitmap => ConvertToRgbaDds(image, _mipMaps, cancel, rgba, width, height),
CombinedTexture.TextureSaveType.BC3 => ConvertToCompressedDds(image, _mipMaps, false, _device, cancel, rgba, width, height), CombinedTexture.TextureSaveType.BC3 => _textures.ConvertToCompressedDds(image, _mipMaps, false, cancel, rgba, width, height),
CombinedTexture.TextureSaveType.BC7 => ConvertToCompressedDds(image, _mipMaps, true, _device, cancel, rgba, width, height), CombinedTexture.TextureSaveType.BC7 => _textures.ConvertToCompressedDds(image, _mipMaps, true, cancel, rgba, width, height),
_ => throw new Exception("Wrong save type."), _ => throw new Exception("Wrong save type."),
}; };
@ -326,8 +323,8 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
} }
/// <summary> Convert an existing image to a block compressed .dds. Does not create a deep copy of an existing dds of the correct format and just returns the existing one. </summary> /// <summary> Convert an existing image to a block compressed .dds. Does not create a deep copy of an existing dds of the correct format and just returns the existing one. </summary>
public static BaseImage ConvertToCompressedDds(BaseImage input, bool mipMaps, bool bc7, Device? device, CancellationToken cancel, public BaseImage ConvertToCompressedDds(BaseImage input, bool mipMaps, bool bc7, CancellationToken cancel, byte[]? rgba = null,
byte[]? rgba = null, int width = 0, int height = 0) int width = 0, int height = 0)
{ {
switch (input.Type.ReduceToBehaviour()) switch (input.Type.ReduceToBehaviour())
{ {
@ -337,12 +334,12 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
cancel.ThrowIfCancellationRequested(); cancel.ThrowIfCancellationRequested();
var dds = ConvertToDds(rgba, width, height).AsDds!; var dds = ConvertToDds(rgba, width, height).AsDds!;
cancel.ThrowIfCancellationRequested(); cancel.ThrowIfCancellationRequested();
return CreateCompressed(dds, mipMaps, bc7, device, cancel); return CreateCompressed(dds, mipMaps, bc7, cancel);
} }
case TextureType.Dds: case TextureType.Dds:
{ {
var scratch = input.AsDds!; var scratch = input.AsDds!;
return CreateCompressed(scratch, mipMaps, bc7, device, cancel); return CreateCompressed(scratch, mipMaps, bc7, cancel);
} }
default: return new BaseImage(); default: return new BaseImage();
} }
@ -390,7 +387,7 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
} }
/// <summary> Create a BC3 or BC7 block-compressed .dds from the input (optionally with mipmaps). Returns input (+ mipmaps) if it is already the correct format. </summary> /// <summary> Create a BC3 or BC7 block-compressed .dds from the input (optionally with mipmaps). Returns input (+ mipmaps) if it is already the correct format. </summary>
public static ScratchImage CreateCompressed(ScratchImage input, bool mipMaps, bool bc7, Device? device, CancellationToken cancel) public ScratchImage CreateCompressed(ScratchImage input, bool mipMaps, bool bc7, CancellationToken cancel)
{ {
var format = bc7 ? DXGIFormat.BC7UNorm : DXGIFormat.BC3UNorm; var format = bc7 ? DXGIFormat.BC7UNorm : DXGIFormat.BC3UNorm;
if (input.Meta.Format == format) if (input.Meta.Format == format)
@ -405,8 +402,9 @@ public sealed class TextureManager(IDataManager gameData, Logger logger, ITextur
input = AddMipMaps(input, mipMaps); input = AddMipMaps(input, mipMaps);
cancel.ThrowIfCancellationRequested(); cancel.ThrowIfCancellationRequested();
// See https://github.com/microsoft/DirectXTex/wiki/Compress#parameters for the format condition. // See https://github.com/microsoft/DirectXTex/wiki/Compress#parameters for the format condition.
if (device is not null && format is DXGIFormat.BC6HUF16 or DXGIFormat.BC6HSF16 or DXGIFormat.BC7UNorm or DXGIFormat.BC7UNormSRGB) if (format is DXGIFormat.BC6HUF16 or DXGIFormat.BC6HSF16 or DXGIFormat.BC7UNorm or DXGIFormat.BC7UNormSRGB)
{ {
var device = uiBuilder.Device;
var dxgiDevice = device.QueryInterface<DxgiDevice>(); var dxgiDevice = device.QueryInterface<DxgiDevice>();
using var deviceClone = new Device(dxgiDevice.Adapter, device.CreationFlags, device.FeatureLevel); using var deviceClone = new Device(dxgiDevice.Adapter, device.CreationFlags, device.FeatureLevel);