Skip to content

Commit f361637

Browse files
Fix frame count in editor #3173
1 parent 8f104b3 commit f361637

20 files changed

+167
-147
lines changed

com.unity.render-pipelines.high-definition/Editor/Lighting/Reflection/HDBakedReflectionSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public static bool BakeProbes(IEnumerable<HDProbe> bakedProbes)
441441
// to update the texture.
442442
// updateCount is a transient data, so don't execute this code before the asset reload.
443443
{
444-
UnityEngine.Random.InitState((int)(1000 * hdPipeline.GetTime()));
444+
UnityEngine.Random.InitState((int)(1000 * EditorApplication.timeSinceStartup));
445445
foreach (var probe in bakedProbes)
446446
{
447447
var c = UnityEngine.Random.Range(2, 10);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ internal bool HasValidRenderedData()
160160
}
161161
else
162162
{
163-
bool hasEverRendered = lastRenderedFrame != int.MinValue;
164163
return hasEverRendered && hasValidTexture;
165164
}
166165
}
@@ -482,16 +481,16 @@ internal Matrix4x4 proxyToWorld
482481
: influenceToWorld;
483482

484483
internal bool wasRenderedAfterOnEnable { get; private set; } = false;
485-
internal int lastRenderedFrame { get; private set; } = int.MinValue;
484+
internal bool hasEverRendered { get; private set; } = false;
486485

487-
internal void SetIsRendered(int frame)
486+
internal void SetIsRendered()
488487
{
489488
#if UNITY_EDITOR
490489
m_WasRenderedDuringAsyncCompilation = ShaderUtil.anythingCompiling;
491490
#endif
492491
m_WasRenderedSinceLastOnDemandRequest = true;
493492
wasRenderedAfterOnEnable = true;
494-
lastRenderedFrame = frame;
493+
hasEverRendered = true;
495494
}
496495

497496
// API

com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/AmbientOcclusion.RenderGraph.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TextureHandle CreateAmbientOcclusionTexture(RenderGraph renderGraph)
1010
return renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { enableRandomWrite = true, colorFormat = GraphicsFormat.R8_UNorm, name = "Ambient Occlusion" });
1111
}
1212

13-
public TextureHandle Render(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle depthPyramid, TextureHandle normalBuffer, TextureHandle motionVectors, int frameCount, in HDUtils.PackedMipChainInfo depthMipInfo, ShaderVariablesRaytracing shaderVariablesRaytracing, TextureHandle rayCountTexture)
13+
public TextureHandle Render(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle depthPyramid, TextureHandle normalBuffer, TextureHandle motionVectors, in HDUtils.PackedMipChainInfo depthMipInfo, ShaderVariablesRaytracing shaderVariablesRaytracing, TextureHandle rayCountTexture)
1414
{
1515
var settings = hdCamera.volumeStack.GetComponent<AmbientOcclusion>();
1616

@@ -31,7 +31,7 @@ public TextureHandle Render(RenderGraph renderGraph, HDCamera hdCamera, TextureH
3131
hdCamera.AllocateAmbientOcclusionHistoryBuffer(scaleFactor);
3232

3333
if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && settings.rayTracing.value)
34-
return m_RaytracingAmbientOcclusion.RenderRTAO(renderGraph, hdCamera, depthPyramid, normalBuffer, motionVectors, rayCountTexture, frameCount, shaderVariablesRaytracing);
34+
return m_RaytracingAmbientOcclusion.RenderRTAO(renderGraph, hdCamera, depthPyramid, normalBuffer, motionVectors, rayCountTexture, shaderVariablesRaytracing);
3535
else
3636
{
3737
var historyRT = hdCamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.AmbientOcclusion);
@@ -42,7 +42,7 @@ public TextureHandle Render(RenderGraph renderGraph, HDCamera hdCamera, TextureH
4242
historyRT.referenceSize.y * historyRT.scaleFactor.y);
4343
var rtScaleForHistory = hdCamera.historyRTHandleProperties.rtHandleScale;
4444

45-
var aoParameters = PrepareRenderAOParameters(hdCamera, historySize * rtScaleForHistory, frameCount, depthMipInfo);
45+
var aoParameters = PrepareRenderAOParameters(hdCamera, historySize * rtScaleForHistory, depthMipInfo);
4646

4747
var packedData = RenderAO(renderGraph, aoParameters, depthPyramid, normalBuffer);
4848
result = DenoiseAO(renderGraph, aoParameters, depthPyramid, motionVectors, packedData, currentHistory, outputHistory);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ struct RenderAOParameters
363363
public ShaderVariablesAmbientOcclusion cb;
364364
}
365365

