Exemple #1
0
        public QueueFamilyIndices(PhysicalDevice device, SurfaceKhr surface)
        {
            int graphicsIndex = -1;
            int presentIndex  = -1;

            int i      = 0;
            var queues = device.GetQueueFamilyProperties();

            foreach (var f in queues)
            {
                if (graphicsIndex < 0 && f.QueueCount > 0 && (f.QueueFlags & QueueFlags.Graphics) != 0)
                {
                    graphicsIndex = i;
                }

                if (presentIndex < 0 && f.QueueCount > 0 && device.GetSurfaceSupportKHR((uint)i, surface))
                {
                    presentIndex = i;
                }

                ++i;
            }

            GraphicsFamily = graphicsIndex;
            PresentFamily  = presentIndex;
        }
Exemple #2
0
        public static bool CheckSwapchainCreateInfo(PhysicalDevice physicalDevice, SwapchainCreateInfoKhr createInfo, bool fixExtend)
        {
            SurfaceKhr surface = createInfo.Surface;

            foreach (uint index in createInfo.QueueFamilyIndices)
            {
                if (!physicalDevice.GetSurfaceSupportKHR(index, surface))
                {
                    return(false);
                }
            }

            bool supportImageFormat = false;

            foreach (SurfaceFormatKhr suportedFormat in physicalDevice.GetSurfaceFormatsKHR(surface))
            {
                if (suportedFormat.Format == createInfo.ImageFormat &&
                    suportedFormat.ColorSpace == createInfo.ImageColorSpace)
                {
                    supportImageFormat = true;
                    break;
                }
            }
            if (!supportImageFormat)
            {
                return(false);
            }

            SurfaceCapabilitiesKhr capabilities = physicalDevice.GetSurfaceCapabilitiesKHR(surface);

            if (fixExtend)
            {
                var extend = createInfo.ImageExtent;
                extend.Width           = Clamp(extend.Width, capabilities.MinImageExtent.Width, capabilities.MaxImageExtent.Width);
                extend.Height          = Clamp(extend.Height, capabilities.MinImageExtent.Height, capabilities.MaxImageExtent.Height);
                createInfo.ImageExtent = extend;
            }
            if (createInfo.PreTransform == SurfaceTransformFlagsKhr.Inherit)
            {
                createInfo.PreTransform = capabilities.CurrentTransform;
            }
            // TODO: Fix up CompositeAlpha if Inherit is set

            if (capabilities.MinImageCount <= createInfo.MinImageCount &&
                capabilities.MaxImageCount >= createInfo.MinImageCount &&
                capabilities.MaxImageArrayLayers <= createInfo.ImageArrayLayers &&
                ((capabilities.SupportedTransforms & createInfo.PreTransform) == createInfo.PreTransform) &&
                ((capabilities.SupportedCompositeAlpha & createInfo.CompositeAlpha) == createInfo.CompositeAlpha) &&
                ((capabilities.SupportedUsageFlags & createInfo.ImageUsage) == createInfo.ImageUsage) &&
                createInfo.ImageExtent.Width >= capabilities.MinImageExtent.Width &&
                createInfo.ImageExtent.Width <= capabilities.MaxImageExtent.Width &&
                createInfo.ImageExtent.Height >= capabilities.MinImageExtent.Height &&
                createInfo.ImageExtent.Height <= capabilities.MaxImageExtent.Height)
            {
                return(true);
            }
            return(false);
        }
Exemple #3
0
        private void InitializeVulkan(PhysicalDevice physDev, SurfaceKhr surface)
        {
            var  queueFamilyProperties = physDev.GetQueueFamilyProperties();
            uint queueFamilyUsedIndex;

            for (queueFamilyUsedIndex = 0; queueFamilyUsedIndex < queueFamilyProperties.Length; ++queueFamilyUsedIndex)
            {
                if (!physDev.GetSurfaceSupportKHR(queueFamilyUsedIndex, surface))
                {
                    continue;
                }
                if (queueFamilyProperties[queueFamilyUsedIndex].QueueFlags.HasFlag(QueueFlags.Graphics))
                {
                    break;
                }
            }

            var queueInfo = new DeviceQueueCreateInfo
            {
                QueuePriorities  = new[] { 1.0f },
                QueueFamilyIndex = queueFamilyUsedIndex
            };

            var deviceInfo = new DeviceCreateInfo
            {
                EnabledExtensionNames = new[]
                {
                    "VK_KHR_swapchain",
                },
                QueueCreateInfos = new[] { queueInfo }
            };

            _device = physDev.CreateDevice(deviceInfo);

            _queue = _device.GetQueue(0, 0);
            _surfaceCapabilities = physDev.GetSurfaceCapabilitiesKHR(surface);
            var surfaceFormat = SelectSurfaceFormat(physDev, surface);

            _swapchainKhr = CreateSwapchainKhr(surface, surfaceFormat);
            _images       = _device.GetSwapchainImagesKHR(_swapchainKhr);
            _renderPass   = CreateRenderPass(surfaceFormat);
            _framebuffers = CreateFramebuffers(_images, surfaceFormat);
            var fenceInfo = new FenceCreateInfo();

            _fence = _device.CreateFence(fenceInfo);
            var semaphoreInfo = new SemaphoreCreateInfo();

            _semaphore      = _device.CreateSemaphore(semaphoreInfo);
            _commandBuffers = CreateCommandBuffers(_images, _framebuffers, _renderPass, _surfaceCapabilities);
        }
