update texture SetData API

pull/14/head
cosmonaut 2021-11-14 21:08:02 -08:00
parent e1a26e7c69
commit 9df9aaeb3a
3 changed files with 74 additions and 59 deletions

@ -1 +1 @@
Subproject commit a44d2cbe6331b7b3c4d66e1e4fc37a1be7e30638 Subproject commit 1befeda8f51e9104de127e1480985771d4a4b380

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Runtime.InteropServices;
using MoonWorks.Math; using MoonWorks.Math;
using RefreshCS; using RefreshCS;
@ -610,6 +611,65 @@ namespace MoonWorks.Graphics
); );
} }
/// <summary>
/// Asynchronously copies data into a texture.
/// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="dataPtr">A pointer to an array of data to copy from.</param>
/// <param name="dataLengthInBytes">The amount of data to copy from the array.</param>
public void SetTextureData(in TextureSlice textureSlice, IntPtr dataPtr, uint dataLengthInBytes)
{
Refresh.Refresh_SetTextureData(
Device.Handle,
Handle,
textureSlice.ToRefreshTextureSlice(),
dataPtr,
dataLengthInBytes
);
}
/// <summary>
/// Asynchronously copies data into a texture.
/// This variant copies into the entire texture.
/// </summary>
/// <param name="dataPtr">A pointer to an array of data to copy from.</param>
/// <param name="dataLengthInBytes">The amount of data to copy from the array.</param>
public void SetTextureData(Texture texture, IntPtr dataPtr, uint dataLengthInBytes)
{
SetTextureData(new TextureSlice(texture), dataPtr, dataLengthInBytes);
}
/// <summary>
/// Asynchronously copies data into the texture.
/// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(in TextureSlice textureSlice, T[] data) where T : unmanaged
{
var size = Marshal.SizeOf<T>();
fixed (T* ptr = &data[0])
{
Refresh.Refresh_SetTextureData(
Device.Handle,
Handle,
textureSlice.ToRefreshTextureSlice(),
(IntPtr) ptr,
(uint) (data.Length * size)
);
}
}
/// <summary>
/// Asynchronously copies data into a texture.
/// This variant copies data into the entire texture.
/// </summary>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(Texture texture, T[] data) where T : unmanaged
{
SetTextureData(new TextureSlice(texture), data);
}
/// <summary> /// <summary>
/// Performs an asynchronous texture-to-texture copy on the GPU. /// Performs an asynchronous texture-to-texture copy on the GPU.
/// </summary> /// </summary>

View File

@ -16,7 +16,15 @@ namespace MoonWorks.Graphics
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture; protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture;
public static Texture LoadPNG(GraphicsDevice device, string filePath) /// <summary>
/// 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!
/// </summary>
/// <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( var pixels = Refresh.Refresh_Image_Load(
filePath, filePath,
@ -25,6 +33,8 @@ namespace MoonWorks.Graphics
out var channels out var channels
); );
var byteCount = (uint)(width * height * channels);
TextureCreateInfo textureCreateInfo; TextureCreateInfo textureCreateInfo;
textureCreateInfo.Width = (uint)width; textureCreateInfo.Width = (uint)width;
textureCreateInfo.Height = (uint)height; textureCreateInfo.Height = (uint)height;
@ -36,10 +46,10 @@ namespace MoonWorks.Graphics
textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler; textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler;
var texture = new Texture(device, textureCreateInfo); var texture = new Texture(device, textureCreateInfo);
commandBuffer.SetTextureData(texture, pixels, byteCount);
texture.SetData(pixels, (uint)(width * height * 4));
Refresh.Refresh_Image_Free(pixels); Refresh.Refresh_Image_Free(pixels);
return texture; return texture;
} }
@ -173,61 +183,6 @@ namespace MoonWorks.Graphics
Height = textureCreateInfo.Height; Height = textureCreateInfo.Height;
} }
/// <summary>
/// Asynchronously copies data into the texture.
/// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="dataPtr">A pointer to an array of data to copy from.</param>
/// <param name="dataLengthInBytes">The amount of data to copy from the array.</param>
public void SetData(in TextureSlice textureSlice, IntPtr dataPtr, uint dataLengthInBytes)
{
Refresh.Refresh_SetTextureData(
Device.Handle,
textureSlice.ToRefreshTextureSlice(),
dataPtr,
dataLengthInBytes
);
}
/// <summary>
/// Asynchronously copies data into the texture.
/// This variant copies into the entire texture.
/// </summary>
/// <param name="dataPtr">A pointer to an array of data to copy from.</param>
/// <param name="dataLengthInBytes">The amount of data to copy from the array.</param>
public void SetData(IntPtr dataPtr, uint dataLengthInBytes)
{
SetData(new TextureSlice(this), dataPtr, dataLengthInBytes);
}
/// <summary>
/// Asynchronously copies data into the texture.
/// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetData<T>(in TextureSlice textureSlice, T[] data) where T : unmanaged
{
var size = Marshal.SizeOf<T>();
fixed (T* ptr = &data[0])
{
Refresh.Refresh_SetTextureData(
Device.Handle,
textureSlice.ToRefreshTextureSlice(),
(IntPtr) ptr,
(uint) (data.Length * size)
);
}
}
/// <summary>
/// Asynchronously copies data into the texture.
/// This variant copies data into the entire texture.
/// </summary>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetData<T>(T[] data) where T : unmanaged
{
SetData(new TextureSlice(this), data);
}
} }
} }