Skip to content

Improve light AABB generation #2 #1906

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 26 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define HALF_MIN_SQRT 0.0078125 // 2^-7 == sqrt(HALF_MIN), useful for ensuring HALF_MIN after x^2
#define HALF_MAX 65504.0
#define UINT_MAX 0xFFFFFFFFu
#define INT_MAX 0x7FFFFFFF


#ifdef SHADER_API_GLES
Expand Down
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 @@ -133,6 +133,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- On platforms that allow it skip the first mip of the depth pyramid and compute it alongside the depth buffer used for low res transparents.
- When trying to install the local configuration package, if another one is already present the user is now asked whether they want to keep it or not.
- Improved MSAA color resolve to fix issues when very bright and very dark samples are resolved together.
- Improve performance of GPU light AABB generation

## [10.0.0] - 2019-06-10

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@

// Used to index into our SFiniteLightBound (g_data) and
// LightVolumeData (_LightVolumeData) buffers.
int GenerateLightCullDataIndex(int lightIndex, uint numVisibleLights, uint eyeIndex)
uint GenerateLightCullDataIndex(uint lightIndex, uint numVisibleLights, uint eyeIndex)
{
lightIndex = min(lightIndex, numVisibleLights - 1); // Stay within bounds

// For monoscopic, there is just one set of light cull data structs.
// In stereo, all of the left eye structs are first, followed by the right eye structs.
const int perEyeBaseIndex = (int)eyeIndex * (int)numVisibleLights;
const uint perEyeBaseIndex = eyeIndex * numVisibleLights;
return (perEyeBaseIndex + lightIndex);
}

struct ScreenSpaceBoundsIndices
{
int min;
int max;
uint min;
uint max;
};

// The returned values are used to index into our AABB screen space bounding box buffer
// Usually named g_vBoundsBuffer. The two values represent the min/max indices.
ScreenSpaceBoundsIndices GenerateScreenSpaceBoundsIndices(int lightIndex, uint numVisibleLights, uint eyeIndex)
ScreenSpaceBoundsIndices GenerateScreenSpaceBoundsIndices(uint lightIndex, uint numVisibleLights, uint eyeIndex)
{
// In the monoscopic mode, there is one set of bounds (min,max -> 2 * g_iNrVisibLights)
// In stereo, there are two sets of bounds (leftMin, leftMax, rightMin, rightMax -> 4 * g_iNrVisibLights)
const int eyeRelativeBase = (int)eyeIndex * 2 * (int)numVisibleLights;
const uint eyeRelativeBase = eyeIndex * 2 * numVisibleLights;

ScreenSpaceBoundsIndices indices;
indices.min = eyeRelativeBase + lightIndex;
indices.max = eyeRelativeBase + lightIndex + (int)numVisibleLights;
indices.max = indices.min + numVisibleLights;

return indices;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ struct SFiniteLightBound
public Vector3 boxAxisY; // Scaled by the extents (half-size)
public Vector3 boxAxisZ; // Scaled by the extents (half-size)
public Vector3 center; // Center of the bounds (box) in camera space
public Vector2 scaleXY; // Scale applied to the top of the box to turn it into a truncated pyramid
public float radius; // Circumscribed sphere for the bounds (box)
public float scaleXY; // Scale applied to the top of the box to turn it into a truncated pyramid (X = Y)
public float radius; // Circumscribed sphere for the bounds (box)
};

[GenerateHLSL]
Expand Down Expand Up @@ -696,7 +696,6 @@ enum ClusterDepthSource : int
{ "TileLightListGen_NoDepthRT_SrcBigTile", "TileLightListGen_DepthRT_SrcBigTile_Oblique", "TileLightListGen_DepthRT_MSAA_SrcBigTile_Oblique" }
};

static int s_GenAABBKernel;
static int s_GenListPerTileKernel;
static int[,] s_ClusterKernels = new int[(int)ClusterPrepassSource.Count, (int)ClusterDepthSource.Count];
static int[,] s_ClusterObliqueKernels = new int[(int)ClusterPrepassSource.Count, (int)ClusterDepthSource.Count];
Expand Down Expand Up @@ -879,8 +878,6 @@ void InitializeLightLoop(IBLFilterBSDF[] iBLFilterBSDFArray)
m_MaxLightsOnScreen = m_MaxDirectionalLightsOnScreen + m_MaxPunctualLightsOnScreen + m_MaxAreaLightsOnScreen + m_MaxEnvLightsOnScreen;
m_MaxPlanarReflectionOnScreen = lightLoopSettings.maxPlanarReflectionOnScreen;

