2021-01-20 03:33:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using RefreshCS;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics
|
|
|
|
|
{
|
|
|
|
|
public class Texture : GraphicsResource
|
|
|
|
|
{
|
|
|
|
|
public uint Width { get; }
|
|
|
|
|
public uint Height { get; }
|
2021-01-20 21:32:48 +00:00
|
|
|
|
public ColorFormat Format { get; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
|
|
|
|
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture;
|
|
|
|
|
|
|
|
|
|
public static Texture LoadPNG(GraphicsDevice device, FileInfo fileInfo)
|
|
|
|
|
{
|
|
|
|
|
var pixels = Refresh.Refresh_Image_Load(
|
|
|
|
|
fileInfo.FullName,
|
|
|
|
|
out var width,
|
|
|
|
|
out var height,
|
|
|
|
|
out var channels
|
|
|
|
|
);
|
|
|
|
|
|
2021-01-20 21:32:48 +00:00
|
|
|
|
TextureCreateInfo textureCreateInfo;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
textureCreateInfo.Width = (uint)width;
|
|
|
|
|
textureCreateInfo.Height = (uint)height;
|
|
|
|
|
textureCreateInfo.Depth = 1;
|
2021-01-20 21:32:48 +00:00
|
|
|
|
textureCreateInfo.Format = ColorFormat.R8G8B8A8;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
textureCreateInfo.IsCube = false;
|
|
|
|
|
textureCreateInfo.LevelCount = 1;
|
2021-01-20 21:32:48 +00:00
|
|
|
|
textureCreateInfo.SampleCount = SampleCount.One;
|
|
|
|
|
textureCreateInfo.UsageFlags = TextureUsageFlags.SamplerBit;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
|
|
|
|
var texture = new Texture(device, ref textureCreateInfo);
|
|
|
|
|
|
|
|
|
|
texture.SetData(pixels, (uint)(width * height * 4));
|
|
|
|
|
|
|
|
|
|
Refresh.Refresh_Image_Free(pixels);
|
|
|
|
|
return texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe static void SavePNG(string path, int width, int height, byte[] pixels)
|
|
|
|
|
{
|
|
|
|
|
fixed (byte* ptr = &pixels[0])
|
|
|
|
|
{
|
|
|
|
|
Refresh.Refresh_Image_SavePNG(path, width, height, (IntPtr) ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Texture CreateTexture2D(
|
|
|
|
|
GraphicsDevice device,
|
|
|
|
|
uint width,
|
|
|
|
|
uint height,
|
2021-01-20 21:32:48 +00:00
|
|
|
|
ColorFormat format,
|
|
|
|
|
TextureUsageFlags usageFlags,
|
|
|
|
|
SampleCount sampleCount = SampleCount.One,
|
2021-01-20 03:33:27 +00:00
|
|
|
|
uint levelCount = 1
|
|
|
|
|
)
|
|
|
|
|
{
|
2021-01-20 21:32:48 +00:00
|
|
|
|
var textureCreateInfo = new TextureCreateInfo
|
2021-01-20 03:33:27 +00:00
|
|
|
|
{
|
|
|
|
|
Width = width,
|
|
|
|
|
Height = height,
|
|
|
|
|
Depth = 1,
|
|
|
|
|
IsCube = false,
|
|
|
|
|
SampleCount = sampleCount,
|
|
|
|
|
LevelCount = levelCount,
|
|
|
|
|
Format = format,
|
|
|
|
|
UsageFlags = usageFlags
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Texture(device, ref textureCreateInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Texture CreateTexture3D(
|
|
|
|
|
GraphicsDevice device,
|
|
|
|
|
uint width,
|
|
|
|
|
uint height,
|
|
|
|
|
uint depth,
|
2021-01-20 21:32:48 +00:00
|
|
|
|
ColorFormat format,
|
|
|
|
|
TextureUsageFlags usageFlags,
|
|
|
|
|
SampleCount sampleCount = SampleCount.One,
|
2021-01-20 03:33:27 +00:00
|
|
|
|
uint levelCount = 1
|
|
|
|
|
)
|
|
|
|
|
{
|
2021-01-20 21:32:48 +00:00
|
|
|
|
var textureCreateInfo = new TextureCreateInfo
|
2021-01-20 03:33:27 +00:00
|
|
|
|
{
|
|
|
|
|
Width = width,
|
|
|
|
|
Height = height,
|
|
|
|
|
Depth = depth,
|
|
|
|
|
IsCube = false,
|
|
|
|
|
SampleCount = sampleCount,
|
|
|
|
|
LevelCount = levelCount,
|
|
|
|
|
Format = format,
|
|
|
|
|
UsageFlags = usageFlags
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Texture(device, ref textureCreateInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Texture CreateTextureCube(
|
|
|
|
|
GraphicsDevice device,
|
|
|
|
|
uint size,
|
2021-01-20 21:32:48 +00:00
|
|
|
|
ColorFormat format,
|
|
|
|
|
TextureUsageFlags usageFlags,
|
|
|
|
|
SampleCount sampleCount = SampleCount.One,
|
2021-01-20 03:33:27 +00:00
|
|
|
|
uint levelCount = 1
|
|
|
|
|
)
|
|
|
|
|
{
|
2021-01-20 21:32:48 +00:00
|
|
|
|
var textureCreateInfo = new TextureCreateInfo
|
2021-01-20 03:33:27 +00:00
|
|
|
|
{
|
|
|
|
|
Width = size,
|
|
|
|
|
Height = size,
|
|
|
|
|
Depth = 1,
|
|
|
|
|
IsCube = true,
|
|
|
|
|
SampleCount = sampleCount,
|
|
|
|
|
LevelCount = levelCount,
|
|
|
|
|
Format = format,
|
|
|
|
|
UsageFlags = usageFlags
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Texture(device, ref textureCreateInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 21:32:48 +00:00
|
|
|
|
public Texture(GraphicsDevice device, ref TextureCreateInfo textureCreateInfo) : base(device)
|
2021-01-20 03:33:27 +00:00
|
|
|
|
{
|
|
|
|
|
Handle = Refresh.Refresh_CreateTexture(
|
|
|
|
|
device.Handle,
|
2021-01-22 01:27:25 +00:00
|
|
|
|
textureCreateInfo.ToRefreshTextureCreateInfo()
|
2021-01-20 03:33:27 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Format = textureCreateInfo.Format;
|
|
|
|
|
Width = textureCreateInfo.Width;
|
|
|
|
|
Height = textureCreateInfo.Height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetData(IntPtr data, uint dataLengthInBytes)
|
|
|
|
|
{
|
|
|
|
|
Refresh.TextureSlice textureSlice;
|
|
|
|
|
textureSlice.texture = Handle;
|
|
|
|
|
textureSlice.rectangle.x = 0;
|
|
|
|
|
textureSlice.rectangle.y = 0;
|
|
|
|
|
textureSlice.rectangle.w = (int)Width;
|
|
|
|
|
textureSlice.rectangle.h = (int)Height;
|
|
|
|
|
textureSlice.level = 0;
|
|
|
|
|
textureSlice.layer = 0;
|
|
|
|
|
textureSlice.depth = 0;
|
|
|
|
|
|
|
|
|
|
Refresh.Refresh_SetTextureData(
|
|
|
|
|
Device.Handle,
|
2021-01-22 01:27:25 +00:00
|
|
|
|
textureSlice,
|
2021-01-20 03:33:27 +00:00
|
|
|
|
data,
|
|
|
|
|
dataLengthInBytes
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetData(ref TextureSlice textureSlice, IntPtr data, uint dataLengthInBytes)
|
|
|
|
|
{
|
|
|
|
|
var refreshTextureSlice = textureSlice.ToRefreshTextureSlice();
|
|
|
|
|
|
|
|
|
|
Refresh.Refresh_SetTextureData(
|
|
|
|
|
Device.Handle,
|
2021-01-22 01:27:25 +00:00
|
|
|
|
refreshTextureSlice,
|
2021-01-20 03:33:27 +00:00
|
|
|
|
data,
|
|
|
|
|
dataLengthInBytes
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|