Browse Source

Added RT Handle scale to deal with unusually sized render textures in HDRP, and reorganized callbacks to avoid circular references between Unity bindings and constant buffer bindings.

master
Nico de Poel 2 years ago
parent
commit
19783ed2c1
  1. 2
      com.unity.render-pipelines.high-definition/Runtime/FidelityFX/CACAO/Cacao.cs
  2. 9
      com.unity.render-pipelines.high-definition/Runtime/FidelityFX/CACAO/CacaoContext.cs
  3. 5
      com.unity.render-pipelines.high-definition/Runtime/FidelityFX/HDRenderPipeline.FidelityFX.Cacao.cs
  4. 18
      com.unity.render-pipelines.high-definition/Runtime/FidelityFX/ffx_cacao_common_hdrp.cginc
  5. 77
      com.unity.render-pipelines.high-definition/Runtime/FidelityFX/shaders/cacao/ffx_cacao_callbacks_hlsl.h
  6. 1
      com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/HDRenderPipeline.AmbientOcclusion.cs

2
com.unity.render-pipelines.high-definition/Runtime/FidelityFX/CACAO/Cacao.cs

@ -228,6 +228,8 @@ namespace FidelityFX
public Vector2 DeinterleavedDepthBufferNormalisedOffset;
public Matrix4x4 NormalsWorldToViewspaceMatrix;
public Vector4 RTHandleScale;
}
[Serializable, StructLayout(LayoutKind.Sequential)]

9
com.unity.render-pipelines.high-definition/Runtime/FidelityFX/CACAO/CacaoContext.cs

@ -106,10 +106,10 @@ namespace FidelityFX
private static readonly ProfilerMarker BilateralUpsampleMarker = new ProfilerMarker("Bilateral Upsample");
private static readonly ProfilerMarker ReinterleaveMarker = new ProfilerMarker("Reinterleave");
public void Draw(CommandBuffer commandBuffer, in Cacao.DispatchInfo dispatchInfo, in Matrix4x4 proj, in Matrix4x4 normalsToView)
public void Draw(CommandBuffer commandBuffer, in Cacao.DispatchInfo dispatchInfo, in Matrix4x4 proj, in Matrix4x4 normalsToView, in Vector4 rtHandleScale)
{
// Update constant buffers
UpdateConstants(ref Constants, _settings, _bufferSizeInfo, proj, normalsToView);
UpdateConstants(ref Constants, _settings, _bufferSizeInfo, proj, normalsToView, rtHandleScale);
commandBuffer.SetBufferData(_constantsBuffer, _constants);
// Clear load counter
@ -241,7 +241,8 @@ namespace FidelityFX
/// <param name="bufferSizeInfo">Cacao.BufferSizeInfo buffer size info.</param>
/// <param name="proj">Projection matrix for the frame.</param>
/// <param name="normalsToView">Normals world space to view space matrix for the frame.</param>
private static void UpdateConstants(ref Cacao.Constants consts, in Cacao.Settings settings, in Cacao.BufferSizeInfo bufferSizeInfo, in Matrix4x4 proj, in Matrix4x4 normalsToView)
/// <param name="rtHandleScale">Scale used for sampling input buffers.</param>
private static void UpdateConstants(ref Cacao.Constants consts, in Cacao.Settings settings, in Cacao.BufferSizeInfo bufferSizeInfo, in Matrix4x4 proj, in Matrix4x4 normalsToView, in Vector4 rtHandleScale)
{
consts.BilateralSigmaSquared = settings.bilateralSigmaSquared;
consts.BilateralSimilarityDistanceSigma = settings.bilateralSimilarityDistanceSigma;
@ -255,6 +256,8 @@ namespace FidelityFX
consts.NormalsWorldToViewspaceMatrix = normalsToView;
}
consts.RTHandleScale = rtHandleScale;
// used to get average load per pixel; 9.0 is there to compensate for only doing every 9th InterlockedAdd in PSPostprocessImportanceMapB for performance reasons
consts.LoadCounterAvgDiv = 9.0f / (bufferSizeInfo.ImportanceMapWidth * bufferSizeInfo.ImportanceMapHeight * 255);

5
com.unity.render-pipelines.high-definition/Runtime/FidelityFX/HDRenderPipeline.FidelityFX.Cacao.cs

@ -21,6 +21,7 @@ namespace UnityEngine.Rendering.HighDefinition
public Matrix4x4 projectionMatrix;
public Matrix4x4 cameraToWorldMatrix;
public Vector4 rtHandleScale;
}
class RenderCacaoPassData
@ -30,6 +31,7 @@ namespace UnityEngine.Rendering.HighDefinition
public Matrix4x4 projectionMatrix;
public Matrix4x4 normalsToView;
public Vector4 rtHandleScale;
public TextureHandle aoOutput;
}
@ -82,6 +84,7 @@ namespace UnityEngine.Rendering.HighDefinition
passData.normalBuffer = normalBuffer;
passData.projectionMatrix = parameters.projectionMatrix;
passData.normalsToView = parameters.cameraToWorldMatrix * Cacao.UnityToCacaoViewMatrix;
passData.rtHandleScale = parameters.rtHandleScale;
builder.SetRenderFunc((RenderCacaoPassData data, RenderGraphContext ctx) =>
{
@ -96,7 +99,7 @@ namespace UnityEngine.Rendering.HighDefinition
ctx.cmd.SetGlobalTexture("_NormalBufferTexture", data.normalBuffer);
ctx.cmd.SetGlobalTexture("_OcclusionTexture", data.aoOutput);
m_CacaoContext.Draw(ctx.cmd, dispatchInfo, data.projectionMatrix, data.normalsToView);
m_CacaoContext.Draw(ctx.cmd, dispatchInfo, data.projectionMatrix, data.normalsToView, data.rtHandleScale);
});
return passData.aoOutput;

