| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 5 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 8 | import java.nio.ByteBuffer;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.HashMap;
|
| 11 | import java.util.Iterator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Locale;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Map.Entry;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.util.VisibleForDebug;
|
| 19 | import net.minecraft.util.profiling.Profiler;
|
| 20 | import net.minecraft.util.profiling.Zone;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class UberGpuBuffer<T> implements AutoCloseable {
|
| 25 | private final @GpuBuffer.Usage int bufferUsage;
|
| 26 | private final int heapSize;
|
| 27 | private final int alignSize;
|
| 28 | private final String name;
|
| 29 | private final List<Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap>> nodes = new ArrayList<>();
|
| 30 | private final StagingBuffer stagingBuffer;
|
| 31 | private final Object2ObjectOpenHashMap<T, UberGpuBuffer.StagedAllocationEntry<? extends T>> stagedAllocations = new Object2ObjectOpenHashMap<>(32);
|
| 32 | private final ObjectOpenHashSet<T> skippedStagedAllocations = new ObjectOpenHashSet<>(32);
|
| 33 | private final Map<T, TlsfAllocator.Allocation> allocationMap = new HashMap<>(256);
|
| 34 |
|
| 35 | public UberGpuBuffer(final String name, final @GpuBuffer.Usage int bufferUsage, final int heapSize, final int alignSize, final StagingBuffer stagingBuffer) {
|
| 36 | this.name = "UberBuffer " + name;
|
| 37 | this.bufferUsage = bufferUsage;
|
| 38 | this.heapSize = heapSize;
|
| 39 | this.alignSize = alignSize;
|
| 40 | this.stagingBuffer = stagingBuffer;
|
| 41 | }
|
| 42 |
|
| 43 | public <U extends T> boolean addAllocation(final U allocationKey, final UberGpuBuffer.UploadCallback<U> callback, final ByteBuffer buffer) {
|
| 44 | StagingBuffer.BufferHandle handle = this.stagingBuffer.tryAppend(buffer);
|
| 45 | if (handle == null) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | UberGpuBuffer.StagedAllocationEntry<U> entry = new UberGpuBuffer.StagedAllocationEntry<>(handle, callback);
|
| 50 | UberGpuBuffer.StagedAllocationEntry<? extends T> oldEntry = this.stagedAllocations.put((T)allocationKey, entry);
|
| 51 | if (oldEntry != null) {
|
| 52 | oldEntry.close();
|
| 53 | }
|
| 54 |
|
| 55 | return true;
|
| 56 | }
|
| 57 |
|
| 58 | public boolean uploadStagedAllocations(final GpuDevice gpuDevice, final StagingBuffer.Uploader uploader) {
|
| 59 | uploader.checkValidFor(this.stagingBuffer);
|
| 60 |
|
| 61 | for (T key : this.stagedAllocations.keySet()) {
|
| 62 | this.freeAllocation(key);
|
| 63 | }
|
| 64 |
|
| 65 | boolean newHeapCreatedOrDestroyed = false;
|
| 66 |
|
| 67 | try (Zone var22 = Profiler.get().zone("uploadStagedAllocations")) {
|
| 68 | for (Entry<T, UberGpuBuffer.StagedAllocationEntry<? extends T>> entry : this.stagedAllocations.entrySet()) {
|
| 69 | try (UberGpuBuffer.StagedAllocationEntry<? extends T> staged = entry.getValue()) {
|
| 70 | long allocationSize = staged.buffer.size();
|
| 71 | if (!this.skippedStagedAllocations.contains(entry.getKey())) {
|
| 72 | TlsfAllocator.Allocation allocation = null;
|
| 73 |
|
| 74 | for (Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap> node : this.nodes) {
|
| 75 | allocation = node.getFirst().allocate(allocationSize, this.alignSize);
|
| 76 | if (allocation != null) {
|
| 77 | break;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | if (allocation == null) {
|
| 82 | try (Zone var25 = Profiler.get().zone("createNewHeap")) {
|
| 83 | assert allocationSize <= this.heapSize;
|
| 84 | String heapName = String.format(Locale.ROOT, "%s %d", this.name, this.nodes.size());
|
| 85 | UberGpuBuffer.UberGpuBufferHeap newHeap = new UberGpuBuffer.UberGpuBufferHeap(this.heapSize, gpuDevice, this.bufferUsage, heapName);
|
| 86 | TlsfAllocator newTlsfAllocator = new TlsfAllocator(newHeap);
|
| 87 | this.nodes.add(new Pair<>(newTlsfAllocator, newHeap));
|
| 88 | allocation = newTlsfAllocator.allocate(allocationSize, this.alignSize);
|
| 89 | newHeapCreatedOrDestroyed = true;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | if (allocation != null) {
|
| 94 | TlsfAllocator.Heap allocationHeap = allocation.getHeap();
|
| 95 | GpuBuffer allocationDestBuffer = ((UberGpuBuffer.UberGpuBufferHeap)allocationHeap).gpuBuffer;
|
| 96 | uploader.copyTo(staged.buffer, allocationDestBuffer, allocation.getOffsetFromHeap());
|
| 97 | this.allocationMap.put(entry.getKey(), allocation);
|
| 98 | runCallbackUnchecked(entry.getKey(), entry.getValue());
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | this.stagedAllocations.clear();
|
| 105 | this.skippedStagedAllocations.clear();
|
| 106 | }
|
| 107 |
|
| 108 | Iterator<Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap>> iterator = this.nodes.iterator();
|
| 109 |
|
| 110 | while (iterator.hasNext()) {
|
| 111 | Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap> node = iterator.next();
|
| 112 | if (node.getFirst().isCompletelyFree()) {
|
| 113 | node.getSecond().gpuBuffer.close();
|
| 114 | iterator.remove();
|
| 115 | newHeapCreatedOrDestroyed = true;
|
| 116 | break;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | return newHeapCreatedOrDestroyed;
|
| 121 | }
|
| 122 |
|
| 123 | private static <T, U extends T> void runCallbackUnchecked(final T key, final UberGpuBuffer.StagedAllocationEntry<U> value) {
|
| 124 | if (value.callback != null) {
|
| 125 | value.callback.bufferHasBeenUploaded((U)key);
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | public TlsfAllocator.@Nullable Allocation getAllocation(final T allocationKey) {
|
| 130 | return this.allocationMap.get(allocationKey);
|
| 131 | }
|
| 132 |
|
| 133 | public void removeAllocation(final T allocationKey) {
|
| 134 | this.skippedStagedAllocations.add(allocationKey);
|
| 135 | this.freeAllocation(allocationKey);
|
| 136 | }
|
| 137 |
|
| 138 | private void freeAllocation(final T allocationKey) {
|
| 139 | TlsfAllocator.Allocation allocation = this.allocationMap.remove(allocationKey);
|
| 140 | if (allocation != null) {
|
| 141 | for (Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap> node : this.nodes) {
|
| 142 | if (node.getSecond() == allocation.getHeap()) {
|
| 143 | node.getFirst().free(allocation);
|
| 144 | break;
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | public GpuBuffer getGpuBuffer(final TlsfAllocator.Allocation allocation) {
|
| 151 | return ((UberGpuBuffer.UberGpuBufferHeap)allocation.getHeap()).gpuBuffer;
|
| 152 | }
|
| 153 |
|
| 154 | @VisibleForDebug
|
| 155 | public void printStatistics() {
|
| 156 | for (int i = 0; i < this.nodes.size(); i++) {
|
| 157 | Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap> node = this.nodes.get(i);
|
| 158 | String heapName = String.format(Locale.ROOT, "%s %d", this.name, i);
|
| 159 | node.getFirst().printAllocatorStatistics(heapName);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public void close() {
|
| 165 | this.stagedAllocations.values().forEach(UberGpuBuffer.StagedAllocationEntry::close);
|
| 166 | this.stagedAllocations.clear();
|
| 167 | this.allocationMap.clear();
|
| 168 |
|
| 169 | for (Pair<TlsfAllocator, UberGpuBuffer.UberGpuBufferHeap> node : this.nodes) {
|
| 170 | node.getSecond().gpuBuffer.close();
|
| 171 | }
|
| 172 |
|
| 173 | this.nodes.clear();
|
| 174 | }
|
| 175 |
|
| 176 | @Environment(EnvType.CLIENT)
|
| 177 | private record StagedAllocationEntry<T>(StagingBuffer.BufferHandle buffer, UberGpuBuffer.@Nullable UploadCallback<T> callback) implements AutoCloseable {
|
| 178 | @Override
|
| 179 | public void close() {
|
| 180 | this.buffer.close();
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Environment(EnvType.CLIENT)
|
| 185 | public static class UberGpuBufferHeap extends TlsfAllocator.Heap {
|
| 186 | private final GpuBuffer gpuBuffer;
|
| 187 |
|
| 188 | public UberGpuBufferHeap(final long size, final GpuDevice gpuDevice, final @GpuBuffer.Usage int usage, final String name) {
|
| 189 | super(size);
|
| 190 | this.gpuBuffer = gpuDevice.createBuffer(() -> name, usage | 8 | 16, size);
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | @Environment(EnvType.CLIENT)
|
| 195 | public interface UploadCallback<T> {
|
| 196 | void bufferHasBeenUploaded(T key);
|
| 197 | }
|
| 198 | }
|
| 199 |
|