CPUの画素の8ビットとの変換を別の型へ分ける
このコミットが含まれているのは:
6ファイル変更
+139
-130
検索条件に一致するファイルはありません
@@ -0,0 +1,116 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
namespace SpectralConvolution;
|
||||
|
||||
internal static class CpuPixels
|
||||
{
|
||||
const int Channels = 4;
|
||||
|
||||
static readonly float[] Units = BuildUnits();
|
||||
|
||||
public static byte ToUnorm(float value)
|
||||
{
|
||||
if (float.IsNaN(value))
|
||||
return 0;
|
||||
return (byte)(Math.Clamp(value, 0f, 1f) * ByteColor.Scale + ByteColor.Rounding);
|
||||
}
|
||||
|
||||
public static bool LoadRow(Span<float> row, ReadOnlySpan<byte> pixels, ReadOnlySpan<int> order, int sourceX, int left, int first, int last, in LightOptions light)
|
||||
{
|
||||
row.Clear();
|
||||
var lit = false;
|
||||
var units = Units;
|
||||
for (var x = first; x < last; x++)
|
||||
{
|
||||
var offset = (left + x - sourceX) * Channels;
|
||||
var blue = pixels[offset];
|
||||
var green = pixels[offset + 1];
|
||||
var red = pixels[offset + 2];
|
||||
var alpha = pixels[offset + 3];
|
||||
if ((blue | green | red | alpha) == 0)
|
||||
continue;
|
||||
|
||||
var target = order[x] * Channels;
|
||||
if (light.Linear)
|
||||
{
|
||||
var (linearRed, linearGreen, linearBlue, linearAlpha) = LightTransform.FromBytes(red, green, blue, alpha, in light);
|
||||
row[target] = linearRed;
|
||||
row[target + 1] = linearGreen;
|
||||
row[target + 2] = linearBlue;
|
||||
row[target + 3] = linearAlpha;
|
||||
}
|
||||
else
|
||||
{
|
||||
row[target] = units[red];
|
||||
row[target + 1] = units[green];
|
||||
row[target + 2] = units[blue];
|
||||
row[target + 3] = units[alpha];
|
||||
}
|
||||
lit = true;
|
||||
}
|
||||
|
||||
return lit;
|
||||
}
|
||||
|
||||
public static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, Span<byte> bytes, Span<float> store)
|
||||
{
|
||||
var count = pixels.Length / Channels;
|
||||
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
|
||||
throw new ArgumentException(null, nameof(bytes));
|
||||
|
||||
ref var from = ref MemoryMarshal.GetReference(pixels);
|
||||
ref var to = ref MemoryMarshal.GetReference(bytes);
|
||||
ref var kept = ref MemoryMarshal.GetReference(store);
|
||||
var scaleVector = Vector128.Create(scale);
|
||||
var gainVector = Vector128.Create(gain);
|
||||
var keep = !store.IsEmpty;
|
||||
for (var pixel = 0; pixel < count; pixel++)
|
||||
{
|
||||
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
|
||||
if (keep)
|
||||
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
|
||||
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), PackBgra(value * gainVector));
|
||||
}
|
||||
}
|
||||
|
||||
public static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, in LightOptions light, int x, int y, Span<byte> bytes, Span<float> store)
|
||||
{
|
||||
var count = pixels.Length / Channels;
|
||||
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
|
||||
throw new ArgumentException(null, nameof(bytes));
|
||||
|
||||
ref var from = ref MemoryMarshal.GetReference(pixels);
|
||||
ref var to = ref MemoryMarshal.GetReference(bytes);
|
||||
ref var kept = ref MemoryMarshal.GetReference(store);
|
||||
var scaleVector = Vector128.Create(scale);
|
||||
var keep = !store.IsEmpty;
|
||||
for (var pixel = 0; pixel < count; pixel++)
|
||||
{
|
||||
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
|
||||
if (keep)
|
||||
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
|
||||
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), LightTransform.ToPacked(value, gain, in light, x + pixel, y));
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static uint PackBgra(Vector128<float> rgba)
|
||||
{
|
||||
var ordered = Vector128.Shuffle(rgba, Vector128.Create(2, 1, 0, 3));
|
||||
ordered = Vector128.ConditionalSelect(Vector128.Equals(ordered, ordered), ordered, Vector128<float>.Zero);
|
||||
var clamped = Vector128.Min(Vector128.Max(ordered, Vector128<float>.Zero), Vector128<float>.One);
|
||||
var levels = Vector128.ConvertToInt32(clamped * Vector128.Create(ByteColor.Scale) + Vector128.Create(ByteColor.Rounding)).AsUInt32();
|
||||
var words = Vector128.Narrow(levels, levels);
|
||||
return Vector128.Narrow(words, words).AsUInt32().ToScalar();
|
||||
}
|
||||
|
||||
static float[] BuildUnits()
|
||||
{
|
||||
var units = new float[ByteColor.Levels];
|
||||
for (var level = 0; level < units.Length; level++)
|
||||
units[level] = level / ByteColor.Scale;
|
||||
return units;
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,6 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
public const int StoredChunkPixels = 16384;
|
||||
const int Channels = 4;
|
||||
|
||||
static readonly float[] Units = BuildUnits();
|
||||
|
||||
readonly Worker[] workers;
|
||||
readonly WorkerPool pool;
|
||||
readonly ConvolveJob convolveJob;
|
||||
@@ -217,21 +215,6 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
static float[] BuildUnits()
|
||||
{
|
||||
var units = new float[ByteColor.Levels];
|
||||
for (var level = 0; level < units.Length; level++)
|
||||
units[level] = level / ByteColor.Scale;
|
||||
return units;
|
||||
}
|
||||
|
||||
public static byte ToUnorm(float value)
|
||||
{
|
||||
if (float.IsNaN(value))
|
||||
return 0;
|
||||
return (byte)(Math.Clamp(value, 0f, 1f) * ByteColor.Scale + ByteColor.Rounding);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed)
|
||||
@@ -273,6 +256,7 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
var order = reversal.AsSpan(0, size);
|
||||
var twiddles = chromatic is { } multi ? multi.Twiddles : spectrum!.Twiddles;
|
||||
var values = spectrum is { } single ? single.Spectrum : default;
|
||||
var light = this.light;
|
||||
|
||||
var lit = false;
|
||||
var first = Math.Max(0, sourceX - left);
|
||||
@@ -281,7 +265,7 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
{
|
||||
var row = work.Slice(y * rowLength, rowLength);
|
||||
var sourceRow = top + y - sourceY;
|
||||
if (sourceRow < 0 || sourceRow >= sourceHeight || first >= last || !LoadRow(row, order, sourceRow, left, first, last))
|
||||
if (sourceRow < 0 || sourceRow >= sourceHeight || first >= last || !CpuPixels.LoadRow(row, source.AsSpan(sourceRow * sourceWidth * Channels, sourceWidth * Channels), order, sourceX, left, first, last, in light))
|
||||
{
|
||||
row.Clear();
|
||||
continue;
|
||||
@@ -322,7 +306,6 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
}
|
||||
|
||||
var scale = 1f / (size * size);
|
||||
var light = this.light;
|
||||
var outputOrigin = plan.RegionX + regionLeft;
|
||||
for (var y = radius; y < radius + validHeight; y++)
|
||||
{
|
||||
@@ -338,9 +321,9 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
var bytes = output.AsSpan(index, validWidth * Channels);
|
||||
var store = convolved is { } convolvedValues ? convolvedValues.AsSpan(index, validWidth * Channels) : default;
|
||||
if (light.IsDefault)
|
||||
Emit(pixels, scale, gain, bytes, store);
|
||||
CpuPixels.Emit(pixels, scale, gain, bytes, store);
|
||||
else
|
||||
Emit(pixels, scale, gain, in light, outputOrigin, plan.RegionY + outputRow, bytes, store);
|
||||
CpuPixels.Emit(pixels, scale, gain, in light, outputOrigin, plan.RegionY + outputRow, bytes, store);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,58 +404,6 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, Span<byte> bytes, Span<float> store)
|
||||
{
|
||||
var count = pixels.Length / Channels;
|
||||
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
|
||||
throw new ArgumentException(null, nameof(bytes));
|
||||
|
||||
ref var from = ref MemoryMarshal.GetReference(pixels);
|
||||
ref var to = ref MemoryMarshal.GetReference(bytes);
|
||||
ref var kept = ref MemoryMarshal.GetReference(store);
|
||||
var scaleVector = Vector128.Create(scale);
|
||||
var gainVector = Vector128.Create(gain);
|
||||
var keep = !store.IsEmpty;
|
||||
for (var pixel = 0; pixel < count; pixel++)
|
||||
{
|
||||
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
|
||||
if (keep)
|
||||
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
|
||||
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), PackBgra(value * gainVector));
|
||||
}
|
||||
}
|
||||
|
||||
static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, in LightOptions light, int x, int y, Span<byte> bytes, Span<float> store)
|
||||
{
|
||||
var count = pixels.Length / Channels;
|
||||
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
|
||||
throw new ArgumentException(null, nameof(bytes));
|
||||
|
||||
ref var from = ref MemoryMarshal.GetReference(pixels);
|
||||
ref var to = ref MemoryMarshal.GetReference(bytes);
|
||||
ref var kept = ref MemoryMarshal.GetReference(store);
|
||||
var scaleVector = Vector128.Create(scale);
|
||||
var keep = !store.IsEmpty;
|
||||
for (var pixel = 0; pixel < count; pixel++)
|
||||
{
|
||||
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
|
||||
if (keep)
|
||||
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
|
||||
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), LightTransform.ToPacked(value, gain, in light, x + pixel, y));
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static uint PackBgra(Vector128<float> rgba)
|
||||
{
|
||||
var ordered = Vector128.Shuffle(rgba, Vector128.Create(2, 1, 0, 3));
|
||||
ordered = Vector128.ConditionalSelect(Vector128.Equals(ordered, ordered), ordered, Vector128<float>.Zero);
|
||||
var clamped = Vector128.Min(Vector128.Max(ordered, Vector128<float>.Zero), Vector128<float>.One);
|
||||
var levels = Vector128.ConvertToInt32(clamped * Vector128.Create(ByteColor.Scale) + Vector128.Create(ByteColor.Rounding)).AsUInt32();
|
||||
var words = Vector128.Narrow(levels, levels);
|
||||
return Vector128.Narrow(words, words).AsUInt32().ToScalar();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static void Copy(ref float from, int fromIndex, ref float to, int toIndex)
|
||||
=> Vector128.LoadUnsafe(ref from, (nuint)fromIndex).StoreUnsafe(ref to, (nuint)toIndex);
|
||||
@@ -503,7 +434,7 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
var bytes = output.AsSpan(first * Channels, count * Channels);
|
||||
if (light.IsDefault)
|
||||
{
|
||||
Emit(pixels, 1f, gain, bytes, default);
|
||||
CpuPixels.Emit(pixels, 1f, gain, bytes, default);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -512,7 +443,7 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
for (var done = 0; done < count;)
|
||||
{
|
||||
var run = storedWidth > 0 ? Math.Min(count - done, storedWidth - column) : count - done;
|
||||
Emit(pixels.Slice(done * Channels, run * Channels), 1f, gain, in light, storedOriginX + column, storedOriginY + row,
|
||||
CpuPixels.Emit(pixels.Slice(done * Channels, run * Channels), 1f, gain, in light, storedOriginX + column, storedOriginY + row,
|
||||
bytes.Slice(done * Channels, run * Channels), default);
|
||||
done += run;
|
||||
column = 0;
|
||||
@@ -520,45 +451,6 @@ internal sealed class CpuTileConvolver : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
bool LoadRow(Span<float> row, ReadOnlySpan<int> order, int sourceRow, int left, int first, int last)
|
||||
{
|
||||
row.Clear();
|
||||
var lit = false;
|
||||
var pixels = source.AsSpan(sourceRow * sourceWidth * Channels, sourceWidth * Channels);
|
||||
var light = this.light;
|
||||
var units = Units;
|
||||
for (var x = first; x < last; x++)
|
||||
{
|
||||
var offset = (left + x - sourceX) * Channels;
|
||||
var blue = pixels[offset];
|
||||
var green = pixels[offset + 1];
|
||||
var red = pixels[offset + 2];
|
||||
var alpha = pixels[offset + 3];
|
||||
if ((blue | green | red | alpha) == 0)
|
||||
continue;
|
||||
|
||||
var target = order[x] * Channels;
|
||||
if (light.Linear)
|
||||
{
|
||||
var (linearRed, linearGreen, linearBlue, linearAlpha) = LightTransform.FromBytes(red, green, blue, alpha, in light);
|
||||
row[target] = linearRed;
|
||||
row[target + 1] = linearGreen;
|
||||
row[target + 2] = linearBlue;
|
||||
row[target + 3] = linearAlpha;
|
||||
}
|
||||
else
|
||||
{
|
||||
row[target] = units[red];
|
||||
row[target + 1] = units[green];
|
||||
row[target + 2] = units[blue];
|
||||
row[target + 3] = units[alpha];
|
||||
}
|
||||
lit = true;
|
||||
}
|
||||
|
||||
return lit;
|
||||
}
|
||||
|
||||
void ClearValid(int regionLeft, int regionTop, int validWidth, int validHeight)
|
||||
{
|
||||
for (var y = 0; y < validHeight; y++)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする