Skip to content

Commit

Permalink
Fix double delete on command buffer in texture loader and give comman…
Browse files Browse the repository at this point in the history
…d pools names
  • Loading branch information
Duttenheim committed Nov 22, 2024
1 parent b8032ca commit a7c0130
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
15 changes: 9 additions & 6 deletions code/render/coregraphics/textureloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,17 @@ TextureLoader::TextureLoader()
this->streamerThreadName = "Texture Streamer Thread";

CoreGraphics::CmdBufferPoolCreateInfo cmdPoolInfo;
cmdPoolInfo.name = "Async Transfer Commandbuffer Pool";
cmdPoolInfo.queue = CoreGraphics::QueueType::TransferQueueType;
cmdPoolInfo.resetable = false;
cmdPoolInfo.shortlived = true;
this->asyncTransferPool = CoreGraphics::CreateCmdBufferPool(cmdPoolInfo);
cmdPoolInfo.name = "Immediate Transfer Commandbuffer Pool";
this->immediateTransferPool = CoreGraphics::CreateCmdBufferPool(cmdPoolInfo);
cmdPoolInfo.queue = CoreGraphics::QueueType::GraphicsQueueType;
cmdPoolInfo.name = "Async Handover Commandbuffer Pool";
this->asyncHandoverPool = CoreGraphics::CreateCmdBufferPool(cmdPoolInfo);
cmdPoolInfo.name = "Immediate Handover Commandbuffer Pool";
this->immediateHandoverPool = CoreGraphics::CreateCmdBufferPool(cmdPoolInfo);
}

Expand Down Expand Up @@ -329,18 +333,15 @@ TextureLoader::StreamResource(const ResourceLoadJob& job)

CoreGraphics::CmdEndMarker(handoverCommands);
CoreGraphics::CmdEndRecord(handoverCommands);
CoreGraphics::CmdBufferIdRelease(handoverCommands);

CoreGraphics::CmdEndMarker(uploadCommands);
CoreGraphics::CmdEndRecord(uploadCommands);
CoreGraphics::CmdBufferIdRelease(uploadCommands);

if (job.immediate)
{
CoreGraphics::FlushUploads(rangesToFlush);
CoreGraphics::SubmissionWaitEvent transferWait = CoreGraphics::SubmitCommandBuffers({ uploadCommands }, CoreGraphics::TransferQueueType, nullptr, "Texture mip upload");
CoreGraphics::SubmissionWaitEvent graphicsWait = CoreGraphics::SubmitCommandBuffers({ handoverCommands }, CoreGraphics::GraphicsQueueType, { transferWait }, "Receive texture");
CoreGraphics::DeferredDestroyCmdBuffer(uploadCommands);

IndexT index = this->mipHandovers.FindIndex(job.id);
if (index == InvalidIndex)
Expand All @@ -353,13 +354,15 @@ TextureLoader::StreamResource(const ResourceLoadJob& job)
// If job is async, add to submit queue
this->mipLoadsToSubmit.Enqueue(MipLoadMainThread{ .id = job.id, .bits = mask, .rangesToFlush = rangesToFlush, .transferCmdBuf = uploadCommands, .graphicsCmdBuf = handoverCommands });
}
CoreGraphics::CmdBufferIdRelease(handoverCommands);
CoreGraphics::CmdBufferIdRelease(uploadCommands);
}
else
{
CoreGraphics::CmdEndMarker(uploadCommands);
CoreGraphics::CmdEndRecord(uploadCommands);
CoreGraphics::CmdBufferIdRelease(uploadCommands);
CoreGraphics::DestroyCmdBuffer(uploadCommands);
CoreGraphics::DeferredDestroyCmdBuffer(uploadCommands);
}
}
if (job.loadState.pendingBits != 0x0)
Expand All @@ -377,8 +380,8 @@ TextureLoader::StreamResource(const ResourceLoadJob& job)
{
// First, delete the initial buffer
CoreGraphics::FreeUploads(handover.rangesToFree);
CoreGraphics::DestroyCmdBuffer(handover.uploadBuffer);
CoreGraphics::DestroyCmdBuffer(handover.receiveBuffer);
CoreGraphics::DeferredDestroyCmdBuffer(handover.uploadBuffer);
CoreGraphics::DeferredDestroyCmdBuffer(handover.receiveBuffer);

loadedBits |= handover.bits;
pendingBits &= ~handover.bits;
Expand Down
12 changes: 11 additions & 1 deletion code/render/coregraphics/vk/vkcommandbuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,21 @@ CreateCmdBufferPool(const CmdBufferPoolCreateInfo& info)
queueFamily
};
VkDevice dev = Vulkan::GetCurrentDevice();
VkResult res = vkCreateCommandPool(dev, &cmdPoolInfo, nullptr, &commandBufferPools.Get<CommandBufferPool_VkCommandPool>(id));
VkCommandPool commandPool;
VkResult res = vkCreateCommandPool(dev, &cmdPoolInfo, nullptr, &commandPool);
commandBufferPools.Set<CommandBufferPool_VkDevice>(id, dev);
n_assert(res == VK_SUCCESS);

commandBufferPools.Set<CommandBufferPool_VkCommandPool>(id, commandPool);

CmdBufferPoolId ret = id;


#if NEBULA_GRAPHICS_DEBUG
ObjectSetName(ret, info.name);
#endif


return ret;
}

Expand Down
41 changes: 40 additions & 1 deletion code/render/coregraphics/vk/vkgraphicsdevice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include "coregraphics/graphicsdevice.h"
#include "profiling/profiling.h"


#ifdef __WIN32__

#include "debug/win32/win32stacktrace.h"
#endif

#include "threading/criticalsection.h"

