From 231461c44a270626fa30d4730370dcdc1941de4a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 15 Jun 2026 22:53:09 +0100 Subject: [PATCH] Add DirectCompute Bokusho page deposition --- src/Aquarium.Engine/Render/D3D12Renderer.cs | 62 +++++- .../Render/Shaders/D3D12BokushoBrush.hlsl | 197 ++++++++++++++++++ .../Render/Shaders/D3D12HeightField.hlsl | 14 ++ 3 files changed, 269 insertions(+), 4 deletions(-) diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index a7597e5..662bb5d 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -139,10 +139,12 @@ public sealed class D3D12Renderer : IAquariumRenderer private const int RootBokushoBrushCanvas = 2; private const int RootBokushoBrushTips = 3; private const int RootBokushoBrushStrokes = 4; + private const int RootBokushoBrushPage = 5; private const int RootBokushoPageConstants = 24; private const int RootBokushoPageCanvas = 25; private const int RootBokushoPageStrokes = 26; private const int RootBokushoPageTips = 27; + private const int RootBokushoPageDensity = 28; private static readonly DebugUi.DebugUiOption[] RenderDebugOptions = [ new(0, "Final"), @@ -231,6 +233,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private ID3D12PipelineState? tubeFieldComputePipelineState; private ID3D12PipelineState? tubeFieldRenderPipelineState; private ID3D12PipelineState? bokushoBrushPipelineState; + private ID3D12PipelineState? bokushoPageClearPipelineState; + private ID3D12PipelineState? bokushoPageDepositPipelineState; private ID3D12PipelineState? fieldReservoirResolvePipelineState; private ID3D12PipelineState?[] sdfProxyPipelineStates = []; private ID3D12PipelineState? bloomPrefilterPipelineState; @@ -303,6 +307,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private readonly D3D12StructuredBuffer bokushoBrushCanvasBuffer; private readonly D3D12StructuredBuffer bokushoBrushTipBuffer; private readonly D3D12StructuredBuffer bokushoBrushStrokeBuffer; + private readonly D3D12StructuredBuffer bokushoPageDensityBuffer; private D3D12StructuredBuffer fieldReservoirCandidateBuffer = null!; private D3D12StructuredBuffer fieldReservoirLockBuffer = null!; private readonly List tubeFieldDrawBatches = []; @@ -389,6 +394,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private Vector3 previousCameraTarget; private Vector3 activeCameraPosition; private Vector3 activeCameraTarget; + private Vector2 activeViewCenter; + private float activeViewRadius; private Vector2 previousViewCenter; private Vector2 previousCursorWorld; private Vector2 previousJitterPixels; @@ -525,6 +532,7 @@ public sealed class D3D12Renderer : IAquariumRenderer bokushoBrushCanvasBuffer = new D3D12StructuredBuffer(device, MaxBokushoBrushValues, sizeof(float), "Aquarium D3D12 Bokusho Brush Canvas Field", allowUnorderedAccess: true); bokushoBrushTipBuffer = new D3D12StructuredBuffer(device, MaxBokushoBrushValues, Marshal.SizeOf(), "Aquarium D3D12 Bokusho Brush Tip Field", allowUnorderedAccess: true); bokushoBrushStrokeBuffer = new D3D12StructuredBuffer(device, MaxBokushoBrushStrokes, Marshal.SizeOf(), "Aquarium D3D12 Bokusho Brush Stroke Packet Buffer"); + bokushoPageDensityBuffer = new D3D12StructuredBuffer(device, HeightFieldTextureSize * HeightFieldTextureSize, sizeof(uint), "Aquarium D3D12 Bokusho Page Density Field", allowUnorderedAccess: true); CreateFieldReservoirBuffers(); resourceRegistry.Add("sdf-light-buffer", sdfLightBuffer); resourceRegistry.Add("sdf-object-buffer", sdfObjectBuffer); @@ -543,6 +551,7 @@ public sealed class D3D12Renderer : IAquariumRenderer resourceRegistry.Add("bokusho-brush-canvas-buffer", bokushoBrushCanvasBuffer); resourceRegistry.Add("bokusho-brush-tip-buffer", bokushoBrushTipBuffer); resourceRegistry.Add("bokusho-brush-stroke-buffer", bokushoBrushStrokeBuffer); + resourceRegistry.Add("bokusho-page-density-buffer", bokushoPageDensityBuffer); commandList = device.CreateCommandList(0, CommandListType.Direct, frames[frameIndex].CommandAllocator, null); commandList.Name = "Aquarium D3D12 Graphics Command List"; commandList.Close(); @@ -1307,6 +1316,8 @@ public sealed class D3D12Renderer : IAquariumRenderer CopySceneState(frame.Scene); activeCameraPosition = frame.CameraPosition; activeCameraTarget = frame.CameraTarget; + activeViewCenter = frame.View.Center; + activeViewRadius = frame.View.Radius; EnsureFractalReservoirBuffers(activeFractalReservoirField); EnsureFractalProgramTransformBuffer(); EnsureBufferFieldProgramBuffers(); @@ -1615,6 +1626,8 @@ public sealed class D3D12Renderer : IAquariumRenderer && tubeFieldComputePipelineState is not null && tubeFieldRenderPipelineState is not null && bokushoBrushPipelineState is not null + && bokushoPageClearPipelineState is not null + && bokushoPageDepositPipelineState is not null && fieldReservoirResolvePipelineState is not null && sdfProxyPipelineStates.All(pipeline => pipeline is not null) && bloomPrefilterPipelineState is not null @@ -1677,6 +1690,7 @@ public sealed class D3D12Renderer : IAquariumRenderer bokushoBrushTipBuffer.Dispose(); bokushoBrushTraceBuffer.Dispose(); bokushoBrushStrokeBuffer.Dispose(); + bokushoPageDensityBuffer.Dispose(); tubeFieldSegmentBuffer.Dispose(); tubeFieldIndexBuffer.Dispose(); tubeFieldVertexBuffer.Dispose(); @@ -2344,6 +2358,12 @@ public sealed class D3D12Renderer : IAquariumRenderer Step("bokusho-brush"); var bokushoBrush = CreateBokushoBrushPipelineState(paths.BokushoBrush); bokushoBrush.Name = "Aquarium D3D12 Bokusho Brush Compute Pipeline"; + Step("bokusho-page-clear"); + var bokushoPageClear = CreateBokushoBrushPipelineState(paths.BokushoBrush, "D3D12BokushoPageClearCS"); + bokushoPageClear.Name = "Aquarium D3D12 Bokusho Page Clear Compute Pipeline"; + Step("bokusho-page-deposit"); + var bokushoPageDeposit = CreateBokushoBrushPipelineState(paths.BokushoBrush, "D3D12BokushoPageDepositCS"); + bokushoPageDeposit.Name = "Aquarium D3D12 Bokusho Page Deposit Compute Pipeline"; Step("field-reservoir-resolve"); var fieldReservoirResolve = CreateFieldReservoirResolvePipelineState(paths.Post); fieldReservoirResolve.Name = "Aquarium D3D12 Field Reservoir Resolve Pipeline"; @@ -2401,6 +2421,8 @@ public sealed class D3D12Renderer : IAquariumRenderer tubeFieldCompute, tubeFieldRender, bokushoBrush, + bokushoPageClear, + bokushoPageDeposit, fieldReservoirResolve, sdfProxies, bloomPrefilter, @@ -4040,6 +4062,8 @@ public sealed class D3D12Renderer : IAquariumRenderer { if (!activeBokushoBrushFrame.HasInput || bokushoBrushPipelineState is null || + bokushoPageClearPipelineState is null || + bokushoPageDepositPipelineState is null || tubeFieldComputePipelineState is null || tubeFieldDrawBatches.Count >= MaxTubeFieldDrawBatches) { @@ -4061,13 +4085,15 @@ public sealed class D3D12Renderer : IAquariumRenderer frame.StrokeP0, frame.StrokeP1, frame.StrokeP2, - frame.StrokeP3); + frame.StrokeP3, + new Vector4(activeViewCenter.X, activeViewCenter.Y, Math.Max(activeViewRadius, 0.001f), HeightFieldTextureSize)); var constantsUpload = frameResources.UploadRing.WriteConstant(constants); bokushoBrushStrokeBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, strokes.AsSpan(0, strokeCount)); bokushoBrushTraceBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess); bokushoBrushCanvasBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess); bokushoBrushTipBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess); + bokushoPageDensityBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess); bokushoBrushStrokeBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); activeCommandList.SetComputeRootSignature(bokushoBrushRootSignature); activeCommandList.SetPipelineState(bokushoBrushPipelineState); @@ -4076,11 +4102,19 @@ public sealed class D3D12Renderer : IAquariumRenderer activeCommandList.SetComputeRootUnorderedAccessView(RootBokushoBrushCanvas, bokushoBrushCanvasBuffer.Resource.GPUVirtualAddress); activeCommandList.SetComputeRootUnorderedAccessView(RootBokushoBrushTips, bokushoBrushTipBuffer.Resource.GPUVirtualAddress); activeCommandList.SetComputeRootShaderResourceView(RootBokushoBrushStrokes, bokushoBrushStrokeBuffer.Resource.GPUVirtualAddress); + activeCommandList.SetComputeRootUnorderedAccessView(RootBokushoBrushPage, bokushoPageDensityBuffer.Resource.GPUVirtualAddress); activeCommandList.Dispatch((uint)(((tuftCount * strokeCount) + 127) / 128), 1, 1); activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(bokushoBrushTraceBuffer.Resource)); activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(bokushoBrushCanvasBuffer.Resource)); activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(bokushoBrushTipBuffer.Resource)); + activeCommandList.SetPipelineState(bokushoPageClearPipelineState); + activeCommandList.Dispatch((uint)(((HeightFieldTextureSize * HeightFieldTextureSize) + 127) / 128), 1, 1); + activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(bokushoPageDensityBuffer.Resource)); + activeCommandList.SetPipelineState(bokushoPageDepositPipelineState); + activeCommandList.Dispatch((uint)(((tuftCount * strokeCount) + 127) / 128), 1, 1); + activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(bokushoPageDensityBuffer.Resource)); + var subdivisions = 2; var requestedSegments = checked((sampleCount - 1) * subdivisions * tuftCount * strokeCount); activeTubeFieldRequestedSegments += requestedSegments; @@ -4587,10 +4621,12 @@ public sealed class D3D12Renderer : IAquariumRenderer bokushoBrushCanvasBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); bokushoBrushTipBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); bokushoBrushStrokeBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); + bokushoPageDensityBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); activeCommandList.SetGraphicsRootConstantBufferView(RootBokushoPageConstants, pageConstantsUpload.GpuVirtualAddress); activeCommandList.SetGraphicsRootShaderResourceView(RootBokushoPageCanvas, bokushoBrushCanvasBuffer.Resource.GPUVirtualAddress); activeCommandList.SetGraphicsRootShaderResourceView(RootBokushoPageStrokes, bokushoBrushStrokeBuffer.Resource.GPUVirtualAddress); activeCommandList.SetGraphicsRootShaderResourceView(RootBokushoPageTips, bokushoBrushTipBuffer.Resource.GPUVirtualAddress); + activeCommandList.SetGraphicsRootShaderResourceView(RootBokushoPageDensity, bokushoPageDensityBuffer.Resource.GPUVirtualAddress); activeCommandList.RSSetViewports(viewport); activeCommandList.RSSetScissorRects(scissorRect); activeCommandList.OMSetRenderTargets(heightFieldRenderTarget.RenderTargetView.Cpu, null); @@ -4755,6 +4791,7 @@ public sealed class D3D12Renderer : IAquariumRenderer Vector4.Zero, Vector4.Zero, Vector4.Zero, + Vector4.Zero, Vector4.Zero); } @@ -4770,7 +4807,8 @@ public sealed class D3D12Renderer : IAquariumRenderer frame.StrokeP0, frame.StrokeP1, frame.StrokeP2, - frame.StrokeP3); + frame.StrokeP3, + new Vector4(activeViewCenter.X, activeViewCenter.Y, Math.Max(activeViewRadius, 0.001f), HeightFieldTextureSize)); } private int BoundedBokushoStrokeCount(AquariumBokushoBrushFrame frame, int sampleCount, int tuftCount) @@ -5799,6 +5837,8 @@ public sealed class D3D12Renderer : IAquariumRenderer tubeFieldComputePipelineState!, tubeFieldRenderPipelineState!, bokushoBrushPipelineState!, + bokushoPageClearPipelineState!, + bokushoPageDepositPipelineState!, fieldReservoirResolvePipelineState!, sdfProxyPipelineStates.Select(pipeline => pipeline!).ToArray(), bloomPrefilterPipelineState!, @@ -5830,6 +5870,8 @@ public sealed class D3D12Renderer : IAquariumRenderer tubeFieldComputePipelineState = pipelines.TubeFieldCompute; tubeFieldRenderPipelineState = pipelines.TubeFieldRender; bokushoBrushPipelineState = pipelines.BokushoBrush; + bokushoPageClearPipelineState = pipelines.BokushoPageClear; + bokushoPageDepositPipelineState = pipelines.BokushoPageDeposit; fieldReservoirResolvePipelineState = pipelines.FieldReservoirResolve; for (var index = 0; index < sdfProxyPipelineStates.Length; index++) { @@ -5863,6 +5905,8 @@ public sealed class D3D12Renderer : IAquariumRenderer tubeFieldComputePipelineState = null; tubeFieldRenderPipelineState = null; bokushoBrushPipelineState = null; + bokushoPageClearPipelineState = null; + bokushoPageDepositPipelineState = null; Array.Clear(sdfProxyPipelineStates); bloomPrefilterPipelineState = null; bloomDownsamplePipelineState = null; @@ -6068,6 +6112,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(77, 0), ShaderVisibility.All), new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(78, 0), ShaderVisibility.All), new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(79, 0), ShaderVisibility.All), + new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(80, 0), ShaderVisibility.All), }; var staticSamplers = new[] { @@ -6296,6 +6341,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(21, 0), ShaderVisibility.All), new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(22, 0), ShaderVisibility.All), new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(78, 0), ShaderVisibility.All), + new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(23, 0), ShaderVisibility.All), }; var description = new RootSignatureDescription( RootSignatureFlags.None, @@ -6585,8 +6631,11 @@ public sealed class D3D12Renderer : IAquariumRenderer } private ID3D12PipelineState CreateBokushoBrushPipelineState(string path) + => CreateBokushoBrushPipelineState(path, "D3D12BokushoBrushCS"); + + private ID3D12PipelineState CreateBokushoBrushPipelineState(string path, string entryPoint) { - var computeShader = CompileShader(path, "D3D12BokushoBrushCS", "cs_5_0"); + var computeShader = CompileShader(path, entryPoint, "cs_5_0"); var description = new ComputePipelineStateDescription { RootSignature = bokushoBrushRootSignature, @@ -7035,7 +7084,8 @@ public sealed class D3D12Renderer : IAquariumRenderer Vector4 StrokeP0, Vector4 StrokeP1, Vector4 StrokeP2, - Vector4 StrokeP3); + Vector4 StrokeP3, + Vector4 PageView); [StructLayout(LayoutKind.Sequential)] private readonly record struct D3D12BokushoBrushStrokePacket( @@ -7197,6 +7247,8 @@ public sealed class D3D12Renderer : IAquariumRenderer ID3D12PipelineState TubeFieldCompute, ID3D12PipelineState TubeFieldRender, ID3D12PipelineState BokushoBrush, + ID3D12PipelineState BokushoPageClear, + ID3D12PipelineState BokushoPageDeposit, ID3D12PipelineState FieldReservoirResolve, IReadOnlyList SdfProxies, ID3D12PipelineState BloomPrefilter, @@ -7215,6 +7267,8 @@ public sealed class D3D12Renderer : IAquariumRenderer BloomDownsample.Dispose(); BloomPrefilter.Dispose(); FieldReservoirResolve.Dispose(); + BokushoPageDeposit.Dispose(); + BokushoPageClear.Dispose(); BokushoBrush.Dispose(); FractalRadiosityReservoir.Dispose(); TubeFieldRender.Dispose(); diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index 45ea98f..79fbde6 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -10,6 +10,7 @@ cbuffer BokushoBrushConstants : register(b4) float4 strokeP1; float4 strokeP2; float4 strokeP3; + float4 pageView; // center.xy, radius, pageSize }; struct BokushoBrushStroke @@ -26,6 +27,7 @@ struct BokushoBrushStroke RWStructuredBuffer BokushoTraceField : register(u20); RWStructuredBuffer BokushoCanvasField : register(u21); RWStructuredBuffer BokushoTipField : register(u22); +RWStructuredBuffer BokushoPageDensityField : register(u23); StructuredBuffer BokushoBrushStrokes : register(t78); float2 StrokePoint(BokushoBrushStroke stroke, float t) @@ -76,6 +78,68 @@ float CanvasPigment(float value) return 0.96 * (1.0 - exp(-max(value, 0.0) * 0.82)); } +uint PagePixelIndex(float2 world, out bool inside) +{ + float pageSize = max(pageView.w, 1.0); + float2 uv = (world - pageView.xy) / max(pageView.z, 0.001) * 0.5 + 0.5; + int2 pixel = int2(floor(uv * (pageSize - 1.0) + 0.5)); + inside = all(pixel >= int2(0, 0)) && all(pixel < int2((int)pageSize, (int)pageSize)); + return (uint)(pixel.y * (int)pageSize + pixel.x); +} + +void DepositSweptPatch(float2 previousTip, float2 tip, float normalRadius, float tangentRadius, float pigment) +{ + float ink = saturate(pigment); + if (ink <= 0.000001) + { + return; + } + + float2 sweep = tip - previousTip; + float sweepLength = length(sweep); + float2 tangent = sweepLength > 0.000001 ? sweep / sweepLength : float2(1.0, 0.0); + float2 normal = float2(-tangent.y, tangent.x); + float patchTangentRadius = max(tangentRadius * 0.26 + sweepLength * 0.5, 0.001); + float patchNormalRadius = max(normalRadius * 0.20, 0.001); + float2 center = (previousTip + tip) * 0.5; + float2 extents = float2( + abs(tangent.x) * patchTangentRadius + abs(normal.x) * patchNormalRadius, + abs(tangent.y) * patchTangentRadius + abs(normal.y) * patchNormalRadius); + float pageSize = max(pageView.w, 1.0); + float2 minUv = (center - extents - pageView.xy) / max(pageView.z, 0.001) * 0.5 + 0.5; + float2 maxUv = (center + extents - pageView.xy) / max(pageView.z, 0.001) * 0.5 + 0.5; + int minX = clamp((int)floor(min(minUv.x, maxUv.x) * (pageSize - 1.0)), 0, (int)pageSize - 1); + int maxX = clamp((int)ceil(max(minUv.x, maxUv.x) * (pageSize - 1.0)), 0, (int)pageSize - 1); + int minY = clamp((int)floor(min(minUv.y, maxUv.y) * (pageSize - 1.0)), 0, (int)pageSize - 1); + int maxY = clamp((int)ceil(max(minUv.y, maxUv.y) * (pageSize - 1.0)), 0, (int)pageSize - 1); + + [loop] + for (int y = minY; y <= maxY; y += 1) + { + float uvY = pageSize <= 1.0 ? 0.0 : (float)y / (pageSize - 1.0); + [loop] + for (int x = minX; x <= maxX; x += 1) + { + float uvX = pageSize <= 1.0 ? 0.0 : (float)x / (pageSize - 1.0); + float2 world = pageView.xy + (float2(uvX, uvY) * 2.0 - 1.0) * pageView.z; + float2 local = world - center; + float tangentDistance = dot(local, tangent) / patchTangentRadius; + float normalDistance = dot(local, normal) / patchNormalRadius; + float ellipse = sqrt(tangentDistance * tangentDistance + normalDistance * normalDistance); + float coverage = smoothstep(1.0, 0.0, ellipse); + if (coverage <= 0.0) + { + continue; + } + + float contribution = ink * coverage * 0.0032; + float density = -log(max(1.0 - saturate(contribution), 0.000001)); + uint fixedDensity = (uint)round(min(density * 65535.0, 65535.0)); + InterlockedAdd(BokushoPageDensityField[(uint)(y * (int)pageSize + x)], fixedDensity); + } + } +} + float LaneHash(uint strokeIndex, uint tuft, uint salt) { uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; @@ -197,6 +261,83 @@ void SimulateBokushoTuftSegment( } } +void SimulateBokushoTuftSegmentToPage( + BokushoBrushStroke stroke, + uint laneKey, + uint tuft, + uint sampleCount, + float restOffset, + float edge, + inout float offset, + inout float2 tip, + inout float stateLoad, + inout float stateWet, + bool writeOutput) +{ + float pressure = saturate(brushMaterial.y * stroke.profile.y * 0.5) * 2.0; + float splay = saturate(brushDynamics.x / 2.0); + float bend = saturate(brushDynamics.y / 2.4); + float friction = saturate(brushDynamics.z); + float radius = max(brushMaterial.x * stroke.profile.x, 0.0001); + float normalRadius = max(radius * stroke.profile.z, 0.0001); + float tangentRadius = max(radius * stroke.profile.w, 0.0001); + float segmentSpan = StrokeSegmentSpan(stroke, sampleCount); + uint stepCount = min(max((uint)ceil(segmentSpan * brushShape.z), 2u), 4096u); + float segmentVelocityScale = 1.0 / segmentSpan; + float split = saturate((0.28 - LaneHash(laneKey, tuft, 53u)) * 3.0) * saturate((edge - 0.20) * 1.5) * stroke.dynamics.w; + + [loop] + for (uint step = 0u; step < stepCount; step += 1u) + { + float t = stepCount <= 1u ? 0.0 : (float)step / (float)(stepCount - 1u); + float2 center = StrokePoint(stroke, t); + float2 tangent = StrokeTangent(stroke, t); + float2 normal = float2(-tangent.y, tangent.x); + float taper = StrokeTaper(stroke, t); + float localPressure = pressure * taper; + float laneCore = smoothstep(0.0, 0.78, 1.0 - edge); + float cohesion = saturate(0.28 + stateWet * 0.44 + laneCore * 0.24 + localPressure * 0.10 - split * 0.18); + float localNormalRadius = max(normalRadius * (0.18 + taper * 0.82) * (0.72 + splay * 0.34 + localPressure * 0.16 - cohesion * 0.08), 0.0001); + float localTangentRadius = max(tangentRadius * (0.24 + taper * 0.76) * (0.86 + bend * 0.18), 0.0001); + float targetOffset = (restOffset + stroke.pose.y * 0.12) * localNormalRadius * (0.58 + splay * 0.34 + localPressure * 0.18 - cohesion * 0.20); + float recovery = saturate(0.05 + stroke.pose.w * 0.08 + stateWet * 0.08 + localPressure * 0.10); + offset = cultmath_lerp(offset, targetOffset, recovery); + + float poseLead = stroke.pose.x * 0.36 + stroke.pose.y * 0.16; + float lag = localTangentRadius * (0.10 + bend * 0.32 + friction * localPressure * 0.16 + edge * 0.06); + float2 dragVector = cultmath_normalize(tangent + normal * poseLead); + float2 desiredTip = center + normal * offset - dragVector * lag; + float2 slip = desiredTip - tip; + float velocity = length(slip) * brushShape.z * segmentVelocityScale / max(radius, 0.001); + float contact = saturate(localPressure * stateLoad * (0.24 + stateWet * 0.62 + laneCore * 0.18)); + float drag = contact * friction * (0.28 + stateWet * 0.34); + float2 previousTip = tip; + tip = tip + slip * (0.18 + recovery * 0.82) * (1.0 - drag * 0.52); + float sweptDistance = length(tip - previousTip); + float sweptPatch = saturate(sweptDistance / max(localTangentRadius, 0.001) * 0.42); + + float tension = saturate(velocity * 0.014 + abs(targetOffset - offset) / max(localNormalRadius, 0.001) * 0.22 + edge * 0.12); + float separation = saturate(split * 0.34 + tension * 0.46 + edge * 0.20 - cohesion * 0.24); + float adhesion = saturate(stateWet * (0.36 + cohesion * 0.40) + localPressure * 0.10 - separation * 0.22); + float dryMemory = saturate((1.0 - stateWet) * 0.62 + separation * 0.32 + velocity * 0.004); + float fiberNoise = LaneHash(laneKey + step * 13u, tuft, 101u); + float continuity = 1.0 - smoothstep(0.18 + dryMemory * 0.28, 0.96, fiberNoise) * dryMemory * (0.38 + edge * 0.22); + float contactTransfer = contact * stateLoad * (0.16 + stateWet * 0.92) * (0.30 + drag * 0.64 + localPressure * 0.22 + sweptPatch * 0.18) * (0.68 + laneCore * 0.50 - separation * 0.14) * continuity; + float airborneRelease = (1.0 - contact) * stateLoad * stateWet * saturate(velocity * 0.010 - adhesion * 0.16) * (0.20 + separation * 0.42 + edge * 0.18); + float consumedPigment = contactTransfer + airborneRelease; + stateLoad = max(0.0, stateLoad - consumedPigment * segmentSpan * (0.010 + localPressure * 0.006)); + stateWet = max(0.0, stateWet - consumedPigment * segmentSpan * (0.006 + dryMemory * 0.003)); + + if (writeOutput) + { + float pigment = (contactTransfer * (0.95 + localPressure * 0.34 + sweptPatch * 0.24) + airborneRelease * (1.6 + velocity * 0.002)) * stroke.dynamics.z; + float patchNormalRadius = localNormalRadius * (0.84 + laneCore * 0.20 + localPressure * 0.14 + sweptPatch * 0.08 - separation * 0.10); + float patchTangentRadius = localTangentRadius * (0.92 + bend * 0.22 + drag * 0.16) + sweptDistance * 0.36; + DepositSweptPatch(previousTip, tip, patchNormalRadius, patchTangentRadius, pigment); + } + } +} + [numthreads(128, 1, 1)] void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) { @@ -241,3 +382,59 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) SimulateBokushoTuftSegment(stroke, strokeIndex, chainStart, tuft, sampleCount, tuftCount, restOffset, edge, offset, tip, stateLoad, stateWet, true); } + +[numthreads(128, 1, 1)] +void D3D12BokushoPageClearCS(uint3 dispatchThreadId : SV_DispatchThreadID) +{ + uint pageValueCount = (uint)(max(pageView.w, 1.0) * max(pageView.w, 1.0)); + uint index = dispatchThreadId.x; + if (index < pageValueCount) + { + BokushoPageDensityField[index] = 0u; + } +} + +[numthreads(128, 1, 1)] +void D3D12BokushoPageDepositCS(uint3 dispatchThreadId : SV_DispatchThreadID) +{ + uint sampleCount = max((uint)round(brushShape.x), 2u); + uint tuftCount = max((uint)round(brushShape.y), 1u); + uint strokeCount = max((uint)round(brushShape.w), 1u); + uint globalIndex = dispatchThreadId.x; + uint strokeIndex = globalIndex / tuftCount; + uint tuft = globalIndex - strokeIndex * tuftCount; + if (strokeIndex >= strokeCount || tuft >= tuftCount) + { + return; + } + + BokushoBrushStroke stroke = BokushoBrushStrokes[strokeIndex]; + uint chainStart = StrokeChainStart(strokeIndex); + BokushoBrushStroke seedStroke = BokushoBrushStrokes[chainStart]; + float laneT = tuftCount <= 1u ? 0.5 : (float)tuft / (float)(tuftCount - 1u); + float restOffset = laneT * 2.0 - 1.0; + float edge = abs(restOffset); + float laneHash = LaneHash(chainStart, tuft, 17u); + float laneLoad = 0.76 + laneHash * 0.34; + float seedPressure = saturate(brushMaterial.y * seedStroke.profile.y * 0.5) * 2.0; + float wetness = saturate(brushMaterial.w / 1.6); + float load = clamp(brushMaterial.z / 1.05, 0.0, 2.7); + float splay = saturate(brushDynamics.x / 2.0); + float seedRadius = max(brushMaterial.x * seedStroke.profile.x, 0.0001); + float seedNormalRadius = max(seedRadius * seedStroke.profile.z, 0.0001); + float seedSplit = saturate((0.28 - LaneHash(chainStart, tuft, 53u)) * 3.0) * saturate((edge - 0.20) * 1.5) * seedStroke.dynamics.w; + float initialCohesion = LaneCohesion(edge, seedSplit, wetness, seedPressure); + float2 tip = StrokePoint(seedStroke, 0.0); + float poseBias = seedStroke.pose.x * 0.18 + seedStroke.pose.y * 0.08; + float offset = (restOffset + poseBias * (1.0 - edge * 0.35)) * seedNormalRadius * (0.42 + splay * 0.38) + (laneHash - 0.5) * seedNormalRadius * 0.05; + float stateLoad = load * (0.86 + initialCohesion * 0.48) * (1.0 - edge * 0.08) * laneLoad * (1.0 - seedSplit * 0.18); + float stateWet = wetness * (0.74 + initialCohesion * 0.24 - seedSplit * 0.08); + + [loop] + for (uint replayStrokeIndex = chainStart; replayStrokeIndex < strokeIndex; replayStrokeIndex += 1u) + { + SimulateBokushoTuftSegmentToPage(BokushoBrushStrokes[replayStrokeIndex], chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, false); + } + + SimulateBokushoTuftSegmentToPage(stroke, chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, true); +} diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 67e5d2b..63ee1c9 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -59,6 +59,7 @@ struct BokushoBrushStroke StructuredBuffer BokushoCanvasField : register(t77); StructuredBuffer BokushoBrushStrokes : register(t78); StructuredBuffer BokushoTipField : register(t79); +StructuredBuffer BokushoPageDensityField : register(t80); #include "CultMath/CultMath.hlsl" @@ -259,6 +260,19 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float bokushoPageHeight(float2 world) { + if (bokushoShape.w > 0.5) + { + float pageSize = 512.0; + float2 uv = (world - viewCenter) / max(viewRadius, 0.001) * 0.5 + 0.5; + int2 pixel = int2(floor(uv * (pageSize - 1.0) + 0.5)); + if (all(pixel >= int2(0, 0)) && all(pixel < int2((int)pageSize, (int)pageSize))) + { + uint densityValue = BokushoPageDensityField[(uint)(pixel.y * (int)pageSize + pixel.x)]; + float density = (float)densityValue / 65535.0; + return saturate(1.0 - exp(-density)); + } + } + uint strokeCount = min(max((uint)round(bokushoShape.w), 0u), 64u); float height = 0.0; [loop]