From e89cafd34bc065f6bf0a06dc3d352ace07ee85bb Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 14 Jan 2021 02:50:02 -0800 Subject: [PATCH] render pass and targets --- lib/RefreshCS | 2 +- src/ColorTarget.cs | 22 ++++++++++++++ src/DepthStencilTarget.cs | 21 ++++++++++++++ src/Rectangle.cs | 18 ++++++++++++ src/RenderPass.cs | 2 +- src/Texture.cs | 61 +++++++++++++++++++++++++-------------- src/TextureSlice.cs | 49 +++++++++++++++++++++++++++++++ 7 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 src/ColorTarget.cs create mode 100644 src/DepthStencilTarget.cs create mode 100644 src/Rectangle.cs create mode 100644 src/TextureSlice.cs diff --git a/lib/RefreshCS b/lib/RefreshCS index c6fddad..5591c81 160000 --- a/lib/RefreshCS +++ b/lib/RefreshCS @@ -1 +1 @@ -Subproject commit c6fddad50aaf4a6d00ca8571fb8f94876d0fcbef +Subproject commit 5591c81d14d75dc24759dc2843e7b05ab6d98ecc diff --git a/src/ColorTarget.cs b/src/ColorTarget.cs new file mode 100644 index 0000000..2eb78e7 --- /dev/null +++ b/src/ColorTarget.cs @@ -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 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); + } + } +} diff --git a/src/DepthStencilTarget.cs b/src/DepthStencilTarget.cs new file mode 100644 index 0000000..6236ad2 --- /dev/null +++ b/src/DepthStencilTarget.cs @@ -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 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; + } + } +} diff --git a/src/Rectangle.cs b/src/Rectangle.cs new file mode 100644 index 0000000..e62cb0a --- /dev/null +++ b/src/Rectangle.cs @@ -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; + } + } +} diff --git a/src/RenderPass.cs b/src/RenderPass.cs index ece64e7..4d62ad7 100644 --- a/src/RenderPass.cs +++ b/src/RenderPass.cs @@ -24,7 +24,7 @@ namespace Campari RefreshDevice device, Refresh.DepthStencilTargetDescription depthStencilTargetDescription, params Refresh.ColorTargetDescription[] colorTargetDescriptions - ) : base(device) + ) : base(device) { Refresh.DepthStencilTargetDescription* depthStencilPtr = &depthStencilTargetDescription; diff --git a/src/Texture.cs b/src/Texture.cs index 5857f29..6b524a5 100644 --- a/src/Texture.cs +++ b/src/Texture.cs @@ -8,48 +8,65 @@ namespace Campari { public uint Height { get; } public uint Width { get; } + public Refresh.ColorFormat Format { get; } protected override Action 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( - fileInfo.FullName, - out var width, - out var height, + fileInfo.FullName, + out var width, + out var height, 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, - Refresh.ColorFormat.R8G8B8A8, - (uint)width, - (uint)height, - 1, - (uint)Refresh.TextureUsageFlagBits.SamplerBit + ref textureCreateInfo ); + Format = textureCreateInfo.format; + } + + public void SetData(IntPtr data, uint dataLengthInBytes) + { Refresh.TextureSlice textureSlice; - textureSlice.texture = textureHandle; + textureSlice.texture = Handle; textureSlice.rectangle.x = 0; textureSlice.rectangle.y = 0; - textureSlice.rectangle.w = width; - textureSlice.rectangle.h = height; + textureSlice.rectangle.w = (int) Width; + textureSlice.rectangle.h = (int) Height; textureSlice.level = 0; textureSlice.layer = 0; textureSlice.depth = 0; Refresh.Refresh_SetTextureData( - device.Handle, + Device.Handle, ref textureSlice, - pixels, - (uint)(width * height * 4) + data, + dataLengthInBytes ); - - Refresh.Refresh_Image_Free(pixels); - - Width = (uint) width; - Height = (uint) height; } } } diff --git a/src/TextureSlice.cs b/src/TextureSlice.cs new file mode 100644 index 0000000..45b5b2e --- /dev/null +++ b/src/TextureSlice.cs @@ -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; + } + } +}