public void UpdateSleep(bool isRelevant, MyTimeSpan currentTime) { if (isRelevant) SleepTime = MyTimeSpan.Zero; else if (!IsSleeping) SleepTime = currentTime; }
public void BeforeRender(MyTimeSpan? currentDrawTime) { using (m_lock.Acquire()) { if (currentDrawTime.HasValue) MyRenderProxy.CurrentDrawTime = currentDrawTime.Value; } }
public void ReadAndClear(MyTimeSpan currentTime, out Value sum, out int count, out Value min, out Value max, out Value last, out MyStatTypeEnum type, out int decimals, out MyTimeSpan inactivityMs) { Lock.Enter(); try { inactivityMs = MyTimeSpan.Zero; if (Count <= 0) // Nothing was written { // Load delta, increment it and save it var delta = IntToDeltaTime(-Count); delta += Count < 0 ? currentTime - LastClear : MyTimeSpan.FromMiliseconds(1); Count = -DeltaTimeToInt(delta); inactivityMs = delta; LastClear = currentTime; // Nothing was written, postpone clear } else { if (currentTime >= (LastRefresh + MyTimeSpan.FromMiliseconds(RefreshRate))) { DrawSum = Sum; DrawCount = Count; DrawMin = Min; DrawMax = Max; DrawLast = Last; LastRefresh = currentTime; if (ClearRate == -1) // Clear with refresh { Count = 0; ClearUnsafe(); } } if (ClearRate != -1 && currentTime >= (LastClear + MyTimeSpan.FromMiliseconds(ClearRate))) { Count = 0; ClearUnsafe(); LastClear = currentTime; } } type = Type; decimals = NumDecimals; } finally { Lock.Exit(); } // No need lock, not accessed anywhere else outside read sum = DrawSum; count = DrawCount; min = DrawMin; max = DrawMax; last = DrawLast; }
public void BeforeRender(MyRenderSettings writeTo, MyTimeSpan? currentDrawTime) { using (m_lock.Acquire()) { if (currentDrawTime.HasValue) MyRenderProxy.CurrentDrawTime = currentDrawTime.Value; writeTo.Synchronize(m_inputRenderSettings); } }
private void SetHealth(float health) { if (health<20) //play heavy breath indefinitely m_healthOverride = MyTimeSpan.MaxValue; else if (health<100) m_healthOverride = MySandboxGame.Static.UpdateTime + MyTimeSpan.FromSeconds(300 / (health - 19.99)); Update(true); }
public static void Draw(Dictionary<MyRenderStats.ColumnEnum, List<MyStats>> m_stats, float scale, Color color) { foreach (var pair in m_stats) { try { foreach (var s in pair.Value) { s.WriteTo(m_tmpDrawText); m_tmpDrawText.AppendLine(); // Newline between each group } Vector2 pos = new Vector2(10, 10); if (pair.Key == MyRenderStats.ColumnEnum.Right) { Vector2 size = MyRender11.GetDebugFont().MeasureString(m_tmpDrawText, scale); if (m_rightColumnWidth < size.X) { m_rightColumnWidth = size.X * m_rightGapSizeRatio; // Add some gap m_rightColumnChangeTime = MyRender11.CurrentDrawTime; } else if (m_rightColumnWidth > size.X * m_rightGapSizeRatio && (MyRender11.CurrentDrawTime - m_rightColumnChangeTime).Seconds > 3) { m_rightColumnWidth = size.X * m_rightGapSizeRatio; m_rightColumnChangeTime = MyRender11.CurrentDrawTime; } pos = new Vector2(MyRender11.ViewportResolution.X - m_rightColumnWidth, 0); } /* Add info about render buffers */ m_tmpDrawText.Append("Hardware Buffers (count/bytes):\n"); foreach (var bufferStatistic in MyManagers.Buffers.GetReport()) { m_tmpDrawText.AppendFormat(" {0}: {1:N0}/{2:N0}\n", bufferStatistic.Name, bufferStatistic.TotalBuffers, bufferStatistic.TotalBytes); } m_tmpDrawText.Append("Textures:\n"); MyFileTextureUsageReport report = MyManagers.FileTextures.GetReport(); m_tmpDrawText.AppendFormat(" Total: {0}\n", report.TexturesTotal); m_tmpDrawText.AppendFormat(" Loaded: {0}\n", report.TexturesLoaded); m_tmpDrawText.AppendFormat(" Memory: {0:N0}\n", report.TotalTextureMemory); MyDebugTextHelpers.DrawText(pos, m_tmpDrawText, color, scale); } finally { m_tmpDrawText.Clear(); } } }
public void AfterUpdate(MyTimeSpan? updateTimestamp) { using (m_lock.Acquire()) { if (updateTimestamp.HasValue) m_inputRenderMessages.CurrentUpdateFrame.UpdateTimestamp = updateTimestamp.Value; m_inputRenderMessages.CommitUpdateFrame(); m_inputBillboards.CommitWrite(); m_inputBillboards.Write.Clear(); m_inputTriangleBillboards.CommitWrite(); m_inputTriangleBillboards.Write.Clear(); } }
public void AfterUpdate(MyRenderSettings inputSettings, MyTimeSpan? updateTimestamp) { using (m_lock.Acquire()) { if (updateTimestamp.HasValue) m_inputRenderMessages.CurrentUpdateFrame.UpdateTimestamp = updateTimestamp.Value; m_inputRenderMessages.CommitUpdateFrame(); m_inputRenderSettings.Synchronize(inputSettings); // TODO: OP! Better settings synchronization m_inputBillboards.CommitWrite(); m_inputBillboards.Write.Clear(); m_inputTriangleBillboards.CommitWrite(); m_inputTriangleBillboards.Write.Clear(); } }
public void Wait() { m_timer.AddElapsed(MyTimeSpan.FromMilliseconds(-m_delta)); var currentTicks = m_timer.ElapsedTicks; // Wait for correct frame start m_targetTicks += TickPerFrame; if ((currentTicks > m_targetTicks + TickPerFrame * 5) || EnableMaxSpeed) { // We're more behind than 5 frames, don't try to catch up // (or we just do not want to wait in EnableMaxSpeed mode) m_targetTicks = currentTicks; } else { // For until correct tick comes if (EnableUpdateWait) { var remaining = MyTimeSpan.FromTicks(m_targetTicks - currentTicks); int waitMs = (int)(remaining.Milliseconds - 0.1); // To handle up to 0.1ms inaccuracy of timer if (waitMs > 0) { m_waiter.Reset(); MyTimer.StartOneShot(waitMs, m_handler); m_waiter.Wait(17 + m_delta); // Never wait more than 17ms //Debug.Assert(MyPerformanceCounter.ElapsedTicks < m_targetTicks); //VRageRender.MyRenderStats.Write("WaitRemaining", (float)MyPerformanceCounter.TicksToMs(m_targetTicks - MyPerformanceCounter.ElapsedTicks), VRageRender.MyStatTypeEnum.MinMaxAvg, 300, 3); } } // Sanity check to prevent freezing in the loop, wait at most 1 and 1/4 of frame if (m_targetTicks < (m_timer.ElapsedTicks + TickPerFrame + TickPerFrame / 4)) { while (m_timer.ElapsedTicks < m_targetTicks) { } // Busy wait for the precise moment } else // Something went terribly wrong, reset target ticks { m_targetTicks = m_timer.ElapsedTicks; } } m_delta = 0; }
/// <summary> /// Adds sample with timestamp, it must be larger than last timestamp! /// </summary> public void AddSample(ref T item, MyTimeSpan sampleTimestamp) { if (sampleTimestamp < m_lastTimeStamp) { //Debug.Fail("Adding sample out of order!"); return; } if (sampleTimestamp == m_lastTimeStamp && m_queue.Count > 0) { m_queue[m_queue.Count - 1] = new Item(item, sampleTimestamp); } else { m_queue.Enqueue(new Item(item, sampleTimestamp)); m_lastTimeStamp = sampleTimestamp; } }
/// <summary> /// Discards old samples, keeps at least 2 samples to be able to interpolate or extrapolate. /// </summary> public void DiscardOld(MyTimeSpan currentTimestamp) { int discardCount = -1; for (int i = 0; i < m_queue.Count; i++) { if (m_queue[i].Timestamp < currentTimestamp) { discardCount++; } else { break; } } for (int i = 0; i < discardCount && m_queue.Count > 2; i++) { m_queue.Dequeue(); } }
public static void EndProfileBlock() { Block ended = ProfileValues.m_block.Pop(); MyTimeSpan elapsed = new MyTimeSpan(ProfileValues.m_timer.ElapsedTicks - ended.Started); using (ProfileValues.m_lock.AcquireExclusiveUsing()) { Stats s; if (!ProfileValues.m_profile.TryGetValue(ended.Name, out s)) { s = new Stats(); ProfileValues.m_profile.Add(ended.Name, s); } s.TimeSpent += elapsed; s.Invokes++; if (ProfileValues.m_block.Count == 0) ProfileValues.m_total.TimeSpent += elapsed; ProfileValues.m_total.Invokes++; } }
public static void Draw(Dictionary<VRageRender.MyRenderStats.ColumnEnum, List<MyStats>> m_stats, float scale, Color color) { foreach (var pair in m_stats) { try { foreach (var s in pair.Value) { s.WriteTo(m_tmpDrawText); m_tmpDrawText.AppendLine(); // Newline between each group } Vector2 pos = new Vector2(0, 0); if (pair.Key == VRageRender.MyRenderStats.ColumnEnum.Right) { Vector2 size = MyRender.MeasureText(m_tmpDrawText, scale); if (m_rightColumnWidth < size.X) { m_rightColumnWidth = size.X * m_rightGapSizeRatio; // Add some gap m_rightColumnChangeTime = MyRender.CurrentDrawTime; } else if(m_rightColumnWidth > size.X * m_rightGapSizeRatio && (MyRender.CurrentDrawTime - m_rightColumnChangeTime).Seconds > 3) { m_rightColumnWidth = size.X * m_rightGapSizeRatio; m_rightColumnChangeTime = MyRender.CurrentDrawTime; } pos = new Vector2(MyRender.ScreenSize.X - m_rightColumnWidth, 0); } MyRender.DrawText(pos, m_tmpDrawText, color, scale); } finally { m_tmpDrawText.Clear(); } } }
/// <summary> /// Create Results from a List of execution times. /// </summary> /// <param name="executionTimes">Execution times. Results does not create a copy, so the list should not be changed.</param> internal Results(ListSnapshots<MyTimeSpan> executionTimes) { UnsortedResults = executionTimes.immutable(); Count = UnsortedResults.Count; Lazy_SortedResults = new Lazy<ReadOnlyList<MyTimeSpan>>(() => { executionTimes.mutable().Sort((MyTimeSpan first, MyTimeSpan second) => { return Math.Sign((first - second).Ticks); }); return new ReadOnlyList<MyTimeSpan>(executionTimes.immutable()); }); Lazy_Total = new Lazy<MyTimeSpan>(() => { MyTimeSpan sum = new MyTimeSpan(0); foreach (MyTimeSpan result in UnsortedResults) sum += result; return sum; }); Lazy_Mean = new Lazy<MyTimeSpan>(() => { return new MyTimeSpan(Total.Ticks / Count); }); Lazy_Median = new Lazy<MyTimeSpan>(() => SortedResults_Interpolate((decimal)Count / 2 - 0.5m)); Lazy_FirstQuartile = new Lazy<MyTimeSpan>(() => SortedResults_Interpolate((decimal)Count / 4 - 0.5m)); Lazy_ThirdQuartile = new Lazy<MyTimeSpan>(() => SortedResults_Interpolate((decimal)Count * 3 / 4 - 0.5m)); }
public void ProfileCustomValue(string name, string member, int line, string file, float value, MyTimeSpan? customTime, string timeFormat, string valueFormat, string callFormat = null) { StartBlock(name, member, line, file); EndBlock(member, line, file, customTime, value, timeFormat, valueFormat, callFormat); }
public void EndBlock(string member, int line, string file, MyTimeSpan? customTime = null, float customValue = 0, string timeFormat = null, string valueFormat = null, string callFormat = null) { Debug.Assert(!EnableAsserts || OwnerThread == Thread.CurrentThread); if (m_levelSkipCount > 0) { m_levelSkipCount--; return; } if (m_currentProfilingStack.Count > 0) { MyProfilerBlock profilingBlock = m_currentProfilingStack.Pop(); CheckEndBlock(profilingBlock, member, file, GetParentId()); profilingBlock.CustomValue = customValue; profilingBlock.TimeFormat = timeFormat; profilingBlock.ValueFormat = valueFormat; profilingBlock.CallFormat = callFormat; profilingBlock.End(MemoryProfiling, customTime); } else { Debug.Fail(String.Format("Unpaired profiling end block encountered for '{0}'{1}File: {2}({3}){1}", member, Environment.NewLine, file, line)); } if (AutoCommit && m_currentProfilingStack.Count == 0) CommitInternal(); }
/// <summary> /// Updates resource. /// </summary> public void Update(MyTimeSpan updateTime) { CheckUpdate(); CheckProfilerDump(); ProfilerShort.Begin("Parallel.RunCallbacks"); ParallelTasks.Parallel.RunCallbacks(); ProfilerShort.End(); TimeSpan elapsedTimespan = new TimeSpan(0, 0, 0, 0, (int)(MyEngineConstants.UPDATE_STEP_SIZE_IN_MILLISECONDS)); // Prevent update when game is paused if (m_updateAllowed || Engine.Platform.Game.IsDedicated) { if (MySandboxGame.IsPaused) { return; } UpdateComponents(); MyParticleEffects.UpdateEffects(); ProfilerShort.Begin("Multiplayer.Tick"); if (MyMultiplayer.Static != null) { MyMultiplayer.Static.Tick(); } ProfilerShort.End(); // update global game time ElapsedGameTime += elapsedTimespan; if (m_lastTimeMemoryLogged + TimeSpan.FromSeconds(30) < DateTime.UtcNow) { MySandboxGame.Log.WriteLine(String.Format("GC Memory: {0} B", GC.GetTotalMemory(false).ToString("##,#"))); m_lastTimeMemoryLogged = DateTime.UtcNow; } if (AutoSaveInMinutes > 0) { if (MySandboxGame.IsGameReady && (updateTime.TimeSpan - m_timeOfSave.TimeSpan) > TimeSpan.FromMinutes(AutoSaveInMinutes)) { MySandboxGame.Log.WriteLine("Autosave initiated"); MyCharacter character = LocalCharacter; bool canSave = (character != null && !character.IsDead) || character == null; MySandboxGame.Log.WriteLine("Character state: " + canSave); canSave &= Sync.IsServer; MySandboxGame.Log.WriteLine("IsServer: " + Sync.IsServer); canSave &= !MyAsyncSaving.InProgress; MySandboxGame.Log.WriteLine("MyAsyncSaving.InProgress: " + MyAsyncSaving.InProgress); if (canSave) { MySandboxGame.Log.WriteLineAndConsole("Autosave"); MyAsyncSaving.Start(() => MySector.ResetEyeAdaptation = true); //black screen after autosave } m_timeOfSave = updateTime; } } if (MySandboxGame.IsGameReady && m_framesToReady > 0) { m_framesToReady--; if (m_framesToReady == 0) { Ready = true; MyAudio.Static.PlayMusic(new MyMusicTrack() { TransitionCategory = MyStringId.GetOrCompute("Default") }); if (OnReady != null) OnReady(); if (OnReady != null) foreach (var cb in OnReady.GetInvocationList()) { OnReady -= (Action)cb; } if (Engine.Platform.Game.IsDedicated) MyLog.Default.WriteLineAndConsole("Game ready... Press Ctrl+C to exit"); } } if (Sync.MultiplayerActive && !Sync.IsServer) CheckMultiplayerStatus(); m_gameplayFrameCounter++; } // In pause, the only thing that needs update in the session is the character and third person spectator. // This is a terrible hack and should be done more systematically else { if (CameraController is MyThirdPersonSpectator) { (CameraController as MyThirdPersonSpectator).UpdateAfterSimulation(); } MyCharacter character = LocalCharacter; if (character != null) { // We can't call UpdateAfterSimulation, because it would update the acceleration and speed of the character //character.UpdateAfterSimulation(); character.UpdateBeforeSimulation(); } } UpdateStatistics(ref elapsedTimespan); DebugDraw(); }
/// <summary> /// Initializes a new instance of the <see cref="MySession"/> class. /// </summary> private MySession(MySyncLayer syncLayer, bool registerComponents = true) { Debug.Assert(syncLayer != null); if (syncLayer == null) MyLog.Default.WriteLine("MySession.Static.MySession() - sync layer is null"); SyncLayer = syncLayer; ElapsedGameTime = new TimeSpan(); // To reset spectator positions Spectator.Reset(); m_timeOfSave = MyTimeSpan.Zero; ElapsedGameTime = new TimeSpan(); Ready = false; MultiplayerLastMsg = 0; MultiplayerAlive = true; MultiplayerDirect = true; AppVersionFromSave = MyFinalBuildConstants.APP_VERSION; Factions.FactionStateChanged += OnFactionsStateChanged; ScriptManager = new MyScriptManager(); GC.Collect(2, GCCollectionMode.Forced); MySandboxGame.Log.WriteLine(String.Format("GC Memory: {0} B", GC.GetTotalMemory(false).ToString("##,#"))); MySandboxGame.Log.WriteLine(String.Format("Process Memory: {0} B", Process.GetCurrentProcess().PrivateMemorySize64.ToString("##,#"))); this.GameFocusManager = new MyGameFocusManager(); }
/// <summary> /// Log timing of currently set GPU block /// </summary> public static void EndGPUBlock(MyTimeSpan time) { MySimpleProfilingBlock block; if (m_GPUBlock != null && m_profilingBlocks.TryGetValue(m_GPUBlock, out block)) { block.Time += time; block.TotalTime += time; block.Frames++; m_GPUBlock = null; } }
public bool ShouldRemove(MyTimeSpan currentTime, MyTimeSpan maxSleep) { return IsSleeping && (currentTime - SleepTime) > maxSleep; }
public static void InfinarioUpdate(MyTimeSpan updateTime) { if (updateTime.Seconds - 60f >= m_lastMinuteUpdate) { m_lastMinuteUpdate = (float)updateTime.Seconds; ReportServerStatus(); } }
public static void SetViewMatrix(MatrixD value, MyTimeSpan? updateTime) { if (MyRender.Settings.EnableCameraInterpolation && updateTime.HasValue) { var world = MatrixD.Invert(value); m_interpolation.AddSample(ref world, updateTime.Value); MatrixD worldOut; float i = m_interpolation.Interpolate(MyRender.InterpolationTime, out worldOut); m_viewMatrix = MatrixD.Invert(worldOut); MyRenderStats.Generic.Write("Camera interpolator", i, MyStatTypeEnum.Max, 250, 2); SetPosition(worldOut.Translation); } else { m_viewMatrix = value; MatrixD invertedViewMatrix; MatrixD.Invert(ref m_viewMatrix, out invertedViewMatrix); SetPosition(invertedViewMatrix.Translation); } InversePositionTranslationMatrix = MatrixD.CreateTranslation(-Position); }
public void Clear() { m_queue.Clear(); m_lastTimeStamp = MyTimeSpan.Zero; }
public MyShipConnector() { #if XB1 // XB1_SYNC_NOREFLECTION ThrowOut = SyncType.CreateAndAddProp<bool>(); CollectAll = SyncType.CreateAndAddProp<bool>(); Strength = SyncType.CreateAndAddProp<float>(); m_connectionState = SyncType.CreateAndAddProp<State>(); #endif // XB1 CreateTerminalControls(); m_connectionState.ValueChanged += (o) => OnConnectionStateChanged(); m_connectionState.ValidateNever(); // Never set by client m_manualDisconnectTime = new MyTimeSpan(-DisconnectSleepTime.Ticks); Strength.Validate = (o) => Strength >= 0 && Strength <= 1; }
public void TryDisconnect() { if (InConstraint && Connected) { m_manualDisconnectTime = m_other.m_manualDisconnectTime = MySandboxGame.Static.UpdateTime; if (Sync.IsServer) { Detach(); } else { MyMultiplayer.RaiseEvent(this, x => x.TryDisconnect); } } }
public void ProfileCustomValue(string name, float value, MyTimeSpan? customTime = null, string timeFormat = null, string valueFormat = null, [CallerMemberName] string member = "", [CallerLineNumber] int line = 0, [CallerFilePath] string file = "") { if (m_levelLimit != -1) { return; } ThreadProfiler.ProfileCustomValue(name, member, line, file, value, customTime, timeFormat, valueFormat); }
public void Log(MyTimeSpan currentTime) { Time += currentTime - TimeStamp; TotalTime += currentTime - TimeStamp; }
public Item(T userdata, MyTimeSpan timespan) { Userdata = userdata; Timestamp = timespan; }
private void UpdateInternal() { using (Stats.Generic.Measure("BeforeUpdate")) { ProfilerShort.Begin("BeforeUpdate"); VRageRender.MyRenderProxy.BeforeUpdate(); ProfilerShort.End(); } ProfilerShort.Begin("Update"); //VRage.Trace.MyTrace.Send(VRage.Trace.TraceWindow.Default, "Update Start"); m_updateTime = m_gameTimer.Elapsed; Update(); ProfilerShort.End(); if (!IsDedicated) { ProfilerShort.Begin("PrepareForDraw"); PrepareForDraw(); ProfilerShort.End(); } using (Stats.Generic.Measure("AfterUpdate")) { ProfilerShort.Begin("AfterUpdate"); VRageRender.MyRenderProxy.AfterUpdate(m_updateTime); ProfilerShort.End(); } //VRage.Trace.MyTrace.Send(VRage.Trace.TraceWindow.Default, "Update End"); }
// TODO: OP! make time mandatory public static void BeforeRender(MyTimeSpan? currentDrawTime) { m_render.SharedData.BeforeRender(m_render.Settings, currentDrawTime); }
// TODO: OP! make time mandatory public static void AfterUpdate(MyTimeSpan? updateTimestamp) { if (m_render.SharedData != null) m_render.SharedData.AfterUpdate(MyRenderProxy.Settings, updateTimestamp); }
public static void End(float customValue = 0, MyTimeSpan? customTime = null, string timeFormat = null, string valueFormat = null, [CallerMemberName] string member = "", [CallerLineNumber] int line = 0, [CallerFilePath] string file = "") { MyRenderProxy.GetRenderProfiler().EndProfilingBlock(customValue, customTime, timeFormat, valueFormat, member, line, file); }
public void GPU_EndProfilingBlock(float customValue = 0, MyTimeSpan? customTime = null, string timeFormat = null, string valueFormat = null, [CallerMemberName] string member = "", [CallerLineNumber] int line = 0, [CallerFilePath] string file = "") { GpuProfiler.EndBlock(member, line, file, customTime, customValue, timeFormat, valueFormat); }
public static void CustomValue(string name, float value, MyTimeSpan? customTime, string timeFormat = null, string valueFormat = null, [CallerMemberName] string member = "", [CallerLineNumber] int line = 0, [CallerFilePath] string file = "") { MyRenderProxy.GetRenderProfiler().ProfileCustomValue(name, value, customTime, timeFormat, valueFormat, member, line, file); }
void IMySession.Update(MyTimeSpan time) { Update(time); }
public void AddElapsed(MyTimeSpan timespan) { m_startTicks -= timespan.Ticks; }