366-
RenderAOParameters PrepareRenderAOParameters(HDCamera camera, Vector2 historySize, int frameCount, in HDUtils.PackedMipChainInfo depthMipInfo)
366+
RenderAOParameters PrepareRenderAOParameters(HDCamera camera, Vector2 historySize, in HDUtils.PackedMipChainInfo depthMipInfo)
367367
{
368368
var parameters = new RenderAOParameters();
369369

@@ -386,6 +386,7 @@ RenderAOParameters PrepareRenderAOParameters(HDCamera camera, Vector2 historySiz
386386

387387
float invHalfTanFOV = -camera.mainViewConstants.projMatrix[1, 1];
388388
float aspectRatio = parameters.runningRes.y / parameters.runningRes.x;
389+
uint frameCount = camera.GetCameraFrameCount();
389390

390391
cb._AOParams0 = new Vector4(
391392
parameters.fullResolution ? 0.0f : 1.0f,
@@ -637,7 +638,7 @@ internal void Dispatch(CommandBuffer cmd, HDCamera camera, RTHandle depthTexture
637638
var rtScaleForHistory = camera.historyRTHandleProperties.rtHandleScale;
638639

639640
var hdrp = (RenderPipelineManager.currentPipeline as HDRenderPipeline);
640-
var aoParameters = PrepareRenderAOParameters(camera, historySize * rtScaleForHistory, frameCount, hdrp.sharedRTManager.GetDepthBufferMipChainInfo());
641+
var aoParameters = PrepareRenderAOParameters(camera, historySize * rtScaleForHistory, hdrp.sharedRTManager.GetDepthBufferMipChainInfo());
641642
using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.HorizonSSAO)))
642643
{
643644
RenderAO(aoParameters, m_PackedDataTex, depthTexture, normalBuffer, cmd);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ public DensityVolumeArtistParameters(Color color, float _meanFreePath, float _an
8484
m_EditorAdvancedFade = false;
8585
}
8686

87-
internal void Update(bool animate, float time)
87+
internal void Update(float time)
8888
{
8989
//Update scrolling based on deltaTime
9090
if (volumeMask != null)
9191
{
92-
float animationTime = animate ? time : 0.0f;
93-
textureOffset = (textureScrollingSpeed * animationTime);
92+
textureOffset = (textureScrollingSpeed * time);
9493
// Switch from right-handed to left-handed coordinate system.
9594
textureOffset.x = -textureOffset.x;
9695
textureOffset.y = -textureOffset.y;
@@ -167,7 +166,7 @@ public partial class DensityVolume : MonoBehaviour
167166

168167

169168
/// <summary>Gather and Update any parameters that may have changed.</summary>
170-
internal void PrepareParameters(bool animate, float time)
169+
internal void PrepareParameters(float time)
171170
{
172171
//Texture has been updated notify the manager
173172
bool updated = previousVolumeMask != parameters.volumeMask;
@@ -185,7 +184,7 @@ internal void PrepareParameters(bool animate, float time)
185184
#endif
186185
}
187186

188-
parameters.Update(animate, time);
187+
parameters.Update(time);
189188
}
190189

191190
private void NotifyUpdatedTexure()

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ public void DeRegisterVolume(DensityVolume volume)
6767

6868
public bool ContainsVolume(DensityVolume volume) => volumes.Contains(volume);
6969

70-
public List<DensityVolume> PrepareDensityVolumeData(CommandBuffer cmd, HDCamera currentCam, float time)
70+
public List<DensityVolume> PrepareDensityVolumeData(CommandBuffer cmd, HDCamera currentCam)
7171
{
7272
//Update volumes
73-
bool animate = currentCam.animateMaterials;
73+
float time = currentCam.time;
7474
foreach (DensityVolume volume in volumes)
7575
{
76-
volume.PrepareParameters(animate, time);
76+
volume.PrepareParameters(time);
7777
}
7878

7979
if (atlasNeedsRefresh)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,8 @@ void UpdateShaderVariablesGlobalVolumetrics(ref ShaderVariablesGlobal cb, HDCame
711711

712712
// Get the interpolated anisotropy value.
713713
var fog = hdCamera.volumeStack.GetComponent<Fog>();
714-
int frameIndex = m_FrameCount;
715-
int currIdx = (frameIndex + 0) & 1;
714+
uint frameIndex = hdCamera.GetCameraFrameCount();
715+
uint currIdx = (frameIndex + 0) & 1;
716716

717717
var currParams = hdCamera.vBufferParams[currIdx];
718718

@@ -736,7 +736,7 @@ void UpdateShaderVariablesGlobalVolumetrics(ref ShaderVariablesGlobal cb, HDCame
736736
cb._VBufferRcpInstancedViewCount = 1.0f / hdCamera.viewCount;
737737
}
738738

739-
DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuffer cmd, float time)
739+
DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuffer cmd)
740740
{
741741
DensityVolumeList densityVolumes = new DensityVolumeList();
742742

@@ -757,7 +757,7 @@ DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuff
757757
m_VisibleVolumeData.Clear();
758758

759759
// Collect all visible finite volume data, and upload it to the GPU.
760-
var volumes = DensityVolumeManager.manager.PrepareDensityVolumeData(cmd, hdCamera, time);
760+
var volumes = DensityVolumeManager.manager.PrepareDensityVolumeData(cmd, hdCamera);
761761

762762
for (int i = 0; i < Math.Min(volumes.Count, k_MaxVisibleVolumeCount); i++)
763763
{

com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4038,7 +4038,7 @@ static void DoFinalPass( in FinalPassParameters parameters,
40384038
#if HDRP_DEBUG_STATIC_POSTFX
40394039
int textureId = 0;
40404040
#else
4041-
int textureId = Time.frameCount % blueNoiseTexture.depth;
4041+
int textureId = (int)hdCamera.GetCameraFrameCount() % blueNoiseTexture.depth;
40424042
#endif
40434043

40444044
finalPassMaterial.EnableKeyword("DITHER");

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ internal struct HistoryEffectValidity
244244
// XR multipass and instanced views are supported (see XRSystem)
245245
internal XRPass xr { get; private set; }
246246

247+
internal float deltaTime => time - lastTime;
248+
247249
// Non oblique projection matrix (RHS)
248250
// TODO: this code is never used and not compatible with XR
249251
internal Matrix4x4 nonObliqueProjMatrix
@@ -451,9 +453,24 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp,
451453

452454
// Different views/tabs may have different values of the "Animated Materials" setting.
453455
animateMaterials = CoreUtils.AreAnimatedMaterialsEnabled(aniCam);
454-
455-
time = animateMaterials ? hdrp.GetTime() : 0;
456-
lastTime = animateMaterials ? hdrp.GetLastTime() : 0;
456+
if (animateMaterials)
457+
{
458+
float newTime, deltaTime;
459+
#if UNITY_EDITOR
460+
newTime = Application.isPlaying ? Time.time : Time.realtimeSinceStartup;
461+
deltaTime = Application.isPlaying ? Time.deltaTime : 0.033f;
462+
#else
463+
newTime = Time.time;
464+
deltaTime = Time.deltaTime;
465+
#endif
466+
time = newTime;
467+
lastTime = newTime - deltaTime;
468+
}
469+
else
470+
{
471+
time = 0;
472+
lastTime = 0;
473+
}
457474

458475
// Make sure that the shadow history identification array is allocated and is at the right size
459476
if (shadowHistoryUsage == null || shadowHistoryUsage.Length != hdrp.currentPlatformRenderPipelineSettings.hdShadowInitParams.maxScreenSpaceShadowSlots)
@@ -673,6 +690,9 @@ internal static void ResetAllHistoryRTHandleSystems(int width, int height)
673690
}
674691
}
675692

693+
unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb)
694+
=> UpdateShaderVariablesGlobalCB(ref cb, (int)cameraFrameCount);
695+
676696
unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb, int frameCount)
677697
{
678698
bool taaEnabled = frameSettings.IsEnabled(FrameSettingsField.Postprocess)
@@ -707,8 +727,13 @@ unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb,
707727

708728
float ct = time;
709729
float pt = lastTime;
730+
#if UNITY_EDITOR
731+
float dt = time - lastTime;
732+
float sdt = dt;
733+
#else
710734
float dt = Time.deltaTime;
711735
float sdt = Time.smoothDeltaTime;
736+
#endif
712737

713738
cb._Time = new Vector4(ct * 0.05f, ct, ct * 2.0f, ct * 3.0f);
714739
cb._SinTime = new Vector4(Mathf.Sin(ct * 0.125f), Mathf.Sin(ct * 0.25f), Mathf.Sin(ct * 0.5f), Mathf.Sin(ct));

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ BuildGPULightListOutput BuildGPULightList( RenderGraph rend
227227
class PushGlobalCameraParamPassData
228228
{
229229
public HDCamera hdCamera;
230-
public int frameCount;
231230
public ShaderVariablesGlobal globalCB;
232231
public ShaderVariablesXR xrCB;
233232

@@ -238,18 +237,17 @@ void PushGlobalCameraParams(RenderGraph renderGraph, HDCamera hdCamera)
238237
using (var builder = renderGraph.AddRenderPass<PushGlobalCameraParamPassData>("Push Global Camera Parameters", out var passData))
239238
{
240239
passData.hdCamera = hdCamera;
241-
passData.frameCount = m_FrameCount;
242240
passData.globalCB = m_ShaderVariablesGlobalCB;
243241
passData.xrCB = m_ShaderVariablesXRCB;
244242

245243
builder.SetRenderFunc(
246-
(PushGlobalCameraParamPassData data, RenderGraphContext context) =>
247-
{
248-
data.hdCamera.UpdateShaderVariablesGlobalCB(ref data.globalCB, data.frameCount);
249-
ConstantBuffer.PushGlobal(context.cmd, data.globalCB, HDShaderIDs._ShaderVariablesGlobal);
250-
data.hdCamera.UpdateShaderVariablesXRCB(ref data.xrCB);
251-
ConstantBuffer.PushGlobal(context.cmd, data.xrCB, HDShaderIDs._ShaderVariablesXR);
252-
});
244+
(PushGlobalCameraParamPassData data, RenderGraphContext context) =>
245+
{
246+
data.hdCamera.UpdateShaderVariablesGlobalCB(ref data.globalCB);
247+
ConstantBuffer.PushGlobal(context.cmd, data.globalCB, HDShaderIDs._ShaderVariablesGlobal);
248+
data.hdCamera.UpdateShaderVariablesXRCB(ref data.xrCB);
249+
ConstantBuffer.PushGlobal(context.cmd, data.xrCB, HDShaderIDs._ShaderVariablesXR);
250+
});
253251
}
254252
}
255253

@@ -440,8 +438,8 @@ TextureHandle RenderSSR( RenderGraph renderGraph,
440438
if (usesRaytracedReflections)
441439
{
442440
result = RenderRayTracedReflections(renderGraph, hdCamera,
443-
prepassOutput.depthBuffer, prepassOutput.stencilBuffer, prepassOutput.normalBuffer, prepassOutput.resolvedMotionVectorsBuffer, clearCoatMask, skyTexture, rayCountTexture,
444-
m_FrameCount, m_ShaderVariablesRayTracingCB, transparent);
441+
prepassOutput.depthBuffer, prepassOutput.stencilBuffer, prepassOutput.normalBuffer, prepassOutput.resolvedMotionVectorsBuffer, clearCoatMask, skyTexture, rayCountTexture,
442+
m_ShaderVariablesRayTracingCB, transparent);
445443
}
446444
else
447445
{

0 commit comments

Comments
 (0)