Skip to content

Commit

Permalink
Merge pull request #7956 from Unity-Technologies/internal/master
Browse files Browse the repository at this point in the history
Internal/master
  • Loading branch information
UnityAljosha authored Sep 5, 2023
2 parents 800063b + 98f306e commit cb1ef66
Show file tree
Hide file tree
Showing 141 changed files with 8,456 additions and 1,127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[EditorTool("Light Placement Tool", typeof(Light))]
class LightPlacementTool : EditorTool
{
private const float k_DefaultZoom = 2f;
private const float k_DefaultZoom = 0.05f;
private bool initialPositionSet;

private static Vector3 initialPivot;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,17 @@ public BakingProfiling(T stage, ref T currentStage)
currentStage = stage;
UpdateProgressBar(stage);

Profiling.Profiler.BeginSample(stage.ToString());
if (LogFile != null)
Profiling.Profiler.BeginSample(stage.ToString());
}

public void OnDispose(ref T currentStage)
{
if (disposed) return;
disposed = true;

Profiling.Profiler.EndSample();
if (LogFile != null)
Profiling.Profiler.EndSample();

UpdateProgressBar(prevStage);
currentStage = prevStage;
Expand All @@ -185,6 +187,7 @@ public class BakingSetupProfiling : BakingProfiling<BakingSetupProfiling.Stages>

public enum Stages
{
OnBakeStarted,
PrepareWorldSubdivision,
EnsurePerSceneDataInOpenScenes,
FindWorldBounds,
Expand All @@ -209,7 +212,6 @@ public class BakingCompleteProfiling : BakingProfiling<BakingCompleteProfiling.S
public enum Stages
{
FinalizingBake,
AddOccluders,
FetchResults,
WriteBakedData,
PerformDilation,
Expand Down Expand Up @@ -612,8 +614,6 @@ static public bool InitializeBake()
CellCountInDirections(out minCellPosition, out maxCellPosition, m_ProfileInfo.cellSizeInMeters);
cellCount = maxCellPosition + Vector3Int.one - minCellPosition;

GeneratePhysicsComponentToModList();

ProbeReferenceVolume.instance.EnsureCurrentBakingSet(m_BakingSet);

foreach (var data in ProbeReferenceVolume.instance.perSceneDataList)
Expand All @@ -628,6 +628,8 @@ static public bool InitializeBake()

static void OnBakeStarted()
{
using var scope = new BakingSetupProfiling(BakingSetupProfiling.Stages.OnBakeStarted);

if (!InitializeBake())
return;

Expand Down Expand Up @@ -872,7 +874,7 @@ static Dictionary<int, int> RemapBakedCells(bool isBakingSubset)
newCells.Add(oldIndex, cell);
}
}
}
}

return oldToNewCellRemapping;
}
Expand Down Expand Up @@ -1012,11 +1014,6 @@ static public void ApplyPostBakeOperations(NativeArray<SphericalHarmonicsL2> sh,
}
}

using (new BakingCompleteProfiling(BakingCompleteProfiling.Stages.AddOccluders))
AddOccluders();

ModifyPhysicsComponentsForBaking();

// Fetch results of all cells
using var fetchScope = new BakingCompleteProfiling(BakingCompleteProfiling.Stages.FetchResults);
BakingCompleteProfiling.GetProgressRange(out float progress0, out float progress1);
Expand Down Expand Up @@ -1186,9 +1183,6 @@ static public void ApplyPostBakeOperations(NativeArray<SphericalHarmonicsL2> sh,
}
fetchScope.Dispose();

RestorePhysicsComponentsAfterBaking();
CleanupOccluders();

m_BakingBatchIndex = 0;

// Force maximum sh bands to perform baking, we need to store what sh bands was selected from the settings as we need to restore it after.
Expand Down Expand Up @@ -1475,44 +1469,54 @@ static BakingCell ConvertCellToBakingCell(CellDesc cellDesc, CellData cellData)

