Decode Bokusho CPU pages from density

This commit is contained in:
Meta Vens 2026-06-16 00:12:35 +01:00
commit 4eb624988a
2 changed files with 129 additions and 10 deletions

View file

@ -119,16 +119,55 @@ public static class BokushoBrushSimulation
{
ArgumentOutOfRangeException.ThrowIfLessThan(width, 1);
ArgumentOutOfRangeException.ThrowIfLessThan(height, 1);
var page = new float[checked(width * height)];
var densityPage = EvaluatePageDensity(source, width, height, viewCenter, viewRadius);
for (var index = 0; index < page.Length; index++)
{
page[index] = DecodePageDensity(densityPage[index]);
}
return page;
}
public static uint[] EvaluatePageDensity(
AquariumBokushoBrushFrame source,
int width,
int height,
Vector2 viewCenter,
float viewRadius)
{
ArgumentOutOfRangeException.ThrowIfLessThan(width, 1);
ArgumentOutOfRangeException.ThrowIfLessThan(height, 1);
var densityPage = new uint[checked(width * height)];
EvaluatePageDensity(source, width, height, viewCenter, viewRadius, densityPage);
return densityPage;
}
private static void EvaluatePageDensity(
AquariumBokushoBrushFrame source,
int width,
int height,
Vector2 viewCenter,
float viewRadius,
Span<uint> densityPage)
{
ArgumentOutOfRangeException.ThrowIfLessThan(width, 1);
ArgumentOutOfRangeException.ThrowIfLessThan(height, 1);
var valueCount = checked(width * height);
if (densityPage.Length < valueCount)
{
throw new ArgumentException("Density page buffer is smaller than the brush simulation page.", nameof(densityPage));
}
var frame = source.HasInput ? source.Normalized() : AquariumBokushoBrushFrame.Empty;
if (!frame.HasInput)
{
return new float[checked(width * height)];
return;
}
var sampleCount = Math.Clamp(frame.SampleCount, 2, 4096);
var tuftCount = Math.Clamp(frame.TuftCount, 1, 4096);
var strokes = EffectiveStrokes(frame);
var page = new float[checked(width * height)];
var wetness = Saturate(frame.Wetness / 1.6f);
var load = Math.Clamp(frame.InkLoad / 1.05f, 0.0f, 2.7f);
var splay = Saturate(frame.Splay / 2.0f);
@ -162,14 +201,12 @@ public static class BokushoBrushSimulation
for (var replayStrokeIndex = chainStart; replayStrokeIndex < strokeIndex; replayStrokeIndex++)
{
SimulateTuftSegmentToPage(strokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, page, width, height, viewCenter, safeRadius, writeOutput: false);
SimulateTuftSegmentToPage(strokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, densityPage, width, height, viewCenter, safeRadius, writeOutput: false);
}
SimulateTuftSegmentToPage(stroke, strokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, page, width, height, viewCenter, safeRadius, writeOutput: true);
SimulateTuftSegmentToPage(stroke, strokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, densityPage, width, height, viewCenter, safeRadius, writeOutput: true);
}
}
return page;
}
private static void SimulateTuftSegment(
@ -281,7 +318,7 @@ public static class BokushoBrushSimulation
ref Vector2 tip,
ref float stateLoad,
ref float stateWet,
Span<float> page,
Span<uint> densityPage,
int width,
int height,
Vector2 viewCenter,
@ -344,7 +381,7 @@ public static class BokushoBrushSimulation
var pigment = (contactTransfer * (0.95f + localPressure * 0.34f + sweptPatch * 0.24f) + airborneRelease * (1.6f + velocity * 0.002f)) * stroke.PigmentScale;
var patchNormalRadius = localNormalRadius * (0.84f + laneCore * 0.20f + localPressure * 0.14f + sweptPatch * 0.08f - separation * 0.10f);
var patchTangentRadius = localTangentRadius * (0.92f + bend * 0.22f + drag * 0.16f) + sweptDistance * 0.36f;
DepositSweptPatch(page, width, height, viewCenter, viewRadius, previousTip, tip, patchNormalRadius, patchTangentRadius, pigment);
DepositSweptPatch(densityPage, width, height, viewCenter, viewRadius, previousTip, tip, patchNormalRadius, patchTangentRadius, pigment);
}
}
}
@ -598,7 +635,7 @@ public static class BokushoBrushSimulation
}
private static void DepositSweptPatch(
Span<float> page,
Span<uint> densityPage,
int width,
int height,
Vector2 viewCenter,
@ -652,7 +689,7 @@ public static class BokushoBrushSimulation
var sampleIndex = y * width + x;
var contribution = PagePatchContribution(ink, coverage);
page[sampleIndex] = 1.0f - (1.0f - page[sampleIndex]) * (1.0f - contribution);
densityPage[sampleIndex] += EncodePageDensityContribution(contribution);
}
}
}

