Display List Refactor (#1360)

This commit is contained in:
sisyphussy authored and GitHub committed 2026-02-04 10:39:20 -08:00
1 parent 1dd98978ad
commit e17f60308d
50 files changed
+1139 -4089

No files matched your search

@@ -1,18 +1,11 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.line.ModelLine;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.primitive.ModelPrimitiveView;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuad;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadViewMutable;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.tri.ModelTriangle;
import com.gtnewhorizons.angelica.glsm.recording.AccumulatedDraw;
import com.gtnewhorizons.angelica.glsm.recording.AccumulatedPrimitiveDraw;
import org.joml.Matrix4f;
import com.gtnewhorizon.gtnhlib.client.renderer.DirectTessellator;
import com.gtnewhorizons.angelica.glsm.recording.CompiledDisplayList;
import com.gtnewhorizons.angelica.glsm.recording.ImmediateModeRecorder;
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Helper class for building test data for display list batching tests.
@@ -22,398 +15,51 @@ 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;
public static void createSimpleDraw() {
final DirectTessellator recorder = ImmediateModeRecorder.getInternalTessellator();
// Record a single quad using immediate mode
recorder.startDrawing(GL11.GL_QUADS);
recorder.addVertex(0, 0, 0);
recorder.addVertex(1, 0, 0);
recorder.addVertex(1, 1, 0);
recorder.addVertex(0, 1, 0);
DisplayListManager.addImmediateModeDraw(ImmediateModeRecorder.end());
}
/**
* 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}
});
public static void createTextureDraw() {
final DirectTessellator recorder = ImmediateModeRecorder.getInternalTessellator();
// Record a single quad using immediate mode
recorder.startDrawing(GL11.GL_QUADS);
recorder.setTextureUV(0, 0);
recorder.addVertex(0, 0, 0);
recorder.setTextureUV(1, 0);
recorder.addVertex(1, 0, 0);
recorder.setTextureUV(1, 1);
recorder.addVertex(1, 1, 0);
recorder.setTextureUV(0, 1);
recorder.addVertex(0, 1, 0);
DisplayListManager.addImmediateModeDraw(ImmediateModeRecorder.end());
}
/**
* 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}
});
public static void createColorDraw() {
final DirectTessellator recorder = ImmediateModeRecorder.getInternalTessellator();
// Record a single quad using immediate mode
recorder.startDrawing(GL11.GL_QUADS);
recorder.setColorRGBA(255, 255, 255, 255);
recorder.addVertex(0, 0, 0);
recorder.addVertex(1, 0, 0);
recorder.addVertex(1, 1, 0);
recorder.addVertex(0, 1, 0);
DisplayListManager.addImmediateModeDraw(ImmediateModeRecorder.end());
}
/**
* 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, 0);
}
public static AccumulatedDraw createDraw(
List<ModelQuadViewMutable> quads,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex,
int matrixGeneration) {
return new AccumulatedDraw(quads, transform, flags, commandIndex, matrixGeneration, 0);
}
public static AccumulatedDraw createDraw(
List<ModelQuadViewMutable> quads,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex,
int matrixGeneration,
int stateGeneration) {
return new AccumulatedDraw(quads, transform, flags, commandIndex, matrixGeneration, stateGeneration);
}
/**
* 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, int matrixGeneration) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
transform,
positionOnly(),
commandIndex,
matrixGeneration
);
}
/**
* Create an AccumulatedDraw with specified transform and stateGeneration.
*/
public static AccumulatedDraw createDrawWithTransformAndBatch(Matrix4f transform, int commandIndex, int matrixGeneration, int stateGeneration) {
return createDraw(
Collections.singletonList(createSimpleQuad()),
transform,
positionOnly(),
commandIndex,
matrixGeneration,
stateGeneration
);
}
/**
* 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);
}
// ==================== Primitive Builders (Lines, Triangles) ====================
/**
* Create a ModelLine from two vertex positions.
*/
public static ModelLine createLine(float x1, float y1, float z1, float x2, float y2, float z2) {
ModelLine line = new ModelLine();
line.setX(0, x1);
line.setY(0, y1);
line.setZ(0, z1);
line.setColor(0, 0xFFFFFFFF);
line.setTexU(0, 0.0f);
line.setTexV(0, 0.0f);
line.setLight(0, 0);
line.setForgeNormal(0, 0);
line.setX(1, x2);
line.setY(1, y2);
line.setZ(1, z2);
line.setColor(1, 0xFFFFFFFF);
line.setTexU(1, 1.0f);
line.setTexV(1, 1.0f);
line.setLight(1, 0);
line.setForgeNormal(1, 0);
return line;
}
/**
* Create a simple unit line from (0,0,0) to (1,0,0).
*/
public static ModelLine createSimpleLine() {
return createLine(0, 0, 0, 1, 0, 0);
}
/**
* Create multiple simple lines.
*/
public static List<ModelLine> createLines(int count) {
List<ModelLine> lines = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
lines.add(createLine(i, 0, 0, i + 1, 0, 0));
}
return lines;
}
/**
* Create a ModelTriangle from three vertex positions.
*/
public static ModelTriangle createTriangle(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3) {
ModelTriangle tri = new ModelTriangle();
tri.setX(0, x1);
tri.setY(0, y1);
tri.setZ(0, z1);
tri.setColor(0, 0xFFFFFFFF);
tri.setTexU(0, 0.0f);
tri.setTexV(0, 0.0f);
tri.setLight(0, 0);
tri.setForgeNormal(0, 0);
tri.setX(1, x2);
tri.setY(1, y2);
tri.setZ(1, z2);
tri.setColor(1, 0xFFFFFFFF);
tri.setTexU(1, 1.0f);
tri.setTexV(1, 0.0f);
tri.setLight(1, 0);
tri.setForgeNormal(1, 0);
tri.setX(2, x3);
tri.setY(2, y3);
tri.setZ(2, z3);
tri.setColor(2, 0xFFFFFFFF);
tri.setTexU(2, 0.5f);
tri.setTexV(2, 1.0f);
tri.setLight(2, 0);
tri.setForgeNormal(2, 0);
return tri;
}
/**
* Create a simple unit triangle at origin.
*/
public static ModelTriangle createSimpleTriangle() {
return createTriangle(0, 0, 0, 1, 0, 0, 0.5f, 1, 0);
}
/**
* Create multiple simple triangles.
*/
public static List<ModelTriangle> createTriangles(int count) {
List<ModelTriangle> triangles = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
triangles.add(createTriangle(i, 0, 0, i + 1, 0, 0, i + 0.5f, 1, 0));
}
return triangles;
}
/**
* Create a mixed list of primitives (lines and triangles).
*/
public static List<ModelPrimitiveView> createMixedPrimitives(int lineCount, int triangleCount) {
List<ModelPrimitiveView> primitives = new ArrayList<>(lineCount + triangleCount);
for (int i = 0; i < lineCount; i++) {
primitives.add(createLine(i, 0, 0, i + 1, 0, 0));
}
for (int i = 0; i < triangleCount; i++) {
primitives.add(createTriangle(i, 1, 0, i + 1, 1, 0, i + 0.5f, 2, 0));
}
return primitives;
}
// ==================== AccumulatedPrimitiveDraw Builders ====================
/**
* Create an AccumulatedPrimitiveDraw with specified parameters.
*/
public static AccumulatedPrimitiveDraw createPrimitiveDraw(
List<ModelPrimitiveView> primitives,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex) {
return new AccumulatedPrimitiveDraw(primitives, transform, flags, commandIndex, 0);
}
public static AccumulatedPrimitiveDraw createPrimitiveDraw(
List<ModelPrimitiveView> primitives,
Matrix4f transform,
CapturingTessellator.Flags flags,
int commandIndex,
int matrixGeneration) {
return new AccumulatedPrimitiveDraw(primitives, transform, flags, commandIndex, matrixGeneration);
}
/**
* Create an AccumulatedPrimitiveDraw with lines only.
*/
public static AccumulatedPrimitiveDraw createLinesDraw(int lineCount, int commandIndex) {
List<ModelPrimitiveView> primitives = new ArrayList<>(lineCount);
for (ModelLine line : createLines(lineCount)) {
primitives.add(line);
}
return createPrimitiveDraw(primitives, identity(), positionOnly(), commandIndex);
}
/**
* Create an AccumulatedPrimitiveDraw with triangles only.
*/
public static AccumulatedPrimitiveDraw createTrianglesDraw(int triangleCount, int commandIndex) {
List<ModelPrimitiveView> primitives = new ArrayList<>(triangleCount);
for (ModelTriangle tri : createTriangles(triangleCount)) {
primitives.add(tri);
}
return createPrimitiveDraw(primitives, identity(), positionOnly(), commandIndex);
}
/**
* Create an AccumulatedPrimitiveDraw with mixed lines and triangles.
*/
public static AccumulatedPrimitiveDraw createMixedPrimitiveDraw(int lineCount, int triangleCount, int commandIndex) {
List<ModelPrimitiveView> primitives = createMixedPrimitives(lineCount, triangleCount);
return createPrimitiveDraw(primitives, identity(), positionOnly(), commandIndex);
}
/**
* Create an AccumulatedPrimitiveDraw with specified transform.
*/
public static AccumulatedPrimitiveDraw createPrimitiveDrawWithTransform(
List<ModelPrimitiveView> primitives, Matrix4f transform, int commandIndex) {
return createPrimitiveDraw(primitives, transform, positionOnly(), commandIndex);
public static CompiledDisplayList createDisplayList(Runnable runnable) {
int list = GL11.glGenLists(1);
GLStateManager.glNewList(list, GL11.GL_COMPILE);
runnable.run();
GLStateManager.glEndList();
CompiledDisplayList compiled = DisplayListManager.getDisplayList(list);
assertNotNull(compiled, "Display list should be compiled");
return compiled;
}
}
@@ -1,19 +1,15 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator;
import com.gtnewhorizon.gtnhlib.client.renderer.vbo.VertexBuffer;
import com.gtnewhorizon.gtnhlib.client.renderer.vbo.IVertexBuffer;
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 org.joml.Matrix4f;
import com.gtnewhorizons.angelica.glsm.recording.CompiledDisplayList;
import com.gtnewhorizons.angelica.glsm.recording.DisplayListVBO;
import com.gtnewhorizons.angelica.glsm.recording.GLCommand;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.gtnewhorizons.angelica.glsm.DisplayListTestHelper.*;
@@ -28,167 +24,99 @@ import static org.junit.jupiter.api.Assertions.*;
* <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++;
private static int getVBOCount(CompiledDisplayList compiledDisplayList) {
Set<IVertexBuffer> vaos = new HashSet<>();
for (DisplayListVBO.SubVBO vbo : compiledDisplayList.getOwnedVbos().getVBOs()) {
vaos.add(vbo.getVAO().getVBO());
}
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 rangeCmd) {
vbos.add(rangeCmd.sharedVbo());
}
}
return vbos;
return vaos.size();
}
// ==================== 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));
CompiledDisplayList compiled = createDisplayList(() -> {
createSimpleDraw();
createSimpleDraw();
createSimpleDraw();
});
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,
assertEquals(1, getVBOCount(compiled),
"3 draws with same format should produce 1 shared VBO");
// Verify: 1 DrawRangeCmd covering all geometry
long drawCount = countDrawRangeCmds(optimized);
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
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();
CompiledDisplayList compiled = createDisplayList(() -> {
createSimpleDraw();
createTextureDraw();
createColorDraw();
});
// Verify: 3 VBOs (one per format)
assertEquals(3, result.ownedVbos().length,
assertEquals(3, getVBOCount(compiled),
"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");
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
int drawRestoreCount = counts.getOrDefault(GLCommand.DRAW_RANGE_RESTORE, 0);
assertEquals(1, drawCount, "3 draws with different formats should produce 3 DrawRangeCmds");
assertEquals(2, drawRestoreCount, "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, 0)); // Identity transform, gen=0
draws.add(createDrawWithTransform(new Matrix4f().translate(1.0f, 0.0f, 0.0f), 1, 1)); // T(1), gen=1
draws.add(createDrawWithTransform(new Matrix4f().translate(3.0f, 0.0f, 0.0f), 2, 2)); // T(3), gen=2
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
CompiledDisplayList compiled = createDisplayList(() -> {
createSimpleDraw();
GLStateManager.glTranslatef(1, 0, 0);
createSimpleDraw();
GLStateManager.glTranslatef(2, 0, 0);
createSimpleDraw();
});
// Verify: 1 shared VBO (same format)
assertEquals(1, result.ownedVbos().length, "Draws with different transforms but same format should share 1 VBO");
assertEquals(1, getVBOCount(compiled), "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);
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
int multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
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 (2 for T(1) and T(3))
long matrixCount = countMultMatrixCmds(optimized);
assertEquals(2, matrixCount, "Should have MultMatrix commands for each unique transform");
assertEquals(2, multMatrixCount, "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();
CompiledDisplayList compiled = createDisplayList(() -> {
for (int i = 0; i < 100; i++) {
createSimpleDraw();
}
});
// Verify: 1 shared VBO
assertEquals(1, result.ownedVbos().length, "100 draws with same format should produce 1 shared VBO");
assertEquals(1, getVBOCount(compiled), "100 draws with same format should produce 1 shared VBO");
// Verify: 1 DrawRangeCmd (all same transform)
long drawCount = countDrawRangeCmds(optimized);
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
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 testCancellingTransformsProduceSeparateDrawRangeCmds() {
@@ -199,128 +127,93 @@ class GLSM_DisplayList_Batching_Test {
// With format-based batching: 1 shared VBO, 3 DrawRangeCmds
// The key point: draws 1 and 3 have the same transform (identity) but draw 2
// has a different transform, so they can't all be merged.
List<AccumulatedDraw> draws = new ArrayList<>();
// Draw 1: captured at identity, gen=0
draws.add(createDrawWithTransform(new Matrix4f(), 0, 0));
// Draw 2: captured at translate(0.5, 0, 0), gen=1 (transform changed)
draws.add(createDrawWithTransform(new Matrix4f().translate(0.5f, 0.0f, 0.0f), 1, 1));
// Draw 3: captured back at identity, gen=2 (transform changed again)
draws.add(createDrawWithTransform(new Matrix4f(), 2, 2));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
CompiledDisplayList compiled = createDisplayList(() -> {
createSimpleDraw();
GLStateManager.glTranslatef(0.5f, 0, 0);
createSimpleDraw();
GLStateManager.glTranslatef(-0.5f, 0, 0);
createSimpleDraw();
});
// Verify: 1 shared VBO (same format)
assertEquals(1, result.ownedVbos().length, "Draws with same format should share 1 VBO regardless of transforms");
assertEquals(1, getVBOCount(compiled), "Draws with same format should share 1 VBO regardless of transforms");
// Verify: 3 DrawRangeCmds (different transforms require separate draw commands)
long drawCount = countDrawRangeCmds(optimized);
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
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<>();
// All same transform → same generation → will batch into single DrawRangeCmd
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0, 1));
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0, 1));
draws.add(createDrawWithTransform(new Matrix4f(commonTransform), 0, 1));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
CompiledDisplayList compiled = createDisplayList(() -> {
GLStateManager.glTranslatef(1.0f, 2.0f, 3.0f);
createSimpleDraw();
createSimpleDraw();
createSimpleDraw();
});
// Verify: 1 shared VBO
assertEquals(1, result.ownedVbos().length,
"Draws with same format should share 1 VBO");
assertEquals(1, getVBOCount(compiled), "Draws with same format should share 1 VBO");
// Verify: 1 DrawRangeCmd (same format AND same transform)
long drawCount = countDrawRangeCmds(optimized);
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
int multMatrixCount = counts.getOrDefault(GLCommand.MULT_MATRIX, 0);
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");
assertEquals(1, multMatrixCount, "Should have exactly 1 MultMatrix for the common transform");
}
/**
* Test that draws with same transform but different stateGeneration do NOT merge.
* This simulates state commands (draw barriers) between draws.
*
* Scenario: Draw1, glColor(red), Draw2, glBindTexture, Draw3
* <br>
* Scenario: Draw1, glColor(red), Draw2, glBindTexture, Draw3, Draw4
* All draws have same transform (gen=1) but different batches (0, 1, 2).
* Expected: 3 separate DrawRangeCmds (no merging across draw barriers).
*
* Expected: 3 separate DrawRangeCmds (only draw 3 & 4 should get merged).
* <br>
* This test will FAIL until FormatBuffer is fixed to check stateGeneration.
*/
@Test
void testDrawsWithDifferentBatchIdDoNotMerge() {
// Setup: 3 draws with same transform generation but different batch IDs
// This simulates state commands (draw barriers) between draws
List<AccumulatedDraw> draws = new ArrayList<>();
draws.add(createDrawWithTransformAndBatch(new Matrix4f(), 0, 1, 0)); // batch 0
draws.add(createDrawWithTransformAndBatch(new Matrix4f(), 1, 1, 1)); // batch 1 (state cmd between)
draws.add(createDrawWithTransformAndBatch(new Matrix4f(), 2, 1, 2)); // batch 2 (state cmd between)
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, draws, null, 1);
DisplayListCommand[] optimized = result.commands();
CompiledDisplayList compiled = createDisplayList(() -> {
createSimpleDraw();
DisplayListManager.drawBarrier();
createSimpleDraw();
DisplayListManager.drawBarrier();
createSimpleDraw();
createSimpleDraw();
});
// Verify: 1 shared VBO (same format)
assertEquals(1, result.ownedVbos().length,
"Draws with same format should share 1 VBO");
assertEquals(1, getVBOCount(compiled), "Draws with same format should share 1 VBO");
// Verify: 3 DrawRangeCmds (NOT merged due to different batch IDs)
// BUG: Currently this will produce 1 DrawRangeCmd because FormatBuffer
// only checks matrixGeneration, not stateGeneration
long drawCount = countDrawRangeCmds(optimized);
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
assertEquals(3, drawCount,
"Draws with same transform but different batch IDs (draw barriers between) must NOT merge");
}
@Test
void testInterleavedFormatsProduceSeparateVBOs() {
// Setup: A A B A A - should produce 2 VBOs (one for A, one for B)
// 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();
CompiledDisplayList compiled = createDisplayList(() -> {
createSimpleDraw(); // A
createSimpleDraw(); // A
createTextureDraw(); // B
createSimpleDraw(); // A
createSimpleDraw(); // A
});
Int2IntMap counts = compiled.getCommandCounts();
int drawCount = counts.getOrDefault(GLCommand.DRAW_RANGE, 0);
int drawRestoreCount = counts.getOrDefault(GLCommand.DRAW_RANGE_RESTORE, 0);
// 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)");
assertEquals(2, getVBOCount(compiled), "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)");
assertEquals(1, drawRestoreCount, "A A B A A pattern with same transform should produce 2 DrawRangeCmds (A's merged, B separate)");
}
}
@@ -1,535 +0,0 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.line.ModelLine;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.primitive.ModelPrimitiveView;
import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.tri.ModelTriangle;
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.AccumulatedPrimitiveDraw;
import com.gtnewhorizons.angelica.glsm.recording.commands.DisplayListCommand;
import com.gtnewhorizons.angelica.glsm.recording.commands.DrawRangeCmd;
import org.joml.Matrix4f;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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 primitive (lines, triangles) handling.
*
* <p>Tests cover:
* <ul>
* <li>AccumulatedPrimitiveDraw deep copying behavior</li>
* <li>TessellatorPrimitiveBuffer accumulation and range merging</li>
* <li>Integration with buildOptimizedDisplayList for primitive VBOs</li>
* <li>Mixed quad + primitive display lists</li>
* </ul>
*/
@ExtendWith(AngelicaExtension.class)
class GLSM_DisplayList_Primitive_Test {
// ==================== Helper Methods ====================
private long countDrawRangeCmds(DisplayListCommand[] commands) {
long count = 0;
for (DisplayListCommand cmd : commands) {
if (cmd instanceof DrawRangeCmd) count++;
}
return count;
}
private Set<VertexBuffer> getUniqueVBOs(DisplayListCommand[] commands) {
Set<VertexBuffer> vbos = new HashSet<>();
for (DisplayListCommand cmd : commands) {
if (cmd instanceof DrawRangeCmd rangeCmd) {
vbos.add(rangeCmd.sharedVbo());
}
}
return vbos;
}
// ==================== AccumulatedPrimitiveDraw Tests ====================
@Test
void testAccumulatedPrimitiveDrawDeepCopiesLines() {
// Setup: Create a line and capture its original values
ModelLine originalLine = createSimpleLine();
float originalX0 = originalLine.getX(0);
float originalY0 = originalLine.getY(0);
List<ModelPrimitiveView> primitives = new ArrayList<>();
primitives.add(originalLine);
// Execute: Create AccumulatedPrimitiveDraw (should deep copy)
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, identity(), positionOnly(), 0, 0);
// Modify original
originalLine.setX(0, 999.0f);
originalLine.setY(0, 888.0f);
// Verify: Copied primitive should be unchanged
ModelPrimitiveView copied = draw.primitives.get(0);
assertTrue(copied instanceof ModelLine, "Copied primitive should be a ModelLine");
ModelLine copiedLine = (ModelLine) copied;
assertEquals(originalX0, copiedLine.getX(0), 0.001f, "Deep copy should preserve original X value");
assertEquals(originalY0, copiedLine.getY(0), 0.001f, "Deep copy should preserve original Y value");
}
@Test
void testAccumulatedPrimitiveDrawDeepCopiesTriangles() {
// Setup: Create a triangle and capture its original values
ModelTriangle originalTri = createSimpleTriangle();
float originalX0 = originalTri.getX(0);
float originalX1 = originalTri.getX(1);
float originalX2 = originalTri.getX(2);
List<ModelPrimitiveView> primitives = new ArrayList<>();
primitives.add(originalTri);
// Execute: Create AccumulatedPrimitiveDraw (should deep copy)
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, identity(), positionOnly(), 0, 0);
// Modify original
originalTri.setX(0, 999.0f);
originalTri.setX(1, 888.0f);
originalTri.setX(2, 777.0f);
// Verify: Copied primitive should be unchanged
ModelPrimitiveView copied = draw.primitives.get(0);
assertTrue(copied instanceof ModelTriangle, "Copied primitive should be a ModelTriangle");
ModelTriangle copiedTri = (ModelTriangle) copied;
assertEquals(originalX0, copiedTri.getX(0), 0.001f, "Deep copy should preserve original X0 value");
assertEquals(originalX1, copiedTri.getX(1), 0.001f, "Deep copy should preserve original X1 value");
assertEquals(originalX2, copiedTri.getX(2), 0.001f, "Deep copy should preserve original X2 value");
}
@Test
void testAccumulatedPrimitiveDrawDeepCopiesMixedPrimitives() {
// Setup: Create mixed primitives
List<ModelPrimitiveView> primitives = createMixedPrimitives(2, 2);
// Execute
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, identity(), positionOnly(), 0, 0);
// Verify: All primitives copied correctly
assertEquals(4, draw.primitives.size(), "Should have 4 primitives (2 lines + 2 triangles)");
int lineCount = 0;
int triCount = 0;
for (ModelPrimitiveView prim : draw.primitives) {
if (prim instanceof ModelLine) lineCount++;
else if (prim instanceof ModelTriangle) triCount++;
}
assertEquals(2, lineCount, "Should have 2 lines");
assertEquals(2, triCount, "Should have 2 triangles");
}
@Test
void testAccumulatedPrimitiveDrawDeepCopiesTransform() {
// Setup
Matrix4f originalTransform = translate(1.0f, 2.0f, 3.0f);
List<ModelPrimitiveView> primitives = new ArrayList<>();
primitives.add(createSimpleLine());
// Execute
AccumulatedPrimitiveDraw draw = new AccumulatedPrimitiveDraw(
primitives, originalTransform, positionOnly(), 0, 0);
// Modify original transform
originalTransform.identity();
// Verify: Stored transform should be unchanged (not identity)
assertFalse(draw.transform.equals(new Matrix4f()), "Transform should be deep copied, not identity");
assertEquals(1.0f, draw.transform.m30(), 0.001f, "Transform X translation should be preserved");
assertEquals(2.0f, draw.transform.m31(), 0.001f, "Transform Y translation should be preserved");
assertEquals(3.0f, draw.transform.m32(), 0.001f, "Transform Z translation should be preserved");
}
// ==================== TessellatorPrimitiveBuffer Tests ====================
@Test
void testPrimitiveBufferSeparatesLinesByTransform() {
// Setup: Create primitive buffer and add draws with different transforms
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
AccumulatedPrimitiveDraw draw1 = createLinesDraw(2, 0); // 2 lines at identity
AccumulatedPrimitiveDraw draw2 = createPrimitiveDrawWithTransform(
createMixedPrimitives(2, 0), translate(1, 0, 0), 1); // 2 lines translated
buffer.addDraw(draw1);
buffer.addDraw(draw2);
// Execute
CompiledPrimitiveBuffers compiled = buffer.finish();
// Verify: Has lines, no triangles
assertNotNull(compiled, "Should compile non-empty buffer");
assertTrue(compiled.hasLines(), "Should have lines");
assertFalse(compiled.hasTriangles(), "Should not have triangles");
// 2 merged ranges (different transforms)
assertEquals(2, compiled.lineMergedRanges().length,
"Different transforms should produce separate merged ranges");
}
@Test
void testPrimitiveBufferMergesSameTransformLines() {
// Setup: Multiple draws with same transform
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
buffer.addDraw(createLinesDraw(2, 0)); // 2 lines
buffer.addDraw(createLinesDraw(3, 0)); // 3 more lines, same transform
buffer.addDraw(createLinesDraw(1, 0)); // 1 more line, same transform
// Execute
CompiledPrimitiveBuffers compiled = buffer.finish();
// Verify: 1 merged range (all same transform)
assertNotNull(compiled);
assertEquals(1, compiled.lineMergedRanges().length,
"Same transform draws should merge into 1 range");
// Total vertices: (2+3+1) lines * 2 vertices = 12
assertEquals(12, compiled.lineMergedRanges()[0].vertexCount(),
"Merged range should contain all line vertices");
}
@Test
void testPrimitiveBufferSeparatesTrianglesByTransform() {
// Setup
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
buffer.addDraw(createTrianglesDraw(2, 0)); // 2 triangles at identity
buffer.addDraw(createPrimitiveDrawWithTransform(
createMixedPrimitives(0, 2), translate(1, 0, 0), 1)); // 2 triangles translated
// Execute
CompiledPrimitiveBuffers compiled = buffer.finish();
// Verify
assertNotNull(compiled);
assertFalse(compiled.hasLines(), "Should not have lines");
assertTrue(compiled.hasTriangles(), "Should have triangles");
assertEquals(2, compiled.triangleMergedRanges().length,
"Different transforms should produce separate merged ranges");
}
@Test
void testPrimitiveBufferMergesSameTransformTriangles() {
// Setup
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
buffer.addDraw(createTrianglesDraw(2, 0)); // 2 triangles
buffer.addDraw(createTrianglesDraw(3, 0)); // 3 more, same transform
// Execute
CompiledPrimitiveBuffers compiled = buffer.finish();
// Verify: 1 merged range
assertNotNull(compiled);
assertEquals(1, compiled.triangleMergedRanges().length,
"Same transform draws should merge into 1 range");
// Total vertices: (2+3) triangles * 3 vertices = 15
assertEquals(15, compiled.triangleMergedRanges()[0].vertexCount(),
"Merged range should contain all triangle vertices");
}
@Test
void testPrimitiveBufferHandlesMixedLinesAndTriangles() {
// Setup: Draw with mixed primitives
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
buffer.addDraw(createMixedPrimitiveDraw(3, 2, 0)); // 3 lines + 2 triangles
// Execute
CompiledPrimitiveBuffers compiled = buffer.finish();
// Verify: Both types present
assertNotNull(compiled);
assertTrue(compiled.hasLines(), "Should have lines");
assertTrue(compiled.hasTriangles(), "Should have triangles");
// Verify counts
assertEquals(6, compiled.lineMergedRanges()[0].vertexCount(),
"Should have 6 line vertices (3 lines * 2)");
assertEquals(6, compiled.triangleMergedRanges()[0].vertexCount(),
"Should have 6 triangle vertices (2 triangles * 3)");
}
@Test
void testPrimitiveBufferEmptyReturnsNull() {
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
CompiledPrimitiveBuffers compiled = buffer.finish();
assertNull(compiled, "Empty buffer should return null");
}
@Test
void testPrimitiveBufferPerDrawRangesTrackEachDraw() {
// Setup: Multiple draws
TessellatorPrimitiveBuffer buffer = new TessellatorPrimitiveBuffer(positionOnly());
buffer.addDraw(createLinesDraw(2, 0)); // 4 vertices
buffer.addDraw(createLinesDraw(3, 1)); // 6 vertices (same transform, different cmd index)
buffer.addDraw(createLinesDraw(1, 2)); // 2 vertices
// Execute
CompiledPrimitiveBuffers compiled = buffer.finish();
// Verify: Per-draw ranges track each individual draw
assertNotNull(compiled);
assertEquals(3, compiled.linePerDrawRanges().length,
"Should have 3 per-draw ranges");
// Verify individual range sizes
assertEquals(4, compiled.linePerDrawRanges()[0].vertexCount());
assertEquals(6, compiled.linePerDrawRanges()[1].vertexCount());
assertEquals(2, compiled.linePerDrawRanges()[2].vertexCount());
}
@Test
void testDifferentFlagsProduceDifferentFormats() {
// Test that format-aware compilation uses optimal vertex sizes
// positionOnly = 12 bytes, positionColor = 16 bytes, fullFormat = 32 bytes
// Position only (12 bytes per vertex)
TessellatorPrimitiveBuffer posOnlyBuffer = new TessellatorPrimitiveBuffer(positionOnly());
posOnlyBuffer.addDraw(createLinesDraw(1, 0));
CompiledPrimitiveBuffers posOnlyCompiled = posOnlyBuffer.finish();
assertNotNull(posOnlyCompiled);
assertEquals(positionOnly(), posOnlyCompiled.flags(), "Should preserve positionOnly flags");
// Position + Color (16 bytes per vertex)
TessellatorPrimitiveBuffer posColorBuffer = new TessellatorPrimitiveBuffer(positionColor());
posColorBuffer.addDraw(createPrimitiveDraw(createMixedPrimitives(1, 0), identity(), positionColor(), 0));
CompiledPrimitiveBuffers posColorCompiled = posColorBuffer.finish();
assertNotNull(posColorCompiled);
assertEquals(positionColor(), posColorCompiled.flags(), "Should preserve positionColor flags");
// Full format (32 bytes per vertex)
TessellatorPrimitiveBuffer fullBuffer = new TessellatorPrimitiveBuffer(fullFormat());
fullBuffer.addDraw(createPrimitiveDraw(createMixedPrimitives(1, 0), identity(), fullFormat(), 0));
CompiledPrimitiveBuffers fullCompiled = fullBuffer.finish();
assertNotNull(fullCompiled);
assertEquals(fullFormat(), fullCompiled.flags(), "Should preserve fullFormat flags");
// Verify the flags differ (ensuring format selection works)
assertNotEquals(posOnlyCompiled.flags(), posColorCompiled.flags());
assertNotEquals(posColorCompiled.flags(), fullCompiled.flags());
}
// ==================== Integration Tests ====================
@Test
void testBuildOptimizedDisplayListWithLinesOnly() {
// Setup: Only primitive draws
List<AccumulatedDraw> quadDraws = new ArrayList<>();
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
primitiveDraws.add(createLinesDraw(5, 0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 VBO for lines
assertEquals(1, result.ownedVbos().length, "Should have 1 VBO for lines");
// Verify: 1 DrawRangeCmd
assertEquals(1, countDrawRangeCmds(optimized), "Should have 1 DrawRangeCmd");
}
@Test
void testBuildOptimizedDisplayListWithTrianglesOnly() {
// Setup
List<AccumulatedDraw> quadDraws = new ArrayList<>();
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
primitiveDraws.add(createTrianglesDraw(3, 0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 VBO for triangles
assertEquals(1, result.ownedVbos().length, "Should have 1 VBO for triangles");
assertEquals(1, countDrawRangeCmds(optimized), "Should have 1 DrawRangeCmd");
}
@Test
void testBuildOptimizedDisplayListWithMixedPrimitives() {
// Setup: Lines and triangles
List<AccumulatedDraw> quadDraws = new ArrayList<>();
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
primitiveDraws.add(createLinesDraw(3, 0));
primitiveDraws.add(createTrianglesDraw(2, 1));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 2 VBOs (one for lines, one for triangles)
assertEquals(2, result.ownedVbos().length,
"Should have 2 VBOs (lines + triangles)");
assertEquals(2, countDrawRangeCmds(optimized),
"Should have 2 DrawRangeCmds");
}
@Test
void testBuildOptimizedDisplayListWithQuadsAndPrimitives() {
// Setup: Mixed quads and primitives
List<AccumulatedDraw> quadDraws = new ArrayList<>();
quadDraws.add(createSimpleDraw(0)); // 1 quad
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
primitiveDraws.add(createLinesDraw(2, 1)); // 2 lines
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 2 VBOs (quads + lines)
assertEquals(2, result.ownedVbos().length,
"Should have 2 VBOs (quads + lines)");
assertEquals(2, countDrawRangeCmds(optimized),
"Should have 2 DrawRangeCmds");
// Verify unique VBOs in commands
Set<VertexBuffer> uniqueVBOs = getUniqueVBOs(optimized);
assertEquals(2, uniqueVBOs.size(), "Commands should reference 2 unique VBOs");
}
@Test
void testBuildOptimizedDisplayListWithQuadsLinesAndTriangles() {
// Setup: All three types
List<AccumulatedDraw> quadDraws = new ArrayList<>();
quadDraws.add(createSimpleDraw(0));
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
primitiveDraws.add(createLinesDraw(2, 1));
primitiveDraws.add(createTrianglesDraw(2, 2));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 3 VBOs (quads + lines + triangles)
assertEquals(3, result.ownedVbos().length,
"Should have 3 VBOs (quads + lines + triangles)");
assertEquals(3, countDrawRangeCmds(optimized),
"Should have 3 DrawRangeCmds");
}
@Test
void testBuildOptimizedDisplayListPrimitiveTransformMerging() {
// Setup: Multiple draws with same transform should merge
List<AccumulatedDraw> quadDraws = new ArrayList<>();
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
// Add 3 line draws at same transform
primitiveDraws.add(createLinesDraw(2, 0));
primitiveDraws.add(createLinesDraw(3, 0));
primitiveDraws.add(createLinesDraw(1, 0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 VBO, 1 merged DrawRangeCmd
assertEquals(1, result.ownedVbos().length, "Should have 1 VBO");
assertEquals(1, countDrawRangeCmds(optimized),
"Same-transform draws should merge into 1 DrawRangeCmd");
}
@Test
void testBuildOptimizedDisplayListPrimitiveDifferentTransforms() {
// Setup: Draws with different transforms
List<AccumulatedDraw> quadDraws = new ArrayList<>();
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
List<ModelPrimitiveView> lines1 = new ArrayList<>();
lines1.add(createSimpleLine());
primitiveDraws.add(new AccumulatedPrimitiveDraw(lines1, identity(), positionOnly(), 0, 0));
List<ModelPrimitiveView> lines2 = new ArrayList<>();
lines2.add(createSimpleLine());
primitiveDraws.add(new AccumulatedPrimitiveDraw(lines2, translate(1, 0, 0), positionOnly(), 1, 1));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: 1 VBO (same format), 2 DrawRangeCmds (different transforms)
assertEquals(1, result.ownedVbos().length,
"Same-type primitives should share VBO");
assertEquals(2, countDrawRangeCmds(optimized),
"Different transforms should produce separate DrawRangeCmds");
}
@Test
void testBuildOptimizedDisplayListNullPrimitiveDraws() {
// Setup: null primitiveDraws list (backwards compatibility)
List<AccumulatedDraw> quadDraws = new ArrayList<>();
quadDraws.add(createSimpleDraw(0));
List<DisplayListCommand> commands = new ArrayList<>();
// Execute: Pass null for primitiveDraws
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, null, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: Works normally with just quads
assertEquals(1, result.ownedVbos().length, "Should have 1 VBO for quads");
assertEquals(1, countDrawRangeCmds(optimized), "Should have 1 DrawRangeCmd");
}
@Test
void testBuildOptimizedDisplayListEmptyPrimitiveDraws() {
// Setup: Empty primitiveDraws list
List<AccumulatedDraw> quadDraws = new ArrayList<>();
quadDraws.add(createSimpleDraw(0));
List<AccumulatedPrimitiveDraw> primitiveDraws = new ArrayList<>();
List<DisplayListCommand> commands = new ArrayList<>();
// Execute
OptimizedListResult result = DisplayListManager.buildOptimizedDisplayList(
commands, quadDraws, primitiveDraws, 1);
DisplayListCommand[] optimized = result.commands();
// Verify: Works normally with just quads
assertEquals(1, result.ownedVbos().length, "Should have 1 VBO for quads");
assertEquals(1, countDrawRangeCmds(optimized), "Should have 1 DrawRangeCmd");
}
}
@@ -3,7 +3,6 @@ package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.AngelicaExtension;
import org.joml.Matrix4f;
import org.joml.Matrix4fStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
@@ -16,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(AngelicaExtension.class)
public class GLMS_MatrixStack_UnitTest {
public class GLSM_MatrixStack_UnitTest {
final static FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
Matrix4f getMatrix(int matrix, boolean cached) {
@@ -42,7 +42,7 @@ public class GLSM_VAO_UnitTest {
// Set the vertex attrib to a defined state
int vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL11.glVertexPointer(defaultVertexSize, defaultVertexType, defaultVertexStride, 0);
@@ -53,16 +53,16 @@ public class GLSM_VAO_UnitTest {
verifyState(GL11.GL_VERTEX_ARRAY_TYPE, defaultVertexType, "GL_VERTEX_ARRAY_TYPE - Setup");
verifyState(GL11.GL_VERTEX_ARRAY_STRIDE, defaultVertexStride, "GL_VERTEX_ARRAY_STRIDE - Setup");
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
// Verify VAO & VBO creation
int vao = UniversalVAO.genVertexArrays();
Assertions.assertTrue(vao > 0, "GL_VERTEX_ARRAY_BINDING - VAO Created");
UniversalVAO.bindVertexArray(vao);
glBindVertexArray(vao);
verifyState(GL30.GL_VERTEX_ARRAY_BINDING, vao, "GL_VERTEX_ARRAY_BINDING - Bound");
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
verifyState(GL15.GL_ARRAY_BUFFER_BINDING, vbo, "GL_ARRAY_BUFFER_BINDING - Bound");
verifyState(GL13.GL_CLIENT_ACTIVE_TEXTURE, GL13.GL_TEXTURE1, "GL_CLIENT_ACTIVE_TEXTURE - Preserved");
@@ -81,10 +81,10 @@ public class GLSM_VAO_UnitTest {
verifyState(GL11.GL_VERTEX_ARRAY_TYPE, vaoVertexType, "GL_VERTEX_ARRAY_TYPE - Set");
verifyState(GL11.GL_VERTEX_ARRAY_STRIDE, vaoVertexStride, "GL_VERTEX_ARRAY_STRIDE - Set");
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
// Check if everything got reset to default
UniversalVAO.bindVertexArray(0);
glBindVertexArray(0);
verifyState(GL30.GL_VERTEX_ARRAY_BINDING, 0, "GL_VERTEX_ARRAY_BINDING - Unbound");
verifyState(GL15.GL_ARRAY_BUFFER_BINDING, 0, "GL_ARRAY_BUFFER_BINDING - Unbound");
@@ -96,15 +96,27 @@ public class GLSM_VAO_UnitTest {
verifyState(GL11.GL_NORMAL_ARRAY, true, "GL_NORMAL_ARRAY - Reset");
// Check if the VAO states are still valid after re-binding it
UniversalVAO.bindVertexArray(vao);
glBindVertexArray(vao);
verifyState(GL11.GL_VERTEX_ARRAY, vaoVertexEnabled, "GL_VERTEX_ARRAY - Preserved");
verifyState(GL11.GL_VERTEX_ARRAY_SIZE, vaoVertexSize, "GL_VERTEX_ARRAY_SIZE - Preserved");
verifyState(GL11.GL_VERTEX_ARRAY_TYPE, vaoVertexType, "GL_VERTEX_ARRAY_TYPE - Preserved");
verifyState(GL11.GL_VERTEX_ARRAY_STRIDE, vaoVertexStride, "GL_VERTEX_ARRAY_STRIDE - Preserved");
UniversalVAO.bindVertexArray(0);
glBindVertexArray(0);
GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
glBindVertexArray(vao);
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GLStateManager.glPopAttrib();
verifyState(GL30.GL_VERTEX_ARRAY_BINDING, vao, "GL_VERTEX_ARRAY_BINDING - Not affected by glPushAttrib");
verifyState(GL15.GL_ARRAY_BUFFER_BINDING, vbo, "GL_ARRAY_BUFFER_BINDING - Not affected by glPushAttrib");
GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
GL15.glDeleteBuffers(vbo);
UniversalVAO.deleteVertexArrays(vao);
@@ -117,4 +129,9 @@ public class GLSM_VAO_UnitTest {
verifyState(GL11.GL_COLOR_ARRAY, false, "GL_COLOR_ARRAY Default State");
verifyState(GL11.GL_INDEX_ARRAY, false, "GL_INDEX_ARRAY Default State");
}
private static void glBindVertexArray(int array) {
GLStateManager.boundVAO = array;
UniversalVAO.bindVertexArray(array);
}
}
@@ -1,9 +1,10 @@
package com.gtnewhorizons.angelica.glsm.recording;
import com.gtnewhorizon.gtnhlib.client.renderer.DirectTessellator;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat;
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;
@@ -33,14 +34,6 @@ class RecordingDataStructuresTest {
}
}
@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 testCompiledDisplayListCreation() {
// Create a display list with PushMatrix, Scale, PopMatrix
@@ -73,182 +66,40 @@ class RecordingDataStructuresTest {
}
@Test
void testImmediateModeRecorderCapturesQuads() {
ImmediateModeRecorder recorder = new ImmediateModeRecorder();
void testImmediateModeRecorder() {
DirectTessellator recorder = ImmediateModeRecorder.getInternalTessellator();
// 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);
recorder.startDrawing(GL11.GL_QUADS);
recorder.setTextureUV(0, 0);
recorder.addVertex(0, 0, 0);
recorder.setTextureUV(1, 0);
recorder.addVertex(1, 0, 0);
recorder.setTextureUV(1, 1);
recorder.addVertex(1, 1, 0);
recorder.setTextureUV(0, 1);
recorder.addVertex(0, 1, 0);
// end() now returns quads immediately (for correct command interleaving)
ImmediateModeRecorder.Result result = recorder.end();
DirectTessellator result = ImmediateModeRecorder.end();
assertNotNull(result);
assertEquals(1, result.quads().size());
assertTrue(result.flags().hasTexture); // We called setTexCoord
assertTrue(ImmediateModeRecorder.hasGeometry());
assertEquals(4, result.getVertexCount());
assertEquals(GL11.GL_QUADS, result.getDrawMode());
assertTrue(result.hasTexture); // We called setTexCoord
assertFalse(result.hasNormals);
assertFalse(result.hasColor);
assertFalse(result.hasBrightness);
assertEquals(DefaultVertexFormat.POSITION_TEXTURE, result.getVertexFormat());
// After end(), recorder should be empty (quads were returned immediately)
assertFalse(recorder.hasGeometry());
assertNull(recorder.getQuadsAndClear());
}
result.reset();
@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");
assertFalse(ImmediateModeRecorder.hasGeometry());
assertEquals(0, result.getVertexCount());
assertFalse(result.hasTexture);
assertFalse(result.hasNormals);
assertFalse(result.hasColor);
assertFalse(result.hasBrightness);
assertNull(result.getVertexFormat());
}
}