GPU内部シェーダーをビルド時DXBC化
このコミットが含まれているのは:
9ファイル変更
+868
-447
検索条件に一致するファイルはありません
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LuaScript.Engine.Shader;
|
||||
|
||||
namespace LuaScript.Engine.Processing
|
||||
{
|
||||
[GpuPixelOperationShader(nameof(RunConvolve), "Shaders/convolve.hlsl", "convolve")]
|
||||
internal sealed partial class GpuConvolveOperation(PixelShaderRunner runner) : GpuPixelOperation(runner)
|
||||
{
|
||||
private const int ConvolveWorkThreshold = 262144;
|
||||
private const int MaxConvolveSize = 31;
|
||||
private const int MaxConvolveValidationCount = 64;
|
||||
|
||||
private readonly PixelShaderInput[] _resources = new PixelShaderInput[1];
|
||||
private readonly float[] _constants = new float[1024];
|
||||
private readonly List<ConvolveValidation> _validations = [];
|
||||
|
||||
public bool TryConvolve(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
if (!IsTextureSupported(target, width, height) || !IsSupported(kernel, size, divisor, offset))
|
||||
return false;
|
||||
|
||||
long work = (long)width * height * size * size;
|
||||
if (work < ConvolveWorkThreshold || !EnsureValidated(target, width, height, kernel, size, divisor, offset))
|
||||
return false;
|
||||
|
||||
SetConstants(width, height, kernel, size, divisor, offset);
|
||||
_resources[0] = new PixelShaderInput(target, width, height);
|
||||
return RunConvolve(_resources.AsSpan(0, 1), _constants.AsSpan(0, size * size + 8), target, width, height);
|
||||
}
|
||||
|
||||
private bool EnsureValidated(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
for (int i = 0; i < _validations.Count; i++)
|
||||
{
|
||||
if (_validations[i].Matches(kernel, size, divisor, offset))
|
||||
return _validations[i].Valid;
|
||||
}
|
||||
|
||||
if (_validations.Count >= MaxConvolveValidationCount)
|
||||
_validations.Clear();
|
||||
|
||||
bool valid = Validate(target, width, height, kernel, size, divisor, offset);
|
||||
_validations.Add(new ConvolveValidation(kernel, size, divisor, offset, valid));
|
||||
return valid;
|
||||
}
|
||||
|
||||
private bool Validate(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
int probeWidth = Math.Min(width, 8);
|
||||
int probeHeight = Math.Min(height, 7);
|
||||
var probe = CopyProbe(target, width, height, probeWidth, probeHeight);
|
||||
var cpu = (byte[])probe.Clone();
|
||||
var gpu = (byte[])probe.Clone();
|
||||
double[]? scratch = null;
|
||||
PixelBufferSoftwareProcessor.Convolve(cpu, probeWidth, probeHeight, kernel, size, divisor, offset, ref scratch);
|
||||
SetConstants(probeWidth, probeHeight, kernel, size, divisor, offset);
|
||||
_resources[0] = new PixelShaderInput(gpu, probeWidth, probeHeight);
|
||||
return RunConvolve(_resources.AsSpan(0, 1), _constants.AsSpan(0, size * size + 8), gpu, probeWidth, probeHeight) && Equal(cpu, gpu);
|
||||
}
|
||||
|
||||
private void SetConstants(int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
_constants[0] = width;
|
||||
_constants[1] = height;
|
||||
_constants[2] = (float)divisor;
|
||||
_constants[3] = (float)offset;
|
||||
_constants[4] = size;
|
||||
_constants[5] = size / 2;
|
||||
_constants[6] = 0f;
|
||||
_constants[7] = 0f;
|
||||
int taps = size * size;
|
||||
for (int i = 0; i < taps; i++)
|
||||
_constants[i + 8] = (float)kernel[i];
|
||||
}
|
||||
|
||||
private static bool IsSupported(double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
if (size < 1 || (size & 1) == 0 || size > MaxConvolveSize || divisor == 0d || !IsFloatCompatible(divisor) || !IsFloatCompatible(offset))
|
||||
return false;
|
||||
int taps = size * size;
|
||||
if (kernel.Length < taps || taps + 8 > 1024)
|
||||
return false;
|
||||
for (int i = 0; i < taps; i++)
|
||||
{
|
||||
if (!IsFloatCompatible(kernel[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsFloatCompatible(double value)
|
||||
{
|
||||
return double.IsFinite(value) && float.IsFinite((float)value);
|
||||
}
|
||||
|
||||
private partial bool RunConvolve(ReadOnlySpan<PixelShaderInput> resources, ReadOnlySpan<float> constants, byte[] target, int width, int height);
|
||||
|
||||
private sealed class ConvolveValidation
|
||||
{
|
||||
private readonly double[] _kernel;
|
||||
private readonly int _size;
|
||||
private readonly double _divisor;
|
||||
private readonly double _offset;
|
||||
|
||||
public ConvolveValidation(double[] kernel, int size, double divisor, double offset, bool valid)
|
||||
{
|
||||
int taps = size * size;
|
||||
_kernel = new double[taps];
|
||||
Array.Copy(kernel, _kernel, taps);
|
||||
_size = size;
|
||||
_divisor = divisor;
|
||||
_offset = offset;
|
||||
Valid = valid;
|
||||
}
|
||||
|
||||
public bool Valid { get; }
|
||||
|
||||
public bool Matches(double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
if (_size != size || BitConverter.DoubleToInt64Bits(_divisor) != BitConverter.DoubleToInt64Bits(divisor) ||
|
||||
BitConverter.DoubleToInt64Bits(_offset) != BitConverter.DoubleToInt64Bits(offset))
|
||||
return false;
|
||||
int taps = size * size;
|
||||
for (int i = 0; i < taps; i++)
|
||||
{
|
||||
if (BitConverter.DoubleToInt64Bits(_kernel[i]) != BitConverter.DoubleToInt64Bits(kernel[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using LuaScript.Engine.Shader;
|
||||
|
||||
namespace LuaScript.Engine.Processing
|
||||
{
|
||||
[GpuPixelOperationShader(nameof(RunFillFull), "Shaders/fill.hlsl", "fill_full")]
|
||||
[GpuPixelOperationShader(nameof(RunFillPartial), "Shaders/fill.hlsl", "fill_partial")]
|
||||
internal sealed partial class GpuFillOperation(PixelShaderRunner runner) : GpuPixelOperation(runner)
|
||||
{
|
||||
private const int FillFullThreshold = 1048576;
|
||||
private const int FillPartialThreshold = 1048576;
|
||||
|
||||
private readonly PixelShaderInput[] _resources = new PixelShaderInput[1];
|
||||
private readonly float[] _constants = new float[8];
|
||||
|
||||
private bool? _fullValidated;
|
||||
private bool? _partialValidated;
|
||||
|
||||
public bool TryFill(byte[] target, int width, int height, double r, double g, double b, double a, int x, int y, int fillWidth, int fillHeight)
|
||||
{
|
||||
if (!IsTextureSupported(target, width, height) || !AreFinite(r, g, b, a))
|
||||
return false;
|
||||
|
||||
long pixels = (long)width * height;
|
||||
bool full = x == 0 && y == 0 && fillWidth == width && fillHeight == height;
|
||||
if (full)
|
||||
{
|
||||
if (pixels < FillFullThreshold || !EnsureFullValidated())
|
||||
return false;
|
||||
SetConstants(r, g, b, a);
|
||||
return RunFillFull(_constants.AsSpan(0, 4), target, width, height);
|
||||
}
|
||||
|
||||
long area = (long)fillWidth * fillHeight;
|
||||
if (pixels < FillPartialThreshold || area * 4L < pixels * 3L || !EnsurePartialValidated())
|
||||
return false;
|
||||
|
||||
SetConstants(r, g, b, a);
|
||||
_constants[4] = x;
|
||||
_constants[5] = y;
|
||||
_constants[6] = x + fillWidth;
|
||||
_constants[7] = y + fillHeight;
|
||||
_resources[0] = new PixelShaderInput(target, width, height);
|
||||
return RunFillPartial(_resources.AsSpan(0, 1), _constants.AsSpan(0, 8), target, width, height);
|
||||
}
|
||||
|
||||
private bool EnsureFullValidated()
|
||||
{
|
||||
if (_fullValidated.HasValue)
|
||||
return _fullValidated.Value;
|
||||
|
||||
const int width = 7;
|
||||
const int height = 5;
|
||||
var source = CreateProbe(width, height);
|
||||
var cpu = (byte[])source.Clone();
|
||||
var gpu = (byte[])source.Clone();
|
||||
PixelBufferSoftwareProcessor.Fill(cpu, width, height, 23d, 197d, 89d, 173d, 0, 0, width, height);
|
||||
SetConstants(23d, 197d, 89d, 173d);
|
||||
_fullValidated = RunFillFull(_constants.AsSpan(0, 4), gpu, width, height) && Equal(cpu, gpu);
|
||||
return _fullValidated.Value;
|
||||
}
|
||||
|
||||
private bool EnsurePartialValidated()
|
||||
{
|
||||
if (_partialValidated.HasValue)
|
||||
return _partialValidated.Value;
|
||||
|
||||
const int width = 7;
|
||||
const int height = 5;
|
||||
var source = CreateProbe(width, height);
|
||||
var cpu = (byte[])source.Clone();
|
||||
var gpu = (byte[])source.Clone();
|
||||
PixelBufferSoftwareProcessor.Fill(cpu, width, height, 23d, 197d, 89d, 173d, 1, 2, 4, 2);
|
||||
SetConstants(23d, 197d, 89d, 173d);
|
||||
_constants[4] = 1f;
|
||||
_constants[5] = 2f;
|
||||
_constants[6] = 5f;
|
||||
_constants[7] = 4f;
|
||||
_resources[0] = new PixelShaderInput(gpu, width, height);
|
||||
_partialValidated = RunFillPartial(_resources.AsSpan(0, 1), _constants.AsSpan(0, 8), gpu, width, height) && Equal(cpu, gpu);
|
||||
return _partialValidated.Value;
|
||||
}
|
||||
|
||||
private void SetConstants(double r, double g, double b, double a)
|
||||
{
|
||||
double aK = Math.Clamp(a, 0d, 255d) / 255d;
|
||||
byte pb = (byte)Math.Clamp(b * aK, 0d, 255d);
|
||||
byte pg = (byte)Math.Clamp(g * aK, 0d, 255d);
|
||||
byte pr = (byte)Math.Clamp(r * aK, 0d, 255d);
|
||||
byte pa = (byte)Math.Clamp(a, 0d, 255d);
|
||||
_constants[0] = pr / 255f;
|
||||
_constants[1] = pg / 255f;
|
||||
_constants[2] = pb / 255f;
|
||||
_constants[3] = pa / 255f;
|
||||
}
|
||||
|
||||
private static bool AreFinite(double r, double g, double b, double a)
|
||||
{
|
||||
return double.IsFinite(r) && double.IsFinite(g) && double.IsFinite(b) && double.IsFinite(a);
|
||||
}
|
||||
|
||||
private static byte[] CreateProbe(int width, int height)
|
||||
{
|
||||
var data = new byte[width * height * 4];
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
int index = (y * width + x) * 4;
|
||||
byte a = (byte)((x * 31 + y * 47) & 255);
|
||||
data[index] = a == 0 ? (byte)0 : (byte)((x * 17 + y * 29) % (a + 1));
|
||||
data[index + 1] = a == 0 ? (byte)0 : (byte)((x * 43 + y * 11) % (a + 1));
|
||||
data[index + 2] = a == 0 ? (byte)0 : (byte)((x * 7 + y * 53) % (a + 1));
|
||||
data[index + 3] = a;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private partial bool RunFillFull(ReadOnlySpan<float> constants, byte[] target, int width, int height);
|
||||
|
||||
private partial bool RunFillPartial(ReadOnlySpan<PixelShaderInput> resources, ReadOnlySpan<float> constants, byte[] target, int width, int height);
|
||||
}
|
||||
}
|
||||
@@ -1,348 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LuaScript.Engine.Shader;
|
||||
|
||||
namespace LuaScript.Engine.Processing
|
||||
{
|
||||
internal sealed class GpuPixelBufferProcessor(PixelShaderRunner runner) : IPixelBufferProcessor
|
||||
{
|
||||
private const int MaxTextureSize = 16384;
|
||||
private const int FillFullThreshold = 1048576;
|
||||
private const int FillPartialThreshold = 1048576;
|
||||
private const int ResizeThreshold = 262144;
|
||||
private const int ConvolveWorkThreshold = 262144;
|
||||
private const int MaxConvolveSize = 31;
|
||||
private const int MaxConvolveValidationCount = 64;
|
||||
|
||||
private readonly object _gate = new();
|
||||
private readonly PixelShaderInput[] _resources = new PixelShaderInput[1];
|
||||
private readonly float[] _constants = new float[1024];
|
||||
private readonly List<ConvolveValidation> _convolveValidations = [];
|
||||
|
||||
private byte[] _resizeTarget = [];
|
||||
private bool? _fillFullValidated;
|
||||
private bool? _fillPartialValidated;
|
||||
private bool? _resizeNearestValidated;
|
||||
private bool? _resizeLinearValidated;
|
||||
private readonly GpuFillOperation _fill = new(runner);
|
||||
private readonly GpuConvolveOperation _convolve = new(runner);
|
||||
private readonly GpuResizeOperation _resize = new(runner);
|
||||
|
||||
public bool TryFill(byte[] target, int width, int height, double r, double g, double b, double a, int x, int y, int fillWidth, int fillHeight)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (!IsTextureSupported(target, width, height) || !AreFinite(r, g, b, a))
|
||||
return false;
|
||||
|
||||
long pixels = (long)width * height;
|
||||
bool full = x == 0 && y == 0 && fillWidth == width && fillHeight == height;
|
||||
if (full)
|
||||
{
|
||||
if (pixels < FillFullThreshold || !EnsureFillFullValidated())
|
||||
return false;
|
||||
SetFillConstants(r, g, b, a);
|
||||
return Run(GpuPixelOperationShaderRegistry.Fill, "fill_full", ReadOnlySpan<PixelShaderInput>.Empty, _constants.AsSpan(0, 4), target, width, height);
|
||||
}
|
||||
|
||||
long area = (long)fillWidth * fillHeight;
|
||||
if (pixels < FillPartialThreshold || area * 4L < pixels * 3L || !EnsureFillPartialValidated())
|
||||
return false;
|
||||
|
||||
SetFillConstants(r, g, b, a);
|
||||
_constants[4] = x;
|
||||
_constants[5] = y;
|
||||
_constants[6] = x + fillWidth;
|
||||
_constants[7] = y + fillHeight;
|
||||
_resources[0] = new PixelShaderInput(target, width, height);
|
||||
return Run(GpuPixelOperationShaderRegistry.Fill, "fill_partial", _resources.AsSpan(0, 1), _constants.AsSpan(0, 8), target, width, height);
|
||||
}
|
||||
return _fill.TryFill(target, width, height, r, g, b, a, x, y, fillWidth, fillHeight);
|
||||
}
|
||||
|
||||
public bool TryConvolve(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (!IsTextureSupported(target, width, height) || !IsConvolveSupported(kernel, size, divisor, offset))
|
||||
return false;
|
||||
|
||||
long work = (long)width * height * size * size;
|
||||
if (work < ConvolveWorkThreshold || !EnsureConvolveValidated(target, width, height, kernel, size, divisor, offset))
|
||||
return false;
|
||||
|
||||
SetConvolveConstants(width, height, kernel, size, divisor, offset);
|
||||
_resources[0] = new PixelShaderInput(target, width, height);
|
||||
return Run(GpuPixelOperationShaderRegistry.Convolve, "convolve", _resources.AsSpan(0, 1), _constants.AsSpan(0, size * size + 8), target, width, height);
|
||||
}
|
||||
return _convolve.TryConvolve(target, width, height, kernel, size, divisor, offset);
|
||||
}
|
||||
|
||||
public bool TryResize(byte[] source, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, bool linear, out byte[]? target)
|
||||
{
|
||||
target = null;
|
||||
lock (_gate)
|
||||
{
|
||||
if (!IsTextureSupported(source, sourceWidth, sourceHeight) || !IsTextureSizeSupported(targetWidth, targetHeight))
|
||||
return false;
|
||||
|
||||
long pixels = (long)targetWidth * targetHeight;
|
||||
if (pixels < ResizeThreshold || !(linear ? EnsureResizeLinearValidated(source, sourceWidth, sourceHeight) : EnsureResizeNearestValidated(source, sourceWidth, sourceHeight)))
|
||||
return false;
|
||||
|
||||
return RunResizeShader(source, sourceWidth, sourceHeight, targetWidth, targetHeight, linear, out target);
|
||||
}
|
||||
}
|
||||
|
||||
private bool EnsureFillFullValidated()
|
||||
{
|
||||
if (_fillFullValidated.HasValue)
|
||||
return _fillFullValidated.Value;
|
||||
|
||||
const int width = 7;
|
||||
const int height = 5;
|
||||
var source = CreateProbe(width, height);
|
||||
var cpu = (byte[])source.Clone();
|
||||
var gpu = (byte[])source.Clone();
|
||||
PixelBufferSoftwareProcessor.Fill(cpu, width, height, 23d, 197d, 89d, 173d, 0, 0, width, height);
|
||||
SetFillConstants(23d, 197d, 89d, 173d);
|
||||
_fillFullValidated = Run(GpuPixelOperationShaderRegistry.Fill, "fill_full", ReadOnlySpan<PixelShaderInput>.Empty, _constants.AsSpan(0, 4), gpu, width, height) && Equal(cpu, gpu);
|
||||
return _fillFullValidated.Value;
|
||||
}
|
||||
|
||||
private bool EnsureFillPartialValidated()
|
||||
{
|
||||
if (_fillPartialValidated.HasValue)
|
||||
return _fillPartialValidated.Value;
|
||||
|
||||
const int width = 7;
|
||||
const int height = 5;
|
||||
var source = CreateProbe(width, height);
|
||||
var cpu = (byte[])source.Clone();
|
||||
var gpu = (byte[])source.Clone();
|
||||
PixelBufferSoftwareProcessor.Fill(cpu, width, height, 23d, 197d, 89d, 173d, 1, 2, 4, 2);
|
||||
SetFillConstants(23d, 197d, 89d, 173d);
|
||||
_constants[4] = 1f;
|
||||
_constants[5] = 2f;
|
||||
_constants[6] = 5f;
|
||||
_constants[7] = 4f;
|
||||
_resources[0] = new PixelShaderInput(gpu, width, height);
|
||||
_fillPartialValidated = Run(GpuPixelOperationShaderRegistry.Fill, "fill_partial", _resources.AsSpan(0, 1), _constants.AsSpan(0, 8), gpu, width, height) && Equal(cpu, gpu);
|
||||
return _fillPartialValidated.Value;
|
||||
}
|
||||
|
||||
private bool EnsureResizeNearestValidated(byte[] source, int sourceWidth, int sourceHeight)
|
||||
{
|
||||
if (_resizeNearestValidated.HasValue)
|
||||
return _resizeNearestValidated.Value;
|
||||
_resizeNearestValidated = ValidateResize(source, sourceWidth, sourceHeight, false);
|
||||
return _resizeNearestValidated.Value;
|
||||
}
|
||||
|
||||
private bool EnsureResizeLinearValidated(byte[] source, int sourceWidth, int sourceHeight)
|
||||
{
|
||||
if (_resizeLinearValidated.HasValue)
|
||||
return _resizeLinearValidated.Value;
|
||||
_resizeLinearValidated = ValidateResize(source, sourceWidth, sourceHeight, true);
|
||||
return _resizeLinearValidated.Value;
|
||||
}
|
||||
|
||||
private bool ValidateResize(byte[] source, int sourceWidth, int sourceHeight, bool linear)
|
||||
{
|
||||
int probeWidth = Math.Min(sourceWidth, 8);
|
||||
int probeHeight = Math.Min(sourceHeight, 7);
|
||||
int targetWidth = Math.Max(1, probeWidth * 2 - 3);
|
||||
int targetHeight = Math.Max(1, probeHeight * 2 - 5);
|
||||
var probe = CopyProbe(source, sourceWidth, sourceHeight, probeWidth, probeHeight);
|
||||
byte[]? sourceScratch = null;
|
||||
byte[]? targetScratch = null;
|
||||
var cpu = PixelBufferSoftwareProcessor.Resize(probe, probeWidth, probeHeight, targetWidth, targetHeight, linear, ref sourceScratch, ref targetScratch);
|
||||
return RunResizeShader(probe, probeWidth, probeHeight, targetWidth, targetHeight, linear, out var gpu) && gpu is not null && Equal(cpu, gpu);
|
||||
}
|
||||
|
||||
private bool EnsureConvolveValidated(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
for (int i = 0; i < _convolveValidations.Count; i++)
|
||||
{
|
||||
if (_convolveValidations[i].Matches(kernel, size, divisor, offset))
|
||||
return _convolveValidations[i].Valid;
|
||||
}
|
||||
|
||||
if (_convolveValidations.Count >= MaxConvolveValidationCount)
|
||||
_convolveValidations.Clear();
|
||||
|
||||
bool valid = ValidateConvolve(target, width, height, kernel, size, divisor, offset);
|
||||
_convolveValidations.Add(new ConvolveValidation(kernel, size, divisor, offset, valid));
|
||||
return valid;
|
||||
}
|
||||
|
||||
private bool ValidateConvolve(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
int probeWidth = Math.Min(width, 8);
|
||||
int probeHeight = Math.Min(height, 7);
|
||||
var probe = CopyProbe(target, width, height, probeWidth, probeHeight);
|
||||
var cpu = (byte[])probe.Clone();
|
||||
var gpu = (byte[])probe.Clone();
|
||||
double[]? scratch = null;
|
||||
PixelBufferSoftwareProcessor.Convolve(cpu, probeWidth, probeHeight, kernel, size, divisor, offset, ref scratch);
|
||||
SetConvolveConstants(probeWidth, probeHeight, kernel, size, divisor, offset);
|
||||
_resources[0] = new PixelShaderInput(gpu, probeWidth, probeHeight);
|
||||
return Run(GpuPixelOperationShaderRegistry.Convolve, "convolve", _resources.AsSpan(0, 1), _constants.AsSpan(0, size * size + 8), gpu, probeWidth, probeHeight) && Equal(cpu, gpu);
|
||||
}
|
||||
|
||||
private bool RunResizeShader(byte[] source, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, bool linear, out byte[]? target)
|
||||
{
|
||||
int count = checked(targetWidth * targetHeight * 4);
|
||||
if (_resizeTarget.Length != count)
|
||||
_resizeTarget = new byte[count];
|
||||
|
||||
_constants[0] = sourceWidth;
|
||||
_constants[1] = sourceHeight;
|
||||
_constants[2] = targetWidth;
|
||||
_constants[3] = targetHeight;
|
||||
_resources[0] = new PixelShaderInput(source, sourceWidth, sourceHeight);
|
||||
target = _resizeTarget;
|
||||
string entry = linear ? "resize_linear" : "resize_nearest";
|
||||
return Run(GpuPixelOperationShaderRegistry.Resize, entry, _resources.AsSpan(0, 1), _constants.AsSpan(0, 4), _resizeTarget, targetWidth, targetHeight);
|
||||
}
|
||||
|
||||
private bool Run(string hlsl, string entryPoint, ReadOnlySpan<PixelShaderInput> resources, ReadOnlySpan<float> constants, byte[] target, int width, int height)
|
||||
{
|
||||
return runner.TryRunHardware(
|
||||
hlsl,
|
||||
entryPoint,
|
||||
resources,
|
||||
constants,
|
||||
PixelShaderBlend.Copy,
|
||||
PixelShaderSampler.None,
|
||||
target,
|
||||
width,
|
||||
height,
|
||||
out _) == PixelShaderRunStatus.Success;
|
||||
}
|
||||
|
||||
private void SetFillConstants(double r, double g, double b, double a)
|
||||
{
|
||||
double aK = Math.Clamp(a, 0d, 255d) / 255d;
|
||||
byte pb = (byte)Math.Clamp(b * aK, 0d, 255d);
|
||||
byte pg = (byte)Math.Clamp(g * aK, 0d, 255d);
|
||||
byte pr = (byte)Math.Clamp(r * aK, 0d, 255d);
|
||||
byte pa = (byte)Math.Clamp(a, 0d, 255d);
|
||||
_constants[0] = pr / 255f;
|
||||
_constants[1] = pg / 255f;
|
||||
_constants[2] = pb / 255f;
|
||||
_constants[3] = pa / 255f;
|
||||
}
|
||||
|
||||
private void SetConvolveConstants(int width, int height, double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
_constants[0] = width;
|
||||
_constants[1] = height;
|
||||
_constants[2] = (float)divisor;
|
||||
_constants[3] = (float)offset;
|
||||
_constants[4] = size;
|
||||
_constants[5] = size / 2;
|
||||
_constants[6] = 0f;
|
||||
_constants[7] = 0f;
|
||||
int taps = size * size;
|
||||
for (int i = 0; i < taps; i++)
|
||||
_constants[i + 8] = (float)kernel[i];
|
||||
}
|
||||
|
||||
private static bool IsTextureSupported(byte[] buffer, int width, int height)
|
||||
{
|
||||
return IsTextureSizeSupported(width, height) && (long)buffer.Length >= (long)width * height * 4;
|
||||
}
|
||||
|
||||
private static bool IsTextureSizeSupported(int width, int height)
|
||||
{
|
||||
return width > 0 && height > 0 && width <= MaxTextureSize && height <= MaxTextureSize;
|
||||
}
|
||||
|
||||
private static bool IsConvolveSupported(double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
if (size < 1 || (size & 1) == 0 || size > MaxConvolveSize || divisor == 0d || !IsFloatCompatible(divisor) || !IsFloatCompatible(offset))
|
||||
return false;
|
||||
int taps = size * size;
|
||||
if (kernel.Length < taps || taps + 8 > 1024)
|
||||
return false;
|
||||
for (int i = 0; i < taps; i++)
|
||||
{
|
||||
if (!IsFloatCompatible(kernel[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool AreFinite(double r, double g, double b, double a)
|
||||
{
|
||||
return double.IsFinite(r) && double.IsFinite(g) && double.IsFinite(b) && double.IsFinite(a);
|
||||
}
|
||||
|
||||
private static bool IsFloatCompatible(double value)
|
||||
{
|
||||
return double.IsFinite(value) && float.IsFinite((float)value);
|
||||
}
|
||||
|
||||
private static bool Equal(byte[] expected, byte[] actual)
|
||||
{
|
||||
return expected.AsSpan().SequenceEqual(actual);
|
||||
}
|
||||
|
||||
private static byte[] CopyProbe(byte[] source, int width, int height, int probeWidth, int probeHeight)
|
||||
{
|
||||
var data = new byte[probeWidth * probeHeight * 4];
|
||||
for (int y = 0; y < probeHeight; y++)
|
||||
Buffer.BlockCopy(source, y * width * 4, data, y * probeWidth * 4, probeWidth * 4);
|
||||
return data;
|
||||
}
|
||||
|
||||
private static byte[] CreateProbe(int width, int height)
|
||||
{
|
||||
var data = new byte[width * height * 4];
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
int index = (y * width + x) * 4;
|
||||
byte a = (byte)((x * 31 + y * 47) & 255);
|
||||
data[index] = a == 0 ? (byte)0 : (byte)((x * 17 + y * 29) % (a + 1));
|
||||
data[index + 1] = a == 0 ? (byte)0 : (byte)((x * 43 + y * 11) % (a + 1));
|
||||
data[index + 2] = a == 0 ? (byte)0 : (byte)((x * 7 + y * 53) % (a + 1));
|
||||
data[index + 3] = a;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private sealed class ConvolveValidation
|
||||
{
|
||||
private readonly double[] _kernel;
|
||||
private readonly int _size;
|
||||
private readonly double _divisor;
|
||||
private readonly double _offset;
|
||||
|
||||
public ConvolveValidation(double[] kernel, int size, double divisor, double offset, bool valid)
|
||||
{
|
||||
int taps = size * size;
|
||||
_kernel = new double[taps];
|
||||
Array.Copy(kernel, _kernel, taps);
|
||||
_size = size;
|
||||
_divisor = divisor;
|
||||
_offset = offset;
|
||||
Valid = valid;
|
||||
}
|
||||
|
||||
public bool Valid { get; }
|
||||
|
||||
public bool Matches(double[] kernel, int size, double divisor, double offset)
|
||||
{
|
||||
if (_size != size || BitConverter.DoubleToInt64Bits(_divisor) != BitConverter.DoubleToInt64Bits(divisor) ||
|
||||
BitConverter.DoubleToInt64Bits(_offset) != BitConverter.DoubleToInt64Bits(offset))
|
||||
return false;
|
||||
int taps = size * size;
|
||||
for (int i = 0; i < taps; i++)
|
||||
{
|
||||
if (BitConverter.DoubleToInt64Bits(_kernel[i]) != BitConverter.DoubleToInt64Bits(kernel[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return _resize.TryResize(source, sourceWidth, sourceHeight, targetWidth, targetHeight, linear, out target);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using LuaScript.Engine.Shader;
|
||||
|
||||
namespace LuaScript.Engine.Processing
|
||||
{
|
||||
internal abstract class GpuPixelOperation(PixelShaderRunner runner)
|
||||
{
|
||||
protected const int MaxTextureSize = 16384;
|
||||
|
||||
protected bool Run(byte[] bytecode, ReadOnlySpan<PixelShaderInput> resources, ReadOnlySpan<float> constants, byte[] target, int width, int height)
|
||||
{
|
||||
return runner.TryRunHardware(
|
||||
bytecode,
|
||||
resources,
|
||||
constants,
|
||||
PixelShaderBlend.Copy,
|
||||
PixelShaderSampler.None,
|
||||
target,
|
||||
width,
|
||||
height,
|
||||
out _) == PixelShaderRunStatus.Success;
|
||||
}
|
||||
|
||||
protected static bool IsTextureSupported(byte[] buffer, int width, int height)
|
||||
{
|
||||
return IsTextureSizeSupported(width, height) && (long)buffer.Length >= (long)width * height * 4;
|
||||
}
|
||||
|
||||
protected static bool IsTextureSizeSupported(int width, int height)
|
||||
{
|
||||
return width > 0 && height > 0 && width <= MaxTextureSize && height <= MaxTextureSize;
|
||||
}
|
||||
|
||||
protected static bool Equal(byte[] expected, byte[] actual)
|
||||
{
|
||||
return expected.AsSpan().SequenceEqual(actual);
|
||||
}
|
||||
|
||||
protected static byte[] CopyProbe(byte[] source, int width, int height, int probeWidth, int probeHeight)
|
||||
{
|
||||
var data = new byte[probeWidth * probeHeight * 4];
|
||||
for (int y = 0; y < probeHeight; y++)
|
||||
Buffer.BlockCopy(source, y * width * 4, data, y * probeWidth * 4, probeWidth * 4);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,13 @@ using System;
|
||||
|
||||
namespace LuaScript.Engine.Processing
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
|
||||
internal sealed class GpuPixelOperationShaderAttribute(string apiName, string fileName) : Attribute
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
||||
internal sealed class GpuPixelOperationShaderAttribute(string methodName, string fileName, string entryPoint) : Attribute
|
||||
{
|
||||
public string ApiName { get; } = apiName;
|
||||
public string MethodName { get; } = methodName;
|
||||
|
||||
public string FileName { get; } = fileName;
|
||||
|
||||
public string EntryPoint { get; } = entryPoint;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using LuaScript.Engine.Shader;
|
||||
|
||||
namespace LuaScript.Engine.Processing
|
||||
{
|
||||
[GpuPixelOperationShader(nameof(RunResizeNearest), "Shaders/resize.hlsl", "resize_nearest")]
|
||||
[GpuPixelOperationShader(nameof(RunResizeLinear), "Shaders/resize.hlsl", "resize_linear")]
|
||||
internal sealed partial class GpuResizeOperation(PixelShaderRunner runner) : GpuPixelOperation(runner)
|
||||
{
|
||||
private const int ResizeThreshold = 262144;
|
||||
|
||||
private readonly PixelShaderInput[] _resources = new PixelShaderInput[1];
|
||||
private readonly float[] _constants = new float[4];
|
||||
|
||||
private byte[] _target = [];
|
||||
private bool? _nearestValidated;
|
||||
private bool? _linearValidated;
|
||||
|
||||
public bool TryResize(byte[] source, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, bool linear, out byte[]? target)
|
||||
{
|
||||
target = null;
|
||||
if (!IsTextureSupported(source, sourceWidth, sourceHeight) || !IsTextureSizeSupported(targetWidth, targetHeight))
|
||||
return false;
|
||||
|
||||
long pixels = (long)targetWidth * targetHeight;
|
||||
if (pixels < ResizeThreshold || !(linear ? EnsureLinearValidated(source, sourceWidth, sourceHeight) : EnsureNearestValidated(source, sourceWidth, sourceHeight)))
|
||||
return false;
|
||||
|
||||
return RunShader(source, sourceWidth, sourceHeight, targetWidth, targetHeight, linear, out target);
|
||||
}
|
||||
|
||||
private bool EnsureNearestValidated(byte[] source, int sourceWidth, int sourceHeight)
|
||||
{
|
||||
if (_nearestValidated.HasValue)
|
||||
return _nearestValidated.Value;
|
||||
_nearestValidated = Validate(source, sourceWidth, sourceHeight, false);
|
||||
return _nearestValidated.Value;
|
||||
}
|
||||
|
||||
private bool EnsureLinearValidated(byte[] source, int sourceWidth, int sourceHeight)
|
||||
{
|
||||
if (_linearValidated.HasValue)
|
||||
return _linearValidated.Value;
|
||||
_linearValidated = Validate(source, sourceWidth, sourceHeight, true);
|
||||
return _linearValidated.Value;
|
||||
}
|
||||
|
||||
private bool Validate(byte[] source, int sourceWidth, int sourceHeight, bool linear)
|
||||
{
|
||||
int probeWidth = Math.Min(sourceWidth, 8);
|
||||
int probeHeight = Math.Min(sourceHeight, 7);
|
||||
int targetWidth = Math.Max(1, probeWidth * 2 - 3);
|
||||
int targetHeight = Math.Max(1, probeHeight * 2 - 5);
|
||||
var probe = CopyProbe(source, sourceWidth, sourceHeight, probeWidth, probeHeight);
|
||||
byte[]? sourceScratch = null;
|
||||
byte[]? targetScratch = null;
|
||||
var cpu = PixelBufferSoftwareProcessor.Resize(probe, probeWidth, probeHeight, targetWidth, targetHeight, linear, ref sourceScratch, ref targetScratch);
|
||||
return RunShader(probe, probeWidth, probeHeight, targetWidth, targetHeight, linear, out var gpu) && gpu is not null && Equal(cpu, gpu);
|
||||
}
|
||||
|
||||
private bool RunShader(byte[] source, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, bool linear, out byte[]? target)
|
||||
{
|
||||
int count = checked(targetWidth * targetHeight * 4);
|
||||
if (_target.Length != count)
|
||||
_target = new byte[count];
|
||||
|
||||
_constants[0] = sourceWidth;
|
||||
_constants[1] = sourceHeight;
|
||||
_constants[2] = targetWidth;
|
||||
_constants[3] = targetHeight;
|
||||
_resources[0] = new PixelShaderInput(source, sourceWidth, sourceHeight);
|
||||
target = _target;
|
||||
return linear
|
||||
? RunResizeLinear(_resources.AsSpan(0, 1), _constants.AsSpan(0, 4), _target, targetWidth, targetHeight)
|
||||
: RunResizeNearest(_resources.AsSpan(0, 1), _constants.AsSpan(0, 4), _target, targetWidth, targetHeight);
|
||||
}
|
||||
|
||||
private partial bool RunResizeNearest(ReadOnlySpan<PixelShaderInput> resources, ReadOnlySpan<float> constants, byte[] target, int width, int height);
|
||||
|
||||
private partial bool RunResizeLinear(ReadOnlySpan<PixelShaderInput> resources, ReadOnlySpan<float> constants, byte[] target, int width, int height);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
using LuaScript.Engine.Processing;
|
||||
|
||||
[assembly: GpuPixelOperationShader("fill", "Shaders/fill.hlsl")]
|
||||
[assembly: GpuPixelOperationShader("convolve", "Shaders/convolve.hlsl")]
|
||||
[assembly: GpuPixelOperationShader("resize", "Shaders/resize.hlsl")]
|
||||
@@ -42,6 +42,7 @@ namespace LuaScript.Engine.Shader
|
||||
|
||||
private readonly Lock _locker = new();
|
||||
private readonly Dictionary<(string Hlsl, string Entry), CompiledShader> _shaders = [];
|
||||
private readonly Dictionary<byte[], ID3D11PixelShader> _precompiled = new(ReferenceEqualityComparer.Instance);
|
||||
private readonly InputSlot[] _inputs = new InputSlot[MaxResources];
|
||||
private readonly ID3D11ShaderResourceView[] _boundViews = new ID3D11ShaderResourceView[MaxResources];
|
||||
private readonly ID3D11ShaderResourceView[] _nullViews = new ID3D11ShaderResourceView[MaxResources];
|
||||
@@ -87,49 +88,16 @@ namespace LuaScript.Engine.Shader
|
||||
int targetWidth,
|
||||
int targetHeight,
|
||||
out string? error)
|
||||
{
|
||||
return TryRunCore(hlsl, entryPoint, resources, constants, blend, sampler, target, targetWidth, targetHeight, false, out error);
|
||||
}
|
||||
|
||||
public PixelShaderRunStatus TryRunHardware(
|
||||
string hlsl,
|
||||
string entryPoint,
|
||||
ReadOnlySpan<PixelShaderInput> resources,
|
||||
ReadOnlySpan<float> constants,
|
||||
PixelShaderBlend blend,
|
||||
PixelShaderSampler sampler,
|
||||
byte[] target,
|
||||
int targetWidth,
|
||||
int targetHeight,
|
||||
out string? error)
|
||||
{
|
||||
return TryRunCore(hlsl, entryPoint, resources, constants, blend, sampler, target, targetWidth, targetHeight, true, out error);
|
||||
}
|
||||
|
||||
private PixelShaderRunStatus TryRunCore(
|
||||
string hlsl,
|
||||
string entryPoint,
|
||||
ReadOnlySpan<PixelShaderInput> resources,
|
||||
ReadOnlySpan<float> constants,
|
||||
PixelShaderBlend blend,
|
||||
PixelShaderSampler sampler,
|
||||
byte[] target,
|
||||
int targetWidth,
|
||||
int targetHeight,
|
||||
bool hardwareOnly,
|
||||
out string? error)
|
||||
{
|
||||
error = null;
|
||||
lock (_locker)
|
||||
{
|
||||
if (_disposed || _unavailable)
|
||||
return PixelShaderRunStatus.Unavailable;
|
||||
if (targetWidth <= 0 || targetHeight <= 0 || (long)target.Length < (long)targetWidth * targetHeight * 4)
|
||||
if (!CanRun(target, targetWidth, targetHeight))
|
||||
return PixelShaderRunStatus.Unavailable;
|
||||
|
||||
try
|
||||
{
|
||||
if (hardwareOnly ? !EnsureHardwareDevice() : !EnsureDevice())
|
||||
if (!EnsureDevice())
|
||||
return PixelShaderRunStatus.Unavailable;
|
||||
|
||||
var compiled = GetOrCompile(hlsl, entryPoint);
|
||||
@@ -150,6 +118,46 @@ namespace LuaScript.Engine.Shader
|
||||
}
|
||||
}
|
||||
|
||||
public PixelShaderRunStatus TryRunHardware(
|
||||
byte[] bytecode,
|
||||
ReadOnlySpan<PixelShaderInput> resources,
|
||||
ReadOnlySpan<float> constants,
|
||||
PixelShaderBlend blend,
|
||||
PixelShaderSampler sampler,
|
||||
byte[] target,
|
||||
int targetWidth,
|
||||
int targetHeight,
|
||||
out string? error)
|
||||
{
|
||||
error = null;
|
||||
lock (_locker)
|
||||
{
|
||||
if (!CanRun(target, targetWidth, targetHeight))
|
||||
return PixelShaderRunStatus.Unavailable;
|
||||
|
||||
try
|
||||
{
|
||||
if (!EnsureHardwareDevice())
|
||||
return PixelShaderRunStatus.Unavailable;
|
||||
|
||||
Execute(GetOrCreate(bytecode), resources, constants, blend, sampler, target, targetWidth, targetHeight);
|
||||
return PixelShaderRunStatus.Success;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
HandleFailure(ex);
|
||||
return PixelShaderRunStatus.Unavailable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanRun(byte[] target, int targetWidth, int targetHeight)
|
||||
{
|
||||
return !_disposed && !_unavailable &&
|
||||
targetWidth > 0 && targetHeight > 0 &&
|
||||
(long)target.Length >= (long)targetWidth * targetHeight * 4;
|
||||
}
|
||||
|
||||
public void Invalidate()
|
||||
{
|
||||
lock (_locker)
|
||||
@@ -334,6 +342,16 @@ namespace LuaScript.Engine.Shader
|
||||
return compiled;
|
||||
}
|
||||
|
||||
private ID3D11PixelShader GetOrCreate(byte[] bytecode)
|
||||
{
|
||||
if (_precompiled.TryGetValue(bytecode, out var cached))
|
||||
return cached;
|
||||
|
||||
var shader = _device!.CreatePixelShader(bytecode);
|
||||
_precompiled[bytecode] = shader;
|
||||
return shader;
|
||||
}
|
||||
|
||||
private static byte[] CompileOrThrow(string source, string entryPoint, string profile)
|
||||
{
|
||||
var result = Compiler.Compile(Encoding.UTF8.GetBytes(source), entryPoint, string.Empty, profile, out var bytecode, out var errors);
|
||||
@@ -596,6 +614,9 @@ namespace LuaScript.Engine.Shader
|
||||
foreach (var compiled in _shaders.Values)
|
||||
compiled.Shader?.Dispose();
|
||||
_shaders.Clear();
|
||||
foreach (var shader in _precompiled.Values)
|
||||
shader.Dispose();
|
||||
_precompiled.Clear();
|
||||
}
|
||||
|
||||
private void ReleaseDeviceObjects()
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする