Skip to content

Commit 0279aa4

Browse files
authored
Merge pull request #8049 from Unity-Technologies/internal/master
Internal/master
2 parents 9b6c281 + f6b9768 commit 0279aa4

File tree

467 files changed

+50850
-9694
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

467 files changed

+50850
-9694
lines changed

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/DynamicGI/DynamicGISkyOcclusion.compute

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,43 @@ void MainRayGenShader(
2626

2727
RayGenExecute(dispatchInfo);
2828
}
29+
30+
#pragma kernel EncodeShadingDirection
31+
32+
StructuredBuffer<float3> _SkyShadingPrecomputedDirection;
33+
StructuredBuffer<float3> _SkyShadingDirections;
34+
RWStructuredBuffer<uint> _SkyShadingIndices;
35+
36+
uint _ProbeCount;
37+
38+
uint LinearSearchClosestDirection(float3 direction)
39+
{
40+
int indexMax = 255;
41+
float bestDot = -10.0f;
42+
int bestIndex = 0;
43+
44+
for (int index=0; index< indexMax; index++)
45+
{
46+
float currentDot = dot(direction, _SkyShadingPrecomputedDirection[index]);
47+
if (currentDot > bestDot)
48+
{
49+
bestDot = currentDot;
50+
bestIndex = index;
51+
}
52+
}
53+
return bestIndex;
54+
}
55+
56+
[numthreads(64, 1, 1)]
57+
void EncodeShadingDirection(uint probeId : SV_DispatchThreadID)
58+
{
59+
if (probeId >= _ProbeCount)
60+
return;
61+
62+
uint bestDirectionIndex = 255;
63+
float norm = length(_SkyShadingDirections[probeId]);
64+
if (norm > 0.0001f)
65+
bestDirectionIndex = LinearSearchClosestDirection(_SkyShadingDirections[probeId] / norm);
66+
67+
_SkyShadingIndices[probeId] = bestDirectionIndex;
68+
}

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/DynamicGI/DynamicGISkyOcclusion.hlsl

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,8 @@ int _BackFaceCulling;
1919
int _BakeSkyShadingDirection;
2020

2121
StructuredBuffer<float3> _ProbePositions;
22-
StructuredBuffer<float3> _SkyShadingPrecomputedDirection;
2322
RWStructuredBuffer<float4> _SkyOcclusionOut;
2423
RWStructuredBuffer<float3> _SkyShadingOut;
25-
RWStructuredBuffer<uint> _SkyShadingDirectionIndexOut;
26-
27-
28-
uint LinearSearchClosestDirection(float3 direction)
29-
{
30-
int indexMax = 255;
31-
float bestDot = -10.0f;
32-
int bestIndex = 0;
33-
34-
for (int index=0; index< indexMax; index++)
35-
{
36-
float currentDot = dot(direction, _SkyShadingPrecomputedDirection[index]);
37-
if (currentDot > bestDot)
38-
{
39-
bestDot = currentDot;
40-
bestIndex = index;
41-
}
42-
}
43-
return bestIndex;
44-
}
45-
4624

4725
void RayGenExecute(UnifiedRT::DispatchInfo dispatchInfo)
4826
{
@@ -168,23 +146,5 @@ void RayGenExecute(UnifiedRT::DispatchInfo dispatchInfo)
168146
// The 1.125f exponent comes from experimental testing. It's the value that works the best when trying to match a bake and deringing done with the lightmapper, but it has no theoretical explanation.
169147
// In the future, we should replace these custom windowing and deringing operations with the ones used in the lightmapper to implement a more academical solution.
170148
_SkyOcclusionOut[probeId].yzw *= lerp(1.0f, radianceToIrradianceFactor, pow(windowL1, 1.125f));
171-
172-
173-
// Normalize computed direction
174-
if (_BakeSkyShadingDirection > 0)
175-
{
176-
uint bestDirectionIndex = 255;
177-
float norm = length(_SkyShadingOut[probeId]);
178-
if (norm > 0.0001f)
179-
{
180-
_SkyShadingOut[probeId] /= norm;
181-
bestDirectionIndex = LinearSearchClosestDirection(_SkyShadingOut[probeId]);
182-
}
183-
else
184-
{
185-
_SkyShadingOut[probeId] = float3(0.0f, 0.0f, 0.0f);
186-
}
187-
_SkyShadingDirectionIndexOut[probeId] = bestDirectionIndex;
188-
}
189149
}
190150
}

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/ProbeAdjustmentVolumeEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static void DrawBakingHelpers(SerializedProbeAdjustmentVolume p, Editor owner)
261261
using (new EditorGUI.DisabledScope(bakingSet == null))
262262
{
263263
if (GUILayout.Button(Styles.s_PreviewLighting))
264-
ProbeGIBaking.BakeAdjustmentVolume(bakingSet, ptv);
264+
AdaptiveProbeVolumes.BakeAdjustmentVolume(bakingSet, ptv);
265265

266266
ProbeVolumeLightingTab.BakeAPVButton();
267267
}

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/ProbeGIBaking.Dilate.cs

Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System.Collections.Generic;
2+
using UnityEditor;
3+
4+
using Cell = UnityEngine.Rendering.ProbeReferenceVolume.Cell;
25

