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

680 lines
27 KiB
C++
Raw Normal View History

2023-11-01 23:12:30 +01:00
#include "Graphics.h"
#include "Allocator.h"
#include "Buffer.h"
2024-06-09 12:20:04 +02:00
#include "Command.h"
#include "Debug.h"
#include "Descriptor.h"
#include "Framebuffer.h"
#include "Graphics/Enums.h"
2024-06-09 10:44:24 +02:00
#include "Graphics/Graphics.h"
#include "Graphics/Initializer.h"
2023-11-01 23:12:30 +01:00
#include "PipelineCache.h"
2024-06-13 15:43:03 +02:00
#include "Query.h"
2024-06-09 10:44:24 +02:00
#include "RayTracing.h"
2024-06-09 12:20:04 +02:00
#include "RenderPass.h"
#include "Shader.h"
#include "Window.h"
2021-04-01 16:40:14 +02:00
#include <GLFW/glfw3.h>
#include <cstring>
#include <vulkan/vulkan_core.h>
2024-06-09 12:20:04 +02:00
2024-01-17 17:57:59 +01:00
#define VMA_IMPLEMENTATION
#include "vk_mem_alloc.h"
2020-03-02 19:07:49 +01:00
2022-04-15 23:45:44 +02:00
using namespace Seele;
using namespace Seele::Vulkan;
2024-01-26 16:26:22 +01:00
thread_local PCommandPool Seele::Vulkan::Graphics::graphicsCommands = nullptr;
thread_local PCommandPool Seele::Vulkan::Graphics::computeCommands = nullptr;
thread_local PCommandPool Seele::Vulkan::Graphics::transferCommands = nullptr;
2022-01-12 14:40:26 +01:00
2024-06-13 15:43:03 +02:00
PFN_vkCmdDrawMeshTasksEXT cmdDrawMeshTasks;
PFN_vkCmdDrawMeshTasksIndirectEXT cmdDrawMeshTasksIndirect;
PFN_vkSetDebugUtilsObjectNameEXT setDebugUtilsObjectName;
PFN_vkCreateAccelerationStructureKHR createAccelerationStructure;
PFN_vkCmdBuildAccelerationStructuresKHR cmdBuildAccelerationStructures;
PFN_vkGetAccelerationStructureBuildSizesKHR getAccelerationStructureBuildSize;
void vkCmdDrawMeshTasksEXT(VkCommandBuffer command, uint32 groupX, uint32 groupY, uint32 groupZ) {
cmdDrawMeshTasks(command, groupX, groupY, groupZ);
}
void vkCmdDrawMeshTasksIndirectEXT(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount,
uint32_t stride) {
cmdDrawMeshTasksIndirect(commandBuffer, buffer, offset, drawCount, stride);
}
VkResult vkSetDebugUtilsObjectNameEXT(VkDevice device, const VkDebugUtilsObjectNameInfoEXT* pNameInfo) {
return setDebugUtilsObjectName(device, pNameInfo);
}
VkResult vkCreateAccelerationStructureKHR(VkDevice device, const VkAccelerationStructureCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkAccelerationStructureKHR* pAccelerationStructure) {
return createAccelerationStructure(device, pCreateInfo, pAllocator, pAccelerationStructure);
}
void vkCmdBuildAccelerationStructuresKHR(VkCommandBuffer commandBuffer, uint32_t infoCount,
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos) {
cmdBuildAccelerationStructures(commandBuffer, infoCount, pInfos, ppBuildRangeInfos);
}
void vkGetAccelerationStructureBuildSizesKHR(VkDevice device, VkAccelerationStructureBuildTypeKHR buildType,
const VkAccelerationStructureBuildGeometryInfoKHR* pBuildInfo,
const uint32_t* pMaxPrimitiveCounts, VkAccelerationStructureBuildSizesInfoKHR* pSizeInfo) {
getAccelerationStructureBuildSize(device, buildType, pBuildInfo, pMaxPrimitiveCounts, pSizeInfo);
}
2024-06-09 12:20:04 +02:00
Graphics::Graphics() : instance(VK_NULL_HANDLE), handle(VK_NULL_HANDLE), physicalDevice(VK_NULL_HANDLE), callback(VK_NULL_HANDLE) {}
2024-06-09 12:20:04 +02:00
Graphics::~Graphics() {
2023-12-02 10:55:00 +01:00
vkDeviceWaitIdle(handle);
pipelineCache = nullptr;
allocatedFramebuffers.clear();
shaderCompiler = nullptr;
2024-01-26 16:26:22 +01:00
pools.clear();
2024-02-01 22:54:20 +01:00
queues.clear();
destructionManager = nullptr;
2024-04-04 08:30:59 +02:00
allocator = nullptr;
vkDestroyDevice(handle, nullptr);
2024-01-26 23:19:18 +01:00
DestroyDebugUtilsMessengerEXT(instance, nullptr, callback);
vkDestroyInstance(instance, nullptr);
}
2024-06-09 12:20:04 +02:00
void Graphics::init(GraphicsInitializer initInfo) {
initInstance(initInfo);
#if ENABLE_VALIDATION
setupDebugCallback();
#endif
pickPhysicalDevice();
createDevice(initInfo);
2024-01-17 17:57:59 +01:00
VmaAllocatorCreateInfo createInfo = {
2024-06-13 22:47:51 +02:00
.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT | VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT,
2024-01-20 19:14:19 +01:00
.physicalDevice = physicalDevice,
2024-01-17 17:57:59 +01:00
.device = handle,
2024-01-20 19:14:19 +01:00
.preferredLargeHeapBlockSize = 0,
2024-01-17 17:57:59 +01:00
.pAllocationCallbacks = nullptr,
.pDeviceMemoryCallbacks = nullptr,
.pHeapSizeLimit = nullptr,
.pVulkanFunctions = nullptr,
2024-01-20 19:14:19 +01:00
.instance = instance,
2024-01-17 17:57:59 +01:00
.vulkanApiVersion = VK_API_VERSION_1_3,
2024-01-20 19:14:19 +01:00
.pTypeExternalMemoryHandleTypes = nullptr,
2024-01-17 17:57:59 +01:00
};
vmaCreateAllocator(&createInfo, &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
}
2024-06-09 12:20:04 +02:00
Gfx::OWindow Graphics::createWindow(const WindowCreateInfo& createInfo) { return new Window(this, createInfo); }
2020-03-02 19:07:49 +01:00
2024-06-09 12:20:04 +02:00
Gfx::OViewport Graphics::createViewport(Gfx::PWindow owner, const ViewportCreateInfo& viewportInfo) {
2024-04-09 16:41:12 +02:00
return new Viewport(owner, viewportInfo);
2020-04-12 15:47:19 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::ORenderPass Graphics::createRenderPass(Gfx::RenderTargetLayout layout, Array<Gfx::SubPassDependency> dependencies,
Gfx::PViewport renderArea) {
return new RenderPass(this, std::move(layout), std::move(dependencies), renderArea);
2020-04-12 15:47:19 +02:00
}
2024-06-09 12:20:04 +02:00
void Graphics::beginRenderPass(Gfx::PRenderPass renderPass) {
PRenderPass rp = renderPass.cast<RenderPass>();
uint32 framebufferHash = rp->getFramebufferHash();
PFramebuffer framebuffer;
{
auto found = allocatedFramebuffers.find(framebufferHash);
2024-06-09 12:20:04 +02:00
if (found == allocatedFramebuffers.end()) {
2023-11-01 23:12:30 +01:00
allocatedFramebuffers[framebufferHash] = new Framebuffer(this, rp, rp->getLayout());
framebuffer = allocatedFramebuffers[framebufferHash];
2024-06-09 12:20:04 +02:00
} else {
2023-11-01 23:12:30 +01:00
framebuffer = std::move(found->value);
}
}
getGraphicsCommands()->getCommands()->beginRenderPass(rp, framebuffer);
2020-03-02 19:07:49 +01:00
}
2024-06-13 15:43:03 +02:00
void Graphics::endRenderPass() { getGraphicsCommands()->getCommands()->endRenderPass(); }
2020-04-12 15:47:19 +02:00
2024-06-09 12:20:04 +02:00
void Graphics::waitDeviceIdle() { vkDeviceWaitIdle(handle); }
2024-03-03 10:29:09 +01:00
2024-06-09 12:20:04 +02:00
void Graphics::executeCommands(Array<Gfx::ORenderCommand> commands) {
2024-04-12 09:27:30 +02:00
getGraphicsCommands()->getCommands()->executeCommands(std::move(commands));
2020-05-05 01:51:13 +02:00
}
2024-06-09 12:20:04 +02:00
void Graphics::executeCommands(Array<Gfx::OComputeCommand> commands) {
2024-04-12 09:27:30 +02:00
getComputeCommands()->getCommands()->executeCommands(std::move(commands));
2021-05-10 23:57:55 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OTexture2D Graphics::createTexture2D(const TextureCreateInfo& createInfo) { return new Texture2D(this, createInfo); }
2020-04-12 15:47:19 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OTexture3D Graphics::createTexture3D(const TextureCreateInfo& createInfo) { return new Texture3D(this, createInfo); }
2023-01-29 18:58:59 +01:00
2024-06-09 12:20:04 +02:00
Gfx::OTextureCube Graphics::createTextureCube(const TextureCreateInfo& createInfo) { return new TextureCube(this, createInfo); }
2023-01-29 18:58:59 +01:00
2024-06-09 12:20:04 +02:00
Gfx::OUniformBuffer Graphics::createUniformBuffer(const UniformBufferCreateInfo& bulkData) { return new UniformBuffer(this, bulkData); }
2020-04-12 15:47:19 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OShaderBuffer Graphics::createShaderBuffer(const ShaderBufferCreateInfo& bulkData) { return new ShaderBuffer(this, bulkData); }
2024-06-13 22:47:51 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OVertexBuffer Graphics::createVertexBuffer(const VertexBufferCreateInfo& bulkData) { return new VertexBuffer(this, bulkData); }
2020-04-12 15:47:19 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OIndexBuffer Graphics::createIndexBuffer(const IndexBufferCreateInfo& bulkData) { return new IndexBuffer(this, bulkData); }
2024-06-13 22:47:51 +02:00
2024-06-09 12:20:04 +02:00
Gfx::ORenderCommand Graphics::createRenderCommand(const std::string& name) { return getGraphicsCommands()->createRenderCommand(name); }
2021-05-10 23:57:55 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OComputeCommand Graphics::createComputeCommand(const std::string& name) { return getComputeCommands()->createComputeCommand(name); }
2020-10-03 11:00:10 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OVertexShader Graphics::createVertexShader(const ShaderCreateInfo& createInfo) {
2023-11-01 23:12:30 +01:00
OVertexShader shader = new VertexShader(this);
shader->create(createInfo);
2023-11-12 16:11:27 +01:00
return shader;
2020-06-02 11:46:18 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OFragmentShader Graphics::createFragmentShader(const ShaderCreateInfo& createInfo) {
2023-11-05 10:36:01 +01:00
OFragmentShader shader = new FragmentShader(this);
shader->create(createInfo);
2023-11-12 16:11:27 +01:00
return shader;
2020-06-02 11:46:18 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OComputeShader Graphics::createComputeShader(const ShaderCreateInfo& createInfo) {
2023-11-05 10:36:01 +01:00
OComputeShader shader = new ComputeShader(this);
shader->create(createInfo);
2023-11-12 16:11:27 +01:00
return shader;
2020-06-02 11:46:18 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OTaskShader Graphics::createTaskShader(const ShaderCreateInfo& createInfo) {
2023-11-05 10:36:01 +01:00
OTaskShader shader = new TaskShader(this);
shader->create(createInfo);
2023-11-12 16:11:27 +01:00
return shader;
2020-06-02 11:46:18 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OMeshShader Graphics::createMeshShader(const ShaderCreateInfo& createInfo) {
2023-11-05 10:36:01 +01:00
OMeshShader shader = new MeshShader(this);
shader->create(createInfo);
2023-11-12 16:11:27 +01:00
return shader;
2021-05-10 23:57:55 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo createInfo) {
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
2024-06-09 12:20:04 +02:00
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::MeshPipelineCreateInfo createInfo) {
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
2024-06-09 12:20:04 +02:00
Gfx::PComputePipeline Graphics::createComputePipeline(Gfx::ComputePipelineCreateInfo createInfo) {
2023-11-09 22:15:51 +01:00
return pipelineCache->createPipeline(std::move(createInfo));
2021-05-06 17:02:10 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OSampler Graphics::createSampler(const SamplerCreateInfo& createInfo) {
2023-11-15 17:42:57 +01:00
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,
};
return new Sampler(this, vkInfo);
2020-10-03 11:00:10 +02:00
}
2023-11-12 16:11:27 +01:00
2024-06-09 12:20:04 +02:00
Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name) { return new DescriptorLayout(this, name); }
2023-11-12 16:11:27 +01:00
2024-06-09 12:20:04 +02:00
Gfx::OPipelineLayout Graphics::createPipelineLayout(const std::string& name, Gfx::PPipelineLayout baseLayout) {
2024-04-23 08:11:44 +02:00
return new PipelineLayout(this, name, baseLayout);
2020-06-02 11:46:18 +02:00
}
2020-04-12 15:47:19 +02:00
2024-06-09 12:20:04 +02:00
Gfx::OVertexInput Graphics::createVertexInput(VertexInputStateCreateInfo createInfo) { return new VertexInput(createInfo); }
2024-06-11 16:55:20 +02:00
Gfx::OOcclusionQuery Graphics::createOcclusionQuery() { return new OcclusionQuery(this); }
2024-06-15 21:47:20 +02:00
Gfx::OPipelineStatisticsQuery Graphics::createPipelineStatisticsQuery() { return new PipelineStatisticsQuery(this); }
2024-06-09 12:20:04 +02:00
void Graphics::resolveTexture(Gfx::PTexture source, Gfx::PTexture destination) {
2023-12-10 22:27:59 +01:00
PTextureBase sourceTex = source.cast<TextureBase>();
PTextureBase destinationTex = destination.cast<TextureBase>();
VkImageResolve resolve = {
2024-06-09 12:20:04 +02:00
.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(),
},
2023-12-10 22:27:59 +01:00
};
2024-06-09 12:20:04 +02:00
vkCmdResolveImage(getGraphicsCommands()->getCommands()->getHandle(), sourceTex->getImage(), cast(sourceTex->getLayout()),
destinationTex->getImage(), cast(destinationTex->getLayout()), 1, &resolve);
2023-12-10 22:27:59 +01:00
}
2024-06-11 14:15:29 +02:00
void Graphics::copyTexture(Gfx::PTexture source, Gfx::PTexture destination) {
PTextureBase src = source.cast<TextureBase>();
PTextureBase dst = destination.cast<TextureBase>();
2024-06-13 22:47:51 +02:00
VkImageBlit blit = {
.srcSubresource =
{
.aspectMask = src->getAspect(),
.mipLevel = 0,
.baseArrayLayer = 0,
.layerCount = 1,
},
.srcOffsets =
{
2024-06-15 21:47:20 +02:00
{0, 0, 0},
{(int32)src->getWidth(), (int32)src->getHeight(), (int32)src->getDepth()},
2024-06-13 22:47:51 +02:00
},
.dstSubresource =
{
.aspectMask = dst->getAspect(),
.mipLevel = 0,
.baseArrayLayer = 0,
.layerCount = 1,
},
.dstOffsets =
{
2024-06-15 21:47:20 +02:00
{0, 0, 0},
{(int32)dst->getWidth(), (int32)dst->getHeight(), (int32)dst->getDepth()},
2024-06-13 22:47:51 +02:00
},
};
2024-06-11 14:15:29 +02:00
PCommand command = getGraphicsCommands()->getCommands();
vkCmdBlitImage(command->getHandle(), src->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst->getImage(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
2024-06-13 15:43:03 +02:00
src->getAspect() & VK_IMAGE_ASPECT_DEPTH_BIT ? VK_FILTER_NEAREST : VK_FILTER_LINEAR);
2024-06-11 14:15:29 +02:00
}
2024-06-09 12:20:04 +02:00
Gfx::OBottomLevelAS Graphics::createBottomLevelAccelerationStructure(const Gfx::BottomLevelASCreateInfo& createInfo) {
2024-06-09 10:44:24 +02:00
return new BottomLevelAS(this, createInfo);
2023-11-05 10:36:01 +01:00
}
2023-11-15 00:06:00 +01:00
2024-06-09 12:20:04 +02:00
Gfx::OTopLevelAS Graphics::createTopLevelAccelerationStructure(const Gfx::TopLevelASCreateInfo& createInfo) {
2024-06-09 10:44:24 +02:00
return new TopLevelAS(this, createInfo);
}
2024-06-18 09:48:00 +02:00
Gfx::ORayGenShader Graphics::createRayGenShader(const ShaderCreateInfo& createInfo) {
ORayGenShader shader = new RayGenShader(this);
shader->create(createInfo);
return shader;
}
Gfx::OAnyHitShader Graphics::createAnyHitShader(const ShaderCreateInfo& createInfo) {
OAnyHitShader shader = new AnyHitShader(this);
shader->create(createInfo);
return shader;
}
Gfx::OClosestHitShader Graphics::createClosestHitShader(const ShaderCreateInfo& createInfo) {
OClosestHitShader shader = new ClosestHitShader(this);
shader->create(createInfo);
return shader;
}
Gfx::OMissShader Graphics::createMissShader(const ShaderCreateInfo& createInfo) {
OMissShader shader = new MissShader(this);
shader->create(createInfo);
return shader;
}
Gfx::OIntersectionShader Graphics::createIntersectionShader(const ShaderCreateInfo& createInfo) {
OIntersectionShader shader = new IntersectionShader(this);
shader->create(createInfo);
return shader;
}
Gfx::OCallableShader Graphics::createCallableShader(const ShaderCreateInfo& createInfo) {
OCallableShader shader = new CallableShader(this);
shader->create(createInfo);
return shader;
}
2024-06-09 12:20:04 +02:00
PCommandPool Graphics::getQueueCommands(Gfx::QueueType queueType) {
switch (queueType) {
case Gfx::QueueType::GRAPHICS:
return getGraphicsCommands();
case Gfx::QueueType::COMPUTE:
return getComputeCommands();
case Gfx::QueueType::TRANSFER:
return getTransferCommands();
default:
throw new std::logic_error("invalid queue type");
}
2020-06-02 11:46:18 +02:00
}
2024-06-09 12:20:04 +02:00
PCommandPool Graphics::getGraphicsCommands() {
if (graphicsCommands == nullptr) {
2024-01-26 16:26:22 +01:00
std::unique_lock l(poolLock);
2024-02-01 08:42:24 +01:00
graphicsCommands = pools.add(new CommandPool(this, queues[graphicsQueue]));
}
2022-01-12 14:40:26 +01:00
return graphicsCommands;
2020-04-01 02:17:49 +02:00
}
2024-06-09 12:20:04 +02:00
PCommandPool Graphics::getComputeCommands() {
if (computeCommands == nullptr) {
if (graphicsQueue == computeQueue) {
2024-02-01 08:42:24 +01:00
computeCommands = getGraphicsCommands();
2024-06-09 12:20:04 +02:00
} else {
2024-02-01 08:42:24 +01:00
std::unique_lock l(poolLock);
computeCommands = pools.add(new CommandPool(this, queues[computeQueue]));
}
}
2022-01-12 14:40:26 +01:00
return computeCommands;
2020-04-01 02:17:49 +02:00
}
2024-06-09 12:20:04 +02:00
PCommandPool Graphics::getTransferCommands() {
if (transferCommands == nullptr) {
if (graphicsQueue == transferQueue) {
2024-02-01 08:42:24 +01:00
transferCommands = getGraphicsCommands();
2024-06-09 12:20:04 +02:00
} else {
2024-02-01 08:42:24 +01:00
std::unique_lock l(poolLock);
transferCommands = pools.add(new CommandPool(this, queues[transferQueue]));
}
}
2022-01-12 14:40:26 +01:00
return transferCommands;
2020-04-01 02:17:49 +02:00
}
2024-01-17 17:57:59 +01:00
2024-06-14 22:12:26 +02:00
VmaAllocator Graphics::getAllocator() const { return allocator; }
2020-04-01 02:17:49 +02:00
2024-06-09 12:20:04 +02:00
PDestructionManager Graphics::getDestructionManager() { return destructionManager; }
2023-11-12 16:11:27 +01:00
2024-06-09 12:20:04 +02:00
Array<const char*> Graphics::getRequiredExtensions() {
Array<const char*> extensions;
2020-03-13 12:44:33 +01:00
unsigned int glfwExtensionCount = 0;
2024-06-09 12:20:04 +02:00
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
2020-03-13 12:44:33 +01:00
2024-06-09 12:20:04 +02:00
for (unsigned int i = 0; i < glfwExtensionCount; i++) {
extensions.add(glfwExtensions[i]);
}
2020-10-14 01:49:43 +02:00
#if ENABLE_VALIDATION
2024-01-26 23:19:18 +01:00
extensions.add(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
2020-03-13 12:44:33 +01:00
#endif // ENABLE_VALIDATION
return extensions;
2020-03-13 12:44:33 +01:00
}
2024-06-14 22:12:26 +02:00
2024-06-09 12:20:04 +02:00
void Graphics::initInstance(GraphicsInitializer initInfo) {
glfwInit();
2024-01-19 22:57:45 +01:00
assert(glfwVulkanSupported());
2024-01-02 16:48:03 +01:00
VkApplicationInfo appInfo = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
2024-06-09 10:44:24 +02:00
.pNext = nullptr,
2024-01-02 16:48:03 +01:00
.pApplicationName = initInfo.applicationName,
.applicationVersion = VK_MAKE_VERSION(0, 0, 1),
.pEngineName = initInfo.engineName,
.engineVersion = VK_MAKE_VERSION(0, 0, 1),
2024-05-22 10:30:45 +02:00
.apiVersion = VK_API_VERSION_1_3,
2024-01-02 16:48:03 +01:00
};
2024-06-09 12:20:04 +02:00
2024-01-02 16:48:03 +01:00
Array<const char*> extensions = getRequiredExtensions();
2024-06-09 12:20:04 +02:00
for (uint32 i = 0; i < initInfo.instanceExtensions.size(); ++i) {
extensions.add(initInfo.instanceExtensions[i]);
}
2024-01-17 17:57:59 +01:00
#ifdef __APPLE__
extensions.add("VK_KHR_portability_enumeration");
#endif
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
2024-01-02 16:48:03 +01:00
VkInstanceCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
2024-06-09 10:44:24 +02:00
.pNext = nullptr,
2024-01-17 17:57:59 +01:00
#if __APPLE__
.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
#endif
2024-01-02 16:48:03 +01:00
.pApplicationInfo = &appInfo,
.enabledLayerCount = (uint32)initInfo.layers.size(),
.ppEnabledLayerNames = initInfo.layers.data(),
.enabledExtensionCount = (uint32)extensions.size(),
.ppEnabledExtensionNames = extensions.data(),
};
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
}
2024-06-14 22:12:26 +02:00
2024-06-09 12:20:04 +02:00
void Graphics::setupDebugCallback() {
2024-01-26 23:19:18 +01:00
VkDebugUtilsMessengerCreateInfoEXT createInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
2023-11-15 17:42:57 +01:00
.pNext = nullptr,
2024-01-26 23:19:18 +01:00
.flags = 0,
2024-06-09 12:20:04 +02:00
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
2024-01-26 23:19:18 +01:00
.pfnUserCallback = &debugCallback,
2023-11-15 17:42:57 +01:00
.pUserData = nullptr,
};
2024-01-26 23:19:18 +01:00
VK_CHECK(CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &callback));
}
2024-06-09 12:20:04 +02:00
void Graphics::pickPhysicalDevice() {
uint32 physicalDeviceCount;
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr);
Array<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
VkPhysicalDevice bestDevice = VK_NULL_HANDLE;
uint32 deviceRating = 0;
2024-06-13 22:47:51 +02:00
props = VkPhysicalDeviceProperties2{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
.pNext = &accelerationProperties,
};
accelerationProperties = VkPhysicalDeviceAccelerationStructurePropertiesKHR{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR,
2024-06-14 22:12:26 +02:00
.pNext = &rayTracingProperties,
};
rayTracingProperties = VkPhysicalDeviceRayTracingPipelinePropertiesKHR{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR,
2024-06-13 22:47:51 +02:00
.pNext = nullptr,
};
2024-06-09 12:20:04 +02:00
for (auto dev : physicalDevices) {
uint32 currentRating = 0;
2024-06-13 22:47:51 +02:00
vkGetPhysicalDeviceProperties2(dev, &props);
if (props.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
std::cout << "found dedicated gpu " << props.properties.deviceName << std::endl;
currentRating += 100;
2024-06-13 22:47:51 +02:00
} else if (props.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) {
std::cout << "found integrated gpu " << props.properties.deviceName << std::endl;
currentRating += 10;
}
2024-06-09 12:20:04 +02:00
if (currentRating > deviceRating) {
deviceRating = currentRating;
bestDevice = dev;
2024-06-13 22:47:51 +02:00
std::cout << "bestDevice: " << props.properties.deviceName << std::endl;
}
}
physicalDevice = bestDevice;
2024-01-17 17:57:59 +01:00
2024-06-13 22:47:51 +02:00
vkGetPhysicalDeviceProperties2(physicalDevice, &props);
2024-06-14 22:12:26 +02:00
rayTracingFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR,
.pNext = nullptr,
.rayTracingPipeline = true,
.rayTraversalPrimitiveCulling = true,
};
2024-06-13 22:47:51 +02:00
accelerationFeatures = {
2024-06-13 15:43:03 +02:00
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
2024-06-14 22:12:26 +02:00
.pNext = &rayTracingFeatures,
2024-06-13 15:43:03 +02:00
.accelerationStructure = true,
};
meshShaderFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT,
2024-06-13 22:47:51 +02:00
.pNext = &accelerationFeatures,
2024-06-13 15:43:03 +02:00
.taskShader = true,
.meshShader = true,
.meshShaderQueries = true,
};
features12 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
.pNext = &meshShaderFeatures,
2024-06-13 22:47:51 +02:00
.storageBuffer8BitAccess = true,
2024-06-13 15:43:03 +02:00
.bufferDeviceAddress = true,
};
2024-06-13 22:47:51 +02:00
features = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
.pNext = &features12,
.features =
{
.geometryShader = true,
.fillModeNonSolid = true,
.wideLines = true,
.occlusionQueryPrecise = true,
2024-06-15 21:47:20 +02:00
.pipelineStatisticsQuery = true,
2024-06-13 22:47:51 +02:00
.fragmentStoresAndAtomics = true,
.shaderInt64 = true,
.inheritedQueries = true,
},
};
2024-06-09 12:20:04 +02:00
if (Gfx::useMeshShading) {
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());
2024-06-09 12:20:04 +02:00
for (size_t i = 0; i < count; ++i) {
if (std::strcmp(VK_EXT_MESH_SHADER_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
2024-05-22 19:29:23 +02:00
meshShadingEnabled = true;
2023-11-07 16:55:13 +01:00
break;
}
}
}
}
2024-06-09 12:20:04 +02:00
void Graphics::createDevice(GraphicsInitializer initializer) {
uint32_t numQueueFamilies = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &numQueueFamilies, nullptr);
Array<VkQueueFamilyProperties> queueProperties(numQueueFamilies);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &numQueueFamilies, queueProperties.data());
Array<VkDeviceQueueCreateInfo> queueInfos;
2024-06-09 12:20:04 +02:00
struct QueueCreateInfo {
int32 familyIndex = -1;
int32 queueIndex = -1;
};
QueueCreateInfo graphicsQueueInfo;
QueueCreateInfo transferQueueInfo;
QueueCreateInfo computeQueueInfo;
uint32 numPriorities = 0;
2024-06-09 12:20:04 +02:00
auto checkFamilyProperty = [](VkQueueFamilyProperties currProps, uint32 checkBit) {
return (currProps.queueFlags & checkBit) == checkBit;
};
2024-01-17 17:57:59 +01:00
auto updateQueueInfo = [&queueProperties](uint32 familyIndex, QueueCreateInfo& info, uint32& numQueues) {
2024-06-09 12:20:04 +02:00
if (info.familyIndex == -1) {
if (queueProperties[familyIndex].queueCount == numQueues) {
2024-02-01 08:42:24 +01:00
return;
}
2024-01-17 17:57:59 +01:00
info.familyIndex = familyIndex;
2024-02-01 08:42:24 +01:00
info.queueIndex = numQueues++;
2024-01-17 17:57:59 +01:00
}
};
2024-06-09 12:20:04 +02:00
for (uint32 familyIndex = 0; familyIndex < queueProperties.size(); ++familyIndex) {
uint32 numQueuesForFamily = 0;
VkQueueFamilyProperties currProps = queueProperties[familyIndex];
2021-10-16 13:21:53 +02:00
2024-06-09 12:20:04 +02:00
if (checkFamilyProperty(currProps, VK_QUEUE_GRAPHICS_BIT)) {
2024-02-01 08:42:24 +01:00
updateQueueInfo(familyIndex, graphicsQueueInfo, numQueuesForFamily);
}
2024-06-09 12:20:04 +02:00
if (Gfx::useAsyncCompute) {
if (checkFamilyProperty(currProps, VK_QUEUE_COMPUTE_BIT)) {
2024-02-01 08:42:24 +01:00
updateQueueInfo(familyIndex, computeQueueInfo, numQueuesForFamily);
}
}
2024-06-09 12:20:04 +02:00
if ((currProps.queueFlags ^ VK_QUEUE_TRANSFER_BIT) == 0) {
2024-02-01 08:42:24 +01:00
updateQueueInfo(familyIndex, transferQueueInfo, numQueuesForFamily);
}
2024-06-09 12:20:04 +02:00
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,
2024-06-09 10:44:24 +02:00
.pQueuePriorities = nullptr,
2023-11-15 17:42:57 +01:00
};
numPriorities += numQueuesForFamily;
queueInfos.add(info);
}
}
Array<float> queuePriorities;
queuePriorities.resize(numPriorities);
2024-06-09 12:20:04 +02:00
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
2024-06-09 12:20:04 +02:00
for (int32_t queueIndex = 0; queueIndex < (int32_t)currQueue.queueCount; ++queueIndex) {
*currentPriority++ = 1.0f;
}
}
2024-06-13 15:43:03 +02:00
2024-06-09 12:20:04 +02:00
if (supportMeshShading()) {
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
}
2024-01-17 17:57:59 +01:00
#ifdef __APPLE__
initializer.deviceExtensions.add("VK_KHR_portability_subset");
#endif
initializer.deviceExtensions.add(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
initializer.deviceExtensions.add(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
2024-06-14 22:12:26 +02:00
initializer.deviceExtensions.add(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME);
2024-06-13 22:47:51 +02:00
initializer.deviceExtensions.add(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
2023-11-15 17:42:57 +01:00
VkDeviceCreateInfo deviceInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2024-06-13 15:43:03 +02:00
.pNext = &features,
2023-11-15 17:42:57 +01:00
.queueCreateInfoCount = (uint32)queueInfos.size(),
.pQueueCreateInfos = queueInfos.data(),
.enabledExtensionCount = (uint32)initializer.deviceExtensions.size(),
.ppEnabledExtensionNames = initializer.deviceExtensions.data(),
2024-06-13 15:43:03 +02:00
.pEnabledFeatures = nullptr,
2023-11-15 17:42:57 +01:00
};
2020-03-24 21:05:32 +01:00
VK_CHECK(vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &handle));
2024-06-09 12:20:04 +02:00
// std::cout << "Vulkan handle: " << handle << std::endl;
2020-03-24 21:05:32 +01:00
2024-02-01 08:42:24 +01:00
graphicsQueue = 0;
computeQueue = 0;
transferQueue = 0;
queues.add(new Queue(this, graphicsQueueInfo.familyIndex, graphicsQueueInfo.queueIndex));
2024-06-09 12:20:04 +02:00
if (computeQueueInfo.familyIndex != -1) {
2024-02-01 08:42:24 +01:00
computeQueue = queues.size();
queues.add(new Queue(this, computeQueueInfo.familyIndex, computeQueueInfo.queueIndex));
}
2024-06-09 12:20:04 +02:00
if (transferQueueInfo.familyIndex != -1) {
2024-02-01 08:42:24 +01:00
transferQueue = queues.size();
queues.add(new Queue(this, transferQueueInfo.familyIndex, transferQueueInfo.queueIndex));
}
2024-06-09 12:20:04 +02:00
2024-02-01 08:42:24 +01:00
queueMapping.graphicsFamily = queues[graphicsQueue]->getFamilyIndex();
queueMapping.computeFamily = queues[computeQueue]->getFamilyIndex();
queueMapping.transferFamily = queues[transferQueue]->getFamilyIndex();
2024-06-13 15:43:03 +02:00
cmdDrawMeshTasks = (PFN_vkCmdDrawMeshTasksEXT)vkGetDeviceProcAddr(handle, "vkCmdDrawMeshTasksEXT");
cmdDrawMeshTasksIndirect = (PFN_vkCmdDrawMeshTasksIndirectEXT)vkGetDeviceProcAddr(handle, "vkCmdDrawMeshTasksIndirectEXT");
2024-02-20 21:07:17 +01:00
setDebugUtilsObjectName = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(instance, "vkSetDebugUtilsObjectNameEXT");
2024-06-13 15:43:03 +02:00
createAccelerationStructure = (PFN_vkCreateAccelerationStructureKHR)vkGetDeviceProcAddr(handle, "vkCreateAccelerationStructureKHR");
cmdBuildAccelerationStructures =
(PFN_vkCmdBuildAccelerationStructuresKHR)vkGetDeviceProcAddr(handle, "vkCmdBuildAccelerationStructuresKHR");
getAccelerationStructureBuildSize =
(PFN_vkGetAccelerationStructureBuildSizesKHR)vkGetDeviceProcAddr(handle, "vkGetAccelerationStructureBuildSizesKHR");
}