Upload fewer things (#1811)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2026-06-04 13:11:59 -07:00
1 parent eef3d2f50a
commit ec278d3d7a
13 files changed
+690 -311

No files matched your search

@@ -103,8 +103,15 @@ public class CompatUniformManager {
}
}
/** Per-program cached uniform locations. Maps program ID → int[LOC_COUNT]. */
private static final Int2ObjectOpenHashMap<int[]> programLocations = new Int2ObjectOpenHashMap<>();
/** Per-program cached uniform locations + last-uploaded generations. Maps program ID -> state. */
private static final Int2ObjectOpenHashMap<ProgramUniforms> programUniforms = new Int2ObjectOpenHashMap<>();
private static final class ProgramUniforms {
final int[] locs;
int lastMvGen = -1, lastMvLinearGen = -1, lastProjGen = -1, lastTexMatGen = -1;
int lastFragmentGen = -1, lastLightingGen = -1, lastClipPlaneGen = -1;
ProgramUniforms(int[] locs) { this.locs = locs; }
}
// Reusable NIO buffers for upload
private static final FloatBuffer mat4Buf = BufferUtils.createFloatBuffer(16);
@@ -123,15 +130,6 @@ public class CompatUniformManager {
new Matrix4f().scale(LIGHTMAP_SCALE).translate(8.0f, 8.0f, 8.0f).get(lightmapMatrixBuf);
}
// Dirty tracking: skip uploads when state hasn't changed and program is the same
private static int lastProgram = -1;
private static int lastMvGen = -1;
private static int lastProjGen = -1;
private static int lastTexMatGen = -1;
private static int lastFragmentGen = -1;
private static int lastLightingGen = -1;
private static int lastClipPlaneGen = -1;
private CompatUniformManager() {}
/**
@@ -154,45 +152,46 @@ public class CompatUniformManager {
}
if (hasAny) {
programLocations.put(program, locs);
programUniforms.put(program, new ProgramUniforms(locs));
GLStateManager.LOGGER.debug("CompatUniformManager: program {} has compat uniforms", program);
}
}
public static void onUseProgram(int program) {
public static void refreshCompatUniforms(int program) {
if (program == 0) return;
int[] locs = programLocations.get(program);
if (locs == null) return;
final ProgramUniforms pu = programUniforms.get(program);
if (pu == null) return;
final int[] locs = pu.locs;
final boolean programChanged = program != lastProgram;
lastProgram = program;
// Matrix uniforms — skip if generation unchanged and same program
// Matrix uniforms — skip if this program's storage already holds the current generation
final int mvGen = GLStateManager.mvGeneration;
final int mvLinearGen = GLStateManager.mvLinearGeneration;
final int projGen = GLStateManager.projGeneration;
final int texMatGen = GLStateManager.texMatrixGeneration;
final boolean mvChanged = programChanged || mvGen != lastMvGen;
final boolean projChanged = programChanged || projGen != lastProjGen;
final boolean texMatChanged = programChanged || texMatGen != lastTexMatGen;
final boolean mvChanged = mvGen != pu.lastMvGen;
final boolean mvLinearChanged = mvLinearGen != pu.lastMvLinearGen;
final boolean projChanged = projGen != pu.lastProjGen;
final boolean texMatChanged = texMatGen != pu.lastTexMatGen;
if (mvChanged || projChanged || texMatChanged) {
uploadMatrices(locs, mvChanged, projChanged, texMatChanged);
lastMvGen = mvGen;
lastProjGen = projGen;
lastTexMatGen = texMatGen;
uploadMatrices(locs, mvChanged, mvLinearChanged, projChanged, texMatChanged);
pu.lastMvGen = mvGen;
pu.lastMvLinearGen = mvLinearGen;
pu.lastProjGen = projGen;
pu.lastTexMatGen = texMatGen;
}
// Fragment-category uniforms (fog, alpha) — skip if generation unchanged
final int fragGen = GLStateManager.fragmentGeneration;
if (programChanged || fragGen != lastFragmentGen) {
lastFragmentGen = fragGen;
if (fragGen != pu.lastFragmentGen) {
pu.lastFragmentGen = fragGen;
uploadFragmentUniforms(locs);
}
// Lighting-derived uniforms (scene color, light sources, material)
final int litGen = GLStateManager.lightingGeneration;
if (programChanged || litGen != lastLightingGen) {
lastLightingGen = litGen;
if (litGen != pu.lastLightingGen) {
pu.lastLightingGen = litGen;
if (locs[LOC_SCENE_COLOR] != -1) uploadSceneColor(locs);
uploadLightSources(locs);
uploadMaterial(locs);
@@ -201,14 +200,14 @@ public class CompatUniformManager {
// Clip plane equations + enabled bool — uploaded when enable state or equations change
if (locs[LOC_CLIP_PLANES] != -1 || locs[LOC_CLIP_PLANES_ENABLED] != -1) {
final int cpGen = GLStateManager.clipPlaneGeneration;
if (programChanged || cpGen != lastClipPlaneGen) {
lastClipPlaneGen = cpGen;
if (cpGen != pu.lastClipPlaneGen) {
pu.lastClipPlaneGen = cpGen;
uploadClipPlanes(locs);
}
}
}
private static void uploadMatrices(int[] locs, boolean mvChanged, boolean projChanged, boolean texMatChanged) {
private static void uploadMatrices(int[] locs, boolean mvChanged, boolean mvLinearChanged, boolean projChanged, boolean texMatChanged) {
final Matrix4f mv = GLStateManager.getModelViewMatrix();
final Matrix4f proj = GLStateManager.getProjectionMatrix();
@@ -230,7 +229,9 @@ public class CompatUniformManager {
scratchMatrix.get(mat4Buf);
RENDER_BACKEND.uniformMatrix4(locs[LOC_MODELVIEW_INVERSE], false, mat4Buf);
}
}
if (mvLinearChanged) {
// Normal Matrix (inverse-transpose of upper-left 3x3 of ModelView)
if (locs[LOC_NORMAL] != -1 || locs[LOC_IRIS_NORMAL] != -1) {
mv.normal(normalMatrix);
@@ -244,7 +245,6 @@ public class CompatUniformManager {
normalMatrix.get(mat3Buf);
RENDER_BACKEND.uniformMatrix3(locs[LOC_IRIS_NORMAL], false, mat3Buf);
}
}
}
@@ -414,14 +414,15 @@ public class CompatUniformManager {
}
public static void onDeleteProgram(int program) {
programLocations.remove(program);
programUniforms.remove(program);
}
public static boolean hasProgram(int program) {
return programLocations.containsKey(program);
return programUniforms.containsKey(program);
}
public static int[] getLocations(int program) {
return programLocations.get(program);
final ProgramUniforms pu = programUniforms.get(program);
return pu == null ? null : pu.locs;
}
}
@@ -144,7 +144,8 @@ public class GLStateManager {
// Generation counters for FFP uniform dirty tracking. Bumped when the corresponding GLSM state changes.
// Per-matrix-mode generation counters — avoids re-uploading all matrices when only one mode changed
public static int mvGeneration; // modelview matrix changes
public static int mvGeneration; // modelview matrix changes (any: translation, rotation, scale)
public static int mvLinearGeneration;
public static int projGeneration; // projection matrix changes
public static int texMatrixGeneration; // texture matrix changes
public static int lightingGeneration;
@@ -301,6 +302,7 @@ public class GLStateManager {
// Saved generation counters at push time — used to detect whether state actually changed during push/pop scope
private static final int[] savedMvGen = new int[MAX_ATTRIB_STACK_DEPTH];
private static final int[] savedMvLinearGen = new int[MAX_ATTRIB_STACK_DEPTH];
private static final int[] savedProjGen = new int[MAX_ATTRIB_STACK_DEPTH];
private static final int[] savedTexMatGen = new int[MAX_ATTRIB_STACK_DEPTH];
private static final int[] savedLightingGen = new int[MAX_ATTRIB_STACK_DEPTH];
@@ -2876,6 +2878,22 @@ public class GLStateManager {
}
}
colorMaterial.enable();
bakeCurrentColorIntoTrackedMaterial();
}
public static void disableColorMaterial() {
final RecordMode mode = DisplayListManager.getRecordMode();
if (mode != RecordMode.NONE) {
DisplayListManager.recordDisable(GL11.GL_COLOR_MATERIAL);
if (mode == RecordMode.COMPILE) {
return;
}
}
bakeCurrentColorIntoTrackedMaterial();
colorMaterial.disable();
}
private static void bakeCurrentColorIntoTrackedMaterial() {
final float r = getColor().getRed();
final float g = getColor().getGreen();
final float b = getColor().getBlue();
@@ -2906,17 +2924,6 @@ public class GLStateManager {
}
}
public static void disableColorMaterial() {
final RecordMode mode = DisplayListManager.getRecordMode();
if (mode != RecordMode.NONE) {
DisplayListManager.recordDisable(GL11.GL_COLOR_MATERIAL);
if (mode == RecordMode.COMPILE) {
return;
}
}
colorMaterial.disable();
}
public static void disableLighting() {
final RecordMode mode = DisplayListManager.getRecordMode();
if (mode != RecordMode.NONE) {
@@ -3047,16 +3054,19 @@ public class GLStateManager {
}
// Update cached state (FFP shader reads from cache)
if (isCachingEnabled()) {
boolean changed = true;
boolean changed = false;
switch (pname) {
case GL11.GL_FOG_DENSITY -> { fogState.setDensity(param); fragmentGeneration++; }
case GL11.GL_FOG_START -> { fogState.setStart(param); fragmentGeneration++; }
case GL11.GL_FOG_END -> { fogState.setEnd(param); fragmentGeneration++; }
case GL11.GL_FOG_MODE -> { fogState.setFogMode((int) param); fragmentGeneration++; }
default -> changed = false;
case GL11.GL_FOG_DENSITY -> { if (BYPASS_CACHE || fogState.getDensity() != param) { fogState.setDensity(param); changed = true; } }
case GL11.GL_FOG_START -> { if (BYPASS_CACHE || fogState.getStart() != param) { fogState.setStart(param); changed = true; } }
case GL11.GL_FOG_END -> { if (BYPASS_CACHE || fogState.getEnd() != param) { fogState.setEnd(param); changed = true; } }
case GL11.GL_FOG_MODE -> { if (BYPASS_CACHE || fogState.getFogMode() != (int) param) { fogState.setFogMode((int) param); changed = true; } }
default -> {}
}
if (changed && GLSMHooks.FOG_STATE_CHANGE.hasListeners()) {
GLSMHooks.FOG_STATE_CHANGE.post(GLSMHooks.fogStateChangeEvent);
if (changed) {
fragmentGeneration++;
if (GLSMHooks.FOG_STATE_CHANGE.hasListeners()) {
GLSMHooks.FOG_STATE_CHANGE.post(GLSMHooks.fogStateChangeEvent);
}
}
}
}
@@ -3070,7 +3080,7 @@ public class GLStateManager {
}
}
// Update cached state (FFP shader reads from cache)
if (isCachingEnabled() && pname == GL11.GL_FOG_MODE) {
if (isCachingEnabled() && pname == GL11.GL_FOG_MODE && (BYPASS_CACHE || fogState.getFogMode() != param)) {
fogState.setFogMode(param);
fragmentGeneration++;
if (GLSMHooks.FOG_STATE_CHANGE.hasListeners()) {
@@ -3221,6 +3231,7 @@ public class GLStateManager {
// Snapshot generation counters so we can detect actual changes at pop time
savedMvGen[attribDepth] = mvGeneration;
savedMvLinearGen[attribDepth] = mvLinearGeneration;
savedProjGen[attribDepth] = projGeneration;
savedTexMatGen[attribDepth] = texMatrixGeneration;
savedLightingGen[attribDepth] = lightingGeneration;
@@ -3329,6 +3340,7 @@ public class GLStateManager {
if ((mask & GL11.GL_TEXTURE_BIT) != 0 && fragmentGeneration != savedFragmentGen[depth]) fragmentGeneration++; // texenv state
if ((mask & GL11.GL_TRANSFORM_BIT) != 0) {
if (mvGeneration != savedMvGen[depth]) mvGeneration++;
if (mvLinearGeneration != savedMvLinearGen[depth]) mvLinearGeneration++;
if (projGeneration != savedProjGen[depth]) projGeneration++;
if (texMatrixGeneration != savedTexMatGen[depth]) texMatrixGeneration++;
}
@@ -3446,8 +3458,15 @@ public class GLStateManager {
/** Bump the generation counter for the currently active matrix mode. */
private static void bumpMatrixGeneration() {
bumpMatrixGeneration(true);
}
private static void bumpMatrixGeneration(boolean linearChanged) {
switch (matrixMode.getMode()) {
case GL11.GL_MODELVIEW -> mvGeneration++;
case GL11.GL_MODELVIEW -> {
mvGeneration++;
if (linearChanged) mvLinearGeneration++;
}
case GL11.GL_PROJECTION -> projGeneration++;
case GL11.GL_TEXTURE -> texMatrixGeneration++;
}
@@ -3474,7 +3493,7 @@ public class GLStateManager {
}
if (isCachingEnabled()) {
getMatrixStack().translate(x, y, z);
bumpMatrixGeneration();
bumpMatrixGeneration(false);
}
}
@@ -3485,7 +3504,7 @@ public class GLStateManager {
}
if (isCachingEnabled()) {
getMatrixStack().translate((float) x, (float) y, (float) z);
bumpMatrixGeneration();
bumpMatrixGeneration(false);
}
}
@@ -3956,64 +3975,72 @@ public class GLStateManager {
public static void glSamplerParameteri(int sampler, int pname, int param) { RENDER_BACKEND.samplerParameteri(sampler, pname, param); }
public static void glSamplerParameterf(int sampler, int pname, float param) { RENDER_BACKEND.samplerParameterf(sampler, pname, param); }
private static void glMaterialFront(int pname, FloatBuffer params) {
switch (pname) {
private static boolean glMaterialFront(int pname, FloatBuffer params) {
return switch (pname) {
case GL11.GL_AMBIENT -> frontMaterial.setAmbient(params);
case GL11.GL_DIFFUSE -> frontMaterial.setDiffuse(params);
case GL11.GL_SPECULAR -> frontMaterial.setSpecular(params);
case GL11.GL_EMISSION -> frontMaterial.setEmission(params);
case GL11.GL_SHININESS -> frontMaterial.setShininess(params);
case GL11.GL_AMBIENT_AND_DIFFUSE -> {
frontMaterial.setAmbient(params);
frontMaterial.setDiffuse(params);
final boolean a = frontMaterial.setAmbient(params);
final boolean d = frontMaterial.setDiffuse(params);
yield a || d;
}
case GL11.GL_COLOR_INDEXES -> frontMaterial.setColorIndexes(params);
}
default -> false;
};
}
private static void glMaterialBack(int pname, FloatBuffer params) {
switch (pname) {
private static boolean glMaterialBack(int pname, FloatBuffer params) {
return switch (pname) {
case GL11.GL_AMBIENT -> backMaterial.setAmbient(params);
case GL11.GL_DIFFUSE -> backMaterial.setDiffuse(params);
case GL11.GL_SPECULAR -> backMaterial.setSpecular(params);
case GL11.GL_EMISSION -> backMaterial.setEmission(params);
case GL11.GL_SHININESS -> backMaterial.setShininess(params);
case GL11.GL_AMBIENT_AND_DIFFUSE -> {
backMaterial.setAmbient(params);
backMaterial.setDiffuse(params);
final boolean a = backMaterial.setAmbient(params);
final boolean d = backMaterial.setDiffuse(params);
yield a || d;
}
case GL11.GL_COLOR_INDEXES -> backMaterial.setColorIndexes(params);
}
default -> false;
};
}
private static void glMaterialFront(int pname, IntBuffer params) {
switch (pname) {
private static boolean glMaterialFront(int pname, IntBuffer params) {
return switch (pname) {
case GL11.GL_AMBIENT -> frontMaterial.setAmbient(params);
case GL11.GL_DIFFUSE -> frontMaterial.setDiffuse(params);
case GL11.GL_SPECULAR -> frontMaterial.setSpecular(params);
case GL11.GL_EMISSION -> frontMaterial.setEmission(params);
case GL11.GL_SHININESS -> frontMaterial.setShininess(params);
case GL11.GL_AMBIENT_AND_DIFFUSE -> {
frontMaterial.setAmbient(params);
frontMaterial.setDiffuse(params);
final boolean a = frontMaterial.setAmbient(params);
final boolean d = frontMaterial.setDiffuse(params);
yield a || d;
}
case GL11.GL_COLOR_INDEXES -> frontMaterial.setColorIndexes(params);
}
default -> false;
};
}
private static void glMaterialBack(int pname, IntBuffer params) {
switch (pname) {
private static boolean glMaterialBack(int pname, IntBuffer params) {
return switch (pname) {
case GL11.GL_AMBIENT -> backMaterial.setAmbient(params);
case GL11.GL_DIFFUSE -> backMaterial.setDiffuse(params);
case GL11.GL_SPECULAR -> backMaterial.setSpecular(params);
case GL11.GL_EMISSION -> backMaterial.setEmission(params);
case GL11.GL_SHININESS -> backMaterial.setShininess(params);
case GL11.GL_AMBIENT_AND_DIFFUSE -> {
backMaterial.setAmbient(params);
backMaterial.setDiffuse(params);
final boolean a = backMaterial.setAmbient(params);
final boolean d = backMaterial.setDiffuse(params);
yield a || d;
}
case GL11.GL_COLOR_INDEXES -> backMaterial.setColorIndexes(params);
}
default -> false;
};
}
public static void glMaterial(int face, int pname, FloatBuffer params) {
@@ -4024,17 +4051,19 @@ public class GLStateManager {
return;
}
}
boolean changed = false;
if (face == GL11.GL_FRONT) {
glMaterialFront(pname, params);
changed = glMaterialFront(pname, params);
} else if (face == GL11.GL_BACK) {
glMaterialBack(pname, params);
changed = glMaterialBack(pname, params);
} else if (face == GL11.GL_FRONT_AND_BACK) {
glMaterialFront(pname, params);
glMaterialBack(pname, params);
final boolean f = glMaterialFront(pname, params);
final boolean b = glMaterialBack(pname, params);
changed = f || b;
} else {
throw new RuntimeException("Unsupported face value for glMaterial: " + face);
}
lightingGeneration++;
if (changed) lightingGeneration++;
}
public static void glMaterial(int face, int pname, IntBuffer params) {
@@ -4053,17 +4082,19 @@ public class GLStateManager {
return;
}
}
boolean changed = false;
if (face == GL11.GL_FRONT) {
glMaterialFront(pname, params);
changed = glMaterialFront(pname, params);
} else if (face == GL11.GL_BACK) {
glMaterialBack(pname, params);
changed = glMaterialBack(pname, params);
} else if (face == GL11.GL_FRONT_AND_BACK) {
glMaterialFront(pname, params);
glMaterialBack(pname, params);
final boolean f = glMaterialFront(pname, params);
final boolean b = glMaterialBack(pname, params);
changed = f || b;
} else {
throw new RuntimeException("Unsupported face value for glMaterial: " + face);
}
lightingGeneration++;
if (changed) lightingGeneration++;
}
public static void glMaterialf(int face, int pname, float val) {
@@ -4079,17 +4110,19 @@ public class GLStateManager {
return;
}
boolean changed = false;
if (face == GL11.GL_FRONT) {
frontMaterial.setShininess(val);
changed = frontMaterial.setShininess(val);
} else if (face == GL11.GL_BACK) {
backMaterial.setShininess(val);
changed = backMaterial.setShininess(val);
} else if (face == GL11.GL_FRONT_AND_BACK) {
frontMaterial.setShininess(val);
backMaterial.setShininess(val);
final boolean f = frontMaterial.setShininess(val);
final boolean b = backMaterial.setShininess(val);
changed = f || b;
} else {
throw new RuntimeException("Unsupported face value for glMaterial: " + face);
}
lightingGeneration++;
if (changed) lightingGeneration++;
}
public static void glMateriali(int face, int pname, int val) {
@@ -4107,7 +4140,7 @@ public class GLStateManager {
}
}
final LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0];
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_AMBIENT -> lightState.setAmbient(params);
case GL11.GL_DIFFUSE -> lightState.setDiffuse(params);
case GL11.GL_SPECULAR -> lightState.setSpecular(params);
@@ -4118,9 +4151,9 @@ public class GLStateManager {
case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(params);
case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(params);
case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(params);
default -> {}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
public static void glLight(int light, int pname, IntBuffer params) {
@@ -4140,7 +4173,7 @@ public class GLStateManager {
}
}
final LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0];
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_AMBIENT -> lightState.setAmbient(params);
case GL11.GL_DIFFUSE -> lightState.setDiffuse(params);
case GL11.GL_SPECULAR -> lightState.setSpecular(params);
@@ -4151,9 +4184,9 @@ public class GLStateManager {
case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(params);
case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(params);
case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(params);
default -> {}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
public static void glLightf(int light, int pname, float param) {
@@ -4165,15 +4198,15 @@ public class GLStateManager {
}
}
final LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0];
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_SPOT_EXPONENT -> lightState.setSpotExponent(param);
case GL11.GL_SPOT_CUTOFF -> lightState.setSpotCutoff(param);
case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(param);
case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(param);
case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(param);
default -> {}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
public static void glLighti(int light, int pname, int param) {
@@ -4185,16 +4218,15 @@ public class GLStateManager {
}
}
final LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0];
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_SPOT_EXPONENT -> lightState.setSpotExponent(param);
case GL11.GL_SPOT_CUTOFF -> lightState.setSpotCutoff(param);
case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(param);
case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(param);
case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(param);
default -> {
}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
public static void glLightModel(int pname, FloatBuffer params) {
@@ -4205,14 +4237,13 @@ public class GLStateManager {
return;
}
}
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_LIGHT_MODEL_AMBIENT -> lightModel.setAmbient(params);
case GL11.GL_LIGHT_MODEL_LOCAL_VIEWER -> lightModel.setLocalViewer(params);
case GL11.GL_LIGHT_MODEL_TWO_SIDE -> lightModel.setTwoSide(params);
default -> {
}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
public static void glLightModel(int pname, IntBuffer params) {
@@ -4231,15 +4262,14 @@ public class GLStateManager {
return;
}
}
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_LIGHT_MODEL_AMBIENT -> lightModel.setAmbient(params);
case GL12.GL_LIGHT_MODEL_COLOR_CONTROL -> lightModel.setColorControl(params);
case GL11.GL_LIGHT_MODEL_LOCAL_VIEWER -> lightModel.setLocalViewer(params);
case GL11.GL_LIGHT_MODEL_TWO_SIDE -> lightModel.setTwoSide(params);
default -> {
}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
public static void glLightModelf(int pname, float param) {
@@ -4252,13 +4282,12 @@ public class GLStateManager {
}
if (isCachingEnabled()) {
switch (pname) {
final boolean changed = switch (pname) {
case GL11.GL_LIGHT_MODEL_LOCAL_VIEWER -> lightModel.setLocalViewer(param);
case GL11.GL_LIGHT_MODEL_TWO_SIDE -> lightModel.setTwoSide(param);
default -> {
}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
}
@@ -4272,14 +4301,13 @@ public class GLStateManager {
}
if (isCachingEnabled()) {
switch (pname) {
final boolean changed = switch (pname) {
case GL12.GL_LIGHT_MODEL_COLOR_CONTROL -> lightModel.setColorControl(param);
case GL11.GL_LIGHT_MODEL_LOCAL_VIEWER -> lightModel.setLocalViewer(param);
case GL11.GL_LIGHT_MODEL_TWO_SIDE -> lightModel.setTwoSide(param);
default -> {
}
}
lightingGeneration++;
default -> false;
};
if (changed) lightingGeneration++;
}
}
@@ -4354,7 +4382,6 @@ public class GLStateManager {
GLDebug.debugMessage("Activating Program - " + program + ":" + programName);
}
RENDER_BACKEND.useProgram(program);
CompatUniformManager.onUseProgram(program);
if (GLSMHooks.PROGRAM_CHANGE.hasListeners()) {
GLSMHooks.programChangeEvent.previousProgram = prev;
GLSMHooks.programChangeEvent.newProgram = program;
@@ -26,6 +26,16 @@ public class Program {
@Getter private final VertexKey vertexKey;
@Getter private final FragmentKey fragmentKey;
// Per-program uniform dirty-tracking
static final class UploadState {
int mvGen = -1, mvLinearGen = -1, projGen = -1, texMatGen = -1;
int lightingGen = -1, fragmentGen = -1, colorGen = -1, normalGen = -1, texCoordGen = -1;
int texGenGen = -1, clipPlaneGen = -1;
float lightmapX = Float.NaN, lightmapY = Float.NaN, lineWidth = Float.NaN;
int viewportWidth = -1, viewportHeight = -1;
}
final UploadState uploadState = new UploadState();
// Uniform locations (-1 = not present in this variant)
// Matrices
public int locModelViewMatrix = -1;
@@ -36,6 +36,7 @@ public class ShaderManager {
@Getter
private boolean active = false;
private Program currentProgram = null;
private int lastBoundProgramId = -1;
private long currentVertexKeyPacked = Long.MIN_VALUE;
private final long[] currentFKScratch = new long[FragmentKey.MAX_UNITS];
private final long[] currentFKPacked = new long[FragmentKey.MAX_UNITS];
@@ -94,6 +95,7 @@ public class ShaderManager {
public void deactivate() {
active = false;
currentProgram = null;
lastBoundProgramId = -1;
currentVertexKeyPacked = Long.MIN_VALUE;
currentFKLen = 0;
}
@@ -104,16 +106,13 @@ public class ShaderManager {
if (bh != null) bh.flushDeferredBlend();
if (!active) {
if (enabled) {
final int currentProgramId = GLStateManager.getActiveProgram();
if (currentProgramId != 0) {
CompatUniformManager.onUseProgram(currentProgramId);
return;
}
active = true;
} else {
final int currentProgramId = GLStateManager.getActiveProgram();
if (currentProgramId != 0) {
CompatUniformManager.refreshCompatUniforms(currentProgramId);
return;
}
if (!enabled) return;
active = true;
}
final int fkLen = FragmentKey.packFromState(currentFKScratch);
@@ -153,7 +152,11 @@ public class ShaderManager {
System.arraycopy(currentFKScratch, 0, currentFKPacked, 0, fkLen);
currentFKLen = fkLen;
currentProgram = cache.getOrCreate(vkPacked, currentFKPacked, currentFKLen);
RENDER_BACKEND.useProgram(currentProgram.getProgramId());
final int programId = currentProgram.getProgramId();
if (programId != lastBoundProgramId) {
RENDER_BACKEND.useProgram(programId);
lastBoundProgramId = programId;
}
}
private void uploadUniforms() {
@@ -46,116 +46,99 @@ public class Uniforms {
private static final double LN2 = java.lang.Math.log(2.0);
private static final double SQRT_LN2 = java.lang.Math.sqrt(LN2);
// Dirty tracking: last-uploaded generation per category + program ID.
// Program change forces full re-upload since uniform locations differ.
private int lastProgramId = -1;
private int lastMvGen = -1;
private int lastProjGen = -1;
private int lastTexMatGen = -1;
private int lastLightingGen = -1;
private int lastFragmentGen = -1;
private int lastColorGen = -1;
private int lastNormalGen = -1;
private int lastTexCoordGen = -1;
private float lastLightmapX = Float.NaN;
private float lastLightmapY = Float.NaN;
private int lastTexGenGen = -1;
private int lastClipPlaneGen = -1;
private float lastLineWidth = Float.NaN;
private int lastViewportWidth = -1;
private int lastViewportHeight = -1;
/**
* Upload all relevant uniforms to the given FFP program based on current GLSM state.
* Uses generation counters to skip categories that haven't changed since last upload.
*/
public void upload(Program program) {
final boolean programChanged = program.getProgramId() != lastProgramId;
lastProgramId = program.getProgramId();
final Program.UploadState st = program.uploadState;
final int mvGen = GLStateManager.mvGeneration;
final int mvLinearGen = GLStateManager.mvLinearGeneration;
final int projGen = GLStateManager.projGeneration;
final int texMatGen = GLStateManager.texMatrixGeneration;
final boolean mvChanged = programChanged || mvGen != lastMvGen;
final boolean projChanged = programChanged || projGen != lastProjGen;
final boolean texMatChanged = programChanged || texMatGen != lastTexMatGen;
final boolean mvChanged = mvGen != st.mvGen;
final boolean mvLinearChanged = mvLinearGen != st.mvLinearGen;
final boolean projChanged = projGen != st.projGen;
final boolean texMatChanged = texMatGen != st.texMatGen;
if (mvChanged || projChanged || texMatChanged) {
uploadMatrices(program, mvChanged, projChanged, texMatChanged);
lastMvGen = mvGen;
lastProjGen = projGen;
lastTexMatGen = texMatGen;
uploadMatrices(program, mvChanged, mvLinearChanged, projChanged, texMatChanged);
st.mvGen = mvGen;
st.mvLinearGen = mvLinearGen;
st.projGen = projGen;
st.texMatGen = texMatGen;
}
if (program.getVertexKey().lightingEnabled()) {
final int litGen = GLStateManager.lightingGeneration;
if (programChanged || litGen != lastLightingGen) {
if (litGen != st.lightingGen) {
uploadLighting(program);
lastLightingGen = litGen;
st.lightingGen = litGen;
}
}
// Current color/normal/texcoord — skip if generation unchanged
if (!program.getVertexKey().hasVertexColor()) {
final int colorGen = GLStateManager.colorGeneration;
if (programChanged || colorGen != lastColorGen) {
if (colorGen != st.colorGen) {
uploadCurrentColor(program);
lastColorGen = colorGen;
st.colorGen = colorGen;
}
}
if (!program.getVertexKey().hasVertexNormal() && program.getVertexKey().lightingEnabled()) {
final int normalGen = ShaderManager.getNormalGeneration();
if (programChanged || normalGen != lastNormalGen) {
if (normalGen != st.normalGen) {
uploadCurrentNormal(program);
lastNormalGen = normalGen;
st.normalGen = normalGen;
}
}
if (program.getVertexKey().anyUnitTexCoordEnabled()) {
final int texGen = ShaderManager.getTexCoordGeneration();
if (programChanged || texGen != lastTexCoordGen) {
if (texGen != st.texCoordGen) {
uploadCurrentTexCoords(program);
lastTexCoordGen = texGen;
st.texCoordGen = texGen;
}
}
if (program.getVertexKey().lightmapEnabled() && !program.getVertexKey().hasVertexLightmap()) {
if (programChanged || GLSMConfig.lastBrightnessX != lastLightmapX || GLSMConfig.lastBrightnessY != lastLightmapY) {
if (GLSMConfig.lastBrightnessX != st.lightmapX || GLSMConfig.lastBrightnessY != st.lightmapY) {
uploadCurrentLightmapCoord(program);
lastLightmapX = GLSMConfig.lastBrightnessX;
lastLightmapY = GLSMConfig.lastBrightnessY;
st.lightmapX = GLSMConfig.lastBrightnessX;
st.lightmapY = GLSMConfig.lastBrightnessY;
}
}
if (program.getVertexKey().texGenEnabled()) {
final int tgGen = GLStateManager.texGenGeneration;
if (programChanged || tgGen != lastTexGenGen) {
if (tgGen != st.texGenGen) {
uploadTexGen(program);
lastTexGenGen = tgGen;
st.texGenGen = tgGen;
}
}
if (program.getVertexKey().clipPlanesEnabled()) {
final int cpGen = GLStateManager.clipPlaneGeneration;
if (programChanged || cpGen != lastClipPlaneGen) {
if (cpGen != st.clipPlaneGen) {
uploadClipPlanes(program);
lastClipPlaneGen = cpGen;
st.clipPlaneGen = cpGen;
}
}
final int fragGen = GLStateManager.fragmentGeneration;
if (programChanged || fragGen != lastFragmentGen) {
if (fragGen != st.fragmentGen) {
uploadFragmentUniforms(program);
lastFragmentGen = fragGen;
st.fragmentGen = fragGen;
}
// Wide line emulation uniforms
if (program.locLineWidth != -1 && program.locViewportSize != -1) {
uploadWideLineUniforms(program, programChanged);
uploadWideLineUniforms(program);
}
}
private void uploadMatrices(Program program, boolean mvChanged, boolean projChanged, boolean texMatChanged) {
private void uploadMatrices(Program program, boolean mvChanged, boolean mvLinearChanged, boolean projChanged, boolean texMatChanged) {
final Matrix4f mv = GLStateManager.getModelViewMatrix();
final Matrix4f proj = GLStateManager.getProjectionMatrix();
@@ -165,7 +148,9 @@ public class Uniforms {
mv.get(mat4Buf);
RENDER_BACKEND.uniformMatrix4(program.locModelViewMatrix, false, mat4Buf);
}
}
if (mvLinearChanged) {
// Normal matrix = inverse transpose of upper-left 3x3 of ModelView
if (program.locNormalMatrix != -1 || program.locNormalScale != -1) {
mv.normal(normalMatrix);
@@ -314,12 +299,13 @@ public class Uniforms {
if (program.locCurrentColor == -1) return;
// Upload the current color from GLSM
final var color = GLStateManager.getColor();
tempVec4.set(
Math.clamp(0f, 1f, color.getRed()),
Math.clamp(0f, 1f, color.getGreen()),
Math.clamp(0f, 1f, color.getBlue()),
Math.clamp(0f, 1f, color.getAlpha()));
vec4Buf.clear();
vec4Buf.put(Math.clamp(0f, 1f, color.getRed()));
vec4Buf.put(Math.clamp(0f, 1f, color.getGreen()));
vec4Buf.put(Math.clamp(0f, 1f, color.getBlue()));
vec4Buf.put(Math.clamp(0f, 1f, color.getAlpha()));
vec4Buf.flip();
tempVec4.get(vec4Buf);
RENDER_BACKEND.uniform4(program.locCurrentColor, vec4Buf);
}
@@ -342,10 +328,7 @@ public class Uniforms {
// Upload current normal from FFPShaderManager's tracked normal
final var normal = ShaderManager.getCurrentNormal();
vec3Buf.clear();
vec3Buf.put(normal.x);
vec3Buf.put(normal.y);
vec3Buf.put(normal.z);
vec3Buf.flip();
normal.get(vec3Buf);
RENDER_BACKEND.uniform3(program.locCurrentNormal, vec3Buf);
}
@@ -381,10 +364,7 @@ public class Uniforms {
private void uploadPlane(int loc, float[] plane) {
if (loc == -1) return;
vec4Buf.clear();
vec4Buf.put(plane[0]);
vec4Buf.put(plane[1]);
vec4Buf.put(plane[2]);
vec4Buf.put(plane[3]);
vec4Buf.put(plane, 0, 4);
vec4Buf.flip();
RENDER_BACKEND.uniform4(loc, vec4Buf);
}
@@ -470,29 +450,26 @@ public class Uniforms {
}
}
private void uploadWideLineUniforms(Program program, boolean programChanged) {
private void uploadWideLineUniforms(Program program) {
final Program.UploadState st = program.uploadState;
final float lineWidth = GLStateManager.getLineState().getWidth();
if (programChanged || lineWidth != lastLineWidth) {
if (lineWidth != st.lineWidth) {
RENDER_BACKEND.uniform1f(program.locLineWidth, lineWidth);
lastLineWidth = lineWidth;
st.lineWidth = lineWidth;
}
final int vw = GLStateManager.getViewportState().width;
final int vh = GLStateManager.getViewportState().height;
if (programChanged || vw != lastViewportWidth || vh != lastViewportHeight) {
if (vw != st.viewportWidth || vh != st.viewportHeight) {
RENDER_BACKEND.uniform2f(program.locViewportSize, vw, vh);
lastViewportWidth = vw;
lastViewportHeight = vh;
st.viewportWidth = vw;
st.viewportHeight = vh;
}
}
private void uploadVec4(int loc, Vector4f v) {
if (loc == -1) return;
vec4Buf.clear();
vec4Buf.put(v.x);
vec4Buf.put(v.y);
vec4Buf.put(v.z);
vec4Buf.put(v.w);
vec4Buf.flip();
v.get(vec4Buf);
RENDER_BACKEND.uniform4(loc, vec4Buf);
}
@@ -27,65 +27,75 @@ public class LightModelState implements ISettableState<LightModelState> {
twoSide = 0.0F;
}
public void setAmbient(FloatBuffer newBuffer) {
public boolean setAmbient(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) {
this.ambient.set(vector4f);
return true;
}
return false;
}
public void setAmbient(IntBuffer newBuffer) {
public boolean setAmbient(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) {
this.ambient.set(vector4f);
return true;
}
return false;
}
public void setColorControl(int val) {
public boolean setColorControl(int val) {
if (GLStateManager.shouldBypassCache() || this.colorControl != val) {
this.colorControl = val;
return true;
}
return false;
}
public void setColorControl(IntBuffer newBuffer) {
setColorControl(newBuffer.get());
public boolean setColorControl(IntBuffer newBuffer) {
return setColorControl(newBuffer.get());
}
public void setLocalViewer(float val) {
public boolean setLocalViewer(float val) {
if (GLStateManager.shouldBypassCache() || Float.compare(this.localViewer, val) != 0) {
this.localViewer = val;
return true;
}
return false;
}
public void setLocalViewer(FloatBuffer newBuffer) {
setLocalViewer(newBuffer.get());
public boolean setLocalViewer(FloatBuffer newBuffer) {
return setLocalViewer(newBuffer.get());
}
public void setLocalViewer(IntBuffer newBuffer) {
setLocalViewer((float) newBuffer.get());
public boolean setLocalViewer(IntBuffer newBuffer) {
return setLocalViewer((float) newBuffer.get());
}
public void setLocalViewer(int val) {
setLocalViewer((float) val);
public boolean setLocalViewer(int val) {
return setLocalViewer((float) val);
}
public void setTwoSide(float val) {
public boolean setTwoSide(float val) {
if (GLStateManager.shouldBypassCache() || Float.compare(this.twoSide, val) != 0) {
this.twoSide = val;
return true;
}
return false;
}
public void setTwoSide(FloatBuffer newBuffer) {
setTwoSide(newBuffer.get());
public boolean setTwoSide(FloatBuffer newBuffer) {
return setTwoSide(newBuffer.get());
}
public void setTwoSide(IntBuffer newBuffer) {
setTwoSide((float) newBuffer.get());
public boolean setTwoSide(IntBuffer newBuffer) {
return setTwoSide((float) newBuffer.get());
}
public void setTwoSide(int val) {
setTwoSide((float) val);
public boolean setTwoSide(int val) {
return setTwoSide((float) val);
}
@Override
@@ -17,6 +17,7 @@ public class LightState implements ISettableState<LightState> {
private static final Vector4i vector4i = new Vector4i();
private static final Vector4f vector4f = new Vector4f();
private static final Vector3f vector3f = new Vector3f();
private static final Vector3i vector3i = new Vector3i();
private static final Matrix3f matrix3f = new Matrix3f();
@@ -27,6 +28,10 @@ public class LightState implements ISettableState<LightState> {
public final Vector4f specular;
public final Vector4f position;
public final Vector3f spotDirection;
private final Vector4f rawPosition;
private int posMvGeneration = -1;
private final Vector3f rawSpotDirection;
private int spotDirLinearGeneration = -1;
public float spotExponent;
public float spotCutoff;
public float spotCosCutoff;
@@ -66,178 +71,221 @@ public class LightState implements ISettableState<LightState> {
this.linearAttenuation = linearAttenuation;
this.quadraticAttenuation = quadraticAttenuation;
this.rawPosition = new Vector4f(position);
this.position = position;
this.position.mul(GLStateManager.getModelViewMatrix());
this.posMvGeneration = GLStateManager.mvGeneration;
this.rawSpotDirection = new Vector3f(spotDirection);
this.spotDirection = spotDirection;
GLStateManager.getModelViewMatrix().get3x3(matrix3f);
this.spotDirection.mul(matrix3f);
this.spotDirLinearGeneration = GLStateManager.mvLinearGeneration;
}
public void setAmbient(FloatBuffer newBuffer) {
public boolean setAmbient(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) {
this.ambient.set(vector4f);
return true;
}
return false;
}
public void setAmbient(IntBuffer newBuffer) {
public boolean setAmbient(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) {
this.ambient.set(vector4f);
return true;
}
return false;
}
public void setDiffuse(FloatBuffer newBuffer) {
public boolean setDiffuse(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.diffuse.equals(vector4f)) {
this.diffuse.set(vector4f);
return true;
}
return false;
}
public void setDiffuse(IntBuffer newBuffer) {
public boolean setDiffuse(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.diffuse.equals(vector4f)) {
this.diffuse.set(vector4f);
return true;
}
return false;
}
public void setSpecular(FloatBuffer newBuffer) {
public boolean setSpecular(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.specular.equals(vector4f)) {
this.specular.set(vector4f);
return true;
}
return false;
}
public void setSpecular(IntBuffer newBuffer) {
public boolean setSpecular(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.specular.equals(vector4f)) {
this.specular.set(vector4f);
return true;
}
return false;
}
public void setPosition(FloatBuffer newBuffer) {
// We are bypassing cache everytime for position, because the necessary components to enable tracking the cache
// are big and probably more than just bypassing. We would need to track the modelview matrix it was transformed
// with and the untransformed coordinates in addition to the final transformation.
this.position.set(newBuffer);
this.position.mul(GLStateManager.getModelViewMatrix());
public boolean setPosition(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
return applyPosition(vector4f);
}
public void setPosition(IntBuffer newBuffer) {
public boolean setPosition(IntBuffer newBuffer) {
vector4i.set(newBuffer);
this.position.set((float) vector4i.x, (float) vector4i.y, (float) vector4i.z, (float) vector4i.w);
this.position.mul(GLStateManager.getModelViewMatrix());
vector4f.set((float) vector4i.x, (float) vector4i.y, (float) vector4i.z, (float) vector4i.w);
return applyPosition(vector4f);
}
public void setSpotDirection(FloatBuffer newBuffer) {
// We are bypassing cache everytime for spot direction, because the necessary components to enable tracking the cache
// are big and probably more than just bypassing. We would need to track the modelview matrix it was transformed
// with and the untransformed coordinates in addition to the final transformation.
GLStateManager.getModelViewMatrix().get3x3(matrix3f);
this.spotDirection.set(newBuffer);
this.spotDirection.mul(matrix3f);
private boolean applyPosition(Vector4f raw) {
final int mvGen = GLStateManager.mvGeneration;
if (GLStateManager.shouldBypassCache() || this.posMvGeneration != mvGen || !this.rawPosition.equals(raw)) {
this.rawPosition.set(raw);
this.posMvGeneration = mvGen;
this.position.set(raw);
this.position.mul(GLStateManager.getModelViewMatrix());
return true;
}
return false;
}
public void setSpotDirection(IntBuffer newBuffer) {
public boolean setSpotDirection(FloatBuffer newBuffer) {
vector3f.set(newBuffer);
return applySpotDirection(vector3f);
}
public boolean setSpotDirection(IntBuffer newBuffer) {
vector3i.set(newBuffer);
GLStateManager.getModelViewMatrix().get3x3(matrix3f);
this.spotDirection.set((float) vector3i.x, (float) vector3i.y, (float) vector3i.z);
this.spotDirection.mul(matrix3f);
vector3f.set((float) vector3i.x, (float) vector3i.y, (float) vector3i.z);
return applySpotDirection(vector3f);
}
public void setSpotExponent(FloatBuffer newBuffer) {
setSpotExponent(newBuffer.get());
private boolean applySpotDirection(Vector3f raw) {
final int mvLinearGen = GLStateManager.mvLinearGeneration;
if (GLStateManager.shouldBypassCache() || this.spotDirLinearGeneration != mvLinearGen || !this.rawSpotDirection.equals(raw)) {
this.rawSpotDirection.set(raw);
this.spotDirLinearGeneration = mvLinearGen;
GLStateManager.getModelViewMatrix().get3x3(matrix3f);
this.spotDirection.set(raw);
this.spotDirection.mul(matrix3f);
return true;
}
return false;
}
public void setSpotExponent(IntBuffer newBuffer) {
setSpotExponent((float) newBuffer.get());
public boolean setSpotExponent(FloatBuffer newBuffer) {
return setSpotExponent(newBuffer.get());
}
public void setSpotExponent(int i) {
setSpotExponent((float) i);
public boolean setSpotExponent(IntBuffer newBuffer) {
return setSpotExponent((float) newBuffer.get());
}
public void setSpotExponent(float f) {
public boolean setSpotExponent(int i) {
return setSpotExponent((float) i);
}
public boolean setSpotExponent(float f) {
if (GLStateManager.shouldBypassCache() || Float.compare(f, this.spotExponent) != 0) {
this.spotExponent = f;
return true;
}
return false;
}
public void setSpotCutoff(FloatBuffer newBuffer) {
setSpotCutoff(newBuffer.get());
public boolean setSpotCutoff(FloatBuffer newBuffer) {
return setSpotCutoff(newBuffer.get());
}
public void setSpotCutoff(IntBuffer newBuffer) {
setSpotCutoff((float) newBuffer.get());
public boolean setSpotCutoff(IntBuffer newBuffer) {
return setSpotCutoff((float) newBuffer.get());
}
public void setSpotCutoff(int i) {
setSpotCutoff((float) i);
public boolean setSpotCutoff(int i) {
return setSpotCutoff((float) i);
}
public void setSpotCutoff(float f) {
public boolean setSpotCutoff(float f) {
if (GLStateManager.shouldBypassCache() || Float.compare(f, this.spotCutoff) != 0) {
this.spotCutoff = f;
this.spotCosCutoff = (float) Math.cos(Math.toRadians(f));
return true;
}
return false;
}
public void setConstantAttenuation(FloatBuffer newBuffer) {
setConstantAttenuation(newBuffer.get());
public boolean setConstantAttenuation(FloatBuffer newBuffer) {
return setConstantAttenuation(newBuffer.get());
}
public void setConstantAttenuation(IntBuffer newBuffer) {
setConstantAttenuation((float) newBuffer.get());
public boolean setConstantAttenuation(IntBuffer newBuffer) {
return setConstantAttenuation((float) newBuffer.get());
}
public void setConstantAttenuation(int i) {
setConstantAttenuation((float) i);
public boolean setConstantAttenuation(int i) {
return setConstantAttenuation((float) i);
}
public void setConstantAttenuation(float f) {
public boolean setConstantAttenuation(float f) {
if (GLStateManager.shouldBypassCache() || Float.compare(f, this.constantAttenuation) != 0) {
this.constantAttenuation = f;
return true;
}
return false;
}
public void setLinearAttenuation(FloatBuffer newBuffer) {
setLinearAttenuation(newBuffer.get());
public boolean setLinearAttenuation(FloatBuffer newBuffer) {
return setLinearAttenuation(newBuffer.get());
}
public void setLinearAttenuation(IntBuffer newBuffer) {
setLinearAttenuation((float) newBuffer.get());
public boolean setLinearAttenuation(IntBuffer newBuffer) {
return setLinearAttenuation((float) newBuffer.get());
}
public void setLinearAttenuation(int i) {
setLinearAttenuation((float) i);
public boolean setLinearAttenuation(int i) {
return setLinearAttenuation((float) i);
}
public void setLinearAttenuation(float f) {
public boolean setLinearAttenuation(float f) {
if (GLStateManager.shouldBypassCache() || Float.compare(f, this.linearAttenuation) != 0) {
this.linearAttenuation = f;
return true;
}
return false;
}
public void setQuadraticAttenuation(FloatBuffer newBuffer) {
setQuadraticAttenuation(newBuffer.get());
public boolean setQuadraticAttenuation(FloatBuffer newBuffer) {
return setQuadraticAttenuation(newBuffer.get());
}
public void setQuadraticAttenuation(IntBuffer newBuffer) {
setQuadraticAttenuation((float) newBuffer.get());
public boolean setQuadraticAttenuation(IntBuffer newBuffer) {
return setQuadraticAttenuation((float) newBuffer.get());
}
public void setQuadraticAttenuation(int i) {
setQuadraticAttenuation((float) i);
public boolean setQuadraticAttenuation(int i) {
return setQuadraticAttenuation((float) i);
}
public void setQuadraticAttenuation(float f) {
public boolean setQuadraticAttenuation(float f) {
if (GLStateManager.shouldBypassCache() || Float.compare(f, this.quadraticAttenuation) != 0) {
this.quadraticAttenuation = f;
return true;
}
return false;
}
@Override
@@ -246,7 +294,11 @@ public class LightState implements ISettableState<LightState> {
this.diffuse.set(state.diffuse);
this.specular.set(state.specular);
this.position.set(state.position);
this.rawPosition.set(state.rawPosition);
this.posMvGeneration = state.posMvGeneration;
this.spotDirection.set(state.spotDirection);
this.rawSpotDirection.set(state.rawSpotDirection);
this.spotDirLinearGeneration = state.spotDirLinearGeneration;
this.spotExponent = state.spotExponent;
this.spotCutoff = state.spotCutoff;
this.spotCosCutoff = state.spotCosCutoff;
@@ -37,82 +37,100 @@ public class MaterialState implements ISettableState<MaterialState> {
colorIndexes = new Vector3f(0.0F, 1.0F, 1.0F);
}
public void setAmbient(FloatBuffer newBuffer) {
public boolean setAmbient(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) {
this.ambient.set(vector4f);
return true;
}
return false;
}
public void setAmbient(IntBuffer newBuffer) {
public boolean setAmbient(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) {
this.ambient.set(vector4f);
return true;
}
return false;
}
public void setDiffuse(FloatBuffer newBuffer) {
public boolean setDiffuse(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.diffuse.equals(vector4f)) {
this.diffuse.set(vector4f);
return true;
}
return false;
}
public void setDiffuse(IntBuffer newBuffer) {
public boolean setDiffuse(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.diffuse.equals(vector4f)) {
this.diffuse.set(vector4f);
return true;
}
return false;
}
public void setSpecular(FloatBuffer newBuffer) {
public boolean setSpecular(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.specular.equals(vector4f)) {
this.specular.set(vector4f);
return true;
}
return false;
}
public void setSpecular(IntBuffer newBuffer) {
public boolean setSpecular(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.specular.equals(vector4f)) {
this.specular.set(vector4f);
return true;
}
return false;
}
public void setEmission(FloatBuffer newBuffer) {
public boolean setEmission(FloatBuffer newBuffer) {
vector4f.set(newBuffer);
if (GLStateManager.shouldBypassCache() || !this.emission.equals(vector4f)) {
this.emission.set(vector4f);
return true;
}
return false;
}
public void setEmission(IntBuffer newBuffer) {
public boolean setEmission(IntBuffer newBuffer) {
vector4i.set(newBuffer);
vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w));
if (GLStateManager.shouldBypassCache() || !this.emission.equals(vector4f)) {
this.emission.set(vector4f);
return true;
}
return false;
}
public void setShininess(float val) {
public boolean setShininess(float val) {
if (GLStateManager.shouldBypassCache() || Float.compare(this.shininess, val) != 0) {
this.shininess = val;
return true;
}
return false;
}
// On the buffer versions of shininess, we need to call `get(0)` explicitly specifying the index
// otherwise calling `get()` advances the buffer position, and some mods may re-use the same buffer
// in subsequent calls, resulting in a BufferUnderflowException.
public void setShininess(FloatBuffer newBuffer) { setShininess(newBuffer.get(0)); }
public boolean setShininess(FloatBuffer newBuffer) { return setShininess(newBuffer.get(0)); }
public void setShininess(IntBuffer newBuffer) { setShininess((float) newBuffer.get(0)); }
public boolean setShininess(IntBuffer newBuffer) { return setShininess((float) newBuffer.get(0)); }
public void setShininess(int val) { setShininess((float) val);}
public boolean setShininess(int val) { return setShininess((float) val);}
public void setColorIndexes(FloatBuffer newBuffer) {
public boolean setColorIndexes(FloatBuffer newBuffer) {
// You are reading this correctly, nvidia drivers flip the y and z values in
// glMaterial specifically for GL_COLOR_INDEXES. Other drivers do not do this.
// This probably breaks things but who knows, GLSM is setup to track to what the
@@ -125,15 +143,19 @@ public class MaterialState implements ISettableState<MaterialState> {
if (GLStateManager.shouldBypassCache() || !this.colorIndexes.equals(vector3f)) {
this.colorIndexes.set(vector3f);
return true;
}
return false;
}
public void setColorIndexes(IntBuffer newBuffer) {
public boolean setColorIndexes(IntBuffer newBuffer) {
vector3i.set(newBuffer.get(0), newBuffer.get(2), newBuffer.get(1));
vector3f.set((float) vector3i.x, (float) vector3i.y, (float) vector3i.z);
if (GLStateManager.shouldBypassCache() || !this.colorIndexes.equals(vector3f)) {
this.colorIndexes.set(vector3f);
return true;
}
return false;
}
@Override
@@ -68,7 +68,7 @@ class CompatUniformManagerTest {
// Use program and upload — should not error
GL20.glUseProgram(program);
CompatUniformManager.onUseProgram(program);
CompatUniformManager.refreshCompatUniforms(program);
// Verify no GL errors
assertEquals(0, org.lwjgl.opengl.GL11.glGetError(), "GL error after uniform upload");
@@ -146,7 +146,7 @@ class CompatUniformManagerTest {
// Use program and upload — should not error
GL20.glUseProgram(program);
CompatUniformManager.onUseProgram(program);
CompatUniformManager.refreshCompatUniforms(program);
assertEquals(0, org.lwjgl.opengl.GL11.glGetError(), "GL error after fog uniform upload");
}
@@ -0,0 +1,40 @@
package com.gtnewhorizons.angelica.glsm;
import com.gtnewhorizons.angelica.glsm.hooks.GLSMInitConfig;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;
/**
* 3.3 core-profile context extension
*/
public class GLSMCoreExtension implements BeforeAllCallback, AfterAllCallback {
private boolean ownsDisplay = false;
@Override
public void beforeAll(ExtensionContext context) throws LWJGLException {
if (Display.isCreated()) return;
Display.setDisplayModeAndFullscreen(new DisplayMode(800, 600));
Display.setResizable(false);
Display.setFullscreen(false);
Display.create(
new PixelFormat().withDepthBits(24).withStencilBits(8),
new ContextAttribs(3, 3).withProfileCore(true).withForwardCompatible(true));
ownsDisplay = true;
GLStateManager.initialize(GLSMInitConfig.builder().displaySize(800, 600).build());
GLStateManager.setRunningSplash(false);
GLStateManager.markSplashComplete();
GLStateManager.BYPASS_CACHE = false;
}
@Override
public void afterAll(ExtensionContext context) {
if (ownsDisplay && Display.isCreated()) Display.destroy();
ownsDisplay = false;
}
}
@@ -0,0 +1,174 @@
package com.gtnewhorizons.angelica.glsm;
import org.joml.Vector4f;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.opengl.GL11;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(GLSMCoreExtension.class)
public class GLSM_DirtyTracking_UnitTest {
static final FloatBuffer f4b = ByteBuffer.allocateDirect(4 << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
static final FloatBuffer f3b = ByteBuffer.allocateDirect(3 << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
@BeforeEach
void pushTrackedState() {
GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT | GL11.GL_FOG_BIT | GL11.GL_CURRENT_BIT);
}
@AfterEach
void popTrackedState() {
GLStateManager.glPopAttrib();
}
@Test
void materialBumpsOnlyOnChange() {
newf4b(0.1F, 0.2F, 0.3F, 0.4F);
GLStateManager.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT, f4b);
final int gen = GLStateManager.lightingGeneration;
newf4b(0.1F, 0.2F, 0.3F, 0.4F);
GLStateManager.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT, f4b);
assertEquals(gen, GLStateManager.lightingGeneration, "identical material must not bump lightingGeneration");
newf4b(0.9F, 0.2F, 0.3F, 0.4F);
GLStateManager.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT, f4b);
assertEquals(gen + 1, GLStateManager.lightingGeneration, "changed material must bump lightingGeneration once");
}
@Test
void lightModelBumpsOnlyOnChange() {
newf4b(0.5F, 0.5F, 0.5F, 1.0F);
GLStateManager.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, f4b);
final int gen = GLStateManager.lightingGeneration;
newf4b(0.5F, 0.5F, 0.5F, 1.0F);
GLStateManager.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, f4b);
assertEquals(gen, GLStateManager.lightingGeneration, "identical light model ambient must not bump");
}
@Test
void lightColorBumpsOnlyOnChange() {
newf4b(0.25F, 0.5F, 0.75F, 1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, f4b);
final int gen = GLStateManager.lightingGeneration;
newf4b(0.25F, 0.5F, 0.75F, 1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, f4b);
assertEquals(gen, GLStateManager.lightingGeneration, "identical light diffuse must not bump");
}
@Test
void lightPositionDirtySkipsUnderSameModelview() {
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glTranslatef(1.0F, 2.0F, 3.0F);
newf4b(0.1F, 0.2F, 0.3F, 1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, f4b);
final int gen = GLStateManager.lightingGeneration;
newf4b(0.1F, 0.2F, 0.3F, 1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, f4b);
assertEquals(gen, GLStateManager.lightingGeneration, "same position under same modelview must not bump");
GLStateManager.glTranslatef(5.0F, 0.0F, 0.0F);
newf4b(0.1F, 0.2F, 0.3F, 1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, f4b);
assertEquals(gen + 1, GLStateManager.lightingGeneration, "same raw position under changed modelview must bump");
GLStateManager.glPopMatrix();
}
@Test
void translationDoesNotBumpLinearGeneration() {
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glPushMatrix();
final int mv = GLStateManager.mvGeneration;
final int lin = GLStateManager.mvLinearGeneration;
GLStateManager.glTranslatef(1.0F, 2.0F, 3.0F);
assertEquals(mv + 1, GLStateManager.mvGeneration, "translate must bump mvGeneration");
assertEquals(lin, GLStateManager.mvLinearGeneration, "translate must NOT bump mvLinearGeneration");
GLStateManager.glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
assertEquals(mv + 2, GLStateManager.mvGeneration, "rotate must bump mvGeneration");
assertEquals(lin + 1, GLStateManager.mvLinearGeneration, "rotate must bump mvLinearGeneration");
GLStateManager.glScalef(2.0F, 2.0F, 2.0F);
assertEquals(lin + 2, GLStateManager.mvLinearGeneration, "scale must bump mvLinearGeneration");
GLStateManager.glPopMatrix();
}
@Test
void fogBumpsOnlyOnChange() {
GLStateManager.glFogf(GL11.GL_FOG_DENSITY, 0.5F);
final int gen = GLStateManager.fragmentGeneration;
GLStateManager.glFogf(GL11.GL_FOG_DENSITY, 0.5F);
assertEquals(gen, GLStateManager.fragmentGeneration, "identical fog density must not bump fragmentGeneration");
GLStateManager.glFogf(GL11.GL_FOG_DENSITY, 0.7F);
assertEquals(gen + 1, GLStateManager.fragmentGeneration, "changed fog density must bump fragmentGeneration");
}
@Test
void spotDirectionDirtySkipsUnderTranslationOnly() {
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
newf3b(0.0F, 0.0F, -1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, f3b);
final int gen = GLStateManager.lightingGeneration;
GLStateManager.glTranslatef(5.0F, 0.0F, 0.0F);
newf3b(0.0F, 0.0F, -1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, f3b);
assertEquals(gen, GLStateManager.lightingGeneration, "spot direction under translation-only must not bump");
GLStateManager.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
newf3b(0.0F, 0.0F, -1.0F);
GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, f3b);
assertEquals(gen + 1, GLStateManager.lightingGeneration, "same raw spot direction under changed 3x3 must bump");
GLStateManager.glPopMatrix();
}
@Test
void colorMaterialFreezesLastColorOnDisable() {
GLStateManager.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE);
GLStateManager.glColor4f(1.0F, 0.0F, 0.0F, 1.0F); // enable-time color
GLStateManager.enableColorMaterial();
GLStateManager.glColor4f(0.0F, 0.0F, 1.0F, 1.0F); // last color while enabled
GLStateManager.disableColorMaterial();
final Vector4f diffuse = GLStateManager.getFrontMaterial().diffuse;
assertEquals(0.0F, diffuse.x, "diffuse must freeze at last color (blue), not enable-time red");
assertEquals(0.0F, diffuse.y, "green channel must be 0");
assertEquals(1.0F, diffuse.z, "blue channel must be 1");
}
static void newf4b(float x, float y, float z, float w) {
f4b.clear();
f4b.put(x).put(y).put(z).put(w);
f4b.flip();
}
static void newf3b(float x, float y, float z) {
f3b.clear();
f3b.put(x).put(y).put(z);
f3b.flip();
}
}
@@ -0,0 +1,63 @@
package com.gtnewhorizons.angelica.glsm.ffp;
import com.gtnewhorizons.angelica.glsm.GLSMCoreExtension;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@ExtendWith(GLSMCoreExtension.class)
class ProgramUniformTrackingTest {
private static Program buildProgram(boolean hasColor) {
final VertexKey vk = VertexKey.fromState(hasColor, false, false, false, 0);
final FragmentKey fk = FragmentKey.fromState();
return Program.create(vk, fk, VertexShaderGenerator.generate(vk), FragmentShaderGenerator.generate(fk), null);
}
@Test
void matrixTrackingIsPerProgram() {
final Uniforms uniforms = new Uniforms();
final Program a = buildProgram(true);
final Program b = buildProgram(false);
assertNotEquals(a.getProgramId(), b.getProgramId(), "distinct vertex keys must yield distinct programs");
try {
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glPushMatrix();
GLStateManager.glLoadIdentity();
GLStateManager.glTranslatef(1.0F, 2.0F, 3.0F);
final int genA = GLStateManager.mvGeneration;
GL20.glUseProgram(a.getProgramId());
uniforms.upload(a);
assertEquals(genA, a.uploadState.mvGen, "A records the mv generation it uploaded");
GLStateManager.glTranslatef(4.0F, 0.0F, 0.0F);
final int genB = GLStateManager.mvGeneration;
assertNotEquals(genA, genB);
GL20.glUseProgram(b.getProgramId());
uniforms.upload(b);
assertEquals(genB, b.uploadState.mvGen, "B records the new mv generation");
assertEquals(genA, a.uploadState.mvGen, "A is unchanged -- tracking is per-program, not global");
// Return to A with the modelview still at genB: A is stale -> re-uploads and catches up.
GL20.glUseProgram(a.getProgramId());
uniforms.upload(a);
assertEquals(genB, a.uploadState.mvGen, "A re-uploads on return and catches up to genB");
// Re-upload A with no change: gate (mvGeneration == A.mvGen) is satisfied -> skip.
uniforms.upload(a);
assertEquals(genB, a.uploadState.mvGen, "A already current -> skipped");
GLStateManager.glPopMatrix();
} finally {
GL20.glUseProgram(0);
a.destroy();
b.destroy();
}
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ pluginManagement {
}
plugins {
id("com.gtnewhorizons.gtnhsettingsconvention") version ("2.0.24")
id("com.gtnewhorizons.gtnhsettingsconvention") version ("2.0.25")
}
include("glsm")