From 916962da6cdf3a03dcc429b29d03dc741f21423d Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 13 Dec 2022 00:34:16 -0800 Subject: [PATCH] garbage collection optimizations --- src/Graphics/GraphicsDevice.cs | 67 ++++++++++++++++++++++++++++++++++ src/Input/Keyboard.cs | 13 ++++--- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/Graphics/GraphicsDevice.cs b/src/Graphics/GraphicsDevice.cs index 97fd954..e79cae4 100644 --- a/src/Graphics/GraphicsDevice.cs +++ b/src/Graphics/GraphicsDevice.cs @@ -101,6 +101,73 @@ namespace MoonWorks.Graphics return new CommandBuffer(this, Refresh.Refresh_AcquireCommandBuffer(Handle, 0)); } + public unsafe void Submit(CommandBuffer commandBuffer) + { + var commandBufferPtrs = stackalloc IntPtr[1]; + + commandBufferPtrs[0] = commandBuffer.Handle; + + Refresh.Refresh_Submit( + Handle, + 1, + (IntPtr) commandBufferPtrs + ); + } + + public unsafe void Submit( + CommandBuffer commandBufferOne, + CommandBuffer commandBufferTwo + ) { + var commandBufferPtrs = stackalloc IntPtr[2]; + + commandBufferPtrs[0] = commandBufferOne.Handle; + commandBufferPtrs[1] = commandBufferTwo.Handle; + + Refresh.Refresh_Submit( + Handle, + 2, + (IntPtr) commandBufferPtrs + ); + } + + public unsafe void Submit( + CommandBuffer commandBufferOne, + CommandBuffer commandBufferTwo, + CommandBuffer commandBufferThree + ) { + var commandBufferPtrs = stackalloc IntPtr[3]; + + commandBufferPtrs[0] = commandBufferOne.Handle; + commandBufferPtrs[1] = commandBufferTwo.Handle; + commandBufferPtrs[2] = commandBufferThree.Handle; + + Refresh.Refresh_Submit( + Handle, + 3, + (IntPtr) commandBufferPtrs + ); + } + + public unsafe void Submit( + CommandBuffer commandBufferOne, + CommandBuffer commandBufferTwo, + CommandBuffer commandBufferThree, + CommandBuffer commandBufferFour + ) { + var commandBufferPtrs = stackalloc IntPtr[4]; + + commandBufferPtrs[0] = commandBufferOne.Handle; + commandBufferPtrs[1] = commandBufferTwo.Handle; + commandBufferPtrs[2] = commandBufferThree.Handle; + commandBufferPtrs[3] = commandBufferFour.Handle; + + Refresh.Refresh_Submit( + Handle, + 4, + (IntPtr) commandBufferPtrs + ); + } + public unsafe void Submit(params CommandBuffer[] commandBuffers) { var commandBufferPtrs = stackalloc IntPtr[commandBuffers.Length]; diff --git a/src/Input/Keyboard.cs b/src/Input/Keyboard.cs index 015d58a..72e3433 100644 --- a/src/Input/Keyboard.cs +++ b/src/Input/Keyboard.cs @@ -11,6 +11,7 @@ namespace MoonWorks.Input public IntPtr State { get; private set; } + private KeyCode[] KeyCodes; private KeyboardButton[] Keys { get; } private int numKeys; @@ -40,8 +41,10 @@ namespace MoonWorks.Input { SDL.SDL_GetKeyboardState(out numKeys); + KeyCodes = Enum.GetValues(); Keys = new KeyboardButton[numKeys]; - foreach (KeyCode keycode in Enum.GetValues(typeof(KeyCode))) + + foreach (KeyCode keycode in KeyCodes) { Keys[(int) keycode] = new KeyboardButton(this, keycode); } @@ -53,18 +56,18 @@ namespace MoonWorks.Input State = SDL.SDL_GetKeyboardState(out _); - foreach (int keycode in Enum.GetValues(typeof(KeyCode))) + foreach (KeyCode keycode in KeyCodes) { - var button = Keys[keycode]; + var button = Keys[(int) keycode]; button.Update(); if (button.IsPressed) { - if (TextInputBindings.TryGetValue((KeyCode) keycode, out var textIndex)) + if (TextInputBindings.TryGetValue(keycode, out var textIndex)) { Inputs.OnTextInput(TextInputCharacters[(textIndex)]); } - else if (IsDown(KeyCode.LeftControl) && (KeyCode) keycode == KeyCode.V) + else if (IsDown(KeyCode.LeftControl) && keycode == KeyCode.V) { Inputs.OnTextInput(TextInputCharacters[6]); }