Ejemplo n.º 1
0
        public VkResult Submit(int submitCount, VkSubmitInfo[] pSubmits, VkFence fence)
        {
            for (int i = 0; i < submitCount; i++)
            {
                var submit = pSubmits[i];

                if (submit.waitSemaphoreCount > 0)
                {
                    SoftwareSemaphore.WaitAll(submit.waitSemaphoreCount, submit.pWaitSemaphores);
                }

                for (int cmdIndex = 0; cmdIndex < submit.commandBufferCount; cmdIndex++)
                {
                    var cmdBuffer = (SoftwareCommandBuffer)submit.pCommandBuffers[cmdIndex];
                    cmdBuffer.Execute();
                }

                if (submit.signalSemaphoreCount > 0)
                {
                    SoftwareSemaphore.SignalAll(submit.signalSemaphoreCount, submit.pSignalSemaphores);
                }
            }

            ((SoftwareFence)fence)?.Signal();

            return(VkResult.VK_SUCCESS);
        }
Ejemplo n.º 2
0
        public unsafe Fence(Device device, bool isSignaled = false)
        {
            _device = device;
            var createInfo = new VkFenceCreateInfo
            {
                sType = VkStructureType.FenceCreateInfo,
                flags = (
                    isSignaled ?
                    VkFenceCreateFlags.Signaled :
                    VkFenceCreateFlags.None
                    )
            };

            VkFence fence;

            if (VulkanNative.vkCreateFence(
                    device.Handle,
                    &createInfo,
                    null,
                    &fence
                    ) != VkResult.Success)
            {
                throw new Exception("failed to create fence");
            }
            _handle = fence;
        }
Ejemplo n.º 3
0
        public TransferManager(GraphicsDevice graphics)
        {
            Graphics = graphics;
            Buffer   = new((ulong)INITIAL_HOST_SIZE.B);
            Buffer.CanDestroyImmediately = true;             // Safe to do since all operations with this are synchronous

            // Create command objects
            VkCommandPoolCreateInfo      cpci = new(VkCommandPoolCreateFlags.Transient, Graphics.GraphicsQueue.FamilyIndex);
            VulkanHandle <VkCommandPool> poolHandle;

            Graphics.VkDevice.CreateCommandPool(&cpci, null, &poolHandle)
            .Throw("Failed to create command pool for transfer");
            _pool = new(poolHandle, Graphics.VkDevice);
            VkCommandBufferAllocateInfo    cbai = new(_pool, VkCommandBufferLevel.Primary, 1);
            VulkanHandle <VkCommandBuffer> cmdHandle;

            Graphics.VkDevice.AllocateCommandBuffers(&cbai, &cmdHandle)
            .Throw("Failed to allocate command buffer for transfer");
            _cmd = new(cmdHandle, _pool);
            VkFenceCreateInfo      fci = new(VkFenceCreateFlags.NoFlags);
            VulkanHandle <VkFence> fenceHandle;

            Graphics.VkDevice.CreateFence(&fci, null, &fenceHandle)
            .Throw("Failed to create fence for transfer");
            _fence = new(fenceHandle, Graphics.VkDevice);
        }
Ejemplo n.º 4
0
        public void Render()
        {
            if (!isInitialized)
            {
                return;
            }

            if (this.submitInfos == null)
            {
                InitRenderParams();
            }

            VkDevice    device = this.device; VkSwapchainKHR swapchain = this.swapchain;
            VkSemaphore semaphore = this.vkSemaphore; VkFence fence = this.vkFence;
            VkQueue     queue = this.vkQueue;
            //uint nextIndex = device.AcquireNextImageKHR(swapchain, ulong.MaxValue, semaphore);
            UInt32 nextIndex;

            vkAPI.vkAcquireNextImageKHR(device, swapchain, ulong.MaxValue,
                                        semaphore, new VkFence(), &nextIndex).Check();
            //device.ResetFence(fence);
            vkAPI.vkResetFences(device, 1, &fence).Check();

            //queue.Submit(ref this.submitInfos[nextIndex], fence);
            //VkSubmitInfo submitInfo = this.submitInfos[nextIndex];
            //vkAPI.vkQueueSubmit(queue, 1, &submitInfo, fence).Check();
            vkAPI.vkQueueSubmit(queue, 1, &this.submitInfos[nextIndex], fence).Check();
            //device.WaitForFence(fence, true, 100000000);
            vkAPI.vkWaitForFences(device, 1, &fence, true, 100000000).Check();
            //queue.PresentKHR(ref this.presentInfos[nextIndex]);
            //VkPresentInfoKHR presentInfo = this.presentInfos[nextIndex];
            //vkAPI.vkQueuePresentKHR(queue, &presentInfo).Check();
            vkAPI.vkQueuePresentKHR(queue, &this.presentInfos[nextIndex]).Check();
        }
