Enabled device extensions can now be set, display error message if device cannot be created

This commit is contained in:
saschawillems 2017-02-09 19:22:48 +01:00
parent b30fd58557
commit 1c595c61e8
3 changed files with 21 additions and 13 deletions

View file

@ -50,6 +50,9 @@ namespace vk
uint32_t transfer; uint32_t transfer;
} queueFamilyIndices; } queueFamilyIndices;
/** @brief Typecast to VkDevice */
operator VkDevice() { return logicalDevice; };
/** /**
* Default constructor * Default constructor
* *
@ -222,7 +225,7 @@ namespace vk
* *
* @return VkResult of the device creation call * @return VkResult of the device creation call
*/ */
VkResult createLogicalDevice(VkPhysicalDeviceFeatures enabledFeatures, bool useSwapChain = true, VkQueueFlags requestedQueueTypes = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT) VkResult createLogicalDevice(VkPhysicalDeviceFeatures enabledFeatures, std::vector<const char*> enabledExtensions, bool useSwapChain = true, VkQueueFlags requestedQueueTypes = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT)
{ {
// Desired queues need to be requested upon logical device creation // Desired queues need to be requested upon logical device creation
// Due to differing queue family configurations of Vulkan implementations this can be a bit tricky, especially if the application // Due to differing queue family configurations of Vulkan implementations this can be a bit tricky, especially if the application
@ -294,7 +297,7 @@ namespace vk
} }
// Create the logical device representation // Create the logical device representation
std::vector<const char*> deviceExtensions; std::vector<const char*> deviceExtensions(enabledExtensions);
if (useSwapChain) if (useSwapChain)
{ {
// If the device will be used for presenting to a display via a swapchain we need to request the swapchain extension // If the device will be used for presenting to a display via a swapchain we need to request the swapchain extension

View file

@ -20,33 +20,33 @@ VkResult VulkanExampleBase::createInstance(bool enableValidation)
appInfo.pEngineName = name.c_str(); appInfo.pEngineName = name.c_str();
appInfo.apiVersion = VK_API_VERSION_1_0; appInfo.apiVersion = VK_API_VERSION_1_0;
std::vector<const char*> enabledExtensions = { VK_KHR_SURFACE_EXTENSION_NAME }; std::vector<const char*> instanceExtensions = { VK_KHR_SURFACE_EXTENSION_NAME };
// Enable surface extensions depending on os // Enable surface extensions depending on os
#if defined(_WIN32) #if defined(_WIN32)
enabledExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
enabledExtensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
#elif defined(_DIRECT2DISPLAY) #elif defined(_DIRECT2DISPLAY)
enabledExtensions.push_back(VK_KHR_DISPLAY_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_DISPLAY_EXTENSION_NAME);
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
enabledExtensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#elif defined(__linux__) #elif defined(__linux__)
enabledExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
#endif #endif
VkInstanceCreateInfo instanceCreateInfo = {}; VkInstanceCreateInfo instanceCreateInfo = {};
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.pNext = NULL; instanceCreateInfo.pNext = NULL;
instanceCreateInfo.pApplicationInfo = &appInfo; instanceCreateInfo.pApplicationInfo = &appInfo;
if (enabledExtensions.size() > 0) if (instanceExtensions.size() > 0)
{ {
if (settings.validation) if (settings.validation)
{ {
enabledExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); instanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
} }
instanceCreateInfo.enabledExtensionCount = (uint32_t)enabledExtensions.size(); instanceCreateInfo.enabledExtensionCount = (uint32_t)instanceExtensions.size();
instanceCreateInfo.ppEnabledExtensionNames = enabledExtensions.data(); instanceCreateInfo.ppEnabledExtensionNames = instanceExtensions.data();
} }
if (settings.validation) if (settings.validation)
{ {
@ -851,7 +851,10 @@ void VulkanExampleBase::initVulkan()
// This is handled by a separate class that gets a logical device representation // This is handled by a separate class that gets a logical device representation
// and encapsulates functions related to a device // and encapsulates functions related to a device
vulkanDevice = new vk::VulkanDevice(physicalDevice); vulkanDevice = new vk::VulkanDevice(physicalDevice);
VK_CHECK_RESULT(vulkanDevice->createLogicalDevice(enabledFeatures)); VkResult res = vulkanDevice->createLogicalDevice(enabledFeatures, enabledExtensions);
if (res != VK_SUCCESS) {
vkTools::exitFatal("Could not create Vulkan device: \n" + vkTools::errorString(res), "Fatal error");
}
device = vulkanDevice->logicalDevice; device = vulkanDevice->logicalDevice;
// todo: remove // todo: remove

View file

@ -85,6 +85,8 @@ protected:
* @note By default no phyiscal device features are enabled * @note By default no phyiscal device features are enabled
*/ */
VkPhysicalDeviceFeatures enabledFeatures{}; VkPhysicalDeviceFeatures enabledFeatures{};
/** @brief Set of device extensions to be enabled for this example (must be set in the derived constructor) */
std::vector<const char*> enabledExtensions;
/** @brief Logical device, application's view of the physical device (GPU) */ /** @brief Logical device, application's view of the physical device (GPU) */
// todo: getter? should always point to VulkanDevice->device // todo: getter? should always point to VulkanDevice->device
VkDevice device; VkDevice device;