diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java index 193a32879..fc9bf2ad8 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java @@ -135,9 +135,9 @@ public class GLStateManager { @Setter @Getter private static boolean runningSplash = true; private static volatile boolean splashComplete = false; - private static volatile Thread drawableGLHolder = MainThread; - // Reference to SharedDrawable so makeCurrent() can distinguish it from DrawableGL - @Setter private static volatile Drawable sharedDrawable = null; + @Getter @Setter private static Thread drawableGLHolder = MainThread; + // Reference to DrawableGL (main display context) - works with both FML and BLS splash + @Setter private static Drawable drawableGL = null; public static boolean isCachingEnabled() { if (splashComplete) return true; @@ -2319,31 +2319,31 @@ public class GLStateManager { drawable.makeCurrent(); CurrentThread = Thread.currentThread(); - // During splash, track which thread holds DrawableGL for caching - if (!splashComplete && sharedDrawable != null) { - if (drawable == sharedDrawable) { - // Switching to SharedDrawable - disable caching for this thread - if (drawableGLHolder == CurrentThread) { - drawableGLHolder = null; + // Post-splash: single context, always cache + if (splashComplete) return; + + if (drawableGL == null) { + // Lazy-capture DrawableGL reference (Display.getDrawable()) on first opportunity + try { + if (drawable == Display.getDrawable()) { + drawableGL = drawable; } - } else { - // Switching to DrawableGL - enable caching for this thread - drawableGLHolder = CurrentThread; + } catch (Exception e) { + // Display not ready - can't identify DrawableGL yet, will retry next call + LOGGER.warn("Display not ready in makeCurrent", e); + return; } } - } - /** - * Set whether the current thread holds DrawableGL (the main display context). - * Called by mixins at context switch points during splash. - */ - public static void setCachingEnabled(boolean enabled) { - final Thread current = Thread.currentThread(); - if (enabled) { - drawableGLHolder = current; - } else if (drawableGLHolder == current) { + if (drawableGL != null && drawable == drawableGL) { + // Switching TO DrawableGL - enable caching for this thread + drawableGLHolder = CurrentThread; + } + else if (drawableGLHolder == CurrentThread) { + // This thread held DrawableGL but is switching AWAY - disable caching drawableGLHolder = null; } + // else: Thread switching to non-DrawableGL, wasn't holder - no change needed } /** @@ -2353,7 +2353,7 @@ public class GLStateManager { public static void markSplashComplete() { splashComplete = true; drawableGLHolder = null; - sharedDrawable = null; + drawableGL = null; } public static void glNewList(int list, int mode) { diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java index 1e4885b3a..27100bb3a 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java @@ -1,9 +1,9 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.glsm; import com.gtnewhorizons.angelica.glsm.GLStateManager; -import org.lwjgl.opengl.Drawable; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; 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; @@ -19,37 +19,19 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; * to the main game loop. * * GLStateManager.makeCurrent() automatically handles caching based on whether the drawable - * is SharedDrawable or DrawableGL. This mixin just needs to: - * 1. Register the SharedDrawable reference when it's created - * 2. Mark splash complete when finish() is called + * is DrawableGL or not. DrawableGL is captured early in OpenGlHelper.initializeTextures(). */ @SuppressWarnings("deprecation") @Mixin(value = cpw.mods.fml.client.SplashProgress.class, remap = false) public class MixinSplashProgressCaching { - - @Shadow private static Drawable d; + private static final Logger LOGGER = LogManager.getLogger("Angelica"); /** - * Before start() calls d.makeCurrent(), register the SharedDrawable reference. - * This allows GLStateManager.makeCurrent() to distinguish SharedDrawable from DrawableGL. + * On return from finish() - mark splash complete */ - @Inject(method = "start", - at = @At(value = "INVOKE", - target = "Lorg/lwjgl/opengl/Drawable;makeCurrent()V")) - private static void angelica$captureSharedDrawable(CallbackInfo ci) { - GLStateManager.setSharedDrawable(d); - } - - /** - * After finish() calls Display.getDrawable().makeCurrent() (DrawableGL), mark splash complete. - * This is the final switch to DrawableGL that persists into the main game loop. - * markSplashComplete() enables the fast path that bypasses holder tracking. - */ - @Inject(method = "finish", - at = @At(value = "INVOKE", - target = "Lorg/lwjgl/opengl/Drawable;makeCurrent()V", - shift = At.Shift.AFTER)) + @Inject(method = "finish", at = @At("RETURN")) private static void angelica$enableCachingOnFinish(CallbackInfo ci) { GLStateManager.markSplashComplete(); + LOGGER.info("Splash Complete"); } } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java index 4681fce2a..d738f9456 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java @@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.startup; import com.gtnewhorizons.angelica.glsm.GLStateManager; import net.minecraft.client.renderer.OpenGlHelper; +import org.lwjgl.opengl.Display; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -11,6 +12,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MixinInitGLStateManager { @Inject(method = "initializeTextures", at = @At("RETURN")) private static void angelica$initializeGLStateManager(CallbackInfo ci) { + GLStateManager.setDrawableGL(Display.getDrawable()); GLStateManager.preInit(); GLStateManager.init(); } diff --git a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_ServerSideState_UnitTest.java b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_ServerSideState_UnitTest.java index 6954bb317..514b8d1f4 100644 --- a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_ServerSideState_UnitTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_ServerSideState_UnitTest.java @@ -60,11 +60,11 @@ public class GLSM_ServerSideState_UnitTest { sharedDrawable = new SharedDrawable(Display.getDrawable()); - // Register the sharedDrawable so makeCurrent() can detect it - GLStateManager.setSharedDrawable(sharedDrawable); + // Register the main display context (DrawableGL) so makeCurrent() can identify it + GLStateManager.setDrawableGL(Display.getDrawable()); - // Main thread holds DrawableGL, so enable caching for it: This sets drawableGLHolder = current thread - GLStateManager.setCachingEnabled(true); + // Switch to DrawableGL to enable caching for main thread + GLStateManager.makeCurrent(Display.getDrawable()); // Verify main thread has caching enabled assertTrue(GLStateManager.isCachingEnabled(), "Main thread should have caching enabled (it holds DrawableGL)"); @@ -140,7 +140,7 @@ public class GLSM_ServerSideState_UnitTest { } // Restore original state - GLStateManager.setSharedDrawable(null); + GLStateManager.setDrawableGL(null); setSplashComplete(originalSplashComplete); if (originalSplashComplete) { GLStateManager.markSplashComplete(); diff --git a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureInfoCache_UnitTest.java b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureInfoCache_UnitTest.java index 709854236..925a2f9aa 100644 --- a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureInfoCache_UnitTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureInfoCache_UnitTest.java @@ -32,11 +32,12 @@ public class GLSM_TextureInfoCache_UnitTest { // Disable caching (simulate SharedDrawable context) Field splashCompleteField = GLStateManager.class.getDeclaredField("splashComplete"); splashCompleteField.setAccessible(true); - boolean original = splashCompleteField.getBoolean(null); + boolean originalSplash = splashCompleteField.getBoolean(null); + Thread originalHolder = GLStateManager.getDrawableGLHolder(); try { splashCompleteField.setBoolean(null, false); - GLStateManager.setCachingEnabled(false); + GLStateManager.setDrawableGLHolder(null); // Current thread != null, so caching disabled assertFalse(GLStateManager.isCachingEnabled()); GLStateManager.glDeleteTextures(texId); @@ -46,7 +47,8 @@ public class GLSM_TextureInfoCache_UnitTest { // Cache entry should be gone - we should get a fresh object assertNotSame(info, TextureInfoCache.INSTANCE.getInfo(texId)); } finally { - splashCompleteField.setBoolean(null, original); + splashCompleteField.setBoolean(null, originalSplash); + GLStateManager.setDrawableGLHolder(originalHolder); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); } }