Fixing initial frames

This commit is contained in:
Dynamitos
2024-06-18 23:33:03 +02:00
parent f6ebbc2067
commit e501a69b36
12 changed files with 207 additions and 18 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ class ShaderBuffer : public Buffer {
public: public:
ShaderBuffer(QueueFamilyMapping mapping, const ShaderBufferCreateInfo& createInfo); ShaderBuffer(QueueFamilyMapping mapping, const ShaderBufferCreateInfo& createInfo);
virtual ~ShaderBuffer(); virtual ~ShaderBuffer();
virtual void rotateBuffer(uint64 size, bool preserveContents = false) = 0; virtual void rotateBuffer(uint64 size, bool preserveContents = false, uint32 fillValue = 0) = 0;
virtual void updateContents(const ShaderBufferCreateInfo& sourceData) = 0; virtual void updateContents(const ShaderBufferCreateInfo& sourceData) = 0;
constexpr uint32 getNumElements() const { return numElements; } constexpr uint32 getNumElements() const { return numElements; }
+7
View File
@@ -1,7 +1,14 @@
#include "RayTracing.h" #include "RayTracing.h"
#include "RayTracing.h"
using namespace Seele::Gfx; using namespace Seele::Gfx;
RayTracingPipeline::RayTracingPipeline(Gfx::PPipelineLayout layout) {}
RayTracingPipeline::~RayTracingPipeline() {}
PPipelineLayout RayTracingPipeline::getPipelineLayout() const { return layout; }
BottomLevelAS::BottomLevelAS() {} BottomLevelAS::BottomLevelAS() {}
BottomLevelAS::~BottomLevelAS() {} BottomLevelAS::~BottomLevelAS() {}
+7 -1
View File
@@ -3,10 +3,16 @@
namespace Seele { namespace Seele {
namespace Gfx { namespace Gfx {
DECLARE_REF(PipelineLayout)
class RayTracingPipeline class RayTracingPipeline
{ {
public: public:
private: RayTracingPipeline(PPipelineLayout layout);
virtual ~RayTracingPipeline();
PPipelineLayout getPipelineLayout() const;
protected:
PPipelineLayout layout;
}; };
DEFINE_REF(RayTracingPipeline) DEFINE_REF(RayTracingPipeline)
class BottomLevelAS { class BottomLevelAS {
@@ -270,7 +270,8 @@ void LightCullingPass::setupFrustums() {
.dynamic = false, .dynamic = false,
.name = "FrustumDispatch", .name = "FrustumDispatch",
}); });
frustumDispatchParamsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
frustumBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ frustumBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = .sourceData =
{ {
@@ -282,6 +283,8 @@ void LightCullingPass::setupFrustums() {
.dynamic = false, .dynamic = false,
.name = "FrustumBuffer", .name = "FrustumBuffer",
}); });
frustumBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
Gfx::PDescriptorSet dispatchParamsSet = dispatchParamsLayout->allocateDescriptorSet(); Gfx::PDescriptorSet dispatchParamsSet = dispatchParamsLayout->allocateDescriptorSet();
dispatchParamsSet->updateBuffer(0, frustumDispatchParamsBuffer); dispatchParamsSet->updateBuffer(0, frustumDispatchParamsBuffer);
@@ -11,7 +11,7 @@ VisibilityPass::~VisibilityPass() {}
void VisibilityPass::beginFrame(const Component::Camera& cam) { void VisibilityPass::beginFrame(const Component::Camera& cam) {
RenderPass::beginFrame(cam); RenderPass::beginFrame(cam);
cullingBuffer->rotateBuffer(VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo), true); cullingBuffer->rotateBuffer(VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo), true, 0xffffffff);
} }
void VisibilityPass::render() { void VisibilityPass::render() {
+8 -3
View File
@@ -187,7 +187,7 @@ void Buffer::readContents(uint64 regionOffset, uint64 regionSize, void* buffer)
getAlloc()->readContents(regionOffset, regionSize, buffer); getAlloc()->readContents(regionOffset, regionSize, buffer);
} }
void Buffer::rotateBuffer(uint64 size, bool preserveContents) { void Buffer::rotateBuffer(uint64 size, bool preserveContents, uint32 fillValue) {
assert(dynamic); assert(dynamic);
if (buffers.size() > 0) { if (buffers.size() > 0) {
size = std::max(getSize(), size); size = std::max(getSize(), size);
@@ -216,6 +216,11 @@ void Buffer::rotateBuffer(uint64 size, bool preserveContents) {
} }
if (preserveContents) { if (preserveContents) {
copyBuffer(currentBuffer, i); copyBuffer(currentBuffer, i);
if (buffers[i]->size > buffers[currentBuffer]->size) {
PCommand command = graphics->getQueueCommands(getAlloc()->owner)->getCommands();
vkCmdFillBuffer(command->getHandle(), buffers[i]->buffer, buffers[currentBuffer]->size,
buffers[i]->size - buffers[currentBuffer]->size, fillValue);
}
} }
currentBuffer = i; currentBuffer = i;
return; return;
@@ -344,9 +349,9 @@ void ShaderBuffer::updateContents(const ShaderBufferCreateInfo& createInfo) {
} }
} }
void ShaderBuffer::rotateBuffer(uint64 size, bool preserveContents) { void ShaderBuffer::rotateBuffer(uint64 size, bool preserveContents, uint32 fillValue) {
assert(dynamic); assert(dynamic);
Vulkan::Buffer::rotateBuffer(size, preserveContents); Vulkan::Buffer::rotateBuffer(size, preserveContents, fillValue);
} }
void ShaderBuffer::clear() { void ShaderBuffer::clear() {
+2 -2
View File
@@ -45,7 +45,7 @@ class Buffer {
VkBufferUsageFlags usage; VkBufferUsageFlags usage;
bool dynamic; bool dynamic;
std::string name; std::string name;
void rotateBuffer(uint64 size, bool preserveContents = false); void rotateBuffer(uint64 size, bool preserveContents = false, uint32 fillValue = 0);
void createBuffer(uint64 size); void createBuffer(uint64 size);
void copyBuffer(uint64 src, uint64 dest); void copyBuffer(uint64 src, uint64 dest);
@@ -107,7 +107,7 @@ class ShaderBuffer : public Gfx::ShaderBuffer, public Buffer {
ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& sourceData); ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& sourceData);
virtual ~ShaderBuffer(); virtual ~ShaderBuffer();
virtual void updateContents(const ShaderBufferCreateInfo& createInfo) override; virtual void updateContents(const ShaderBufferCreateInfo& createInfo) override;
virtual void rotateBuffer(uint64 size, bool preserveContents = false) override; virtual void rotateBuffer(uint64 size, bool preserveContents = false, uint32 fillValue = 0) override;
virtual void clear() override; virtual void clear() override;
+9
View File
@@ -34,6 +34,7 @@ PFN_vkSetDebugUtilsObjectNameEXT setDebugUtilsObjectName;
PFN_vkCreateAccelerationStructureKHR createAccelerationStructure; PFN_vkCreateAccelerationStructureKHR createAccelerationStructure;
PFN_vkCmdBuildAccelerationStructuresKHR cmdBuildAccelerationStructures; PFN_vkCmdBuildAccelerationStructuresKHR cmdBuildAccelerationStructures;
PFN_vkGetAccelerationStructureBuildSizesKHR getAccelerationStructureBuildSize; PFN_vkGetAccelerationStructureBuildSizesKHR getAccelerationStructureBuildSize;
PFN_vkCreateRayTracingPipelinesKHR createRayTracingPipelines;
void vkCmdDrawMeshTasksEXT(VkCommandBuffer command, uint32 groupX, uint32 groupY, uint32 groupZ) { void vkCmdDrawMeshTasksEXT(VkCommandBuffer command, uint32 groupX, uint32 groupY, uint32 groupZ) {
cmdDrawMeshTasks(command, groupX, groupY, groupZ); cmdDrawMeshTasks(command, groupX, groupY, groupZ);
@@ -64,6 +65,13 @@ void vkGetAccelerationStructureBuildSizesKHR(VkDevice device, VkAccelerationStru
getAccelerationStructureBuildSize(device, buildType, pBuildInfo, pMaxPrimitiveCounts, pSizeInfo); getAccelerationStructureBuildSize(device, buildType, pBuildInfo, pMaxPrimitiveCounts, pSizeInfo);
} }
VkResult vkCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, VkPipelineCache pipelineCache,
uint32_t createInfoCount, const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines)
{
return createRayTracingPipelines(device, deferredOperation, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines);
}
Graphics::Graphics() : instance(VK_NULL_HANDLE), handle(VK_NULL_HANDLE), physicalDevice(VK_NULL_HANDLE), callback(VK_NULL_HANDLE) {} Graphics::Graphics() : instance(VK_NULL_HANDLE), handle(VK_NULL_HANDLE), physicalDevice(VK_NULL_HANDLE), callback(VK_NULL_HANDLE) {}
Graphics::~Graphics() { Graphics::~Graphics() {
@@ -684,4 +692,5 @@ void Graphics::createDevice(GraphicsInitializer initializer) {
(PFN_vkCmdBuildAccelerationStructuresKHR)vkGetDeviceProcAddr(handle, "vkCmdBuildAccelerationStructuresKHR"); (PFN_vkCmdBuildAccelerationStructuresKHR)vkGetDeviceProcAddr(handle, "vkCmdBuildAccelerationStructuresKHR");
getAccelerationStructureBuildSize = getAccelerationStructureBuildSize =
(PFN_vkGetAccelerationStructureBuildSizesKHR)vkGetDeviceProcAddr(handle, "vkGetAccelerationStructureBuildSizesKHR"); (PFN_vkGetAccelerationStructureBuildSizesKHR)vkGetDeviceProcAddr(handle, "vkGetAccelerationStructureBuildSizesKHR");
createRayTracingPipelines = (PFN_vkCreateRayTracingPipelinesKHR)vkGetDeviceProcAddr(handle, "vkCreateRayTracingPipelinesKHR");
} }
+153 -2
View File
@@ -6,7 +6,6 @@
#include "Shader.h" #include "Shader.h"
#include <fstream> #include <fstream>
using namespace Seele; using namespace Seele;
using namespace Seele::Vulkan; using namespace Seele::Vulkan;
@@ -466,4 +465,156 @@ PComputePipeline PipelineCache::createPipeline(Gfx::ComputePipelineCreateInfo co
return result; return result;
} }
PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateInfo createInfo) { return nullptr; } PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateInfo createInfo) {
Array<VkPipelineShaderStageCreateInfo> shaderStages;
Array<VkRayTracingShaderGroupCreateInfoKHR> shaderGroups;
{
auto rayGen = createInfo.rayGenShader.cast<RayGenShader>();
shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
.module = rayGen->getModuleHandle(),
.pName = rayGen->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = static_cast<uint32>(shaderStages.size() - 1),
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
});
}
{
for (auto gfxHit : createInfo.closestHitShaders) {
auto hit = gfxHit.cast<ClosestHitShader>();
shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR,
.module = hit->getModuleHandle(),
.pName = hit->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.generalShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = static_cast<uint32>(shaderStages.size() - 1),
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
});
}
}
{
for (auto gfxHit : createInfo.anyHitShaders) {
auto hit = gfxHit.cast<AnyHitShader>();
shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_ANY_HIT_BIT_KHR,
.module = hit->getModuleHandle(),
.pName = hit->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.generalShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = static_cast<uint32>(shaderStages.size() - 1),
.intersectionShader = VK_SHADER_UNUSED_KHR,
});
}
}
{
for (auto gfxIntersect : createInfo.intersectionShaders) {
auto intersect = gfxIntersect.cast<IntersectionShader>();
shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_INTERSECTION_BIT_KHR,
.module = intersect->getModuleHandle(),
.pName = intersect->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.generalShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = static_cast<uint32>(shaderStages.size() - 1),
});
}
}
{
for (auto gfxMiss : createInfo.missShaders) {
auto miss = gfxMiss.cast<MissShader>();
shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_MISS_BIT_KHR,
.module = miss->getModuleHandle(),
.pName = miss->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = static_cast<uint32>(shaderStages.size() - 1),
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
});
}
}
{
for (auto gfxCallable : createInfo.callableShaders) {
auto miss = gfxCallable.cast<CallableShader>();
shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_CALLABLE_BIT_KHR,
.module = miss->getModuleHandle(),
.pName = miss->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = static_cast<uint32>(shaderStages.size() - 1),
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
});
}
}
uint32 hash = CRC::Calculate(shaderStages.data(), sizeof(VkPipelineShaderStageCreateInfo) * shaderStages.size(), CRC::CRC_32());
hash = CRC::Calculate(shaderGroups.data(), sizeof(VkRayTracingShaderGroupCreateInfoKHR) * shaderGroups.size(), CRC::CRC_32(), hash);
VkRayTracingPipelineCreateInfoKHR pipelineInfo = {
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
.pNext = nullptr,
.stageCount = static_cast<uint32>(shaderStages.size()),
.pStages = shaderStages.data(),
.groupCount = static_cast<uint32>(shaderGroups.size()),
.pGroups = shaderGroups.data(),
.maxPipelineRayRecursionDepth = 24,
.layout = createInfo.pipelineLayout.cast<PipelineLayout>()->getHandle(),
};
VkPipeline pipelineHandle;
VK_CHECK(vkCreateRayTracingPipelinesKHR(graphics->getDevice(), VK_NULL_HANDLE, cache, 1, &pipelineInfo, nullptr, &pipelineHandle));
ORayTracingPipeline pipeline = new RayTracingPipeline(graphics, pipelineHandle, createInfo.pipelineLayout);
PRayTracingPipeline handle = pipeline;
rayTracingPipelines[hash] = std::move(pipeline);
return handle;
}
@@ -17,6 +17,7 @@ class PipelineCache {
private: private:
Map<uint32, OGraphicsPipeline> graphicsPipelines; Map<uint32, OGraphicsPipeline> graphicsPipelines;
Map<uint32, OComputePipeline> computePipelines; Map<uint32, OComputePipeline> computePipelines;
Map<uint32, ORayTracingPipeline> rayTracingPipelines;
std::mutex cacheLock; std::mutex cacheLock;
VkPipelineCache cache; VkPipelineCache cache;
PGraphics graphics; PGraphics graphics;
+8 -6
View File
@@ -93,8 +93,7 @@ BottomLevelAS::BottomLevelAS(PGraphics graphics, const Gfx::BottomLevelASCreateI
VmaAllocationCreateInfo bufferAllocInfo = { VmaAllocationCreateInfo bufferAllocInfo = {
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
}; };
buffer = buffer = new BufferAllocation(graphics, "BLAS", bufferInfo, bufferAllocInfo, Gfx::QueueType::GRAPHICS);
new BufferAllocation(graphics, "BLAS", bufferInfo, bufferAllocInfo, Gfx::QueueType::GRAPHICS);
VkAccelerationStructureCreateInfoKHR blasInfo = { VkAccelerationStructureCreateInfoKHR blasInfo = {
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR, .sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
@@ -140,13 +139,16 @@ BottomLevelAS::BottomLevelAS(PGraphics graphics, const Gfx::BottomLevelASCreateI
graphics->getDestructionManager()->queueResourceForDestruction(std::move(transformBuffer)); graphics->getDestructionManager()->queueResourceForDestruction(std::move(transformBuffer));
graphics->getDestructionManager()->queueResourceForDestruction(std::move(scratchAlloc)); graphics->getDestructionManager()->queueResourceForDestruction(std::move(scratchAlloc));
//todo: compact // todo: compact
} }
BottomLevelAS::~BottomLevelAS() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffer)); } BottomLevelAS::~BottomLevelAS() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffer)); }
TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& createInfo) { TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& createInfo) {}
}
TopLevelAS::~TopLevelAS() {} TopLevelAS::~TopLevelAS() {}
RayTracingPipeline::RayTracingPipeline(PGraphics graphics, VkPipeline handle, Gfx::PPipelineLayout layout)
: Gfx::RayTracingPipeline(layout), graphics(graphics), pipeline(handle) {}
RayTracingPipeline::~RayTracingPipeline() {}
+5
View File
@@ -33,7 +33,12 @@ DEFINE_REF(TopLevelAS)
class RayTracingPipeline : public Gfx::RayTracingPipeline class RayTracingPipeline : public Gfx::RayTracingPipeline
{ {
public: public:
RayTracingPipeline(PGraphics graphics, VkPipeline handle, Gfx::PPipelineLayout layout);
virtual ~RayTracingPipeline();
private: private:
PGraphics graphics;
VkPipeline pipeline;
}; };
DEFINE_REF(RayTracingPipeline) DEFINE_REF(RayTracingPipeline)
} // namespace Vulkan } // namespace Vulkan