无法仅在调试模式下创建逻辑设备

Cannot create logical device only in debug mode

我得到 VK_ERROR_FEATURE_NOT_PRESENT(-8)

但我正在使用 vkGetPhysicalDeviceFeatures 获取功能。

我的代码:

std::vector<VkDeviceQueueCreateInfo> LogicalDevice::CreateDeviceQueueCreateInfos(QueueFamilies queueFamilies)
{
    std::vector uniqueQueueFamilies = queueFamilies.GetUniqueQueueFamilies();
    std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;

    for (auto queueFamily : uniqueQueueFamilies)
    {
        const int countOfQueues = queueFamily.CountOfQueues;
        std::vector<float> queuePriorities(countOfQueues);

        for (int indexOfPriority = 0; indexOfPriority < countOfQueues; indexOfPriority++)
        {
            queuePriorities[indexOfPriority] = 1.0f - ( (float) indexOfPriority / countOfQueues);
        }

        VkDeviceQueueCreateInfo queueCreateInfo{};
        queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
        queueCreateInfo.queueFamilyIndex = queueFamily.Index.value();
        queueCreateInfo.queueCount = queueFamily.CountOfQueues;
        queueCreateInfo.flags = queueFamily.Flags;
        queueCreateInfo.pQueuePriorities = queuePriorities.data();

        queueCreateInfos.push_back(queueCreateInfo);
    }

    return queueCreateInfos;
}

VkDeviceCreateInfo LogicalDevice::GetDeviceCreateInfo(std::vector<VkDeviceQueueCreateInfo> deviceQueueCreateInfos, VkPhysicalDevice physicalDevice)
{
    VkPhysicalDeviceFeatures deviceFeatures{};

    vkGetPhysicalDeviceFeatures(physicalDevice, &deviceFeatures);

    VkDeviceCreateInfo deviceCreateInfo{};

    deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    deviceCreateInfo.queueCreateInfoCount = static_cast<uint32_t>(deviceQueueCreateInfos.size());
    deviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfos.data();
    deviceCreateInfo.pEnabledFeatures = &deviceFeatures;

    return deviceCreateInfo;
}

void LogicalDevice::Initialize(VkPhysicalDevice physicalDevice, VkSurfaceKHR surfaceForPickingPhysicalDevice)
{
    m_queueFamilies = QueueFamilies::GetQueueFamilies(physicalDevice, surfaceForPickingPhysicalDevice);

    std::vector<VkDeviceQueueCreateInfo> deviceQueueCreateInfos = CreateDeviceQueueCreateInfos(m_queueFamilies);

    VkDeviceCreateInfo deviceCreateInfo = GetDeviceCreateInfo(deviceQueueCreateInfos, physicalDevice);

    VkResult result = vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &m_vulkanDevice);

    if (result != VK_SUCCESS) 
    {
        throw new std::runtime_error("Cannot create logical device.");
    }
} 

您将特征读入并在​​创建信息结构中指向的 deviceFeature 变量是 GetDeviceCreateInfo 的本地变量。这是您调用 vkCreateDevice 时的 out-of-scope,这会导致未定义的行为。您可能会在设备创建时收到随机垃圾,这会导致该错误。