diff --git a/addons/GDTask/GDTask.Delay.cs b/addons/GDTask/GDTask.Delay.cs index f7de42f..74eb6ad 100644 --- a/addons/GDTask/GDTask.Delay.cs +++ b/addons/GDTask/GDTask.Delay.cs @@ -253,7 +253,8 @@ static NextFramePromise() TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size); } - int frameCount; + bool isMainThread; + ulong frameCount; CancellationToken cancellationToken; GDTaskCompletionSourceCore core; @@ -273,7 +274,9 @@ public static IGDTaskSource Create(PlayerLoopTiming timing, CancellationToken ca result = new NextFramePromise(); } - result.frameCount = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1; + result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (result.isMainThread) + result.frameCount = Engine.GetProcessFrames(); result.cancellationToken = cancellationToken; TaskTracker.TrackActiveTask(result, 3); @@ -319,7 +322,7 @@ public bool MoveNext() return false; } - if (frameCount == Engine.GetFramesDrawn()) + if (isMainThread && frameCount == Engine.GetProcessFrames()) { return true; } @@ -348,7 +351,8 @@ static DelayFramePromise() TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size); } - int initialFrame; + bool isMainThread; + ulong initialFrame; int delayFrameCount; CancellationToken cancellationToken; @@ -373,7 +377,9 @@ public static IGDTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, result.delayFrameCount = delayFrameCount; result.cancellationToken = cancellationToken; - result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1; + result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (result.isMainThread) + result.initialFrame = Engine.GetProcessFrames(); TaskTracker.TrackActiveTask(result, 3); @@ -427,7 +433,7 @@ public bool MoveNext() } // skip in initial frame. - if (initialFrame == Engine.GetFramesDrawn()) + if (isMainThread && initialFrame == Engine.GetProcessFrames()) { #if DEBUG // force use Realtime. @@ -476,7 +482,8 @@ static DelayPromise() TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size); } - int initialFrame; + bool isMainThread; + ulong initialFrame; double delayTimeSpan; double elapsed; CancellationToken cancellationToken; @@ -502,7 +509,9 @@ public static IGDTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timi result.elapsed = 0.0f; result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds; result.cancellationToken = cancellationToken; - result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1; + result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (result.isMainThread) + result.initialFrame = Engine.GetProcessFrames(); TaskTracker.TrackActiveTask(result, 3); @@ -549,7 +558,7 @@ public bool MoveNext() if (elapsed == 0.0f) { - if (initialFrame == Engine.GetFramesDrawn()) + if (isMainThread && initialFrame == Engine.GetProcessFrames()) { return true; } diff --git a/addons/GDTask/PlayerLoopTimer.cs b/addons/GDTask/PlayerLoopTimer.cs index e91a424..aada6bc 100644 --- a/addons/GDTask/PlayerLoopTimer.cs +++ b/addons/GDTask/PlayerLoopTimer.cs @@ -142,7 +142,8 @@ bool IPlayerLoopItem.MoveNext() sealed class DeltaTimePlayerLoopTimer : PlayerLoopTimer { - int initialFrame; + bool isMainThread; + ulong initialFrame; double elapsed; double interval; @@ -156,7 +157,7 @@ protected override bool MoveNextCore() { if (elapsed == 0.0) { - if (initialFrame == Engine.GetFramesDrawn()) + if (isMainThread && initialFrame == Engine.GetProcessFrames()) { return true; } @@ -174,7 +175,9 @@ protected override bool MoveNextCore() protected override void ResetCore(TimeSpan? interval) { this.elapsed = 0.0; - this.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1; + this.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (this.isMainThread) + this.initialFrame = Engine.GetProcessFrames(); if (interval != null) { this.interval = (float)interval.Value.TotalSeconds;