@@ -26,21 +26,21 @@ public ChunkMeshBufferBuilder(ChunkVertexType vertexType, int initialCapacity, b
2626
2727 this .buffer = null ;
2828
29- this .capacity = initialCapacity ;
29+ this .capacity = 0 ;
3030 this .initialCapacity = initialCapacity ;
3131
3232 this .analyzer = collectSortState ? new TranslucentQuadAnalyzer () : null ;
3333 }
3434
3535 public void push (ChunkVertexEncoder .Vertex [] vertices , Material material ) {
36- var vertexStart = this .count ;
37- var vertexCount = vertices .length ;
36+ var vertexStart = this .count * this . stride ;
37+ var vertexSize = vertices .length * this . stride ;
3838
39- if (this . count + vertexCount >= this .capacity ) {
40- this .grow (this . stride * vertexCount );
39+ if (vertexStart + vertexSize >= this .capacity ) {
40+ this .grow (vertexSize );
4141 }
4242
43- long ptr = MemoryUtil .memAddress (this .buffer , this . count * this . stride );
43+ long ptr = MemoryUtil .memAddress (this .buffer , vertexStart );
4444
4545 if (this .analyzer != null ) {
4646 for (ChunkVertexEncoder .Vertex vertex : vertices ) {
@@ -52,20 +52,18 @@ public void push(ChunkVertexEncoder.Vertex[] vertices, Material material) {
5252 ptr = this .encoder .write (ptr , material , vertex , this .sectionIndex );
5353 }
5454
55- this .count += vertexCount ;
55+ this .count += vertices . length ;
5656 }
5757
58- private void grow (int len ) {
59- // The new capacity will at least as large as the write it needs to service
60- int cap = Math .max (this .capacity * 2 , this .capacity + len );
58+ private void grow (int bytesNeeded ) {
59+ // Grow by a factor of 2, or by however many bytes more we need, whichever is larger.
60+ int newCapacity = Math .max (this .capacity * 2 , this .capacity + bytesNeeded );
61+ // Ensure we allocate at least initialCapacity bytes
62+ newCapacity = Math .max (newCapacity , this .initialCapacity );
6163
62- // Update the buffer and capacity now
63- this .setBufferSize (cap * this .stride );
64- }
6564
66- private void setBufferSize (int capacity ) {
67- this .buffer = MemoryUtil .memRealloc (this .buffer , capacity * this .stride );
68- this .capacity = capacity ;
65+ this .buffer = MemoryUtil .memRealloc (this .buffer , newCapacity );
66+ this .capacity = newCapacity ;
6967 }
7068
7169 public void start (int sectionIndex ) {
@@ -74,8 +72,6 @@ public void start(int sectionIndex) {
7472 if (this .analyzer != null ) {
7573 this .analyzer .clear ();
7674 }
77-
78- this .setBufferSize (this .initialCapacity );
7975 }
8076
8177 @ Nullable
@@ -89,6 +85,7 @@ public void destroy() {
8985 }
9086
9187 this .buffer = null ;
88+ this .capacity = 0 ;
9289 }
9390
9491 public boolean isEmpty () {
0 commit comments