Skip to content

Commit ea64305

Browse files
committed
Revert "add arena content caching", prune diffs
1 parent cfe09fc commit ea64305

9 files changed

Lines changed: 12 additions & 101 deletions

File tree

common/src/main/java/net/caffeinemc/mods/sodium/client/gl/arena/GlBufferArena.java

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package net.caffeinemc.mods.sodium.client.gl.arena;
22

3-
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
43
import net.caffeinemc.mods.sodium.client.gl.arena.staging.StagingBuffer;
54
import net.caffeinemc.mods.sodium.client.gl.buffer.GlBuffer;
65
import net.caffeinemc.mods.sodium.client.gl.buffer.GlBufferUsage;
76
import net.caffeinemc.mods.sodium.client.gl.buffer.GlMutableBuffer;
87
import net.caffeinemc.mods.sodium.client.gl.device.CommandList;
9-
import net.jpountz.xxhash.XXHash64;
10-
import net.jpountz.xxhash.XXHashFactory;
118

129
import java.nio.ByteBuffer;
1310
import java.util.ArrayList;
@@ -29,17 +26,12 @@ public class GlBufferArena {
2926

3027
private GlBufferSegment head;
3128

32-
private static final XXHash64 NATIVE_HASH = XXHashFactory.fastestInstance().hash64();
33-
private static final XXHash64 JAVA_HASH = XXHashFactory.fastestJavaInstance().hash64();
34-
private static final int NATIVE_HASH_BYTES_THRESHOLD = 512; // TODO: tune this?
35-
private final Long2ReferenceOpenHashMap<GlBufferSegment> cache;
36-
3729
private long capacity;
3830
private long used;
3931

4032
private final int stride;
4133

42-
public GlBufferArena(CommandList commands, int initialCapacity, int stride, StagingBuffer stagingBuffer, boolean enableCache) {
34+
public GlBufferArena(CommandList commands, int initialCapacity, int stride, StagingBuffer stagingBuffer) {
4335
this.capacity = initialCapacity;
4436
this.resizeIncrement = initialCapacity / 16;
4537

@@ -52,12 +44,6 @@ public GlBufferArena(CommandList commands, int initialCapacity, int stride, Stag
5244
commands.allocateStorage(this.arenaBuffer, this.capacity * stride, BUFFER_USAGE);
5345

5446
this.stagingBuffer = stagingBuffer;
55-
56-
if (enableCache) {
57-
this.cache = new Long2ReferenceOpenHashMap<>();
58-
} else {
59-
this.cache = null;
60-
}
6147
}
6248

6349
private void resize(CommandList commandList, long newCapacity) {
@@ -240,10 +226,6 @@ public void free(GlBufferSegment entry) {
240226
throw new IllegalStateException("Already freed");
241227
}
242228

243-
if (entry.isHashed()) {
244-
this.cache.remove(entry.getHash());
245-
}
246-
247229
entry.setFree(true);
248230

249231
this.used -= entry.getLength();
@@ -283,7 +265,7 @@ public boolean upload(CommandList commandList, Stream<PendingUpload> stream) {
283265
// A linked list is used as we'll be randomly removing elements and want O(1) performance
284266
List<PendingUpload> queue = stream.collect(Collectors.toCollection(LinkedList::new));
285267

286-
// Try to upload all the data into free segments first
268+
// Try to upload all of the data into free segments first
287269
this.tryUploads(commandList, queue);
288270

289271
// If we weren't able to upload some buffers, they will have been left behind in the queue
@@ -315,46 +297,18 @@ private void tryUploads(CommandList commandList, List<PendingUpload> queue) {
315297
this.stagingBuffer.flush(commandList);
316298
}
317299

318-
private long getBufferHash(ByteBuffer data) {
319-
var seed = System.identityHashCode(this);
320-
var length = data.remaining();
321-
if (length < NATIVE_HASH_BYTES_THRESHOLD) {
322-
return JAVA_HASH.hash(data, 0, length, seed);
323-
} else {
324-
return NATIVE_HASH.hash(data, 0, length, seed);
325-
}
326-
}
327-
328300
private boolean tryUpload(CommandList commandList, PendingUpload upload) {
329-
ByteBuffer data = upload.getDataBuffer().getDirectBuffer();
301+
ByteBuffer data = upload.getDataBuffer()
302+
.getDirectBuffer();
330303

331304
int elementCount = data.remaining() / this.stride;
332305

333-
// return a buffer segment with the same content if there is one based on the hash of the incoming content
334-
GlBufferSegment matchingSegment = null;
335-
long hash = 0;
336-
if (this.cache != null) {
337-
hash = this.getBufferHash(data);
338-
matchingSegment = this.cache.get(hash);
339-
}
340-
if (matchingSegment != null) {
341-
upload.setResult(matchingSegment);
342-
matchingSegment.addRef();
343-
return true;
344-
}
345-
346306
GlBufferSegment dst = this.alloc(elementCount);
347307

348308
if (dst == null) {
349309
return false;
350310
}
351311

352-
// if a new segment was needed (cache miss), set the calculated hash on the segment
353-
if (this.cache != null) {
354-
dst.setHash(hash);
355-
this.cache.put(hash, dst);
356-
}
357-
358312
// Copy the data into our staging buffer, then copy it into the arena's buffer
359313
this.stagingBuffer.enqueueCopy(commandList, data, this.arenaBuffer, dst.getOffset() * this.stride);
360314

common/src/main/java/net/caffeinemc/mods/sodium/client/gl/arena/GlBufferSegment.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ public class GlBufferSegment {
66
private final GlBufferArena arena;
77

88
private boolean free = false;
9-
private int refCount = 1;
10-
private long hash;
11-
private boolean isHashed = false;
129

1310
private int offset; /* Uint32 */
1411
private int length; /* Uint32 */
@@ -45,33 +42,8 @@ protected void setLength(long length /* Uint32 */) {
4542
this.length = UInt32.downcast(length);
4643
}
4744

48-
public void setHash(long hash) {
49-
this.hash = hash;
50-
this.isHashed = true;
51-
}
52-
53-
public long getHash() {
54-
return this.hash;
55-
}
56-
57-
public boolean isHashed() {
58-
return this.isHashed;
59-
}
60-
61-
public void addRef() {
62-
if (this.isFree()) {
63-
throw new IllegalStateException("Cannot add ref to free segment");
64-
}
65-
this.refCount++;
66-
}
67-
6845
protected void setFree(boolean free) {
6946
this.free = free;
70-
if (this.free) {
71-
this.refCount = 0;
72-
} else {
73-
this.refCount = Math.max(this.refCount, 1);
74-
}
7547
}
7648

7749
protected boolean isFree() {
@@ -95,11 +67,7 @@ protected void setPrev(GlBufferSegment prev) {
9567
}
9668

9769
public void delete() {
98-
// only actually free if there's no more users
99-
if (--this.refCount == 0) {
100-
this.arena.free(this);
101-
this.isHashed = false;
102-
}
70+
this.arena.free(this);
10371
}
10472

10573
protected void mergeInto(GlBufferSegment entry) {

common/src/main/java/net/caffeinemc/mods/sodium/client/gl/device/MultiDrawBatch.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public boolean isEmpty() {
5353
public int getIndexBufferSize() {
5454
int elements = 0;
5555

56-
// since there's command combining, all facings might be rendered at the same time with a single command which requires a bigger index buffer
5756
for (var index = 0; index < this.size; index++) {
5857
elements = Math.max(elements, MemoryUtil.memGetInt(this.pElementCount + ((long) index * Integer.BYTES)));
5958
}

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/DefaultChunkRenderer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.caffeinemc.mods.sodium.client.render.chunk;
22

33
import net.caffeinemc.mods.sodium.client.SodiumClientMod;
4-
import net.caffeinemc.mods.sodium.client.gl.attribute.GlVertexAttributeBinding;
54
import net.caffeinemc.mods.sodium.client.gl.device.CommandList;
65
import net.caffeinemc.mods.sodium.client.gl.device.DrawCommandList;
76
import net.caffeinemc.mods.sodium.client.gl.device.MultiDrawBatch;
@@ -16,7 +15,6 @@
1615
import net.caffeinemc.mods.sodium.client.render.chunk.lists.ChunkRenderList;
1716
import net.caffeinemc.mods.sodium.client.render.chunk.lists.ChunkRenderListIterable;
1817
import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion;
19-
import net.caffeinemc.mods.sodium.client.render.chunk.shader.ChunkShaderBindingPoints;
2018
import net.caffeinemc.mods.sodium.client.render.chunk.shader.ChunkShaderInterface;
2119
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
2220
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.SortBehavior;

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/RenderSectionManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,9 @@ public Collection<String> getDebugStrings() {
701701
count++;
702702
}
703703

704-
list.add(String.format("Geometry Pool: %d/%d MiB (%d buffers)", MathUtil.toMib(geometryDeviceUsed), MathUtil.toMib(geometryDeviceAllocated), count));
705-
list.add(String.format("Index Pool: %d/%d MiB", MathUtil.toMib(indexDeviceUsed), MathUtil.toMib(indexDeviceAllocated)));
704+
list.add(String.format("Pools: Geometry %d/%d MiB, Index %d/%d MiB (%d buffers)",
705+
MathUtil.toMib(geometryDeviceUsed), MathUtil.toMib(geometryDeviceAllocated),
706+
MathUtil.toMib(indexDeviceUsed), MathUtil.toMib(indexDeviceAllocated), count));
706707
list.add(String.format("Transfer Queue: %s", this.regions.getStagingBuffer().toString()));
707708

708709
list.add(String.format("Chunk Builder: Permits=%02d (E %03d) | Busy=%02d | Total=%02d",

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/SharedQuadIndexBuffer.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ public void delete(CommandList commandList) {
7373
commandList.deleteBuffer(this.buffer);
7474
}
7575

76-
public GlIndexType getIndexFormat() {
77-
return this.indexType.getFormat();
78-
}
79-
80-
public IndexType getIndexType() {
81-
return this.indexType;
82-
}
83-
8476
public enum IndexType {
8577
SHORT(GlIndexType.UNSIGNED_SHORT, 64 * 1024) {
8678
@Override

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/ChunkBuildBuffers.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
77
import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionInfo;
88
import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionMeshParts;
9-
import net.caffeinemc.mods.sodium.client.render.chunk.data.SectionRenderDataUnsafe;
109
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.DefaultTerrainRenderPasses;
1110
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
1211
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.Material;

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/data/SectionRenderDataUnsafe.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class SectionRenderDataUnsafe {
3939
private static final long OFFSET_SLICE_MASK = 16;
4040
private static final long OFFSET_ELEMENT_COUNTS = 20;
4141

42-
private static final long ALIGNMENT = 64;
43-
private static final long STRIDE = 64; // cache-line friendly! :)
42+
private static final long ALIGNMENT = 8;
43+
private static final long STRIDE = 48; // cache-line friendly! :)
4444

4545
public static long allocateHeap(int count) {
4646
final var bytes = STRIDE * count;

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/region/RenderRegion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ public DeviceResources(CommandList commandList, StagingBuffer stagingBuffer) {
229229

230230
// the magic number 756 for the initial size is arbitrary, it was made up.
231231
var initialVertices = 756;
232-
this.geometryArena = new GlBufferArena(commandList, REGION_SIZE * initialVertices, stride, stagingBuffer, false);
232+
this.geometryArena = new GlBufferArena(commandList, REGION_SIZE * initialVertices, stride, stagingBuffer);
233233
var initialIndices = (initialVertices / 4) * 6;
234-
this.indexArena = new GlBufferArena(commandList, REGION_SIZE * initialIndices, Integer.BYTES, stagingBuffer, true);
234+
this.indexArena = new GlBufferArena(commandList, REGION_SIZE * initialIndices, Integer.BYTES, stagingBuffer);
235235
}
236236

237237
public void updateTessellation(CommandList commandList, GlTessellation tessellation) {

0 commit comments

Comments
 (0)