Add residual quadtree erosion residency
This commit is contained in:
parent
db69e28d2c
commit
5158bb4103
15 changed files with 373 additions and 93 deletions
|
|
@ -540,14 +540,23 @@ public readonly record struct AquariumPlanetarySurfacePageInput(Vector4 Directio
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly record struct AquariumPlanetarySurfacePageMetadata(Vector4 Address, Vector4 Layout, Vector4 Bounds, Vector4 State);
|
||||
|
||||
public sealed class AquariumPlanetarySurfacePage
|
||||
{
|
||||
public long ContentKey { get; init; }
|
||||
public IReadOnlyList<AquariumPlanetarySurfacePageInput> Samples { get; init; } = [];
|
||||
public AquariumPlanetarySurfacePageMetadata Metadata { get; init; }
|
||||
public bool HasInput => ContentKey != 0 && Samples.Count > 0;
|
||||
}
|
||||
|
||||
public sealed class AquariumPlanetarySurfacePageSet
|
||||
{
|
||||
public static AquariumPlanetarySurfacePageSet Empty { get; } = new();
|
||||
public string GeneratorEntryPoint { get; init; } = "D3D12PlanetaryTerrainPageCS";
|
||||
public long Version { get; init; }
|
||||
public IReadOnlyList<AquariumPlanetarySurfacePageInput> Samples { get; init; } = [];
|
||||
public AquariumPlanetarySurfacePageMetadata Metadata { get; init; }
|
||||
public bool HasInput => Version > 0 && Samples.Count > 0;
|
||||
public long ContentVersion { get; init; }
|
||||
public long PresentationVersion { get; init; }
|
||||
public IReadOnlyList<AquariumPlanetarySurfacePage> Pages { get; init; } = [];
|
||||
public int SampleCount => Pages.Sum(page => page.Samples.Count);
|
||||
public bool HasInput => ContentVersion != 0 && Pages.Count > 0 && Pages.All(page => page.HasInput);
|
||||
}
|
||||
|
||||
public sealed class AquariumBokushoBrushFrame
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ internal static class D3D12ComputeProbe
|
|||
{
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(0, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(0, 0), ShaderVisibility.All),
|
||||
new RootParameter(new RootConstants(0, 0, 1), ShaderVisibility.All),
|
||||
new RootParameter(new RootConstants(0, 0, 3), ShaderVisibility.All),
|
||||
};
|
||||
var signatureDescription = new RootSignatureDescription(RootSignatureFlags.None, parameters, []);
|
||||
using var signature = device.CreateRootSignature(0, in signatureDescription, RootSignatureVersion.Version1);
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const int RootPlanetarySurfacePage = 29;
|
||||
private const int RootPlanetarySurfacePageMetadata = 30;
|
||||
private const int RootPlanetarySurfacePageSummary = 31;
|
||||
private const int RootPlanetarySurfacePageSet = 32;
|
||||
private const int RootFusionFrameConstants = 0;
|
||||
private const int RootFusionSeeds = 1;
|
||||
private const int RootFusionSensorCameras = 2;
|
||||
|
|
@ -321,8 +322,10 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private D3D12StructuredBuffer? planetarySurfacePageOutputBuffer;
|
||||
private D3D12StructuredBuffer? planetarySurfacePageMetadataBuffer;
|
||||
private D3D12StructuredBuffer? planetarySurfacePageSummaryBuffer;
|
||||
private D3D12StructuredBuffer? planetarySurfacePageSetBuffer;
|
||||
private AquariumPlanetarySurfacePageSet activePlanetarySurfacePages = AquariumPlanetarySurfacePageSet.Empty;
|
||||
private long residentPlanetarySurfacePageVersion;
|
||||
private long residentPlanetarySurfacePageContentVersion;
|
||||
private long residentPlanetarySurfacePagePresentationVersion;
|
||||
private D3D12StructuredBuffer fieldReservoirCandidateBuffer = null!;
|
||||
private D3D12StructuredBuffer fieldReservoirLockBuffer = null!;
|
||||
private readonly List<D3D12TubeFieldDrawBatch> tubeFieldDrawBatches = [];
|
||||
|
|
@ -1762,6 +1765,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
bokushoSourcePointBuffer.Dispose();
|
||||
bokushoPageDensityBuffer.Dispose();
|
||||
planetarySurfacePageMetadataBuffer?.Dispose();
|
||||
planetarySurfacePageSetBuffer?.Dispose();
|
||||
planetarySurfacePageSummaryBuffer?.Dispose();
|
||||
planetarySurfacePageOutputBuffer?.Dispose();
|
||||
planetarySurfacePageInputBuffer?.Dispose();
|
||||
|
|
@ -3777,6 +3781,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
context.CommandList.SetGraphicsRootShaderResourceView(RootPlanetarySurfacePage, planetarySurfacePageOutputBuffer.Resource.GPUVirtualAddress);
|
||||
context.CommandList.SetGraphicsRootShaderResourceView(RootPlanetarySurfacePageMetadata, planetarySurfacePageMetadataBuffer.Resource.GPUVirtualAddress);
|
||||
context.CommandList.SetGraphicsRootShaderResourceView(RootPlanetarySurfacePageSummary, planetarySurfacePageSummaryBuffer!.Resource.GPUVirtualAddress);
|
||||
context.CommandList.SetGraphicsRootShaderResourceView(RootPlanetarySurfacePageSet, planetarySurfacePageSetBuffer!.Resource.GPUVirtualAddress);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(RootSceneOverdraw, frameResources.SceneOverdrawDescriptor.Gpu);
|
||||
|
||||
context.CommandList.RSSetViewports(viewport);
|
||||
|
|
@ -3898,32 +3903,52 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
|
||||
private void EnsurePlanetarySurfacePageBuffers()
|
||||
{
|
||||
var count = Math.Max(activePlanetarySurfacePages.Samples.Count, 1);
|
||||
if (planetarySurfacePageInputBuffer?.ElementCount == count) return;
|
||||
planetarySurfacePageMetadataBuffer?.Dispose(); planetarySurfacePageSummaryBuffer?.Dispose(); planetarySurfacePageOutputBuffer?.Dispose(); planetarySurfacePageInputBuffer?.Dispose();
|
||||
planetarySurfacePageInputBuffer = new D3D12StructuredBuffer(device, count, Marshal.SizeOf<AquariumPlanetarySurfacePageInput>(), "Aquarium Planetary Page Inputs");
|
||||
planetarySurfacePageOutputBuffer = new D3D12StructuredBuffer(device, count, 32, "Aquarium Planetary Page Outputs", allowUnorderedAccess: true);
|
||||
planetarySurfacePageMetadataBuffer = new D3D12StructuredBuffer(device, 1, Marshal.SizeOf<AquariumPlanetarySurfacePageMetadata>(), "Aquarium Planetary Page Metadata");
|
||||
planetarySurfacePageSummaryBuffer = new D3D12StructuredBuffer(device, 1, 32, "Aquarium Planetary Page Summary", allowUnorderedAccess: true);
|
||||
residentPlanetarySurfacePageVersion = 0;
|
||||
var sampleCount = Math.Max(activePlanetarySurfacePages.SampleCount, 1);
|
||||
var pageCount = Math.Max(activePlanetarySurfacePages.Pages.Count, 1);
|
||||
if (planetarySurfacePageInputBuffer?.ElementCount == sampleCount && planetarySurfacePageMetadataBuffer?.ElementCount == pageCount) return;
|
||||
planetarySurfacePageSetBuffer?.Dispose(); planetarySurfacePageMetadataBuffer?.Dispose(); planetarySurfacePageSummaryBuffer?.Dispose(); planetarySurfacePageOutputBuffer?.Dispose(); planetarySurfacePageInputBuffer?.Dispose();
|
||||
planetarySurfacePageInputBuffer = new D3D12StructuredBuffer(device, sampleCount, Marshal.SizeOf<AquariumPlanetarySurfacePageInput>(), "Aquarium Planetary Page Inputs");
|
||||
planetarySurfacePageOutputBuffer = new D3D12StructuredBuffer(device, sampleCount, 32, "Aquarium Planetary Page Outputs", allowUnorderedAccess: true);
|
||||
planetarySurfacePageMetadataBuffer = new D3D12StructuredBuffer(device, pageCount, Marshal.SizeOf<AquariumPlanetarySurfacePageMetadata>(), "Aquarium Planetary Page Metadata");
|
||||
planetarySurfacePageSummaryBuffer = new D3D12StructuredBuffer(device, pageCount, 32, "Aquarium Planetary Page Summaries", allowUnorderedAccess: true);
|
||||
planetarySurfacePageSetBuffer = new D3D12StructuredBuffer(device, 1, 16, "Aquarium Planetary Page Set");
|
||||
residentPlanetarySurfacePageContentVersion = 0;
|
||||
residentPlanetarySurfacePagePresentationVersion = 0;
|
||||
}
|
||||
|
||||
private void DispatchPlanetarySurfacePage(ID3D12GraphicsCommandList activeCommandList, FrameResources frameResources)
|
||||
{
|
||||
if (planetarySurfacePageInputBuffer is null || planetarySurfacePageOutputBuffer is null || planetarySurfacePageMetadataBuffer is null || planetarySurfacePageSummaryBuffer is null) return;
|
||||
if (planetarySurfacePageInputBuffer is null || planetarySurfacePageOutputBuffer is null || planetarySurfacePageMetadataBuffer is null || planetarySurfacePageSummaryBuffer is null || planetarySurfacePageSetBuffer is null) return;
|
||||
if (!activePlanetarySurfacePages.HasInput)
|
||||
{
|
||||
if (residentPlanetarySurfacePageVersion != 0)
|
||||
if (residentPlanetarySurfacePageContentVersion != 0)
|
||||
{
|
||||
planetarySurfacePageMetadataBuffer.Upload(activeCommandList, frameResources.UploadRing, new[] { default(AquariumPlanetarySurfacePageMetadata) });
|
||||
residentPlanetarySurfacePageVersion = 0;
|
||||
planetarySurfacePageSetBuffer.Upload(activeCommandList, frameResources.UploadRing, new[] { Vector4.Zero });
|
||||
residentPlanetarySurfacePageContentVersion = 0;
|
||||
residentPlanetarySurfacePagePresentationVersion = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (residentPlanetarySurfacePageVersion == activePlanetarySurfacePages.Version) return;
|
||||
var samples = activePlanetarySurfacePages.Samples as AquariumPlanetarySurfacePageInput[] ?? activePlanetarySurfacePages.Samples.ToArray();
|
||||
var pages = activePlanetarySurfacePages.Pages;
|
||||
var metadata = new AquariumPlanetarySurfacePageMetadata[pages.Count];
|
||||
var samples = new AquariumPlanetarySurfacePageInput[activePlanetarySurfacePages.SampleCount];
|
||||
var sampleOffset = 0;
|
||||
for (var pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
||||
{
|
||||
var page = pages[pageIndex];
|
||||
for (var index = 0; index < page.Samples.Count; index++) samples[sampleOffset + index] = page.Samples[index];
|
||||
metadata[pageIndex] = page.Metadata with { Layout = new Vector4(sampleOffset, page.Metadata.Layout.Y, page.Metadata.Layout.Z, page.Metadata.Layout.W) };
|
||||
sampleOffset += page.Samples.Count;
|
||||
}
|
||||
if (residentPlanetarySurfacePagePresentationVersion != activePlanetarySurfacePages.PresentationVersion)
|
||||
{
|
||||
planetarySurfacePageMetadataBuffer.Upload(activeCommandList, frameResources.UploadRing, metadata);
|
||||
planetarySurfacePageSetBuffer.Upload(activeCommandList, frameResources.UploadRing, new[] { new Vector4(pages.Count, 0, 0, 1) });
|
||||
residentPlanetarySurfacePagePresentationVersion = activePlanetarySurfacePages.PresentationVersion;
|
||||
}
|
||||
if (residentPlanetarySurfacePageContentVersion == activePlanetarySurfacePages.ContentVersion) return;
|
||||
planetarySurfacePageInputBuffer.Upload(activeCommandList, frameResources.UploadRing, samples);
|
||||
planetarySurfacePageMetadataBuffer.Upload(activeCommandList, frameResources.UploadRing, new[] { activePlanetarySurfacePages.Metadata });
|
||||
planetarySurfacePageOutputBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
activeCommandList.SetComputeRootSignature(planetarySurfacePageRootSignature);
|
||||
activeCommandList.SetPipelineState(planetarySurfacePagePipelineState!);
|
||||
|
|
@ -3937,12 +3962,17 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
activeCommandList.SetPipelineState(planetarySurfacePageSummaryPipelineState!);
|
||||
activeCommandList.SetComputeRootShaderResourceView(0, planetarySurfacePageOutputBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRootUnorderedAccessView(1, planetarySurfacePageSummaryBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.SetComputeRoot32BitConstant(2, (uint)samples.Length, 0);
|
||||
activeCommandList.Dispatch(1, 1, 1);
|
||||
for (var pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
||||
{
|
||||
activeCommandList.SetComputeRoot32BitConstant(2, (uint)pages[pageIndex].Samples.Count, 0);
|
||||
activeCommandList.SetComputeRoot32BitConstant(2, (uint)metadata[pageIndex].Layout.X, 1);
|
||||
activeCommandList.SetComputeRoot32BitConstant(2, (uint)pageIndex, 2);
|
||||
activeCommandList.Dispatch(1, 1, 1);
|
||||
}
|
||||
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(planetarySurfacePageSummaryBuffer.Resource));
|
||||
planetarySurfacePageSummaryBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
planetarySurfacePageOutputBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
residentPlanetarySurfacePageVersion = activePlanetarySurfacePages.Version;
|
||||
residentPlanetarySurfacePageContentVersion = activePlanetarySurfacePages.ContentVersion;
|
||||
}
|
||||
|
||||
private void DispatchFractalReservoirs(ID3D12GraphicsCommandList activeCommandList, FrameResources frameResources)
|
||||
|
|
@ -6326,6 +6356,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(81, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(82, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(83, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(84, 0), ShaderVisibility.All),
|
||||
};
|
||||
var staticSamplers = new[]
|
||||
{
|
||||
|
|
@ -6357,7 +6388,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
{
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(0, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(0, 0), ShaderVisibility.All),
|
||||
new RootParameter(new RootConstants(0, 0, 1), ShaderVisibility.All),
|
||||
new RootParameter(new RootConstants(0, 0, 3), ShaderVisibility.All),
|
||||
};
|
||||
var description = new RootSignatureDescription(RootSignatureFlags.None, parameters, []);
|
||||
return device.CreateRootSignature(0, in description, RootSignatureVersion.Version1);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ struct PageOutput { float4 height_gradient; float4 masks; };
|
|||
struct PageSummary { float4 bounds; float4 metadata; };
|
||||
StructuredBuffer<PageOutput> summary_inputs : register(t0);
|
||||
RWStructuredBuffer<PageSummary> summary_outputs : register(u0);
|
||||
cbuffer PageConstants : register(b0) { uint page_sample_count; }
|
||||
cbuffer PageConstants : register(b0) { uint page_sample_count; uint page_sample_offset; uint page_summary_index; }
|
||||
|
||||
[numthreads(1,1,1)]
|
||||
void D3D12ZyphosTerrainPageSummaryCS(uint3 id : SV_DispatchThreadID)
|
||||
|
|
@ -14,12 +14,12 @@ void D3D12ZyphosTerrainPageSummaryCS(uint3 id : SV_DispatchThreadID)
|
|||
float maximum_unresolved = 0.0;
|
||||
[loop] for (uint index = 0; index < page_sample_count; index++)
|
||||
{
|
||||
float4 sample = summary_inputs[index].height_gradient;
|
||||
float4 sample = summary_inputs[page_sample_offset + index].height_gradient;
|
||||
minimum_height = min(minimum_height, sample.x);
|
||||
maximum_height = max(maximum_height, sample.x);
|
||||
maximum_slope = max(maximum_slope, length(sample.yz));
|
||||
maximum_unresolved = max(maximum_unresolved, sample.w);
|
||||
}
|
||||
summary_outputs[0].bounds = float4(minimum_height, maximum_height, maximum_slope, maximum_unresolved);
|
||||
summary_outputs[0].metadata = float4(page_sample_count, 0.0, 0.0, 1.0);
|
||||
summary_outputs[page_summary_index].bounds = float4(minimum_height, maximum_height, maximum_slope, maximum_unresolved);
|
||||
summary_outputs[page_summary_index].metadata = float4(page_sample_count, page_sample_offset, page_summary_index, 1.0);
|
||||
}
|
||||
|
|
|
|||
3
src/Aquarium.Zyphos/Properties/AssemblyInfo.cs
Normal file
3
src/Aquarium.Zyphos/Properties/AssemblyInfo.cs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Aquarium.Engine.Tests")]
|
||||
|
|
@ -244,10 +244,13 @@ float sdfDistance(float3 p, int sdfIndex)
|
|||
float zyPlanetTraceBoundRadius(SdfObject sdfObject)
|
||||
{
|
||||
float legacyBound = max(sdfObject.centerRadius.w * 1.42, 0.001);
|
||||
ZyPlanetPageSummary summary = zy_planet_page_summary[0];
|
||||
ZyPlanetPageMetadata metadata = zy_planet_page_metadata[0];
|
||||
if (metadata.state.x < 0.5 || summary.metadata.w < 0.5) return legacyBound;
|
||||
float erosionExtent = max(abs(summary.bounds.x), abs(summary.bounds.y)) + max(summary.bounds.w, 0.0);
|
||||
float erosionExtent=0.0; int pageCount=min((int)zy_planet_page_set[0].state.x,64);
|
||||
[loop] for(int pageIndex=0;pageIndex<pageCount;pageIndex++)
|
||||
{
|
||||
ZyPlanetPageSummary summary=zy_planet_page_summary[pageIndex]; ZyPlanetPageMetadata metadata=zy_planet_page_metadata[pageIndex];
|
||||
if(metadata.state.x<0.5||summary.metadata.w<0.5)continue;
|
||||
erosionExtent+=max(abs(summary.bounds.x),abs(summary.bounds.y))*saturate(metadata.state.y)+max(summary.bounds.w,0.0);
|
||||
}
|
||||
return max(legacyBound, max(sdfObject.state.x, 0.001) + erosionExtent);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,11 @@
|
|||
struct ZyPlanetPageOutput { float4 height_gradient; float4 masks; };
|
||||
struct ZyPlanetPageMetadata { float4 address; float4 layout; float4 bounds; float4 state; };
|
||||
struct ZyPlanetPageSummary { float4 bounds; float4 metadata; };
|
||||
struct ZyPlanetPageSet { float4 state; };
|
||||
StructuredBuffer<ZyPlanetPageOutput> zy_planet_page : register(t81);
|
||||
StructuredBuffer<ZyPlanetPageMetadata> zy_planet_page_metadata : register(t82);
|
||||
StructuredBuffer<ZyPlanetPageSummary> zy_planet_page_summary : register(t83);
|
||||
StructuredBuffer<ZyPlanetPageSet> zy_planet_page_set : register(t84);
|
||||
|
||||
static const float ZY_TERRAIN_NON_EROSION_SLOPE_BOUND = 8.0;
|
||||
|
||||
|
|
@ -20,38 +22,51 @@ float2 zyTerrainCubeFaceUv(float3 dir, out float face)
|
|||
if(dir.z>=0){face=4;return dir.xy/a.z;}face=5;return float2(-dir.x,dir.y)/a.z;
|
||||
}
|
||||
|
||||
bool zyTrySampleErosionPage(float3 dir, out float4 heightGradient, out float2 masks)
|
||||
bool zyTerrainPageLocal(float3 dir, ZyPlanetPageMetadata metadata, out float2 local)
|
||||
{
|
||||
ZyPlanetPageMetadata metadata=zy_planet_page_metadata[0]; heightGradient=0; masks=0;
|
||||
if(metadata.state.x<0.5)return false;
|
||||
float face; float2 uv=zyTerrainCubeFaceUv(dir,face); if(abs(face-metadata.address.x)>0.25)return false;
|
||||
float axisTiles=exp2(metadata.address.y); float2 local=(uv*0.5+0.5)*axisTiles-metadata.address.zw;
|
||||
if(any(local<0.0)||any(local>1.0))return false;
|
||||
float face; float2 uv=zyTerrainCubeFaceUv(dir,face); local=0.0;
|
||||
if(metadata.state.x<0.5||abs(face-metadata.address.x)>0.25)return false;
|
||||
float axisTiles=exp2(metadata.address.y); local=(uv*0.5+0.5)*axisTiles-metadata.address.zw;
|
||||
return all(local>=0.0)&&all(local<=1.0);
|
||||
}
|
||||
|
||||
void zySampleErosionPage(int pageIndex, float2 local, out float4 heightGradient, out float2 masks)
|
||||
{
|
||||
ZyPlanetPageMetadata metadata=zy_planet_page_metadata[pageIndex];
|
||||
float storage=metadata.layout.y, interior=metadata.layout.z, border=metadata.layout.w;
|
||||
float2 texel=local*(interior-1.0)+border; float2 base=floor(texel); float2 fraction=frac(texel);
|
||||
int2 p0=(int2)clamp(base,0.0,storage-1.0); int2 p1=min(p0+1,(int2)(storage-1.0));
|
||||
ZyPlanetPageOutput a=zy_planet_page[p0.y*(int)storage+p0.x], b=zy_planet_page[p0.y*(int)storage+p1.x];
|
||||
ZyPlanetPageOutput c=zy_planet_page[p1.y*(int)storage+p0.x], d=zy_planet_page[p1.y*(int)storage+p1.x];
|
||||
int offset=(int)metadata.layout.x;
|
||||
ZyPlanetPageOutput a=zy_planet_page[offset+p0.y*(int)storage+p0.x], b=zy_planet_page[offset+p0.y*(int)storage+p1.x];
|
||||
ZyPlanetPageOutput c=zy_planet_page[offset+p1.y*(int)storage+p0.x], d=zy_planet_page[offset+p1.y*(int)storage+p1.x];
|
||||
heightGradient=lerp(lerp(a.height_gradient,b.height_gradient,fraction.x),lerp(c.height_gradient,d.height_gradient,fraction.x),fraction.y);
|
||||
masks=lerp(lerp(a.masks.xy,b.masks.xy,fraction.x),lerp(c.masks.xy,d.masks.xy,fraction.x),fraction.y); return true;
|
||||
masks=lerp(lerp(a.masks.xy,b.masks.xy,fraction.x),lerp(c.masks.xy,d.masks.xy,fraction.x),fraction.y);
|
||||
}
|
||||
|
||||
bool zyTryGetErosionPageSummary(float3 dir, out ZyPlanetPageSummary summary)
|
||||
bool zyTrySampleErosionPage(float3 dir, out float4 heightGradient, out float2 masks)
|
||||
{
|
||||
summary = zy_planet_page_summary[0];
|
||||
ZyPlanetPageMetadata metadata = zy_planet_page_metadata[0];
|
||||
if (metadata.state.x < 0.5 || summary.metadata.w < 0.5) return false;
|
||||
float face; float2 uv = zyTerrainCubeFaceUv(dir, face);
|
||||
if (abs(face - metadata.address.x) > 0.25) return false;
|
||||
float axisTiles = exp2(metadata.address.y);
|
||||
float2 local = (uv * 0.5 + 0.5) * axisTiles - metadata.address.zw;
|
||||
return all(local >= 0.0) && all(local <= 1.0);
|
||||
heightGradient=0.0; masks=0.0; bool found=false;
|
||||
int pageCount=min((int)zy_planet_page_set[0].state.x,64);
|
||||
[loop] for(int pageIndex=0;pageIndex<pageCount;pageIndex++)
|
||||
{
|
||||
float2 local; ZyPlanetPageMetadata metadata=zy_planet_page_metadata[pageIndex];
|
||||
if(!zyTerrainPageLocal(dir,metadata,local))continue;
|
||||
float4 contribution; float2 pageMasks; zySampleErosionPage(pageIndex,local,contribution,pageMasks);
|
||||
float blend=saturate(metadata.state.y); heightGradient+=contribution*blend;
|
||||
masks=found?lerp(masks,pageMasks,blend):pageMasks; found=true;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
float zyTerrainConservativeDistanceScale(float3 dir)
|
||||
{
|
||||
ZyPlanetPageSummary summary;
|
||||
float erosionSlope = zyTryGetErosionPageSummary(dir, summary) ? summary.bounds.z : 0.0;
|
||||
float erosionSlope=0.0; int pageCount=min((int)zy_planet_page_set[0].state.x,64);
|
||||
[loop] for(int pageIndex=0;pageIndex<pageCount;pageIndex++)
|
||||
{
|
||||
float2 local; ZyPlanetPageMetadata metadata=zy_planet_page_metadata[pageIndex];
|
||||
if(zyTerrainPageLocal(dir,metadata,local)&&zy_planet_page_summary[pageIndex].metadata.w>0.5)
|
||||
erosionSlope+=max(zy_planet_page_summary[pageIndex].bounds.z,0.0)*saturate(metadata.state.y);
|
||||
}
|
||||
float totalSlopeBound = ZY_TERRAIN_NON_EROSION_SLOPE_BOUND + max(erosionSlope, 0.0);
|
||||
return rsqrt(1.0 + totalSlopeBound * totalSlopeBound);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,13 @@ StructuredBuffer<PageInput> page_inputs : register(t0);
|
|||
RWStructuredBuffer<PageOutput> page_outputs : register(u0);
|
||||
cbuffer PageConstants : register(b0) { uint page_sample_count; }
|
||||
|
||||
float zyPageErosion(float3 direction, float radius, float spacing)
|
||||
float zyPageErosion(float3 direction, float radius, float spacing, float parent_spacing)
|
||||
{
|
||||
float3 dir=normalize(direction); return zyAdvancedErosion(dir,zySphericalField(dir),radius,spacing).x;
|
||||
float3 dir=normalize(direction);
|
||||
float child=zyAdvancedErosion(dir,zySphericalField(dir),radius,spacing).x;
|
||||
if(parent_spacing<=0.0)return child;
|
||||
float parent=zyAdvancedErosion(dir,zySphericalField(dir),radius,parent_spacing).x;
|
||||
return child-parent;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -16,11 +20,11 @@ float zyPageErosion(float3 direction, float radius, float spacing)
|
|||
void D3D12ZyphosTerrainPageCS(uint3 id : SV_DispatchThreadID)
|
||||
{
|
||||
if(id.x>=page_sample_count) return;
|
||||
PageInput input=page_inputs[id.x]; float3 dir=normalize(input.direction_radius.xyz); float radius=input.direction_radius.w; float spacing=input.sampling.x;
|
||||
PageInput input=page_inputs[id.x]; float3 dir=normalize(input.direction_radius.xyz); float radius=input.direction_radius.w; float spacing=input.sampling.x; float parent_spacing=input.sampling.y;
|
||||
float3 reference=abs(dir.z)<0.8?float3(0,0,1):float3(0,1,0); float3 tangent_x=normalize(cross(reference,dir)); float3 tangent_y=normalize(cross(dir,tangent_x));
|
||||
float angular=max(spacing/radius,1.0e-6); float center=zyPageErosion(dir,radius,spacing);
|
||||
float dx=(zyPageErosion(normalize(dir+tangent_x*angular),radius,spacing)-zyPageErosion(normalize(dir-tangent_x*angular),radius,spacing))/(2.0*spacing);
|
||||
float dy=(zyPageErosion(normalize(dir+tangent_y*angular),radius,spacing)-zyPageErosion(normalize(dir-tangent_y*angular),radius,spacing))/(2.0*spacing);
|
||||
float angular=max(spacing/radius,1.0e-6); float center=zyPageErosion(dir,radius,spacing,parent_spacing);
|
||||
float dx=(zyPageErosion(normalize(dir+tangent_x*angular),radius,spacing,parent_spacing)-zyPageErosion(normalize(dir-tangent_x*angular),radius,spacing,parent_spacing))/(2.0*spacing);
|
||||
float dy=(zyPageErosion(normalize(dir+tangent_y*angular),radius,spacing,parent_spacing)-zyPageErosion(normalize(dir-tangent_y*angular),radius,spacing,parent_spacing))/(2.0*spacing);
|
||||
float4 erosion=zyAdvancedErosion(dir,zySphericalField(dir),radius,spacing);
|
||||
page_outputs[id.x].height_gradient=float4(center,dx,dy,erosion.w);
|
||||
page_outputs[id.x].masks=float4(erosion.y,erosion.z,0,1);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ struct PageOutput { float4 height_gradient; float4 masks; };
|
|||
struct PageSummary { float4 bounds; float4 metadata; };
|
||||
StructuredBuffer<PageOutput> summary_inputs : register(t0);
|
||||
RWStructuredBuffer<PageSummary> summary_outputs : register(u0);
|
||||
cbuffer PageConstants : register(b0) { uint page_sample_count; }
|
||||
cbuffer PageConstants : register(b0) { uint page_sample_count; uint page_sample_offset; uint page_summary_index; }
|
||||
|
||||
[numthreads(1,1,1)]
|
||||
void D3D12ZyphosTerrainPageSummaryCS(uint3 id : SV_DispatchThreadID)
|
||||
|
|
@ -11,10 +11,10 @@ void D3D12ZyphosTerrainPageSummaryCS(uint3 id : SV_DispatchThreadID)
|
|||
float minimum_height=3.402823466e+38, maximum_height=-3.402823466e+38, maximum_slope=0.0, maximum_unresolved=0.0;
|
||||
[loop] for(uint index=0;index<page_sample_count;index++)
|
||||
{
|
||||
float4 sample=summary_inputs[index].height_gradient;
|
||||
float4 sample=summary_inputs[page_sample_offset+index].height_gradient;
|
||||
minimum_height=min(minimum_height,sample.x); maximum_height=max(maximum_height,sample.x);
|
||||
maximum_slope=max(maximum_slope,length(sample.yz)); maximum_unresolved=max(maximum_unresolved,sample.w);
|
||||
}
|
||||
summary_outputs[0].bounds=float4(minimum_height,maximum_height,maximum_slope,maximum_unresolved);
|
||||
summary_outputs[0].metadata=float4(page_sample_count,0,0,1);
|
||||
summary_outputs[page_summary_index].bounds=float4(minimum_height,maximum_height,maximum_slope,maximum_unresolved);
|
||||
summary_outputs[page_summary_index].metadata=float4(page_sample_count,page_sample_offset,page_summary_index,1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,44 +2,149 @@ using System.Numerics;
|
|||
using Aquarium.Engine.Fractal;
|
||||
using Aquarium.Engine.Fractal.Lod;
|
||||
using Aquarium.Engine.Render;
|
||||
using CultMath;
|
||||
|
||||
namespace Aquarium.Zyphos;
|
||||
|
||||
internal static class ZyphosPlanetarySurfacePages
|
||||
{
|
||||
private static readonly Dictionary<CubeFace, AquariumPlanetarySurfacePageSet> Pages = [];
|
||||
private const int InteriorSize = 17;
|
||||
private const int BorderSize = 2;
|
||||
private const int MaximumLevel = 5;
|
||||
private const float ArrivalSeconds = 0.35f;
|
||||
private static readonly Dictionary<long, AquariumPlanetarySurfacePageInput[]> Content = [];
|
||||
private static readonly Dictionary<long, float> ArrivalTimes = [];
|
||||
private static readonly Dictionary<long, float> DepartureTimes = [];
|
||||
private static readonly Dictionary<long, CubeTileKey> ResidentTiles = [];
|
||||
private static float lastTimeSeconds = float.NegativeInfinity;
|
||||
|
||||
public static AquariumPlanetarySurfacePageSet ForCamera(Vector3 cameraPosition)
|
||||
internal static void Reset()
|
||||
{
|
||||
var direction = Vector3.Normalize(cameraPosition - ZyphosUmbrosSystem.ZyphosCenter);
|
||||
var face = DominantFace(direction);
|
||||
if (Pages.TryGetValue(face, out var cached)) return cached;
|
||||
var request = new PlanetarySurfacePageRequest(new CubeTileKey(face, 0, 0, 0), 17, 2);
|
||||
var radius = ZyphosUmbrosSystem.ZyphosSurfaceRadius;
|
||||
var spacing = PlanetarySurfacePageSampling.NominalAngularTexelSize(request) * radius;
|
||||
var samples = new AquariumPlanetarySurfacePageInput[request.StorageSize * request.StorageSize];
|
||||
for (var y = 0; y < request.StorageSize; y++) for (var x = 0; x < request.StorageSize; x++)
|
||||
ArrivalTimes.Clear(); DepartureTimes.Clear(); ResidentTiles.Clear();
|
||||
lastTimeSeconds=float.NegativeInfinity;
|
||||
}
|
||||
|
||||
public static AquariumPlanetarySurfacePageSet ForCamera(Vector3 cameraPosition, float timeSeconds)
|
||||
{
|
||||
if (timeSeconds < lastTimeSeconds)
|
||||
{
|
||||
var sampleDirection = PlanetarySurfacePageSampling.Direction(request, x, y);
|
||||
samples[y * request.StorageSize + x] = new AquariumPlanetarySurfacePageInput(new Vector4(sampleDirection, radius), new Vector4(spacing, 0, 0, 0));
|
||||
ArrivalTimes.Clear(); DepartureTimes.Clear(); ResidentTiles.Clear();
|
||||
}
|
||||
var metadata = new AquariumPlanetarySurfacePageMetadata(
|
||||
new Vector4((int)face, 0, 0, 0),
|
||||
new Vector4(0, request.StorageSize, request.InteriorSize, request.BorderSize),
|
||||
Vector4.Zero,
|
||||
new Vector4(1, (int)face + 1, PlanetarySurfacePageSampling.AngularTexelSize(request), spacing));
|
||||
return Pages[face] = new AquariumPlanetarySurfacePageSet
|
||||
lastTimeSeconds = timeSeconds;
|
||||
var relative = cameraPosition - ZyphosUmbrosSystem.ZyphosCenter;
|
||||
var distance = Math.Max(relative.Length(), 0.001f);
|
||||
var direction = relative / distance;
|
||||
var desired = DesiredTiles(direction, distance).ToDictionary(ContentKey);
|
||||
foreach (var (key, tile) in desired)
|
||||
{
|
||||
ResidentTiles[key] = tile;
|
||||
if (!ArrivalTimes.TryGetValue(key, out var arrival)) ArrivalTimes[key] = arrival = timeSeconds;
|
||||
DepartureTimes.Remove(key);
|
||||
}
|
||||
foreach (var (key, tile) in ResidentTiles.ToArray())
|
||||
{
|
||||
if (tile.Level == 0 || desired.ContainsKey(key)) continue;
|
||||
if (!DepartureTimes.ContainsKey(key)) DepartureTimes[key] = timeSeconds;
|
||||
if (timeSeconds - DepartureTimes[key] < ArrivalSeconds) continue;
|
||||
ResidentTiles.Remove(key); ArrivalTimes.Remove(key); DepartureTimes.Remove(key);
|
||||
}
|
||||
|
||||
var residents = ResidentTiles.OrderBy(pair => pair.Value.Level).ThenBy(pair => pair.Key).ToArray();
|
||||
var pages = new AquariumPlanetarySurfacePage[residents.Length];
|
||||
for (var index = 0; index < residents.Length; index++)
|
||||
{
|
||||
var (key, tile) = residents[index];
|
||||
var blend = tile.Level == 0 ? 1.0f : DepartureTimes.TryGetValue(key, out var departure)
|
||||
? Math.Clamp(1.0f - (timeSeconds - departure) / ArrivalSeconds, 0.0f, 1.0f)
|
||||
: Math.Clamp((timeSeconds - ArrivalTimes[key]) / ArrivalSeconds, 0.0f, 1.0f);
|
||||
pages[index] = BuildPage(tile, key, blend);
|
||||
}
|
||||
|
||||
var contentVersion = Version(residents.Select(pair => pair.Key));
|
||||
var presentationVersion = Version(pages.Select(page => (long)HashCode.Combine(page.ContentKey, BitConverter.SingleToInt32Bits(page.Metadata.State.Y))));
|
||||
return new AquariumPlanetarySurfacePageSet
|
||||
{
|
||||
GeneratorEntryPoint = "D3D12ZyphosTerrainPageCS",
|
||||
Version = (int)face + 1, Samples = samples, Metadata = metadata,
|
||||
ContentVersion = contentVersion,
|
||||
PresentationVersion = presentationVersion,
|
||||
Pages = pages,
|
||||
};
|
||||
}
|
||||
|
||||
private static CubeFace DominantFace(Vector3 d)
|
||||
private static AquariumPlanetarySurfacePage BuildPage(CubeTileKey tile, long key, float blend)
|
||||
{
|
||||
var request = new PlanetarySurfacePageRequest(tile, InteriorSize, BorderSize);
|
||||
var radius = ZyphosUmbrosSystem.ZyphosSurfaceRadius;
|
||||
var spacing = PlanetarySurfacePageSampling.NominalAngularTexelSize(request) * radius;
|
||||
var parentSpacing = tile.Level == 0
|
||||
? 0.0f
|
||||
: PlanetarySurfacePageSampling.NominalAngularTexelSize(new PlanetarySurfacePageRequest(tile.Parent(), InteriorSize, BorderSize)) * radius;
|
||||
if (!Content.TryGetValue(key, out var samples))
|
||||
{
|
||||
samples = new AquariumPlanetarySurfacePageInput[request.StorageSize * request.StorageSize];
|
||||
for (var y = 0; y < request.StorageSize; y++) for (var x = 0; x < request.StorageSize; x++)
|
||||
{
|
||||
var sampleDirection = PlanetarySurfacePageSampling.Direction(request, x, y);
|
||||
samples[y * request.StorageSize + x] = new AquariumPlanetarySurfacePageInput(
|
||||
new Vector4(sampleDirection, radius), new Vector4(spacing, parentSpacing, 0, 0));
|
||||
}
|
||||
Content[key] = samples;
|
||||
}
|
||||
|
||||
return new AquariumPlanetarySurfacePage
|
||||
{
|
||||
ContentKey = key,
|
||||
Samples = samples,
|
||||
Metadata = new AquariumPlanetarySurfacePageMetadata(
|
||||
new Vector4((int)tile.Face, tile.Level, tile.X, tile.Y),
|
||||
new Vector4(0, request.StorageSize, request.InteriorSize, request.BorderSize),
|
||||
Vector4.Zero,
|
||||
new Vector4(1, blend, PlanetarySurfacePageSampling.NominalAngularTexelSize(request), spacing)),
|
||||
};
|
||||
}
|
||||
|
||||
private static IEnumerable<CubeTileKey> DesiredTiles(Vector3 direction, float distance)
|
||||
{
|
||||
foreach (var rootFace in Enum.GetValues<CubeFace>()) yield return new CubeTileKey(rootFace, 0, 0, 0);
|
||||
var altitude = Math.Max(distance - ZyphosUmbrosSystem.ZyphosSurfaceRadius, 0.001f);
|
||||
var footprint = Math.Max(altitude / 720.0f * 2.0f, ZyphosUmbrosSystem.ZyphosSurfaceRadius / 65536.0f);
|
||||
var radius=ZyphosUmbrosSystem.ZyphosSurfaceRadius;
|
||||
var baseWavelength=radius*0.075f*0.7f;
|
||||
var baseAmplitude=0.12f*radius*0.075f;
|
||||
var targetLevel=MaximumLevel;
|
||||
for(var level=0;level<=MaximumLevel;level++)
|
||||
{
|
||||
var request=new PlanetarySurfacePageRequest(new CubeTileKey(CubeFace.PositiveX,level,0,0),InteriorSize,BorderSize);
|
||||
var spacing=PlanetarySurfacePageSampling.NominalAngularTexelSize(request)*radius;
|
||||
var unresolved=ErosionFrequencyBands.Select(baseWavelength,spacing,7,2.0f,baseAmplitude,0.5f).UnresolvedHeightBound;
|
||||
if(unresolved<=footprint){targetLevel=level;break;}
|
||||
}
|
||||
var (face, uv) = FaceUv(direction);
|
||||
for (var level = 1; level <= targetLevel; level++)
|
||||
{
|
||||
var count = 1 << level;
|
||||
var x = Math.Clamp((int)((uv.X * 0.5f + 0.5f) * count), 0, count - 1);
|
||||
var y = Math.Clamp((int)((uv.Y * 0.5f + 0.5f) * count), 0, count - 1);
|
||||
yield return new CubeTileKey(face, level, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
private static (CubeFace Face, Vector2 Uv) FaceUv(Vector3 d)
|
||||
{
|
||||
var a = Vector3.Abs(d);
|
||||
if (a.X >= a.Y && a.X >= a.Z) return d.X >= 0 ? CubeFace.PositiveX : CubeFace.NegativeX;
|
||||
if (a.Y >= a.Z) return d.Y >= 0 ? CubeFace.PositiveY : CubeFace.NegativeY;
|
||||
return d.Z >= 0 ? CubeFace.PositiveZ : CubeFace.NegativeZ;
|
||||
if (a.X >= a.Y && a.X >= a.Z) return d.X >= 0 ? (CubeFace.PositiveX, new(-d.Z / a.X, d.Y / a.X)) : (CubeFace.NegativeX, new(d.Z / a.X, d.Y / a.X));
|
||||
if (a.Y >= a.Z) return d.Y >= 0 ? (CubeFace.PositiveY, new(d.X / a.Y, -d.Z / a.Y)) : (CubeFace.NegativeY, new(d.X / a.Y, d.Z / a.Y));
|
||||
return d.Z >= 0 ? (CubeFace.PositiveZ, new(d.X / a.Z, d.Y / a.Z)) : (CubeFace.NegativeZ, new(-d.X / a.Z, d.Y / a.Z));
|
||||
}
|
||||
|
||||
private static long ContentKey(CubeTileKey tile) => 1L + (long)tile.Face + ((long)tile.Level << 3) + ((long)tile.X << 9) + ((long)tile.Y << 29);
|
||||
|
||||
private static long Version(IEnumerable<long> values)
|
||||
{
|
||||
const long offset = 1469598103934665603;
|
||||
const long prime = 1099511628211;
|
||||
var hash = offset;
|
||||
foreach (var value in values) hash = unchecked((hash ^ value) * prime);
|
||||
return hash == 0 ? 1 : hash;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ public static class ZyphosSceneBuilder
|
|||
PriorityFocus = fractalPlan.ReservoirPriorityFocus,
|
||||
ProgramTransforms = fractalPlan.GpuProgramTransforms,
|
||||
},
|
||||
PlanetarySurfacePages = ZyphosPlanetarySurfacePages.ForCamera(cameraPosition),
|
||||
PlanetarySurfacePages = ZyphosPlanetarySurfacePages.ForCamera(cameraPosition, timeSeconds),
|
||||
SdfObjects = BuildSdfObjects(timeSeconds, previousTimeSeconds),
|
||||
SdfLights = BuildSdfLights(timeSeconds),
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue