mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-21 12:52:19 +00:00
Set entity, block entity and item IDs per vertex (#2265)
This commit is contained in:
1 parent
cfc6e12e00
commit
ab3bf17696
26 files changed
+720
-95
No files matched your search
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user