| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 6 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 7 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 8 | import java.nio.ByteBuffer;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.List;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class DynamicGpuDataStorageMapped<T extends DynamicGpuDataStorage.DynamicGpuData> implements DynamicGpuDataStorage<T> {
|
| 19 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 20 | private final List<MappableRingBuffer> oldBuffers = new ArrayList<>();
|
| 21 | private final int blockSize;
|
| 22 | private MappableRingBuffer ringBuffer;
|
| 23 | private int nextBlock;
|
| 24 | private int capacity;
|
| 25 | private @Nullable T lastData;
|
| 26 | private final String label;
|
| 27 | private final @GpuBuffer.Usage int usage;
|
| 28 |
|
| 29 | public DynamicGpuDataStorageMapped(final String label, final int dataSize, final @GpuBuffer.Usage int usage, final int initialCapacity) {
|
| 30 | GpuDevice device = RenderSystem.getDevice();
|
| 31 | this.usage = usage;
|
| 32 | this.blockSize = usage == 128 ? Mth.roundToward(dataSize, device.getDeviceInfo().limits().minUniformOffsetAlignment()) : dataSize;
|
| 33 | this.capacity = Mth.smallestEncompassingPowerOfTwo(initialCapacity);
|
| 34 | this.nextBlock = 0;
|
| 35 | this.ringBuffer = new MappableRingBuffer(() -> label + " x" + this.blockSize, this.usage | 2, this.blockSize * this.capacity);
|
| 36 | this.label = label;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void endFrame() {
|
| 41 | this.nextBlock = 0;
|
| 42 | this.lastData = null;
|
| 43 | this.ringBuffer.rotate();
|
| 44 | if (!this.oldBuffers.isEmpty()) {
|
| 45 | for (MappableRingBuffer oldBuffer : this.oldBuffers) {
|
| 46 | oldBuffer.close();
|
| 47 | }
|
| 48 |
|
| 49 | this.oldBuffers.clear();
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | private void resizeBuffers(final int newCapacity) {
|
| 54 | this.capacity = newCapacity;
|
| 55 | this.nextBlock = 0;
|
| 56 | this.lastData = null;
|
| 57 | this.oldBuffers.add(this.ringBuffer);
|
| 58 | this.ringBuffer = new MappableRingBuffer(() -> this.label + " x" + this.blockSize, this.usage | 2, this.blockSize * this.capacity);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public GpuBufferSlice writeData(final T gpuData) {
|
| 63 | if (this.lastData != null && this.lastData.equals(gpuData)) {
|
| 64 | return this.ringBuffer.currentBuffer().slice((this.nextBlock - 1) * this.blockSize, this.blockSize);
|
| 65 | }
|
| 66 |
|
| 67 | if (this.nextBlock >= this.capacity) {
|
| 68 | int newCapacity = this.capacity * 2;
|
| 69 | LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
|
| 70 | this.resizeBuffers(newCapacity);
|
| 71 | }
|
| 72 |
|
| 73 | int offset = this.nextBlock * this.blockSize;
|
| 74 |
|
| 75 | try (GpuBufferSlice.MappedView view = this.ringBuffer.currentBuffer().slice(offset, this.blockSize).map(false, true)) {
|
| 76 | gpuData.write(view.data());
|
| 77 | }
|
| 78 |
|
| 79 | this.nextBlock++;
|
| 80 | this.lastData = gpuData;
|
| 81 | return this.ringBuffer.currentBuffer().slice(offset, this.blockSize);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public GpuBufferSlice[] writeData(final T[] dataArray) {
|
| 86 | if (dataArray.length == 0) {
|
| 87 | return new GpuBufferSlice[0];
|
| 88 | }
|
| 89 |
|
| 90 | if (this.nextBlock + dataArray.length > this.capacity) {
|
| 91 | int newCapacity = Mth.smallestEncompassingPowerOfTwo(Math.max(this.capacity + 1, dataArray.length));
|
| 92 | LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
|
| 93 | this.resizeBuffers(newCapacity);
|
| 94 | }
|
| 95 |
|
| 96 | int firstOffset = this.nextBlock * this.blockSize;
|
| 97 | GpuBufferSlice[] result = new GpuBufferSlice[dataArray.length];
|
| 98 |
|
| 99 | try (GpuBufferSlice.MappedView view = this.ringBuffer.currentBuffer().slice(firstOffset, dataArray.length * this.blockSize).map(false, true)) {
|
| 100 | ByteBuffer byteBuffer = view.data();
|
| 101 |
|
| 102 | for (int i = 0; i < dataArray.length; i++) {
|
| 103 | T data = dataArray[i];
|
| 104 | result[i] = this.ringBuffer.currentBuffer().slice(firstOffset + i * this.blockSize, this.blockSize);
|
| 105 | byteBuffer.position(i * this.blockSize);
|
| 106 | data.write(byteBuffer);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | this.nextBlock += dataArray.length;
|
| 111 | this.lastData = dataArray[dataArray.length - 1];
|
| 112 | return result;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public GpuBufferSlice writeDataBatched(final List<T> dataList) {
|
| 117 | if (dataList.isEmpty()) {
|
| 118 | return new GpuBufferSlice(null, 0L, 0L);
|
| 119 | }
|
| 120 |
|
| 121 | if (this.nextBlock + dataList.size() > this.capacity) {
|
| 122 | int newCapacity = Mth.smallestEncompassingPowerOfTwo(Math.max(this.capacity + 1, dataList.size()));
|
| 123 | LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
|
| 124 | this.resizeBuffers(newCapacity);
|
| 125 | }
|
| 126 |
|
| 127 | int firstOffset = this.nextBlock * this.blockSize;
|
| 128 | GpuBufferSlice result = this.ringBuffer.currentBuffer().slice(firstOffset, dataList.size() * this.blockSize);
|
| 129 |
|
| 130 | try (GpuBufferSlice.MappedView view = result.map(false, true)) {
|
| 131 | ByteBuffer byteBuffer = view.data();
|
| 132 |
|
| 133 | for (int i = 0; i < dataList.size(); i++) {
|
| 134 | T data = dataList.get(i);
|
| 135 | byteBuffer.position(i * this.blockSize);
|
| 136 | data.write(byteBuffer);
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | this.nextBlock = this.nextBlock + dataList.size();
|
| 141 | this.lastData = dataList.getLast();
|
| 142 | return result;
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public GpuBufferSlice[] writeDataBatchedMultiple(final List<List<T>> dataLists) {
|
| 147 | if (dataLists.isEmpty()) {
|
| 148 | return new GpuBufferSlice[0];
|
| 149 | }
|
| 150 |
|
| 151 | int totalCount = 0;
|
| 152 |
|
| 153 | for (List<T> data : dataLists) {
|
| 154 | totalCount += data.size();
|
| 155 | }
|
| 156 |
|
| 157 | if (this.nextBlock + totalCount > this.capacity) {
|
| 158 | int newCapacity = Mth.smallestEncompassingPowerOfTwo(Math.max(this.capacity + 1, totalCount));
|
| 159 | LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
|
| 160 | this.resizeBuffers(newCapacity);
|
| 161 | }
|
| 162 |
|
| 163 | int offset = this.nextBlock * this.blockSize;
|
| 164 | GpuBufferSlice[] result = new GpuBufferSlice[dataLists.size()];
|
| 165 | int bufferPositionIndex = 0;
|
| 166 |
|
| 167 | try (GpuBufferSlice.MappedView view = this.ringBuffer.currentBuffer().slice(offset, totalCount * this.blockSize).map(false, true)) {
|
| 168 | ByteBuffer byteBuffer = view.data();
|
| 169 |
|
| 170 | for (int i = 0; i < dataLists.size(); i++) {
|
| 171 | List<T> dataList = dataLists.get(i);
|
| 172 | result[i] = this.ringBuffer.currentBuffer().slice(offset, dataList.size() * this.blockSize);
|
| 173 |
|
| 174 | for (T data : dataList) {
|
| 175 | byteBuffer.position(bufferPositionIndex++ * this.blockSize);
|
| 176 | data.write(byteBuffer);
|
| 177 | }
|
| 178 |
|
| 179 | offset += dataList.size() * this.blockSize;
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | this.nextBlock += totalCount;
|
| 184 | this.lastData = dataLists.getLast().getLast();
|
| 185 | return result;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public @GpuBuffer.Usage int usage() {
|
| 190 | return this.usage;
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public void close() {
|
| 195 | for (MappableRingBuffer oldBuffer : this.oldBuffers) {
|
| 196 | oldBuffer.close();
|
| 197 | }
|
| 198 |
|
| 199 | this.ringBuffer.close();
|
| 200 | }
|
| 201 | }
|
| 202 |
|