Exemple #1
0
        protected SwapchainData CreateSwapchain(PhysicalDevice physicalDevice, SurfaceKHR surface, uint windowWidth, uint windowHeight)
        {
            var data = new SwapchainData();

            var surfaceFormats = physicalDevice.GetSurfaceFormatsKHR(surface);
            var surfaceFormat  = surfaceFormats[0].Format;

            var surfaceCapabilities = physicalDevice.GetSurfaceCapabilitiesKHR(surface);

            var desiredImageCount = Math.Min(surfaceCapabilities.MinImageCount + 1, surfaceCapabilities.MaxImageCount);

            SurfaceTransformFlagsKHR preTransform;

            if ((surfaceCapabilities.SupportedTransforms & SurfaceTransformFlagsKHR.Identity) != 0)
            {
                preTransform = SurfaceTransformFlagsKHR.Identity;
            }
            else
            {
                preTransform = surfaceCapabilities.CurrentTransform;
            }

            var presentModes = physicalDevice.GetSurfacePresentModesKHR(surface);

            var swapChainPresentMode = PresentModeKHR.Fifo;

            if (presentModes.Contains(PresentModeKHR.Mailbox))
            {
                swapChainPresentMode = PresentModeKHR.Mailbox;
            }
            else if (presentModes.Contains(PresentModeKHR.Immediate))
            {
                swapChainPresentMode = PresentModeKHR.Immediate;
            }

            var imageExtent         = new Extent2D(windowWidth, windowHeight);
            var swapchainCreateInfo = new SwapchainCreateInfoKHR
            {
                Surface       = surface,
                MinImageCount = desiredImageCount,

                ImageFormat      = surfaceFormat,
                ImageExtent      = imageExtent,
                ImageArrayLayers = 1,
                ImageUsage       = ImageUsageFlags.ColorAttachment,
                ImageSharingMode = SharingMode.Exclusive,
                //ImageColorSpace    = ColorSpaceKHR.SrgbNonlinear,

                QueueFamilyIndices = null,
                PreTransform       = preTransform,
                CompositeAlpha     = CompositeAlphaFlagsKHR.Opaque,
                PresentMode        = swapChainPresentMode,
                Clipped            = true,
            };

            data.Swapchain   = device.CreateSwapchainKHR(swapchainCreateInfo);
            data.ImageFormat = surfaceFormat;

            return(data);
        }
        void OnRenderCallback(Queue queue, CommandBuffer[] cmdBuffer, SwapchainData swapchainData)
        {
            var semaphoreCreateInfo = new SemaphoreCreateInfo();
            var presentSemaphore    = device.CreateSemaphore(semaphoreCreateInfo);

            semaphoreCreateInfo.Dispose();

            var currentBufferIndex = (int)device.AcquireNextImageKHR(swapchainData.Swapchain, ulong.MaxValue, presentSemaphore, null);

            SubmitForExecution(queue, presentSemaphore, cmdBuffer[currentBufferIndex]);

            // Present the image (display it on the screen)
            var presentInfo = new PresentInfoKHR(new[] { swapchainData.Swapchain }, new[] { (uint)currentBufferIndex });

            queue.PresentKHR(presentInfo);
            presentInfo.Dispose();

            queue.WaitIdle(); // wait for execution to finish

            device.DestroySemaphore(presentSemaphore);
        }
Exemple #3
0
        //static int i;

        void OnRenderCallback(Queue queue, CommandBuffer[] cmdBuffer, SwapchainData swapchainData, UniformData uniformData, Transform transform)
        {
            //transform.model *= OpenTK.Matrix4.CreateRotationX(i++);
            //CopyUBO(transform, uniformData);

            var semaphoreCreateInfo = new SemaphoreCreateInfo();
            var presentSemaphore    = device.CreateSemaphore(semaphoreCreateInfo);

            semaphoreCreateInfo.Dispose();

            var currentBufferIndex = (int)device.AcquireNextImageKHR(swapchainData.Swapchain, ulong.MaxValue, presentSemaphore, null);

            SubmitForExecution(queue, presentSemaphore, cmdBuffer[currentBufferIndex]);

            var presentInfo = new PresentInfoKHR(new[] { swapchainData.Swapchain }, new[] { (uint)currentBufferIndex });

            queue.PresentKHR(presentInfo);
            presentInfo.Dispose();

            queue.WaitIdle();

            device.DestroySemaphore(presentSemaphore);
        }