update tests for new image loader API

pull/2/head
cosmonaut 2023-04-18 23:39:45 -07:00
parent a2d1fea543
commit c84668b4b3
8 changed files with 52 additions and 75 deletions

View File

@ -9,6 +9,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x64</Platforms> <Platforms>x64</Platforms>
</PropertyGroup> </PropertyGroup>

View File

@ -1,4 +1,5 @@
using MoonWorks; using System.Runtime.InteropServices;
using MoonWorks;
using MoonWorks.Graphics; using MoonWorks.Graphics;
using MoonWorks.Math.Float; using MoonWorks.Math.Float;
@ -14,7 +15,7 @@ namespace MoonWorks.Test
private Texture textureSmallCopy; private Texture textureSmallCopy;
private Sampler sampler; 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 // Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadVert")); ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadVert"));
@ -69,16 +70,22 @@ namespace MoonWorks.Test
} }
); );
// Load the texture. Copy-pasted from Texture.LoadPNG, // Load the texture. Storing the texture bytes so we can compare them.
// but with the texture bytes stored. var fileStream = new System.IO.FileStream(TestUtils.GetTexturePath("ravioli.png"), System.IO.FileMode.Open, System.IO.FileAccess.Read);
var pixels = RefreshCS.Refresh.Refresh_Image_LoadPNGFromFile( var fileLength = fileStream.Length;
TestUtils.GetTexturePath("ravioli.png"), var fileBuffer = NativeMemory.Alloc((nuint) fileLength);
out var width, var fileSpan = new System.Span<byte>(fileBuffer, (int) fileLength);
out var height, fileStream.ReadExactly(fileSpan);
out var channels
);
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 textureCreateInfo = new TextureCreateInfo();
textureCreateInfo.Width = (uint) width; textureCreateInfo.Width = (uint) width;
@ -90,12 +97,7 @@ namespace MoonWorks.Test
textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler; textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler;
originalTexture = new Texture(GraphicsDevice, textureCreateInfo); originalTexture = new Texture(GraphicsDevice, textureCreateInfo);
cmdbuf.SetTextureData(originalTexture, pixels, byteCount); cmdbuf.SetTextureData(originalTexture, pixels, (uint) byteCount);
byte[] textureBytes = new byte[byteCount];
System.Runtime.InteropServices.Marshal.Copy(pixels, textureBytes, 0, (int) byteCount);
RefreshCS.Refresh.Refresh_Image_FreePNG(pixels);
// Create a 1:1 copy of the texture // Create a 1:1 copy of the texture
textureCopy = new Texture(GraphicsDevice, textureCreateInfo); textureCopy = new Texture(GraphicsDevice, textureCreateInfo);
@ -122,18 +124,18 @@ namespace MoonWorks.Test
); );
// Copy the texture to a buffer // Copy the texture to a buffer
Buffer compareBuffer = Buffer.Create<byte>(GraphicsDevice, 0, (uint) textureBytes.Length); Buffer compareBuffer = Buffer.Create<byte>(GraphicsDevice, 0, (uint) byteCount);
cmdbuf.CopyTextureToBuffer(new TextureSlice(originalTexture), compareBuffer); cmdbuf.CopyTextureToBuffer(new TextureSlice(originalTexture), compareBuffer);
GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Submit(cmdbuf);
GraphicsDevice.Wait(); GraphicsDevice.Wait();
// Compare the original bytes to the copied bytes. // Compare the original bytes to the copied bytes.
byte[] copiedBytes = new byte[textureBytes.Length]; var copiedBytes = NativeMemory.Alloc((nuint) byteCount);
compareBuffer.GetData(copiedBytes); var copiedSpan = new System.Span<byte>(copiedBytes, byteCount);
compareBuffer.GetData(copiedSpan);
var originalSpan = new System.ReadOnlySpan<byte>(textureBytes); var originalSpan = new System.Span<byte>((void*) pixels, byteCount);
var copiedSpan = new System.ReadOnlySpan<byte>(copiedBytes);
if (System.MemoryExtensions.SequenceEqual(originalSpan, copiedSpan)) 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!"); 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) { } protected override void Update(System.TimeSpan delta) { }

View File

