Ejemplo n.º 1
0
def init_device(instance):
    ''' Enumerate and initialize a device '''
    gpu_count = 0
    gpu_count = vk.vkEnumeratePhysicalDevices(instance, )
    for index, gpus in enumerate(gpu_count):
        print("Enumerated Device: {} _____ {}".format(index, gpus))
    properties = vk.vkGetPhysicalDeviceFormatProperties(
        gpu_count[0],
        vk.VK_FORMAT_B8G8R8_UINT,
    )
    #features = vk.vkGetPhysicalDeviceFeatures(gpu_count[0])

    queue_families = vk.vkGetPhysicalDeviceQueueFamilyProperties(gpu_count[0])
    for index, queue in enumerate(queue_families):
        print("Compute Queue availability for index {} is {}".format(
            index, bool(queue.queueFlags & vk.VK_QUEUE_COMPUTE_BIT)))

    device_queue_info = vk.VkDeviceQueueCreateInfo(
        sType=vk.VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
        queueFamilyIndex=1,
        queueCount=1,
        pQueuePriorities=1.0)

    device_info = vk.VkDeviceCreateInfo(
        sType=vk.VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
        queueCreateInfoCount=1,
        pQueueCreateInfos=device_queue_info,
        enabledExtensionCount=0,
        ppEnabledLayerNames=None,
        ppEnabledExtensionNames=None,
        pEnabledFeatures=None,
        enabledLayerCount=0)
    device = vk.vkCreateDevice(gpu_count[0], pCreateInfo=None, pAllocator=0)

    return device
Ejemplo n.º 2
0
    def _query_queue_families(self, vk_physical_device):
        """Query available device queue families."""
        graphics = None
        families = vk.vkGetPhysicalDeviceQueueFamilyProperties(
            vk_physical_device)
        for i, family in enumerate(families):
            if graphics is None and family.queueFlags & vk.VK_QUEUE_GRAPHICS_BIT:
                graphics = i

        return QueueFamily(graphics)
Ejemplo n.º 3
0
    def __find_queue_families(self, device):
        indices = QueueFamilyIndices()
        family_properties = vk.vkGetPhysicalDeviceQueueFamilyProperties(device)
        for i, prop in enumerate(family_properties):
            if prop.queueCount > 0 and \
                    prop.queueFlags & vk.VK_QUEUE_GRAPHICS_BIT:
                indices.graphicsFamily = i

            if indices.is_complete:
                break

        return indices
Ejemplo n.º 4
0
    def _get_queue_families(physical_device, surface, pfn):
        '''Get graphic and present queue families

        Check for graphic and presentation queue families.

        *Parameters:*

        - `physical_device`: The `VkPhysicalDevice` to check for
        - `surface`: The `VkSurfaceKHR` to present
        - `pfn`: Function `vkGetPhysicalDeviceSurfaceSupportKHR` callable

        *Returns:*

        A tuple with graphic index and present index or None
        '''
        queue_families = vk.vkGetPhysicalDeviceQueueFamilyProperties(
            physical_device)  # noqa

        graphic_index = -1
        present_index = -1

        for i, queue_family in enumerate(queue_families):
            # Queue family needs queues
            if queue_family.queueCount <= 0:
                continue

            # Check that queue family support present queue
            present_available = pfn(physical_device, i, surface)

            if queue_family.queueFlags & vk.VK_QUEUE_GRAPHICS_BIT:
                graphic_index = i
            if present_available:
                present_index = i

        if graphic_index == -1 or present_index == -1:
            return None

        return graphic_index, present_index
Ejemplo n.º 5
0
Archivo: context.py Proyecto: js78/vulk
    def _get_queue_families(physical_device, surface, pfn):
        '''Get graphic and present queue families

        Check for graphic and presentation queue families.

        *Parameters:*

        - `physical_device`: The `VkPhysicalDevice` to check for
        - `surface`: The `VkSurfaceKHR` to present
        - `pfn`: Function `vkGetPhysicalDeviceSurfaceSupportKHR` callable

        *Returns:*

        A tuple with graphic index and present index or None
        '''
        queue_families = vk.vkGetPhysicalDeviceQueueFamilyProperties(physical_device) # noqa

        graphic_index = -1
        present_index = -1

        for i, queue_family in enumerate(queue_families):
            # Queue family needs queues
            if queue_family.queueCount <= 0:
                continue

            # Check that queue family support present queue
            present_available = pfn(physical_device, i, surface)

            if queue_family.queueFlags & vk.VK_QUEUE_GRAPHICS_BIT:
                graphic_index = i
            if present_available:
                present_index = i

        if graphic_index == -1 or present_index == -1:
            return None

        return graphic_index, present_index
Ejemplo n.º 6
0
    def queue_families(self):

        families = vulkan.vkGetPhysicalDeviceQueueFamilyProperties(
            self.vk_device)
        return [QueueFamily(idx, qf) for idx, qf in enumerate(families)]
