| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 4 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
|
| 5 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline.CreateInfo.AttribBinding;
|
| 6 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline.CreateInfo.VertexBuffer;
|
| 7 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 8 | import java.util.Arrays;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.function.BiFunction;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.lwjgl.opengl.ARBVertexAttribBinding;
|
| 15 | import org.lwjgl.opengl.GL33C;
|
| 16 | import org.lwjgl.opengl.GLCapabilities;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public abstract sealed class VertexArray implements UncheckedAutoCloseable permits VertexArray.Emulated, VertexArray.Separate {
|
| 20 | protected final int vertexArrayId = GL33C.glGenVertexArrays();
|
| 21 |
|
| 22 | public static BiFunction<GlProgram, BackendRenderPipeline.CreateInfo, VertexArray> createSource(
|
| 23 | final GLCapabilities capabilities, final Set<String> enabledExtensions
|
| 24 | ) {
|
| 25 | if (capabilities.GL_ARB_vertex_attrib_binding && GlDevice.USE_GL_ARB_vertex_attrib_binding) {
|
| 26 | enabledExtensions.add("GL_ARB_vertex_attrib_binding");
|
| 27 | return VertexArray.Separate::new;
|
| 28 | } else {
|
| 29 | return VertexArray.Emulated::new;
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | private VertexArray() {
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void close() {
|
| 38 | GL33C.glDeleteVertexArrays(this.vertexArrayId);
|
| 39 | }
|
| 40 |
|
| 41 | public abstract void bind(final @Nullable GpuBufferSlice[] vertexBuffers);
|
| 42 |
|
| 43 | @Environment(EnvType.CLIENT)
|
| 44 | private static final class Emulated extends VertexArray {
|
| 45 | private final int[] bufferStride = new int[16];
|
| 46 | private final int[] bufferDivisor = new int[16];
|
| 47 | private final int[] bufferIndex = new int[16];
|
| 48 | private final int[] channelCount = new int[16];
|
| 49 | private final int[] glType = new int[16];
|
| 50 | private final boolean[] isInteger = new boolean[16];
|
| 51 | private final boolean[] isNormalized = new boolean[16];
|
| 52 | private final int[] elementOffset = new int[16];
|
| 53 |
|
| 54 | private Emulated(final GlProgram program, final BackendRenderPipeline.CreateInfo createInfo) {
|
| 55 | GlStateManager._glBindVertexArray(this.vertexArrayId);
|
| 56 |
|
| 57 | for (VertexBuffer vertexBuffer : createInfo.vertexBuffers()) {
|
| 58 | this.bufferStride[vertexBuffer.bufferSlot()] = vertexBuffer.stride();
|
| 59 | this.bufferDivisor[vertexBuffer.bufferSlot()] = vertexBuffer.stepRate();
|
| 60 | }
|
| 61 |
|
| 62 | Arrays.fill(this.bufferIndex, -1);
|
| 63 |
|
| 64 | for (AttribBinding attribBinding : createInfo.attribBindings()) {
|
| 65 | int attribLocation = attribBinding.location();
|
| 66 | GL33C.glEnableVertexAttribArray(attribLocation);
|
| 67 | this.bufferIndex[attribLocation] = attribBinding.bufferSlot();
|
| 68 | int glExternalId = GlConst.toGlExternalId(attribBinding.format());
|
| 69 | this.glType[attribLocation] = GlConst.toGlType(attribBinding.format());
|
| 70 | this.isInteger[attribLocation] = GlConst.isGlFormatInteger(glExternalId);
|
| 71 | this.isNormalized[attribLocation] = GlConst.isFormatNormalized(attribBinding.format());
|
| 72 | this.channelCount[attribLocation] = GlConst.glFormatChannelCount(glExternalId);
|
| 73 | this.elementOffset[attribLocation] = attribBinding.offset();
|
| 74 | }
|
| 75 |
|
| 76 | GlStateManager._glBindVertexArray(0);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public void bind(final @Nullable GpuBufferSlice[] vertexBuffers) {
|
| 81 | GlStateManager._glBindVertexArray(this.vertexArrayId);
|
| 82 |
|
| 83 | for (int attributeIndex = 0; attributeIndex < 16; attributeIndex++) {
|
| 84 | if (this.bufferIndex[attributeIndex] != -1) {
|
| 85 | int vertexBufferIndex = this.bufferIndex[attributeIndex];
|
| 86 | if (vertexBuffers[vertexBufferIndex] == null) {
|
| 87 | throw new IllegalStateException("Vertex buffer slot " + vertexBufferIndex + " not specified but required");
|
| 88 | }
|
| 89 |
|
| 90 | GlStateManager._glBindBuffer(34962, ((GlBuffer)vertexBuffers[vertexBufferIndex].buffer()).handle());
|
| 91 | long totalOffset = vertexBuffers[vertexBufferIndex].offset() + this.elementOffset[attributeIndex];
|
| 92 | if (this.isInteger[attributeIndex]) {
|
| 93 | GL33C.glVertexAttribIPointer(
|
| 94 | attributeIndex, this.channelCount[attributeIndex], this.glType[attributeIndex], this.bufferStride[vertexBufferIndex], totalOffset
|
| 95 | );
|
| 96 | } else {
|
| 97 | GL33C.glVertexAttribPointer(
|
| 98 | attributeIndex,
|
| 99 | this.channelCount[attributeIndex],
|
| 100 | this.glType[attributeIndex],
|
| 101 | this.isNormalized[attributeIndex],
|
| 102 | this.bufferStride[vertexBufferIndex],
|
| 103 | totalOffset
|
| 104 | );
|
| 105 | }
|
| 106 |
|
| 107 | GL33C.glVertexAttribDivisor(attributeIndex, this.bufferDivisor[vertexBufferIndex]);
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Environment(EnvType.CLIENT)
|
| 114 | private static final class Separate extends VertexArray {
|
| 115 | private final boolean needsMesaWorkaround;
|
| 116 | private final int[] strides = new int[16];
|
| 117 |
|
| 118 | private Separate(final GlProgram program, final BackendRenderPipeline.CreateInfo createInfo) {
|
| 119 | if ("Mesa".equals(GL33C.glGetString(7936))) {
|
| 120 | String version = GL33C.glGetString(7938);
|
| 121 | this.needsMesaWorkaround = version.contains("25.0.0") || version.contains("25.0.1") || version.contains("25.0.2");
|
| 122 | } else {
|
| 123 | this.needsMesaWorkaround = false;
|
| 124 | }
|
| 125 |
|
| 126 | GlStateManager._glBindVertexArray(this.vertexArrayId);
|
| 127 |
|
| 128 | for (VertexBuffer vertexBuffer : createInfo.vertexBuffers()) {
|
| 129 | this.strides[vertexBuffer.bufferSlot()] = vertexBuffer.stride();
|
| 130 | ARBVertexAttribBinding.glVertexBindingDivisor(vertexBuffer.bufferSlot(), vertexBuffer.stepRate());
|
| 131 | }
|
| 132 |
|
| 133 | for (AttribBinding attribBinding : createInfo.attribBindings()) {
|
| 134 | int attribLocation = attribBinding.location();
|
| 135 | GL33C.glEnableVertexAttribArray(attribLocation);
|
| 136 | int glExternalId = GlConst.toGlExternalId(attribBinding.format());
|
| 137 | int glType = GlConst.toGlType(attribBinding.format());
|
| 138 | boolean isIntegerFormat = GlConst.isGlFormatInteger(glExternalId);
|
| 139 | boolean isNormalizedFormat = GlConst.isFormatNormalized(attribBinding.format());
|
| 140 | int channelCount = GlConst.glFormatChannelCount(glExternalId);
|
| 141 | if (isIntegerFormat) {
|
| 142 | ARBVertexAttribBinding.glVertexAttribIFormat(attribLocation, channelCount, glType, attribBinding.offset());
|
| 143 | } else {
|
| 144 | ARBVertexAttribBinding.glVertexAttribFormat(attribLocation, channelCount, glType, isNormalizedFormat, attribBinding.offset());
|
| 145 | }
|
| 146 |
|
| 147 | ARBVertexAttribBinding.glVertexAttribBinding(attribLocation, attribBinding.bufferSlot());
|
| 148 | }
|
| 149 |
|
| 150 | GlStateManager._glBindVertexArray(0);
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public void bind(final @Nullable GpuBufferSlice[] vertexBuffers) {
|
| 155 | GlStateManager._glBindVertexArray(this.vertexArrayId);
|
| 156 |
|
| 157 | for (int i = 0; i < vertexBuffers.length; i++) {
|
| 158 | if (this.strides[i] != 0) {
|
| 159 | GpuBufferSlice vertexBufferSlice = vertexBuffers[i];
|
| 160 | if (vertexBufferSlice == null) {
|
| 161 | throw new IllegalStateException("Vertex buffer slot " + i + " not specified but required");
|
| 162 | }
|
| 163 |
|
| 164 | GlBuffer vertexBuffer = (GlBuffer)vertexBufferSlice.buffer();
|
| 165 | if (this.needsMesaWorkaround) {
|
| 166 | ARBVertexAttribBinding.glBindVertexBuffer(i, 0, 0L, 0);
|
| 167 | }
|
| 168 |
|
| 169 | ARBVertexAttribBinding.glBindVertexBuffer(i, vertexBuffer.handle(), vertexBufferSlice.offset(), this.strides[i]);
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|