36
namespace UnityEngine.Rendering
47
{
5-
partial class ProbeGIBaking
8+
partial class AdaptiveProbeVolumes
69
{
710
static ComputeShader dilationShader;
811
static int dilationKernel = -1;
@@ -16,6 +19,13 @@ static void InitDilationShaders()
1619
}
1720
}
1821

22+
static void GetProbeAndChunkIndex(int globalProbeIndex, out int chunkIndex, out int chunkProbeIndex)
23+
{
24+
var chunkSizeInProbeCount = ProbeBrickPool.GetChunkSizeInProbeCount();
25+
chunkIndex = globalProbeIndex / chunkSizeInProbeCount;
26+
chunkProbeIndex = globalProbeIndex - chunkIndex * chunkSizeInProbeCount;
27+
}
28+
1929
[GenerateHLSL(needAccessors = false)]
2030
struct DilatedProbe
2131
{
@@ -97,10 +107,7 @@ internal void ToSphericalHarmonicsShaderConstants(ProbeReferenceVolume.Cell cell
97107
if (cellChunkData.skyOcclusionDataL0L1.Length != 0)
98108
WriteToShaderSkyOcclusion(SO_L0L1, cellChunkData.skyOcclusionDataL0L1, index * 4);
99109
if (cellChunkData.skyShadingDirectionIndices.Length != 0)
100-
{
101-
var directions = DynamicSkyPrecomputedDirections.GetPrecomputedDirections();
102-
cellChunkData.skyShadingDirectionIndices[index] = (byte)LinearSearchClosestDirection(directions, SO_Direction);
103-
}
110+
cellChunkData.skyShadingDirectionIndices[index] = (byte)SkyOcclusionBaker.EncodeSkyShadingDirection(SO_Direction);
104111
}
105112
}
106113

@@ -167,6 +174,112 @@ public void Dispose()
167174
static readonly int _DilationParameters2 = Shader.PropertyToID("_DilationParameters2");
168175
static readonly int _OutputProbes = Shader.PropertyToID("_OutputProbes");
169176

