Add GPU-resident flame iteration state
This commit is contained in:
parent
710b979f26
commit
c9eb44cc0c
13 changed files with 178 additions and 37 deletions
|
|
@ -178,6 +178,11 @@ public readonly record struct AquariumPackedFractalIfsTransform(
|
|||
Vector4 PostMatrix,
|
||||
Vector4 PostTranslation);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly record struct AquariumPackedFractalFlameState(
|
||||
Vector4 PointSupportMaterial,
|
||||
Vector4 RandomStep);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly record struct AquariumPackedSdfEnvelopeReservoir(
|
||||
Vector4 CenterRadius,
|
||||
|
|
|
|||
|
|
@ -455,6 +455,8 @@ public sealed class AquariumFractalReservoirField
|
|||
|
||||
public int CandidatesPerReservoirUpdate { get; init; } = 2;
|
||||
|
||||
public int SplatUpdatesPerFrame { get; init; }
|
||||
|
||||
public int ReservoirUpdatesPerPass { get; init; }
|
||||
|
||||
public IReadOnlyList<AquariumPackedFractalIfsTransform> ProgramTransforms { get; init; } = [];
|
||||
|
|
@ -465,6 +467,8 @@ public sealed class AquariumFractalReservoirField
|
|||
SplatCount > 0 &&
|
||||
Depth > 0 &&
|
||||
CandidatesPerReservoirUpdate > 0 &&
|
||||
SplatUpdatesPerFrame > 0 &&
|
||||
SplatUpdatesPerFrame <= SplatCount &&
|
||||
ReservoirUpdatesPerPass > 0 &&
|
||||
ReservoirUpdatesPerPass <= SplatCount;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const int RootFractalSdfReservoirs = 2;
|
||||
private const int RootFractalPbrReservoirs = 3;
|
||||
private const int RootFractalRadiosityReservoirs = 4;
|
||||
private const int RootFractalProgramTransforms = 5;
|
||||
private const int RootFractalFlameStates = 5;
|
||||
private const int RootFractalProgramTransforms = 6;
|
||||
private static readonly DebugUi.DebugUiOption[] RenderDebugOptions =
|
||||
[
|
||||
new(0, "Final"),
|
||||
|
|
@ -172,6 +173,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private D3D12StructuredBuffer? fractalSdfReservoirBuffer;
|
||||
private D3D12StructuredBuffer? fractalPbrReservoirBuffer;
|
||||
private D3D12StructuredBuffer? fractalRadiosityReservoirBuffer;
|
||||
private D3D12StructuredBuffer? fractalFlameStateBuffer;
|
||||
private D3D12StructuredBuffer? fractalProgramTransformBuffer;
|
||||
private readonly Dictionary<string, D3D12ExternalSensorTexture> externalSensorTextures = new(StringComparer.Ordinal);
|
||||
private readonly D3D12CubeTexture studioPmremTexture;
|
||||
|
|
@ -1338,7 +1340,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return;
|
||||
}
|
||||
|
||||
if (fractalSplatBuffer is not null && fractalSplatBuffer.ElementCount >= field.SplatCount)
|
||||
if (fractalSplatBuffer is not null && fractalFlameStateBuffer is not null && fractalSplatBuffer.ElementCount >= field.SplatCount && fractalFlameStateBuffer.ElementCount >= field.SplatCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
@ -1361,6 +1363,10 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
field.SplatCount,
|
||||
"fractal-radiosity-reservoir-buffer",
|
||||
"Aquarium D3D12 Fractal Radiosity Reservoir Buffer");
|
||||
fractalFlameStateBuffer = CreateFractalReservoirBuffer<AquariumPackedFractalFlameState>(
|
||||
field.SplatCount,
|
||||
"fractal-flame-state-buffer",
|
||||
"Aquarium D3D12 Fractal Flame Iteration State Buffer");
|
||||
}
|
||||
|
||||
private D3D12StructuredBuffer CreateFractalReservoirBuffer<T>(int elementCount, string registryName, string resourceName)
|
||||
|
|
@ -1403,16 +1409,19 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private void DisposeFractalReservoirBuffers()
|
||||
{
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-program-transform-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-flame-state-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-radiosity-reservoir-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-pbr-material-reservoir-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-sdf-envelope-reservoir-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("fractal-sdf-splat-buffer");
|
||||
fractalProgramTransformBuffer?.Dispose();
|
||||
fractalFlameStateBuffer?.Dispose();
|
||||
fractalRadiosityReservoirBuffer?.Dispose();
|
||||
fractalPbrReservoirBuffer?.Dispose();
|
||||
fractalSdfReservoirBuffer?.Dispose();
|
||||
fractalSplatBuffer?.Dispose();
|
||||
fractalProgramTransformBuffer = null;
|
||||
fractalFlameStateBuffer = null;
|
||||
fractalRadiosityReservoirBuffer = null;
|
||||
fractalPbrReservoirBuffer = null;
|
||||
fractalSdfReservoirBuffer = null;
|
||||
|
|
@ -1728,6 +1737,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
fractalSdfReservoirBuffer is null ||
|
||||
fractalPbrReservoirBuffer is null ||
|
||||
fractalRadiosityReservoirBuffer is null ||
|
||||
fractalFlameStateBuffer is null ||
|
||||
fractalProgramTransformBuffer is null)
|
||||
{
|
||||
return;
|
||||
|
|
@ -1750,14 +1760,19 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
fractalSdfReservoirBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
fractalPbrReservoirBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
fractalRadiosityReservoirBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
fractalFlameStateBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
activeCommandList.SetComputeRootSignature(fractalReservoirRootSignature);
|
||||
BindFractalReservoirConstants(activeCommandList);
|
||||
var splatDispatchCount = temporalFrameIndex == 0
|
||||
? activeFractalReservoirField.SplatCount
|
||||
: activeFractalReservoirField.SplatUpdatesPerFrame;
|
||||
BindFractalReservoirConstants(activeCommandList, splatDispatchCount);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalSplats, fractalSplatBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalSdfReservoirs, fractalSdfReservoirBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalPbrReservoirs, fractalPbrReservoirBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalRadiosityReservoirs, fractalRadiosityReservoirBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(RootFractalFlameStates, fractalFlameStateBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootShaderResourceView(RootFractalProgramTransforms, fractalProgramTransformBuffer.Resource.GPUVirtualAddress);
|
||||
Dispatch(fractalSplatPipelineState!, activeFractalReservoirField.SplatCount);
|
||||
Dispatch(fractalSplatPipelineState!, splatDispatchCount);
|
||||
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(fractalSplatBuffer.Resource));
|
||||
Dispatch(fractalSdfReservoirPipelineState!, activeFractalReservoirField.ReservoirUpdatesPerPass);
|
||||
Dispatch(fractalPbrReservoirPipelineState!, activeFractalReservoirField.ReservoirUpdatesPerPass);
|
||||
|
|
@ -1775,7 +1790,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
}
|
||||
}
|
||||
|
||||
private void BindFractalReservoirConstants(ID3D12GraphicsCommandList activeCommandList)
|
||||
private void BindFractalReservoirConstants(ID3D12GraphicsCommandList activeCommandList, int splatDispatchCount)
|
||||
{
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)activeFractalReservoirField.SplatCount, 0);
|
||||
activeCommandList.SetComputeRoot32BitConstant(RootFractalConstants, (uint)temporalFrameIndex, 1);
|
||||
|
|
@ -1785,6 +1800,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
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)splatDispatchCount, 8);
|
||||
}
|
||||
|
||||
private void CreateGpuSensorTextureViews(AquariumGpuSensorFrame sensorFrame, D3D12DescriptorSlot firstDescriptor)
|
||||
|
|
@ -2080,17 +2096,19 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
$"{frameResources.TransientShaderDescriptors.Describe()}; " +
|
||||
$"{staticShaderDescriptorArena.Describe()}; " +
|
||||
$"{renderTargetViewArena.Describe()}");
|
||||
if (activeFractalReservoirField.HasInput && fractalSplatBuffer is not null && fractalSdfReservoirBuffer is not null && fractalPbrReservoirBuffer is not null && fractalRadiosityReservoirBuffer is not null)
|
||||
if (activeFractalReservoirField.HasInput && fractalSplatBuffer is not null && fractalSdfReservoirBuffer is not null && fractalPbrReservoirBuffer is not null && fractalRadiosityReservoirBuffer is not null && fractalFlameStateBuffer is not null)
|
||||
{
|
||||
var residentBytes =
|
||||
fractalSplatBuffer.SizeBytes +
|
||||
fractalSdfReservoirBuffer.SizeBytes +
|
||||
fractalPbrReservoirBuffer.SizeBytes +
|
||||
fractalRadiosityReservoirBuffer.SizeBytes;
|
||||
fractalRadiosityReservoirBuffer.SizeBytes +
|
||||
fractalFlameStateBuffer.SizeBytes;
|
||||
Console.WriteLine(
|
||||
$"D3D12 fractal reservoirs: splats {activeFractalReservoirField.SplatCount:N0}; " +
|
||||
$"visible {visibleFractalSplatCount:N0}; " +
|
||||
$"program transforms {activeFractalProgramTransforms.Length:N0}; " +
|
||||
$"splat updates/frame {activeFractalReservoirField.SplatUpdatesPerFrame:N0}; " +
|
||||
$"updates/pass {activeFractalReservoirField.ReservoirUpdatesPerPass:N0}; " +
|
||||
$"candidates/update {activeFractalReservoirField.CandidatesPerReservoirUpdate}; " +
|
||||
$"resident {residentBytes / (1024.0 * 1024.0):0.0} MiB");
|
||||
|
|
@ -2611,11 +2629,12 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
{
|
||||
var rootParameters = new[]
|
||||
{
|
||||
new RootParameter(new RootConstants(0, 0, 8), ShaderVisibility.All),
|
||||
new RootParameter(new RootConstants(0, 0, 9), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(0, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(1, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(2, 0), ShaderVisibility.All),
|
||||
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),
|
||||
};
|
||||
var description = new RootSignatureDescription(
|
||||
|
|
|
|||
|
|
@ -41,6 +41,12 @@ struct FractalIfsTransform
|
|||
float4 postTranslation;
|
||||
};
|
||||
|
||||
struct FlameIterationState
|
||||
{
|
||||
float4 pointSupportMaterial;
|
||||
float4 randomStep;
|
||||
};
|
||||
|
||||
cbuffer ReceiptConstants : register(b0)
|
||||
{
|
||||
uint SplatCount;
|
||||
|
|
@ -51,12 +57,14 @@ cbuffer ReceiptConstants : register(b0)
|
|||
uint ReservoirUpdatesPerPass;
|
||||
uint ProgramTransformCount;
|
||||
uint ProgramMode;
|
||||
uint SplatDispatchCount;
|
||||
};
|
||||
|
||||
RWStructuredBuffer<FractalSdfSplat> Splats : register(u0);
|
||||
RWStructuredBuffer<SdfEnvelopeReservoir> SdfReservoirs : register(u1);
|
||||
RWStructuredBuffer<PbrMaterialReservoir> PbrReservoirs : register(u2);
|
||||
RWStructuredBuffer<RadiosityReservoir> RadiosityReservoirs : register(u3);
|
||||
RWStructuredBuffer<FlameIterationState> FlameStates : register(u4);
|
||||
StructuredBuffer<FractalIfsTransform> ProgramTransforms : register(t0);
|
||||
|
||||
uint Hash(uint x)
|
||||
|
|
@ -74,6 +82,21 @@ float Random01(uint value)
|
|||
return (float)(Hash(value) & 16777215u) / 16777216.0;
|
||||
}
|
||||
|
||||
uint XorShift(inout uint state)
|
||||
{
|
||||
uint value = state == 0u ? 0xA341316Cu : state;
|
||||
value ^= value << 13;
|
||||
value ^= value >> 17;
|
||||
value ^= value << 5;
|
||||
state = value == 0u ? 0xA341316Cu : value;
|
||||
return state;
|
||||
}
|
||||
|
||||
float StateRandom01(inout uint state)
|
||||
{
|
||||
return (float)(XorShift(state) & 16777215u) / 16777216.0;
|
||||
}
|
||||
|
||||
float2 CubeTileFaceUv(float2 authoredPoint, float4 tileAddress)
|
||||
{
|
||||
float level = max(tileAddress.y, 0.0);
|
||||
|
|
@ -118,10 +141,19 @@ float3 FractalPoint(uint index, out float radius)
|
|||
{
|
||||
if (ProgramTransformCount > 0u && ProgramMode == 2u)
|
||||
{
|
||||
uint n = Hash(index ^ Seed);
|
||||
float2 p = 0.0;
|
||||
float material = 0.0;
|
||||
float support = 1.0;
|
||||
FlameIterationState state = FlameStates[index];
|
||||
uint n = asuint(state.randomStep.x);
|
||||
float step = state.randomStep.y;
|
||||
if (FrameIndex == 0u || n == 0u)
|
||||
{
|
||||
n = Hash(index ^ Seed);
|
||||
state.pointSupportMaterial = float4(0.0, 0.0, 1.0, 0.0);
|
||||
step = 0.0;
|
||||
}
|
||||
|
||||
float2 p = state.pointSupportMaterial.xy;
|
||||
float support = max(state.pointSupportMaterial.z, 0.000001);
|
||||
float material = state.pointSupportMaterial.w;
|
||||
float totalWeight = 0.0;
|
||||
[loop]
|
||||
for (uint weightIndex = 0u; weightIndex < ProgramTransformCount; weightIndex++)
|
||||
|
|
@ -132,7 +164,7 @@ float3 FractalPoint(uint index, out float radius)
|
|||
[loop]
|
||||
for (uint depth = 0; depth < Depth; depth++)
|
||||
{
|
||||
float target = Random01(n + depth * 747796405u + FrameIndex * 1664525u) * max(totalWeight, 0.000001);
|
||||
float target = StateRandom01(n) * max(totalWeight, 0.000001);
|
||||
float cumulative = 0.0;
|
||||
uint transformIndex = ProgramTransformCount - 1u;
|
||||
[loop]
|
||||
|
|
@ -168,7 +200,7 @@ float3 FractalPoint(uint index, out float radius)
|
|||
{
|
||||
float power = abs(transform.tileAddress.z) < 1.0 ? 1.0 : transform.tileAddress.z;
|
||||
float absPower = max(abs(power), 1.0);
|
||||
float branch = floor(Random01(n + depth * 2246822519u + 97u) * absPower);
|
||||
float branch = floor(StateRandom01(n) * absPower);
|
||||
float angle = (theta + 6.28318530717958647692 * branch) / power;
|
||||
float radial = pow(max(r, 0.000001), transform.materialSeedShape.w / power);
|
||||
nextPoint += julian * radial * float2(cos(angle), sin(angle));
|
||||
|
|
@ -178,11 +210,11 @@ float3 FractalPoint(uint index, out float radius)
|
|||
if (blur != 0.0)
|
||||
{
|
||||
float blurRadius = blur * (
|
||||
Random01(n + depth * 3266489917u + 11u) +
|
||||
Random01(n + depth * 3266489917u + 23u) +
|
||||
Random01(n + depth * 3266489917u + 37u) +
|
||||
Random01(n + depth * 3266489917u + 53u) - 2.0);
|
||||
float blurAngle = Random01(n + depth * 668265263u + 71u) * 6.28318530717958647692;
|
||||
StateRandom01(n) +
|
||||
StateRandom01(n) +
|
||||
StateRandom01(n) +
|
||||
StateRandom01(n) - 2.0);
|
||||
float blurAngle = StateRandom01(n) * 6.28318530717958647692;
|
||||
nextPoint += blurRadius * float2(cos(blurAngle), sin(blurAngle));
|
||||
}
|
||||
|
||||
|
|
@ -194,9 +226,12 @@ float3 FractalPoint(uint index, out float radius)
|
|||
p = nextPoint;
|
||||
material = transform.materialSeedShape.z;
|
||||
support *= saturate(max(length(m.xy), length(m.zw)));
|
||||
n = Hash(n + asuint(transform.materialSeedShape.w) + transformIndex + depth);
|
||||
}
|
||||
|
||||
FlameIterationState nextState;
|
||||
nextState.pointSupportMaterial = float4(p, support, material);
|
||||
nextState.randomStep = float4(asfloat(n), step + (float)Depth, 0.0, 0.0);
|
||||
FlameStates[index] = nextState;
|
||||
radius = max(0.0025 * max(support, 0.04), 0.00015);
|
||||
return float3(p, material * 0.08);
|
||||
}
|
||||
|
|
@ -301,13 +336,13 @@ float4 ReservoirStats(uint index, uint passKind, float baseTarget, out uint sele
|
|||
void D3D12FractalSplatReceiptCS(uint3 id : SV_DispatchThreadID)
|
||||
{
|
||||
uint updateIndex = id.x;
|
||||
if (updateIndex >= SplatCount)
|
||||
if (updateIndex >= SplatDispatchCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uint index = updateIndex;
|
||||
if (ProgramMode == 2u && FrameIndex > 0u)
|
||||
if (FrameIndex > 0u && SplatDispatchCount < SplatCount)
|
||||
{
|
||||
index = ReservoirIndex(updateIndex, 4u);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ public static class ZyphosSceneBuilder
|
|||
FractalReservoirField = new AquariumFractalReservoirField
|
||||
{
|
||||
SplatCount = 2_000_000,
|
||||
SplatUpdatesPerFrame = 50_000,
|
||||
ReservoirUpdatesPerPass = 20_000,
|
||||
WorldCenterRadius = new Vector4(ZyphosUmbrosSystem.ZyphosCenter, ZyphosUmbrosSystem.ZyphosSurfaceRadius),
|
||||
ProgramTransforms = fractalPlan.GpuProgramTransforms,
|
||||
|
|
|
|||
Loading…
Reference in a new issue