Selecting a physical device
This commit is contained in:
@@ -4,9 +4,6 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define GLFW_INCLUDE_VULKAN
|
#define GLFW_INCLUDE_VULKAN
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -14,6 +11,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
|
||||||
const uint32_t WIDTH = 800;
|
const uint32_t WIDTH = 800;
|
||||||
@@ -46,6 +44,7 @@ class HelloTriangleApplication{
|
|||||||
GLFWwindow* window;
|
GLFWwindow* window;
|
||||||
VkInstance instance;
|
VkInstance instance;
|
||||||
VkDebugUtilsMessengerEXT debugMessenger;
|
VkDebugUtilsMessengerEXT debugMessenger;
|
||||||
|
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||||
|
|
||||||
void initWindow(){
|
void initWindow(){
|
||||||
glfwInit();
|
glfwInit();
|
||||||
@@ -59,8 +58,82 @@ class HelloTriangleApplication{
|
|||||||
void initVulkan(){
|
void initVulkan(){
|
||||||
createInstance();
|
createInstance();
|
||||||
setupDebugMessenger();
|
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) {
|
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger) {
|
||||||
auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
||||||
if (func != nullptr) {
|
if (func != nullptr) {
|
||||||
|
|||||||
Reference in New Issue
Block a user