177+
// Can definitively be optimized later on.
178+
// Also note that all the bookkeeping of all the reference volumes will likely need to change when we move to
179+
// proper UX.
180+
internal static void PerformDilation()
181+
{
182+
var prv = ProbeReferenceVolume.instance;
183+
var perSceneDataList = prv.perSceneDataList;
184+
if (perSceneDataList.Count == 0) return;
185+
SetBakingContext(perSceneDataList);
186+
187+
List<Cell> tempLoadedCells = new List<Cell>();
188+
189+
if (m_BakingSet.hasDilation)
190+
{
191+
var dilationSettings = m_BakingSet.settings.dilationSettings;
192+
193+
// Make sure all assets are loaded.
194+
prv.PerformPendingOperations();
195+
196+
// TODO: This loop is very naive, can be optimized, but let's first verify if we indeed want this or not.
197+
for (int iterations = 0; iterations < dilationSettings.dilationIterations; ++iterations)
198+
{
199+
// Try to load all available cells to the GPU. Might not succeed depending on the memory budget.
200+
prv.LoadAllCells();
201+
202+
// Dilate all cells
203+
List<Cell> dilatedCells = new List<Cell>(prv.cells.Values.Count);
204+
bool everythingLoaded = !prv.hasUnloadedCells;
205+
206+
if (everythingLoaded)
207+
{
208+
foreach (var cell in prv.cells.Values)
209+
{
210+
if (m_CellsToDilate.ContainsKey(cell.desc.index))
211+
{
212+
PerformDilation(cell, m_BakingSet);
213+
dilatedCells.Add(cell);
214+
}
215+
}
216+
}
217+
else
218+
{
219+
// When everything does not fit in memory, we are going to dilate one cell at a time.
220+
// To do so, we load the cell and all its neighbours and then dilate.
221+
// This is an inefficient use of memory but for now most of the time is spent in reading back the result anyway so it does not introduce any performance regression.
222+
223+
// Free All memory to make room for each cell and its neighbors for dilation.
224+
prv.UnloadAllCells();
225+
226+
foreach (var cell in prv.cells.Values)
227+
{
228+
if (!m_CellsToDilate.ContainsKey(cell.desc.index))
229+
continue;
230+
231+
var cellPos = cell.desc.position;
232+
// Load the cell and all its neighbors before doing dilation.
233+
for (int x = -1; x <= 1; ++x)
234+
{
235+
for (int y = -1; y <= 1; ++y)
236+
{
237+
for (int z = -1; z <= 1; ++z)
238+
{
239+
Vector3Int pos = cellPos + new Vector3Int(x, y, z);
240+
if (m_CellPosToIndex.TryGetValue(pos, out var cellToLoadIndex))
241+
{
242+
if (prv.cells.TryGetValue(cellToLoadIndex, out var cellToLoad))
243+
{
244+
if (prv.LoadCell(cellToLoad))
245+
{
246+
tempLoadedCells.Add(cellToLoad);
247+
}
248+
else
249+
Debug.LogError($"Not enough memory to perform dilation for cell {cell.desc.index}");
250+
}
251+
}
252+
}
253+
}
254+
}
255+
256+
PerformDilation(cell, m_BakingSet);
257+
dilatedCells.Add(cell);
258+
259+
// Free memory again.
260+
foreach (var cellToUnload in tempLoadedCells)
261+
prv.UnloadCell(cellToUnload);
262+
tempLoadedCells.Clear();
263+
}
264+
}
265+
266+
// Now write back the assets.
267+
WriteDilatedCells(dilatedCells);
268+
269+
AssetDatabase.SaveAssets();
270+
AssetDatabase.Refresh();
271+
272+
// Reload data
273+
foreach (var sceneData in perSceneDataList)
274+
{
275+
sceneData.QueueSceneRemoval();
276+
sceneData.QueueSceneLoading();
277+
}
278+
prv.PerformPendingOperations();
279+
}
280+
}
281+
}
282+
170283
static void PerformDilation(ProbeReferenceVolume.Cell cell, ProbeVolumeBakingSet bakingSet)
171284
{
172285
InitDilationShaders();
@@ -235,5 +348,36 @@ static void PerformDilation(ProbeReferenceVolume.Cell cell, ProbeVolumeBakingSet
235348
data.ExtractDilatedProbes();
236349
data.Dispose();
237350
}
351+
352+
// NOTE: This is somewhat hacky and is going to likely be slow (or at least slower than it could).
353+
// It is only a first iteration of the concept that won't be as impactful on memory as other options.
354+
internal static void RevertDilation()
355+
{
356+
if (m_BakingSet == null)
357+
{
358+
if (ProbeReferenceVolume.instance.perSceneDataList.Count == 0) return;
359+
SetBakingContext(ProbeReferenceVolume.instance.perSceneDataList);
360+
}
361+
362+
var dilationSettings = m_BakingSet.settings.dilationSettings;
363+
var blackProbe = new SphericalHarmonicsL2();
364+
365+
int chunkSizeInProbes = ProbeBrickPool.GetChunkSizeInProbeCount();
366+
foreach (var cell in ProbeReferenceVolume.instance.cells.Values)
367+
{
368+
for (int i = 0; i < cell.data.validity.Length; ++i)
369+
{
370+
if (dilationSettings.enableDilation && dilationSettings.dilationDistance > 0.0f && cell.data.validity[i] > dilationSettings.dilationValidityThreshold)
371+
{
372+
GetProbeAndChunkIndex(i, out var chunkIndex, out var index);
373+
374+
var cellChunkData = GetCellChunkData(cell.data, chunkIndex);
375+
376+
WriteToShaderCoeffsL0L1(blackProbe, cellChunkData.shL0L1RxData, cellChunkData.shL1GL1RyData, cellChunkData.shL1BL1RzData, index * 4);
377+
WriteToShaderCoeffsL2(blackProbe, cellChunkData.shL2Data_0, cellChunkData.shL2Data_1, cellChunkData.shL2Data_2, cellChunkData.shL2Data_3, index * 4);
378+
}
379+
}
380+
}
381+
}
238382
}
239383
}

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/ProbeGIBaking.Invalidation.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
using System;
12
using System.Collections.Generic;
2-
using UnityEditor;
33
using Unity.Collections;
4-
using Unity.Collections.LowLevel.Unsafe;
5-
using Unity.Jobs;
6-
using System;
74

85
namespace UnityEngine.Rendering
96
{
10-
partial class ProbeGIBaking
7+
partial class AdaptiveProbeVolumes
118
{
129
// We use this scratch memory as a way of spoofing the texture.
1310
static DynamicArray<float> s_Validity_locData = new DynamicArray<float>();

0 commit comments

Comments
 (0)