Campari/src/Texture.cs

172 lines
5.4 KiB
C#
Raw Normal View History

2021-01-14 09:26:54 +00:00
using System;
using System.IO;
using RefreshCS;
namespace Campari
{
public class Texture : GraphicsResource
{
public uint Width { get; }
2021-01-15 03:14:56 +00:00
public uint Height { get; }
2021-01-14 10:50:02 +00:00
public Refresh.ColorFormat Format { get; }
2021-01-14 09:26:54 +00:00
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture;
2021-01-16 02:13:53 +00:00
public static Texture LoadPNG(GraphicsDevice device, FileInfo fileInfo)
2021-01-14 09:26:54 +00:00
{
var pixels = Refresh.Refresh_Image_Load(
2021-01-14 10:50:02 +00:00
fileInfo.FullName,
out var width,
out var height,
2021-01-14 09:26:54 +00:00
out var channels
);
2021-01-14 10:50:02 +00:00
2021-01-16 02:08:58 +00:00
Campari.TextureCreateInfo textureCreateInfo;
textureCreateInfo.Width = (uint)width;
textureCreateInfo.Height = (uint)height;
textureCreateInfo.Depth = 1;
textureCreateInfo.Format = Refresh.ColorFormat.R8G8B8A8;
textureCreateInfo.IsCube = false;
textureCreateInfo.LevelCount = 1;
textureCreateInfo.SampleCount = Refresh.SampleCount.One;
textureCreateInfo.UsageFlags = Refresh.TextureUsageFlags.SamplerBit;
2021-01-14 10:50:02 +00:00
var texture = new Texture(device, ref textureCreateInfo);
2021-01-16 02:08:58 +00:00
texture.SetData(pixels, (uint)(width * height * 4));
2021-01-14 10:50:02 +00:00
Refresh.Refresh_Image_Free(pixels);
return texture;
}
2021-01-16 03:42:31 +00:00
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);
}
}
2021-01-16 02:08:58 +00:00
public static Texture CreateTexture2D(
2021-01-16 02:13:53 +00:00
GraphicsDevice device,
2021-01-16 02:08:58 +00:00
uint width,
uint height,
Refresh.ColorFormat format,
Refresh.TextureUsageFlags usageFlags,
Refresh.SampleCount sampleCount = Refresh.SampleCount.One,
uint levelCount = 1
)
2021-01-14 10:50:02 +00:00
{
2021-01-16 02:08:58 +00:00
var textureCreateInfo = new Campari.TextureCreateInfo
{
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(
2021-01-16 02:13:53 +00:00
GraphicsDevice device,
2021-01-16 02:08:58 +00:00
uint width,
uint height,
uint depth,
Refresh.ColorFormat format,
Refresh.TextureUsageFlags usageFlags,
Refresh.SampleCount sampleCount = Refresh.SampleCount.One,
uint levelCount = 1
)
{
var textureCreateInfo = new Campari.TextureCreateInfo
{
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(
2021-01-16 02:13:53 +00:00
GraphicsDevice device,
2021-01-16 02:08:58 +00:00
uint size,
Refresh.ColorFormat format,
Refresh.TextureUsageFlags usageFlags,
Refresh.SampleCount sampleCount = Refresh.SampleCount.One,
uint levelCount = 1
)
{
var textureCreateInfo = new Campari.TextureCreateInfo
{
Width = size,
Height = size,
Depth = 1,
IsCube = true,
SampleCount = sampleCount,
LevelCount = levelCount,
Format = format,
UsageFlags = usageFlags
};
return new Texture(device, ref textureCreateInfo);
}
2021-01-16 02:13:53 +00:00
public Texture(GraphicsDevice device, ref Campari.TextureCreateInfo textureCreateInfo) : base(device)
2021-01-16 02:08:58 +00:00
{
var refreshTextureCreateInfo = textureCreateInfo.ToRefreshTextureCreateInfo();
2021-01-14 10:50:02 +00:00
Handle = Refresh.Refresh_CreateTexture(
2021-01-14 09:26:54 +00:00
device.Handle,
2021-01-16 02:08:58 +00:00
ref refreshTextureCreateInfo
2021-01-14 09:26:54 +00:00
);
2021-01-16 02:08:58 +00:00
Format = textureCreateInfo.Format;
Width = textureCreateInfo.Width;
Height = textureCreateInfo.Height;
2021-01-14 10:50:02 +00:00
}
public void SetData(IntPtr data, uint dataLengthInBytes)
{
2021-01-14 09:26:54 +00:00
Refresh.TextureSlice textureSlice;
2021-01-14 10:50:02 +00:00
textureSlice.texture = Handle;
2021-01-14 09:26:54 +00:00
textureSlice.rectangle.x = 0;
textureSlice.rectangle.y = 0;
2021-01-16 02:08:58 +00:00
textureSlice.rectangle.w = (int)Width;
textureSlice.rectangle.h = (int)Height;
2021-01-14 09:26:54 +00:00
textureSlice.level = 0;
textureSlice.layer = 0;
textureSlice.depth = 0;
Refresh.Refresh_SetTextureData(
2021-01-14 10:50:02 +00:00
Device.Handle,
2021-01-14 09:26:54 +00:00
ref textureSlice,
2021-01-14 10:50:02 +00:00
data,
dataLengthInBytes
2021-01-14 09:26:54 +00:00
);
}
2021-01-16 02:08:58 +00:00
public void SetData(ref TextureSlice textureSlice, IntPtr data, uint dataLengthInBytes)
{
var refreshTextureSlice = textureSlice.ToRefreshTextureSlice();
Refresh.Refresh_SetTextureData(
Device.Handle,
ref refreshTextureSlice,
data,
dataLengthInBytes
);
}
2021-01-14 09:26:54 +00:00
}
}