| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 4 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 5 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 6 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.renderer.MappableRingBuffer;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 | import org.lwjgl.system.MemoryUtil;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public abstract class StagingBuffer implements AutoCloseable {
|
| 16 | private int nextWriteOffset;
|
| 17 | private int usedBufferCount;
|
| 18 |
|
| 19 | public static StagingBuffer create(final String name, final GpuDevice gpuDevice, final int bufferSize) {
|
| 20 | return gpuDevice.getDeviceInfo().hintsAndWorkarounds().writeToBufferIsSlow() && gpuDevice.getDeviceInfo().features().persistentMapping()
|
| 21 | ? new StagingBuffer.PersistentlyMapped(name, bufferSize)
|
| 22 | : new StagingBuffer.Cpu(bufferSize);
|
| 23 | }
|
| 24 |
|
| 25 | public StagingBuffer.@Nullable BufferHandle tryAppend(final ByteBuffer buffer) {
|
| 26 | int writeOffset = this.nextWriteOffset;
|
| 27 | int bufferSize = buffer.remaining();
|
| 28 | ByteBuffer writeBuffer = this.getWriteBuffer();
|
| 29 | if (bufferSize > writeBuffer.capacity()) {
|
| 30 | throw new IllegalArgumentException("Cannot fit allocation of size " + bufferSize + " into staging buffer of size " + writeBuffer.capacity());
|
| 31 | }
|
| 32 |
|
| 33 | if (bufferSize > writeBuffer.capacity() - writeOffset) {
|
| 34 | return null;
|
| 35 | }
|
| 36 |
|
| 37 | MemoryUtil.memCopy(buffer, writeBuffer.position(writeOffset));
|
| 38 | this.nextWriteOffset += bufferSize;
|
| 39 | this.usedBufferCount++;
|
| 40 | return new StagingBuffer.BufferHandle(writeOffset, bufferSize);
|
| 41 | }
|
| 42 |
|
| 43 | protected abstract ByteBuffer getWriteBuffer();
|
| 44 |
|
| 45 | protected abstract void copyTo(final CommandEncoder encoder, final GpuBuffer dstBuffer, long dstOffset, long stagingBufferOffset, long copySize);
|
| 46 |
|
| 47 | protected void rotateBuffer() {
|
| 48 | }
|
| 49 |
|
| 50 | public StagingBuffer.Uploader startUploading(final CommandEncoder encoder) {
|
| 51 | return new StagingBuffer.Uploader(encoder);
|
| 52 | }
|
| 53 |
|
| 54 | private void tryClearAndRotate() {
|
| 55 | if (this.nextWriteOffset > 0 && this.usedBufferCount == 0) {
|
| 56 | this.rotateBuffer();
|
| 57 | this.nextWriteOffset = 0;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public abstract void close();
|
| 63 |
|
| 64 | @Environment(EnvType.CLIENT)
|
| 65 | public class BufferHandle implements AutoCloseable {
|
| 66 | private final int offset;
|
| 67 | private final int size;
|
| 68 | private boolean closed;
|
| 69 |
|
| 70 | public BufferHandle(final int offset, final int size) {
|
| 71 | this.offset = offset;
|
| 72 | this.size = size;
|
| 73 | }
|
| 74 |
|
| 75 | private void checkValidFor(final StagingBuffer stagingBuffer) {
|
| 76 | if (this.closed) {
|
| 77 | throw new IllegalStateException("Buffer has already been closed");
|
| 78 | }
|
| 79 |
|
| 80 | if (stagingBuffer != StagingBuffer.this) {
|
| 81 | throw new IllegalArgumentException("Buffer is not valid for " + stagingBuffer);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public long size() {
|
| 86 | return this.size;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void close() {
|
| 91 | if (!this.closed) {
|
| 92 | this.closed = true;
|
| 93 | StagingBuffer.this.usedBufferCount--;
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Environment(EnvType.CLIENT)
|
| 99 | private static class Cpu extends StagingBuffer {
|
| 100 | private final ByteBuffer stagingBuffer;
|
| 101 |
|
| 102 | private Cpu(final int bufferSize) {
|
| 103 | this.stagingBuffer = MemoryUtil.memAlloc(bufferSize);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | protected ByteBuffer getWriteBuffer() {
|
| 108 | return this.stagingBuffer;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | protected void copyTo(final CommandEncoder encoder, final GpuBuffer dstBuffer, final long dstOffset, final long stagingBufferOffset, final long copySize) {
|
| 113 | encoder.writeToBuffer(dstBuffer.slice(dstOffset, copySize), this.stagingBuffer.slice((int)stagingBufferOffset, (int)copySize));
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void close() {
|
| 118 | MemoryUtil.memFree(this.stagingBuffer);
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Environment(EnvType.CLIENT)
|
| 123 | private static class PersistentlyMapped extends StagingBuffer {
|
| 124 | private final MappableRingBuffer mappableRingBuffer;
|
| 125 | private GpuBufferSlice.MappedView currentMappedView;
|
| 126 | private GpuBuffer currentGPUBuffer;
|
| 127 | private ByteBuffer currentBuffer;
|
| 128 |
|
| 129 | private PersistentlyMapped(final String name, final int bufferSize) {
|
| 130 | this.mappableRingBuffer = new MappableRingBuffer(() -> name + " staging buffer", 18, bufferSize / 2);
|
| 131 | this.currentGPUBuffer = this.mappableRingBuffer.currentBuffer();
|
| 132 | this.currentMappedView = this.currentGPUBuffer.map(false, true);
|
| 133 | this.currentBuffer = this.currentMappedView.data();
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected ByteBuffer getWriteBuffer() {
|
| 138 | return this.currentBuffer;
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | protected void copyTo(final CommandEncoder encoder, final GpuBuffer dstBuffer, final long dstOffset, final long stagingBufferOffset, final long copySize) {
|
| 143 | encoder.copyToBuffer(this.currentGPUBuffer.slice(stagingBufferOffset, copySize), dstBuffer.slice(dstOffset, copySize));
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected void rotateBuffer() {
|
| 148 | this.currentMappedView.close();
|
| 149 | this.mappableRingBuffer.rotate();
|
| 150 | this.currentGPUBuffer = this.mappableRingBuffer.currentBuffer();
|
| 151 | this.currentMappedView = this.currentGPUBuffer.map(false, true);
|
| 152 | this.currentBuffer = this.currentMappedView.data();
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void close() {
|
| 157 | this.currentMappedView.close();
|
| 158 | this.mappableRingBuffer.close();
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | @Environment(EnvType.CLIENT)
|
| 163 | public class Uploader implements AutoCloseable {
|
| 164 | private final CommandEncoder encoder;
|
| 165 |
|
| 166 | public Uploader(final CommandEncoder encoder) {
|
| 167 | this.encoder = encoder;
|
| 168 | }
|
| 169 |
|
| 170 | public void copyTo(final StagingBuffer.BufferHandle srcBuffer, final GpuBuffer dstBuffer, final long dstOffset) {
|
| 171 | srcBuffer.checkValidFor(StagingBuffer.this);
|
| 172 | StagingBuffer.this.copyTo(this.encoder, dstBuffer, dstOffset, srcBuffer.offset, srcBuffer.size);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public void close() {
|
| 177 | StagingBuffer.this.tryClearAndRotate();
|
| 178 | }
|
| 179 |
|
| 180 | public void checkValidFor(final StagingBuffer stagingBuffer) {
|
| 181 | if (stagingBuffer != StagingBuffer.this) {
|
| 182 | throw new IllegalArgumentException("Uploader is not valid for " + stagingBuffer);
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|