// Runtime data layout is for GPU consumption.
// We need to convert it back to a linear layout for the baking cell.
int brickCount = probeCount / ProbeBrickPool.kBrickProbeCountTotal;
int probeIndex = 0;
int chunkOffsetInProbes = 0;
var chunksCount = cellDesc.shChunkCount;
var chunkSizeInProbes = ProbeBrickPool.GetChunkSizeInProbeCount();
Vector3Int locSize = ProbeBrickPool.ProbeCountToDataLocSize(chunkSizeInProbes);

var blackSH = GetBlackSH();

for (int brickIndex = 0; brickIndex < brickCount; ++brickIndex)
for (int chunkIndex = 0; chunkIndex < chunksCount; ++chunkIndex)
{
int chunkIndex = brickIndex / ProbeBrickPool.GetChunkSizeInBrickCount();
var cellChunkData = GetCellChunkData(cellData, chunkIndex);

for (int z = 0; z < ProbeBrickPool.kBrickProbeCountPerDim; z++)
for (int brickIndex = 0; brickIndex < m_BakingSet.chunkSizeInBricks; ++brickIndex)
{
for (int y = 0; y < ProbeBrickPool.kBrickProbeCountPerDim; y++)
if (probeIndex >= probeCount)
break;

for (int z = 0; z < ProbeBrickPool.kBrickProbeCountPerDim; z++)
{
for (int x = 0; x < ProbeBrickPool.kBrickProbeCountPerDim; x++)
for (int y = 0; y < ProbeBrickPool.kBrickProbeCountPerDim; y++)
{
var remappedIndex = GetProbeGPUIndex(brickIndex, x, y, z, locSize);
for (int x = 0; x < ProbeBrickPool.kBrickProbeCountPerDim; x++)
{
var remappedIndex = GetProbeGPUIndex(brickIndex, x, y, z, locSize);

// Scenario data can be invalid due to partially baking the set.
if (cellChunkData.scenarioValid)
ReadFullFromShaderCoeffsL0L1L2(ref bc.sh[probeIndex], cellChunkData.shL0L1RxData, cellChunkData.shL1GL1RyData, cellChunkData.shL1BL1RzData,
cellChunkData.shL2Data_0, cellChunkData.shL2Data_1, cellChunkData.shL2Data_2, cellChunkData.shL2Data_3, remappedIndex);
else
bc.sh[probeIndex] = blackSH;
// Scenario data can be invalid due to partially baking the set.
if (cellChunkData.scenarioValid)
ReadFullFromShaderCoeffsL0L1L2(ref bc.sh[probeIndex], cellChunkData.shL0L1RxData, cellChunkData.shL1GL1RyData, cellChunkData.shL1BL1RzData,
cellChunkData.shL2Data_0, cellChunkData.shL2Data_1, cellChunkData.shL2Data_2, cellChunkData.shL2Data_3, remappedIndex);
else
bc.sh[probeIndex] = blackSH;

bc.validityNeighbourMask[probeIndex] = cellChunkData.validityNeighMaskData[remappedIndex];

bc.probePositions[probeIndex] = cellData.probePositions[remappedIndex];
bc.validity[probeIndex] = cellData.validity[remappedIndex];
bc.validityNeighbourMask[probeIndex] = cellChunkData.validityNeighMaskData[remappedIndex];
bc.touchupVolumeInteraction[probeIndex] = cellData.touchupVolumeInteraction[remappedIndex];
if (hasVirtualOffsets)
bc.offsetVectors[probeIndex] = cellData.offsetVectors[remappedIndex];
remappedIndex += chunkOffsetInProbes;
bc.probePositions[probeIndex] = cellData.probePositions[remappedIndex];
bc.validity[probeIndex] = cellData.validity[remappedIndex];
bc.touchupVolumeInteraction[probeIndex] = cellData.touchupVolumeInteraction[remappedIndex];
if (hasVirtualOffsets)
bc.offsetVectors[probeIndex] = cellData.offsetVectors[remappedIndex];

probeIndex++;
probeIndex++;
}
}
}
}

chunkOffsetInProbes += chunkSizeInProbes;
}