Ejemplo n.º 5
0
 public static extern VkResult AcquireNextImageKHR(
     VkDevice device,
     VkSwapchainKHR swapchain,
     ulong timeout,
     VkSemaphore semaphore,
     VkFence fence,
     out uint pImageIndex
     );
Ejemplo n.º 6
0
        public void Submit(CommandBuffer[] buffers, VkSemaphore[] wait, VkPipelineStageFlag[] waitStages,
                           VkSemaphore[] signal, VkFence submit)
        {
            unsafe
            {
                // ReSharper disable once PossibleNullReferenceException
                Debug.Assert((waitStages == null && wait == null) || waitStages.Length == wait.Length);
                var arrayBuffers = buffers.Where(x => !(x is CommandBufferPooledExclusiveUse)).Select(x =>
                {
                    x.AssertBuilt();
                    return(x.Handle);
                }).ToArray();
                var pooledBuffers = buffers.OfType <CommandBufferPooledExclusiveUse>().ToArray();
                Debug.Assert(pooledBuffers.Length == 0 || submit == VkFence.Null,
                             "Can't use custom submit fence on pooled buffers");
                fixed(VkSemaphore *waitPtr = wait)
                fixed(VkPipelineStageFlag * waitStagePtr = waitStages)
                fixed(VkSemaphore * signalPtr            = signal)
                {
                    if (arrayBuffers.Length > 0)
                        fixed(VkCommandBuffer *buffer = arrayBuffers)
                        {
                            var info = new VkSubmitInfo()
                            {
                                SType = VkStructureType.SubmitInfo,
                                PNext = IntPtr.Zero,
                                CommandBufferCount   = (uint)arrayBuffers.Length,
                                PCommandBuffers      = buffer,
                                SignalSemaphoreCount = (uint)(signal?.Length ?? 0),
                                PSignalSemaphores    = signalPtr,
                                WaitSemaphoreCount   = (uint)(wait?.Length ?? 0),
                                PWaitSemaphores      = waitPtr,
                                PWaitDstStageMask    = waitStagePtr,
                            };

                            VkException.Check(VkQueue.vkQueueSubmit(Handle, 1, &info, submit));
                        }
                    foreach (var pooled in pooledBuffers)
                    {
                        var handle = pooled.Handle;
                        var info   = new VkSubmitInfo()
                        {
                            SType = VkStructureType.SubmitInfo,
                            PNext = IntPtr.Zero,
                            CommandBufferCount   = 1,
                            PCommandBuffers      = &handle,
                            SignalSemaphoreCount = (uint)(signal?.Length ?? 0),
                            PSignalSemaphores    = signalPtr,
                            WaitSemaphoreCount   = (uint)(wait?.Length ?? 0),
                            PWaitSemaphores      = waitPtr,
                            PWaitDstStageMask    = waitStagePtr,
                        };
                        pooled.DoSubmit(Handle, info);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public uint AcquireNextImage(VkSemaphore semaphore, VkFence fence, ulong timeout = ulong.MaxValue)
 {
     unsafe
     {
         uint index = uint.MaxValue;
         Device.Handle.AcquireNextImageKHR(Handle, timeout, semaphore, fence, &index);
         return(index);
     }
 }
Ejemplo n.º 8
0
        private void DisposeVulkanFence(VkFence vulkanFence)
        {
            AssertDisposing(_state);

            if (vulkanFence != VK_NULL_HANDLE)
            {
                vkDestroyFence(Device.VulkanDevice, vulkanFence, pAllocator: null);
            }
        }
Ejemplo n.º 9
0
        protected virtual void Draw(Timer timer)
        {
            // Acquire an index of drawing image for this frame.
            uint     nextImageIndex;
            VkResult result = vkAcquireNextImageKHR(Context.Device, Swapchain, ulong.MaxValue, ImageAvailableSemaphore, VkFence.Null, out nextImageIndex);

            result.CheckResult();

            // Use a fence to wait until the command buffer has finished execution before using it again
            VkFence fence = SubmitFences[nextImageIndex];

            result = vkWaitForFences(Context.Device, 1, &fence, false, ulong.MaxValue);
            result.CheckResult();

            result = vkResetFences(Context.Device, 1, &fence);
            result.CheckResult();

            VkSemaphore          signalSemaphore = RenderingFinishedSemaphore;
            VkSemaphore          waitSemaphore   = ImageAvailableSemaphore;
            VkPipelineStageFlags waitStages      = VkPipelineStageFlags.ColorAttachmentOutput;
            VkCommandBuffer      commandBuffer   = CommandBuffers[nextImageIndex];

            VkSubmitInfo submitInfo = new VkSubmitInfo()
            {
                sType = VkStructureType.SubmitInfo,
                waitSemaphoreCount   = 1,
                pWaitSemaphores      = &waitSemaphore,
                pWaitDstStageMask    = &waitStages,
                commandBufferCount   = 1,
                pCommandBuffers      = &commandBuffer,
                signalSemaphoreCount = 1,
                pSignalSemaphores    = &signalSemaphore,
            };

            result = vkQueueSubmit(Context.GraphicsQueue, 1, &submitInfo, SubmitFences[nextImageIndex]);
            result.CheckResult();

            // Present the color output to screen.
            VkSemaphore    waitSemaphoreHandle = RenderingFinishedSemaphore;
            VkSwapchainKHR swapchainHandle     = Swapchain;
            var            nativePresentInfo   = new VkPresentInfoKHR
            {
                sType = VkStructureType.PresentInfoKHR,
                pNext = null,
                waitSemaphoreCount = 1,
                pWaitSemaphores    = &waitSemaphoreHandle,
                swapchainCount     = 1,
                pSwapchains        = &swapchainHandle,
                pImageIndices      = &nextImageIndex
            };

            result = vkQueuePresentKHR(Context.PresentQueue, &nativePresentInfo);
            result.CheckResult();
        }
Ejemplo n.º 10
0
        public void EndOneTimeCommands(VkCommandBuffer cb, VkFence fence)
        {
            vkEndCommandBuffer(cb);

            VkSubmitInfo submitInfo = VkSubmitInfo.New();

            submitInfo.commandBufferCount = 1;
            submitInfo.pCommandBuffers    = &cb;

            vkQueueSubmit(GraphicsQueue, 1, ref submitInfo, fence);
            vkQueueWaitIdle(GraphicsQueue);

            vkFreeCommandBuffers(_device, GraphicsCommandPool, 1, ref cb);
        }
Ejemplo n.º 11
0
        public static void SetDebugMarkerName(this VkFence obj, Device dev, string name)
        {
            if (!dev.DebugMarkersEnabled)
            {
                return;
            }
            VkDebugMarkerObjectNameInfoEXT dmo = new VkDebugMarkerObjectNameInfoEXT(VkDebugReportObjectTypeEXT.FenceEXT,
                                                                                    obj.Handle)
            {
                pObjectName = name.Pin()
            };

            Utils.CheckResult(vkDebugMarkerSetObjectNameEXT(dev.VkDev, ref dmo));
            name.Unpin();
        }
Ejemplo n.º 12
0
        public static void SetDebugMarkerName(this VkFence obj, Device dev, string name)
        {
            if (!dev.debugUtilsEnabled)
            {
                return;
            }
            VkDebugUtilsObjectNameInfoEXT dmo = new VkDebugUtilsObjectNameInfoEXT(VkObjectType.Fence,
                                                                                  (ulong)obj.Handle)
            {
                pObjectName = name.Pin()
            };

            Utils.CheckResult(vkSetDebugUtilsObjectNameEXT(dev.VkDev, ref dmo));
            name.Unpin();
        }
Ejemplo n.º 13
0
        public VkResult AcquireNextImage(ulong timeout, Semaphore semaphore, Fence fence, out uint index)
        {
            VkSemaphore sTemp = VkSemaphore.Null;
            VkFence     fTemp = VkFence.Null;

            if (semaphore != null)
            {
                sTemp = semaphore.Native;
            }
            if (fence != null)
            {
                fTemp = fence.Native;
            }

            var result = Device.Commands.acquireNextImage(Device.Native, swapchain, timeout, sTemp, fTemp, out index);

            return(result);
        }
Ejemplo n.º 14
0
        public static VkResult vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, long timeout, VkSemaphore semaphore, VkFence fence, out int pImageIndex)
        {
            VkPreconditions.CheckNull(device, nameof(device));
            VkPreconditions.CheckNull(swapchain, nameof(swapchain));
            VkPreconditions.CheckRange(timeout, 1, long.MaxValue, nameof(timeout));

            return(GetDevice(device).AcquireNextImage(swapchain, timeout, semaphore, fence, out pImageIndex));
        }
Ejemplo n.º 15
0
 public void Submit(CommandBuffer cmd, VkSemaphore wait = default(VkSemaphore), VkSemaphore signal = default(VkSemaphore), VkFence fence = default(VkFence))
 {
     cmd.Submit(handle, wait, signal, fence);
 }
Ejemplo n.º 16
0
 public static extern VkResult GetFenceStatus(
     VkDevice device,
     VkFence fence
     );
Ejemplo n.º 17
0
 public static extern void DestroyFence(
     VkDevice device,
     VkFence fence,
     IntPtr pAllocator
     );
Ejemplo n.º 18
0
 public static extern VkResult CreateFence(
     VkDevice device,
     ref VkFenceCreateInfo pCreateInfo,
     IntPtr pAllocator,
     out VkFence pFence
     );
Ejemplo n.º 19
0
 public static VkResult vkQueueSubmit(VkQueue queue, VkSubmitInfo submit, VkFence fence)
 {
     return(vkQueueSubmit(queue, 1, &submit, fence));
 }
Ejemplo n.º 20
0
        public void Init(IntPtr hwnd, IntPtr processHandle)
        {
            if (this.isInitialized)
            {
                return;
            }

            this.instance = InitInstance();
            InitDebugCallback(this.instance);
            this.surface          = InitSurface(this.instance, hwnd, processHandle);
            this.vkPhysicalDevice = InitPhysicalDevice(this.instance);
            VkSurfaceFormatKHR surfaceFormat = SelectFormat(this.vkPhysicalDevice, this.surface);

            this.device = CreateDevice(this.vkPhysicalDevice, this.surface);

            this.vkQueue = this.device.GetQueue(0, 0);

            VkSurfaceCapabilitiesKHR surfaceCapabilities;

            //this.vkPhysicalDevice.GetSurfaceCapabilitiesKhr(this.vkSurface, out surfaceCapabilities);
            vkAPI.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this.vkPhysicalDevice, this.surface, &surfaceCapabilities).Check();

            this.swapchain = CreateSwapchain(this.device, this.surface, surfaceFormat, surfaceCapabilities);

            this.vkImages = this.device.GetSwapchainImages(this.swapchain);

            this.renderPass = CreateRenderPass(this.device, surfaceFormat);

            this.framebuffers = CreateFramebuffers(this.device, this.vkImages, surfaceFormat, this.renderPass, surfaceCapabilities);

            this.vkFence     = this.device.CreateFence();
            this.vkSemaphore = this.device.CreateSemaphore();

            // buffers for vertex data.
            VkBuffer vertexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Vertices, VkBufferUsageFlagBits.VertexBuffer, typeof(float));

            VkBuffer indexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Indexes, VkBufferUsageFlagBits.IndexBuffer, typeof(short));

            var uniformBufferData = new AreaUniformBuffer(1, 1);

            this.originalWidth  = 1; this.width = this.originalWidth;
            this.originalHeight = 1; this.height = this.originalHeight;

            this.uniformBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, uniformBufferData, VkBufferUsageFlagBits.UniformBuffer, typeof(AreaUniformBuffer));

            this.descriptorSetLayout = CreateDescriptorSetLayout(this.device);

            this.vkPipelineLayout = CreatePipelineLayout(this.device, this.descriptorSetLayout);

            VkPipeline pipeline = CreatePipeline(this.device, surfaceCapabilities, this.renderPass, this.vkPipelineLayout);

            this.descriptorSet = CreateDescriptorSet(this.device, this.descriptorSetLayout);

            UpdateDescriptorSets(this.device, this.uniformBuffer, this.descriptorSet);

            this.commandBuffers = CreateCommandBuffers(
                this.device, this.renderPass, surfaceCapabilities,
                this.vkImages, this.framebuffers, pipeline,
                vertexBuffer, indexBuffer, (uint)Indexes.Length,
                this.vkPipelineLayout, this.descriptorSet);

            this.isInitialized = true;
        }
