Skip to content

Sort light probes by solid angle (sphere) instead of volume [Hold] #46

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

Open
wants to merge 4 commits into
base: HDRP/staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Change default texture for detailmap to grey
- Optimize Shadow RT load on Tile based architecture platforms.
- Improved quality of SSAO.
- Changed the priority of the reflection probe from biggest to solid angle of spherical volume.

## [7.1.1] - 2019-09-05

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2241,9 +2241,9 @@ bool PrepareLightsForGPU(CommandBuffer cmd, HDCamera hdCamera, CullingResults cu
lightVolumeType = LightVolumeType.Sphere;
++envLightCount;

var logVolume = CalculateProbeLogVolume(probe.bounds);
var volumePriority = CalculateProbePriority(probe.reflectionProbe.transform.position, probe.bounds, camera);

m_SortKeys[sortCount++] = PackProbeKey(logVolume, lightVolumeType, 0u, probeIndex); // Sort by volume
m_SortKeys[sortCount++] = PackProbeKey(volumePriority, lightVolumeType, 0u, probeIndex); // Sort by volume
}
else
{
Expand All @@ -2269,9 +2269,9 @@ bool PrepareLightsForGPU(CommandBuffer cmd, HDCamera hdCamera, CullingResults cu
lightVolumeType = LightVolumeType.Sphere;
++envLightCount;

var logVolume = CalculateProbeLogVolume(probe.bounds);
var volumePriority = CalculateProbePriority(probe.transform.position, probe.bounds, camera);

m_SortKeys[sortCount++] = PackProbeKey(logVolume, lightVolumeType, 1u, planarProbeIndex); // Sort by volume
m_SortKeys[sortCount++] = PackProbeKey(volumePriority, lightVolumeType, 1u, planarProbeIndex); // Sort by volume
}
}

Expand Down Expand Up @@ -2401,15 +2401,25 @@ internal void UpdateEnvLighCameraRelativetData(ref EnvLightData envLightData, Ve
}
}

static float CalculateProbeLogVolume(Bounds bounds)
static uint CalculateProbePriority(Vector3 probePosition, Bounds bounds, Camera camera)
{
//Notes:
// - 1+ term is to prevent having negative values in the log result
// - 1000* is too keep 3 digit after the dot while we truncate the result later
// - 1048575 is 2^20-1 as we pack the result on 20bit later
float boxVolume = 8f* bounds.extents.x * bounds.extents.y * bounds.extents.z;
float logVolume = Mathf.Clamp(Mathf.Log(1 + boxVolume, 1.05f)*1000, 0, 1048575);
return logVolume;
float probeDistance = Vector3.Distance(probePosition, camera.transform.position);
// We take the diagonal of the bounds and create a sphere of this diameter.
float halfDiagonalBoundLength = Mathf.Sqrt(bounds.size.x * bounds.size.x + bounds.size.y * bounds.size.y + bounds.size.z * bounds.size.z) / 2;
// We do this to compute the spherical cap instead of the visibility of the bounds.
float distanceDelta = Mathf.Sqrt((probeDistance * probeDistance) - (halfDiagonalBoundLength * halfDiagonalBoundLength));
// Finally compute the solid angle of the sphere
float solidAngle = 2.0f * Mathf.PI * (1.0f - (distanceDelta / probeDistance));

// In case the camera is inside the volume, we put the maximum possible value + the radius of the sphere
if (halfDiagonalBoundLength > probeDistance)
solidAngle = 2.0f * Mathf.PI + halfDiagonalBoundLength;

// Encode the solide angle into 20 bit integer:
solidAngle = Mathf.Clamp(Mathf.Log(1 + solidAngle, 1.05f) * 1000, 0, 1048575);

// Invert the result because we want probe with bigger priority first
return 1048575 - (uint)solidAngle;
}

static void UnpackProbeSortKey(uint sortKey, out LightVolumeType lightVolumeType, out int probeIndex, out int listType)
Expand All @@ -2419,10 +2429,10 @@ static void UnpackProbeSortKey(uint sortKey, out LightVolumeType lightVolumeType
listType = (int)((sortKey >> 8) & 1);
}

static uint PackProbeKey(float logVolume, LightVolumeType lightVolumeType, uint listType, int probeIndex)
static uint PackProbeKey(uint priority, LightVolumeType lightVolumeType, uint listType, int probeIndex)
{
// 20 bit volume, 3 bit LightVolumeType, 1 bit list type, 8 bit index
return (uint)logVolume << 12 | (uint)lightVolumeType << 9 | listType << 8 | ((uint)probeIndex & 0xFF);
return priority << 12 | (uint)lightVolumeType << 9 | listType << 8 | ((uint)probeIndex & 0xFF);
}

struct BuildGPULightListParameters
Expand Down