From 313c2667ff7cd68de3b4f852b682ed3417eada9b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 22:12:48 +0100 Subject: [PATCH] Fuse shared sensor textures in Aquarium --- docs/temporal-sdf-gaussian-field.md | 12 +- .../Render/AquariumRenderPlan.cs | 3 +- .../Render/D3D12DescriptorArena.cs | 26 ++++ .../Render/D3D12ExternalSensorTexture.cs | 142 ++++++++++++++++++ src/Aquarium.Engine/Render/D3D12Renderer.cs | 102 ++++++++++++- .../Render/Shaders/D3D12LocalCastFusion.hlsl | 109 ++++++++++++-- .../GpuSensorFrameContractTests.cs | 4 +- 7 files changed, 374 insertions(+), 24 deletions(-) create mode 100644 src/Aquarium.Engine/Render/D3D12ExternalSensorTexture.cs diff --git a/docs/temporal-sdf-gaussian-field.md b/docs/temporal-sdf-gaussian-field.md index 5b8aa46..015505e 100644 --- a/docs/temporal-sdf-gaussian-field.md +++ b/docs/temporal-sdf-gaussian-field.md @@ -105,10 +105,14 @@ Ownership: Gaussian storage, dispatch, and the transition back to shader-resource state for the draw. -Current Aquarium cut: the contract and D3D12 camera metadata buffer exist, and -the fusion root now accepts that buffer beside fallback seeds. Next cut: import -the external texture handles as SRVs, bind packed camera planes and Leap packed -maps, then move stereo/flow/feature extraction directly into GPU kernels. +Current Aquarium cut: the contract, D3D12 camera metadata buffer, external +texture importer, and sensor SRV table exist. A producer may provide either a +duplicated shared handle or a named shared handle for each camera/Leap plane. +When sensor textures are present, Aquarium can dispatch fusion without fallback +seeds and write RGB-derived Gaussian samples into the temporal buffer. Next cut: +replace the first-pass per-texture sampling with calibrated stereo/flow/feature +kernels, Leap packed-map channel extraction, and confidence-weighted surface +correspondence. The first shader pass also uses camera-facing proxy planes to evaluate each kernel. True ray-integrated volume compositing, Gaussian depth sorting, and diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs index 9b47b56..0c27465 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs @@ -304,7 +304,8 @@ public readonly record struct AquariumExternalGpuTexture( int Height, AquariumGpuSensorPixelFormat PixelFormat, long TimestampNs, - int PlaneIndex = 0); + int PlaneIndex = 0, + string SharedHandleName = ""); public readonly record struct AquariumGpuSensorCamera( string SensorId, diff --git a/src/Aquarium.Engine/Render/D3D12DescriptorArena.cs b/src/Aquarium.Engine/Render/D3D12DescriptorArena.cs index 9812b5f..e658e31 100644 --- a/src/Aquarium.Engine/Render/D3D12DescriptorArena.cs +++ b/src/Aquarium.Engine/Render/D3D12DescriptorArena.cs @@ -42,6 +42,32 @@ internal sealed class D3D12DescriptorArena : IDisposable Heap.GetGPUDescriptorHandleForHeapStart() + (index * descriptorSize)); } + public D3D12DescriptorSlot AllocateRange(int count) + { + if (count <= 0) + { + throw new ArgumentOutOfRangeException(nameof(count), count, "Descriptor range count must be positive."); + } + + if (used + count > Capacity) + { + throw new InvalidOperationException($"D3D12 descriptor arena '{Name}' exhausted ({Capacity} {Type} descriptors; requested range {count})."); + } + + var index = used; + used += count; + return new D3D12DescriptorSlot( + Heap.GetCPUDescriptorHandleForHeapStart() + (index * descriptorSize), + Heap.GetGPUDescriptorHandleForHeapStart() + (index * descriptorSize)); + } + + public D3D12DescriptorSlot Offset(D3D12DescriptorSlot start, int offset) + { + return new D3D12DescriptorSlot( + start.Cpu + (offset * descriptorSize), + start.Gpu + (offset * descriptorSize)); + } + public void Reset() { used = 0; diff --git a/src/Aquarium.Engine/Render/D3D12ExternalSensorTexture.cs b/src/Aquarium.Engine/Render/D3D12ExternalSensorTexture.cs new file mode 100644 index 0000000..775a48a --- /dev/null +++ b/src/Aquarium.Engine/Render/D3D12ExternalSensorTexture.cs @@ -0,0 +1,142 @@ +using Aquarium.Engine.Render; +using SharpGen.Runtime; +using Vortice.Direct3D12; +using Vortice.DXGI; + +namespace Aquarium.Engine.Render; + +internal sealed class D3D12ExternalSensorTexture : IDisposable +{ + private D3D12ExternalSensorTexture( + string key, + IntPtr sharedHandle, + ID3D12Resource resource, + int width, + int height, + Format format, + long timestampNs) + { + Key = key; + SharedHandle = sharedHandle; + Resource = resource; + Width = width; + Height = height; + Format = format; + TimestampNs = timestampNs; + } + + public string Key { get; } + + public IntPtr SharedHandle { get; } + + public ID3D12Resource Resource { get; } + + public int Width { get; } + + public int Height { get; } + + public Format Format { get; } + + public long TimestampNs { get; private set; } + + public bool Matches(AquariumExternalGpuTexture texture) + { + return SharedHandle == texture.SharedHandle + && string.Equals(Key, CacheKey(texture), StringComparison.Ordinal) + && Width == texture.Width + && Height == texture.Height + && Format == ToDxgiFormat(texture.PixelFormat); + } + + public void UpdateTimestamp(long timestampNs) + { + TimestampNs = timestampNs; + } + + public void CreateShaderResourceView(ID3D12Device device, D3D12DescriptorSlot descriptor) + { + device.CreateShaderResourceView( + Resource, + new ShaderResourceViewDescription + { + Format = Format, + ViewDimension = ShaderResourceViewDimension.Texture2D, + Shader4ComponentMapping = ShaderComponentMapping.Default, + Texture2D = new Texture2DShaderResourceView { MipLevels = 1 }, + }, + descriptor.Cpu); + } + + public static bool TryOpen(ID3D12Device device, AquariumExternalGpuTexture texture, out D3D12ExternalSensorTexture externalTexture) + { + externalTexture = null!; + if ((texture.SharedHandle == IntPtr.Zero && string.IsNullOrWhiteSpace(texture.SharedHandleName)) + || texture.Width <= 0 + || texture.Height <= 0) + { + return false; + } + + var format = ToDxgiFormat(texture.PixelFormat); + if (format == Format.Unknown) + { + return false; + } + + try + { + var sharedHandle = texture.SharedHandle; + if (sharedHandle == IntPtr.Zero) + { + sharedHandle = device.OpenSharedHandleByName(texture.SharedHandleName); + } + + var resource = device.OpenSharedHandle(sharedHandle); + resource.Name = $"Aquarium external sensor texture {texture.Handle.Name}"; + externalTexture = new D3D12ExternalSensorTexture( + CacheKey(texture), + sharedHandle, + resource, + texture.Width, + texture.Height, + format, + texture.TimestampNs); + return true; + } + catch (SharpGenException) + { + return false; + } + catch (InvalidOperationException) + { + return false; + } + } + + public static string CacheKey(AquariumExternalGpuTexture texture) + { + return texture.SharedHandle != IntPtr.Zero + ? $"handle:{texture.SharedHandle.ToInt64():x}" + : $"name:{texture.SharedHandleName}"; + } + + public static Format ToDxgiFormat(AquariumGpuSensorPixelFormat format) + { + return format switch + { + AquariumGpuSensorPixelFormat.Bgra8Unorm => Format.B8G8R8A8_UNorm, + AquariumGpuSensorPixelFormat.Rgba8Unorm => Format.R8G8B8A8_UNorm, + AquariumGpuSensorPixelFormat.R8Unorm => Format.R8_UNorm, + AquariumGpuSensorPixelFormat.R16Unorm => Format.R16_UNorm, + AquariumGpuSensorPixelFormat.R16Float => Format.R16_Float, + AquariumGpuSensorPixelFormat.Rg8Unorm => Format.R8G8_UNorm, + AquariumGpuSensorPixelFormat.LeapPackedMap => Format.R8G8B8A8_UNorm, + _ => Format.Unknown, + }; + } + + public void Dispose() + { + Resource.Dispose(); + } +} diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index a673b46..2670ada 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -32,6 +32,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private const int MaxSdfLightCount = 64; private const int MaxSdfObjectCount = 64; private const int MaxGpuSensorCameraCount = 32; + private const int MaxGpuSensorTextureCount = 8; + private const int GpuSensorSamplesPerTexture = 131_072; private const int MaxTemporalGaussianCount = 1_048_576; private const float SurfaceTransparentMinZ = -1.85f; private const float SurfaceTransparentMaxZ = 0.45f; @@ -56,7 +58,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private const int RootFusionFrameConstants = 0; private const int RootFusionSeeds = 1; private const int RootFusionSensorCameras = 2; - private const int RootFusionOutput = 3; + private const int RootFusionSensorTextures = 3; + private const int RootFusionOutput = 4; private static readonly DebugUi.DebugUiOption[] RenderDebugOptions = [ new(0, "Final"), @@ -136,6 +139,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private readonly D3D12StructuredBuffer gpuSensorCameraBuffer; private readonly D3D12StructuredBuffer gpuFusionSeedBuffer; private readonly D3D12StructuredBuffer temporalGaussianBuffer; + private readonly Dictionary externalSensorTextures = new(StringComparer.Ordinal); private readonly D3D12CubeTexture studioPmremTexture; private readonly D3D12CubeTexture studioIrradianceTexture; private readonly AquariumSdfLight[] sdfLights = new AquariumSdfLight[MaxSdfLightCount]; @@ -145,6 +149,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private readonly D3D12TemporalGaussianPacket[] temporalGaussians = new D3D12TemporalGaussianPacket[MaxTemporalGaussianCount]; private int temporalGaussianCount; private int gpuSensorCameraCount; + private int gpuSensorTextureCount; + private int gpuFusionSeedCount; private bool temporalGaussiansGpuGenerated; private Viewport viewport; private RawRect scissorRect; @@ -540,8 +546,8 @@ public sealed class D3D12Renderer : IAquariumRenderer new Vector4( temporalGaussianCount, gpuSensorCameraCount, - activeAccumulationWindow, - activePresentationDelay))); + gpuSensorTextureCount, + gpuFusionSeedCount))); frameResources.FrameConstantsDescriptor = frameResources.TransientShaderDescriptors.Allocate(); device.CreateConstantBufferView( new ConstantBufferViewDescription(frameConstants.GpuVirtualAddress, frameConstants.SizeInBytes), @@ -558,6 +564,8 @@ public sealed class D3D12Renderer : IAquariumRenderer sdfObjectBuffer.CreateShaderResourceView(device, frameResources.SdfObjectDescriptor); frameResources.GpuSensorCameraDescriptor = frameResources.TransientShaderDescriptors.Allocate(); gpuSensorCameraBuffer.CreateShaderResourceView(device, frameResources.GpuSensorCameraDescriptor); + frameResources.GpuSensorTextureDescriptor = frameResources.TransientShaderDescriptors.AllocateRange(MaxGpuSensorTextureCount); + CreateGpuSensorTextureViews(frame.Scene.GpuSensorFrame, frameResources.GpuSensorTextureDescriptor); frameResources.GpuFusionSeedDescriptor = frameResources.TransientShaderDescriptors.Allocate(); gpuFusionSeedBuffer.CreateShaderResourceView(device, frameResources.GpuFusionSeedDescriptor); frameResources.TemporalGaussianDescriptor = frameResources.TransientShaderDescriptors.Allocate(); @@ -779,6 +787,12 @@ public sealed class D3D12Renderer : IAquariumRenderer temporalGaussianBuffer.Dispose(); gpuFusionSeedBuffer.Dispose(); gpuSensorCameraBuffer.Dispose(); + foreach (var texture in externalSensorTextures.Values) + { + texture.Dispose(); + } + + externalSensorTextures.Clear(); sdfObjectBuffer.Dispose(); sdfLightBuffer.Dispose(); DisposeBloomRenderTargets(); @@ -1434,7 +1448,7 @@ public sealed class D3D12Renderer : IAquariumRenderer gpuSensorCameraBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, gpuSensorCameras.AsSpan(0, gpuSensorCameraCount)); if (temporalGaussiansGpuGenerated) { - gpuFusionSeedBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, gpuFusionSeeds.AsSpan(0, temporalGaussianCount)); + gpuFusionSeedBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, gpuFusionSeeds.AsSpan(0, gpuFusionSeedCount)); } else { @@ -1465,6 +1479,7 @@ public sealed class D3D12Renderer : IAquariumRenderer activeCommandList.SetComputeRootDescriptorTable(RootFusionFrameConstants, frameResources.FrameConstantsDescriptor.Gpu); activeCommandList.SetComputeRootDescriptorTable(RootFusionSeeds, frameResources.GpuFusionSeedDescriptor.Gpu); activeCommandList.SetComputeRootDescriptorTable(RootFusionSensorCameras, frameResources.GpuSensorCameraDescriptor.Gpu); + activeCommandList.SetComputeRootDescriptorTable(RootFusionSensorTextures, frameResources.GpuSensorTextureDescriptor.Gpu); activeCommandList.SetComputeRootDescriptorTable(RootFusionOutput, frameResources.TemporalGaussianUnorderedAccessDescriptor.Gpu); activeCommandList.Dispatch((uint)((temporalGaussianCount + 127) / 128), 1, 1); temporalGaussianBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); @@ -1475,6 +1490,65 @@ public sealed class D3D12Renderer : IAquariumRenderer } } + private void CreateGpuSensorTextureViews(AquariumGpuSensorFrame sensorFrame, D3D12DescriptorSlot firstDescriptor) + { + var activeHandles = new HashSet(StringComparer.Ordinal); + gpuSensorTextureCount = Math.Min(sensorFrame.ExternalTextures.Count, MaxGpuSensorTextureCount); + + for (var index = 0; index < MaxGpuSensorTextureCount; index++) + { + var descriptor = frames[frameIndex].TransientShaderDescriptors.Offset(firstDescriptor, index); + if (index >= gpuSensorTextureCount) + { + CreateNullSensorTextureView(descriptor); + continue; + } + + var texture = sensorFrame.ExternalTextures[index]; + var cacheKey = D3D12ExternalSensorTexture.CacheKey(texture); + activeHandles.Add(cacheKey); + if (!externalSensorTextures.TryGetValue(cacheKey, out var externalTexture) + || !externalTexture.Matches(texture)) + { + externalTexture?.Dispose(); + if (D3D12ExternalSensorTexture.TryOpen(device, texture, out var openedTexture)) + { + externalTexture = openedTexture; + externalSensorTextures[cacheKey] = externalTexture; + } + else + { + externalSensorTextures.Remove(cacheKey); + CreateNullSensorTextureView(descriptor); + continue; + } + } + + externalTexture.UpdateTimestamp(texture.TimestampNs); + externalTexture.CreateShaderResourceView(device, descriptor); + } + + foreach (var staleHandle in externalSensorTextures.Keys.Where(handle => !activeHandles.Contains(handle)).ToArray()) + { + externalSensorTextures[staleHandle].Dispose(); + externalSensorTextures.Remove(staleHandle); + } + } + + private void CreateNullSensorTextureView(D3D12DescriptorSlot descriptor) + { + device.CreateShaderResourceView( + null, + new ShaderResourceViewDescription + { + Format = Format.B8G8R8A8_UNorm, + ViewDimension = Vortice.Direct3D12.ShaderResourceViewDimension.Texture2D, + Shader4ComponentMapping = ShaderComponentMapping.Default, + Texture2D = new Texture2DShaderResourceView { MipLevels = 1 }, + }, + descriptor.Cpu); + } + private void RenderHeightField(ID3D12GraphicsCommandList activeCommandList, FrameResources frameResources) { var viewport = new Viewport(0.0f, 0.0f, HeightFieldTextureSize, HeightFieldTextureSize); @@ -1510,6 +1584,8 @@ public sealed class D3D12Renderer : IAquariumRenderer Array.Clear(sdfLights); temporalGaussianCount = 0; gpuSensorCameraCount = 0; + gpuSensorTextureCount = 0; + gpuFusionSeedCount = 0; temporalGaussiansGpuGenerated = false; heightFieldBrushConstants = D3D12HeightFieldBrushConstants.FromBrushes(scene.HeightFieldBrushes); @@ -1535,6 +1611,7 @@ public sealed class D3D12Renderer : IAquariumRenderer { var seedCount = Math.Min(scene.GpuFusionField.Seeds.Count, MaxTemporalGaussianCount); temporalGaussianCount = seedCount; + gpuFusionSeedCount = seedCount; temporalGaussiansGpuGenerated = true; for (var index = 0; index < seedCount; index++) { @@ -1543,6 +1620,14 @@ public sealed class D3D12Renderer : IAquariumRenderer return; } + gpuSensorTextureCount = Math.Min(scene.GpuSensorFrame.ExternalTextures.Count, MaxGpuSensorTextureCount); + if (gpuSensorTextureCount > 0 && gpuSensorCameraCount > 0) + { + temporalGaussianCount = Math.Min(MaxTemporalGaussianCount, gpuSensorTextureCount * GpuSensorSamplesPerTexture); + temporalGaussiansGpuGenerated = true; + return; + } + var gaussianCount = Math.Min(scene.TemporalGaussianField.Gaussians.Count, MaxTemporalGaussianCount); temporalGaussianCount = gaussianCount; for (var index = 0; index < gaussianCount; index++) @@ -2080,6 +2165,12 @@ public sealed class D3D12Renderer : IAquariumRenderer 27, 0, D3D12.DescriptorRangeOffsetAppend); + var sensorTextureRange = new DescriptorRange( + DescriptorRangeType.ShaderResourceView, + MaxGpuSensorTextureCount, + 28, + 0, + D3D12.DescriptorRangeOffsetAppend); var outputRange = new DescriptorRange( DescriptorRangeType.UnorderedAccessView, 1, @@ -2091,6 +2182,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(new RootDescriptorTable([frameRange]), ShaderVisibility.All), new RootParameter(new RootDescriptorTable([seedRange]), ShaderVisibility.All), new RootParameter(new RootDescriptorTable([sensorCameraRange]), ShaderVisibility.All), + new RootParameter(new RootDescriptorTable([sensorTextureRange]), ShaderVisibility.All), new RootParameter(new RootDescriptorTable([outputRange]), ShaderVisibility.All), }; var description = new RootSignatureDescription( @@ -2412,6 +2504,8 @@ public sealed class D3D12Renderer : IAquariumRenderer public D3D12DescriptorSlot GpuSensorCameraDescriptor { get; set; } + public D3D12DescriptorSlot GpuSensorTextureDescriptor { get; set; } + public D3D12DescriptorSlot GpuFusionSeedDescriptor { get; set; } public D3D12DescriptorSlot TemporalGaussianDescriptor { get; set; } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12LocalCastFusion.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12LocalCastFusion.hlsl index 5fb3fef..0af2beb 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12LocalCastFusion.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12LocalCastFusion.hlsl @@ -63,32 +63,113 @@ struct GpuSensorCamera StructuredBuffer fusionSeeds : register(t26); StructuredBuffer sensorCameras : register(t27); +Texture2D sensorTextures[8] : register(t28); RWStructuredBuffer temporalGaussiansOut : register(u0); +float Hash01(uint value) +{ + value ^= value >> 16; + value *= 2246822519u; + value ^= value >> 13; + value *= 3266489917u; + value ^= value >> 16; + return (float)(value & 0x00ffffffu) / 16777215.0; +} + +float3 TransformSensorPoint(GpuSensorCamera camera, float3 sensorPoint) +{ + float4 p = float4(sensorPoint, 1.0); + return float3( + dot(camera.worldFromSensor0, p), + dot(camera.worldFromSensor1, p), + dot(camera.worldFromSensor2, p)); +} + +float4 LoadSensorTexture(uint slot, int3 pixel) +{ + switch (slot) + { + case 0u: return sensorTextures[0].Load(pixel); + case 1u: return sensorTextures[1].Load(pixel); + case 2u: return sensorTextures[2].Load(pixel); + case 3u: return sensorTextures[3].Load(pixel); + case 4u: return sensorTextures[4].Load(pixel); + case 5u: return sensorTextures[5].Load(pixel); + case 6u: return sensorTextures[6].Load(pixel); + default: return sensorTextures[7].Load(pixel); + } +} + [numthreads(128, 1, 1)] void D3D12LocalCastFusionCS(uint3 dispatchThreadId : SV_DispatchThreadID) { uint index = dispatchThreadId.x; - uint seedCount = (uint)temporalGaussianInfo.x; - if (index >= seedCount) + uint outputCount = (uint)temporalGaussianInfo.x; + uint cameraCount = (uint)temporalGaussianInfo.y; + uint textureCount = (uint)temporalGaussianInfo.z; + uint seedCount = (uint)temporalGaussianInfo.w; + if (index >= outputCount) { return; } - GpuFusionSeed seed = fusionSeeds[index]; - uint cameraCount = (uint)temporalGaussianInfo.y; TemporalGaussian gaussian; - gaussian.centerHistoryWeight = seed.centerHistoryWeight; - gaussian.previousCenterFieldId = seed.previousCenterFieldId; - gaussian.velocityConfidence = seed.velocityConfidence; - gaussian.radiiFalloff = seed.radiiFalloff; - gaussian.orientation = float4(0.0, 0.0, 0.0, 1.0); - gaussian.colorOpacity = seed.colorOpacity; - gaussian.shapePad = seed.shapePad; - if (cameraCount > 0) + if (index < seedCount) { - GpuSensorCamera camera = sensorCameras[index % cameraCount]; - gaussian.shapePad.w = camera.textureRangeTime.z; + GpuFusionSeed seed = fusionSeeds[index]; + gaussian.centerHistoryWeight = seed.centerHistoryWeight; + gaussian.previousCenterFieldId = seed.previousCenterFieldId; + gaussian.velocityConfidence = seed.velocityConfidence; + gaussian.radiiFalloff = seed.radiiFalloff; + gaussian.orientation = float4(0.0, 0.0, 0.0, 1.0); + gaussian.colorOpacity = seed.colorOpacity; + gaussian.shapePad = seed.shapePad; + temporalGaussiansOut[index] = gaussian; + return; } + + if (textureCount == 0 || cameraCount == 0) + { + gaussian.centerHistoryWeight = float4(0.0, 0.0, 0.0, 0.0); + gaussian.previousCenterFieldId = float4(0.0, 0.0, 0.0, 0.0); + gaussian.velocityConfidence = float4(0.0, 0.0, 0.0, 0.0); + gaussian.radiiFalloff = float4(0.001, 0.001, 0.001, 4.0); + gaussian.orientation = float4(0.0, 0.0, 0.0, 1.0); + gaussian.colorOpacity = float4(0.0, 0.0, 0.0, 0.0); + gaussian.shapePad = float4(1.0, 0.0, 0.0, 0.0); + temporalGaussiansOut[index] = gaussian; + return; + } + + uint textureSampleIndex = index - seedCount; + uint textureSlot = min(textureCount - 1, textureSampleIndex / max(1u, ((outputCount - seedCount) / textureCount))); + GpuSensorCamera camera = sensorCameras[min(cameraCount - 1, textureSlot % cameraCount)]; + uint width = max(1u, (uint)camera.extentsKind.x); + uint height = max(1u, (uint)camera.extentsKind.y); + uint localIndex = textureSampleIndex + textureSlot * 747796405u; + uint x = (localIndex * 73u + (uint)(Hash01(localIndex) * 37.0)) % width; + uint y = (localIndex / width * 41u + (uint)(Hash01(localIndex ^ 0x9e3779b9u) * 29.0)) % height; + float4 rgba = LoadSensorTexture(textureSlot, int3((int)x, (int)y, 0)); + float luma = dot(rgba.rgb, float3(0.2126, 0.7152, 0.0722)); + float fx = max(1.0, camera.intrinsics.x); + float fy = max(1.0, camera.intrinsics.y); + float cx = camera.intrinsics.z; + float cy = camera.intrinsics.w; + float depth = 0.75 + luma * 2.25; + float3 sensorPoint = float3(((float)x - cx) / fx * depth, ((float)y - cy) / fy * depth, depth); + float3 center = TransformSensorPoint(camera, sensorPoint); + float targetPixels = lerp(1.75, 3.75, Hash01(localIndex ^ 0x85ebca6bu)); + float focalPixels = max(1.0, 0.5 * (fx + fy)); + float screenFitRadius = targetPixels * depth / focalPixels; + float radius = clamp(screenFitRadius, 0.0015, 0.018); + float confidence = saturate(0.25 + abs(luma - 0.5) * 1.5); + + gaussian.centerHistoryWeight = float4(center, confidence); + gaussian.previousCenterFieldId = float4(center, (float)(textureSlot + 1u)); + gaussian.velocityConfidence = float4(0.0, 0.0, 0.0, confidence); + gaussian.radiiFalloff = float4(radius * 1.65, radius, radius * 1.25, 5.8); + gaussian.orientation = float4(0.0, 0.0, 0.0, 1.0); + gaussian.colorOpacity = float4(rgba.rgb * 1.35, saturate(0.08 + confidence * 0.42)); + gaussian.shapePad = float4(1.85, (float)x / (float)width, (float)y / (float)height, camera.textureRangeTime.z); temporalGaussiansOut[index] = gaussian; } diff --git a/tests/Aquarium.Engine.Tests/GpuSensorFrameContractTests.cs b/tests/Aquarium.Engine.Tests/GpuSensorFrameContractTests.cs index dce7a17..969fff5 100644 --- a/tests/Aquarium.Engine.Tests/GpuSensorFrameContractTests.cs +++ b/tests/Aquarium.Engine.Tests/GpuSensorFrameContractTests.cs @@ -36,7 +36,8 @@ public sealed class GpuSensorFrameContractTests 1920, 1080, AquariumGpuSensorPixelFormat.Bgra8Unorm, - 10_000_000_000) + 10_000_000_000, + SharedHandleName: "LocalCastBridge/KiyoProLeft/Bgra") ], }; @@ -45,6 +46,7 @@ public sealed class GpuSensorFrameContractTests Assert.True(scene.GpuSensorFrame.HasInput); Assert.Equal("kiyo-pro-left", scene.GpuSensorFrame.Cameras[0].SensorId); Assert.Equal(AquariumGpuSensorPixelFormat.Bgra8Unorm, scene.GpuSensorFrame.ExternalTextures[0].PixelFormat); + Assert.Equal("LocalCastBridge/KiyoProLeft/Bgra", scene.GpuSensorFrame.ExternalTextures[0].SharedHandleName); Assert.Equal(18.0f, scene.GpuSensorFrame.AccumulationWindowSeconds, 6); } }