Skip to content

Commit bfdbcf2

Browse files
committed
clean more code & a small test for sub allocation purposes
1 parent be47096 commit bfdbcf2

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

src/nbl/ext/ImGui/ImGui.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,6 @@ namespace nbl::ext::imgui
636636

637637
ImGui::Render(); // note it doesn't touch GPU or graphics API at all, its an internal ImGUI call to update & prepare the data for rendering so we can call GetDrawData()
638638

639-
struct
640-
{
641-
const uint64_t oldie;
642-
uint64_t newie;
643-
} scratchSemaphoreCounters = { .oldie = info.scratchSemaphore.value, .newie = 0u };
644-
645639
auto* commandBuffer = info.getScratchCommandBuffer();
646640

647641
ImGuiIO& io = ImGui::GetIO();
@@ -760,6 +754,9 @@ namespace nbl::ext::imgui
760754
// retry, second attempt cull frees and execute deferred memory deallocation of offsets no longer in use
761755
unallocatedSize = m_mdi.streamingTDBufferST->multi_allocate(timeout, MDI_ALLOCATION_COUNT, multiAllocParams.offsets.data(), multiAllocParams.byteSizes.data(), MDI_ALIGNMENTS.data());
762756

757+
// still no? then we will try suballocating smaller pieces of memory due to possible fragmentation issues & upstream in nice loop (if we get to a point when suballocation could not cover whole 1 sub draw call then we need to submit overflow & rerecord stuff + repeat)
758+
// that's TODO but first a small test for not tightly packed index + vertex buffer
759+
763760
if (unallocatedSize != 0u)
764761
{
765762
m_creationParams.utilities->getLogger()->log("Could not multi alloc mdi buffer!", system::ILogger::ELL_ERROR);
@@ -778,7 +775,7 @@ namespace nbl::ext::imgui
778775
m_creationParams.utilities->getLogger()->log("[MDI::EBC_INDEX_BUFFERS offset] = \"%s\" bytes", system::ILogger::ELL_ERROR, getOffsetStr(multiAllocParams.offsets[MDI::EBC_INDEX_BUFFERS]).c_str());
779776
m_creationParams.utilities->getLogger()->log("[MDI::EBC_VERTEX_BUFFERS offset] = \"%s\" bytes", system::ILogger::ELL_ERROR, getOffsetStr(multiAllocParams.offsets[MDI::EBC_VERTEX_BUFFERS]).c_str());
780777

781-
exit(0x45); // TODO: handle OOB memory requests, probably need to extend the mdi buffer/let user pass more size at init
778+
exit(0x45);
782779
}
783780
}
784781

@@ -791,12 +788,17 @@ namespace nbl::ext::imgui
791788
auto binding = mdiBuffer->getBoundMemory();
792789
assert(binding.memory->isCurrentlyMapped());
793790

794-
auto* const indirectsMappedPointer = reinterpret_cast<VkDrawIndexedIndirectCommand*>(reinterpret_cast<uint8_t*>(m_mdi.streamingTDBufferST->getBufferPointer()) + multiAllocParams.offsets[MDI::EBC_DRAW_INDIRECT_STRUCTURES]);
795-
auto* const elementsMappedPointer = reinterpret_cast<PerObjectData*>(reinterpret_cast<uint8_t*>(m_mdi.streamingTDBufferST->getBufferPointer()) + multiAllocParams.offsets[MDI::EBC_ELEMENT_STRUCTURES]);
796-
auto* indicesMappedPointer = reinterpret_cast<ImDrawIdx*>(reinterpret_cast<uint8_t*>(m_mdi.streamingTDBufferST->getBufferPointer()) + multiAllocParams.offsets[MDI::EBC_INDEX_BUFFERS]);
797-
auto* verticesMappedPointer = reinterpret_cast<ImDrawVert*>(reinterpret_cast<uint8_t*>(m_mdi.streamingTDBufferST->getBufferPointer()) + multiAllocParams.offsets[MDI::EBC_VERTEX_BUFFERS]);
791+
auto* const mdiData = reinterpret_cast<uint8_t*>(m_mdi.streamingTDBufferST->getBufferPointer());
798792

799-
size_t globalIOffset = {}, globalVOffset = {}, drawID = {};
793+
auto* const indirectsMappedPointer = reinterpret_cast<VkDrawIndexedIndirectCommand*>(mdiData + multiAllocParams.offsets[MDI::EBC_DRAW_INDIRECT_STRUCTURES]);
794+
auto* const elementsMappedPointer = reinterpret_cast<PerObjectData*>(mdiData + multiAllocParams.offsets[MDI::EBC_ELEMENT_STRUCTURES]);
795+
auto* const indicesMappedPointer = reinterpret_cast<ImDrawIdx*>(mdiData + multiAllocParams.offsets[MDI::EBC_INDEX_BUFFERS]);
796+
auto* const verticesMappedPointer = reinterpret_cast<ImDrawVert*>(mdiData + multiAllocParams.offsets[MDI::EBC_VERTEX_BUFFERS]);
797+
798+
const auto indexObjectGlobalOffset = indicesMappedPointer - reinterpret_cast<ImDrawIdx*>(mdiData);
799+
const auto vertexObjectGlobalOffset = verticesMappedPointer - reinterpret_cast<ImDrawVert*>(mdiData);
800+
801+
size_t cmdListIndexOffset = {}, cmdListVertexOffset = {}, drawID = {};
800802

