| 1 | package com.mojang.renderpearl.api.vertex;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.GpuFormat;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.stream.Collectors;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class VertexFormat {
|
| 16 | private static final int VERTEX_ALIGNMENT = 4;
|
| 17 | public static final int MAX_VERTEX_ELEMENTS = 16;
|
| 18 | private final Map<String, VertexFormatElement> elements = new Object2ObjectArrayMap<>(16);
|
| 19 | private final int vertexSize;
|
| 20 | private final int stepRate;
|
| 21 | private final List<VertexFormatElement> elementValues;
|
| 22 |
|
| 23 | private VertexFormat(final List<VertexFormatElement> elements, final int vertexSize, final int stepRate) {
|
| 24 | this.vertexSize = vertexSize;
|
| 25 | this.stepRate = stepRate;
|
| 26 |
|
| 27 | for (VertexFormatElement element : elements) {
|
| 28 | this.elements.putIfAbsent(element.name(), element);
|
| 29 | }
|
| 30 |
|
| 31 | this.elementValues = elements;
|
| 32 | }
|
| 33 |
|
| 34 | public static VertexFormat.Builder builder(final int stepRate) {
|
| 35 | return new VertexFormat.Builder(stepRate);
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public String toString() {
|
| 40 | return "VertexFormat" + this.elementValues.stream().map(VertexFormatElement::name).collect(Collectors.joining(", ", "[", "]"));
|
| 41 | }
|
| 42 |
|
| 43 | public int getVertexSize() {
|
| 44 | return this.vertexSize;
|
| 45 | }
|
| 46 |
|
| 47 | public int getStepRate() {
|
| 48 | return this.stepRate;
|
| 49 | }
|
| 50 |
|
| 51 | public List<VertexFormatElement> getElements() {
|
| 52 | return this.elementValues;
|
| 53 | }
|
| 54 |
|
| 55 | public @Nullable VertexFormatElement getElement(final String attributeName) {
|
| 56 | return this.elements.get(attributeName);
|
| 57 | }
|
| 58 |
|
| 59 | public boolean contains(final String attributeName) {
|
| 60 | return this.elements.containsKey(attributeName);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public boolean equals(final Object o) {
|
| 65 | return this == o ? true : o instanceof VertexFormat format && this.elements.equals(format.elements) && this.vertexSize == format.vertexSize;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public int hashCode() {
|
| 70 | return this.elementValues.hashCode();
|
| 71 | }
|
| 72 |
|
| 73 | @Environment(EnvType.CLIENT)
|
| 74 | public static class Builder {
|
| 75 | private final List<VertexFormatElement> elements = new ArrayList<>(16);
|
| 76 | private int offset = 0;
|
| 77 | private final int stepRate;
|
| 78 |
|
| 79 | private Builder(final int stepRate) {
|
| 80 | this.stepRate = stepRate;
|
| 81 | }
|
| 82 |
|
| 83 | private void createAttribute(final String name, final int offset, final GpuFormat elementFormat) {
|
| 84 | if (this.elements.size() >= 16) {
|
| 85 | throw new IllegalArgumentException("Having more than 16 attributes are not supported");
|
| 86 | }
|
| 87 |
|
| 88 | if (!Mth.isMultipleOf(offset, elementFormat.byteAlignment())) {
|
| 89 | throw new IllegalArgumentException(name + " is not aligned to " + elementFormat.byteAlignment() + " as required by " + elementFormat);
|
| 90 | }
|
| 91 |
|
| 92 | VertexFormatElement element = new VertexFormatElement(name, offset, elementFormat);
|
| 93 | this.elements.add(element);
|
| 94 | }
|
| 95 |
|
| 96 | private void validateUniqueName(final String name) {
|
| 97 | for (VertexFormatElement element : this.elements) {
|
| 98 | if (element.name().equals(name)) {
|
| 99 | throw new IllegalArgumentException("Another vertex attribute exists with the name " + name);
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public VertexFormat.Builder addAttribute(final String name, final GpuFormat elementFormat) {
|
| 105 | this.validateUniqueName(name);
|
| 106 | this.createAttribute(name, this.offset, elementFormat);
|
| 107 | this.offset = this.offset + elementFormat.blockSize();
|
| 108 | return this;
|
| 109 | }
|
| 110 |
|
| 111 | public VertexFormat.Builder addAttribute(final String name, final int stride, final GpuFormat elementFormat) {
|
| 112 | this.validateUniqueName(name);
|
| 113 | this.createAttribute(name, this.offset, elementFormat);
|
| 114 | this.offset += stride;
|
| 115 | return this;
|
| 116 | }
|
| 117 |
|
| 118 | public VertexFormat.Builder addAttribute(final String name, final GpuFormat elementFormat, final int columnCount) {
|
| 119 | this.validateUniqueName(name);
|
| 120 |
|
| 121 | for (int i = 0; i < columnCount; i++) {
|
| 122 | this.createAttribute(name, this.offset, elementFormat);
|
| 123 | this.offset = this.offset + elementFormat.blockSize();
|
| 124 | }
|
| 125 |
|
| 126 | return this;
|
| 127 | }
|
| 128 |
|
| 129 | public VertexFormat.Builder addAttribute(final String name, final int offset, final int stride, final GpuFormat elementFormat, final int columnCount) {
|
| 130 | this.validateUniqueName(name);
|
| 131 | int offsetTracker = offset;
|
| 132 |
|
| 133 | for (int i = 0; i < columnCount; i++) {
|
| 134 | this.createAttribute(name, offsetTracker, elementFormat);
|
| 135 | offsetTracker += stride;
|
| 136 | }
|
| 137 |
|
| 138 | this.offset = Math.max(this.offset, offsetTracker);
|
| 139 | return this;
|
| 140 | }
|
| 141 |
|
| 142 | public VertexFormat build() {
|
| 143 | int vertexSize = this.offset;
|
| 144 | if (!Mth.isMultipleOf(vertexSize, 4)) {
|
| 145 | throw new IllegalStateException("Vertex size must be a multiple of 4, was " + vertexSize);
|
| 146 | } else {
|
| 147 | return new VertexFormat(this.elements, vertexSize, this.stepRate);
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|