From 36e6c6f332cf9ff855c79fd5d1c870e055ad7bbb Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 13 Dec 2022 00:52:35 -0800 Subject: [PATCH] optimize AcquireSwapchainTexture --- src/Graphics/CommandBuffer.cs | 14 +++++++------- src/Graphics/GraphicsDevice.cs | 4 ++++ src/Graphics/GraphicsResource.cs | 2 +- src/Graphics/Resources/Texture.cs | 7 ++++--- src/Window.cs | 2 ++ 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index 5f9432d..90a3a6b 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1630,13 +1630,13 @@ namespace MoonWorks.Graphics return null; } - return new Texture( - Device, - texturePtr, - window.SwapchainFormat, - width, - height - ); + // Override the texture properties to avoid allocating a new texture instance! + window.SwapchainTexture.Handle = texturePtr; + window.SwapchainTexture.Width = width; + window.SwapchainTexture.Height = height; + window.SwapchainTexture.Format = window.SwapchainFormat; + + return window.SwapchainTexture; } /// diff --git a/src/Graphics/GraphicsDevice.cs b/src/Graphics/GraphicsDevice.cs index e79cae4..2e134df 100644 --- a/src/Graphics/GraphicsDevice.cs +++ b/src/Graphics/GraphicsDevice.cs @@ -73,6 +73,10 @@ namespace MoonWorks.Graphics { window.Claimed = true; window.SwapchainFormat = GetSwapchainFormat(window); + if (window.SwapchainTexture == null) + { + window.SwapchainTexture = new Texture(this, IntPtr.Zero, window.SwapchainFormat, 0, 0); + } } return success; diff --git a/src/Graphics/GraphicsResource.cs b/src/Graphics/GraphicsResource.cs index 27343df..2e68546 100644 --- a/src/Graphics/GraphicsResource.cs +++ b/src/Graphics/GraphicsResource.cs @@ -5,7 +5,7 @@ namespace MoonWorks.Graphics public abstract class GraphicsResource : IDisposable { public GraphicsDevice Device { get; } - public IntPtr Handle { get; protected set; } + public IntPtr Handle { get; internal set; } public bool IsDisposed { get; private set; } protected abstract Action QueueDestroyFunction { get; } diff --git a/src/Graphics/Resources/Texture.cs b/src/Graphics/Resources/Texture.cs index 124eaef..190ac53 100644 --- a/src/Graphics/Resources/Texture.cs +++ b/src/Graphics/Resources/Texture.cs @@ -9,14 +9,15 @@ namespace MoonWorks.Graphics /// public class Texture : GraphicsResource { - public uint Width { get; } - public uint Height { get; } + public uint Width { get; internal set; } + public uint Height { get; internal set; } public uint Depth { get; } - public TextureFormat Format { get; } + public TextureFormat Format { get; internal set; } public bool IsCube { get; } public uint LevelCount { get; } public TextureUsageFlags UsageFlags { get; } + // FIXME: this allocates a delegate instance protected override Action QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture; /// diff --git a/src/Window.cs b/src/Window.cs index 65175d0..194f7d0 100644 --- a/src/Window.cs +++ b/src/Window.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using MoonWorks.Graphics; using SDL2; namespace MoonWorks @@ -10,6 +11,7 @@ namespace MoonWorks public ScreenMode ScreenMode { get; private set; } public uint Width { get; private set; } public uint Height { get; private set; } + internal Texture SwapchainTexture { get; set; } = null; public bool Claimed { get; internal set; } public MoonWorks.Graphics.TextureFormat SwapchainFormat { get; internal set; }