2018-09-27 20:09:42 -04:00
|
|
|
#include "renderer.h"
|
|
|
|
|
2018-09-27 22:47:56 -04:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-09-27 20:09:42 -04:00
|
|
|
Renderer::Renderer() {
|
2018-09-27 20:33:45 -04:00
|
|
|
createInstance();
|
|
|
|
createLogicalDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
Renderer::~Renderer() {
|
|
|
|
vkDestroyInstance(instance_, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::createInstance() {
|
2018-09-27 20:09:42 -04:00
|
|
|
VkInstanceCreateInfo instanceCreateInfo = {};
|
|
|
|
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
|
|
|
|
vkCreateInstance(&instanceCreateInfo, nullptr, &instance_);
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:33:45 -04:00
|
|
|
void Renderer::createLogicalDevice() {
|
|
|
|
uint32_t physicalDeviceCount = 0;
|
|
|
|
vkEnumeratePhysicalDevices(instance_, &physicalDeviceCount, nullptr);
|
|
|
|
|
|
|
|
VkPhysicalDevice* physicalDevices = new VkPhysicalDevice[physicalDeviceCount];
|
|
|
|
vkEnumeratePhysicalDevices(instance_, &physicalDeviceCount, physicalDevices);
|
|
|
|
|
|
|
|
VkPhysicalDevice physicalDevice = nullptr;
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < physicalDeviceCount; i++) {
|
|
|
|
VkPhysicalDeviceProperties properties = {};
|
|
|
|
vkGetPhysicalDeviceProperties(physicalDevices[i], &properties);
|
2018-09-27 22:47:56 -04:00
|
|
|
|
2018-09-27 20:33:45 -04:00
|
|
|
physicalDevice = physicalDevices[i];
|
|
|
|
}
|
|
|
|
|
2018-09-27 22:47:56 -04:00
|
|
|
uint32_t queueFamilyPropertiesCount = 0;
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyPropertiesCount, nullptr);
|
|
|
|
|
|
|
|
VkQueueFamilyProperties* queueFamilyProperties = new VkQueueFamilyProperties[queueFamilyPropertiesCount];
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyPropertiesCount, queueFamilyProperties);
|
|
|
|
|
|
|
|
uint32_t graphicsFamilyIndex = 0;
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < queueFamilyPropertiesCount; i++) {
|
|
|
|
if(queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
|
|
|
graphicsFamilyIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<uint32_t> queueFamilyIndices = {graphicsFamilyIndex};
|
|
|
|
std::vector<VkDeviceQueueCreateInfo> deviceQueueCreateInfos;
|
|
|
|
for(auto queueFamilyIndex : queueFamilyIndices) {
|
|
|
|
const float priority = 1.0f;
|
|
|
|
|
|
|
|
VkDeviceQueueCreateInfo queueCreateInfo = {};
|
|
|
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|
|
|
queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
|
|
|
|
queueCreateInfo.queueCount = 1;
|
|
|
|
queueCreateInfo.pQueuePriorities = &priority;
|
|
|
|
|
|
|
|
deviceQueueCreateInfos.push_back(queueCreateInfo);
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:33:45 -04:00
|
|
|
VkDeviceCreateInfo deviceCreateInfo = {};
|
|
|
|
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
2018-09-27 22:47:56 -04:00
|
|
|
deviceCreateInfo.queueCreateInfoCount = deviceQueueCreateInfos.size();
|
|
|
|
deviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfos.data();
|
2018-09-27 20:33:45 -04:00
|
|
|
|
|
|
|
vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device_);
|
2018-09-27 22:47:56 -04:00
|
|
|
|
|
|
|
vkGetDeviceQueue(device_, graphicsFamilyIndex, 0, &graphicsQueue_);
|
2018-09-27 20:09:42 -04:00
|
|
|
}
|