-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathProbeGIBaking.Invalidation.cs
More file actions
201 lines (169 loc) · 8.89 KB
/
ProbeGIBaking.Invalidation.cs
File metadata and controls
201 lines (169 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Collections.Generic;
using UnityEditor;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using System;
namespace UnityEngine.Rendering
{
partial class ProbeGIBaking
{
// We use this scratch memory as a way of spoofing the texture.
static DynamicArray<float> s_Validity_locData = new DynamicArray<float>();
static DynamicArray<int> s_ProbeIndices = new DynamicArray<int>();
static Dictionary<Vector3, Bounds> s_ForceInvalidatedProbesAndTouchupVols = new Dictionary<Vector3, Bounds>();
internal static Vector3Int GetSampleOffset(int i)
{
return new Vector3Int(i & 1, (i >> 1) & 1, (i >> 2) & 1);
}
const float k_MinValidityForLeaking = 0.05f;
internal static int PackValidity(float[] validity)
{
int outputByte = 0;
for (int i = 0; i < 8; ++i)
{
int val = (validity[i] > k_MinValidityForLeaking) ? 0 : 1;
outputByte |= (val << i);
}
return outputByte;
}
static void StoreScratchData(int x, int y, int z, int dataWidth, int dataHeight, float value, int probeIndex)
{
int index = x + dataWidth * (y + dataHeight * z);
s_Validity_locData[index] = value;
s_ProbeIndices[index] = probeIndex;
}
static float ReadValidity(int x, int y, int z, int dataWidth, int dataHeight)
{
int index = x + dataWidth * (y + dataHeight * z);
return s_Validity_locData[index];
}
static int ReadProbeIndex(int x, int y, int z, int dataWidth, int dataHeight)
{
int index = x + dataWidth * (y + dataHeight * z);
return s_ProbeIndices[index];
}
// TODO: This whole process will need optimization.
static bool NeighbourhoodIsEmptySpace(Vector3 pos, float searchDistance, Bounds boundsToCheckAgainst)
{
Vector3 halfExtents = 0.5f * searchDistance * Vector3.one;
Vector3 brickCenter = pos + halfExtents;
Collider[] colliders = Physics.OverlapBox(brickCenter, halfExtents);
if (colliders.Length > 0) return false;
// TO_VERIFY: Shall we do this check?
//foreach (var collider in colliders)
//{
// if (collider.bounds.Intersects(boundsToCheckAgainst))
// return false;
//}
return true;
}
// This is very much modeled to be as close as possible to the way bricks are loaded in the texture pool.
// Not necessarily a good thing.
static void ComputeValidityMasks(BakingCell bakingCell)
{
var bricks = bakingCell.bricks;
var cell = bakingCell;
int chunkSize = ProbeBrickPool.GetChunkSizeInBrickCount();
int brickChunksCount = (bricks.Length + chunkSize - 1) / chunkSize;
var probeHasEmptySpaceInGrid = new NativeArray<bool>(bakingCell.probePositions.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
int shidx = 0;
for (int chunkIndex = 0; chunkIndex < brickChunksCount; ++chunkIndex)
{
Vector3Int locSize = ProbeBrickPool.ProbeCountToDataLocSize(ProbeBrickPool.GetChunkSizeInProbeCount());
int size = locSize.x * locSize.y * locSize.z;
int count = ProbeBrickPool.GetChunkSizeInProbeCount();
int bx = 0, by = 0, bz = 0;
s_Validity_locData.Resize(size);
s_ProbeIndices.Resize(size);
Dictionary<Vector3Int, (float, Vector3, Bounds)> probesToRestoreInfo = new Dictionary<Vector3Int, (float, Vector3, Bounds)>();
HashSet<Vector3Int> probesToRestore = new HashSet<Vector3Int>();
for (int brickIdx = 0; brickIdx < count; brickIdx += ProbeBrickPool.kBrickProbeCountTotal)
{
for (int z = 0; z < ProbeBrickPool.kBrickProbeCountPerDim; z++)
{
for (int y = 0; y < ProbeBrickPool.kBrickProbeCountPerDim; y++)
{
for (int x = 0; x < ProbeBrickPool.kBrickProbeCountPerDim; x++)
{
int ix = bx + x;
int iy = by + y;
int iz = bz + z;
if (shidx >= cell.validity.Length)
{
StoreScratchData(ix, iy, iz, locSize.x, locSize.y, 1.0f, shidx);
}
else
{
StoreScratchData(ix, iy, iz, locSize.x, locSize.y, cell.validity[shidx], shidx);
// Check if we need to do some extra check on this probe.
bool hasFreeNeighbourhood = false;
Bounds invalidatingTouchupBound;
if (s_ForceInvalidatedProbesAndTouchupVols.TryGetValue(cell.probePositions[shidx], out invalidatingTouchupBound))
{
int actualBrickIdx = brickIdx / ProbeBrickPool.kBrickProbeCountTotal;
float brickSize = ProbeReferenceVolume.CellSize(cell.bricks[actualBrickIdx].subdivisionLevel);
Vector3 position = cell.probePositions[shidx];
probesToRestore.Add(new Vector3Int(ix, iy, iz));
var searchDistance = (brickSize * m_BakingProfile.minBrickSize) / ProbeBrickPool.kBrickCellCount;
hasFreeNeighbourhood = NeighbourhoodIsEmptySpace(position, searchDistance, invalidatingTouchupBound);
}
probeHasEmptySpaceInGrid[shidx] = hasFreeNeighbourhood;
}
shidx++;
}
}
}
// update the pool index
bx += ProbeBrickPool.kBrickProbeCountPerDim;
if (bx >= locSize.x)
{
bx = 0;
by += ProbeBrickPool.kBrickProbeCountPerDim;
if (by >= locSize.y)
{
by = 0;
bz += ProbeBrickPool.kBrickProbeCountPerDim;
}
}
}
for (int x = 0; x < locSize.x; ++x)
{
for (int y = 0; y < locSize.y; ++y)
{
for (int z = 0; z < locSize.z; ++z)
{
int outIdx = ReadProbeIndex(x, y, z, locSize.x, locSize.y);
float probeValidity = ReadValidity(x, y, z, locSize.x, locSize.y);
if (outIdx < cell.validity.Length)
{
float[] validities = new float[8];
bool forceAllValid = false;
for (int o = 0; o < 8; ++o)
{
Vector3Int off = GetSampleOffset(o);
Vector3Int samplePos = new Vector3Int(Mathf.Clamp(x + off.x, 0, locSize.x - 1),
Mathf.Clamp(y + off.y, 0, locSize.y - 1),
Mathf.Clamp(z + off.z, 0, ProbeBrickPool.kBrickProbeCountPerDim - 1));
if (probesToRestore.Contains(samplePos))
{
if (probeHasEmptySpaceInGrid[outIdx])
{
forceAllValid = true;
}
}
validities[o] = ReadValidity(samplePos.x, samplePos.y, samplePos.z, locSize.x, locSize.y);
}
byte mask = forceAllValid ? (byte)255 : Convert.ToByte(PackValidity(validities));
float validity = probeValidity;
cell.validity[outIdx] = validity;
cell.validityNeighbourMask[outIdx] = mask;
}
}
}
}
}
probeHasEmptySpaceInGrid.Dispose();
}
}
}