Skip to content

Fixed ambient probe convolution warning #6523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3d81248
Refactor SkyManager with render graph (WIP1)
JulienIgnace-Unity Oct 13, 2021
b1fb2a7
WIP2
JulienIgnace-Unity Oct 13, 2021
02dde63
WIP 3
JulienIgnace-Unity Oct 13, 2021
c78a480
WIP 4
JulienIgnace-Unity Oct 14, 2021
f0117ee
Fixed SkyRenderer.Update not having a proper command buffer
JulienIgnace-Unity Oct 14, 2021
63dd5a0
Removed hack to wait for ambient probe readback on first frame.
JulienIgnace-Unity Oct 14, 2021
745ffe2
Fixed RequiresPreRenderSky API to not use stale BuiltinParameters
JulienIgnace-Unity Oct 15, 2021
293600c
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Oct 15, 2021
da3c895
Fixed cubemap allocation for cloud ambient computation
JulienIgnace-Unity Oct 15, 2021
cee99d9
Volumetric Lighting now uses an ambient probe directly coming from GP…
JulienIgnace-Unity Oct 21, 2021
22a2cb6
Fixed cases with no sky and where ambient convolution should not outp…
JulienIgnace-Unity Oct 21, 2021
9787551
Ambient probe read by the APV is now directly a compute buffer instea…
JulienIgnace-Unity Oct 22, 2021
86db727
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Oct 22, 2021
f0e1478
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Nov 8, 2021
6dd6902
Remvoed useless comment and changed function signature based on revie…
JulienIgnace-Unity Nov 9, 2021
8dec347
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Nov 18, 2021
f91af06
Merge branch 'hd/gpu-ambient-probe' of https://github.com/Unity-Techn…
JulienIgnace-Unity Nov 22, 2021
b29beeb
Post merge fix
JulienIgnace-Unity Nov 22, 2021
6403b13
Refactor ambient probe convolutions with proper math and added a SH u…
JulienIgnace-Unity Nov 23, 2021
b55a256
Fixed SH inversion
JulienIgnace-Unity Nov 23, 2021
b47aa76
Moved some SH functions to utility file
JulienIgnace-Unity Nov 24, 2021
b37c0ff
Update SphericalHarmonics.hlsl
sebastienlagarde Nov 24, 2021
399f3a4
Update BuiltinGIUtilities.hlsl
sebastienlagarde Nov 24, 2021
3f9be80
Update AmbientProbeConvolution.compute
sebastienlagarde Nov 24, 2021
a693b9f
Update changelog
JulienIgnace-Unity Nov 24, 2021
ee4f615
Update AmbientProbeConvolution.compute
sebastienlagarde Nov 24, 2021
72eb1e1
Merge branch 'master' into hd/gpu-ambient-probe
JulienIgnace-Unity Nov 24, 2021
c38d2c6
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Nov 29, 2021
ea5ff0c
Merge branch 'hd/gpu-ambient-probe' of https://github.com/Unity-Techn…
JulienIgnace-Unity Dec 1, 2021
dd716f9
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Dec 2, 2021
dcac9ad
Apply formatting changes
Dec 2, 2021
a214dc5
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Dec 6, 2021
1ca76c3
Fixed null ref when clouds are enbaled
JulienIgnace-Unity Dec 6, 2021
e1366f2
Fixed shader compilation
JulienIgnace-Unity Dec 6, 2021
c46b6cf
Fixed warning in ambient probe convolution shader
JulienIgnace-Unity Dec 8, 2021
94d87ab
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Dec 8, 2021
1b37c11
Post merge fix
JulienIgnace-Unity Dec 8, 2021
d611830
Whitespace
JulienIgnace-Unity Dec 8, 2021
ef8c4e9
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Dec 10, 2021
58726ab
Merge branch 'master' of https://github.com/Unity-Technologies/Graphi…
JulienIgnace-Unity Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ RWStructuredBuffer<float> _AmbientProbeOutputBuffer;
RWStructuredBuffer<float4> _VolumetricAmbientProbeOutputBuffer;
RWStructuredBuffer<float4> _DiffuseAmbientProbeOutputBuffer;

