From 85784e46971e3211a60fe37de78504b8c2b96503 Mon Sep 17 00:00:00 2001
From: embeddedt <42941056+embeddedt@users.noreply.github.com>
Date: Fri, 1 Sep 2023 08:56:00 -0400
Subject: [PATCH] Force-rebind lightmap texture when Quartz+Lodestone are
active
We should not need to do this. Mods should use RenderSystem
Works around:
* https://github.com/BiggerSeries/Quartz/issues/12
* https://github.com/LodestarMC/Lodestone/issues/9
---
.../sodium/client/quirks/QuirkManager.java | 39 +++++++++++++++++++
.../chunk/shader/ChunkShaderInterface.java | 11 ++++++
2 files changed, 50 insertions(+)
create mode 100644 src/main/java/me/jellysquid/mods/sodium/client/quirks/QuirkManager.java
diff --git a/src/main/java/me/jellysquid/mods/sodium/client/quirks/QuirkManager.java b/src/main/java/me/jellysquid/mods/sodium/client/quirks/QuirkManager.java
new file mode 100644
index 000000000..ec079a519
--- /dev/null
+++ b/src/main/java/me/jellysquid/mods/sodium/client/quirks/QuirkManager.java
@@ -0,0 +1,39 @@
+package me.jellysquid.mods.sodium.client.quirks;
+
+import me.jellysquid.mods.sodium.client.SodiumClientMod;
+import net.minecraftforge.fml.ModList;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+public class QuirkManager {
+ /**
+ * Force rebinding of the 2nd texture (lightmap) if Quartz and Lodestone are installed. This fixes
+ * LodestarMC/Lodestone#9.
+ *
+ * Only tested on 18.2.
+ */
+ public static final boolean REBIND_LIGHTMAP_TEXTURE = Stream.of("quartz", "lodestone").allMatch(QuirkManager::isLoaded);
+
+ static {
+ try {
+ List enabledQuirks = new ArrayList<>();
+ for(Field f : QuirkManager.class.getDeclaredFields()) {
+ if(f.getType() == boolean.class && Modifier.isStatic(f.getModifiers())) {
+ if(f.getBoolean(null))
+ enabledQuirks.add(f.getName());
+ }
+ }
+ SodiumClientMod.logger().warn("Enabled the following quirks in QuirkManager: [{}]", String.join(", ", enabledQuirks));
+ } catch(ReflectiveOperationException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static boolean isLoaded(String modId) {
+ return ModList.get().isLoaded(modId);
+ }
+}
diff --git a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/shader/ChunkShaderInterface.java b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/shader/ChunkShaderInterface.java
index 05d9b3d0a..49990e41d 100644
--- a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/shader/ChunkShaderInterface.java
+++ b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/shader/ChunkShaderInterface.java
@@ -4,6 +4,8 @@
import me.jellysquid.mods.sodium.client.gl.buffer.GlMutableBuffer;
import me.jellysquid.mods.sodium.client.gl.shader.uniform.*;
import me.jellysquid.mods.sodium.client.model.vertex.type.ChunkVertexType;
+import me.jellysquid.mods.sodium.client.quirks.QuirkManager;
+import org.lwjgl.opengl.GL13;
import repack.joml.Matrix4f;
import org.lwjgl.opengl.GL32C;
@@ -44,6 +46,15 @@ public void setup(ChunkVertexType vertexType) {
RenderSystem.activeTexture(GL32C.GL_TEXTURE2);
RenderSystem.bindTexture(RenderSystem.getShaderTexture(2));
+ if(QuirkManager.REBIND_LIGHTMAP_TEXTURE) {
+ // Some mods inject their own rendering logic, and do not use the RenderSystem methods, which causes
+ // the cache to become outdated.
+ // In this case we unconditionally rebind our texture. We just told the cache about it above, so we
+ // can never cause the same issue we complain about. ;)
+ GL13.glActiveTexture(GL32C.GL_TEXTURE2);
+ GL32C.glBindTexture(GL32C.GL_TEXTURE_2D, RenderSystem.getShaderTexture(2));
+ }
+
this.uniformBlockTex.setInt(0);
this.uniformLightTex.setInt(2);