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
+2
View File
@@ -1,4 +1,5 @@
#include "Command.h"
#include "Command.h"
using namespace Seele;
using namespace Seele::Gfx;
@@ -10,3 +11,4 @@ RenderCommand::~RenderCommand() {}
ComputeCommand::ComputeCommand() {}
ComputeCommand::~ComputeCommand() {}
-5
View File
@@ -12,8 +12,6 @@ void Mesh::save(ArchiveBuffer& buffer) const {
Serialization::save(buffer, transform);
Serialization::save(buffer, vertexData->getTypeName());
Serialization::save(buffer, vertexCount);
Serialization::save(buffer, indices);
Serialization::save(buffer, meshlets);
Serialization::save(buffer, referencedMaterial->getFolderPath());
Serialization::save(buffer, referencedMaterial->getName());
vertexData->serializeMesh(id, vertexCount, buffer);
@@ -25,15 +23,12 @@ void Mesh::load(ArchiveBuffer& buffer) {
Serialization::load(buffer, typeName);
Serialization::load(buffer, vertexCount);
vertexData = VertexData::findByTypeName(typeName);
Serialization::load(buffer, indices);
Serialization::load(buffer, meshlets);
std::string refFolder;
Serialization::load(buffer, refFolder);
std::string refId;
Serialization::load(buffer, refId);
referencedMaterial = AssetRegistry::findMaterialInstance(refFolder, refId);
id = vertexData->allocateVertexData(vertexCount);
vertexData->loadMesh(id, indices, meshlets);
vertexData->deserializeMesh(id, buffer);
blas = buffer.getGraphics()->createBottomLevelAccelerationStructure(Gfx::BottomLevelASCreateInfo{
.mesh = this,
-2
View File
@@ -16,8 +16,6 @@ class Mesh {
MeshId id;
uint64 vertexCount;
PMaterialInstanceAsset referencedMaterial;
Array<uint32> indices;
Array<Meshlet> meshlets;
Gfx::OBottomLevelAS blas;
void save(ArchiveBuffer& buffer) const;
void load(ArchiveBuffer& buffer);
@@ -53,6 +53,7 @@ void Seele::StaticMeshVertexData::loadColors(uint64 offset, const Array<Vector4>
}
void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) {
VertexData::serializeMesh(id, numVertices, buffer);
uint64 offset;
{
std::unique_lock l(vertexDataLock);
@@ -82,6 +83,7 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB
}
void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
VertexData::deserializeMesh(id, buffer);
uint64 offset;
{
std::unique_lock l(vertexDataLock);
+31 -2
View File
@@ -147,8 +147,7 @@ void VertexData::createDescriptors() {
}
}
}
for (uint32 i = 0; i < transparentData.size(); ++i)
{
for (uint32 i = 0; i < transparentData.size(); ++i) {
transparentData[i].offsets.instanceOffset = instanceData.size();
instanceData.add(transparentData[i].instanceData);
instanceMeshData.add(transparentData[i].meshData);
@@ -299,6 +298,35 @@ MeshId VertexData::allocateVertexData(uint64 numVertices) {
return res;
}
void VertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) {
std::unique_lock l(vertexDataLock);
Array<Meshlet> out;
MeshData data = meshData[id];
for (size_t i = 0; i < data.numMeshlets; ++i)
{
MeshletDescription& desc = meshlets[i + data.meshletOffset];
Meshlet m;
std::memcpy(m.uniqueVertices, &vertexIndices[desc.vertexOffset], desc.vertexCount * sizeof(uint32));
std::memcpy(m.primitiveLayout, &primitiveIndices[desc.primitiveOffset], desc.primitiveCount * 3 * sizeof(uint8));
m.numPrimitives = desc.primitiveCount;
m.numVertices = desc.vertexCount;
m.boundingBox = desc.bounding;
out.add(std::move(m));
}
Array<uint32> ind(data.numIndices);
std::memcpy(ind.data(), &indices[data.firstIndex], data.numIndices * sizeof(uint32));
Serialization::save(buffer, out);
Serialization::save(buffer, ind);
}
void VertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
Array<Meshlet> in;
Array<uint32> ind;
Serialization::load(buffer, in);
Serialization::load(buffer, ind);
loadMesh(id, ind, in);
}
List<VertexData*> vertexDataList;
List<VertexData*> VertexData::getList() { return vertexDataList; }
@@ -362,6 +390,7 @@ void VertexData::init(Gfx::PGraphics _graphics) {
}
void VertexData::destroy() {
cullingOffsetBuffer = nullptr;
instanceBuffer = nullptr;
instanceMeshDataBuffer = nullptr;
instanceDataLayout = nullptr;
+2 -2
View File
@@ -61,8 +61,8 @@ class VertexData {
MeshId allocateVertexData(uint64 numVertices);
uint64 getMeshOffset(MeshId id) const { return meshOffsets[id]; }
uint64 getMeshVertexCount(MeshId id) { return meshVertexCounts[id]; }
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) = 0;
virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) = 0;
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer);
virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer);
virtual void bindBuffers(Gfx::PRenderCommand command) = 0;
virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0;
virtual Gfx::PDescriptorSet getVertexDataSet() = 0;
+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));
}