Exemple #4
0
        public static uint GetIndexOfFirstAvailablePresentQueueFamily(this PhysicalDevice @this, SurfaceKhr surface)
        {
            var queueFamilies = @this.GetQueueFamilyProperties();

            for (uint i = 0; i < queueFamilies.Length; i++)
            {
                if (@this.GetSurfaceSupportKHR(i, surface))
                {
                    return(i);
                }
            }

            throw new VulkanException($"Could not find a queue family on device {@this.GetName()} that can present to given surface");
        }
        public virtual void Initialize(PhysicalDevice physicalDevice, SurfaceKhr surface)
        {
            var queueFamilyProperties = physicalDevice.GetQueueFamilyProperties();

            uint queueFamilyUsedIndex;

            for (queueFamilyUsedIndex = 0; queueFamilyUsedIndex < queueFamilyProperties.Length; ++queueFamilyUsedIndex)
            {
                if (!physicalDevice.GetSurfaceSupportKHR(queueFamilyUsedIndex, surface))
                {
                    continue;
                }

                if (queueFamilyProperties [queueFamilyUsedIndex].QueueFlags.HasFlag(QueueFlags.Graphics))
                {
                    break;
                }
            }

            var queueInfo = new DeviceQueueCreateInfo {
                QueuePriorities = new float [] { 1.0f }, QueueFamilyIndex = queueFamilyUsedIndex
            };

            var deviceInfo = new DeviceCreateInfo {
                EnabledExtensionNames = new string [] { "VK_KHR_swapchain" },
                QueueCreateInfos      = new DeviceQueueCreateInfo [] { queueInfo }
            };

            device = physicalDevice.CreateDevice(deviceInfo);
            queue  = device.GetQueue(0, 0);
            surfaceCapabilities = physicalDevice.GetSurfaceCapabilitiesKHR(surface);
            var surfaceFormat = SelectFormat(physicalDevice, surface);

            swapchain    = CreateSwapchain(surface, surfaceFormat);
            images       = device.GetSwapchainImagesKHR(swapchain);
            renderPass   = CreateRenderPass(surfaceFormat);
            framebuffers = CreateFramebuffers(images, surfaceFormat);
            var fenceInfo = new FenceCreateInfo();

            fence = device.CreateFence(fenceInfo);
            var semaphoreInfo = new SemaphoreCreateInfo();

            semaphore   = device.CreateSemaphore(semaphoreInfo);
            initialized = true;
        }
Exemple #6
0
        private QueueFamilyIndices FindQueueFamilies(PhysicalDevice device)
        {
            var queueFamilyProperties = device.GetQueueFamilyProperties();
            var queueFamilyIndices    = new QueueFamilyIndices();

            for (int queueFamilyUsedIndex = 0; queueFamilyUsedIndex < queueFamilyProperties.Length; queueFamilyUsedIndex++)
            {
                //Check Present Support
                if (device.GetSurfaceSupportKHR((uint)queueFamilyUsedIndex, data.surface))
                {
                    queueFamilyIndices.PresentFamily = queueFamilyUsedIndex;
                }
                //Check Graphics Support
                if (queueFamilyProperties[queueFamilyUsedIndex].QueueFlags.HasFlag(QueueFlags.Graphics))
                {
                    queueFamilyIndices.GraphicsFamily = queueFamilyUsedIndex;
                }
            }
            return(queueFamilyIndices);
        }
 public bool Supports(SurfaceKhr surface)
 {
     return(PhysicalDevice.GetSurfaceSupportKHR((uint)QueueIndex, surface));
 }