return bc;
Expand Down Expand Up @@ -1685,10 +1689,9 @@ unsafe static void WriteBakingCells(BakingCell[] bakingCells)

for (var i = 0; i < bakingCells.Length; ++i)
{
AnalyzeBrickForIndirectionEntries(ref bakingCells[i]);
var bakingCell = bakingCells[i];

AnalyzeBrickForIndirectionEntries(ref bakingCell);

m_BakingSet.cellDescs.Add(bakingCell.index, new CellDesc
{
position = bakingCell.position,
Expand Down Expand Up @@ -1841,7 +1844,7 @@ unsafe static void WriteBakingCells(BakingCell[] bakingCells)
positionsChunkTarget[index] = Vector3.zero;
touchupVolumeInteractionChunkTarget[index] = 0.0f;
if (hasVirtualOffsets)
offsetChunkTarget[index] = Vector3.zero;
offsetChunkTarget[index] = Vector3.zero;
}
else
{
Expand Down Expand Up @@ -2064,16 +2067,9 @@ public static void OnBakeCompletedCleanup()
UnityEditor.Experimental.Lightmapping.additionalBakedProbesCompleted -= OnAdditionalProbesBakeCompleted;
if (m_BakingBatch != null)
UnityEditor.Experimental.Lightmapping.SetAdditionalBakedProbes(m_BakingBatch.index, null);

RestorePhysicsComponentsAfterBaking();
CleanupOccluders();
}
}

s_ExcludedColliders = null;
s_ExcludedRigidBodies = null;
s_AddedOccluders = null;

// We need to reset that view
ProbeReferenceVolume.instance.ResetDebugViewToMaxSubdiv();
}
Expand All @@ -2083,6 +2079,7 @@ public static Vector3[] RunPlacement()
ClearBakingBatch();

ProbeSubdivisionResult result;
GIContributors? contributors = null;

float prevBrickSize = ProbeReferenceVolume.instance.MinBrickSize();
int prevMaxSubdiv = ProbeReferenceVolume.instance.GetMaxSubdivision();
Expand All @@ -2096,6 +2093,7 @@ public static Vector3[] RunPlacement()
else
{
var ctx = PrepareProbeSubdivisionContext();
contributors = ctx.contributors;

// Subdivide the scene and place the bricks
using (new BakingSetupProfiling(BakingSetupProfiling.Stages.BakeBricks))
Expand All @@ -2108,7 +2106,7 @@ public static Vector3[] RunPlacement()
{
float brickSize = m_ProfileInfo.minBrickSize;
Matrix4x4 newRefToWS = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(brickSize, brickSize, brickSize));
ApplySubdivisionResults(result, newRefToWS, out positions);
ApplySubdivisionResults(result, contributors, newRefToWS, out positions);
}

// Restore loaded asset settings
Expand Down Expand Up @@ -2228,11 +2226,14 @@ public static void ModifyProfileFromLoadedData(ProbeVolumeBakingSet bakingSet)
}

// Converts brick information into positional data at kBrickProbeCountPerDim * kBrickProbeCountPerDim * kBrickProbeCountPerDim resolution
internal static void ConvertBricksToPositions(ref BakingCell cell, Brick[] bricks, Vector3[] outProbePositions, int[] outBrickSubdiv)
internal static void ConvertBricksToPositions(Brick[] bricks, out Vector3[] outProbePositions, out int[] outBrickSubdiv)
{
int posIdx = 0;
float scale = ProbeReferenceVolume.instance.MinBrickSize() / ProbeBrickPool.kBrickCellCount;

outProbePositions = new Vector3[bricks.Length * ProbeBrickPool.kBrickProbeCountTotal];
outBrickSubdiv = new int[bricks.Length * ProbeBrickPool.kBrickProbeCountTotal];

foreach (var b in bricks)
{
int brickSize = ProbeReferenceVolume.CellSize(b.subdivisionLevel);
Expand All @@ -2248,6 +2249,7 @@ internal static void ConvertBricksToPositions(ref BakingCell cell, Brick[] brick

outProbePositions[posIdx] = (Vector3)probeOffset * scale;
outBrickSubdiv[posIdx] = b.subdivisionLevel;

posIdx++;
}
}
Expand All @@ -2261,7 +2263,7 @@ static int PosToIndex(Vector3Int pos)
return normalizedPos.z * (cellCount.x * cellCount.y) + normalizedPos.y * cellCount.x + normalizedPos.x;
}

