Repository: libsdl-org/SDL
File: src/gpu/vulkan/SDL_gpu_vulkan.c (C)

Code before:
        *deviceRank = devicePriority[deviceProperties.deviceType];
    } else if (*deviceRank > devicePriority[deviceProperties.deviceType]) {
        /* Device is outranked by a previous device, don't even try to
         * run a query and reset the rank to avoid overwrites
         */
        *deviceRank = 0;
        return 0;
    }

    /* If we prefer high performance, sum up all device local memory (rounded to megabytes)
     * to deviceRank. In the niche case of someone having multiple dedicated GPUs in the same
     * system, this theoretically picks the most powerful one (or at least the one with the
     * most memory!)
     */

    renderer->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemory);
    Uint64 videoMemory = 0;
    for (i = 0; i < deviceMemory.memoryHeapCount; i++) {
        VkMemoryHeap heap = deviceMemory.memoryHeaps[i];
        if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
            videoMemory += heap.size;
        }
    }
    // Round it to megabytes (as per the vulkan spec videoMemory is in bytes)
    Uint64 videoMemoryRounded = videoMemory / 1024 / 1024;
    *deviceRank += videoMemoryRounded;

    renderer->vkGetPhysicalDeviceFeatures(
        physicalDevice,
        &deviceFeatures);

    if ((!deviceFeatures.independentBlend && renderer->desiredDeviceFeatures.independentBlend) ||
        (!deviceFeatures.imageCubeArray && renderer->desiredDeviceFeatures.imageCubeArray) ||
        (!deviceFeatures.depthClamp && renderer->desiredDeviceFeatures.depthClamp) ||
        (!deviceFeatures.shaderClipDistance && renderer->desiredDeviceFeatures.shaderClipDistance) ||
        (!deviceFeatures.drawIndirectFirstInstance && renderer->desiredDeviceFeatures.drawIndirectFirstInstance) ||
        (!deviceFeatures.sampleRateShading && renderer->desiredDeviceFeatures.sampleRateShading) ||
        (!deviceFeatures.samplerAnisotropy && renderer->desiredDeviceFeatures.samplerAnisotropy)) {
        return 0;
    }

    if (!VULKAN_INTERNAL_CheckDeviceExtensions(
            renderer,
            physicalDevice,
            physicalDeviceExtensions)) {
        return 0;
    }

    renderer->vkGetPhysicalDeviceQueueFamilyProperties(
        physicalDevice,
        &queueFamilyCount,

Review comment (performance):
Will adding a memory number directly to `deviceRank` effectively discard the GPU type preference specified by `DEVICE_PRIORITY_HIGHPERFORMANCE` and `DEVICE_PRIORITY_LOWPOWER`, most of the time?

I'd expect it to store the GPU type order number in the high bits and the VRAM in the low bits, or something similar, instead.

Code after:
        *deviceRank = devicePriority[deviceProperties.deviceType];
    } else if (*deviceRank > devicePriority[deviceProperties.deviceType]) {
        /* Device is outranked by a previous device, don't even try to
         * run a query and reset the rank to avoid overwrites
         */
        *deviceRank = 0;
        return 0;
    }

    renderer->vkGetPhysicalDeviceFeatures(
        physicalDevice,
        &deviceFeatures);

    if ((!deviceFeatures.independentBlend && renderer->desiredDeviceFeatures.independentBlend) ||
        (!deviceFeatures.imageCubeArray && renderer->desiredDeviceFeatures.imageCubeArray) ||
        (!deviceFeatures.depthClamp && renderer->desiredDeviceFeatures.depthClamp) ||
        (!deviceFeatures.shaderClipDistance && renderer->desiredDeviceFeatures.shaderClipDistance) ||
        (!deviceFeatures.drawIndirectFirstInstance && renderer->desiredDeviceFeatures.drawIndirectFirstInstance) ||
        (!deviceFeatures.sampleRateShading && renderer->desiredDeviceFeatures.sampleRateShading) ||
        (!deviceFeatures.samplerAnisotropy && renderer->desiredDeviceFeatures.samplerAnisotropy)) {
        return 0;
    }

    if (!VULKAN_INTERNAL_CheckDeviceExtensions(
            renderer,
            physicalDevice,
            physicalDeviceExtensions)) {
        return 0;
    }

    renderer->vkGetPhysicalDeviceQueueFamilyProperties(
        physicalDevice,
        &queueFamilyCount,
        NULL);

    queueProps = SDL_stack_alloc(
        VkQueueFamilyProperties,
        queueFamilyCount);
    renderer->vkGetPhysicalDeviceQueueFamilyProperties(
        physicalDevice,
        &queueFamilyCount,
        queueProps);

    queueFamilyBest = 0;
    *queueFamilyIndex = SDL_MAX_UINT32;
    for (i = 0; i < queueFamilyCount; i += 1) {
        supportsPresent = SDL_Vulkan_GetPresentationSupport(
            renderer->instance,
            physicalDevice,
            i);
        if (!supportsPresent ||