Add display list compilation/optimization (#1141)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2025-12-02 21:43:17 -08:00
1 parent 55811b9d7b
commit 33de1de9c4
118 files changed
+11342 -670

No files matched your search

@@ -1,20 +1,27 @@
package com.gtnewhorizons.angelica;
import com.gtnewhorizons.angelica.glsm.GLDebug;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import com.gtnewhorizons.angelica.glsm.RenderSystem;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
import org.lwjgl.util.glu.GLU;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
public class AngelicaExtension implements BeforeAllCallback, AfterEachCallback, ExtensionContext.Store.CloseableResource {
public class AngelicaExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, ExtensionContext.Store.CloseableResource {
private static boolean started = false;
private static DisplayMode displayMode;
@@ -22,6 +29,10 @@ public class AngelicaExtension implements BeforeAllCallback, AfterEachCallback,
public static String glRenderer;
public static String glVersion;
// Expected stack depths (1 = just the base matrix, no pushes)
private static final int EXPECTED_MODELVIEW_STACK_DEPTH = 1;
private static final int EXPECTED_PROJECTION_STACK_DEPTH = 1;
@Override
public void beforeAll(ExtensionContext context) throws LWJGLException {
if (!started) {
@@ -31,12 +42,16 @@ public class AngelicaExtension implements BeforeAllCallback, AfterEachCallback,
Display.setDisplayModeAndFullscreen(displayMode);
Display.setResizable(false);
Display.setFullscreen(false);
final PixelFormat format = new PixelFormat().withDepthBits(24);
final PixelFormat format = new PixelFormat().withDepthBits(24).withStencilBits(8);
Display.create(format);
// Set MainThread to the current test thread (the one with the GL context)
setMainThread(Thread.currentThread());
// Warm-up State Manager features
GLStateManager.preInit();
GLStateManager.setRunningSplash(false); // So we don't bypass the cache
GLStateManager.markSplashComplete(); // Enable caching for tests
GLStateManager.BYPASS_CACHE = false; // Just to be sure
RenderSystem.initRenderer();
context.getRoot().getStore(GLOBAL).put("AngelicaExtension", this);
@@ -50,13 +65,75 @@ public class AngelicaExtension implements BeforeAllCallback, AfterEachCallback,
}
}
private static void setMainThread(Thread thread) {
try {
// Use Unsafe to set final static field - works on all Java versions
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
Field mainThreadField = GLStateManager.class.getDeclaredField("MainThread");
Object base = unsafe.staticFieldBase(mainThreadField);
long offset = unsafe.staticFieldOffset(mainThreadField);
unsafe.putObject(base, offset, thread);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Failed to set MainThread for tests", e);
}
}
@Override
public void close() {
Display.destroy();
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
// Clear any pending GL errors from previous tests
while (GL11.glGetError() != GL11.GL_NO_ERROR) {
// drain errors
}
// Reset matrix stacks to known state by popping until we're at base level
GL11.glMatrixMode(GL11.GL_MODELVIEW);
while (GL11.glGetInteger(GL11.GL_MODELVIEW_STACK_DEPTH) > EXPECTED_MODELVIEW_STACK_DEPTH) {
GL11.glPopMatrix();
}
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_PROJECTION);
while (GL11.glGetInteger(GL11.GL_PROJECTION_STACK_DEPTH) > EXPECTED_PROJECTION_STACK_DEPTH) {
GL11.glPopMatrix();
}
GL11.glLoadIdentity();
// Reset to MODELVIEW mode (typical default)
GL11.glMatrixMode(GL11.GL_MODELVIEW);
// Clear any errors from cleanup
while (GL11.glGetError() != GL11.GL_NO_ERROR) {
// drain errors
}
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
assertEquals(GL11.GL_NO_ERROR, GL11.glGetError(), "GL Error");
// Check for GL errors first
int error = GL11.glGetError();
assertEquals(GL11.GL_NO_ERROR, error,
() -> "GL Error: " + GLU.gluErrorString(error));
// Check matrix stack depths - unbalanced push/pop is a common bug
int modelviewDepth = GL11.glGetInteger(GL11.GL_MODELVIEW_STACK_DEPTH);
int projectionDepth = GL11.glGetInteger(GL11.GL_PROJECTION_STACK_DEPTH);
assertEquals(EXPECTED_MODELVIEW_STACK_DEPTH, modelviewDepth,
"MODELVIEW stack depth mismatch - unbalanced glPushMatrix/glPopMatrix");
assertEquals(EXPECTED_PROJECTION_STACK_DEPTH, projectionDepth,
"PROJECTION stack depth mismatch - unbalanced glPushMatrix/glPopMatrix");
// Check matrix mode is reset to MODELVIEW
int matrixMode = GL11.glGetInteger(GL11.GL_MATRIX_MODE);
assertEquals(GL11.GL_MODELVIEW, matrixMode,
() -> "Matrix mode not reset to MODELVIEW, was: " + GLDebug.getMatrixModeName(matrixMode));
}
}
@@ -0,0 +1,208 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuad;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadViewMutable;
import com.gtnewhorizons.angelica.glsm.recording.AccumulatedDraw;
import org.joml.Matrix4f;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Helper class for building test data for display list batching tests.
* Allows creating quads and AccumulatedDraws without GL context.
*/
public final class DisplayListTestHelper {
private DisplayListTestHelper() {}
// ==================== Quad Builders ====================
/**
* Create a quad from 4 vertex positions.
* @param vertices 4 vertices, each as [x, y, z]
*/
public static ModelQuad createQuad(float[][] vertices) {
if (vertices.length != 4) {
throw new IllegalArgumentException("Quad requires exactly 4 vertices");
}
ModelQuad quad = new ModelQuad();
for (int i = 0; i < 4; i++) {
quad.setX(i, vertices[i][0]);
quad.setY(i, vertices[i][1]);
quad.setZ(i, vertices[i][2]);
// Set default color (white, fully opaque)
quad.setColor(i, 0xFFFFFFFF);
// Set default UVs
quad.setTexU(i, i == 1 || i == 2 ? 1.0f : 0.0f);
quad.setTexV(i, i >= 2 ? 1.0f : 0.0f);
}
return quad;
}
/**
* Create a simple unit quad at origin (0,0,0) to (1,1,0).
*/
public static ModelQuad createSimpleQuad() {
return createQuad(new float[][] {
{0, 0, 0},
{1, 0, 0},
{1, 1, 0},
{0, 1, 0}
});
}
/**
* Create a quad at a specific position (offset from origin).
*/
public static ModelQuad createQuadAt(float x, float y, float z) {
return createQuad(new float[][] {
{x, y, z},
{x + 1, y, z},
{x + 1, y + 1, z},
{x, y + 1, z}
});
}
/**
* Create multiple simple quads.
*/
public static List<ModelQuadViewMutable> createQuads(int count) {
List<ModelQuadViewMutable> quads = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
quads.add(createSimpleQuad());
}
return quads;
}
// ==================== AccumulatedDraw Builders ====================
/**
* Create an AccumulatedDraw with specified parameters.
*/
public static AccumulatedDraw createDraw(
List<ModelQuadViewMutable> quads,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex) {
return new AccumulatedDraw(quads, transform, flags, commandIndex);
}
/**
* Create an AccumulatedDraw with a single quad and identity transform.
*/
public static AccumulatedDraw createSimpleDraw(int commandIndex) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
new Matrix4f(),
positionOnly(),
commandIndex
);
}
/**
* Create an AccumulatedDraw with specified transform.
*/
public static AccumulatedDraw createDrawWithTransform(Matrix4f transform, int commandIndex) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
transform,
positionOnly(),
commandIndex
);
}
/**
* Create an AccumulatedDraw with specified flags.
*/
public static AccumulatedDraw createDrawWithFlags(CapturingTessellator.Flags flags, int commandIndex) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
new Matrix4f(),
flags,
commandIndex
);
}
/**
* Create an AccumulatedDraw with multiple quads.
*/
public static AccumulatedDraw createDrawWithQuads(int quadCount, int commandIndex) {
return createDraw(
createQuads(quadCount),
new Matrix4f(),
positionOnly(),
commandIndex
);
}
// ==================== Flag Helpers ====================
/**
* Flags for position-only vertices (no color, texture, brightness, normals).
*/
public static CapturingTessellator.Flags positionOnly() {
return new CapturingTessellator.Flags(false, false, false, false);
}
/**
* Flags for position + color vertices.
*/
public static CapturingTessellator.Flags positionColor() {
return new CapturingTessellator.Flags(false, false, true, false);
}
/**
* Flags for position + texture vertices.
*/
public static CapturingTessellator.Flags positionTexture() {
return new CapturingTessellator.Flags(true, false, false, false);
}
/**
* Flags for position + color + texture vertices.
*/
public static CapturingTessellator.Flags positionColorTexture() {
return new CapturingTessellator.Flags(true, false, true, false);
}
/**
* Flags for full vertex format (all attributes).
*/
public static CapturingTessellator.Flags fullFormat() {
return new CapturingTessellator.Flags(true, true, true, true);
}
// ==================== Transform Helpers ====================
/**
* Create identity transform.
*/
public static Matrix4f identity() {
return new Matrix4f();
}
/**
* Create translation transform.
*/
public static Matrix4f translate(float x, float y, float z) {
return new Matrix4f().translate(x, y, z);
}
/**
* Create rotation transform (around Y axis).
*/
public static Matrix4f rotateY(float degrees) {
return new Matrix4f().rotateY((float) Math.toRadians(degrees));
}
/**
* Create scale transform.
*/
public static Matrix4f scale(float s) {
return new Matrix4f().scale(s);
}
}
@@ -104,16 +104,6 @@ public class GLMS_MatrixStack_UnitTest {
GLStateManager.glPopMatrix();
testMatrix.identity();
validateMatrix(testMatrix, matrix.getEnum, "Popped " + matrix.name + " Matrix");
}
@AfterAll
static void cleanup() {
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_PROJECTION);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_TEXTURE);
GLStateManager.glLoadIdentity();
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
}
@@ -0,0 +1,320 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator;
import com.gtnewhorizon.gtnhlib.client.renderer.vbo.VertexBuffer;
import com.gtnewhorizons.angelica.AngelicaExtension;
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;
import java.util.List;
import java.util.Set;
import static com.gtnewhorizons.angelica.glsm.DisplayListTestHelper.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for display list format-based batching behavior.
*
* <p>With format-based batching, VBO allocation depends ONLY on vertex format:
* <ul>
* <li>All draws with the same format share a single VBO</li>
* <li>Different transforms produce separate DrawRangeCmd but share the VBO</li>
* <li>Different formats produce separate VBOs</li>
* </ul>
*
* <p>Tests call {@link DisplayListManager#buildOptimizedDisplayList} directly
* with manually constructed AccumulatedDraws. GL context needed for VBO creation.</p>
*/
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_Batching_Test {
// ==================== Helper Methods ====================
private long countDrawRangeCmds(DisplayListCommand[] commands) {
long count = 0;
for (DisplayListCommand cmd : commands) {
if (cmd instanceof DrawRangeCmd) count++;
}
return count;
}
private long countMultMatrixCmds(DisplayListCommand[] commands) {
long count = 0;
for (DisplayListCommand cmd : commands) {
if (cmd instanceof MultMatrixCmd) count++;
}
return count;
}
private Set<VertexBuffer> getUniqueVBOs(DisplayListCommand[] commands) {
Set<VertexBuffer> vbos = new HashSet<>();
for (DisplayListCommand cmd : commands) {
if (cmd instanceof DrawRangeCmd) {
vbos.add(((DrawRangeCmd) cmd).sharedVbo());
}
}
return vbos;
}
// ==================== Pure Unit Tests (No GL Context) ====================
@Test
void testConsecutiveDrawsWithSameFormatShareSingleVBO() {
// Setup: 3 draws at the same command index, same format
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createSimpleDraw(0));
draws.add(createSimpleDraw(0));
draws.add(createSimpleDraw(0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 VBO for all draws (format-based batching)
assertEquals(1, result.ownedVbos().length,
"3 draws with same format should produce 1 shared VBO");
// Verify: 1 DrawRangeCmd covering all geometry
long drawCount = countDrawRangeCmds(optimized);
assertEquals(1, drawCount,
"3 consecutive draws with same format and transform should produce 1 DrawRangeCmd");
}
@Test
void testDrawsWithDifferentFormatsProduceSeparateVBOs() {
// Setup: 3 draws with different formats
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithFlags(positionOnly(), 0));
draws.add(createDrawWithFlags(positionTexture(), 0));
draws.add(createDrawWithFlags(positionColorTexture(), 0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 3 VBOs (one per format)
assertEquals(3, result.ownedVbos().length,
"3 draws with different formats should produce 3 VBOs");
// Verify: 3 DrawRangeCmds
long drawCount = countDrawRangeCmds(optimized);
assertEquals(3, drawCount, "3 draws with different formats should produce 3 DrawRangeCmds");
}
@Test
void testDifferentTransformsSameFormatShareVBO() {
// 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
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);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 shared VBO (same format)
assertEquals(1, result.ownedVbos().length, "Draws with different transforms but same format should share 1 VBO");
// Verify: 3 DrawRangeCmds (one per draw, each with different transform)
long drawCount = countDrawRangeCmds(optimized);
assertEquals(3, drawCount, "3 draws with different transforms should produce 3 DrawRangeCmds");
// Verify: All DrawRangeCmds reference the same VBO
Set<VertexBuffer> uniqueVBOs = getUniqueVBOs(optimized);
assertEquals(1, uniqueVBOs.size(), "All DrawRangeCmds should reference the same shared VBO");
// Should have MultMatrix commands for the transforms
long matrixCount = countMultMatrixCmds(optimized);
assertTrue(matrixCount >= 2, "Should have MultMatrix commands for each unique transform");
}
@Test
void testManyDrawsSameFormatProduceSingleVBO() {
// Setup: 100 draws at same position, same format
List<AccumulatedDraw> draws = new ArrayList<>();
for (int i = 0; i < 100; i++) {
draws.add(createSimpleDraw(0));
}
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 shared VBO
assertEquals(1, result.ownedVbos().length, "100 draws with same format should produce 1 shared VBO");
// Verify: 1 DrawRangeCmd (all same transform)
long drawCount = countDrawRangeCmds(optimized);
assertEquals(1, drawCount,
"100 consecutive draws with same format and transform should produce 1 DrawRangeCmd");
}
@Test
void testDrawsWithMultipleQuadsSameFormat() {
// Setup: draws with varying quad counts should share VBO if same format
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithQuads(1, 0));
draws.add(createDrawWithQuads(5, 0));
draws.add(createDrawWithQuads(10, 0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 shared VBO (total 16 quads)
assertEquals(1, result.ownedVbos().length, "Draws with different quad counts but same format should share 1 VBO");
// Verify: 1 DrawRangeCmd (all same transform)
long drawCount = countDrawRangeCmds(optimized);
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
// (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
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));
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(
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 regardless of transforms");
// Verify: 3 DrawRangeCmds (different transforms require separate draw commands)
long drawCount = countDrawRangeCmds(optimized);
assertEquals(3, drawCount, "Draws with different recorded transforms must produce separate DrawRangeCmds");
}
@Test
void testDrawsWithSameTransformProduceSingleDrawRangeCmd() {
// Draws with the same format AND same transform produce a single DrawRangeCmd
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));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 shared VBO
assertEquals(1, result.ownedVbos().length,
"Draws with same format should share 1 VBO");
// Verify: 1 DrawRangeCmd (same format AND same transform)
long drawCount = countDrawRangeCmds(optimized);
assertEquals(1, drawCount, "Draws with same format and transform should produce 1 DrawRangeCmd");
// Should have 1 MultMatrix for the common transform
long matrixCount = countMultMatrixCmds(optimized);
assertEquals(1, matrixCount,
"Should have exactly 1 MultMatrix for the common transform");
}
@Test
void testInterleavedFormatsProduceSeparateVBOs() {
// Setup: A A B A A - should produce 2 VBOs (one for A, one for B)
// With format-based batching, all A draws are grouped together and merged
// (since they have same transform), producing 2 DrawRangeCmds total
CapturingTessellator.Flags flagsA = positionOnly();
CapturingTessellator.Flags flagsB = positionTexture();
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithFlags(flagsA, 0)); // A
draws.add(createDrawWithFlags(flagsA, 0)); // A
draws.add(createDrawWithFlags(flagsB, 0)); // B
draws.add(createDrawWithFlags(flagsA, 0)); // A
draws.add(createDrawWithFlags(flagsA, 0)); // A
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 2 VBOs (one for format A, one for format B)
assertEquals(2, result.ownedVbos().length, "A A B A A pattern should produce 2 VBOs (one per format)");
// Verify: 2 DrawRangeCmds (all A's merged into 1, B is 1)
// With format-based batching, all A draws go into the same FormatBuffer
// and get merged (same transform). Order within same command index doesn't matter.
long drawCount = countDrawRangeCmds(optimized);
assertEquals(2, drawCount, "A A B A A pattern with same transform should produce 2 DrawRangeCmds (A's merged, B separate)");
}
}
@@ -0,0 +1,176 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.junit.jupiter.api.AfterEach;
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;
/**
* Tests for display list format optimization.
* Verifies that display lists work correctly with different vertex attribute combinations
* and that optimal formats are selected.
*/
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_FormatOptimization_Test {
private int displayList = -1;
@AfterEach
void cleanup() {
if (displayList > 0) {
GLStateManager.glDeleteLists(displayList, 1);
displayList = -1;
}
// Reset GL state to avoid test pollution
GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GLStateManager.glTexCoord2f(0.0f, 0.0f);
GLStateManager.glNormal3f(0.0f, 0.0f, 1.0f);
}
@Test
void testPositionOnlyFormat() {
// Create a display list with only position data (no color, texture, brightness, or normals)
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.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
GLStateManager.glCallList(displayList);
}
@Test
void testPositionTextureFormat() {
// Create a display list with position and texture coordinates
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.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
GLStateManager.glCallList(displayList);
}
@Test
void testPositionColorTextureFormat() {
// Create a display list with position, color, and texture
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.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
GLStateManager.glCallList(displayList);
}
@Test
void testFullFormat() {
// Create a display list with all attributes: position, color, texture, and normal
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.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
GLStateManager.glCallList(displayList);
}
@Test
void testMultipleDrawsInDisplayList() {
// Test a display list with multiple draws with different formats
displayList = GL11.glGenLists(1);
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();
// 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();
// 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.glEndList();
// Call the list to ensure it doesn't crash (verifies compilation succeeded)
GLStateManager.glCallList(displayList);
}
}
@@ -0,0 +1,265 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.joml.Matrix4f;
import org.junit.jupiter.api.AfterEach;
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 for nested display list execution.
* Verifies that child display lists compose correctly with parent transforms.
*/
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_Nesting_Test {
private int parentList = -1;
private int childList = -1;
@AfterEach
void cleanup() {
if (parentList > 0) {
GLStateManager.glDeleteLists(parentList, 1);
parentList = -1;
}
if (childList > 0) {
GLStateManager.glDeleteLists(childList, 1);
childList = -1;
}
// Reset GL state
GLStateManager.glLoadIdentity();
GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
@Test
void testNestedDisplayListWithParentTransform() {
// Create child list: just a simple quad at origin
childList = GL11.glGenLists(1);
GLStateManager.glNewList(childList, 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.glEndList();
// Create parent list: translate then call child
parentList = GL11.glGenLists(1);
GLStateManager.glNewList(parentList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f); // Parent transform
GLStateManager.glCallList(childList); // Should render child at (10,0,0)
GLStateManager.glEndList();
// Execute parent list
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(parentList);
// Check final matrix state - should have parent's translate applied
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Expected: translate(10, 0, 0)
Matrix4f expected = new Matrix4f().translate(10.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
// Compare buffers directly (both are column-major OpenGL format)
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testNestedDisplayListWithBothTransforms() {
// Create child list: translate and draw
childList = GL11.glGenLists(1);
GLStateManager.glNewList(childList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); // Child transform
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.glEndList();
// Create parent list: translate then call child
parentList = GL11.glGenLists(1);
GLStateManager.glNewList(parentList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f); // Parent transform
GLStateManager.glCallList(childList); // Child adds (1,0,0) to parent's (10,0,0)
GLStateManager.glEndList();
// Execute parent list
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(parentList);
// Check final matrix state - should be translate(10,0,0) * translate(1,0,0) = translate(11,0,0)
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Expected: translate(11, 0, 0) = parent(10) + child(1)
Matrix4f expected = new Matrix4f().translate(11.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
// Compare buffers directly
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testNestedDisplayListWithPushPop() {
// Create child list with push/pop (shouldn't affect parent's matrix)
childList = GL11.glGenLists(1);
GLStateManager.glNewList(childList, GL11.GL_COMPILE);
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); // Child transform
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.glPopMatrix(); // Restore matrix to parent's state
GLStateManager.glEndList();
// Create parent list
parentList = GL11.glGenLists(1);
GLStateManager.glNewList(parentList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f); // Parent transform
GLStateManager.glCallList(childList); // Child uses push/pop
GLStateManager.glEndList();
// Execute parent list
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(parentList);
// Check final matrix state - should still be translate(10,0,0) because child did push/pop
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Expected: translate(10, 0, 0) only (child's transform was popped)
Matrix4f expected = new Matrix4f().translate(10.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
// Compare buffers directly
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testDeepNesting() {
// Create deeply nested lists: grandchild -> child -> parent
int grandchildList = GL11.glGenLists(1);
GLStateManager.glNewList(grandchildList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); // Grandchild: +1
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.glEndList();
childList = GL11.glGenLists(1);
GLStateManager.glNewList(childList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f); // Child: +10
GLStateManager.glCallList(grandchildList); // Grandchild: +1
GLStateManager.glEndList();
parentList = GL11.glGenLists(1);
GLStateManager.glNewList(parentList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(100.0f, 0.0f, 0.0f); // Parent: +100
GLStateManager.glCallList(childList); // Child: +10, Grandchild: +1
GLStateManager.glEndList();
// Execute parent list
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(parentList);
// Check final matrix state - should be 100 + 10 + 1 = 111
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Expected: translate(111, 0, 0)
Matrix4f expected = new Matrix4f().translate(111.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
// Compare buffers directly
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch", i));
}
GLStateManager.glPopMatrix();
// Cleanup grandchild
GLStateManager.glDeleteLists(grandchildList, 1);
}
@Test
void testMismatchedPushPopInSeparateLists() {
// This test verifies that push and pop matrix operations can be split across separate display lists.
// This is a uncommon but technically valid per the spec.
// Get initial stack depth
int initialDepth = GL11.glGetInteger(GL11.GL_MODELVIEW_STACK_DEPTH);
// Create a display list that only pushes the matrix
int matrixPushList = GL11.glGenLists(1);
GLStateManager.glNewList(matrixPushList, GL11.GL_COMPILE);
GLStateManager.glPushMatrix();
GLStateManager.glEndList();
// Create a display list that only pops the matrix
int matrixPopList = GL11.glGenLists(1);
GLStateManager.glNewList(matrixPopList, GL11.GL_COMPILE);
GLStateManager.glPopMatrix();
GLStateManager.glEndList();
// Verify initial stack depth hasn't changed after compilation
assertEquals(initialDepth, GL11.glGetInteger(GL11.GL_MODELVIEW_STACK_DEPTH), "Stack depth should be unchanged after compiling display lists");
// Call the push list - should increase stack depth
GLStateManager.glCallList(matrixPushList);
int depthAfterPush = GL11.glGetInteger(GL11.GL_MODELVIEW_STACK_DEPTH);
assertEquals(initialDepth + 1, depthAfterPush, "Stack depth should increase by 1 after calling push list");
// Optionally do some operations here that would benefit from the pushed matrix
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f);
// Call the pop list - should restore stack depth
GLStateManager.glCallList(matrixPopList);
int depthAfterPop = GL11.glGetInteger(GL11.GL_MODELVIEW_STACK_DEPTH);
assertEquals(initialDepth, depthAfterPop, "Stack depth should return to initial after calling pop list");
// Cleanup
GLStateManager.glDeleteLists(matrixPushList, 1);
GLStateManager.glDeleteLists(matrixPopList, 1);
}
}
@@ -0,0 +1,320 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.joml.Matrix4f;
import org.junit.jupiter.api.AfterEach;
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 java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* Stress tests for display list system.
* Verifies system stability under high load conditions.
*
* <p>Note: Tests that require actual geometry capture via Tessellator
* need the full Minecraft environment. These tests focus on matrix
* operations and display list lifecycle which work with raw GL calls.</p>
*/
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_Stress_Test {
private final List<Integer> testLists = new ArrayList<>();
@AfterEach
void cleanup() {
for (int list : testLists) {
if (list > 0) {
GLStateManager.glDeleteLists(list, 1);
}
}
testLists.clear();
GLStateManager.glLoadIdentity();
}
private int createAndTrackList() {
int list = GL11.glGenLists(1);
testLists.add(list);
return list;
}
@Test
void testManyDisplayListsWithTransforms() {
// Create and compile many display lists with transforms
final int numLists = 500;
for (int i = 0; i < numLists; i++) {
int list = createAndTrackList();
GLStateManager.glNewList(list, GL11.GL_COMPILE);
// Each list has a unique transform
GLStateManager.glTranslatef(i * 0.1f, 0, 0);
GLStateManager.glEndList();
// Verify each list was compiled
assertTrue(DisplayListManager.displayListExists(list),
"Display list " + list + " should exist after compilation");
}
// Verify all lists still exist
for (int list : testLists) {
assertTrue(DisplayListManager.displayListExists(list),
"All " + numLists + " display lists should exist");
}
}
@Test
void testManyTransformsInSingleList() {
// Create a single display list with many transform operations
final int numTransforms = 1000;
int list = createAndTrackList();
GLStateManager.glNewList(list, GL11.GL_COMPILE);
// Many push/pop pairs with transforms
for (int i = 0; i < numTransforms; i++) {
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(i * 0.001f, 0, 0);
GLStateManager.glRotatef(i * 0.36f, 0, 1, 0);
GLStateManager.glPopMatrix();
}
// Final transform that should be the result
GLStateManager.glTranslatef(42.0f, 0, 0);
GLStateManager.glEndList();
// Execute and verify final transform
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(list);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// All push/pop pairs cancel out, only final translate remains
assertEquals(42.0f, actualBuf.get(12), 0.0001f,
"After many push/pop pairs, only final transform should remain");
GLStateManager.glPopMatrix();
}
@Test
void testRapidCompileDeleteCycle() {
// Rapidly create and delete display lists to test resource management
final int cycles = 100;
for (int cycle = 0; cycle < cycles; cycle++) {
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE);
GLStateManager.glTranslatef(cycle, 0, 0);
GLStateManager.glEndList();
// Immediately delete
GLStateManager.glDeleteLists(list, 1);
// Verify it's gone
assertFalse(DisplayListManager.displayListExists(list),
"Display list should be deleted after glDeleteLists");
}
}
@Test
void testDeepNestingChain() {
// Create a chain of display lists that call each other
final int chainLength = 50;
int[] chain = new int[chainLength];
// Create from innermost to outermost
for (int i = 0; i < chainLength; i++) {
chain[i] = createAndTrackList();
GLStateManager.glNewList(chain[i], GL11.GL_COMPILE);
// Each list translates by 1 and calls the previous list (if any)
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f);
if (i > 0) {
GLStateManager.glCallList(chain[i - 1]);
}
GLStateManager.glEndList();
}
// Execute the outermost list
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(chain[chainLength - 1]);
// Final matrix should have translation = chainLength (each list adds 1)
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
Matrix4f expected = new Matrix4f().translate(chainLength, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch for chain of %d lists", i, chainLength));
}
GLStateManager.glPopMatrix();
}
@Test
void testManyPushPopOperations() {
// Stress test push/pop stack operations
int list = createAndTrackList();
GLStateManager.glNewList(list, GL11.GL_COMPILE);
// Go deep into the stack
final int stackDepth = 30; // GL minimum is 32
for (int i = 0; i < stackDepth; i++) {
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f);
}
// Pop all the way back
for (int i = 0; i < stackDepth; i++) {
GLStateManager.glPopMatrix();
}
// Add final known transform
GLStateManager.glTranslatef(99.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Execute and verify
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(list);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// All pushes and pops cancel out, only final 99 translate remains
assertEquals(99.0f, actualBuf.get(12), 0.0001f,
"After balanced push/pop, only final transform should remain");
GLStateManager.glPopMatrix();
}
@Test
void testRapidListSwitching() {
// Create multiple lists and rapidly switch between them
final int numLists = 20;
int[] lists = new int[numLists];
// Create all lists with unique transforms
for (int i = 0; i < numLists; i++) {
lists[i] = createAndTrackList();
GLStateManager.glNewList(lists[i], GL11.GL_COMPILE);
GLStateManager.glTranslatef(i + 1, 0, 0); // List i translates by (i+1)
GLStateManager.glEndList();
}
// Rapidly switch between calling different lists
for (int iteration = 0; iteration < 100; iteration++) {
for (int i = 0; i < numLists; i++) {
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(lists[i]);
// Verify correct transform was applied
FloatBuffer buf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buf);
assertEquals(i + 1, buf.get(12), 0.0001f,
"List " + i + " should translate by " + (i + 1));
GLStateManager.glPopMatrix();
}
}
}
@Test
void testRecompileSameId() {
// Test recompiling a display list with the same ID
int list = createAndTrackList();
// First compilation
GLStateManager.glNewList(list, GL11.GL_COMPILE);
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Verify first compilation
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(list);
FloatBuffer buf1 = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buf1);
assertEquals(5.0f, buf1.get(12), 0.0001f, "First compilation should translate by 5");
GLStateManager.glPopMatrix();
// Recompile with different transform
GLStateManager.glNewList(list, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Verify recompilation
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(list);
FloatBuffer buf2 = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buf2);
assertEquals(10.0f, buf2.get(12), 0.0001f, "Recompilation should translate by 10");
GLStateManager.glPopMatrix();
}
@Test
void testComplexTransformSequence() {
// Test a complex sequence of transforms that could expose numerical issues
int list = createAndTrackList();
GLStateManager.glNewList(list, GL11.GL_COMPILE);
// Apply transforms that together should result in a known value
GLStateManager.glTranslatef(100.0f, 0.0f, 0.0f);
GLStateManager.glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
GLStateManager.glTranslatef(50.0f, 0.0f, 0.0f); // This goes in Y direction after rotate
GLStateManager.glRotatef(-90.0f, 0.0f, 0.0f, 1.0f); // Cancel the rotation
GLStateManager.glTranslatef(25.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(list);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Build expected with JOML
Matrix4f expected = new Matrix4f()
.translate(100.0f, 0.0f, 0.0f)
.rotate((float) Math.toRadians(90.0f), 0.0f, 0.0f, 1.0f)
.translate(50.0f, 0.0f, 0.0f)
.rotate((float) Math.toRadians(-90.0f), 0.0f, 0.0f, 1.0f)
.translate(25.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.001f,
String.format("Matrix[%d] mismatch in complex transform", i));
}
GLStateManager.glPopMatrix();
}
}
@@ -0,0 +1,648 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import com.gtnewhorizons.angelica.glsm.recording.CompiledDisplayList;
import com.gtnewhorizons.angelica.glsm.recording.GLCommand;
import org.joml.Matrix4f;
import org.junit.jupiter.api.AfterEach;
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 java.util.List;
import java.util.Map;
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>
*/
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_TransformCollapsing_Test {
private int testList = -1;
private int nestedList = -1;
@AfterEach
void cleanup() {
if (testList > 0) {
GLStateManager.glDeleteLists(testList, 1);
testList = -1;
}
if (nestedList > 0) {
GLStateManager.glDeleteLists(nestedList, 1);
nestedList = -1;
}
GLStateManager.glLoadIdentity();
}
// ==================== Transform Collapsing Tests ====================
@Test
void testConsecutiveTransformsCollapseToSingleMultMatrix() {
// Multiple consecutive transforms should collapse to one MultMatrix in optimized list
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// 5 consecutive transforms
GLStateManager.glTranslatef(1.0f, 2.0f, 3.0f);
GLStateManager.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
GLStateManager.glScalef(2.0f, 2.0f, 2.0f);
GLStateManager.glTranslatef(-0.5f, -0.5f, -0.5f);
GLStateManager.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Verify optimization: should have collapsed to 1 MultMatrix
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled, "Display list should be compiled");
Map<Integer, Integer> counts = compiled.getCommandCounts();
int multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
int individualTransformCount = counts.getOrDefault(GLCommand.TRANSLATE, 0)
+ counts.getOrDefault(GLCommand.ROTATE, 0)
+ counts.getOrDefault(GLCommand.SCALE, 0);
assertEquals(1, multMatrixCount,
"5 consecutive transforms should collapse to 1 MultMatrix");
assertEquals(0, individualTransformCount,
"No individual transform commands should remain in optimized list");
// Verify behavioral correctness: matrix state should match expected
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
Matrix4f expected = new Matrix4f()
.translate(1.0f, 2.0f, 3.0f)
.rotate((float) Math.toRadians(45.0f), 0.0f, 1.0f, 0.0f)
.scale(2.0f, 2.0f, 2.0f)
.translate(-0.5f, -0.5f, -0.5f)
.rotate((float) Math.toRadians(30.0f), 1.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch after collapsed transforms", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testPushPopPreservesStackSemantics() {
// Push/Pop should maintain proper stack semantics with collapsed transforms
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f); // Outer translate
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f); // Inner translate (isolated)
GLStateManager.glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
GLStateManager.glPopMatrix();
GLStateManager.glTranslatef(0.0f, 3.0f, 0.0f); // After pop
GLStateManager.glEndList();
// Verify optimization: should have Push, Pop, and collapsed MultMatrix commands
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
Map<Integer, Integer> counts = compiled.getCommandCounts();
int pushCount = counts.getOrDefault(GLCommand.PUSH_MATRIX, 0);
int popCount = counts.getOrDefault(GLCommand.POP_MATRIX, 0);
assertEquals(1, pushCount, "Should have exactly 1 PushMatrix");
assertEquals(1, popCount, "Should have exactly 1 PopMatrix");
// Verify behavioral correctness
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Expected: translate(10,0,0) then translate(0,3,0) = translate(10,3,0)
// The inner push/pop isolates the (5,0,0) translate and rotation
Matrix4f expected = new Matrix4f()
.translate(10.0f, 0.0f, 0.0f)
.translate(0.0f, 3.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch - push/pop should isolate inner transforms", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testNestedPushPopWithTransforms() {
// Nested push/pop with transforms at each level
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); // Level 0
GLStateManager.glPushMatrix(); // Enter level 1
GLStateManager.glTranslatef(2.0f, 0.0f, 0.0f);
GLStateManager.glPushMatrix(); // Enter level 2
GLStateManager.glTranslatef(3.0f, 0.0f, 0.0f);
GLStateManager.glPopMatrix(); // Back to level 1
GLStateManager.glTranslatef(4.0f, 0.0f, 0.0f); // At level 1
GLStateManager.glPopMatrix(); // Back to level 0
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f); // At level 0
GLStateManager.glEndList();
// Verify behavioral correctness
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Final state: translate(1,0,0) + translate(5,0,0) = translate(6,0,0)
// Level 1 and 2 transforms are isolated by push/pop
Matrix4f expected = new Matrix4f()
.translate(1.0f, 0.0f, 0.0f)
.translate(5.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
assertEquals(expectedBuf.get(12), actualBuf.get(12), 0.0001f,
"X translation should be 6.0 (1+5, inner transforms isolated)");
GLStateManager.glPopMatrix(); // Balance the push from line 185
}
@Test
void testCallListFlushesTransform() {
// Create a nested display list
nestedList = GL11.glGenLists(1);
GLStateManager.glNewList(nestedList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(100.0f, 0.0f, 0.0f); // Large translate to verify it's applied
GLStateManager.glEndList();
// Create parent list that transforms then calls nested list
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f); // Before CallList
GLStateManager.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
GLStateManager.glCallList(nestedList); // Barrier - should flush transforms
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); // After CallList
GLStateManager.glEndList();
// Verify optimization: MultMatrix should be emitted before CallList
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
List<Integer> opcodes = compiled.getCommandOpcodes();
// Find positions of MultMatrix and CallList
int multMatrixIndex = opcodes.indexOf(GLCommand.MULT_MATRIX);
int callListIndex = opcodes.indexOf(GLCommand.CALL_LIST);
assertTrue(multMatrixIndex >= 0, "Should have MultMatrix command");
assertTrue(callListIndex >= 0, "Should have CallList command");
assertTrue(multMatrixIndex < callListIndex,
"MultMatrix should be emitted BEFORE CallList (transform flush at barrier)");
// Verify behavioral correctness
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Build expected: translate(10,0,0) * rotate(90,Y) * translate(100,0,0) * translate(1,0,0)
// After 90-degree Y rotation, X axis points into -Z, so nested translate goes along -Z
Matrix4f expected = new Matrix4f()
.translate(10.0f, 0.0f, 0.0f)
.rotate((float) Math.toRadians(90.0f), 0.0f, 1.0f, 0.0f)
.translate(100.0f, 0.0f, 0.0f)
.translate(1.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch - nested list should see parent's transformed state", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testIdentityTransformNotEmitted() {
// A display list with only identity-equivalent transforms should not emit MultMatrix
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// These cancel out to identity
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f);
GLStateManager.glTranslatef(-5.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
Map<Integer, Integer> counts = compiled.getCommandCounts();
int multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
assertEquals(0, multMatrixCount,
"Identity-equivalent transforms should not emit MultMatrix");
}
@Test
void testTransformBetweenDrawsCreatesSeparateBatches() {
// This test verifies that draws with different transforms are not batched together
// Note: Without actual Tessellator integration, we can only verify command structure
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// Simulate: Transform A, Draw, Transform B, Draw
// The draws should NOT be in the same batch
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f);
// (Would have a draw here in real usage)
GLStateManager.glTranslatef(2.0f, 0.0f, 0.0f);
// (Would have another draw here)
GLStateManager.glEndList();
// Verify the transforms are accumulated correctly
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Total translation should be 3.0 (1+2)
assertEquals(3.0f, actualBuf.get(12), 0.0001f,
"Consecutive translates should accumulate");
GLStateManager.glPopMatrix();
}
@Test
void testLoadIdentityResetsAccumulation() {
// LoadIdentity should reset accumulated transforms
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(100.0f, 100.0f, 100.0f); // Big translate
GLStateManager.glLoadIdentity(); // Reset
GLStateManager.glTranslatef(1.0f, 2.0f, 3.0f); // New translate
GLStateManager.glEndList();
// Verify: final state should only have the second translate
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
assertEquals(1.0f, actualBuf.get(12), 0.0001f, "X should be 1.0 (LoadIdentity reset)");
assertEquals(2.0f, actualBuf.get(13), 0.0001f, "Y should be 2.0 (LoadIdentity reset)");
assertEquals(3.0f, actualBuf.get(14), 0.0001f, "Z should be 3.0 (LoadIdentity reset)");
GLStateManager.glPopMatrix();
}
@Test
void testMultMatrixAccumulation() {
// MultMatrix should be accumulated like other transforms
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f);
// Create a rotation matrix and multiply
Matrix4f rotMatrix = new Matrix4f().rotate((float) Math.toRadians(90.0f), 0.0f, 1.0f, 0.0f);
FloatBuffer rotBuf = BufferUtils.createFloatBuffer(16);
rotMatrix.get(rotBuf);
rotBuf.rewind();
GLStateManager.glMultMatrix(rotBuf);
GLStateManager.glTranslatef(2.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Verify optimization: should collapse to 1 MultMatrix
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
Map<Integer, Integer> counts = compiled.getCommandCounts();
int multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
assertEquals(1, multMatrixCount,
"Translate + MultMatrix + Translate should collapse to 1 MultMatrix");
// Verify behavioral correctness
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
Matrix4f expected = new Matrix4f()
.translate(1.0f, 0.0f, 0.0f)
.rotate((float) Math.toRadians(90.0f), 0.0f, 1.0f, 0.0f)
.translate(2.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] mismatch for MultMatrix accumulation", i));
}
GLStateManager.glPopMatrix();
}
// ==================== Batcher Flush Ordering Tests ====================
@Test
void testDrawsBeforePushMatrixAreEmittedBeforePush() {
// This test verifies that draws occurring before a PushMatrix are flushed
// before the PushMatrix command, not delayed until later.
// This was a bug where the batcher wasn't flushed at PushMatrix.
// Create a list with: Transform, (implicit draw would go here), PushMatrix, Transform, PopMatrix
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// Outer transform
GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f);
// Note: If there was a draw here, it should be emitted BEFORE PushMatrix
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(5.0f, 0.0f, 0.0f); // Inner transform
GLStateManager.glPopMatrix();
// Final transform after pop
GLStateManager.glTranslatef(2.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Verify the command structure
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled, "Display list should be compiled");
List<Integer> opcodes = compiled.getCommandOpcodes();
// Find indices of key commands
int pushIndex = opcodes.indexOf(GLCommand.PUSH_MATRIX);
int popIndex = opcodes.indexOf(GLCommand.POP_MATRIX);
// Count MultMatrix commands before first PushMatrix
int multMatrixBeforePushCount = 0;
for (int i = 0; i < opcodes.size() && i < pushIndex; i++) {
if (opcodes.get(i) == GLCommand.MULT_MATRIX) {
multMatrixBeforePushCount++;
}
}
assertTrue(pushIndex >= 0, "Should have PushMatrix command");
assertTrue(popIndex >= 0, "Should have PopMatrix command");
assertTrue(pushIndex < popIndex, "PushMatrix should come before PopMatrix");
// The outer translate(1,0,0) should be emitted as MultMatrix before PushMatrix
assertTrue(multMatrixBeforePushCount >= 1,
"Transform before PushMatrix should be emitted before the Push");
// Verify behavioral correctness: final state should be translate(1,0,0) + translate(2,0,0) = translate(3,0,0)
// The inner translate(5,0,0) is isolated by push/pop
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
assertEquals(3.0f, actualBuf.get(12), 0.0001f,
"X translation should be 3.0 (1+2, inner transform isolated by push/pop)");
GLStateManager.glPopMatrix();
}
@Test
void testDrawsBeforePopMatrixAreEmittedBeforePop() {
// Verify draws inside a push/pop block are emitted before the pop
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glPushMatrix();
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f);
// Draws here should be emitted before PopMatrix
GLStateManager.glPopMatrix();
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
List<Integer> opcodes = compiled.getCommandOpcodes();
int pushIndex = opcodes.indexOf(GLCommand.PUSH_MATRIX);
int popIndex = opcodes.indexOf(GLCommand.POP_MATRIX);
assertTrue(pushIndex >= 0, "Should have PushMatrix");
assertTrue(popIndex >= 0, "Should have PopMatrix");
assertTrue(pushIndex < popIndex, "Push should come before Pop");
// The inner translate should be emitted between push and pop (or collapsed to residual)
// At minimum, the structure should be valid
assertTrue(popIndex > pushIndex, "PopMatrix should follow PushMatrix");
}
// ==================== LoadMatrix Tests ====================
@Test
void testLoadMatrixSetsAbsoluteTransform() {
// LoadMatrix should set an absolute matrix, not multiply
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// First, apply a large transform
GLStateManager.glTranslatef(100.0f, 200.0f, 300.0f);
// Then load a specific matrix (should replace, not multiply)
Matrix4f loadedMatrix = new Matrix4f()
.translate(1.0f, 2.0f, 3.0f)
.rotate((float) Math.toRadians(45.0f), 0.0f, 1.0f, 0.0f);
FloatBuffer loadBuf = BufferUtils.createFloatBuffer(16);
loadedMatrix.get(loadBuf);
loadBuf.rewind();
GLStateManager.glLoadMatrix(loadBuf);
GLStateManager.glEndList();
// Verify the LoadMatrixCmd is present
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled, "Display list should be compiled");
Map<Integer, Integer> counts = compiled.getCommandCounts();
int loadMatrixCount = counts.getOrDefault(GLCommand.LOAD_MATRIX, 0);
assertEquals(1, loadMatrixCount,
"Should have exactly 1 LoadMatrix command (can't be absorbed)");
// Verify behavioral correctness: matrix should equal the loaded matrix
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
loadedMatrix.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] should match loaded matrix (not multiplied)", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testLoadMatrixResetsRelativeTransform() {
// After LoadMatrix, subsequent transforms should be relative to the loaded matrix
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// Load a rotation matrix
Matrix4f loadedMatrix = new Matrix4f()
.rotate((float) Math.toRadians(90.0f), 0.0f, 1.0f, 0.0f);
FloatBuffer loadBuf = BufferUtils.createFloatBuffer(16);
loadedMatrix.get(loadBuf);
loadBuf.rewind();
GLStateManager.glLoadMatrix(loadBuf);
// Then translate - this should be relative to the loaded matrix
GLStateManager.glTranslatef(10.0f, 0.0f, 0.0f);
GLStateManager.glEndList();
// Verify behavioral correctness
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
// Expected: loadedMatrix * translate(10,0,0)
Matrix4f expected = new Matrix4f(loadedMatrix).translate(10.0f, 0.0f, 0.0f);
FloatBuffer expectedBuf = BufferUtils.createFloatBuffer(16);
expected.get(expectedBuf);
for (int i = 0; i < 16; i++) {
assertEquals(expectedBuf.get(i), actualBuf.get(i), 0.0001f,
String.format("Matrix[%d] - transform after LoadMatrix should be relative to loaded matrix", i));
}
GLStateManager.glPopMatrix();
}
@Test
void testLoadMatrixThenLoadIdentity() {
// LoadMatrix followed by LoadIdentity should result in identity
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// Load a complex matrix
Matrix4f loadedMatrix = new Matrix4f()
.translate(50.0f, 60.0f, 70.0f)
.rotate((float) Math.toRadians(45.0f), 1.0f, 1.0f, 0.0f);
FloatBuffer loadBuf = BufferUtils.createFloatBuffer(16);
loadedMatrix.get(loadBuf);
loadBuf.rewind();
GLStateManager.glLoadMatrix(loadBuf);
// Then reset to identity
GLStateManager.glLoadIdentity();
// Then apply a simple translate
GLStateManager.glTranslatef(1.0f, 2.0f, 3.0f);
GLStateManager.glEndList();
// Verify: final state should just be translate(1,2,3)
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
assertEquals(1.0f, actualBuf.get(12), 0.0001f, "X should be 1.0");
assertEquals(2.0f, actualBuf.get(13), 0.0001f, "Y should be 2.0");
assertEquals(3.0f, actualBuf.get(14), 0.0001f, "Z should be 3.0");
GLStateManager.glPopMatrix();
}
@Test
void testMultipleLoadMatrixCalls() {
// Multiple LoadMatrix calls should each be recorded
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
// First load
Matrix4f matrix1 = new Matrix4f().translate(10.0f, 0.0f, 0.0f);
FloatBuffer buf1 = BufferUtils.createFloatBuffer(16);
matrix1.get(buf1);
buf1.rewind();
GLStateManager.glLoadMatrix(buf1);
// Second load (replaces first)
Matrix4f matrix2 = new Matrix4f().translate(0.0f, 20.0f, 0.0f);
FloatBuffer buf2 = BufferUtils.createFloatBuffer(16);
matrix2.get(buf2);
buf2.rewind();
GLStateManager.glLoadMatrix(buf2);
GLStateManager.glEndList();
// Verify: final state should be matrix2
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glCallList(testList);
FloatBuffer actualBuf = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, actualBuf);
assertEquals(0.0f, actualBuf.get(12), 0.0001f, "X should be 0.0 (from second LoadMatrix)");
assertEquals(20.0f, actualBuf.get(13), 0.0001f, "Y should be 20.0 (from second LoadMatrix)");
GLStateManager.glPopMatrix();
}
}
@@ -1,11 +1,13 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.opengl.GL11;
import static com.gtnewhorizons.angelica.util.GLSMUtil.verifyState;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_UnitTest {
@@ -43,4 +45,114 @@ class GLSM_DisplayList_UnitTest {
// Reset state that we changed during test
GLStateManager.glDisable(GL11.GL_LIGHTING);
}
// ==================== CullFace Tests ====================
@Test
void testCullFaceInDisplayList() {
// Get initial cull face mode
int initialMode = GL11.glGetInteger(GL11.GL_CULL_FACE_MODE);
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE);
GLStateManager.glCullFace(GL11.GL_FRONT);
GLStateManager.glEndList();
// After compile, mode should be unchanged
assertEquals(initialMode, GL11.glGetInteger(GL11.GL_CULL_FACE_MODE), "CullFace mode should be unchanged after GL_COMPILE");
// Call the display list
GLStateManager.glCallList(list);
// Now mode should be GL_FRONT
assertEquals(GL11.GL_FRONT, GL11.glGetInteger(GL11.GL_CULL_FACE_MODE), "CullFace mode should be GL_FRONT after calling display list");
// Cleanup
GLStateManager.glCullFace(GL11.GL_BACK); // Reset to default
GLStateManager.glDeleteLists(list, 1);
}
@Test
void testCullFaceCompileAndExecute() {
// Get initial cull face mode
int initialMode = GL11.glGetInteger(GL11.GL_CULL_FACE_MODE);
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE_AND_EXECUTE);
GLStateManager.glCullFace(GL11.GL_FRONT_AND_BACK);
GLStateManager.glEndList();
// After compile and execute, mode should be changed
assertEquals(GL11.GL_FRONT_AND_BACK, GL11.glGetInteger(GL11.GL_CULL_FACE_MODE), "CullFace mode should be GL_FRONT_AND_BACK after GL_COMPILE_AND_EXECUTE");
// Cleanup
GLStateManager.glCullFace(GL11.GL_BACK);
GLStateManager.glDeleteLists(list, 1);
}
// ==================== LogicOp Tests ====================
@Test
void testLogicOpInDisplayList() {
// Get initial logic op
int initialOp = GL11.glGetInteger(GL11.GL_LOGIC_OP_MODE);
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE);
GLStateManager.glLogicOp(GL11.GL_XOR);
GLStateManager.glEndList();
// After compile, op should be unchanged
assertEquals(initialOp, GL11.glGetInteger(GL11.GL_LOGIC_OP_MODE), "LogicOp should be unchanged after GL_COMPILE");
// Call the display list
GLStateManager.glCallList(list);
// Now op should be GL_XOR
assertEquals(GL11.GL_XOR, GL11.glGetInteger(GL11.GL_LOGIC_OP_MODE), "LogicOp should be GL_XOR after calling display list");
// Cleanup
GLStateManager.glLogicOp(GL11.GL_COPY); // Reset to default
GLStateManager.glDeleteLists(list, 1);
}
@Test
void testLogicOpCompileAndExecute() {
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE_AND_EXECUTE);
GLStateManager.glLogicOp(GL11.GL_INVERT);
GLStateManager.glEndList();
// After compile and execute, op should be changed
assertEquals(GL11.GL_INVERT, GL11.glGetInteger(GL11.GL_LOGIC_OP_MODE), "LogicOp should be GL_INVERT after GL_COMPILE_AND_EXECUTE");
// Cleanup
GLStateManager.glLogicOp(GL11.GL_COPY);
GLStateManager.glDeleteLists(list, 1);
}
@Test
void testMultipleStateChangesInDisplayList() {
// Test that multiple state commands work together
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE);
GLStateManager.glCullFace(GL11.GL_FRONT);
GLStateManager.glLogicOp(GL11.GL_AND);
GLStateManager.glEnable(GL11.GL_LIGHTING);
GLStateManager.glEndList();
// Call the list
GLStateManager.glCallList(list);
// Verify all states changed
assertEquals(GL11.GL_FRONT, GL11.glGetInteger(GL11.GL_CULL_FACE_MODE), "CullFace should be GL_FRONT");
assertEquals(GL11.GL_AND, GL11.glGetInteger(GL11.GL_LOGIC_OP_MODE), "LogicOp should be GL_AND");
verifyState(GL11.GL_LIGHTING, true, "GL_LIGHTING should be enabled");
// Cleanup
GLStateManager.glCullFace(GL11.GL_BACK);
GLStateManager.glLogicOp(GL11.GL_COPY);
GLStateManager.glDisable(GL11.GL_LIGHTING);
GLStateManager.glDeleteLists(list, 1);
}
}
@@ -12,6 +12,7 @@ import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL32;
import java.nio.FloatBuffer;
import java.util.ArrayList;
@@ -516,4 +517,356 @@ class GLSM_PushPop_UnitTest {
verifyState(GL11.GL_VIEWPORT, new int[] { 0, 0, 800, 600 }, "Viewport - Reset");
verifyState(GL11.GL_DEPTH_RANGE, new float[] { 0.0f, 1.0f }, "Depth Range - Reset");
}
@Test
void testPushPopLineBit() {
/*
* GL_LINE_BIT
* GL_LINE_SMOOTH enable bit
* GL_LINE_STIPPLE enable bit
* Line stipple pattern and repeat counter
* Line width
*/
verifyState(GL11.GL_LINE_WIDTH, 1.0f, "Line Width - Initial");
verifyState(GL11.GL_LINE_STIPPLE_PATTERN, (short) 0xFFFF, "Line Stipple Pattern - Initial");
verifyState(GL11.GL_LINE_STIPPLE_REPEAT, 1, "Line Stipple Repeat - Initial");
verifyIsEnabled(GL11.GL_LINE_SMOOTH, false, "Line Smooth - Initial");
verifyIsEnabled(GL11.GL_LINE_STIPPLE, false, "Line Stipple - Initial");
GLStateManager.glPushAttrib(GL11.GL_LINE_BIT);
GLStateManager.glLineWidth(2.5f);
GLStateManager.glLineStipple(3, (short) 0xAAAA);
GLStateManager.glEnable(GL11.GL_LINE_SMOOTH);
GLStateManager.glEnable(GL11.GL_LINE_STIPPLE);
verifyState(GL11.GL_LINE_WIDTH, 2.5f, "Line Width");
verifyState(GL11.GL_LINE_STIPPLE_PATTERN, (short) 0xAAAA, "Line Stipple Pattern");
verifyState(GL11.GL_LINE_STIPPLE_REPEAT, 3, "Line Stipple Repeat");
verifyIsEnabled(GL11.GL_LINE_SMOOTH, true, "Line Smooth");
verifyIsEnabled(GL11.GL_LINE_STIPPLE, true, "Line Stipple");
GLStateManager.glPopAttrib();
verifyState(GL11.GL_LINE_WIDTH, 1.0f, "Line Width - Reset");
verifyState(GL11.GL_LINE_STIPPLE_PATTERN, (short) 0xFFFF, "Line Stipple Pattern - Reset");
verifyState(GL11.GL_LINE_STIPPLE_REPEAT, 1, "Line Stipple Repeat - Reset");
verifyIsEnabled(GL11.GL_LINE_SMOOTH, false, "Line Smooth - Reset");
verifyIsEnabled(GL11.GL_LINE_STIPPLE, false, "Line Stipple - Reset");
}
@Test
void testPushPopPointBit() {
/*
* GL_POINT_BIT
* GL_POINT_SMOOTH enable bit
* Point size
*/
verifyState(GL11.GL_POINT_SIZE, 1.0f, "Point Size - Initial");
verifyIsEnabled(GL11.GL_POINT_SMOOTH, false, "Point Smooth - Initial");
GLStateManager.glPushAttrib(GL11.GL_POINT_BIT);
GLStateManager.glPointSize(5.0f);
GLStateManager.glEnable(GL11.GL_POINT_SMOOTH);
verifyState(GL11.GL_POINT_SIZE, 5.0f, "Point Size");
verifyIsEnabled(GL11.GL_POINT_SMOOTH, true, "Point Smooth");
GLStateManager.glPopAttrib();
verifyState(GL11.GL_POINT_SIZE, 1.0f, "Point Size - Reset");
verifyIsEnabled(GL11.GL_POINT_SMOOTH, false, "Point Smooth - Reset");
}
@Test
void testPushPopPolygonBit() {
/*
* GL_POLYGON_BIT
* GL_CULL_FACE enable bit
* GL_CULL_FACE_MODE setting
* GL_FRONT_FACE setting
* GL_POLYGON_MODE setting
* GL_POLYGON_SMOOTH enable bit
* GL_POLYGON_STIPPLE enable bit
* GL_POLYGON_OFFSET_FILL enable bit
* GL_POLYGON_OFFSET_LINE enable bit
* GL_POLYGON_OFFSET_POINT enable bit
* GL_POLYGON_OFFSET_FACTOR
* GL_POLYGON_OFFSET_UNITS
*/
verifyIsEnabled(GL11.GL_CULL_FACE, false, "Cull Face - Initial");
verifyState(GL11.GL_CULL_FACE_MODE, GL11.GL_BACK, "Cull Face Mode - Initial");
verifyState(GL11.GL_FRONT_FACE, GL11.GL_CCW, "Front Face - Initial");
verifyState(GL11.GL_POLYGON_MODE, new int[] { GL11.GL_FILL, GL11.GL_FILL }, "Polygon Mode - Initial");
verifyIsEnabled(GL11.GL_POLYGON_SMOOTH, false, "Polygon Smooth - Initial");
verifyIsEnabled(GL11.GL_POLYGON_STIPPLE, false, "Polygon Stipple - Initial");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_FILL, false, "Polygon Offset Fill - Initial");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_LINE, false, "Polygon Offset Line - Initial");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_POINT, false, "Polygon Offset Point - Initial");
verifyState(GL11.GL_POLYGON_OFFSET_FACTOR, 0.0f, "Polygon Offset Factor - Initial");
verifyState(GL11.GL_POLYGON_OFFSET_UNITS, 0.0f, "Polygon Offset Units - Initial");
GLStateManager.glPushAttrib(GL11.GL_POLYGON_BIT);
GLStateManager.glEnable(GL11.GL_CULL_FACE);
GLStateManager.glCullFace(GL11.GL_FRONT);
GLStateManager.glFrontFace(GL11.GL_CW);
GLStateManager.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
GLStateManager.glEnable(GL11.GL_POLYGON_SMOOTH);
GLStateManager.glEnable(GL11.GL_POLYGON_STIPPLE);
GLStateManager.glEnable(GL11.GL_POLYGON_OFFSET_FILL);
GLStateManager.glEnable(GL11.GL_POLYGON_OFFSET_LINE);
GLStateManager.glEnable(GL11.GL_POLYGON_OFFSET_POINT);
GLStateManager.glPolygonOffset(1.5f, 2.0f);
verifyIsEnabled(GL11.GL_CULL_FACE, true, "Cull Face");
verifyState(GL11.GL_CULL_FACE_MODE, GL11.GL_FRONT, "Cull Face Mode");
verifyState(GL11.GL_FRONT_FACE, GL11.GL_CW, "Front Face");
verifyState(GL11.GL_POLYGON_MODE, new int[] { GL11.GL_LINE, GL11.GL_LINE }, "Polygon Mode");
verifyIsEnabled(GL11.GL_POLYGON_SMOOTH, true, "Polygon Smooth");
verifyIsEnabled(GL11.GL_POLYGON_STIPPLE, true, "Polygon Stipple");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_FILL, true, "Polygon Offset Fill");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_LINE, true, "Polygon Offset Line");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_POINT, true, "Polygon Offset Point");
verifyState(GL11.GL_POLYGON_OFFSET_FACTOR, 1.5f, "Polygon Offset Factor");
verifyState(GL11.GL_POLYGON_OFFSET_UNITS, 2.0f, "Polygon Offset Units");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_CULL_FACE, false, "Cull Face - Reset");
verifyState(GL11.GL_CULL_FACE_MODE, GL11.GL_BACK, "Cull Face Mode - Reset");
verifyState(GL11.GL_FRONT_FACE, GL11.GL_CCW, "Front Face - Reset");
verifyState(GL11.GL_POLYGON_MODE, new int[] { GL11.GL_FILL, GL11.GL_FILL }, "Polygon Mode - Reset");
verifyIsEnabled(GL11.GL_POLYGON_SMOOTH, false, "Polygon Smooth - Reset");
verifyIsEnabled(GL11.GL_POLYGON_STIPPLE, false, "Polygon Stipple - Reset");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_FILL, false, "Polygon Offset Fill - Reset");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_LINE, false, "Polygon Offset Line - Reset");
verifyIsEnabled(GL11.GL_POLYGON_OFFSET_POINT, false, "Polygon Offset Point - Reset");
verifyState(GL11.GL_POLYGON_OFFSET_FACTOR, 0.0f, "Polygon Offset Factor - Reset");
verifyState(GL11.GL_POLYGON_OFFSET_UNITS, 0.0f, "Polygon Offset Units - Reset");
}
@Test
void testPushPopStencilBufferBit() {
/*
* GL_STENCIL_BUFFER_BIT
* GL_STENCIL_TEST enable bit
* Stencil function and reference value
* Stencil value mask
* Stencil fail, pass, and depth buffer pass actions
* Stencil buffer clear value
* Stencil buffer writemask
*/
// The default stencil mask is clamped to the stencil buffer bit depth
// Query the actual initial mask from GL to get the hardware-specific value
final int initialStencilMask = GL11.glGetInteger(GL11.GL_STENCIL_VALUE_MASK);
final int initialWriteMask = GL11.glGetInteger(GL11.GL_STENCIL_WRITEMASK);
verifyIsEnabled(GL11.GL_STENCIL_TEST, false, "Stencil Test - Initial");
verifyState(GL11.GL_STENCIL_FUNC, GL11.GL_ALWAYS, "Stencil Func - Initial");
verifyState(GL11.GL_STENCIL_REF, 0, "Stencil Ref - Initial");
verifyState(GL11.GL_STENCIL_VALUE_MASK, initialStencilMask, "Stencil Value Mask - Initial");
verifyState(GL11.GL_STENCIL_FAIL, GL11.GL_KEEP, "Stencil Fail - Initial");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_FAIL, GL11.GL_KEEP, "Stencil Pass Depth Fail - Initial");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_PASS, GL11.GL_KEEP, "Stencil Pass Depth Pass - Initial");
verifyState(GL11.GL_STENCIL_CLEAR_VALUE, 0, "Stencil Clear Value - Initial");
verifyState(GL11.GL_STENCIL_WRITEMASK, initialWriteMask, "Stencil Write Mask - Initial");
GLStateManager.glPushAttrib(GL11.GL_STENCIL_BUFFER_BIT);
GLStateManager.glEnable(GL11.GL_STENCIL_TEST);
GLStateManager.glStencilFunc(GL11.GL_LESS, 127, 0x7F);
GLStateManager.glStencilOp(GL11.GL_INCR, GL11.GL_DECR, GL11.GL_REPLACE);
GLStateManager.glStencilMask(0x0F);
GLStateManager.glClearStencil(64);
verifyIsEnabled(GL11.GL_STENCIL_TEST, true, "Stencil Test");
verifyState(GL11.GL_STENCIL_FUNC, GL11.GL_LESS, "Stencil Func");
verifyState(GL11.GL_STENCIL_REF, 127, "Stencil Ref");
verifyState(GL11.GL_STENCIL_VALUE_MASK, 0x7F, "Stencil Value Mask");
verifyState(GL11.GL_STENCIL_FAIL, GL11.GL_INCR, "Stencil Fail");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_FAIL, GL11.GL_DECR, "Stencil Pass Depth Fail");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_PASS, GL11.GL_REPLACE, "Stencil Pass Depth Pass");
verifyState(GL11.GL_STENCIL_CLEAR_VALUE, 64, "Stencil Clear Value");
verifyState(GL11.GL_STENCIL_WRITEMASK, 0x0F, "Stencil Write Mask");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_STENCIL_TEST, false, "Stencil Test - Reset");
verifyState(GL11.GL_STENCIL_FUNC, GL11.GL_ALWAYS, "Stencil Func - Reset");
verifyState(GL11.GL_STENCIL_REF, 0, "Stencil Ref - Reset");
verifyState(GL11.GL_STENCIL_VALUE_MASK, initialStencilMask, "Stencil Value Mask - Reset");
verifyState(GL11.GL_STENCIL_FAIL, GL11.GL_KEEP, "Stencil Fail - Reset");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_FAIL, GL11.GL_KEEP, "Stencil Pass Depth Fail - Reset");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_PASS, GL11.GL_KEEP, "Stencil Pass Depth Pass - Reset");
verifyState(GL11.GL_STENCIL_CLEAR_VALUE, 0, "Stencil Clear Value - Reset");
verifyState(GL11.GL_STENCIL_WRITEMASK, initialWriteMask, "Stencil Write Mask - Reset");
}
@Test
void testPushPopStencilBufferBitSeparate() {
/*
* GL_STENCIL_BUFFER_BIT with GL 2.0 separate front/back state
*/
// The default stencil mask is clamped to the stencil buffer bit depth
// Query the actual initial masks from GL to get the hardware-specific values
final int initialFrontMask = GL11.glGetInteger(GL11.GL_STENCIL_VALUE_MASK);
final int initialFrontWriteMask = GL11.glGetInteger(GL11.GL_STENCIL_WRITEMASK);
final int initialBackMask = GL11.glGetInteger(GL20.GL_STENCIL_BACK_VALUE_MASK);
final int initialBackWriteMask = GL11.glGetInteger(GL20.GL_STENCIL_BACK_WRITEMASK);
// GL 2.0 separate stencil queries
verifyState(GL20.GL_STENCIL_BACK_FUNC, GL11.GL_ALWAYS, "Stencil Back Func - Initial");
verifyState(GL20.GL_STENCIL_BACK_REF, 0, "Stencil Back Ref - Initial");
verifyState(GL20.GL_STENCIL_BACK_VALUE_MASK, initialBackMask, "Stencil Back Value Mask - Initial");
verifyState(GL20.GL_STENCIL_BACK_FAIL, GL11.GL_KEEP, "Stencil Back Fail - Initial");
verifyState(GL20.GL_STENCIL_BACK_PASS_DEPTH_FAIL, GL11.GL_KEEP, "Stencil Back Pass Depth Fail - Initial");
verifyState(GL20.GL_STENCIL_BACK_PASS_DEPTH_PASS, GL11.GL_KEEP, "Stencil Back Pass Depth Pass - Initial");
verifyState(GL20.GL_STENCIL_BACK_WRITEMASK, initialBackWriteMask, "Stencil Back Write Mask - Initial");
GLStateManager.glPushAttrib(GL11.GL_STENCIL_BUFFER_BIT);
// Use separate functions to set different front/back state
GLStateManager.glStencilFuncSeparate(GL11.GL_FRONT, GL11.GL_LESS, 10, 0x0A);
GLStateManager.glStencilFuncSeparate(GL11.GL_BACK, GL11.GL_GREATER, 20, 0x0B);
GLStateManager.glStencilOpSeparate(GL11.GL_FRONT, GL11.GL_INCR, GL11.GL_DECR, GL11.GL_ZERO);
GLStateManager.glStencilOpSeparate(GL11.GL_BACK, GL11.GL_KEEP, GL11.GL_INVERT, GL11.GL_REPLACE);
GLStateManager.glStencilMaskSeparate(GL11.GL_FRONT, 0x11);
GLStateManager.glStencilMaskSeparate(GL11.GL_BACK, 0x22);
// Verify front state
verifyState(GL11.GL_STENCIL_FUNC, GL11.GL_LESS, "Stencil Front Func");
verifyState(GL11.GL_STENCIL_REF, 10, "Stencil Front Ref");
verifyState(GL11.GL_STENCIL_VALUE_MASK, 0x0A, "Stencil Front Value Mask");
verifyState(GL11.GL_STENCIL_FAIL, GL11.GL_INCR, "Stencil Front Fail");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_FAIL, GL11.GL_DECR, "Stencil Front Pass Depth Fail");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_PASS, GL11.GL_ZERO, "Stencil Front Pass Depth Pass");
verifyState(GL11.GL_STENCIL_WRITEMASK, 0x11, "Stencil Front Write Mask");
// Verify back state
verifyState(GL20.GL_STENCIL_BACK_FUNC, GL11.GL_GREATER, "Stencil Back Func");
verifyState(GL20.GL_STENCIL_BACK_REF, 20, "Stencil Back Ref");
verifyState(GL20.GL_STENCIL_BACK_VALUE_MASK, 0x0B, "Stencil Back Value Mask");
verifyState(GL20.GL_STENCIL_BACK_FAIL, GL11.GL_KEEP, "Stencil Back Fail");
verifyState(GL20.GL_STENCIL_BACK_PASS_DEPTH_FAIL, GL11.GL_INVERT, "Stencil Back Pass Depth Fail");
verifyState(GL20.GL_STENCIL_BACK_PASS_DEPTH_PASS, GL11.GL_REPLACE, "Stencil Back Pass Depth Pass");
verifyState(GL20.GL_STENCIL_BACK_WRITEMASK, 0x22, "Stencil Back Write Mask");
GLStateManager.glPopAttrib();
// Verify front state reset
verifyState(GL11.GL_STENCIL_FUNC, GL11.GL_ALWAYS, "Stencil Front Func - Reset");
verifyState(GL11.GL_STENCIL_REF, 0, "Stencil Front Ref - Reset");
verifyState(GL11.GL_STENCIL_VALUE_MASK, initialFrontMask, "Stencil Front Value Mask - Reset");
verifyState(GL11.GL_STENCIL_FAIL, GL11.GL_KEEP, "Stencil Front Fail - Reset");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_FAIL, GL11.GL_KEEP, "Stencil Front Pass Depth Fail - Reset");
verifyState(GL11.GL_STENCIL_PASS_DEPTH_PASS, GL11.GL_KEEP, "Stencil Front Pass Depth Pass - Reset");
verifyState(GL11.GL_STENCIL_WRITEMASK, initialFrontWriteMask, "Stencil Front Write Mask - Reset");
// Verify back state reset
verifyState(GL20.GL_STENCIL_BACK_FUNC, GL11.GL_ALWAYS, "Stencil Back Func - Reset");
verifyState(GL20.GL_STENCIL_BACK_REF, 0, "Stencil Back Ref - Reset");
verifyState(GL20.GL_STENCIL_BACK_VALUE_MASK, initialBackMask, "Stencil Back Value Mask - Reset");
verifyState(GL20.GL_STENCIL_BACK_FAIL, GL11.GL_KEEP, "Stencil Back Fail - Reset");
verifyState(GL20.GL_STENCIL_BACK_PASS_DEPTH_FAIL, GL11.GL_KEEP, "Stencil Back Pass Depth Fail - Reset");
verifyState(GL20.GL_STENCIL_BACK_PASS_DEPTH_PASS, GL11.GL_KEEP, "Stencil Back Pass Depth Pass - Reset");
verifyState(GL20.GL_STENCIL_BACK_WRITEMASK, initialBackWriteMask, "Stencil Back Write Mask - Reset");
}
// ==================== Lazy Copy-on-Write Tests ====================
@Test
void testNestedPushPopEnableBit_ModifyAtBothLevels() {
// Test: modify same state at both depth levels
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - Initial");
verifyIsEnabled(GL11.GL_LIGHTING, false, "Lighting - Initial");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.enableBlend();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After first enable");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.disableBlend();
GLStateManager.enableLighting();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After second push disable");
verifyIsEnabled(GL11.GL_LIGHTING, true, "Lighting - After enable at depth 2");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After first pop (should restore to depth 1 value)");
verifyIsEnabled(GL11.GL_LIGHTING, false, "Lighting - After first pop (should restore to depth 1 value)");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After second pop (should restore to initial)");
verifyIsEnabled(GL11.GL_LIGHTING, false, "Lighting - After second pop (should restore to initial)");
}
@Test
void testNestedPushPopEnableBit_ModifyOnlyAtInnerLevel() {
// Test: modify state only at depth 2, not at depth 1
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - Initial");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
// Don't modify blend at depth 1
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.enableBlend();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After enable at depth 2");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop to depth 1 (should restore)");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop to depth 0 (should still be false)");
}
@Test
void testNestedPushPopEnableBit_ModifyOnlyAtOuterLevel() {
// Test: modify state only at depth 1, not at depth 2
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - Initial");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.enableBlend();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After enable at depth 1");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
// Don't modify blend at depth 2
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - At depth 2 (unchanged)");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After pop to depth 1 (should still be true)");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop to depth 0 (should restore to initial)");
}
@Test
void testMultipleModificationsAtSameDepth() {
// Test: multiple modifications to same state at same depth should only save once
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - Initial");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.enableBlend(); // First modification - should save false
GLStateManager.disableBlend(); // Second modification - should NOT save again
GLStateManager.enableBlend(); // Third modification - should NOT save again
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After multiple toggles");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop (should restore original false)");
}
@Test
void testTripleNestedPushPop() {
// Test: three levels of nesting
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - Initial");
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.enableBlend();
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.disableBlend();
GLStateManager.glPushAttrib(GL11.GL_ENABLE_BIT);
GLStateManager.enableBlend();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - At depth 3");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop to depth 2");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, true, "Blend - After pop to depth 1");
GLStateManager.glPopAttrib();
verifyIsEnabled(GL11.GL_BLEND, false, "Blend - After pop to depth 0");
}
}
@@ -0,0 +1,267 @@
package com.gtnewhorizons.angelica.glsm.recording;
import com.gtnewhorizons.angelica.AngelicaExtension;
import com.gtnewhorizons.angelica.glsm.DisplayListManager;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import com.gtnewhorizons.angelica.glsm.recording.commands.DrawCommand;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.opengl.GL11;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests for display list recording data structures.
*/
@ExtendWith(AngelicaExtension.class)
class RecordingDataStructuresTest {
private int testList = -1;
@AfterEach
void cleanup() {
if (testList > 0) {
GLStateManager.glDeleteLists(testList, 1);
testList = -1;
}
}
@Test
void testDrawCommandCreation() {
DrawCommand cmd = new DrawCommand(0, 4, GL11.GL_QUADS);
assertEquals(0, cmd.firstVertex());
assertEquals(4, cmd.vertexCount());
assertEquals(GL11.GL_QUADS, cmd.primitiveType());
}
@Test
void testRecordedGeometryCreation() {
ByteBuffer buffer = ByteBuffer.allocate(16);
DrawCommand cmd1 = new DrawCommand(0, 4, GL11.GL_QUADS);
DrawCommand cmd2 = new DrawCommand(4, 3, GL11.GL_TRIANGLES);
List<DrawCommand> commands = Arrays.asList(cmd1, cmd2);
RecordedGeometry geom = new RecordedGeometry(buffer, null, commands, 7);
assertEquals(buffer, geom.buffer());
assertEquals(commands, geom.drawCommands());
assertEquals(7, geom.totalVertexCount());
assertEquals(16, geom.getByteSize());
}
@Test
void testCompiledDisplayListCreation() {
// Create a display list with PushMatrix, Scale, PopMatrix
testList = GL11.glGenLists(1);
GLStateManager.glNewList(testList, GL11.GL_COMPILE);
GLStateManager.glPushMatrix();
GLStateManager.glScalef(2.0f, 2.0f, 2.0f);
GLStateManager.glPopMatrix();
GLStateManager.glEndList();
// Verify the compiled display list
CompiledDisplayList compiled = DisplayListManager.getDisplayList(testList);
assertNotNull(compiled, "Display list should be compiled");
assertNotNull(compiled.getCommandBuffer(), "Command buffer should not be null");
// Get command counts to verify structure
Map<Integer, Integer> counts = compiled.getCommandCounts();
// Should have PushMatrix and PopMatrix
assertEquals(1, counts.getOrDefault(GLCommand.PUSH_MATRIX, 0), "Should have 1 PushMatrix");
assertEquals(1, counts.getOrDefault(GLCommand.POP_MATRIX, 0), "Should have 1 PopMatrix");
// Scale should be collapsed into a MultMatrix (optimizer collapses transforms)
// Or if in debug mode, might have individual commands - just check we have some transforms
int transformCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0)
+ counts.getOrDefault(GLCommand.SCALE, 0);
assertTrue(transformCount >= 0, "Should have transform commands");
}
@Test
void testImmediateModeRecorderCapturesQuads() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// Record a single quad using immediate mode
recorder.begin(GL11.GL_QUADS);
recorder.setTexCoord(0, 0);
recorder.vertex(0, 0, 0);
recorder.setTexCoord(1, 0);
recorder.vertex(1, 0, 0);
recorder.setTexCoord(1, 1);
recorder.vertex(1, 1, 0);
recorder.setTexCoord(0, 1);
recorder.vertex(0, 1, 0);
// end() now returns quads immediately (for correct command interleaving)
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertEquals(1, result.quads().size());
assertTrue(result.flags().hasTexture); // We called setTexCoord
// After end(), recorder should be empty (quads were returned immediately)
assertFalse(recorder.hasGeometry());
assertNull(recorder.getQuadsAndClear());
}
@Test
void testImmediateModeRecorderCapturesTriangles() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// Record 2 triangles (6 vertices, should become 2 degenerate quads)
recorder.begin(GL11.GL_TRIANGLES);
recorder.vertex(0, 0, 0);
recorder.vertex(1, 0, 0);
recorder.vertex(0.5f, 1, 0);
recorder.vertex(2, 0, 0);
recorder.vertex(3, 0, 0);
recorder.vertex(2.5f, 1, 0);
// end() now returns quads immediately
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertEquals(2, result.quads().size()); // 2 triangles = 2 degenerate quads
// Recorder should be empty after end()
assertFalse(recorder.hasGeometry());
}
@Test
void testImmediateModeRecorderCapturesTriangleFan() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// Record a triangle fan (center + 3 outer vertices = 2 triangles = 2 quads)
recorder.begin(GL11.GL_TRIANGLE_FAN);
recorder.vertex(0, 0, 0); // Center
recorder.vertex(1, 0, 0);
recorder.vertex(1, 1, 0);
recorder.vertex(0, 1, 0);
// end() now returns quads immediately
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertEquals(2, result.quads().size()); // 2 triangles from fan
// Recorder should be empty after end()
assertFalse(recorder.hasGeometry());
}
@Test
void testImmediateModeRecorderEmptyAfterReset() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
recorder.begin(GL11.GL_QUADS);
recorder.vertex(0, 0, 0);
recorder.vertex(1, 0, 0);
recorder.vertex(1, 1, 0);
recorder.vertex(0, 1, 0);
// end() returns quads immediately, so recorder is empty after
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertEquals(1, result.quads().size());
// Already empty after end(), but reset() should still work
assertFalse(recorder.hasGeometry());
recorder.reset();
assertFalse(recorder.hasGeometry());
assertNull(recorder.getQuadsAndClear());
}
@Test
void testImmediateModeRecorderNormalAndColor() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
recorder.setNormal(0, 0, 1); // Normal pointing +Z
recorder.begin(GL11.GL_QUADS);
recorder.vertex(0, 0, 0);
recorder.vertex(1, 0, 0);
recorder.vertex(1, 1, 0);
recorder.vertex(0, 1, 0);
// end() now returns quads immediately
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertTrue(result.flags().hasNormals);
}
@Test
void testImmediateModeRecorderThrowsOnInvalidState() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// glEnd without glBegin
assertThrows(IllegalStateException.class, () -> recorder.end());
// glVertex without glBegin
assertThrows(IllegalStateException.class, () -> recorder.vertex(0, 0, 0));
// Nested glBegin
recorder.begin(GL11.GL_QUADS);
assertThrows(IllegalStateException.class, () -> recorder.begin(GL11.GL_QUADS));
recorder.end();
// getQuadsAndClear while in primitive
recorder.begin(GL11.GL_QUADS);
assertThrows(IllegalStateException.class, () -> recorder.getQuadsAndClear());
}
@Test
void testImmediateModeRecorderCapturesLines() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// Record 2 lines (4 vertices)
recorder.begin(GL11.GL_LINES);
recorder.vertex(0, 0, 0);
recorder.vertex(1, 0, 0);
recorder.vertex(0, 1, 0);
recorder.vertex(1, 1, 0);
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertTrue(result.quads().isEmpty(), "Lines should not produce quads");
assertEquals(4, result.lines().size(), "4 vertices for 2 lines");
}
@Test
void testImmediateModeRecorderCapturesLineStrip() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// Record a line strip: 4 vertices = 3 line segments = 6 line vertices
recorder.begin(GL11.GL_LINE_STRIP);
recorder.vertex(0, 0, 0);
recorder.vertex(1, 0, 0);
recorder.vertex(1, 1, 0);
recorder.vertex(0, 1, 0);
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertTrue(result.quads().isEmpty(), "Line strips should not produce quads");
assertEquals(6, result.lines().size(), "4 vertices in strip = 3 segments = 6 line vertices");
}
@Test
void testImmediateModeRecorderCapturesLineLoop() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
// Record a line loop: 4 vertices = 4 line segments (closes back to start) = 8 line vertices
recorder.begin(GL11.GL_LINE_LOOP);
recorder.vertex(0, 0, 0);
recorder.vertex(1, 0, 0);
recorder.vertex(1, 1, 0);
recorder.vertex(0, 1, 0);
ImmediateModeRecorder.Result result = recorder.end();
assertNotNull(result);
assertTrue(result.quads().isEmpty(), "Line loops should not produce quads");
assertEquals(8, result.lines().size(), "4 vertices in loop = 4 segments (including closing) = 8 line vertices");
}
}
@@ -70,6 +70,14 @@ public class GLSMUtil {
);
}
public static void verifyState(int glCap, short expected, String message) {
// For short state (e.g., line stipple pattern), GL returns it as int, we compare as short
assertAll(message,
() -> assertEquals(expected, (short) GL11.glGetInteger(glCap), "GL State Mismatch"),
() -> assertEquals(expected, (short) GLStateManager.glGetInteger(glCap), "GLSM State Mismatch")
);
}
public static void verifyState(int glCap, float expected) {
verifyState(glCap, expected, "Float State Mismatch");
}
@@ -253,6 +261,25 @@ public class GLSMUtil {
// Reset draw buffer
GLStateManager.glDrawBuffer(GLStateManager.DEFAULT_DRAW_BUFFER);
// Reset line state
GLStateManager.glLineWidth(1.0f);
GLStateManager.glLineStipple(1, (short) 0xFFFF);
// Reset point state
GLStateManager.glPointSize(1.0f);
// Reset polygon state
GLStateManager.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
GLStateManager.glPolygonOffset(0.0f, 0.0f);
GLStateManager.glCullFace(GL11.GL_BACK);
GLStateManager.glFrontFace(GL11.GL_CCW);
// Reset stencil state
GLStateManager.glStencilFunc(GL11.GL_ALWAYS, 0, 0xFFFFFFFF);
GLStateManager.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP);
GLStateManager.glStencilMask(0xFFFFFFFF);
GLStateManager.glClearStencil(0);
}
@@ -0,0 +1,62 @@
package com.gtnewhorizons.angelica.utils;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.nio.FloatBuffer;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAllocFloat;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memFree;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests for BufferUtil.
*/
@ExtendWith(AngelicaExtension.class)
class BufferUtilTest {
@Test
void testCopyDirectBufferCopiesData() {
FloatBuffer src = memAllocFloat(4);
src.put(1.0f).put(2.0f).put(3.0f).put(4.0f);
src.flip();
FloatBuffer dst = BufferUtil.copyDirectBuffer(src);
assertTrue(dst.isDirect(), "Returned buffer should be direct");
try {
assertEquals(4, dst.remaining());
assertEquals(1.0f, dst.get(0), 0.0001f);
assertEquals(2.0f, dst.get(1), 0.0001f);
assertEquals(3.0f, dst.get(2), 0.0001f);
assertEquals(4.0f, dst.get(3), 0.0001f);
} finally {
memFree(src);
memFree(dst);
}
}
@Test
void testCopyDirectBufferPreservesSourcePosition() {
FloatBuffer src = memAllocFloat(4);
src.put(1.0f).put(2.0f).put(3.0f).put(4.0f);
src.flip();
src.position(1); // Start at second element
int posBefore = src.position();
FloatBuffer dst = BufferUtil.copyDirectBuffer(src);
try {
assertEquals(posBefore, src.position(), "Source position should be preserved");
assertEquals(3, dst.remaining(), "Should copy remaining elements only");
assertEquals(2.0f, dst.get(0), 0.0001f);
assertEquals(3.0f, dst.get(1), 0.0001f);
assertEquals(4.0f, dst.get(2), 0.0001f);
} finally {
memFree(src);
memFree(dst);
}
}
}
@@ -0,0 +1,15 @@
package net.minecraft.launchwrapper;
import java.util.HashMap;
import java.util.Map;
/**
* Stub for tests - provides blackboard without loading real launchwrapper.
*/
public class Launch {
public static Map<String, Object> blackboard = new HashMap<>();
static {
blackboard.put("lwjgl3ify:rfb-booted", Boolean.FALSE);
}
}