Ejemplo n.º 21
0
 public static VkResult vkQueueBindSparse(VkQueue queue, VkBindSparseInfo bindInfo, VkFence fence)
 {
     return(vkQueueBindSparse(queue, 1, &bindInfo, fence));
 }
Ejemplo n.º 22
0
 public override VkResult AcquireNextImage(VkSwapchainKHR swapchain, long timeout, VkSemaphore semaphore, VkFence fence, out int pImageIndex)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 23
0
 public static VkResult vkResetFences(VkDevice device, VkFence fence)
 {
     return(vkResetFences(device, 1, &fence));
 }
Ejemplo n.º 24
0
 public VkResult AcquireNextImage(VkSwapchainKHR swapchain, long timeout, VkSemaphore semaphore, VkFence fence, out int pImageIndex)
 {
     pImageIndex      = m_NextImageIndex;
     m_NextImageIndex = (m_NextImageIndex + 1) % m_Images.Count;
     // semaphore?.Signal();
     // fence?.Signal();
     return(VkResult.VK_SUCCESS);
 }
Ejemplo n.º 25
0
 public static VkResult vkQueueSubmit(VkQueue queue, ReadOnlySpan <VkSubmitInfo> submits, VkFence fence)
 {
     fixed(VkSubmitInfo *submitsPtr = submits)
     {
         return(vkQueueSubmit(queue, submits.Length, submitsPtr, fence));
     }
 }
