Ejemplo n.º 1
0
        public void CreateDevice()
        {
            /*
             * We create the logical device in this function.
             */

            /*
             * When creating the device, we also specify what queues it has.
             */
            // Store memory properties of the physical device.
            PhysicalDeviceMemoryProperties MemoryProperties = physicalDevice.GetMemoryProperties();
            PhysicalDeviceFeatures         Features         = physicalDevice.GetFeatures();
            PhysicalDeviceProperties       Properties       = physicalDevice.GetProperties();

            // Create a logical device.
            var queueCreateInfos = new DeviceQueueCreateInfo[1];

            queueCreateInfos[0] = new DeviceQueueCreateInfo(computeQueueFamilyIndex, 1, 1.0f);

            var deviceCreateInfo = new DeviceCreateInfo(
                queueCreateInfos,
                new[] { Constant.DeviceExtension.NVExternalMemory },
                Features);

            device = physicalDevice.CreateDevice(deviceCreateInfo);

            // Get queue(s).
            queue = device.GetQueue(computeQueueFamilyIndex);

            // Create command pool(s).
            //commandPool = device.CreateCommandPool(new CommandPoolCreateInfo(computeQueueFamilyIndex));
        }
Ejemplo n.º 2
0
 void PhysicalDeviceProperties(PhysicalDeviceProperties physicalDeviceProperties)
 {
     WriteLine($"ApiVersion     = {(Vulkan.Version)physicalDeviceProperties.ApiVersion}");
     WriteLine($"DriverVersion  = {physicalDeviceProperties.DriverVersion}");
     WriteLine($"VendorID       = {physicalDeviceProperties.VendorID.ToString("X4")}");
     WriteLine($"DeviceID       = {physicalDeviceProperties.DeviceID.ToString("X4")}");
     WriteLine($"DeviceType     = {physicalDeviceProperties.DeviceType}");
     WriteLine($"DeviceName     = {physicalDeviceProperties.DeviceName}");
     //PipelineCacheUUID
 }
Ejemplo n.º 3
0
        public PhysicalDeviceInfo(PhysicalDevice physicalDevice)
        {
            PhysicalDevice        = physicalDevice;
            Properties            = PhysicalDevice.GetProperties();
            QueueFamilyProperties = PhysicalDevice.GetQueueFamilyProperties();
            MemoryProperties      = PhysicalDevice.GetMemoryProperties();
            Features = PhysicalDevice.GetFeatures();

            GraphicsQFamilies      = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.Graphics));
            ComputeQFamilies       = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.Compute));
            TransferQFamilies      = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.Transfer));
            SparseBindingQFamilies = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.SparseBinding));
        }
Ejemplo n.º 4
0
        private static string ParseDriverVersion(ref PhysicalDeviceProperties properties)
        {
            uint driverVersionRaw = properties.DriverVersion;

            // NVIDIA differ from the standard here and use a different format.
            if (properties.VendorID == 0x10DE)
            {
                return($"{(driverVersionRaw >> 22) & 0x3FF}.{(driverVersionRaw >> 14) & 0xFF}.{(driverVersionRaw >> 6) & 0xFF}.{driverVersionRaw & 0x3F}");
            }
            else
            {
                return(ParseStandardVulkanVersion(driverVersionRaw));
            }
        }
