Beispiel #1
0
 void RecreateSwapchain()
 {
     VK.DeviceWaitIdle(device);
     CreateSwapchain();
     CreateImageViews();
     CreateRenderPass();
     CreateGraphicsPipeline();
     CreateFramebuffers();
     CreateCommandBuffers();
 }
Beispiel #2
0
        void MainLoop()
        {
            var submitInfo = new VkSubmitInfo();

            submitInfo.sType = CSGL.Vulkan.VkStructureType.SubmitInfo;

            var waitSemaphores   = new Native <VkSemaphore>(imageAvailableSemaphore);
            var waitStages       = new Native <CSGL.Vulkan.VkPipelineStageFlags>(CSGL.Vulkan.VkPipelineStageFlags.ColorAttachmentOutputBit);
            var signalSemaphores = new Native <VkSemaphore>(renderFinishedSemaphore);
            var swapchains       = new Native <VkSwapchainKHR>(swapchain);

            var commandBuffer = new Native <VkCommandBuffer>();
            var indexNative   = new Native <uint>();

            submitInfo.waitSemaphoreCount   = 1;
            submitInfo.pWaitSemaphores      = waitSemaphores.Address;
            submitInfo.pWaitDstStageMask    = waitStages.Address;
            submitInfo.commandBufferCount   = 1;
            submitInfo.pCommandBuffers      = commandBuffer.Address;
            submitInfo.signalSemaphoreCount = 1;
            submitInfo.pSignalSemaphores    = signalSemaphores.Address;

            var submitInfoNative = new Native <VkSubmitInfo>(submitInfo);

            var presentInfo = new VkPresentInfoKHR();

            presentInfo.sType = CSGL.Vulkan.VkStructureType.PresentInfoKhr;
            presentInfo.waitSemaphoreCount = 1;
            presentInfo.pWaitSemaphores    = signalSemaphores.Address;
            presentInfo.swapchainCount     = 1;
            presentInfo.pSwapchains        = swapchains.Address;
            presentInfo.pImageIndices      = indexNative.Address;

            while (true)
            {
                GLFW.PollEvents();
                if (GLFW.WindowShouldClose(window))
                {
                    break;
                }

                if (reCreateSwapchainFlag)
                {
                    reCreateSwapchainFlag = false;
                    RecreateSwapchain();
                }

                uint imageIndex;
                var  result = acquireNextImage(device, swapchain, ulong.MaxValue, imageAvailableSemaphore, VkFence.Null, out imageIndex);

                if (result == CSGL.Vulkan.VkResult.ErrorOutOfDateKhr || result == CSGL.Vulkan.VkResult.SuboptimalKhr)
                {
                    RecreateSwapchain();
                    continue;
                }

                commandBuffer.Value = commandBuffers[(int)imageIndex];
                swapchains.Value    = swapchain;
                indexNative.Value   = imageIndex;

                VK.QueueSubmit(graphicsQueue, 1, submitInfoNative.Address, VkFence.Null);
                result = queuePresent(presentQueue, ref presentInfo);

                if (result == CSGL.Vulkan.VkResult.ErrorOutOfDateKhr || result == CSGL.Vulkan.VkResult.SuboptimalKhr)
                {
                    RecreateSwapchain();
                }
            }

            VK.DeviceWaitIdle(device);
            waitSemaphores.Dispose();
            waitStages.Dispose();
            signalSemaphores.Dispose();
            swapchains.Dispose();
            commandBuffer.Dispose();
            submitInfoNative.Dispose();
        }