Skip to content

Commit

Permalink
Fix block mesh caching
Browse files Browse the repository at this point in the history
- Fix block mesh caching by properly implementing equals method on key objects; Fix #20; Fix #24
- Fix atlas stitch handler running for all atlases
- Use alternative unbakedModelGetter to prevent resource loading errors with mods that do not properly load custom model dependencies; Fix #22; Fix #30
- Reuse TextureContextMap instances
- Fix resource leak when getting CTM metadata
- De-multithread AtlasStitchCallbackHandler and ResourceUtil
- Enforce texture collection ordering and size
- Rename some classes and methods and change some visibility modifiers
  • Loading branch information
PepperCode1 committed Aug 6, 2021
1 parent f1d6b6e commit 3721745
Show file tree
Hide file tree
Showing 30 changed files with 176 additions and 152 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modCompileOnly "com.terraformersmc:modmenu:${project.mod_menu_version}"
modCompileOnly("com.terraformersmc:modmenu:${project.mod_menu_version}") {
exclude(group: "net.fabricmc.fabric-api")
}

afterEvaluate {
testmodImplementation sourceSets.main.output
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
org.gradle.jvmargs = -Xmx1G

# Fabric Properties
minecraft_version = 1.17
minecraft_version = 1.17.1
# https://maven.fabricmc.net/net/fabricmc/yarn/
yarn_mappings = 1.17+build.13
yarn_mappings = 1.17.1+build.32
# https://maven.fabricmc.net/net/fabricmc/fabric-loader/
loader_version = 0.11.6
# https://maven.fabricmc.net/net/fabricmc/fabric-loom/
loom_version = 0.8-SNAPSHOT
loom_version = 0.9-SNAPSHOT

# Mod Properties
mod_version = 0.5.0+1.17
mod_version = 0.5.1+1.17
maven_group = team.chisel
archives_base_name = ctm-fabric

# Dependencies
# https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/
fabric_version = 0.36.0+1.17
fabric_version = 0.37.1+1.17
# https://maven.terraformersmc.com/releases/com/terraformersmc/modmenu/
mod_menu_version = 2.0.2
mod_menu_version = 2.0.4

checkstyle_version = 8.44
4 changes: 2 additions & 2 deletions src/main/java/team/chisel/ctm/api/client/ContextProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public interface ContextProvider {
*
* <p>As of yet, this method is unused.
*
* @param data The compressed data, which will match what is produced by {@link TextureContext#getCompressedData()}.
* @param data The compressed data, which will match what is produced by {@link TextureContext#serialize()}.
*/
@Deprecated
default TextureContext getContextFromData(long data) {
default TextureContext deserializeContext(long data) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public interface TextureContext {
/**
* Gets the compressed data. Will only use bits up to the given compressed data length.
*/
long getCompressedData();
long serialize();
}
16 changes: 8 additions & 8 deletions src/main/java/team/chisel/ctm/client/CTMClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import team.chisel.ctm.client.event.DeserializeModelJsonCallback;
import team.chisel.ctm.client.event.ModelsAddedCallback;
import team.chisel.ctm.client.event.ModelsLoadedCallback;
import team.chisel.ctm.client.handler.CTMAtlasStitchCallbackHandler;
import team.chisel.ctm.client.handler.CTMDeserializeModelJsonCallbackHandler;
import team.chisel.ctm.client.handler.CTMModelsAddedCallbackHandler;
import team.chisel.ctm.client.handler.CTMModelsLoadedCallbackHandler;
import team.chisel.ctm.client.handler.AtlasStitchCallbackHandler;
import team.chisel.ctm.client.handler.DeserializeModelJsonCallbackHandler;
import team.chisel.ctm.client.handler.ModelsAddedCallbackHandler;
import team.chisel.ctm.client.handler.ModelsLoadedCallbackHandler;
import team.chisel.ctm.client.texture.type.TextureTypeCTM;
import team.chisel.ctm.client.texture.type.TextureTypeEdges;
import team.chisel.ctm.client.texture.type.TextureTypeEdgesFull;
Expand Down Expand Up @@ -61,10 +61,10 @@ public void onInitializeClient() {
}

Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap = new HashMap<>();
DeserializeModelJsonCallback.EVENT.register(new CTMDeserializeModelJsonCallbackHandler(jsonOverrideMap));
ModelsAddedCallback.EVENT.register(new CTMModelsAddedCallbackHandler(jsonOverrideMap));
AtlasStitchCallback.EVENT.register(new CTMAtlasStitchCallbackHandler());
ModelsLoadedCallback.EVENT.register(new CTMModelsLoadedCallbackHandler());
DeserializeModelJsonCallback.EVENT.register(new DeserializeModelJsonCallbackHandler(jsonOverrideMap));
ModelsAddedCallback.EVENT.register(new ModelsAddedCallbackHandler(jsonOverrideMap));
AtlasStitchCallback.EVENT.register(new AtlasStitchCallbackHandler());
ModelsLoadedCallback.EVENT.register(new ModelsLoadedCallbackHandler());

TextureType type;
TextureTypeRegistry.INSTANCE.register("ctm", new TextureTypeCTM());
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/team/chisel/ctm/client/config/CTMConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CTMConfigScreen(Screen parent, ConfigManager configManager) {

@Override
protected void init() {
addDrawableChild(new ButtonWidget(width / 2 - 90 - 75, (int) (height * 0.5F) - 10, 150, 20, getBooleanOptionText("options.ctm.disable_ctm", configManager.getConfig().disableCTM),
addDrawableChild(new ButtonWidget(width / 2 - 90 - 75, height / 2 - 10, 150, 20, getBooleanOptionText("options.ctm.disable_ctm", configManager.getConfig().disableCTM),
(button) -> {
boolean value = !configManager.getConfig().disableCTM;
button.setMessage(getBooleanOptionText("options.ctm.disable_ctm", value));
Expand All @@ -34,7 +34,7 @@ protected void init() {
}
));

addDrawableChild(new ButtonWidget(width / 2 + 90 - 75, (int) (height * 0.5F) - 10, 150, 20, getBooleanOptionText("options.ctm.connect_inside_ctm", configManager.getConfig().connectInsideCTM),
addDrawableChild(new ButtonWidget(width / 2 + 90 - 75, height / 2 - 10, 150, 20, getBooleanOptionText("options.ctm.connect_inside_ctm", configManager.getConfig().connectInsideCTM),
(button) -> {
boolean value = !configManager.getConfig().connectInsideCTM;
button.setMessage(getBooleanOptionText("options.ctm.connect_inside_ctm", value));
Expand All @@ -45,19 +45,19 @@ protected void init() {
}
));

addDrawableChild(new ButtonWidget(width / 2 - 100, (int) (height * 0.88F) - 10, 200, 20, ScreenTexts.DONE, (button) -> onClose()));
addDrawableChild(new ButtonWidget(width / 2 - 100, (int) (height * 0.8F), 200, 20, ScreenTexts.DONE, (button) -> onClose()));
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
renderBackground(matrices);
drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF);
drawCenteredText(matrices, textRenderer, title, width / 2, (int) (height * 0.15F), 0xFFFFFF);
super.render(matrices, mouseX, mouseY, delta);
}

@Override
public void onClose() {
client.openScreen(parent);
client.setScreen(parent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package team.chisel.ctm.client.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import com.google.common.collect.Sets;

import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;

import team.chisel.ctm.client.CTMClient;
import team.chisel.ctm.client.event.AtlasStitchCallback;
import team.chisel.ctm.client.resource.CTMMetadataSection;
import team.chisel.ctm.client.util.ResourceUtil;

public class CTMAtlasStitchCallbackHandler implements AtlasStitchCallback {
public class AtlasStitchCallbackHandler implements AtlasStitchCallback {
@Override
public void onAtlasStitch(SpriteAtlasTexture atlas, Set<Identifier> sprites) {
Set<Identifier> newSprites = Sets.newConcurrentHashSet();
List<CompletableFuture<Void>> futures = new ArrayList<>(sprites.size());
if (!atlas.getId().equals(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE)) {
return;
}

Set<Identifier> newSprites = new HashSet<>();
for (Identifier identifier : sprites) {
futures.add(CompletableFuture.runAsync(() -> addSprites(identifier, newSprites), Util.getMainWorkerExecutor()));
addSprites(identifier, newSprites);
}

CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
sprites.addAll(newSprites);
}

private void addSprites(Identifier identifier, Set<Identifier> newSprites) {
CTMMetadataSection metadata = ResourceUtil.getMetadataSafe(ResourceUtil.toTextureIdentifier(identifier));
if (metadata != null) {
addSprites(identifier, metadata, newSprites);
// Load proxy data
if (metadata.getProxy() != null) {
Identifier proxy = metadata.getProxy();
Expand All @@ -43,6 +37,8 @@ private void addSprites(Identifier identifier, Set<Identifier> newSprites) {
if (proxyMetadata != null) {
addSprites(proxy, proxyMetadata, newSprites);
}
} else {
addSprites(identifier, metadata, newSprites);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import team.chisel.ctm.client.resource.ModelParser;
import team.chisel.ctm.client.resource.ModelParserV1;

public class CTMDeserializeModelJsonCallbackHandler implements DeserializeModelJsonCallback {
public class DeserializeModelJsonCallbackHandler implements DeserializeModelJsonCallback {
private static final Map<Integer, ModelParser> PARSERS = new ImmutableMap.Builder<Integer, ModelParser>()
.put(1, new ModelParserV1())
.build();

private final Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap;

public CTMDeserializeModelJsonCallbackHandler(Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap) {
public DeserializeModelJsonCallbackHandler(Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap) {
this.jsonOverrideMap = jsonOverrideMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import com.google.gson.JsonElement;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

import net.minecraft.client.render.model.ModelLoader;
Expand All @@ -24,17 +26,27 @@
import team.chisel.ctm.client.util.ResourceUtil;
import team.chisel.ctm.client.util.VoidSet;

public class CTMModelsAddedCallbackHandler implements ModelsAddedCallback {
public class ModelsAddedCallbackHandler implements ModelsAddedCallback {
private final Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap;

public CTMModelsAddedCallbackHandler(Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap) {
public ModelsAddedCallbackHandler(Map<JsonUnbakedModel, Int2ObjectMap<JsonElement>> jsonOverrideMap) {
this.jsonOverrideMap = jsonOverrideMap;
}

@Override
public void onModelsAdded(ModelLoader modelLoader, ResourceManager resourceManager, Profiler profiler, Map<Identifier, UnbakedModel> unbakedModels, Map<Identifier, UnbakedModel> modelsToBake) {
Map<Identifier, UnbakedModel> wrappedModels = new HashMap<>();

UnbakedModel missingModel = unbakedModels.get(ModelLoader.MISSING_ID);
Function<Identifier, UnbakedModel> unbakedModelGetter = id -> {
UnbakedModel unbakedModel = unbakedModels.get(id);
if (unbakedModel == null) {
return missingModel;
}
return unbakedModel;
};
VoidSet<Pair<String, String>> voidSet = VoidSet.get();

// Check which models should be wrapped
for (Map.Entry<Identifier, UnbakedModel> entry : unbakedModels.entrySet()) {
Identifier identifier = entry.getKey();
Expand All @@ -45,7 +57,7 @@ public void onModelsAdded(ModelLoader modelLoader, ResourceManager resourceManag
continue;
}

Collection<SpriteIdentifier> dependencies = unbakedModel.getTextureDependencies(modelLoader::getOrLoadModel, VoidSet.get());
Collection<SpriteIdentifier> dependencies = unbakedModel.getTextureDependencies(unbakedModelGetter, voidSet);
if (unbakedModel instanceof JsonUnbakedModel) {
JsonUnbakedModel jsonModel = (JsonUnbakedModel) unbakedModel;
// Do not wrap builtin models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import team.chisel.ctm.client.model.CTMBakedModel;
import team.chisel.ctm.client.util.ResourceUtil;

public class CTMModelsLoadedCallbackHandler implements ModelsLoadedCallback {
public class ModelsLoadedCallbackHandler implements ModelsLoadedCallback {
@Override
public void onModelsLoaded(ModelLoader modelLoader, ResourceManager resourceManager, Profiler profiler) {
CTMBakedModel.invalidateCaches();
Expand Down
Loading

0 comments on commit 3721745

Please sign in to comment.