シルエットハッシュによる構造・描画キャッシュを導入

このコミットが含まれているのは:
routersys committed 2026-07-17 17:45:13 +09:00
1 親 7329f8b89a
コミット 3db838a079
5ファイル変更
+202 -26

検索条件に一致するファイルはありません

+37
ファイルの表示
@@ -524,6 +524,43 @@ public sealed class DielectricBreakdownEffectTests
}
}
[Fact]
public void SimulateCachesStructureUntilInputsChange()
{
using var pipeline = DielectricBreakdownPipeline.TryCreate();
if (pipeline is null)
{
Assert.Skip("Direct3D 12 is unavailable.");
return;
}
const int width = 128;
const int height = 128;
var source = CreateSquareSource(width, height, 48, 16, 32, 32);
var device = GraphicsDevice.GetDefault();
using var sourceTexture = device.AllocateReadWriteTexture2D<Bgra32, Float4>(width, height);
var sourcePixels = new Bgra32[source.Length];
for (var index = 0; index < source.Length; index++)
sourcePixels[index].PackedValue = unchecked((uint)source[index]);
sourceTexture.CopyFrom(sourcePixels);
var parameters = CreateParameters(seed: 3);
Assert.True(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in parameters));
Assert.False(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in parameters));
var growthChanged = parameters with { Growth = 0.5f };
Assert.False(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in growthChanged));
var seedChanged = parameters with { Seed = 4 };
Assert.True(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in seedChanged));
var movedSource = CreateSquareSource(width, height, 32, 32, 32, 32);
for (var index = 0; index < movedSource.Length; index++)
sourcePixels[index].PackedValue = unchecked((uint)movedSource[index]);
sourceTexture.CopyFrom(sourcePixels);
Assert.True(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in seedChanged));
}
[Fact]
public void Direct2DInteropProducesLightningFromOpaqueCore()
{
+40 -13
ファイルの表示
@@ -22,9 +22,11 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
private bool _hasOutput;
private bool _hasOutputOffset;
private bool _hasCropRect;
private bool _hasRenderState;
private Vector2 _outputOffset;
private Vector4 _cropRect;
private Parameters _parameters;
private RenderState _renderState;
public DielectricBreakdownEffectProcessor(IGraphicsDevicesAndContext devices, DielectricBreakdownEffect item)
: base(devices)
@@ -119,10 +121,11 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
parameters.Color.B / 255f,
Math.Max(parameters.Seed, 0));
bool structureChanged;
_interop.BeginCompute();
try
{
_pipeline.Simulate(
structureChanged = _pipeline.Simulate(
_interop.SourceTexture,
canvasWidth,
canvasHeight,
@@ -142,25 +145,39 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
_effect.Amount = 0f;
_parameters = parameters;
_isFirst = true;
_hasRenderState = false;
return effectDescription.DrawDescription;
}
if (!_interop.OutputCovers(rect.Width, rect.Height))
_outputCrop.SetInput(0, null, true);
var outputChanged = _interop.EnsureOutput(rect.Width, rect.Height);
_interop.BeginCompute();
try
var renderState = new RenderState(
pipelineParameters.Growth,
pipelineParameters.Thickness,
pipelineParameters.Glow,
pipelineParameters.ColorR,
pipelineParameters.ColorG,
pipelineParameters.ColorB,
rect);
if (structureChanged || outputChanged || !_hasOutput || !_hasRenderState || _renderState != renderState)
{
_pipeline.RenderVisible(
_interop.OutputTexture,
canvasWidth,
canvasHeight,
rect,
in pipelineParameters);
}
finally
{
_interop.EndCompute();
_interop.BeginCompute();
try
{
_pipeline.RenderVisible(
_interop.OutputTexture,
canvasWidth,
canvasHeight,
rect,
in pipelineParameters);
}
finally
{
_interop.EndCompute();
}
_renderState = renderState;
_hasRenderState = true;
}
if (outputChanged || !_hasOutput)
@@ -270,6 +287,7 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
_hasOutput = false;
_hasOutputOffset = false;
_hasCropRect = false;
_hasRenderState = false;
}
protected override void Dispose(bool disposing)
@@ -292,6 +310,15 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
}
}
private readonly record struct RenderState(
float Growth,
float Thickness,
float Glow,
float ColorR,
float ColorG,
float ColorB,
DielectricBreakdownPipeline.PixelRect Rect);
private readonly record struct Parameters(
float Amount,
float Growth,
+49
ファイルの表示
@@ -120,6 +120,55 @@ internal readonly partial struct SilhouetteShader(
}
}
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MaskHashResetShader(
ReadWriteBuffer<int> scratch) : IComputeShader
{
private readonly ReadWriteBuffer<int> scratch = scratch;
public void Execute()
{
if (ThreadIds.X != 0)
return;
scratch[6] = 0;
scratch[7] = 0;
}
}
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MaskHashShader(
ReadWriteBuffer<int> mask,
ReadWriteBuffer<int> scratch,
int gridWidth,
int gridHeight) : IComputeShader
{
private readonly ReadWriteBuffer<int> mask = mask;
private readonly ReadWriteBuffer<int> scratch = scratch;
private readonly int gridWidth = gridWidth;
private readonly int gridHeight = gridHeight;
public void Execute()
{
var gx = ThreadIds.X;
var gy = ThreadIds.Y;
if (gx >= gridWidth || gy >= gridHeight)
return;
var index = gy * gridWidth + gx;
if (mask[index] != 1)
return;
var mixed = (uint)index * 0x9E3779B9u;
mixed ^= mixed >> 16;
mixed *= 0x85EBCA6Bu;
mixed ^= mixed >> 13;
Hlsl.InterlockedAdd(ref scratch[6], (int)mixed);
Hlsl.InterlockedXor(ref scratch[7], (int)(mixed * 0xC2B2AE35u));
}
}
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct ElectrodeInitShader(
+73 -12
ファイルの表示
@@ -19,6 +19,10 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
private readonly ReadBackBuffer<int> _scratchReadBack;
private readonly ReadBackBuffer<int> _growthLogReadBack;
private readonly ReadBackBuffer<int> _parentLogReadBack;
private readonly int[] _cachedGrowthLog;
private readonly int[] _cachedParentLog;
private int _cachedTotal;
private StructureKey? _structureKey;
private ReadWriteBuffer<int>? _rowCounts;
private ReadWriteBuffer<int>? _rowOffsets;
private ReadWriteBuffer<int>? _potential;
@@ -46,6 +50,8 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
_scratchReadBack = device.AllocateReadBackBuffer<int>(DielectricBreakdownSettings.ScratchLength);
_growthLogReadBack = device.AllocateReadBackBuffer<int>(DielectricBreakdownSettings.MaximumStepCount);
_parentLogReadBack = device.AllocateReadBackBuffer<int>(DielectricBreakdownSettings.MaximumStepCount);
_cachedGrowthLog = new int[DielectricBreakdownSettings.MaximumStepCount];
_cachedParentLog = new int[DielectricBreakdownSettings.MaximumStepCount];
}
public static DielectricBreakdownPipeline? TryCreate()
@@ -118,7 +124,7 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
RecordFullPipeline(in context, source, destination, width, height, in parameters);
}
internal void Simulate(
internal bool Simulate(
ReadWriteTexture2D<Bgra32, Float4> source,
int canvasWidth,
int canvasHeight,
@@ -131,25 +137,53 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
EnsureGridFor(canvasWidth, canvasHeight, parameters.Quality);
var derived = Derive(canvasWidth, canvasHeight, in parameters);
using (ComputeContext context = _device.CreateComputeContext())
RecordGrowthStage(in context, source, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, in derived, in parameters);
{
RecordSilhouetteStage(in context, source, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, in derived);
RecordMaskHashStage(in context);
}
_scratchReadBack.CopyFrom(_scratch);
var hashed = _scratchReadBack.Span;
var key = new StructureKey(
hashed[6],
hashed[7],
canvasWidth,
canvasHeight,
parameters.Quality,
parameters.Seed,
parameters.AngleDegrees,
parameters.ReachPixels,
parameters.Branching);
if (_structureKey == key)
return false;
using (ComputeContext context = _device.CreateComputeContext())
RecordGrowthStage(in context, in derived, in parameters);
_scratchReadBack.CopyFrom(_scratch);
_growthLogReadBack.CopyFrom(_growthLog);
_parentLogReadBack.CopyFrom(_parentLog);
var scratch = _scratchReadBack.Span;
var contact = scratch[1];
_cachedTotal = contact != derived.Sentinel ? contact : scratch[4];
if (_cachedTotal > 0)
{
_growthLogReadBack.Span[.._cachedTotal].CopyTo(_cachedGrowthLog);
_parentLogReadBack.Span[.._cachedTotal].CopyTo(_cachedParentLog);
}
_structureKey = key;
return true;
}
internal bool TryGetVisibleBounds(int canvasWidth, int canvasHeight, in Parameters parameters, out PixelRect rect)
{
rect = default;
var derived = Derive(canvasWidth, canvasHeight, in parameters);
var scratch = _scratchReadBack.Span;
var contact = scratch[1];
var total = contact != derived.Sentinel ? contact : scratch[4];
var total = _cachedTotal;
var visible = parameters.Growth * total;
if (total <= 0 || visible <= 0f)
return false;
var growthLog = _growthLogReadBack.Span;
var parentLog = _parentLogReadBack.Span;
var growthLog = _cachedGrowthLog;
var parentLog = _cachedParentLog;
var minX = int.MaxValue;
var minY = int.MaxValue;
var maxX = int.MinValue;
@@ -203,18 +237,37 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
int height,
in Parameters parameters)
{
_structureKey = null;
var derived = Derive(width, height, in parameters);
RecordGrowthStage(in context, source, 0, 0, width, height, in derived, in parameters);
RecordSilhouetteStage(in context, source, 0, 0, width, height, in derived);
RecordGrowthStage(in context, in derived, in parameters);
RecordRenderStage(in context, output, new PixelRect(0, 0, width, height), in derived, in parameters);
}
private void RecordGrowthStage(
private void RecordSilhouetteStage(
in ComputeContext context,
ReadWriteTexture2D<Bgra32, Float4> source,
int sourceOffsetX,
int sourceOffsetY,
int sourceWidth,
int sourceHeight,
in DerivedValues derived)
{
context.For(_gridWidth, _gridHeight, new SilhouetteShader(
source, _mask!, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, _gridWidth, _gridHeight, derived.CellSize, 0.05f));
context.Barrier(_mask!);
}
private void RecordMaskHashStage(in ComputeContext context)
{
context.For(1, new MaskHashResetShader(_scratch));
context.Barrier(_scratch);
context.For(_gridWidth, _gridHeight, new MaskHashShader(_mask!, _scratch, _gridWidth, _gridHeight));
context.Barrier(_scratch);
}
private void RecordGrowthStage(
in ComputeContext context,
in DerivedValues derived,
in Parameters parameters)
{
@@ -232,9 +285,6 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
var potential = _potential!;
context.For(1, new InitScratchShader(_scratch, derived.Sentinel));
context.For(gridWidth, gridHeight, new SilhouetteShader(
source, mask, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, gridWidth, gridHeight, derived.CellSize, 0.05f));
context.Barrier(mask);
context.Barrier(_scratch);
context.For(gridWidth, gridHeight, new ElectrodeInitShader(mask, state, birth, parent, _scratch, gridWidth, gridHeight, derived.DirectionX, derived.DirectionY, DielectricBreakdownSettings.ProjectionQuantScale));
context.Barrier(state);
@@ -487,6 +537,17 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
internal readonly record struct PixelRect(int X, int Y, int Width, int Height);
private readonly record struct StructureKey(
int MaskHashSum,
int MaskHashMix,
int CanvasWidth,
int CanvasHeight,
DielectricBreakdownQuality Quality,
int Seed,
float AngleDegrees,
float ReachPixels,
float Branching);
private readonly record struct DerivedValues(
float CellSize,
int MaxSteps,
+3 -1
ファイルの表示
@@ -18,13 +18,15 @@ internal static class DielectricBreakdownSettings
public const int GrowthThreadCount = 1024;
public const int MaximumStepCount = 1536;
public const float PotentialQuantScale = 16384f;
public const int ScratchLength = 6;
public const int ScratchLength = 8;
public const int ScratchChargeCount = 0;
public const int ScratchContactStep = 1;
public const int ScratchContactCell = 2;
public const int ScratchSilhouetteMaxProjection = 3;
public const int ScratchGrownSteps = 4;
public const int ScratchCandidateCount = 5;
public const int ScratchMaskHashSum = 6;
public const int ScratchMaskHashMix = 7;
public static QualitySettings GetQuality(DielectricBreakdownQuality quality)
=> quality switch