Add force sync frames.

This commit is contained in:
2023-01-01 18:52:28 +08:00
parent 989a6b5554
commit a416f772d4
3 changed files with 34 additions and 19 deletions

View File

@@ -54,8 +54,8 @@ namespace Cryville.Crtr {
static bool disableGC = true; static bool disableGC = true;
static float clippingDist = 1f; static float clippingDist = 1f;
static float renderDist = 6f; static float renderDist = 6f;
static float renderStep = 0.05f; static double renderStep = 0.05;
public static float actualRenderStep = 0f; public static double actualRenderStep = 0;
static bool autoRenderStep = false; static bool autoRenderStep = false;
public static float soundOffset = 0; public static float soundOffset = 0;
static float startOffset = 0; static float startOffset = 0;
@@ -111,7 +111,7 @@ namespace Cryville.Crtr {
bool texloaddone; bool texloaddone;
diag::Stopwatch texloadtimer = new diag::Stopwatch(); diag::Stopwatch texloadtimer = new diag::Stopwatch();
bool firstFrame; int forceSyncFrames;
double atime0; double atime0;
void Update() { void Update() {
if (started) GameUpdate(); if (started) GameUpdate();
@@ -123,10 +123,18 @@ namespace Cryville.Crtr {
try { try {
if (Screen.width != screenSize.x || Screen.height != screenSize.y) if (Screen.width != screenSize.x || Screen.height != screenSize.y)
throw new InvalidOperationException("Window resized while playing"); throw new InvalidOperationException("Window resized while playing");
float dt = firstFrame double dt, step;
? 1f / Application.targetFrameRate if (forceSyncFrames != 0) {
: Time.deltaTime; forceSyncFrames--;
firstFrame = false; double target = Game.AudioClient.Position - atime0;
dt = target - cbus.Time;
step = autoRenderStep ? 1f / Application.targetFrameRate : renderStep;
inputProxy.SyncTime(target);
}
else {
dt = Time.deltaTime;
step = autoRenderStep ? Time.smoothDeltaTime : renderStep;
}
inputProxy.ForceTick(); inputProxy.ForceTick();
cbus.ForwardByTime(dt); cbus.ForwardByTime(dt);
bbus.ForwardByTime(dt); bbus.ForwardByTime(dt);
@@ -135,10 +143,6 @@ namespace Cryville.Crtr {
bbus.CopyTo(2, tbus); bbus.CopyTo(2, tbus);
bbus.CopyTo(3, nbus); bbus.CopyTo(3, nbus);
UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.EndSample();
float step = autoRenderStep ? ( firstFrame
? 1f / Application.targetFrameRate
: Time.smoothDeltaTime
) : renderStep;
actualRenderStep = step; actualRenderStep = step;
nbus.ForwardStepByTime(clippingDist, step); nbus.ForwardStepByTime(clippingDist, step);
@@ -250,14 +254,15 @@ namespace Cryville.Crtr {
status.text = sttext; status.text = sttext;
} }
void OnCameraPostRender(Camera cam) { void OnCameraPostRender(Camera cam) {
if (!started) return;
if (!logEnabled) return; if (!logEnabled) return;
if (started) timetext = string.Format( timetext = string.Format(
"\nSTime: {0:R}\nATime: {1:R}\nITime: {2:R}", "\nSTime: {0:R}s {3}\ndATime: {1:+0.0ms;-0.0ms;0} {3}\ndITime: {2:+0.0ms;-0.0ms;0} {3}",
cbus.Time, cbus.Time,
Game.AudioClient.Position - atime0, (Game.AudioClient.Position - atime0 - cbus.Time) * 1e3,
inputProxy.GetTimestampAverage() (inputProxy.GetTimestampAverage() - cbus.Time) * 1e3,
forceSyncFrames != 0 ? "(force sync)" : ""
); );
else timetext = string.Empty;
} }
#endregion #endregion
@@ -309,7 +314,7 @@ namespace Cryville.Crtr {
autoRenderStep = renderStep == 0; autoRenderStep = renderStep == 0;
soundOffset = Settings.Default.SoundOffset; soundOffset = Settings.Default.SoundOffset;
startOffset = Settings.Default.StartOffset; startOffset = Settings.Default.StartOffset;
firstFrame = true; forceSyncFrames= Settings.Default.ForceSyncFrames;
texloaddone = false; texloaddone = false;
Game.NetworkTaskWorker.SuspendBackgroundTasks(); Game.NetworkTaskWorker.SuspendBackgroundTasks();
Game.AudioSession = Game.AudioSequencer.NewSession(); Game.AudioSession = Game.AudioSequencer.NewSession();

View File

@@ -232,8 +232,7 @@ namespace Cryville.Crtr {
public void SyncTime(double time) { public void SyncTime(double time) {
foreach (var s in _sproxies.Keys) { foreach (var s in _sproxies.Keys) {
var h = s.Handler; var h = s.Handler;
if (!_timeOrigins.ContainsKey(h)) _timeOrigins[h] = h.GetCurrentTimestamp() - time;
_timeOrigins.Add(h, h.GetCurrentTimestamp() - time);
} }
} }
public void ForceTick() { public void ForceTick() {

View File

@@ -34,6 +34,17 @@ namespace Cryville.Crtr {
} }
} }
[Category("gameplay")]
[Range(-1, 5)]
public int ForceSyncFrames {
get {
return PlayerPrefs.GetInt("ForceSyncFrames", 5);
}
set {
PlayerPrefs.SetInt("ForceSyncFrames", value);
}
}
[Browsable(false)] [Browsable(false)]
[Category("data")] [Category("data")]
[Description("The directory where the game files are stored.")] [Description("The directory where the game files are stored.")]