2023-11-01 23:12:30 +01:00
|
|
|
#include "Graphics.h"
|
2023-11-15 17:42:57 +01:00
|
|
|
#include "Debug.h"
|
2023-11-01 23:12:30 +01:00
|
|
|
#include "Allocator.h"
|
|
|
|
|
#include "Buffer.h"
|
2023-11-06 22:24:40 +01:00
|
|
|
#include "Graphics/Enums.h"
|
2023-11-01 23:12:30 +01:00
|
|
|
#include "PipelineCache.h"
|
2023-11-15 00:06:00 +01:00
|
|
|
#include "Command.h"
|
2023-11-15 17:42:57 +01:00
|
|
|
#include "Descriptor.h"
|
2023-11-16 22:58:47 +01:00
|
|
|
#include "Window.h"
|
2023-11-01 23:12:30 +01:00
|
|
|
#include "RenderPass.h"
|
|
|
|
|
#include "Framebuffer.h"
|
2023-11-05 10:36:01 +01:00
|
|
|
#include "Shader.h"
|
2021-04-01 16:40:14 +02:00
|
|
|
#include <GLFW/glfw3.h>
|
2023-11-06 22:24:40 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
#include <vulkan/vulkan_core.h>
|
2020-03-02 19:07:49 +01:00
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
using namespace Seele;
|
2020-03-20 01:27:40 +01:00
|
|
|
using namespace Seele::Vulkan;
|
2020-03-15 15:58:01 +01:00
|
|
|
|
2023-11-15 17:42:57 +01:00
|
|
|
thread_local OCommandPool Seele::Vulkan::Graphics::graphicsCommands = nullptr;
|
|
|
|
|
thread_local OCommandPool Seele::Vulkan::Graphics::computeCommands = nullptr;
|
|
|
|
|
thread_local OCommandPool Seele::Vulkan::Graphics::transferCommands = nullptr;
|
|
|
|
|
thread_local OCommandPool Seele::Vulkan::Graphics::dedicatedTransferCommands = nullptr;
|
2022-01-12 14:40:26 +01:00
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
Graphics::Graphics()
|
2021-11-14 20:36:53 +01: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
|
|
|
{
|
2023-12-02 10:55:00 +01:00
|
|
|
vkDeviceWaitIdle(handle);
|
|
|
|
|
graphicsCommands = nullptr;
|
|
|
|
|
computeCommands = nullptr;
|
|
|
|
|
transferCommands = nullptr;
|
|
|
|
|
dedicatedTransferCommands = nullptr;
|
|
|
|
|
pipelineCache = nullptr;
|
|
|
|
|
allocator = nullptr;
|
|
|
|
|
destructionManager = nullptr;
|
|
|
|
|
stagingManager = nullptr;
|
|
|
|
|
allocatedFramebuffers.clear();
|
|
|
|
|
shaderCompiler = nullptr;
|
2021-11-14 20:36:53 +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
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
initInstance(initInfo);
|
2020-10-30 18:45:35 +01:00
|
|
|
#if ENABLE_VALIDATION
|
2021-11-14 20:36:53 +01:00
|
|
|
setupDebugCallback();
|
2020-10-30 18:45:35 +01:00
|
|
|
#endif
|
2021-11-14 20:36:53 +01:00
|
|
|
pickPhysicalDevice();
|
|
|
|
|
createDevice(initInfo);
|
|
|
|
|
allocator = new Allocator(this);
|
|
|
|
|
stagingManager = new StagingManager(this, allocator);
|
|
|
|
|
pipelineCache = new PipelineCache(this, "pipeline.cache");
|
2023-11-12 16:11:27 +01:00
|
|
|
destructionManager = new DestructionManager(this);
|
2020-03-02 19:07:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OWindow Graphics::createWindow(const WindowCreateInfo &createInfo)
|
2020-03-02 19:07:49 +01:00
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
return new Window(this, createInfo);
|
2020-03-02 19:07:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OViewport Graphics::createViewport(Gfx::PWindow owner, const ViewportCreateInfo &viewportInfo)
|
2020-03-02 19:07:49 +01:00
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
return new Viewport(this, owner, viewportInfo);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::ORenderPass Graphics::createRenderPass(Gfx::ORenderTargetLayout layout, Gfx::PViewport renderArea)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
return new RenderPass(this, std::move(layout), renderArea);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
void Graphics::beginRenderPass(Gfx::PRenderPass renderPass)
|
|
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
PRenderPass rp = renderPass.cast<RenderPass>();
|
|
|
|
|
uint32 framebufferHash = rp->getFramebufferHash();
|
|
|
|
|
PFramebuffer framebuffer;
|
|
|
|
|
{
|
|
|
|
|
auto found = allocatedFramebuffers.find(framebufferHash);
|
|
|
|
|
if (found == allocatedFramebuffers.end())
|
|
|
|
|
{
|
2023-11-01 23:12:30 +01:00
|
|
|
allocatedFramebuffers[framebufferHash] = new Framebuffer(this, rp, rp->getLayout());
|
|
|
|
|
framebuffer = allocatedFramebuffers[framebufferHash];
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-01 23:12:30 +01:00
|
|
|
framebuffer = std::move(found->value);
|
2021-11-14 20:36:53 +01: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
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
getGraphicsCommands()->getCommands()->endRenderPass();
|
|
|
|
|
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
|
|
|
{
|
2021-11-14 20:36:53 +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)
|
|
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
getComputeCommands()->getCommands()->executeCommands(commands);
|
2021-05-10 23:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OTexture2D Graphics::createTexture2D(const TextureCreateInfo &createInfo)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new Texture2D(this, createInfo);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OTexture3D Graphics::createTexture3D(const TextureCreateInfo &createInfo)
|
2023-01-29 18:58:59 +01:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new Texture3D(this, createInfo);
|
2023-01-29 18:58:59 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OTextureCube Graphics::createTextureCube(const TextureCreateInfo &createInfo)
|
2023-01-29 18:58:59 +01:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new TextureCube(this, createInfo);
|
2023-01-29 18:58:59 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OUniformBuffer Graphics::createUniformBuffer(const UniformBufferCreateInfo &bulkData)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new UniformBuffer(this, bulkData);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OShaderBuffer Graphics::createShaderBuffer(const ShaderBufferCreateInfo &bulkData)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new ShaderBuffer(this, bulkData);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OVertexBuffer Graphics::createVertexBuffer(const VertexBufferCreateInfo &bulkData)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new VertexBuffer(this, bulkData);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OIndexBuffer Graphics::createIndexBuffer(const IndexBufferCreateInfo &bulkData)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new IndexBuffer(this, bulkData);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
2021-05-10 23:57:55 +02:00
|
|
|
Gfx::PRenderCommand Graphics::createRenderCommand(const std::string& name)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
return getGraphicsCommands()->createRenderCommand(name);
|
2021-05-10 23:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::PComputeCommand Graphics::createComputeCommand(const std::string& name)
|
|
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return getComputeCommands()->createComputeCommand(name);
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OVertexShader Graphics::createVertexShader(const ShaderCreateInfo& createInfo)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-01 23:12:30 +01:00
|
|
|
OVertexShader shader = new VertexShader(this);
|
2021-11-14 20:36:53 +01:00
|
|
|
shader->create(createInfo);
|
2023-11-12 16:11:27 +01:00
|
|
|
return shader;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::OFragmentShader Graphics::createFragmentShader(const ShaderCreateInfo& createInfo)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
OFragmentShader shader = new FragmentShader(this);
|
2021-11-14 20:36:53 +01:00
|
|
|
shader->create(createInfo);
|
2023-11-12 16:11:27 +01:00
|
|
|
return shader;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::OComputeShader Graphics::createComputeShader(const ShaderCreateInfo& createInfo)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
OComputeShader shader = new ComputeShader(this);
|
2021-11-14 20:36:53 +01:00
|
|
|
shader->create(createInfo);
|
2023-11-12 16:11:27 +01:00
|
|
|
return shader;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::OTaskShader Graphics::createTaskShader(const ShaderCreateInfo& createInfo)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
OTaskShader shader = new TaskShader(this);
|
2021-11-14 20:36:53 +01:00
|
|
|
shader->create(createInfo);
|
2023-11-12 16:11:27 +01:00
|
|
|
return shader;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::OMeshShader Graphics::createMeshShader(const ShaderCreateInfo& createInfo)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
OMeshShader shader = new MeshShader(this);
|
2021-11-14 20:36:53 +01:00
|
|
|
shader->create(createInfo);
|
2023-11-12 16:11:27 +01:00
|
|
|
return shader;
|
2021-05-10 23:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo createInfo)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2023-11-09 22:15:51 +01:00
|
|
|
return pipelineCache->createPipeline(std::move(createInfo));
|
2023-11-05 10:36:01 +01:00
|
|
|
}
|
2023-11-26 11:27:39 +01:00
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::MeshPipelineCreateInfo createInfo)
|
2023-11-05 10:36:01 +01:00
|
|
|
{
|
2023-11-09 22:15:51 +01:00
|
|
|
return pipelineCache->createPipeline(std::move(createInfo));
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
Gfx::PComputePipeline Graphics::createComputePipeline(Gfx::ComputePipelineCreateInfo createInfo)
|
2021-05-06 17:02:10 +02:00
|
|
|
{
|
2023-11-09 22:15:51 +01:00
|
|
|
return pipelineCache->createPipeline(std::move(createInfo));
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-15 17:42:57 +01:00
|
|
|
Gfx::OSampler Graphics::createSampler(const SamplerCreateInfo& createInfo)
|
2020-10-03 11:00:10 +02:00
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
OSampler sampler = new Sampler();
|
|
|
|
|
VkSamplerCreateInfo vkInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = createInfo.flags,
|
|
|
|
|
.magFilter = cast(createInfo.magFilter),
|
|
|
|
|
.minFilter = cast(createInfo.minFilter),
|
|
|
|
|
.mipmapMode = cast(createInfo.mipmapMode),
|
|
|
|
|
.addressModeU = cast(createInfo.addressModeU),
|
|
|
|
|
.addressModeV = cast(createInfo.addressModeV),
|
|
|
|
|
.addressModeW = cast(createInfo.addressModeW),
|
|
|
|
|
.mipLodBias = createInfo.mipLodBias,
|
|
|
|
|
.anisotropyEnable = createInfo.anisotropyEnable,
|
|
|
|
|
.maxAnisotropy = createInfo.maxAnisotropy,
|
|
|
|
|
.compareEnable = createInfo.compareEnable,
|
|
|
|
|
.compareOp = cast(createInfo.compareOp),
|
|
|
|
|
.minLod = createInfo.minLod,
|
|
|
|
|
.maxLod = createInfo.maxLod,
|
|
|
|
|
.borderColor = cast(createInfo.borderColor),
|
|
|
|
|
.unnormalizedCoordinates = createInfo.unnormalizedCoordinates,
|
|
|
|
|
};
|
2021-11-14 20:36:53 +01:00
|
|
|
VK_CHECK(vkCreateSampler(handle, &vkInfo, nullptr, &sampler->sampler));
|
2023-11-12 16:11:27 +01:00
|
|
|
return sampler;
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
2023-11-12 16:11:27 +01:00
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new DescriptorLayout(this, name);
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2023-11-12 16:11:27 +01:00
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::OPipelineLayout Graphics::createPipelineLayout(Gfx::PPipelineLayout baseLayout)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
return new PipelineLayout(this, baseLayout);
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2023-12-10 22:27:59 +01:00
|
|
|
void Graphics::resolveTexture(Gfx::PTexture source, Gfx::PTexture destination)
|
|
|
|
|
{
|
|
|
|
|
PTextureBase sourceTex = source.cast<TextureBase>();
|
|
|
|
|
PTextureBase destinationTex = destination.cast<TextureBase>();
|
|
|
|
|
VkImageResolve resolve = {
|
|
|
|
|
.srcSubresource = VkImageSubresourceLayers {
|
|
|
|
|
.aspectMask = sourceTex->getAspect(),
|
|
|
|
|
.mipLevel = 0,
|
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
|
.layerCount = 1,
|
|
|
|
|
},
|
|
|
|
|
.srcOffset = VkOffset3D {
|
|
|
|
|
.x = 0,
|
|
|
|
|
.y = 0,
|
|
|
|
|
.z = 0,
|
|
|
|
|
},
|
|
|
|
|
.dstSubresource = VkImageSubresourceLayers {
|
|
|
|
|
.aspectMask = sourceTex->getAspect(),
|
|
|
|
|
.mipLevel = 0,
|
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
|
.layerCount = 1,
|
|
|
|
|
},
|
|
|
|
|
.dstOffset = VkOffset3D {
|
|
|
|
|
.x = 0,
|
|
|
|
|
.y = 0,
|
|
|
|
|
.z = 0,
|
|
|
|
|
},
|
|
|
|
|
.extent = VkExtent3D {
|
|
|
|
|
.width = sourceTex->getWidth(),
|
|
|
|
|
.height = sourceTex->getHeight(),
|
|
|
|
|
.depth = sourceTex->getDepth(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
vkCmdResolveImage(getGraphicsCommands()->getCommands()->getHandle(), sourceTex->getImage(), cast(sourceTex->getLayout()), destinationTex->getImage(), cast(destinationTex->getLayout()), 1, &resolve);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
void Graphics::vkCmdDrawMeshTasksEXT(VkCommandBuffer handle, uint32 groupX, uint32 groupY, uint32 groupZ)
|
|
|
|
|
{
|
|
|
|
|
cmdDrawMeshTasks(handle, groupX, groupY, groupZ);
|
|
|
|
|
}
|
2023-11-15 00:06:00 +01:00
|
|
|
|
2023-11-15 17:42:57 +01:00
|
|
|
PCommandPool Graphics::getQueueCommands(Gfx::QueueType queueType)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
switch (queueType)
|
|
|
|
|
{
|
|
|
|
|
case Gfx::QueueType::GRAPHICS:
|
|
|
|
|
return getGraphicsCommands();
|
|
|
|
|
case Gfx::QueueType::COMPUTE:
|
|
|
|
|
return getComputeCommands();
|
|
|
|
|
case Gfx::QueueType::TRANSFER:
|
|
|
|
|
return getTransferCommands();
|
|
|
|
|
case Gfx::QueueType::DEDICATED_TRANSFER:
|
|
|
|
|
return getDedicatedTransferCommands();
|
|
|
|
|
default:
|
|
|
|
|
throw new std::logic_error("invalid queue type");
|
|
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2023-11-15 17:42:57 +01:00
|
|
|
PCommandPool Graphics::getGraphicsCommands()
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if(graphicsCommands == nullptr)
|
2021-11-14 20:36:53 +01:00
|
|
|
{
|
2023-11-15 00:06:00 +01:00
|
|
|
graphicsCommands = new CommandPool(this, graphicsQueue);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
return graphicsCommands;
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
2023-11-15 17:42:57 +01:00
|
|
|
PCommandPool Graphics::getComputeCommands()
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if(computeCommands == nullptr)
|
2021-11-14 20:36:53 +01:00
|
|
|
{
|
2023-11-15 00:06:00 +01:00
|
|
|
computeCommands = new CommandPool(this, computeQueue);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
return computeCommands;
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
2023-11-15 17:42:57 +01:00
|
|
|
PCommandPool Graphics::getTransferCommands()
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if(transferCommands == nullptr)
|
2021-11-14 20:36:53 +01:00
|
|
|
{
|
2023-11-15 00:06:00 +01:00
|
|
|
transferCommands = new CommandPool(this, transferQueue);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
return transferCommands;
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
2023-11-15 17:42:57 +01:00
|
|
|
PCommandPool Graphics::getDedicatedTransferCommands()
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
2022-02-24 22:38:26 +01:00
|
|
|
if(dedicatedTransferCommands == nullptr)
|
2021-11-14 20:36:53 +01:00
|
|
|
{
|
2023-11-15 00:06:00 +01:00
|
|
|
dedicatedTransferCommands = new CommandPool(this, dedicatedTransferQueue != nullptr ? dedicatedTransferQueue : transferQueue);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
return dedicatedTransferCommands;
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
|
|
|
|
PAllocator Graphics::getAllocator()
|
|
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
return allocator;
|
2020-04-01 02:17:49 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
PStagingManager Graphics::getStagingManager()
|
|
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
return stagingManager;
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-12 16:11:27 +01:00
|
|
|
PDestructionManager Graphics::getDestructionManager()
|
|
|
|
|
{
|
|
|
|
|
return destructionManager;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
Array<const char *> Graphics::getRequiredExtensions()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
Array<const char *> extensions;
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
unsigned int glfwExtensionCount = 0;
|
|
|
|
|
const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
for (unsigned int i = 0; i < glfwExtensionCount; i++)
|
|
|
|
|
{
|
|
|
|
|
extensions.add(glfwExtensions[i]);
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
#if ENABLE_VALIDATION
|
2021-11-14 20:36:53 +01:00
|
|
|
extensions.add(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
2020-03-13 12:44:33 +01:00
|
|
|
#endif // ENABLE_VALIDATION
|
2021-11-14 20:36:53 +01:00
|
|
|
return extensions;
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2020-03-20 01:27:40 +01:00
|
|
|
void Graphics::initInstance(GraphicsInitializer initInfo)
|
2020-03-15 15:58:01 +01:00
|
|
|
{
|
2021-11-14 20:36:53 +01: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_2;
|
2020-03-15 15:58:01 +01:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
VkInstanceCreateInfo info = {};
|
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
|
info.pApplicationInfo = &appInfo;
|
|
|
|
|
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();
|
2020-10-14 01:49:43 +02:00
|
|
|
#if ENABLE_VALIDATION
|
2023-11-17 18:46:37 +01:00
|
|
|
initInfo.layers.add("VK_LAYER_KHRONOS_validation");
|
|
|
|
|
#endif
|
2021-11-14 20:36:53 +01:00
|
|
|
info.enabledLayerCount = (uint32)initInfo.layers.size();
|
|
|
|
|
info.ppEnabledLayerNames = initInfo.layers.data();
|
|
|
|
|
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
2020-03-20 01:27:40 +01:00
|
|
|
void Graphics::setupDebugCallback()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
VkDebugReportCallbackCreateInfoEXT createInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT,
|
|
|
|
|
.pfnCallback = &debugCallback,
|
|
|
|
|
.pUserData = nullptr,
|
|
|
|
|
};
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
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
|
|
|
{
|
2021-11-14 20:36:53 +01:00
|
|
|
uint32 physicalDeviceCount;
|
|
|
|
|
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr);
|
|
|
|
|
Array<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
|
|
|
|
|
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
|
|
|
|
|
VkPhysicalDevice bestDevice = VK_NULL_HANDLE;
|
|
|
|
|
uint32 deviceRating = 0;
|
2023-11-22 13:18:54 +01:00
|
|
|
features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
|
2023-12-12 11:37:00 +01:00
|
|
|
features.pNext = &features11;
|
|
|
|
|
features11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
|
|
|
|
|
features11.pNext = &features12;
|
2023-11-22 13:18:54 +01:00
|
|
|
features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
|
|
|
|
|
features12.pNext = nullptr;
|
2021-11-14 20:36:53 +01:00
|
|
|
for (auto dev : physicalDevices)
|
|
|
|
|
{
|
|
|
|
|
uint32 currentRating = 0;
|
|
|
|
|
vkGetPhysicalDeviceProperties(dev, &props);
|
2023-11-22 13:18:54 +01:00
|
|
|
vkGetPhysicalDeviceFeatures2(dev, &features);
|
2021-11-14 20:36:53 +01:00
|
|
|
if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "found dedicated gpu " << props.deviceName << std::endl;
|
|
|
|
|
currentRating += 100;
|
|
|
|
|
}
|
|
|
|
|
else if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "found integrated gpu " << props.deviceName << std::endl;
|
|
|
|
|
currentRating += 10;
|
|
|
|
|
}
|
|
|
|
|
if (currentRating > deviceRating)
|
|
|
|
|
{
|
|
|
|
|
deviceRating = currentRating;
|
|
|
|
|
bestDevice = dev;
|
|
|
|
|
std::cout << "bestDevice: " << props.deviceName << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
physicalDevice = bestDevice;
|
2023-11-22 13:18:54 +01:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
vkGetPhysicalDeviceProperties(physicalDevice, &props);
|
2023-11-22 13:18:54 +01:00
|
|
|
vkGetPhysicalDeviceFeatures2(physicalDevice, &features);
|
2023-11-07 16:55:13 +01:00
|
|
|
if (Gfx::useMeshShading)
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
uint32 count = 0;
|
2023-11-18 11:04:30 +01:00
|
|
|
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, nullptr);
|
2023-11-07 16:55:13 +01:00
|
|
|
Array<VkExtensionProperties> extensionProps(count);
|
2023-11-18 11:04:30 +01:00
|
|
|
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, extensionProps.data());
|
2023-11-07 16:55:13 +01:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
2023-11-07 16:55:13 +01:00
|
|
|
if (std::strcmp(VK_EXT_MESH_SHADER_EXTENSION_NAME, extensionProps[i].extensionName) == 0)
|
|
|
|
|
{
|
2023-12-12 11:37:00 +01:00
|
|
|
//meshShadingEnabled = true;
|
2023-11-07 16:55:13 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2023-11-06 22:24:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
{
|
2021-11-14 20:36:53 +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
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
Array<VkDeviceQueueCreateInfo> queueInfos;
|
|
|
|
|
struct QueueCreateInfo
|
|
|
|
|
{
|
|
|
|
|
int32 familyIndex = -1;
|
|
|
|
|
int32 queueIndex = -1;
|
|
|
|
|
};
|
|
|
|
|
QueueCreateInfo graphicsQueueInfo;
|
|
|
|
|
QueueCreateInfo transferQueueInfo;
|
|
|
|
|
QueueCreateInfo dedicatedTransferQueueInfo;
|
|
|
|
|
QueueCreateInfo computeQueueInfo;
|
|
|
|
|
QueueCreateInfo asyncComputeInfo;
|
|
|
|
|
uint32 numPriorities = 0;
|
|
|
|
|
auto checkFamilyProperty = [](VkQueueFamilyProperties currProps, uint32 checkBit){
|
|
|
|
|
return (currProps.queueFlags & checkBit) == checkBit;
|
|
|
|
|
};
|
|
|
|
|
for (uint32 familyIndex = 0; familyIndex < queueProperties.size(); ++familyIndex)
|
|
|
|
|
{
|
|
|
|
|
uint32 numQueuesForFamily = 0;
|
|
|
|
|
VkQueueFamilyProperties currProps = queueProperties[familyIndex];
|
2021-10-16 13:21:53 +02:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
if (checkFamilyProperty(currProps, VK_QUEUE_GRAPHICS_BIT))
|
|
|
|
|
{
|
|
|
|
|
if (graphicsQueueInfo.familyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
graphicsQueueInfo.familyIndex = familyIndex;
|
|
|
|
|
numQueuesForFamily++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-13 14:56:13 +01:00
|
|
|
if (currProps.queueFlags & VK_QUEUE_COMPUTE_BIT)
|
2021-11-14 20:36:53 +01:00
|
|
|
{
|
|
|
|
|
if (computeQueueInfo.familyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
computeQueueInfo.familyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
if (Gfx::useAsyncCompute && numQueueFamilies > 1)
|
|
|
|
|
{
|
|
|
|
|
if (asyncComputeInfo.familyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
if (familyIndex == (uint32)graphicsQueueInfo.familyIndex)
|
|
|
|
|
{
|
|
|
|
|
if (currProps.queueCount > 1)
|
|
|
|
|
{
|
|
|
|
|
asyncComputeInfo.familyIndex = familyIndex;
|
|
|
|
|
numQueuesForFamily++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
asyncComputeInfo.familyIndex = familyIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-13 14:56:13 +01:00
|
|
|
if (currProps.queueFlags & VK_QUEUE_TRANSFER_BIT)
|
2021-11-14 20:36:53 +01:00
|
|
|
{
|
|
|
|
|
if (transferQueueInfo.familyIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
transferQueueInfo.familyIndex = familyIndex;
|
|
|
|
|
numQueuesForFamily++;
|
|
|
|
|
}
|
|
|
|
|
if ((currProps.queueFlags ^ VK_QUEUE_TRANSFER_BIT) == 0)
|
|
|
|
|
{
|
|
|
|
|
dedicatedTransferQueueInfo.familyIndex = familyIndex;
|
|
|
|
|
numQueuesForFamily++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (numQueuesForFamily > 0)
|
|
|
|
|
{
|
2023-11-15 17:42:57 +01:00
|
|
|
VkDeviceQueueCreateInfo info = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.queueFamilyIndex = familyIndex,
|
|
|
|
|
.queueCount = numQueuesForFamily,
|
|
|
|
|
};
|
2021-11-14 20:36:53 +01:00
|
|
|
numPriorities += numQueuesForFamily;
|
|
|
|
|
queueInfos.add(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
for (int32_t queueIndex = 0; queueIndex < (int32_t)currQueue.queueCount; ++queueIndex)
|
|
|
|
|
{
|
|
|
|
|
*currentPriority++ = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
VkPhysicalDeviceMeshShaderFeaturesEXT enabledMeshShaderFeatures = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT,
|
2023-11-22 13:18:54 +01:00
|
|
|
.pNext = nullptr,
|
2023-11-05 10:36:01 +01:00
|
|
|
.taskShader = VK_TRUE,
|
|
|
|
|
.meshShader = VK_TRUE,
|
|
|
|
|
};
|
2023-11-07 16:55:13 +01:00
|
|
|
if (supportMeshShading())
|
2023-11-05 10:36:01 +01:00
|
|
|
{
|
2023-11-22 13:18:54 +01:00
|
|
|
features12.pNext = &enabledMeshShaderFeatures;
|
2023-11-18 11:04:30 +01:00
|
|
|
initializer.deviceExtensions.add(VK_EXT_MESH_SHADER_EXTENSION_NAME);
|
2023-11-05 10:36:01 +01:00
|
|
|
}
|
2023-11-15 17:42:57 +01:00
|
|
|
VkDeviceCreateInfo deviceInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
2023-12-12 11:37:00 +01:00
|
|
|
.pNext = &features11,
|
2023-11-15 17:42:57 +01:00
|
|
|
.queueCreateInfoCount = (uint32)queueInfos.size(),
|
|
|
|
|
.pQueueCreateInfos = queueInfos.data(),
|
|
|
|
|
.enabledExtensionCount = (uint32)initializer.deviceExtensions.size(),
|
|
|
|
|
.ppEnabledExtensionNames = initializer.deviceExtensions.data(),
|
2023-11-22 13:18:54 +01:00
|
|
|
.pEnabledFeatures = &features.features,
|
2023-11-15 17:42:57 +01:00
|
|
|
};
|
2020-03-24 21:05:32 +01:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
VK_CHECK(vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &handle));
|
|
|
|
|
std::cout << "Vulkan handle: " << handle << std::endl;
|
2020-03-24 21:05:32 +01:00
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
cmdDrawMeshTasks = (PFN_vkCmdDrawMeshTasksEXT)vkGetDeviceProcAddr(handle, "vkCmdDrawMeshTasksEXT");
|
|
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
graphicsQueue = new Queue(this, Gfx::QueueType::GRAPHICS, graphicsQueueInfo.familyIndex, 0);
|
|
|
|
|
if (Gfx::useAsyncCompute && asyncComputeInfo.familyIndex != -1)
|
|
|
|
|
{
|
|
|
|
|
if (asyncComputeInfo.familyIndex == graphicsQueueInfo.familyIndex)
|
|
|
|
|
{
|
|
|
|
|
// Same family as graphics, but different queue
|
|
|
|
|
computeQueue = new Queue(this, Gfx::QueueType::COMPUTE, asyncComputeInfo.familyIndex, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Different family
|
|
|
|
|
computeQueue = new Queue(this, Gfx::QueueType::COMPUTE, asyncComputeInfo.familyIndex, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
computeQueue = new Queue(this, Gfx::QueueType::COMPUTE, computeQueueInfo.familyIndex, 0);
|
|
|
|
|
}
|
|
|
|
|
transferQueue = new Queue(this, Gfx::QueueType::TRANSFER, transferQueueInfo.familyIndex, 0);
|
|
|
|
|
if (dedicatedTransferQueueInfo.familyIndex != -1)
|
|
|
|
|
{
|
|
|
|
|
dedicatedTransferQueue = new Queue(this, Gfx::QueueType::DEDICATED_TRANSFER, dedicatedTransferQueueInfo.familyIndex, 0);
|
|
|
|
|
}
|
|
|
|
|
queueMapping.graphicsFamily = graphicsQueue->getFamilyIndex();
|
|
|
|
|
queueMapping.computeFamily = computeQueue->getFamilyIndex();
|
|
|
|
|
queueMapping.transferFamily = transferQueue->getFamilyIndex();
|
2023-11-05 10:36:01 +01:00
|
|
|
queueMapping.dedicatedTransferFamily = dedicatedTransferQueue != nullptr ? dedicatedTransferQueue->getFamilyIndex() : transferQueue->getFamilyIndex();
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|