| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.blaze3d.vertex.BufferBuilder;
|
| 5 | import com.mojang.blaze3d.vertex.ByteBufferBuilder;
|
| 6 | import com.mojang.blaze3d.vertex.CompactVectorArray;
|
| 7 | import com.mojang.blaze3d.vertex.MeshData;
|
| 8 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 9 | import com.mojang.blaze3d.vertex.VertexSorting;
|
| 10 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 11 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 12 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 13 | import com.mojang.renderpearl.api.commands.GpuFence;
|
| 14 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 15 | import com.mojang.renderpearl.api.pipeline.IndexType;
|
| 16 | import com.mojang.renderpearl.api.pipeline.PrimitiveTopology;
|
| 17 | import com.mojang.renderpearl.api.vertex.VertexFormat;
|
| 18 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 19 | import java.nio.ByteBuffer;
|
| 20 | import java.util.ArrayList;
|
| 21 | import java.util.List;
|
| 22 | import java.util.Objects;
|
| 23 | import java.util.function.Supplier;
|
| 24 | import net.fabricmc.api.EnvType;
|
| 25 | import net.fabricmc.api.Environment;
|
| 26 | import net.minecraft.client.renderer.StagedVertexBuffer.GpuBufferPool.PendingRecycle;
|
| 27 | import net.minecraft.util.Mth;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class StagedVertexBuffer implements AutoCloseable {
|
| 32 | private final ByteBufferBuilder stagingBuffer;
|
| 33 | private final StagedVertexBuffer.GpuBufferPool stagingGpuBufferPool;
|
| 34 | private final List<StagedVertexBuffer.Draw> draws = new ArrayList<>();
|
| 35 | private StagedVertexBuffer.@Nullable Draw lastBuildingDraw;
|
| 36 | private @Nullable BufferBuilder lastVertexBuilder;
|
| 37 | private final StagedVertexBuffer.GpuBufferPool vertexBufferPool;
|
| 38 | private final StagedVertexBuffer.GpuBufferPool indexBufferPool;
|
| 39 | private @Nullable GpuBuffer currentVertexBuffer;
|
| 40 | private @Nullable GpuBuffer currentIndexBuffer;
|
| 41 |
|
| 42 | public StagedVertexBuffer(final Supplier<String> label, final int initialCapacity) {
|
| 43 | this.stagingBuffer = new ByteBufferBuilder(initialCapacity);
|
| 44 | this.stagingGpuBufferPool = new StagedVertexBuffer.GpuBufferPool(() -> label.get() + " - Staging", 22);
|
| 45 | this.vertexBufferPool = new StagedVertexBuffer.GpuBufferPool(() -> label.get() + " - Vertex", 40);
|
| 46 | this.indexBufferPool = new StagedVertexBuffer.GpuBufferPool(() -> label.get() + " - Index", 72);
|
| 47 | }
|
| 48 |
|
| 49 | public StagedVertexBuffer.Draw appendDraw(final VertexFormat format, final PrimitiveTopology primitiveTopology) {
|
| 50 | return this.appendDraw(format, primitiveTopology, null);
|
| 51 | }
|
| 52 |
|
| 53 | public StagedVertexBuffer.Draw appendDraw(final VertexFormat format, final PrimitiveTopology primitiveTopology, final @Nullable VertexSorting quadSorting) {
|
| 54 | if (this.currentVertexBuffer != null) {
|
| 55 | throw new IllegalStateException("Cannot append draw after upload");
|
| 56 | }
|
| 57 |
|
| 58 | if (quadSorting != null && primitiveTopology != PrimitiveTopology.QUADS) {
|
| 59 | throw new IllegalArgumentException("Cannot sort draw with " + primitiveTopology);
|
| 60 | }
|
| 61 |
|
| 62 | StagedVertexBuffer.Draw draw = new StagedVertexBuffer.Draw(format, primitiveTopology, quadSorting);
|
| 63 | this.draws.add(draw);
|
| 64 | return draw;
|
| 65 | }
|
| 66 |
|
| 67 | public VertexConsumer getVertexBuilder(final StagedVertexBuffer.Draw draw) {
|
| 68 | if (this.currentVertexBuffer != null) {
|
| 69 | throw new IllegalStateException("Cannot append draw after upload");
|
| 70 | }
|
| 71 |
|
| 72 | if (this.lastBuildingDraw == draw) {
|
| 73 | return Objects.requireNonNull(this.lastVertexBuilder);
|
| 74 | }
|
| 75 |
|
| 76 | this.finishLastVertexBuilder();
|
| 77 | this.lastBuildingDraw = draw;
|
| 78 | this.lastVertexBuilder = new BufferBuilder(this.stagingBuffer, draw.primitiveTopology, draw.format);
|
| 79 | return this.lastVertexBuilder;
|
| 80 | }
|
| 81 |
|
| 82 | private void finishLastVertexBuilder() {
|
| 83 | if (this.lastVertexBuilder != null) {
|
| 84 | MeshData mesh = this.lastVertexBuilder.build();
|
| 85 | if (mesh != null) {
|
| 86 | Objects.requireNonNull(this.lastBuildingDraw).append(mesh);
|
| 87 | }
|
| 88 |
|
| 89 | this.lastVertexBuilder = null;
|
| 90 | this.lastBuildingDraw = null;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | public void upload() {
|
| 95 | if (this.currentVertexBuffer != null) {
|
| 96 | throw new IllegalStateException("Already uploaded");
|
| 97 | }
|
| 98 |
|
| 99 | if (!this.draws.isEmpty()) {
|
| 100 | this.finishLastVertexBuilder();
|
| 101 | int nextVertexOffset = 0;
|
| 102 | int nextIndexOffset = 0;
|
| 103 |
|
| 104 | for (StagedVertexBuffer.Draw draw : this.draws) {
|
| 105 | if (!draw.isEmpty()) {
|
| 106 | draw.vertexOffset = Mth.roundToward(nextVertexOffset, draw.format.getVertexSize());
|
| 107 | nextVertexOffset = draw.vertexOffset + draw.vertexBufferSize;
|
| 108 | if (draw.quadSorting != null) {
|
| 109 | IndexType indexType = draw.indexType();
|
| 110 | draw.indexOffset = Mth.roundToward(nextIndexOffset, indexType.bytes);
|
| 111 | nextIndexOffset = draw.indexOffset + draw.indexCount * indexType.bytes;
|
| 112 | } else {
|
| 113 | RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(draw.primitiveTopology);
|
| 114 | autoIndices.getBuffer(draw.indexCount);
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | int vertexBufferSize = nextVertexOffset;
|
| 120 | int indexBufferSize = nextIndexOffset;
|
| 121 | if (vertexBufferSize != 0) {
|
| 122 | GpuDevice device = RenderSystem.getDevice();
|
| 123 | this.currentVertexBuffer = this.vertexBufferPool.acquire(device, vertexBufferSize);
|
| 124 | this.currentIndexBuffer = indexBufferSize > 0 ? this.indexBufferPool.acquire(device, indexBufferSize) : null;
|
| 125 | this.uploadDrawsToBuffers(device, this.draws, this.currentVertexBuffer, this.currentIndexBuffer, vertexBufferSize, indexBufferSize);
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | private void uploadDrawsToBuffers(
|
| 131 | final GpuDevice device,
|
| 132 | final List<StagedVertexBuffer.Draw> draws,
|
| 133 | final GpuBuffer vertexGpuBuffer,
|
| 134 | final @Nullable GpuBuffer indexGpuBuffer,
|
| 135 | final int vertexBufferSize,
|
| 136 | final int indexBufferSize
|
| 137 | ) {
|
| 138 | CommandEncoder commandEncoder = device.createCommandEncoder();
|
| 139 | int stagingBufferSize = vertexBufferSize + indexBufferSize;
|
| 140 | GpuBuffer stagingBuffer = this.stagingGpuBufferPool.acquire(device, stagingBufferSize);
|
| 141 |
|
| 142 | try (GpuBufferSlice.MappedView view = stagingBuffer.slice(0L, stagingBufferSize).map(false, true)) {
|
| 143 | ByteBuffer buffer = view.data();
|
| 144 |
|
| 145 | for (StagedVertexBuffer.Draw draw : draws) {
|
| 146 | if (!draw.isEmpty()) {
|
| 147 | buffer.position(draw.vertexOffset);
|
| 148 |
|
| 149 | for (ByteBufferBuilder.Result slice : draw.vertexBufferSlices) {
|
| 150 | buffer.put(slice.byteBuffer());
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | for (StagedVertexBuffer.Draw draw : draws) {
|
| 156 | if (!draw.isEmpty()) {
|
| 157 | if (indexGpuBuffer != null && draw.quadSorting != null) {
|
| 158 | MeshData.SortState sortState = new MeshData.SortState(decodeSortingPoints(draw), draw.indexType());
|
| 159 | buffer.position(vertexBufferSize + draw.indexOffset);
|
| 160 | sortState.writeSortedIndexBuffer(buffer, draw.quadSorting);
|
| 161 | }
|
| 162 |
|
| 163 | draw.freeVertexData();
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | commandEncoder.copyToBuffer(stagingBuffer.slice(0L, vertexBufferSize), vertexGpuBuffer.slice(0L, vertexBufferSize));
|
| 169 | if (indexGpuBuffer != null) {
|
| 170 | commandEncoder.copyToBuffer(stagingBuffer.slice(vertexBufferSize, indexBufferSize), indexGpuBuffer.slice(0L, indexBufferSize));
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | private static CompactVectorArray decodeSortingPoints(final StagedVertexBuffer.Draw draw) {
|
| 175 | VertexFormat format = draw.format;
|
| 176 | CompactVectorArray points = new CompactVectorArray(draw.vertexCount / 4);
|
| 177 | int offset = 0;
|
| 178 |
|
| 179 | for (ByteBufferBuilder.Result vertexBuffer : draw.vertexBufferSlices) {
|
| 180 | int vertexCount = vertexBuffer.size() / format.getVertexSize();
|
| 181 | MeshData.decodeQuadCentroids(vertexBuffer.byteBuffer(), vertexCount, format, points, offset);
|
| 182 | offset += vertexCount / 4;
|
| 183 | }
|
| 184 |
|
| 185 | return points;
|
| 186 | }
|
| 187 |
|
| 188 | public StagedVertexBuffer.@Nullable ExecuteInfo getExecuteInfo(final StagedVertexBuffer.Draw draw) {
|
| 189 | if (draw.isEmpty()) {
|
| 190 | return null;
|
| 191 | } else if (this.currentVertexBuffer == null) {
|
| 192 | throw new IllegalStateException("Cannot execute before upload");
|
| 193 | } else {
|
| 194 | int baseVertex = draw.vertexOffset / draw.format.getVertexSize();
|
| 195 | if (this.currentIndexBuffer != null && draw.quadSorting != null) {
|
| 196 | IndexType indexType = draw.indexType();
|
| 197 | int firstIndex = draw.indexOffset / indexType.bytes;
|
| 198 | return new StagedVertexBuffer.ExecuteInfo(
|
| 199 | this.currentVertexBuffer, this.currentIndexBuffer, indexType, baseVertex, firstIndex, draw.indexCount, draw.primitiveTopology
|
| 200 | );
|
| 201 | } else {
|
| 202 | RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(draw.primitiveTopology);
|
| 203 | autoIndices.requestIndexCount(draw.indexCount);
|
| 204 | return new StagedVertexBuffer.ExecuteInfo(this.currentVertexBuffer, null, autoIndices.type(), baseVertex, 0, draw.indexCount, draw.primitiveTopology);
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | public void requestIndexCount(final StagedVertexBuffer.Draw draw) {
|
| 210 | if (this.currentIndexBuffer == null || draw.quadSorting == null) {
|
| 211 | RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(draw.primitiveTopology);
|
| 212 | autoIndices.requestIndexCount(draw.indexCount);
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | public void endDraw() {
|
| 217 | this.draws.clear();
|
| 218 | this.currentVertexBuffer = null;
|
| 219 | this.currentIndexBuffer = null;
|
| 220 | }
|
| 221 |
|
| 222 | public void endFrame() {
|
| 223 | this.endDraw();
|
| 224 | GpuDevice device = RenderSystem.getDevice();
|
| 225 | this.stagingGpuBufferPool.endFrame(device);
|
| 226 | this.vertexBufferPool.endFrame(device);
|
| 227 | this.indexBufferPool.endFrame(device);
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public void close() {
|
| 232 | this.stagingBuffer.close();
|
| 233 | this.stagingGpuBufferPool.close();
|
| 234 | this.vertexBufferPool.close();
|
| 235 | this.indexBufferPool.close();
|
| 236 | }
|
| 237 |
|
| 238 | @Environment(EnvType.CLIENT)
|
| 239 | public static class Draw {
|
| 240 | private final VertexFormat format;
|
| 241 | private final PrimitiveTopology primitiveTopology;
|
| 242 | private final @Nullable VertexSorting quadSorting;
|
| 243 | private final List<ByteBufferBuilder.Result> vertexBufferSlices = new ArrayList<>();
|
| 244 | private int vertexBufferSize;
|
| 245 | private int vertexCount;
|
| 246 | private int indexCount;
|
| 247 | private int vertexOffset;
|
| 248 | private int indexOffset;
|
| 249 |
|
| 250 | private Draw(final VertexFormat format, final PrimitiveTopology primitiveTopology, final @Nullable VertexSorting quadSorting) {
|
| 251 | this.format = format;
|
| 252 | this.primitiveTopology = primitiveTopology;
|
| 253 | this.quadSorting = quadSorting;
|
| 254 | }
|
| 255 |
|
| 256 | private void append(final MeshData mesh) {
|
| 257 | assert mesh.indexBuffer() == null;
|
| 258 | this.vertexBufferSlices.add(mesh.vertexBufferSlice());
|
| 259 | this.vertexBufferSize = this.vertexBufferSize + mesh.vertexBuffer().remaining();
|
| 260 | this.vertexCount = this.vertexCount + mesh.drawState().vertexCount();
|
| 261 | this.indexCount = this.indexCount + mesh.drawState().indexCount();
|
| 262 | }
|
| 263 |
|
| 264 | private IndexType indexType() {
|
| 265 | return IndexType.least(this.vertexCount);
|
| 266 | }
|
| 267 |
|
| 268 | private void freeVertexData() {
|
| 269 | this.vertexBufferSlices.forEach(ByteBufferBuilder.Result::close);
|
| 270 | this.vertexBufferSlices.clear();
|
| 271 | }
|
| 272 |
|
| 273 | public boolean isEmpty() {
|
| 274 | return this.vertexCount == 0;
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | @Environment(EnvType.CLIENT)
|
| 279 | public record ExecuteInfo(
|
| 280 | GpuBuffer vertexBuffer,
|
| 281 | @Nullable GpuBuffer customIndexBuffer,
|
| 282 | IndexType indexType,
|
| 283 | int baseVertex,
|
| 284 | int firstIndex,
|
| 285 | int indexCount,
|
| 286 | PrimitiveTopology topology
|
| 287 | ) {
|
| 288 | public GpuBuffer indexBuffer() {
|
| 289 | if (this.customIndexBuffer == null) {
|
| 290 | RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(this.topology);
|
| 291 | return autoIndices.getBuffer();
|
| 292 | } else {
|
| 293 | return this.customIndexBuffer;
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 |
|
| 298 | @Environment(EnvType.CLIENT)
|
| 299 | private static class GpuBufferPool implements AutoCloseable {
|
| 300 | private static final int BUFFER_SIZE_INCREMENT = 262144;
|
| 301 | private static final int MAX_REUSE_SIZE_FACTOR = 4;
|
| 302 | private final Supplier<String> label;
|
| 303 | private final @GpuBuffer.Usage int usage;
|
| 304 | private final List<GpuBuffer> available = new ArrayList<>();
|
| 305 | private final List<GpuBuffer> usedThisFrame = new ArrayList<>();
|
| 306 | private final List<PendingRecycle> pendingRecycle = new ArrayList<>();
|
| 307 |
|
| 308 | private GpuBufferPool(final Supplier<String> label, final @GpuBuffer.Usage int usage) {
|
| 309 | this.label = label;
|
| 310 | this.usage = usage;
|
| 311 | }
|
| 312 |
|
| 313 | private void tryRecycleBuffers() {
|
| 314 | this.pendingRecycle.removeIf(buffer -> {
|
| 315 | List<GpuBuffer> recycled = buffer.tryRecycle();
|
| 316 | if (recycled != null) {
|
| 317 | this.available.addAll(recycled);
|
| 318 | return true;
|
| 319 | } else {
|
| 320 | return false;
|
| 321 | }
|
| 322 | });
|
| 323 | }
|
| 324 |
|
| 325 | public GpuBuffer acquire(final GpuDevice device, final int minSize) {
|
| 326 | int roundedMinSize = Mth.roundToward(minSize, 262144);
|
| 327 | GpuBuffer buffer = this.takeBestAvailable(roundedMinSize, roundedMinSize * 4);
|
| 328 | if (buffer == null) {
|
| 329 | buffer = device.createBuffer(this.label, this.usage, roundedMinSize);
|
| 330 | }
|
| 331 |
|
| 332 | this.usedThisFrame.add(buffer);
|
| 333 | return buffer;
|
| 334 | }
|
| 335 |
|
| 336 | private @Nullable GpuBuffer takeBestAvailable(final int minSize, final int maxSize) {
|
| 337 | int bestIndex = -1;
|
| 338 | long bestSize = maxSize + 1;
|
| 339 |
|
| 340 | for (int i = 0; i < this.available.size(); i++) {
|
| 341 | long size = this.available.get(i).size();
|
| 342 | if (size == minSize) {
|
| 343 | return this.available.remove(i);
|
| 344 | }
|
| 345 |
|
| 346 | if (size > minSize && size < bestSize) {
|
| 347 | bestIndex = i;
|
| 348 | bestSize = size;
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | return bestIndex == -1 ? null : this.available.remove(bestIndex);
|
| 353 | }
|
| 354 |
|
| 355 | public void endFrame(final GpuDevice device) {
|
| 356 | if (!this.usedThisFrame.isEmpty()) {
|
| 357 | GpuFence fence = device.createCommandEncoder().createFence();
|
| 358 | this.pendingRecycle.add(new PendingRecycle(List.copyOf(this.usedThisFrame), fence));
|
| 359 | this.usedThisFrame.clear();
|
| 360 | }
|
| 361 |
|
| 362 | if (!this.available.isEmpty()) {
|
| 363 | this.available.forEach(UncheckedAutoCloseable::close);
|
| 364 | this.available.clear();
|
| 365 | }
|
| 366 |
|
| 367 | this.tryRecycleBuffers();
|
| 368 | }
|
| 369 |
|
| 370 | @Override
|
| 371 | public void close() {
|
| 372 | this.available.forEach(UncheckedAutoCloseable::close);
|
| 373 | this.usedThisFrame.forEach(UncheckedAutoCloseable::close);
|
| 374 | this.pendingRecycle.forEach(PendingRecycle::close);
|
| 375 | this.available.clear();
|
| 376 | this.usedThisFrame.clear();
|
| 377 | this.pendingRecycle.clear();
|
| 378 | }
|
| 379 | }
|
| 380 | }
|
| 381 |
|