Compare commits

...
Author SHA1 Message Date
Algent 824473f093 Fix VAOManager desync & Post-splash gap 2026-08-13 17:23:08 +02:00
Algent 48fcc4c943 Fix state leak 2026-08-13 11:33:02 +02:00
3 changed files with 78 additions and 19 deletions

No files matched your search

@@ -169,6 +169,9 @@ public class GLStateManager {
}
static GLContextState enterWorkerContext() {
final GLContextState existing = tlWorkerContext.get();
if (existing != null) return existing;
final GLContextState st = new GLContextState();
tlWorkerContext.set(st);
synchronized (GLStateManager.class) {
@@ -310,7 +313,7 @@ public class GLStateManager {
private static volatile boolean splashComplete = false;
@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;
@Setter private static volatile Drawable drawableGL = null;
private static final ReentrantLock DRAW_LOCK = new ReentrantLock();
@@ -3338,15 +3341,9 @@ public class GLStateManager {
}
CurrentThread = Thread.currentThread();
if (!splashComplete && CurrentThread != MainThread && !autoVaoBound.get()) {
autoVaoBound.set(Boolean.TRUE);
glBindVertexArray(glGenVertexArrays());
}
if (splashComplete) return;
if (drawableGL == null) {
// Lazy-capture DrawableGL reference (Display.getDrawable()) on first opportunity
// Lazy-capture DrawableGL reference (Display.getDrawable()) on first opportunity.
// markSplashComplete clears it, so this re-runs after the splash too.
try {
if (drawable == Display.getDrawable()) {
drawableGL = drawable;
@@ -3358,14 +3355,30 @@ public class GLStateManager {
}
}
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;
final boolean isDrawableGL = drawableGL != null && drawable == drawableGL;
if (isDrawableGL) {
exitWorkerContext();
} else {
enterWorkerContext();
}
if (!splashComplete) {
if (isDrawableGL) {
// 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;
}
}
// Worker contexts have no VAO of their own; core profile rejects draws without one.
// DrawableGL already carries defaultVAO once initialize() has run, but during the splash
// it may not have, so keep binding there too.
if (CurrentThread != MainThread && !autoVaoBound.get() && (!isDrawableGL || !splashComplete)) {
autoVaoBound.set(Boolean.TRUE);
glBindVertexArray(glGenVertexArrays());
}
// else: Thread switching to non-DrawableGL, wasn't holder - no change needed
}
public static void releaseContext(Drawable drawable) throws LWJGLException {
@@ -3373,6 +3386,7 @@ public class GLStateManager {
if (!handled) {
drawable.releaseContext();
}
exitWorkerContext();
if (drawableGLHolder == Thread.currentThread()) {
drawableGLHolder = null;
}
@@ -6104,9 +6118,9 @@ public class GLStateManager {
}
final boolean locked = acquireDrawLock();
try {
VAOManager.onBindVertexArrayPre(array);
if (glCtx.boundVAO != array) {
glCtx.boundVAO = array;
VAOManager.onBindVertexArrayPre(array);
RENDER_BACKEND.bindVertexArray(array);
}
} finally {
@@ -19,6 +19,9 @@ public final class VAOManager {
static final Int2ObjectOpenHashMap<VAOData> eboOwners = new Int2ObjectOpenHashMap<>();
private static VAOData current = null;
// Which VAO name `current` reflects. Per-context boundVAO caching can skip a bind that is a
// no-op for that context but still a switch for this global tracker, so it needs its own id.
private static int currentVaoId = -1;
private static int currentVertexFlags = 0;
private static Attrib[] currentAttribs = null;
public static int boundEBO = 0;
@@ -29,6 +32,7 @@ public final class VAOManager {
public static void init(int defaultVAO) {
final VAOData data = new VAOData();
current = data;
currentVaoId = defaultVAO;
currentAttribs = data.attribs;
vaoMap.put(defaultVAO, data);
}
@@ -38,6 +42,9 @@ public final class VAOManager {
}
public static void onBindVertexArrayPre(int vaoId) {
if (vaoId == currentVaoId) return;
currentVaoId = vaoId;
// Save old VAO data
if (current != null) {
current.vertexFlags = currentVertexFlags;
@@ -66,7 +73,10 @@ public final class VAOManager {
final VAOData data = vaoMap.remove(vaoId);
if (data == null) return;
removeEboOwner(data.ebo, data);
if (data == current) current = null;
if (data == current) {
current = null;
currentVaoId = -1;
}
}
public static void onDeleteBuffer(int buffer) {
@@ -106,7 +106,7 @@ public class GLSM_ServerSideState_UnitTest {
backgroundError = e;
} finally {
try {
sharedDrawable.releaseContext();
GLStateManager.releaseContext(sharedDrawable);
} catch (LWJGLException e) {
// Ignore on cleanup
}
@@ -342,6 +342,41 @@ public class GLSM_ServerSideState_UnitTest {
}
}
@Test
void testDepthStateDoesNotBleedFromSharedDrawable() throws InterruptedException {
final boolean originalDepthTest = GLStateManager.getDepthTest().isEnabled();
final int originalDepthFunc = GLStateManager.getDepthState().getFunc();
final boolean originalDepthMask = GLStateManager.getDepthState().isEnabled();
try {
GLStateManager.enableDepthTest();
GLStateManager.glDepthFunc(GL11.GL_LEQUAL);
GLStateManager.glDepthMask(true);
executeOnSharedDrawable(() -> {
GLStateManager.disableDepthTest();
GLStateManager.glDepthFunc(GL11.GL_ALWAYS);
GLStateManager.glDepthMask(false);
assertFalse(GLStateManager.getDepthTest().isEnabled());
assertEquals(GL11.GL_ALWAYS, GLStateManager.getDepthState().getFunc());
assertFalse(GLStateManager.getDepthState().isEnabled());
});
assertTrue(GLStateManager.getDepthTest().isEnabled(), "SharedDrawable depth test leaked into the main cache");
assertEquals(GL11.GL_LEQUAL, GLStateManager.getDepthState().getFunc(), "SharedDrawable depth func leaked into the main cache");
assertTrue(GLStateManager.getDepthState().isEnabled(), "SharedDrawable depth mask leaked into the main cache");
} finally {
if (originalDepthTest) {
GLStateManager.enableDepthTest();
} else {
GLStateManager.disableDepthTest();
}
GLStateManager.glDepthFunc(originalDepthFunc);
GLStateManager.glDepthMask(originalDepthMask);
}
}
/**
* Test glTexParameterf (float parameters like LOD_BIAS).
*/