801803
for (int n = 0; n < drawData->CmdListsCount; n++)
802804
{
@@ -811,11 +813,17 @@ namespace nbl::ext::imgui
811813
auto* indirect = indirectsMappedPointer + drawID;
812814
auto* element = elementsMappedPointer + drawID;
813815

814-
indirect->firstIndex = pcmd->IdxOffset + globalIOffset;
815816
indirect->firstInstance = drawID; // use base instance as draw ID
816817
indirect->indexCount = pcmd->ElemCount;
817818
indirect->instanceCount = 1u;
818-
indirect->vertexOffset = pcmd->VtxOffset + globalVOffset;
819+
820+
indirect->firstIndex = pcmd->IdxOffset + cmdListIndexOffset;
821+
indirect->vertexOffset = pcmd->VtxOffset + cmdListVertexOffset;
822+
823+
// TEST: I think this could be illegal in vulkan explaining why I get weird flickering but valid scene still I see (the geometry seems to be OK),
824+
// could try BDA to get vertex + index instead and this is valid for sure
825+
// indirect->firstIndex = indexObjectGlobalOffset + pcmd->IdxOffset + cmdListIndexOffset;
826+
// indirect->vertexOffset = vertexObjectGlobalOffset + pcmd->VtxOffset + cmdListVertexOffset;
819827

820828
const auto scissor = clip.getScissor(clipRectangle);
821829

@@ -837,21 +845,29 @@ namespace nbl::ext::imgui
837845
}
838846

839847
// update mdi's vertex & index buffers
840-
::memcpy(indicesMappedPointer + globalIOffset, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
841-
::memcpy(verticesMappedPointer + globalVOffset, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
848+
::memcpy(indicesMappedPointer + cmdListIndexOffset, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
849+
::memcpy(verticesMappedPointer + cmdListVertexOffset, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
842850

843-
globalIOffset += cmd_list->IdxBuffer.Size;
844-
globalVOffset += cmd_list->VtxBuffer.Size;
851+
cmdListIndexOffset += cmd_list->IdxBuffer.Size;
852+
cmdListVertexOffset += cmd_list->VtxBuffer.Size;
845853
}
854+
855+
// TEST: flush
856+
// const ILogicalDevice::MappedMemoryRange memoryRange(binding.memory, 0ull, binding.memory->getAllocationSize());
857+
// m_creationParams.utilities->getLogicalDevice()->flushMappedMemoryRanges(1u, &memoryRange);
846858
}
847859

848860
auto* rawPipeline = pipeline.get();
849861
commandBuffer->bindGraphicsPipeline(rawPipeline);
850862
commandBuffer->bindDescriptorSets(EPBP_GRAPHICS, rawPipeline->getLayout(), 0, 1, &descriptorSet);
863+
864+
const auto offset = mdiBuffer->getBoundMemory().offset;
851865
{
852866
const asset::SBufferBinding<const video::IGPUBuffer> binding =
853867
{
854868
.offset = multiAllocParams.offsets[MDI::EBC_INDEX_BUFFERS],
869+
// TEST: start of MDI buffer
870+
// .offset = offset, // 0u
855871
.buffer = smart_refctd_ptr(mdiBuffer)
856872
};
857873

@@ -866,6 +882,8 @@ namespace nbl::ext::imgui
866882
const asset::SBufferBinding<const video::IGPUBuffer> bindings[] =
867883
{{
868884
.offset = multiAllocParams.offsets[MDI::EBC_VERTEX_BUFFERS],
885+
// TEST: start of MDI buffer
886+
// .offset = offset, // 0u
869887
.buffer = smart_refctd_ptr(mdiBuffer)
870888
}};
871889

@@ -922,17 +940,6 @@ namespace nbl::ext::imgui
922940
};
923941

924942
commandBuffer->drawIndexedIndirect(binding, drawCount, sizeof(VkDrawIndexedIndirectCommand));
925-
926-
scratchSemaphoreCounters.newie = info.scratchSemaphore.value;
927-
928-
if (scratchSemaphoreCounters.newie != scratchSemaphoreCounters.oldie)
929-
{
930-
const auto overflows = scratchSemaphoreCounters.newie - scratchSemaphoreCounters.oldie;
931-
m_creationParams.utilities->getLogger()->log("%d overflows when rendering UI!\n", nbl::system::ILogger::ELL_PERFORMANCE, overflows);
932-
933-
// TODO: handle them?
934-
exit(0x45);
935-
}
936943
}
937944

938945
return true;

0 commit comments

Comments
 (0)