出力矩形を可視構造の厳密範囲へ動的最小化
このコミットが含まれているのは:
7ファイル変更
+520
-139
検索条件に一致するファイルはありません
@@ -54,6 +54,51 @@ foreach (var quality in new[] { DielectricBreakdownQuality.Balanced, DielectricB
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var device = ComputeSharp.GraphicsDevice.GetDefault();
|
||||
const int canvas = 4096;
|
||||
const int item = 400;
|
||||
var margin = (canvas - item) / 2;
|
||||
var bigPixels = new ComputeSharp.Bgra32[item * item];
|
||||
for (var y = 0; y < 120; y++)
|
||||
for (var x = 0; x < item; x++)
|
||||
bigPixels[y * item + x].PackedValue = 0xFFB0B0B8u;
|
||||
using var bigSource = device.AllocateReadWriteTexture2D<ComputeSharp.Bgra32, ComputeSharp.Float4>(item, item);
|
||||
bigSource.CopyFrom(bigPixels);
|
||||
using var fullSource = device.AllocateReadWriteTexture2D<ComputeSharp.Bgra32, ComputeSharp.Float4>(canvas, canvas);
|
||||
using var fullOutput = device.AllocateReadWriteTexture2D<ComputeSharp.Bgra32, ComputeSharp.Float4>(canvas, canvas);
|
||||
var parameters = new DielectricBreakdownPipeline.Parameters(DielectricBreakdownQuality.High, 1f, 0.5f, 0f, 1600f, 2.5f, 0.5f, 0.7f, 0.8f, 1f, 7);
|
||||
|
||||
pipeline.Process(fullSource, fullOutput, canvas, canvas, in parameters);
|
||||
pipeline.WaitForCompletion();
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
const int fullFrames = 5;
|
||||
for (var frame = 0; frame < fullFrames; frame++)
|
||||
pipeline.Process(fullSource, fullOutput, canvas, canvas, in parameters);
|
||||
pipeline.WaitForCompletion();
|
||||
stopwatch.Stop();
|
||||
Console.WriteLine($"full canvas {canvas}x{canvas}: {stopwatch.Elapsed.TotalMilliseconds / fullFrames:F2} ms/frame");
|
||||
|
||||
pipeline.Simulate(bigSource, canvas, canvas, margin, margin, item, item, in parameters);
|
||||
if (pipeline.TryGetVisibleBounds(canvas, canvas, in parameters, out var rect))
|
||||
{
|
||||
using var rectOutput = device.AllocateReadWriteTexture2D<ComputeSharp.Bgra32, ComputeSharp.Float4>(rect.Width, rect.Height);
|
||||
pipeline.RenderVisible(rectOutput, canvas, canvas, rect, in parameters);
|
||||
pipeline.WaitForCompletion();
|
||||
stopwatch.Restart();
|
||||
const int rectFrames = 10;
|
||||
for (var frame = 0; frame < rectFrames; frame++)
|
||||
{
|
||||
pipeline.Simulate(bigSource, canvas, canvas, margin, margin, item, item, in parameters);
|
||||
pipeline.TryGetVisibleBounds(canvas, canvas, in parameters, out rect);
|
||||
pipeline.RenderVisible(rectOutput, canvas, canvas, rect, in parameters);
|
||||
}
|
||||
pipeline.WaitForCompletion();
|
||||
stopwatch.Stop();
|
||||
Console.WriteLine($"visible rect {rect.Width}x{rect.Height} at ({rect.X},{rect.Y}): {stopwatch.Elapsed.TotalMilliseconds / rectFrames:F2} ms/frame");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var growth in new[] { 0.25f, 0.5f, 0.75f, 0.95f, 1f })
|
||||
{
|
||||
var parameters = new DielectricBreakdownPipeline.Parameters(DielectricBreakdownQuality.High, growth, 0.5f, 0f, 200f, 2.5f, 0.5f, 0.7f, 0.8f, 1f, 7);
|
||||
|
||||
@@ -466,6 +466,64 @@ public sealed class DielectricBreakdownEffectTests
|
||||
Assert.Equal(0, allocated);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.3f)]
|
||||
[InlineData(1f)]
|
||||
public void VisibleBoundsCoverAllLitPixelsAndMatchFullRender(float growth)
|
||||
{
|
||||
using var pipeline = DielectricBreakdownPipeline.TryCreate();
|
||||
if (pipeline is null)
|
||||
{
|
||||
Assert.Skip("Direct3D 12 is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
const int width = 192;
|
||||
const int height = 192;
|
||||
var source = CreateSquareSource(width, height, 80, 8, 32, 24);
|
||||
var full = new int[source.Length];
|
||||
var parameters = CreateParameters(growth: growth, reachPixels: 80f, glow: 0.6f, seed: 5);
|
||||
pipeline.Process(source, full, width, height, in parameters);
|
||||
|
||||
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);
|
||||
|
||||
pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in parameters);
|
||||
Assert.True(pipeline.TryGetVisibleBounds(width, height, in parameters, out var rect));
|
||||
Assert.True(rect.Width > 0 && rect.Height > 0);
|
||||
Assert.True(rect.X >= 0 && rect.Y >= 0);
|
||||
Assert.True(rect.X + rect.Width <= width && rect.Y + rect.Height <= height);
|
||||
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
if (full[y * width + x] == 0)
|
||||
continue;
|
||||
Assert.InRange(x, rect.X, rect.X + rect.Width - 1);
|
||||
Assert.InRange(y, rect.Y, rect.Y + rect.Height - 1);
|
||||
}
|
||||
}
|
||||
|
||||
using var outputTexture = device.AllocateReadWriteTexture2D<Bgra32, Float4>(rect.Width, rect.Height);
|
||||
pipeline.RenderVisible(outputTexture, width, height, rect, in parameters);
|
||||
var result = new Bgra32[rect.Width * rect.Height];
|
||||
outputTexture.CopyTo(result);
|
||||
|
||||
for (var y = 0; y < rect.Height; y++)
|
||||
{
|
||||
for (var x = 0; x < rect.Width; x++)
|
||||
{
|
||||
var expected = unchecked((uint)full[(rect.Y + y) * width + rect.X + x]);
|
||||
Assert.Equal(expected, result[y * rect.Width + x].PackedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Direct2DInteropProducesLightningFromOpaqueCore()
|
||||
{
|
||||
|
||||
@@ -14,12 +14,16 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
private DielectricBreakdownGpuInterop? _interop;
|
||||
private DielectricBreakdownPipeline? _pipeline;
|
||||
private DielectricBreakdownCustomEffect? _effect;
|
||||
private Crop? _outputCrop;
|
||||
private ID2D1Image? _outputCropOutput;
|
||||
private AffineTransform2D? _outputTransform;
|
||||
private ID2D1Image? _outputTransformOutput;
|
||||
private bool _isFirst = true;
|
||||
private bool _hasOutput;
|
||||
private bool _hasOutputOffset;
|
||||
private bool _hasCropRect;
|
||||
private Vector2 _outputOffset;
|
||||
private Vector4 _cropRect;
|
||||
private Parameters _parameters;
|
||||
|
||||
public DielectricBreakdownEffectProcessor(IGraphicsDevicesAndContext devices, DielectricBreakdownEffect item)
|
||||
@@ -31,7 +35,7 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
|
||||
public override DrawDescription Update(EffectDescription effectDescription)
|
||||
{
|
||||
if (IsPassThroughEffect || _effect is null || _outputTransform is null || _outputTransformOutput is null || _interop is null || _pipeline is null || input is null)
|
||||
if (IsPassThroughEffect || _effect is null || _outputCrop is null || _outputTransform is null || _outputTransformOutput is null || _interop is null || _pipeline is null || input is null)
|
||||
return effectDescription.DrawDescription;
|
||||
|
||||
var frame = effectDescription.ItemPosition.Frame;
|
||||
@@ -96,18 +100,11 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
}
|
||||
var canvasWidth = (int)canvasWidthValue;
|
||||
var canvasHeight = (int)canvasHeightValue;
|
||||
var itemWidth = (int)widthValue;
|
||||
var itemHeight = (int)heightValue;
|
||||
|
||||
if (!_interop.MatchesSize(canvasWidth, canvasHeight))
|
||||
_outputTransform.SetInput(0, null, true);
|
||||
var resourcesChanged = _interop.EnsureResources(canvasWidth, canvasHeight);
|
||||
var outputOffset = new Vector2(bounds.Left - margin, bounds.Top - margin);
|
||||
if (!_hasOutputOffset || _outputOffset != outputOffset)
|
||||
{
|
||||
_outputTransform.TransformMatrix = Matrix3x2.CreateTranslation(outputOffset);
|
||||
_outputOffset = outputOffset;
|
||||
_hasOutputOffset = true;
|
||||
}
|
||||
_interop.RenderInput(input, new Vortice.RawRectF(bounds.Left - margin, bounds.Top - margin, bounds.Right + margin, bounds.Bottom + margin));
|
||||
_interop.EnsureSource(itemWidth, itemHeight);
|
||||
_interop.RenderInput(input, new Vortice.RawRectF(bounds.Left, bounds.Top, bounds.Left + itemWidth, bounds.Top + itemHeight));
|
||||
|
||||
var pipelineParameters = new DielectricBreakdownPipeline.Parameters(
|
||||
parameters.Quality,
|
||||
@@ -125,11 +122,14 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
_interop.BeginCompute();
|
||||
try
|
||||
{
|
||||
_pipeline.Process(
|
||||
_pipeline.Simulate(
|
||||
_interop.SourceTexture,
|
||||
_interop.OutputTexture,
|
||||
canvasWidth,
|
||||
canvasHeight,
|
||||
margin,
|
||||
margin,
|
||||
itemWidth,
|
||||
itemHeight,
|
||||
in pipelineParameters);
|
||||
}
|
||||
finally
|
||||
@@ -137,11 +137,51 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
_interop.EndCompute();
|
||||
}
|
||||
|
||||
if (resourcesChanged || !_hasOutput)
|
||||
if (!_pipeline.TryGetVisibleBounds(canvasWidth, canvasHeight, in pipelineParameters, out var rect))
|
||||
{
|
||||
_outputTransform.SetInput(0, _interop.OutputBitmap, true);
|
||||
_effect.Amount = 0f;
|
||||
_parameters = parameters;
|
||||
_isFirst = true;
|
||||
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
|
||||
{
|
||||
_pipeline.RenderVisible(
|
||||
_interop.OutputTexture,
|
||||
canvasWidth,
|
||||
canvasHeight,
|
||||
rect,
|
||||
in pipelineParameters);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_interop.EndCompute();
|
||||
}
|
||||
|
||||
if (outputChanged || !_hasOutput)
|
||||
{
|
||||
_outputCrop.SetInput(0, _interop.OutputBitmap, true);
|
||||
_effect.SetInput(1, _outputTransformOutput, true);
|
||||
}
|
||||
var cropRect = new Vector4(0f, 0f, rect.Width, rect.Height);
|
||||
if (!_hasCropRect || _cropRect != cropRect)
|
||||
{
|
||||
_outputCrop.Rectangle = cropRect;
|
||||
_cropRect = cropRect;
|
||||
_hasCropRect = true;
|
||||
}
|
||||
var outputOffset = new Vector2(bounds.Left - margin + rect.X, bounds.Top - margin + rect.Y);
|
||||
if (!_hasOutputOffset || _outputOffset != outputOffset)
|
||||
{
|
||||
_outputTransform.TransformMatrix = Matrix3x2.CreateTranslation(outputOffset);
|
||||
_outputOffset = outputOffset;
|
||||
_hasOutputOffset = true;
|
||||
}
|
||||
_hasOutput = true;
|
||||
_parameters = parameters;
|
||||
_isFirst = false;
|
||||
@@ -161,6 +201,8 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
}
|
||||
|
||||
DielectricBreakdownCustomEffect? effect = null;
|
||||
Crop? outputCrop = null;
|
||||
ID2D1Image? outputCropOutput = null;
|
||||
AffineTransform2D? outputTransform = null;
|
||||
ID2D1Image? outputTransformOutput = null;
|
||||
ID2D1Image? output = null;
|
||||
@@ -174,18 +216,25 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
interop.Dispose();
|
||||
return null;
|
||||
}
|
||||
outputCrop = new Crop(devices.DeviceContext);
|
||||
outputCropOutput = outputCrop.Output;
|
||||
outputTransform = new AffineTransform2D(devices.DeviceContext)
|
||||
{
|
||||
BorderMode = BorderMode.Hard,
|
||||
};
|
||||
outputTransform.SetInput(0, outputCropOutput, true);
|
||||
outputTransformOutput = outputTransform.Output;
|
||||
output = effect.Output;
|
||||
_interop = interop;
|
||||
_pipeline = pipeline;
|
||||
_effect = effect;
|
||||
_outputCrop = outputCrop;
|
||||
_outputCropOutput = outputCropOutput;
|
||||
_outputTransform = outputTransform;
|
||||
_outputTransformOutput = outputTransformOutput;
|
||||
disposer.Collect(effect);
|
||||
disposer.Collect(outputCrop);
|
||||
disposer.Collect(outputCropOutput);
|
||||
disposer.Collect(outputTransform);
|
||||
disposer.Collect(outputTransformOutput);
|
||||
disposer.Collect(output);
|
||||
@@ -196,6 +245,8 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
output?.Dispose();
|
||||
outputTransformOutput?.Dispose();
|
||||
outputTransform?.Dispose();
|
||||
outputCropOutput?.Dispose();
|
||||
outputCrop?.Dispose();
|
||||
effect?.Dispose();
|
||||
pipeline.Dispose();
|
||||
interop.Dispose();
|
||||
@@ -214,10 +265,11 @@ internal sealed class DielectricBreakdownEffectProcessor : VideoEffectProcessorB
|
||||
{
|
||||
_effect?.SetInput(0, null, true);
|
||||
_effect?.SetInput(1, null, true);
|
||||
_outputTransform?.SetInput(0, null, true);
|
||||
_outputCrop?.SetInput(0, null, true);
|
||||
_isFirst = true;
|
||||
_hasOutput = false;
|
||||
_hasOutputOffset = false;
|
||||
_hasCropRect = false;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
|
||||
@@ -26,8 +26,10 @@ internal sealed class DielectricBreakdownGpuInterop : IDisposable
|
||||
private ID3D11Texture2D? _outputD3D11Texture;
|
||||
private ID2D1Bitmap1? _sourceBitmap;
|
||||
private ID2D1Bitmap1? _outputBitmap;
|
||||
private int _width;
|
||||
private int _height;
|
||||
private int _sourceWidth;
|
||||
private int _sourceHeight;
|
||||
private int _outputWidth;
|
||||
private int _outputHeight;
|
||||
private ulong _fenceValue;
|
||||
private bool _computeActive;
|
||||
private bool _disposed;
|
||||
@@ -56,8 +58,11 @@ internal sealed class DielectricBreakdownGpuInterop : IDisposable
|
||||
|
||||
public ID2D1Bitmap1 OutputBitmap => _outputBitmap!;
|
||||
|
||||
public bool MatchesSize(int width, int height)
|
||||
=> _sourceTexture is not null && _width == width && _height == height;
|
||||
public bool SourceMatches(int width, int height)
|
||||
=> _sourceTexture is not null && _sourceWidth == width && _sourceHeight == height;
|
||||
|
||||
public bool OutputCovers(int width, int height)
|
||||
=> _outputTexture is not null && _outputWidth >= width && _outputHeight >= height;
|
||||
|
||||
public static unsafe DielectricBreakdownGpuInterop? TryCreate(IGraphicsDevicesAndContext devices)
|
||||
{
|
||||
@@ -98,13 +103,34 @@ internal sealed class DielectricBreakdownGpuInterop : IDisposable
|
||||
|
||||
public bool EnsureResources(int width, int height)
|
||||
{
|
||||
if (MatchesSize(width, height))
|
||||
var sourceChanged = EnsureSource(width, height);
|
||||
var outputChanged = EnsureOutput(width, height);
|
||||
return sourceChanged || outputChanged;
|
||||
}
|
||||
|
||||
public bool EnsureSource(int width, int height)
|
||||
{
|
||||
if (SourceMatches(width, height))
|
||||
return false;
|
||||
|
||||
if (_sourceTexture is not null)
|
||||
WaitForIdle();
|
||||
ReleaseResources();
|
||||
CreateResources(width, height);
|
||||
ReleaseSourceResources();
|
||||
CreateSourceResources(width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool EnsureOutput(int width, int height)
|
||||
{
|
||||
if (OutputCovers(width, height))
|
||||
return false;
|
||||
|
||||
var capacityWidth = Math.Max(width, _outputWidth);
|
||||
var capacityHeight = Math.Max(height, _outputHeight);
|
||||
if (_outputTexture is not null)
|
||||
WaitForIdle();
|
||||
ReleaseOutputResources();
|
||||
CreateOutputResources(capacityWidth, capacityHeight);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -168,82 +194,102 @@ internal sealed class DielectricBreakdownGpuInterop : IDisposable
|
||||
return enumerator.Current;
|
||||
}
|
||||
|
||||
private void CreateResources(int width, int height)
|
||||
private void CreateSourceResources(int width, int height)
|
||||
{
|
||||
ReadWriteTexture2D<Bgra32, Float4>? sourceTexture = null;
|
||||
ReadWriteTexture2D<Bgra32, Float4>? outputTexture = null;
|
||||
ID3D11Texture2D? sourceD3D11Texture = null;
|
||||
ID3D11Texture2D? outputD3D11Texture = null;
|
||||
ID2D1Bitmap1? sourceBitmap = null;
|
||||
ID2D1Bitmap1? outputBitmap = null;
|
||||
nint sourceHandle = 0;
|
||||
nint outputHandle = 0;
|
||||
try
|
||||
{
|
||||
sourceTexture = InteropServices.AllocateSharedReadWriteTexture2D<Bgra32, Float4>(_device, width, height);
|
||||
outputTexture = InteropServices.AllocateSharedReadWriteTexture2D<Bgra32, Float4>(_device, width, height);
|
||||
sourceHandle = InteropServices.CreateSharedHandle(sourceTexture);
|
||||
outputHandle = InteropServices.CreateSharedHandle(outputTexture);
|
||||
sourceD3D11Texture = _d3dDevice.OpenSharedResource1<ID3D11Texture2D>(sourceHandle);
|
||||
outputD3D11Texture = _d3dDevice.OpenSharedResource1<ID3D11Texture2D>(outputHandle);
|
||||
using var sourceSurface = sourceD3D11Texture.QueryInterface<IDXGISurface>();
|
||||
using var outputSurface = outputD3D11Texture.QueryInterface<IDXGISurface>();
|
||||
var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied);
|
||||
sourceBitmap = _renderContext.CreateBitmapFromDxgiSurface(
|
||||
sourceSurface,
|
||||
new BitmapProperties1(pixelFormat, 96f, 96f, BitmapOptions.Target));
|
||||
outputBitmap = _renderContext.CreateBitmapFromDxgiSurface(
|
||||
outputSurface,
|
||||
new BitmapProperties1(pixelFormat, 96f, 96f, BitmapOptions.None));
|
||||
_renderContext.Target = sourceBitmap;
|
||||
|
||||
_sourceTexture = sourceTexture;
|
||||
_outputTexture = outputTexture;
|
||||
_sourceD3D11Texture = sourceD3D11Texture;
|
||||
_outputD3D11Texture = outputD3D11Texture;
|
||||
_sourceBitmap = sourceBitmap;
|
||||
_outputBitmap = outputBitmap;
|
||||
_width = width;
|
||||
_height = height;
|
||||
_sourceWidth = width;
|
||||
_sourceHeight = height;
|
||||
sourceTexture = null;
|
||||
outputTexture = null;
|
||||
sourceD3D11Texture = null;
|
||||
outputD3D11Texture = null;
|
||||
sourceBitmap = null;
|
||||
outputBitmap = null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sourceHandle != 0)
|
||||
CloseHandle(sourceHandle);
|
||||
if (outputHandle != 0)
|
||||
CloseHandle(outputHandle);
|
||||
outputBitmap?.Dispose();
|
||||
sourceBitmap?.Dispose();
|
||||
outputD3D11Texture?.Dispose();
|
||||
sourceD3D11Texture?.Dispose();
|
||||
outputTexture?.Dispose();
|
||||
sourceTexture?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseResources()
|
||||
private void CreateOutputResources(int width, int height)
|
||||
{
|
||||
ReadWriteTexture2D<Bgra32, Float4>? outputTexture = null;
|
||||
ID3D11Texture2D? outputD3D11Texture = null;
|
||||
ID2D1Bitmap1? outputBitmap = null;
|
||||
nint outputHandle = 0;
|
||||
try
|
||||
{
|
||||
outputTexture = InteropServices.AllocateSharedReadWriteTexture2D<Bgra32, Float4>(_device, width, height);
|
||||
outputHandle = InteropServices.CreateSharedHandle(outputTexture);
|
||||
outputD3D11Texture = _d3dDevice.OpenSharedResource1<ID3D11Texture2D>(outputHandle);
|
||||
using var outputSurface = outputD3D11Texture.QueryInterface<IDXGISurface>();
|
||||
var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied);
|
||||
outputBitmap = _renderContext.CreateBitmapFromDxgiSurface(
|
||||
outputSurface,
|
||||
new BitmapProperties1(pixelFormat, 96f, 96f, BitmapOptions.None));
|
||||
|
||||
_outputTexture = outputTexture;
|
||||
_outputD3D11Texture = outputD3D11Texture;
|
||||
_outputBitmap = outputBitmap;
|
||||
_outputWidth = width;
|
||||
_outputHeight = height;
|
||||
outputTexture = null;
|
||||
outputD3D11Texture = null;
|
||||
outputBitmap = null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (outputHandle != 0)
|
||||
CloseHandle(outputHandle);
|
||||
outputBitmap?.Dispose();
|
||||
outputD3D11Texture?.Dispose();
|
||||
outputTexture?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseSourceResources()
|
||||
{
|
||||
_renderContext.Target = null;
|
||||
_outputBitmap?.Dispose();
|
||||
_sourceBitmap?.Dispose();
|
||||
_outputD3D11Texture?.Dispose();
|
||||
_sourceD3D11Texture?.Dispose();
|
||||
_outputTexture?.Dispose();
|
||||
_sourceTexture?.Dispose();
|
||||
_outputBitmap = null;
|
||||
_sourceBitmap = null;
|
||||
_outputD3D11Texture = null;
|
||||
_sourceD3D11Texture = null;
|
||||
_outputTexture = null;
|
||||
_sourceTexture = null;
|
||||
_width = 0;
|
||||
_height = 0;
|
||||
_sourceWidth = 0;
|
||||
_sourceHeight = 0;
|
||||
}
|
||||
|
||||
private void ReleaseOutputResources()
|
||||
{
|
||||
_outputBitmap?.Dispose();
|
||||
_outputD3D11Texture?.Dispose();
|
||||
_outputTexture?.Dispose();
|
||||
_outputBitmap = null;
|
||||
_outputD3D11Texture = null;
|
||||
_outputTexture = null;
|
||||
_outputWidth = 0;
|
||||
_outputHeight = 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@@ -257,7 +303,8 @@ internal sealed class DielectricBreakdownGpuInterop : IDisposable
|
||||
finally
|
||||
{
|
||||
_disposed = true;
|
||||
ReleaseResources();
|
||||
ReleaseOutputResources();
|
||||
ReleaseSourceResources();
|
||||
_renderContext.Dispose();
|
||||
_fence.Dispose();
|
||||
_d3dContext.Dispose();
|
||||
|
||||
@@ -72,8 +72,10 @@ internal readonly partial struct InitScratchShader(
|
||||
internal readonly partial struct SilhouetteShader(
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
ReadWriteBuffer<int> mask,
|
||||
int width,
|
||||
int height,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
float cellSize,
|
||||
@@ -81,8 +83,10 @@ internal readonly partial struct SilhouetteShader(
|
||||
{
|
||||
private readonly ReadWriteTexture2D<Bgra32, Float4> source = source;
|
||||
private readonly ReadWriteBuffer<int> mask = mask;
|
||||
private readonly int width = width;
|
||||
private readonly int height = height;
|
||||
private readonly int sourceOffsetX = sourceOffsetX;
|
||||
private readonly int sourceOffsetY = sourceOffsetY;
|
||||
private readonly int sourceWidth = sourceWidth;
|
||||
private readonly int sourceHeight = sourceHeight;
|
||||
private readonly int gridWidth = gridWidth;
|
||||
private readonly int gridHeight = gridHeight;
|
||||
private readonly float cellSize = cellSize;
|
||||
@@ -95,17 +99,17 @@ internal readonly partial struct SilhouetteShader(
|
||||
if (gx >= gridWidth || gy >= gridHeight)
|
||||
return;
|
||||
|
||||
var x0 = Hlsl.Max((int)(gx * cellSize), 0);
|
||||
var x1 = Hlsl.Min((int)Hlsl.Ceil((gx + 1) * cellSize), width);
|
||||
var y0 = Hlsl.Max((int)(gy * cellSize), 0);
|
||||
var y1 = Hlsl.Min((int)Hlsl.Ceil((gy + 1) * cellSize), height);
|
||||
var x0 = Hlsl.Max((int)(gx * cellSize), sourceOffsetX);
|
||||
var x1 = Hlsl.Min((int)Hlsl.Ceil((gx + 1) * cellSize), sourceOffsetX + sourceWidth);
|
||||
var y0 = Hlsl.Max((int)(gy * cellSize), sourceOffsetY);
|
||||
var y1 = Hlsl.Min((int)Hlsl.Ceil((gy + 1) * cellSize), sourceOffsetY + sourceHeight);
|
||||
|
||||
var found = 0;
|
||||
for (var y = y0; y < y1 && found == 0; y++)
|
||||
{
|
||||
for (var x = x0; x < x1; x++)
|
||||
{
|
||||
if (source[new Int2(x, y)].W > alphaThreshold)
|
||||
if (source[new Int2(x - sourceOffsetX, y - sourceOffsetY)].W > alphaThreshold)
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
@@ -318,6 +322,7 @@ internal readonly partial struct GrowthShader(
|
||||
ReadWriteBuffer<int> candidates,
|
||||
ReadWriteBuffer<int> candidateFlags,
|
||||
ReadWriteBuffer<int> growthLog,
|
||||
ReadWriteBuffer<int> parentLog,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
int maxSteps,
|
||||
@@ -344,6 +349,7 @@ internal readonly partial struct GrowthShader(
|
||||
private readonly ReadWriteBuffer<int> candidates = candidates;
|
||||
private readonly ReadWriteBuffer<int> candidateFlags = candidateFlags;
|
||||
private readonly ReadWriteBuffer<int> growthLog = growthLog;
|
||||
private readonly ReadWriteBuffer<int> parentLog = parentLog;
|
||||
private readonly int gridWidth = gridWidth;
|
||||
private readonly int gridHeight = gridHeight;
|
||||
private readonly int maxSteps = maxSteps;
|
||||
@@ -508,8 +514,10 @@ internal readonly partial struct GrowthShader(
|
||||
state[chosen] = 1;
|
||||
birth[chosen] = step + 1;
|
||||
var packed = reduction[4];
|
||||
parent[chosen] = packed >= 0 ? 65535 - (packed & 131071) : chosen;
|
||||
var parentCell = packed >= 0 ? 65535 - (packed & 131071) : chosen;
|
||||
parent[chosen] = parentCell;
|
||||
growthLog[step] = chosen;
|
||||
parentLog[step] = parentCell;
|
||||
scratch[0] = chargeCount + 1;
|
||||
scratch[4] = step + 1;
|
||||
var projection = (chosenX + 0.5f) * directionX + (chosenY + 0.5f) * directionY;
|
||||
@@ -539,6 +547,7 @@ internal readonly partial struct GrowthShader(
|
||||
else if (thread == 0)
|
||||
{
|
||||
growthLog[step] = -1;
|
||||
parentLog[step] = -1;
|
||||
}
|
||||
Hlsl.DeviceMemoryBarrierWithGroupSync();
|
||||
}
|
||||
@@ -776,6 +785,8 @@ internal readonly partial struct GlowDepositShader(
|
||||
ReadWriteBuffer<int> glowAccum,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
int glowOffsetX,
|
||||
int glowOffsetY,
|
||||
int glowWidth,
|
||||
int glowHeight,
|
||||
int sentinel,
|
||||
@@ -795,6 +806,8 @@ internal readonly partial struct GlowDepositShader(
|
||||
private readonly ReadWriteBuffer<int> glowAccum = glowAccum;
|
||||
private readonly int gridWidth = gridWidth;
|
||||
private readonly int gridHeight = gridHeight;
|
||||
private readonly int glowOffsetX = glowOffsetX;
|
||||
private readonly int glowOffsetY = glowOffsetY;
|
||||
private readonly int glowWidth = glowWidth;
|
||||
private readonly int glowHeight = glowHeight;
|
||||
private readonly int sentinel = sentinel;
|
||||
@@ -849,8 +862,8 @@ internal readonly partial struct GlowDepositShader(
|
||||
for (var i = 0; i < samples; i++)
|
||||
{
|
||||
var fraction = (i + 0.5f) / samples;
|
||||
var sx = (int)((px + segmentX * fraction) * 0.25f);
|
||||
var sy = (int)((py + segmentY * fraction) * 0.25f);
|
||||
var sx = (int)((px + segmentX * fraction) * 0.25f) - glowOffsetX;
|
||||
var sy = (int)((py + segmentY * fraction) * 0.25f) - glowOffsetY;
|
||||
if (sx < 0 || sx >= glowWidth || sy < 0 || sy >= glowHeight)
|
||||
continue;
|
||||
Hlsl.InterlockedAdd(ref glowAccum[sy * glowWidth + sx], deposit);
|
||||
@@ -941,10 +954,14 @@ internal readonly partial struct RenderShader(
|
||||
ReadWriteBuffer<int> scratch,
|
||||
ReadWriteBuffer<float> glowMap,
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
int width,
|
||||
int height,
|
||||
int rectOffsetX,
|
||||
int rectOffsetY,
|
||||
int rectWidth,
|
||||
int rectHeight,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
int glowOffsetX,
|
||||
int glowOffsetY,
|
||||
int glowWidth,
|
||||
int glowHeight,
|
||||
int sentinel,
|
||||
@@ -969,10 +986,14 @@ internal readonly partial struct RenderShader(
|
||||
private readonly ReadWriteBuffer<int> scratch = scratch;
|
||||
private readonly ReadWriteBuffer<float> glowMap = glowMap;
|
||||
private readonly ReadWriteTexture2D<Bgra32, Float4> output = output;
|
||||
private readonly int width = width;
|
||||
private readonly int height = height;
|
||||
private readonly int rectOffsetX = rectOffsetX;
|
||||
private readonly int rectOffsetY = rectOffsetY;
|
||||
private readonly int rectWidth = rectWidth;
|
||||
private readonly int rectHeight = rectHeight;
|
||||
private readonly int gridWidth = gridWidth;
|
||||
private readonly int gridHeight = gridHeight;
|
||||
private readonly int glowOffsetX = glowOffsetX;
|
||||
private readonly int glowOffsetY = glowOffsetY;
|
||||
private readonly int glowWidth = glowWidth;
|
||||
private readonly int glowHeight = glowHeight;
|
||||
private readonly int sentinel = sentinel;
|
||||
@@ -990,10 +1011,10 @@ internal readonly partial struct RenderShader(
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
var x = ThreadIds.X;
|
||||
var y = ThreadIds.Y;
|
||||
if (x >= width || y >= height)
|
||||
if (ThreadIds.X >= rectWidth || ThreadIds.Y >= rectHeight)
|
||||
return;
|
||||
var x = ThreadIds.X + rectOffsetX;
|
||||
var y = ThreadIds.Y + rectOffsetY;
|
||||
|
||||
var cell = FindNearestSite(x, y);
|
||||
var bestEnergy = 0f;
|
||||
@@ -1115,8 +1136,8 @@ internal readonly partial struct RenderShader(
|
||||
|
||||
private float SampleGlow(int x, int y)
|
||||
{
|
||||
var fx = Hlsl.Clamp(x * 0.25f - 0.5f, 0f, glowWidth - 1f);
|
||||
var fy = Hlsl.Clamp(y * 0.25f - 0.5f, 0f, glowHeight - 1f);
|
||||
var fx = Hlsl.Clamp(x * 0.25f - 0.5f - glowOffsetX, 0f, glowWidth - 1f);
|
||||
var fy = Hlsl.Clamp(y * 0.25f - 0.5f - glowOffsetY, 0f, glowHeight - 1f);
|
||||
var ix0 = (int)fx;
|
||||
var iy0 = (int)fy;
|
||||
var wx = fx - ix0;
|
||||
|
||||
@@ -15,6 +15,10 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
private ReadWriteBuffer<int>? _candidates;
|
||||
private ReadWriteBuffer<int>? _candidateFlags;
|
||||
private readonly ReadWriteBuffer<int> _growthLog;
|
||||
private readonly ReadWriteBuffer<int> _parentLog;
|
||||
private readonly ReadBackBuffer<int> _scratchReadBack;
|
||||
private readonly ReadBackBuffer<int> _growthLogReadBack;
|
||||
private readonly ReadBackBuffer<int> _parentLogReadBack;
|
||||
private ReadWriteBuffer<int>? _rowCounts;
|
||||
private ReadWriteBuffer<int>? _rowOffsets;
|
||||
private ReadWriteBuffer<int>? _potential;
|
||||
@@ -38,6 +42,10 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
_device = device;
|
||||
_scratch = device.AllocateReadWriteBuffer<int>(DielectricBreakdownSettings.ScratchLength);
|
||||
_growthLog = device.AllocateReadWriteBuffer<int>(DielectricBreakdownSettings.MaximumStepCount);
|
||||
_parentLog = device.AllocateReadWriteBuffer<int>(DielectricBreakdownSettings.MaximumStepCount);
|
||||
_scratchReadBack = device.AllocateReadBackBuffer<int>(DielectricBreakdownSettings.ScratchLength);
|
||||
_growthLogReadBack = device.AllocateReadBackBuffer<int>(DielectricBreakdownSettings.MaximumStepCount);
|
||||
_parentLogReadBack = device.AllocateReadBackBuffer<int>(DielectricBreakdownSettings.MaximumStepCount);
|
||||
}
|
||||
|
||||
public static DielectricBreakdownPipeline? TryCreate()
|
||||
@@ -72,13 +80,14 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
public void Process(ReadOnlySpan<int> source, Span<int> destination, int width, int height, in Parameters parameters)
|
||||
{
|
||||
var pixelCount = checked(width * height);
|
||||
EnsureResources(width, height, parameters.Quality);
|
||||
EnsureGridFor(width, height, parameters.Quality);
|
||||
EnsureGlow(pixelCount);
|
||||
EnsurePackedTextures(width, height);
|
||||
var sourceTexture = _packedSource!;
|
||||
var outputTexture = _packedOutput!;
|
||||
sourceTexture.CopyFrom(MemoryMarshal.Cast<int, Bgra32>(source[..pixelCount]));
|
||||
using (ComputeContext context = _device.CreateComputeContext())
|
||||
RecordPipeline(in context, sourceTexture, outputTexture, width, height, in parameters);
|
||||
RecordFullPipeline(in context, sourceTexture, outputTexture, width, height, in parameters);
|
||||
outputTexture.CopyTo(MemoryMarshal.Cast<int, Bgra32>(destination[..pixelCount]));
|
||||
}
|
||||
|
||||
@@ -89,9 +98,10 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
int height,
|
||||
in Parameters parameters)
|
||||
{
|
||||
EnsureResources(width, height, parameters.Quality);
|
||||
EnsureGridFor(width, height, parameters.Quality);
|
||||
EnsureGlow(checked(width * height));
|
||||
using ComputeContext context = _device.CreateComputeContext();
|
||||
RecordPipeline(in context, source, destination, width, height, in parameters);
|
||||
RecordFullPipeline(in context, source, destination, width, height, in parameters);
|
||||
context.Submit();
|
||||
}
|
||||
|
||||
@@ -102,12 +112,90 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
int height,
|
||||
in Parameters parameters)
|
||||
{
|
||||
EnsureResources(width, height, parameters.Quality);
|
||||
EnsureGridFor(width, height, parameters.Quality);
|
||||
EnsureGlow(checked(width * height));
|
||||
using ComputeContext context = _device.CreateComputeContext();
|
||||
RecordPipeline(in context, source, destination, width, height, in parameters);
|
||||
RecordFullPipeline(in context, source, destination, width, height, in parameters);
|
||||
}
|
||||
|
||||
private void RecordPipeline(
|
||||
internal void Simulate(
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
int canvasWidth,
|
||||
int canvasHeight,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
in Parameters parameters)
|
||||
{
|
||||
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);
|
||||
_scratchReadBack.CopyFrom(_scratch);
|
||||
_growthLogReadBack.CopyFrom(_growthLog);
|
||||
_parentLogReadBack.CopyFrom(_parentLog);
|
||||
}
|
||||
|
||||
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 visible = parameters.Growth * total;
|
||||
if (total <= 0 || visible <= 0f)
|
||||
return false;
|
||||
|
||||
var growthLog = _growthLogReadBack.Span;
|
||||
var parentLog = _parentLogReadBack.Span;
|
||||
var minX = int.MaxValue;
|
||||
var minY = int.MaxValue;
|
||||
var maxX = int.MinValue;
|
||||
var maxY = int.MinValue;
|
||||
for (var step = 0; step < total && step < visible; step++)
|
||||
{
|
||||
var cell = growthLog[step];
|
||||
if (cell < 0)
|
||||
continue;
|
||||
var parent = parentLog[step];
|
||||
minX = Math.Min(minX, Math.Min(cell % _gridWidth, parent % _gridWidth));
|
||||
maxX = Math.Max(maxX, Math.Max(cell % _gridWidth, parent % _gridWidth));
|
||||
minY = Math.Min(minY, Math.Min(cell / _gridWidth, parent / _gridWidth));
|
||||
maxY = Math.Max(maxY, Math.Max(cell / _gridWidth, parent / _gridWidth));
|
||||
}
|
||||
if (minX == int.MaxValue)
|
||||
return false;
|
||||
|
||||
var padding = (int)MathF.Ceiling(derived.Thickness) + (derived.GlowRadius + 2) * 4;
|
||||
var left = Math.Clamp(((int)(minX * derived.CellSize) - padding) & ~3, 0, canvasWidth);
|
||||
var top = Math.Clamp(((int)(minY * derived.CellSize) - padding) & ~3, 0, canvasHeight);
|
||||
var right = Math.Clamp((int)MathF.Ceiling((maxX + 1) * derived.CellSize) + padding, 0, canvasWidth);
|
||||
var bottom = Math.Clamp((int)MathF.Ceiling((maxY + 1) * derived.CellSize) + padding, 0, canvasHeight);
|
||||
var width = Math.Min((right - left + 3) & ~3, canvasWidth - left);
|
||||
var height = Math.Min((bottom - top + 3) & ~3, canvasHeight - top);
|
||||
if (width <= 0 || height <= 0)
|
||||
return false;
|
||||
|
||||
rect = new PixelRect(left, top, width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
internal void RenderVisible(
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
int canvasWidth,
|
||||
int canvasHeight,
|
||||
PixelRect rect,
|
||||
in Parameters parameters)
|
||||
{
|
||||
EnsureGlow(checked(rect.Width * rect.Height));
|
||||
var derived = Derive(canvasWidth, canvasHeight, in parameters);
|
||||
using ComputeContext context = _device.CreateComputeContext();
|
||||
RecordRenderStage(in context, output, rect, in derived, in parameters);
|
||||
}
|
||||
|
||||
private void RecordFullPipeline(
|
||||
in ComputeContext context,
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
@@ -115,55 +203,40 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
int height,
|
||||
in Parameters parameters)
|
||||
{
|
||||
var settings = DielectricBreakdownSettings.GetQuality(parameters.Quality);
|
||||
var derived = Derive(width, height, in parameters);
|
||||
RecordGrowthStage(in context, source, 0, 0, width, height, in derived, in parameters);
|
||||
RecordRenderStage(in context, output, new PixelRect(0, 0, width, height), in derived, in parameters);
|
||||
}
|
||||
|
||||
private void RecordGrowthStage(
|
||||
in ComputeContext context,
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
in DerivedValues derived,
|
||||
in Parameters parameters)
|
||||
{
|
||||
var gridWidth = _gridWidth;
|
||||
var gridHeight = _gridHeight;
|
||||
var (_, _, cellSize) = DielectricBreakdownSettings.GetGridSize(width, height, settings.GridResolution);
|
||||
var reachCellCount = Math.Max(parameters.ReachPixels, 1f) / cellSize;
|
||||
var maxSteps = DielectricBreakdownSettings.GetStepCount(reachCellCount, settings.MaxSteps);
|
||||
var sentinel = maxSteps + 1;
|
||||
var maxWalk = maxSteps + 4;
|
||||
|
||||
var radians = parameters.AngleDegrees * (MathF.PI / 180f);
|
||||
var directionX = MathF.Sin(radians);
|
||||
var directionY = MathF.Cos(radians);
|
||||
var projectionMin = MathF.Min(0f, gridWidth * directionX) + MathF.Min(0f, gridHeight * directionY);
|
||||
var projectionMax = MathF.Max(0f, gridWidth * directionX) + MathF.Max(0f, gridHeight * directionY);
|
||||
var projectionInverseRange = 1f / MathF.Max(projectionMax - projectionMin, 1e-4f);
|
||||
var eta = DielectricBreakdownSettings.GetEta(parameters.Branching);
|
||||
var reachQuantized = (int)(reachCellCount * DielectricBreakdownSettings.ProjectionQuantScale);
|
||||
var thickness = Math.Max(parameters.Thickness, 0.05f);
|
||||
var glowWidth = (width + 3) / 4;
|
||||
var glowHeight = (height + 3) / 4;
|
||||
var glowRadius = Math.Clamp((int)((2f + parameters.Glow * 58f) * 0.25f), 1, 16);
|
||||
var glowStrength = parameters.Glow * 0.27f * (glowRadius + 1);
|
||||
var flashTau = MathF.Max(DielectricBreakdownSettings.FlashTauFraction * maxSteps, 1f);
|
||||
var tipTau = MathF.Max(0.03f * maxSteps, 1f);
|
||||
|
||||
var mask = _mask!;
|
||||
var state = _state!;
|
||||
var birth = _birth!;
|
||||
var parent = _parent!;
|
||||
var mainFlag = _mainFlag!;
|
||||
var charges = _charges!;
|
||||
var candidates = _candidates!;
|
||||
var candidateFlags = _candidateFlags!;
|
||||
var rowCounts = _rowCounts!;
|
||||
var rowOffsets = _rowOffsets!;
|
||||
var potential = _potential!;
|
||||
var intensity = _intensity!;
|
||||
var jumpFloodA = _jumpFloodA!;
|
||||
var jumpFloodB = _jumpFloodB!;
|
||||
var glowAccum = _glowAccum!;
|
||||
var glowTemp = _glowTemp!;
|
||||
var glowMap = _glowMap!;
|
||||
|
||||
context.For(1, new InitScratchShader(_scratch, sentinel));
|
||||
context.For(gridWidth, gridHeight, new FillIntPlaneShader(mainFlag, gridWidth, gridHeight, 0));
|
||||
context.For(gridWidth, gridHeight, new SilhouetteShader(source, mask, width, height, gridWidth, gridHeight, cellSize, 0.05f));
|
||||
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, directionX, directionY, DielectricBreakdownSettings.ProjectionQuantScale));
|
||||
context.For(gridWidth, gridHeight, new ElectrodeInitShader(mask, state, birth, parent, _scratch, gridWidth, gridHeight, derived.DirectionX, derived.DirectionY, DielectricBreakdownSettings.ProjectionQuantScale));
|
||||
context.Barrier(state);
|
||||
context.For(gridHeight, new ChargeRowCountShader(state, rowCounts, gridWidth, gridHeight));
|
||||
context.Barrier(rowCounts);
|
||||
@@ -176,20 +249,49 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
context.Barrier(potential);
|
||||
|
||||
context.For(DielectricBreakdownSettings.GrowthThreadCount, new GrowthShader(
|
||||
state, birth, parent, potential, _scratch, candidates, candidateFlags, _growthLog,
|
||||
gridWidth, gridHeight, maxSteps, sentinel, parameters.Seed, reachQuantized, eta,
|
||||
state, birth, parent, potential, _scratch, candidates, candidateFlags, _growthLog, _parentLog,
|
||||
gridWidth, gridHeight, derived.MaxSteps, derived.Sentinel, parameters.Seed, derived.ReachQuantized, derived.Eta,
|
||||
DielectricBreakdownSettings.ChargeRadius, DielectricBreakdownSettings.FieldBias,
|
||||
directionX, directionY, projectionMin, projectionInverseRange, DielectricBreakdownSettings.ProjectionQuantScale));
|
||||
derived.DirectionX, derived.DirectionY, derived.ProjectionMin, derived.ProjectionInverseRange, DielectricBreakdownSettings.ProjectionQuantScale));
|
||||
context.Barrier(state);
|
||||
context.Barrier(birth);
|
||||
context.Barrier(parent);
|
||||
context.Barrier(potential);
|
||||
context.Barrier(_scratch);
|
||||
context.Barrier(_growthLog);
|
||||
context.Barrier(_parentLog);
|
||||
}
|
||||
|
||||
context.For(1, new MainChannelShader(parent, birth, mainFlag, _scratch, sentinel, maxWalk));
|
||||
private void RecordRenderStage(
|
||||
in ComputeContext context,
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
PixelRect rect,
|
||||
in DerivedValues derived,
|
||||
in Parameters parameters)
|
||||
{
|
||||
var gridWidth = _gridWidth;
|
||||
var gridHeight = _gridHeight;
|
||||
var state = _state!;
|
||||
var birth = _birth!;
|
||||
var parent = _parent!;
|
||||
var mainFlag = _mainFlag!;
|
||||
var intensity = _intensity!;
|
||||
var jumpFloodA = _jumpFloodA!;
|
||||
var jumpFloodB = _jumpFloodB!;
|
||||
var glowAccum = _glowAccum!;
|
||||
var glowTemp = _glowTemp!;
|
||||
var glowMap = _glowMap!;
|
||||
var glowOffsetX = rect.X / 4;
|
||||
var glowOffsetY = rect.Y / 4;
|
||||
var glowWidth = (rect.Width + 3) / 4;
|
||||
var glowHeight = (rect.Height + 3) / 4;
|
||||
|
||||
context.For(gridWidth, gridHeight, new FillIntPlaneShader(mainFlag, gridWidth, gridHeight, 0));
|
||||
context.Barrier(mainFlag);
|
||||
context.For(1, new MainChannelShader(parent, birth, mainFlag, _scratch, derived.Sentinel, derived.MaxWalk));
|
||||
context.Barrier(mainFlag);
|
||||
context.For(gridWidth, gridHeight, new IntensityShader(
|
||||
state, birth, parent, mainFlag, intensity, gridWidth, gridHeight, maxWalk,
|
||||
state, birth, parent, mainFlag, intensity, gridWidth, gridHeight, derived.MaxWalk,
|
||||
DielectricBreakdownSettings.LeaderIntensity, DielectricBreakdownSettings.SideIntensity, DielectricBreakdownSettings.SideDecayPerHop));
|
||||
context.Barrier(intensity);
|
||||
|
||||
@@ -197,17 +299,17 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
context.Barrier(glowAccum);
|
||||
context.For(gridWidth, gridHeight, new GlowDepositShader(
|
||||
state, birth, parent, mainFlag, intensity, _scratch, glowAccum,
|
||||
gridWidth, gridHeight, glowWidth, glowHeight, sentinel, cellSize, parameters.Growth,
|
||||
DielectricBreakdownSettings.TipBoost, 1f / tipTau,
|
||||
DielectricBreakdownSettings.FlashAmplitude, 1f / flashTau));
|
||||
gridWidth, gridHeight, glowOffsetX, glowOffsetY, glowWidth, glowHeight, derived.Sentinel, derived.CellSize, parameters.Growth,
|
||||
DielectricBreakdownSettings.TipBoost, derived.InverseTipTau,
|
||||
DielectricBreakdownSettings.FlashAmplitude, derived.InverseFlashTau));
|
||||
context.Barrier(glowAccum);
|
||||
context.For(glowWidth, glowHeight, new GlowBlurHorizontalShader(glowAccum, glowTemp, glowWidth, glowHeight, glowRadius));
|
||||
context.For(glowWidth, glowHeight, new GlowBlurHorizontalShader(glowAccum, glowTemp, glowWidth, glowHeight, derived.GlowRadius));
|
||||
context.Barrier(glowTemp);
|
||||
context.For(glowWidth, glowHeight, new GlowBlurVerticalShader(glowTemp, glowMap, glowWidth, glowHeight, glowRadius));
|
||||
context.For(glowWidth, glowHeight, new GlowBlurVerticalShader(glowTemp, glowMap, glowWidth, glowHeight, derived.GlowRadius));
|
||||
context.Barrier(glowMap);
|
||||
|
||||
context.For(gridWidth, gridHeight, new JumpFloodSeedShader(
|
||||
state, birth, _scratch, jumpFloodA, gridWidth, gridHeight, sentinel, parameters.Growth));
|
||||
state, birth, _scratch, jumpFloodA, gridWidth, gridHeight, derived.Sentinel, parameters.Growth));
|
||||
context.Barrier(jumpFloodA);
|
||||
|
||||
var reading = jumpFloodA;
|
||||
@@ -225,21 +327,51 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
stepSize >>= 1;
|
||||
}
|
||||
|
||||
context.For(width, height, new RenderShader(
|
||||
context.For(rect.Width, rect.Height, new RenderShader(
|
||||
reading, state, birth, parent, mainFlag, intensity, _scratch, glowMap, output,
|
||||
width, height, gridWidth, gridHeight, glowWidth, glowHeight, sentinel, cellSize, parameters.Growth,
|
||||
thickness, glowStrength,
|
||||
DielectricBreakdownSettings.TipBoost, 1f / tipTau,
|
||||
DielectricBreakdownSettings.FlashAmplitude, 1f / flashTau,
|
||||
rect.X, rect.Y, rect.Width, rect.Height, gridWidth, gridHeight,
|
||||
glowOffsetX, glowOffsetY, glowWidth, glowHeight, derived.Sentinel, derived.CellSize, parameters.Growth,
|
||||
derived.Thickness, derived.GlowStrength,
|
||||
DielectricBreakdownSettings.TipBoost, derived.InverseTipTau,
|
||||
DielectricBreakdownSettings.FlashAmplitude, derived.InverseFlashTau,
|
||||
parameters.ColorR, parameters.ColorG, parameters.ColorB));
|
||||
}
|
||||
|
||||
private void EnsureResources(int width, int height, DielectricBreakdownQuality quality)
|
||||
private static DerivedValues Derive(int width, int height, in Parameters parameters)
|
||||
{
|
||||
var settings = DielectricBreakdownSettings.GetQuality(parameters.Quality);
|
||||
var (gridWidth, gridHeight, cellSize) = DielectricBreakdownSettings.GetGridSize(width, height, settings.GridResolution);
|
||||
var reachCellCount = Math.Max(parameters.ReachPixels, 1f) / cellSize;
|
||||
var maxSteps = DielectricBreakdownSettings.GetStepCount(reachCellCount, settings.MaxSteps);
|
||||
var radians = parameters.AngleDegrees * (MathF.PI / 180f);
|
||||
var directionX = MathF.Sin(radians);
|
||||
var directionY = MathF.Cos(radians);
|
||||
var projectionMin = MathF.Min(0f, gridWidth * directionX) + MathF.Min(0f, gridHeight * directionY);
|
||||
var projectionMax = MathF.Max(0f, gridWidth * directionX) + MathF.Max(0f, gridHeight * directionY);
|
||||
var glowRadius = DielectricBreakdownSettings.GetGlowRadius(parameters.Glow);
|
||||
return new DerivedValues(
|
||||
cellSize,
|
||||
maxSteps,
|
||||
maxSteps + 1,
|
||||
maxSteps + 4,
|
||||
directionX,
|
||||
directionY,
|
||||
projectionMin,
|
||||
1f / MathF.Max(projectionMax - projectionMin, 1e-4f),
|
||||
DielectricBreakdownSettings.GetEta(parameters.Branching),
|
||||
(int)(reachCellCount * DielectricBreakdownSettings.ProjectionQuantScale),
|
||||
Math.Max(parameters.Thickness, 0.05f),
|
||||
glowRadius,
|
||||
parameters.Glow * 0.27f * (glowRadius + 1),
|
||||
1f / MathF.Max(0.03f * maxSteps, 1f),
|
||||
1f / MathF.Max(DielectricBreakdownSettings.FlashTauFraction * maxSteps, 1f));
|
||||
}
|
||||
|
||||
private void EnsureGridFor(int width, int height, DielectricBreakdownQuality quality)
|
||||
{
|
||||
var settings = DielectricBreakdownSettings.GetQuality(quality);
|
||||
var (gridWidth, gridHeight, _) = DielectricBreakdownSettings.GetGridSize(width, height, settings.GridResolution);
|
||||
EnsureGrid(gridWidth, gridHeight);
|
||||
EnsureGlow(checked(width * height));
|
||||
}
|
||||
|
||||
private void EnsureGrid(int gridWidth, int gridHeight)
|
||||
@@ -345,10 +477,33 @@ internal sealed class DielectricBreakdownPipeline : IDisposable
|
||||
_packedOutput = null;
|
||||
_packedWidth = 0;
|
||||
_packedHeight = 0;
|
||||
_scratchReadBack.Dispose();
|
||||
_growthLogReadBack.Dispose();
|
||||
_parentLogReadBack.Dispose();
|
||||
_growthLog.Dispose();
|
||||
_parentLog.Dispose();
|
||||
_scratch.Dispose();
|
||||
}
|
||||
|
||||
internal readonly record struct PixelRect(int X, int Y, int Width, int Height);
|
||||
|
||||
private readonly record struct DerivedValues(
|
||||
float CellSize,
|
||||
int MaxSteps,
|
||||
int Sentinel,
|
||||
int MaxWalk,
|
||||
float DirectionX,
|
||||
float DirectionY,
|
||||
float ProjectionMin,
|
||||
float ProjectionInverseRange,
|
||||
float Eta,
|
||||
int ReachQuantized,
|
||||
float Thickness,
|
||||
int GlowRadius,
|
||||
float GlowStrength,
|
||||
float InverseTipTau,
|
||||
float InverseFlashTau);
|
||||
|
||||
internal readonly record struct Parameters(
|
||||
DielectricBreakdownQuality Quality,
|
||||
float Growth,
|
||||
|
||||
@@ -46,6 +46,9 @@ internal static class DielectricBreakdownSettings
|
||||
public static int GetStepCount(float reachCells, int maxSteps)
|
||||
=> Math.Clamp((int)(reachCells * 10f) + 96, 128, maxSteps);
|
||||
|
||||
public static int GetGlowRadius(float glow)
|
||||
=> Math.Clamp((int)((2f + glow * 58f) * 0.25f), 1, 16);
|
||||
|
||||
public static float GetEta(float branching)
|
||||
=> MaximumEta - Math.Clamp(branching, 0f, 1f) * (MaximumEta - MinimumEta);
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする