diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 976333a..3ba5b4c 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -1967,7 +1967,7 @@ public sealed class D3D12Renderer : IAquariumRenderer else { Step("reservoir-history-update"); - reservoirHistoryUpdate = CreateReservoirHistoryUpdatePipelineState(paths.Post); + reservoirHistoryUpdate = CreateReservoirHistoryUpdatePipelineState(paths.ReservoirHistoryUpdate); reservoirHistoryUpdate.Name = "Aquarium D3D12 Reservoir History Update Pipeline"; } Step("resolve"); @@ -6155,9 +6155,10 @@ public sealed class D3D12Renderer : IAquariumRenderer IReadOnlyList SdfShaders, string SdfMath, IReadOnlyList Includes, - string Post) + string Post, + string ReservoirHistoryUpdate) { - public IReadOnlyList All { get; } = [HeightField, Scene, Spline, TemporalGaussian, GpuSensorFusion, StereoDepth, PointCloud, TubeField, FractalReservoir, FractalSplatRender, SdfCommon, SdfProxy, ..SdfShaders, SdfMath, ..Includes, Post]; + public IReadOnlyList All { get; } = [HeightField, Scene, Spline, TemporalGaussian, GpuSensorFusion, StereoDepth, PointCloud, TubeField, FractalReservoir, FractalSplatRender, SdfCommon, SdfProxy, ..SdfShaders, SdfMath, ..Includes, Post, ReservoirHistoryUpdate]; public static D3D12ShaderPaths FromManifest(string root, AquariumShaderManifest manifest) { @@ -6181,7 +6182,8 @@ public sealed class D3D12Renderer : IAquariumRenderer manifest.SdfShaderPaths.Select(shaderPath).ToArray(), shaderPath(manifest.SdfMathInclude), [.. includes.Select(shaderPath), shaderPath("D3D12Aces2.hlsl")], - shaderPath(manifest.PostShader)); + shaderPath(manifest.PostShader), + shaderPath("D3D12ReservoirHistoryUpdate.hlsl")); } } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12ReservoirHistoryUpdate.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12ReservoirHistoryUpdate.hlsl new file mode 100644 index 0000000..17aa4fb --- /dev/null +++ b/src/Aquarium.Engine/Render/Shaders/D3D12ReservoirHistoryUpdate.hlsl @@ -0,0 +1,116 @@ +cbuffer AquariumFrame : register(b0) +{ + float2 resolution; + float timeSeconds; + float viewRadius; + float3 cameraPosition; + float farDistance; + float3 cameraTarget; + float sceneFlags; + float2 viewCenter; + float frameIndex; + float previousTimeSeconds; + float3 previousCameraPosition; + float previousViewRadius; + float3 previousCameraTarget; + float previousSceneFlags; + float2 previousViewCenter; + float2 jitterPixels; + float2 previousJitterPixels; + float renderDebugMode; + float exposure; + float bloomIntensity; + float bloomVeilIntensity; + float4 cursorWorlds; + float4 temporalGaussianInfo; + float4 cameraFrustumXy; + float4 cameraFrustumZ; + float4 gpuFusionInfo; + float4 fractalReservoirInfo; + float4 fractalReservoirFrame; + float4 reservoirBudgetInfo; +}; + +Texture2D sourceTexture : register(t0); +Texture2D currentSceneMetadataTexture : register(t5); +Texture2D currentSceneControlTexture : register(t7); +Texture2D currentReservoirGuideTexture : register(t26); + +#include "D3D12FieldReservoir.hlsli" + +StructuredBuffer fieldReservoirCandidates : register(t45); +RWStructuredBuffer reservoirHistoryWrite : register(u22); +RWTexture2D reservoirResolvedTexture : register(u23); + +float4 loadTexturePixel(Texture2D source, uint2 pixel) +{ + return source.Load(int3(pixel, 0)); +} + +FieldReservoirSample sceneFieldReservoirSample(uint2 pixel, float2 uv) +{ + float4 colorTravel = loadTexturePixel(sourceTexture, pixel); + float4 metadata = loadTexturePixel(currentSceneMetadataTexture, pixel); + float4 control = loadTexturePixel(currentSceneControlTexture, pixel); + float4 guide = loadTexturePixel(currentReservoirGuideTexture, pixel); + float target = fieldReservoirDefaultTarget(colorTravel, control, guide); + return makeFieldReservoirSample( + colorTravel, + metadata, + control, + guide, + float4(uv, colorTravel.w, 0.0), + float4(uv, uv), + float4(1.0, 1.0, FieldDomainKindScreen, FieldShiftKindNone), + target, + 1.0, + 1.0, + FieldProposalKindDeterministicStructural); +} + +FieldReservoirSample currentFrameReservoirSample(uint2 pixel, float2 uv) +{ + uint width = (uint)max(resolution.x, 1.0); + uint baseIndex = (pixel.y * width + pixel.x) * FieldReservoirSlotsPerPixel; + FieldReservoirSample current = sceneFieldReservoirSample(pixel, uv); + FieldReservoirSample candidate = fieldReservoirCandidates[baseIndex + FieldReservoirRowCurrent]; + current = mergeFieldReservoirVisibilityProposals( + current, + candidate, + fieldReservoirRandom01(pixel, (uint)frameIndex, 31u), + farDistance); + current.guide.y = 0.0; + current.guide.w = 0.0; + return current; +} + +float4 reservoirHistoryColor(FieldReservoirSample sample) +{ + if (!fieldReservoirSampleValid(sample, farDistance)) + { + return float4(0.0, 0.0, 0.0, farDistance + 1.0); + } + + return float4(fieldReservoirResolvedColor(sample, farDistance), sample.colorTravel.w); +} + +[numthreads(8, 8, 1)] +void D3D12ReservoirHistoryUpdateCS(uint3 dispatchThreadId : SV_DispatchThreadID) +{ + uint2 dimensions = (uint2)max(resolution, float2(1.0, 1.0)); + if (dispatchThreadId.x >= dimensions.x || dispatchThreadId.y >= dimensions.y) + { + return; + } + + uint2 pixel = dispatchThreadId.xy; + float2 uv = (float2(pixel) + 0.5) / float2(dimensions); + uint baseIndex = (pixel.y * dimensions.x + pixel.x) * FieldReservoirSlotsPerPixel; + FieldReservoirSample current = currentFrameReservoirSample(pixel, uv); + + reservoirHistoryWrite[baseIndex + FieldReservoirRowCurrent] = current; + reservoirHistoryWrite[baseIndex + FieldReservoirRowTemporal] = current; + reservoirHistoryWrite[baseIndex + FieldReservoirRowSpatial] = current; + reservoirHistoryWrite[baseIndex + FieldReservoirRowFinal] = current; + reservoirResolvedTexture[pixel] = reservoirHistoryColor(current); +}