Skip to content

Commit

Permalink
Restored BufferBuilder optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Asek3 committed Jan 28, 2024
1 parent b60cf92 commit fe12713
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public class MixinBufferBuilder {
@Shadow
private VertexFormat vertexFormat;

@Shadow
private int buildStart;

/**
* @reason Reduce allocations, use stack allocations, avoid unnecessary math and pointer bumping, inline comparators
* @author JellySquid
Expand All @@ -38,15 +35,14 @@ public void sortVertexData(float cameraX, float cameraY, float cameraZ) {
int vertexStride = this.vertexFormat.getSize();
int quadStride = this.vertexFormat.getIntegerSize() * 4;

int quadStart = this.buildStart / 4;
int quadCount = this.vertexCount / 4;
int vertexSizeInteger = this.vertexFormat.getIntegerSize();

float[] distanceArray = new float[quadCount];
int[] indicesArray = new int[quadCount];

for (int quadIdx = 0; quadIdx < quadCount; ++quadIdx) {
distanceArray[quadIdx] = getDistanceSq(floatBuffer, cameraX, cameraY, cameraZ, vertexSizeInteger, quadStart + (quadIdx * vertexStride));
distanceArray[quadIdx] = getDistanceSq(floatBuffer, cameraX, cameraY, cameraZ, vertexSizeInteger, quadIdx * vertexStride);
indicesArray[quadIdx] = quadIdx;
}

Expand All @@ -60,24 +56,24 @@ public void sortVertexData(float cameraX, float cameraY, float cameraZ) {
int m = indicesArray[l];

if (m != l) {
sliceQuad(floatBuffer, m, quadStride, quadStart);
sliceQuad(floatBuffer, m, quadStride);
tmp.clear();
tmp.put(floatBuffer);

int n = m;

for (int o = indicesArray[m]; n != l; o = indicesArray[o]) {
sliceQuad(floatBuffer, o, quadStride, quadStart);
sliceQuad(floatBuffer, o, quadStride);
FloatBuffer floatBuffer3 = floatBuffer.slice();

sliceQuad(floatBuffer, n, quadStride, quadStart);
sliceQuad(floatBuffer, n, quadStride);
floatBuffer.put(floatBuffer3);

bits.set(n);
n = o;
}

sliceQuad(floatBuffer, l, quadStride, quadStart);
sliceQuad(floatBuffer, l, quadStride);
tmp.flip();

floatBuffer.put(tmp);
Expand All @@ -91,8 +87,8 @@ private static void mergeSort(int[] indicesArray, float[] distanceArray) {
mergeSort(indicesArray, 0, indicesArray.length, distanceArray, Arrays.copyOf(indicesArray, indicesArray.length));
}

private static void sliceQuad(FloatBuffer floatBuffer, int quadIdx, int quadStride, int quadStart) {
int base = quadStart + (quadIdx * quadStride);
private static void sliceQuad(FloatBuffer floatBuffer, int quadIdx, int quadStride) {
int base = quadIdx * quadStride;

floatBuffer.limit(base + quadStride);
floatBuffer.position(base);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/vintagium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// "features.block.MixinBlockModelRenderer",
"features.block.MixinWorldRenderer",
"features.buffer_builder.fast_advance.MixinVertexFormat",
//"features.buffer_builder.fast_sort.MixinBufferBuilder",
"features.buffer_builder.fast_sort.MixinBufferBuilder",
"features.chunk_rendering.AccessorActiveRenderInfo",
"features.chunk_rendering.AccessorBlockFluidRenderer",
"features.chunk_rendering.AccessorBlockRenderDispatcher",
Expand Down

0 comments on commit fe12713

Please sign in to comment.