Ejemplo n.º 5
0
        private void FindPhysicalDevice()
        {
            /*
             * In this function, we find a physical device that can be used with Vulkan.
             */

            /*
             * So, first we will list all physical devices on the system with vkEnumeratePhysicalDevices .
             */
            List <PhysicalDevice> physicalDevices = new List <PhysicalDevice>(instance.EnumeratePhysicalDevices());

            /*
             * Next, we choose a device that can be used for our purposes.
             * With VkPhysicalDeviceFeatures(), we can retrieve a fine-grained list of physical features supported by the device.
             * However, in this demo, we are simply launching a simple compute shader, and there are no
             * special physical features demanded for this task.
             * With VkPhysicalDeviceProperties(), we can obtain a list of physical device properties. Most importantly,
             * we obtain a list of physical device limitations. For this application, we launch a compute shader,
             * and the maximum size of the workgroups and total number of compute shader invocations is limited by the physical device,
             * and we should ensure that the limitations named maxComputeWorkGroupCount, maxComputeWorkGroupInvocations and
             * maxComputeWorkGroupSize are not exceeded by our application.  Moreover, we are using a storage buffer in the compute shader,
             * and we should ensure that it is not larger than the device can handle, by checking the limitation maxStorageBufferRange.
             * However, in our application, the workgroup size and total number of shader invocations is relatively small, and the storage buffer is
             * not that large, and thus a vast majority of devices will be able to handle it. This can be verified by looking at some devices at_
             * http://vulkan.gpuinfo.org/
             * Therefore, to keep things simple and clean, we will not perform any such checks here, and just pick the first physical
             * device in the list. But in a real and serious application, those limitations should certainly be taken into account.
             */
            foreach (PhysicalDevice device in physicalDevices)
            {
                QueueFamilyProperties[] queueFamilyProperties = device.GetQueueFamilyProperties();
                for (int j = 0; j < queueFamilyProperties.Length; j++)
                {
                    if (queueFamilyProperties[j].QueueFlags.HasFlag(Queues.Compute))
                    {
                        // As above stated, we do a feature check, because my intel gpu has limited memory
                        // and runs in a very old API version that doesn't fully support compute
                        PhysicalDeviceProperties Properties = device.GetProperties();
                        if (Properties.ApiVersion.Patch < 38)
                        {
                            continue;
                        }
                        computeQueueFamilyIndex = j;
                        physicalDevice          = device;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        void AppGpuDumpProps(AppGpu gpu, StreamWriter output)
        {
            PhysicalDeviceProperties props = gpu.Props;

            output.WriteLine("VkPhysicalDeviceProperties:");
            output.WriteLine("===========================");
            output.WriteLine("\tapiVersion     = {0}", props.ApiVersion);
            output.WriteLine("\tdriverVersion  = {0}", props.DriverVersion);
            output.WriteLine("\tvendorID       = 0x{0:x}", props.VendorID);
            output.WriteLine("\tdeviceID       = 0x{0:x}", props.DeviceID);
            output.WriteLine("\tdeviceType     = {0}", GetVkName(props.DeviceType.ToString(), "", ""));
            output.WriteLine("\tdeviceName     = {0}", props.DeviceName);

            AppDumpLimits(props.Limits, output);
            AppDumpSparseProps(props.SparseProperties, output);;
        }
        internal HostDevice(
            PhysicalDevice vulkanPhysicaldevice,
            SurfaceType surfaceType,
            Logger logger = null)
        {
            if (vulkanPhysicaldevice == null)
            {
                throw new ArgumentNullException(nameof(vulkanPhysicaldevice));
            }
            this.physicalDevice         = vulkanPhysicaldevice;
            this.surfaceType            = surfaceType;
            this.logger                 = logger;
            this.properties             = vulkanPhysicaldevice.GetProperties();
            this.deviceMemoryProperties = vulkanPhysicaldevice.GetMemoryProperties();
            this.supportedFeatures      = vulkanPhysicaldevice.GetFeatures();
            this.availableExtensions    = vulkanPhysicaldevice.EnumerateExtensionProperties();
            this.queueFamilies          = vulkanPhysicaldevice.GetQueueFamilyProperties();

            logger?.Log(nameof(HostDevice), $"Found device: {Name}");
            logger?.LogList(nameof(HostDevice), $"{Name} available extensions:", availableExtensions);
        }
Ejemplo n.º 8
0
        public Context(GameWindow window)
        {
            Window              = window;
            Instance            = ToDispose(VKHelper.CreateInstance());
            DebugReportCallback = ToDispose(VKHelper.CreateDebugReportCallback(Instance));
            Surface             = ToDispose(VKHelper.CreateSurface(Instance, Window.Handle));

            foreach (PhysicalDevice physicalDevice in Instance.EnumeratePhysicalDevices())
            {
                QueueFamilyProperties[] queueFamilyProperties = physicalDevice.GetQueueFamilyProperties();
                for (int i = 0; i < queueFamilyProperties.Length; i++)
                {
                    if (queueFamilyProperties[i].QueueFlags.HasFlag(Queues.Graphics))
                    {
                        if (GraphicsQueueFamilyIndex == -1)
                        {
                            GraphicsQueueFamilyIndex = i;
                        }
                        if (ComputeQueueFamilyIndex == -1)
                        {
                            ComputeQueueFamilyIndex = i;
                        }

                        if (physicalDevice.GetSurfaceSupportKhr(i, Surface) &&
                            VKHelper.GetPresentationSupport(physicalDevice, i))
                        {
                            PresentQueueFamilyIndex = i;
                        }

                        if (GraphicsQueueFamilyIndex != -1 &&
                            ComputeQueueFamilyIndex != -1 &&
                            PresentQueueFamilyIndex != -1)
                        {
                            PhysicalDevice = physicalDevice;
                            break;
                        }
                    }
                }
                if (PhysicalDevice != null)
                {
                    break;
                }
            }

            if (PhysicalDevice == null)
            {
                throw new InvalidOperationException("No suitable physical device found.");
            }

            GenerateDepthStencilFormat();

            // Store memory properties of the physical device.
            MemoryProperties = PhysicalDevice.GetMemoryProperties();
            Features         = PhysicalDevice.GetFeatures();
            Properties       = PhysicalDevice.GetProperties();

            // Create a logical device.
            bool sameGraphicsAndPresent = GraphicsQueueFamilyIndex == PresentQueueFamilyIndex;
            var  queueCreateInfos       = new DeviceQueueCreateInfo[sameGraphicsAndPresent ? 1 : 2];

            queueCreateInfos[0] = new DeviceQueueCreateInfo(GraphicsQueueFamilyIndex, 1, 1.0f);
            if (!sameGraphicsAndPresent)
            {
                queueCreateInfos[1] = new DeviceQueueCreateInfo(PresentQueueFamilyIndex, 1, 1.0f);
            }

            var deviceCreateInfo = new DeviceCreateInfo(
                queueCreateInfos,
                new[] { Constant.DeviceExtension.KhrSwapchain, Constant.DeviceExtension.KhrMaintenance1 },
                Features);

            Device = PhysicalDevice.CreateDevice(deviceCreateInfo);

            // Get queue(s).
            GraphicsQueue = Device.GetQueue(GraphicsQueueFamilyIndex);
            ComputeQueue  = ComputeQueueFamilyIndex == GraphicsQueueFamilyIndex
                ? GraphicsQueue
                : Device.GetQueue(ComputeQueueFamilyIndex);
            PresentQueue = PresentQueueFamilyIndex == GraphicsQueueFamilyIndex
                ? GraphicsQueue
                : Device.GetQueue(PresentQueueFamilyIndex);

            Content = new Content(this);

            GraphicsCommandPool = ToDispose(Device.CreateCommandPool(new CommandPoolCreateInfo(GraphicsQueueFamilyIndex, CommandPoolCreateFlags.ResetCommandBuffer)));
            ComputeCommandPool  = ToDispose(Device.CreateCommandPool(new CommandPoolCreateInfo(ComputeQueueFamilyIndex)));

            Graphics = ToDispose(new Graphics(this));

            Build();
        }
Ejemplo n.º 9
0
        public void GetProperties()
        {
            PhysicalDeviceProperties properties = PhysicalDevice.GetProperties();

            Assert.True(properties.DeviceName.Length > 0);
        }
Ejemplo n.º 10
0
        private static unsafe bool IsSuitableDevice(Vk api, PhysicalDevice physicalDevice, PhysicalDeviceProperties properties, SurfaceKHR surface,
                                                    out uint queueCount, out uint familyIndex)
        {
            queueCount  = 0;
            familyIndex = 0;

            if (properties.DeviceType == PhysicalDeviceType.Cpu)
            {
                return(false);
            }

            var  extensionMatches = 0;
            uint propertiesCount;

            api.EnumerateDeviceExtensionProperties(physicalDevice, (byte *)null, &propertiesCount, null).ThrowOnError();

            var extensionProperties = new ExtensionProperties[propertiesCount];

            fixed(ExtensionProperties *pExtensionProperties = extensionProperties)
            {
                api.EnumerateDeviceExtensionProperties(
                    physicalDevice,
                    (byte *)null,
                    &propertiesCount,
                    pExtensionProperties).ThrowOnError();

                for (var i = 0; i < propertiesCount; i++)
                {
                    var extensionName = Marshal.PtrToStringAnsi((IntPtr)pExtensionProperties[i].ExtensionName);

                    if (VulkanInitialization.RequiredExtensions.Contains(extensionName))
                    {
                        extensionMatches++;
                    }
                }
            }

            if (extensionMatches == VulkanInitialization.RequiredExtensions.Length)
            {
                familyIndex = FindSuitableQueueFamily(api, physicalDevice, surface, out queueCount);

                return(familyIndex != uint.MaxValue);
            }

            return(false);
        }
Ejemplo n.º 11
0
 internal static unsafe extern void vkGetPhysicalDeviceProperties(PhysicalDevice physicalDevice, PhysicalDeviceProperties* properties);
Ejemplo n.º 12
0
 public unsafe void GetProperties(out PhysicalDeviceProperties properties)
 {
     fixed (PhysicalDeviceProperties* __properties__ = &properties)
     {
         vkGetPhysicalDeviceProperties(this, __properties__);
     }
 }