// If we use local VGPRs as a scratch buffer we end up using too many register
// To avoid that we go through memory.
// This is quite messy and bad for performance but this shader should never be critical so it should be fine.
// Uint is used as it's the only format supported everywhere as read/write from same thread.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*on all platforms

Many platform do support typed loads on other formats

RWStructuredBuffer<uint> _ScratchBuffer;

uniform float4 _FogParameters;

#define _FogDimmer _FogParameters.x
Expand All @@ -40,6 +46,45 @@ uniform float4 _FogParameters;
groupshared float outputSHCoeffsLDS[SH_COEFF_COUNT * SAMPLE_COUNT / 2];
#endif

void PackSHFromScratchBuffer(RWStructuredBuffer<float4> buffer)
{
int c = 0;
for (c = 0; c < 3; c++)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra white line

{
buffer[c] = float4(asfloat(_ScratchBuffer[c * 9 + 3]), asfloat(_ScratchBuffer[c * 9 + 1]), asfloat(_ScratchBuffer[c * 9 + 2]), asfloat(_ScratchBuffer[c * 9 + 0]) - asfloat(_ScratchBuffer[c * 9 + 6]));
}

// Quadratic (4/5)
for (c = 0; c < 3; c++)
{
buffer[3 + c] = float4(asfloat(_ScratchBuffer[c * 9 + 4]), asfloat(_ScratchBuffer[c * 9 + 5]), asfloat(_ScratchBuffer[c * 9 + 6]) * 3.0f, asfloat(_ScratchBuffer[c * 9 + 7]));
}

// Quadratic (5)
buffer[6] = float4(asfloat(_ScratchBuffer[0 * 9 + 8]), asfloat(_ScratchBuffer[1 * 9 + 8]), asfloat(_ScratchBuffer[2 * 9 + 8]), 1.0f);
}

void ConvolveZonalFromScratchBuffer(float3 zh)
{
for (int l = 0; l <= 2; l++)
{
float n = sqrt((4.0f * PI) / (2 * l + 1));
float k = zh[l];
float p = n * k;

for (int m = -l; m <= l; m++)
{
int i = l * (l + 1) + m;

for (int c = 0; c < 3; c++)
{
_ScratchBuffer[c * 9 + i] = asuint(asfloat(_ScratchBuffer[c * 9 + i]) * p);
}
}
}
}

[numthreads(SAMPLE_COUNT, 1, 1)]
void KERNEL_NAME(uint dispatchThreadId : SV_DispatchThreadID)
{
Expand Down Expand Up @@ -173,16 +218,14 @@ void KERNEL_NAME(uint dispatchThreadId : SV_DispatchThreadID)
_AmbientProbeOutputBuffer[i] = outputSHCoeffs[i] * kClampedCosineCoefs[i % 9] * kSHBasisCoef[i % 9];
}

float result[27];

#if OUTPUT_DIFFUSE
for (i = 0; i < SH_COEFF_COUNT; ++i)
{
result[i] = _AmbientProbeOutputBuffer[i];
_ScratchBuffer[i] = asuint(_AmbientProbeOutputBuffer[i]);
}

// Diffuse convolution packed to be ready for shader consumption
PackSH(_DiffuseAmbientProbeOutputBuffer, result);
PackSHFromScratchBuffer(_DiffuseAmbientProbeOutputBuffer);
#endif

#if OUTPUT_VOLUMETRIC
Expand All @@ -191,24 +234,24 @@ void KERNEL_NAME(uint dispatchThreadId : SV_DispatchThreadID)
// Apply FogDimmer
for (i = 0; i < SH_COEFF_COUNT; ++i)
{
result[i] = outputSHCoeffs[i] *_FogDimmer;
_ScratchBuffer[i] = asuint(outputSHCoeffs[i] *_FogDimmer);
}

