| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 4 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 5 | import com.mojang.renderpearl.api.buffers.TransientMemory;
|
| 6 | import com.mojang.renderpearl.backend.opengl.GlTransientMemory.Fallback.GlAllocation;
|
| 7 | import com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.Rotation;
|
| 8 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator;
|
| 9 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator.Allocator.CpuBlock;
|
| 10 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 11 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 12 | import it.unimi.dsi.fastutil.ints.IntComparator;
|
| 13 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 14 | import java.nio.ByteBuffer;
|
| 15 | import java.util.List;
|
| 16 | import java.util.stream.IntStream;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.lwjgl.opengl.ARBBufferStorage;
|
| 22 | import org.lwjgl.opengl.GL33C;
|
| 23 | import org.lwjgl.system.MemoryUtil;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public abstract class GlTransientMemory implements TransientMemory, UncheckedAutoCloseable {
|
| 27 | private static final long BLOCK_SIZE = 524288L;
|
| 28 | private static final long MAX_CPU_ALIGNMENT = 16L;
|
| 29 | private static final long MAX_GPU_ALIGNMENT = Long.highestOneBit(Long.MAX_VALUE);
|
| 30 | final GlCommandEncoder encoder;
|
| 31 | protected final DirectStateAccess dsa;
|
| 32 | protected final BufferStorage bufferStorage;
|
| 33 | protected final GlDebugLabel debugLabels;
|
| 34 | protected final GlHeuristics heuristics;
|
| 35 | private final TransientBlockAllocator<CpuBlock> cpuBlockAllocator = new TransientBlockAllocator(524288L, 16L, CpuBlock.memalloc());
|
| 36 |
|
| 37 | GlTransientMemory(final GlDevice device, final GlCommandEncoder encoder) {
|
| 38 | this.encoder = encoder;
|
| 39 | this.dsa = device.directStateAccess();
|
| 40 | this.bufferStorage = device.getBufferStorage();
|
| 41 | this.debugLabels = device.debugLabels();
|
| 42 | this.heuristics = device.heuristics();
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void close() {
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public ByteBuffer allocateCpu(final long size, final long alignment, final long minimumAllocation, final long elementSize) {
|
| 51 | assert size <= 2147483647L;
|
| 52 | TransientBlockAllocator.Allocation<CpuBlock> alloc = this.cpuBlockAllocator.allocate(size, alignment, minimumAllocation, elementSize);
|
| 53 | return MemoryUtil.memByteBuffer(alloc.block().address() + alloc.offset(), (int)alloc.size());
|
| 54 | }
|
| 55 |
|
| 56 | public void rotate() {
|
| 57 | this.cpuBlockAllocator.rotate().run();
|
| 58 | }
|
| 59 |
|
| 60 | @Environment(EnvType.CLIENT)
|
| 61 | static class Fallback extends GlTransientMemory {
|
| 62 | private final TransientBlockAllocator<GlAllocation> blockAllocator = new TransientBlockAllocator(
|
| 63 | 524288L, GlTransientMemory.MAX_GPU_ALIGNMENT, TransientBlockAllocator.Allocator.create(this::allocateGlBlock, this::freeGlBlock)
|
| 64 | );
|
| 65 |
|
| 66 | Fallback(final GlDevice device, final GlCommandEncoder encoder) {
|
| 67 | super(device, encoder);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void close() {
|
| 72 | this.rotate();
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void rotate() {
|
| 77 | this.blockAllocator.rotate().run();
|
| 78 | }
|
| 79 |
|
| 80 | private GlAllocation allocateGlBlock(final long size) {
|
| 81 | GlBuffer buffer = this.bufferStorage.createBuffer(this.heuristics, this.dsa, 40, size);
|
| 82 | this.debugLabels.applyLabel(buffer, () -> "OpenGL Transient Buffer");
|
| 83 | long hostPtr = MemoryUtil.nmemAlloc(size);
|
| 84 | return new GlAllocation(buffer, hostPtr);
|
| 85 | }
|
| 86 |
|
| 87 | private void freeGlBlock(final GlAllocation allocation) {
|
| 88 | allocation.glBuffer.close();
|
| 89 | MemoryUtil.nmemFree(allocation.hostBuffer);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public GpuBufferSlice.MappedView allocateStaging(
|
| 94 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 95 | ) {
|
| 96 | TransientBlockAllocator.Allocation<GlAllocation> allocation = this.blockAllocator.allocate(size, alignment, minimumAllocation, elementSize);
|
| 97 | GlTransientMemory.TransientGpuBuffer transientBuffer = new GlTransientMemory.TransientGpuBuffer(
|
| 98 | allocation.block().glBuffer().handle(), usage, allocation.block().glBuffer().size()
|
| 99 | );
|
| 100 | GpuBufferSlice slice = new GpuBufferSlice(transientBuffer, allocation.offset(), allocation.size());
|
| 101 | ByteBuffer hostBuffer = MemoryUtil.memByteBuffer(allocation.block().hostBuffer + allocation.offset(), (int)allocation.size());
|
| 102 | return new GpuBufferSlice.MappedView(
|
| 103 | slice,
|
| 104 | hostBuffer,
|
| 105 | () -> this.dsa
|
| 106 | .bufferSubData(
|
| 107 | transientBuffer.handle(), slice.offset(), MemoryUtil.memByteBuffer(allocation.block().hostBuffer + allocation.offset(), (int)allocation.size()), usage
|
| 108 | )
|
| 109 | );
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public GpuBufferSlice allocateGpu(
|
| 114 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 115 | ) {
|
| 116 | TransientBlockAllocator.Allocation<GlAllocation> allocation = this.blockAllocator.allocate(size, alignment, minimumAllocation, elementSize);
|
| 117 | GlTransientMemory.TransientGpuBuffer transientBuffer = new GlTransientMemory.TransientGpuBuffer(
|
| 118 | allocation.block().glBuffer().handle(), usage, allocation.block().glBuffer().size()
|
| 119 | );
|
| 120 | return new GpuBufferSlice(transientBuffer, allocation.offset(), allocation.size());
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public GpuBufferSlice.MappedView allocateGpuMapped(
|
| 125 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 126 | ) {
|
| 127 | return this.allocateStaging(size, alignment, usage, minimumAllocation, elementSize);
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public GpuBufferSlice uploadStaging(
|
| 132 | final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 133 | ) {
|
| 134 | return this.uploadGpu(data, alignment, usage, minimumAllocation, elementSize);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public GpuBufferSlice uploadGpu(
|
| 139 | final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 140 | ) {
|
| 141 | long totalSize = 0L;
|
| 142 |
|
| 143 | for (int i = 0; i < data.size(); i++) {
|
| 144 | totalSize += data.get(i).remaining();
|
| 145 | totalSize = Mth.roundToward(totalSize, alignment);
|
| 146 | }
|
| 147 |
|
| 148 | GpuBufferSlice bufferSlice = this.allocateGpu(totalSize, alignment, usage);
|
| 149 | int target = GlUtil.selectBufferBindTarget(usage);
|
| 150 | GlStateManager._glBindBuffer(target, ((GlBuffer)bufferSlice.buffer()).handle());
|
| 151 | long ptr = GL33C.nglMapBufferRange(target, bufferSlice.offset(), totalSize, 38);
|
| 152 | long offset = 0L;
|
| 153 |
|
| 154 | for (int i = 0; i < data.size(); i++) {
|
| 155 | ByteBuffer buffer = data.get(i);
|
| 156 | MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), ptr + offset, buffer.remaining());
|
| 157 | offset += buffer.remaining();
|
| 158 | offset = Mth.roundToward(offset, alignment);
|
| 159 | }
|
| 160 |
|
| 161 | GL33C.glUnmapBuffer(target);
|
| 162 | GlStateManager._glBindBuffer(target, 0);
|
| 163 | return bufferSlice;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public List<GpuBufferSlice> multiUploadStaging(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage) {
|
| 168 | return this.multiUploadGpu(data, alignment, usage);
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public List<GpuBufferSlice> multiUploadGpu(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage) {
|
| 173 | ReferenceArrayList<GpuBufferSlice> uploadedBuffers = new ReferenceArrayList<>();
|
| 174 | uploadedBuffers.size(data.size());
|
| 175 | IntArrayList sortedDataIndices = IntArrayList.toList(IntStream.range(0, data.size()));
|
| 176 | sortedDataIndices.sort(IntComparator.comparing(index -> data.get(index).remaining()));
|
| 177 | int target = GlUtil.selectBufferBindTarget(usage);
|
| 178 |
|
| 179 | while (!sortedDataIndices.isEmpty()) {
|
| 180 | boolean allocatedAnything = false;
|
| 181 |
|
| 182 | for (int i = sortedDataIndices.size() - 1; i >= 0; i--) {
|
| 183 | int bufferIndex = sortedDataIndices.getInt(i);
|
| 184 | ByteBuffer currentBuffer = data.get(bufferIndex);
|
| 185 | if (this.blockAllocator.canAllocateInCurrentBlock(currentBuffer.remaining(), alignment)) {
|
| 186 | sortedDataIndices.removeInt(i);
|
| 187 | GpuBufferSlice bufferSlice = this.allocateGpu(currentBuffer.remaining(), alignment, usage);
|
| 188 | uploadedBuffers.set(bufferIndex, bufferSlice);
|
| 189 | GlStateManager._glBindBuffer(target, ((GlBuffer)bufferSlice.buffer()).handle());
|
| 190 | long ptr = GL33C.nglMapBufferRange(target, bufferSlice.offset(), bufferSlice.length(), 38);
|
| 191 | MemoryUtil.memCopy(MemoryUtil.memAddress(currentBuffer), ptr, bufferSlice.length());
|
| 192 | GL33C.glUnmapBuffer(target);
|
| 193 | allocatedAnything = true;
|
| 194 | break;
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | if (!allocatedAnything) {
|
| 199 | int bufferIndex = sortedDataIndices.popInt();
|
| 200 | ByteBuffer currentBuffer = data.get(bufferIndex);
|
| 201 | GpuBufferSlice bufferSlice = this.allocateGpu(currentBuffer.remaining(), alignment, usage);
|
| 202 | uploadedBuffers.set(bufferIndex, bufferSlice);
|
| 203 | GlStateManager._glBindBuffer(target, ((GlBuffer)bufferSlice.buffer()).handle());
|
| 204 | long ptr = GL33C.nglMapBufferRange(target, bufferSlice.offset(), bufferSlice.length(), 38);
|
| 205 | MemoryUtil.memCopy(MemoryUtil.memAddress(currentBuffer), ptr, bufferSlice.length());
|
| 206 | GL33C.glUnmapBuffer(target);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | GlStateManager._glBindBuffer(target, 0);
|
| 211 | return uploadedBuffers;
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | @Environment(EnvType.CLIENT)
|
| 216 | static class PersistentMapping extends GlTransientMemory {
|
| 217 | private final TransientBlockAllocator<com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation> stagingBlockAllocator;
|
| 218 | private final TransientBlockAllocator<com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation> gpuBlockAllocator;
|
| 219 | private final TransientBlockAllocator<com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation> gpuMappedBlockAllocator;
|
| 220 | private final @Nullable Rotation[] rotations = new Rotation[2];
|
| 221 |
|
| 222 | PersistentMapping(final GlDevice device, final GlCommandEncoder encoder) {
|
| 223 | super(device, encoder);
|
| 224 | this.stagingBlockAllocator = new TransientBlockAllocator(
|
| 225 | 524288L, GlTransientMemory.MAX_GPU_ALIGNMENT, TransientBlockAllocator.Allocator.create(size -> this.allocateGlBlock(size, true, true), this::freeGlBlock)
|
| 226 | );
|
| 227 | this.gpuBlockAllocator = new TransientBlockAllocator(
|
| 228 | 524288L, GlTransientMemory.MAX_GPU_ALIGNMENT, TransientBlockAllocator.Allocator.create(size -> this.allocateGlBlock(size, false, false), this::freeGlBlock)
|
| 229 | );
|
| 230 | this.gpuMappedBlockAllocator = new TransientBlockAllocator(
|
| 231 | 524288L, GlTransientMemory.MAX_GPU_ALIGNMENT, TransientBlockAllocator.Allocator.create(size -> this.allocateGlBlock(size, false, true), this::freeGlBlock)
|
| 232 | );
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public void close() {
|
| 237 | GL33C.glFinish();
|
| 238 |
|
| 239 | for (Rotation rotation : this.rotations) {
|
| 240 | if (rotation != null) {
|
| 241 | rotation.run();
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | public void rotate() {
|
| 248 | Rotation previousRotation = this.rotations[this.encoder.currentSubmitSlot()];
|
| 249 | this.rotations[this.encoder.currentSubmitSlot()] = new Rotation(
|
| 250 | this.stagingBlockAllocator.rotate(), this.gpuBlockAllocator.rotate(), this.gpuMappedBlockAllocator.rotate()
|
| 251 | );
|
| 252 | if (previousRotation != null) {
|
| 253 | previousRotation.run();
|
| 254 | }
|
| 255 |
|
| 256 | super.rotate();
|
| 257 | }
|
| 258 |
|
| 259 | private com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation allocateGlBlock(
|
| 260 | final long size, final boolean host, final boolean mapped
|
| 261 | ) {
|
| 262 | int glBuffer = this.dsa.createBuffer();
|
| 263 | int usageFlags = 0;
|
| 264 | if (host) {
|
| 265 | usageFlags |= 512;
|
| 266 | }
|
| 267 |
|
| 268 | if (mapped) {
|
| 269 | usageFlags |= 2;
|
| 270 | usageFlags |= 64;
|
| 271 | usageFlags |= 128;
|
| 272 | }
|
| 273 |
|
| 274 | GlStateManager._glBindBuffer(34962, glBuffer);
|
| 275 | ARBBufferStorage.glBufferStorage(34962, size, usageFlags);
|
| 276 | long hostPtr;
|
| 277 | if (mapped) {
|
| 278 | hostPtr = GL33C.nglMapBufferRange(34962, 0L, size, 226);
|
| 279 | } else {
|
| 280 | hostPtr = 0L;
|
| 281 | }
|
| 282 |
|
| 283 | GlStateManager._glBindBuffer(34962, 0);
|
| 284 | this.debugLabels.applyLabel(new GlTransientMemory.TransientGpuBuffer(glBuffer, 0, size), () -> "OpenGL Transient Buffer");
|
| 285 | return new com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation(glBuffer, hostPtr, size);
|
| 286 | }
|
| 287 |
|
| 288 | private void freeGlBlock(final com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation allocation) {
|
| 289 | GlStateManager._glDeleteBuffers(allocation.glBuffer);
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public GpuBufferSlice.MappedView allocateStaging(
|
| 294 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 295 | ) {
|
| 296 | assert size <= 2147483647L;
|
| 297 | TransientBlockAllocator.Allocation<com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation> alloc = this.stagingBlockAllocator
|
| 298 | .allocate(size, alignment, minimumAllocation, elementSize);
|
| 299 | GlTransientMemory.TransientGpuBuffer apiBuffer = new GlTransientMemory.TransientGpuBuffer(alloc.block().glBuffer, usage, (int)alloc.block().size);
|
| 300 | ByteBuffer cpuBuffer = MemoryUtil.memByteBuffer(alloc.block().hostPtr + alloc.offset(), (int)alloc.size());
|
| 301 | return new GpuBufferSlice.MappedView(new GpuBufferSlice(apiBuffer, alloc.offset(), alloc.size()), cpuBuffer, () -> {});
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public GpuBufferSlice allocateGpu(
|
| 306 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 307 | ) {
|
| 308 | assert size <= 2147483647L;
|
| 309 | TransientBlockAllocator.Allocation<com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation> alloc = this.gpuBlockAllocator
|
| 310 | .allocate(size, alignment, minimumAllocation, elementSize);
|
| 311 | GlTransientMemory.TransientGpuBuffer apiBuffer = new GlTransientMemory.TransientGpuBuffer(alloc.block().glBuffer, usage, (int)alloc.block().size);
|
| 312 | return new GpuBufferSlice(apiBuffer, alloc.offset(), alloc.size());
|
| 313 | }
|
| 314 |
|
| 315 | @Override
|
| 316 | public GpuBufferSlice.MappedView allocateGpuMapped(
|
| 317 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 318 | ) {
|
| 319 | assert size <= 2147483647L;
|
| 320 | TransientBlockAllocator.Allocation<com.mojang.renderpearl.backend.opengl.GlTransientMemory.PersistentMapping.GlAllocation> alloc = this.gpuMappedBlockAllocator
|
| 321 | .allocate(size, alignment, minimumAllocation, elementSize);
|
| 322 | GlTransientMemory.TransientGpuBuffer apiBuffer = new GlTransientMemory.TransientGpuBuffer(alloc.block().glBuffer, usage, (int)alloc.block().size);
|
| 323 | ByteBuffer cpuBuffer = MemoryUtil.memByteBuffer(alloc.block().hostPtr + alloc.offset(), (int)alloc.size());
|
| 324 | return new GpuBufferSlice.MappedView(new GpuBufferSlice(apiBuffer, alloc.offset(), alloc.size()), cpuBuffer, () -> {});
|
| 325 | }
|
| 326 |
|
| 327 | @Override
|
| 328 | public GpuBufferSlice uploadStaging(
|
| 329 | final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 330 | ) {
|
| 331 | return this.upload(data, alignment, usage, minimumAllocation, elementSize, true);
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | public GpuBufferSlice uploadGpu(
|
| 336 | final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 337 | ) {
|
| 338 | return this.upload(data, alignment, usage, minimumAllocation, elementSize, false);
|
| 339 | }
|
| 340 |
|
| 341 | public GpuBufferSlice upload(
|
| 342 | final List<ByteBuffer> data,
|
| 343 | final long alignment,
|
| 344 | final @GpuBuffer.Usage int usage,
|
| 345 | final long minimumAllocation,
|
| 346 | final long elementSize,
|
| 347 | final boolean staging
|
| 348 | ) {
|
| 349 | long totalSize = 0L;
|
| 350 |
|
| 351 | for (ByteBuffer buffer : data) {
|
| 352 | totalSize += buffer.remaining();
|
| 353 | totalSize = Mth.roundToward(totalSize, alignment);
|
| 354 | }
|
| 355 |
|
| 356 | try (GpuBufferSlice.MappedView mapped = staging
|
| 357 | ? this.allocateStaging(totalSize, alignment, usage, minimumAllocation, elementSize)
|
| 358 | : this.allocateGpuMapped(totalSize, alignment, usage, minimumAllocation, elementSize)) {
|
| 359 | long mappedPtr = MemoryUtil.memAddress(mapped.data());
|
| 360 | long offset = 0L;
|
| 361 |
|
| 362 | for (ByteBuffer buffer : data) {
|
| 363 | MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), mappedPtr + offset, Math.min(mapped.slice().length() - offset, buffer.remaining()));
|
| 364 | offset += buffer.remaining();
|
| 365 | offset = Mth.roundToward(offset, alignment);
|
| 366 | if (offset >= mapped.slice().length()) {
|
| 367 | break;
|
| 368 | }
|
| 369 | }
|
| 370 |
|
| 371 | return mapped.slice();
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | @Override
|
| 376 | public List<GpuBufferSlice> multiUploadStaging(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage) {
|
| 377 | return this.multiUpload(data, alignment, usage, true);
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | public List<GpuBufferSlice> multiUploadGpu(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage) {
|
| 382 | return this.multiUpload(data, alignment, usage, false);
|
| 383 | }
|
| 384 |
|
| 385 | public List<GpuBufferSlice> multiUpload(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final boolean staging) {
|
| 386 | ReferenceArrayList<GpuBufferSlice> uploadedBuffers = new ReferenceArrayList<>();
|
| 387 | uploadedBuffers.size(data.size());
|
| 388 | TransientBlockAllocator<?> allocatorInUse = staging ? this.stagingBlockAllocator : this.gpuMappedBlockAllocator;
|
| 389 | IntArrayList sortedDataIndices = IntArrayList.toList(IntStream.range(0, data.size()));
|
| 390 | sortedDataIndices.sort(IntComparator.comparing(index -> data.get(index).remaining()));
|
| 391 |
|
| 392 | while (!sortedDataIndices.isEmpty()) {
|
| 393 | boolean allocatedAnything = false;
|
| 394 |
|
| 395 | for (int i = sortedDataIndices.size() - 1; i >= 0; i--) {
|
| 396 | int bufferIndex = sortedDataIndices.getInt(i);
|
| 397 | ByteBuffer currentBuffer = data.get(bufferIndex);
|
| 398 | if (allocatorInUse.canAllocateInCurrentBlock(currentBuffer.remaining(), alignment)) {
|
| 399 | sortedDataIndices.removeInt(i);
|
| 400 |
|
| 401 | try (GpuBufferSlice.MappedView view = staging
|
| 402 | ? this.allocateStaging(currentBuffer.remaining(), alignment, usage)
|
| 403 | : this.allocateGpuMapped(currentBuffer.remaining(), alignment, usage)) {
|
| 404 | MemoryUtil.memCopy(currentBuffer, view.data());
|
| 405 | uploadedBuffers.set(bufferIndex, view.slice());
|
| 406 | }
|
| 407 |
|
| 408 | allocatedAnything = true;
|
| 409 | break;
|
| 410 | }
|
| 411 | }
|
| 412 |
|
| 413 | if (!allocatedAnything) {
|
| 414 | int bufferIndex = sortedDataIndices.popInt();
|
| 415 | ByteBuffer currentBuffer = data.get(bufferIndex);
|
| 416 |
|
| 417 | try (GpuBufferSlice.MappedView view = this.allocateGpuMapped(currentBuffer.remaining(), alignment, usage)) {
|
| 418 | MemoryUtil.memCopy(currentBuffer, view.data());
|
| 419 | uploadedBuffers.set(bufferIndex, view.slice());
|
| 420 | }
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | return uploadedBuffers;
|
| 425 | }
|
| 426 | }
|
| 427 |
|
| 428 | @Environment(EnvType.CLIENT)
|
| 429 | private class TransientGpuBuffer extends GlBuffer {
|
| 430 | private boolean closed = false;
|
| 431 | private final long bufferSubmitIndex = GlTransientMemory.this.encoder.currentSubmitIndex();
|
| 432 |
|
| 433 | protected TransientGpuBuffer(final int handle, final @GpuBuffer.Usage int usage, final long size) {
|
| 434 | super(usage, size, handle, true);
|
| 435 | }
|
| 436 |
|
| 437 | @Override
|
| 438 | public boolean isClosed() {
|
| 439 | if (this.closed) {
|
| 440 | return true;
|
| 441 | }
|
| 442 |
|
| 443 | this.closed = this.bufferSubmitIndex < GlTransientMemory.this.encoder.currentSubmitIndex();
|
| 444 | return this.closed;
|
| 445 | }
|
| 446 |
|
| 447 | @Override
|
| 448 | public void close() {
|
| 449 | this.closed = true;
|
| 450 | }
|
| 451 |
|
| 452 | @Override
|
| 453 | public GpuBufferSlice.MappedView map(final long offset, final long length, final boolean read, final boolean write) {
|
| 454 | throw new IllegalStateException("Cannot map Transient buffer");
|
| 455 | }
|
| 456 |
|
| 457 | @Override
|
| 458 | public GpuBufferSlice slice(final long offset, final long length) {
|
| 459 | throw new IllegalStateException("Cannot slice transient buffer");
|
| 460 | }
|
| 461 |
|
| 462 | @Override
|
| 463 | public GpuBufferSlice slice() {
|
| 464 | throw new IllegalStateException("Cannot slice transient buffer");
|
| 465 | }
|
| 466 | }
|
| 467 | }
|
| 468 |
|