| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import com.mojang.jtracy.MemoryPool;
|
| 4 | import com.mojang.jtracy.TracyClient;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.nio.ByteBuffer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 | import org.lwjgl.system.MemoryUtil;
|
| 12 | import org.lwjgl.system.MemoryUtil.MemoryAllocator;
|
| 13 | import org.slf4j.Logger;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class ByteBufferBuilder implements AutoCloseable {
|
| 17 | private static final MemoryPool MEMORY_POOL = TracyClient.createMemoryPool("ByteBufferBuilder");
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private static final MemoryAllocator ALLOCATOR = MemoryUtil.getAllocator(false);
|
| 20 | private static final long DEFAULT_MAX_CAPACITY = 4294967295L;
|
| 21 | private static final int MAX_GROWTH_SIZE = 2097152;
|
| 22 | private static final int BUFFER_FREED_GENERATION = -1;
|
| 23 | private long pointer;
|
| 24 | private long capacity;
|
| 25 | private final long maxCapacity;
|
| 26 | private long writeOffset;
|
| 27 | private long nextResultOffset;
|
| 28 | private int resultCount;
|
| 29 | private int generation;
|
| 30 |
|
| 31 | public ByteBufferBuilder(final int initialCapacity, final long maxCapacity) {
|
| 32 | this.capacity = initialCapacity;
|
| 33 | this.maxCapacity = maxCapacity;
|
| 34 | this.pointer = ALLOCATOR.malloc(initialCapacity);
|
| 35 | MEMORY_POOL.malloc(this.pointer, initialCapacity);
|
| 36 | if (this.pointer == 0L) {
|
| 37 | throw new OutOfMemoryError("Failed to allocate " + initialCapacity + " bytes");
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public ByteBufferBuilder(final int initialCapacity) {
|
| 42 | this(initialCapacity, 4294967295L);
|
| 43 | }
|
| 44 |
|
| 45 | public static ByteBufferBuilder exactlySized(final int capacity) {
|
| 46 | return new ByteBufferBuilder(capacity, capacity);
|
| 47 | }
|
| 48 |
|
| 49 | public long reserve(final int size) {
|
| 50 | long offset = this.writeOffset;
|
| 51 | long nextOffset = Math.addExact(offset, size);
|
| 52 | this.ensureCapacity(nextOffset);
|
| 53 | this.writeOffset = nextOffset;
|
| 54 | return Math.addExact(this.pointer, offset);
|
| 55 | }
|
| 56 |
|
| 57 | private void ensureCapacity(final long requiredCapacity) {
|
| 58 | if (requiredCapacity > this.capacity) {
|
| 59 | if (requiredCapacity > this.maxCapacity) {
|
| 60 | throw new IllegalArgumentException("Maximum capacity of ByteBufferBuilder (" + this.maxCapacity + ") exceeded, required " + requiredCapacity);
|
| 61 | }
|
| 62 |
|
| 63 | long preferredGrowth = Math.min(this.capacity, 2097152L);
|
| 64 | long newCapacity = Mth.clamp(this.capacity + preferredGrowth, requiredCapacity, this.maxCapacity);
|
| 65 | this.resize(newCapacity);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | private void resize(final long newCapacity) {
|
| 70 | MEMORY_POOL.free(this.pointer);
|
| 71 | this.pointer = ALLOCATOR.realloc(this.pointer, newCapacity);
|
| 72 | MEMORY_POOL.malloc(this.pointer, (int)Math.min(newCapacity, 2147483647L));
|
| 73 | LOGGER.debug("Needed to grow BufferBuilder buffer: Old size {} bytes, new size {} bytes.", this.capacity, newCapacity);
|
| 74 | if (this.pointer == 0L) {
|
| 75 | throw new OutOfMemoryError("Failed to resize buffer from " + this.capacity + " bytes to " + newCapacity + " bytes");
|
| 76 | }
|
| 77 |
|
| 78 | this.capacity = newCapacity;
|
| 79 | }
|
| 80 |
|
| 81 | public ByteBufferBuilder.@Nullable Result build() {
|
| 82 | this.checkOpen();
|
| 83 | long offset = this.nextResultOffset;
|
| 84 | long size = this.writeOffset - offset;
|
| 85 | if (size == 0L) {
|
| 86 | return null;
|
| 87 | }
|
| 88 |
|
| 89 | if (size > 2147483647L) {
|
| 90 | throw new IllegalStateException("Cannot build buffer larger than 2147483647 bytes (was " + size + ")");
|
| 91 | }
|
| 92 |
|
| 93 | this.nextResultOffset = this.writeOffset;
|
| 94 | this.resultCount++;
|
| 95 | return new ByteBufferBuilder.Result(offset, (int)size, this.generation);
|
| 96 | }
|
| 97 |
|
| 98 | public void clear() {
|
| 99 | if (this.resultCount > 0) {
|
| 100 | LOGGER.warn("Clearing BufferBuilder with unused batches");
|
| 101 | }
|
| 102 |
|
| 103 | this.discard();
|
| 104 | }
|
| 105 |
|
| 106 | public void discard() {
|
| 107 | this.checkOpen();
|
| 108 | if (this.resultCount > 0) {
|
| 109 | this.discardResults();
|
| 110 | this.resultCount = 0;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private boolean isValid(final int generation) {
|
| 115 | return generation == this.generation;
|
| 116 | }
|
| 117 |
|
| 118 | private void freeResult() {
|
| 119 | if (--this.resultCount <= 0) {
|
| 120 | this.discardResults();
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private void discardResults() {
|
| 125 | long currentSize = this.writeOffset - this.nextResultOffset;
|
| 126 | if (currentSize > 0L) {
|
| 127 | MemoryUtil.memCopy(this.pointer + this.nextResultOffset, this.pointer, currentSize);
|
| 128 | }
|
| 129 |
|
| 130 | this.writeOffset = currentSize;
|
| 131 | this.nextResultOffset = 0L;
|
| 132 | this.generation++;
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void close() {
|
| 137 | if (this.pointer != 0L) {
|
| 138 | MEMORY_POOL.free(this.pointer);
|
| 139 | ALLOCATOR.free(this.pointer);
|
| 140 | this.pointer = 0L;
|
| 141 | this.generation = -1;
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | private void checkOpen() {
|
| 146 | if (this.pointer == 0L) {
|
| 147 | throw new IllegalStateException("Buffer has been freed");
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | @Environment(EnvType.CLIENT)
|
| 152 | public class Result implements AutoCloseable {
|
| 153 | private final long offset;
|
| 154 | private final int size;
|
| 155 | private final int generation;
|
| 156 | private boolean closed;
|
| 157 |
|
| 158 | private Result(final long offset, final int size, final int generation) {
|
| 159 | this.offset = offset;
|
| 160 | this.size = size;
|
| 161 | this.generation = generation;
|
| 162 | }
|
| 163 |
|
| 164 | public ByteBuffer byteBuffer() {
|
| 165 | if (!ByteBufferBuilder.this.isValid(this.generation)) {
|
| 166 | throw new IllegalStateException("Buffer is no longer valid");
|
| 167 | } else {
|
| 168 | return MemoryUtil.memByteBuffer(ByteBufferBuilder.this.pointer + this.offset, this.size);
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | public int size() {
|
| 173 | return this.size;
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public void close() {
|
| 178 | if (!this.closed) {
|
| 179 | this.closed = true;
|
| 180 | if (ByteBufferBuilder.this.isValid(this.generation)) {
|
| 181 | ByteBufferBuilder.this.freeResult();
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|