Skip to content

Commit 199c0f2

Browse files
Fix frame count in editor (#3173)
* Removed unused variables and fix sky framecount * Update effects to use camera frame count. Fix camera time in editor * Fix camera cache * Fix camera time in build * Fix compilation error in test project
1 parent cae06c6 commit 199c0f2

File tree

19 files changed

+111
-111
lines changed

19 files changed

+111
-111
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
@@ -440,7 +440,7 @@ public static bool BakeProbes(IEnumerable<HDProbe> bakedProbes)
440440
// to update the texture.
441441
// updateCount is a transient data, so don't execute this code before the asset reload.
442442
{
443-
UnityEngine.Random.InitState((int)(1000 * hdPipeline.GetTime()));
443+
UnityEngine.Random.InitState((int)(1000 * EditorApplication.timeSinceStartup));
444444
foreach (var probe in bakedProbes)
445445
{
446446
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
@@ -196,7 +196,6 @@ internal bool HasValidRenderedData()
196196
}
197197
else
198198
{
199-
bool hasEverRendered = lastRenderedFrame != int.MinValue;
200199
return hasEverRendered && hasValidTexture;
201200
}
202201
}
@@ -520,16 +519,16 @@ internal Matrix4x4 proxyToWorld
520519
: influenceToWorld;
521520

522521
internal bool wasRenderedAfterOnEnable { get; private set; } = false;
523-
internal int lastRenderedFrame { get; private set; } = int.MinValue;
522+
internal bool hasEverRendered { get; private set; } = false;
524523

525-
internal void SetIsRendered(int frame)
524+
internal void SetIsRendered()
526525
{
527526
#if UNITY_EDITOR
528527
m_WasRenderedDuringAsyncCompilation = ShaderUtil.anythingCompiling;
529528
#endif
530529
m_WasRenderedSinceLastOnDemandRequest = true;
531530
wasRenderedAfterOnEnable = true;
532-
lastRenderedFrame = frame;
531+
hasEverRendered = true;
533532
}
534533

535534
// 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct RenderAOParameters
281281
public ShaderVariablesAmbientOcclusion cb;
282282
}
283283

