Files
Seele/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp
T

399 lines
12 KiB
C++
Raw Normal View History

2020-04-12 15:47:19 +02:00
#include "Containers/Array.h"
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 "VulkanInitializer.h"
2020-03-24 21:05:32 +01:00
#include "VulkanCommandBuffer.h"
2020-04-12 15:47:19 +02:00
#include "VulkanRenderPass.h"
#include "VulkanFramebuffer.h"
2020-05-05 01:51:13 +02:00
#include "VulkanPipelineCache.h"
2020-04-12 15:47:19 +02:00
#include "Graphics/GraphicsResources.h"
#include <glfw/glfw3.h>
2020-03-02 19:07:49 +01:00
using namespace Seele::Vulkan;
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)
{
}
Graphics::~Graphics()
{
2020-04-12 15:47:19 +02:00
allocator = nullptr;
stagingManager = nullptr;
graphicsCommands = nullptr;
computeCommands = nullptr;
transferCommands = nullptr;
dedicatedTransferCommands = nullptr;
viewports.clear();
2020-03-24 21:05:32 +01:00
vkDestroyDevice(handle, nullptr);
DestroyDebugReportCallbackEXT(instance, nullptr, callback);
vkDestroyInstance(instance, nullptr);
}
void Graphics::init(GraphicsInitializer initInfo)
2020-03-02 19:07:49 +01:00
{
initInstance(initInfo);
2020-03-13 12:44:33 +01:00
setupDebugCallback();
pickPhysicalDevice();
2020-03-24 21:05:32 +01:00
createDevice(initInfo);
allocator = new Allocator(this);
2020-04-12 15:47:19 +02:00
stagingManager = new StagingManager(this, allocator);
2020-03-24 21:05:32 +01:00
graphicsCommands = new CommandBufferManager(this, graphicsQueue);
computeCommands = new CommandBufferManager(this, computeQueue);
transferCommands = new CommandBufferManager(this, transferQueue);
dedicatedTransferCommands = new CommandBufferManager(this, dedicatedTransferQueue);
2020-05-05 01:51:13 +02:00
pipelineCache = new PipelineCache(this, "pipeline.cache");
2020-03-02 19:07:49 +01:00
}
2020-04-12 15:47:19 +02:00
Gfx::PWindow Graphics::createWindow(const WindowCreateInfo &createInfo)
2020-03-02 19:07:49 +01:00
{
2020-04-12 15:47:19 +02:00
PWindow result = new Window(this, createInfo);
return result;
2020-03-02 19:07:49 +01:00
}
2020-04-12 15:47:19 +02:00
Gfx::PViewport Graphics::createViewport(Gfx::PWindow owner, const ViewportCreateInfo &viewportInfo)
2020-03-02 19:07:49 +01:00
{
2020-04-12 15:47:19 +02:00
PViewport result = new Viewport(this, owner, viewportInfo);
viewports.add(result);
return result;
}
Gfx::PRenderPass Graphics::createRenderPass(Gfx::PRenderTargetLayout layout)
{
PRenderPass result = new RenderPass(this, layout);
return result;
}
void Graphics::beginRenderPass(Gfx::PRenderPass renderPass)
{
PRenderPass rp = renderPass.cast<RenderPass>();
uint32 framebufferHash = rp->getFramebufferHash();
PFramebuffer framebuffer;
auto found = allocatedFramebuffers.find(framebufferHash);
2020-05-05 01:51:13 +02:00
if (found == allocatedFramebuffers.end())
2020-04-12 15:47:19 +02:00
{
framebuffer = new Framebuffer(this, rp, rp->getLayout());
}
else
{
framebuffer = found->value;
}
graphicsCommands->getCommands()->beginRenderPass(rp, framebuffer);
2020-03-02 19:07:49 +01:00
}
2020-04-12 15:47:19 +02:00
void Graphics::endRenderPass()
2020-03-02 19:07:49 +01:00
{
2020-04-12 15:47:19 +02:00
graphicsCommands->getCommands()->endRenderPass();
}
2020-05-05 01:51:13 +02:00
void Graphics::executeCommands(Array<Gfx::PRenderCommand> commands)
{
Array<VkCommandBuffer> cmdBuffers(commands.size());
for (uint32 i = 0; i < commands.size(); ++i)
{
PSecondaryCmdBuffer buf = commands[i].cast<SecondaryCmdBuffer>();
buf->end();
cmdBuffers[i] = buf->getHandle();
}
vkCmdExecuteCommands(graphicsCommands->getCommands()->getHandle(), cmdBuffers.size(), cmdBuffers.data());
}
2020-04-12 15:47:19 +02:00
Gfx::PTexture2D Graphics::createTexture2D(const TextureCreateInfo &createInfo)
{
PTexture2D result = new Texture2D(this, createInfo.width, createInfo.height, createInfo.bArray,
createInfo.bArray, createInfo.arrayLayers, createInfo.format,
createInfo.samples, createInfo.usage, createInfo.queueType);
return result;
}
2020-05-05 01:51:13 +02:00
Gfx::PUniformBuffer Graphics::createUniformBuffer(const BulkResourceData &bulkData)
2020-04-12 15:47:19 +02:00
{
PUniformBuffer uniformBuffer = new UniformBuffer(this, bulkData);
return uniformBuffer;
}
2020-05-05 01:51:13 +02:00
Gfx::PStructuredBuffer Graphics::createStructuredBuffer(const BulkResourceData &bulkData)
2020-04-12 15:47:19 +02:00
{
PStructuredBuffer structuredBuffer = new StructuredBuffer(this, bulkData);
return structuredBuffer;
}
2020-05-05 01:51:13 +02:00
Gfx::PVertexBuffer Graphics::createVertexBuffer(const VertexBufferCreateInfo &bulkData)
2020-04-12 15:47:19 +02:00
{
PVertexBuffer vertexBuffer = new VertexBuffer(this, bulkData);
return vertexBuffer;
}
2020-05-05 01:51:13 +02:00
Gfx::PIndexBuffer Graphics::createIndexBuffer(const IndexBufferCreateInfo &bulkData)
2020-04-12 15:47:19 +02:00
{
PIndexBuffer indexBuffer = new IndexBuffer(this, bulkData);
return indexBuffer;
}
2020-05-05 01:51:13 +02:00
Gfx::PRenderCommand Graphics::createRenderCommand()
{
PSecondaryCmdBuffer cmdBuffer = graphicsCommands->createSecondaryCmdBuffer();
cmdBuffer->begin(getGraphicsCommands()->getCommands());
return cmdBuffer;
}
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(const GraphicsPipelineCreateInfo& createInfo)
{
PGraphicsPipeline pipeline = pipelineCache->createPipeline(createInfo);
return pipeline;
}
2020-04-12 15:47:19 +02:00
VkInstance Graphics::getInstance() const
{
return instance;
2020-03-05 11:43:38 +01:00
}
VkDevice Graphics::getDevice() const
2020-03-13 12:44:33 +01:00
{
return handle;
}
VkPhysicalDevice Graphics::getPhysicalDevice() const
2020-03-13 12:44:33 +01:00
{
return physicalDevice;
}
2020-04-01 02:17:49 +02:00
PCommandBufferManager Graphics::getGraphicsCommands()
{
return graphicsCommands;
}
PCommandBufferManager Graphics::getComputeCommands()
{
return computeCommands;
}
PCommandBufferManager Graphics::getTransferCommands()
{
return transferCommands;
}
PCommandBufferManager Graphics::getDedicatedTransferCommands()
{
return dedicatedTransferCommands;
}
PAllocator Graphics::getAllocator()
{
return allocator;
}
2020-04-12 15:47:19 +02:00
PStagingManager Graphics::getStagingManager()
{
return stagingManager;
}
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;
}
void Graphics::initInstance(GraphicsInitializer initInfo)
{
2020-05-05 01:51:13 +02:00
glfwInit();
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();
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));
}
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));
}
void Graphics::pickPhysicalDevice()
{
uint32 physicalDeviceCount;
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr);
Array<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
2020-04-12 15:47:19 +02:00
VkPhysicalDevice bestDevice = VK_NULL_HANDLE;
uint32 deviceRating = 0;
2020-04-12 15:47:19 +02:00
for (auto dev : physicalDevices)
{
uint32 currentRating = 0;
2020-04-12 15:47:19 +02:00
vkGetPhysicalDeviceProperties(dev, &props);
vkGetPhysicalDeviceFeatures(dev, &features);
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;
2020-04-12 15:47:19 +02:00
bestDevice = dev;
}
}
2020-04-12 15:47:19 +02:00
physicalDevice = bestDevice;
vkGetPhysicalDeviceProperties(physicalDevice, &props);
vkGetPhysicalDeviceFeatures(physicalDevice, &features);
}
2020-03-24 21:05:32 +01:00
void Graphics::createDevice(GraphicsInitializer initializer)
{
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-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;
2020-04-12 15:47:19 +02:00
uint32 numPriorities = 0;
for (uint32 familyIndex = 0; familyIndex < queueProperties.size(); ++familyIndex)
2020-03-24 21:05:32 +01:00
{
2020-04-01 02:17:49 +02:00
uint32 numQueuesForFamily = 0;
VkQueueFamilyProperties currProps = queueProperties[familyIndex];
2020-04-12 15:47:19 +02:00
// bool bSparse = currProps.queueFlags & VK_QUEUE_SPARSE_BINDING_BIT;
2020-04-01 02:17:49 +02:00
currProps.queueFlags = currProps.queueFlags ^ VK_QUEUE_SPARSE_BINDING_BIT;
2020-03-24 21:05:32 +01:00
if ((currProps.queueFlags & VK_QUEUE_GRAPHICS_BIT) == VK_QUEUE_GRAPHICS_BIT)
{
if (graphicsQueueFamilyIndex == -1)
{
graphicsQueueFamilyIndex = familyIndex;
2020-04-01 02:17:49 +02:00
numQueuesForFamily++;
2020-03-24 21:05:32 +01:00
}
}
if ((currProps.queueFlags & VK_QUEUE_COMPUTE_BIT) == VK_QUEUE_COMPUTE_BIT)
{
if (computeQueueFamilyIndex == -1)
{
computeQueueFamilyIndex = familyIndex;
}
if (Gfx::useAsyncCompute)
{
if (asyncComputeFamilyIndex == -1)
{
2020-04-12 15:47:19 +02:00
if (familyIndex == (uint32)graphicsQueueFamilyIndex)
2020-03-24 21:05:32 +01:00
{
if (currProps.queueCount > 1)
{
asyncComputeFamilyIndex = familyIndex;
2020-04-01 02:17:49 +02:00
numQueuesForFamily++;
2020-03-24 21:05:32 +01:00
}
}
else
{
asyncComputeFamilyIndex = familyIndex;
}
}
}
}
if ((currProps.queueFlags & VK_QUEUE_TRANSFER_BIT) == VK_QUEUE_TRANSFER_BIT)
{
if (transferQueueFamilyIndex == -1)
{
transferQueueFamilyIndex = familyIndex;
2020-04-01 02:17:49 +02:00
numQueuesForFamily++;
2020-03-24 21:05:32 +01:00
}
if ((currProps.queueFlags ^ VK_QUEUE_TRANSFER_BIT) == 0)
{
dedicatedTransferQueueFamilyIndex = familyIndex;
2020-04-01 02:17:49 +02:00
numQueuesForFamily++;
2020-03-24 21:05:32 +01:00
}
}
2020-04-12 15:47:19 +02:00
if (numQueuesForFamily > 0)
2020-04-01 02:17:49 +02:00
{
VkDeviceQueueCreateInfo info =
init::DeviceQueueCreateInfo(familyIndex, numQueuesForFamily);
2020-04-12 15:47:19 +02:00
numPriorities += numQueuesForFamily;
2020-04-01 02:17:49 +02:00
queueInfos.add(info);
}
2020-03-24 21:05:32 +01:00
}
2020-04-12 15:47:19 +02:00
Array<float> queuePriorities;
queuePriorities.resize(numPriorities);
float *currentPriority = queuePriorities.data();
for (uint32 index = 0; index < queueInfos.size(); ++index)
{
VkDeviceQueueCreateInfo &currQueue = queueInfos[index];
currQueue.pQueuePriorities = currentPriority;
for (int32_t queueIndex = 0; queueIndex < (int32_t)currQueue.queueCount; ++queueIndex)
{
*currentPriority++ = 1.0f;
}
}
2020-03-24 21:05:32 +01:00
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));
2020-04-12 15:47:19 +02:00
graphicsQueue = new Queue(this, Gfx::QueueType::GRAPHICS, graphicsQueueFamilyIndex, 0);
2020-03-24 21:05:32 +01:00
if (Gfx::useAsyncCompute && asyncComputeFamilyIndex != -1)
{
if (asyncComputeFamilyIndex == graphicsQueueFamilyIndex)
{
// Same family as graphics, but different queue
2020-04-12 15:47:19 +02:00
computeQueue = new Queue(this, Gfx::QueueType::COMPUTE, asyncComputeFamilyIndex, 1);
2020-03-24 21:05:32 +01:00
}
else
{
// Different family
2020-04-12 15:47:19 +02:00
computeQueue = new Queue(this, Gfx::QueueType::COMPUTE, asyncComputeFamilyIndex, 0);
2020-03-24 21:05:32 +01:00
}
}
else
{
2020-04-12 15:47:19 +02:00
computeQueue = new Queue(this, Gfx::QueueType::COMPUTE, computeQueueFamilyIndex, 0);
2020-03-24 21:05:32 +01:00
}
2020-04-12 15:47:19 +02:00
transferQueue = new Queue(this, Gfx::QueueType::TRANSFER, transferQueueFamilyIndex, 0);
2020-03-24 21:05:32 +01:00
if (dedicatedTransferQueueFamilyIndex != -1)
{
2020-04-12 15:47:19 +02:00
dedicatedTransferQueue = new Queue(this, Gfx::QueueType::DEDICATED_TRANSFER, dedicatedTransferQueueFamilyIndex, 0);
}
else
{
dedicatedTransferQueue = transferQueue;
2020-03-24 21:05:32 +01:00
}
queueMapping.graphicsFamily = graphicsQueue->getFamilyIndex();
queueMapping.computeFamily = computeQueue->getFamilyIndex();
queueMapping.transferFamily = transferQueue->getFamilyIndex();
queueMapping.dedicatedTransferFamily = dedicatedTransferQueue->getFamilyIndex();
}