| 1 | package com.mojang.renderpearl.backend.util;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator.Allocator.1;
|
| 4 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator.Allocator.Block;
|
| 5 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 6 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import java.util.function.LongFunction;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import org.jetbrains.annotations.Contract;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class TransientBlockAllocator<T extends Block> implements UncheckedAutoCloseable {
|
| 17 | private final long blockSize;
|
| 18 | private final long maxAlignment;
|
| 19 | private final TransientBlockAllocator.Allocator<T> allocator;
|
| 20 | private final Consumer<T> onBlockUse;
|
| 21 | private final ReferenceArrayList<T> specialBlocks = new ReferenceArrayList<>();
|
| 22 | private final ReferenceArrayList<T> freeBlocks = new ReferenceArrayList<>();
|
| 23 | private final ReferenceArrayList<T> usedBlocks = new ReferenceArrayList<>();
|
| 24 | private @Nullable T currentBlock;
|
| 25 | private long currentOffset = 0L;
|
| 26 |
|
| 27 | public TransientBlockAllocator(final long blockSize, final long maxAlignment, final TransientBlockAllocator.Allocator<T> allocator) {
|
| 28 | this(blockSize, maxAlignment, allocator, var0 -> {});
|
| 29 | }
|
| 30 |
|
| 31 | public TransientBlockAllocator(
|
| 32 | final long blockSize, final long maxAlignment, final TransientBlockAllocator.Allocator<T> allocator, final Consumer<T> onBlockUse
|
| 33 | ) {
|
| 34 | this.blockSize = blockSize;
|
| 35 | this.maxAlignment = maxAlignment;
|
| 36 | this.allocator = allocator;
|
| 37 | this.onBlockUse = onBlockUse;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void close() {
|
| 42 | this.rotate().run();
|
| 43 | this.rotate().run();
|
| 44 | }
|
| 45 |
|
| 46 | public long blockSize() {
|
| 47 | return this.blockSize;
|
| 48 | }
|
| 49 |
|
| 50 | public Runnable rotate() {
|
| 51 | this.currentBlock = null;
|
| 52 | this.currentOffset = this.blockSize;
|
| 53 | this.freeBlocks.forEach(this.allocator::free);
|
| 54 | this.freeBlocks.clear();
|
| 55 | if (this.usedBlocks.isEmpty() && this.specialBlocks.isEmpty()) {
|
| 56 | return () -> {};
|
| 57 | }
|
| 58 |
|
| 59 | ReferenceArrayList<T> blocksUsedThisRotation = this.usedBlocks.clone();
|
| 60 | this.usedBlocks.clear();
|
| 61 | ReferenceArrayList<T> specialBlocksUsedThisRotation = this.specialBlocks.clone();
|
| 62 | this.specialBlocks.clear();
|
| 63 | return () -> {
|
| 64 | this.freeBlocks.ensureCapacity(this.freeBlocks.size() + blocksUsedThisRotation.size());
|
| 65 |
|
| 66 | for (T usedBlock : blocksUsedThisRotation) {
|
| 67 | if (usedBlock.suboptimal()) {
|
| 68 | this.allocator.free(usedBlock);
|
| 69 | } else {
|
| 70 | this.freeBlocks.add(usedBlock);
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | specialBlocksUsedThisRotation.forEach(this.allocator::free);
|
| 75 | };
|
| 76 | }
|
| 77 |
|
| 78 | @Contract(pure = true)
|
| 79 | public boolean canAllocateInBlock(final long size, final long alignment) {
|
| 80 | return size <= this.blockSize && alignment <= this.maxAlignment;
|
| 81 | }
|
| 82 |
|
| 83 | @Contract(pure = true)
|
| 84 | public boolean canAllocateInCurrentBlock(final long size, final long alignment) {
|
| 85 | if (this.currentBlock == null && this.canAllocateInBlock(size, alignment)) {
|
| 86 | return true;
|
| 87 | }
|
| 88 |
|
| 89 | long alignedOffset = Mth.roundToward(this.currentOffset, alignment);
|
| 90 | return size <= this.blockSize - alignedOffset && alignment <= this.maxAlignment;
|
| 91 | }
|
| 92 |
|
| 93 | private T allocateBlock() {
|
| 94 | if (this.freeBlocks.isEmpty()) {
|
| 95 | this.freeBlocks.add(this.allocator.alloc(this.blockSize));
|
| 96 | }
|
| 97 |
|
| 98 | T block = this.freeBlocks.pop();
|
| 99 | this.onBlockUse.accept(block);
|
| 100 | this.usedBlocks.add(block);
|
| 101 | return block;
|
| 102 | }
|
| 103 |
|
| 104 | public TransientBlockAllocator.Allocation<T> allocate(final long size, final long alignment, final long minimumAllocation, final long elementSize) {
|
| 105 | if (alignment > this.maxAlignment) {
|
| 106 | throw new IllegalArgumentException("Alignment requirement over maximum supported alignment");
|
| 107 | }
|
| 108 |
|
| 109 | if (size == this.blockSize) {
|
| 110 | return new TransientBlockAllocator.Allocation<>(this.allocateBlock(), 0L, this.blockSize);
|
| 111 | }
|
| 112 |
|
| 113 | if (!this.canAllocateInBlock(size, alignment)) {
|
| 114 | T specialBlock = this.allocator.alloc(size);
|
| 115 | this.onBlockUse.accept(specialBlock);
|
| 116 | this.specialBlocks.add(specialBlock);
|
| 117 | return new TransientBlockAllocator.Allocation<>(specialBlock, 0L, size);
|
| 118 | }
|
| 119 |
|
| 120 | if (this.currentBlock == null) {
|
| 121 | this.currentBlock = this.allocateBlock();
|
| 122 | this.currentOffset = 0L;
|
| 123 | }
|
| 124 |
|
| 125 | if (this.canAllocateInCurrentBlock(size, alignment)) {
|
| 126 | assert this.currentBlock != null;
|
| 127 | long alignedOffset = Mth.roundToward(this.currentOffset, alignment);
|
| 128 | this.currentOffset = alignedOffset + size;
|
| 129 | T block = this.currentBlock;
|
| 130 | return new TransientBlockAllocator.Allocation<>(block, alignedOffset, size);
|
| 131 | }
|
| 132 |
|
| 133 | if (this.canAllocateInCurrentBlock(minimumAllocation, alignment)) {
|
| 134 | assert this.currentBlock != null;
|
| 135 | long alignedOffset = Mth.roundToward(this.currentOffset, alignment);
|
| 136 | long allocatedSize = (this.blockSize - alignedOffset) / elementSize * elementSize;
|
| 137 | this.currentOffset = alignedOffset + allocatedSize;
|
| 138 | T block = this.currentBlock;
|
| 139 | return new TransientBlockAllocator.Allocation<>(block, alignedOffset, allocatedSize);
|
| 140 | }
|
| 141 |
|
| 142 | T newBlock = this.allocateBlock();
|
| 143 | if (this.currentOffset > size) {
|
| 144 | this.currentBlock = newBlock;
|
| 145 | this.currentOffset = size;
|
| 146 | }
|
| 147 |
|
| 148 | return new TransientBlockAllocator.Allocation<>(newBlock, 0L, size);
|
| 149 | }
|
| 150 |
|
| 151 | @Environment(EnvType.CLIENT)
|
| 152 | public record Allocation<T>(T block, long offset, long size) {
|
| 153 | }
|
| 154 |
|
| 155 | @Environment(EnvType.CLIENT)
|
| 156 | public interface Allocator<T extends Block> {
|
| 157 | T alloc(long size);
|
| 158 |
|
| 159 | void free(T t);
|
| 160 |
|
| 161 | static <T extends Block> TransientBlockAllocator.Allocator<T> create(final LongFunction<T> alloc, final Consumer<T> free) {
|
| 162 | return new 1(alloc, free);
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 |
|