| 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 java.nio.ByteOrder;
|
| 8 | import java.util.stream.Collectors;
|
| 9 | import java.util.stream.IntStream;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.util.ARGB;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 | import org.lwjgl.system.MemoryUtil;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class BufferBuilder implements VertexConsumer {
|
| 19 | private static final int MAX_VERTEX_COUNT = 16777215;
|
| 20 | private static final long NOT_BUILDING = -1L;
|
| 21 | private static final long UNKNOWN_ELEMENT = -1L;
|
| 22 | private static final boolean IS_LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN;
|
| 23 | private final ByteBufferBuilder buffer;
|
| 24 | private long vertexPointer = -1L;
|
| 25 | private int vertices;
|
| 26 | private final VertexFormat format;
|
| 27 | private final PrimitiveTopology primitiveTopology;
|
| 28 | private final boolean blockFormat;
|
| 29 | private final boolean entityFormat;
|
| 30 | private final int vertexSize;
|
| 31 | private final int initialElementsToFill;
|
| 32 | private int elementsToFill;
|
| 33 | private boolean building = true;
|
| 34 | private static final int POSITION_SEMANTIC_ID = 0;
|
| 35 | private static final int COLOR_SEMANTIC_ID = 1;
|
| 36 | private static final int UV0_SEMANTIC_ID = 2;
|
| 37 | private static final int UV1_SEMANTIC_ID = 3;
|
| 38 | private static final int UV2_SEMANTIC_ID = 4;
|
| 39 | private static final int UV3_SEMANTIC_ID = 5;
|
| 40 | private static final int NORMAL_SEMANTIC_ID = 6;
|
| 41 | private static final int LINE_WIDTH_SEMANTIC_ID = 7;
|
| 42 | private static final String[] elementNames = new String[]{"Position", "Color", "UV0", "UV1", "UV2", "UV3", "Normal", "LineWidth"};
|
| 43 | private final @Nullable VertexFormatElement[] elements = new VertexFormatElement[elementNames.length];
|
| 44 |
|
| 45 | public BufferBuilder(final ByteBufferBuilder buffer, final PrimitiveTopology primitiveTopology, final VertexFormat format) {
|
| 46 | if (!format.contains("Position")) {
|
| 47 | throw new IllegalArgumentException("Cannot build mesh with no position element");
|
| 48 | }
|
| 49 |
|
| 50 | this.buffer = buffer;
|
| 51 | this.primitiveTopology = primitiveTopology;
|
| 52 | this.format = format;
|
| 53 | this.vertexSize = format.getVertexSize();
|
| 54 | int elementsMask = 0;
|
| 55 |
|
| 56 | for (int i = 0; i < elementNames.length; i++) {
|
| 57 | String elementName = elementNames[i];
|
| 58 | VertexFormatElement element = format.getElement(elementName);
|
| 59 | if (element != null) {
|
| 60 | elementsMask |= 1 << i;
|
| 61 | }
|
| 62 |
|
| 63 | this.elements[i] = element;
|
| 64 | }
|
| 65 |
|
| 66 | this.initialElementsToFill = elementsMask & -2;
|
| 67 | this.blockFormat = format == DefaultVertexFormat.BLOCK;
|
| 68 | this.entityFormat = format == DefaultVertexFormat.ENTITY;
|
| 69 | }
|
| 70 |
|
| 71 | public @Nullable MeshData build() {
|
| 72 | this.ensureBuilding();
|
| 73 | this.endLastVertex();
|
| 74 | MeshData mesh = this.storeMesh();
|
| 75 | this.building = false;
|
| 76 | this.vertexPointer = -1L;
|
| 77 | return mesh;
|
| 78 | }
|
| 79 |
|
| 80 | public MeshData buildOrThrow() {
|
| 81 | MeshData buffer = this.build();
|
| 82 | if (buffer == null) {
|
| 83 | throw new IllegalStateException("BufferBuilder was empty");
|
| 84 | } else {
|
| 85 | return buffer;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | private void ensureBuilding() {
|
| 90 | if (!this.building) {
|
| 91 | throw new IllegalStateException("Not building!");
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | private @Nullable MeshData storeMesh() {
|
| 96 | if (this.vertices == 0) {
|
| 97 | return null;
|
| 98 | }
|
| 99 |
|
| 100 | ByteBufferBuilder.Result vertexBuffer = this.buffer.build();
|
| 101 | if (vertexBuffer == null) {
|
| 102 | return null;
|
| 103 | }
|
| 104 |
|
| 105 | int indices = this.primitiveTopology.indexCount(this.vertices);
|
| 106 | IndexType indexType = IndexType.least(this.vertices);
|
| 107 | return new MeshData(vertexBuffer, new MeshData.DrawState(this.format, this.vertices, indices, this.primitiveTopology, indexType));
|
| 108 | }
|
| 109 |
|
| 110 | private long beginVertex() {
|
| 111 | this.ensureBuilding();
|
| 112 | this.endLastVertex();
|
| 113 | if (this.vertices >= 16777215) {
|
| 114 | throw new IllegalStateException("Trying to write too many vertices (>16777215) into BufferBuilder");
|
| 115 | }
|
| 116 |
|
| 117 | this.vertices++;
|
| 118 | long pointer = this.buffer.reserve(this.vertexSize);
|
| 119 | this.vertexPointer = pointer;
|
| 120 | return pointer;
|
| 121 | }
|
| 122 |
|
| 123 | private long beginElement(final int semanticID) {
|
| 124 | int oldElements = this.elementsToFill;
|
| 125 | int newElements = oldElements & ~(1 << semanticID);
|
| 126 | VertexFormatElement element = this.elements[semanticID];
|
| 127 | if (newElements != oldElements && element != null) {
|
| 128 | this.elementsToFill = newElements;
|
| 129 | long vertexPointer = this.vertexPointer;
|
| 130 | if (vertexPointer == -1L) {
|
| 131 | throw new IllegalArgumentException("Not currently building vertex");
|
| 132 | } else {
|
| 133 | return vertexPointer + element.offset();
|
| 134 | }
|
| 135 | } else {
|
| 136 | return -1L;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | private void endLastVertex() {
|
| 141 | if (this.vertices != 0) {
|
| 142 | if (this.elementsToFill != 0) {
|
| 143 | String missingElements = IntStream.range(0, elementNames.length)
|
| 144 | .filter(i -> (this.elementsToFill & i) != 0)
|
| 145 | .mapToObj(i -> elementNames[i])
|
| 146 | .collect(Collectors.joining(", "));
|
| 147 | throw new IllegalStateException("Missing elements in vertex: " + missingElements);
|
| 148 | }
|
| 149 |
|
| 150 | if (this.primitiveTopology == PrimitiveTopology.LINES) {
|
| 151 | long pointer = this.buffer.reserve(this.vertexSize);
|
| 152 | MemoryUtil.memCopy(pointer - this.vertexSize, pointer, this.vertexSize);
|
| 153 | this.vertices++;
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | private static void putRgba(final long pointer, final int argb) {
|
| 159 | int abgr = ARGB.toABGR(argb);
|
| 160 | MemoryUtil.memPutInt(pointer, IS_LITTLE_ENDIAN ? abgr : Integer.reverseBytes(abgr));
|
| 161 | }
|
| 162 |
|
| 163 | private static void putPackedUv(final long pointer, final int packedUv) {
|
| 164 | if (IS_LITTLE_ENDIAN) {
|
| 165 | MemoryUtil.memPutInt(pointer, packedUv);
|
| 166 | } else {
|
| 167 | MemoryUtil.memPutShort(pointer, (short)(packedUv & 65535));
|
| 168 | MemoryUtil.memPutShort(pointer + 2L, (short)(packedUv >> 16 & 65535));
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public VertexConsumer addVertex(final float x, final float y, final float z) {
|
| 174 | VertexFormatElement positionElement = this.elements[0];
|
| 175 | long pointer = this.beginVertex() + positionElement.offset();
|
| 176 | this.elementsToFill = this.initialElementsToFill;
|
| 177 | putVec3f(pointer, x, y, z);
|
| 178 | return this;
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public VertexConsumer setColor(final int r, final int g, final int b, final int a) {
|
| 183 | long pointer = this.beginElement(1);
|
| 184 | if (pointer != -1L) {
|
| 185 | MemoryUtil.memPutByte(pointer, (byte)r);
|
| 186 | MemoryUtil.memPutByte(pointer + 1L, (byte)g);
|
| 187 | MemoryUtil.memPutByte(pointer + 2L, (byte)b);
|
| 188 | MemoryUtil.memPutByte(pointer + 3L, (byte)a);
|
| 189 | }
|
| 190 |
|
| 191 | return this;
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public VertexConsumer setColor(final int color) {
|
| 196 | long pointer = this.beginElement(1);
|
| 197 | if (pointer != -1L) {
|
| 198 | putRgba(pointer, color);
|
| 199 | }
|
| 200 |
|
| 201 | return this;
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | public VertexConsumer setUv(final float u, final float v) {
|
| 206 | long pointer = this.beginElement(2);
|
| 207 | if (pointer != -1L) {
|
| 208 | MemoryUtil.memPutFloat(pointer, u);
|
| 209 | MemoryUtil.memPutFloat(pointer + 4L, v);
|
| 210 | }
|
| 211 |
|
| 212 | return this;
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public VertexConsumer setUv1(final int u, final int v) {
|
| 217 | return this.uvShort((short)u, (short)v, 3);
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public VertexConsumer setOverlay(final int packedOverlayCoords) {
|
| 222 | long pointer = this.beginElement(3);
|
| 223 | if (pointer != -1L) {
|
| 224 | putPackedUv(pointer, packedOverlayCoords);
|
| 225 | }
|
| 226 |
|
| 227 | return this;
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public VertexConsumer setUv2(final int u, final int v) {
|
| 232 | return this.uvShort((short)u, (short)v, 4);
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public VertexConsumer setUv3(final float u, final float v) {
|
| 237 | long pointer = this.beginElement(5);
|
| 238 | if (pointer != -1L) {
|
| 239 | MemoryUtil.memPutFloat(pointer, u);
|
| 240 | MemoryUtil.memPutFloat(pointer + 4L, v);
|
| 241 | }
|
| 242 |
|
| 243 | return this;
|
| 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | public VertexConsumer setLight(final int packedLightCoords) {
|
| 248 | long pointer = this.beginElement(4);
|
| 249 | if (pointer != -1L) {
|
| 250 | putPackedUv(pointer, packedLightCoords);
|
| 251 | }
|
| 252 |
|
| 253 | return this;
|
| 254 | }
|
| 255 |
|
| 256 | private VertexConsumer uvShort(final short u, final short v, final int semanticID) {
|
| 257 | long pointer = this.beginElement(semanticID);
|
| 258 | if (pointer != -1L) {
|
| 259 | MemoryUtil.memPutShort(pointer, u);
|
| 260 | MemoryUtil.memPutShort(pointer + 2L, v);
|
| 261 | }
|
| 262 |
|
| 263 | return this;
|
| 264 | }
|
| 265 |
|
| 266 | @Override
|
| 267 | public VertexConsumer setNormal(final float x, final float y, final float z) {
|
| 268 | long pointer = this.beginElement(6);
|
| 269 | if (pointer != -1L) {
|
| 270 | putNormals(pointer, x, y, z);
|
| 271 | }
|
| 272 |
|
| 273 | return this;
|
| 274 | }
|
| 275 |
|
| 276 | @Override
|
| 277 | public VertexConsumer setLineWidth(final float width) {
|
| 278 | long pointer = this.beginElement(7);
|
| 279 | if (pointer != -1L) {
|
| 280 | MemoryUtil.memPutFloat(pointer, width);
|
| 281 | }
|
| 282 |
|
| 283 | return this;
|
| 284 | }
|
| 285 |
|
| 286 | private static byte normalIntValue(final float c) {
|
| 287 | return (byte)((int)(Mth.clamp(c, -1.0F, 1.0F) * 127.0F) & 0xFF);
|
| 288 | }
|
| 289 |
|
| 290 | private static void putVec3f(final long pointer, final float x, final float y, final float z) {
|
| 291 | MemoryUtil.memPutFloat(pointer, x);
|
| 292 | MemoryUtil.memPutFloat(pointer + 4L, y);
|
| 293 | MemoryUtil.memPutFloat(pointer + 8L, z);
|
| 294 | }
|
| 295 |
|
| 296 | private static void putNormals(final long pointer, final float nx, final float ny, final float nz) {
|
| 297 | MemoryUtil.memPutByte(pointer, normalIntValue(nx));
|
| 298 | MemoryUtil.memPutByte(pointer + 1L, normalIntValue(ny));
|
| 299 | MemoryUtil.memPutByte(pointer + 2L, normalIntValue(nz));
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public void addVertex(
|
| 304 | final float x,
|
| 305 | final float y,
|
| 306 | final float z,
|
| 307 | final int color,
|
| 308 | final float u,
|
| 309 | final float v,
|
| 310 | final int overlayCoords,
|
| 311 | final int lightCoords,
|
| 312 | final float nx,
|
| 313 | final float ny,
|
| 314 | final float nz
|
| 315 | ) {
|
| 316 | if (this.blockFormat) {
|
| 317 | long pointer = this.beginVertex();
|
| 318 | putVec3f(pointer, x, y, z);
|
| 319 | putRgba(pointer + 12L, color);
|
| 320 | MemoryUtil.memPutFloat(pointer + 16L, u);
|
| 321 | MemoryUtil.memPutFloat(pointer + 20L, v);
|
| 322 | putPackedUv(pointer + 24L, lightCoords);
|
| 323 | } else if (this.entityFormat) {
|
| 324 | long pointer = this.beginVertex();
|
| 325 | putVec3f(pointer, x, y, z);
|
| 326 | putRgba(pointer + 12L, color);
|
| 327 | MemoryUtil.memPutFloat(pointer + 16L, u);
|
| 328 | MemoryUtil.memPutFloat(pointer + 20L, v);
|
| 329 | putPackedUv(pointer + 24L, overlayCoords);
|
| 330 | putPackedUv(pointer + 28L, lightCoords);
|
| 331 | putNormals(pointer + 32L, nx, ny, nz);
|
| 332 | } else {
|
| 333 | VertexConsumer.super.addVertex(x, y, z, color, u, v, overlayCoords, lightCoords, nx, ny, nz);
|
| 334 | }
|
| 335 | }
|
| 336 | }
|
| 337 |
|