Skip to content

Commit

Permalink
The hardest problem
Browse files Browse the repository at this point in the history
- Rename most InstancePager terminology
- Rename MODEL_INDEX buffer stuffs
  • Loading branch information
Jozufozu committed Sep 3, 2024
1 parent e83a308 commit 2537584
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public final class BufferBindings {
public static final int INSTANCE = 0;
public static final int TARGET = 1;
public static final int MODEL_INDEX = 2;
public static final int PAGE_FRAME_DESCRIPTOR = 2;
public static final int MODEL = 3;
public static final int DRAW = 4;
public static final int LIGHT_LUT = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public class IndirectBuffers {
// Offsets to the vbos
private static final long INSTANCE_HANDLE_OFFSET = HANDLE_OFFSET;
private static final long TARGET_HANDLE_OFFSET = INT_SIZE;
private static final long MODEL_INDEX_HANDLE_OFFSET = INT_SIZE * 2;
private static final long PAGE_FRAME_DESCRIPTOR_HANDLE_OFFSET = INT_SIZE * 2;
private static final long MODEL_HANDLE_OFFSET = INT_SIZE * 3;
private static final long DRAW_HANDLE_OFFSET = INT_SIZE * 4;

// Offsets to the sizes
private static final long INSTANCE_SIZE_OFFSET = SIZE_OFFSET;
private static final long TARGET_SIZE_OFFSET = SIZE_OFFSET + PTR_SIZE;
private static final long MODEL_INDEX_SIZE_OFFSET = SIZE_OFFSET + PTR_SIZE * 2;
private static final long PAGE_FRAME_DESCRIPTOR_SIZE_OFFSET = SIZE_OFFSET + PTR_SIZE * 2;
private static final long MODEL_SIZE_OFFSET = SIZE_OFFSET + PTR_SIZE * 3;
private static final long DRAW_SIZE_OFFSET = SIZE_OFFSET + PTR_SIZE * 4;

Expand All @@ -62,7 +62,7 @@ public class IndirectBuffers {
private final MemoryBlock multiBindBlock;
private final long instanceStride;

public final InstancePager pageFile;
public final ObjectStorage objectStorage;
public final ResizableStorageArray target;
public final ResizableStorageArray model;
public final ResizableStorageArray draw;
Expand All @@ -71,7 +71,7 @@ public class IndirectBuffers {
this.instanceStride = instanceStride;
this.multiBindBlock = MemoryBlock.calloc(BUFFERS_SIZE_BYTES, 1);

pageFile = new InstancePager(instanceStride);
objectStorage = new ObjectStorage(instanceStride);
target = new ResizableStorageArray(INT_SIZE, INSTANCE_GROWTH_FACTOR);
model = new ResizableStorageArray(MODEL_STRIDE, MODEL_GROWTH_FACTOR);
draw = new ResizableStorageArray(DRAW_COMMAND_STRIDE, DRAW_GROWTH_FACTOR);
Expand All @@ -83,15 +83,15 @@ void updateCounts(int instanceCount, int modelCount, int drawCount) {
draw.ensureCapacity(drawCount);

final long ptr = multiBindBlock.ptr();
MemoryUtil.memPutInt(ptr + INSTANCE_HANDLE_OFFSET, pageFile.objects.handle());
MemoryUtil.memPutInt(ptr + INSTANCE_HANDLE_OFFSET, objectStorage.objectBuffer.handle());
MemoryUtil.memPutInt(ptr + TARGET_HANDLE_OFFSET, target.handle());
MemoryUtil.memPutInt(ptr + MODEL_INDEX_HANDLE_OFFSET, pageFile.pageTable.handle());
MemoryUtil.memPutInt(ptr + PAGE_FRAME_DESCRIPTOR_HANDLE_OFFSET, objectStorage.frameDescriptorBuffer.handle());
MemoryUtil.memPutInt(ptr + MODEL_HANDLE_OFFSET, model.handle());
MemoryUtil.memPutInt(ptr + DRAW_HANDLE_OFFSET, draw.handle());

MemoryUtil.memPutAddress(ptr + INSTANCE_SIZE_OFFSET, pageFile.objects.capacity());
MemoryUtil.memPutAddress(ptr + INSTANCE_SIZE_OFFSET, objectStorage.objectBuffer.capacity());
MemoryUtil.memPutAddress(ptr + TARGET_SIZE_OFFSET, INT_SIZE * instanceCount);
MemoryUtil.memPutAddress(ptr + MODEL_INDEX_SIZE_OFFSET, pageFile.pageTable.capacity());
MemoryUtil.memPutAddress(ptr + PAGE_FRAME_DESCRIPTOR_SIZE_OFFSET, objectStorage.frameDescriptorBuffer.capacity());
MemoryUtil.memPutAddress(ptr + MODEL_SIZE_OFFSET, MODEL_STRIDE * modelCount);
MemoryUtil.memPutAddress(ptr + DRAW_SIZE_OFFSET, DRAW_COMMAND_STRIDE * drawCount);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public void bindForCrumbling() {
public void delete() {
multiBindBlock.free();

pageFile.delete();
objectStorage.delete();
target.delete();
model.delete();
draw.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void upload(StagingBuffer stagingBuffer) {
// Upload only instances that have changed.
uploadInstances(stagingBuffer);

buffers.pageFile.uploadTable(stagingBuffer);
buffers.objectStorage.uploadDescriptors(stagingBuffer);

// We need to upload the models every frame to reset the instance count.
uploadModels(stagingBuffer);
Expand All @@ -119,7 +119,7 @@ public void dispatchCull() {
cullProgram.bind();

buffers.bindForCompute();
glDispatchCompute(buffers.pageFile.capacity(), 1, 1);
glDispatchCompute(buffers.objectStorage.capacity(), 1, 1);
}

public void dispatchApply() {
Expand Down Expand Up @@ -172,7 +172,7 @@ public boolean hasVisualType(VisualType visualType) {
}

public void add(IndirectInstancer<I> instancer, InstancerKey<I> key, MeshPool meshPool) {
instancer.pageFile = buffers.pageFile.createAllocation();
instancer.mapping = buffers.objectStorage.createMapping();
instancer.postUpdate(instancers.size(), -1);

instancers.add(instancer);
Expand Down Expand Up @@ -245,7 +245,7 @@ private void drawBarrier() {

private void uploadInstances(StagingBuffer stagingBuffer) {
for (var instancer : instancers) {
instancer.uploadInstances(stagingBuffer, buffers.pageFile.objects.handle());
instancer.uploadInstances(stagingBuffer, buffers.objectStorage.objectBuffer.handle());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.UnknownNullability;
import org.joml.Vector4fc;
import org.lwjgl.system.MemoryUtil;

Expand All @@ -23,7 +24,7 @@ public class IndirectInstancer<I extends Instance> extends AbstractInstancer<I>

private final AtomicBitSet changedPages = new AtomicBitSet();

public InstancePager.Allocation pageFile;
public ObjectStorage.@UnknownNullability Mapping mapping;

private int modelIndex = -1;
private int baseInstance = -1;
Expand All @@ -42,14 +43,14 @@ public void notifyDirty(int index) {
return;
}
changed.set(index);
changedPages.set(InstancePager.object2Page(index));
changedPages.set(ObjectStorage.objectIndex2PageIndex(index));
}

@Override
protected void setRangeChanged(int start, int end) {
super.setRangeChanged(start, end);

changedPages.set(InstancePager.object2Page(start), InstancePager.object2Page(end) + 1);
changedPages.set(ObjectStorage.objectIndex2PageIndex(start), ObjectStorage.objectIndex2PageIndex(end) + 1);
}

public void addDraw(IndirectDraw draw) {
Expand All @@ -67,7 +68,7 @@ public void update() {
public void postUpdate(int modelIndex, int baseInstance) {
this.modelIndex = modelIndex;
this.baseInstance = baseInstance;
pageFile.update(modelIndex, instanceCount());
mapping.update(modelIndex, instanceCount());
}

public void writeModel(long ptr) {
Expand All @@ -81,20 +82,20 @@ public void writeModel(long ptr) {
}

public void uploadInstances(StagingBuffer stagingBuffer, int instanceVbo) {
int numPages = pageFile.pageCount();
int numPages = mapping.pageCount();

var instanceCount = instances.size();

for (int page = changedPages.nextSetBit(0); page >= 0 && page < numPages; page = changedPages.nextSetBit(page + 1)) {
int startObject = InstancePager.page2Object(page);
int startObject = ObjectStorage.pageIndex2ObjectIndex(page);

if (startObject >= instanceCount) {
break;
}

int endObject = Math.min(instanceCount, InstancePager.page2Object(page + 1));
int endObject = Math.min(instanceCount, ObjectStorage.pageIndex2ObjectIndex(page + 1));

long baseByte = pageFile.page2ByteOffset(page);
long baseByte = mapping.page2ByteOffset(page);
long size = (endObject - startObject) * instanceStride;

stagingBuffer.enqueueCopy(size, instanceVbo, baseByte, ptr -> {
Expand All @@ -115,7 +116,7 @@ public void delete() {
draw.delete();
}

pageFile.delete();
mapping.delete();
}

public int modelIndex() {
Expand Down
Loading

0 comments on commit 2537584

Please sign in to comment.