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-06-02 11:46:18 +02:00
|
|
|
#include "VulkanDescriptorSets.h"
|
|
|
|
|
#include "VulkanShader.h"
|
2020-04-12 15:47:19 +02:00
|
|
|
#include "Graphics/GraphicsResources.h"
|
2021-04-01 16:40:14 +02: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()
|
2021-04-01 16:40:14 +02:00
|
|
|
: instance(VK_NULL_HANDLE)
|
|
|
|
|
, handle(VK_NULL_HANDLE)
|
|
|
|
|
, physicalDevice(VK_NULL_HANDLE)
|
|
|
|
|
, callback(VK_NULL_HANDLE)
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
Graphics::~Graphics()
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
viewports.clear();
|
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
|
|
|
}
|
|
|
|
|
|
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-10-30 18:45:35 +01:00
|
|
|
#if ENABLE_VALIDATION
|
2020-03-13 12:44:33 +01:00
|
|
|
setupDebugCallback();
|
2020-10-30 18:45:35 +01:00
|
|
|
#endif
|
2020-03-15 15:58:01 +01:00
|
|
|
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-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;
|
|
|
|
|
}
|
2021-09-23 10:10:39 +02:00
|
|
|
Gfx::PRenderPass Graphics::createRenderPass(Gfx::PRenderTargetLayout layout, Gfx::PViewport renderArea)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2021-09-23 10:10:39 +02:00
|
|
|
PRenderPass result = new RenderPass(this, layout, renderArea);
|
2020-04-12 15:47:19 +02:00
|
|
|
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());
|
2020-09-19 14:36:50 +02:00
|
|
|
allocatedFramebuffers[framebufferHash] = framebuffer;
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
framebuffer = found->value;
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
getGraphicsCommands()->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-06-08 01:44:47 +02:00
|
|
|
getGraphicsCommands()->getCommands()->endRenderPass();
|
2021-05-10 23:57:55 +02:00
|
|
|
getGraphicsCommands()->submitCommands();
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 23:57:55 +02:00
|
|
|
void Graphics::executeCommands(const Array<Gfx::PRenderCommand>& commands)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2020-11-03 01:18:30 +01:00
|
|
|
getGraphicsCommands()->getCommands()->executeCommands(commands);
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 23:57:55 +02:00
|
|
|
void Graphics::executeCommands(const Array<Gfx::PComputeCommand>& commands)
|
|
|
|
|
{
|
|
|
|
|
getComputeCommands()->getCommands()->executeCommands(commands);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
Gfx::PTexture2D Graphics::createTexture2D(const TextureCreateInfo &createInfo)
|
|
|
|
|
{
|
2020-06-02 11:46:18 +02:00
|
|
|
PTexture2D result = new Texture2D(this, createInfo);
|
2020-04-12 15:47:19 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 01:18:30 +01:00
|
|
|
Gfx::PUniformBuffer Graphics::createUniformBuffer(const UniformBufferCreateInfo &bulkData)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
|
|
|
|
PUniformBuffer uniformBuffer = new UniformBuffer(this, bulkData);
|
|
|
|
|
return uniformBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 23:57:55 +02:00
|
|
|
Gfx::PStructuredBuffer Graphics::createStructuredBuffer(const StructuredBufferCreateInfo &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;
|
|
|
|
|
}
|
2021-05-10 23:57:55 +02:00
|
|
|
Gfx::PRenderCommand Graphics::createRenderCommand(const std::string& name)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2021-05-10 23:57:55 +02:00
|
|
|
PRenderCommand cmdBuffer = getGraphicsCommands()->createRenderCommand(name);
|
|
|
|
|
return cmdBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::PComputeCommand Graphics::createComputeCommand(const std::string& name)
|
|
|
|
|
{
|
|
|
|
|
PComputeCommand cmdBuffer = getComputeCommands()->createComputeCommand(name);
|
2020-05-05 01:51:13 +02:00
|
|
|
return cmdBuffer;
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
|
|
|
|
Gfx::PVertexDeclaration Graphics::createVertexDeclaration(const Array<Gfx::VertexElement>& element)
|
|
|
|
|
{
|
|
|
|
|
PVertexDeclaration declaration = new VertexDeclaration(element);
|
|
|
|
|
return declaration;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
Gfx::PVertexShader Graphics::createVertexShader(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PVertexShader shader = new VertexShader(this);
|
|
|
|
|
shader->create(createInfo);
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
Gfx::PControlShader Graphics::createControlShader(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PControlShader shader = new ControlShader(this);
|
|
|
|
|
shader->create(createInfo);
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
Gfx::PEvaluationShader Graphics::createEvaluationShader(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PEvaluationShader shader = new EvaluationShader(this);
|
|
|
|
|
shader->create(createInfo);
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
Gfx::PGeometryShader Graphics::createGeometryShader(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PGeometryShader shader = new GeometryShader(this);
|
|
|
|
|
shader->create(createInfo);
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
Gfx::PFragmentShader Graphics::createFragmentShader(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PFragmentShader shader = new FragmentShader(this);
|
|
|
|
|
shader->create(createInfo);
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
2021-05-10 23:57:55 +02:00
|
|
|
Gfx::PComputeShader Graphics::createComputeShader(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PComputeShader shader = new ComputeShader(this);
|
|
|
|
|
shader->create(createInfo);
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 01:51:13 +02:00
|
|
|
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(const GraphicsPipelineCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PGraphicsPipeline pipeline = pipelineCache->createPipeline(createInfo);
|
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
Gfx::PComputePipeline Graphics::createComputePipeline(const ComputePipelineCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
PComputePipeline pipeline = pipelineCache->createPipeline(createInfo);
|
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 16:40:14 +02:00
|
|
|
Gfx::PSamplerState Graphics::createSamplerState(const SamplerCreateInfo&)
|
2020-10-03 11:00:10 +02:00
|
|
|
{
|
|
|
|
|
PSamplerState sampler = new SamplerState(); // TODO: proper sampler creation
|
|
|
|
|
VkSamplerCreateInfo vkInfo =
|
|
|
|
|
init::SamplerCreateInfo();
|
|
|
|
|
VK_CHECK(vkCreateSampler(handle, &vkInfo, nullptr, &sampler->sampler));
|
|
|
|
|
return sampler;
|
|
|
|
|
}
|
2021-05-06 17:02:10 +02:00
|
|
|
Gfx::PDescriptorLayout Graphics::createDescriptorLayout(const std::string& name)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2021-05-06 17:02:10 +02:00
|
|
|
PDescriptorLayout layout = new DescriptorLayout(this, name);
|
2020-06-02 11:46:18 +02:00
|
|
|
return layout;
|
|
|
|
|
}
|
|
|
|
|
Gfx::PPipelineLayout Graphics::createPipelineLayout()
|
|
|
|
|
{
|
|
|
|
|
PPipelineLayout layout = new PipelineLayout(this);
|
|
|
|
|
return layout;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2021-05-10 23:57:55 +02:00
|
|
|
void Graphics::copyTexture(Gfx::PTexture srcTexture, Gfx::PTexture dstTexture)
|
|
|
|
|
{
|
|
|
|
|
Texture2D* src = (Texture2D*)srcTexture->getTexture2D();
|
|
|
|
|
Texture2D* dst = (Texture2D*)dstTexture->getTexture2D();
|
|
|
|
|
TextureHandle* srcHandle = (TextureHandle*)src->getNativeHandle();
|
|
|
|
|
TextureHandle* dstHandle = (TextureHandle*)dst->getNativeHandle();
|
|
|
|
|
Gfx::SeImageLayout srcLayout = srcHandle->getLayout();
|
|
|
|
|
Gfx::SeImageLayout dstLayout = dstHandle->getLayout();
|
|
|
|
|
Gfx::QueueType dstOwner = dstHandle->currentOwner;
|
|
|
|
|
src->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
|
|
|
|
dst->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
|
|
|
|
dstTexture->transferOwnership(srcHandle->currentOwner);
|
|
|
|
|
PCmdBuffer cmdBuffer = getQueueCommands(srcHandle->currentOwner)->getCommands();
|
|
|
|
|
if(srcHandle->getAspect() != dstHandle->getAspect())
|
|
|
|
|
{
|
|
|
|
|
/*VkMemoryRequirements imageRequirements;
|
|
|
|
|
vkGetImageMemoryRequirements(handle, srcHandle->getImage(), &imageRequirements);
|
|
|
|
|
PStructuredBuffer tempBuffer = createStructuredBuffer();
|
|
|
|
|
VkBufferImageCopy bufferImageCopy;
|
|
|
|
|
bufferImageCopy.bufferOffset = 0;
|
|
|
|
|
bufferImageCopy.bufferRowLength = srcTexture->getSizeX();
|
|
|
|
|
bufferImageCopy.bufferImageHeight = srcTexture->getSizeY();
|
|
|
|
|
bufferImageCopy.imageExtent.width = srcTexture->getSizeX();
|
|
|
|
|
bufferImageCopy.imageExtent.height = srcTexture->getSizeY();
|
|
|
|
|
bufferImageCopy.imageExtent.depth = 1;
|
|
|
|
|
bufferImageCopy.imageOffset.x = 0;
|
|
|
|
|
bufferImageCopy.imageOffset.y = 0;
|
|
|
|
|
bufferImageCopy.imageOffset.z = 0;
|
|
|
|
|
bufferImageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
|
|
|
bufferImageCopy.imageSubresource.baseArrayLayer = 0;
|
|
|
|
|
bufferImageCopy.imageSubresource.layerCount = 1;
|
|
|
|
|
bufferImageCopy.imageSubresource.mipLevel = 0;
|
|
|
|
|
|
|
|
|
|
vkCmdCopyImageToBuffer(cmdBuffer->getHandle(), srcHandle->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, tempBufferAllocation->getHandle(), 1, &bufferImageCopy);
|
|
|
|
|
|
|
|
|
|
bufferImageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
|
|
|
|
|
vkCmdCopyBufferToImage(cmdBuffer->getHandle(), tempBufferAllocation->getHandle(), dstHandle->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferImageCopy);
|
|
|
|
|
delete tempBufferAllocation;*/
|
|
|
|
|
throw new std::logic_error("Not yet implemented!");
|
|
|
|
|
}
|
|
|
|
|
else if (src->getSizeX() != dst->getSizeX()
|
|
|
|
|
|| src->getSizeY() != dst->getSizeY())
|
|
|
|
|
{
|
|
|
|
|
throw new std::logic_error("Not yet implemented!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
VkImageCopy copy;
|
|
|
|
|
std::memset(©, 0, sizeof(VkImageCopy));
|
|
|
|
|
copy.extent.width = srcTexture->getSizeX();
|
|
|
|
|
copy.extent.height = srcTexture->getSizeY();
|
|
|
|
|
copy.extent.depth = 1;
|
|
|
|
|
copy.srcSubresource.aspectMask = srcHandle->getAspect();
|
|
|
|
|
copy.srcSubresource.layerCount = 1;
|
|
|
|
|
copy.dstSubresource.aspectMask = dstHandle->getAspect();
|
|
|
|
|
copy.dstSubresource.layerCount = 1;
|
|
|
|
|
vkCmdCopyImage(cmdBuffer->getHandle(),
|
|
|
|
|
srcHandle->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
|
|
|
dstHandle->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
|
1, ©);
|
|
|
|
|
src->changeLayout(srcLayout);
|
|
|
|
|
dst->changeLayout(dstLayout);
|
|
|
|
|
dstTexture->transferOwnership(dstOwner);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
VkInstance Graphics::getInstance() const
|
|
|
|
|
{
|
|
|
|
|
return instance;
|
2020-03-05 11:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
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-06-02 11:46:18 +02:00
|
|
|
PCommandBufferManager Graphics::getQueueCommands(Gfx::QueueType queueType)
|
|
|
|
|
{
|
|
|
|
|
switch (queueType)
|
|
|
|
|
{
|
|
|
|
|
case Gfx::QueueType::GRAPHICS:
|
2020-06-08 01:44:47 +02:00
|
|
|
return getGraphicsCommands();
|
2020-06-02 11:46:18 +02:00
|
|
|
case Gfx::QueueType::COMPUTE:
|
2020-06-08 01:44:47 +02:00
|
|
|
return getComputeCommands();
|
2020-06-02 11:46:18 +02:00
|
|
|
case Gfx::QueueType::TRANSFER:
|
2020-06-08 01:44:47 +02:00
|
|
|
return getTransferCommands();
|
2020-06-02 11:46:18 +02:00
|
|
|
case Gfx::QueueType::DEDICATED_TRANSFER:
|
2020-06-08 01:44:47 +02:00
|
|
|
return getDedicatedTransferCommands();
|
2020-06-02 11:46:18 +02:00
|
|
|
default:
|
|
|
|
|
throw new std::logic_error("invalid queue type");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
std::mutex graphicsCommandLock;
|
2020-04-01 02:17:49 +02:00
|
|
|
PCommandBufferManager Graphics::getGraphicsCommands()
|
|
|
|
|
{
|
2020-06-08 01:44:47 +02:00
|
|
|
std::scoped_lock lock(graphicsCommandLock);
|
|
|
|
|
auto id = std::this_thread::get_id();
|
|
|
|
|
if(graphicsCommands.find(id) == graphicsCommands.end())
|
|
|
|
|
{
|
|
|
|
|
graphicsCommands[id] = new CommandBufferManager(this, graphicsQueue);
|
|
|
|
|
}
|
|
|
|
|
return graphicsCommands[id];
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
std::mutex computeCommandLock;
|
2020-04-01 02:17:49 +02:00
|
|
|
PCommandBufferManager Graphics::getComputeCommands()
|
|
|
|
|
{
|
2020-06-08 01:44:47 +02:00
|
|
|
std::scoped_lock lock(computeCommandLock);
|
|
|
|
|
auto id = std::this_thread::get_id();
|
|
|
|
|
if(computeCommands.find(id) == computeCommands.end())
|
|
|
|
|
{
|
|
|
|
|
computeCommands[id] = new CommandBufferManager(this, computeQueue);
|
|
|
|
|
}
|
|
|
|
|
return computeCommands[id];
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
std::mutex transferCommandLock;
|
2020-04-01 02:17:49 +02:00
|
|
|
PCommandBufferManager Graphics::getTransferCommands()
|
|
|
|
|
{
|
2020-06-08 01:44:47 +02:00
|
|
|
std::scoped_lock lock(transferCommandLock);
|
|
|
|
|
auto id = std::this_thread::get_id();
|
|
|
|
|
if(transferCommands.find(id) == transferCommands.end())
|
|
|
|
|
{
|
|
|
|
|
transferCommands[id] = new CommandBufferManager(this, transferQueue);
|
|
|
|
|
}
|
|
|
|
|
return transferCommands[id];
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
std::mutex dedicatedCommandLock;
|
2020-04-01 02:17:49 +02:00
|
|
|
PCommandBufferManager Graphics::getDedicatedTransferCommands()
|
|
|
|
|
{
|
2020-06-08 01:44:47 +02:00
|
|
|
std::scoped_lock lock(dedicatedCommandLock);
|
|
|
|
|
auto id = std::this_thread::get_id();
|
|
|
|
|
if(dedicatedTransferCommands.find(id) == dedicatedTransferCommands.end())
|
|
|
|
|
{
|
|
|
|
|
dedicatedTransferCommands[id] = new CommandBufferManager(this, dedicatedTransferQueue);
|
|
|
|
|
}
|
|
|
|
|
return dedicatedTransferCommands[id];
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
|
|
|
|
PAllocator Graphics::getAllocator()
|
|
|
|
|
{
|
|
|
|
|
return allocator;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
PStagingManager Graphics::getStagingManager()
|
|
|
|
|
{
|
|
|
|
|
return stagingManager;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 23:07:38 +02:00
|
|
|
|
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]);
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
#if ENABLE_VALIDATION
|
2020-03-13 12:44:33 +01:00
|
|
|
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
|
|
|
{
|
2020-05-05 01:51:13 +02:00
|
|
|
glfwInit();
|
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);
|
2020-10-03 11:00:10 +02:00
|
|
|
appInfo.apiVersion = VK_API_VERSION_1_2;
|
2020-03-15 15:58:01 +01:00
|
|
|
|
|
|
|
|
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();
|
2020-10-14 01:49:43 +02:00
|
|
|
#if ENABLE_VALIDATION
|
2020-03-15 15:58:01 +01:00
|
|
|
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));
|
2021-06-04 18:27:49 +02:00
|
|
|
|
2021-10-13 15:20:30 +02:00
|
|
|
//crashTracker.Initialize();
|
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());
|
2020-04-12 15:47:19 +02:00
|
|
|
VkPhysicalDevice bestDevice = VK_NULL_HANDLE;
|
2020-03-15 15:58:01 +01:00
|
|
|
uint32 deviceRating = 0;
|
2020-04-12 15:47:19 +02:00
|
|
|
for (auto dev : physicalDevices)
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
2020-03-16 13:29:17 +01:00
|
|
|
uint32 currentRating = 0;
|
2020-04-12 15:47:19 +02:00
|
|
|
vkGetPhysicalDeviceProperties(dev, &props);
|
|
|
|
|
vkGetPhysicalDeviceFeatures(dev, &features);
|
2020-03-16 13:29:17 +01:00
|
|
|
if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
|
|
|
|
{
|
2021-10-15 23:12:29 +02:00
|
|
|
std::cout << "found dedicated gpu " << props.deviceName << std::endl;
|
2020-03-16 13:29:17 +01:00
|
|
|
currentRating += 100;
|
|
|
|
|
}
|
|
|
|
|
else if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
|
|
|
|
|
{
|
2021-10-15 23:12:29 +02:00
|
|
|
std::cout << "found integrated gpu " << props.deviceName << std::endl;
|
2020-03-16 13:29:17 +01:00
|
|
|
currentRating += 10;
|
|
|
|
|
}
|
|
|
|
|
if (currentRating > deviceRating)
|
|
|
|
|
{
|
|
|
|
|
deviceRating = currentRating;
|
2020-04-12 15:47:19 +02:00
|
|
|
bestDevice = dev;
|
2021-10-15 23:12:29 +02:00
|
|
|
std::cout << "bestDevice: " << props.deviceName << std::endl;
|
2020-03-16 13:29:17 +01:00
|
|
|
}
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
physicalDevice = bestDevice;
|
|
|
|
|
vkGetPhysicalDeviceProperties(physicalDevice, &props);
|
|
|
|
|
vkGetPhysicalDeviceFeatures(physicalDevice, &features);
|
2020-03-16 13:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
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;
|
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);
|
2021-06-04 18:27:49 +02:00
|
|
|
#if ENABLE_VALIDATION
|
|
|
|
|
VkDeviceDiagnosticsConfigCreateInfoNV crashDiagInfo;
|
|
|
|
|
crashDiagInfo.sType = VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV;
|
|
|
|
|
crashDiagInfo.pNext = nullptr;
|
|
|
|
|
crashDiagInfo.flags =
|
|
|
|
|
VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV |
|
|
|
|
|
VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV |
|
|
|
|
|
VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV;
|
|
|
|
|
deviceInfo.pNext = &crashDiagInfo;
|
|
|
|
|
#endif
|
2020-03-24 21:05:32 +01:00
|
|
|
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();
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|