perf(window): cache per-frame heap-bind structs across frames
Render() rebuilt two VkBindHeapInfoEXT structs every frame and recomputed reservedRangeOffset via a runtime subtract + bitmask against Device::descriptorHeapProperties. The heap address/size per slot are stable for a given heap; only currentBuffer varies. Precompute numFrames resource/sampler bind structs and cache them, keyed on the descriptorHeap pointer. The cache rebuilds whenever the heap is (re)assigned and otherwise just indexes by currentBuffer. Pointer-keyed invalidation catches any heap reassignment — the same point a setter hook would fire — and is independent of onResize, as the heaps never resize today (per the issue's correctness caveat). Resolves #42 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
050a44d118
commit
8b95b7e883
2 changed files with 41 additions and 21 deletions
|
|
@ -734,27 +734,35 @@ void Window::Render() {
|
||||||
// Pass-side dispatches still run with the same heaps bound — moving
|
// Pass-side dispatches still run with the same heaps bound — moving
|
||||||
// the bind earlier doesn't change anything for them.
|
// the bind earlier doesn't change anything for them.
|
||||||
if (descriptorHeap) {
|
if (descriptorHeap) {
|
||||||
VkBindHeapInfoEXT resourceHeapInfo = {
|
// Rebuild the cached per-frame bind structs only when the heap changes.
|
||||||
.sType = VK_STRUCTURE_TYPE_BIND_HEAP_INFO_EXT,
|
// The address/size of each frame's heap and the reservedRangeOffset
|
||||||
.heapRange = {
|
// bitmask are stable for a given heap, so this runs once per assignment
|
||||||
.address = descriptorHeap->resourceHeap[currentBuffer].address,
|
// rather than every frame.
|
||||||
.size = static_cast<std::uint32_t>(descriptorHeap->resourceHeap[currentBuffer].size)
|
if (descriptorHeap != cachedDescriptorHeap) {
|
||||||
},
|
for (std::uint8_t i = 0; i < numFrames; ++i) {
|
||||||
.reservedRangeOffset = (descriptorHeap->resourceHeap[currentBuffer].size - Device::descriptorHeapProperties.minResourceHeapReservedRange) & ~(Device::descriptorHeapProperties.imageDescriptorAlignment - 1),
|
resourceHeapInfos[i] = {
|
||||||
.reservedRangeSize = Device::descriptorHeapProperties.minResourceHeapReservedRange
|
.sType = VK_STRUCTURE_TYPE_BIND_HEAP_INFO_EXT,
|
||||||
};
|
.heapRange = {
|
||||||
Device::vkCmdBindResourceHeapEXT(drawCmdBuffers[currentBuffer], &resourceHeapInfo);
|
.address = descriptorHeap->resourceHeap[i].address,
|
||||||
|
.size = static_cast<std::uint32_t>(descriptorHeap->resourceHeap[i].size)
|
||||||
VkBindHeapInfoEXT samplerHeapInfo = {
|
},
|
||||||
.sType = VK_STRUCTURE_TYPE_BIND_HEAP_INFO_EXT,
|
.reservedRangeOffset = (descriptorHeap->resourceHeap[i].size - Device::descriptorHeapProperties.minResourceHeapReservedRange) & ~(Device::descriptorHeapProperties.imageDescriptorAlignment - 1),
|
||||||
.heapRange = {
|
.reservedRangeSize = Device::descriptorHeapProperties.minResourceHeapReservedRange
|
||||||
.address = descriptorHeap->samplerHeap[currentBuffer].address,
|
};
|
||||||
.size = static_cast<std::uint32_t>(descriptorHeap->samplerHeap[currentBuffer].size)
|
samplerHeapInfos[i] = {
|
||||||
},
|
.sType = VK_STRUCTURE_TYPE_BIND_HEAP_INFO_EXT,
|
||||||
.reservedRangeOffset = descriptorHeap->samplerHeap[currentBuffer].size - Device::descriptorHeapProperties.minSamplerHeapReservedRange,
|
.heapRange = {
|
||||||
.reservedRangeSize = Device::descriptorHeapProperties.minSamplerHeapReservedRange
|
.address = descriptorHeap->samplerHeap[i].address,
|
||||||
};
|
.size = static_cast<std::uint32_t>(descriptorHeap->samplerHeap[i].size)
|
||||||
Device::vkCmdBindSamplerHeapEXT(drawCmdBuffers[currentBuffer], &samplerHeapInfo);
|
},
|
||||||
|
.reservedRangeOffset = descriptorHeap->samplerHeap[i].size - Device::descriptorHeapProperties.minSamplerHeapReservedRange,
|
||||||
|
.reservedRangeSize = Device::descriptorHeapProperties.minSamplerHeapReservedRange
|
||||||
|
};
|
||||||
|
}
|
||||||
|
cachedDescriptorHeap = descriptorHeap;
|
||||||
|
}
|
||||||
|
Device::vkCmdBindResourceHeapEXT(drawCmdBuffers[currentBuffer], &resourceHeapInfos[currentBuffer]);
|
||||||
|
Device::vkCmdBindSamplerHeapEXT(drawCmdBuffers[currentBuffer], &samplerHeapInfos[currentBuffer]);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdate.Invoke({startTime, startTime-lastFrameBegin});
|
onUpdate.Invoke({startTime, startTime-lastFrameBegin});
|
||||||
|
|
|
||||||
|
|
@ -264,6 +264,18 @@ export namespace Crafter {
|
||||||
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
std::vector<RenderPass*> passes;
|
std::vector<RenderPass*> passes;
|
||||||
DescriptorHeapVulkan* descriptorHeap = nullptr;
|
DescriptorHeapVulkan* descriptorHeap = nullptr;
|
||||||
|
// Cached per-frame heap-bind structs. The heap address/size per slot
|
||||||
|
// are stable once a heap is assigned; only currentBuffer varies frame
|
||||||
|
// to frame, so the structs (and the reservedRangeOffset arithmetic)
|
||||||
|
// are computed once per heap rather than every frame in Render().
|
||||||
|
// Keyed on the heap pointer: whenever descriptorHeap differs from
|
||||||
|
// cachedDescriptorHeap the cache is rebuilt. This invalidates on any
|
||||||
|
// heap (re)assignment — heaps never resize today, so a stable pointer
|
||||||
|
// means stable ranges. Not tied to onResize (the heap is independent
|
||||||
|
// of swapchain size).
|
||||||
|
VkBindHeapInfoEXT resourceHeapInfos[numFrames];
|
||||||
|
VkBindHeapInfoEXT samplerHeapInfos[numFrames];
|
||||||
|
DescriptorHeapVulkan* cachedDescriptorHeap = nullptr;
|
||||||
std::optional<std::array<float, 4>> clearColor;
|
std::optional<std::array<float, 4>> clearColor;
|
||||||
#else
|
#else
|
||||||
// DOM mode: the page IS the window. WebGPU device and canvas are
|
// DOM mode: the page IS the window. WebGPU device and canvas are
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue