| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.pipeline.IndexType;
|
| 4 | import com.mojang.renderpearl.api.pipeline.PrimitiveTopology;
|
| 5 | import com.mojang.renderpearl.api.vertex.VertexFormat;
|
| 6 | import com.mojang.renderpearl.api.vertex.VertexFormatElement;
|
| 7 | import it.unimi.dsi.fastutil.ints.IntConsumer;
|
| 8 | import java.nio.ByteBuffer;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import org.apache.commons.lang3.mutable.MutableLong;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 | import org.lwjgl.system.MemoryUtil;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class MeshData implements AutoCloseable {
|
| 17 | private final ByteBufferBuilder.Result vertexBuffer;
|
| 18 | private ByteBufferBuilder.@Nullable Result indexBuffer;
|
| 19 | private final MeshData.DrawState drawState;
|
| 20 |
|
| 21 | public MeshData(final ByteBufferBuilder.Result vertexBuffer, final MeshData.DrawState drawState) {
|
| 22 | this.vertexBuffer = vertexBuffer;
|
| 23 | this.drawState = drawState;
|
| 24 | }
|
| 25 |
|
| 26 | public static void decodeQuadCentroids(
|
| 27 | final ByteBuffer vertexBuffer, final int vertexCount, final VertexFormat format, final CompactVectorArray output, final int outputIndex
|
| 28 | ) {
|
| 29 | VertexFormatElement positionElement = format.getElement("Position");
|
| 30 | if (positionElement == null) {
|
| 31 | throw new IllegalArgumentException("Cannot identify quad centers with no position element");
|
| 32 | }
|
| 33 |
|
| 34 | int positionOffset = vertexBuffer.position() + positionElement.offset();
|
| 35 | int vertexStride = format.getVertexSize();
|
| 36 | int quadStride = vertexStride * 4;
|
| 37 | int quadCount = vertexCount / 4;
|
| 38 |
|
| 39 | for (int i = 0; i < quadCount; i++) {
|
| 40 | int firstPosOffset = i * quadStride + positionOffset;
|
| 41 | int secondPosOffset = firstPosOffset + vertexStride * 2;
|
| 42 | float x0 = vertexBuffer.getFloat(firstPosOffset + 0);
|
| 43 | float y0 = vertexBuffer.getFloat(firstPosOffset + 4);
|
| 44 | float z0 = vertexBuffer.getFloat(firstPosOffset + 8);
|
| 45 | float x1 = vertexBuffer.getFloat(secondPosOffset + 0);
|
| 46 | float y1 = vertexBuffer.getFloat(secondPosOffset + 4);
|
| 47 | float z1 = vertexBuffer.getFloat(secondPosOffset + 8);
|
| 48 | float xMid = (x0 + x1) / 2.0F;
|
| 49 | float yMid = (y0 + y1) / 2.0F;
|
| 50 | float zMid = (z0 + z1) / 2.0F;
|
| 51 | output.set(outputIndex + i, xMid, yMid, zMid);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public ByteBuffer vertexBuffer() {
|
| 56 | return this.vertexBuffer.byteBuffer();
|
| 57 | }
|
| 58 |
|
| 59 | public @Nullable ByteBuffer indexBuffer() {
|
| 60 | return this.indexBuffer != null ? this.indexBuffer.byteBuffer() : null;
|
| 61 | }
|
| 62 |
|
| 63 | public ByteBufferBuilder.Result vertexBufferSlice() {
|
| 64 | return this.vertexBuffer;
|
| 65 | }
|
| 66 |
|
| 67 | public MeshData.DrawState drawState() {
|
| 68 | return this.drawState;
|
| 69 | }
|
| 70 |
|
| 71 | public MeshData.@Nullable SortState sortQuads(final ByteBufferBuilder indexBufferTarget, final VertexSorting sorting) {
|
| 72 | if (this.drawState.primitiveTopology() != PrimitiveTopology.QUADS) {
|
| 73 | return null;
|
| 74 | }
|
| 75 |
|
| 76 | CompactVectorArray centroids = new CompactVectorArray(this.drawState.vertexCount() / 4);
|
| 77 | decodeQuadCentroids(this.vertexBuffer.byteBuffer(), this.drawState.vertexCount(), this.drawState.format(), centroids, 0);
|
| 78 | MeshData.SortState sortState = new MeshData.SortState(centroids, this.drawState.indexType());
|
| 79 | this.indexBuffer = sortState.buildSortedIndexBuffer(indexBufferTarget, sorting);
|
| 80 | return sortState;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void close() {
|
| 85 | this.vertexBuffer.close();
|
| 86 | if (this.indexBuffer != null) {
|
| 87 | this.indexBuffer.close();
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Environment(EnvType.CLIENT)
|
| 92 | public record DrawState(VertexFormat format, int vertexCount, int indexCount, PrimitiveTopology primitiveTopology, IndexType indexType) {
|
| 93 | }
|
| 94 |
|
| 95 | @Environment(EnvType.CLIENT)
|
| 96 | public record SortState(CompactVectorArray centroids, IndexType indexType) {
|
| 97 | public ByteBufferBuilder.@Nullable Result buildSortedIndexBuffer(final ByteBufferBuilder target, final VertexSorting sorting) {
|
| 98 | int[] startIndices = sorting.sort(this.centroids);
|
| 99 | long pointer = target.reserve(startIndices.length * 6 * this.indexType.bytes);
|
| 100 | writeIndices(startIndices, this.indexWriter(pointer, this.indexType));
|
| 101 | return target.build();
|
| 102 | }
|
| 103 |
|
| 104 | public void writeSortedIndexBuffer(final ByteBuffer target, final VertexSorting sorting) {
|
| 105 | IntConsumer output = switch (this.indexType) {
|
| 106 | case SHORT -> value -> target.putShort((short)value);
|
| 107 | case INT -> target::putInt;
|
| 108 | };
|
| 109 | writeIndices(sorting.sort(this.centroids), output);
|
| 110 | }
|
| 111 |
|
| 112 | private static void writeIndices(final int[] startIndices, final IntConsumer output) {
|
| 113 | for (int startIndex : startIndices) {
|
| 114 | output.accept(startIndex * 4 + 0);
|
| 115 | output.accept(startIndex * 4 + 1);
|
| 116 | output.accept(startIndex * 4 + 2);
|
| 117 | output.accept(startIndex * 4 + 2);
|
| 118 | output.accept(startIndex * 4 + 3);
|
| 119 | output.accept(startIndex * 4 + 0);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | private IntConsumer indexWriter(final long pointer, final IndexType indexType) {
|
| 124 | MutableLong nextIndex = new MutableLong(pointer);
|
| 125 |
|
| 126 | return switch (indexType) {
|
| 127 | case SHORT -> value -> MemoryUtil.memPutShort(nextIndex.getAndAdd(2L), (short)value);
|
| 128 | case INT -> value -> MemoryUtil.memPutInt(nextIndex.getAndAdd(4L), value);
|
| 129 | };
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|