Skip to content

Commit

Permalink
Fix patching
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Dec 14, 2023
1 parent 3d69f1b commit 540a45f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static Map<PatchShaderType, String> patchVanilla(
ShaderAttributeInputs inputs,
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap) {
return transform(vertex, geometry, tessControl, tessEval, fragment,
new VanillaParameters(Patch.VANILLA, textureMap, alpha, isLines, hasChunkOffset, inputs, geometry != null));
new VanillaParameters(Patch.VANILLA, textureMap, alpha, isLines, hasChunkOffset, inputs, geometry != null, tessControl != null || tessEval != null));
}

public static Map<PatchShaderType, String> patchSodium(String vertex, String geometry, String tessControl, String tessEval, String fragment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AttributeParameters(Patch patch,
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap,
boolean hasGeometry,
InputAvailability inputs) {
super(patch, textureMap, hasGeometry);
super(patch, textureMap, hasGeometry, false);
this.inputs = inputs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

public abstract class GeometryInfoParameters extends Parameters {
public final boolean hasGeometry;
public final boolean hasTesselation;
// WARNING: adding new fields requires updating hashCode and equals methods!

public GeometryInfoParameters(Patch patch,
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap, boolean hasGeometry) {
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap, boolean hasGeometry, boolean hasTesselation) {
super(patch, textureMap);
this.hasGeometry = hasGeometry;
this.hasTesselation = hasTesselation;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public VanillaParameters(
Patch patch,
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap,
AlphaTest alpha, boolean isLines, boolean hasChunkOffset,
ShaderAttributeInputs inputs, boolean hasGeometry) {
super(patch, textureMap, hasGeometry);
ShaderAttributeInputs inputs, boolean hasGeometry, boolean hasTesselation) {
super(patch, textureMap, hasGeometry, hasTesselation);
this.alpha = alpha;
this.isLines = isLines;
this.hasChunkOffset = hasChunkOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ public static void patchOverlayColor(
// Some shader packs incorrectly ignore the alpha value, and assume that rgb
// will be zero if there is no hit flash, we try to emulate that here
"entityColor.rgb *= float(entityColor.a != 0.0);");
} else if (parameters.type.glShaderType == ShaderType.TESSELATION_CONTROL) {
// replace read references to grab the color from the first vertex.
root.replaceReferenceExpressions(t, "entityColor", "entityColor[gl_InvocationID]");

// TODO: this is passthrough behavior
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"out vec4 entityColorTCS[];",
"in vec4 entityColor[];",
"out vec4 iris_vertexColorTCS[];",
"in vec4 iris_vertexColor[];");
tree.prependMainFunctionBody(t,
"entityColorTCS[gl_InvocationID] = entityColor[gl_InvocationID];",
"iris_vertexColorTCS[gl_InvocationID] = iris_vertexColor[gl_InvocationID];");
} else if (parameters.type.glShaderType == ShaderType.TESSELATION_EVAL) {
// replace read references to grab the color from the first vertex.
root.replaceReferenceExpressions(t, "entityColor", "entityColorTCS[0]");

// TODO: this is passthrough behavior
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"out vec4 entityColorTES;",
"in vec4 entityColorTCS[];",
"out vec4 iris_vertexColorTES;",
"in vec4 iris_vertexColorTCS[];");
tree.prependMainFunctionBody(t,
"entityColorTES = entityColorTCS[0];",
"iris_vertexColorTES = iris_vertexColorTCS[0];");
} else if (parameters.type.glShaderType == ShaderType.GEOMETRY) {
// replace read references to grab the color from the first vertex.
root.replaceReferenceExpressions(t, "entityColor", "entityColor[0]");
Expand All @@ -180,6 +206,11 @@ public static void patchOverlayColor(
tree.prependMainFunctionBody(t,
"entityColorGS = entityColor[0];",
"iris_vertexColorGS = iris_vertexColor[0];");

if (parameters.hasTesselation) {
root.rename("iris_vertexColor", "iris_vertexColorTES");
root.rename("entityColor", "entityColorTES");
}
} else if (parameters.type.glShaderType == ShaderType.FRAGMENT) {
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"in vec4 entityColor;", "in vec4 iris_vertexColor;");
Expand All @@ -190,6 +221,9 @@ public static void patchOverlayColor(
if (parameters.hasGeometry) {
root.rename("entityColor", "entityColorGS");
root.rename("iris_vertexColor", "iris_vertexColorGS");
} else if (parameters.hasTesselation) {
root.rename("entityColor", "entityColorTES");
root.rename("iris_vertexColor", "iris_vertexColorTES");
}
}
}
Expand Down Expand Up @@ -238,20 +272,41 @@ public static void patchEntityId(
// stage.
tree.prependMainFunctionBody(t,
"iris_entityInfo = iris_Entity;");
} else if (parameters.type.glShaderType == ShaderType.TESSELATION_CONTROL) {
// TODO: this is passthrough behavior
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"flat out ivec3 iris_entityInfoTCS[];",
"flat in ivec3 iris_entityInfo[];");
root.replaceReferenceExpressions(t, "iris_entityInfo", "iris_EntityInfo[gl_InvocationID]");

tree.prependMainFunctionBody(t,
"iris_entityInfoTCS[gl_InvocationID] = iris_entityInfo[gl_InvocationID];");
} else if (parameters.type.glShaderType == ShaderType.TESSELATION_EVAL) {
// TODO: this is passthrough behavior
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"flat out ivec3 iris_entityInfoTES;",
"flat in ivec3 iris_entityInfoTCS[];");
tree.prependMainFunctionBody(t,
"iris_entityInfoTES = iris_entityInfoTCS[0];");

root.replaceReferenceExpressions(t, "iris_entityInfo", "iris_EntityInfoTCS[0]");

} else if (parameters.type.glShaderType == ShaderType.GEOMETRY) {
// TODO: this is passthrough behavior
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"flat out ivec3 iris_entityInfoGS;",
"flat in ivec3 iris_entityInfo[];");
"flat in ivec3 iris_entityInfo" + (parameters.hasTesselation ? "TES" : "") + "[];");
tree.prependMainFunctionBody(t,
"iris_entityInfoGS = iris_entityInfo[0];");
"iris_entityInfoGS = iris_entityInfo" + (parameters.hasTesselation ? "TES" : "") + "[0];");
} else if (parameters.type.glShaderType == ShaderType.FRAGMENT) {
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"flat in ivec3 iris_entityInfo;");

// Different output name to avoid a name collision in the geometry shader.
if (parameters.hasGeometry) {
root.rename("iris_entityInfo", "iris_EntityInfoGS");
} else if (parameters.hasTesselation) {
root.rename("iris_entityInfo", "iris_entityInfoTES");
}
}
}
Expand Down

0 comments on commit 540a45f

Please sign in to comment.