diff --git a/lib/RefreshCS b/lib/RefreshCS index 10f54d6b..fb5fee5f 160000 --- a/lib/RefreshCS +++ b/lib/RefreshCS @@ -1 +1 @@ -Subproject commit 10f54d6b034d685d314233f9507a4bbac7f235b4 +Subproject commit fb5fee5f568bbb9f42920f0fcdd9f85024f5bf00 diff --git a/src/Graphics/Resources/Texture.cs b/src/Graphics/Resources/Texture.cs index 48a57a1c..008b7617 100644 --- a/src/Graphics/Resources/Texture.cs +++ b/src/Graphics/Resources/Texture.cs @@ -28,10 +28,13 @@ namespace MoonWorks.Graphics /// /// /// - /// - public static Texture LoadPNG(GraphicsDevice device, CommandBuffer commandBuffer, string filePath) - { - var pixels = Refresh.Refresh_Image_Load( + /// A Texture object. + public static Texture LoadPNG( + GraphicsDevice device, + CommandBuffer commandBuffer, + string filePath + ) { + var pixels = Refresh.Refresh_Image_LoadPNGFromFile( filePath, out var width, out var height, @@ -53,7 +56,56 @@ namespace MoonWorks.Graphics var texture = new Texture(device, textureCreateInfo); commandBuffer.SetTextureData(texture, pixels, byteCount); - Refresh.Refresh_Image_Free(pixels); + Refresh.Refresh_Image_FreePNG(pixels); + + return texture; + } + + /// + /// Loads a PNG from a byte array. + /// NOTE: You can queue as many of these as you want on to a command buffer but it MUST be submitted! + /// + /// + /// + /// + /// A Texture object. + public unsafe static Texture LoadPNG( + GraphicsDevice device, + CommandBuffer commandBuffer, + byte[] data + ) { + IntPtr pixels; + int width, height, numChannels; + + fixed (byte* ptr = &data[0]) + { + pixels = Refresh.Refresh_Image_LoadPNGFromMemory( + (nint) ptr, + data.Length, + out width, + out height, + out numChannels + ); + } + + TextureCreateInfo textureCreateInfo = new TextureCreateInfo + { + Width = (uint) width, + Height = (uint) height, + Depth = 1, + Format = TextureFormat.R8G8B8A8, + IsCube = false, + LevelCount = 1, + SampleCount = SampleCount.One, + UsageFlags = TextureUsageFlags.Sampler + }; + + var byteCount = (uint) (width * height * numChannels); + + var texture = new Texture(device, textureCreateInfo); + commandBuffer.SetTextureData(texture, pixels, byteCount); + + Refresh.Refresh_Image_FreePNG(pixels); return texture; } @@ -74,6 +126,97 @@ namespace MoonWorks.Graphics } } + /// + /// Loads a QOI 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! + /// + /// + /// + /// + /// A Texture object. + public unsafe static Texture LoadQOI( + GraphicsDevice device, + CommandBuffer commandBuffer, + string filePath + ) { + var pixels = Refresh.Refresh_Image_LoadQOIFromFile( + filePath, + out var width, + out var height, + out var numChannels + ); + + var byteCount = (uint) (width * height * numChannels); + + TextureCreateInfo textureCreateInfo = new TextureCreateInfo + { + Width = (uint) width, + Height = (uint) height, + Depth = 1, + Format = TextureFormat.R8G8B8A8, + IsCube = false, + LevelCount = 1, + SampleCount = SampleCount.One, + UsageFlags = TextureUsageFlags.Sampler + }; + + var texture = new Texture(device, textureCreateInfo); + commandBuffer.SetTextureData(texture, pixels, byteCount); + + Refresh.Refresh_Image_FreeQOI(pixels); + + return texture; + } + + /// + /// Loads a QOI from a byte array. + /// NOTE: You can queue as many of these as you want on to a command buffer but it MUST be submitted! + /// + /// + /// + /// + /// A Texture object. + public unsafe static Texture LoadQOI( + GraphicsDevice device, + CommandBuffer commandBuffer, + byte[] data + ) { + IntPtr pixels; + int width, height, numChannels; + + fixed (byte* ptr = &data[0]) + { + pixels = Refresh.Refresh_Image_LoadQOIFromMemory( + (nint) ptr, + data.Length, + out width, + out height, + out numChannels + ); + } + + TextureCreateInfo textureCreateInfo = new TextureCreateInfo + { + Width = (uint) width, + Height = (uint) height, + Depth = 1, + Format = TextureFormat.R8G8B8A8, + IsCube = false, + LevelCount = 1, + SampleCount = SampleCount.One, + UsageFlags = TextureUsageFlags.Sampler + }; + + var byteCount = (uint) (width * height * numChannels); + + var texture = new Texture(device, textureCreateInfo); + commandBuffer.SetTextureData(texture, pixels, byteCount); + + Refresh.Refresh_Image_FreePNG(pixels); + + return texture; + } + public static Texture LoadDDS(GraphicsDevice graphicsDevice, CommandBuffer commandBuffer, System.IO.Stream stream) { using (var reader = new BinaryReader(stream))