Expose Bokusho density parity readback
This commit is contained in:
parent
4eb624988a
commit
e189884b97
5 changed files with 83 additions and 11 deletions
|
|
@ -6,4 +6,8 @@
|
|||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\CultMath\src\CultMath\CultMath.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Numerics;
|
||||
using CultMath;
|
||||
|
||||
namespace Aquarium.Engine.Render;
|
||||
|
||||
|
|
@ -758,18 +759,13 @@ public static class BokushoBrushSimulation
|
|||
|
||||
private static Vector2 CatmullRom(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
|
||||
{
|
||||
var t2 = t * t;
|
||||
var t3 = t2 * t;
|
||||
return 0.5f * ((2.0f * p1) +
|
||||
(-p0 + p2) * t +
|
||||
(2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t2 +
|
||||
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
|
||||
return (Vector2)math.catmullrom((float2)p0, (float2)p1, (float2)p2, (float2)p3, t);
|
||||
}
|
||||
|
||||
private static Vector2 Normalize(Vector2 value)
|
||||
{
|
||||
var length = value.Length();
|
||||
return length > 0.000001f && float.IsFinite(length) ? value / length : Vector2.Zero;
|
||||
return length > 0.000001f && float.IsFinite(length) ? (Vector2)math.normalize((float2)value) : Vector2.Zero;
|
||||
}
|
||||
|
||||
private static float Lerp(float left, float right, float t) => left + (right - left) * t;
|
||||
|
|
@ -788,7 +784,7 @@ public static class BokushoBrushSimulation
|
|||
public static uint EncodePageDensityContribution(float contribution)
|
||||
{
|
||||
var density = -MathF.Log(MathF.Max(1.0f - Saturate(contribution), 0.000001f));
|
||||
return (uint)MathF.Round(MathF.Min(density * PageDensityScale, PageDensityScale));
|
||||
return (uint)MathF.Floor(MathF.Min(density * PageDensityScale, PageDensityScale) + 0.5f);
|
||||
}
|
||||
|
||||
public static float DecodePageDensity(uint encodedDensity)
|
||||
|
|
@ -798,8 +794,7 @@ public static class BokushoBrushSimulation
|
|||
|
||||
private static float SmoothStep(float edge0, float edge1, float x)
|
||||
{
|
||||
var t = Saturate((x - edge0) / (edge1 - edge0));
|
||||
return t * t * (3.0f - 2.0f * t);
|
||||
return math.smoothstep(edge0, edge1, x);
|
||||
}
|
||||
|
||||
private static float Saturate(float value) => Math.Clamp(value, 0.0f, 1.0f);
|
||||
|
|
|
|||
|
|
@ -76,7 +76,9 @@ public static class AquariumHost
|
|||
var readyFrames = 0;
|
||||
var requiredReadyFrames = ParseHeadlessReadyFrames();
|
||||
var captureFramePath = ParseCaptureFramePath(args);
|
||||
var captureBokushoDensityPath = ParseCaptureBokushoDensityPath(args);
|
||||
var capturedFrame = false;
|
||||
var capturedBokushoDensity = false;
|
||||
IAquariumRuntime? sceneReadyRuntime = null;
|
||||
|
||||
try
|
||||
|
|
@ -149,7 +151,23 @@ public static class AquariumHost
|
|||
capturedFrame = true;
|
||||
}
|
||||
|
||||
if (runtime.Options.Headless && frames >= 2 && readyFrames >= requiredReadyFrames)
|
||||
if (runtime.Options.Headless
|
||||
&& !capturedBokushoDensity
|
||||
&& !string.IsNullOrWhiteSpace(captureBokushoDensityPath)
|
||||
&& readyFrames >= requiredReadyFrames)
|
||||
{
|
||||
renderer.SaveBokushoPageDensityRaw(captureBokushoDensityPath);
|
||||
Console.WriteLine($"Headless Aquarium Bokusho density captured: {Path.GetFullPath(captureBokushoDensityPath)}");
|
||||
capturedBokushoDensity = true;
|
||||
}
|
||||
|
||||
var needsFrameCapture = !string.IsNullOrWhiteSpace(captureFramePath) && !capturedFrame;
|
||||
var needsBokushoDensityCapture = !string.IsNullOrWhiteSpace(captureBokushoDensityPath) && !capturedBokushoDensity;
|
||||
if (runtime.Options.Headless
|
||||
&& frames >= 2
|
||||
&& readyFrames >= requiredReadyFrames
|
||||
&& !needsFrameCapture
|
||||
&& !needsBokushoDensityCapture)
|
||||
{
|
||||
Console.WriteLine("Headless Aquarium completed requested frames.");
|
||||
break;
|
||||
|
|
@ -215,6 +233,20 @@ public static class AquariumHost
|
|||
return Environment.GetEnvironmentVariable("AQUARIUM_CAPTURE_FRAME");
|
||||
}
|
||||
|
||||
private static string? ParseCaptureBokushoDensityPath(IReadOnlyCollection<string> args)
|
||||
{
|
||||
var values = args.ToArray();
|
||||
for (var index = 0; index < values.Length - 1; index++)
|
||||
{
|
||||
if (string.Equals(values[index], "--capture-bokusho-density", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return values[index + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return Environment.GetEnvironmentVariable("AQUARIUM_CAPTURE_BOKUSHO_DENSITY");
|
||||
}
|
||||
|
||||
private static string? ParseCachePath(IReadOnlyCollection<string> args)
|
||||
{
|
||||
var values = args.ToArray();
|
||||
|
|
|
|||
|
|
@ -1585,6 +1585,45 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
PngImageWriter.WriteRgba(path, width, height, rgba);
|
||||
}
|
||||
|
||||
public unsafe void SaveBokushoPageDensityRaw(string path)
|
||||
{
|
||||
if (!hasPresentedReadyFrame)
|
||||
{
|
||||
throw new InvalidOperationException("No completed frame is available to save.");
|
||||
}
|
||||
|
||||
WaitForGpu();
|
||||
|
||||
var bufferBytes = bokushoPageDensityBuffer.SizeBytes;
|
||||
using var readback = device.CreateCommittedResource(
|
||||
HeapType.Readback,
|
||||
ResourceDescription.Buffer((ulong)bufferBytes),
|
||||
ResourceStates.CopyDest,
|
||||
null);
|
||||
readback.Name = "Aquarium D3D12 Bokusho Page Density Readback";
|
||||
|
||||
using var captureAllocator = device.CreateCommandAllocator(CommandListType.Direct);
|
||||
using var captureList = device.CreateCommandList<ID3D12GraphicsCommandList>(0, CommandListType.Direct, captureAllocator, null);
|
||||
bokushoPageDensityBuffer.Transition(captureList, ResourceStates.CopySource);
|
||||
captureList.CopyBufferRegion(readback, 0, bokushoPageDensityBuffer.Resource, 0, (ulong)bufferBytes);
|
||||
bokushoPageDensityBuffer.Transition(captureList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
captureList.Close();
|
||||
commandQueue.ExecuteCommandList(captureList);
|
||||
WaitForGpu();
|
||||
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)) ?? ".");
|
||||
var mapped = readback.Map<byte>(0);
|
||||
try
|
||||
{
|
||||
using var stream = File.Create(path);
|
||||
stream.Write(new ReadOnlySpan<byte>(mapped, bufferBytes));
|
||||
}
|
||||
finally
|
||||
{
|
||||
readback.Unmap(0, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static Vector2 TemporalJitterPixels(int index)
|
||||
{
|
||||
const float scale = 0.42f;
|
||||
|
|
|
|||
|
|
@ -27,4 +27,6 @@ public interface IAquariumRenderer : IAquariumFieldResourceBroker, IDisposable
|
|||
void Render(AquariumFrame frame, int width, int height);
|
||||
|
||||
void SaveFramePng(string path);
|
||||
|
||||
void SaveBokushoPageDensityRaw(string path);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue