Display List Overhaul (#1200)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2025-12-24 20:30:48 -08:00
1 parent d3366ad31a
commit ee6c79802a
46 files changed
+3795 -1671

No files matched your search

@@ -0,0 +1,267 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import java.nio.FloatBuffer;
import static org.junit.jupiter.api.Assertions.*;
/** Tests GLSM display list behavior matches OpenGL spec. */
@ExtendWith(AngelicaExtension.class)
class DisplayListCompilationGLSMTest {
private int testList = -1;
private static void assertMatrixEquals(FloatBuffer expected, FloatBuffer actual, String message) {
for (int i = 0; i < 16; i++) {
assertEquals(expected.get(i), actual.get(i), 0.001f, message + " (element " + i + ")");
}
}
@BeforeEach
void setup() {
// Ensure not recording from a previous failed test
if (DisplayListManager.isRecording()) {
try { GLStateManager.glEndList(); } catch (Exception ignored) {}
}
GLStateManager.disableBlend();
GLStateManager.disableDepthTest();
GLStateManager.disableCull();
// Reset matrix to identity for transform tests
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glLoadIdentity();
}
@AfterEach
void cleanup() {
// End any open recording
if (DisplayListManager.isRecording()) {
try { GLStateManager.glEndList(); } catch (Exception ignored) {}
}
if (testList > 0) {
GLStateManager.glDeleteLists(testList, 1);
testList = -1;
}
GLStateManager.disableBlend();
GLStateManager.disableDepthTest();
GLStateManager.disableCull();
}
@Test
void glsm_compile_doesNotChangeState() {
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.enableBlend();
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "During GL_COMPILE: state unchanged");
GLStateManager.glEndList();
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "After GL_COMPILE: state unchanged");
GLStateManager.glCallList(testList);
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After playback: state changed");
}
@Test
void glsm_compileAndExecute_changesStateImmediately() {
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GLStateManager.enableBlend();
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "During GL_COMPILE_AND_EXECUTE: state changed");
GLStateManager.glEndList();
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After GL_COMPILE_AND_EXECUTE: state remains");
}
@Test
void glsm_compile_transformNotApplied() {
FloatBuffer before = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, before);
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10, 20, 30);
FloatBuffer during = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, during);
assertMatrixEquals(before, during, "During GL_COMPILE: matrix unchanged");
GLStateManager.glEndList();
}
@Test
void glsm_compileAndExecute_transformQueuedUntilFlush() {
// Transforms queue until draw/glEndList (state commands don't flush)
FloatBuffer identity = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, identity);
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GLStateManager.glTranslatef(10, 0, 0); // Queued
FloatBuffer beforeFlush = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, beforeFlush);
assertMatrixEquals(identity, beforeFlush, "Before flush: transform queued");
GLStateManager.enableBlend(); // State commands DON'T flush transforms anymore
FloatBuffer afterStateCmd = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterStateCmd);
assertMatrixEquals(identity, afterStateCmd, "After state cmd: transform still queued");
GLStateManager.glEndList(); // glEndList flushes pending transforms
FloatBuffer afterEndList = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterEndList);
assertEquals(10.0f, afterEndList.get(12), 0.001f, "After glEndList: transform applied");
}
@Test
void glsm_playback_appliesRecordedState() {
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.enableBlend();
GLStateManager.glEndList();
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "After compile: unchanged");
GLStateManager.glCallList(testList);
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After playback: enabled");
}
@Test
void glsm_compile_multipleStateChanges() {
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.enableBlend();
GLStateManager.enableDepthTest();
GLStateManager.enableCull();
GLStateManager.glEndList();
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "After GL_COMPILE: BLEND unchanged");
assertFalse(GL11.glIsEnabled(GL11.GL_DEPTH_TEST), "After GL_COMPILE: DEPTH_TEST unchanged");
assertFalse(GL11.glIsEnabled(GL11.GL_CULL_FACE), "After GL_COMPILE: CULL_FACE unchanged");
GLStateManager.glCallList(testList);
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After playback: BLEND enabled");
assertTrue(GL11.glIsEnabled(GL11.GL_DEPTH_TEST), "After playback: DEPTH_TEST enabled");
assertTrue(GL11.glIsEnabled(GL11.GL_CULL_FACE), "After playback: CULL_FACE enabled");
}
@Test
void glsm_compile_matrixModeChangeRecorded() {
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glTranslatef(5, 0, 0);
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glTranslatef(10, 0, 0);
GLStateManager.glEndList();
FloatBuffer proj = BufferUtils.createFloatBuffer(16);
FloatBuffer mv = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, proj);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mv);
assertEquals(0.0f, proj.get(12), 0.001f, "After GL_COMPILE: PROJECTION unchanged");
assertEquals(0.0f, mv.get(12), 0.001f, "After GL_COMPILE: MODELVIEW unchanged");
GLStateManager.glCallList(testList);
proj.clear(); mv.clear();
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, proj);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mv);
assertEquals(5.0f, proj.get(12), 0.001f, "After playback: PROJECTION has translate(5)");
assertEquals(10.0f, mv.get(12), 0.001f, "After playback: MODELVIEW has translate(10)");
}
@Test
void glsm_compile_pushPopMatrixRecorded() {
GLStateManager.glTranslatef(100, 0, 0); // Base transform
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(10, 0, 0);
GLStateManager.glPopMatrix();
GLStateManager.glEndList();
FloatBuffer afterCompile = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterCompile);
assertEquals(100.0f, afterCompile.get(12), 0.001f, "After GL_COMPILE: matrix at base");
GLStateManager.glCallList(testList);
FloatBuffer afterPlay = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterPlay);
assertEquals(100.0f, afterPlay.get(12), 0.001f, "After playback: matrix at base");
}
@Test
void glsm_compileAndExecute_matrixModeFlushesTransforms() {
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GLStateManager.glTranslatef(10, 0, 0); // Queued
GLStateManager.glRotatef(45, 0, 1, 0); // Queued
FloatBuffer beforeMode = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, beforeMode);
assertEquals(0.0f, beforeMode.get(12), 0.001f, "Before MatrixMode: queued");
GLStateManager.glMatrixMode(GL11.GL_PROJECTION); // Barrier - flushes
FloatBuffer afterMode = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterMode);
assertNotEquals(0.0f, afterMode.get(12), "After MatrixMode: flushed to MODELVIEW");
FloatBuffer proj = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, proj);
assertEquals(0.0f, proj.get(12), 0.001f, "PROJECTION still identity");
GLStateManager.glEndList();
// Cleanup: restore matrix mode to MODELVIEW
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
}
@Test
void glsm_compileAndExecute_pushPopMatrixQueued() {
// State commands don't flush, but push/pop ARE matrix barriers
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(10, 0, 0);
FloatBuffer beforeFlush = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, beforeFlush);
assertEquals(0.0f, beforeFlush.get(12), 0.001f, "Before flush: queued");
GLStateManager.enableBlend(); // NOT a barrier in new model
FloatBuffer afterStateCmd = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterStateCmd);
assertEquals(0.0f, afterStateCmd.get(12), 0.001f, "After state cmd: still queued");
GLStateManager.glPopMatrix(); // Matrix barrier: flushes transform, then pops
FloatBuffer afterPop = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterPop);
assertEquals(0.0f, afterPop.get(12), 0.001f, "After pop: back to identity (pushed state)");
GLStateManager.glEndList();
}
}
@@ -0,0 +1,261 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import java.nio.FloatBuffer;
import static org.junit.jupiter.api.Assertions.*;
/** Tests raw GL11 display list behavior (reference for GLSM). */
@ExtendWith(AngelicaExtension.class)
class DisplayListCompilationRawGLTest {
private int testList = -1;
private static void assertMatrixEquals(FloatBuffer expected, FloatBuffer actual, String message) {
for (int i = 0; i < 16; i++) {
assertEquals(expected.get(i), actual.get(i), 0.001f, message + " (element " + i + ")");
}
}
@BeforeEach
void setup() {
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_CULL_FACE);
}
@AfterEach
void cleanup() {
if (testList > 0) {
GL11.glDeleteLists(testList, 1);
testList = -1;
}
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_CULL_FACE);
}
@Test
void rawGL_compile_doesNotChangeState() {
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE);
GL11.glEnable(GL11.GL_BLEND);
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "During GL_COMPILE: state unchanged");
GL11.glEndList();
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "After GL_COMPILE: state unchanged");
GL11.glCallList(testList);
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After playback: state changed");
}
@Test
void rawGL_compileAndExecute_changesStateImmediately() {
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GL11.glEnable(GL11.GL_BLEND);
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "During GL_COMPILE_AND_EXECUTE: state changed");
GL11.glEndList();
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After GL_COMPILE_AND_EXECUTE: state remains");
}
@Test
void rawGL_compile_transformNotApplied() {
FloatBuffer before = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, before);
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE);
GL11.glTranslatef(10, 20, 30);
FloatBuffer during = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, during);
assertMatrixEquals(before, during, "During GL_COMPILE: matrix unchanged");
GL11.glEndList();
}
@Test
void rawGL_compileAndExecute_transformAppliedImmediately() {
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GL11.glTranslatef(10, 0, 0);
FloatBuffer matrix = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrix);
assertEquals(10.0f, matrix.get(12), 0.001f, "During GL_COMPILE_AND_EXECUTE: transform applied");
GL11.glEndList();
}
@Test
void rawGL_compile_multipleStateChanges() {
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE);
GL11.glEnable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEndList();
assertFalse(GL11.glIsEnabled(GL11.GL_BLEND), "After GL_COMPILE: BLEND unchanged");
assertFalse(GL11.glIsEnabled(GL11.GL_DEPTH_TEST), "After GL_COMPILE: DEPTH_TEST unchanged");
assertFalse(GL11.glIsEnabled(GL11.GL_CULL_FACE), "After GL_COMPILE: CULL_FACE unchanged");
GL11.glCallList(testList);
assertTrue(GL11.glIsEnabled(GL11.GL_BLEND), "After playback: BLEND enabled");
assertTrue(GL11.glIsEnabled(GL11.GL_DEPTH_TEST), "After playback: DEPTH_TEST enabled");
assertTrue(GL11.glIsEnabled(GL11.GL_CULL_FACE), "After playback: CULL_FACE enabled");
}
@Test
void rawGL_compile_matrixModeChangeRecorded() {
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glTranslatef(5, 0, 0);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glTranslatef(10, 0, 0);
GL11.glEndList();
FloatBuffer proj = BufferUtils.createFloatBuffer(16);
FloatBuffer mv = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, proj);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mv);
assertEquals(0.0f, proj.get(12), 0.001f, "After GL_COMPILE: PROJECTION unchanged");
assertEquals(0.0f, mv.get(12), 0.001f, "After GL_COMPILE: MODELVIEW unchanged");
GL11.glCallList(testList);
proj.clear(); mv.clear();
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, proj);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mv);
assertEquals(5.0f, proj.get(12), 0.001f, "After playback: PROJECTION has translate(5)");
assertEquals(10.0f, mv.get(12), 0.001f, "After playback: MODELVIEW has translate(10)");
}
@Test
void rawGL_compile_pushPopMatrixRecorded() {
GL11.glTranslatef(100, 0, 0); // Base transform
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE);
GL11.glPushMatrix();
GL11.glTranslatef(10, 0, 0);
GL11.glPopMatrix();
GL11.glEndList();
FloatBuffer afterCompile = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterCompile);
assertEquals(100.0f, afterCompile.get(12), 0.001f, "After GL_COMPILE: matrix at base");
GL11.glCallList(testList);
FloatBuffer afterPlay = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterPlay);
assertEquals(100.0f, afterPlay.get(12), 0.001f, "After playback: matrix at base (push/pop balanced)");
}
@Test
void rawGL_compileAndExecute_pushPopMatrixExecutedImmediately() {
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GL11.glPushMatrix();
GL11.glTranslatef(10, 0, 0);
FloatBuffer inside = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, inside);
assertEquals(10.0f, inside.get(12), 0.001f, "Inside push: translate visible");
GL11.glPopMatrix();
FloatBuffer afterPop = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterPop);
assertEquals(0.0f, afterPop.get(12), 0.001f, "After pop: back to identity");
GL11.glEndList();
}
// ==================== Immediate Mode with Transforms (Raw GL) ====================
@Test
void rawGL_compile_immediateModeWithTransforms() {
// Verifies GL_COMPILE doesn't execute, playback applies transforms
GL11.glLoadIdentity();
GL11.glTranslatef(100.0f, 0.0f, 0.0f); // Base position
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE);
GL11.glPushMatrix();
GL11.glTranslatef(50.0f, 25.0f, 0.0f);
GL11.glScalef(0.5f, 0.5f, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0.0f, 0.0f, 0.0f);
GL11.glVertex3f(16.0f, 0.0f, 0.0f);
GL11.glVertex3f(16.0f, 16.0f, 0.0f);
GL11.glVertex3f(0.0f, 16.0f, 0.0f);
GL11.glEnd();
GL11.glPopMatrix();
GL11.glEndList();
// Matrix should still be at base position after GL_COMPILE
FloatBuffer afterCompile = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterCompile);
assertEquals(100.0f, afterCompile.get(12), 0.001f, "After GL_COMPILE: matrix unchanged");
// Playback - push/pop should preserve base transform
GL11.glCallList(testList);
FloatBuffer afterPlayback = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, afterPlayback);
assertEquals(100.0f, afterPlayback.get(12), 0.001f, "After playback: base position preserved");
assertEquals(1.0f, afterPlayback.get(0), 0.001f, "After playback: no scale (popped)");
GL11.glLoadIdentity();
}
@Test
void rawGL_compileAndExecute_immediateModeExecutesImmediately() {
// Verifies GL_COMPILE_AND_EXECUTE executes transforms during recording
GL11.glLoadIdentity();
testList = GL11.glGenLists(1);
GL11.glNewList(testList, GL11.GL_COMPILE_AND_EXECUTE);
GL11.glScalef(0.5f, 0.5f, 0.5f);
FloatBuffer during = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, during);
assertEquals(0.5f, during.get(0), 0.001f, "During COMPILE_AND_EXECUTE: scale applied");
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0.0f, 0.0f, 0.0f);
GL11.glVertex3f(1.0f, 0.0f, 0.0f);
GL11.glVertex3f(1.0f, 1.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 0.0f);
GL11.glEnd();
GL11.glEndList();
FloatBuffer after = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, after);
assertEquals(0.5f, after.get(0), 0.001f, "After COMPILE_AND_EXECUTE: scale remains");
GL11.glLoadIdentity();
}
}
@@ -92,7 +92,26 @@ public final class DisplayListTestHelper {
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex) {
return new AccumulatedDraw(quads, transform, flags, commandIndex);
return new AccumulatedDraw(quads, transform, flags, commandIndex, 0);
}
public static AccumulatedDraw createDraw(
List<ModelQuadViewMutable> quads,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex,
int matrixGeneration) {
return new AccumulatedDraw(quads, transform, flags, commandIndex, matrixGeneration, 0);
}
public static AccumulatedDraw createDraw(
List<ModelQuadViewMutable> quads,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex,
int matrixGeneration,
int stateGeneration) {
return new AccumulatedDraw(quads, transform, flags, commandIndex, matrixGeneration, stateGeneration);
}
/**
@@ -110,12 +129,27 @@ public final class DisplayListTestHelper {
/**
* Create an AccumulatedDraw with specified transform.
*/
public static AccumulatedDraw createDrawWithTransform(Matrix4f transform, int commandIndex) {
public static AccumulatedDraw createDrawWithTransform(Matrix4f transform, int commandIndex, int matrixGeneration) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
transform,
positionOnly(),
commandIndex
commandIndex,
matrixGeneration
);
}
/**
* Create an AccumulatedDraw with specified transform and stateGeneration.
*/
public static AccumulatedDraw createDrawWithTransformAndBatch(Matrix4f transform, int commandIndex, int matrixGeneration, int stateGeneration) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
transform,
positionOnly(),
commandIndex,
matrixGeneration,
stateGeneration
);
}
@@ -333,7 +367,16 @@ public final class DisplayListTestHelper {
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex) {
return new AccumulatedPrimitiveDraw(primitives, transform, flags, commandIndex);
return new AccumulatedPrimitiveDraw(primitives, transform, flags, commandIndex, 0);
}
public static AccumulatedPrimitiveDraw createPrimitiveDraw(
List<ModelPrimitiveView> primitives,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex,
int matrixGeneration) {
return new AccumulatedPrimitiveDraw(primitives, transform, flags, commandIndex, matrixGeneration);
}
/**
@@ -7,11 +7,9 @@ import com.gtnewhorizons.angelica.glsm.recording.AccumulatedDraw;
import com.gtnewhorizons.angelica.glsm.recording.commands.DisplayListCommand;
import com.gtnewhorizons.angelica.glsm.recording.commands.DrawRangeCmd;
import com.gtnewhorizons.angelica.glsm.recording.commands.MultMatrixCmd;
import com.gtnewhorizons.angelica.glsm.recording.commands.TranslateCmd;
import org.joml.Matrix4f;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
import java.util.HashSet;
@@ -120,15 +118,11 @@ class GLSM_DisplayList_Batching_Test {
// Setup: draws with different transforms but same format
// With format-based batching, they share 1 VBO but produce separate DrawRangeCmds
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithTransform(new Matrix4f(), 0)); // Identity transform
draws.add(createDrawWithTransform(new Matrix4f().translate(1.0f, 0.0f, 0.0f), 2)); // After translate
draws.add(createDrawWithTransform(new Matrix4f().translate(3.0f, 0.0f, 0.0f), 4)); // After more translates
draws.add(createDrawWithTransform(new Matrix4f(), 0, 0)); // Identity transform, gen=0
draws.add(createDrawWithTransform(new Matrix4f().translate(1.0f, 0.0f, 0.0f), 1, 1)); // T(1), gen=1
draws.add(createDrawWithTransform(new Matrix4f().translate(3.0f, 0.0f, 0.0f), 2, 2)); // T(3), gen=2
List<DisplayListCommand> commands = new ArrayList<>();
commands.add(new TranslateCmd(1.0, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 0
commands.add(new TranslateCmd(2.0, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 1
commands.add(new TranslateCmd(3.0, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 2
commands.add(new TranslateCmd(4.0, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 3
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(commands, draws, null, 1);
@@ -145,9 +139,9 @@ class GLSM_DisplayList_Batching_Test {
Set<VertexBuffer> uniqueVBOs = getUniqueVBOs(optimized);
assertEquals(1, uniqueVBOs.size(), "All DrawRangeCmds should reference the same shared VBO");
// Should have MultMatrix commands for the transforms
// Should have MultMatrix commands for the transforms (2 for T(1) and T(3))
long matrixCount = countMultMatrixCmds(optimized);
assertTrue(matrixCount >= 2, "Should have MultMatrix commands for each unique transform");
assertEquals(2, matrixCount, "Should have MultMatrix commands for each unique transform");
}
@Test
@@ -196,52 +190,25 @@ class GLSM_DisplayList_Batching_Test {
assertEquals(1, drawCount, "Draws with different quad counts but same format and transform should produce 1 DrawRangeCmd");
}
@Test
void testEmptyDrawListProducesNoVBOs() {
// Setup: no draws
List<AccumulatedDraw> draws = new ArrayList<>();
List<DisplayListCommand> commands = new ArrayList<>();
commands.add(new TranslateCmd(1.0, 0.0, 0.0, GL11.GL_MODELVIEW));
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: no VBOs
assertEquals(0, result.ownedVbos().length, "No draws should produce no VBOs");
// Verify: no DrawRangeCmds
long drawCount = countDrawRangeCmds(optimized);
assertEquals(0, drawCount, "No draws should produce no DrawRangeCmds");
// But should still have the collapsed transform
long matrixCount = countMultMatrixCmds(optimized);
assertEquals(1, matrixCount,
"Transform should still be emitted even without draws");
}
@Test
void testCancellingTransformsProduceSeparateDrawRangeCmds() {
// This tests the specific bug where transforms cancel out in the command stream
// This tests the specific bug where transforms cancel out over the sequence
// (e.g., translate(+0.5) then translate(-0.5)) but draws captured in between
// have different transform states that must be preserved.
//
// With format-based batching: 1 shared VBO, 3 DrawRangeCmds
// The key point: draws 1 and 3 have the same transform (identity) but draw 2
// has a different transform, so they can't all be merged.
List<AccumulatedDraw> draws = new ArrayList<>();
// Draw 1: captured at identity
draws.add(createDrawWithTransform(new Matrix4f(), 0));
// Draw 2: captured at translate(0.5, 0, 0) - BEFORE the cancelling translate(-0.5)
draws.add(createDrawWithTransform(new Matrix4f().translate(0.5f, 0.0f, 0.0f), 2));
// Draw 3: captured back at identity (after translate(-0.5) cancelled out)
draws.add(createDrawWithTransform(new Matrix4f(), 4));
// Draw 1: captured at identity, gen=0
draws.add(createDrawWithTransform(new Matrix4f(), 0, 0));
// Draw 2: captured at translate(0.5, 0, 0), gen=1 (transform changed)
draws.add(createDrawWithTransform(new Matrix4f().translate(0.5f, 0.0f, 0.0f), 1, 1));
// Draw 3: captured back at identity, gen=2 (transform changed again)
draws.add(createDrawWithTransform(new Matrix4f(), 2, 2));
List<DisplayListCommand> commands = new ArrayList<>();
commands.add(new TranslateCmd(0.5, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 0: move +0.5
commands.add(new TranslateCmd(-0.5, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 1: move -0.5 (cancels out)
commands.add(new TranslateCmd(0.5, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 2: move +0.5
commands.add(new TranslateCmd(-0.5, 0.0, 0.0, GL11.GL_MODELVIEW)); // index 3: move -0.5 (cancels out)
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
@@ -262,9 +229,10 @@ class GLSM_DisplayList_Batching_Test {
Matrix4f commonTransform = new Matrix4f().translate(1.0f, 2.0f, 3.0f);
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0));
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0));
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0));
// All same transform → same generation → will batch into single DrawRangeCmd
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0, 1));
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0, 1));
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0, 1));
List<DisplayListCommand> commands = new ArrayList<>();
@@ -286,6 +254,44 @@ class GLSM_DisplayList_Batching_Test {
"Should have exactly 1 MultMatrix for the common transform");
}
/**
* Test that draws with same transform but different stateGeneration do NOT merge.
* This simulates state commands (draw barriers) between draws.
*
* Scenario: Draw1, glColor(red), Draw2, glBindTexture, Draw3
* All draws have same transform (gen=1) but different batches (0, 1, 2).
* Expected: 3 separate DrawRangeCmds (no merging across draw barriers).
*
* This test will FAIL until FormatBuffer is fixed to check stateGeneration.
*/
@Test
void testDrawsWithDifferentBatchIdDoNotMerge() {
// Setup: 3 draws with same transform generation but different batch IDs
// This simulates state commands (draw barriers) between draws
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithTransformAndBatch(new Matrix4f(), 0, 1, 0)); // batch 0
draws.add(createDrawWithTransformAndBatch(new Matrix4f(), 1, 1, 1)); // batch 1 (state cmd between)
draws.add(createDrawWithTransformAndBatch(new Matrix4f(), 2, 1, 2)); // batch 2 (state cmd between)
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 shared VBO (same format)
assertEquals(1, result.ownedVbos().length,
"Draws with same format should share 1 VBO");
// Verify: 3 DrawRangeCmds (NOT merged due to different batch IDs)
// BUG: Currently this will produce 1 DrawRangeCmd because FormatBuffer
// only checks matrixGeneration, not stateGeneration
long drawCount = countDrawRangeCmds(optimized);
assertEquals(3, drawCount,
"Draws with same transform but different batch IDs (draw barriers between) must NOT merge");
}
@Test
void testInterleavedFormatsProduceSeparateVBOs() {
// Setup: A A B A A - should produce 2 VBOs (one for A, one for B)
@@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.opengl.GL11;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static com.gtnewhorizons.angelica.util.GLSMUtil.verifyState;
/**
* Tests for display list format optimization.
@@ -33,15 +33,16 @@ class GLSM_DisplayList_FormatOptimization_Test {
@Test
void testPositionOnlyFormat() {
// Create a display list with only position data (no color, texture, brightness, or normals)
// Use GLStateManager for all calls to ensure they go through GLSM (test env doesn't redirect GL11)
displayList = GL11.glGenLists(1);
GLStateManager.glNewList(displayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0, 0, 0);
GL11.glVertex3f(1, 0, 0);
GL11.glVertex3f(1, 1, 0);
GL11.glVertex3f(0, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glVertex3f(0, 0, 0);
GLStateManager.glVertex3f(1, 0, 0);
GLStateManager.glVertex3f(1, 1, 0);
GLStateManager.glVertex3f(0, 1, 0);
GLStateManager.glEnd();
GLStateManager.glEndList();
@@ -55,16 +56,16 @@ class GLSM_DisplayList_FormatOptimization_Test {
displayList = GL11.glGenLists(1);
GLStateManager.glNewList(displayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(0, 0, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(1, 0, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(1, 1, 0);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(0, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glTexCoord2f(0, 0);
GLStateManager.glVertex3f(0, 0, 0);
GLStateManager.glTexCoord2f(1, 0);
GLStateManager.glVertex3f(1, 0, 0);
GLStateManager.glTexCoord2f(1, 1);
GLStateManager.glVertex3f(1, 1, 0);
GLStateManager.glTexCoord2f(0, 1);
GLStateManager.glVertex3f(0, 1, 0);
GLStateManager.glEnd();
GLStateManager.glEndList();
@@ -78,25 +79,27 @@ class GLSM_DisplayList_FormatOptimization_Test {
displayList = GL11.glGenLists(1);
GLStateManager.glNewList(displayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor4f(1, 1, 1, 1);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(0, 0, 0);
GL11.glColor4f(1, 0, 0, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(1, 0, 0);
GL11.glColor4f(0, 1, 0, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(1, 1, 0);
GL11.glColor4f(0, 0, 1, 1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(0, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glColor4f(1, 1, 1, 1);
GLStateManager.glTexCoord2f(0, 0);
GLStateManager.glVertex3f(0, 0, 0);
GLStateManager.glColor4f(1, 0, 0, 1);
GLStateManager.glTexCoord2f(1, 0);
GLStateManager.glVertex3f(1, 0, 0);
GLStateManager.glColor4f(0, 1, 0, 1);
GLStateManager.glTexCoord2f(1, 1);
GLStateManager.glVertex3f(1, 1, 0);
GLStateManager.glColor4f(0, 0, 1, 1);
GLStateManager.glTexCoord2f(0, 1);
GLStateManager.glVertex3f(0, 1, 0);
GLStateManager.glEnd();
GLStateManager.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
// Call the list and verify color is restored to last value (blue)
GLStateManager.glCallList(displayList);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{0f, 0f, 1f, 1f},
"After playback, color should be blue (last color in immediate mode block)");
}
@Test
@@ -105,29 +108,31 @@ class GLSM_DisplayList_FormatOptimization_Test {
displayList = GL11.glGenLists(1);
GLStateManager.glNewList(displayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
GL11.glNormal3f(0, 0, 1);
GL11.glColor4f(1, 1, 1, 1);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(0, 0, 0);
GL11.glNormal3f(0, 0, 1);
GL11.glColor4f(1, 0, 0, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(1, 0, 0);
GL11.glNormal3f(0, 0, 1);
GL11.glColor4f(0, 1, 0, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(1, 1, 0);
GL11.glNormal3f(0, 0, 1);
GL11.glColor4f(0, 0, 1, 1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(0, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glNormal3f(0, 0, 1);
GLStateManager.glColor4f(1, 1, 1, 1);
GLStateManager.glTexCoord2f(0, 0);
GLStateManager.glVertex3f(0, 0, 0);
GLStateManager.glNormal3f(0, 0, 1);
GLStateManager.glColor4f(1, 0, 0, 1);
GLStateManager.glTexCoord2f(1, 0);
GLStateManager.glVertex3f(1, 0, 0);
GLStateManager.glNormal3f(0, 0, 1);
GLStateManager.glColor4f(0, 1, 0, 1);
GLStateManager.glTexCoord2f(1, 1);
GLStateManager.glVertex3f(1, 1, 0);
GLStateManager.glNormal3f(0, 0, 1);
GLStateManager.glColor4f(0, 0, 1, 1);
GLStateManager.glTexCoord2f(0, 1);
GLStateManager.glVertex3f(0, 1, 0);
GLStateManager.glEnd();
GLStateManager.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
// Call the list and verify color is restored to last value (blue)
GLStateManager.glCallList(displayList);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{0f, 0f, 1f, 1f},
"After playback, color should be blue (last color in immediate mode block)");
}
@Test
@@ -137,40 +142,82 @@ class GLSM_DisplayList_FormatOptimization_Test {
GLStateManager.glNewList(displayList, GL11.GL_COMPILE);
// First draw: position only
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0, 0, 0);
GL11.glVertex3f(1, 0, 0);
GL11.glVertex3f(1, 1, 0);
GL11.glVertex3f(0, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glVertex3f(0, 0, 0);
GLStateManager.glVertex3f(1, 0, 0);
GLStateManager.glVertex3f(1, 1, 0);
GLStateManager.glVertex3f(0, 1, 0);
GLStateManager.glEnd();
// Second draw: position + color
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor4f(1, 0, 0, 1);
GL11.glVertex3f(2, 0, 0);
GL11.glColor4f(0, 1, 0, 1);
GL11.glVertex3f(3, 0, 0);
GL11.glColor4f(0, 0, 1, 1);
GL11.glVertex3f(3, 1, 0);
GL11.glColor4f(1, 1, 0, 1);
GL11.glVertex3f(2, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glColor4f(1, 0, 0, 1);
GLStateManager.glVertex3f(2, 0, 0);
GLStateManager.glColor4f(0, 1, 0, 1);
GLStateManager.glVertex3f(3, 0, 0);
GLStateManager.glColor4f(0, 0, 1, 1);
GLStateManager.glVertex3f(3, 1, 0);
GLStateManager.glColor4f(1, 1, 0, 1);
GLStateManager.glVertex3f(2, 1, 0);
GLStateManager.glEnd();
// Third draw: position + texture
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(4, 0, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(5, 0, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(5, 1, 0);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(4, 1, 0);
GL11.glEnd();
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glTexCoord2f(0, 0);
GLStateManager.glVertex3f(4, 0, 0);
GLStateManager.glTexCoord2f(1, 0);
GLStateManager.glVertex3f(5, 0, 0);
GLStateManager.glTexCoord2f(1, 1);
GLStateManager.glVertex3f(5, 1, 0);
GLStateManager.glTexCoord2f(0, 1);
GLStateManager.glVertex3f(4, 1, 0);
GLStateManager.glEnd();
GLStateManager.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
// Call the list and verify color is restored to last value from second draw (yellow)
// Third draw has no color, so doesn't affect GL_CURRENT_COLOR
GLStateManager.glCallList(displayList);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{1f, 1f, 0f, 1f},
"After playback, color should be yellow (last color from second draw)");
}
/**
* Verifies that color is properly restored after immediate mode VBO playback.
* After glCallList, GL_CURRENT_COLOR should be the last color set in the display list.
*/
@Test
void testColorRestoredAfterImmediateModeVBOPlayback() {
// Set initial color to white
GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{1f, 1f, 1f, 1f}, "Initial color should be white");
// Create display list with immediate mode that sets color to BLUE (0, 0, 1, 1)
displayList = GL11.glGenLists(1);
GLStateManager.glNewList(displayList, GL11.GL_COMPILE);
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // Blue
GLStateManager.glVertex3f(0, 0, 0);
GLStateManager.glVertex3f(1, 0, 0);
GLStateManager.glVertex3f(1, 1, 0);
GLStateManager.glVertex3f(0, 1, 0);
GLStateManager.glEnd();
GLStateManager.glEndList();
// Call the display list
GLStateManager.glCallList(displayList);
// After playback, GL_CURRENT_COLOR should be blue (the last color in the display list)
// AND the GLSM cache should also be blue (synced with GL)
verifyState(GL11.GL_CURRENT_COLOR, new float[]{0f, 0f, 1f, 1f},
"After display list playback, color should be blue (last color in the list)");
// Now set color to red - this should ACTUALLY change the color
GLStateManager.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{1f, 0f, 0f, 1f},
"After setting color to red, color should be red");
}
}
@@ -69,7 +69,7 @@ class GLSM_DisplayList_Primitive_Test {
// Execute: Create AccumulatedPrimitiveDraw (should deep copy)
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, identity(), positionOnly(), 0);
primitives, identity(), positionOnly(), 0, 0);
// Modify original
originalLine.setX(0, 999.0f);
@@ -96,7 +96,7 @@ class GLSM_DisplayList_Primitive_Test {
// Execute: Create AccumulatedPrimitiveDraw (should deep copy)
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, identity(), positionOnly(), 0);
primitives, identity(), positionOnly(), 0, 0);
// Modify original
originalTri.setX(0, 999.0f);
@@ -119,7 +119,7 @@ class GLSM_DisplayList_Primitive_Test {
// Execute
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, identity(), positionOnly(), 0);
primitives, identity(), positionOnly(), 0, 0);
// Verify: All primitives copied correctly
assertEquals(4, draw.primitives.size(), "Should have 4 primitives (2 lines + 2 triangles)");
@@ -143,7 +143,7 @@ class GLSM_DisplayList_Primitive_Test {
// Execute
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, originalTransform, positionOnly(), 0);
primitives, originalTransform, positionOnly(), 0, 0);
// Modify original transform
originalTransform.identity();
@@ -475,11 +475,11 @@ class GLSM_DisplayList_Primitive_Test {
List<ModelPrimitiveView> lines1 = new ArrayList<>();
lines1.add(createSimpleLine());
primitiveDraws.add(new AccumulatedPrimitiveDraw(lines1, identity(), positionOnly(), 0));
primitiveDraws.add(new AccumulatedPrimitiveDraw(lines1, identity(), positionOnly(), 0, 0));
List<ModelPrimitiveView> lines2 = new ArrayList<>();
lines2.add(createSimpleLine());
primitiveDraws.add(new AccumulatedPrimitiveDraw(lines2, translate(1, 0, 0), positionOnly(), 1));
primitiveDraws.add(new AccumulatedPrimitiveDraw(lines2, translate(1, 0, 0), positionOnly(), 1, 1));
List<DisplayListCommand> commands = new ArrayList<>();
@@ -16,18 +16,7 @@ import java.nio.FloatBuffer;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for transform collapsing optimization in display lists.
*
* <p>The optimization collapses consecutive MODELVIEW transforms into single MultMatrix
* commands, emitting them at "barriers" (draws, CallList, exit). These tests verify:</p>
* <ul>
* <li>Consecutive transforms collapse to single MultMatrix</li>
* <li>Matrix state is correct after execution</li>
* <li>Push/Pop maintain proper stack semantics</li>
* <li>Barriers (draws, CallList) correctly flush pending transforms</li>
* </ul>
*/
/** Tests for transform collapsing optimization in display lists. */
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_TransformCollapsing_Test {
@@ -268,9 +257,14 @@ class GLSM_DisplayList_TransformCollapsing_Test {
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
Int2IntMap counts = compiled.getCommandCounts();
int multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
// If compiled is null, the list is empty (correct - no commands needed)
// If compiled exists, verify no MultMatrix commands
int multMatrixCount = 0;
if (compiled != null) {
Int2IntMap counts = compiled.getCommandCounts();
multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
}
assertEquals(0, multMatrixCount,
"Identity-equivalent transforms should not emit MultMatrix");
@@ -503,7 +497,7 @@ class GLSM_DisplayList_TransformCollapsing_Test {
GLStateManager.glEndList();
// Verify the LoadMatrixCmd is present
// Verify the LOAD_MATRIX command is present
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled, "Display list should be compiled");
@@ -645,4 +639,320 @@ class GLSM_DisplayList_TransformCollapsing_Test {
GLStateManager.glPopMatrix();
}
// ==================== Immediate Mode with Transforms Tests ====================
@Test
void testImmediateModeDrawCommandStructure() {
// Verifies MultMatrix emitted before DrawRange, push/pop ordering correct
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glPushMatrix();
GLStateManager.glScalef(0.25f, 0.25f, 0.25f);
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glVertex3f(0.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(2.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(2.0f, 2.0f, 0.0f);
GLStateManager.glVertex3f(0.0f, 2.0f, 0.0f);
GLStateManager.glEnd();
GLStateManager.glPopMatrix();
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled);
IntList opcodes = compiled.getCommandOpcodes();
int pushIndex = opcodes.indexOf(GLCommand.PUSH_MATRIX);
int popIndex = opcodes.indexOf(GLCommand.POP_MATRIX);
int multMatrixIndex = opcodes.indexOf(GLCommand.MULT_MATRIX);
int drawRangeIndex = opcodes.indexOf(GLCommand.DRAW_RANGE);
assertTrue(pushIndex < multMatrixIndex, "Push before MultMatrix");
assertTrue(multMatrixIndex < drawRangeIndex, "MultMatrix before DrawRange");
assertTrue(drawRangeIndex < popIndex, "DrawRange before Pop");
}
@Test
void testImmediateModeDrawMatrixValues() {
// Verifies matrix values correct after playback with translate + scale
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(100.0f, 50.0f, 0.0f);
GLStateManager.glScalef(0.5f, 0.5f, 1.0f);
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glVertex3f(0.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(16.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(16.0f, 16.0f, 0.0f);
GLStateManager.glVertex3f(0.0f, 16.0f, 0.0f);
GLStateManager.glEnd();
GLStateManager.glEndList();
// Verify transforms collapsed
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
Int2IntMap counts = compiled.getCommandCounts();
assertEquals(1, counts.getOrDefault(GLCommand.MULT_MATRIX, 0), "Translate+Scale -> 1 MultMatrix");
assertEquals(0, counts.getOrDefault(GLCommand.TRANSLATE, 0), "No individual Translate");
assertEquals(0, counts.getOrDefault(GLCommand.SCALE, 0), "No individual Scale");
// Verify matrix values after playback
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer matrixBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrixBuf);
assertEquals(0.5f, matrixBuf.get(0), 0.0001f, "Scale X");
assertEquals(0.5f, matrixBuf.get(5), 0.0001f, "Scale Y");
assertEquals(1.0f, matrixBuf.get(10), 0.0001f, "Scale Z");
assertEquals(100.0f, matrixBuf.get(12), 0.0001f, "Translate X");
assertEquals(50.0f, matrixBuf.get(13), 0.0001f, "Translate Y");
GLStateManager.glPopMatrix();
}
// ==================== Cross-Mode Transform Collapsing Tests ====================
@Test
void testTransformsAcrossMatrixModeSwitch() {
// Transforms must not leak across matrix mode boundaries
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glLoadIdentity();
// Execute directly via GL to get expected results
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glTranslatef(1.0f, 0.0f, 0.0f);
GL11.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GL11.glScalef(2.0f, 2.0f, 2.0f);
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glTranslatef(3.0f, 0.0f, 0.0f);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GL11.glTranslatef(5.0f, 0.0f, 0.0f);
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
FloatBuffer expectedMV = BufferUtils.createFloatBuffer(16);
FloatBuffer expectedProj = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, expectedMV);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, expectedProj);
// Reset and test via display list
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f);
GLStateManager.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glScalef(2.0f, 2.0f, 2.0f);
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glTranslatef(3.0f, 0.0f, 0.0f);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f);
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glEndList();
GLStateManager.glCallList(testList);
FloatBuffer actualMV = BufferUtils.createFloatBuffer(16);
FloatBuffer actualProj = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualMV);
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, actualProj);
for (int i = 0; i < 16; i++) {
assertEquals(expectedMV.get(i), actualMV.get(i), 0.0001f, "MODELVIEW[" + i + "]");
assertEquals(expectedProj.get(i), actualProj.get(i), 0.0001f, "PROJECTION[" + i + "]");
}
// Cleanup
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
}
@Test
void testBarrierCommandsBetweenScaleAndDraw() {
// State commands between transform and draw must not break transform capture
for (int mode : new int[]{GL11.GL_COMPILE, GL11.GL_COMPILE_AND_EXECUTE}) {
if (testList > 0) {
GLStateManager.glDeleteLists(testList, 1);
}
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, mode);
GLStateManager.glScalef(0.5f, 0.5f, 1.0f);
// Barrier commands between scale and draw
GLStateManager.glDepthMask(true);
GLStateManager.disableLighting();
GLStateManager.enableTexture();
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glVertex3f(0.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(1.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(1.0f, 1.0f, 0.0f);
GLStateManager.glVertex3f(0.0f, 1.0f, 0.0f);
GLStateManager.glEnd();
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled);
Int2IntMap counts = compiled.getCommandCounts();
assertEquals(1, counts.getOrDefault(GLCommand.MULT_MATRIX, 0), "1 MultMatrix for mode " + mode);
assertEquals(1, counts.getOrDefault(GLCommand.DRAW_RANGE, 0), "1 DrawRange for mode " + mode);
IntList opcodes = compiled.getCommandOpcodes();
assertTrue(opcodes.indexOf(GLCommand.MULT_MATRIX) < opcodes.indexOf(GLCommand.DRAW_RANGE),
"MultMatrix before DrawRange for mode " + mode);
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer matrixBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrixBuf);
assertEquals(0.5f, matrixBuf.get(0), 0.0001f, "Scale X for mode " + mode);
assertEquals(0.5f, matrixBuf.get(5), 0.0001f, "Scale Y for mode " + mode);
GLStateManager.glPopMatrix();
}
}
// ==================== Optimizer Push/Pop Reset Tests ====================
@Test
void testOptimizerResetsAfterPushMatrix() {
// Optimizer must reset after PUSH_MATRIX to avoid spurious inverse transform emission
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// T1: transforms before push (collapsed)
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f);
GLStateManager.glScalef(2.0f, 2.0f, 2.0f);
GLStateManager.glPushMatrix();
// T2: transforms after push (collapsed separately)
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f);
GLStateManager.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
GLStateManager.glBegin(GL11.GL_QUADS);
GLStateManager.glVertex3f(0.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(1.0f, 0.0f, 0.0f);
GLStateManager.glVertex3f(1.0f, 1.0f, 0.0f);
GLStateManager.glVertex3f(0.0f, 1.0f, 0.0f);
GLStateManager.glEnd();
GLStateManager.glPopMatrix();
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled);
Int2IntMap counts = compiled.getCommandCounts();
IntList opcodes = compiled.getCommandOpcodes();
// Should have exactly 2 MULT_MATRIX (T1 before push, T2 after push)
// Bug would produce 3 (T1, T2, spurious T1^-1)
assertEquals(2, counts.getOrDefault(GLCommand.MULT_MATRIX, 0),
"2 MULT_MATRIX expected (T1 before push, T2 after push)");
// Verify ordering
int firstMultMatrix = opcodes.indexOf(GLCommand.MULT_MATRIX);
int pushIndex = opcodes.indexOf(GLCommand.PUSH_MATRIX);
int drawIndex = opcodes.indexOf(GLCommand.DRAW_RANGE);
int popIndex = opcodes.indexOf(GLCommand.POP_MATRIX);
assertTrue(firstMultMatrix < pushIndex, "T1 before PUSH");
assertTrue(pushIndex < drawIndex, "PUSH before DRAW");
assertTrue(drawIndex < popIndex, "DRAW before POP");
// Verify final matrix after pop = T1
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer matrixBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrixBuf);
assertEquals(2.0f, matrixBuf.get(0), 0.0001f, "Scale X after pop");
assertEquals(2.0f, matrixBuf.get(5), 0.0001f, "Scale Y after pop");
assertEquals(5.0f, matrixBuf.get(12), 0.0001f, "Translate X after pop");
GLStateManager.glPopMatrix();
}
@Test
void testTransformCollapsingEquivalence() {
// Core equivalence test: verify that collapsed transforms produce
// the exact same result as executing transforms sequentially.
//
// This tests the fundamental invariant of the optimization.
// Use axis-aligned rotations to minimize float/double precision differences.
// Transform sequence: translate, rotate around Y, scale, rotate around X, translate
final float tx1 = 5.0f, ty1 = 3.0f, tz1 = -2.0f;
final float angle1 = 45.0f;
final float sx = 1.5f, sy = 0.8f, sz = 2.0f;
final float angle2 = 30.0f;
final float tx2 = -1.0f, ty2 = 4.0f, tz2 = 0.5f;
// Execute directly via GL
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glLoadIdentity();
GL11.glTranslatef(tx1, ty1, tz1);
GL11.glRotatef(angle1, 0.0f, 1.0f, 0.0f); // Y-axis rotation
GL11.glScalef(sx, sy, sz);
GL11.glRotatef(angle2, 1.0f, 0.0f, 0.0f); // X-axis rotation
GL11.glTranslatef(tx2, ty2, tz2);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, expectedBuf);
// Reset and execute via display list
GLStateManager.glLoadIdentity();
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(tx1, ty1, tz1);
GLStateManager.glRotatef(angle1, 0.0f, 1.0f, 0.0f);
GLStateManager.glScalef(sx, sy, sz);
GLStateManager.glRotatef(angle2, 1.0f, 0.0f, 0.0f);
GLStateManager.glTranslatef(tx2, ty2, tz2);
GLStateManager.glEndList();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Verify equivalence
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d]: collapsed transform must equal sequential execution", i));
}
GLStateManager.glLoadIdentity();
}
}
@@ -17,6 +17,8 @@ import org.lwjgl.opengl.GL32;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static com.gtnewhorizons.angelica.util.GLSMUtil.resetGLState;
import static com.gtnewhorizons.angelica.util.GLSMUtil.verifyIsEnabled;
import static com.gtnewhorizons.angelica.util.GLSMUtil.verifyLightState;
@@ -869,4 +871,307 @@ class GLSM_PushPop_UnitTest {
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop to depth 0");
}
// ==================== Display List + Push/Pop Interaction Tests ====================
@Test
void testDisplayListCallInsidePushPopAllAttribBits() {
// Tests the pattern:
// 1. Push ALL attrib bits
// 2. Switch to TEXTURE1, enable GL_TEXTURE_2D
// 3. Switch back to TEXTURE0
// 4. Call a display list that changes texture state
// 5. Pop attrib
// 6. Verify state restored correctly
//
// This pattern is used by mods that wrap vanilla entity rendering.
// Setup: create textures and verify initial state
final int texA = GL11.glGenTextures();
final int texB = GL11.glGenTextures();
final int texC = GL11.glGenTextures();
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texA);
verifyState(GL11.GL_TEXTURE_BINDING_2D, texA, "Initial binding unit 0");
verifyState(GL13.GL_ACTIVE_TEXTURE, GL13.GL_TEXTURE0, "Initial active unit");
// Verify unit 1 starts with no texture and TEXTURE_2D disabled
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
verifyState(GL11.GL_TEXTURE_BINDING_2D, 0, "Initial binding unit 1");
verifyIsEnabled(GL11.GL_TEXTURE_2D, false, "Initial TEXTURE_2D state unit 1");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
// Create display list that changes texture state on multiple units
final int listId = GL11.glGenLists(1);
GLStateManager.glNewList(listId, GL11.GL_COMPILE);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texB);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texC);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glEndList();
// === Begin pattern ===
GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
// Switch to TEXTURE1, enable GL_TEXTURE_2D, switch back
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glEnable(GL11.GL_TEXTURE_2D);
verifyIsEnabled(GL11.GL_TEXTURE_2D, true, "TEXTURE_2D enabled on unit 1");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
// Call display list (simulates calling vanilla model/entity display lists)
DisplayListManager.glCallList(listId);
// Verify state changed by display list
verifyState(GL11.GL_TEXTURE_BINDING_2D, texB, "After display list - unit 0 binding");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
verifyState(GL11.GL_TEXTURE_BINDING_2D, texC, "After display list - unit 1 binding");
verifyIsEnabled(GL11.GL_TEXTURE_2D, true, "After display list - TEXTURE_2D still enabled on unit 1");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
// Pop attrib - should restore all state
GLStateManager.glPopAttrib();
// === CRITICAL VERIFICATIONS ===
// If any of these fail, we've found the bug
// Active texture unit should be restored
verifyState(GL13.GL_ACTIVE_TEXTURE, GL13.GL_TEXTURE0, "After pop - active unit");
// Unit 0 binding should be restored to texA
verifyState(GL11.GL_TEXTURE_BINDING_2D, texA, "After pop - unit 0 binding");
// Unit 1 state should be fully restored
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
verifyState(GL11.GL_TEXTURE_BINDING_2D, 0, "After pop - unit 1 binding");
verifyIsEnabled(GL11.GL_TEXTURE_2D, false, "After pop - TEXTURE_2D disabled on unit 1");
// Cleanup - reset all state to defaults
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLStateManager.glDeleteTextures(texA);
GLStateManager.glDeleteTextures(texB);
GLStateManager.glDeleteTextures(texC);
GLStateManager.glDeleteLists(listId, 1);
}
@Test
void testDisplayListWithNestedPushPopInsideOuterPushPop() {
// Tests nested push/pop where the display list itself contains push/pop
// Pattern:
// glPushAttrib(ALL)
// glCallList(...) // list contains: glPushAttrib, change state, glPopAttrib
// glPopAttrib()
final int texA = GL11.glGenTextures();
final int texB = GL11.glGenTextures();
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texA);
verifyState(GL11.GL_TEXTURE_BINDING_2D, texA, "Initial binding");
// Create display list with its own push/pop
final int listId = GL11.glGenLists(1);
GLStateManager.glNewList(listId, GL11.GL_COMPILE);
GLStateManager.glPushAttrib(GL11.GL_TEXTURE_BIT);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texB);
GLStateManager.glPopAttrib();
GLStateManager.glEndList();
// Outer push
GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
// Change texture
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
verifyState(GL11.GL_TEXTURE_BINDING_2D, 0, "After unbind in outer push");
// Call display list with nested push/pop
DisplayListManager.glCallList(listId);
// After display list's pop, should be back to 0 (what outer context had)
verifyState(GL11.GL_TEXTURE_BINDING_2D, 0, "After display list with nested push/pop");
// Outer pop
GLStateManager.glPopAttrib();
// Should restore to original texA
verifyState(GL11.GL_TEXTURE_BINDING_2D, texA, "After outer pop");
// Cleanup - reset state to defaults
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLStateManager.glDeleteTextures(texA);
GLStateManager.glDeleteTextures(texB);
GLStateManager.glDeleteLists(listId, 1);
}
@Test
void testTextureUnitSwitchingWithDisplayListModifyingDifferentUnit() {
// Tests: outer code works on unit 1, display list modifies unit 0
// Verifies both units are correctly restored after pop
final int texA = GL11.glGenTextures();
final int texB = GL11.glGenTextures();
final int texC = GL11.glGenTextures();
// Initial state: texA on unit 0, nothing on unit 1
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texA);
// Create display list that only modifies unit 0
final int listId = GL11.glGenLists(1);
GLStateManager.glNewList(listId, GL11.GL_COMPILE);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texC);
GLStateManager.glEndList();
// Push, then work on unit 1
GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texB);
GLStateManager.glEnable(GL11.GL_TEXTURE_2D);
// Call display list - modifies unit 0 while we're "on" unit 1
DisplayListManager.glCallList(listId);
// Verify: unit 0 changed, unit 1 unchanged
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
verifyState(GL11.GL_TEXTURE_BINDING_2D, texC, "After list - unit 0 changed to texC");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
verifyState(GL11.GL_TEXTURE_BINDING_2D, texB, "After list - unit 1 still texB");
verifyIsEnabled(GL11.GL_TEXTURE_2D, true, "After list - unit 1 still enabled");
// Pop
GLStateManager.glPopAttrib();
// Both units should be restored
verifyState(GL13.GL_ACTIVE_TEXTURE, GL13.GL_TEXTURE0, "After pop - active unit");
verifyState(GL11.GL_TEXTURE_BINDING_2D, texA, "After pop - unit 0 restored to texA");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
verifyState(GL11.GL_TEXTURE_BINDING_2D, 0, "After pop - unit 1 restored to 0");
verifyIsEnabled(GL11.GL_TEXTURE_2D, false, "After pop - unit 1 disabled");
// Cleanup - reset all state to defaults
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLStateManager.glDeleteTextures(texA);
GLStateManager.glDeleteTextures(texB);
GLStateManager.glDeleteTextures(texC);
GLStateManager.glDeleteLists(listId, 1);
}
@Test
void testColorStateWithDisplayListInsidePushPop() {
// Tests color state restoration - white screen could indicate color issue
// Pattern: push, set color, call display list that changes color, pop
verifyState(GL11.GL_CURRENT_COLOR, FLOAT_ARRAY_4_1, "Initial color white");
// Create display list that sets color to red
final int listId = GL11.glGenLists(1);
GLStateManager.glNewList(listId, GL11.GL_COMPILE);
GLStateManager.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
GLStateManager.glEndList();
// Push (note: GL_CURRENT_BIT includes color)
GLStateManager.glPushAttrib(GL11.GL_CURRENT_BIT);
// Set color to green
GLStateManager.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{0.0f, 1.0f, 0.0f, 1.0f}, "After setting green");
// Call display list - sets to red
DisplayListManager.glCallList(listId);
verifyState(GL11.GL_CURRENT_COLOR, new float[]{1.0f, 0.0f, 0.0f, 1.0f}, "After display list - red");
// Pop - should restore to white (initial state before push)
GLStateManager.glPopAttrib();
verifyState(GL11.GL_CURRENT_COLOR, FLOAT_ARRAY_4_1, "After pop - restored to white");
// Cleanup - ensure color is reset to white
GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GLStateManager.glDeleteLists(listId, 1);
}
@Test
void testBlendStateWithDisplayListInsidePushPop() {
// Tests blend state restoration - incorrect blend could cause visual issues
verifyState(GL11.GL_CURRENT_COLOR, FLOAT_ARRAY_4_1, "color should still be white");
verifyIsEnabled(GL11.GL_BLEND, false, "Initial blend disabled");
verifyState(GL11.GL_BLEND_SRC, GL11.GL_ONE, "Initial blend src");
verifyState(GL11.GL_BLEND_DST, GL11.GL_ZERO, "Initial blend dst");
// Create display list that enables blend with specific function
final int listId = GL11.glGenLists(1);
GLStateManager.glNewList(listId, GL11.GL_COMPILE);
GLStateManager.enableBlend();
GLStateManager.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GLStateManager.glEndList();
// Push
GLStateManager.glPushAttrib(GL11.GL_COLOR_BUFFER_BIT);
// Set different blend state
GLStateManager.enableBlend();
GLStateManager.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// Call display list - changes blend function
DisplayListManager.glCallList(listId);
verifyIsEnabled(GL11.GL_BLEND, true, "After list - blend enabled");
verifyState(GL11.GL_BLEND_SRC, GL11.GL_ONE, "After list - blend src ONE");
verifyState(GL11.GL_BLEND_DST, GL11.GL_ONE, "After list - blend dst ONE");
// Pop
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "After pop - blend disabled");
verifyState(GL11.GL_BLEND_SRC, GL11.GL_ONE, "After pop - blend src restored");
verifyState(GL11.GL_BLEND_DST, GL11.GL_ZERO, "After pop - blend dst restored");
// Cleanup - ensure blend is reset to defaults
GLStateManager.disableBlend();
GLStateManager.glBlendFunc(GL11.GL_ONE, GL11.GL_ZERO);
GLStateManager.glDeleteLists(listId, 1);
// Verify GLSM cache matches actual GL state (exposes cache sync bugs)
verifyState(GL11.GL_CURRENT_COLOR, FLOAT_ARRAY_4_1, "color should still be white");
}
@Test
void testTextureUnitPopAttribRestoresCorrectUnit() {
// Setup: Unit 0 ENABLED, Unit 1 disabled
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glEnable(GL11.GL_TEXTURE_2D);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glDisable(GL11.GL_TEXTURE_2D);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
verifyIsEnabled(GL11.GL_TEXTURE_2D, true, "Initial - Unit 0 enabled");
GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
// Modify ONLY unit 1
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
GLStateManager.glEnable(GL11.GL_TEXTURE_2D);
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glPopAttrib();
// Unit 0 should still be enabled - bug causes it to be disabled
verifyIsEnabled(GL11.GL_TEXTURE_2D, true, "After pop - Unit 0 still enabled");
GLStateManager.glActiveTexture(GL13.GL_TEXTURE1);
verifyIsEnabled(GL11.GL_TEXTURE_2D, false, "After pop - Unit 1 disabled");
// Cleanup
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
GLStateManager.glDisable(GL11.GL_TEXTURE_2D);
}
}