Skip to content

Commit

Permalink
Sodium 0.5.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Aug 13, 2023
1 parent 5cfec7b commit a737a19
Show file tree
Hide file tree
Showing 32 changed files with 423 additions and 546 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.1";
static final String customSodiumName = "sodium-fabric-mc1.20.1-0.5.0-rc2.jar";
static final String customSodiumName = "sodium-fabric-mc1.20.1-0.5.1-beta1.jar";

private static final String[] SOURCE_SETS = new String[] {
"main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
float x = buffer.getFloat(posIndex);
float y = buffer.getFloat(posIndex + 4);
float z = buffer.getFloat(posIndex + 8);
this.putInt(0, ExtendedDataHelper.computeMidBlock(x, y, z, currentLocalPosX, currentLocalPosY, currentLocalPosZ));
this.putInt(0, ExtendedDataHelper.computeMidBlock(x, y, z, currentLocalPosX, currentLocalPosY, currentLocalPosZ, 1));
this.nextElement();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public void patchShaders(ChunkVertexType vertexType) {
sources.getVertexSource().orElse(null),
sources.getGeometrySource().orElse(null),
sources.getFragmentSource().orElse(null),
AlphaTest.ALWAYS, inputs,
vertexType.getPositionScale(), vertexType.getPositionOffset(), vertexType.getTextureScale(), parent.getTextureMap());
AlphaTest.ALWAYS, inputs, parent.getTextureMap());
terrainSolidVertex = Optional.ofNullable(transformed.get(PatchShaderType.VERTEX));
terrainSolidGeometry = Optional.ofNullable(transformed.get(PatchShaderType.GEOMETRY));
terrainSolidFragment = Optional.ofNullable(transformed.get(PatchShaderType.FRAGMENT));
Expand Down Expand Up @@ -180,8 +179,7 @@ public void patchShaders(ChunkVertexType vertexType) {
sources.getVertexSource().orElse(null),
sources.getGeometrySource().orElse(null),
sources.getFragmentSource().orElse(null),
terrainCutoutAlpha.orElse(AlphaTests.ONE_TENTH_ALPHA), inputs,
vertexType.getPositionScale(), vertexType.getPositionOffset(), vertexType.getTextureScale(), parent.getTextureMap());
terrainCutoutAlpha.orElse(AlphaTests.ONE_TENTH_ALPHA), inputs, parent.getTextureMap());
terrainCutoutVertex = Optional.ofNullable(transformed.get(PatchShaderType.VERTEX));
terrainCutoutGeometry = Optional.ofNullable(transformed.get(PatchShaderType.GEOMETRY));
terrainCutoutFragment = Optional.ofNullable(transformed.get(PatchShaderType.FRAGMENT));
Expand Down Expand Up @@ -213,8 +211,7 @@ public void patchShaders(ChunkVertexType vertexType) {
sources.getVertexSource().orElse(null),
sources.getGeometrySource().orElse(null),
sources.getFragmentSource().orElse(null),
translucentAlpha.orElse(AlphaTest.ALWAYS), inputs,
vertexType.getPositionScale(), vertexType.getPositionOffset(), vertexType.getTextureScale(), parent.getTextureMap());
translucentAlpha.orElse(AlphaTest.ALWAYS), inputs, parent.getTextureMap());
translucentVertex = Optional.ofNullable(transformed.get(PatchShaderType.VERTEX));
translucentGeometry = Optional.ofNullable(transformed.get(PatchShaderType.GEOMETRY));
translucentFragment = Optional.ofNullable(transformed.get(PatchShaderType.FRAGMENT));
Expand Down Expand Up @@ -245,15 +242,13 @@ public void patchShaders(ChunkVertexType vertexType) {
sources.getVertexSource().orElse(null),
sources.getGeometrySource().orElse(null),
sources.getFragmentSource().orElse(null),
AlphaTest.ALWAYS, inputs,
vertexType.getPositionScale(), vertexType.getPositionOffset(), vertexType.getTextureScale(), parent.getTextureMap());
AlphaTest.ALWAYS, inputs, parent.getTextureMap());
Map<PatchShaderType, String> transformedCutout = TransformPatcher.patchSodium(
sources.getName(),
sources.getVertexSource().orElse(null),
sources.getGeometrySource().orElse(null),
sources.getFragmentSource().orElse(null),
shadowAlpha.get(), inputs,
vertexType.getPositionScale(), vertexType.getPositionOffset(), vertexType.getTextureScale(), parent.getTextureMap());
shadowAlpha.get(), inputs, parent.getTextureMap());
shadowVertex = Optional.ofNullable(transformed.get(PatchShaderType.VERTEX));
shadowGeometry = Optional.ofNullable(transformed.get(PatchShaderType.GEOMETRY));
shadowCutoutFragment = Optional.ofNullable(transformedCutout.get(PatchShaderType.FRAGMENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,9 @@ public static Map<PatchShaderType, String> patchVanilla(

public static Map<PatchShaderType, String> patchSodium(String name, String vertex, String geometry, String fragment,
AlphaTest alpha, ShaderAttributeInputs inputs,
float positionScale, float positionOffset, float textureScale,
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap) {
return transform(name, vertex, geometry, fragment,
new SodiumParameters(Patch.SODIUM, textureMap, alpha, inputs, positionScale, positionOffset,
textureScale));
new SodiumParameters(Patch.SODIUM, textureMap, alpha, inputs));
}

public static Map<PatchShaderType, String> patchComposite(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

public class SodiumParameters extends Parameters {
public final ShaderAttributeInputs inputs;
public final float positionScale;
public final float positionOffset;
public final float textureScale;
// WARNING: adding new fields requires updating hashCode and equals methods!

// DO NOT include this field in hashCode or equals, it's mutable!
Expand All @@ -22,13 +19,9 @@ public class SodiumParameters extends Parameters {
public SodiumParameters(Patch patch,
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap,
AlphaTest alpha,
ShaderAttributeInputs inputs,
float positionScale, float positionOffset, float textureScale) {
ShaderAttributeInputs inputs) {
super(patch, textureMap);
this.inputs = inputs;
this.positionScale = positionScale;
this.positionOffset = positionOffset;
this.textureScale = textureScale;

this.alpha = alpha;
}
Expand All @@ -47,9 +40,6 @@ public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((inputs == null) ? 0 : inputs.hashCode());
result = prime * result + Float.floatToIntBits(positionScale);
result = prime * result + Float.floatToIntBits(positionOffset);
result = prime * result + Float.floatToIntBits(textureScale);
result = prime * result + ((alpha == null) ? 0 : alpha.hashCode());
return result;
}
Expand All @@ -68,12 +58,6 @@ public boolean equals(Object obj) {
return false;
} else if (!inputs.equals(other.inputs))
return false;
if (Float.floatToIntBits(positionScale) != Float.floatToIntBits(other.positionScale))
return false;
if (Float.floatToIntBits(positionOffset) != Float.floatToIntBits(other.positionOffset))
return false;
if (Float.floatToIntBits(textureScale) != Float.floatToIntBits(other.textureScale))
return false;
if (alpha == null) {
if (other.alpha != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public static void transform(
SodiumParameters parameters) {
CommonTransformer.transform(t, tree, root, parameters, false);

replaceMidTexCoord(t, tree, root, parameters.textureScale);
replaceMidTexCoord(t, tree, root, 1.0f / 65536.0f);

root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix0, "mat4(1.0)");
root.replaceReferenceExpressions(t, "at_midBlock", "iris_midBlock.xyz");
root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix1, "iris_LightmapTextureMatrix");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_FUNCTIONS, "uniform mat4 iris_LightmapTextureMatrix;");
root.rename("gl_ProjectionMatrix", "iris_ProjectionMatrix");
Expand All @@ -42,15 +43,15 @@ public static void transform(

if (parameters.inputs.hasTex()) {
root.replaceReferenceExpressions(t, "gl_MultiTexCoord0",
"vec4(_vert_tex_diffuse_coord, 0.0, 1.0)");
"vec4(_vert_tex_coord, 0.0, 1.0)");
} else {
root.replaceReferenceExpressions(t, "gl_MultiTexCoord0",
"vec4(0.0, 0.0, 0.0, 1.0)");
}

if (parameters.inputs.hasLight()) {
root.replaceReferenceExpressions(t, "gl_MultiTexCoord1",
"vec4(_vert_tex_light_coord, 0.0, 1.0)");
"vec4(_vert_light, 0.0, 1.0)");
} else {
root.replaceReferenceExpressions(t, "gl_MultiTexCoord1",
"vec4(240.0, 240.0, 0.0, 1.0)");
Expand All @@ -66,15 +67,17 @@ public static void transform(

if (parameters.inputs.hasColor()) {
// TODO: Handle the fragment shader here
root.rename("gl_Color", "_vert_color");
root.replaceReferenceExpressions(t, "gl_Color", "vec4(_vert_color, " + (BlockRenderingSettings.INSTANCE.shouldUseSeparateAo() ? "iris_midBlock.w" : "1.0") + ")");
addIfNotExists(root, t, tree, "iris_midBlock", Type.F32VEC4, StorageQualifier.StorageType.IN);

} else {
root.replaceReferenceExpressions(t, "gl_Color", "vec4(1.0)");
}

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;");
root.replaceReferenceExpressions(t, "gl_Normal", "iris_Normal");
addIfNotExists(root, t, tree, "iris_Normal", Type.F32VEC3, StorageQualifier.StorageType.IN);
} else {
root.replaceReferenceExpressions(t, "gl_Normal", "vec3(0.0, 0.0, 1.0)");
}
Expand Down Expand Up @@ -111,7 +114,7 @@ public static void transform(
"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); }");
"vec4 getVertexPosition() { return vec4(_vert_position + u_RegionOffset + _get_draw_translation(_vert_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,37 +138,54 @@ 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)";
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_FUNCTIONS,
// translated from sodium's chunk_vertex.glsl
"in uvec4 in_VertexData;",
"vec3 _vert_position;",
"vec2 _vert_tex_diffuse_coord;",
"ivec2 _vert_tex_light_coord;",
"vec4 _vert_color;",
"uint _draw_id;",
"vec2 _vert_tex_coord;",
"vec3 _vert_color;",
"vec2 _vert_light;",
"uint _vert_material;",
"uint _vert_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) * " + parameters.positionScale + " + "
+ parameters.positionOffset + ");" +
"_vert_tex_diffuse_coord = (a_TexCoord * " + parameters.textureScale + ");" +
"_vert_tex_light_coord = a_LightCoord;" +
"_vert_color = " + separateAo + ";" +
"_draw_id = (a_PosId.w >> 8u) & 0xFFu; }",

"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" +
"}",
"""
void _vert_init() {
// Vertex Position
uvec3 packed_position = uvec3(
(in_VertexData[0] >> 0) & 0xFFFFu,
(in_VertexData[0] >> 16) & 0xFFFFu,
(in_VertexData[1] >> 0) & 0xFFFFu
);
_vert_position = (vec3(packed_position) * (32.0 / 65536.0)) - 8.0;
// Vertex Material
_vert_material = (in_VertexData[1] >> 16) & 0xFFu;
// Vertex Mesh ID
_vert_mesh_id = (in_VertexData[1] >> 24) & 0xFFu;
// Vertex Color
uvec3 packed_color = (uvec3(in_VertexData[2]) >> uvec3(0, 8, 16)) & uvec3(0xFFu);
_vert_color = vec3(packed_color) * (1.0 / 255.0);
// Vertex Light
uvec2 packed_light = (uvec2(in_VertexData[2]) >> uvec2(24, 28)) & uvec2(0xFu);
_vert_light = vec2(packed_light) * 16.0;
// Vertex Texture Coords
uvec2 packed_tex_coord = (uvec2(in_VertexData[3]) >> uvec2(0, 16)) & uvec2(0xFFFFu);
_vert_tex_coord = vec2(packed_tex_coord) * (1.0 / 65536.0);
}""",
"vec3 _get_draw_translation(uint pos) {\n" +
" return _get_relative_chunk_coord(pos) * vec3(16.0f);\n" +
" return (uvec3(pos) >> uvec3(5u, 0u, 2u) & uvec3(7u, 3u, 7u)) * vec3(16.0f);\n" +
"}\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);
addIfNotExists(root, t, tree, "a_LightCoord", Type.I32VEC2, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "in_VertexData", Type.U32VEC4, StorageQualifier.StorageType.IN);
if (BlockRenderingSettings.INSTANCE.shouldUseSeparateAo()) {
addIfNotExists(root, t, tree, "iris_midBlock", Type.F32VEC4, StorageQualifier.StorageType.IN);
}
tree.prependMainFunctionBody(t, "_vert_init();");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public boolean isVisible(AABB aabb) {

// For Sodium
// TODO: change this to respect intersections on 1.18+!
public int fastAabbTest(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
public int fastAabbTest(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
if (boxCuller != null && boxCuller.isCulled(minX, minY, minZ, maxX, maxY, maxZ)) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ public final class ExtendedDataHelper {
/** All fluids have a ShadersMod render type of 1, to match behavior of Minecraft 1.7 and earlier. */
public static final short FLUID_RENDER_TYPE = 1;

public static int packMidBlock(float x, float y, float z) {
return ((int) (x * 64) & 0xFF) | (((int) (y * 64) & 0xFF) << 8) | (((int) (z * 64) & 0xFF) << 16);
public static int packMidBlock(float x, float y, float z, float w) {
return ((int) (x * 64) & 0xFF) | (((int) (y * 64) & 0xFF) << 8) | (((int) (z * 64) & 0xFF) << 16) | (((int) (w) & 0xFF) << 24);
}

public static int computeMidBlock(float x, float y, float z, int localPosX, int localPosY, int localPosZ) {
public static int computeMidBlock(float x, float y, float z, int localPosX, int localPosY, int localPosZ, float separateAo) {
return packMidBlock(
localPosX + 0.5f - x,
localPosY + 0.5f - y,
localPosZ + 0.5f - z
localPosZ + 0.5f - z,
separateAo
);
}
}
10 changes: 10 additions & 0 deletions src/main/java/net/coderbot/iris/vertices/NormI8.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public static int pack(Vector3f normal) {
public static int pack(float x, float y, float z, float w) {
return ((int) (x * 127) & 0xFF) | (((int) (y * 127) & 0xFF) << 8) | (((int) (z * 127) & 0xFF) << 16) | (((int) (w * 127) & 0xFF) << 24);
}
/**
* Packs the specified vector components into a 32-bit integer in XYZ ordering with the 8 bits of padding at the
* end.
* @param x The x component of the normal's vector
* @param y The y component of the normal's vector
* @param z The z component of the normal's vector
*/
public static int packColor(float x, float y, float z, float w) {
return ((int) (x * 127) & 0xFF) | (((int) (y * 127) & 0xFF) << 8) | (((int) (z * 127) & 0xFF) << 16) | (((int) w & 0xFF) << 24);
}

/**
* Encodes a float in the range of -1.0..1.0 to a normalized unsigned integer in the range of 0..255 which can then
Expand Down
Loading

0 comments on commit a737a19

Please sign in to comment.