| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.ScissorState;
|
| 4 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 5 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 6 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 7 | import com.mojang.renderpearl.api.pipeline.IndexType;
|
| 8 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
|
| 9 | import com.mojang.renderpearl.backend.api.RenderPassBackend;
|
| 10 | import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
|
| 11 | import it.unimi.dsi.fastutil.booleans.BooleanList;
|
| 12 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 13 | import it.unimi.dsi.fastutil.objects.ReferenceList;
|
| 14 | import java.nio.ByteBuffer;
|
| 15 | import java.nio.IntBuffer;
|
| 16 | import java.util.function.Supplier;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 | import org.lwjgl.PointerBuffer;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | class GlRenderPass implements RenderPassBackend {
|
| 24 | private final GlCommandEncoder encoder;
|
| 25 | private final GlDevice device;
|
| 26 | private final ScissorState defaultScissorState;
|
| 27 | protected @Nullable GlRenderPipeline pipeline;
|
| 28 | protected final @Nullable GpuBufferSlice[] vertexBuffers = new GpuBufferSlice[16];
|
| 29 | protected boolean vertexBufferDirty = true;
|
| 30 | protected @Nullable GpuBuffer indexBuffer;
|
| 31 | protected IndexType indexType = IndexType.INT;
|
| 32 | protected boolean indexBufferDirty = false;
|
| 33 | private final ScissorState scissorState = new ScissorState();
|
| 34 | protected boolean scissorStateDirty = true;
|
| 35 | protected final ReferenceList<Object> uniforms = new ReferenceArrayList<>();
|
| 36 | protected final BooleanList dirtyUniforms = new BooleanArrayList();
|
| 37 | protected boolean anyUniformDirty = false;
|
| 38 | protected @Nullable GpuBufferSlice pushConstants;
|
| 39 | protected boolean pushConstantsDirty = false;
|
| 40 | protected final int colorAttachmentCount;
|
| 41 |
|
| 42 | public GlRenderPass(final GlCommandEncoder encoder, final GlDevice device, final int colorAttachmentCount, final ScissorState defaultScissorState) {
|
| 43 | this.encoder = encoder;
|
| 44 | this.device = device;
|
| 45 | this.colorAttachmentCount = colorAttachmentCount;
|
| 46 | this.defaultScissorState = defaultScissorState;
|
| 47 | this.scissorState.setFrom(defaultScissorState);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void pushDebugGroup(final Supplier<String> label) {
|
| 52 | this.device.debugLabels().pushDebugGroup(label);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void popDebugGroup() {
|
| 57 | this.device.debugLabels().popDebugGroup();
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void setPipeline(final BackendRenderPipeline pipeline) {
|
| 62 | if (!(pipeline instanceof GlRenderPipeline glRenderPipeline)) {
|
| 63 | throw new IllegalArgumentException("Pipeline must be instance of GlRenderPipeline");
|
| 64 | } else {
|
| 65 | if (this.pipeline == null || this.pipeline != pipeline) {
|
| 66 | this.uniforms.clear();
|
| 67 | this.uniforms.size(glRenderPipeline.program().uniformCount());
|
| 68 | this.dirtyUniforms.clear();
|
| 69 | this.dirtyUniforms.size(glRenderPipeline.program().uniformCount());
|
| 70 |
|
| 71 | for (int i = 0; i < this.dirtyUniforms.size(); i++) {
|
| 72 | this.dirtyUniforms.set(i, true);
|
| 73 | }
|
| 74 |
|
| 75 | this.anyUniformDirty = true;
|
| 76 | }
|
| 77 |
|
| 78 | this.pipeline = glRenderPipeline;
|
| 79 | this.vertexBufferDirty = true;
|
| 80 | this.indexBufferDirty = this.indexBuffer != null;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void setUniform(final int index, final @Nullable Object value) {
|
| 86 | this.uniforms.set(index, value);
|
| 87 | this.dirtyUniforms.set(index, true);
|
| 88 | this.anyUniformDirty = true;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void pushConstants(final ByteBuffer value) {
|
| 93 | this.pushConstants = this.encoder.transientMemory().uploadGpu(value, this.device.getDeviceInfo().limits().minUniformOffsetAlignment(), 128);
|
| 94 | this.pushConstantsDirty = true;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public void enableScissor(final int x, final int y, final int width, final int height) {
|
| 99 | this.scissorState.enable(x, y, width, height);
|
| 100 | this.scissorStateDirty = true;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public void disableScissor() {
|
| 105 | this.scissorState.setFrom(this.defaultScissorState);
|
| 106 | this.scissorStateDirty = true;
|
| 107 | }
|
| 108 |
|
| 109 | public boolean isScissorEnabled() {
|
| 110 | return this.scissorState.enabled();
|
| 111 | }
|
| 112 |
|
| 113 | public int getScissorX() {
|
| 114 | return this.scissorState.x();
|
| 115 | }
|
| 116 |
|
| 117 | public int getScissorY() {
|
| 118 | return this.scissorState.y();
|
| 119 | }
|
| 120 |
|
| 121 | public int getScissorWidth() {
|
| 122 | return this.scissorState.width();
|
| 123 | }
|
| 124 |
|
| 125 | public int getScissorHeight() {
|
| 126 | return this.scissorState.height();
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public void setVertexBuffer(final int slot, final @Nullable GpuBufferSlice vertexBuffer) {
|
| 131 | GpuBuffer inputBuffer = vertexBuffer != null ? vertexBuffer.buffer() : null;
|
| 132 | GpuBuffer existingBuffer = this.vertexBuffers[slot] != null ? this.vertexBuffers[slot].buffer() : null;
|
| 133 | long inputOffset = vertexBuffer != null ? vertexBuffer.offset() : 0L;
|
| 134 | long exitingOffset = this.vertexBuffers[slot] != null ? this.vertexBuffers[slot].offset() : 0L;
|
| 135 | this.vertexBufferDirty |= inputBuffer != existingBuffer || inputOffset != exitingOffset;
|
| 136 | this.vertexBuffers[slot] = vertexBuffer;
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public void setIndexBuffer(final @Nullable GpuBuffer indexBuffer, final IndexType indexType) {
|
| 141 | this.indexBuffer = indexBuffer;
|
| 142 | this.indexType = indexType;
|
| 143 | this.indexBufferDirty = true;
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public void drawIndexed(final int indexCount, final int instanceCount, final int firstIndex, final int vertexOffset, final int firstInstance) {
|
| 148 | this.encoder.executeDraw(this, vertexOffset, firstIndex, indexCount, this.indexType, instanceCount, firstInstance);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public void multiDrawIndexed(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount) {
|
| 153 | throw new UnsupportedOperationException("OpenGL does not support the multiDrawDirectInterleaved device feature");
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public void multiDrawIndexed(final PointerBuffer firstIndexOffsets, final IntBuffer indexCounts, final IntBuffer vertexOffsets, final int drawCount) {
|
| 158 | this.encoder.executeDraws(this, this.indexType, firstIndexOffsets, indexCounts, vertexOffsets, drawCount);
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public void drawIndexedIndirect(final GpuBufferSlice commands, final int drawCount) {
|
| 163 | this.encoder.executeDrawIndirect(this, this.indexType, (GlBuffer)commands.buffer(), commands.offset(), drawCount);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void draw(final int vertexCount, final int instanceCount, final int firstVertex, final int firstInstance) {
|
| 168 | this.encoder.executeDraw(this, firstVertex, 0, vertexCount, null, instanceCount, firstInstance);
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public void multiDraw(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount) {
|
| 173 | throw new UnsupportedOperationException("OpenGL does not support the multiDrawDirectInterleaved device feature");
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public void multiDraw(final IntBuffer firstVertices, final IntBuffer vertexCounts, final int drawCount) {
|
| 178 | this.encoder.executeDraws(this, null, null, vertexCounts, firstVertices, drawCount);
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public void drawIndirect(final GpuBufferSlice commands, final int drawCount) {
|
| 183 | this.encoder.executeDrawIndirect(this, null, (GlBuffer)commands.buffer(), commands.offset(), drawCount);
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public void writeTimestamp(final GpuQueryPool pool, final int index) {
|
| 188 | ((GlQueryPool)pool).writeTimestamp(index);
|
| 189 | }
|
| 190 | }
|
| 191 |
|