Selecting a physical device
This commit is contained in:
@@ -4,9 +4,6 @@
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
@@ -14,6 +11,7 @@
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
|
||||
|
||||
const uint32_t WIDTH = 800;
|
||||
@@ -46,6 +44,7 @@ class HelloTriangleApplication{
|
||||
GLFWwindow* window;
|
||||
VkInstance instance;
|
||||
VkDebugUtilsMessengerEXT debugMessenger;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
|
||||
void initWindow(){
|
||||
glfwInit();
|
||||
@@ -59,8 +58,82 @@ class HelloTriangleApplication{
|
||||
void initVulkan(){
|
||||
createInstance();
|
||||
setupDebugMessenger();
|
||||
pickPhysicalDevice();
|
||||
}
|
||||
|
||||
void pickPhysicalDevice(){
|
||||
uint32_t deviceCount = 0;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
||||
|
||||
if(deviceCount == 0 ){
|
||||
throw std::runtime_error("failed to find GPU's with Vulkan support!");
|
||||
}
|
||||
|
||||
std::vector<VkPhysicalDevice> devices(deviceCount);
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
||||
|
||||
for(const auto& device : devices){
|
||||
if(isDeviceSuitable(device)){
|
||||
physicalDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(physicalDevice == VK_NULL_HANDLE){
|
||||
throw std::runtime_error("failed to find a suitable GPU");
|
||||
}
|
||||
}
|
||||
|
||||
bool isDeviceSuitable(VkPhysicalDevice device){
|
||||
VkPhysicalDeviceProperties deviceProperties;
|
||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||
|
||||
VkPhysicalDeviceFeatures deviceFeatures;
|
||||
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
|
||||
|
||||
|
||||
QueueFamilyIndices indices = findQueueFamilies(device);
|
||||
|
||||
return indices.isComplete();
|
||||
}
|
||||
|
||||
struct QueueFamilyIndices {
|
||||
std::optional<uint32_t> graphicsFamily;
|
||||
|
||||
bool isComplete(){
|
||||
return graphicsFamily.has_value();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
QueueFamilyIndices findQueueFamilies (VkPhysicalDevice device ){
|
||||
QueueFamilyIndices indices;
|
||||
|
||||
uint32_t queueFamilyCount = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
||||
|
||||
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
||||
|
||||
int i = 0;
|
||||
for (const auto& queueFamily : queueFamilies){
|
||||
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT){
|
||||
indices.graphicsFamily = i;
|
||||
}
|
||||
|
||||
if(indices.isComplete()){
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger) {
|
||||
auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
||||
if (func != nullptr) {
|
||||
|
||||
Reference in New Issue
Block a user