From 86a2612b58bd86254fbfdfaa236ca3eb39c230f0 Mon Sep 17 00:00:00 2001 From: Oliver-makes-code Date: Mon, 22 Jan 2024 11:50:43 -0600 Subject: [PATCH] Add some profiler stats --- Client/Keybinding/Keybinds.cs | 9 +- Client/Rendering/CameraStateManager.cs | 7 +- Client/Rendering/GameRenderer.cs | 2 +- .../Entity/ControlledClientPlayerEntity.cs | 88 ++++++++++--------- Core/Game.cs | 2 +- 5 files changed, 59 insertions(+), 49 deletions(-) diff --git a/Client/Keybinding/Keybinds.cs b/Client/Keybinding/Keybinds.cs index 83bf4fa..ef77721 100644 --- a/Client/Keybinding/Keybinds.cs +++ b/Client/Keybinding/Keybinds.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Veldrid; +using Voxel.Common.Util.Profiling; using Voxel.Core.Input.Gamepad; using VMouseButton = Veldrid.MouseButton; @@ -112,8 +113,12 @@ public static void WriteToConfig() { ClientConfig.keybindings = keybinds; } + private static readonly Profiler.ProfilerKey BindKey = Profiler.GetProfilerKey("Poll Keybinds"); + public static void Poll() { - foreach (var bind in Keybindings.Values) - bind.Poll(); + using (BindKey.Push()) { + foreach (var bind in Keybindings.Values) + bind.Poll(); + } } } diff --git a/Client/Rendering/CameraStateManager.cs b/Client/Rendering/CameraStateManager.cs index 4dd5de4..8f5bfad 100644 --- a/Client/Rendering/CameraStateManager.cs +++ b/Client/Rendering/CameraStateManager.cs @@ -40,12 +40,13 @@ public CameraStateManager(RenderSystem system) { )); } - public void SetToCamera(Camera c, double timeSinceLastTick) { + public void SetToCamera(Camera c) { c.UpdateFrustum(); currentCameraPosition = c.position; - var data = new CameraData(); - data.VPMatrix = ((quat)c.rotationVec.RotationVecToQuat()).ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed; + var data = new CameraData { + VPMatrix = ((quat)c.rotationVec.RotationVecToQuat()).ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed + }; CameraBuffer.value = data; } diff --git a/Client/Rendering/GameRenderer.cs b/Client/Rendering/GameRenderer.cs index 9319968..1348bab 100644 --- a/Client/Rendering/GameRenderer.cs +++ b/Client/Rendering/GameRenderer.cs @@ -73,7 +73,7 @@ public override void Render(double delta) { MainCamera.position = Client.PlayerEntity?.SmoothPosition(Client.smoothFactor) + Client.PlayerEntity?.eyeOffset ?? dvec3.Zero; MainCamera.rotationVec = Client.PlayerEntity?.rotation ?? dvec2.Zero; - CameraStateManager.SetToCamera(MainCamera, Client.timeSinceLastTick); + CameraStateManager.SetToCamera(MainCamera); WorldRenderer.Render(delta); GuiRenderer.Render(delta); diff --git a/Client/World/Entity/ControlledClientPlayerEntity.cs b/Client/World/Entity/ControlledClientPlayerEntity.cs index a2a8af3..e7574a7 100644 --- a/Client/World/Entity/ControlledClientPlayerEntity.cs +++ b/Client/World/Entity/ControlledClientPlayerEntity.cs @@ -6,11 +6,13 @@ using Voxel.Common.Network.Packets.C2S.Gameplay.Actions; using Voxel.Common.Network.Packets.Utils; using Voxel.Common.Util; +using Voxel.Common.Util.Profiling; using Voxel.Core.Util; namespace Voxel.Client.World.Entity; public class ControlledClientPlayerEntity : ClientPlayerEntity { + private static readonly Profiler.ProfilerKey PlayerKey = Profiler.GetProfilerKey("Update Player Input"); private dvec3 vel; @@ -23,63 +25,65 @@ public ControlledClientPlayerEntity() { } public void Update(double delta) { - var movement = Keybinds.Move.axis; - var looking = -Keybinds.Look.axis; + using (PlayerKey.Push()) { + var movement = Keybinds.Move.axis; + var looking = -Keybinds.Look.axis; - movement += new dvec2(0, -1) * Keybinds.Forward.strength; - movement += new dvec2(0, 1) * Keybinds.Backward.strength; - movement += new dvec2(-1, 0) * Keybinds.StrafeLeft.strength; - movement += new dvec2(1, 0) * Keybinds.StrafeRight.strength; + movement += new dvec2(0, -1) * Keybinds.Forward.strength; + movement += new dvec2(0, 1) * Keybinds.Backward.strength; + movement += new dvec2(-1, 0) * Keybinds.StrafeLeft.strength; + movement += new dvec2(1, 0) * Keybinds.StrafeRight.strength; - if (Keybinds.LookLeft.isPressed || Keybinds.LookRight.isPressed) - cameraPanTimers.x += (float)delta * MathF.Abs((float)(Keybinds.LookLeft.strength - Keybinds.LookRight.strength)); - else - cameraPanTimers.x = 0; - if (Keybinds.LookUp.isPressed || Keybinds.LookDown.isPressed) - cameraPanTimers.y += (float)delta * MathF.Abs((float)(Keybinds.LookUp.strength - Keybinds.LookDown.strength)); - else - cameraPanTimers.y = 0; + if (Keybinds.LookLeft.isPressed || Keybinds.LookRight.isPressed) + cameraPanTimers.x += (float)delta * MathF.Abs((float)(Keybinds.LookLeft.strength - Keybinds.LookRight.strength)); + else + cameraPanTimers.x = 0; + if (Keybinds.LookUp.isPressed || Keybinds.LookDown.isPressed) + cameraPanTimers.y += (float)delta * MathF.Abs((float)(Keybinds.LookUp.strength - Keybinds.LookDown.strength)); + else + cameraPanTimers.y = 0; - looking += new dvec2(Keybinds.LookLeft.strength - Keybinds.LookRight.strength, Keybinds.LookUp.strength - Keybinds.LookDown.strength) * new dvec2(cameraPanEase.F(cameraPanTimers.x), cameraPanEase.F(cameraPanTimers.y)); + looking += new dvec2(Keybinds.LookLeft.strength - Keybinds.LookRight.strength, Keybinds.LookUp.strength - Keybinds.LookDown.strength) * new dvec2(cameraPanEase.F(cameraPanTimers.x), cameraPanEase.F(cameraPanTimers.y)); - if (movement.LengthSqr > 1) - movement = movement.Normalized; + if (movement.LengthSqr > 1) + movement = movement.Normalized; - var movement3d = new dvec3(movement.x, 0, movement.y); + var movement3d = new dvec3(movement.x, 0, movement.y); - // movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength; - // movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength; + // movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength; + // movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength; - rotation += new dvec2((float)(looking.y * delta) * 1, (float)(looking.x * delta) * 1); - if (VoxelClient.isMouseCapruted) - rotation += VoxelClient.Instance.InputManager.MouseDelta.swizzle.yx * -1 / 192; + rotation += new dvec2((float)(looking.y * delta) * 1, (float)(looking.x * delta) * 1); + if (VoxelClient.isMouseCapruted) + rotation += VoxelClient.Instance.InputManager.MouseDelta.swizzle.yx * -1 / 192; - if (rotation.x < -MathF.PI/2) - rotation.x = -MathF.PI/2; - if (rotation.x > MathF.PI/2) - rotation.x = MathF.PI/2; + if (rotation.x < -MathF.PI/2) + rotation.x = -MathF.PI/2; + if (rotation.x > MathF.PI/2) + rotation.x = MathF.PI/2; - movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d * 4; - var localVel = dvec2.Lerp(velocity.xz, movement3d.xz, 0.9); - velocity = velocity.WithXZ(localVel); + movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d * 4; + var localVel = dvec2.Lerp(velocity.xz, movement3d.xz, 0.9); + velocity = velocity.WithXZ(localVel); - if (Keybinds.Jump.isPressed) - Jump(); + if (Keybinds.Jump.isPressed) + Jump(); - if (Keybinds.Crouch.justPressed) - position -= new dvec3(0, 1, 0); + if (Keybinds.Crouch.justPressed) + position -= new dvec3(0, 1, 0); - var transformUpdate = PacketPool.GetPacket(); - transformUpdate.Position = position; - transformUpdate.Rotation = rotation; - VoxelClient.Instance.connection!.SendPacket(transformUpdate); + var transformUpdate = PacketPool.GetPacket(); + transformUpdate.Position = position; + transformUpdate.Rotation = rotation; + VoxelClient.Instance.connection!.SendPacket(transformUpdate); - if (Keybinds.Attack.justPressed) - BreakBlock(); + if (Keybinds.Attack.justPressed) + BreakBlock(); - if (Keybinds.Use.justPressed) - PlaceBlock(); + if (Keybinds.Use.justPressed) + PlaceBlock(); + } } private void BreakBlock() { diff --git a/Core/Game.cs b/Core/Game.cs index f38eb86..48ddaf0 100644 --- a/Core/Game.cs +++ b/Core/Game.cs @@ -43,7 +43,7 @@ public void Run(int tps = 20, string windowTitle = "Game") { var gdo = new GraphicsDeviceOptions { PreferDepthRangeZeroToOne = true, PreferStandardClipSpaceYDirection = true, - SyncToVerticalBlank = true, + SyncToVerticalBlank = false, }; VeldridStartup.CreateWindowAndGraphicsDevice(wci, gdo, GraphicsBackend.Vulkan, out var nw, out var gd);