18
com.unity.render-pipelines.high-definition/Runtime/FidelityFX/ffx_cacao_common_hdrp.cginc

@ -14,7 +14,7 @@
#define COORD_TEXTURE2D_X_LOD(coord, lod) uint3(coord, lod)
#endif
float4 FFX_CACAO_Prepare_SampleDepthOffsets(float2 uv)
float4 FFX_CACAO_Prepare_SampleDepthOffsets_Unity(float2 uv)
{
float4 samples;
samples.x = _CameraDepthTexture.SampleLevel(s_point_clamp_sampler, UV_TEXTURE2D_X(uv), 0.0f, int2(0, 2)).r;
@ -24,27 +24,27 @@ float4 FFX_CACAO_Prepare_SampleDepthOffsets(float2 uv)
return samples;
}
float4 FFX_CACAO_Prepare_GatherDepth(float2 uv)
float4 FFX_CACAO_Prepare_GatherDepth_Unity(float2 uv)
{
return _CameraDepthTexture.GatherRed(s_point_clamp_sampler, UV_TEXTURE2D_X(uv));
}
float FFX_CACAO_Prepare_LoadDepth(int2 coord)
float FFX_CACAO_Prepare_LoadDepth_Unity(int2 coord)
{
return _CameraDepthTexture.Load(COORD_TEXTURE2D_X_LOD(coord, 0)).r;
}
float FFX_CACAO_Prepare_LoadDepthOffset(int2 coord, int2 offset)
float FFX_CACAO_Prepare_LoadDepthOffset_Unity(int2 coord, int2 offset)
{
return _CameraDepthTexture.Load(COORD_TEXTURE2D_X_LOD(coord, 0), offset).r;
}
float4 FFX_CACAO_Prepare_GatherDepthOffset(float2 uv, int2 offset)
float4 FFX_CACAO_Prepare_GatherDepthOffset_Unity(float2 uv, int2 offset)
{
return _CameraDepthTexture.GatherRed(s_point_clamp_sampler, UV_TEXTURE2D_X(uv), offset);
}
float4 FFX_CACAO_BilateralUpscale_LoadDepths(int2 coord)
float4 FFX_CACAO_BilateralUpscale_LoadDepths_Unity(int2 coord)
{
float4 depths;
depths.x = _CameraDepthTexture.Load(COORD_TEXTURE2D_X_LOD(coord, 0), int2(0, 0)).r;
@ -54,7 +54,7 @@ float4 FFX_CACAO_BilateralUpscale_LoadDepths(int2 coord)
return depths;
}
float3 LoadSceneNormals(int2 coord)
float3 LoadSceneNormals_Unity(int2 coord)
{
NormalData normalData;
DecodeFromNormalBuffer(coord, normalData);
@ -63,12 +63,12 @@ float3 LoadSceneNormals(int2 coord)
RW_TEXTURE2D_X(float, _OcclusionTexture);
void FFX_CACAO_Apply_StoreOutput(uint2 coord, float val)
void FFX_CACAO_Apply_StoreOutput_Unity(uint2 coord, float val)
{
_OcclusionTexture[COORD_TEXTURE2D_X(coord)] = 1.0 - val;
}
void FFX_CACAO_BilateralUpscale_StoreOutput(uint2 coord, int2 offset, float val)
void FFX_CACAO_BilateralUpscale_StoreOutput_Unity(uint2 coord, int2 offset, float val)
{
_OcclusionTexture[COORD_TEXTURE2D_X(coord + offset)] = 1.0 - val;
}

77
com.unity.render-pipelines.high-definition/Runtime/FidelityFX/shaders/cacao/ffx_cacao_callbacks_hlsl.h

@ -112,6 +112,8 @@
FfxFloat32x2 g_FFX_CACAO_DeinterleavedDepthBufferNormalisedOffset;
float4x4 g_FFX_CACAO_NormalsWorldToViewspaceMatrix;
FfxFloat32x4 g_FFX_CACAO_RTHandleScale;
};
#define FFX_CACAO_CONSTANT_BUFFER_1_SIZE 172 // Number of 32-bit values. This must be kept in sync with max( cbRCAS , cbSPD) size.
@ -428,6 +430,15 @@ float4x4 NormalsWorldToViewspaceMatrix(){
#endif
}
float2 ClampAndScaleUV(float2 UV)
{
#if defined(CACAO_BIND_CB_CACAO)
return min(UV, 1.0f) * g_FFX_CACAO_RTHandleScale.xy;
#else
return UV;
#endif
}
#if defined(FFX_GPU)
#define FFX_CACAO_ROOTSIG_STRINGIFY(p) FFX_CACAO_ROOTSIG_STR(p)
#define FFX_CACAO_ROOTSIG_STR(p) #p
@ -645,18 +656,20 @@ FfxFloat32x2 FFX_CACAO_Apply_LoadSSAOPass(FfxUInt32x2 coord, FfxUInt32 passnum)
#endif
}
// void FFX_CACAO_Apply_StoreOutput(FfxUInt32x2 coord, FfxFloat32 val)
// {
void FFX_CACAO_Apply_StoreOutput(FfxUInt32x2 coord, FfxFloat32 val)
{
FFX_CACAO_Apply_StoreOutput_Unity(coord, val);
// #if defined CACAO_BIND_UAV_OUTPUT
// g_RwOutput[coord] = val;
// #endif
// }
}
// =============================================================================
// Prepare
// FfxFloat32x4 FFX_CACAO_Prepare_SampleDepthOffsets(FfxFloat32x2 uv)
// {
FfxFloat32x4 FFX_CACAO_Prepare_SampleDepthOffsets(FfxFloat32x2 uv)
{
return FFX_CACAO_Prepare_SampleDepthOffsets_Unity(ClampAndScaleUV(uv));
// #if defined CACAO_BIND_SRV_DEPTH_IN
// FfxFloat32x4 samples;
// samples.x = g_DepthIn.SampleLevel(g_PointClampSampler, uv, 0.0f, FfxInt32x2(0, 2));
@ -667,49 +680,53 @@ FfxFloat32x2 FFX_CACAO_Apply_LoadSSAOPass(FfxUInt32x2 coord, FfxUInt32 passnum)
// #else
// return FfxFloat32x4(0, 0, 0, 0);
// #endif
// }
//
// FfxFloat32x4 FFX_CACAO_Prepare_GatherDepth(FfxFloat32x2 uv)
// {
}
FfxFloat32x4 FFX_CACAO_Prepare_GatherDepth(FfxFloat32x2 uv)
{
return FFX_CACAO_Prepare_GatherDepth_Unity(uv);
// #if defined CACAO_BIND_SRV_DEPTH_IN
// return g_DepthIn.GatherRed(g_PointClampSampler, uv);
// #else
// return FfxFloat32x4(0, 0, 0, 0);
// #endif
// }
//
// FfxFloat32 FFX_CACAO_Prepare_LoadDepth(FfxUInt32x2 coord)
// {
}
FfxFloat32 FFX_CACAO_Prepare_LoadDepth(FfxUInt32x2 coord)
{
return FFX_CACAO_Prepare_LoadDepth_Unity(coord);
// #if defined CACAO_BIND_SRV_DEPTH_IN
// return g_DepthIn.Load(FfxInt32x3(coord, 0));
// #else
// return 0;
// #endif
// }
//
// FfxFloat32 FFX_CACAO_Prepare_LoadDepthOffset(FfxUInt32x2 coord, FfxInt32x2 offset)
// {
}
FfxFloat32 FFX_CACAO_Prepare_LoadDepthOffset(FfxUInt32x2 coord, FfxInt32x2 offset)
{
return FFX_CACAO_Prepare_LoadDepthOffset_Unity(coord, offset);
// #if defined CACAO_BIND_SRV_DEPTH_IN
// return g_DepthIn.Load(FfxInt32x3(coord, 0), offset);
// #else
// return 0;
// #endif
// }
//
// FfxFloat32x4 FFX_CACAO_Prepare_GatherDepthOffset(FfxFloat32x2 uv, FfxInt32x2 offset)
// {
}
FfxFloat32x4 FFX_CACAO_Prepare_GatherDepthOffset(FfxFloat32x2 uv, FfxInt32x2 offset)
{
return FFX_CACAO_Prepare_GatherDepthOffset_Unity(ClampAndScaleUV(uv), offset);
// #if defined CACAO_BIND_SRV_DEPTH_IN
// return g_DepthIn.GatherRed(g_PointClampSampler, uv, offset);
// #else
// return FfxFloat32x4(0, 0, 0, 0);
// #endif
// }
}
FfxFloat32x3 FFX_CACAO_Prepare_LoadNormal(FfxUInt32x2 coord)
{
#if defined CACAO_BIND_SRV_NORMAL_IN
//FfxFloat32x3 normal = g_NormalIn.Load(FfxInt32x3(coord, 0)).xyz;
FfxFloat32x3 normal = LoadSceneNormals(coord);
FfxFloat32x3 normal = LoadSceneNormals_Unity(coord);
//normal = normal * NormalsUnpackMul().xxx + NormalsUnpackAdd().xxx;
normal = mul(normal, (float3x3)NormalsWorldToViewspaceMatrix()).xyz;
// normal = normalize(normal);
@ -824,12 +841,13 @@ void FFX_CACAO_Importance_LoadCounterInterlockedAdd(FfxUInt32 val)
// These resources ping/pong which is handled by schedule dispatch
// void FFX_CACAO_BilateralUpscale_StoreOutput(FfxUInt32x2 coord, FfxInt32x2 offset, FfxFloat32 val)
// {
void FFX_CACAO_BilateralUpscale_StoreOutput(FfxUInt32x2 coord, FfxInt32x2 offset, FfxFloat32 val)
{
FFX_CACAO_BilateralUpscale_StoreOutput_Unity(coord, offset, val);
// #if defined CACAO_BIND_UAV_OUTPUT
// g_RwOutput[coord + offset] = val;
// #endif
// }
}
FfxFloat32 FFX_CACAO_BilateralUpscale_SampleSSAOLinear(FfxFloat32x2 uv, FfxUInt32 index)
{
@ -858,8 +876,9 @@ FfxFloat32x2 FFX_CACAO_BilateralUpscale_LoadSSAO(FfxUInt32x2 coord, FfxUInt32 in
#endif
}
// FfxFloat32x4 FFX_CACAO_BilateralUpscale_LoadDepths(FfxUInt32x2 coord)
// {
FfxFloat32x4 FFX_CACAO_BilateralUpscale_LoadDepths(FfxUInt32x2 coord)
{
return FFX_CACAO_BilateralUpscale_LoadDepths_Unity(coord);
// FfxFloat32x4 depths;
// #if defined CACAO_BIND_SRV_DEPTH_IN
// depths.x = g_DepthIn.Load(FfxInt32x3(coord, 0), FfxInt32x2(0, 0));
@ -870,7 +889,7 @@ FfxFloat32x2 FFX_CACAO_BilateralUpscale_LoadSSAO(FfxUInt32x2 coord, FfxUInt32 in
// #else
// return FfxFloat32x4(0, 0, 0, 0);
// #endif
// }
}
FfxFloat32 FFX_CACAO_BilateralUpscale_LoadDownscaledDepth(FfxUInt32x2 coord, FfxUInt32 index)
{

1
com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/HDRenderPipeline.AmbientOcclusion.cs

@ -164,6 +164,7 @@ namespace UnityEngine.Rendering.HighDefinition
downsample = !settings.fullResolution,
projectionMatrix = hdCamera.camera.projectionMatrix,
cameraToWorldMatrix = hdCamera.camera.cameraToWorldMatrix,
rtHandleScale = hdCamera.historyRTHandleProperties.rtHandleScale,
};
cacaoParameters.settings.radius = settings.radius.value;

Loading…
Cancel
Save