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"
|
2023-11-06 22:24:40 +01:00
|
|
|
#include "Graphics/Enums.h"
|
2024-06-09 10:44:24 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
|
|
|
|
#include "Graphics/Initializer.h"
|
2024-07-12 13:33:52 +02:00
|
|
|
#include "Graphics/StaticMeshVertexData.h"
|
|
|
|
|
#include "Graphics/slang-compile.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>
|
2023-11-06 22:24:40 +01:00
|
|
|
#include <cstring>
|
2025-05-16 13:04:43 +02:00
|
|
|
#include <numeric>
|
2023-11-06 22:24:40 +01:00
|
|
|
#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;
|
2020-03-20 01:27:40 +01:00
|
|
|
using namespace Seele::Vulkan;
|
2020-03-15 15:58:01 +01:00
|
|
|
|
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;
|
2024-10-18 19:01:04 +02:00
|
|
|
PFN_vkQueueBeginDebugUtilsLabelEXT queueBeginDebugUtilsLabelEXT;
|
|
|
|
|
PFN_vkQueueEndDebugUtilsLabelEXT queueEndDebugUtilsLabelEXT;
|
2024-10-30 10:28:10 +01:00
|
|
|
PFN_vkCmdBeginDebugUtilsLabelEXT cmdBeginDebugUtilsLabelEXT;
|
|
|
|
|
PFN_vkCmdEndDebugUtilsLabelEXT cmdEndDebugUtilsLabelEXT;
|
2024-06-13 15:43:03 +02:00
|
|
|
PFN_vkCreateAccelerationStructureKHR createAccelerationStructure;
|
|
|
|
|
PFN_vkCmdBuildAccelerationStructuresKHR cmdBuildAccelerationStructures;
|
|
|
|
|
PFN_vkGetAccelerationStructureBuildSizesKHR getAccelerationStructureBuildSize;
|
2024-06-18 23:33:03 +02:00
|
|
|
PFN_vkCreateRayTracingPipelinesKHR createRayTracingPipelines;
|
2024-07-10 21:07:10 +02:00
|
|
|
PFN_vkGetRayTracingShaderGroupHandlesKHR getRayTracingShaderGroupHandles;
|
|
|
|
|
PFN_vkCmdTraceRaysKHR cmdTraceRays;
|
2024-07-12 22:05:52 +02:00
|
|
|
PFN_vkGetAccelerationStructureDeviceAddressKHR getAccelerationStructureDeviceAddress;
|
2024-06-13 15:43:03 +02:00
|
|
|
|
|
|
|
|
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) {
|
2024-07-05 12:02:46 +02:00
|
|
|
if (setDebugUtilsObjectName != nullptr) {
|
|
|
|
|
return setDebugUtilsObjectName(device, pNameInfo);
|
|
|
|
|
}
|
|
|
|
|
return VK_SUCCESS;
|
2024-06-13 15:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-18 19:01:04 +02:00
|
|
|
void vkQueueBeginDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT* pLabelInfo) {
|
|
|
|
|
if (queueBeginDebugUtilsLabelEXT != nullptr) {
|
|
|
|
|
queueBeginDebugUtilsLabelEXT(queue, pLabelInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vkQueueEndDebugUtilsLabelEXT(VkQueue queue) {
|
|
|
|
|
if (queueEndDebugUtilsLabelEXT != nullptr) {
|
|
|
|
|
queueEndDebugUtilsLabelEXT(queue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 10:28:10 +01:00
|
|
|
void vkCmdBeginDebugUtilsLabelEXT(VkCommandBuffer commandBuffer, const VkDebugUtilsLabelEXT* pLabelInfo) {
|
|
|
|
|
if (cmdBeginDebugUtilsLabelEXT != nullptr) {
|
|
|
|
|
cmdBeginDebugUtilsLabelEXT(commandBuffer, pLabelInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vkCmdEndDebugUtilsLabelEXT(VkCommandBuffer commandBuffer) {
|
|
|
|
|
if (cmdEndDebugUtilsLabelEXT != nullptr) {
|
|
|
|
|
cmdEndDebugUtilsLabelEXT(commandBuffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 15:43:03 +02:00
|
|
|
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-18 23:33:03 +02:00
|
|
|
VkResult vkCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, VkPipelineCache pipelineCache,
|
2024-07-10 21:07:10 +02:00
|
|
|
uint32_t createInfoCount, const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
|
|
|
|
|
const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines) {
|
2024-06-18 23:33:03 +02:00
|
|
|
return createRayTracingPipelines(device, deferredOperation, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 21:07:10 +02:00
|
|
|
VkResult vkGetRayTracingShaderGroupHandlesKHR(VkDevice device, VkPipeline pipeline, uint32_t firstGroup, uint32_t groupCount,
|
|
|
|
|
size_t dataSize, void* pData) {
|
|
|
|
|
return getRayTracingShaderGroupHandles(device, pipeline, firstGroup, groupCount, dataSize, pData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vkCmdTraceRaysKHR(VkCommandBuffer commandBuffer, const VkStridedDeviceAddressRegionKHR* pRaygenShaderBindingTable,
|
|
|
|
|
const VkStridedDeviceAddressRegionKHR* pMissShaderBindingTable,
|
|
|
|
|
const VkStridedDeviceAddressRegionKHR* pHitShaderBindingTable,
|
|
|
|
|
const VkStridedDeviceAddressRegionKHR* pCallableShaderBindingTable, uint32_t width, uint32_t height,
|
|
|
|
|
uint32_t depth) {
|
|
|
|
|
cmdTraceRays(commandBuffer, pRaygenShaderBindingTable, pMissShaderBindingTable, pHitShaderBindingTable, pCallableShaderBindingTable,
|
|
|
|
|
width, height, depth);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 22:05:52 +02:00
|
|
|
VkDeviceAddress vkGetAccelerationStructureDeviceAddressKHR(VkDevice device, const VkAccelerationStructureDeviceAddressInfoKHR* pInfo) {
|
|
|
|
|
return getAccelerationStructureDeviceAddress(device, pInfo);
|
|
|
|
|
}
|
|
|
|
|
|
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) {}
|
2020-03-15 15:58:01 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
Graphics::~Graphics() {
|
2023-12-02 10:55:00 +01:00
|
|
|
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;
|
2021-11-14 20:36:53 +01:00
|
|
|
vkDestroyDevice(handle, nullptr);
|
2024-01-26 23:19:18 +01:00
|
|
|
DestroyDebugUtilsMessengerEXT(instance, nullptr, callback);
|
2021-11-14 20:36:53 +01:00
|
|
|
vkDestroyInstance(instance, nullptr);
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void Graphics::init(GraphicsInitializer initInfo) {
|
2021-11-14 20:36:53 +01:00
|
|
|
initInstance(initInfo);
|
2025-03-07 21:48:27 +01:00
|
|
|
#ifdef ENABLE_VALIDATION
|
|
|
|
|
setupDebugCallback();
|
|
|
|
|
#endif
|
2021-11-14 20:36:53 +01:00
|
|
|
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);
|
2021-11-14 20:36:53 +01:00
|
|
|
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-01-31 09:49:53 +01:00
|
|
|
|
2025-05-18 08:49:22 +02:00
|
|
|
Gfx::ORenderPass Graphics::createRenderPass(Gfx::RenderTargetLayout layout, Array<Gfx::SubPassDependency> dependencies, URect,
|
2025-05-16 13:04:43 +02:00
|
|
|
std::string name, Array<uint32> viewMasks, Array<uint32> correlationMasks) {
|
|
|
|
|
// todo: re-introduce render area to renderpass
|
2025-04-11 21:02:52 +02:00
|
|
|
return new RenderPass(this, std::move(layout), std::move(dependencies), name, std::move(viewMasks), std::move(correlationMasks));
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
2024-06-09 12:20:04 +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);
|
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);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
getGraphicsCommands()->getCommands()->beginRenderPass(rp, framebuffer);
|
2020-03-02 19:07:49 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-10 21:07:10 +02:00
|
|
|
void Graphics::endRenderPass() { getGraphicsCommands()->getCommands()->endRenderPass(); }
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2024-08-07 21:19:33 +02:00
|
|
|
void Graphics::waitDeviceIdle() {
|
|
|
|
|
getGraphicsCommands()->submitCommands();
|
2024-10-18 19:01:04 +02:00
|
|
|
vkDeviceWaitIdle(handle);
|
2024-07-18 11:45:56 +02:00
|
|
|
getGraphicsCommands()->refreshCommands();
|
|
|
|
|
}
|
2024-03-03 10:29:09 +01:00
|
|
|
|
2025-03-07 21:48:27 +01:00
|
|
|
void Graphics::executeCommands(Gfx::ORenderCommand commands) {
|
|
|
|
|
Array<Gfx::ORenderCommand> commandArray;
|
|
|
|
|
commandArray.add(std::move(commands));
|
|
|
|
|
getGraphicsCommands()->getCommands()->executeCommands(std::move(commandArray));
|
|
|
|
|
}
|
|
|
|
|
|
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-08-13 22:44:04 +02:00
|
|
|
void Graphics::executeCommands(Gfx::OComputeCommand commands) {
|
|
|
|
|
Array<Gfx::OComputeCommand> commandArray;
|
|
|
|
|
commandArray.add(std::move(commands));
|
|
|
|
|
getComputeCommands()->getCommands()->executeCommands(std::move(commandArray));
|
|
|
|
|
}
|
|
|
|
|
|
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-07-12 13:33:52 +02:00
|
|
|
void Graphics::beginShaderCompilation(const ShaderCompilationInfo& createInfo) {
|
|
|
|
|
beginCompilation(createInfo, SLANG_SPIRV, createInfo.rootSignature);
|
|
|
|
|
}
|
2024-07-10 21:07: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);
|
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
|
|
|
}
|
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);
|
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
|
|
|
}
|
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);
|
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
|
|
|
}
|
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);
|
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
|
|
|
}
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
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-07-10 21:07:10 +02:00
|
|
|
Gfx::PRayTracingPipeline Graphics::createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo createInfo) {
|
|
|
|
|
return pipelineCache->createPipeline(std::move(createInfo));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-05-16 13:04:43 +02:00
|
|
|
Gfx::OSampler Graphics::createSampler(const SamplerCreateInfo& createInfo) { return new Sampler(this, createInfo); }
|
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-01-19 09:49:42 +01:00
|
|
|
|
2024-07-01 12:17:04 +02:00
|
|
|
Gfx::OOcclusionQuery Graphics::createOcclusionQuery(const std::string& name) { return new OcclusionQuery(this, name); }
|
2024-06-11 16:55:20 +02:00
|
|
|
|
2024-07-01 12:17:04 +02:00
|
|
|
Gfx::OPipelineStatisticsQuery Graphics::createPipelineStatisticsQuery(const std::string& name) {
|
|
|
|
|
return new PipelineStatisticsQuery(this, name);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 21:02:52 +02:00
|
|
|
Gfx::OTimestampQuery Graphics::createTimestampQuery(uint64, const std::string& name) { return new TimestampQuery(this, name); }
|
2024-06-15 21:47:20 +02:00
|
|
|
|
2024-10-18 19:01:04 +02:00
|
|
|
void Graphics::beginDebugRegion(const std::string& name) {
|
|
|
|
|
VkDebugUtilsLabelEXT label = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.pLabelName = name.c_str(),
|
|
|
|
|
};
|
2024-10-30 10:28:10 +01:00
|
|
|
vkCmdBeginDebugUtilsLabelEXT(getGraphicsCommands()->getCommands()->getHandle(), &label);
|
2024-10-18 19:01:04 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-30 10:28:10 +01:00
|
|
|
void Graphics::endDebugRegion() { vkCmdEndDebugUtilsLabelEXT(getGraphicsCommands()->getCommands()->getHandle()); }
|
2024-10-18 19:01:04 +02:00
|
|
|
|
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-07-12 13:33:52 +02:00
|
|
|
Gfx::SeImageLayout srcLayout = src->getLayout();
|
|
|
|
|
Gfx::SeImageLayout dstLayout = dst->getLayout();
|
|
|
|
|
src->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
|
|
|
|
|
dst->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, Gfx::SE_ACCESS_MEMORY_WRITE_BIT | Gfx::SE_ACCESS_MEMORY_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
|
|
|
|
|
|
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-07-12 13:33:52 +02:00
|
|
|
|
|
|
|
|
src->changeLayout(srcLayout, Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_MEMORY_READ_BIT | Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
|
|
|
|
|
|
|
|
|
|
dst->changeLayout(dstLayout, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_MEMORY_READ_BIT | Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
|
2024-06-11 14:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-07 20:14:00 +02:00
|
|
|
void Graphics::copyBuffer(Gfx::PShaderBuffer srcBuffer, Gfx::PShaderBuffer dstBuffer) {
|
|
|
|
|
PShaderBuffer src = srcBuffer.cast<ShaderBuffer>();
|
|
|
|
|
PShaderBuffer dst = dstBuffer.cast<ShaderBuffer>();
|
|
|
|
|
|
|
|
|
|
VkBufferCopy region = {
|
|
|
|
|
.srcOffset = 0,
|
|
|
|
|
.dstOffset = 0,
|
|
|
|
|
.size = src->getSize(),
|
|
|
|
|
};
|
|
|
|
|
vkCmdCopyBuffer(getGraphicsCommands()->getCommands()->getHandle(), src->getHandle(), dst->getHandle(), 1, ®ion);
|
|
|
|
|
}
|
|
|
|
|
|
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-07-12 13:33:52 +02:00
|
|
|
void Graphics::buildBottomLevelAccelerationStructures(Array<Gfx::PBottomLevelAS> data) {
|
2025-04-08 22:01:42 +02:00
|
|
|
if (!supportRayTracing() || data.empty())
|
2025-01-08 19:15:12 +01:00
|
|
|
return;
|
2024-07-15 08:32:50 +02:00
|
|
|
Gfx::PShaderBuffer verticesBuffer = StaticMeshVertexData::getInstance()->getPositionBuffer();
|
2024-07-12 13:33:52 +02:00
|
|
|
Gfx::PIndexBuffer indexBuffer = StaticMeshVertexData::getInstance()->getIndexBuffer();
|
2024-10-18 19:01:04 +02:00
|
|
|
|
2024-07-15 08:32:50 +02:00
|
|
|
// Setup vertices and indices for a single triangle
|
2024-10-18 19:01:04 +02:00
|
|
|
|
2024-07-15 08:32:50 +02:00
|
|
|
// Create buffers for the bottom level geometry
|
|
|
|
|
// For the sake of simplicity we won't stage the vertex data to the GPU memory
|
2024-07-12 13:33:52 +02:00
|
|
|
|
2024-07-15 08:32:50 +02:00
|
|
|
// Note that the buffer usage flags for buffers consumed by the bottom level acceleration structure require special flags
|
2024-07-12 13:33:52 +02:00
|
|
|
VkBufferCreateInfo transformBufferInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.size = sizeof(VkTransformMatrixKHR) * data.size(),
|
|
|
|
|
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT |
|
|
|
|
|
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
|
|
|
};
|
|
|
|
|
VmaAllocationCreateInfo transformAllocInfo = {
|
|
|
|
|
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
|
|
|
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Array<VkTransformMatrixKHR> matrices;
|
2025-05-16 13:04:43 +02:00
|
|
|
for (const auto& gfxBlas : data) {
|
2024-07-12 13:33:52 +02:00
|
|
|
const auto blas = gfxBlas.cast<BottomLevelAS>();
|
|
|
|
|
matrices.add(blas->getTransform());
|
|
|
|
|
}
|
|
|
|
|
OBufferAllocation transformBuffer =
|
|
|
|
|
new BufferAllocation(this, "TransformBuffer", transformBufferInfo, transformAllocInfo, Gfx::QueueType::GRAPHICS);
|
|
|
|
|
transformBuffer->updateContents(0, sizeof(VkTransformMatrixKHR) * matrices.size(), matrices.data());
|
2024-07-12 22:05:52 +02:00
|
|
|
transformBuffer->pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_TRANSFER_READ_BIT,
|
|
|
|
|
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR | VK_PIPELINE_STAGE_TRANSFER_BIT);
|
2024-07-15 08:32:50 +02:00
|
|
|
verticesBuffer->pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_READ_BIT,
|
2024-10-18 19:01:04 +02:00
|
|
|
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
|
2024-07-12 22:05:52 +02:00
|
|
|
indexBuffer->pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_READ_BIT,
|
2024-10-18 19:01:04 +02:00
|
|
|
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
|
2024-07-12 13:33:52 +02:00
|
|
|
|
|
|
|
|
Array<VkAccelerationStructureGeometryKHR> geometries(data.size());
|
|
|
|
|
Array<VkAccelerationStructureBuildGeometryInfoKHR> buildGeometries(data.size());
|
|
|
|
|
Array<VkAccelerationStructureBuildSizesInfoKHR> buildSizes(data.size());
|
|
|
|
|
Array<OBufferAllocation> scratchBuffers(data.size());
|
|
|
|
|
Array<VkAccelerationStructureBuildRangeInfoKHR> buildRanges(data.size());
|
|
|
|
|
Array<VkAccelerationStructureBuildRangeInfoKHR*> buildRangePointers(data.size());
|
|
|
|
|
for (uint32 i = 0; i < data.size(); ++i) {
|
|
|
|
|
auto blas = data[i].cast<BottomLevelAS>();
|
|
|
|
|
VkDeviceOrHostAddressConstKHR vertexDataAddress = {
|
2024-07-15 08:32:50 +02:00
|
|
|
.deviceAddress = verticesBuffer.cast<ShaderBuffer>()->getDeviceAddress() + blas->getVertexOffset(),
|
2024-07-12 13:33:52 +02:00
|
|
|
};
|
|
|
|
|
VkDeviceOrHostAddressConstKHR indexDataAddress = {
|
|
|
|
|
.deviceAddress = indexBuffer.cast<IndexBuffer>()->getDeviceAddress() + blas->getIndexOffset(),
|
|
|
|
|
};
|
|
|
|
|
VkDeviceOrHostAddressConstKHR transformDataAddress = {
|
|
|
|
|
.deviceAddress = transformBuffer->deviceAddress + i * sizeof(VkTransformMatrixKHR),
|
|
|
|
|
};
|
|
|
|
|
geometries[i] = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR,
|
|
|
|
|
.geometry =
|
|
|
|
|
{
|
|
|
|
|
.triangles =
|
|
|
|
|
{
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
.vertexData = vertexDataAddress,
|
2024-12-25 14:59:08 +01:00
|
|
|
.vertexStride = sizeof(Vector),
|
2024-07-12 13:33:52 +02:00
|
|
|
.maxVertex = static_cast<uint32_t>(blas->getVertexCount()),
|
|
|
|
|
.indexType = VK_INDEX_TYPE_UINT32,
|
|
|
|
|
.indexData = indexDataAddress,
|
|
|
|
|
.transformData = transformDataAddress,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
.flags = VK_GEOMETRY_OPAQUE_BIT_KHR,
|
|
|
|
|
};
|
|
|
|
|
buildGeometries[i] = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR,
|
|
|
|
|
.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR,
|
|
|
|
|
.geometryCount = 1,
|
|
|
|
|
.pGeometries = &geometries[i],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
buildSizes[i] = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
};
|
|
|
|
|
const uint32 primitiveCount = blas->getPrimitiveCount();
|
|
|
|
|
vkGetAccelerationStructureBuildSizesKHR(handle, VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &buildGeometries[i],
|
|
|
|
|
&primitiveCount, &buildSizes[i]);
|
|
|
|
|
|
|
|
|
|
VkBufferCreateInfo bufferInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.size = buildSizes[i].accelerationStructureSize,
|
|
|
|
|
.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
|
|
|
|
|
};
|
|
|
|
|
VmaAllocationCreateInfo bufferAllocInfo = {
|
|
|
|
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
|
|
|
|
};
|
|
|
|
|
blas->buffer = new BufferAllocation(this, "BLAS", bufferInfo, bufferAllocInfo, Gfx::QueueType::GRAPHICS);
|
|
|
|
|
|
|
|
|
|
VkAccelerationStructureCreateInfoKHR blasInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.createFlags = 0,
|
|
|
|
|
.buffer = blas->buffer->buffer,
|
|
|
|
|
.offset = 0,
|
|
|
|
|
.size = buildSizes[i].accelerationStructureSize,
|
|
|
|
|
.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR,
|
|
|
|
|
};
|
|
|
|
|
VK_CHECK(vkCreateAccelerationStructureKHR(handle, &blasInfo, nullptr, &blas->handle));
|
|
|
|
|
|
|
|
|
|
VkBufferCreateInfo scratchInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.size = buildSizes[i].buildScratchSize,
|
|
|
|
|
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
|
|
|
|
|
};
|
|
|
|
|
VmaAllocationCreateInfo scratchAllocInfo = {
|
|
|
|
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
|
|
|
|
};
|
|
|
|
|
scratchBuffers[i] = new BufferAllocation(this, "ScratchBuffer", scratchInfo, scratchAllocInfo, Gfx::QueueType::GRAPHICS,
|
2025-05-18 08:49:22 +02:00
|
|
|
props.get<VkPhysicalDeviceAccelerationStructurePropertiesKHR>().minAccelerationStructureScratchOffsetAlignment);
|
2024-07-12 13:33:52 +02:00
|
|
|
|
|
|
|
|
buildGeometries[i].dstAccelerationStructure = blas->handle;
|
|
|
|
|
buildGeometries[i].scratchData.deviceAddress = scratchBuffers[i]->deviceAddress;
|
|
|
|
|
|
|
|
|
|
buildRanges[i] = VkAccelerationStructureBuildRangeInfoKHR{
|
|
|
|
|
.primitiveCount = primitiveCount,
|
|
|
|
|
.primitiveOffset = 0,
|
|
|
|
|
.firstVertex = 0,
|
|
|
|
|
.transformOffset = 0,
|
|
|
|
|
};
|
|
|
|
|
buildRangePointers[i] = &buildRanges[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PCommand cmd = graphicsCommands->getCommands();
|
2025-04-11 21:02:52 +02:00
|
|
|
vkCmdBuildAccelerationStructuresKHR(cmd->getHandle(), (uint32)buildGeometries.size(), buildGeometries.data(),
|
|
|
|
|
buildRangePointers.data());
|
2024-07-12 13:33:52 +02:00
|
|
|
cmd->bindResource(PBufferAllocation(transformBuffer));
|
|
|
|
|
destructionManager->queueResourceForDestruction(std::move(transformBuffer));
|
|
|
|
|
for (auto& scratchAlloc : scratchBuffers) {
|
|
|
|
|
cmd->bindResource(PBufferAllocation(scratchAlloc));
|
|
|
|
|
destructionManager->queueResourceForDestruction(std::move(scratchAlloc));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2021-11-14 20:36:53 +01:00
|
|
|
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]));
|
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
|
|
|
}
|
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]));
|
|
|
|
|
}
|
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
|
|
|
}
|
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]));
|
|
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
|
2021-11-14 20:36:53 +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++) {
|
2021-11-14 20:36:53 +01:00
|
|
|
extensions.add(glfwExtensions[i]);
|
|
|
|
|
}
|
2024-09-03 11:03:23 +02:00
|
|
|
#ifdef ENABLE_VALIDATION
|
2024-01-26 23:19:18 +01:00
|
|
|
extensions.add(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
2024-09-03 11:03:23 +02:00
|
|
|
#endif
|
2021-11-14 20:36:53 +01:00
|
|
|
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) {
|
2021-11-14 20:36:53 +01:00
|
|
|
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),
|
2025-05-18 08:49:22 +02:00
|
|
|
.apiVersion = VK_API_VERSION_1_4,
|
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) {
|
2021-11-14 20:36:53 +01:00
|
|
|
extensions.add(initInfo.instanceExtensions[i]);
|
|
|
|
|
}
|
2024-01-17 17:57:59 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
extensions.add("VK_KHR_portability_enumeration");
|
2023-11-17 18:46:37 +01:00
|
|
|
#endif
|
2024-07-16 15:32:51 +02:00
|
|
|
Array<const char*> layers = initInfo.layers;
|
2024-10-18 19:01:04 +02:00
|
|
|
// layers.add("VK_LAYER_KHRONOS_validation");
|
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,
|
2024-07-16 15:32:51 +02:00
|
|
|
.enabledLayerCount = (uint32)layers.size(),
|
|
|
|
|
.ppEnabledLayerNames = layers.data(),
|
2024-01-02 16:48:03 +01:00
|
|
|
.enabledExtensionCount = (uint32)extensions.size(),
|
|
|
|
|
.ppEnabledExtensionNames = extensions.data(),
|
|
|
|
|
};
|
2021-11-14 20:36:53 +01:00
|
|
|
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
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-07-12 22:05:52 +02:00
|
|
|
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
|
2024-06-09 12:20:04 +02:00
|
|
|
.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));
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void Graphics::pickPhysicalDevice() {
|
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;
|
2024-06-09 12:20:04 +02:00
|
|
|
for (auto dev : physicalDevices) {
|
2021-11-14 20:36:53 +01:00
|
|
|
uint32 currentRating = 0;
|
2025-05-18 08:49:22 +02:00
|
|
|
vkGetPhysicalDeviceProperties2(dev, &props.get());
|
|
|
|
|
if (props.get().properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
|
|
|
|
|
std::cout << "found dedicated gpu " << props.get().properties.deviceName << std::endl;
|
2021-11-14 20:36:53 +01:00
|
|
|
currentRating += 100;
|
2025-05-18 08:49:22 +02:00
|
|
|
} else if (props.get().properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) {
|
|
|
|
|
std::cout << "found integrated gpu " << props.get().properties.deviceName << std::endl;
|
2021-11-14 20:36:53 +01:00
|
|
|
currentRating += 10;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (currentRating > deviceRating) {
|
2021-11-14 20:36:53 +01:00
|
|
|
deviceRating = currentRating;
|
|
|
|
|
bestDevice = dev;
|
2025-05-18 08:49:22 +02:00
|
|
|
std::cout << "bestDevice: " << props.get().properties.deviceName << std::endl;
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
physicalDevice = bestDevice;
|
2024-01-17 17:57:59 +01:00
|
|
|
|
2025-05-18 08:49:22 +02:00
|
|
|
vkGetPhysicalDeviceProperties2(physicalDevice, &props.get());
|
2025-05-23 16:12:33 +02:00
|
|
|
features.get<VkPhysicalDeviceFeatures2>().features = {
|
|
|
|
|
.geometryShader = true,
|
2025-07-08 22:06:27 +02:00
|
|
|
.sampleRateShading = true,
|
2025-05-23 16:12:33 +02:00
|
|
|
.fillModeNonSolid = true,
|
|
|
|
|
.wideLines = true,
|
|
|
|
|
.pipelineStatisticsQuery = true,
|
|
|
|
|
.fragmentStoresAndAtomics = true,
|
|
|
|
|
.shaderInt64 = true,
|
|
|
|
|
.shaderInt16 = true,
|
|
|
|
|
.inheritedQueries = true,
|
|
|
|
|
};
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan11Features>().multiview = true;
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan11Features>().storageBuffer16BitAccess = true;
|
|
|
|
|
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().descriptorIndexing = true;
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().descriptorBindingPartiallyBound = true;
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().bufferDeviceAddress = true;
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().storageBuffer8BitAccess = true;
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().shaderInt8 = true;
|
|
|
|
|
|
2025-07-08 22:06:27 +02:00
|
|
|
rayTracingFeatures.get<VkPhysicalDeviceAccelerationStructureFeaturesKHR>().accelerationStructure = true;
|
2025-05-23 16:12:33 +02:00
|
|
|
|
2025-07-08 22:06:27 +02:00
|
|
|
rayTracingFeatures.get<VkPhysicalDeviceRayTracingPipelineFeaturesKHR>().rayTracingPipeline = true;
|
2025-05-23 16:12:33 +02:00
|
|
|
|
2025-07-08 22:06:27 +02:00
|
|
|
meshFeatures.get<VkPhysicalDeviceMeshShaderFeaturesEXT>().meshShader = true;
|
|
|
|
|
meshFeatures.get<VkPhysicalDeviceMeshShaderFeaturesEXT>().taskShader = true;
|
|
|
|
|
meshFeatures.get<VkPhysicalDeviceMeshShaderFeaturesEXT>().meshShaderQueries = true;
|
2025-01-08 19:15:12 +01:00
|
|
|
bool rayTracingPipelineSupport = false;
|
|
|
|
|
bool accelerationStructureSupport = false;
|
2025-05-16 13:04:43 +02:00
|
|
|
bool hostOperationsSupport = false;
|
|
|
|
|
bool deviceAddressSupport = false;
|
|
|
|
|
bool descriptorIndexingSupport = false;
|
|
|
|
|
bool spirv14Support = false;
|
|
|
|
|
bool shaderFloatControls = false;
|
2025-01-08 19:15:12 +01:00
|
|
|
uint32 count = 0;
|
|
|
|
|
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, nullptr);
|
|
|
|
|
Array<VkExtensionProperties> extensionProps(count);
|
|
|
|
|
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, extensionProps.data());
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
|
if (Gfx::useMeshShading) {
|
2024-06-09 12:20:04 +02:00
|
|
|
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
|
|
|
}
|
2023-11-06 22:24:40 +01:00
|
|
|
}
|
2025-01-08 19:15:12 +01:00
|
|
|
if (std::strcmp(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
rayTracingPipelineSupport = true;
|
|
|
|
|
}
|
|
|
|
|
if (std::strcmp(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
accelerationStructureSupport = true;
|
|
|
|
|
}
|
2025-05-16 13:04:43 +02:00
|
|
|
if (std::strcmp(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
hostOperationsSupport = true;
|
|
|
|
|
}
|
|
|
|
|
if (std::strcmp(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
deviceAddressSupport = true;
|
|
|
|
|
}
|
|
|
|
|
if (std::strcmp(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
descriptorIndexingSupport = true;
|
|
|
|
|
}
|
|
|
|
|
if (std::strcmp(VK_KHR_SPIRV_1_4_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
spirv14Support = true;
|
|
|
|
|
}
|
|
|
|
|
if (std::strcmp(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME, extensionProps[i].extensionName) == 0) {
|
|
|
|
|
shaderFloatControls = true;
|
|
|
|
|
}
|
2025-01-08 19:15:12 +01:00
|
|
|
}
|
2025-05-23 05:07:03 +02:00
|
|
|
rayTracingEnabled = rayTracingPipelineSupport && accelerationStructureSupport && hostOperationsSupport && deviceAddressSupport &&
|
2025-05-16 13:04:43 +02:00
|
|
|
descriptorIndexingSupport && spirv14Support && shaderFloatControls;
|
2020-03-16 13:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void Graphics::createDevice(GraphicsInitializer initializer) {
|
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());
|
2025-05-23 05:07:03 +02:00
|
|
|
|
2021-11-14 20:36:53 +01:00
|
|
|
Array<VkDeviceQueueCreateInfo> queueInfos;
|
2024-06-09 12:20:04 +02:00
|
|
|
struct QueueCreateInfo {
|
2021-11-14 20:36:53 +01:00
|
|
|
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) {
|
2021-11-14 20:36:53 +01:00
|
|
|
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) {
|
2021-11-14 20:36:53 +01:00
|
|
|
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);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
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);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
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);
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
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
|
|
|
};
|
2021-11-14 20:36:53 +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];
|
2021-11-14 20:36:53 +01:00
|
|
|
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) {
|
2025-05-18 08:49:22 +02:00
|
|
|
*currentPriority++ = 0.0f;
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-18 08:49:22 +02:00
|
|
|
initializer.deviceExtensions.add(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
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);
|
2025-07-08 22:06:27 +02:00
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().pNext = &meshFeatures.get();
|
2023-11-05 10:36:01 +01:00
|
|
|
}
|
2025-01-08 19:15:12 +01:00
|
|
|
if (supportRayTracing()) {
|
2025-05-16 13:04:43 +02:00
|
|
|
// ray tracing itself
|
2025-01-08 19:15:12 +01:00
|
|
|
initializer.deviceExtensions.add(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
|
|
|
|
|
initializer.deviceExtensions.add(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME);
|
2025-05-16 13:04:43 +02:00
|
|
|
// required by acceleration_structure
|
2025-01-08 19:15:12 +01:00
|
|
|
initializer.deviceExtensions.add(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
|
2025-05-16 13:04:43 +02:00
|
|
|
initializer.deviceExtensions.add(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
|
|
|
|
|
initializer.deviceExtensions.add(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
|
|
|
|
|
// required for ray tracing pipeline
|
|
|
|
|
initializer.deviceExtensions.add(VK_KHR_SPIRV_1_4_EXTENSION_NAME);
|
|
|
|
|
// required for spirv_1_4
|
|
|
|
|
initializer.deviceExtensions.add(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME);
|
2025-07-08 22:06:27 +02:00
|
|
|
if (supportMeshShading()) {
|
|
|
|
|
meshFeatures.get().pNext = &rayTracingFeatures.get();
|
|
|
|
|
} else {
|
|
|
|
|
features.get<VkPhysicalDeviceVulkan12Features>().pNext = &rayTracingFeatures.get();
|
|
|
|
|
}
|
2025-01-08 19:15:12 +01:00
|
|
|
}
|
2024-01-17 17:57:59 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
initializer.deviceExtensions.add("VK_KHR_portability_subset");
|
|
|
|
|
#endif
|
2023-11-15 17:42:57 +01:00
|
|
|
VkDeviceCreateInfo deviceInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
2025-05-18 08:49:22 +02:00
|
|
|
.pNext = &features.get(),
|
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
|
|
|
};
|
2021-11-14 20:36:53 +01:00
|
|
|
VK_CHECK(vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &handle));
|
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) {
|
2025-03-26 13:38:48 +01:00
|
|
|
computeQueue = (uint32)queues.size();
|
2024-02-01 08:42:24 +01:00
|
|
|
queues.add(new Queue(this, computeQueueInfo.familyIndex, computeQueueInfo.queueIndex));
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (transferQueueInfo.familyIndex != -1) {
|
2025-03-26 13:38:48 +01:00
|
|
|
transferQueue = (uint32)queues.size();
|
2024-02-01 08:42:24 +01:00
|
|
|
queues.add(new Queue(this, transferQueueInfo.familyIndex, transferQueueInfo.queueIndex));
|
2021-11-14 20:36:53 +01:00
|
|
|
}
|
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
|
|
|
|
2024-07-01 12:17:04 +02:00
|
|
|
graphicsProps = queueProperties[queueMapping.graphicsFamily];
|
|
|
|
|
|
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-10-18 19:01:04 +02:00
|
|
|
queueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkQueueBeginDebugUtilsLabelEXT");
|
|
|
|
|
queueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkQueueEndDebugUtilsLabelEXT");
|
2024-10-30 10:28:10 +01:00
|
|
|
cmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkCmdBeginDebugUtilsLabelEXT");
|
|
|
|
|
cmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkCmdEndDebugUtilsLabelEXT");
|
2024-06-13 15:43:03 +02:00
|
|
|
|
2025-01-08 19:15:12 +01:00
|
|
|
if (rayTracingEnabled) {
|
|
|
|
|
createAccelerationStructure = (PFN_vkCreateAccelerationStructureKHR)vkGetDeviceProcAddr(handle, "vkCreateAccelerationStructureKHR");
|
|
|
|
|
cmdBuildAccelerationStructures =
|
|
|
|
|
(PFN_vkCmdBuildAccelerationStructuresKHR)vkGetDeviceProcAddr(handle, "vkCmdBuildAccelerationStructuresKHR");
|
|
|
|
|
getAccelerationStructureBuildSize =
|
|
|
|
|
(PFN_vkGetAccelerationStructureBuildSizesKHR)vkGetDeviceProcAddr(handle, "vkGetAccelerationStructureBuildSizesKHR");
|
|
|
|
|
createRayTracingPipelines = (PFN_vkCreateRayTracingPipelinesKHR)vkGetDeviceProcAddr(handle, "vkCreateRayTracingPipelinesKHR");
|
|
|
|
|
getRayTracingShaderGroupHandles =
|
|
|
|
|
(PFN_vkGetRayTracingShaderGroupHandlesKHR)vkGetDeviceProcAddr(handle, "vkGetRayTracingShaderGroupHandlesKHR");
|
|
|
|
|
cmdTraceRays = (PFN_vkCmdTraceRaysKHR)vkGetDeviceProcAddr(handle, "vkCmdTraceRaysKHR");
|
|
|
|
|
getAccelerationStructureDeviceAddress =
|
|
|
|
|
(PFN_vkGetAccelerationStructureDeviceAddressKHR)vkGetDeviceProcAddr(handle, "vkGetAccelerationStructureDeviceAddressKHR");
|
|
|
|
|
}
|
2020-03-15 15:58:01 +01:00
|
|
|
}
|