Use Aetheria blue noise for Grid coverage

This commit is contained in:
Meta Vens 2026-05-08 21:50:01 +01:00
commit c534a4eaa7
9 changed files with 80 additions and 5 deletions

View file

@ -24,6 +24,7 @@
<ItemGroup>
<Content Include="Assets\Aquarium-Engine-Icon.ico" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Assets\Fonts\**\*.ttf" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Assets\Textures\**\*" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Render\Shaders\**\*.hlsl" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

Binary file not shown.

After

Width:  |  Height:  |  Size: 384 KiB

View file

@ -13,9 +13,11 @@ namespace Aquarium.Engine.Render;
public sealed class D3D11Renderer : IDisposable
{
private const string ShaderRelativePath = "Render/Shaders/Aquarium.hlsl";
private const string DitherTextureRelativePath = "Assets/Textures/Aetheria-LDR_LLL1_0.r8";
private static readonly TimeSpan ShaderReloadPollInterval = TimeSpan.FromMilliseconds(250);
private static readonly TimeSpan ShaderReloadWriteSettleTime = TimeSpan.FromMilliseconds(150);
private const int GridHeightTextureSize = 128;
private const int DitherTextureSize = 512;
private const int FroxelCountX = 8;
private const int FroxelCountY = 8;
private const int FroxelCountZ = 4;
@ -34,7 +36,10 @@ public sealed class D3D11Renderer : IDisposable
private readonly ID3D11Texture2D gridHeightTexture;
private readonly ID3D11RenderTargetView gridHeightRenderTargetView;
private readonly ID3D11ShaderResourceView gridHeightShaderResourceView;
private readonly ID3D11Texture2D ditherTexture;
private readonly ID3D11ShaderResourceView ditherShaderResourceView;
private readonly ID3D11SamplerState gridSampler;
private readonly ID3D11SamplerState ditherSampler;
private readonly ID3D11Buffer frameConstantBuffer;
private readonly ID3D11Buffer froxelPrimitiveBuffer;
private readonly ID3D11ShaderResourceView froxelPrimitiveShaderResourceView;
@ -102,6 +107,9 @@ public sealed class D3D11Renderer : IDisposable
gridHeightTexture = CreateGridHeightTexture();
gridHeightRenderTargetView = device.CreateRenderTargetView(gridHeightTexture);
gridHeightShaderResourceView = device.CreateShaderResourceView(gridHeightTexture);
startupProgress?.Invoke("Loading Aetheria blue-noise dither texture");
ditherTexture = CreateDitherTexture(Path.Combine(AppContext.BaseDirectory, DitherTextureRelativePath));
ditherShaderResourceView = device.CreateShaderResourceView(ditherTexture);
gridSampler = device.CreateSamplerState(new SamplerDescription
{
Filter = Filter.MinMagMipLinear,
@ -112,6 +120,16 @@ public sealed class D3D11Renderer : IDisposable
MinLOD = 0.0f,
MaxLOD = float.MaxValue,
});
ditherSampler = device.CreateSamplerState(new SamplerDescription
{
Filter = Filter.MinMagMipPoint,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
ComparisonFunc = ComparisonFunction.Never,
MinLOD = 0.0f,
MaxLOD = float.MaxValue,
});
frameConstantBuffer = device.CreateBuffer(
48,
BindFlags.ConstantBuffer,
@ -164,7 +182,10 @@ public sealed class D3D11Renderer : IDisposable
froxelPrimitiveShaderResourceView.Dispose();
froxelPrimitiveBuffer.Dispose();
frameConstantBuffer.Dispose();
ditherSampler.Dispose();
gridSampler.Dispose();
ditherShaderResourceView.Dispose();
ditherTexture.Dispose();
gridHeightShaderResourceView.Dispose();
gridHeightRenderTargetView.Dispose();
gridHeightTexture.Dispose();
@ -196,10 +217,32 @@ public sealed class D3D11Renderer : IDisposable
return device.CreateTexture2D(description);
}
private ID3D11Texture2D CreateDitherTexture(string path)
{
var pixels = File.ReadAllBytes(path);
if (pixels.Length != DitherTextureSize * DitherTextureSize)
{
throw new InvalidOperationException($"Dither texture {path} must be {DitherTextureSize}x{DitherTextureSize} R8 data.");
}
return device.CreateTexture2D(
pixels,
Format.R8_UNorm,
DitherTextureSize,
DitherTextureSize,
1,
1,
BindFlags.ShaderResource,
ResourceOptionFlags.None,
ResourceUsage.Default,
CpuAccessFlags.None);
}
private void RenderGridHeight()
{
context.PSUnsetShaderResource(0);
context.PSUnsetShaderResource(1);
context.PSUnsetShaderResource(2);
context.OMSetRenderTargets(gridHeightRenderTargetView);
context.RSSetViewport(0.0f, 0.0f, GridHeightTextureSize, GridHeightTextureSize, 0.0f, 1.0f);
context.ClearRenderTargetView(gridHeightRenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f));
@ -223,7 +266,9 @@ public sealed class D3D11Renderer : IDisposable
context.PSSetConstantBuffer(0, frameConstantBuffer);
context.PSSetShaderResource(0, gridHeightShaderResourceView);
context.PSSetShaderResource(1, froxelPrimitiveShaderResourceView);
context.PSSetShaderResource(2, ditherShaderResourceView);
context.PSSetSampler(0, gridSampler);
context.PSSetSampler(1, ditherSampler);
context.Draw(3, 0);
}

View file

@ -12,7 +12,9 @@ cbuffer AquariumFrame : register(b0)
Texture2D<float4> gridHeightTexture : register(t0);
StructuredBuffer<int4> froxelPrimitiveIds : register(t1);
Texture2D<float> ditherTexture : register(t2);
SamplerState gridSampler : register(s0);
SamplerState ditherSampler : register(s1);
static const float PI = 3.14159265359;
static const float SURFACE_EPSILON = 0.0015;
@ -101,6 +103,14 @@ float hash31(float3 p)
return frac((p.x + p.y) * p.z) * 2.0 - 1.0;
}
float stochasticTransparency(float2 screenUv, float alpha)
{
float frameSeed = floor(timeSeconds * 60.0);
float2 ditherUv = screenUv * (resolution / 512.0);
float dither = frac(ditherTexture.SampleLevel(ditherSampler, ditherUv, 0.0).r + frameSeed * 1.61803398875);
return alpha - dither - 0.001 * (1.0 - ceil(alpha));
}
float noised3(float3 x)
{
float3 p = floor(x);
@ -1178,20 +1188,25 @@ float4 AquariumPS(VertexOut input) : SV_Target
int materialId;
float travel;
float3 color = 0.0;
float nearestSolidTravel = farDistance + 1.0;
if (raymarchBodies(cameraPosition, rayDirection, hitPosition, materialId, travel))
{
float3 normal = surfaceNormal(hitPosition, materialId);
color = shade(hitPosition, normal, materialId, rayDirection);
color *= exp(-travel * 0.012);
nearestSolidTravel = travel;
}
float3 gridHitPosition;
float gridTravel;
if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel))
if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel) && gridTravel < nearestSolidTravel)
{
float4 gridOverlay = shadeGridOverlay(gridHitPosition);
color = lerp(color, gridOverlay.rgb, gridOverlay.a);
if (stochasticTransparency(screenUv, gridOverlay.a) > 0.0)
{
color = gridOverlay.rgb;
}
}
color += float3(0.001, 0.003, 0.004);