Skip to content

Commit f716ea0

Browse files
alekseiunityEvergreen
authored andcommitted
Removed RenderSettings.ambientProbe assignment that should not exist.
Fixes the issue with accumulating ambient probe lighting when sky light multiplier is not equal to one. https://jira.unity3d.com/browse/UUM-69385
1 parent 31255e0 commit f716ea0

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/GPUResidentBatcher.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ public void DestroyMeshes(NativeArray<int> destroyedMeshes)
8484
m_InstanceCullingBatcher.DestroyMeshes(destroyedMeshes);
8585
}
8686

87+
internal void FreeRendererGroupInstances(NativeArray<int> rendererGroupIDs)
88+
{
89+
if (rendererGroupIDs.Length == 0)
90+
return;
91+
92+
var instances = new NativeList<InstanceHandle>(rendererGroupIDs.Length, Allocator.TempJob);
93+
m_BatchersContext.ScheduleQueryRendererGroupInstancesJob(rendererGroupIDs, instances).Complete();
94+
DestroyInstances(instances.AsArray());
95+
instances.Dispose();
96+
97+
m_BatchersContext.FreeRendererGroupInstances(rendererGroupIDs);
98+
}
99+
87100
public void InstanceOcclusionTest(RenderGraph renderGraph, in OcclusionCullingSettings settings, ReadOnlySpan<SubviewOcclusionTest> subviewOcclusionTests)
88101
{
89102
if (!m_BatchersContext.hasBoundingSpheres)
@@ -122,13 +135,18 @@ public void UpdateSelectedRenderers(NativeArray<int> renderersID)
122135

123136
public void PostCullBeginCameraRendering(RenderRequestBatcherContext context)
124137
{
125-
RenderSettings.ambientProbe = context.ambientProbe;
126-
m_BatchersContext.UpdateAmbientProbeAndGpuBuffer(context.ambientProbe);
127138
m_InstanceCullingBatcher.PostCullBeginCameraRendering(context);
128139
}
129140

141+
public void OnSetupAmbientProbe()
142+
{
143+
m_BatchersContext.UpdateAmbientProbeAndGpuBuffer(forceUpdate: false);
144+
}
145+
130146
private void UpdateRendererData(in GPUDrivenRendererGroupData rendererData, IList<Mesh> meshes, IList<Material> materials)
131147
{
148+
FreeRendererGroupInstances(rendererData.invalidRendererGroupID);
149+
132150
if (rendererData.rendererGroupID.Length == 0)
133151
return;
134152

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/GPUResidentDrawer.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public static void PostCullBeginCameraRendering(RenderRequestBatcherContext cont
6464
s_Instance?.batcher.PostCullBeginCameraRendering(context);
6565
}
6666

67+
68+
/// <summary>
69+
/// Utility function for updating probe data after global ambient probe is set up
70+
/// </summary>
71+
public static void OnSetupAmbientProbe()
72+
{
73+
s_Instance?.batcher.OnSetupAmbientProbe();
74+
}
75+
6776
/// <summary>
6877
/// Utility function to run an occlusion test in compute to update indirect draws.
6978
/// This function will dispatch compute shaders to run the given occlusion test and
@@ -445,8 +454,9 @@ private void Dispose()
445454

446455
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
447456
{
457+
// Loaded scene might contain light probes that would affect existing objects. Hence we have to update all probes data.
448458
if(mode == LoadSceneMode.Additive)
449-
m_BatchersContext.UpdateAmbientProbeAndGpuBuffer(RenderSettings.ambientProbe, true);
459+
m_BatchersContext.UpdateAmbientProbeAndGpuBuffer(forceUpdate: true);
450460
}
451461

452462
private static void PostPostLateUpdateStatic()
@@ -540,22 +550,24 @@ private void OnEndCameraRendering(ScriptableRenderContext context, Camera camera
540550

541551
private void PostPostLateUpdate()
542552
{
553+
m_BatchersContext.UpdateAmbientProbeAndGpuBuffer(forceUpdate: false);
554+
543555
Profiler.BeginSample("GPUResidentDrawer.DispatchChanges");
544556
var lodGroupTransformData = m_Dispatcher.GetTransformChangesAndClear<LODGroup>(TransformTrackingType.GlobalTRS, Allocator.TempJob);
545557
var lodGroupData = m_Dispatcher.GetTypeChangesAndClear<LODGroup>(Allocator.TempJob, noScriptingArray: true);
546558
var meshDataSorted = m_Dispatcher.GetTypeChangesAndClear<Mesh>(Allocator.TempJob, sortByInstanceID: true, noScriptingArray: true);
547559
var materialData = m_Dispatcher.GetTypeChangesAndClear<Material>(Allocator.TempJob, noScriptingArray: true);
548560
Profiler.EndSample();
549561

550-
Profiler.BeginSample("GPUResindentDrawer.ProcessMaterials");
562+
Profiler.BeginSample("GPUResidentDrawer.ProcessMaterials");
551563
ProcessMaterials(materialData.destroyedID);
552564
Profiler.EndSample();
553565

554-
Profiler.BeginSample("GPUResindentDrawer.ProcessMeshes");
566+
Profiler.BeginSample("GPUResidentDrawer.ProcessMeshes");
555567
ProcessMeshes(meshDataSorted.destroyedID);
556568
Profiler.EndSample();
557569

558-
Profiler.BeginSample("GPUResindentDrawer.ProcessLODGroups");
570+
Profiler.BeginSample("GPUResidentDrawer.ProcessLODGroups");
559571
ProcessLODGroups(lodGroupData.changedID, lodGroupData.destroyedID, lodGroupTransformData.transformedID);
560572
Profiler.EndSample();
561573

@@ -564,12 +576,11 @@ private void PostPostLateUpdate()
564576
meshDataSorted.Dispose();
565577
materialData.Dispose();
566578

567-
Profiler.BeginSample("GPUResindentDrawer.ProcessDraws");
579+
Profiler.BeginSample("GPUResidentDrawer.ProcessDraws");
568580
m_MeshRendererDrawer.ProcessDraws();
569581
// Add more drawers here ...
570582
Profiler.EndSample();
571583

572-
m_BatchersContext.UpdateAmbientProbeAndGpuBuffer(RenderSettings.ambientProbe);
573584
m_BatchersContext.UpdateInstanceMotions();
574585

575586
m_Batcher.UpdateFrame();
@@ -610,7 +621,7 @@ private void ProcessLODGroups(NativeArray<int> changedID, NativeArray<int> destr
610621

611622
internal void ProcessRenderers(NativeArray<int> rendererGroupsID)
612623
{
613-
Profiler.BeginSample("GPUResindentDrawer.ProcessMeshRenderers");
624+
Profiler.BeginSample("GPUResidentDrawer.ProcessMeshRenderers");
614625

615626
var changedInstances = new NativeArray<InstanceHandle>(rendererGroupsID.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
616627
ScheduleQueryRendererGroupInstancesJob(rendererGroupsID, changedInstances).Complete();
@@ -624,7 +635,7 @@ internal void ProcessRenderers(NativeArray<int> rendererGroupsID)
624635

625636
internal void TransformInstances(NativeArray<InstanceHandle> instances, NativeArray<Matrix4x4> localToWorldMatrices)
626637
{
627-
Profiler.BeginSample("GPUResindentDrawer.TransformInstances");
638+
Profiler.BeginSample("GPUResidentDrawer.TransformInstances");
628639

629640
m_BatchersContext.UpdateInstanceTransforms(instances, localToWorldMatrices);
630641

@@ -633,7 +644,7 @@ internal void TransformInstances(NativeArray<InstanceHandle> instances, NativeAr
633644

634645
internal void FreeInstances(NativeArray<InstanceHandle> instances)
635646
{
636-
Profiler.BeginSample("GPUResindentDrawer.FreeInstances");
647+
Profiler.BeginSample("GPUResidentDrawer.FreeInstances");
637648

638649
m_Batcher.DestroyInstances(instances);
639650
m_BatchersContext.FreeInstances(instances);
@@ -643,17 +654,9 @@ internal void FreeInstances(NativeArray<InstanceHandle> instances)
643654

644655
internal void FreeRendererGroupInstances(NativeArray<int> rendererGroupIDs)
645656
{
646-
if(rendererGroupIDs.Length == 0)
647-
return;
648-
649-
Profiler.BeginSample("GPUResindentDrawer.FreeRendererGroupInstances");
650-
651-
var instances = new NativeList<InstanceHandle>(rendererGroupIDs.Length, Allocator.TempJob);
652-
ScheduleQueryRendererGroupInstancesJob(rendererGroupIDs, instances).Complete();
653-
m_Batcher.DestroyInstances(instances.AsArray());
654-
instances.Dispose();
657+
Profiler.BeginSample("GPUResidentDrawer.FreeRendererGroupInstances");
655658

656-
m_BatchersContext.FreeRendererGroupInstances(rendererGroupIDs);
659+
m_Batcher.FreeRendererGroupInstances(rendererGroupIDs);
657660

658661
Profiler.EndSample();
659662
}

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/GPUResidentDrawerTypes.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public struct RenderRequestBatcherContext
1515
/// CommandBuffer that will be used for resulting commands
1616
/// </summary>
1717
public CommandBuffer commandBuffer;
18-
19-
/// <summary>
20-
/// Ambient probe to be set
21-
/// </summary>
22-
public SphericalHarmonicsL2 ambientProbe;
2318
}
2419

2520
/// <summary>

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/RenderersBatchersContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ public void UpdateInstanceTransforms(NativeArray<InstanceHandle> instances, Nati
307307
ChangeInstanceBufferVersion();
308308
}
309309

310-
public void UpdateAmbientProbeAndGpuBuffer(SphericalHarmonicsL2 ambientProbe, bool forceUpdate = false)
310+
public void UpdateAmbientProbeAndGpuBuffer(bool forceUpdate)
311311
{
312-
if (m_CachedAmbientProbe != ambientProbe || forceUpdate)
312+
if (forceUpdate || m_CachedAmbientProbe != RenderSettings.ambientProbe)
313313
{
314-
m_CachedAmbientProbe = ambientProbe;
314+
m_CachedAmbientProbe = RenderSettings.ambientProbe;
315315
m_InstanceDataSystem.UpdateAllInstanceProbes(m_RenderersParameters, m_InstanceDataBuffer);
316316
ChangeInstanceBufferVersion();
317317
}

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,11 +2360,7 @@ protected override void Render(ScriptableRenderContext renderContext, Camera[] c
23602360

23612361
var cmd = CommandBufferPool.Get("");
23622362

2363-
GPUResidentDrawer.PostCullBeginCameraRendering(new RenderRequestBatcherContext
2364-
{
2365-
commandBuffer = cmd,
2366-
ambientProbe = renderRequest.hdCamera.cameraFrameCount < 2 ? RenderSettings.ambientProbe : m_SkyManager.GetAmbientProbe(renderRequest.hdCamera)
2367-
});
2363+
GPUResidentDrawer.PostCullBeginCameraRendering(new RenderRequestBatcherContext { commandBuffer = cmd });
23682364

23692365
// The HDProbe store only one RenderData per probe, however RenderData can be view dependent (e.g. planar probes).
23702366
// To avoid that the render data for the wrong view is used, we previously store a copy of the render data

Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ internal void SetupAmbientProbe(HDCamera hdCamera)
708708
RenderSettings.ambientMode = AmbientMode.Custom; // Needed to specify ourselves the ambient probe (this will update internal ambient probe data passed to shaders)
709709
RenderSettings.ambientProbe = GetAmbientProbe(hdCamera);
710710

711+
// We need to inform GPUResidentDrawer that the ambient probe has been set up by a camera, so it can refresh the probe instance data on the GPU.
712+
GPUResidentDrawer.OnSetupAmbientProbe();
713+
711714
// If a camera just returns from being disabled, sky is not setup yet for it.
712715
if (hdCamera.lightingSky == null && hdCamera.skyAmbientMode == SkyAmbientMode.Dynamic)
713716
{

Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ static void RenderSingleCamera(ScriptableRenderContext context, UniversalCameraD
778778
var data = frameData.Create<UniversalRenderingData>();
779779
data.cullResults = context.Cull(ref cullingParameters);
780780

781-
GPUResidentDrawer.PostCullBeginCameraRendering(new RenderRequestBatcherContext { commandBuffer = cmd, ambientProbe = RenderSettings.ambientProbe });
781+
GPUResidentDrawer.PostCullBeginCameraRendering(new RenderRequestBatcherContext { commandBuffer = cmd });
782782

783783
var isForwardPlus = cameraData.renderer is UniversalRenderer { renderingModeActual: RenderingMode.ForwardPlus };
784784

0 commit comments

Comments
 (0)