284-
RenderAOParameters PrepareRenderAOParameters(HDCamera camera, Vector2 historySize, int frameCount, in HDUtils.PackedMipChainInfo depthMipInfo)
284+
RenderAOParameters PrepareRenderAOParameters(HDCamera camera, Vector2 historySize, in HDUtils.PackedMipChainInfo depthMipInfo)
285285
{
286286
var parameters = new RenderAOParameters();
287287

@@ -304,6 +304,7 @@ RenderAOParameters PrepareRenderAOParameters(HDCamera camera, Vector2 historySiz
304304

305305
float invHalfTanFOV = -camera.mainViewConstants.projMatrix[1, 1];
306306
float aspectRatio = parameters.runningRes.y / parameters.runningRes.x;
307+
uint frameCount = camera.GetCameraFrameCount();
307308

308309
cb._AOParams0 = new Vector4(
309310
parameters.fullResolution ? 0.0f : 1.0f,

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
@@ -89,13 +89,12 @@ public DensityVolumeArtistParameters(Color color, float _meanFreePath, float _an
8989
m_EditorAdvancedFade = false;
9090
}
9191

92-
internal void Update(bool animate, float time)
92+
internal void Update(float time)
9393
{
9494
//Update scrolling based on deltaTime
9595
if (volumeMask != null)
9696
{
97-
float animationTime = animate ? time : 0.0f;
98-
textureOffset = (textureScrollingSpeed * animationTime);
97+
textureOffset = (textureScrollingSpeed * time);
9998
// Switch from right-handed to left-handed coordinate system.
10099
textureOffset.x = -textureOffset.x;
101100
textureOffset.y = -textureOffset.y;
@@ -180,7 +179,7 @@ public partial class DensityVolume : MonoBehaviour
180179

181180

182181
/// <summary>Gather and Update any parameters that may have changed.</summary>
183-
internal void PrepareParameters(bool animate, float time)
182+
internal void PrepareParameters(float time)
184183
{
185184
//Texture has been updated notify the manager
186185
bool updated = previousVolumeMask != parameters.volumeMask;
@@ -201,7 +200,7 @@ internal void PrepareParameters(bool animate, float time)
201200
#endif
202201
}
203202

204-
parameters.Update(animate, time);
203+
parameters.Update(time);
205204
}
206205

207206
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
@@ -105,12 +105,12 @@ public void DeRegisterVolume(DensityVolume volume)
105105

106106
public bool ContainsVolume(DensityVolume volume) => m_Volumes.Contains(volume);
107107

108-
public List<DensityVolume> PrepareDensityVolumeData(CommandBuffer cmd, HDCamera currentCam, float time)
108+
public List<DensityVolume> PrepareDensityVolumeData(CommandBuffer cmd, HDCamera currentCam)
109109
{
110110
//Update volumes
111-
bool animate = currentCam.animateMaterials;
111+
float time = currentCam.time;
112112
foreach (DensityVolume volume in m_Volumes)
113-
volume.PrepareParameters(animate, time);
113+
volume.PrepareParameters(time);
114114

115115
using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.UpdateDensityVolumeAtlas)))
116116
{

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
@@ -719,8 +719,8 @@ void UpdateShaderVariablesGlobalVolumetrics(ref ShaderVariablesGlobal cb, HDCame
719719

720720
// Get the interpolated anisotropy value.
721721
var fog = hdCamera.volumeStack.GetComponent<Fog>();
722-
int frameIndex = m_FrameCount;
723-
int currIdx = (frameIndex + 0) & 1;
722+
uint frameIndex = hdCamera.GetCameraFrameCount();
723+
uint currIdx = (frameIndex + 0) & 1;
724724

725725
var currParams = hdCamera.vBufferParams[currIdx];
726726

@@ -744,7 +744,7 @@ void UpdateShaderVariablesGlobalVolumetrics(ref ShaderVariablesGlobal cb, HDCame
744744
cb._VBufferRcpInstancedViewCount = 1.0f / hdCamera.viewCount;
745745
}
746746

747-
DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuffer cmd, float time)
747+
DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuffer cmd)
748748
{
749749
DensityVolumeList densityVolumes = new DensityVolumeList();
750750

@@ -765,7 +765,7 @@ DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuff
765765
m_VisibleVolumeData.Clear();
766766

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

770770
for (int i = 0; i < Math.Min(volumes.Count, k_MaxVisibleDensityVolumeCount); i++)
771771
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3421,7 +3421,7 @@ static void DoFinalPass(in FinalPassParameters parameters,
34213421
#if HDRP_DEBUG_STATIC_POSTFX
34223422
int textureId = 0;
34233423
#else
3424-
int textureId = Time.frameCount % blueNoiseTexture.depth;
3424+
int textureId = (int)hdCamera.GetCameraFrameCount() % blueNoiseTexture.depth;
34253425
#endif
34263426

34273427
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
@@ -246,6 +246,8 @@ internal struct HistoryEffectValidity
246246
// XR multipass and instanced views are supported (see XRSystem)
247247
internal XRPass xr { get; private set; }
248248

249+
internal float deltaTime => time - lastTime;
250+
249251
// Non oblique projection matrix (RHS)
250252
// TODO: this code is never used and not compatible with XR
251253
internal Matrix4x4 nonObliqueProjMatrix
@@ -453,9 +455,24 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp,
453455

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

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

694+
unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb)
695+
=> UpdateShaderVariablesGlobalCB(ref cb, (int)cameraFrameCount);
696+
677697
unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb, int frameCount)
678698
{
679699
bool taaEnabled = frameSettings.IsEnabled(FrameSettingsField.Postprocess)
@@ -708,8 +728,13 @@ unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb,
708728

709729
float ct = time;
710730
float pt = lastTime;
731+
#if UNITY_EDITOR
732+
float dt = time - lastTime;
733+
float sdt = dt;
734+
#else
711735
float dt = Time.deltaTime;
712736
float sdt = Time.smoothDeltaTime;
737+
#endif
713738

714739
cb._Time = new Vector4(ct * 0.05f, ct, ct * 2.0f, ct * 3.0f);
715740
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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ BuildGPULightListOutput BuildGPULightList(RenderGraph render
227227
class PushGlobalCameraParamPassData
228228
{
229229
public HDCamera hdCamera;
230-
public int frameCount;
231230
public ShaderVariablesGlobal globalCB;
232231
public ShaderVariablesXR xrCB;
233232
}
@@ -237,14 +236,13 @@ void PushGlobalCameraParams(RenderGraph renderGraph, HDCamera hdCamera)
237236
using (var builder = renderGraph.AddRenderPass<PushGlobalCameraParamPassData>("Push Global Camera Parameters", out var passData))
238237
{
239238
passData.hdCamera = hdCamera;
240-
passData.frameCount = m_FrameCount;
241239
passData.globalCB = m_ShaderVariablesGlobalCB;
242240
passData.xrCB = m_ShaderVariablesXRCB;
243241

244242
builder.SetRenderFunc(
245243
(PushGlobalCameraParamPassData data, RenderGraphContext context) =>
246244
{
247-
data.hdCamera.UpdateShaderVariablesGlobalCB(ref data.globalCB, data.frameCount);
245+
data.hdCamera.UpdateShaderVariablesGlobalCB(ref data.globalCB);
248246
ConstantBuffer.PushGlobal(context.cmd, data.globalCB, HDShaderIDs._ShaderVariablesGlobal);
249247
data.hdCamera.UpdateShaderVariablesXRCB(ref data.xrCB);
250248
ConstantBuffer.PushGlobal(context.cmd, data.xrCB, HDShaderIDs._ShaderVariablesXR);
@@ -442,7 +440,7 @@ TextureHandle RenderSSR(RenderGraph renderGraph,
442440
{
443441
result = RenderRayTracedReflections(renderGraph, hdCamera,
444442
prepassOutput.depthBuffer, prepassOutput.stencilBuffer, prepassOutput.normalBuffer, prepassOutput.resolvedMotionVectorsBuffer, clearCoatMask, skyTexture, rayCountTexture,
445-
m_FrameCount, m_ShaderVariablesRayTracingCB, transparent);
443+
m_ShaderVariablesRayTracingCB, transparent);
446444
}
447445
else
448446
{

0 commit comments

Comments
 (0)