render pass and targets

main
cosmonaut 2021-01-14 02:50:02 -08:00
parent 7a7e26d84e
commit e89cafd34b
7 changed files with 151 additions and 24 deletions

@ -1 +1 @@
Subproject commit c6fddad50aaf4a6d00ca8571fb8f94876d0fcbef Subproject commit 5591c81d14d75dc24759dc2843e7b05ab6d98ecc

22
src/ColorTarget.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
using RefreshCS;
namespace Campari
{
public class ColorTarget : GraphicsResource
{
public uint Width { get; }
public uint Height { get; }
public Texture Texture { get; }
public Refresh.ColorFormat Format => Texture.Format;
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyColorTarget;
public ColorTarget(RefreshDevice device, Refresh.SampleCount sampleCount, ref TextureSlice textureSlice) : base(device)
{
var refreshTextureSlice = textureSlice.ToRefreshTextureSlice();
Handle = Refresh.Refresh_CreateColorTarget(device.Handle, sampleCount, ref refreshTextureSlice);
}
}
}

21
src/DepthStencilTarget.cs Normal file
View File

@ -0,0 +1,21 @@
using System;
using RefreshCS;
namespace Campari
{
class DepthStencilTarget : GraphicsResource
{
public uint Width { get; }
public uint Height { get; }
public Refresh.DepthFormat Format { get; }
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyDepthStencilTarget;
public DepthStencilTarget(RefreshDevice device, uint width, uint height, Refresh.DepthFormat depthFormat) : base(device)
{
Handle = Refresh.Refresh_CreateDepthStencilTarget(device.Handle, width, height, depthFormat);
Width = width;
Height = height;
}
}
}

18
src/Rectangle.cs Normal file
View File

@ -0,0 +1,18 @@
namespace Campari
{
public struct Rectangle
{
public int X { get; }
public int Y { get; }
public int W { get; }
public int H { get; }
public Rectangle(int x, int y, int w, int h)
{
X = x;
Y = y;
W = w;
H = h;
}
}
}

View File

@ -24,7 +24,7 @@ namespace Campari
RefreshDevice device, RefreshDevice device,
Refresh.DepthStencilTargetDescription depthStencilTargetDescription, Refresh.DepthStencilTargetDescription depthStencilTargetDescription,
params Refresh.ColorTargetDescription[] colorTargetDescriptions params Refresh.ColorTargetDescription[] colorTargetDescriptions
) : base(device) ) : base(device)
{ {
Refresh.DepthStencilTargetDescription* depthStencilPtr = &depthStencilTargetDescription; Refresh.DepthStencilTargetDescription* depthStencilPtr = &depthStencilTargetDescription;

View File

@ -8,48 +8,65 @@ namespace Campari
{ {
public uint Height { get; } public uint Height { get; }
public uint Width { get; } public uint Width { get; }
public Refresh.ColorFormat Format { get; }
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture; protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture;
public Texture(RefreshDevice device, FileInfo fileInfo) : base(device) public static Texture LoadPNG(RefreshDevice device, FileInfo fileInfo)
{ {
var pixels = Refresh.Refresh_Image_Load( var pixels = Refresh.Refresh_Image_Load(
fileInfo.FullName, fileInfo.FullName,
out var width, out var width,
out var height, out var height,
out var channels out var channels
); );
IntPtr textureHandle = Refresh.Refresh_CreateTexture2D( 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(
device.Handle, device.Handle,
Refresh.ColorFormat.R8G8B8A8, ref textureCreateInfo
(uint)width,
(uint)height,
1,
(uint)Refresh.TextureUsageFlagBits.SamplerBit
); );
Format = textureCreateInfo.format;
}
public void SetData(IntPtr data, uint dataLengthInBytes)
{
Refresh.TextureSlice textureSlice; Refresh.TextureSlice textureSlice;
textureSlice.texture = textureHandle; textureSlice.texture = Handle;
textureSlice.rectangle.x = 0; textureSlice.rectangle.x = 0;
textureSlice.rectangle.y = 0; textureSlice.rectangle.y = 0;
textureSlice.rectangle.w = width; textureSlice.rectangle.w = (int) Width;
textureSlice.rectangle.h = height; textureSlice.rectangle.h = (int) Height;
textureSlice.level = 0; textureSlice.level = 0;
textureSlice.layer = 0; textureSlice.layer = 0;
textureSlice.depth = 0; textureSlice.depth = 0;
Refresh.Refresh_SetTextureData( Refresh.Refresh_SetTextureData(
device.Handle, Device.Handle,
ref textureSlice, ref textureSlice,
pixels, data,
(uint)(width * height * 4) dataLengthInBytes
); );
Refresh.Refresh_Image_Free(pixels);
Width = (uint) width;
Height = (uint) height;
} }
} }
} }

49
src/TextureSlice.cs Normal file
View File

@ -0,0 +1,49 @@
namespace Campari
{
public struct TextureSlice
{
public Texture Texture { get; }
public Rectangle Rectangle { get; }
public uint Depth { get; }
public uint Layer { get; }
public uint Level { get; }
public TextureSlice(Texture texture)
{
Texture = texture;
Rectangle = new Rectangle(0, 0, (int) texture.Width, (int) texture.Height);
Depth = 0;
Layer = 0;
Level = 0;
}
public TextureSlice(Texture texture, Rectangle rectangle, uint depth = 0, uint layer = 0, uint level = 0)
{
Texture = texture;
Rectangle = rectangle;
Depth = depth;
Layer = layer;
Level = level;
}
public RefreshCS.Refresh.TextureSlice ToRefreshTextureSlice()
{
RefreshCS.Refresh.TextureSlice textureSlice = new RefreshCS.Refresh.TextureSlice
{
texture = Texture.Handle,
rectangle = new RefreshCS.Refresh.Rect
{
x = Rectangle.X,
y = Rectangle.Y,
w = Rectangle.W,
h = Rectangle.H
},
depth = Depth,
layer = Layer,
level = Level
};
return textureSlice;
}
}
}