s_GenAABBKernel = buildScreenAABBShader.FindKernel("ScreenBoundsAABB");

// Cluster
{
s_ClearVoxelAtomicKernel = clearClusterAtomicIndexShader.FindKernel("ClearAtomic");
Expand Down Expand Up @@ -1766,9 +1763,9 @@ void GetLightVolumeDataAndBound(LightCategory lightCategory, GPULightType gpuLig
fAltDx *= range; fAltDy *= range;

// Handle case of pyramid with this select (currently unused)
var altDist = Mathf.Sqrt(fAltDy * fAltDy + (true ? 1.0f : 2.0f) * fAltDx * fAltDx);
bound.radius = altDist > (0.5f * range) ? altDist : (0.5f * range); // will always pick fAltDist
bound.scaleXY = squeeze ? new Vector2(0.01f, 0.01f) : new Vector2(1.0f, 1.0f);
var altDist = Mathf.Sqrt(fAltDy * fAltDy + (true ? 1.0f : 2.0f) * fAltDx * fAltDx);
bound.radius = altDist > (0.5f * range) ? altDist : (0.5f * range); // will always pick fAltDist
bound.scaleXY = squeeze ? 0.01f : 1.0f;

lightVolumeData.lightAxisX = vx;
lightVolumeData.lightAxisY = vy;
Expand All @@ -1780,16 +1777,19 @@ void GetLightVolumeDataAndBound(LightCategory lightCategory, GPULightType gpuLig
}
else if (gpuLightType == GPULightType.Point)
{
Vector3 vx = xAxisVS;
Vector3 vy = yAxisVS;
Vector3 vz = zAxisVS;
// Construct a view-space axis-aligned bounding cube around the bounding sphere.
// This allows us to utilize the same polygon clipping technique for all lights.
// Non-axis-aligned vectors may result in a larger screen-space AABB.
Vector3 vx = new Vector3(1, 0, 0);
Vector3 vy = new Vector3(0, 1, 0);
Vector3 vz = new Vector3(0, 0, 1);

bound.center = positionVS;
bound.boxAxisX = vx * range;
bound.boxAxisY = vy * range;
bound.boxAxisZ = vz * range;
bound.scaleXY.Set(1.0f, 1.0f);
bound.radius = range;
bound.scaleXY = 1.0f;
bound.radius = range;

// fill up ldata
lightVolumeData.lightAxisX = vx;
Expand All @@ -1810,7 +1810,7 @@ void GetLightVolumeDataAndBound(LightCategory lightCategory, GPULightType gpuLig
bound.boxAxisY = extents.y * yAxisVS;
bound.boxAxisZ = extents.z * zAxisVS;
bound.radius = extents.magnitude;
bound.scaleXY.Set(1.0f, 1.0f);
bound.scaleXY = 1.0f;

lightVolumeData.lightPos = centerVS;
lightVolumeData.lightAxisX = xAxisVS;
Expand All @@ -1830,7 +1830,7 @@ void GetLightVolumeDataAndBound(LightCategory lightCategory, GPULightType gpuLig
bound.boxAxisY = extents.y * yAxisVS;
bound.boxAxisZ = extents.z * zAxisVS;
bound.radius = extents.magnitude;
bound.scaleXY.Set(1.0f, 1.0f);
bound.scaleXY = 1.0f;

lightVolumeData.lightPos = centerVS;
lightVolumeData.lightAxisX = xAxisVS;
Expand All @@ -1850,7 +1850,7 @@ void GetLightVolumeDataAndBound(LightCategory lightCategory, GPULightType gpuLig
bound.boxAxisY = extents.y * yAxisVS;
bound.boxAxisZ = extents.z * zAxisVS;
bound.radius = extents.magnitude;
bound.scaleXY.Set(1.0f, 1.0f);
bound.scaleXY = 1.0f;

lightVolumeData.lightPos = centerVS;
lightVolumeData.lightAxisX = xAxisVS;
Expand Down Expand Up @@ -2065,8 +2065,8 @@ void GetEnvLightVolumeDataAndBound(HDProbe probe, LightVolumeType lightVolumeTyp
bound.boxAxisX = influenceRightVS * influenceExtents.x;
bound.boxAxisY = influenceUpVS * influenceExtents.x;
bound.boxAxisZ = influenceForwardVS * influenceExtents.x;
bound.scaleXY.Set(1.0f, 1.0f);
bound.radius = influenceExtents.x;
bound.scaleXY = 1.0f;
bound.radius = influenceExtents.x;
break;
}
case LightVolumeType.Box:
Expand All @@ -2075,8 +2075,8 @@ void GetEnvLightVolumeDataAndBound(HDProbe probe, LightVolumeType lightVolumeTyp
bound.boxAxisX = influenceExtents.x * influenceRightVS;
bound.boxAxisY = influenceExtents.y * influenceUpVS;
bound.boxAxisZ = influenceExtents.z * influenceForwardVS;
bound.scaleXY.Set(1.0f, 1.0f);
bound.radius = influenceExtents.magnitude;
bound.scaleXY = 1.0f;
bound.radius = influenceExtents.magnitude;

// The culling system culls pixels that are further
// than a threshold to the box influence extents.
Expand Down Expand Up @@ -2124,7 +2124,7 @@ void CreateBoxVolumeDataAndBound(OrientedBBox obb, LightCategory category, Light
bound.boxAxisY = extentConservativeY * upVS;
bound.boxAxisZ = extentConservativeZ * forwardVS;
bound.radius = extentConservativeMagnitude;
bound.scaleXY.Set(1.0f, 1.0f);
bound.scaleXY = 1.0f;

// The culling system culls pixels that are further
// than a threshold to the box influence extents.
Expand Down Expand Up @@ -3087,13 +3087,21 @@ static void GenerateLightsScreenSpaceAABBs(in BuildGPULightListParameters parame
{
if (parameters.totalLightCount != 0)
{
// With XR single-pass, we have one set of light bounds per view to iterate over (bounds are in view space for each view)
cmd.SetComputeBufferParam(parameters.screenSpaceAABBShader, parameters.screenSpaceAABBKernel, HDShaderIDs.g_data, resources.convexBoundsBuffer);
cmd.SetComputeBufferParam(parameters.screenSpaceAABBShader, parameters.screenSpaceAABBKernel, HDShaderIDs.g_vBoundsBuffer, resources.AABBBoundsBuffer);
using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.GenerateLightAABBs)))
{
// With XR single-pass, we have one set of light bounds per view to iterate over (bounds are in view space for each view)
cmd.SetComputeBufferParam(parameters.screenSpaceAABBShader, parameters.screenSpaceAABBKernel, HDShaderIDs.g_data, resources.convexBoundsBuffer);
cmd.SetComputeBufferParam(parameters.screenSpaceAABBShader, parameters.screenSpaceAABBKernel, HDShaderIDs.g_vBoundsBuffer, resources.AABBBoundsBuffer);

ConstantBuffer.Push(cmd, parameters.lightListCB, parameters.screenSpaceAABBShader, HDShaderIDs._ShaderVariablesLightList);

ConstantBuffer.Push(cmd, parameters.lightListCB, parameters.screenSpaceAABBShader, HDShaderIDs._ShaderVariablesLightList);
const int threadsPerLight = 4; // Shader: THREADS_PER_LIGHT (4)
const int threadsPerGroup = 64; // Shader: THREADS_PER_GROUP (64)

cmd.DispatchCompute(parameters.screenSpaceAABBShader, parameters.screenSpaceAABBKernel, (parameters.totalLightCount + 7) / 8, parameters.viewCount, 1);
int groupCount = HDUtils.DivRoundUp(parameters.totalLightCount * threadsPerLight, threadsPerGroup);

cmd.DispatchCompute(parameters.screenSpaceAABBShader, parameters.screenSpaceAABBKernel, groupCount, parameters.viewCount, 1);
}
}
}

Expand Down Expand Up @@ -3408,12 +3416,7 @@ unsafe BuildGPULightListParameters PrepareBuildGPULightListParameters( HDCamera

// Screen space AABB
parameters.screenSpaceAABBShader = buildScreenAABBShader;
parameters.screenSpaceAABBShader.shaderKeywords = null;
if (isProjectionOblique)
{
parameters.screenSpaceAABBShader.EnableKeyword("USE_OBLIQUE_MODE");
}
parameters.screenSpaceAABBKernel = s_GenAABBKernel;
parameters.screenSpaceAABBKernel = 0;

// Big tile prepass
parameters.runBigTilePrepass = hdCamera.frameSettings.IsEnabled(FrameSettingsField.BigTilePrepass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct SFiniteLightBound
float3 boxAxisY;
float3 boxAxisZ;
float3 center;
float2 scaleXY;
float scaleXY;
float radius;
};

Expand Down
Loading