Bind blue noise grid dither in D3D12
This commit is contained in:
parent
2019b9e3b2
commit
db94ade4c1
5 changed files with 137 additions and 12 deletions
|
|
@ -23,6 +23,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
{
|
||||
private const int BackBufferCount = 2;
|
||||
private const int GridHeightTextureSize = 128;
|
||||
private const int DitherTextureSize = 512;
|
||||
private const int MediumFroxelDownscale = 8;
|
||||
private const int MediumFroxelAtlasColumns = 8;
|
||||
private const int MediumFroxelAtlasRows = 4;
|
||||
|
|
@ -38,6 +39,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const int BloomLevelCount = 3;
|
||||
private const float SunRadius = 1.12f;
|
||||
private const Format SceneHdrFormat = Format.R16G16B16A16_Float;
|
||||
private const string DitherTextureRelativePath = "Assets/Textures/Aetheria-LDR_LLL1_0.r8";
|
||||
private const string GridShaderRelativePath = "Render/Shaders/D3D12Grid.hlsl";
|
||||
private const string SmokeShaderRelativePath = "Render/Shaders/D3D12Smoke.hlsl";
|
||||
private const string MediumShaderRelativePath = "Render/Shaders/D3D12Medium.hlsl";
|
||||
|
|
@ -90,6 +92,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private D3D12RenderTarget gridHeightRenderTarget;
|
||||
private D3D12RenderTarget mediumVolumeRenderTarget;
|
||||
private D3D12RenderTarget mediumTransportRenderTarget;
|
||||
private readonly ID3D12Resource ditherTexture;
|
||||
private D3D12RenderTarget sceneRenderTarget;
|
||||
private D3D12RenderTarget sceneMetadataRenderTarget;
|
||||
private D3D12RenderTarget sceneControlRenderTarget;
|
||||
|
|
@ -102,6 +105,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private readonly D3D12StructuredBuffer fieldInstanceBuffer;
|
||||
private Int4[] froxelPrimitiveIds = [];
|
||||
private readonly FieldInstanceGpu[] fieldInstances = new FieldInstanceGpu[FieldInstanceCount];
|
||||
private readonly byte[] ditherTextureData;
|
||||
private readonly GridHeightBrushCpu[] gridHeightBrushes = new GridHeightBrushCpu[GridHeightBrushCount];
|
||||
private Viewport viewport;
|
||||
private RawRect scissorRect;
|
||||
|
|
@ -134,6 +138,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private bool pipelineBuildInProgressReported;
|
||||
private bool initialPipelineReadyReported;
|
||||
private bool hasPresentedReadyFrame;
|
||||
private bool ditherTextureUploaded;
|
||||
private static readonly TimeSpan ShaderReloadPollInterval = TimeSpan.FromMilliseconds(250);
|
||||
private static readonly TimeSpan ShaderReloadWriteSettleTime = TimeSpan.FromMilliseconds(300);
|
||||
|
||||
|
|
@ -188,6 +193,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
gridHeightRenderTarget = CreateGridHeightRenderTarget();
|
||||
mediumVolumeRenderTarget = CreateMediumVolumeRenderTarget("medium-volume-target", "Aquarium D3D12 Medium Volume Target");
|
||||
mediumTransportRenderTarget = CreateMediumVolumeRenderTarget("medium-transport-target", "Aquarium D3D12 Medium Transport Target");
|
||||
ditherTextureData = LoadDitherTexture(Path.Combine(AppContext.BaseDirectory, DitherTextureRelativePath));
|
||||
ditherTexture = CreateDitherTexture();
|
||||
sceneRenderTarget = CreateSceneRenderTarget();
|
||||
sceneMetadataRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-metadata-target", "Aquarium D3D12 Scene Metadata Target");
|
||||
sceneControlRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-control-target", "Aquarium D3D12 Scene Control Target");
|
||||
|
|
@ -330,6 +337,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
fieldInstanceBuffer.CreateShaderResourceView(device, frameResources.FieldInstanceDescriptor);
|
||||
frameResources.GridHeightDescriptor = frameResources.TransientShaderDescriptors.Allocate();
|
||||
gridHeightRenderTarget.CreateShaderResourceView(device, frameResources.GridHeightDescriptor);
|
||||
frameResources.DitherDescriptor = frameResources.TransientShaderDescriptors.Allocate();
|
||||
CreateDitherShaderResourceView(frameResources.DitherDescriptor);
|
||||
frameResources.MediumTargetsDescriptor = frameResources.TransientShaderDescriptors.Allocate();
|
||||
mediumVolumeRenderTarget.CreateShaderResourceView(device, frameResources.MediumTargetsDescriptor);
|
||||
frameResources.MediumTransportDescriptor = frameResources.TransientShaderDescriptors.Allocate();
|
||||
|
|
@ -368,6 +377,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
|
||||
var recordCpuStart = Stopwatch.GetTimestamp();
|
||||
commandList.BeginEvent("Aquarium D3D12 Frame");
|
||||
UploadDitherTextureIfNeeded(commandList, frameResources);
|
||||
UploadFieldResources(commandList, frameResources);
|
||||
RenderGridHeight(commandList, frameResources);
|
||||
if (ShouldRenderMediumVolume())
|
||||
|
|
@ -468,6 +478,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
fullscreenRootSignature.Dispose();
|
||||
fieldInstanceBuffer.Dispose();
|
||||
froxelPrimitiveBuffer.Dispose();
|
||||
ditherTexture.Dispose();
|
||||
DisposeBloomRenderTargets();
|
||||
DisposeHistoryRenderTargets();
|
||||
sceneRenderTarget.Dispose();
|
||||
|
|
@ -811,6 +822,50 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return target;
|
||||
}
|
||||
|
||||
private ID3D12Resource CreateDitherTexture()
|
||||
{
|
||||
var resource = device.CreateCommittedResource(
|
||||
HeapType.Default,
|
||||
ResourceDescription.Texture2D(
|
||||
Format.R8_UNorm,
|
||||
DitherTextureSize,
|
||||
DitherTextureSize,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
Vortice.Direct3D12.ResourceFlags.None),
|
||||
ResourceStates.CopyDest,
|
||||
null);
|
||||
resource.Name = "Aquarium D3D12 Aetheria Blue Noise Dither Texture";
|
||||
return resource;
|
||||
}
|
||||
|
||||
private static byte[] LoadDitherTexture(string path)
|
||||
{
|
||||
var data = File.ReadAllBytes(path);
|
||||
if (data.Length != DitherTextureSize * DitherTextureSize)
|
||||
{
|
||||
throw new InvalidOperationException($"Dither texture {path} must be {DitherTextureSize}x{DitherTextureSize} R8 data.");
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private void CreateDitherShaderResourceView(D3D12DescriptorSlot descriptor)
|
||||
{
|
||||
device.CreateShaderResourceView(
|
||||
ditherTexture,
|
||||
new ShaderResourceViewDescription
|
||||
{
|
||||
Format = Format.R8_UNorm,
|
||||
ViewDimension = Vortice.Direct3D12.ShaderResourceViewDimension.Texture2D,
|
||||
Shader4ComponentMapping = ShaderComponentMapping.Default,
|
||||
Texture2D = new Texture2DShaderResourceView { MipLevels = 1 },
|
||||
},
|
||||
descriptor.Cpu);
|
||||
}
|
||||
|
||||
private D3D12RenderTarget CreateMediumVolumeRenderTarget(string registryName, string resourceName)
|
||||
{
|
||||
var target = new D3D12RenderTarget(
|
||||
|
|
@ -847,6 +902,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
context.CommandList.SetGraphicsRootSignature(fullscreenRootSignature);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(0, frameResources.FrameConstantsDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(1, frameResources.GridHeightDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(12, frameResources.DitherDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(3, frameResources.FroxelPrimitiveDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(4, frameResources.FieldInstanceDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(5, frameResources.MediumTargetsDescriptor.Gpu);
|
||||
|
|
@ -1022,6 +1078,47 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
}
|
||||
}
|
||||
|
||||
private void UploadDitherTextureIfNeeded(ID3D12GraphicsCommandList activeCommandList, FrameResources frameResources)
|
||||
{
|
||||
if (ditherTextureUploaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
activeCommandList.BeginEvent("Dither Texture Upload");
|
||||
try
|
||||
{
|
||||
var upload = frameResources.UploadRing.WriteArray<byte>(ditherTextureData);
|
||||
var footprint = new SubresourceFootPrint(
|
||||
Format.R8_UNorm,
|
||||
DitherTextureSize,
|
||||
DitherTextureSize,
|
||||
1,
|
||||
DitherTextureSize);
|
||||
var placedFootprint = new PlacedSubresourceFootPrint
|
||||
{
|
||||
Offset = (ulong)upload.OffsetBytes,
|
||||
Footprint = footprint,
|
||||
};
|
||||
activeCommandList.CopyTextureRegion(
|
||||
new TextureCopyLocation(ditherTexture, 0),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
new TextureCopyLocation(frameResources.UploadRing.Resource, placedFootprint),
|
||||
null);
|
||||
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierTransition(
|
||||
ditherTexture,
|
||||
ResourceStates.CopyDest,
|
||||
ResourceStates.PixelShaderResource));
|
||||
ditherTextureUploaded = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
activeCommandList.EndEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private void RenderGridHeight(ID3D12GraphicsCommandList activeCommandList, FrameResources frameResources)
|
||||
{
|
||||
var viewport = new Viewport(0.0f, 0.0f, GridHeightTextureSize, GridHeightTextureSize);
|
||||
|
|
@ -1785,6 +1882,12 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
8,
|
||||
0,
|
||||
D3D12.DescriptorRangeOffsetAppend);
|
||||
var ditherRange = new DescriptorRange(
|
||||
DescriptorRangeType.ShaderResourceView,
|
||||
1,
|
||||
15,
|
||||
0,
|
||||
D3D12.DescriptorRangeOffsetAppend);
|
||||
var rootParameters = new[]
|
||||
{
|
||||
new RootParameter(new RootDescriptorTable([constantBufferRange]), ShaderVisibility.All),
|
||||
|
|
@ -1799,10 +1902,25 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
new RootParameter(new RootDescriptorTable([historyRange]), ShaderVisibility.Pixel),
|
||||
new RootParameter(new RootDescriptorTable([historyMetadataRange]), ShaderVisibility.Pixel),
|
||||
new RootParameter(new RootDescriptorTable([historyControlRange]), ShaderVisibility.Pixel),
|
||||
new RootParameter(new RootDescriptorTable([ditherRange]), ShaderVisibility.Pixel),
|
||||
};
|
||||
var staticSamplers = new[]
|
||||
{
|
||||
new StaticSamplerDescription(ShaderVisibility.Pixel, 0, 0),
|
||||
new StaticSamplerDescription(
|
||||
new Vortice.Direct3D12.SamplerDescription(
|
||||
Filter.MinMagMipPoint,
|
||||
TextureAddressMode.Wrap,
|
||||
TextureAddressMode.Wrap,
|
||||
TextureAddressMode.Wrap,
|
||||
0.0f,
|
||||
0,
|
||||
ComparisonFunction.Never,
|
||||
0.0f,
|
||||
float.MaxValue),
|
||||
ShaderVisibility.Pixel,
|
||||
1,
|
||||
0),
|
||||
};
|
||||
var description = new RootSignatureDescription(
|
||||
RootSignatureFlags.AllowInputAssemblerInputLayout,
|
||||
|
|
@ -2212,6 +2330,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
|
||||
public D3D12DescriptorSlot GridHeightDescriptor { get; set; }
|
||||
|
||||
public D3D12DescriptorSlot DitherDescriptor { get; set; }
|
||||
|
||||
public D3D12DescriptorSlot MediumTargetsDescriptor { get; set; }
|
||||
|
||||
public D3D12DescriptorSlot MediumTransportDescriptor { get; set; }
|
||||
|
|
|
|||
|
|
@ -26,7 +26,9 @@ Texture2D<float4> gridHeightTexture : register(t0);
|
|||
StructuredBuffer<int4> froxelPrimitiveIds : register(t1);
|
||||
Texture2D<float4> mediumVolumeTexture : register(t13);
|
||||
Texture2D<float4> mediumTransportTexture : register(t14);
|
||||
Texture2D<float> ditherTexture : register(t15);
|
||||
SamplerState gridSampler : register(s0);
|
||||
SamplerState ditherSampler : register(s1);
|
||||
|
||||
static const int PLANET_COUNT = 5;
|
||||
static const float SUN_RADIUS = 1.12;
|
||||
|
|
@ -51,6 +53,7 @@ static const float TERRAIN_ISOLINE_PIXEL_WIDTH = 0.54;
|
|||
static const float TERRAIN_FIELD_LINE_PIXEL_WIDTH = 0.38;
|
||||
static const float3 GRID_COLOR = float3(0.30, 0.90, 0.82);
|
||||
static const float GRID_ALPHA_SCALE = 0.24;
|
||||
static const float DITHER_TEXTURE_SIZE = 512.0;
|
||||
|
||||
struct VertexOut
|
||||
{
|
||||
|
|
@ -94,16 +97,10 @@ VertexOut FullscreenTriangleVS(uint vertexId : SV_VertexID)
|
|||
return output;
|
||||
}
|
||||
|
||||
float hash21(float2 p)
|
||||
{
|
||||
p = frac(p * float2(123.34, 456.21));
|
||||
p += dot(p, p + 45.32);
|
||||
return frac(p.x * p.y);
|
||||
}
|
||||
|
||||
float stochasticTransparency(float2 screenUv, float alpha)
|
||||
{
|
||||
float dither = hash21(floor(screenUv * resolution));
|
||||
float2 ditherUv = screenUv * (resolution / DITHER_TEXTURE_SIZE);
|
||||
float dither = ditherTexture.SampleLevel(ditherSampler, ditherUv, 0.0).r;
|
||||
return alpha - dither - 0.001 * (1.0 - ceil(alpha));
|
||||
}
|
||||
|
||||
|
|
@ -125,6 +122,13 @@ float3 rayDirectionForPixel(float2 pixel, float2 jitter, float3 camera, float2 c
|
|||
return normalize(forward * 1.6 + right * ndc.x + up * ndc.y);
|
||||
}
|
||||
|
||||
float hash21(float2 p)
|
||||
{
|
||||
p = frac(p * float2(123.34, 456.21));
|
||||
p += dot(p, p + 45.32);
|
||||
return frac(p.x * p.y);
|
||||
}
|
||||
|
||||
float planetRadius(int index)
|
||||
{
|
||||
return lerp(0.34, 0.62, hash21(float2(index, 19.7)));
|
||||
|
|
|
|||
|
|
@ -101,3 +101,4 @@
|
|||
{"timestamp":"2026-05-09T22:01:24.4341856+01:00","topic":"d3d12-grid-depth-residency","lesson":"Depth-sparse CPU binning for the Grid is the wrong bootstrap contract. At grazing angles the Grid is a global transparent height sheet and guessed crossing slice ranges produce sliced bands. Bin the Grid by low-res screen tube and write it to all depth slices for tubes that may see the Grid radius; the shader exact height-sheet intersection remains the authority for whether an event exists inside each interval.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T22:18:03.0000000+01:00","topic":"d3d12-grid-stochastic-retreat","lesson":"The D3D12 transparent froxel candidate experiment made the Grid angle-dependent, spiky, and brittle. The Grid should follow the proven D3D11 contract: trace it directly as a stochastic surface before the nearest solid, write Grid identity/travel/normal/coverage for TAA, and do not leave dead transparent-surface binning code in the renderer. Git is the archive.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T22:30:46.5567253+01:00","topic":"d3d12-grid-width-stochastic-reference","lesson":"The D3D11 reference Grid used periodic line domains with fwidth() and pixel-width constants for stable line width. The D3D12 world-space ddx/ddy footprint made width angle-dependent. D3D12 now uses the D3D11 line/contour/field-line width math and a screen-pixel-stable hash dither; the remaining quality gap is binding Aetheria blue-noise texture for proper stochastic transparency.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T22:43:12.0000000+01:00","topic":"d3d12-grid-blue-noise-dither","lesson":"D3D12 Grid stochastic coverage now uses Aetheria's copied 512x512 R8 blue-noise texture instead of a hash. The texture is uploaded once to a default-heap R8_UNorm resource, exposed through a transient SRV at t15, and sampled point/wrap at s1 in screen space without a frame-wide animated seed so per-pixel coverage stays stable for TAA.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
"D3D12 Grid height must be brush-rendered, not a fullscreen per-texel loop over bodies. Use a top-down Grid-space orthographic brush pass: base field draw, then one additive up-facing quad per body into a scalar R16_Float height target. Height is one value; do not spend extra channels unless the pass contract actually gains more data. Do not use R32G32B32A32_Float for additive brush composition.",
|
||||
"D3D12 field data should live in default-heap structured buffers, updated from the per-frame upload ring by CopyBufferRegion, then bound through transient SRV descriptors owned by the consuming pass. Do not make shaders read dynamic field tables straight from upload memory. D3D11 is reference-only during migration; do not pay DRY tax to preserve it.",
|
||||
"Do not put the D3D12 Grid through transparent froxel candidate/event traversal. The one-pass transparent binning experiment made the Grid angle-dependent and brittle. The live Grid contract is direct scene tracing as a stochastic surface before the nearest solid, writing Grid identity/travel/normal/coverage for TAA accumulation. Git holds the old experiment; runtime code should stay lean.",
|
||||
"D3D12 Grid line width normalization should match the D3D11 reference: use periodic line domains with `fwidth()` and pixel-width constants, not world-space `ddx/ddy(p.xy)` footprint guesses. Hash dither is only a temporary D3D12 fallback; bind Aetheria's blue-noise texture for the proper stochastic mask.",
|
||||
"D3D12 Grid line width normalization should match the D3D11 reference: use periodic line domains with `fwidth()` and pixel-width constants, not world-space `ddx/ddy(p.xy)` footprint guesses. D3D12 binds Aetheria's 512x512 R8 blue-noise texture at `t15` with point/wrap sampling at `s1` for screen-space stochastic coverage; keep it screen-stable unless temporal animation is deliberately designed.",
|
||||
"D3D12 solid scene candidate discovery belongs in the view-froxel volume. At 720p the active tube is 160x90x32; primitive ids are indexed by low-res screen cell and depth slice, then scene rays test exact solid intersections inside that slice interval. The CPU-filled view-froxel buffers are bootstrap only; the durable path should become GPU/raster binning so per-frame upload bandwidth does not become the next stupid tax.",
|
||||
"D3D12 pixel shader UAV registers share the output namespace. If a shader writes SV_Target0, bind the first manual UAV at u1, not u0.",
|
||||
"D3D12 shader routing derives its editable source directory from --shader-source/AQUARIUM_SHADER_SOURCE, then watches D3D12Grid/Medium/Scene/Post/Smoke HLSL files directly. Shader/PSO builds run on a background task; startup presents a lightweight loading frame until the first pipeline set is ready, and hot reload keeps the previous pipeline set if compilation fails.",
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ for empty-space skipping; transparent surface candidate buffers, descriptors,
|
|||
root parameters, shader structs, and event traversal were cut rather than left
|
||||
as dead scaffolding. D3D12 Grid line width now mirrors the D3D11 reference:
|
||||
periodic domains use `fwidth()` and pixel-width constants instead of world-space
|
||||
`ddx/ddy` footprint guesses. The temporary D3D12 stochastic mask is screen-pixel
|
||||
stable; next polish pass should bind the Aetheria blue-noise texture like D3D11
|
||||
instead of keeping a hash dither.
|
||||
`ddx/ddy` footprint guesses. The stochastic mask now samples Aetheria's copied
|
||||
512x512 R8 blue-noise texture in screen space through D3D12 `t15/s1`, point and
|
||||
wrap sampled, without a frame-wide animation seed.
|
||||
|
||||
## Hot Context
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue