From 05593d9bf15e7d0ff161717ed3955675e063b2d9 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Fri, 4 Nov 2022 23:20:07 -0400 Subject: [PATCH] bunch of Refresh api changes, plus more validation --- src/Graphics/CommandBuffer.cs | 105 ++++----------------- src/Graphics/RefreshEnums.cs | 5 +- src/Graphics/RefreshStructs.cs | 23 +++-- src/Graphics/Resources/GraphicsPipeline.cs | 3 +- src/Graphics/Resources/Texture.cs | 17 +--- src/Graphics/State/MultisampleState.cs | 8 ++ src/Graphics/State/TextureCreateInfo.cs | 2 - 7 files changed, 48 insertions(+), 115 deletions(-) diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index a0746d3..45a602e 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -16,6 +16,7 @@ namespace MoonWorks.Graphics GraphicsPipeline currentGraphicsPipeline; ComputePipeline currentComputePipeline; bool renderPassActive; + SampleCount currentSampleCount; // called from RefreshDevice internal CommandBuffer(GraphicsDevice device, IntPtr handle) @@ -25,6 +26,7 @@ namespace MoonWorks.Graphics currentGraphicsPipeline = null; currentComputePipeline = null; renderPassActive = false; + currentSampleCount = SampleCount.One; } // FIXME: we can probably use the NativeMemory functions to not have to generate arrays here @@ -55,7 +57,6 @@ namespace MoonWorks.Graphics Refresh.Refresh_BeginRenderPass( Device.Handle, Handle, - IntPtr.Zero, (IntPtr) pColorAttachmentInfos, (uint) colorAttachmentInfos.Length, IntPtr.Zero @@ -78,7 +79,7 @@ namespace MoonWorks.Graphics ) { #if DEBUG - AssertValidDepthAttachments(depthStencilAttachmentInfo); + AssertValidDepthAttachment(depthStencilAttachmentInfo); AssertValidColorAttachments(colorAttachmentInfos, false); #endif @@ -96,88 +97,6 @@ namespace MoonWorks.Graphics Refresh.Refresh_BeginRenderPass( Device.Handle, Handle, - IntPtr.Zero, - pColorAttachmentInfos, - (uint) colorAttachmentInfos.Length, - &refreshDepthStencilAttachmentInfo - ); - } - - renderPassActive = true; - } - - /// - /// Begins a render pass. - /// All render state, resource binding, and draw commands must be made within a render pass. - /// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass. - /// - /// The rectangle that should be drawn to on the attachments. - /// The color attachments to use in the render pass. - public unsafe void BeginRenderPass( - in Rect renderArea, - params ColorAttachmentInfo[] colorAttachmentInfos - ) - { -#if DEBUG - AssertValidColorAttachments(colorAttachmentInfos, true); -#endif - - var refreshColorAttachmentInfos = new Refresh.ColorAttachmentInfo[colorAttachmentInfos.Length]; - - for (var i = 0; i < colorAttachmentInfos.Length; i += 1) - { - refreshColorAttachmentInfos[i] = colorAttachmentInfos[i].ToRefresh(); - } - - fixed (Refresh.ColorAttachmentInfo* pColorAttachmentInfos = refreshColorAttachmentInfos) - { - Refresh.Refresh_BeginRenderPass( - Device.Handle, - Handle, - renderArea.ToRefresh(), - (IntPtr) pColorAttachmentInfos, - (uint) colorAttachmentInfos.Length, - IntPtr.Zero - ); - } - - renderPassActive = true; - } - - /// - /// Begins a render pass. - /// All render state, resource binding, and draw commands must be made within a render pass. - /// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass. - /// - /// The rectangle that should be drawn to on the attachments. - /// The depth stencil attachment to use in the render pass. - /// The color attachments to use in the render pass. - public unsafe void BeginRenderPass( - in Rect renderArea, - DepthStencilAttachmentInfo depthStencilAttachmentInfo, - params ColorAttachmentInfo[] colorAttachmentInfos - ) - { -#if DEBUG - AssertValidDepthAttachments(depthStencilAttachmentInfo); - AssertValidColorAttachments(colorAttachmentInfos, false); -#endif - - var refreshColorAttachmentInfos = new Refresh.ColorAttachmentInfo[colorAttachmentInfos.Length]; - - for (var i = 0; i < colorAttachmentInfos.Length; i += 1) - { - refreshColorAttachmentInfos[i] = colorAttachmentInfos[i].ToRefresh(); - } - - var refreshDepthStencilAttachmentInfo = depthStencilAttachmentInfo.ToRefresh(); - - fixed (Refresh.ColorAttachmentInfo* pColorAttachmentInfos = refreshColorAttachmentInfos) - { - Refresh.Refresh_BeginRenderPass( - Device.Handle, - Handle, - renderArea.ToRefresh(), pColorAttachmentInfos, (uint) colorAttachmentInfos.Length, &refreshDepthStencilAttachmentInfo @@ -312,6 +231,15 @@ namespace MoonWorks.Graphics GraphicsPipeline graphicsPipeline ) { +#if DEBUG + AssertRenderPassActive(); + + if (graphicsPipeline.SampleCount != currentSampleCount) + { + throw new System.ArgumentException("The sample count of the bound GraphicsPipeline must match the sample count of the current render pass!"); + } +#endif + Refresh.Refresh_BindGraphicsPipeline( Device.Handle, Handle, @@ -1073,6 +1001,8 @@ namespace MoonWorks.Graphics throw new System.ArgumentException("Render pass must contain at least one attachment!"); } + currentSampleCount = (colorAttachmentInfos.Length > 0) ? colorAttachmentInfos[0].SampleCount : SampleCount.One; + if (colorAttachmentInfos.Length > 4) { throw new System.ArgumentException("Render pass cannot have more than 4 color attachments!"); @@ -1090,10 +1020,15 @@ namespace MoonWorks.Graphics { throw new System.ArgumentException("Render pass color attachment UsageFlags must include TextureUsageFlags.ColorTarget!"); } + + if (colorAttachmentInfos[i].SampleCount != currentSampleCount) + { + throw new System.ArgumentException("All color attachments in a render pass must have the same SampleCount!"); + } } } - private void AssertValidDepthAttachments(DepthStencilAttachmentInfo depthStencilAttachmentInfo) + private void AssertValidDepthAttachment(DepthStencilAttachmentInfo depthStencilAttachmentInfo) { if (depthStencilAttachmentInfo.Texture == null || depthStencilAttachmentInfo.Texture.Handle == IntPtr.Zero) diff --git a/src/Graphics/RefreshEnums.cs b/src/Graphics/RefreshEnums.cs index 1c859c4..7f178ae 100644 --- a/src/Graphics/RefreshEnums.cs +++ b/src/Graphics/RefreshEnums.cs @@ -110,10 +110,7 @@ namespace MoonWorks.Graphics One, Two, Four, - Eight, - Sixteen, - ThirtyTwo, - SixtyFour + Eight } public enum CubeMapFace : uint diff --git a/src/Graphics/RefreshStructs.cs b/src/Graphics/RefreshStructs.cs index d2ccd9a..ad9e530 100644 --- a/src/Graphics/RefreshStructs.cs +++ b/src/Graphics/RefreshStructs.cs @@ -209,25 +209,35 @@ namespace MoonWorks.Graphics public LoadOp LoadOp; public StoreOp StoreOp; - public ColorAttachmentInfo(Texture texture, Color clearColor, StoreOp storeOp = StoreOp.Store) + public ColorAttachmentInfo( + Texture texture, + Color clearColor, + SampleCount sampleCount = SampleCount.One, + StoreOp storeOp = StoreOp.Store + ) { Texture = texture; Depth = 0; Layer = 0; Level = 0; - SampleCount = SampleCount.One; + SampleCount = sampleCount; ClearColor = clearColor; LoadOp = LoadOp.Clear; StoreOp = storeOp; } - public ColorAttachmentInfo(Texture texture, LoadOp loadOp = LoadOp.DontCare, StoreOp storeOp = StoreOp.Store) + public ColorAttachmentInfo( + Texture texture, + LoadOp loadOp = LoadOp.DontCare, + SampleCount sampleCount = SampleCount.One, + StoreOp storeOp = StoreOp.Store + ) { Texture = texture; Depth = 0; Layer = 0; Level = 0; - SampleCount = SampleCount.One; + SampleCount = sampleCount; ClearColor = Color.White; LoadOp = loadOp; StoreOp = storeOp; @@ -344,16 +354,13 @@ namespace MoonWorks.Graphics public struct ColorAttachmentDescription { public TextureFormat Format; - public SampleCount SampleCount; public ColorAttachmentBlendState BlendState; public ColorAttachmentDescription( TextureFormat format, - ColorAttachmentBlendState blendState, - SampleCount sampleCount = SampleCount.One + ColorAttachmentBlendState blendState ) { Format = format; - SampleCount = sampleCount; BlendState = blendState; } } diff --git a/src/Graphics/Resources/GraphicsPipeline.cs b/src/Graphics/Resources/GraphicsPipeline.cs index 11026eb..ccf88cd 100644 --- a/src/Graphics/Resources/GraphicsPipeline.cs +++ b/src/Graphics/Resources/GraphicsPipeline.cs @@ -14,6 +14,7 @@ namespace MoonWorks.Graphics public GraphicsShaderInfo VertexShaderInfo { get; } public GraphicsShaderInfo FragmentShaderInfo { get; } + internal SampleCount SampleCount { get; } public unsafe GraphicsPipeline( GraphicsDevice device, @@ -46,7 +47,6 @@ namespace MoonWorks.Graphics for (var i = 0; i < attachmentInfo.ColorAttachmentDescriptions.Length; i += 1) { colorAttachmentDescriptions[i].format = (Refresh.TextureFormat) attachmentInfo.ColorAttachmentDescriptions[i].Format; - colorAttachmentDescriptions[i].sampleCount = (Refresh.SampleCount) attachmentInfo.ColorAttachmentDescriptions[i].SampleCount; colorAttachmentDescriptions[i].blendState = attachmentInfo.ColorAttachmentDescriptions[i].BlendState.ToRefresh(); } @@ -112,6 +112,7 @@ namespace MoonWorks.Graphics VertexShaderInfo = vertexShaderInfo; FragmentShaderInfo = fragmentShaderInfo; + SampleCount = multisampleState.MultisampleCount; } } } diff --git a/src/Graphics/Resources/Texture.cs b/src/Graphics/Resources/Texture.cs index a049786..124eaef 100644 --- a/src/Graphics/Resources/Texture.cs +++ b/src/Graphics/Resources/Texture.cs @@ -15,7 +15,6 @@ namespace MoonWorks.Graphics public TextureFormat Format { get; } public bool IsCube { get; } public uint LevelCount { get; } - public SampleCount SampleCount { get; } public TextureUsageFlags UsageFlags { get; } protected override Action QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture; @@ -46,7 +45,6 @@ namespace MoonWorks.Graphics textureCreateInfo.Format = TextureFormat.R8G8B8A8; textureCreateInfo.IsCube = false; textureCreateInfo.LevelCount = 1; - textureCreateInfo.SampleCount = SampleCount.One; textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler; var texture = new Texture(device, textureCreateInfo); @@ -83,12 +81,12 @@ namespace MoonWorks.Graphics if (isCube) { - texture = CreateTextureCube(graphicsDevice, (uint) width, format, TextureUsageFlags.Sampler, SampleCount.One, (uint) levels); + texture = CreateTextureCube(graphicsDevice, (uint) width, format, TextureUsageFlags.Sampler, (uint) levels); faces = 6; } else { - texture = CreateTexture2D(graphicsDevice, (uint) width, (uint) height, format, TextureUsageFlags.Sampler, SampleCount.One, (uint) levels); + texture = CreateTexture2D(graphicsDevice, (uint) width, (uint) height, format, TextureUsageFlags.Sampler, (uint) levels); faces = 1; } @@ -124,7 +122,6 @@ namespace MoonWorks.Graphics /// The height of the texture. /// The format of the texture. /// Specifies how the texture will be used. - /// Specifies the multisample count. /// Specifies the number of mip levels. public static Texture CreateTexture2D( GraphicsDevice device, @@ -132,7 +129,6 @@ namespace MoonWorks.Graphics uint height, TextureFormat format, TextureUsageFlags usageFlags, - SampleCount sampleCount = SampleCount.One, uint levelCount = 1 ) { @@ -142,7 +138,6 @@ namespace MoonWorks.Graphics Height = height, Depth = 1, IsCube = false, - SampleCount = sampleCount, LevelCount = levelCount, Format = format, UsageFlags = usageFlags @@ -160,7 +155,6 @@ namespace MoonWorks.Graphics /// The depth of the texture. /// The format of the texture. /// Specifies how the texture will be used. - /// Specifies the multisample count. /// Specifies the number of mip levels. public static Texture CreateTexture3D( GraphicsDevice device, @@ -169,7 +163,6 @@ namespace MoonWorks.Graphics uint depth, TextureFormat format, TextureUsageFlags usageFlags, - SampleCount sampleCount = SampleCount.One, uint levelCount = 1 ) { @@ -179,7 +172,6 @@ namespace MoonWorks.Graphics Height = height, Depth = depth, IsCube = false, - SampleCount = sampleCount, LevelCount = levelCount, Format = format, UsageFlags = usageFlags @@ -195,14 +187,12 @@ namespace MoonWorks.Graphics /// The length of one side of the cube. /// The format of the texture. /// Specifies how the texture will be used. - /// Specifies the multisample count. /// Specifies the number of mip levels. public static Texture CreateTextureCube( GraphicsDevice device, uint size, TextureFormat format, TextureUsageFlags usageFlags, - SampleCount sampleCount = SampleCount.One, uint levelCount = 1 ) { @@ -212,7 +202,6 @@ namespace MoonWorks.Graphics Height = size, Depth = 1, IsCube = true, - SampleCount = sampleCount, LevelCount = levelCount, Format = format, UsageFlags = usageFlags @@ -241,7 +230,6 @@ namespace MoonWorks.Graphics Height = textureCreateInfo.Height; Depth = textureCreateInfo.Depth; IsCube = textureCreateInfo.IsCube; - SampleCount = textureCreateInfo.SampleCount; LevelCount = textureCreateInfo.LevelCount; UsageFlags = textureCreateInfo.UsageFlags; } @@ -265,7 +253,6 @@ namespace MoonWorks.Graphics Height = height; Depth = 1; IsCube = false; - SampleCount = SampleCount.One; LevelCount = 1; UsageFlags = TextureUsageFlags.ColorTarget; } diff --git a/src/Graphics/State/MultisampleState.cs b/src/Graphics/State/MultisampleState.cs index 4b6806f..acbcec0 100644 --- a/src/Graphics/State/MultisampleState.cs +++ b/src/Graphics/State/MultisampleState.cs @@ -13,5 +13,13 @@ MultisampleCount = SampleCount.One, SampleMask = uint.MaxValue }; + + public MultisampleState( + SampleCount sampleCount, + uint sampleMask = uint.MaxValue + ) { + MultisampleCount = sampleCount; + SampleMask = sampleMask; + } } } diff --git a/src/Graphics/State/TextureCreateInfo.cs b/src/Graphics/State/TextureCreateInfo.cs index f7044bf..c4f7225 100644 --- a/src/Graphics/State/TextureCreateInfo.cs +++ b/src/Graphics/State/TextureCreateInfo.cs @@ -8,7 +8,6 @@ namespace MoonWorks.Graphics public uint Height; public uint Depth; public bool IsCube; - public SampleCount SampleCount; public uint LevelCount; public TextureFormat Format; public TextureUsageFlags UsageFlags; @@ -21,7 +20,6 @@ namespace MoonWorks.Graphics height = Height, depth = Depth, isCube = Conversions.BoolToByte(IsCube), - sampleCount = (Refresh.SampleCount) SampleCount, levelCount = LevelCount, format = (Refresh.TextureFormat) Format, usageFlags = (Refresh.TextureUsageFlags) UsageFlags -- 2.25.1