Lower texture spline fields into reservoirs
This commit is contained in:
parent
7bb12cc103
commit
ea208eebed
5 changed files with 587 additions and 11 deletions
|
|
@ -0,0 +1,242 @@
|
|||
using System.Globalization;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Aquarium.Engine.Render;
|
||||
|
||||
public static class AquariumFieldScriptCompiler
|
||||
{
|
||||
public static AquariumBufferFieldFrame Compile(
|
||||
string source,
|
||||
IReadOnlyDictionary<string, AquariumTextureFieldBinding> textureBindings,
|
||||
float reservoirRadius = 1.0f)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(source);
|
||||
ArgumentNullException.ThrowIfNull(textureBindings);
|
||||
|
||||
AquariumFractalReservoirField reservoir = AquariumFractalReservoirField.Empty;
|
||||
var textures = new List<AquariumTextureFieldBinding>();
|
||||
var programs = new List<AquariumTextureSplineFieldProgram>();
|
||||
var nodes = new List<AquariumFieldGraphNode>();
|
||||
|
||||
var lines = source.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n');
|
||||
for (var lineIndex = 0; lineIndex < lines.Length; lineIndex++)
|
||||
{
|
||||
var line = StripComment(lines[lineIndex]).Trim();
|
||||
if (line.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var tokens = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
var args = ParseArgs(tokens.Skip(1), lineIndex);
|
||||
switch (tokens[0])
|
||||
{
|
||||
case "texture":
|
||||
var textureId = Required(args, "id", lineIndex);
|
||||
if (!textureBindings.TryGetValue(textureId, out var texture))
|
||||
{
|
||||
throw new FormatException($"Unknown texture binding `{textureId}` at line {lineIndex + 1}.");
|
||||
}
|
||||
|
||||
textures.Add(texture);
|
||||
break;
|
||||
case "surface":
|
||||
nodes.Add(ParseSurfaceNode(args, lineIndex));
|
||||
break;
|
||||
case "splinefield":
|
||||
programs.Add(ParseSplineField(args, nodes, lineIndex));
|
||||
break;
|
||||
case "reservoir":
|
||||
reservoir = ParseReservoir(args, reservoirRadius, lineIndex);
|
||||
break;
|
||||
default:
|
||||
throw new FormatException($"Unknown field DSL command `{tokens[0]}` at line {lineIndex + 1}.");
|
||||
}
|
||||
}
|
||||
|
||||
if (textures.Count == 0)
|
||||
{
|
||||
throw new FormatException("Field DSL must bind at least one `texture id=...`.");
|
||||
}
|
||||
|
||||
if (programs.Count == 0)
|
||||
{
|
||||
throw new FormatException("Field DSL must declare at least one `splinefield`.");
|
||||
}
|
||||
|
||||
if (!reservoir.HasInput)
|
||||
{
|
||||
var splatCount = programs.Sum(program => Math.Max(1, program.ProbePolicy.MaxProbeCount));
|
||||
reservoir = DefaultReservoir(splatCount, reservoirRadius);
|
||||
}
|
||||
|
||||
return new AquariumBufferFieldFrame
|
||||
{
|
||||
Textures = textures,
|
||||
TextureSplineFields = programs,
|
||||
Reservoir = reservoir,
|
||||
SourceScript = source,
|
||||
};
|
||||
}
|
||||
|
||||
private static AquariumTextureSplineFieldProgram ParseSplineField(
|
||||
IReadOnlyDictionary<string, string> args,
|
||||
IReadOnlyList<AquariumFieldGraphNode> surfaceNodes,
|
||||
int lineIndex)
|
||||
{
|
||||
var axis = Enum.Parse<AquariumTextureAxis>(Required(args, "frequencyAxis", lineIndex), ignoreCase: true);
|
||||
var radius = Float(args, "radius", 0.016f, lineIndex);
|
||||
var alpha = Float(args, "alpha", 0.88f, lineIndex);
|
||||
var feather = Float(args, "feather", 0.20f, lineIndex);
|
||||
var subdivisions = Int(args, "subdivisions", 4, lineIndex);
|
||||
var maxProbes = Int(args, "maxProbes", 4096, lineIndex);
|
||||
return new AquariumTextureSplineFieldProgram(
|
||||
Required(args, "id", lineIndex),
|
||||
Required(args, "texture", lineIndex),
|
||||
axis,
|
||||
Int(args, "firstColumn", 0, lineIndex),
|
||||
Int(args, "columns", 1, lineIndex),
|
||||
Int(args, "columnStride", 1, lineIndex),
|
||||
Int(args, "rollingModulo", 0, lineIndex),
|
||||
subdivisions,
|
||||
Vec3(args, "origin", new Vector3(-0.78f, -0.52f, -0.10f), lineIndex),
|
||||
Vec3(args, "axisStep", new Vector3(0.016f, 0.0f, 0.0f), lineIndex),
|
||||
Vec3(args, "columnStep", new Vector3(0.0f, -0.055f, 0.030f), lineIndex),
|
||||
Float(args, "amplitudeScale", 0.42f, lineIndex),
|
||||
new AquariumSplineTubeAppearance(
|
||||
Vec4(args, "emission", new Vector4(1.0f, 0.72f, 0.22f, 1.0f), lineIndex),
|
||||
radius,
|
||||
alpha,
|
||||
Float(args, "zeroThreshold", 0.74f, lineIndex),
|
||||
feather,
|
||||
Float(args, "tangent", 1.0f, lineIndex),
|
||||
Float(args, "curvature", 0.62f, lineIndex),
|
||||
Float(args, "normal", 0.34f, lineIndex),
|
||||
Float(args, "derivative", 0.82f, lineIndex)),
|
||||
new AquariumSplineTubeProbePolicy(
|
||||
maxProbes,
|
||||
Float(args, "density", 1.0f, lineIndex),
|
||||
Float(args, "minimumContribution", 0.006f, lineIndex),
|
||||
UInt(args, "seed", 0xB11FF13Du, lineIndex)),
|
||||
surfaceNodes.ToArray());
|
||||
}
|
||||
|
||||
private static AquariumFieldGraphNode ParseSurfaceNode(IReadOnlyDictionary<string, string> args, int lineIndex)
|
||||
{
|
||||
var inputs = args.TryGetValue("inputs", out var inputText)
|
||||
? inputText.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
: [];
|
||||
return new AquariumFieldGraphNode(
|
||||
Required(args, "id", lineIndex),
|
||||
Required(args, "op", lineIndex),
|
||||
inputs,
|
||||
Vec4(args, "value", Vector4.Zero, lineIndex));
|
||||
}
|
||||
|
||||
private static AquariumFractalReservoirField ParseReservoir(IReadOnlyDictionary<string, string> args, float reservoirRadius, int lineIndex)
|
||||
{
|
||||
return DefaultReservoir(
|
||||
Int(args, "splats", 65536, lineIndex),
|
||||
reservoirRadius,
|
||||
Int(args, "updates", 65536, lineIndex),
|
||||
Int(args, "candidates", 2, lineIndex),
|
||||
UInt(args, "seed", 0xA17EA11u, lineIndex));
|
||||
}
|
||||
|
||||
private static AquariumFractalReservoirField DefaultReservoir(
|
||||
int splatCount,
|
||||
float reservoirRadius,
|
||||
int? updates = null,
|
||||
int candidates = 2,
|
||||
uint seed = 0xA17EA11u)
|
||||
{
|
||||
var count = Math.Clamp(splatCount, 1, 4_194_304);
|
||||
return new AquariumFractalReservoirField
|
||||
{
|
||||
SplatCount = count,
|
||||
Depth = 1,
|
||||
Seed = seed,
|
||||
CandidatesPerReservoirUpdate = Math.Clamp(candidates, 1, 8),
|
||||
SplatUpdatesPerFrame = count,
|
||||
ReservoirUpdatesPerPass = Math.Clamp(updates ?? count, 1, count),
|
||||
ProgramMode = 3,
|
||||
WorldCenterRadius = new Vector4(0.0f, 0.0f, 12.0f, MathF.Max(0.0001f, reservoirRadius)),
|
||||
};
|
||||
}
|
||||
|
||||
private static IReadOnlyDictionary<string, string> ParseArgs(IEnumerable<string> tokens, int lineIndex)
|
||||
{
|
||||
var args = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var token in tokens)
|
||||
{
|
||||
var separator = token.IndexOf('=', StringComparison.Ordinal);
|
||||
if (separator <= 0 || separator == token.Length - 1)
|
||||
{
|
||||
throw new FormatException($"Invalid key=value token `{token}` at line {lineIndex + 1}.");
|
||||
}
|
||||
|
||||
args[token[..separator]] = token[(separator + 1)..];
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
private static string StripComment(string line)
|
||||
{
|
||||
var index = line.IndexOf('#', StringComparison.Ordinal);
|
||||
return index < 0 ? line : line[..index];
|
||||
}
|
||||
|
||||
private static string Required(IReadOnlyDictionary<string, string> args, string key, int lineIndex)
|
||||
{
|
||||
if (!args.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new FormatException($"Missing `{key}` at line {lineIndex + 1}.");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static int Int(IReadOnlyDictionary<string, string> args, string key, int fallback, int lineIndex) =>
|
||||
args.TryGetValue(key, out var value) ? int.Parse(value, CultureInfo.InvariantCulture) : fallback;
|
||||
|
||||
private static uint UInt(IReadOnlyDictionary<string, string> args, string key, uint fallback, int lineIndex) =>
|
||||
args.TryGetValue(key, out var value) ? uint.Parse(value, CultureInfo.InvariantCulture) : fallback;
|
||||
|
||||
private static float Float(IReadOnlyDictionary<string, string> args, string key, float fallback, int lineIndex) =>
|
||||
args.TryGetValue(key, out var value) ? float.Parse(value, CultureInfo.InvariantCulture) : fallback;
|
||||
|
||||
private static Vector3 Vec3(IReadOnlyDictionary<string, string> args, string key, Vector3 fallback, int lineIndex)
|
||||
{
|
||||
if (!args.TryGetValue(key, out var value))
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
|
||||
var parts = value.Split(',', StringSplitOptions.TrimEntries);
|
||||
if (parts.Length != 3)
|
||||
{
|
||||
throw new FormatException($"`{key}` expects three comma-separated values at line {lineIndex + 1}.");
|
||||
}
|
||||
|
||||
return new Vector3(Parse(parts[0]), Parse(parts[1]), Parse(parts[2]));
|
||||
}
|
||||
|
||||
private static Vector4 Vec4(IReadOnlyDictionary<string, string> args, string key, Vector4 fallback, int lineIndex)
|
||||
{
|
||||
if (!args.TryGetValue(key, out var value))
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
|
||||
var parts = value.Split(',', StringSplitOptions.TrimEntries);
|
||||
if (parts.Length != 4)
|
||||
{
|
||||
throw new FormatException($"`{key}` expects four comma-separated values at line {lineIndex + 1}.");
|
||||
}
|
||||
|
||||
return new Vector4(Parse(parts[0]), Parse(parts[1]), Parse(parts[2]), Parse(parts[3]));
|
||||
}
|
||||
|
||||
private static float Parse(string value) => float.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
|
@ -461,6 +461,8 @@ public sealed class AquariumFractalReservoirField
|
|||
|
||||
public IReadOnlyList<AquariumPackedFractalIfsTransform> ProgramTransforms { get; init; } = [];
|
||||
|
||||
public uint ProgramMode { get; init; }
|
||||
|
||||
public Vector4 WorldCenterRadius { get; init; }
|
||||
|
||||
public Vector4 PriorityFocus { get; init; }
|
||||
|
|
@ -519,9 +521,82 @@ public sealed class AquariumBufferFieldFrame
|
|||
|
||||
public IReadOnlyList<AquariumSplineTubeField> SplineTubeFields { get; init; } = [];
|
||||
|
||||
public bool HasInput => SplineTubeFields.Count > 0;
|
||||
public IReadOnlyList<AquariumTextureFieldBinding> Textures { get; init; } = [];
|
||||
|
||||
public IReadOnlyList<AquariumTextureSplineFieldProgram> TextureSplineFields { get; init; } = [];
|
||||
|
||||
public AquariumFractalReservoirField Reservoir { get; init; } = AquariumFractalReservoirField.Empty;
|
||||
|
||||
public string? SourceScript { get; init; }
|
||||
|
||||
public bool HasInput => SplineTubeFields.Count > 0 || TextureSplineFields.Count > 0;
|
||||
}
|
||||
|
||||
public enum AquariumTextureAxis
|
||||
{
|
||||
X = 0,
|
||||
Y = 1,
|
||||
}
|
||||
|
||||
public enum AquariumRollingModuloMode
|
||||
{
|
||||
None = 0,
|
||||
Columns = 1,
|
||||
Rows = 2,
|
||||
}
|
||||
|
||||
public sealed record AquariumTextureFieldBinding(
|
||||
string Id,
|
||||
int Width,
|
||||
int Height,
|
||||
int Channels,
|
||||
int RollingOffset,
|
||||
AquariumRollingModuloMode RollingMode,
|
||||
IReadOnlyList<float> Samples)
|
||||
{
|
||||
public bool HasInput =>
|
||||
!string.IsNullOrWhiteSpace(Id) &&
|
||||
Width > 0 &&
|
||||
Height > 0 &&
|
||||
Channels > 0 &&
|
||||
Samples.Count >= Width * Height * Channels;
|
||||
}
|
||||
|
||||
public sealed record AquariumTextureSplineFieldProgram(
|
||||
string Id,
|
||||
string TextureId,
|
||||
AquariumTextureAxis FrequencyAxis,
|
||||
int FirstColumn,
|
||||
int ColumnCount,
|
||||
int ColumnStride,
|
||||
int RollingWindowModulo,
|
||||
int Subdivisions,
|
||||
Vector3 Origin,
|
||||
Vector3 AxisStep,
|
||||
Vector3 ColumnStep,
|
||||
float AmplitudeScale,
|
||||
AquariumSplineTubeAppearance Appearance,
|
||||
AquariumSplineTubeProbePolicy ProbePolicy,
|
||||
IReadOnlyList<AquariumFieldGraphNode> SurfaceGraph);
|
||||
|
||||
public sealed record AquariumFieldGraphNode(
|
||||
string Id,
|
||||
string Op,
|
||||
IReadOnlyList<string> Inputs,
|
||||
Vector4 Value);
|
||||
|
||||
public readonly record struct AquariumPackedTextureSplineFieldProgram(
|
||||
Vector4 DimensionsAxisMode,
|
||||
Vector4 AxisModeOffset,
|
||||
Vector4 ColumnModulo,
|
||||
Vector4 SubdivisionProbe,
|
||||
Vector4 OriginAmplitude,
|
||||
Vector4 AxisStepRadius,
|
||||
Vector4 ColumnStepAlpha,
|
||||
Vector4 Emission,
|
||||
Vector4 SurfaceWeights0,
|
||||
Vector4 SurfaceWeights1);
|
||||
|
||||
public sealed record AquariumSplineTubeField(
|
||||
string Id,
|
||||
string BufferId,
|
||||
|
|
@ -597,6 +672,10 @@ public readonly record struct AquariumSplineTubeProbePolicy(
|
|||
public sealed class AquariumBufferFieldFrameBuilder
|
||||
{
|
||||
private readonly List<AquariumSplineTubeField> splineTubeFields = [];
|
||||
private readonly List<AquariumTextureFieldBinding> textures = [];
|
||||
private readonly List<AquariumTextureSplineFieldProgram> textureSplineFields = [];
|
||||
private AquariumFractalReservoirField reservoir = AquariumFractalReservoirField.Empty;
|
||||
private string? sourceScript;
|
||||
|
||||
public AquariumBufferFieldFrameBuilder SplineTube(
|
||||
string id,
|
||||
|
|
@ -616,7 +695,51 @@ public sealed class AquariumBufferFieldFrameBuilder
|
|||
return this;
|
||||
}
|
||||
|
||||
public AquariumBufferFieldFrame Build() => new() { SplineTubeFields = splineTubeFields };
|
||||
public AquariumBufferFieldFrameBuilder Texture(
|
||||
string id,
|
||||
int width,
|
||||
int height,
|
||||
int channels,
|
||||
int rollingOffset,
|
||||
AquariumRollingModuloMode rollingMode,
|
||||
IReadOnlyList<float> samples)
|
||||
{
|
||||
var texture = new AquariumTextureFieldBinding(id, width, height, channels, rollingOffset, rollingMode, samples);
|
||||
if (!texture.HasInput)
|
||||
{
|
||||
throw new ArgumentException($"Texture field `{id}` has invalid dimensions or sample count.", nameof(samples));
|
||||
}
|
||||
|
||||
textures.Add(texture);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AquariumBufferFieldFrameBuilder TextureSplineField(AquariumTextureSplineFieldProgram program)
|
||||
{
|
||||
textureSplineFields.Add(program);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AquariumBufferFieldFrameBuilder Reservoir(AquariumFractalReservoirField field)
|
||||
{
|
||||
reservoir = field;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AquariumBufferFieldFrameBuilder Script(string script)
|
||||
{
|
||||
sourceScript = script;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AquariumBufferFieldFrame Build() => new()
|
||||
{
|
||||
SplineTubeFields = splineTubeFields,
|
||||
Textures = textures,
|
||||
TextureSplineFields = textureSplineFields,
|
||||
Reservoir = reservoir,
|
||||
SourceScript = sourceScript,
|
||||
};
|
||||
}
|
||||
|
||||
public sealed class AquariumSplineFrame
|
||||
|
|
|
|||
|
|
@ -78,6 +78,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const int RootFractalRadiosityReservoirs = 4;
|
||||
private const int RootFractalFlameStates = 5;
|
||||
private const int RootFractalProgramTransforms = 6;
|
||||
private const int RootFractalTextureSplinePrograms = 7;
|
||||
private const int RootFractalTextureSamples = 8;
|
||||
private static readonly DebugUi.DebugUiOption[] RenderDebugOptions =
|
||||
[
|
||||
new(0, "Final"),
|
||||
|
|
@ -177,6 +179,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private D3D12StructuredBuffer? fractalRadiosityReservoirBuffer;
|
||||
private D3D12StructuredBuffer? fractalFlameStateBuffer;
|
||||
private D3D12StructuredBuffer? fractalProgramTransformBuffer;
|
||||
private D3D12StructuredBuffer? bufferFieldTextureSplineProgramBuffer;
|
||||
private D3D12StructuredBuffer? bufferFieldTextureSampleBuffer;
|
||||
private readonly Dictionary<string, D3D12ExternalSensorTexture> externalSensorTextures = new(StringComparer.Ordinal);
|
||||
private readonly D3D12CubeTexture studioPmremTexture;
|
||||
private readonly D3D12CubeTexture studioIrradianceTexture;
|
||||
|
|
@ -198,6 +202,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private bool temporalGaussiansGpuGenerated;
|
||||
private AquariumFractalReservoirField activeFractalReservoirField = AquariumFractalReservoirField.Empty;
|
||||
private AquariumBufferFieldFrame activeBufferFieldFrame = AquariumBufferFieldFrame.Empty;
|
||||
private AquariumPackedTextureSplineFieldProgram[] activeTextureSplinePrograms = [];
|
||||
private float[] activeTextureFieldSamples = [];
|
||||
private AquariumSplineFrame activeSplineFrame = AquariumSplineFrame.Empty;
|
||||
private Viewport viewport;
|
||||
private RawRect scissorRect;
|
||||
|
|
@ -569,6 +575,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
activeCameraTarget = frame.CameraTarget;
|
||||
EnsureFractalReservoirBuffers(activeFractalReservoirField);
|
||||
EnsureFractalProgramTransformBuffer();
|
||||
EnsureBufferFieldProgramBuffers();
|
||||
var activeGpuSensorInput = frame.Scene.GpuSensorFrame.HasInput;
|
||||
var activeAccumulationWindow = activeGpuSensorInput
|
||||
? frame.Scene.GpuSensorFrame.AccumulationWindowSeconds
|
||||
|
|
@ -875,6 +882,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
studioIrradianceTexture.Dispose();
|
||||
studioPmremTexture.Dispose();
|
||||
DisposeFractalReservoirBuffers();
|
||||
bufferFieldTextureSplineProgramBuffer?.Dispose();
|
||||
bufferFieldTextureSampleBuffer?.Dispose();
|
||||
temporalGaussianBuffer.Dispose();
|
||||
gpuFusionPointBuffer.Dispose();
|
||||
gpuFusionSeedBuffer.Dispose();
|
||||
|
|
@ -1422,8 +1431,46 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
resourceRegistry.Add("fractal-program-transform-buffer", fractalProgramTransformBuffer);
|
||||
}
|
||||
|
||||
private void EnsureBufferFieldProgramBuffers()
|
||||
{
|
||||
if (!activeFractalReservoirField.HasInput || !activeBufferFieldFrame.HasInput)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var requiredProgramCount = Math.Max(activeTextureSplinePrograms.Length, 1);
|
||||
if (bufferFieldTextureSplineProgramBuffer is null || bufferFieldTextureSplineProgramBuffer.ElementCount < requiredProgramCount)
|
||||
{
|
||||
WaitForGpu();
|
||||
resourceRegistry.RemoveStructuredBuffer("buffer-field-texture-spline-program-buffer");
|
||||
bufferFieldTextureSplineProgramBuffer?.Dispose();
|
||||
bufferFieldTextureSplineProgramBuffer = new D3D12StructuredBuffer(
|
||||
device,
|
||||
requiredProgramCount,
|
||||
Marshal.SizeOf<AquariumPackedTextureSplineFieldProgram>(),
|
||||
"Aquarium D3D12 Buffer Field Texture Spline Program Buffer");
|
||||
resourceRegistry.Add("buffer-field-texture-spline-program-buffer", bufferFieldTextureSplineProgramBuffer);
|
||||
}
|
||||
|
||||
var requiredSampleCount = Math.Max(activeTextureFieldSamples.Length, 1);
|
||||
if (bufferFieldTextureSampleBuffer is null || bufferFieldTextureSampleBuffer.ElementCount < requiredSampleCount)
|
||||
{
|
||||
WaitForGpu();
|
||||
resourceRegistry.RemoveStructuredBuffer("buffer-field-texture-sample-buffer");
|
||||
bufferFieldTextureSampleBuffer?.Dispose();
|
||||
bufferFieldTextureSampleBuffer = new D3D12StructuredBuffer(
|
||||
device,
|
||||
requiredSampleCount,
|
||||
Marshal.SizeOf<float>(),
|
||||
"Aquarium D3D12 Buffer Field Texture Sample Buffer");
|
||||
resourceRegistry.Add("buffer-field-texture-sample-buffer", bufferFieldTextureSampleBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeFractalReservoirBuffers()
|
||||
{
|
||||
resourceRegistry.RemoveStructuredBuffer("buffer-field-texture-sample-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("buffer-field-texture-spline-program-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-program-transform-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-flame-state-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-radiosity-reservoir-buffer");
|
||||
|
|
@ -1436,12 +1483,16 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
fractalPbrReservoirBuffer?.Dispose();
|
||||
fractalSdfReservoirBuffer?.Dispose();
|
||||
fractalSplatBuffer?.Dispose();
|
||||
bufferFieldTextureSampleBuffer?.Dispose();
|
||||
bufferFieldTextureSplineProgramBuffer?.Dispose();
|
||||
fractalProgramTransformBuffer = null;
|
||||
fractalFlameStateBuffer = null;
|
||||
fractalRadiosityReservoirBuffer = null;
|
||||
fractalPbrReservoirBuffer = null;
|
||||
fractalSdfReservoirBuffer = null;
|
||||
fractalSplatBuffer = null;
|
||||
bufferFieldTextureSampleBuffer = null;
|
||||
bufferFieldTextureSplineProgramBuffer = null;
|
||||
}
|
||||
|
||||
private (int Width, int Height) ResolveGraphTargetSize(AquariumTargetSize size)
|
||||
|
|
@ -1896,6 +1947,16 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
fractalProgramTransformBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, emptyProgram);
|
||||
}
|
||||
|
||||
if (activeTextureSplinePrograms.Length > 0 && bufferFieldTextureSplineProgramBuffer is not null)
|
||||
{
|
||||
bufferFieldTextureSplineProgramBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, activeTextureSplinePrograms);
|
||||
}
|
||||
|
||||
if (activeTextureFieldSamples.Length > 0 && bufferFieldTextureSampleBuffer is not null)
|
||||
{
|
||||
bufferFieldTextureSampleBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, activeTextureFieldSamples);
|
||||
}
|
||||
|
||||
fractalSplatBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
fractalSdfReservoirBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
fractalPbrReservoirBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
|
|
@ -1912,6 +1973,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalRadiosityReservoirs, fractalRadiosityReservoirBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalFlameStates, fractalFlameStateBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootShaderResourceView(RootFractalProgramTransforms, fractalProgramTransformBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootShaderResourceView(RootFractalTextureSplinePrograms, bufferFieldTextureSplineProgramBuffer?.Resource.GPUVirtualAddress ?? fractalProgramTransformBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootShaderResourceView(RootFractalTextureSamples, bufferFieldTextureSampleBuffer?.Resource.GPUVirtualAddress ?? fractalProgramTransformBuffer.Resource.GPUVirtualAddress);
|
||||
Dispatch(fractalSplatPipelineState!, splatDispatchCount);
|
||||
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(fractalSplatBuffer.Resource));
|
||||
Dispatch(fractalSdfReservoirPipelineState!, activeFractalReservoirField.ReservoirUpdatesPerPass);
|
||||
|
|
@ -1938,8 +2001,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, activeFractalReservoirField.Seed, 3);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)activeFractalReservoirField.CandidatesPerReservoirUpdate, 4);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)activeFractalReservoirField.ReservoirUpdatesPerPass, 5);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)activeFractalProgramTransforms.Length, 6);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, activeFractalProgramTransforms.Length > 0 ? 1u : 0u, 7);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)Math.Max(activeFractalProgramTransforms.Length, activeTextureSplinePrograms.Length), 6);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, activeBufferFieldFrame.TextureSplineFields.Count > 0 ? 3u : activeFractalReservoirField.ProgramMode, 7);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)splatDispatchCount, 8);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, BitConverter.SingleToUInt32Bits(activeFractalReservoirField.PriorityFocus.X), 9);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, BitConverter.SingleToUInt32Bits(activeFractalReservoirField.PriorityFocus.Y), 10);
|
||||
|
|
@ -2047,12 +2110,16 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
gpuFusionPointCount = 0;
|
||||
gpuFusionPointSource = default;
|
||||
temporalGaussiansGpuGenerated = false;
|
||||
activeFractalReservoirField = scene.FractalReservoirField.HasInput
|
||||
activeFractalReservoirField = scene.BufferFieldFrame.Reservoir.HasInput
|
||||
? scene.BufferFieldFrame.Reservoir
|
||||
: scene.FractalReservoirField.HasInput
|
||||
? scene.FractalReservoirField
|
||||
: AquariumFractalReservoirField.Empty;
|
||||
activeBufferFieldFrame = scene.BufferFieldFrame.HasInput
|
||||
? scene.BufferFieldFrame
|
||||
: AquariumBufferFieldFrame.Empty;
|
||||
activeTextureSplinePrograms = PackTextureSplinePrograms(activeBufferFieldFrame);
|
||||
activeTextureFieldSamples = FlattenTextureSamples(activeBufferFieldFrame);
|
||||
activeSplineFrame = scene.SplineFrame.HasInput
|
||||
? scene.SplineFrame
|
||||
: AquariumSplineFrame.Empty;
|
||||
|
|
@ -2208,6 +2275,72 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
new Vector4(gaussian.ShapePower, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
|
||||
private static AquariumPackedTextureSplineFieldProgram[] PackTextureSplinePrograms(AquariumBufferFieldFrame frame)
|
||||
{
|
||||
if (!frame.HasInput || frame.TextureSplineFields.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var textureOffsets = new Dictionary<string, (AquariumTextureFieldBinding Texture, int Offset, int Index)>(StringComparer.Ordinal);
|
||||
var offset = 0;
|
||||
for (var index = 0; index < frame.Textures.Count; index++)
|
||||
{
|
||||
var texture = frame.Textures[index];
|
||||
textureOffsets[texture.Id] = (texture, offset, index);
|
||||
offset += texture.Samples.Count;
|
||||
}
|
||||
|
||||
var programs = new AquariumPackedTextureSplineFieldProgram[frame.TextureSplineFields.Count];
|
||||
for (var index = 0; index < frame.TextureSplineFields.Count; index++)
|
||||
{
|
||||
var program = frame.TextureSplineFields[index];
|
||||
if (!textureOffsets.TryGetValue(program.TextureId, out var textureItem))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var texture = textureItem.Texture;
|
||||
var appearance = program.Appearance.Normalized();
|
||||
var policy = program.ProbePolicy.Normalized();
|
||||
programs[index] = new AquariumPackedTextureSplineFieldProgram(
|
||||
new Vector4(textureItem.Index, texture.Width, texture.Height, texture.Channels),
|
||||
new Vector4((float)program.FrequencyAxis, (float)texture.RollingMode, texture.RollingOffset, textureItem.Offset),
|
||||
new Vector4(program.FirstColumn, program.ColumnCount, program.ColumnStride, program.RollingWindowModulo),
|
||||
new Vector4(program.Subdivisions, policy.MaxProbeCount, policy.BaseDensity, policy.MinimumVisualContribution),
|
||||
new Vector4(program.Origin, program.AmplitudeScale),
|
||||
new Vector4(program.AxisStep, appearance.Radius),
|
||||
new Vector4(program.ColumnStep, appearance.Alpha),
|
||||
appearance.Emission,
|
||||
new Vector4(appearance.ZeroThreshold, appearance.Feather, appearance.TangentWeight, appearance.CurvatureWeight),
|
||||
new Vector4(appearance.NormalWeight, appearance.DerivativeWeight, policy.Seed, 0.0f));
|
||||
}
|
||||
|
||||
return programs;
|
||||
}
|
||||
|
||||
private static float[] FlattenTextureSamples(AquariumBufferFieldFrame frame)
|
||||
{
|
||||
if (!frame.HasInput || frame.Textures.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var samples = new float[frame.Textures.Sum(texture => texture.Samples.Count)];
|
||||
var offset = 0;
|
||||
foreach (var texture in frame.Textures)
|
||||
{
|
||||
for (var index = 0; index < texture.Samples.Count; index++)
|
||||
{
|
||||
samples[offset + index] = texture.Samples[index];
|
||||
}
|
||||
|
||||
offset += texture.Samples.Count;
|
||||
}
|
||||
|
||||
return samples;
|
||||
}
|
||||
|
||||
private static float SceneFlags(AquariumSceneState scene)
|
||||
{
|
||||
var flags = 0;
|
||||
|
|
@ -2791,6 +2924,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(3, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(4, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(0, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(1, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(2, 0), ShaderVisibility.All),
|
||||
};
|
||||
var description = new RootSignatureDescription(
|
||||
RootSignatureFlags.None,
|
||||
|
|
|
|||
|
|
@ -41,6 +41,20 @@ struct FractalIfsTransform
|
|||
float4 postTranslation;
|
||||
};
|
||||
|
||||
struct TextureSplineFieldProgram
|
||||
{
|
||||
float4 dimensionsAxisMode;
|
||||
float4 axisModeOffset;
|
||||
float4 columnModulo;
|
||||
float4 subdivisionProbe;
|
||||
float4 originAmplitude;
|
||||
float4 axisStepRadius;
|
||||
float4 columnStepAlpha;
|
||||
float4 emission;
|
||||
float4 surfaceWeights0;
|
||||
float4 surfaceWeights1;
|
||||
};
|
||||
|
||||
struct FlameIterationState
|
||||
{
|
||||
float4 pointSupportMaterial;
|
||||
|
|
@ -70,6 +84,8 @@ RWStructuredBuffer<PbrMaterialReservoir> PbrReservoirs : register(u2);
|
|||
RWStructuredBuffer<RadiosityReservoir> RadiosityReservoirs : register(u3);
|
||||
RWStructuredBuffer<FlameIterationState> FlameStates : register(u4);
|
||||
StructuredBuffer<FractalIfsTransform> ProgramTransforms : register(t0);
|
||||
StructuredBuffer<TextureSplineFieldProgram> TextureSplinePrograms : register(t1);
|
||||
StructuredBuffer<float> TextureFieldSamples : register(t2);
|
||||
|
||||
static const float FIELD_ENCODING_SIGNED_DISTANCE = 0.0;
|
||||
static const float FIELD_ENCODING_DENSITY = 2.0;
|
||||
|
|
@ -146,6 +162,63 @@ float3 CubeSphereDirection(float face, float2 uv)
|
|||
|
||||
float3 FractalPoint(uint index, out float radius, out float fieldEncoding)
|
||||
{
|
||||
if (ProgramTransformCount > 0u && ProgramMode == 3u)
|
||||
{
|
||||
uint programIndex = index % ProgramTransformCount;
|
||||
TextureSplineFieldProgram program = TextureSplinePrograms[programIndex];
|
||||
uint localIndex = index / ProgramTransformCount;
|
||||
uint width = max((uint)round(program.dimensionsAxisMode.y), 1u);
|
||||
uint height = max((uint)round(program.dimensionsAxisMode.z), 1u);
|
||||
uint channels = max((uint)round(program.dimensionsAxisMode.w), 1u);
|
||||
uint frequencyAxis = (uint)round(program.axisModeOffset.x);
|
||||
uint rollingMode = (uint)round(program.axisModeOffset.y);
|
||||
uint rollingOffset = (uint)round(program.axisModeOffset.z);
|
||||
uint sampleOffset = (uint)round(program.axisModeOffset.w);
|
||||
uint firstColumn = (uint)max(round(program.columnModulo.x), 0.0);
|
||||
uint columnCount = max((uint)round(program.columnModulo.y), 1u);
|
||||
uint columnStride = max((uint)round(program.columnModulo.z), 1u);
|
||||
uint rollingModulo = (uint)max(round(program.columnModulo.w), 0.0);
|
||||
uint axisSamples = frequencyAxis == 0u ? width : height;
|
||||
uint column = (localIndex / axisSamples) % columnCount;
|
||||
uint frequencyIndex = localIndex % axisSamples;
|
||||
uint textureColumn = firstColumn + column * columnStride;
|
||||
if (rollingModulo > 0u)
|
||||
{
|
||||
textureColumn = (textureColumn + rollingOffset) % rollingModulo;
|
||||
}
|
||||
|
||||
uint x = frequencyAxis == 0u ? frequencyIndex : textureColumn;
|
||||
uint y = frequencyAxis == 0u ? textureColumn : frequencyIndex;
|
||||
if (rollingMode == 1u)
|
||||
{
|
||||
x = (x + rollingOffset) % width;
|
||||
}
|
||||
else if (rollingMode == 2u)
|
||||
{
|
||||
y = (y + rollingOffset) % height;
|
||||
}
|
||||
|
||||
x = min(x, width - 1u);
|
||||
y = min(y, height - 1u);
|
||||
uint sampleIndex = sampleOffset + ((y * width + x) * channels);
|
||||
float amplitude = TextureFieldSamples[sampleIndex];
|
||||
uint h = Hash(index + FrameIndex * 1664525u + asuint(program.surfaceWeights1.z));
|
||||
float jitter = (Random01(h) - 0.5) / max((float)axisSamples, 1.0);
|
||||
float t = ((float)frequencyIndex + 0.5 + jitter) / max((float)axisSamples, 1.0);
|
||||
float c = ((float)column + 0.5) / max((float)columnCount, 1.0);
|
||||
float3 p = program.originAmplitude.xyz +
|
||||
program.axisStepRadius.xyz * ((float)frequencyIndex + jitter) +
|
||||
program.columnStepAlpha.xyz * (float)column +
|
||||
float3(0.0, amplitude * program.originAmplitude.w, 0.0);
|
||||
|
||||
float neighbor = TextureFieldSamples[sampleOffset + ((y * width + min(x + 1u, width - 1u)) * channels)];
|
||||
float derivative = neighbor - amplitude;
|
||||
float visualContribution = saturate(abs(amplitude) + abs(derivative) * program.surfaceWeights0.w);
|
||||
radius = max(program.axisStepRadius.w * (0.55 + visualContribution * 0.90), 0.0002);
|
||||
fieldEncoding = FIELD_ENCODING_DENSITY;
|
||||
return p + float3(0.0, 0.0, (Random01(h + 31u) - 0.5) * radius * 0.35 + t * 0.0 + c * 0.0);
|
||||
}
|
||||
|
||||
if (ProgramTransformCount > 0u && ProgramMode == 2u)
|
||||
{
|
||||
fieldEncoding = FIELD_ENCODING_DENSITY;
|
||||
|
|
@ -403,7 +476,10 @@ void D3D12FractalSplatReceiptCS(uint3 id : SV_DispatchThreadID)
|
|||
splat.centerRadius = float4(p, radius);
|
||||
splat.orientation = float4(0.0, 0.0, 0.0, 1.0);
|
||||
splat.radiiFalloff = float4(radius, radius * 0.72, radius * 0.45, 4.0);
|
||||
splat.materialConfidence = float4((float)(h & 1023u) / 1023.0, 1.0, fieldEncoding, 1.0);
|
||||
float material = ProgramMode == 3u
|
||||
? saturate(abs(p.y) * 0.7 + radius * 18.0 + Random01(h) * 0.15)
|
||||
: (float)(h & 1023u) / 1023.0;
|
||||
splat.materialConfidence = float4(material, 1.0, fieldEncoding, 1.0);
|
||||
splat.key = float4((float)index, (float)FrameIndex, (float)Depth, asfloat(h));
|
||||
Splats[index] = splat;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ SceneOut ResolveFractalSplat(FractalSplatVertexOut input, bool renderTransparent
|
|||
float material = saturate(splat.materialConfidence.x);
|
||||
float3 fallbackColor = transparentField
|
||||
? lerp(float3(0.18, 0.08, 0.32), float3(1.0, 0.44, 0.12), material)
|
||||
: lerp(float3(0.08, 0.38, 0.18), float3(0.78, 0.61, 0.32), material);
|
||||
: lerp(float3(0.18, 0.58, 0.28), float3(1.15, 0.78, 0.28), material);
|
||||
float3 pbrColor = lerp(float3(0.06, 0.30, 0.14), saturate(pbr.baseColorRoughMetal.rgb), 0.72);
|
||||
float3 radiosityColor = saturate(radiosity.radianceDistance.rgb);
|
||||
float reservoirConfidence = min(
|
||||
|
|
@ -211,12 +211,12 @@ SceneOut ResolveFractalSplat(FractalSplatVertexOut input, bool renderTransparent
|
|||
float roughness = pbrResident ? saturate(pbr.baseColorRoughMetal.w) : 0.6;
|
||||
float diffuse = transparentField ? 0.52 + 0.36 * viewFacing : 0.32 + 0.50 * starFacing + 0.18 * viewFacing;
|
||||
float fresnel = pow(saturate(1.0 - viewFacing), 2.2);
|
||||
float3 litColor = color * diffuse + color * 0.18;
|
||||
float3 litColor = color * diffuse + color * (transparentField ? 0.18 : 0.72);
|
||||
litColor += radiosityResident ? radiosityColor * (0.24 + reservoirConfidence * 0.48) : 0.0;
|
||||
litColor += lerp(float3(0.015, 0.06, 0.05), transparentField ? float3(0.95, 0.28, 0.08) : float3(0.24, 0.30, 0.20), 1.0 - roughness) * fresnel * (transparentField ? 0.72 : 0.48);
|
||||
float opacity = transparentField
|
||||
? saturate(edgeCoverage * edgeCoverage * (0.18 + reservoirConfidence * 0.32))
|
||||
: saturate(edgeCoverage * (0.78 + reservoirConfidence * 0.22));
|
||||
? saturate(edgeCoverage * edgeCoverage * (0.48 + reservoirConfidence * 0.42))
|
||||
: saturate(edgeCoverage * (0.92 + reservoirConfidence * 0.28));
|
||||
float reservoirUpdatedFrame = min(
|
||||
sdfResident ? sdf.validation.y : frameIndex,
|
||||
min(
|
||||
|
|
@ -226,7 +226,7 @@ SceneOut ResolveFractalSplat(FractalSplatVertexOut input, bool renderTransparent
|
|||
float domainValidity = reservoirConfidence > 0.0 ? 1.0 : 0.0;
|
||||
|
||||
SceneOut output;
|
||||
output.colorTravel = float4(litColor * opacity, min(input.travel - surfaceZ * input.worldRadius, farDistance + 1.0));
|
||||
output.colorTravel = float4(litColor * opacity * (transparentField ? 1.0 : 4.0), min(input.travel - surfaceZ * input.worldRadius, farDistance + 1.0));
|
||||
output.metadata = float4(FIELD_ID_FRACTAL_SPLAT_BASE, normal);
|
||||
output.control = float4(opacity, reservoirConfidence, transparentField ? splat.materialConfidence.z / 10.0 : saturate((sdfResident ? sdf.centerRadius.w : splat.centerRadius.w) * 40.0), 0.0);
|
||||
output.reservoirGuide = float4(reservoirConfidence, reservoirSampleAge, domainValidity, 0.0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue