Skip to content

Commit

Permalink
Ctrl + Alt + N
Browse files Browse the repository at this point in the history
- IndirectInstancer#uploadInstances: 46% of render thread to 26%
- Inline #enqueueCopy to avoid allocating LongConsumers
- Do not even bother to track individual changed indices, instead rely
  on just the changedPage set
  • Loading branch information
Jozufozu committed Sep 16, 2024
1 parent c658b2b commit e1b594a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public void notifyDirty(int index) {
if (index < 0 || index >= instanceCount()) {
return;
}
changed.set(index);
changedPages.set(ObjectStorage.objectIndex2PageIndex(index));
}

Expand Down Expand Up @@ -102,15 +101,34 @@ public void uploadInstances(StagingBuffer stagingBuffer, int instanceVbo) {
long baseByte = mapping.page2ByteOffset(page);
long size = (endObject - startObject) * instanceStride;

stagingBuffer.enqueueCopy(size, instanceVbo, baseByte, ptr -> {
// Because writes are broken into pages, we end up with significantly more calls into
// StagingBuffer#enqueueCopy and the allocations for the writer got out of hand. Here
// we've inlined the enqueueCopy call and do not allocate the write lambda at all.
// Doing so cut upload times in half.

// Try to write directly into the staging buffer if there is enough contiguous space.
long direct = stagingBuffer.reserveForCopy(size, instanceVbo, baseByte);

if (direct != MemoryUtil.NULL) {
for (int i = startObject; i < endObject; i++) {
writer.write(ptr, instances.get(i));
ptr += instanceStride;
var instance = instances.get(i);
writer.write(direct, instance);
direct += instanceStride;
}
});
continue;
}

// Otherwise, write to a scratch buffer and enqueue a copy.
var block = stagingBuffer.getScratch(size);
var ptr = block.ptr();
for (int i = startObject; i < endObject; i++) {
var instance = instances.get(i);
writer.write(ptr, instance);
ptr += instanceStride;
}
stagingBuffer.enqueueCopy(block.ptr(), size, instanceVbo, baseByte);
}

changed.clear();
changedPages.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void delete() {
FlwMemoryTracker._freeCpuMemory(capacity);
}

private MemoryBlock getScratch(long size) {
public MemoryBlock getScratch(long size) {
if (scratch == null) {
scratch = MemoryBlock.malloc(size);
} else if (scratch.size() < size) {
Expand Down

0 comments on commit e1b594a

Please sign in to comment.