-
Notifications
You must be signed in to change notification settings - Fork 497
/
ModelCache.java
147 lines (124 loc) · 5.41 KB
/
ModelCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package buildcraft.core.lib.client.model;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.ImmutableList;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.vertex.VertexFormat;
import buildcraft.api.core.BCLog;
import buildcraft.core.lib.config.DetailedConfigOption;
/** Implements a caching system for models with potentially infinite variants. Automatically expires entries after a
* configurable time period, and up to a maximum number. */
public class ModelCache<K> implements IModelCache<K> {
private static final DetailedConfigOption OPTION_DEBUG = new DetailedConfigOption("render.cache.debug", "false");
private final String name;
private final DetailedConfigOption optionCacheSize;
private final IModelGenerator<K> generator;
private final LoadingCache<K, ModelValue> modelCache;
private final boolean keepMutable, needGL;
private final VertexFormat glVertexFormat;
public ModelCache(String detailedName, IModelGenerator<K> generator) {
this(detailedName, 1600, generator);
}
public ModelCache(String detailedName, int defaultMaxSize, IModelGenerator<K> generator) {
this(new ModelCacheBuilder<>(detailedName, generator).setMaxSize(defaultMaxSize));
}
public ModelCache(ModelCacheBuilder<K> builder) {
this.generator = builder.generator;
this.name = builder.detailedName;
int defaultMaxSize = builder.maxSize;
optionCacheSize = new DetailedConfigOption("render.cache." + name + ".maxsize", Integer.toString(defaultMaxSize));
int maxSize = optionCacheSize.getAsInt();
if (maxSize < 0) maxSize = 0;
if (OPTION_DEBUG.getAsBoolean()) {
BCLog.logger.info("Making cache " + name + " with a maximum size of " + maxSize);
}
modelCache = CacheBuilder.newBuilder().maximumSize(maxSize).removalListener(this::onRemove).build(CacheLoader.from(this::load));
keepMutable = builder.keepMutable;
needGL = builder.needGL;
this.glVertexFormat = builder.glVertexFormat;
}
private void onRemove(RemovalNotification<K, ModelValue> notification) {
if (OPTION_DEBUG.getAsBoolean()) {
BCLog.logger.info("Cache[" + name + "]Remove: " + notification.getKey());
}
notification.getValue().cleanup();
}
private ModelValue load(K key) {
if (OPTION_DEBUG.getAsBoolean()) {
BCLog.logger.info("Cache[" + name + "]Miss: " + key);
}
return new ModelValue(generator.generate(key));
}
@Override
public void appendAsMutable(K key, List<MutableQuad> quads) {
quads.addAll(modelCache.getUnchecked(key).mutableQuads);
}
@Override
public ImmutableList<BakedQuad> bake(K key, VertexFormat format) {
ModelValue value = modelCache.getUnchecked(key);
return value.bake(format);
}
@Override
public void render(K key, WorldRenderer wr) {
for (MutableQuad q : modelCache.getUnchecked(key).mutableQuads) {
q.render(wr);
}
}
@Override
public void renderDisplayList(K key) {
ModelValue value = modelCache.getUnchecked(key);
GL11.glCallList(value.glDisplayList);
}
public interface IModelGenerator<T> {
List<MutableQuad> generate(T key);
}
private class ModelValue {
private final ImmutableList<MutableQuad> mutableQuads;
// Identity because VertexFormat is mutable, so we cannot guarentee that nothing changes it.
private Map<VertexFormat, ImmutableList<BakedQuad>> bakedQuads = new IdentityHashMap<>();
private int glDisplayList;
public ModelValue(List<MutableQuad> quads) {
if (keepMutable) mutableQuads = ImmutableList.copyOf(quads);
else mutableQuads = ImmutableList.of();
if (needGL) {
glDisplayList = GLAllocation.generateDisplayLists(1);
GL11.glNewList(glDisplayList, GL11.GL_COMPILE);
Tessellator t = Tessellator.getInstance();
WorldRenderer wr = t.getWorldRenderer();
wr.begin(GL11.GL_QUADS, glVertexFormat);
for (MutableQuad q : quads) {
q.render(wr);
}
t.draw();
GL11.glEndList();
} else {
glDisplayList = -1;
}
}
public ImmutableList<BakedQuad> bake(VertexFormat format) {
if (!bakedQuads.containsKey(format)) {
ImmutableList.Builder<BakedQuad> builder = ImmutableList.builder();
for (MutableQuad mutable : mutableQuads) {
builder.add(mutable.toUnpacked(format));
}
bakedQuads.put(format, builder.build());
}
return bakedQuads.get(format);
}
private void cleanup() {
if (glDisplayList > 0) {
GLAllocation.deleteDisplayLists(glDisplayList);
glDisplayList = -1;
}
}
}
}