2020-03-02 19:07:49 +01:00
|
|
|
#include "VulkanGraphics.h"
|
2020-03-13 12:44:33 +01:00
|
|
|
#include "VulkanAllocator.h"
|
2020-03-24 21:05:32 +01:00
|
|
|
#include "VulkanQueue.h"
|
2020-03-13 12:44:33 +01:00
|
|
|
#include "Containers/Array.h"
|
|
|
|
|
#include "VulkanInitializer.h"
|
2020-03-24 21:05:32 +01:00
|
|
|
#include "VulkanCommandBuffer.h"
|
2020-03-05 11:43:38 +01:00
|
|
|
#include <GLFW/glfw3.h>
|
2020-03-02 19:07:49 +01:00
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
using namespace Seele::Vulkan;
|
2020-03-15 15:58:01 +01:00
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
Graphics::Graphics()
|
2020-03-24 21:05:32 +01:00
|
|
|
: callback(VK_NULL_HANDLE), handle(VK_NULL_HANDLE), instance(VK_NULL_HANDLE), physicalDevice(VK_NULL_HANDLE)
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
|
|
|
|
glfwInit();
|
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
Graphics::~Graphics()
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
vkDestroyDevice(handle, nullptr);
|
|
|
|
|
DestroyDebugReportCallbackEXT(instance, nullptr, callback);
|
|
|
|
|
vkDestroyInstance(instance, nullptr);
|
2020-03-15 15:58:01 +01:00
|
|
|
glfwTerminate();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void Graphics::init(GraphicsInitializer initInfo)
|
2020-03-02 19:07:49 +01:00
|
|
|
{
|
2020-03-15 15:58:01 +01:00
|
|
|
initInstance(initInfo);
|
2020-03-13 12:44:33 +01:00
|
|
|
setupDebugCallback();
|
2020-03-15 15:58:01 +01:00
|
|
|
pickPhysicalDevice();
|
2020-03-24 21:05:32 +01:00
|
|
|
createDevice(initInfo);
|
|
|
|
|
allocator = new Allocator(this);
|
|
|
|
|
graphicsCommands = new CommandBufferManager(this, graphicsQueue);
|
|
|
|
|
computeCommands = new CommandBufferManager(this, computeQueue);
|
|
|
|
|
transferCommands = new CommandBufferManager(this, transferQueue);
|
|
|
|
|
dedicatedTransferCommands = new CommandBufferManager(this, dedicatedTransferQueue);
|
2020-03-02 19:07:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
void Graphics::beginFrame(void *windowHandle)
|
2020-03-02 19:07:49 +01:00
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
GLFWwindow *window = static_cast<GLFWwindow *>(windowHandle);
|
2020-03-05 11:43:38 +01:00
|
|
|
glfwPollEvents();
|
2020-03-02 19:07:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
void Graphics::endFrame(void *windowHandle)
|
2020-03-02 19:07:49 +01:00
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
GLFWwindow *window = static_cast<GLFWwindow *>(windowHandle);
|
2020-03-02 19:07:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
void *Graphics::createWindow(const WindowCreateInfo &createInfo)
|
2020-03-02 19:07:49 +01:00
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
GLFWwindow *window = glfwCreateWindow(createInfo.width, createInfo.height, createInfo.title, createInfo.bFullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr);
|
2020-03-05 11:43:38 +01:00
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
VkDevice Graphics::getDevice() const
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
VkPhysicalDevice Graphics::getPhysicalDevice() const
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
|
|
|
|
return physicalDevice;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
Array<const char *> Graphics::getRequiredExtensions()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
Array<const char *> extensions;
|
2020-03-13 12:44:33 +01:00
|
|
|
|
|
|
|
|
unsigned int glfwExtensionCount = 0;
|
2020-03-24 21:05:32 +01:00
|
|
|
const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
for (unsigned int i = 0; i < glfwExtensionCount; i++)
|
|
|
|
|
{
|
2020-03-13 12:44:33 +01:00
|
|
|
extensions.add(glfwExtensions[i]);
|
|
|
|
|
}
|
|
|
|
|
#ifdef ENABLE_VALIDATION
|
|
|
|
|
extensions.add(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
|
|
|
|
#endif // ENABLE_VALIDATION
|
|
|
|
|
return extensions;
|
|
|
|
|
}
|
2020-03-20 01:27:40 +01:00
|
|
|
void Graphics::initInstance(GraphicsInitializer initInfo)
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
|
|
|
|
VkApplicationInfo appInfo = {};
|
|
|
|
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
|
|
|
|
appInfo.pApplicationName = initInfo.applicationName;
|
|
|
|
|
appInfo.applicationVersion = VK_MAKE_VERSION(0, 0, 1);
|
|
|
|
|
appInfo.pEngineName = initInfo.engineName;
|
|
|
|
|
appInfo.engineVersion = VK_MAKE_VERSION(0, 0, 1);
|
|
|
|
|
appInfo.apiVersion = VK_API_VERSION_1_1;
|
|
|
|
|
|
|
|
|
|
VkInstanceCreateInfo info = {};
|
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
|
info.pApplicationInfo = &appInfo;
|
2020-03-24 21:05:32 +01:00
|
|
|
Array<const char *> extensions = getRequiredExtensions();
|
2020-03-15 15:58:01 +01:00
|
|
|
for (uint32 i = 0; i < initInfo.instanceExtensions.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
extensions.add(initInfo.instanceExtensions[i]);
|
|
|
|
|
}
|
|
|
|
|
info.enabledExtensionCount = (uint32)extensions.size();
|
|
|
|
|
info.ppEnabledExtensionNames = extensions.data();
|
|
|
|
|
#ifdef ENABLE_VALIDATION
|
|
|
|
|
info.enabledLayerCount = (uint32)initInfo.layers.size();
|
|
|
|
|
info.ppEnabledLayerNames = initInfo.layers.data();
|
|
|
|
|
#else
|
|
|
|
|
info.enabledLayerCount = 0;
|
|
|
|
|
#endif
|
|
|
|
|
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
|
|
|
|
|
}
|
2020-03-20 01:27:40 +01:00
|
|
|
void Graphics::setupDebugCallback()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
|
|
|
|
VkDebugReportCallbackCreateInfoEXT createInfo =
|
|
|
|
|
init::DebugReportCallbackCreateInfo(
|
|
|
|
|
VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT);
|
|
|
|
|
|
|
|
|
|
VK_CHECK(CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback));
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void Graphics::pickPhysicalDevice()
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
|
|
|
|
uint32 physicalDeviceCount;
|
|
|
|
|
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr);
|
|
|
|
|
Array<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
|
|
|
|
|
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
|
|
|
|
|
VkPhysicalDevice bestDevice;
|
|
|
|
|
uint32 deviceRating = 0;
|
|
|
|
|
for (auto physicalDevice : physicalDevices)
|
|
|
|
|
{
|
2020-03-16 13:29:17 +01:00
|
|
|
uint32 currentRating = 0;
|
2020-03-15 15:58:01 +01:00
|
|
|
vkGetPhysicalDeviceProperties(physicalDevice, &props);
|
2020-03-24 21:05:32 +01:00
|
|
|
vkGetPhysicalDeviceFeatures(physicalDevice, &features);
|
2020-03-16 13:29:17 +01:00
|
|
|
if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
|
|
|
|
{
|
|
|
|
|
currentRating += 100;
|
|
|
|
|
}
|
|
|
|
|
else if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
|
|
|
|
|
{
|
|
|
|
|
currentRating += 10;
|
|
|
|
|
}
|
|
|
|
|
if (currentRating > deviceRating)
|
|
|
|
|
{
|
|
|
|
|
deviceRating = currentRating;
|
|
|
|
|
bestDevice = physicalDevice;
|
|
|
|
|
}
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
2020-03-16 13:29:17 +01:00
|
|
|
this->physicalDevice = bestDevice;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
void Graphics::createDevice(GraphicsInitializer initializer)
|
2020-03-16 13:29:17 +01:00
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
uint32_t numQueueFamilies = 0;
|
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &numQueueFamilies, nullptr);
|
|
|
|
|
Array<VkQueueFamilyProperties> queueProperties(numQueueFamilies);
|
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &numQueueFamilies, queueProperties.data());
|
2020-03-16 13:29:17 +01:00
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
Array<VkDeviceQueueCreateInfo> queueInfos;
|
|
|
|
|
int32_t graphicsQueueFamilyIndex = -1;
|
|
|
|
|
int32_t transferQueueFamilyIndex = -1;
|
|
|
|
|
int32_t dedicatedTransferQueueFamilyIndex = -1;
|
|
|
|
|
int32_t computeQueueFamilyIndex = -1;
|
|
|
|
|
int32_t asyncComputeFamilyIndex = -1;
|
|
|
|
|
for (int32_t familyIndex = 0; familyIndex < queueProperties.size(); ++familyIndex)
|
|
|
|
|
{
|
|
|
|
|
const VkQueueFamilyProperties currProps = queueProperties[familyIndex];
|
|
|
|
|
if ((currProps.queueFlags & VK_QUEUE_GRAPHICS_BIT) == VK_QUEUE_GRAPHICS_BIT)
|
|
|
|
|
{
|
|
|
|
|
if (graphicsQueueFamilyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
graphicsQueueFamilyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((currProps.queueFlags & VK_QUEUE_COMPUTE_BIT) == VK_QUEUE_COMPUTE_BIT)
|
|
|
|
|
{
|
|
|
|
|
if (computeQueueFamilyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
computeQueueFamilyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
if (Gfx::useAsyncCompute)
|
|
|
|
|
{
|
|
|
|
|
if (asyncComputeFamilyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
if (familyIndex == graphicsQueueFamilyIndex)
|
|
|
|
|
{
|
|
|
|
|
if (currProps.queueCount > 1)
|
|
|
|
|
{
|
|
|
|
|
asyncComputeFamilyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncComputeFamilyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((currProps.queueFlags & VK_QUEUE_TRANSFER_BIT) == VK_QUEUE_TRANSFER_BIT)
|
|
|
|
|
{
|
|
|
|
|
if (transferQueueFamilyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
transferQueueFamilyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
if ((currProps.queueFlags ^ VK_QUEUE_TRANSFER_BIT) == 0)
|
|
|
|
|
{
|
|
|
|
|
dedicatedTransferQueueFamilyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VkDeviceQueueCreateInfo info =
|
|
|
|
|
init::DeviceQueueCreateInfo(familyIndex, 1);
|
|
|
|
|
queueInfos.add(info);
|
|
|
|
|
}
|
|
|
|
|
VkDeviceCreateInfo deviceInfo = init::DeviceCreateInfo(
|
|
|
|
|
queueInfos.data(),
|
|
|
|
|
(uint32)queueInfos.size(),
|
|
|
|
|
&features);
|
|
|
|
|
deviceInfo.enabledExtensionCount = (uint32)initializer.deviceExtensions.size();
|
|
|
|
|
deviceInfo.ppEnabledExtensionNames = initializer.deviceExtensions.data();
|
|
|
|
|
deviceInfo.enabledLayerCount = (uint32_t)initializer.layers.size();
|
|
|
|
|
deviceInfo.ppEnabledLayerNames = initializer.layers.data();
|
|
|
|
|
|
|
|
|
|
VK_CHECK(vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &handle));
|
|
|
|
|
|
|
|
|
|
graphicsQueue = new Queue(this, QueueType::GRAPHICS, graphicsQueueFamilyIndex, 0);
|
|
|
|
|
if (Gfx::useAsyncCompute && asyncComputeFamilyIndex != -1)
|
|
|
|
|
{
|
|
|
|
|
if (asyncComputeFamilyIndex == graphicsQueueFamilyIndex)
|
|
|
|
|
{
|
|
|
|
|
// Same family as graphics, but different queue
|
|
|
|
|
computeQueue = new Queue(this, QueueType::COMPUTE, asyncComputeFamilyIndex, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Different family
|
|
|
|
|
computeQueue = new Queue(this, QueueType::COMPUTE, asyncComputeFamilyIndex, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
computeQueue = new Queue(this, QueueType::COMPUTE, computeQueueFamilyIndex, 0);
|
|
|
|
|
}
|
|
|
|
|
transferQueue = new Queue(this, QueueType::TRANSFER, transferQueueFamilyIndex, 0);
|
|
|
|
|
if (dedicatedTransferQueueFamilyIndex != -1)
|
|
|
|
|
{
|
|
|
|
|
dedicatedTransferQueue = new Queue(this, QueueType::DEDICATED_TRANSFER, dedicatedTransferQueueFamilyIndex, 0);
|
|
|
|
|
}
|
|
|
|
|
queueMapping.graphicsFamily = graphicsQueue->getFamilyIndex();
|
|
|
|
|
queueMapping.computeFamily = computeQueue->getFamilyIndex();
|
|
|
|
|
queueMapping.transferFamily = transferQueue->getFamilyIndex();
|
|
|
|
|
queueMapping.dedicatedTransferFamily = dedicatedTransferQueue->getFamilyIndex();
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|