| 1 | package net.minecraft.world.level.levelgen.densityfunction;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.concurrent.ConcurrentHashMap;
|
| 7 | import java.util.concurrent.locks.ReentrantLock;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.minecraft.util.Interval;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.op.CacheFunction;
|
| 11 |
|
| 12 | public class DensityFunctionCompiler {
|
| 13 | private final DensityFunction.CompileContext context;
|
| 14 | private final Map<DensityFunction, DensitySampler> samplers = new ConcurrentHashMap<>();
|
| 15 | private final Function<DensityFunction, DensitySampler> optimizeAndCompile = this::optimizeAndCompile;
|
| 16 | private final DfRewriteRule optimizerRule = DfRewriteRule.sequence(new DfRewriteRule() {
|
| 17 | @Override
|
| 18 | public DensityFunction rewrite(DensityFunction function) {
|
| 19 | function = DfRewriteRule.INLINE_REFERENCE.rewrite(function);
|
| 20 | return function instanceof CacheFunction cache ? DensityFunctionCompiler.this.reuseOrPrepareCache(cache) : function.rewriteChildren(this);
|
| 21 | }
|
| 22 | }, DfRewriteRule.SLICE_UNIFORM_AXES);
|
| 23 | private final ReentrantLock compileLock = new ReentrantLock();
|
| 24 | private final Map<DensityFunction, DensityFunctionCompiler.PreparedCache> preparedCaches = new HashMap<>();
|
| 25 | private int nextCacheId;
|
| 26 |
|
| 27 | public DensityFunctionCompiler(final DensityFunction.CompileContext context) {
|
| 28 | this.context = context;
|
| 29 | }
|
| 30 |
|
| 31 | public DensitySampler getSampler(final DensityFunction function) {
|
| 32 | return this.samplers.computeIfAbsent(function, this.optimizeAndCompile);
|
| 33 | }
|
| 34 |
|
| 35 | private DensitySampler optimizeAndCompile(final DensityFunction function) {
|
| 36 | this.compileLock.lock();
|
| 37 |
|
| 38 | try {
|
| 39 | DensityFunction optimizedFunction = this.optimizerRule.rewrite(function);
|
| 40 | return optimizedFunction.compileSampler(this.context);
|
| 41 | } finally {
|
| 42 | this.compileLock.unlock();
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | private DensityFunction reuseOrPrepareCache(final CacheFunction cache) {
|
| 47 | DensityFunctionCompiler.PreparedCache prepared = this.preparedCaches.get(cache.input());
|
| 48 | if (prepared == null) {
|
| 49 | prepared = this.prepareCache(cache);
|
| 50 | this.preparedCaches.put(cache.input(), prepared);
|
| 51 | }
|
| 52 |
|
| 53 | return prepared;
|
| 54 | }
|
| 55 |
|
| 56 | private DensityFunctionCompiler.PreparedCache prepareCache(final CacheFunction cache) {
|
| 57 | int id = this.nextCacheId++;
|
| 58 | DensityFunction input = this.optimizerRule.rewrite(cache.input());
|
| 59 | CachingDensitySampler cachingSampler = new CachingDensitySampler(id, input.compileSampler(this.context));
|
| 60 | return new DensityFunctionCompiler.PreparedCache(id, input.range(), input.domainAxes(), cachingSampler);
|
| 61 | }
|
| 62 |
|
| 63 | private record PreparedCache(int id, Interval range, @DensityFunction.Axes int domainAxes, DensitySampler cachingSampler) implements DensityFunction {
|
| 64 | @Override
|
| 65 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 66 | return this.cachingSampler;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 71 | return this;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public MapCodec<DensityFunctionCompiler.PreparedCache> codec() {
|
| 76 | throw new UnsupportedOperationException("PreparedCache should never be encoded");
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|