Skip to content

Commit 38a7e4e

Browse files
committed
Probe Volumes: Use GlobalObjectId.targetObjectId (#16)
* Probe Volumes: Use GlobalObjectId.targetObjectId to uniquely identify Probe Volumes, and Probe Volume Asset ownership, rather than GetInstanceID() - which was not guarenteed to be persistent. This PR cleans up the related logic, allowing us to guarentee non-colliding bake ids, avoids hanging on to asset references during duplication of probe volumes, and, in the case where you have intentionally referenced a probe volume asset on a probe volume that does not own it, avoids baking that asset, which would previously stomp source data. * Probe Volumes: GlobalUniqueID: Use a custom stack-only + packed representation of the full GlobalObjectId, rather than only using GlobalObjectID.targetObjectId to avoid collisions when different scene probe volumes happen to have the same targetObjectId. This also allows full backwards asset lookup in the future if necessary, which should help debugging + build tools. Co-authored-by: pastasfuture <[email protected]>
1 parent d8b0cf4 commit 38a7e4e

File tree

10 files changed

+394
-125
lines changed

10 files changed

+394
-125
lines changed

com.unity.render-pipelines.high-definition/Editor/Lighting/ProbeVolume/ProbeVolumeSHPreview.shader

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Shader "Hidden/Debug/ProbeVolumeSHPreview"
55
_Exposure("_Exposure", Range(-10.0,10.0)) = 0.0
66
_ProbeVolumeResolution("_ProbeVolumeResolution", Vector) = (0, 0, 0, 0)
77
_ProbeVolumeProbeDisplayRadiusWS("_ProbeVolumeProbeDisplayRadiusWS", Float) = 1.0
8-
_ProbeVolumeAtlasBias("_ProbeVolumeAtlasBias", Vector) = (0, 0, 0, 0)
8+
_ProbeVolumeAtlasBiasTexels("_ProbeVolumeAtlasBiasTexels", Vector) = (0, 0, 0, 0)
99
_ProbeVolumeIsResidentInAtlas("_ProbeVolumeIsResidentInAtlas", Float) = 0.0
1010
_ProbeVolumeHighlightNegativeRinging("_ProbeVolumeHighlightNegativeRinging", Float) = 0.0
1111
_ProbeVolumeDrawValidity("_ProbeVolumeDrawValidity", Float) = 0.0
@@ -56,7 +56,7 @@ Shader "Hidden/Debug/ProbeVolumeSHPreview"
5656
float3 _ProbeVolumeResolution;
5757
float4x4 _ProbeIndex3DToPositionWSMatrix;
5858
float _ProbeVolumeProbeDisplayRadiusWS;
59-
float3 _ProbeVolumeAtlasBias;
59+
float3 _ProbeVolumeAtlasBiasTexels;
6060
int _ProbeVolumeIsResidentInAtlas;
6161
int _ProbeVolumeHighlightNegativeRinging;
6262
int _ProbeVolumeDrawValidity;
@@ -205,7 +205,7 @@ Shader "Hidden/Debug/ProbeVolumeSHPreview"
205205
{
206206
// Due to probeIndex3D getting stored as a float and interpolated, we need to round before converting to int.
207207
// Otherwise our texel coordinate will oscillate between probes randomly (based on precision).
208-
int3 probeIndexAtlas3D = (int3)(i.probeIndex3D + 0.5 + _ProbeVolumeAtlasBias);
208+
int3 probeIndexAtlas3D = (int3)(i.probeIndex3D + 0.5 + _ProbeVolumeAtlasBiasTexels);
209209

210210
probeSampleData = SampleProbeData(probeIndexAtlas3D, normalWS);
211211
}

com.unity.render-pipelines.high-definition/Runtime/Lighting/AdditionalGIBakeRequestsManager.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ namespace UnityEngine.Rendering.HighDefinition
1414
public class AdditionalGIBakeRequestsManager
1515
{
1616
// The baking ID for the extra requests
17-
// TODO: Need to ensure this never conflicts with bake IDs from others interacting with the API.
18-
// In our project, this is ProbeVolumes.
19-
internal static readonly int s_BakingID = 912345678;
17+
internal static readonly int s_BakingID = int.MaxValue;
2018

2119
private static AdditionalGIBakeRequestsManager s_Instance = new AdditionalGIBakeRequestsManager();
2220
/// <summary>
@@ -28,6 +26,9 @@ private AdditionalGIBakeRequestsManager()
2826
{
2927
if (!Application.isPlaying)
3028
{
29+
lightmapperBakeIDFromBakeID.Clear();
30+
lightmapperBakeIDNext = 0;
31+
3132
SubscribeOnBakeStarted();
3233
}
3334
}
@@ -37,6 +38,9 @@ private AdditionalGIBakeRequestsManager()
3738
if (!Application.isPlaying)
3839
{
3940
UnsubscribeOnBakeStarted();
41+
42+
lightmapperBakeIDFromBakeID.Clear();
43+
lightmapperBakeIDNext = 0;
4044
}
4145
}
4246

@@ -48,6 +52,36 @@ private AdditionalGIBakeRequestsManager()
4852

4953
private static readonly Vector2 s_FreelistSentinel = new Vector2(float.MaxValue, float.MaxValue);
5054

55+
// Lightmapper API uses ints as keys, but we want to use full, stable, GlobalObjectIds as keys.
56+
// Rather than hashing and hoping we don't collide, lets handle this robustly by
57+
// keeping a dictionary of ProbeVolumeGlobalUniqueID->int bit keys.
58+
private Dictionary<ProbeVolumeGlobalUniqueID, int> lightmapperBakeIDFromBakeID = new Dictionary<ProbeVolumeGlobalUniqueID, int>(32);
59+
private int lightmapperBakeIDNext = 0;
60+
61+
internal bool TryGetLightmapperBakeIDFromBakeID(ProbeVolumeGlobalUniqueID bakeID, out int lightmapperBakeID)
62+
{
63+
bool success = false;
64+
if (lightmapperBakeIDFromBakeID.TryGetValue(bakeID, out lightmapperBakeID))
65+
{
66+
success = true;
67+
}
68+
else if (lightmapperBakeIDNext == s_BakingID)
69+
{
70+
success = false;
71+
lightmapperBakeID = -1;
72+
Debug.LogWarningFormat("Error: Used up all lightmapper bake IDs. This should never happen. Somehow all {0} ids have been used up. This must be the result of a bug. Unlikely that you created and baked {0} unique bake requests. Quit and reopen unity to flush all IDs.", s_BakingID - 1);
73+
}
74+
else
75+
{
76+
success = true;
77+
lightmapperBakeID = lightmapperBakeIDNext;
78+
++lightmapperBakeIDNext;
79+
lightmapperBakeIDFromBakeID.Add(bakeID, lightmapperBakeID);
80+
}
81+
82+
return success;
83+
}
84+
5185
/// <summary>
5286
/// Enqueue a request for probe rendering at the specified location.
5387
/// </summary>
@@ -282,4 +316,4 @@ private void EnsureRequestPositionsSanitized()
282316
}
283317
}
284318
}
285-
#endif
319+
#endif

com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/IProbeVolumeList.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ interface IProbeVolumeList
1515

1616
ref ProbeVolumeArtistParameters GetParameters(int i);
1717

18-
int GetAtlasID(int i);
19-
int GetBakeID(int i);
18+
ProbeVolumeGlobalUniqueID GetAtlasID(int i);
2019
ProbeVolume.ProbeVolumeAtlasKey ComputeProbeVolumeAtlasKey(int i);
2120
ProbeVolume.ProbeVolumeAtlasKey GetProbeVolumeAtlasKeyPrevious(int i);
2221
void SetProbeVolumeAtlasKeyPrevious(int i, ProbeVolume.ProbeVolumeAtlasKey key);

0 commit comments

Comments
 (0)