Route D3D12 solids through view froxels
This commit is contained in:
parent
c57f39e76b
commit
aadd3e1fbc
5 changed files with 173 additions and 335 deletions
|
|
@ -31,19 +31,12 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const Format GridHeightFormat = Format.R16_Float;
|
||||
private const int PlanetCount = 5;
|
||||
private const int GridHeightBrushCount = PlanetCount + 1;
|
||||
private const int FroxelCountX = 8;
|
||||
private const int FroxelCountY = 8;
|
||||
private const int FroxelCountZ = 4;
|
||||
private const int FroxelSlotCount = 2;
|
||||
private const int FroxelBufferElementCount = FroxelCountX * FroxelCountY * FroxelCountZ * FroxelSlotCount;
|
||||
private const int FieldInstanceCount = PlanetCount + 5;
|
||||
private const int TransparentSurfaceCount = 1;
|
||||
private const float GridTransparentMinZ = -1.85f;
|
||||
private const float GridTransparentMaxZ = 0.45f;
|
||||
private const int BloomLevelCount = 3;
|
||||
private const float SunRadius = 1.12f;
|
||||
private const float FroxelMinZ = -2.0f;
|
||||
private const float FroxelMaxZ = 6.0f;
|
||||
private const Format SceneHdrFormat = Format.R16G16B16A16_Float;
|
||||
private const string GridShaderRelativePath = "Render/Shaders/D3D12Grid.hlsl";
|
||||
private const string SmokeShaderRelativePath = "Render/Shaders/D3D12Smoke.hlsl";
|
||||
|
|
@ -105,11 +98,11 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private readonly D3D12RenderTarget[] historyControlRenderTargets = new D3D12RenderTarget[2];
|
||||
private readonly D3D12RenderTarget[] bloomRenderTargets = new D3D12RenderTarget[BloomLevelCount];
|
||||
private readonly D3D12RenderTarget[] bloomScratchTargets = new D3D12RenderTarget[BloomLevelCount];
|
||||
private readonly D3D12StructuredBuffer froxelPrimitiveBuffer;
|
||||
private D3D12StructuredBuffer froxelPrimitiveBuffer;
|
||||
private readonly D3D12StructuredBuffer fieldInstanceBuffer;
|
||||
private D3D12StructuredBuffer transparentSurfaceIndexBuffer;
|
||||
private readonly D3D12StructuredBuffer transparentSurfaceBuffer;
|
||||
private readonly Int4[] froxelPrimitiveIds = new Int4[FroxelBufferElementCount];
|
||||
private Int4[] froxelPrimitiveIds = [];
|
||||
private readonly FieldInstanceGpu[] fieldInstances = new FieldInstanceGpu[FieldInstanceCount];
|
||||
private Int4[] transparentSurfaceIds = [];
|
||||
private readonly TransparentSurfaceGpu[] transparentSurfaces = new TransparentSurfaceGpu[TransparentSurfaceCount];
|
||||
|
|
@ -203,7 +196,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
sceneControlRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-control-target", "Aquarium D3D12 Scene Control Target");
|
||||
CreateHistoryRenderTargets();
|
||||
CreateBloomRenderTargets();
|
||||
froxelPrimitiveBuffer = new D3D12StructuredBuffer(device, FroxelBufferElementCount, Marshal.SizeOf<Int4>(), "Aquarium D3D12 Froxel Primitive Buffer");
|
||||
froxelPrimitiveBuffer = CreateViewFroxelPrimitiveBuffer();
|
||||
fieldInstanceBuffer = new D3D12StructuredBuffer(device, FieldInstanceCount, Marshal.SizeOf<FieldInstanceGpu>(), "Aquarium D3D12 Field Instance Buffer");
|
||||
transparentSurfaceIndexBuffer = CreateTransparentSurfaceIndexBuffer();
|
||||
transparentSurfaceBuffer = new D3D12StructuredBuffer(device, TransparentSurfaceCount, Marshal.SizeOf<TransparentSurfaceGpu>(), "Aquarium D3D12 Transparent Surface Buffer");
|
||||
|
|
@ -598,6 +591,16 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
|
||||
private int TransparentSurfaceBufferElementCount => ViewFroxelCount;
|
||||
|
||||
private D3D12StructuredBuffer CreateViewFroxelPrimitiveBuffer()
|
||||
{
|
||||
froxelPrimitiveIds = new Int4[ViewFroxelCount];
|
||||
return new D3D12StructuredBuffer(
|
||||
device,
|
||||
ViewFroxelCount,
|
||||
Marshal.SizeOf<Int4>(),
|
||||
"Aquarium D3D12 View-Froxel Primitive Buffer");
|
||||
}
|
||||
|
||||
private D3D12StructuredBuffer CreateTransparentSurfaceIndexBuffer()
|
||||
{
|
||||
transparentSurfaceIds = new Int4[TransparentSurfaceBufferElementCount];
|
||||
|
|
@ -664,6 +667,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
resourceRegistry.RemoveRenderTarget("medium-event-target");
|
||||
resourceRegistry.RemoveRenderTarget("medium-volume-target");
|
||||
resourceRegistry.RemoveRenderTarget("grid-height-target");
|
||||
resourceRegistry.RemoveStructuredBuffer("froxel-primitive-buffer");
|
||||
resourceRegistry.RemoveStructuredBuffer("transparent-surface-index-buffer");
|
||||
DisposeBloomRenderTargets();
|
||||
DisposeHistoryRenderTargets();
|
||||
|
|
@ -673,6 +677,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
mediumTransportRenderTarget.Dispose();
|
||||
mediumVolumeRenderTarget.Dispose();
|
||||
gridHeightRenderTarget.Dispose();
|
||||
froxelPrimitiveBuffer.Dispose();
|
||||
transparentSurfaceIndexBuffer.Dispose();
|
||||
for (var index = 0; index < frames.Length; index++)
|
||||
{
|
||||
|
|
@ -695,7 +700,9 @@ 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");
|
||||
froxelPrimitiveBuffer = CreateViewFroxelPrimitiveBuffer();
|
||||
transparentSurfaceIndexBuffer = CreateTransparentSurfaceIndexBuffer();
|
||||
resourceRegistry.Add("froxel-primitive-buffer", froxelPrimitiveBuffer);
|
||||
resourceRegistry.Add("transparent-surface-index-buffer", transparentSurfaceIndexBuffer);
|
||||
sceneRenderTarget = CreateSceneRenderTarget();
|
||||
sceneMetadataRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-metadata-target", "Aquarium D3D12 Scene Metadata Target");
|
||||
|
|
@ -1195,74 +1202,72 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
|
||||
private void AddPrimitiveToFroxels(AquariumFrame frame, int primitiveId, Vector3 center, float boundRadius)
|
||||
{
|
||||
var min = center - new Vector3(boundRadius);
|
||||
var max = center + new Vector3(boundRadius);
|
||||
var minCell = FroxelCellForPosition(frame, min);
|
||||
var maxCell = FroxelCellForPosition(frame, max);
|
||||
|
||||
for (var z = minCell.Z; z <= maxCell.Z; z++)
|
||||
var gridOrigin = new Vector3(frame.Grid.Center.X, frame.Grid.Center.Y, 0.0f);
|
||||
var farDistance = Vector3.Distance(frame.CameraPosition, gridOrigin) + MathF.Max(frame.Grid.Radius, 0.001f);
|
||||
for (var y = 0; y < mediumFroxelHeight; y++)
|
||||
{
|
||||
for (var y = minCell.Y; y <= maxCell.Y; y++)
|
||||
for (var x = 0; x < mediumFroxelWidth; x++)
|
||||
{
|
||||
for (var x = minCell.X; x <= maxCell.X; x++)
|
||||
if (!TraceViewFroxelSphere(
|
||||
frame.CameraPosition,
|
||||
frame.Grid.Center,
|
||||
x,
|
||||
y,
|
||||
center,
|
||||
boundRadius,
|
||||
out var enterTravel,
|
||||
out var exitTravel))
|
||||
{
|
||||
AddPrimitiveToFroxel(FroxelIndex(x, y, z), primitiveId);
|
||||
continue;
|
||||
}
|
||||
|
||||
enterTravel = Math.Clamp(enterTravel, 0.0f, farDistance);
|
||||
exitTravel = Math.Clamp(exitTravel, 0.0f, farDistance);
|
||||
if (exitTravel <= enterTravel)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var firstSlice = ClampCell(enterTravel / farDistance, MediumFroxelSliceCount);
|
||||
var lastSlice = ClampCell(exitTravel / farDistance, MediumFroxelSliceCount);
|
||||
for (var slice = firstSlice; slice <= lastSlice; slice++)
|
||||
{
|
||||
AddPrimitiveToViewFroxel(ViewFroxelIndex(x, y, slice), primitiveId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddPrimitiveToFroxel(int froxelIndex, int primitiveId)
|
||||
private void AddPrimitiveToViewFroxel(int viewFroxelIndex, int primitiveId)
|
||||
{
|
||||
var baseElement = froxelIndex * FroxelSlotCount;
|
||||
for (var slotGroup = 0; slotGroup < FroxelSlotCount; slotGroup++)
|
||||
var element = froxelPrimitiveIds[viewFroxelIndex];
|
||||
if (element.X == primitiveId || element.Y == primitiveId || element.Z == primitiveId || element.W == primitiveId)
|
||||
{
|
||||
var elementIndex = baseElement + slotGroup;
|
||||
var element = froxelPrimitiveIds[elementIndex];
|
||||
|
||||
if (element.X == primitiveId || element.Y == primitiveId || element.Z == primitiveId || element.W == primitiveId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.X == -1)
|
||||
{
|
||||
froxelPrimitiveIds[elementIndex] = element with { X = primitiveId };
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.Y == -1)
|
||||
{
|
||||
froxelPrimitiveIds[elementIndex] = element with { Y = primitiveId };
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.Z == -1)
|
||||
{
|
||||
froxelPrimitiveIds[elementIndex] = element with { Z = primitiveId };
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.W == -1)
|
||||
{
|
||||
froxelPrimitiveIds[elementIndex] = element with { W = primitiveId };
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static FroxelCell FroxelCellForPosition(AquariumFrame frame, Vector3 position)
|
||||
{
|
||||
var gridCenter = frame.Grid.Center;
|
||||
var gridRadius = MathF.Max(frame.Grid.Radius, 0.001f);
|
||||
var localX = ((position.X - gridCenter.X) / gridRadius) * 0.5f + 0.5f;
|
||||
var localY = ((position.Y - gridCenter.Y) / gridRadius) * 0.5f + 0.5f;
|
||||
var localZ = (position.Z - FroxelMinZ) / (FroxelMaxZ - FroxelMinZ);
|
||||
if (element.X == -1)
|
||||
{
|
||||
froxelPrimitiveIds[viewFroxelIndex] = element with { X = primitiveId };
|
||||
return;
|
||||
}
|
||||
|
||||
return new FroxelCell(
|
||||
ClampCell(localX, FroxelCountX),
|
||||
ClampCell(localY, FroxelCountY),
|
||||
ClampCell(localZ, FroxelCountZ));
|
||||
if (element.Y == -1)
|
||||
{
|
||||
froxelPrimitiveIds[viewFroxelIndex] = element with { Y = primitiveId };
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.Z == -1)
|
||||
{
|
||||
froxelPrimitiveIds[viewFroxelIndex] = element with { Z = primitiveId };
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.W == -1)
|
||||
{
|
||||
froxelPrimitiveIds[viewFroxelIndex] = element with { W = primitiveId };
|
||||
}
|
||||
}
|
||||
|
||||
private static int ClampCell(float normalized, int count)
|
||||
|
|
@ -1270,11 +1275,6 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return Math.Clamp((int)MathF.Floor(normalized * count), 0, count - 1);
|
||||
}
|
||||
|
||||
private static int FroxelIndex(int x, int y, int z)
|
||||
{
|
||||
return x + y * FroxelCountX + z * FroxelCountX * FroxelCountY;
|
||||
}
|
||||
|
||||
private void BuildFieldInstanceTable(AquariumFrame frame)
|
||||
{
|
||||
fieldInstances[0] = FieldInstanceGpu.Sphere(
|
||||
|
|
@ -1437,6 +1437,47 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return hit;
|
||||
}
|
||||
|
||||
private bool TraceViewFroxelSphere(
|
||||
Vector3 cameraPosition,
|
||||
Vector2 gridCenter,
|
||||
int x,
|
||||
int y,
|
||||
Vector3 center,
|
||||
float radius,
|
||||
out float enterTravel,
|
||||
out float exitTravel)
|
||||
{
|
||||
enterTravel = float.PositiveInfinity;
|
||||
exitTravel = 0.0f;
|
||||
Span<Vector2> samples =
|
||||
[
|
||||
new Vector2(x * MediumFroxelDownscale, y * MediumFroxelDownscale),
|
||||
new Vector2((x + 1) * MediumFroxelDownscale, y * MediumFroxelDownscale),
|
||||
new Vector2(x * MediumFroxelDownscale, (y + 1) * MediumFroxelDownscale),
|
||||
new Vector2((x + 1) * MediumFroxelDownscale, (y + 1) * MediumFroxelDownscale),
|
||||
new Vector2((x + 0.5f) * MediumFroxelDownscale, (y + 0.5f) * MediumFroxelDownscale),
|
||||
];
|
||||
|
||||
var hit = false;
|
||||
foreach (var pixel in samples)
|
||||
{
|
||||
var clampedPixel = new Vector2(
|
||||
Math.Clamp(pixel.X, 0.0f, MathF.Max(width - 1.0f, 0.0f)),
|
||||
Math.Clamp(pixel.Y, 0.0f, MathF.Max(height - 1.0f, 0.0f)));
|
||||
var direction = RayDirectionForPixel(clampedPixel, cameraPosition, gridCenter);
|
||||
if (!TraceSphere(cameraPosition, direction, center, radius, out var sampleEnter, out var sampleExit))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
enterTravel = MathF.Min(enterTravel, sampleEnter);
|
||||
exitTravel = MathF.Max(exitTravel, sampleExit);
|
||||
hit = true;
|
||||
}
|
||||
|
||||
return hit;
|
||||
}
|
||||
|
||||
private Vector3 RayDirectionForPixel(Vector2 pixel, Vector3 cameraPosition, Vector2 gridCenter)
|
||||
{
|
||||
var ndc = ((pixel * 2.0f) - new Vector2(width, height)) / MathF.Max(height, 1.0f);
|
||||
|
|
@ -1471,6 +1512,31 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return exitTravel >= enterTravel;
|
||||
}
|
||||
|
||||
private static bool TraceSphere(Vector3 origin, Vector3 direction, Vector3 center, float radius, out float enterTravel, out float exitTravel)
|
||||
{
|
||||
var oc = origin - center;
|
||||
var b = Vector3.Dot(oc, direction);
|
||||
var c = Vector3.Dot(oc, oc) - radius * radius;
|
||||
var h = b * b - c;
|
||||
if (h < 0.0f)
|
||||
{
|
||||
enterTravel = 0.0f;
|
||||
exitTravel = 0.0f;
|
||||
return false;
|
||||
}
|
||||
|
||||
h = MathF.Sqrt(h);
|
||||
enterTravel = -b - h;
|
||||
exitTravel = -b + h;
|
||||
if (exitTravel <= 0.0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
enterTravel = MathF.Max(enterTravel, 0.0f);
|
||||
return exitTravel > enterTravel;
|
||||
}
|
||||
|
||||
private int ViewFroxelIndex(int x, int y, int slice)
|
||||
{
|
||||
return x + y * mediumFroxelWidth + slice * mediumFroxelWidth * mediumFroxelHeight;
|
||||
|
|
|
|||
|
|
@ -50,12 +50,6 @@ static const int MEDIUM_FROXEL_ATLAS_COLUMNS = 8;
|
|||
static const int MEDIUM_FROXEL_ATLAS_ROWS = 4;
|
||||
static const int MEDIUM_FROXEL_DOWNSCALE = 8;
|
||||
static const int MEDIUM_FROXEL_SLICE_COUNT = MEDIUM_FROXEL_ATLAS_COLUMNS * MEDIUM_FROXEL_ATLAS_ROWS;
|
||||
static const int FROXEL_COUNT_X = 8;
|
||||
static const int FROXEL_COUNT_Y = 8;
|
||||
static const int FROXEL_COUNT_Z = 4;
|
||||
static const int FROXEL_SLOT_COUNT = 2;
|
||||
static const float FROXEL_MIN_Z = -2.0;
|
||||
static const float FROXEL_MAX_Z = 6.0;
|
||||
static const float PI = 3.14159265359;
|
||||
static const float GRID_HEIGHT_TEXEL_COUNT = 128.0;
|
||||
static const float TERRAIN_ISOLINE_SPACING = 0.12;
|
||||
|
|
@ -267,184 +261,6 @@ float primitiveFieldId(int primitiveId)
|
|||
return primitiveId == 0 ? FIELD_ID_SELF : FIELD_ID_PLANET_BASE + (float)(primitiveId - 1);
|
||||
}
|
||||
|
||||
int clampCell(float normalized, int count)
|
||||
{
|
||||
return clamp((int)floor(normalized * (float)count), 0, count - 1);
|
||||
}
|
||||
|
||||
int froxelIndexForPosition(float3 p)
|
||||
{
|
||||
float2 local = gridLocal(p.xy);
|
||||
float localZ = (p.z - FROXEL_MIN_Z) / (FROXEL_MAX_Z - FROXEL_MIN_Z);
|
||||
if (abs(local.x) > 1.0 || abs(local.y) > 1.0 || localZ < 0.0 || localZ > 1.0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int x = clampCell(local.x * 0.5 + 0.5, FROXEL_COUNT_X);
|
||||
int y = clampCell(local.y * 0.5 + 0.5, FROXEL_COUNT_Y);
|
||||
int z = clampCell(localZ, FROXEL_COUNT_Z);
|
||||
return x + y * FROXEL_COUNT_X + z * FROXEL_COUNT_X * FROXEL_COUNT_Y;
|
||||
}
|
||||
|
||||
int froxelIndexForCell(int3 cell)
|
||||
{
|
||||
return cell.x + cell.y * FROXEL_COUNT_X + cell.z * FROXEL_COUNT_X * FROXEL_COUNT_Y;
|
||||
}
|
||||
|
||||
float3 froxelWorldMin()
|
||||
{
|
||||
return float3(gridCenter - gridRadius.xx, FROXEL_MIN_Z);
|
||||
}
|
||||
|
||||
float3 froxelWorldMax()
|
||||
{
|
||||
return float3(gridCenter + gridRadius.xx, FROXEL_MAX_Z);
|
||||
}
|
||||
|
||||
float3 froxelCellSize()
|
||||
{
|
||||
float3 boundsMin = froxelWorldMin();
|
||||
float3 boundsMax = froxelWorldMax();
|
||||
return (boundsMax - boundsMin) / float3(FROXEL_COUNT_X, FROXEL_COUNT_Y, FROXEL_COUNT_Z);
|
||||
}
|
||||
|
||||
bool traceFroxelBounds(float3 origin, float3 direction, out float enterTravel, out float exitTravel)
|
||||
{
|
||||
float3 boundsMin = froxelWorldMin();
|
||||
float3 boundsMax = froxelWorldMax();
|
||||
enterTravel = 0.0;
|
||||
exitTravel = farDistance;
|
||||
bool hit = true;
|
||||
|
||||
if (abs(direction.x) < 0.000001)
|
||||
{
|
||||
if (origin.x < boundsMin.x || origin.x > boundsMax.x)
|
||||
{
|
||||
hit = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float t0 = (boundsMin.x - origin.x) / direction.x;
|
||||
float t1 = (boundsMax.x - origin.x) / direction.x;
|
||||
enterTravel = max(enterTravel, min(t0, t1));
|
||||
exitTravel = min(exitTravel, max(t0, t1));
|
||||
}
|
||||
|
||||
if (abs(direction.y) < 0.000001)
|
||||
{
|
||||
if (origin.y < boundsMin.y || origin.y > boundsMax.y)
|
||||
{
|
||||
hit = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float t0 = (boundsMin.y - origin.y) / direction.y;
|
||||
float t1 = (boundsMax.y - origin.y) / direction.y;
|
||||
enterTravel = max(enterTravel, min(t0, t1));
|
||||
exitTravel = min(exitTravel, max(t0, t1));
|
||||
}
|
||||
|
||||
if (abs(direction.z) < 0.000001)
|
||||
{
|
||||
if (origin.z < boundsMin.z || origin.z > boundsMax.z)
|
||||
{
|
||||
hit = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float t0 = (boundsMin.z - origin.z) / direction.z;
|
||||
float t1 = (boundsMax.z - origin.z) / direction.z;
|
||||
enterTravel = max(enterTravel, min(t0, t1));
|
||||
exitTravel = min(exitTravel, max(t0, t1));
|
||||
}
|
||||
|
||||
enterTravel = max(enterTravel, 0.0);
|
||||
exitTravel = min(exitTravel, farDistance);
|
||||
return hit && exitTravel > enterTravel;
|
||||
}
|
||||
|
||||
struct FroxelDda
|
||||
{
|
||||
int3 cell;
|
||||
int3 stepCell;
|
||||
float3 nextBoundaryTravel;
|
||||
float3 deltaTravel;
|
||||
float exitTravel;
|
||||
int active;
|
||||
};
|
||||
|
||||
FroxelDda beginFroxelDda(float3 origin, float3 direction, float enterTravel, float exitTravel)
|
||||
{
|
||||
FroxelDda dda;
|
||||
float3 boundsMin = froxelWorldMin();
|
||||
float3 cellSize = froxelCellSize();
|
||||
float3 p = origin + direction * (enterTravel + 0.0001);
|
||||
float3 local = (p - boundsMin) / cellSize;
|
||||
dda.cell = clamp((int3)floor(local), int3(0, 0, 0), int3(FROXEL_COUNT_X - 1, FROXEL_COUNT_Y - 1, FROXEL_COUNT_Z - 1));
|
||||
dda.stepCell = int3(direction.x >= 0.0 ? 1 : -1, direction.y >= 0.0 ? 1 : -1, direction.z >= 0.0 ? 1 : -1);
|
||||
|
||||
float3 nextBoundary = boundsMin + (float3)dda.cell * cellSize;
|
||||
nextBoundary += float3(
|
||||
direction.x >= 0.0 ? cellSize.x : 0.0,
|
||||
direction.y >= 0.0 ? cellSize.y : 0.0,
|
||||
direction.z >= 0.0 ? cellSize.z : 0.0);
|
||||
|
||||
float3 absDirection = max(abs(direction), 0.000001);
|
||||
dda.nextBoundaryTravel = (nextBoundary - origin) / direction;
|
||||
dda.nextBoundaryTravel = float3(
|
||||
abs(direction.x) < 0.000001 ? farDistance + 1.0 : dda.nextBoundaryTravel.x,
|
||||
abs(direction.y) < 0.000001 ? farDistance + 1.0 : dda.nextBoundaryTravel.y,
|
||||
abs(direction.z) < 0.000001 ? farDistance + 1.0 : dda.nextBoundaryTravel.z);
|
||||
dda.deltaTravel = cellSize / absDirection;
|
||||
dda.deltaTravel = float3(
|
||||
abs(direction.x) < 0.000001 ? farDistance + 1.0 : dda.deltaTravel.x,
|
||||
abs(direction.y) < 0.000001 ? farDistance + 1.0 : dda.deltaTravel.y,
|
||||
abs(direction.z) < 0.000001 ? farDistance + 1.0 : dda.deltaTravel.z);
|
||||
dda.exitTravel = exitTravel;
|
||||
dda.active = 1;
|
||||
return dda;
|
||||
}
|
||||
|
||||
float froxelDdaCellExit(FroxelDda dda)
|
||||
{
|
||||
return min(min(dda.nextBoundaryTravel.x, dda.nextBoundaryTravel.y), min(dda.nextBoundaryTravel.z, dda.exitTravel));
|
||||
}
|
||||
|
||||
void advanceFroxelDda(inout FroxelDda dda)
|
||||
{
|
||||
float axisTravel = min(dda.nextBoundaryTravel.x, min(dda.nextBoundaryTravel.y, dda.nextBoundaryTravel.z));
|
||||
if (axisTravel >= dda.exitTravel)
|
||||
{
|
||||
dda.active = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (dda.nextBoundaryTravel.x <= axisTravel + 0.00001)
|
||||
{
|
||||
dda.cell.x += dda.stepCell.x;
|
||||
dda.nextBoundaryTravel.x += dda.deltaTravel.x;
|
||||
}
|
||||
if (dda.nextBoundaryTravel.y <= axisTravel + 0.00001)
|
||||
{
|
||||
dda.cell.y += dda.stepCell.y;
|
||||
dda.nextBoundaryTravel.y += dda.deltaTravel.y;
|
||||
}
|
||||
if (dda.nextBoundaryTravel.z <= axisTravel + 0.00001)
|
||||
{
|
||||
dda.cell.z += dda.stepCell.z;
|
||||
dda.nextBoundaryTravel.z += dda.deltaTravel.z;
|
||||
}
|
||||
|
||||
dda.active =
|
||||
dda.cell.x >= 0 && dda.cell.x < FROXEL_COUNT_X &&
|
||||
dda.cell.y >= 0 && dda.cell.y < FROXEL_COUNT_Y &&
|
||||
dda.cell.z >= 0 && dda.cell.z < FROXEL_COUNT_Z ? 1 : 0;
|
||||
}
|
||||
|
||||
void considerPrimitiveHit(float3 origin, float3 direction, int primitiveId, float intervalStart, float intervalEnd, inout SolidHit nearest)
|
||||
{
|
||||
if (primitiveId < 0)
|
||||
|
|
@ -473,15 +289,11 @@ void considerFroxelPrimitiveHits(float3 origin, float3 direction, int froxelInde
|
|||
return;
|
||||
}
|
||||
|
||||
[unroll]
|
||||
for (int slotGroup = 0; slotGroup < FROXEL_SLOT_COUNT; slotGroup++)
|
||||
{
|
||||
int4 ids = froxelPrimitiveIds[froxelIndex * FROXEL_SLOT_COUNT + slotGroup];
|
||||
considerPrimitiveHit(origin, direction, ids.x, intervalStart, intervalEnd, nearest);
|
||||
considerPrimitiveHit(origin, direction, ids.y, intervalStart, intervalEnd, nearest);
|
||||
considerPrimitiveHit(origin, direction, ids.z, intervalStart, intervalEnd, nearest);
|
||||
considerPrimitiveHit(origin, direction, ids.w, intervalStart, intervalEnd, nearest);
|
||||
}
|
||||
int4 ids = froxelPrimitiveIds[froxelIndex];
|
||||
considerPrimitiveHit(origin, direction, ids.x, intervalStart, intervalEnd, nearest);
|
||||
considerPrimitiveHit(origin, direction, ids.y, intervalStart, intervalEnd, nearest);
|
||||
considerPrimitiveHit(origin, direction, ids.z, intervalStart, intervalEnd, nearest);
|
||||
considerPrimitiveHit(origin, direction, ids.w, intervalStart, intervalEnd, nearest);
|
||||
}
|
||||
|
||||
float gridSurfaceDistanceAt(float3 origin, float3 direction, float travel)
|
||||
|
|
@ -627,40 +439,11 @@ void considerFroxelTransparentEvents(float3 origin, float3 direction, int froxel
|
|||
return;
|
||||
}
|
||||
|
||||
[unroll]
|
||||
for (int slotGroup = 0; slotGroup < FROXEL_SLOT_COUNT; slotGroup++)
|
||||
{
|
||||
int4 ids = transparentSurfaceIds[froxelIndex * FROXEL_SLOT_COUNT + slotGroup];
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.x, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.y, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.z, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.w, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
}
|
||||
}
|
||||
|
||||
SolidHit nearestFroxelSolidHit(float3 origin, float3 direction, int froxelIndex, float intervalStart, float intervalEnd)
|
||||
{
|
||||
SolidHit nearest;
|
||||
nearest.hit = false;
|
||||
nearest.travel = intervalEnd;
|
||||
nearest.normal = 0.0;
|
||||
nearest.fieldId = 0.0;
|
||||
nearest.primitiveId = -1;
|
||||
considerFroxelPrimitiveHits(origin, direction, froxelIndex, intervalStart, intervalEnd, nearest);
|
||||
return nearest;
|
||||
}
|
||||
|
||||
TransparentEvent nearestFroxelTransparentEvent(float3 origin, float3 direction, int froxelIndex, uint consumedSurfaceMask, float intervalStart, float intervalEnd)
|
||||
{
|
||||
TransparentEvent nearest;
|
||||
nearest.hit = false;
|
||||
nearest.travel = intervalEnd;
|
||||
nearest.color = 0.0;
|
||||
nearest.alpha = 0.0;
|
||||
nearest.coverage = 0.0;
|
||||
nearest.surfaceId = -1;
|
||||
considerFroxelTransparentEvents(origin, direction, froxelIndex, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
return nearest;
|
||||
int4 ids = transparentSurfaceIds[froxelIndex];
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.x, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.y, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.z, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
considerTransparentSurfaceEvent(origin, direction, ids.w, consumedSurfaceMask, intervalStart, intervalEnd, nearest);
|
||||
}
|
||||
|
||||
float2 mediumAtlasUv(float2 uv, int sliceIndex)
|
||||
|
|
@ -714,6 +497,20 @@ TransparentEvent nearestViewFroxelTransparentEvent(float2 uv, float3 origin, flo
|
|||
return nearest;
|
||||
}
|
||||
|
||||
SolidHit nearestViewFroxelSolidHit(float2 uv, float3 origin, float3 direction, int sliceIndex)
|
||||
{
|
||||
float intervalStart = mediumSliceStartTravel(sliceIndex);
|
||||
float intervalEnd = min(mediumSliceEndTravel(sliceIndex), farDistance);
|
||||
SolidHit nearest;
|
||||
nearest.hit = false;
|
||||
nearest.travel = intervalEnd;
|
||||
nearest.normal = 0.0;
|
||||
nearest.fieldId = 0.0;
|
||||
nearest.primitiveId = -1;
|
||||
considerFroxelPrimitiveHits(origin, direction, viewFroxelIndexForUvSlice(uv, sliceIndex), intervalStart, intervalEnd, nearest);
|
||||
return nearest;
|
||||
}
|
||||
|
||||
void integrateMediumSlice(
|
||||
float2 uv,
|
||||
int sliceIndex,
|
||||
|
|
@ -802,43 +599,12 @@ RayMarchResult traverseRay(float2 uv, float3 origin, float3 direction)
|
|||
float eventSupportSum = 0.0;
|
||||
float densityAccumulation = 0.0;
|
||||
|
||||
float froxelEnter;
|
||||
float froxelExit;
|
||||
bool hasFroxelTube = traceFroxelBounds(origin, direction, froxelEnter, froxelExit);
|
||||
SolidHit nearestSolid;
|
||||
nearestSolid.hit = false;
|
||||
nearestSolid.travel = farDistance;
|
||||
nearestSolid.normal = 0.0;
|
||||
nearestSolid.fieldId = 0.0;
|
||||
nearestSolid.primitiveId = -1;
|
||||
|
||||
if (hasFroxelTube)
|
||||
{
|
||||
FroxelDda dda = beginFroxelDda(origin, direction, froxelEnter, froxelExit);
|
||||
float cellStart = froxelEnter;
|
||||
[loop]
|
||||
for (int ddaStep = 0; ddaStep < 64 && dda.active != 0; ddaStep++)
|
||||
{
|
||||
float cellEnd = froxelDdaCellExit(dda);
|
||||
if (cellEnd <= cellStart + 0.00001)
|
||||
{
|
||||
advanceFroxelDda(dda);
|
||||
cellStart = cellEnd;
|
||||
continue;
|
||||
}
|
||||
|
||||
int froxelIndex = froxelIndexForCell(dda.cell);
|
||||
SolidHit cellSolid = nearestFroxelSolidHit(origin, direction, froxelIndex, cellStart, min(cellEnd, nearestSolid.travel));
|
||||
if (cellSolid.hit && cellSolid.travel < nearestSolid.travel)
|
||||
{
|
||||
nearestSolid = cellSolid;
|
||||
}
|
||||
|
||||
cellStart = cellEnd;
|
||||
advanceFroxelDda(dda);
|
||||
}
|
||||
}
|
||||
|
||||
TransparentEvent nearestTransparent;
|
||||
nearestTransparent.hit = false;
|
||||
nearestTransparent.travel = farDistance;
|
||||
|
|
@ -849,6 +615,12 @@ RayMarchResult traverseRay(float2 uv, float3 origin, float3 direction)
|
|||
[loop]
|
||||
for (int sliceIndex = 0; sliceIndex < MEDIUM_FROXEL_SLICE_COUNT; sliceIndex++)
|
||||
{
|
||||
SolidHit cellSolid = nearestViewFroxelSolidHit(uv, origin, direction, sliceIndex);
|
||||
if (cellSolid.hit && cellSolid.travel < nearestSolid.travel)
|
||||
{
|
||||
nearestSolid = cellSolid;
|
||||
}
|
||||
|
||||
TransparentEvent cellTransparent = nearestViewFroxelTransparentEvent(uv, origin, direction, sliceIndex, 0u);
|
||||
if (cellTransparent.hit && cellTransparent.travel < nearestTransparent.travel)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -93,3 +93,4 @@
|
|||
{"timestamp":"2026-05-09T20:42:27.8430452+01:00","topic":"d3d12-froxel-transparent-surfaces","lesson":"Fixed view-travel slices are not a froxel traversal. Transparent Grid/particle surfaces need candidate discovery by spatial froxel DDA with exact cell intervals, event selection/deduplication, and shading/filtering after the canonical event is chosen so ddx/ddy are not evaluated inside divergent traversal. The headless smoke timeout must track the ready-frame contract; debug D3D12 PSO compilation can exceed 15 seconds while the app intentionally presents the loading frame.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","scripts/dev-reload.ps1","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"]}
|
||||
{"timestamp":"2026-05-09T21:01:42.6014332+01:00","topic":"d3d12-view-froxel-transparent-surfaces","lesson":"Transparent surfaces must be binned into the view-froxel tube marched by the scene ray, not into the coarse 8x8x4 world broadphase. Aquarium now bootstraps this with a CPU-filled candidate buffer indexed by low-res screen cell and depth slice; this proves the topology, but the lasting implementation should be a GPU/raster binning pass to avoid large per-frame uploads.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/D3D12ResourceRegistry.cs","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T21:13:51.8362280+01:00","topic":"d3d12-view-froxel-transparent-lookup","lesson":"View-froxel transparent lookup must not depend on the coarse world broadphase. A ray that misses the legacy 8x8x4 solid broadphase can still have valid transparent/medium candidates in its screen-space tube; only solid lookup should be gated by the world broadphase hit.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl"]}
|
||||
{"timestamp":"2026-05-09T21:19:15.6975837+01:00","topic":"d3d12-view-froxel-all-candidates","lesson":"The D3D12 scene path must not keep a parallel world-space candidate broadphase. Solid primitives and transparent surfaces now both use view-froxel candidate buffers indexed by low-res screen cell and depth slice; the scene ray marches its assigned tube and tests candidates only inside each slice interval. CPU binning is temporary scaffolding until GPU/raster binning owns the producer side.","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.",
|
||||
"Transparent D3D12 surfaces must enter the same froxel/field candidate path as volumetrics, but not as pre-averaged froxel color. Grid line transparency is a scene-traversal transparent event: the CPU bins the Grid only into a conservative height slab, the shader brackets the height-sheet crossing inside the active ray interval, samples the height texture at the hit, evaluates cartesian lines/isolines/field lines there, composites without terminating, and keeps marching. Future transparent particles and billboard surfaces need the same candidate/event pipe with quad intersections and richer per-froxel event lists rather than alpha side channels or medium-atlas summaries.",
|
||||
"D3D12 transparent surface discovery belongs in the view-froxel volume, not the 8x8x4 world broadphase. At 720p the active tube is 160x90x32; candidate ids are indexed by low-res screen cell and depth slice, then scene rays test exact transparent intersections inside that slice interval. The CPU-filled view-froxel buffer is a bootstrap only; the durable path should become a GPU/raster binning pass so per-frame upload bandwidth does not become the next stupid tax.",
|
||||
"D3D12 scene candidate discovery belongs in the view-froxel volume. At 720p the active tube is 160x90x32; primitive and transparent candidate ids are indexed by low-res screen cell and depth slice, then scene rays test exact solid/transparent 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.",
|
||||
"Use Vortice at the graphics API membrane. Keep CultMath as the CPU math grammar. Do not let third-party math opinions leak inward unless the conversion is explicit.",
|
||||
|
|
|
|||
|
|
@ -10,14 +10,13 @@ DirectWrite debug UI through a narrow D3D11On12 overlay bridge. D3D11 remains
|
|||
selectable only as temporary reference ballast until visible D3D12 use survives
|
||||
the cut.
|
||||
|
||||
Current renderer correction: transparent Grid candidates now live in the
|
||||
view-froxel volume, not the coarse 8x8x4 world broadphase. CPU bootstrap code
|
||||
intersects each low-res 8x8 pixel tube with the Grid height slab, marks the
|
||||
overlapping 160x90x32-at-720p depth slices, and the scene shader looks up
|
||||
transparent candidate ids by `(lowX, lowY, slice)` before doing exact Grid
|
||||
height-sheet intersection for that slice interval. Solids still use the old
|
||||
world broadphase. Next steel-plate step is replacing the CPU-filled transparent
|
||||
candidate buffer with a GPU/raster binning pass.
|
||||
Current renderer correction: D3D12 scene candidates now live in the view-froxel
|
||||
volume. CPU bootstrap code intersects each low-res 8x8 pixel tube with solid
|
||||
sphere bounds and the Grid height slab, marks the overlapping
|
||||
160x90x32-at-720p depth slices, and the scene shader looks up solid and
|
||||
transparent candidate ids by `(lowX, lowY, slice)` before doing exact
|
||||
intersection for that slice interval. Next steel-plate step is replacing the
|
||||
CPU-filled candidate buffers with GPU/raster binning passes.
|
||||
|
||||
## Hot Context
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue