From 752ddea72825faf84e38f2fd10efe9ca0c35a8d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sat, 4 Dec 2021 20:52:08 +0100 Subject: [PATCH] test vk.net ptr proxies --- vke/src/ShaderInfo.cs | 9 +- vke/src/SpecializationConstant.cs | 9 +- vke/src/base/CommandBuffer.cs | 32 ++--- vke/src/base/DescriptorPool.cs | 16 +-- vke/src/base/DescriptorSetWrites.cs | 12 +- vke/src/base/Device.cs | 10 +- vke/src/base/FrameBuffer.cs | 6 +- vke/src/base/GraphicPipeline.cs | 41 +++--- vke/src/base/GraphicPipelineConfig.cs | 14 +- vke/src/base/Instance.cs | 2 +- vke/src/base/PipelineLayout.cs | 24 ++-- vke/src/base/Queue.cs | 185 +++++++++++++------------- vke/src/base/RenderPass.cs | 17 +-- vke/src/base/SubPass.cs | 23 ++-- vke/vke.csproj | 2 +- 15 files changed, 181 insertions(+), 221 deletions(-) diff --git a/vke/src/ShaderInfo.cs b/vke/src/ShaderInfo.cs index ba3c9c1..10291a1 100644 --- a/vke/src/ShaderInfo.cs +++ b/vke/src/ShaderInfo.cs @@ -58,12 +58,11 @@ namespace vke { /// destroyed on Dispose. /// public ShaderInfo (VkShaderStageFlags stageFlags, VkShaderModule module, SpecializationInfo specializationInfo = null, string entryPoint = "main") { - EntryPoint = new FixedUtf8String (entryPoint); - info.stage = stageFlags; - info.pName = EntryPoint; + info.pName = entryPoint; info.module = module; - info.pSpecializationInfo = (specializationInfo == null) ? IntPtr.Zero : specializationInfo.InfosPtr; + if (specializationInfo != null) + info.pSpecializationInfo = specializationInfo.infos; } #region IDisposable Support @@ -71,7 +70,7 @@ namespace vke { protected virtual void Dispose (bool disposing) { if (!disposedValue) { - if (disposing) + if (disposing) EntryPoint.Dispose (); else System.Diagnostics.Debug.WriteLine ("VKE ShaderInfo disposed by finalizer"); diff --git a/vke/src/SpecializationConstant.cs b/vke/src/SpecializationConstant.cs index 66b1619..a2a095e 100644 --- a/vke/src/SpecializationConstant.cs +++ b/vke/src/SpecializationConstant.cs @@ -51,9 +51,8 @@ namespace vke { IntPtr pData; VkSpecializationMapEntry[] entries; - VkSpecializationInfo infos; + public VkSpecializationInfo infos; - public IntPtr InfosPtr { get; private set; } #region CTOR public SpecializationInfo (params SpecializationConstant[] constants) { @@ -73,17 +72,15 @@ namespace vke { } infos = new VkSpecializationInfo { - mapEntryCount = (uint)constants.Length, - pMapEntries = entries.Pin (), + pMapEntries = entries, pData = pData, dataSize = (UIntPtr)totSize }; - InfosPtr = infos.Pin (); } #endregion public void Dispose () { - infos.Unpin (); + infos.Dispose(); Marshal.FreeHGlobal (pData); entries.Unpin (); } diff --git a/vke/src/base/CommandBuffer.cs b/vke/src/base/CommandBuffer.cs index 0eef206..6c45cc0 100644 --- a/vke/src/base/CommandBuffer.cs +++ b/vke/src/base/CommandBuffer.cs @@ -21,9 +21,9 @@ namespace vke { inheri.queryFlags = queryFlags; inheri.pipelineStatistics = statFlags; VkCommandBufferBeginInfo cmdBufInfo = new VkCommandBufferBeginInfo (usage); - cmdBufInfo.pInheritanceInfo = inheri.Pin (); + cmdBufInfo.pInheritanceInfo = inheri; Utils.CheckResult (vkBeginCommandBuffer (handle, ref cmdBufInfo)); - inheri.Unpin (); + cmdBufInfo.Dispose(); } } public class PrimaryCommandBuffer : CommandBuffer { @@ -38,27 +38,15 @@ namespace vke { /// Fence. public void Submit (VkQueue queue, VkSemaphore wait = default, VkSemaphore signal = default, Fence fence = null) { VkSubmitInfo submit_info = VkSubmitInfo.New (); + submit_info.pWaitDstStageMask = VkPipelineStageFlags.ColorAttachmentOutput; + if (signal != VkSemaphore.Null) + submit_info.pSignalSemaphores = signal; + if (wait != VkSemaphore.Null) + submit_info.pWaitSemaphores = wait; + submit_info.pCommandBuffers = handle; - IntPtr dstStageMask = Marshal.AllocHGlobal (sizeof (uint)); - Marshal.WriteInt32 (dstStageMask, (int)VkPipelineStageFlags.ColorAttachmentOutput); - - using (PinnedObjects pctx = new PinnedObjects ()) { - submit_info.pWaitDstStageMask = dstStageMask; - if (signal != VkSemaphore.Null) { - submit_info.signalSemaphoreCount = 1; - submit_info.pSignalSemaphores = signal.Pin (pctx); - } - if (wait != VkSemaphore.Null) { - submit_info.waitSemaphoreCount = 1; - submit_info.pWaitSemaphores = wait.Pin (pctx); - } - - submit_info.commandBufferCount = 1; - submit_info.pCommandBuffers = handle.Pin (pctx); - - Utils.CheckResult (vkQueueSubmit (queue, 1, ref submit_info, fence)); - } - Marshal.FreeHGlobal (dstStageMask); + Utils.CheckResult (vkQueueSubmit (queue, 1, ref submit_info, fence)); + submit_info.Dispose(); } /// /// Put the command buffer in the recording state. diff --git a/vke/src/base/DescriptorPool.cs b/vke/src/base/DescriptorPool.cs index 4cbafd9..b832f6a 100644 --- a/vke/src/base/DescriptorPool.cs +++ b/vke/src/base/DescriptorPool.cs @@ -23,7 +23,7 @@ namespace vke { /// /// the logical device that create the pool. /// maximum number of descriptor sets that can be allocated from the pool - public DescriptorPool (Device device, uint maxSets = 1) : base (device) { + public DescriptorPool (Device device, uint maxSets = 1) : base (device) { MaxSets = maxSets; } /// @@ -37,19 +37,18 @@ namespace vke { PoolSizes.AddRange (poolSizes); - Activate (); + Activate (); } #endregion public sealed override void Activate () { - if (state != ActivableState.Activated) { + if (state != ActivableState.Activated) { VkDescriptorPoolCreateInfo info = VkDescriptorPoolCreateInfo.New(); - info.poolSizeCount = (uint)PoolSizes.Count; - info.pPoolSizes = PoolSizes.Pin (); + info.pPoolSizes = PoolSizes; info.maxSets = MaxSets; Utils.CheckResult (vkCreateDescriptorPool (Dev.Handle, ref info, IntPtr.Zero, out handle)); - PoolSizes.Unpin (); + info.Dispose(); } base.Activate (); } @@ -66,12 +65,11 @@ namespace vke { public void Allocate (DescriptorSet descriptorSet) { VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo.New(); allocInfo.descriptorPool = handle; - allocInfo.descriptorSetCount = (uint)descriptorSet.descriptorSetLayouts.Count; - allocInfo.pSetLayouts = descriptorSet.descriptorSetLayouts.Pin(); + allocInfo.pSetLayouts = descriptorSet.descriptorSetLayouts; Utils.CheckResult (vkAllocateDescriptorSets (Dev.Handle, ref allocInfo, out descriptorSet.handle)); - descriptorSet.descriptorSetLayouts.Unpin (); + allocInfo.Dispose(); } public void FreeDescriptorSet (params DescriptorSet[] descriptorSets) { if (descriptorSets.Length == 1) { diff --git a/vke/src/base/DescriptorSetWrites.cs b/vke/src/base/DescriptorSetWrites.cs index 604f718..59d7a40 100644 --- a/vke/src/base/DescriptorSetWrites.cs +++ b/vke/src/base/DescriptorSetWrites.cs @@ -116,15 +116,14 @@ namespace vke { descPtrArray.Add (descriptors[i].Pin (pinCtx)); i++; } - pDescriptors = descPtrArray.Pin (pinCtx); } else { pDescriptors = descriptors[i].Pin (pinCtx); i++; } - if (descriptors[firstDescriptor] is VkDescriptorBufferInfo) + /*if (descriptors[firstDescriptor] is VkDescriptorBufferInfo) wds.pBufferInfo = pDescriptors; else if (descriptors[firstDescriptor] is VkDescriptorImageInfo) - wds.pImageInfo = pDescriptors; + wds.pImageInfo = pDescriptors;*/ WriteDescriptorSets[wdsPtr] = wds; wdsPtr++; @@ -156,10 +155,11 @@ namespace vke { pDescriptors = descriptors[i].Pin (pinCtx); i++; } + /* if (descriptors[firstDescriptor] is VkDescriptorBufferInfo) wds.pBufferInfo = pDescriptors; else if (descriptors[firstDescriptor] is VkDescriptorImageInfo) - wds.pImageInfo = pDescriptors; + wds.pImageInfo = pDescriptors;*/ WriteDescriptorSets[wdsPtr] = wds; wdsPtr++; @@ -190,7 +190,7 @@ namespace vke { wds.descriptorCount = binding.descriptorCount; wds.dstBinding = binding.binding; wds.dstSet = destSet.handle; - wds.pBufferInfo = descriptor.Pin (); + wds.pBufferInfo = descriptor; WriteDescriptorSets.Add (wds); } @@ -202,7 +202,7 @@ namespace vke { wds.descriptorCount = binding.descriptorCount; wds.dstBinding = binding.binding; wds.dstSet = destSet.handle; - wds.pImageInfo = descriptor.Pin (); + wds.pImageInfo = descriptor; WriteDescriptorSets.Add (wds); } diff --git a/vke/src/base/Device.cs b/vke/src/base/Device.cs index cb96241..4288cc1 100644 --- a/vke/src/base/Device.cs +++ b/vke/src/base/Device.cs @@ -75,9 +75,8 @@ namespace vke { } VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo.New (); - deviceCreateInfo.queueCreateInfoCount = (uint)qInfos.Count; - deviceCreateInfo.pQueueCreateInfos = qInfos.Pin (); - deviceCreateInfo.pEnabledFeatures = enabledFeatures.Pin (); + deviceCreateInfo.pQueueCreateInfos = qInfos; + deviceCreateInfo.pEnabledFeatures = enabledFeatures; if (deviceExtensions.Count > 0) { deviceCreateInfo.enabledExtensionCount = (uint)deviceExtensions.Count; @@ -85,8 +84,9 @@ namespace vke { } Utils.CheckResult (vkCreateDevice (phy.Handle, ref deviceCreateInfo, IntPtr.Zero, out dev)); - qInfos.Unpin (); - enabledFeatures.Unpin (); + + deviceCreateInfo.Dispose(); + foreach (List fa in prioritiesLists) fa.Unpin (); diff --git a/vke/src/base/FrameBuffer.cs b/vke/src/base/FrameBuffer.cs index 5885024..5cb9332 100644 --- a/vke/src/base/FrameBuffer.cs +++ b/vke/src/base/FrameBuffer.cs @@ -87,15 +87,15 @@ namespace vke { public sealed override void Activate () { if (state != ActivableState.Activated) { VkImageView[] views = attachments.Select (a => a.Descriptor.imageView).ToArray (); - createInfo.attachmentCount = (uint)views.Length; - createInfo.pAttachments = views.Pin (); + createInfo.pAttachments = views; if (PNext != null) createInfo.pNext = PNext.GetPointer(); Utils.CheckResult (vkCreateFramebuffer (renderPass.Dev.Handle, ref createInfo, IntPtr.Zero, out handle)); - views.Unpin (); + createInfo.Dispose(); + if (PNext != null) PNext.ReleasePointer (); diff --git a/vke/src/base/GraphicPipeline.cs b/vke/src/base/GraphicPipeline.cs index dc68c3d..88c5fc7 100644 --- a/vke/src/base/GraphicPipeline.cs +++ b/vke/src/base/GraphicPipeline.cs @@ -57,54 +57,49 @@ namespace vke { colorBlendInfo.logicOpEnable = cfg.ColorBlendLogicOpEnable; colorBlendInfo.logicOp = cfg.ColorBlendLogicOp; colorBlendInfo.blendConstants = cfg.ColorBlendConstants; - colorBlendInfo.attachmentCount = (uint)cfg.blendAttachments.Count; - colorBlendInfo.pAttachments = cfg.blendAttachments.Pin (pctx); + colorBlendInfo.pAttachments = cfg.blendAttachments; VkPipelineDynamicStateCreateInfo dynStatesInfo = VkPipelineDynamicStateCreateInfo.New (); - dynStatesInfo.dynamicStateCount = (uint)cfg.dynamicStates.Count; - dynStatesInfo.pDynamicStates = cfg.dynamicStates.Cast ().ToArray ().Pin (pctx); + dynStatesInfo.pDynamicStates = cfg.dynamicStates; VkPipelineVertexInputStateCreateInfo vertInputInfo = VkPipelineVertexInputStateCreateInfo.New (); - vertInputInfo.vertexBindingDescriptionCount = (uint)cfg.vertexBindings.Count; - vertInputInfo.pVertexBindingDescriptions = cfg.vertexBindings.Pin (pctx); - vertInputInfo.vertexAttributeDescriptionCount = (uint)cfg.vertexAttributes.Count; - vertInputInfo.pVertexAttributeDescriptions = cfg.vertexAttributes.Pin (pctx); + vertInputInfo.pVertexBindingDescriptions = cfg.vertexBindings; + vertInputInfo.pVertexAttributeDescriptions = cfg.vertexAttributes; VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.New (); if (cfg.Viewports.Count > 0) { - viewportState.viewportCount = (uint)cfg.Viewports.Count; - viewportState.pViewports = cfg.Viewports.Pin (pctx); + viewportState.pViewports = cfg.Viewports; } else viewportState.viewportCount = 1; if (cfg.Scissors.Count > 0) { - viewportState.scissorCount = (uint)cfg.Scissors.Count; - viewportState.pScissors = cfg.Scissors.Pin (pctx); + viewportState.pScissors = cfg.Scissors; } else viewportState.scissorCount = 1; VkGraphicsPipelineCreateInfo info = VkGraphicsPipelineCreateInfo.New (); info.renderPass = RenderPass.handle; info.layout = Layout.handle; - info.pVertexInputState = vertInputInfo.Pin (pctx); - info.pInputAssemblyState = cfg.inputAssemblyState.Pin (pctx); - info.pRasterizationState = cfg.rasterizationState.Pin (pctx); - info.pColorBlendState = colorBlendInfo.Pin (pctx); - info.pMultisampleState = cfg.multisampleState.Pin (pctx); - info.pViewportState = viewportState.Pin (pctx); - info.pDepthStencilState = cfg.depthStencilState.Pin (pctx); - info.pDynamicState = dynStatesInfo.Pin (pctx); - info.stageCount = (uint)cfg.Shaders.Count; - info.pStages = shaderStages.Pin (pctx); + info.pVertexInputState = vertInputInfo; + info.pInputAssemblyState = cfg.inputAssemblyState; + info.pRasterizationState = cfg.rasterizationState; + info.pColorBlendState = colorBlendInfo; + info.pMultisampleState = cfg.multisampleState; + info.pViewportState = viewportState; + info.pDepthStencilState = cfg.depthStencilState; + info.pDynamicState = dynStatesInfo; + info.pStages = shaderStages; info.subpass = cfg.SubpassIndex; if (enableTesselation) { VkPipelineTessellationStateCreateInfo tessellationInfo = VkPipelineTessellationStateCreateInfo.New(); tessellationInfo.patchControlPoints = cfg.TessellationPatchControlPoints; - info.pTessellationState = tessellationInfo.Pin (pctx); + info.pTessellationState = tessellationInfo; } Utils.CheckResult (vkCreateGraphicsPipelines (Dev.Handle, Cache == null ? VkPipelineCache.Null : Cache.handle, 1, ref info, IntPtr.Zero, out handle)); + + info.Dispose (); } } base.Activate (); diff --git a/vke/src/base/GraphicPipelineConfig.cs b/vke/src/base/GraphicPipelineConfig.cs index 1269f5c..97be93b 100644 --- a/vke/src/base/GraphicPipelineConfig.cs +++ b/vke/src/base/GraphicPipelineConfig.cs @@ -37,21 +37,21 @@ namespace vke { public VkPipelineBindPoint bindPoint = VkPipelineBindPoint.Graphics; public VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo.New (); public VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo.New (); - public IList Viewports = new List (); - public IList Scissors = new List (); + public List Viewports = new List (); + public List Scissors = new List (); public VkPipelineDepthStencilStateCreateInfo depthStencilState = VkPipelineDepthStencilStateCreateInfo.New (); public VkPipelineMultisampleStateCreateInfo multisampleState = VkPipelineMultisampleStateCreateInfo.New (); - public IList blendAttachments = new List (); - public IList dynamicStates = new List (); - public IList vertexBindings = new List (); - public IList vertexAttributes = new List (); + public List blendAttachments = new List (); + public List dynamicStates = new List (); + public List vertexBindings = new List (); + public List vertexAttributes = new List (); /// /// List of ShaderInfo's used to in this pipeline configuration. Those shaders have to be Disposed /// after pipeline creation from this configuration. The 'DisposeShaders' helper method with clear the list. /// To replace a single shader between two use of this configuration object to create two different pipelines, use /// the 'ReplaceShader' helper method to automatically dispose the replace shader. /// - public IList Shaders = new List (); + public List Shaders = new List (); public VkBool32 ColorBlendLogicOpEnable = false; public VkLogicOp ColorBlendLogicOp; public Vector4 ColorBlendConstants; diff --git a/vke/src/base/Instance.cs b/vke/src/base/Instance.cs index 4301963..420aca4 100644 --- a/vke/src/base/Instance.cs +++ b/vke/src/base/Instance.cs @@ -86,7 +86,7 @@ namespace vke { }; VkInstanceCreateInfo instanceCreateInfo = VkInstanceCreateInfo.New (); - instanceCreateInfo.pApplicationInfo = appInfo.Pin (pctx); + instanceCreateInfo.pApplicationInfo = appInfo; if (instanceExtensions.Count > 0) { instanceCreateInfo.enabledExtensionCount = (uint)instanceExtensions.Count; diff --git a/vke/src/base/PipelineLayout.cs b/vke/src/base/PipelineLayout.cs index 206a0ef..42a0975 100644 --- a/vke/src/base/PipelineLayout.cs +++ b/vke/src/base/PipelineLayout.cs @@ -20,7 +20,7 @@ namespace vke { #region CTORS public PipelineLayout (Device device) : base (device) { } - public PipelineLayout (Device device, VkPushConstantRange pushConstantRange, params DescriptorSetLayout[] descriptorSetLayouts) + public PipelineLayout (Device device, VkPushConstantRange pushConstantRange, params DescriptorSetLayout[] descriptorSetLayouts) : this (device, descriptorSetLayouts) { PushConstantRanges.Add (pushConstantRange); } @@ -31,38 +31,34 @@ namespace vke { } public PipelineLayout (Device device, params DescriptorSetLayout[] descriptorSetLayouts) :this (device) { - + if (descriptorSetLayouts.Length > 0) DescriptorSetLayouts.AddRange (descriptorSetLayouts); } #endregion - public void AddPushConstants (params VkPushConstantRange[] pushConstantRanges) { + public void AddPushConstants (params VkPushConstantRange[] pushConstantRanges) { foreach (VkPushConstantRange pcr in pushConstantRanges) PushConstantRanges.Add (pcr); } public override void Activate () { if (state != ActivableState.Activated) { - foreach (DescriptorSetLayout dsl in DescriptorSetLayouts) + foreach (DescriptorSetLayout dsl in DescriptorSetLayouts) dsl.Activate (); VkPipelineLayoutCreateInfo info = VkPipelineLayoutCreateInfo.New(); VkDescriptorSetLayout[] dsls = DescriptorSetLayouts.Select (dsl => dsl.handle).ToArray (); if (dsls.Length > 0) { - info.setLayoutCount = (uint)dsls.Length; - info.pSetLayouts = dsls.Pin (); + info.pSetLayouts = dsls; } if (PushConstantRanges.Count > 0) { - info.pushConstantRangeCount = (uint)PushConstantRanges.Count; - info.pPushConstantRanges = PushConstantRanges.Pin(); + info.pPushConstantRanges = PushConstantRanges; } Utils.CheckResult (vkCreatePipelineLayout (Dev.Handle, ref info, IntPtr.Zero, out handle)); - if (dsls.Length > 0) - dsls.Unpin (); - if (PushConstantRanges.Count > 0) - PushConstantRanges.Unpin (); + info.Dispose(); + } base.Activate (); } @@ -75,8 +71,8 @@ namespace vke { protected override void Dispose (bool disposing) { if (state == ActivableState.Activated) { if (disposing) { - foreach (DescriptorSetLayout dsl in DescriptorSetLayouts) - dsl.Dispose (); + foreach (DescriptorSetLayout dsl in DescriptorSetLayouts) + dsl.Dispose (); } else System.Diagnostics.Debug.WriteLine ("VKE Activable PipelineLayout disposed by finalizer"); diff --git a/vke/src/base/Queue.cs b/vke/src/base/Queue.cs index bdcab88..fd4bbaa 100644 --- a/vke/src/base/Queue.cs +++ b/vke/src/base/Queue.cs @@ -8,77 +8,72 @@ using static Vulkan.Vk; namespace vke { - public class PresentQueue : Queue { - public readonly VkSurfaceKHR Surface; - - public PresentQueue (Device _dev, VkQueueFlags requestedFlags, VkSurfaceKHR _surface, float _priority = 0.0f) { - dev = _dev; - priority = _priority; - Surface = _surface; - - qFamIndex = searchQFamily (requestedFlags); - dev.queues.Add (this); - } - - uint searchQFamily (VkQueueFlags requestedFlags) { - //search for dedicated Q - for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { - if (dev.phy.QueueFamilies[i].queueFlags == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface)) - return i; - } - //search Q having flags - for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { - if ((dev.phy.QueueFamilies[i].queueFlags & requestedFlags) == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface)) - return i; - } - - throw new Exception (string.Format ("No Queue with flags {0} found", requestedFlags)); - } - - public void Present (VkPresentInfoKHR present) { - Utils.CheckResult (vkQueuePresentKHR (handle, ref present)); - } - public void Present (SwapChain swapChain, VkSemaphore wait) { - VkPresentInfoKHR present = VkPresentInfoKHR.New(); - - uint idx = swapChain.currentImageIndex; - VkSwapchainKHR sc = swapChain.Handle; - present.swapchainCount = 1; - present.pSwapchains = sc.Pin(); - present.waitSemaphoreCount = 1; - present.pWaitSemaphores = wait.Pin(); - present.pImageIndices = idx.Pin(); - - vkQueuePresentKHR (handle, ref present); - - sc.Unpin (); - wait.Unpin (); - idx.Unpin (); - } - } - - public class Queue { - - protected VkQueue handle; - internal Device dev; + public class PresentQueue : Queue { + public readonly VkSurfaceKHR Surface; + + public PresentQueue (Device _dev, VkQueueFlags requestedFlags, VkSurfaceKHR _surface, float _priority = 0.0f) { + dev = _dev; + priority = _priority; + Surface = _surface; + + qFamIndex = searchQFamily (requestedFlags); + dev.queues.Add (this); + } + + uint searchQFamily (VkQueueFlags requestedFlags) { + //search for dedicated Q + for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { + if (dev.phy.QueueFamilies[i].queueFlags == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface)) + return i; + } + //search Q having flags + for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { + if ((dev.phy.QueueFamilies[i].queueFlags & requestedFlags) == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface)) + return i; + } + + throw new Exception (string.Format ("No Queue with flags {0} found", requestedFlags)); + } + + public void Present (VkPresentInfoKHR present) { + Utils.CheckResult (vkQueuePresentKHR (handle, ref present)); + } + public void Present (SwapChain swapChain, VkSemaphore wait) { + VkPresentInfoKHR present = VkPresentInfoKHR.New(); + + uint idx = swapChain.currentImageIndex; + VkSwapchainKHR sc = swapChain.Handle; + present.pSwapchains = sc; + present.pWaitSemaphores = wait; + + vkQueuePresentKHR (handle, ref present); + + present.Dispose(); + } + } + + public class Queue { + + protected VkQueue handle; + internal Device dev; public Device Dev => dev; - VkQueueFlags flags => dev.phy.QueueFamilies[qFamIndex].queueFlags; - public uint qFamIndex; - public uint index;//index in queue family - public float priority; - public VkQueue Handle => handle; - - protected Queue () { } - public Queue (Device _dev, VkQueueFlags requestedFlags, float _priority = 0.0f) { - dev = _dev; - priority = _priority; - - qFamIndex = searchQFamily (requestedFlags); - dev.queues.Add (this); - } - public CommandPool CreateCommandPool (VkCommandPoolCreateFlags flags = 0) - => new CommandPool (dev, qFamIndex, flags); + VkQueueFlags flags => dev.phy.QueueFamilies[qFamIndex].queueFlags; + public uint qFamIndex; + public uint index;//index in queue family + public float priority; + public VkQueue Handle => handle; + + protected Queue () { } + public Queue (Device _dev, VkQueueFlags requestedFlags, float _priority = 0.0f) { + dev = _dev; + priority = _priority; + + qFamIndex = searchQFamily (requestedFlags); + dev.queues.Add (this); + } + public CommandPool CreateCommandPool (VkCommandPoolCreateFlags flags = 0) + => new CommandPool (dev, qFamIndex, flags); /// /// End command recording, submit, and wait queue idle /// @@ -89,30 +84,30 @@ namespace vke { if (freeCommandBuffer) cmd.Free (); } - public void Submit (PrimaryCommandBuffer cmd, VkSemaphore wait = default, VkSemaphore signal = default, Fence fence = null) { - cmd.Submit (handle, wait, signal, fence); - } - public void WaitIdle () { - Utils.CheckResult (vkQueueWaitIdle (handle)); - } - - uint searchQFamily (VkQueueFlags requestedFlags) { - //search for dedicated Q - for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { - if (dev.phy.QueueFamilies[i].queueFlags == requestedFlags) - return i; - } - //search Q having flags - for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { - if ((dev.phy.QueueFamilies[i].queueFlags & requestedFlags) == requestedFlags) - return i; - } - - throw new Exception (string.Format ("No Queue with flags {0} found", requestedFlags)); - } - - internal void updateHandle () { - vkGetDeviceQueue (Dev.Handle, qFamIndex, index, out handle); - } - } + public void Submit (PrimaryCommandBuffer cmd, VkSemaphore wait = default, VkSemaphore signal = default, Fence fence = null) { + cmd.Submit (handle, wait, signal, fence); + } + public void WaitIdle () { + Utils.CheckResult (vkQueueWaitIdle (handle)); + } + + uint searchQFamily (VkQueueFlags requestedFlags) { + //search for dedicated Q + for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { + if (dev.phy.QueueFamilies[i].queueFlags == requestedFlags) + return i; + } + //search Q having flags + for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) { + if ((dev.phy.QueueFamilies[i].queueFlags & requestedFlags) == requestedFlags) + return i; + } + + throw new Exception (string.Format ("No Queue with flags {0} found", requestedFlags)); + } + + internal void updateHandle () { + vkGetDeviceQueue (Dev.Handle, qFamIndex, index, out handle); + } + } } diff --git a/vke/src/base/RenderPass.cs b/vke/src/base/RenderPass.cs index be32781..27d8687 100644 --- a/vke/src/base/RenderPass.cs +++ b/vke/src/base/RenderPass.cs @@ -105,12 +105,9 @@ namespace vke { 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 (); + renderPassInfo.pAttachments = attachments; + renderPassInfo.pSubpasses = spDescs; + renderPassInfo.pDependencies = dependencies; if (PNext != null) renderPassInfo.pNext = PNext.GetPointer (); @@ -121,9 +118,8 @@ namespace vke { if (PNext != null) PNext.ReleasePointer (); - attachments.Unpin (); - spDescs.Unpin (); - dependencies.Unpin (); + + renderPassInfo.Dispose (); } base.Activate (); } @@ -207,8 +203,7 @@ namespace vke { info.renderPass = handle; info.renderArea.extent.width = width; info.renderArea.extent.height = height; - info.clearValueCount = (uint)ClearValues.Count; - info.pClearValues = ClearValues.Pin (); + info.pClearValues = ClearValues; info.framebuffer = frameBuffer.handle; vkCmdBeginRenderPass (cmd.Handle, ref info, contents); diff --git a/vke/src/base/SubPass.cs b/vke/src/base/SubPass.cs index a7b511d..828e5a6 100644 --- a/vke/src/base/SubPass.cs +++ b/vke/src/base/SubPass.cs @@ -16,7 +16,7 @@ namespace vke { public SubPass () { } public SubPass (params VkImageLayout[] layouts) { - for (uint i = 0; i < layouts.Length; i++) + for (uint i = 0; i < layouts.Length; i++) AddColorReference (i, layouts[i]); } @@ -54,33 +54,30 @@ namespace vke { VkSubpassDescription subpassDescription = new VkSubpassDescription (); subpassDescription.pipelineBindPoint = VkPipelineBindPoint.Graphics; if (colorRefs.Count > 0) { - subpassDescription.colorAttachmentCount = (uint)colorRefs.Count; - subpassDescription.pColorAttachments = colorRefs.Pin(); ; + subpassDescription.pColorAttachments = colorRefs; } if (inputRefs.Count > 0) { - subpassDescription.inputAttachmentCount = (uint)inputRefs.Count; - subpassDescription.pInputAttachments = inputRefs.Pin (); ; + subpassDescription.pInputAttachments = inputRefs; ; } if (preservedRefs.Count > 0) { - subpassDescription.preserveAttachmentCount = (uint)preservedRefs.Count; subpassDescription.pPreserveAttachments = preservedRefs.Pin (); ; } if (resolveRefs.Count > 0) - subpassDescription.pResolveAttachments = resolveRefs.Pin (); + subpassDescription.pResolveAttachments = resolveRefs; if (DepthReference.HasValue) - subpassDescription.pDepthStencilAttachment = DepthReference.Value.Pin(); + subpassDescription.pDepthStencilAttachment = DepthReference.Value; return subpassDescription; - } + } } internal void UnpinLists () { - if (colorRefs.Count > 0) - colorRefs.Unpin (); - if (inputRefs.Count > 0) + if (colorRefs.Count > 0) + colorRefs.Unpin (); + if (inputRefs.Count > 0) inputRefs.Unpin (); - if (preservedRefs.Count > 0) + if (preservedRefs.Count > 0) preservedRefs.Unpin (); if (resolveRefs.Count > 0) resolveRefs.Unpin (); diff --git a/vke/vke.csproj b/vke/vke.csproj index 1bc09be..eff1a34 100644 --- a/vke/vke.csproj +++ b/vke/vke.csproj @@ -48,7 +48,7 @@ - + -- 2.47.3