Skip to content

Commit

Permalink
Use FRAPI SpriteFinder and remove resource reloader
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 8, 2024
1 parent caf1fdd commit 9525934
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ private void bufferQuad(MutableQuadViewImpl quad, float[] brightnesses, Material
ChunkVertexEncoder.Vertex[] vertices = this.vertices;
Vector3f offset = this.posOffset;

float uSum = 0.0f;
float vSum = 0.0f;

for (int dstIndex = 0; dstIndex < 4; dstIndex++) {
int srcIndex = orientation.getVertexIndex(dstIndex);

Expand All @@ -175,13 +172,13 @@ private void bufferQuad(MutableQuadViewImpl quad, float[] brightnesses, Material
out.color = ColorARGB.toABGR(quad.color(srcIndex));
out.ao = brightnesses[srcIndex];

uSum += out.u = quad.u(srcIndex);
vSum += out.v = quad.v(srcIndex);
out.u = quad.u(srcIndex);
out.v = quad.v(srcIndex);

out.light = quad.lightmap(srcIndex);
}

var atlasSprite = SpriteFinderCache.forBlockAtlas().find(uSum / 4.0f, vSum / 4.0f);
var atlasSprite = quad.sprite(SpriteFinderCache.forBlockAtlas());
var materialBits = material.bits();
ModelQuadFacing normalFace = quad.normalFace();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void shadeQuad(MutableQuadViewImpl quad, boolean emissive) {

private void bufferQuad(MutableQuadViewImpl quad, VertexConsumer vertexConsumer) {
QuadEncoder.writeQuadVertices(quad, vertexConsumer, overlay, matPosition, trustedNormals, matNormal);
SpriteUtil.markSpriteActive(SpriteFinderCache.forBlockAtlas().find(quad.getTexU(0), quad.getTexV(0)));
SpriteUtil.markSpriteActive(quad.sprite(SpriteFinderCache.forBlockAtlas()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ protected void shadeQuad(MutableQuadViewImpl quad, LightMode lightMode, boolean

private void bufferQuad(MutableQuadViewImpl quad) {
QuadEncoder.writeQuadVertices(quad, vertexConsumer, overlay, matPosition, trustedNormals, matNormal);
SpriteUtil.markSpriteActive(SpriteFinderCache.forBlockAtlas().find(quad.getTexU(0), quad.getTexV(0)));
SpriteUtil.markSpriteActive(quad.sprite(SpriteFinderCache.forBlockAtlas()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
package net.caffeinemc.mods.sodium.client.render.texture;

import net.caffeinemc.mods.sodium.mixin.core.render.texture.TextureAtlasAccessor;
import net.fabricmc.fabric.api.renderer.v1.model.SpriteFinder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;

/**
* Caches {@link BlockSpriteFinder}s for maximum efficiency. They must be refreshed after each resource reload.
* Caches {@link SpriteFinder}s for maximum efficiency. They must be refreshed after each resource reload.
*
* <p><b>This class should not be used during a resource reload</b>, as returned SpriteFinders may be null or outdated.
*/
public class SpriteFinderCache {
private static BlockSpriteFinder blockAtlasSpriteFinder;
private static SpriteFinder blockAtlasSpriteFinder;

public static SpriteFinder forBlockAtlas() {
if (blockAtlasSpriteFinder == null) {
blockAtlasSpriteFinder = SpriteFinder.get(Minecraft.getInstance().getModelManager().getAtlas(TextureAtlas.LOCATION_BLOCKS));
}

public static BlockSpriteFinder forBlockAtlas() {
return blockAtlasSpriteFinder;
}

public static class ReloadListener implements ResourceManagerReloadListener {
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("sodium", "sprite_finder_cache");
public static final ReloadListener INSTANCE = new ReloadListener();

private ReloadListener() {
}

// BakedModelManager#getAtlas only returns correct results after the BakedModelManager is done reloading
@Override
public void onResourceManagerReload(ResourceManager manager) {
ModelManager modelManager = Minecraft.getInstance().getModelManager();
TextureAtlas atlas = modelManager.getAtlas(TextureAtlas.LOCATION_BLOCKS);
blockAtlasSpriteFinder = new BlockSpriteFinder(((TextureAtlasAccessor) atlas).getTexturesByName(), atlas);
}
public static void resetSpriteFinder() {
blockAtlasSpriteFinder = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.caffeinemc.mods.sodium.mixin.core.render;

import net.caffeinemc.mods.sodium.client.render.texture.SpriteFinderCache;
import net.minecraft.client.renderer.texture.SpriteLoader;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(TextureAtlas.class)
public class TextureAtlasMixin {
@Shadow
@Final
private ResourceLocation location;

@Inject(method = "upload", at = @At("RETURN"))
private void sodium$deleteSpriteFinder(SpriteLoader.Preparations preparations, CallbackInfo ci) {
if (this.location.equals(TextureAtlas.LOCATION_BLOCKS)) {
SpriteFinderCache.resetSpriteFinder();
}
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"core.model.colors.ItemColorsMixin",
"core.model.FaceBakeryMixin",
"core.render.BlockEntityTypeMixin",
"core.render.TextureAtlasMixin",
"core.render.VertexFormatMixin",
"core.render.frustum.FrustumMixin",
"core.render.immediate.consumer.BufferBuilderMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@

import net.caffeinemc.mods.sodium.client.SodiumClientMod;
import net.caffeinemc.mods.sodium.client.render.frapi.SodiumRenderer;
import net.caffeinemc.mods.sodium.client.render.texture.SpriteFinderCache;
import net.caffeinemc.mods.sodium.client.util.FlawlessFrames;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;

public class SodiumFabricMod implements ClientModInitializer {
@Override
Expand All @@ -35,20 +23,6 @@ public void onInitializeClient() {
.getEntrypoints("frex_flawless_frames", Consumer.class)
.forEach(api -> api.accept(FlawlessFrames.getProvider()));

ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new ResourceReload() {
@Override
public ResourceLocation getFabricId() {
return ResourceLocation.fromNamespaceAndPath("sodium", "sprite_finder");
}

@Override
public CompletableFuture<Void> reload(PreparationBarrier preparationBarrier, ResourceManager resourceManager, ProfilerFiller preparationsProfiler, ProfilerFiller reloadProfiler, Executor backgroundExecutor, Executor gameExecutor) {
return SpriteFinderCache.ReloadListener.INSTANCE.reload(preparationBarrier, resourceManager, preparationsProfiler, reloadProfiler, backgroundExecutor, gameExecutor);
}
});
RendererAccess.INSTANCE.registerRenderer(SodiumRenderer.INSTANCE);
}

// This workaround is required to get Loom to properly remap Sodium in a development environment.
public interface ResourceReload extends IdentifiableResourceReloadListener, PreparableReloadListener {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
@Mod("sodium")
public class SodiumForgeMod {
public SodiumForgeMod(IEventBus bus, ModContainer modContainer) {
bus.addListener(this::onResourceReload);
modContainer.registerExtensionPoint(IConfigScreenFactory.class, (minecraft, screen) -> SodiumOptionsGUI.createScreen(screen));
RendererAccess.INSTANCE.registerRenderer(SodiumRenderer.INSTANCE);
}

public void onResourceReload(RegisterClientReloadListenersEvent event) {
event.registerReloadListener(SpriteFinderCache.ReloadListener.INSTANCE);
}
}

0 comments on commit 9525934

Please sign in to comment.