namespace Vulkan
Expand Down Expand Up @@ -333,7 +339,7 @@ SetupAdapter(CoreGraphics::GraphicsDeviceCreateInfo::Features features)

if (validDevice)
{
n_printf("[Graphics Device] Using '%s' as primary graphics adapter", state.deviceProps[i].properties.deviceName);
n_printf("[Graphics Device] Using '%s' as primary graphics adapter\n", state.deviceProps[i].properties.deviceName);
state.currentDevice = i;
CoreGraphics::ReadWriteBufferAlignment = state.deviceProps[i].properties.limits.minStorageBufferOffsetAlignment;
CoreGraphics::ConstantBufferAlignment = state.deviceProps[i].properties.limits.minUniformBufferOffsetAlignment;
Expand Down Expand Up @@ -548,6 +554,7 @@ SparseBufferBind(const VkBuffer buf, const Util::Array<VkSparseMemoryBind>& bind
void
ClearPending()
{
Threading::CriticalScope scope(&delayedDeleteSection);
GraphicsDeviceState::PendingDeletes& pendingDeletes = state.pendingDeletes[state.currentBufferedFrameIndex];

// Clear up any pending deletes
Expand Down Expand Up @@ -707,6 +714,17 @@ NebulaVulkanErrorDebugCallback(
const VkDebugUtilsMessengerCallbackDataEXT* callbackData,
void* userData)
{
#if __WIN32__
Util::Array<Util::String> stacktrace = Win32::Win32StackTrace::GenerateStackTrace();
Util::String format;
// remove the first 7 entries as they are only the assert/error functions and the last 6 as they are windows startup
for (int i = 6; i < Math::min(17,stacktrace.Size() - 6); i++)
{
format.Append(stacktrace[i]);
//format.Append("\n");
}
#endif

n_warning("%s\n", callbackData->pMessage);
return VK_FALSE;
}
Expand Down Expand Up @@ -2608,6 +2626,27 @@ ObjectSetName(const CoreGraphics::CmdBufferId id, const char* name)
n_assert(res == VK_SUCCESS);
}

//------------------------------------------------------------------------------
/**
*/
template<>
void
ObjectSetName(const CoreGraphics::CmdBufferPoolId id, const char* name)
{
VkDebugUtilsObjectNameInfoEXT info =
{
VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
nullptr,
VK_OBJECT_TYPE_COMMAND_POOL,
(uint64_t)Vulkan::CmdBufferPoolGetVk(id),
name
};
VkDevice dev = GetCurrentDevice();
VkResult res = VkDebugObjectName(dev, &info);
n_assert(res == VK_SUCCESS);
}


//------------------------------------------------------------------------------
/**
*/
Expand Down
3 changes: 2 additions & 1 deletion code/render/coregraphics/vk/vkswapchain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ CreateSwapchain(const SwapchainCreateInfo& info)
CoreGraphics::UnlockGraphicsSetupCommandBuffer(cmdBuf);

CoreGraphics::CmdBufferPoolCreateInfo poolInfo;
poolInfo.name = "Swap Commandbuffer Pool";
poolInfo.shortlived = true;
poolInfo.queue = queueType;
poolInfo.resetable = false;
Expand Down Expand Up @@ -327,7 +328,7 @@ SwapchainCopy(const SwapchainId id, const CoreGraphics::CmdBufferId cmdBuf, cons
const Util::Array<VkImage>& images = swapchainAllocator.Get<Swapchain_Images>(id.id);
const DisplayMode& displayMode = swapchainAllocator.Get<Swapchain_DisplayMode>(id.id);
VkImageCopy copy;

copy.srcOffset = { 0, 0, 0 };
copy.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
copy.dstOffset = { 0, 0, 0 };
Expand Down
28 changes: 27 additions & 1 deletion code/render/frame/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,33 @@
]
}
},

{
"pass": {
"name": "ImGui",
"attachments": [
{
"name": "ColorBuffer",
"flags": "Load|Store"
}
],
"subpasses": [
{
"name": "ImGui Pass",
"subpass_dependencies": [],
"attachments": [
"ColorBuffer"
],
"ops": [
{
"subgraph": {
"name": "ImGUI"
}
}
]
}
]
}
},
{
"copy": {
"name": "ColorBuffer Copy",
Expand Down
9 changes: 8 additions & 1 deletion fips-files/generators/framescriptc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,14 @@ def FormatSetup(self, file):
file.WriteLine("CoreGraphics::DestroyCmdBufferPool(CmdPool_{});".format(self.name))
file.DecreaseIndent()
file.WriteLine("}")
file.WriteLine("CmdPool_{} = CoreGraphics::CreateCmdBufferPool({{ .queue = CoreGraphics::QueueType::{}QueueType, .resetable = false, .shortlived = true }});".format(self.name, self.queue))
file.WriteLine("CoreGraphics::CmdBufferPoolCreateInfo CmdPoolInfo_{};".format(self.name))
file.WriteLine("CmdPoolInfo_{}.queue = CoreGraphics::QueueType::{}QueueType;".format(self.name, self.queue))
file.WriteLine("CmdPoolInfo_{}.resetable = false;".format(self.name))
file.WriteLine("CmdPoolInfo_{}.shortlived = true;".format(self.name))
file.WriteLine("#if NEBULA_GRAPHICS_DEBUG")
file.WriteLine('CmdPoolInfo_{}.name = "{}";'.format(self.name, self.name))
file.WriteLine("#endif // NEBULA_GRAPHICS_DEBUG")
file.WriteLine("CmdPool_{} = CoreGraphics::CreateCmdBufferPool(CmdPoolInfo_{});".format(self.name, self.name))
for op in self.ops:
op.FormatSetup(file)

Expand Down

0 comments on commit a7c0130

Please sign in to comment.