Set entity, block entity and item IDs per vertex (#2265)

This commit is contained in:
Eclipse-Sol authored and GitHub committed 2026-10-16 21:47:40 -07:00
1 parent cfc6e12e00
commit ab3bf17696
26 files changed
+720 -95

No files matched your search

@@ -38,6 +38,7 @@ public final class ImmediateExtendedAttribs implements ImmediateExtendedAttribHa
private static final int WANTS_MID = 1;
private static final int WANTS_TANGENT = 2;
private static final int WANTS_ENTITY_IDS = 4;
private static final int NEUTRAL_TANGENT = NormI8.pack(1.0f, 0.0f, 0.0f, 1.0f);
@@ -57,6 +58,7 @@ public final class ImmediateExtendedAttribs implements ImmediateExtendedAttribHa
wants = 0;
if (GLStateManager.glGetAttribLocation(program, "mc_midTexCoord") >= 0) wants |= WANTS_MID;
if (GLStateManager.glGetAttribLocation(program, "at_tangent") >= 0) wants |= WANTS_TANGENT;
if (GLStateManager.glGetAttribLocation(program, "iris_Entity") == LOC_ENTITY_IDS) wants |= WANTS_ENTITY_IDS;
programWantsExtCache.put(program, wants);
}
currentProgramWants = wants;
@@ -83,8 +85,7 @@ public final class ImmediateExtendedAttribs implements ImmediateExtendedAttribHa
@Override
public boolean wantsExtended() {
GLSMHooks.resolvePendingProgram();
return Iris.enabled && currentProgramWants != 0 && GLStateManager.getActiveProgram() != 0;
return programWants(WANTS_MID | WANTS_TANGENT);
}
@Override
@@ -92,6 +93,17 @@ public final class ImmediateExtendedAttribs implements ImmediateExtendedAttribHa
return (currentProgramWants & WANTS_TANGENT) != 0;
}
@Override
public boolean wantsEntityIds() {
return programWants(WANTS_ENTITY_IDS);
}
private static boolean programWants(int mask) {
if (!Iris.enabled) return false;
GLSMHooks.resolvePendingProgram();
return (currentProgramWants & mask) != 0 && GLStateManager.getActiveProgram() != 0;
}
@Override
public boolean wantsExtendedCapture() {
return Iris.enabled && Iris.getCurrentPack().isPresent();
@@ -26,6 +26,7 @@ import net.coderbot.iris.pipeline.DeferredWorldRenderingPipeline;
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.coderbot.iris.samplers.IrisSamplers;
import net.coderbot.iris.texture.pbr.PBRTextureManager;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.SystemTimeUniforms;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import org.lwjgl.opengl.GL11;
@@ -65,6 +66,9 @@ public class IrisGLSMBridge {
public static void installImmediateExtendedHandler() {
GLSMHooks.immediateExtendedHandler = new ImmediateExtendedAttribs();
final CapturedRenderingState state = CapturedRenderingState.INSTANCE;
state.setEntityIdsListener(() -> GLStateManager.setEntityIds(
state.getCurrentRenderedEntity(), state.getCurrentRenderedBlockEntity(), state.getCurrentRenderedItem()));
}
public static void installPostTransformHook() {
@@ -16,6 +16,7 @@ import com.gtnewhorizons.angelica.glsm.ffp.CubeParams;
import com.gtnewhorizons.angelica.glsm.ffp.InstancedAttribs;
import com.gtnewhorizons.angelica.glsm.hooks.GLSMConfig;
import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
import com.gtnewhorizons.angelica.glsm.streaming.TessellatorEntityIds;
import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer;
import com.gtnewhorizons.angelica.glsm.states.AlphaState;
import com.gtnewhorizons.angelica.glsm.states.BlendState;
@@ -652,6 +653,7 @@ public final class ModelPartBatcher {
private TessellatorRoute routeTessellatorDraw(Tessellator tess) {
if (!active || mode != Mode.BLOCK_ENTITIES || !GLStateManager.isMainThread() || !BatchEligibility.insideRenderer()) return TessellatorRoute.DRAW_NOW;
if (!tess.isDrawing || tess.drawMode != GL11.GL_QUADS || tess.vertexCount == 0 || (tess.vertexCount & 3) != 0) return TessellatorRoute.DRAW_NOW;
if (shaderPipeline != null && TessellatorEntityIds.hasIdChanges(tess)) return TessellatorRoute.DRAW_NOW;
if (DisplayListManager.isRecording() || !GLStateManager.glIsEnabled(GL11.GL_DEPTH_TEST) || !unitTexIdentity()) return TessellatorRoute.DRAW_NOW;
if (GLStateManager.getActiveProgram() != (shaderPipeline != null ? shaderPipeline.getActivePassProgramId() : 0)) return TessellatorRoute.DRAW_NOW;
final TesrMaterial material = EntityMaterials.fromCurrentState(true);
@@ -5,6 +5,7 @@ package net.coderbot.iris.gl.shader;
import com.gtnewhorizons.angelica.glsm.GLDebug;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import com.gtnewhorizons.angelica.glsm.RenderSystem;
import com.gtnewhorizons.angelica.glsm.ffp.InstancedAttribs;
import com.gtnewhorizons.angelica.glsm.hooks.ImmediateExtendedAttribHandler;
import com.gtnewhorizons.angelica.glsm.shader.ProgramBinaryCache;
import org.apache.logging.log4j.LogManager;
@@ -24,8 +25,8 @@ public class ProgramCreator {
public static final int AT_MIDBLOCK = ImmediateExtendedAttribHandler.LOC_MIDBLOCK;
// TODO: This is *really* hardcoded, we need to refactor this to support external calls to glBindAttribLocation
private static final String[] ATTRIBUTE_NAMES = { "iris_Entity", "mc_Entity", "mc_midTexCoord", "iris_CubeTangent", "at_tangent", "at_midBlock" };
private static final int[] ATTRIBUTE_LOCATIONS = { MC_ENTITY, MC_ENTITY, MC_MID_TEX_COORD, AT_TANGENT, AT_TANGENT, AT_MIDBLOCK };
private static final String[] ATTRIBUTE_NAMES = { "iris_InstEntity", "mc_Entity", "mc_midTexCoord", "iris_CubeTangent", "at_tangent", "at_midBlock" };
private static final int[] ATTRIBUTE_LOCATIONS = { InstancedAttribs.LOC_ENTITY, MC_ENTITY, MC_MID_TEX_COORD, AT_TANGENT, AT_TANGENT, AT_MIDBLOCK };
@Nullable
public static ProgramBinaryCache.Key cacheKey() {
@@ -1,24 +1,21 @@
package net.coderbot.iris.pipeline.transform;
import com.gtnewhorizons.angelica.glsm.ffp.InstancedAttribs;
import com.gtnewhorizons.angelica.glsm.hooks.ImmediateExtendedAttribHandler;
import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex;
import com.gtnewhorizons.angelica.glsm.shader.ShaderType;
import net.coderbot.iris.pipeline.transform.parameter.AttributeParameters;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.ParserRuleContext;
import org.taumc.glsl.Transformer;
import org.taumc.glsl.grammar.GLSLParser;
class EntityPatcher {
private static final String[] ID_NAMES = { "entityId", "blockEntityId", "currentRenderedItemId" };
private static final String[] ID_COMPONENTS = { ".x", ".y", ".z" };
public static void patchEntityId(Transformer transformer, AttributeParameters parameters) {
if (!hasInstanceAttributes(parameters)) {
injectUniformIfUndeclared(transformer, "entityId", "uniform int entityId;");
injectUniformIfUndeclared(transformer, "blockEntityId", "uniform int blockEntityId;");
injectUniformIfUndeclared(transformer, "currentRenderedItemId", "uniform int currentRenderedItemId;");
return;
}
removeIfDeclared(transformer, "entityId");
removeIfDeclared(transformer, "blockEntityId");
removeIfDeclared(transformer, "currentRenderedItemId");
final String geometryIn = parameters.hasTesselation ? "iris_entityInfoTES" : "iris_entityInfo";
final String info = switch (parameters.type) {
case GEOMETRY -> geometryIn + "[0]";
@@ -26,17 +23,37 @@ class EntityPatcher {
case TESSELATION_EVAL -> "iris_entityInfoTCS[0]";
default -> "iris_entityInfo";
};
transformer.replaceExpression("entityId", info + ".x", GLSLParser::postfix_expression);
transformer.replaceExpression("blockEntityId", info + ".y", GLSLParser::postfix_expression);
transformer.replaceExpression("currentRenderedItemId", info + ".z", GLSLParser::postfix_expression);
final StringBuilder assignments = new StringBuilder();
for (int i = 0; i < ID_NAMES.length; i++) {
final String name = ID_NAMES[i];
if (!transformer.containsCall(name)) continue;
final GLSLParser.Typeless_declarationContext global = topLevelDeclarator(transformer, name);
if (global != null) {
if (!isUniform(global)) continue;
// removeVariable takes the last declaration it finds by name, which can be a pack local
final String removed = "iris_removed_" + name;
((CommonToken) global.IDENTIFIER().getSymbol()).setText(removed);
transformer.removeVariable(removed);
}
transformer.injectVariable("int " + name + ";");
assignments.append(name).append(" = ").append(info).append(ID_COMPONENTS[i]).append("; ");
}
if (parameters.type != ShaderType.VERTEX && assignments.length() > 0) {
transformer.prependMain("{ " + assignments + "}");
}
switch (parameters.type) {
case VERTEX -> {
final boolean instanced = hasInstanceAttributes(parameters);
final String input = instanced ? "iris_InstEntity" : "iris_Entity";
final int location = instanced ? InstancedAttribs.LOC_ENTITY : ImmediateExtendedAttribHandler.LOC_ENTITY_IDS;
removeIfDeclared(transformer, "iris_Entity");
transformer.injectVariable("layout(location = " + InstancedAttribs.LOC_ENTITY + ") in uvec4 iris_Entity;");
removeIfDeclared(transformer, "iris_InstEntity");
transformer.injectVariable("layout(location = " + location + ") in vec4 " + input + ";");
transformer.injectVariable("flat out ivec3 iris_entityInfo;");
transformer.prependMain("iris_entityInfo = ivec3(iris_Entity.x == 65535u ? -1 : int(iris_Entity.x), "
+ "iris_Entity.y == 65535u ? -1 : int(iris_Entity.y), iris_Entity.z == 65535u ? -1 : int(iris_Entity.z));");
transformer.prependMain("{ ivec3 iris_ids = ivec3(round(" + input + ".xyz * 65535.0)); "
+ "iris_entityInfo = ivec3(iris_ids.x == 65535 ? -1 : iris_ids.x, iris_ids.y == 65535 ? -1 : iris_ids.y, "
+ "iris_ids.z == 65535 ? -1 : iris_ids.z); " + assignments + "}");
}
case TESSELATION_CONTROL -> {
transformer.injectVariable("flat out ivec3 iris_entityInfoTCS[];");
@@ -113,6 +130,27 @@ class EntityPatcher {
}
}
private static GLSLParser.Typeless_declarationContext topLevelDeclarator(Transformer transformer, String name) {
final GLSLParser.Typeless_declarationContext[] found = new GLSLParser.Typeless_declarationContext[1];
transformer.mutateTree(root -> {
for (GLSLParser.External_declarationContext external : root.external_declaration()) {
final GLSLParser.DeclarationContext declaration = external.declaration();
if (declaration == null || declaration.init_declarator_list() == null) continue;
for (GLSLParser.Typeless_declarationContext declarator : ShaderIndex.declarators(declaration.init_declarator_list())) {
if (declarator.IDENTIFIER().getText().equals(name)) found[0] = declarator;
}
}
});
return found[0];
}
private static boolean isUniform(GLSLParser.Typeless_declarationContext declarator) {
final ParserRuleContext parent = declarator.getParent();
final GLSLParser.Init_declarator_listContext list = (GLSLParser.Init_declarator_listContext)
(parent instanceof GLSLParser.Single_declarationContext ? parent.getParent() : parent);
return ShaderIndex.hasStorageQualifier(list.single_declaration().fully_specified_type().type_qualifier(), "uniform");
}
private static boolean hasInstanceAttributes(AttributeParameters parameters) {
return parameters.instancing.hasInstanceHead();
}
@@ -35,6 +35,7 @@ public class CapturedRenderingState {
@Getter
private int currentRenderedItem = -1;
private Runnable itemIdListener = null;
private Runnable entityIdsListener = null;
private final IntArrayList itemStack = new IntArrayList();
private final IntArrayList entityStack = new IntArrayList();
private final IntArrayList blockEntityStack = new IntArrayList();
@@ -88,15 +89,25 @@ public class CapturedRenderingState {
if (this.blockEntityIdListener != null) {
this.blockEntityIdListener.run();
}
notifyEntityIdsChanged();
}
private void setCurrentEntity(int entity) {
if (currentRenderedEntity == entity) return;
this.currentRenderedEntity = entity;
if (this.entityIdListener != null) {
this.entityIdListener.run();
private void applyCurrentEntityAndItem(int entity, int item) {
final boolean entityChanged = currentRenderedEntity != entity;
final boolean itemChanged = currentRenderedItem != item;
if (entityChanged) {
this.currentRenderedEntity = entity;
if (this.entityIdListener != null) {
this.entityIdListener.run();
}
}
if (itemChanged) {
this.currentRenderedItem = item;
if (this.itemIdListener != null) {
this.itemIdListener.run();
}
}
if (entityChanged || itemChanged) notifyEntityIdsChanged();
}
/**
@@ -104,15 +115,13 @@ public class CapturedRenderingState {
*/
public void setCurrentEntityAndItem(int entity, int item) {
IrisDisplayListState.recordEntityAndItem(entity, item);
setCurrentEntity(entity);
applyCurrentRenderedItem(item);
applyCurrentEntityAndItem(entity, item);
}
public void setCurrentNamedEntity(NamespacedId entity) {
IrisDisplayListState.recordNamedEntity(entity);
final var ids = BlockRenderingSettings.INSTANCE.getEntityIds();
setCurrentEntity(ids == null ? -1 : ids.applyAsInt(entity));
applyCurrentRenderedItem(0);
applyCurrentEntityAndItem(ids == null ? -1 : ids.applyAsInt(entity), 0);
}
public void pushCurrentEntityAndItem() {
@@ -123,15 +132,13 @@ public class CapturedRenderingState {
public void setCurrentRenderedEntity(Entity entity) {
IrisDisplayListState.recordEntity(entity);
setCurrentEntity(EntityIdHelper.getEntityId(entity));
applyCurrentRenderedItem(0);
applyCurrentEntityAndItem(EntityIdHelper.getEntityId(entity), 0);
}
public void popCurrentEntityAndItem() {
IrisDisplayListState.recordEntityScope(false);
// Restore the playback caller, never the IDs seen while compiling the list.
setCurrentEntity(entityStack.popInt());
applyCurrentRenderedItem(itemStack.popInt());
applyCurrentEntityAndItem(entityStack.popInt(), itemStack.popInt());
}
public void setCurrentEntityColor(float r, float g, float b, float a) {
@@ -209,6 +216,18 @@ public class CapturedRenderingState {
if (this.itemIdListener != null) {
this.itemIdListener.run();
}
notifyEntityIdsChanged();
}
public void setEntityIdsListener(Runnable listener) {
this.entityIdsListener = listener;
notifyEntityIdsChanged();
}
private void notifyEntityIdsChanged() {
if (this.entityIdsListener != null) {
this.entityIdsListener.run();
}
}
public ValueUpdateNotifier getEntityIdNotifier() {