// Apply CornetteShank phase function
float3 zh;
GetCornetteShanksPhaseFunction(zh, _FogAnisotropy);
ConvolveZonal(result, zh);
ConvolveZonalFromScratchBuffer(zh);

// Premultiplies the SH with the polynomial coefficients of SH basis functions,
// which avoids using any constants during SH evaluation.
// The resulting evaluation takes the form:
// (c_0 - c_6) + c_1 y + c_2 z + c_3 x + c_4 x y + c_5 y z + c_6 (3 z^2) + c_7 x z + c_8 (x^2 - y^2)
for (i = 0; i < SH_COEFF_COUNT; ++i)
{
result[i] = result[i] * kSHBasisCoef[i % 9];
_ScratchBuffer[i] = asuint(asfloat(_ScratchBuffer[i]) * kSHBasisCoef[i % 9]);
}

PackSH(_VolumetricAmbientProbeOutputBuffer, result);
PackSHFromScratchBuffer(_VolumetricAmbientProbeOutputBuffer);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class SkyManager
static readonly int s_AmbientProbeOutputBufferParam = Shader.PropertyToID("_AmbientProbeOutputBuffer");
static readonly int s_VolumetricAmbientProbeOutputBufferParam = Shader.PropertyToID("_VolumetricAmbientProbeOutputBuffer");
static readonly int s_DiffuseAmbientProbeOutputBufferParam = Shader.PropertyToID("_DiffuseAmbientProbeOutputBuffer");
static readonly int s_ScratchBufferParam = Shader.PropertyToID("_ScratchBuffer");
static readonly int s_AmbientProbeInputCubemap = Shader.PropertyToID("_AmbientProbeInputCubemap");
static readonly int s_FogParameters = Shader.PropertyToID("_FogParameters");
int m_ComputeAmbientProbeKernel;
Expand Down Expand Up @@ -721,6 +722,7 @@ class UpdateAmbientProbePassData
public ComputeBuffer ambientProbeResult;
public ComputeBuffer diffuseAmbientProbeResult;
public ComputeBuffer volumetricAmbientProbeResult;
public ComputeBufferHandle scratchBuffer;
public Vector4 fogParameters;
public Action<AsyncGPUReadbackRequest> callback;
}
Expand All @@ -738,6 +740,7 @@ internal void UpdateAmbientProbe(RenderGraph renderGraph, TextureHandle skyCubem
passData.skyCubemap = builder.ReadTexture(skyCubemap);
passData.ambientProbeResult = ambientProbeResult;
passData.diffuseAmbientProbeResult = diffuseAmbientProbeResult;
passData.scratchBuffer = builder.CreateTransientComputeBuffer(new ComputeBufferDesc(27, sizeof(uint))); // L2 = 9 channel per component
passData.volumetricAmbientProbeResult = volumetricAmbientProbeResult;
passData.fogParameters = fog != null ? new Vector4(fog.globalLightProbeDimmer.value, fog.anisotropy.value, 0.0f, 0.0f) : Vector4.zero;
passData.callback = callback;
Expand All @@ -746,6 +749,7 @@ internal void UpdateAmbientProbe(RenderGraph renderGraph, TextureHandle skyCubem
(UpdateAmbientProbePassData data, RenderGraphContext ctx) =>
{
ctx.cmd.SetComputeBufferParam(data.computeAmbientProbeCS, data.computeAmbientProbeKernel, s_AmbientProbeOutputBufferParam, data.ambientProbeResult);
ctx.cmd.SetComputeBufferParam(data.computeAmbientProbeCS, data.computeAmbientProbeKernel, s_ScratchBufferParam, data.scratchBuffer);
ctx.cmd.SetComputeTextureParam(data.computeAmbientProbeCS, data.computeAmbientProbeKernel, s_AmbientProbeInputCubemap, data.skyCubemap);
if (data.diffuseAmbientProbeResult != null)
ctx.cmd.SetComputeBufferParam(data.computeAmbientProbeCS, data.computeAmbientProbeKernel, s_DiffuseAmbientProbeOutputBufferParam, data.diffuseAmbientProbeResult);
Expand Down