Add deferred Grid height pass
This commit is contained in:
parent
27ce883b74
commit
78790c2308
3 changed files with 123 additions and 28 deletions
|
|
@ -38,19 +38,20 @@ not directly ported yet. Those need engine systems around the shader rather than
|
|||
more one-pass pixel shader stuffing. The machete remains on the wall, tastefully
|
||||
lit.
|
||||
|
||||
## Missing Grid Pass
|
||||
## Deferred Grid Pass
|
||||
|
||||
The current Vortice renderer does **not** yet have the deferred gravity/Grid
|
||||
height render texture that Aetheria and the Bevy prototype both assume. The
|
||||
single HLSL pass still evaluates analytic Grid height directly while marching.
|
||||
The renderer now has a deferred Grid height target.
|
||||
|
||||
That is temporary. The real Grid path should be:
|
||||
Current frame order:
|
||||
|
||||
1. Render gravity/body sources into a Grid-space height target.
|
||||
2. Sample that height target in the raymarch pass.
|
||||
3. Build terrain normals from adjacent Grid texels in world space.
|
||||
4. Keep the Grid target centered on the camera target while body anchors stay in
|
||||
1. `GridHeightPS` renders body/gravity height into a 128x128 Grid-space
|
||||
`R32G32B32A32_Float` render target.
|
||||
2. `AquariumPS` raymarches solids and terrain against that height texture.
|
||||
3. Terrain normals sample adjacent Grid texels in world space instead of
|
||||
re-evaluating analytic height per normal tap.
|
||||
4. The Grid target is centered on the camera target; body anchors remain in
|
||||
world space.
|
||||
|
||||
Until that pass exists, the shader approximates the same normal construction by
|
||||
sampling `terrainHeight` at adjacent world-space Grid texel offsets.
|
||||
The target currently stores height in `.r` only. The next useful expansion is to
|
||||
store packed gradients in `.g/.b` during the Grid pass so the raymarcher can use
|
||||
one texture fetch for height and a cheaper normal path when fidelity allows it.
|
||||
|
|
|
|||
|
|
@ -10,12 +10,18 @@ namespace Aquarium.Engine.Render;
|
|||
public sealed class D3D11Renderer : IDisposable
|
||||
{
|
||||
private const string ShaderRelativePath = "Render/Shaders/Aquarium.hlsl";
|
||||
private const int GridHeightTextureSize = 128;
|
||||
|
||||
private readonly IDXGISwapChain swapChain;
|
||||
private readonly ID3D11Device device;
|
||||
private readonly ID3D11DeviceContext context;
|
||||
private readonly ID3D11RenderTargetView renderTargetView;
|
||||
private readonly ID3D11Texture2D gridHeightTexture;
|
||||
private readonly ID3D11RenderTargetView gridHeightRenderTargetView;
|
||||
private readonly ID3D11ShaderResourceView gridHeightShaderResourceView;
|
||||
private readonly ID3D11SamplerState gridSampler;
|
||||
private readonly ID3D11VertexShader vertexShader;
|
||||
private readonly ID3D11PixelShader gridHeightPixelShader;
|
||||
private readonly ID3D11PixelShader pixelShader;
|
||||
private readonly ID3D11Buffer frameConstantBuffer;
|
||||
private readonly int width;
|
||||
|
|
@ -60,10 +66,25 @@ public sealed class D3D11Renderer : IDisposable
|
|||
|
||||
var shaderPath = Path.Combine(AppContext.BaseDirectory, ShaderRelativePath);
|
||||
var vertexShaderBytecode = CompileShader(shaderPath, "FullscreenTriangleVS", "vs_5_0");
|
||||
var gridHeightShaderBytecode = CompileShader(shaderPath, "GridHeightPS", "ps_5_0");
|
||||
var pixelShaderBytecode = CompileShader(shaderPath, "AquariumPS", "ps_5_0");
|
||||
|
||||
vertexShader = device.CreateVertexShader(vertexShaderBytecode.Span, null);
|
||||
gridHeightPixelShader = device.CreatePixelShader(gridHeightShaderBytecode.Span, null);
|
||||
pixelShader = device.CreatePixelShader(pixelShaderBytecode.Span, null);
|
||||
gridHeightTexture = CreateGridHeightTexture();
|
||||
gridHeightRenderTargetView = device.CreateRenderTargetView(gridHeightTexture);
|
||||
gridHeightShaderResourceView = device.CreateShaderResourceView(gridHeightTexture);
|
||||
gridSampler = device.CreateSamplerState(new SamplerDescription
|
||||
{
|
||||
Filter = Filter.MinMagMipLinear,
|
||||
AddressU = TextureAddressMode.Clamp,
|
||||
AddressV = TextureAddressMode.Clamp,
|
||||
AddressW = TextureAddressMode.Clamp,
|
||||
ComparisonFunc = ComparisonFunction.Never,
|
||||
MinLOD = 0.0f,
|
||||
MaxLOD = float.MaxValue,
|
||||
});
|
||||
frameConstantBuffer = device.CreateBuffer(
|
||||
48,
|
||||
BindFlags.ConstantBuffer,
|
||||
|
|
@ -84,23 +105,21 @@ public sealed class D3D11Renderer : IDisposable
|
|||
(float2)frame.Grid.Center,
|
||||
new float2(0.0f, 0.0f));
|
||||
|
||||
context.OMSetRenderTargets(renderTargetView);
|
||||
context.RSSetViewport(0.0f, 0.0f, width, height, 0.0f, 1.0f);
|
||||
context.ClearRenderTargetView(renderTargetView, new Color4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
context.UpdateSubresource(in constants, frameConstantBuffer);
|
||||
context.IASetInputLayout(null);
|
||||
context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);
|
||||
context.VSSetShader(vertexShader);
|
||||
context.PSSetShader(pixelShader);
|
||||
context.PSSetConstantBuffer(0, frameConstantBuffer);
|
||||
context.Draw(3, 0);
|
||||
RenderGridHeight();
|
||||
RenderAquarium();
|
||||
swapChain.Present(1, PresentFlags.None);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
frameConstantBuffer.Dispose();
|
||||
gridSampler.Dispose();
|
||||
gridHeightShaderResourceView.Dispose();
|
||||
gridHeightRenderTargetView.Dispose();
|
||||
gridHeightTexture.Dispose();
|
||||
pixelShader.Dispose();
|
||||
gridHeightPixelShader.Dispose();
|
||||
vertexShader.Dispose();
|
||||
renderTargetView.Dispose();
|
||||
context.Dispose();
|
||||
|
|
@ -108,6 +127,54 @@ public sealed class D3D11Renderer : IDisposable
|
|||
swapChain.Dispose();
|
||||
}
|
||||
|
||||
private ID3D11Texture2D CreateGridHeightTexture()
|
||||
{
|
||||
var description = new Texture2DDescription
|
||||
{
|
||||
Width = GridHeightTextureSize,
|
||||
Height = GridHeightTextureSize,
|
||||
MipLevels = 1,
|
||||
ArraySize = 1,
|
||||
Format = Format.R32G32B32A32_Float,
|
||||
SampleDescription = new SampleDescription(1, 0),
|
||||
Usage = ResourceUsage.Default,
|
||||
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
|
||||
CPUAccessFlags = CpuAccessFlags.None,
|
||||
MiscFlags = ResourceOptionFlags.None,
|
||||
};
|
||||
|
||||
return device.CreateTexture2D(description);
|
||||
}
|
||||
|
||||
private void RenderGridHeight()
|
||||
{
|
||||
context.PSUnsetShaderResource(0);
|
||||
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));
|
||||
context.IASetInputLayout(null);
|
||||
context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);
|
||||
context.VSSetShader(vertexShader);
|
||||
context.PSSetShader(gridHeightPixelShader);
|
||||
context.PSSetConstantBuffer(0, frameConstantBuffer);
|
||||
context.Draw(3, 0);
|
||||
}
|
||||
|
||||
private void RenderAquarium()
|
||||
{
|
||||
context.OMSetRenderTargets(renderTargetView);
|
||||
context.RSSetViewport(0.0f, 0.0f, width, height, 0.0f, 1.0f);
|
||||
context.ClearRenderTargetView(renderTargetView, new Color4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
context.IASetInputLayout(null);
|
||||
context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);
|
||||
context.VSSetShader(vertexShader);
|
||||
context.PSSetShader(pixelShader);
|
||||
context.PSSetConstantBuffer(0, frameConstantBuffer);
|
||||
context.PSSetShaderResource(0, gridHeightShaderResourceView);
|
||||
context.PSSetSampler(0, gridSampler);
|
||||
context.Draw(3, 0);
|
||||
}
|
||||
|
||||
private static ReadOnlyMemory<byte> CompileShader(string path, string entryPoint, string profile)
|
||||
{
|
||||
var shaderFlags = ShaderFlags.EnableStrictness;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ cbuffer AquariumFrame : register(b0)
|
|||
float2 _pad1;
|
||||
};
|
||||
|
||||
Texture2D<float4> gridHeightTexture : register(t0);
|
||||
SamplerState gridSampler : register(s0);
|
||||
|
||||
static const float PI = 3.14159265359;
|
||||
static const float FAR_DISTANCE = 90.0;
|
||||
static const float SURFACE_EPSILON = 0.0015;
|
||||
|
|
@ -194,7 +197,17 @@ float3 planetCenter(int index)
|
|||
return float3(xy, 1.15 + planetRadius(index) * 0.72);
|
||||
}
|
||||
|
||||
float terrainHeight(float2 p)
|
||||
float2 gridUv(float2 p)
|
||||
{
|
||||
return gridLocal(p) * 0.5 + 0.5;
|
||||
}
|
||||
|
||||
float2 gridWorld(float2 uv)
|
||||
{
|
||||
return gridCenter + (uv * 2.0 - 1.0) * gridRadius;
|
||||
}
|
||||
|
||||
float analyticTerrainHeight(float2 p)
|
||||
{
|
||||
float positive = 0.0;
|
||||
float negative = 0.0;
|
||||
|
|
@ -223,6 +236,12 @@ float terrainHeight(float2 p)
|
|||
return positive - negative;
|
||||
}
|
||||
|
||||
float terrainHeight(float2 p)
|
||||
{
|
||||
float2 uv = saturate(gridUv(p));
|
||||
return gridHeightTexture.SampleLevel(gridSampler, uv, 0.0).r;
|
||||
}
|
||||
|
||||
float sphereSdf(float3 p, float3 center, float radius)
|
||||
{
|
||||
return length(p - center) - radius;
|
||||
|
|
@ -291,14 +310,14 @@ float sceneDistance(float3 p)
|
|||
|
||||
float3 terrainNormal(float3 p)
|
||||
{
|
||||
float texelWorld = max((gridRadius * 2.0) / GRID_HEIGHT_TEXEL_COUNT, 0.01);
|
||||
float2 dx = float2(texelWorld, 0.0);
|
||||
float2 dy = float2(0.0, texelWorld);
|
||||
float2 uv = saturate(gridUv(p.xy));
|
||||
float2 texel = 1.0 / GRID_HEIGHT_TEXEL_COUNT;
|
||||
float texelWorld = max((gridRadius * 2.0) / GRID_HEIGHT_TEXEL_COUNT, 0.001);
|
||||
|
||||
float hLeft = terrainHeight(p.xy - dx);
|
||||
float hRight = terrainHeight(p.xy + dx);
|
||||
float hDown = terrainHeight(p.xy - dy);
|
||||
float hUp = terrainHeight(p.xy + dy);
|
||||
float hLeft = gridHeightTexture.SampleLevel(gridSampler, uv - float2(texel.x, 0.0), 0.0).r;
|
||||
float hRight = gridHeightTexture.SampleLevel(gridSampler, uv + float2(texel.x, 0.0), 0.0).r;
|
||||
float hDown = gridHeightTexture.SampleLevel(gridSampler, uv - float2(0.0, texel.y), 0.0).r;
|
||||
float hUp = gridHeightTexture.SampleLevel(gridSampler, uv + float2(0.0, texel.y), 0.0).r;
|
||||
|
||||
float3 tangentX = float3(texelWorld * 2.0, 0.0, hRight - hLeft);
|
||||
float3 tangentY = float3(0.0, texelWorld * 2.0, hUp - hDown);
|
||||
|
|
@ -479,3 +498,11 @@ float4 AquariumPS(VertexOut input) : SV_Target
|
|||
color += float3(0.001, 0.003, 0.004);
|
||||
return float4(aces(color), 1.0);
|
||||
}
|
||||
|
||||
float4 GridHeightPS(VertexOut input) : SV_Target
|
||||
{
|
||||
float2 uv = saturate(input.uv);
|
||||
float2 world = gridWorld(uv);
|
||||
float height = analyticTerrainHeight(world);
|
||||
return float4(height, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue