Campari/src/Texture.cs

73 lines
2.3 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 Height { get; }
public uint Width { 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-14 10:50:02 +00:00
public static Texture LoadPNG(RefreshDevice 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
Refresh.TextureCreateInfo textureCreateInfo;
textureCreateInfo.width = (uint) width;
textureCreateInfo.height = (uint) height;
textureCreateInfo.depth = 1;
textureCreateInfo.format = Refresh.ColorFormat.R8G8B8A8;
textureCreateInfo.isCube = 0;
textureCreateInfo.levelCount = 1;
textureCreateInfo.sampleCount = Refresh.SampleCount.One;
textureCreateInfo.usageFlags = (uint) Refresh.TextureUsageFlagBits.SamplerBit;
var texture = new Texture(device, ref textureCreateInfo);
texture.SetData(pixels, (uint) (width * height * 4));
Refresh.Refresh_Image_Free(pixels);
return texture;
}
public Texture(RefreshDevice device, ref Refresh.TextureCreateInfo textureCreateInfo) : base(device)
{
Handle = Refresh.Refresh_CreateTexture(
2021-01-14 09:26:54 +00:00
device.Handle,
2021-01-14 10:50:02 +00:00
ref textureCreateInfo
2021-01-14 09:26:54 +00:00
);
2021-01-14 10:50:02 +00:00
Format = textureCreateInfo.format;
}
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-14 10:50:02 +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
);
}
}
}