Ejemplo n.º 7
0
    def initSurface(self, **kwargs):
        """
Create the os-specific surface
see also https://github.com/gabdube/python-vulkan-triangle for surface management with xcb/wayland/xlib
        """
        if VK_USE_PLATFORM_XCB_KHR:
            connection = kwargs['connection']
            window = kwargs['window']
            pCreateXcbSurfaceKHR = vk.vkGetInstanceProcAddr(
                self.instance, "vkCreateXcbSurfaceKHR")
            surfaceCreateInfo = vk.VkXcbSurfaceCreateInfoKHR(
                sType=vk.VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR,
                #connection = connection,
                connection=vk.ffi.cast('void*', connection._conn),
                window=window)
            self.surface = pCreateXcbSurfaceKHR(instance=self.instance,
                                                pCreateInfo=surfaceCreateInfo,
                                                pAllocator=None)
        if VK_USE_PLATFORM_WIN32_KHR:
            platformHandle = kwargs['platformHandle']
            platformWindow = kwargs['platformWindow']
            pCreateWin32SurfaceKHR = vk.vkGetInstanceProcAddr(
                self.instance, "vkCreateWin32SurfaceKHR")
            surfaceCreateInfo = vk.VkWin32SurfaceCreateInfoKHR(
                sType=vk.VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
                hinstance=platformHandle,
                hwnd=platformWindow)
            self.surface = pCreateWin32SurfaceKHR(
                instance=self.instance,
                pCreateInfo=surfaceCreateInfo,
                pAllocator=None)

        if self.surface is None:
            vks.vulkantools.exitFatal("Could not create surface", -1)

        # Get available queue family properties
        queueProps = vk.vkGetPhysicalDeviceQueueFamilyProperties(
            self.physicalDevice)
        # Iterate over each queue to learn whether it supports presenting:
        # Find a queue with present support
        # Will be used to present the swap chain images to the windowing system

        supportsPresent = []
        for i in range(len(queueProps)):
            supportsPresent.append(
                self.fpGetPhysicalDeviceSurfaceSupportKHR(
                    physicalDevice=self.physicalDevice,
                    queueFamilyIndex=i,
                    surface=self.surface))
        # Search for a graphics and a present queue in the array of queue
        # families, try to find one that supports both
        graphicsQueueNodeIndex = None
        presentQueueNodeIndex = None

        for i in range(len(queueProps)):
            #            print('properties for queue family ' + str(i) + ': ', end='')
            #            if (queueProps[i].queueFlags & vk.VK_QUEUE_GRAPHICS_BIT) != 0:
            #                print('GRAPHICS', end='')
            #            if (queueProps[i].queueFlags & vk.VK_QUEUE_COMPUTE_BIT) != 0:
            #                print(', COMPUTE', end='')
            #            if (queueProps[i].queueFlags & vk.VK_QUEUE_TRANSFER_BIT) != 0:
            #                print(', TRANSFER', end='')
            #            if (queueProps[i].queueFlags & vk.VK_QUEUE_SPARSE_BINDING_BIT) != 0:
            #                print(', SPARSE_BINDING', end='')
            #            if supportsPresent[i] == vk.VK_TRUE:
            #                 print(', SUPPORTS PRESENTATION', end='')
            #            print(', supportsPresent='+str(supportsPresent[i]))
            if (queueProps[i].queueFlags & vk.VK_QUEUE_GRAPHICS_BIT) != 0:
                if graphicsQueueNodeIndex is None:
                    graphicsQueueNodeIndex = i
                if supportsPresent[i] == vk.VK_TRUE:
                    graphicsQueueNodeIndex = i
                    presentQueueNodeIndex = i
                    break
        if presentQueueNodeIndex is None:
            # If there's no queue that supports both present and graphics
            # try to find a separate present queue
            for i in range(len(queueProps)):
                if supportsPresent[i] == vk.VK_TRUE:
                    presentQueueNodeIndex = i
                    break
#        if VK_USE_PLATFORM_WIN32_KHR and presentQueueNodeIndex is None:   # is this a bug in Win32? look at the python code in sdl2_exmaple
#            presentQueueNodeIndex = graphicsQueueNodeIndex
# Exit if either a graphics or a presenting queue hasn't been found
        if graphicsQueueNodeIndex is None or presentQueueNodeIndex is None:
            vks.vulkantools.exitFatal(
                "Could not find a graphics and/or presenting queue!", -1)
        # todo : Add support for separate graphics and presenting queue
        if graphicsQueueNodeIndex != presentQueueNodeIndex:
            vks.vulkantools.exitFatal(
                "Separate graphics and presenting queues are not supported yet!",
                -1)

        self.queueNodeIndex = graphicsQueueNodeIndex

        # Get list of supported surface formats
        surfaceFormats = self.fpGetPhysicalDeviceSurfaceFormatsKHR(
            physicalDevice=self.physicalDevice, surface=self.surface)
        if (len(surfaceFormats) == 1) and (surfaceFormats[0].format
                                           == vk.VK_FORMAT_UNDEFINED):
            self.colorFormat = vk.VK_FORMAT_B8G8R8A8_UNORM
            self.colorSpace = surfaceFormats[0].colorSpace
        else:
            # iterate over the list of available surface format and
            # check for the presence of VK_FORMAT_B8G8R8A8_UNORM
            found_B8G8R8A8_UNORM = False
            for surfaceFormat in surfaceFormats:
                if surfaceFormat.format == vk.VK_FORMAT_B8G8R8A8_UNORM:
                    self.colorFormat = surfaceFormat.format
                    self.colorSpace = surfaceFormat.colorSpace
                    found_B8G8R8A8_UNORM = True
                    break
            # in case VK_FORMAT_B8G8R8A8_UNORM is not available
            # select the first available color format
            if not found_B8G8R8A8_UNORM:
                self.colorFormat = surfaceFormats[0].format
                self.colorSpace = surfaceFormats[0].colorSpace