Fixing destruction

This commit is contained in:
Dynamitos
2024-07-18 11:45:56 +02:00
parent 67c4ce2f7a
commit 3b6eb3ebcc
31 changed files with 173 additions and 82 deletions
+15 -6
View File
@@ -54,6 +54,7 @@ void BufferAllocation::pipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageF
.offset = 0,
.size = size,
};
commandBuffer->bindResource(this);
vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, nullptr, 1, &barrier, 0, nullptr);
}
@@ -77,6 +78,8 @@ void BufferAllocation::transferOwnership(Gfx::QueueType newOwner) {
PCommandPool sourcePool = graphics->getQueueCommands(owner);
PCommandPool dstPool = graphics->getQueueCommands(newOwner);
assert(barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex);
sourcePool->getCommands()->bindResource(this);
dstPool->getCommands()->bindResource(this);
vkCmdPipelineBarrier(sourcePool->getCommands()->getHandle(), VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0,
0, nullptr, 1, &barrier, 0, nullptr);
vkCmdPipelineBarrier(dstPool->getCommands()->getHandle(), VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 0,
@@ -110,17 +113,15 @@ void BufferAllocation::updateContents(uint64 regionOffset, uint64 regionSize, vo
Gfx::QueueType prevOwner = owner;
// transferOwnership(Gfx::QueueType::TRANSFER);
PCommand cmd = graphics->getQueueCommands(Gfx::QueueType::TRANSFER)->getCommands();
PCommand cmd = graphics->getQueueCommands(Gfx::QueueType::GRAPHICS)->getCommands();
VkBufferCopy copy = {
.srcOffset = 0,
.dstOffset = regionOffset,
.size = regionSize,
};
vkCmdCopyBuffer(cmd->getHandle(), staging->buffer, buffer, 1, &copy);
pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT);
cmd->bindResource(PBufferAllocation(this));
cmd->bindResource(PBufferAllocation(staging));
vkCmdCopyBuffer(cmd->getHandle(), staging->buffer, buffer, 1, &copy);
// transferOwnership(prevOwner);
graphics->getDestructionManager()->queueResourceForDestruction(std::move(staging));
@@ -153,9 +154,9 @@ void BufferAllocation::readContents(uint64 regionOffset, uint64 regionSize, void
.dstOffset = 0,
.size = regionSize,
};
vkCmdCopyBuffer(cmd->getHandle(), buffer, staging->buffer, 1, &copy);
cmd->bindResource(PBufferAllocation(this));
cmd->bindResource(PBufferAllocation(staging));
vkCmdCopyBuffer(cmd->getHandle(), buffer, staging->buffer, 1, &copy);
pool->submitCommands();
cmd->getFence()->wait(1000000);
@@ -250,6 +251,7 @@ void Buffer::createBuffer(uint64 size, uint32 destIndex) {
buffers[destIndex] = new BufferAllocation(graphics, name, info, allocInfo, initialOwner);
if (createCleared) {
PCommand command = graphics->getQueueCommands(initialOwner)->getCommands();
command->bindResource(PBufferAllocation(buffers[destIndex]));
vkCmdFillBuffer(command->getHandle(), buffers[destIndex]->buffer, 0, VK_WHOLE_SIZE, clearValue);
pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
@@ -284,6 +286,8 @@ void Buffer::copyBuffer(uint64 src, uint64 dst) {
.size = buffers[dst]->size,
};
VkBufferMemoryBarrier srcBarriers[] = {srcBarrier, clearBarrier};
command->bindResource(PBufferAllocation(buffers[src]));
command->bindResource(PBufferAllocation(buffers[dst]));
vkCmdPipelineBarrier(command->getHandle(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 2,
srcBarriers, 0, nullptr);
VkBufferCopy region = {
@@ -291,6 +295,8 @@ void Buffer::copyBuffer(uint64 src, uint64 dst) {
.dstOffset = 0,
.size = buffers[src]->size,
};
command->bindResource(PBufferAllocation(buffers[src]));
command->bindResource(PBufferAllocation(buffers[dst]));
vkCmdCopyBuffer(command->getHandle(), buffers[src]->buffer, buffers[dst]->buffer, 1, &region);
VkBufferMemoryBarrier dstBarrier = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
@@ -303,6 +309,7 @@ void Buffer::copyBuffer(uint64 src, uint64 dst) {
.offset = 0,
.size = buffers[dst]->size,
};
command->bindResource(PBufferAllocation(buffers[dst]));
vkCmdPipelineBarrier(command->getHandle(), VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 1, &dstBarrier, 0, nullptr);
}
@@ -383,7 +390,9 @@ void ShaderBuffer::rotateBuffer(uint64 size, bool preserveContents) {
}
void ShaderBuffer::clear() {
vkCmdFillBuffer(graphics->getQueueCommands(getAlloc()->owner)->getCommands()->getHandle(), Vulkan::Buffer::getHandle(), 0,
PCommand command = graphics->getQueueCommands(getAlloc()->owner)->getCommands();
command->bindResource(PBufferAllocation(getAlloc()));
vkCmdFillBuffer(command->getHandle(), Vulkan::Buffer::getHandle(), 0,
VK_WHOLE_SIZE, 0);
}
+9 -2
View File
@@ -245,7 +245,7 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uint
auto descriptor = descriptorSet.cast<DescriptorSet>();
assert(descriptor->writeDescriptors.size() == 0);
descriptor->bind();
boundResources.add(descriptor.getHandle());
boundResources.add(descriptor);
for (auto binding : descriptor->boundResources) {
for (auto res : binding) {
res->bind();
@@ -269,7 +269,7 @@ void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorS
auto descriptorSet = descriptorSets[i].cast<DescriptorSet>();
assert(descriptorSet->writeDescriptors.size() == 0);
descriptorSet->bind();
boundResources.add(descriptorSet.getHandle());
boundResources.add(descriptorSet);
for (auto binding : descriptorSet->boundResources) {
for (auto res : binding) {
@@ -550,3 +550,10 @@ void CommandPool::submitCommands(PSemaphore signalSemaphore) {
command->begin();
command->waitForSemaphore(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, waitSemaphore);
}
void CommandPool::refreshCommands() {
for (uint32 i = 0; i < allocatedBuffers.size(); ++i)
{
allocatedBuffers[i]->checkFence();
}
}
+1
View File
@@ -144,6 +144,7 @@ class CommandPool {
OComputeCommand createComputeCommand(const std::string& name);
constexpr VkCommandPool getHandle() const { return commandPool; }
void submitCommands(PSemaphore signalSemaphore = nullptr);
void refreshCommands();
private:
PGraphics graphics;
+2 -5
View File
@@ -125,7 +125,7 @@ Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() {
if (cachedHandles[setIndex] == nullptr) {
cachedHandles[setIndex] = new DescriptorSet(graphics, this);
}
if (cachedHandles[setIndex]->isCurrentlyBound() || cachedHandles[setIndex]->isCurrentlyInUse()) {
if (cachedHandles[setIndex]->isCurrentlyBound()) {
// Currently in use, skip
continue;
}
@@ -133,7 +133,6 @@ Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() {
// If it hasnt been initialized, allocate it
VK_CHECK(vkAllocateDescriptorSets(graphics->getDevice(), &allocInfo, &cachedHandles[setIndex]->setHandle));
}
cachedHandles[setIndex]->allocate();
PDescriptorSet vulkanSet = cachedHandles[setIndex];
@@ -153,7 +152,6 @@ void DescriptorPool::reset() {
if (cachedHandles[i] == nullptr) {
return;
}
cachedHandles[i]->free();
}
if (nextAlloc != nullptr) {
nextAlloc->reset();
@@ -161,8 +159,7 @@ void DescriptorPool::reset() {
}
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner),
bindCount(0), currentlyInUse(false) {
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner){
boundResources.resize(owner->getLayout()->getBindings().size());
for (uint32 i = 0; i < boundResources.size(); ++i)
{
-5
View File
@@ -60,9 +60,6 @@ class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource {
virtual void updateSamplerArray(uint32 binding, Array<Gfx::PSampler> samplers) override;
virtual void updateAccelerationStructure(uint32 binding, Gfx::PTopLevelAS as) override;
constexpr bool isCurrentlyInUse() const { return currentlyInUse; }
constexpr void allocate() { currentlyInUse = true; }
constexpr void free() { currentlyInUse = false; }
constexpr VkDescriptorSet getHandle() const { return setHandle; }
private:
@@ -78,8 +75,6 @@ class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource {
VkDescriptorSet setHandle;
PGraphics graphics;
PDescriptorPool owner;
uint32 bindCount;
bool currentlyInUse;
friend class DescriptorPool;
friend class Command;
friend class RenderCommand;
+5 -5
View File
@@ -100,7 +100,6 @@ VkDeviceAddress vkGetAccelerationStructureDeviceAddressKHR(VkDevice device, cons
Graphics::Graphics() : instance(VK_NULL_HANDLE), handle(VK_NULL_HANDLE), physicalDevice(VK_NULL_HANDLE), callback(VK_NULL_HANDLE) {}
Graphics::~Graphics() {
vkDeviceWaitIdle(handle);
pipelineCache = nullptr;
allocatedFramebuffers.clear();
shaderCompiler = nullptr;
@@ -166,7 +165,10 @@ void Graphics::beginRenderPass(Gfx::PRenderPass renderPass) {
void Graphics::endRenderPass() { getGraphicsCommands()->getCommands()->endRenderPass(); }
void Graphics::waitDeviceIdle() { vkDeviceWaitIdle(handle); }
void Graphics::waitDeviceIdle() {
vkDeviceWaitIdle(handle);
getGraphicsCommands()->refreshCommands();
}
void Graphics::executeCommands(Array<Gfx::ORenderCommand> commands) {
getGraphicsCommands()->getCommands()->executeCommands(std::move(commands));
@@ -527,11 +529,9 @@ void Graphics::buildBottomLevelAccelerationStructures(Array<Gfx::PBottomLevelAS>
PCommand cmd = graphicsCommands->getCommands();
vkCmdBuildAccelerationStructuresKHR(cmd->getHandle(), buildGeometries.size(), buildGeometries.data(), buildRangePointers.data());
transformBuffer->bind();
cmd->bindResource(PBufferAllocation(transformBuffer));
destructionManager->queueResourceForDestruction(std::move(transformBuffer));
for (auto& scratchAlloc : scratchBuffers) {
scratchAlloc->bind();
cmd->bindResource(PBufferAllocation(scratchAlloc));
destructionManager->queueResourceForDestruction(std::move(scratchAlloc));
}
@@ -655,7 +655,7 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
extensions.add("VK_KHR_portability_enumeration");
#endif
Array<const char*> layers = initInfo.layers;
layers.add("VK_LAYER_KHRONOS_validation");
//layers.add("VK_LAYER_KHRONOS_validation");
VkInstanceCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = nullptr,
+1 -3
View File
@@ -190,14 +190,12 @@ TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& crea
};
vkCmdPipelineBarrier(cmd->getHandle(), VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, 0, 0,
nullptr, 1, &barrier, 0, nullptr);
scratchBuffer->bind();
cmd->bindResource(PBufferAllocation(scratchBuffer));
graphics->getDestructionManager()->queueResourceForDestruction(std::move(scratchBuffer));
buffer->bind();
cmd->bindResource(PBufferAllocation(buffer));
instanceAllocation->bind();
cmd->bindResource(PBufferAllocation(instanceAllocation));
}