load images from memory + QOI support

remotes/1695061714407202320/main
cosmonaut 2023-04-03 17:28:00 -07:00
parent 8209051a3c
commit 8134761e44
2 changed files with 149 additions and 6 deletions

@ -1 +1 @@
Subproject commit 10f54d6b034d685d314233f9507a4bbac7f235b4
Subproject commit fb5fee5f568bbb9f42920f0fcdd9f85024f5bf00

View File

@ -28,10 +28,13 @@ namespace MoonWorks.Graphics
/// <param name="device"></param>
/// <param name="commandBuffer"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public static Texture LoadPNG(GraphicsDevice device, CommandBuffer commandBuffer, string filePath)
{
var pixels = Refresh.Refresh_Image_Load(
/// <returns>A Texture object.</returns>
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;
}
/// <summary>
/// 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!
/// </summary>
/// <param name="device"></param>
/// <param name="commandBuffer"></param>
/// <param name="data"></param>
/// <returns>A Texture object.</returns>
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
}
}
/// <summary>
/// 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!
/// </summary>
/// <param name="device"></param>
/// <param name="commandBuffer"></param>
/// <param name="filePath"></param>
/// <returns>A Texture object.</returns>
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;
}
/// <summary>
/// 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!
/// </summary>
/// <param name="device"></param>
/// <param name="commandBuffer"></param>
/// <param name="filePath"></param>
/// <returns>A Texture object.</returns>
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))