public static void ApplySubdivisionResults(ProbeSubdivisionResult results, Matrix4x4 refToWS, out Vector3[] positions)
public static void ApplySubdivisionResults(ProbeSubdivisionResult results, GIContributors? contributors, Matrix4x4 refToWS, out Vector3[] positions)
{
// For now we just have one baking batch. Later we'll have more than one for a set of scenes.
// All probes need to be baked only once for the whole batch and not once per cell
Expand All @@ -2278,8 +2280,8 @@ public static void ApplySubdivisionResults(ProbeSubdivisionResult results, Matri
if (++i % 10 == 0)
EditorUtility.DisplayProgressBar("Baking Probe Volumes", $"({i} of {results.cells.Count}) Subdivide Cell", Mathf.Lerp(progress0, progress1, i / (float)results.cells.Count));

var probePositions = new Vector3[bricks.Length * ProbeBrickPool.kBrickProbeCountTotal];
var brickSubdivLevels = new int[bricks.Length * ProbeBrickPool.kBrickProbeCountTotal];
ConvertBricksToPositions(bricks, out var probePositions, out var brickSubdivLevels);
DeduplicateProbePositions(in probePositions, in brickSubdivLevels, positionToIndex, m_BakingBatch, positionList, out var probeIndices);

BakingCell cell = new BakingCell()
{
Expand All @@ -2288,11 +2290,9 @@ public static void ApplySubdivisionResults(ProbeSubdivisionResult results, Matri
bounds = bounds,
bricks = bricks,
probePositions = probePositions,
probeIndices = probeIndices,
};

ConvertBricksToPositions(ref cell, bricks, probePositions, brickSubdivLevels);
DeduplicateProbePositions(in probePositions, in brickSubdivLevels, positionToIndex, m_BakingBatch, positionList, out cell.probeIndices);

m_BakingBatch.cells.Add(cell);
m_BakingBatch.cellIndex2SceneReferences[cell.index] = new HashSet<string>(results.scenesPerCells[cell.position]);
}
Expand All @@ -2301,7 +2301,7 @@ public static void ApplySubdivisionResults(ProbeSubdivisionResult results, Matri

// Virtually offset positions before passing them to lightmapper
using (new BakingSetupProfiling(BakingSetupProfiling.Stages.ApplyVirtualOffsets))
ApplyVirtualOffsets(positions, out m_BakingBatch.virtualOffsets);
ApplyVirtualOffsets(contributors, positions, out m_BakingBatch.virtualOffsets);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ static class Styles
public static readonly GUIContent virtualOffsetSearchMultiplier = EditorGUIUtility.TrTextContent("Search Distance Multiplier", "Determines the length of the sampling ray Unity uses to search for valid probe positions.");
public static readonly GUIContent virtualOffsetBiasOutGeometry = EditorGUIUtility.TrTextContent("Geometry Bias", "Determines how far Unity pushes a probe out of geometry after a ray hit.");
public static readonly GUIContent virtualOffsetRayOriginBias = EditorGUIUtility.TrTextContent("Ray Origin Bias", "Distance from the probe position used to determine the origin of the sampling ray.");
public static readonly GUIContent virtualOffsetMaxHitsPerRay = EditorGUIUtility.TrTextContent("Max Ray Hits", "How many collisions to allow per ray before determining the Virtual Offset probe position.");
public static readonly GUIContent virtualOffsetCollisionMask = EditorGUIUtility.TrTextContent("Layer Mask", "Layers to include in collision calculations for Virtual Offset.");

public static readonly GUIContent dilationSettingsTitle = EditorGUIUtility.TrTextContent("Probe Dilation Settings");
Expand Down Expand Up @@ -94,13 +93,11 @@ void DrawVirtualOffsetSettings(SerializedProperty virtualOffsetSettings)
var virtualOffsetGeometrySearchMultiplier = virtualOffsetSettings.FindPropertyRelative("searchMultiplier");
var virtualOffsetBiasOutOfGeometry = virtualOffsetSettings.FindPropertyRelative("outOfGeoOffset");
var virtualOffsetRayOriginBias = virtualOffsetSettings.FindPropertyRelative("rayOriginBias");
var virtualOffsetMaxHitsPerRay = virtualOffsetSettings.FindPropertyRelative("maxHitsPerRay");
var virtualOffsetCollisionMask = virtualOffsetSettings.FindPropertyRelative("collisionMask");

EditorGUILayout.PropertyField(virtualOffsetGeometrySearchMultiplier, Styles.virtualOffsetSearchMultiplier);
EditorGUILayout.PropertyField(virtualOffsetBiasOutOfGeometry, Styles.virtualOffsetBiasOutGeometry);
EditorGUILayout.PropertyField(virtualOffsetRayOriginBias, Styles.virtualOffsetRayOriginBias);
EditorGUILayout.PropertyField(virtualOffsetMaxHitsPerRay, Styles.virtualOffsetMaxHitsPerRay);
EditorGUILayout.PropertyField(virtualOffsetCollisionMask, Styles.virtualOffsetCollisionMask);

GUILayout.BeginHorizontal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static class Styles
public static readonly string msgProbeFreeze = "Some scene(s) in this Baking Set are not currently loaded in the Hierarchy. Set Probe Positions to Don't Recalculate to not break compatibility with already baked scenarios.";
public static readonly GUIContent maxDistanceBetweenProbes = new GUIContent("Max Probe Spacing", "Maximum distance between probes, in meters. Determines the number of bricks in a streamable unit.");
public static readonly GUIContent minDistanceBetweenProbes = new GUIContent("Min Probe Spacing", "Minimum distance between probes, in meters.");
public static readonly string simplificationLevelsHighWarning = "High simplification levels have a big memory overhead, they are not recommended except for testing purposes.";
public static readonly string simplificationLevelsHighWarning = "A high number of brick size causes a big memory overhead, this is not recommended except for testing purposes.";
public static readonly GUIContent indexDimensions = new GUIContent("Index Dimensions", "The dimensions of the index buffer.");
public static readonly GUIContent minRendererVolumeSize = new GUIContent("Min Renderer Size", "The smallest Renderer size to consider when placing probes.");
public static readonly GUIContent renderersLayerMask = new GUIContent("Layer Mask", "Specify Layers to use when generating probe positions.");
Expand Down Expand Up @@ -157,10 +157,10 @@ void ProbePlacementGUI()

SimplificationLevelsSlider();

int levels = m_SimplificationLevels.intValue + 1;
int levels = ProbeVolumeBakingSet.GetMaxSubdivision(m_SimplificationLevels.intValue);
MessageType helpBoxType = MessageType.Info;
string helpBoxText = $"Probe Volumes can use a maximum of {levels} subdivision levels.";
if (levels == 5)
string helpBoxText = $"Baked data will contain up to {levels} different size of bricks.";
if (levels == 6)
{
helpBoxType = MessageType.Warning;
helpBoxText += "\n" + Styles.simplificationLevelsHighWarning;
Expand Down
Loading

0 comments on commit cb1ef66

Please sign in to comment.