From 9df9aaeb3a7f99ef4f269437f2e32ef6e0daab47 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 14 Nov 2021 21:08:02 -0800 Subject: [PATCH] update texture SetData API --- lib/RefreshCS | 2 +- src/Graphics/CommandBuffer.cs | 60 ++++++++++++++++++++++++++ src/Graphics/Resources/Texture.cs | 71 ++++++------------------------- 3 files changed, 74 insertions(+), 59 deletions(-) diff --git a/lib/RefreshCS b/lib/RefreshCS index a44d2cbe..1befeda8 160000 --- a/lib/RefreshCS +++ b/lib/RefreshCS @@ -1 +1 @@ -Subproject commit a44d2cbe6331b7b3c4d66e1e4fc37a1be7e30638 +Subproject commit 1befeda8f51e9104de127e1480985771d4a4b380 diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index 33045b3a..ade8a06d 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using MoonWorks.Math; using RefreshCS; @@ -610,6 +611,65 @@ namespace MoonWorks.Graphics ); } + /// + /// Asynchronously copies data into a texture. + /// + /// The texture slice to copy into. + /// A pointer to an array of data to copy from. + /// The amount of data to copy from the array. + public void SetTextureData(in TextureSlice textureSlice, IntPtr dataPtr, uint dataLengthInBytes) + { + Refresh.Refresh_SetTextureData( + Device.Handle, + Handle, + textureSlice.ToRefreshTextureSlice(), + dataPtr, + dataLengthInBytes + ); + } + + /// + /// Asynchronously copies data into a texture. + /// This variant copies into the entire texture. + /// + /// A pointer to an array of data to copy from. + /// The amount of data to copy from the array. + public void SetTextureData(Texture texture, IntPtr dataPtr, uint dataLengthInBytes) + { + SetTextureData(new TextureSlice(texture), dataPtr, dataLengthInBytes); + } + + /// + /// Asynchronously copies data into the texture. + /// + /// The texture slice to copy into. + /// An array of data to copy into the texture. + public unsafe void SetTextureData(in TextureSlice textureSlice, T[] data) where T : unmanaged + { + var size = Marshal.SizeOf(); + + fixed (T* ptr = &data[0]) + { + Refresh.Refresh_SetTextureData( + Device.Handle, + Handle, + textureSlice.ToRefreshTextureSlice(), + (IntPtr) ptr, + (uint) (data.Length * size) + ); + } + } + + /// + /// Asynchronously copies data into a texture. + /// This variant copies data into the entire texture. + /// + /// An array of data to copy into the texture. + public unsafe void SetTextureData(Texture texture, T[] data) where T : unmanaged + { + SetTextureData(new TextureSlice(texture), data); + } + /// /// Performs an asynchronous texture-to-texture copy on the GPU. /// diff --git a/src/Graphics/Resources/Texture.cs b/src/Graphics/Resources/Texture.cs index 6c0009a5..469d1541 100644 --- a/src/Graphics/Resources/Texture.cs +++ b/src/Graphics/Resources/Texture.cs @@ -16,7 +16,15 @@ namespace MoonWorks.Graphics protected override Action QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture; - public static Texture LoadPNG(GraphicsDevice device, string filePath) + /// + /// Loads a PNG from a file path. + /// NOTE: You can queue as many of these as you want on to a command buffer but it MUST be submitted! + /// + /// + /// + /// + /// + public static Texture LoadPNG(GraphicsDevice device, CommandBuffer commandBuffer, string filePath) { var pixels = Refresh.Refresh_Image_Load( filePath, @@ -25,6 +33,8 @@ namespace MoonWorks.Graphics out var channels ); + var byteCount = (uint)(width * height * channels); + TextureCreateInfo textureCreateInfo; textureCreateInfo.Width = (uint)width; textureCreateInfo.Height = (uint)height; @@ -36,10 +46,10 @@ namespace MoonWorks.Graphics textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler; var texture = new Texture(device, textureCreateInfo); - - texture.SetData(pixels, (uint)(width * height * 4)); + commandBuffer.SetTextureData(texture, pixels, byteCount); Refresh.Refresh_Image_Free(pixels); + return texture; } @@ -173,61 +183,6 @@ namespace MoonWorks.Graphics Height = textureCreateInfo.Height; } - /// - /// Asynchronously copies data into the texture. - /// - /// The texture slice to copy into. - /// A pointer to an array of data to copy from. - /// The amount of data to copy from the array. - public void SetData(in TextureSlice textureSlice, IntPtr dataPtr, uint dataLengthInBytes) - { - Refresh.Refresh_SetTextureData( - Device.Handle, - textureSlice.ToRefreshTextureSlice(), - dataPtr, - dataLengthInBytes - ); - } - /// - /// Asynchronously copies data into the texture. - /// This variant copies into the entire texture. - /// - /// A pointer to an array of data to copy from. - /// The amount of data to copy from the array. - public void SetData(IntPtr dataPtr, uint dataLengthInBytes) - { - SetData(new TextureSlice(this), dataPtr, dataLengthInBytes); - } - - /// - /// Asynchronously copies data into the texture. - /// - /// The texture slice to copy into. - /// An array of data to copy into the texture. - public unsafe void SetData(in TextureSlice textureSlice, T[] data) where T : unmanaged - { - var size = Marshal.SizeOf(); - - fixed (T* ptr = &data[0]) - { - Refresh.Refresh_SetTextureData( - Device.Handle, - textureSlice.ToRefreshTextureSlice(), - (IntPtr) ptr, - (uint) (data.Length * size) - ); - } - } - - /// - /// Asynchronously copies data into the texture. - /// This variant copies data into the entire texture. - /// - /// An array of data to copy into the texture. - public unsafe void SetData(T[] data) where T : unmanaged - { - SetData(new TextureSlice(this), data); - } } }