Skip to content

Commit

Permalink
Begin
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Dec 12, 2023
1 parent d922d90 commit 039950c
Show file tree
Hide file tree
Showing 21 changed files with 376 additions and 377 deletions.
4 changes: 2 additions & 2 deletions buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@

public class Buildscript extends SimpleFabricProject {
static final boolean SODIUM = true;
static final boolean CUSTOM_SODIUM = false;
static final boolean CUSTOM_SODIUM = true;
static final String MC_VERSION = "1.20.4";
static final String customSodiumName = "sodium-fabric-mc23w45a-0.5.3git.86b69d2-dirty.jar";
static final String customSodiumName = "sodium-fabric-mc1.20.3-0.5.5+git.d01d1e8.jar";

private static final String[] SOURCE_SETS = new String[] {
"main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.Ints;
import me.jellysquid.mods.sodium.client.gl.shader.ShaderLoader;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ModelQuadFormat;
import net.coderbot.iris.gl.blending.AlphaTest;
import net.coderbot.iris.gl.blending.BlendModeOverride;
import net.coderbot.iris.gl.blending.BufferBlendOverride;
Expand Down Expand Up @@ -270,7 +270,7 @@ public SodiumTerrainPipeline(WorldRenderingPipeline parent, ProgramSet programSe
private static final Supplier<Optional<AlphaTest>> translucentDefault = () -> Optional.of(AlphaTest.ALWAYS);
private static final Supplier<Optional<AlphaTest>> shadowDefault = () -> Optional.of(AlphaTests.ONE_TENTH_ALPHA);

public void patchShaders(ChunkVertexType vertexType) {
public void patchShaders(ModelQuadFormat vertexType) {
ShaderAttributeInputs inputs = new ShaderAttributeInputs(true, true, false, true, true);

Optional<ProgramSource> terrainSolidSource = first(programSet.getGbuffersTerrainSolid(), programSet.getGbuffersTerrain(), programSet.getGbuffersTexturedLit(), programSet.getGbuffersTextured(), programSet.getGbuffersBasic());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,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 < 450) {
versionStatement.version = Version.GLSL45;
}
versionStatement.profile = Profile.CORE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static void transform(
CommonTransformer.transform(t, tree, root, parameters, false);

replaceMidTexCoord(t, tree, root, 1.0f / 65536.0f);
replaceMCEntity(t, tree, root);

root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix0, "mat4(1.0)");
root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix1, "iris_LightmapTextureMatrix");
Expand Down Expand Up @@ -70,11 +71,11 @@ public static void transform(
} else {
root.replaceReferenceExpressions(t, "gl_Color", "vec4(1.0)");
}
root.replaceReferenceExpressions(t, "at_tangent", "iris_Tangent");

if (parameters.type.glShaderType == ShaderType.VERTEX) {
if (parameters.inputs.hasNormal()) {
root.rename("gl_Normal", "iris_Normal");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "in vec3 iris_Normal;");
} else {
root.replaceReferenceExpressions(t, "gl_Normal", "vec3(0.0, 0.0, 1.0)");
}
Expand Down Expand Up @@ -110,8 +111,8 @@ public static void transform(
"uniform mat4 iris_ProjectionMatrix;",
"uniform mat4 iris_ModelViewMatrix;",
"uniform vec3 u_RegionOffset;",
// _draw_translation replaced with Chunks[_draw_id].offset.xyz
"vec4 getVertexPosition() { return vec4(_vert_position + u_RegionOffset + _get_draw_translation(_draw_id), 1.0); }");
// _draw_translation replaced with Chunks[_mesh_id].offset.xyz
"vec4 getVertexPosition() { return vec4(_vert_position + _get_draw_translation(_mesh_id), 1.0); }");
root.replaceReferenceExpressions(t, "gl_Vertex", "getVertexPosition()");

// inject here so that _vert_position is available to the above. (injections
Expand All @@ -135,33 +136,106 @@ public static void injectVertInit(
TranslationUnit tree,
Root root,
SodiumParameters parameters) {
String separateAo = BlockRenderingSettings.INSTANCE.shouldUseSeparateAo() ? "a_Color" : "vec4(a_Color.rgb * a_Color.a, 1.0)";
String separateAo = "vec4(((color.xyz >> (corner_index << 3)) & 0xFFu) / 255.0, unpackUnorm4x8(color.a).wzyx[corner_index])";
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_FUNCTIONS,
"""
struct IrisQuad {
uvec3 position_hi; // offset: 0 size: 16
uvec3 position_lo; // offset: 16 size: 16
uvec4 color; // offset: 32 size: 16
uvec2 tex_diffuse_hi; // offset: 48 size: 8
uvec2 tex_diffuse_lo; // offset: 56 size: 8
uvec2 light; // offset: 64 size: 8
uint material; // offset: 72 size: 4
uint mesh_id; // offset: 76 size: 4
uint midTexCoord; // offset: 80 size: 4
uint normal; // offset: 84 size: 4
uint tangent; // offset: 88 size: 4
uint blockInfo; // offset: 92 size: 4
// midBlock users mald for now
};
""",
"""
layout(std430, binding = 15) buffer QuadBuffer {
IrisQuad ssbo_Quads[];
};
""",
"""
vec3 _unpack_position(int quad_index, int corner_index) {
return vec3(
((ssbo_Quads[quad_index].position_lo >> (corner_index << 3)) & 0xFFu) |
(((ssbo_Quads[quad_index].position_hi >> (corner_index << 3)) & 0xFFu) << 8)
) * 0.00048828125 - 8.0;
}
""",
"""
vec2 _unpack_texcoord(int quad_index, int corner_index) {
return vec2(
((ssbo_Quads[quad_index].tex_diffuse_lo >> (corner_index << 3)) & 0xFFu) |
(((ssbo_Quads[quad_index].tex_diffuse_hi >> (corner_index << 3)) & 0xFFu) << 8)
) / 65535.0;
}
""",
"""
const vec2 CORNERS[4] = vec2[] (
vec2(0.0, 0.0),
vec2(1.0, 0.0),
vec2(1.0, 1.0),
vec2(0.0, 1.0)
);
""",
// translated from sodium's chunk_vertex.glsl
"vec3 _vert_position;",
"vec2 _vert_tex_diffuse_coord;",
"ivec2 _vert_tex_light_coord;",
"vec2 _vert_tex_light_coord;",
"vec4 _vert_color;",
"uint _draw_id;",
"vec3 iris_Normal;",
"vec2 iris_Entity;",
"vec2 mc_midTexCoord;",
"vec4 iris_Tangent;",
"uint _mesh_id;",
"const uint MATERIAL_USE_MIP_OFFSET = 0u;",
"float _material_mip_bias(uint material) {\n" +
" return ((material >> MATERIAL_USE_MIP_OFFSET) & 1u) != 0u ? 0.0f : -4.0f;\n" +
"}",
"void _vert_init() {" +
"_vert_position = (vec3(a_PosId.xyz) * 0.00048828125 + -8.0"
+ ");" +
"_vert_tex_diffuse_coord = (a_TexCoord * 1.52587891E-5);" +
"_vert_tex_light_coord = a_LightCoord;" +

"int quad_index = gl_VertexID >> 2;" +
"int corner_index = gl_VertexID & 3;" +
"int wtf = gl_VertexID % 4;" +
"vec2 v_RelCoord = CORNERS[corner_index];" +
"uvec4 color = ssbo_Quads[quad_index].color;" +
"vec4 light01 = unpackUnorm4x8(ssbo_Quads[quad_index].light[0]);" + // (c0.x, c0.y, c1.x, c1.y)
"vec4 light23 = unpackUnorm4x8(ssbo_Quads[quad_index].light[1]);" + // (c3.x, c3.y, c2.x, c2.y)

"""
vec2 uv = mix(
mix(light01.xy, light01.zw, v_RelCoord.x),
mix(light23.zw, light23.xy, v_RelCoord.x),
v_RelCoord.y);""" +

"_vert_tex_light_coord = clamp(uv, vec2(0.5 / 16.0), vec2(15.5 / 16.0));" +
"_vert_position = _unpack_position(quad_index, corner_index);" +
"_vert_tex_diffuse_coord = _unpack_texcoord(quad_index, corner_index);" +
"iris_Normal = unpackUnorm4x8(ssbo_Quads[quad_index].normal).xyz;" +
"iris_Tangent = unpackUnorm4x8(ssbo_Quads[quad_index].tangent);" +
"iris_Entity = vec2(ssbo_Quads[quad_index].blockInfo & 0xFFFF, ssbo_Quads[quad_index].blockInfo >> 16);" +
"mc_midTexCoord = unpackUnorm2x16(ssbo_Quads[quad_index].midTexCoord);" +
"_vert_color = " + separateAo + ";" +
"_draw_id = (a_PosId.w >> 8u) & 0xFFu; }",
"_mesh_id = ssbo_Quads[quad_index].mesh_id; }",

"uvec3 _get_relative_chunk_coord(uint pos) {\n" +
" // Packing scheme is defined by LocalSectionIndex\n" +
" return uvec3(pos) >> uvec3(5u, 0u, 2u) & uvec3(7u, 3u, 7u);\n" +
"}",
"vec3 _get_draw_translation(uint pos) {\n" +
" return _get_relative_chunk_coord(pos) * vec3(16.0f);\n" +
"}\n");
" return u_RegionOffset + _get_relative_chunk_coord(pos) * vec3(16.0f);\n" +
"}");
addIfNotExists(root, t, tree, "a_PosId", Type.U32VEC4, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "a_TexCoord", Type.F32VEC2, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "a_Color", Type.F32VEC4, StorageQualifier.StorageType.IN);
Expand Down Expand Up @@ -201,21 +275,72 @@ public static void replaceMidTexCoord(ASTParser t,
case BOOL:
return;
case FLOAT32:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "float iris_MidTex = (mc_midTexCoord.x * " + textureScale + ").x;");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "float iris_MidTex;");
tree.prependMainFunctionBody(t, "iris_MidTex = (mc_midTexCoord.x * " + textureScale + ").x;");
break;
case F32VEC2:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec2 iris_MidTex = (mc_midTexCoord.xy * " + textureScale + ").xy;");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec2 iris_MidTex;");
tree.prependMainFunctionBody(t, "iris_MidTex = (mc_midTexCoord.xy * " + textureScale + ").xy;");
break;
case F32VEC3:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec3 iris_MidTex = vec3(mc_midTexCoord.xy * " + textureScale + ", 0.0);");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec3 iris_MidTex;");
tree.prependMainFunctionBody(t, "iris_MidTex = vec3((mc_midTexCoord.xy * " + textureScale + ").xy, 0.0);");
break;
case F32VEC4:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec4 iris_MidTex = vec4(mc_midTexCoord.xy * " + textureScale + ", 0.0, 1.0);");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec4 iris_MidTex;");
tree.prependMainFunctionBody(t, "iris_MidTex = vec4((mc_midTexCoord.xy * " + textureScale + ").xy, 0.0, 1.0);");
break;
default:
throw new IllegalStateException("Somehow got a midTexCoord that is *above* 4 dimensions???");
}

tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "in vec2 mc_midTexCoord;");
}

public static void replaceMCEntity(ASTParser t,
TranslationUnit tree, Root root) {
Type dimension = Type.BOOL;
for (Identifier id : root.identifierIndex.get("mc_Entity")) {
TypeAndInitDeclaration initDeclaration = (TypeAndInitDeclaration) id.getAncestor(
2, 0, TypeAndInitDeclaration.class::isInstance);
if (initDeclaration == null) {
continue;
}
DeclarationExternalDeclaration declaration = (DeclarationExternalDeclaration) initDeclaration.getAncestor(
1, 0, DeclarationExternalDeclaration.class::isInstance);
if (declaration == null) {
continue;
}
if (initDeclaration.getType().getTypeSpecifier() instanceof BuiltinNumericTypeSpecifier numeric) {
dimension = numeric.type;

declaration.detachAndDelete();
initDeclaration.detachAndDelete();
id.detachAndDelete();
break;
}
}

switch (dimension) {
case BOOL:
return;
case FLOAT32:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "float mc_Entity;");
tree.prependMainFunctionBody(t, "mc_Entity = iris_Entity.x;");
break;
case F32VEC2:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec2 mc_Entity;");
tree.prependMainFunctionBody(t, "mc_Entity = iris_Entity.xy;");
break;
case F32VEC3:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec3 mc_Entity;");
tree.prependMainFunctionBody(t, "mc_Entity = vec3(iris_Entity.xy, 0.0);");
break;
case F32VEC4:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec4 mc_Entity;");
tree.prependMainFunctionBody(t, "mc_Entity = vec4(iris_Entity.xy, 0.0, 1.0);");
break;
default:
throw new IllegalStateException("Somehow got a midTexCoord that is *above* 4 dimensions???");
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/net/coderbot/iris/vertices/NormalHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,47 @@ public static void computeFaceNormal(@NotNull Vector3f saveTo, QuadView q) {
saveTo.set(normX, normY, normZ);
}

/**
* Computes the face normal of the given quad and saves it in the provided non-null vector.
*
* <p>Assumes counter-clockwise winding order, which is the norm.
* Expects convex quads with all points co-planar.
*/
public static int computeFaceNormalCompact(float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3) {
// final Direction nominalFace = q.nominalFace();
//
// if (GeometryHelper.isQuadParallelToFace(nominalFace, q)) {
// Vec3i vec = nominalFace.getVector();
// saveTo.set(vec.getX(), vec.getY(), vec.getZ());
// return;
// }


final float dx0 = x2 - x0;
final float dy0 = y2 - y0;
final float dz0 = z2 - z0;
final float dx1 = x3 - x1;
final float dy1 = y3 - y1;
final float dz1 = z3 - z1;

float normX = dy0 * dz1 - dz0 * dy1;
float normY = dz0 * dx1 - dx0 * dz1;
float normZ = dx0 * dy1 - dy0 * dx1;

float l = (float) Math.sqrt(normX * normX + normY * normY + normZ * normZ);

if (l != 0) {
normX /= l;
normY /= l;
normZ /= l;
}

return NormI8.pack(normX, normY, normZ, 0);
}

/**
* Computes the face normal of the given quad with a flipped order and saves it in the provided non-null vector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import me.jellysquid.mods.sodium.client.render.chunk.shader.ChunkShaderOptions;
import me.jellysquid.mods.sodium.client.render.chunk.terrain.DefaultTerrainRenderPasses;
import me.jellysquid.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ModelQuadFormat;
import net.coderbot.iris.Iris;
import net.coderbot.iris.compat.sodium.impl.IrisChunkShaderBindingPoints;
import net.coderbot.iris.gl.blending.AlphaTest;
Expand Down Expand Up @@ -139,7 +139,7 @@ private List<BufferBlendOverride> getBufferBlendOverride(IrisTerrainPass pass, S
}

@Nullable
private GlProgram<IrisChunkShaderInterface> createShader(IrisTerrainPass pass, SodiumTerrainPipeline pipeline, ChunkVertexType vertexType) {
private GlProgram<IrisChunkShaderInterface> createShader(IrisTerrainPass pass, SodiumTerrainPipeline pipeline, ModelQuadFormat vertexType) {
GlShader vertShader = createVertexShader(pass, pipeline);
GlShader geomShader = createGeometryShader(pass, pipeline);
GlShader fragShader = createFragmentShader(pass, pipeline);
Expand Down Expand Up @@ -225,7 +225,7 @@ private SodiumTerrainPipeline getSodiumTerrainPipeline() {
}
}

public void createShaders(SodiumTerrainPipeline pipeline, ChunkVertexType vertexType) {
public void createShaders(SodiumTerrainPipeline pipeline, ModelQuadFormat vertexType) {
if (pipeline != null) {
pipeline.patchShaders(vertexType);
for (IrisTerrainPass pass : IrisTerrainPass.values()) {
Expand All @@ -249,7 +249,7 @@ public void createShaders(SodiumTerrainPipeline pipeline, ChunkVertexType vertex
}

@Nullable
public GlProgram<IrisChunkShaderInterface> getProgramOverride(TerrainRenderPass pass, ChunkVertexType vertexType) {
public GlProgram<IrisChunkShaderInterface> getProgramOverride(TerrainRenderPass pass, ModelQuadFormat vertexType) {
if (versionCounterForSodiumShaderReload != Iris.getPipelineManager().getVersionCounterForSodiumShaderReload()) {
versionCounterForSodiumShaderReload = Iris.getPipelineManager().getVersionCounterForSodiumShaderReload();
deleteShaders();
Expand Down
Loading

0 comments on commit 039950c

Please sign in to comment.