View file

@ -209,6 +209,88 @@ public sealed class BokushoBrushSimulationTests
Assert.InRange(MathF.Abs(gpuPage - cpuPage), 0.0f, 0.00008f);
}
[Fact]
public void CpuPageEvaluationDecodesSharedDensityComposition()
{
var frame = new AquariumBokushoBrushFrame
{
TuftCount = 17,
SampleCount = 64,
PhysicsHz = 500.0f,
BrushRadius = 0.82f,
Pressure = 1.04f,
InkLoad = 1.42f,
Wetness = 1.12f,
Splay = 0.76f,
Bend = 0.70f,
Friction = 0.64f,
Strokes =
[
new AquariumBokushoBrushStroke
{
StrokeP0 = new Vector4(-3.10f, -0.10f, 0.0f, 0.0f),
StrokeP1 = new Vector4(-2.20f, 0.16f, 0.0f, 0.0f),
StrokeP2 = new Vector4(0.28f, 0.24f, 0.0f, 0.0f),
StrokeP3 = new Vector4(1.24f, -0.24f, 0.0f, 0.0f),
RadiusScale = 0.94f,
PressureScale = 1.06f,
NormalScale = 0.62f,
TangentScale = 1.18f,
ShaftTilt = 0.12f,
ShaftRotation = 0.22f,
GripHeight = 0.88f,
Compliance = 1.08f,
EntryTaper = 0.05f,
ExitTaper = 0.18f,
PigmentScale = 1.30f,
SplitScale = 0.82f,
SegmentStart = 0.0f,
SegmentEnd = 0.55f,
SourceStrokeId = 7,
},
new AquariumBokushoBrushStroke
{
StrokeP0 = new Vector4(-0.28f, 0.24f, 0.0f, 0.0f),
StrokeP1 = new Vector4(0.28f, 0.24f, 0.0f, 0.0f),
StrokeP2 = new Vector4(1.82f, -0.18f, 0.0f, 0.0f),
StrokeP3 = new Vector4(2.76f, -0.48f, 0.0f, 0.0f),
RadiusScale = 0.90f,
PressureScale = 0.98f,
NormalScale = 0.58f,
TangentScale = 1.24f,
ShaftTilt = 0.10f,
ShaftRotation = 0.18f,
GripHeight = 0.90f,
Compliance = 1.10f,
EntryTaper = 0.05f,
ExitTaper = 0.24f,
PigmentScale = 1.18f,
SplitScale = 0.92f,
SegmentStart = 0.55f,
SegmentEnd = 1.0f,
SourceStrokeId = 7,
},
],
};
var width = 96;
var height = 72;
var page = BokushoBrushSimulation.EvaluatePage(frame, width, height, Vector2.Zero, 4.0f);
var density = BokushoBrushSimulation.EvaluatePageDensity(frame, width, height, Vector2.Zero, 4.0f);
var decodedInkPixels = 0;
for (var index = 0; index < page.Length; index++)
{
var decoded = BokushoBrushSimulation.DecodePageDensity(density[index]);
Assert.Equal(decoded, page[index]);
if (decoded > 0.001f)
{
decodedInkPixels++;
}
}
Assert.True(decodedInkPixels > 0);
}
[Fact]
public void PagePatchRadiiIncludeBristleFootprintAndSweepDistance()
{