Ejemplo n.º 26
0
        public static VkResult vkQueueSubmit(VkQueue queue, int submitCount, VkSubmitInfo[] pSubmits, VkFence fence)
        {
            VkPreconditions.CheckNull(queue, nameof(queue));
            VkPreconditions.CheckNull(pSubmits, nameof(pSubmits));

            VkPreconditions.CheckRange(submitCount, 1, int.MaxValue, nameof(submitCount));
            VkPreconditions.CheckRange(pSubmits.Length < submitCount, nameof(pSubmits.Length));

            return(GetQueue(queue).Submit(submitCount, pSubmits, fence));
        }
Ejemplo n.º 27
0
 public static VkResult vkQueueBindSparse(VkQueue queue, ReadOnlySpan <VkBindSparseInfo> bindInfo, VkFence fence)
 {
     fixed(VkBindSparseInfo *bindInfoPtr = bindInfo)
     {
         return(vkQueueBindSparse(queue, bindInfo.Length, bindInfoPtr, fence));
     }
 }
Ejemplo n.º 28
0
 public static extern VkResult QueueSubmit(
     VkQueue queue,
     uint submitCount,
     IntPtr pSubmits,
     VkFence fence
     );
Ejemplo n.º 29
0
 public static extern VkResult QueueBindSparse(
     VkQueue queue,
     uint bindInfoCount,
     ref VkBindSparseInfo pBindInfo,
     VkFence fence
     );
Ejemplo n.º 30
0
 public static VkResult vkWaitForFences(VkDevice device, VkFence fence, VkBool32 waitAll, ulong timeout)
 {
     return(vkWaitForFences(device, 1, &fence, waitAll, timeout));
 }