diff --git a/CopyTexture/CopyTexture.csproj b/CopyTexture/CopyTexture.csproj index 2868e63..1c37153 100644 --- a/CopyTexture/CopyTexture.csproj +++ b/CopyTexture/CopyTexture.csproj @@ -9,6 +9,7 @@ Exe net7.0 enable + true x64 diff --git a/CopyTexture/CopyTextureGame.cs b/CopyTexture/CopyTextureGame.cs index 1049bb8..7cea2af 100644 --- a/CopyTexture/CopyTextureGame.cs +++ b/CopyTexture/CopyTextureGame.cs @@ -1,4 +1,5 @@ -using MoonWorks; +using System.Runtime.InteropServices; +using MoonWorks; using MoonWorks.Graphics; using MoonWorks.Math.Float; @@ -14,7 +15,7 @@ namespace MoonWorks.Test private Texture textureSmallCopy; private Sampler sampler; - public CopyTextureGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) + public unsafe CopyTextureGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { // Load the shaders ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadVert")); @@ -69,16 +70,22 @@ namespace MoonWorks.Test } ); - // Load the texture. Copy-pasted from Texture.LoadPNG, - // but with the texture bytes stored. - var pixels = RefreshCS.Refresh.Refresh_Image_LoadPNGFromFile( - TestUtils.GetTexturePath("ravioli.png"), - out var width, - out var height, - out var channels - ); + // Load the texture. Storing the texture bytes so we can compare them. + var fileStream = new System.IO.FileStream(TestUtils.GetTexturePath("ravioli.png"), System.IO.FileMode.Open, System.IO.FileAccess.Read); + var fileLength = fileStream.Length; + var fileBuffer = NativeMemory.Alloc((nuint) fileLength); + var fileSpan = new System.Span(fileBuffer, (int) fileLength); + fileStream.ReadExactly(fileSpan); - var byteCount = (uint)(width * height * channels); + var pixels = RefreshCS.Refresh.Refresh_Image_Load( + (nint) fileBuffer, + (int) fileLength, + out var width, + out var height, + out var byteCount + ); + + NativeMemory.Free(fileBuffer); TextureCreateInfo textureCreateInfo = new TextureCreateInfo(); textureCreateInfo.Width = (uint) width; @@ -90,12 +97,7 @@ namespace MoonWorks.Test textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler; originalTexture = new Texture(GraphicsDevice, textureCreateInfo); - cmdbuf.SetTextureData(originalTexture, pixels, byteCount); - - byte[] textureBytes = new byte[byteCount]; - System.Runtime.InteropServices.Marshal.Copy(pixels, textureBytes, 0, (int) byteCount); - - RefreshCS.Refresh.Refresh_Image_FreePNG(pixels); + cmdbuf.SetTextureData(originalTexture, pixels, (uint) byteCount); // Create a 1:1 copy of the texture textureCopy = new Texture(GraphicsDevice, textureCreateInfo); @@ -122,18 +124,18 @@ namespace MoonWorks.Test ); // Copy the texture to a buffer - Buffer compareBuffer = Buffer.Create(GraphicsDevice, 0, (uint) textureBytes.Length); + Buffer compareBuffer = Buffer.Create(GraphicsDevice, 0, (uint) byteCount); cmdbuf.CopyTextureToBuffer(new TextureSlice(originalTexture), compareBuffer); GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Wait(); // Compare the original bytes to the copied bytes. - byte[] copiedBytes = new byte[textureBytes.Length]; - compareBuffer.GetData(copiedBytes); + var copiedBytes = NativeMemory.Alloc((nuint) byteCount); + var copiedSpan = new System.Span(copiedBytes, byteCount); + compareBuffer.GetData(copiedSpan); - var originalSpan = new System.ReadOnlySpan(textureBytes); - var copiedSpan = new System.ReadOnlySpan(copiedBytes); + var originalSpan = new System.Span((void*) pixels, byteCount); if (System.MemoryExtensions.SequenceEqual(originalSpan, copiedSpan)) { @@ -144,6 +146,8 @@ namespace MoonWorks.Test { Logger.LogError("FAIL! Original texture bytes do not match bytes from CopyTextureToBuffer!"); } + + RefreshCS.Refresh.Refresh_Image_Free(pixels); } protected override void Update(System.TimeSpan delta) { } diff --git a/Cube/CubeGame.cs b/Cube/CubeGame.cs index 58b1e36..92743cc 100644 --- a/Cube/CubeGame.cs +++ b/Cube/CubeGame.cs @@ -47,28 +47,17 @@ namespace MoonWorks.Test void LoadCubemap(CommandBuffer cmdbuf, string[] imagePaths) { - System.IntPtr textureData; - int w, h, numChannels; - for (uint i = 0; i < imagePaths.Length; i++) { - textureData = RefreshCS.Refresh.Refresh_Image_LoadPNGFromFile( - imagePaths[i], - out w, - out h, - out numChannels - ); - cmdbuf.SetTextureData( - new TextureSlice( - skyboxTexture, - new Rect(0, 0, w, h), - 0, - i - ), - textureData, - (uint) (w * h * 4) // w * h * numChannels does not work - ); - RefreshCS.Refresh.Refresh_Image_FreePNG(textureData); + var textureSlice = new TextureSlice( + skyboxTexture, + new Rect(0, 0, (int) skyboxTexture.Width, (int) skyboxTexture.Height), + 0, + i, + 0 + ); + + Texture.SetDataFromFile(cmdbuf, textureSlice, imagePaths[i]); } } diff --git a/Texture3D/Texture3DGame.cs b/Texture3D/Texture3DGame.cs index f66f398..23bd361 100644 --- a/Texture3D/Texture3DGame.cs +++ b/Texture3D/Texture3DGame.cs @@ -80,17 +80,11 @@ namespace MoonWorks.Test i ); - var pixels = Refresh.Refresh_Image_LoadPNGFromFile( - TestUtils.GetTexturePath($"tex3d_{i}.png"), - out var width, - out var height, - out var channels - ); - - var byteCount = (uint) (width * height * channels); - cmdbuf.SetTextureData(slice, pixels, byteCount); - - Refresh.Refresh_Image_FreePNG(pixels); + Texture.SetDataFromFile( + cmdbuf, + slice, + TestUtils.GetTexturePath($"tex3d_{i}.png") + ); } GraphicsDevice.Submit(cmdbuf); diff --git a/TextureMipmaps/TextureMipmapsGame.cs b/TextureMipmaps/TextureMipmapsGame.cs index 6bbf675..fadff4c 100644 --- a/TextureMipmaps/TextureMipmapsGame.cs +++ b/TextureMipmaps/TextureMipmapsGame.cs @@ -80,17 +80,7 @@ namespace MoonWorks.Test (uint) i ); - var pixels = Refresh.Refresh_Image_LoadPNGFromFile( - TestUtils.GetTexturePath($"mip{i}.png"), - out var width, - out var height, - out var channels - ); - - var byteCount = (uint)(width * height * channels); - cmdbuf.SetTextureData(slice, pixels, byteCount); - - Refresh.Refresh_Image_FreePNG(pixels); + Texture.SetDataFromFile(cmdbuf, slice, TestUtils.GetTexturePath($"mip{i}.png")); } GraphicsDevice.Submit(cmdbuf); diff --git a/TexturedAnimatedQuad/TexturedAnimatedQuadGame.cs b/TexturedAnimatedQuad/TexturedAnimatedQuadGame.cs index e5135e1..60ed8d5 100644 --- a/TexturedAnimatedQuad/TexturedAnimatedQuadGame.cs +++ b/TexturedAnimatedQuad/TexturedAnimatedQuadGame.cs @@ -68,9 +68,8 @@ namespace MoonWorks.Test 0, 2, 3, } ); - texture = Texture.LoadPNG(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.png")); + texture = Texture.FromFile(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.png")); GraphicsDevice.Submit(cmdbuf); - GraphicsDevice.Wait(); } protected override void Update(System.TimeSpan delta) diff --git a/TexturedQuad/TexturedQuad.csproj b/TexturedQuad/TexturedQuad.csproj index 7becfeb..a1f01e2 100644 --- a/TexturedQuad/TexturedQuad.csproj +++ b/TexturedQuad/TexturedQuad.csproj @@ -10,6 +10,7 @@ net7.0 enable x64 + false diff --git a/TexturedQuad/TexturedQuadGame.cs b/TexturedQuad/TexturedQuadGame.cs index 9967318..0cc5111 100644 --- a/TexturedQuad/TexturedQuadGame.cs +++ b/TexturedQuad/TexturedQuadGame.cs @@ -25,10 +25,10 @@ namespace MoonWorks.Test private Texture[] textures = new Texture[4]; private string[] imageLoadFormatNames = new string[] { - "PNGFromFile", - "PNGFromMemory", - "QOIFromFile", - "QOIFromMemory" + "PNG from file", + "PNG from memory", + "QOI from file", + "QOI from memory" }; private int currentTextureIndex; @@ -94,12 +94,11 @@ namespace MoonWorks.Test 0, 2, 3, } ); - textures[0] = Texture.LoadPNG(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.png")); - textures[1] = Texture.LoadPNG(GraphicsDevice, cmdbuf, pngBytes); - textures[2] = Texture.LoadQOI(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.qoi")); - textures[3] = Texture.LoadQOI(GraphicsDevice, cmdbuf, qoiBytes); + textures[0] = Texture.FromFile(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.png")); + textures[1] = Texture.FromData(GraphicsDevice, cmdbuf, pngBytes); + textures[2] = Texture.FromFile(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.qoi")); + textures[3] = Texture.FromData(GraphicsDevice, cmdbuf, qoiBytes); GraphicsDevice.Submit(cmdbuf); - GraphicsDevice.Wait(); } protected override void Update(System.TimeSpan delta)