Skip to content

Commit

Permalink
extremely early testing
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 3, 2024
1 parent c6c7b94 commit 605a77a
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ public String toExpression(String alphaAccessor, String alphaThreshold, String i
indentation + "}\n";
}

public String toBoolean(String alphaAccessor, String alphaThreshold) {
if (function == AlphaTestFunction.ALWAYS) {
return "true";
}

if (this == AlphaTests.VERTEX_ALPHA) {
return alphaAccessor + " <= irisInt_vertexColor.a";
}

return alphaAccessor + " " + function.getExpression() + " " + alphaThreshold;
}

public String toBoolean(String alphaAccessor, String alphaThreshold, String vertexColorA) {
if (function == AlphaTestFunction.ALWAYS) {
return "true";
}

if (this == AlphaTests.VERTEX_ALPHA) {
// TODO IMS
return "false";
//return alphaAccessor + " <= " + vertexColorA;
}

return alphaAccessor + " " + function.getExpression() + " " + alphaThreshold;
}


@Override
public boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static boolean shouldOverrideShaders() {
@Redirect(method = "<init>*", require = 1, at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/shaders/Uniform;glBindAttribLocation(IILjava/lang/CharSequence;)V"))
public void iris$redirectBindAttributeLocation(int i, int j, CharSequence charSequence) {
if (((Object) this) instanceof ExtendedShader && ATTRIBUTE_LIST.contains(charSequence)) {
Uniform.glBindAttribLocation(i, j, "iris_" + charSequence);
Uniform.glBindAttribLocation(i, j, "irisInt_" + charSequence);
} else {
Uniform.glBindAttribLocation(i, j, charSequence);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import net.irisshaders.iris.pipeline.transform.transformer.DHTerrainTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.LayoutTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.SodiumCoreTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.SodiumFutureTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.SodiumTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.TextureTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.VanillaCoreTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.VanillaFutureTransformer;
import net.irisshaders.iris.pipeline.transform.transformer.VanillaTransformer;
import net.irisshaders.iris.shaderpack.texture.TextureStage;
import org.antlr.v4.runtime.Token;
Expand Down Expand Up @@ -119,6 +121,8 @@ public TranslationUnit parseTranslationUnit(Root rootInstance, String input) {
parameters.type = type;
Root root = tree.getRoot();

// TODO IMS
/*
// check for illegal references to internal Iris shader interfaces
internalPrefixes.stream()
.flatMap(root.getPrefixIdentifierIndex()::prefixQueryFlat)
Expand All @@ -128,7 +132,7 @@ public TranslationUnit parseTranslationUnit(Root rootInstance, String input) {
"Detected a potential reference to unstable and internal Iris shader interfaces (iris_, irisMain and moj_import). This isn't currently supported. Violation: "
+ id.getName() + ". See debugging.md for more information.");
});

*/
root.indexBuildSession(() -> {
VersionStatement versionStatement = tree.getVersionStatement();
if (versionStatement == null) {
Expand All @@ -144,8 +148,8 @@ public TranslationUnit parseTranslationUnit(Root rootInstance, String input) {

if (profile == Profile.CORE || version.number >= 150 && profile == null || isLine) {
// patch the version number to at least 330
if (version.number < 330) {
versionStatement.version = Version.GLSL33;
if (version.number < 400) {
versionStatement.version = Version.GLSL40;
}

switch (parameters.patch) {
Expand All @@ -168,8 +172,8 @@ public TranslationUnit parseTranslationUnit(Root rootInstance, String input) {
}
} else {
// patch the version number to at least 330
if (version.number < 330) {
versionStatement.version = Version.GLSL33;
if (version.number < 400) {
versionStatement.version = Version.GLSL40;
}
versionStatement.profile = Profile.CORE;

Expand All @@ -179,10 +183,10 @@ public TranslationUnit parseTranslationUnit(Root rootInstance, String input) {
break;
case SODIUM:
SodiumParameters sodiumParameters = (SodiumParameters) parameters;
SodiumTransformer.transform(transformer, tree, root, sodiumParameters);
SodiumFutureTransformer.transform(transformer, tree, root, sodiumParameters);
break;
case VANILLA:
VanillaTransformer.transform(transformer, tree, root, (VanillaParameters) parameters);
VanillaFutureTransformer.transform(transformer, tree, root, (VanillaParameters) parameters);
break;
case DH_TERRAIN:
DHTerrainTransformer.transform(transformer, tree, root, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void patchOverlayColor(
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"uniform sampler2D iris_overlay;",
"out vec4 entityColor;",
"out vec4 iris_vertexColor;",
"out vec4 irisInt_vertexColor;",
parameters.inputs.isIE() ? "uniform ivec2 iris_OverlayUV;" : "in ivec2 iris_UV1;");

// Create our own main function to wrap the existing main function, so that we
Expand All @@ -49,7 +49,7 @@ public static void patchOverlayColor(
tree.prependMainFunctionBody(t,
"vec4 overlayColor = texelFetch(iris_overlay, " + (parameters.inputs.isIE() ? "iris_OverlayUV" : "iris_UV1") + ", 0);",
"entityColor = vec4(overlayColor.rgb, 1.0 - overlayColor.a);",
"iris_vertexColor = iris_Color;",
"irisInt_vertexColor = iris_Color;",
// Workaround for a shader pack bug:
// https://github.com/IrisShaders/Iris/issues/1549
// Some shader packs incorrectly ignore the alpha value, and assume that rgb
Expand All @@ -63,11 +63,11 @@ public static void patchOverlayColor(
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"patch out vec4 entityColorTCS;",
"in vec4 entityColor[];",
"out vec4 iris_vertexColorTCS[];",
"in vec4 iris_vertexColor[];");
"out vec4 irisInt_vertexColorTCS[];",
"in vec4 irisInt_vertexColor[];");
tree.prependMainFunctionBody(t,
"entityColorTCS = entityColor[gl_InvocationID];",
"iris_vertexColorTCS[gl_InvocationID] = iris_vertexColor[gl_InvocationID];");
"irisInt_vertexColorTCS[gl_InvocationID] = irisInt_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");
Expand All @@ -76,11 +76,11 @@ public static void patchOverlayColor(
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"out vec4 entityColorTES;",
"patch in vec4 entityColorTCS;",
"out vec4 iris_vertexColorTES;",
"in vec4 iris_vertexColorTCS[];");
"out vec4 irisInt_vertexColorTES;",
"in vec4 irisInt_vertexColorTCS[];");
tree.prependMainFunctionBody(t,
"entityColorTES = entityColorTCS;",
"iris_vertexColorTES = iris_vertexColorTCS[0];");
"irisInt_vertexColorTES = irisInt_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 @@ -89,29 +89,29 @@ public static void patchOverlayColor(
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"out vec4 entityColorGS;",
"in vec4 entityColor[];",
"out vec4 iris_vertexColorGS;",
"in vec4 iris_vertexColor[];");
"out vec4 irisInt_vertexColorGS;",
"in vec4 irisInt_vertexColor[];");
tree.prependMainFunctionBody(t,
"entityColorGS = entityColor[0];",
"iris_vertexColorGS = iris_vertexColor[0];");
"irisInt_vertexColorGS = irisInt_vertexColor[0];");

if (parameters.hasTesselation) {
root.rename("iris_vertexColor", "iris_vertexColorTES");
root.rename("irisInt_vertexColor", "irisInt_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;");
"in vec4 entityColor;", "in vec4 irisInt_vertexColor;");

tree.prependMainFunctionBody(t, "float iris_vertexColorAlpha = iris_vertexColor.a;");
tree.prependMainFunctionBody(t, "float iris_vertexColorAlpha = irisInt_vertexColor.a;");

// Different output name to avoid a name collision in the geometry shader.
if (parameters.hasGeometry) {
root.rename("entityColor", "entityColorGS");
root.rename("iris_vertexColor", "iris_vertexColorGS");
root.rename("irisInt_vertexColor", "irisInt_vertexColorGS");
} else if (parameters.hasTesselation) {
root.rename("entityColor", "entityColorTES");
root.rename("iris_vertexColor", "iris_vertexColorTES");
root.rename("irisInt_vertexColor", "irisInt_vertexColorTES");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package net.irisshaders.iris.pipeline.transform.transformer;

import io.github.douira.glsl_transformer.ast.node.TranslationUnit;
import io.github.douira.glsl_transformer.ast.query.Root;
import io.github.douira.glsl_transformer.ast.transform.ASTInjectionPoint;
import io.github.douira.glsl_transformer.ast.transform.ASTParser;
import net.irisshaders.iris.gl.blending.AlphaTest;
import net.irisshaders.iris.pipeline.transform.PatchShaderType;

public class FutureTransformer {
public static void addBasicFunctions(ASTParser t,
TranslationUnit tree,
Root root, PatchShaderType shaderType, AlphaTest alphaTest, String vertexColorA) {
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "uniform sampler2D mainTexture;");
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_FUNCTIONS, """
vec4 iris_sampleMainTexture(vec2 coord) {
return texture(mainTexture, coord);
}
""",

"""
vec4 iris_sampleMainTextureOffset(vec2 coord, ivec2 offset) {
return textureOffset(mainTexture, coord, offset);
}
""",

"""
vec4 iris_sampleMainTextureLod(vec2 coord, float lod) {
return textureLod(mainTexture, coord, lod);
}
""",

"""
vec4 iris_sampleMainTextureGather(vec2 coord, int comp) {
return textureGather(mainTexture, coord, comp);
}
""",

"""
vec4 iris_sampleMainTextureGrad(vec2 coord, vec2 dtdx, vec2 dtdy) {
return textureGrad(mainTexture, coord, dtdx, dtdy);
}
""",

"""
vec4 iris_sampleMainTexture(vec2 coord, float bias) {
return texture(mainTexture, coord, bias);
}
""",

"""
vec4 iris_fetchMainTexture(ivec2 coord, int lod) {
return texelFetch(mainTexture, coord, lod);
}
""");

if (root.identifierIndex.has("iris_sampleLightmap")) {
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "uniform sampler2D lightmapTexture;");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_FUNCTIONS, """
vec4 iris_sampleLightmap(vec2 coord) {
return texture(lightmapTexture, coord);
}
""");
}

if (shaderType == PatchShaderType.FRAGMENT) {
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "uniform float iris_currentAlphaTest;");

tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_FUNCTIONS, """
void iris_modifyBase(inout vec2 texCoord, inout vec2 light, inout vec4 color) {
}
""",
// Optional
"""
void iris_modifyNormals(inout vec3 normal, inout vec3 tangent) {
}
""",
"bool iris_discardFragment(vec4 color) {\n" +
"return !(" + alphaTest.toBoolean("color.a", "iris_currentAlphaTest", vertexColorA) + ");\n" +

This comment has been minimized.

Copy link
@douira

douira Sep 3, 2024

Member

this should be a Template, inserting variable strings is slow.

"}",
"""
vec4 iris_modifyOverlay(vec4 color) {
return color;
}
""");
} else if (shaderType == PatchShaderType.VERTEX) {
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_FUNCTIONS, """
vec4 iris_getModelPosition() {
return irisInt_modelPosition;
}
""",
"""
vec4 iris_getViewPosition() {
return irisInt_viewPosition;
}
""",
"""
vec4 iris_getClipPosition() {
return irisInt_clipPosition;
}
""");
}
}

public static void replaceAndAdd(Root root, TranslationUnit tree, ASTParser t, String reference, String replacement, String injection) {
if (root.identifierIndex.has(reference)) {
root.replaceReferenceExpressions(t, reference, replacement);
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, injection);
}
}
}
Loading

0 comments on commit 605a77a

Please sign in to comment.