From 04d94de392b6e5ca4f82acc233df7b3f97abca36 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bruyère Date: Fri, 5 Apr 2019 14:13:20 +0200 Subject: Activable pattern wip --- vke/src/RenderPass.cs | 176 ++++++++++++++++++++++---------------------------- 1 file changed, 76 insertions(+), 100 deletions(-) (limited to 'vke/src/RenderPass.cs') diff --git a/vke/src/RenderPass.cs b/vke/src/RenderPass.cs index 8e73e10..809edf3 100644 --- a/vke/src/RenderPass.cs +++ b/vke/src/RenderPass.cs @@ -30,16 +30,75 @@ using Vulkan; using static Vulkan.VulkanNative; namespace VKE { - public class RenderPass : IDisposable { - internal uint references; - internal VkRenderPass handle; - internal Device dev; - - internal NativeList attachments = new NativeList (); - public NativeList ClearValues = new NativeList (); + public class RenderPass : Activable { + internal VkRenderPass handle; + internal List attachments = new List (); + public List ClearValues = new List (); internal List subpasses = new List (); - NativeList dependencies = new NativeList (); + List dependencies = new List (); + + #region CTORS + public RenderPass (Device device) : base(device) { } + + /// + /// Create default renderpass with one color and one depth attachments + /// + public RenderPass (Device device, VkFormat colorFormat, VkFormat depthFormat, VkSampleCountFlags samples = VkSampleCountFlags.Count1) + : this (device){ + + AddAttachment (colorFormat, (samples == VkSampleCountFlags.Count1) ? VkImageLayout.PresentSrcKHR : VkImageLayout.ColorAttachmentOptimal, samples); + AddAttachment (depthFormat, VkImageLayout.DepthStencilAttachmentOptimal, samples); + + ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.2f) }); + ClearValues.Add (new VkClearValue { depthStencil = new VkClearDepthStencilValue (1.0f, 0) }); + + SubPass subpass0 = new SubPass (); + + subpass0.AddColorReference (0, VkImageLayout.ColorAttachmentOptimal); + subpass0.SetDepthReference (1, VkImageLayout.DepthStencilAttachmentOptimal); + + if (samples != VkSampleCountFlags.Count1) { + AddAttachment (colorFormat, VkImageLayout.PresentSrcKHR, VkSampleCountFlags.Count1); + ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.2f) }); + subpass0.AddResolveReference (2, VkImageLayout.ColorAttachmentOptimal); + } + + + AddSubpass (subpass0); + + AddDependency (VulkanNative.SubpassExternal, 0, + VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.ColorAttachmentOutput, + VkAccessFlags.MemoryRead, VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite); + AddDependency (0, 1, + VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.BottomOfPipe, + VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.MemoryRead); + } + #endregion + + public override void Activate () { + if (state != ActivableState.Activated) { + List spDescs = new List (); + foreach (SubPass sp in subpasses) + spDescs.Add (sp.SubpassDescription); + + VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.New (); + renderPassInfo.attachmentCount = (uint)attachments.Count; + renderPassInfo.pAttachments = attachments.Pin (); + renderPassInfo.subpassCount = (uint)spDescs.Count; + renderPassInfo.pSubpasses = spDescs.Pin (); + renderPassInfo.dependencyCount = (uint)dependencies.Count; + renderPassInfo.pDependencies = dependencies.Pin (); + + handle = dev.CreateRenderPass (renderPassInfo); + + attachments.Unpin (); + spDescs.Unpin (); + dependencies.Unpin (); + } + base.Activate (); + } + public void AddAttachment (VkFormat format, VkImageLayout finalLayout, VkSampleCountFlags samples = VkSampleCountFlags.Count1, @@ -93,72 +152,6 @@ namespace VKE { public void AddSubpass (SubPass subPass) { subpasses.Add (subPass); } - public RenderPass (Device device) { - dev = device; - } - /// - /// Create default renderpass with one color and one depth attachments - /// - public RenderPass (Device device, VkFormat colorFormat, VkFormat depthFormat, VkSampleCountFlags samples = VkSampleCountFlags.Count1) - : this (device){ - - AddAttachment (colorFormat, (samples == VkSampleCountFlags.Count1) ? VkImageLayout.PresentSrcKHR : VkImageLayout.ColorAttachmentOptimal, samples); - AddAttachment (depthFormat, VkImageLayout.DepthStencilAttachmentOptimal, samples); - - ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.2f) }); - ClearValues.Add (new VkClearValue { depthStencil = new VkClearDepthStencilValue (1.0f, 0) }); - - SubPass subpass0 = new SubPass (); - - subpass0.AddColorReference (0, VkImageLayout.ColorAttachmentOptimal); - subpass0.SetDepthReference (1, VkImageLayout.DepthStencilAttachmentOptimal); - - if (samples != VkSampleCountFlags.Count1) { - AddAttachment (colorFormat, VkImageLayout.PresentSrcKHR, VkSampleCountFlags.Count1); - ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.2f) }); - subpass0.AddResolveReference (2, VkImageLayout.ColorAttachmentOptimal); - } - - - AddSubpass (subpass0); - - AddDependency (VulkanNative.SubpassExternal, 0, - VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.ColorAttachmentOutput, - VkAccessFlags.MemoryRead, VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite); - AddDependency (0, 1, - VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.BottomOfPipe, - VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.MemoryRead); - - Activate (); - } - public void Activate () { - if (isDisposed) { - GC.ReRegisterForFinalize (this); - isDisposed = false; - } - using (NativeList spDescs = new NativeList ((uint)subpasses.Count)) { - foreach (SubPass sp in subpasses) - spDescs.Add (sp.SubpassDescription); - - VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.New (); - renderPassInfo.attachmentCount = attachments.Count; - renderPassInfo.pAttachments = attachments.Data; - renderPassInfo.subpassCount = spDescs.Count; - renderPassInfo.pSubpasses = spDescs.Data; - renderPassInfo.dependencyCount = dependencies.Count; - renderPassInfo.pDependencies = dependencies.Data; - - handle = dev.CreateRenderPass (renderPassInfo); - } - } - - //public Framebuffer CreateFramebuffer (SwapChain swapChain) { - // Framebuffer fb; - - // return fb; - //} - - /// /// Begin Render pass with framebuffer extent dimensions /// @@ -174,42 +167,25 @@ namespace VKE { info.renderPass = handle; info.renderArea.extent.width = width; info.renderArea.extent.height = height; - info.clearValueCount = ClearValues.Count; - info.pClearValues = (VkClearValue*) ClearValues.Data.ToPointer(); + info.clearValueCount = (uint)ClearValues.Count; + info.pClearValues = ClearValues.Pin (); info.framebuffer = frameBuffer.handle; vkCmdBeginRenderPass (cmd.Handle, ref info, VkSubpassContents.Inline); + + ClearValues.Unpin (); } public void End (CommandBuffer cmd) { vkCmdEndRenderPass (cmd.Handle); } #region IDisposable Support - private bool isDisposed = false; // Pour détecter les appels redondants - - protected virtual void Dispose (bool disposing) { - if (!isDisposed) { - if (disposing) { - attachments.Dispose (); - ClearValues.Dispose (); - dependencies.Dispose (); - } else - System.Diagnostics.Debug.WriteLine ("A Renderpass has not been disposed."); + protected override void Dispose (bool disposing) { + if (!disposing) + System.Diagnostics.Debug.WriteLine ("VKE Activable RenderPass disposed by finalizer"); + if (state == ActivableState.Activated) dev.DestroyRenderPass (handle); - isDisposed = true; - } - } - - ~RenderPass () { - Dispose (false); - } - public void Dispose () { - if (references>0) - references--; - if (references>0) - return; - Dispose (true); - GC.SuppressFinalize (this); + base.Dispose (disposing); } #endregion } -- cgit v1.3.1-4-g156e