@ -47,28 +47,17 @@ namespace MoonWorks.Test
void LoadCubemap(CommandBuffer cmdbuf, string[] imagePaths) void LoadCubemap(CommandBuffer cmdbuf, string[] imagePaths)
{ {
System.IntPtr textureData;
int w, h, numChannels;
for (uint i = 0; i < imagePaths.Length; i++) for (uint i = 0; i < imagePaths.Length; i++)
{ {
textureData = RefreshCS.Refresh.Refresh_Image_LoadPNGFromFile( var textureSlice = new TextureSlice(
imagePaths[i], skyboxTexture,
out w, new Rect(0, 0, (int) skyboxTexture.Width, (int) skyboxTexture.Height),
out h, 0,
out numChannels i,
); 0
cmdbuf.SetTextureData( );
new TextureSlice(
skyboxTexture, Texture.SetDataFromFile(cmdbuf, textureSlice, imagePaths[i]);
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);
} }
} }

View File

@ -80,17 +80,11 @@ namespace MoonWorks.Test
i i
); );
var pixels = Refresh.Refresh_Image_LoadPNGFromFile( Texture.SetDataFromFile(
TestUtils.GetTexturePath($"tex3d_{i}.png"), cmdbuf,
out var width, slice,
out var height, TestUtils.GetTexturePath($"tex3d_{i}.png")
out var channels );
);
var byteCount = (uint) (width * height * channels);
cmdbuf.SetTextureData(slice, pixels, byteCount);
Refresh.Refresh_Image_FreePNG(pixels);
} }
GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Submit(cmdbuf);

View File

@ -80,17 +80,7 @@ namespace MoonWorks.Test
(uint) i (uint) i
); );
var pixels = Refresh.Refresh_Image_LoadPNGFromFile( Texture.SetDataFromFile(cmdbuf, slice, TestUtils.GetTexturePath($"mip{i}.png"));
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);
} }
GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Submit(cmdbuf);

View File

@ -68,9 +68,8 @@ namespace MoonWorks.Test
0, 2, 3, 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.Submit(cmdbuf);
GraphicsDevice.Wait();
} }
protected override void Update(System.TimeSpan delta) protected override void Update(System.TimeSpan delta)

View File

@ -10,6 +10,7 @@
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Platforms>x64</Platforms> <Platforms>x64</Platforms>
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup> </PropertyGroup>
<Import Project="$(SolutionDir)NativeAOT_Console.targets" Condition="Exists('$(SolutionDir)NativeAOT_Console.targets')" /> <Import Project="$(SolutionDir)NativeAOT_Console.targets" Condition="Exists('$(SolutionDir)NativeAOT_Console.targets')" />

View File

@ -25,10 +25,10 @@ namespace MoonWorks.Test
private Texture[] textures = new Texture[4]; private Texture[] textures = new Texture[4];
private string[] imageLoadFormatNames = new string[] private string[] imageLoadFormatNames = new string[]
{ {
"PNGFromFile", "PNG from file",
"PNGFromMemory", "PNG from memory",
"QOIFromFile", "QOI from file",
"QOIFromMemory" "QOI from memory"
}; };
private int currentTextureIndex; private int currentTextureIndex;
@ -94,12 +94,11 @@ namespace MoonWorks.Test
0, 2, 3, 0, 2, 3,
} }
); );
textures[0] = Texture.LoadPNG(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.png")); textures[0] = Texture.FromFile(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.png"));
textures[1] = Texture.LoadPNG(GraphicsDevice, cmdbuf, pngBytes); textures[1] = Texture.FromData(GraphicsDevice, cmdbuf, pngBytes);
textures[2] = Texture.LoadQOI(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.qoi")); textures[2] = Texture.FromFile(GraphicsDevice, cmdbuf, TestUtils.GetTexturePath("ravioli.qoi"));
textures[3] = Texture.LoadQOI(GraphicsDevice, cmdbuf, qoiBytes); textures[3] = Texture.FromData(GraphicsDevice, cmdbuf, qoiBytes);
GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Submit(cmdbuf);
GraphicsDevice.Wait();
} }
protected override void Update(System.TimeSpan delta) protected override void Update(System.TimeSpan delta)