create logical device
This commit is contained in:
parent
928ed19586
commit
03d8de1ca7
1 changed files with 50 additions and 17 deletions
67
main.cpp
67
main.cpp
|
|
@ -30,24 +30,27 @@ private:
|
|||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
window = glfwCreateWindow(800, 600, "App", nullptr, nullptr);
|
||||
mWindow = glfwCreateWindow(800, 600, "App", nullptr, nullptr);
|
||||
}
|
||||
|
||||
void initVulkan() {
|
||||
createInstance();
|
||||
pickPhysicalDevice();
|
||||
findPhysicalDeviceQueueFamilies();
|
||||
createLogicalDevice();
|
||||
getQueues();
|
||||
}
|
||||
|
||||
void mainLoop() {
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
while(!glfwWindowShouldClose(mWindow)) {
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
vkDestroyInstance(instance, nullptr);
|
||||
glfwDestroyWindow(window);
|
||||
vkDestroyDevice(mDevice, nullptr);
|
||||
vkDestroyInstance(mInstance, nullptr);
|
||||
glfwDestroyWindow(mWindow);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
|
|
@ -78,7 +81,7 @@ private:
|
|||
.ppEnabledExtensionNames = glfwExtensions,
|
||||
};
|
||||
|
||||
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
||||
if (vkCreateInstance(&createInfo, nullptr, &mInstance) != VK_SUCCESS) {
|
||||
throw std::runtime_error("failed to create instance!");
|
||||
}
|
||||
}
|
||||
|
|
@ -107,23 +110,23 @@ private:
|
|||
|
||||
void pickPhysicalDevice() {
|
||||
uint32_t deviceCount = 0;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
||||
vkEnumeratePhysicalDevices(mInstance, &deviceCount, nullptr);
|
||||
|
||||
if (deviceCount == 0) {
|
||||
throw std::runtime_error("no gpu with vulkan support");
|
||||
}
|
||||
|
||||
std::vector<VkPhysicalDevice> devices(deviceCount);
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
||||
vkEnumeratePhysicalDevices(mInstance, &deviceCount, devices.data());
|
||||
|
||||
for (const auto& device : devices) {
|
||||
if (isDeviceSuitable(device)) {
|
||||
physicalDevice = device;
|
||||
mPhysicalDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!physicalDevice) throw std::runtime_error("no suitable gpu");
|
||||
if (!mPhysicalDevice) throw std::runtime_error("no suitable gpu");
|
||||
}
|
||||
|
||||
static bool isDeviceSuitable(VkPhysicalDevice device) {
|
||||
|
|
@ -141,15 +144,15 @@ private:
|
|||
|
||||
void findPhysicalDeviceQueueFamilies() {
|
||||
uint32_t queuesCount = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queuesCount, nullptr);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queuesCount, nullptr);
|
||||
|
||||
std::vector<VkQueueFamilyProperties> queueFamilyProperties(queuesCount);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queuesCount, queueFamilyProperties.data());
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queuesCount, queueFamilyProperties.data());
|
||||
|
||||
int index = 0;
|
||||
for (const auto& familyProperty : queueFamilyProperties) {
|
||||
if (familyProperty.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
||||
graphicsQueueFamilyIndex = index;
|
||||
mGraphicsQueueFamilyIndex = index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
|
@ -157,12 +160,42 @@ private:
|
|||
if (index == -1) throw std::runtime_error("nu require queue families found");
|
||||
}
|
||||
|
||||
private:
|
||||
GLFWwindow* window = nullptr;
|
||||
void createLogicalDevice() {
|
||||
float queuePriority = 1.f;
|
||||
|
||||
VkInstance instance = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
uint32_t graphicsQueueFamilyIndex = -1;
|
||||
VkDeviceQueueCreateInfo queueCreateInfos {
|
||||
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
||||
.queueFamilyIndex = mGraphicsQueueFamilyIndex,
|
||||
.queueCount = 1,
|
||||
.pQueuePriorities = &queuePriority,
|
||||
};
|
||||
|
||||
VkPhysicalDeviceFeatures features {};
|
||||
|
||||
VkDeviceCreateInfo deviceCreateInfo {
|
||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||
.queueCreateInfoCount = 1,
|
||||
.pQueueCreateInfos = &queueCreateInfos,
|
||||
.pEnabledFeatures = &features,
|
||||
};
|
||||
|
||||
vkCreateDevice(mPhysicalDevice, &deviceCreateInfo, nullptr, &mDevice);
|
||||
|
||||
if (mDevice == VK_NULL_HANDLE) throw std::runtime_error("failed to create vulkan logical device");
|
||||
}
|
||||
|
||||
void getQueues() {
|
||||
vkGetDeviceQueue(mDevice, mGraphicsQueueFamilyIndex, 0, &mGraphicsQueue);
|
||||
}
|
||||
|
||||
private:
|
||||
GLFWwindow* mWindow = nullptr;
|
||||
|
||||
VkInstance mInstance = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
|
||||
uint32_t mGraphicsQueueFamilyIndex = -1;
|
||||
VkDevice mDevice = VK_NULL_HANDLE;
|
||||
VkQueue mGraphicsQueue = VK_NULL_HANDLE;
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue