From 6b2e354f9d0cbe51d3f89fffeff966801f4ffb01 Mon Sep 17 00:00:00 2001 From: IMS212 Date: Sun, 13 Aug 2023 19:01:12 -0700 Subject: [PATCH] "Rewrite" vertex format --- .../transformer/SodiumTransformer.java | 136 ++++++--------- .../iris/vertices/ExtendedDataHelper.java | 6 +- .../coderbot/iris/vertices/NormalHelper.java | 164 ++---------------- .../impl/IrisChunkShaderBindingPoints.java | 16 +- .../IrisChunkProgramOverrides.java | 8 +- .../IrisChunkMeshAttributes.java | 5 +- .../terrain_xhfp/QuadViewTerrain.java | 65 +++++++ .../terrain_xhfp/XHFPModelVertexType.java | 62 ++----- .../terrain_xhfp/XHFPTerrainVertex.java | 99 +++++------ .../MixinChunkMeshAttribute.java | 25 ++- .../MixinGlVertexFormatBuilder.java | 32 +++- .../MixinRegionChunkRenderer.java | 37 ++-- 12 files changed, 282 insertions(+), 373 deletions(-) create mode 100644 src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/QuadViewTerrain.java diff --git a/src/main/java/net/coderbot/iris/pipeline/transform/transformer/SodiumTransformer.java b/src/main/java/net/coderbot/iris/pipeline/transform/transformer/SodiumTransformer.java index 752ae2f14a..1ca0780d55 100644 --- a/src/main/java/net/coderbot/iris/pipeline/transform/transformer/SodiumTransformer.java +++ b/src/main/java/net/coderbot/iris/pipeline/transform/transformer/SodiumTransformer.java @@ -22,16 +22,15 @@ public class SodiumTransformer { public static void transform( - ASTParser t, - TranslationUnit tree, - Root root, - SodiumParameters parameters) { + ASTParser t, + TranslationUnit tree, + Root root, + SodiumParameters parameters) { CommonTransformer.transform(t, tree, root, parameters, false); 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"); @@ -43,18 +42,18 @@ public static void transform( if (parameters.inputs.hasTex()) { root.replaceReferenceExpressions(t, "gl_MultiTexCoord0", - "vec4(_vert_tex_coord, 0.0, 1.0)"); + "vec4(_vert_tex_diffuse_coord, 0.0, 1.0)"); } else { root.replaceReferenceExpressions(t, "gl_MultiTexCoord0", - "vec4(0.0, 0.0, 0.0, 1.0)"); + "vec4(0.0, 0.0, 0.0, 1.0)"); } if (parameters.inputs.hasLight()) { root.replaceReferenceExpressions(t, "gl_MultiTexCoord1", - "vec4(_vert_light, 0.0, 1.0)"); + "vec4(_vert_tex_light_coord, 0.0, 1.0)"); } else { root.replaceReferenceExpressions(t, "gl_MultiTexCoord1", - "vec4(240.0, 240.0, 0.0, 1.0)"); + "vec4(240.0, 240.0, 0.0, 1.0)"); } AttributeTransformer.patchMultiTexCoord3(t, tree, root, parameters); @@ -67,19 +66,15 @@ public static void transform( if (parameters.inputs.hasColor()) { // TODO: Handle the fragment shader here - root.replaceReferenceExpressions(t, "gl_Color", "vec4(_vert_color, " + (BlockRenderingSettings.INSTANCE.shouldUseSeparateAo() ? "iris_AOHolder.w" : "1.0") + ")"); - if (parameters.type.glShaderType == ShaderType.VERTEX) { - addIfNotExists(root, t, tree, "iris_AOHolder", Type.F32VEC4, StorageQualifier.StorageType.IN); - } - + root.rename("gl_Color", "_vert_color"); } else { root.replaceReferenceExpressions(t, "gl_Color", "vec4(1.0)"); } if (parameters.type.glShaderType == ShaderType.VERTEX) { if (parameters.inputs.hasNormal()) { - root.replaceReferenceExpressions(t, "gl_Normal", "iris_Normal"); - addIfNotExists(root, t, tree, "iris_Normal", Type.F32VEC3, StorageQualifier.StorageType.IN); + 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)"); } @@ -88,15 +83,15 @@ public static void transform( // TODO: Should probably add the normal matrix as a proper uniform that's // computed on the CPU-side of things root.replaceReferenceExpressions(t, "gl_NormalMatrix", - "iris_NormalMatrix"); + "iris_NormalMatrix"); tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, - "uniform mat3 iris_NormalMatrix;"); + "uniform mat3 iris_NormalMatrix;"); tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, - "uniform mat4 iris_ModelViewMatrixInverse;"); + "uniform mat4 iris_ModelViewMatrixInverse;"); tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, - "uniform mat4 iris_ProjectionMatrixInverse;"); + "uniform mat4 iris_ProjectionMatrixInverse;"); // TODO: All of the transformed variants of the input matrices, preferably // computed on the CPU side... @@ -109,14 +104,14 @@ public static void transform( // chunks. if (root.identifierIndex.has("ftransform")) { tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_FUNCTIONS, - "vec4 ftransform() { return gl_ModelViewProjectionMatrix * gl_Vertex; }"); + "vec4 ftransform() { return gl_ModelViewProjectionMatrix * gl_Vertex; }"); } tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS, - "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(_vert_mesh_id), 1.0); }"); + "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); }"); root.replaceReferenceExpressions(t, "gl_Vertex", "getVertexPosition()"); // inject here so that _vert_position is available to the above. (injections @@ -125,69 +120,52 @@ public static void transform( injectVertInit(t, tree, root, parameters); } else { tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS, - "uniform mat4 iris_ModelViewMatrix;", - "uniform mat4 iris_ProjectionMatrix;"); + "uniform mat4 iris_ModelViewMatrix;", + "uniform mat4 iris_ProjectionMatrix;"); } root.replaceReferenceExpressions(t, "gl_ModelViewProjectionMatrix", - "(iris_ProjectionMatrix * iris_ModelViewMatrix)"); + "(iris_ProjectionMatrix * iris_ModelViewMatrix)"); CommonTransformer.applyIntelHd4000Workaround(root); } public static void injectVertInit( - ASTParser t, - TranslationUnit tree, - Root root, - SodiumParameters parameters) { + ASTParser t, + 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_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() { - // 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 (uvec3(pos) >> uvec3(5u, 0u, 2u) & uvec3(7u, 3u, 7u)) * vec3(16.0f);\n" + + // translated from sodium's chunk_vertex.glsl + "vec3 _vert_position;", + "vec2 _vert_tex_diffuse_coord;", + "ivec2 _vert_tex_light_coord;", + "vec4 _vert_color;", + "uint _draw_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;" + + "_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" + + "}", + "vec3 _get_draw_translation(uint pos) {\n" + + " return _get_relative_chunk_coord(pos) * vec3(16.0f);\n" + "}\n"); - 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); - } + 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); tree.prependMainFunctionBody(t, "_vert_init();"); } diff --git a/src/main/java/net/coderbot/iris/vertices/ExtendedDataHelper.java b/src/main/java/net/coderbot/iris/vertices/ExtendedDataHelper.java index 4824927f0d..8f1b7f3eb8 100644 --- a/src/main/java/net/coderbot/iris/vertices/ExtendedDataHelper.java +++ b/src/main/java/net/coderbot/iris/vertices/ExtendedDataHelper.java @@ -12,9 +12,9 @@ public static int packMidBlock(float x, float y, float z) { public static int computeMidBlock(float x, float y, float z, int localPosX, int localPosY, int localPosZ) { return packMidBlock( - localPosX + 0.5f - x, - localPosY + 0.5f - y, - localPosZ + 0.5f - z + localPosX + 0.5f - x, + localPosY + 0.5f - y, + localPosZ + 0.5f - z ); } } diff --git a/src/main/java/net/coderbot/iris/vertices/NormalHelper.java b/src/main/java/net/coderbot/iris/vertices/NormalHelper.java index 56cd8b0ac1..44aca7fff9 100644 --- a/src/main/java/net/coderbot/iris/vertices/NormalHelper.java +++ b/src/main/java/net/coderbot/iris/vertices/NormalHelper.java @@ -16,7 +16,6 @@ * limitations under the License. */ -import org.joml.Vector2f; import org.joml.Vector3f; import net.minecraft.util.Mth; import org.jetbrains.annotations.NotNull; @@ -86,63 +85,13 @@ 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. - * - *

Assumes counter-clockwise winding order, which is the norm. - * Expects convex quads with all points co-planar. - */ - public static void computeFaceNormalArray(@NotNull Vector3f saveTo, Vector3f[] posHolder) { -// 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 x0 = posHolder[0].x; - final float y0 = posHolder[0].y; - final float z0 = posHolder[0].z; - final float x1 = posHolder[1].x; - final float y1 = posHolder[1].y; - final float z1 = posHolder[1].z; - final float x2 = posHolder[2].x; - final float y2 = posHolder[2].y; - final float z2 = posHolder[2].z; - final float x3 = posHolder[3].x; - final float y3 = posHolder[3].y; - final float z3 = posHolder[3].z; - - 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; - } - - saveTo.set(normX, normY, normZ); - } - /** * Computes the face normal of the given quad with a flipped order and saves it in the provided non-null vector. * *

Assumes counter-clockwise winding order, which is the norm. It will be read clockwise to flip it. * Expects convex quads with all points co-planar. */ - public static void computeFaceNormalFlipped(@NotNull Vector3f saveTo, Vector3f[] posHolder) { + public static void computeFaceNormalFlipped(@NotNull Vector3f saveTo, QuadView q) { // final Direction nominalFace = q.nominalFace(); // // if (GeometryHelper.isQuadParallelToFace(nominalFace, q)) { @@ -151,18 +100,18 @@ public static void computeFaceNormalFlipped(@NotNull Vector3f saveTo, Vector3f[] // return; // } - final float x0 = posHolder[3].x; - final float y0 = posHolder[3].y; - final float z0 = posHolder[3].z; - final float x1 = posHolder[2].x; - final float y1 = posHolder[2].y; - final float z1 = posHolder[2].z; - final float x2 = posHolder[1].x; - final float y2 = posHolder[1].y; - final float z2 = posHolder[1].z; - final float x3 = posHolder[0].x; - final float y3 = posHolder[0].y; - final float z3 = posHolder[0].z; + final float x0 = q.x(3); + final float y0 = q.y(3); + final float z0 = q.z(3); + final float x1 = q.x(2); + final float y1 = q.y(2); + final float z1 = q.z(2); + final float x2 = q.x(1); + final float y2 = q.y(1); + final float z2 = q.z(1); + final float x3 = q.x(0); + final float y3 = q.y(0); + final float z3 = q.z(0); final float dx0 = x2 - x0; final float dy0 = y2 - y0; @@ -432,93 +381,6 @@ public static int computeTangent(float normalX, float normalY, float normalZ, Tr return NormI8.pack(tangentx, tangenty, tangentz, tangentW); } - public static int computeTangentArray(float normalX, float normalY, float normalZ, Vector3f[] posArray, Vector2f[] uvArray) { - // Capture all of the relevant vertex positions - float x0 = posArray[0].x; - float y0 = posArray[0].y; - float z0 = posArray[0].z; - - float x1 = posArray[1].x; - float y1 = posArray[1].y; - float z1 = posArray[1].z; - - float x2 = posArray[2].x; - float y2 = posArray[2].y; - float z2 = posArray[2].z; - - float edge1x = x1 - x0; - float edge1y = y1 - y0; - float edge1z = z1 - z0; - - float edge2x = x2 - x0; - float edge2y = y2 - y0; - float edge2z = z2 - z0; - - float u0 = uvArray[0].x; - float v0 = uvArray[0].y; - - float u1 = uvArray[1].x; - float v1 = uvArray[1].y; - - float u2 = uvArray[2].x; - float v2 = uvArray[2].y; - - float deltaU1 = u1 - u0; - float deltaV1 = v1 - v0; - float deltaU2 = u2 - u0; - float deltaV2 = v2 - v0; - - float fdenom = deltaU1 * deltaV2 - deltaU2 * deltaV1; - float f; - - if (fdenom == 0.0) { - f = 1.0f; - } else { - f = 1.0f / fdenom; - } - - float tangentx = f * (deltaV2 * edge1x - deltaV1 * edge2x); - float tangenty = f * (deltaV2 * edge1y - deltaV1 * edge2y); - float tangentz = f * (deltaV2 * edge1z - deltaV1 * edge2z); - float tcoeff = rsqrt(tangentx * tangentx + tangenty * tangenty + tangentz * tangentz); - tangentx *= tcoeff; - tangenty *= tcoeff; - tangentz *= tcoeff; - - float bitangentx = f * (-deltaU2 * edge1x + deltaU1 * edge2x); - float bitangenty = f * (-deltaU2 * edge1y + deltaU1 * edge2y); - float bitangentz = f * (-deltaU2 * edge1z + deltaU1 * edge2z); - float bitcoeff = rsqrt(bitangentx * bitangentx + bitangenty * bitangenty + bitangentz * bitangentz); - bitangentx *= bitcoeff; - bitangenty *= bitcoeff; - bitangentz *= bitcoeff; - - // predicted bitangent = tangent × normal - // Compute the determinant of the following matrix to get the cross product - // i j k - // tx ty tz - // nx ny nz - - // Be very careful when writing out complex multi-step calculations - // such as vector cross products! The calculation for pbitangentz - // used to be broken because it multiplied values in the wrong order. - - float pbitangentx = tangenty * normalZ - tangentz * normalY; - float pbitangenty = tangentz * normalX - tangentx * normalZ; - float pbitangentz = tangentx * normalY - tangenty * normalX; - - float dot = (bitangentx * pbitangentx) + (bitangenty * pbitangenty) + (bitangentz * pbitangentz); - float tangentW; - - if (dot < 0) { - tangentW = -1.0F; - } else { - tangentW = 1.0F; - } - - return NormI8.pack(tangentx, tangenty, tangentz, tangentW); - } - public static int computeTangent(float normalX, float normalY, float normalZ, float x0, float y0, float z0, float u0, float v0, float x1, float y1, float z1, float u1, float v1, float x2, float y2, float z2, float u2, float v2) { diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/IrisChunkShaderBindingPoints.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/IrisChunkShaderBindingPoints.java index 098ceeec98..43295cbc1e 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/IrisChunkShaderBindingPoints.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/IrisChunkShaderBindingPoints.java @@ -6,10 +6,14 @@ * NB: Make sure this doesn't collide with anything in {@link me.jellysquid.mods.sodium.client.render.chunk.shader.ChunkShaderBindingPoints} */ public class IrisChunkShaderBindingPoints { - public static final int NORMAL = 2; - public static final int AO = 3; - public static final int BLOCK_ID = 4; - public static final int MID_TEX_COORD = 5; - public static final int TANGENT = 6; - public static final int MID_BLOCK = 7; + public static final int ATTRIBUTE_POSITION_ID = 1; + public static final int ATTRIBUTE_COLOR = 2; + public static final int ATTRIBUTE_BLOCK_TEXTURE = 3; + public static final int ATTRIBUTE_LIGHT_TEXTURE = 4; + + public static final int NORMAL = 10; + public static final int BLOCK_ID = 11; + public static final int MID_TEX_COORD = 12; + public static final int TANGENT = 13; + public static final int MID_BLOCK = 14; } diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/shader_overrides/IrisChunkProgramOverrides.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/shader_overrides/IrisChunkProgramOverrides.java index c2fcad4a15..56bcd1cd3e 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/shader_overrides/IrisChunkProgramOverrides.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/shader_overrides/IrisChunkProgramOverrides.java @@ -175,13 +175,15 @@ private GlProgram createShader(IrisTerrainPass pass, S return builder.attachShader(vertShader) .attachShader(fragShader) // The following 4 attributes are part of Sodium - .bindAttribute("in_VertexData", ChunkShaderBindingPoints.ATTRIBUTE_PACKED_DATA) + .bindAttribute("a_PosId", IrisChunkShaderBindingPoints.ATTRIBUTE_POSITION_ID) + .bindAttribute("a_Color", IrisChunkShaderBindingPoints.ATTRIBUTE_COLOR) + .bindAttribute("a_TexCoord", IrisChunkShaderBindingPoints.ATTRIBUTE_BLOCK_TEXTURE) + .bindAttribute("a_LightCoord", IrisChunkShaderBindingPoints.ATTRIBUTE_LIGHT_TEXTURE) .bindAttribute("mc_Entity", IrisChunkShaderBindingPoints.BLOCK_ID) .bindAttribute("mc_midTexCoord", IrisChunkShaderBindingPoints.MID_TEX_COORD) .bindAttribute("at_tangent", IrisChunkShaderBindingPoints.TANGENT) .bindAttribute("iris_Normal", IrisChunkShaderBindingPoints.NORMAL) - .bindAttribute("iris_AOHolder", IrisChunkShaderBindingPoints.AO) - .bindAttribute("iris_midBlock", IrisChunkShaderBindingPoints.MID_BLOCK) + .bindAttribute("at_midBlock", IrisChunkShaderBindingPoints.MID_BLOCK) .link((shader) -> { // TODO: Better way for this? It's a bit too much casting for me. int handle = ((GlObject) shader).handle(); diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/IrisChunkMeshAttributes.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/IrisChunkMeshAttributes.java index 5ec7a70fac..7acc12ee5d 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/IrisChunkMeshAttributes.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/IrisChunkMeshAttributes.java @@ -6,8 +6,11 @@ * Initialized by {@link net.coderbot.iris.compat.sodium.mixin.vertex_format.MixinChunkMeshAttribute} */ public class IrisChunkMeshAttributes { + public static ChunkMeshAttribute POSITION_MATERIAL_MESH; + public static ChunkMeshAttribute COLOR_SHADE; + public static ChunkMeshAttribute BLOCK_TEXTURE; + public static ChunkMeshAttribute LIGHT_TEXTURE; public static ChunkMeshAttribute NORMAL; - public static ChunkMeshAttribute AO; public static ChunkMeshAttribute TANGENT; public static ChunkMeshAttribute MID_TEX_COORD; public static ChunkMeshAttribute BLOCK_ID; diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/QuadViewTerrain.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/QuadViewTerrain.java new file mode 100644 index 0000000000..3eeed94b06 --- /dev/null +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/QuadViewTerrain.java @@ -0,0 +1,65 @@ +package net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp; + +import net.coderbot.iris.vertices.QuadView; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +public abstract class QuadViewTerrain implements QuadView { + long writePointer; + int stride; + + @Override + public float x(int index) { + return XHFPModelVertexType.decodePosition(getShort(writePointer - stride * (3 - index))); + } + + @Override + public float y(int index) { + return XHFPModelVertexType.decodePosition(getShort(writePointer + 2 - stride * (3 - index))); + } + + @Override + public float z(int index) { + return XHFPModelVertexType.decodePosition(getShort(writePointer + 4 - stride * (3 - index))); + } + + @Override + public float u(int index) { + return XHFPModelVertexType.decodeBlockTexture(getShort(writePointer + 12 - stride * (3 - index))); + } + + @Override + public float v(int index) { + return XHFPModelVertexType.decodeBlockTexture(getShort(writePointer + 14 - stride * (3 - index))); + } + + abstract short getShort(long writePointer); + + public static class QuadViewTerrainUnsafe extends QuadViewTerrain { + public void setup(long writePointer, int stride) { + this.writePointer = writePointer; + this.stride = stride; + } + + @Override + short getShort(long writePointer) { + return MemoryUtil.memGetShort(writePointer); + } + } + + public static class QuadViewTerrainNio extends QuadViewTerrain { + private ByteBuffer buffer; + + public void setup(ByteBuffer buffer, int writePointer, int stride) { + this.buffer = buffer; + this.writePointer = writePointer; + this.stride = stride; + } + + @Override + short getShort(long writePointer) { + return buffer.getShort((int) writePointer); + } + } +} diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java index 0dc06c1967..827aab09fa 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java @@ -2,13 +2,9 @@ import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexAttributeFormat; import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexFormat; -import me.jellysquid.mods.sodium.client.render.chunk.terrain.material.Material; import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkMeshAttribute; import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexEncoder; import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType; -import net.caffeinemc.mods.sodium.api.util.ColorABGR; -import net.caffeinemc.mods.sodium.api.util.ColorU8; -import net.coderbot.iris.block_rendering.BlockRenderingSettings; import net.coderbot.iris.compat.sodium.impl.vertex_format.IrisChunkMeshAttributes; import net.coderbot.iris.compat.sodium.impl.vertex_format.IrisGlVertexAttributeFormat; @@ -17,15 +13,17 @@ */ public class XHFPModelVertexType implements ChunkVertexType { public static final int STRIDE = 40; - public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(ChunkMeshAttribute.class, STRIDE) - .addElement(ChunkMeshAttribute.VERTEX_DATA, 0, GlVertexAttributeFormat.UNSIGNED_INT, 4, false, true) - .addElement(IrisChunkMeshAttributes.MID_TEX_COORD, 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) - .addElement(IrisChunkMeshAttributes.TANGENT, 20, IrisGlVertexAttributeFormat.BYTE, 4, true, false) - .addElement(IrisChunkMeshAttributes.NORMAL, 24, IrisGlVertexAttributeFormat.BYTE, 3, true, false) - .addElement(IrisChunkMeshAttributes.BLOCK_ID, 28, IrisGlVertexAttributeFormat.SHORT, 2, false, false) - .addElement(IrisChunkMeshAttributes.MID_BLOCK, 32, IrisGlVertexAttributeFormat.BYTE, 3, false, false) - .addElement(IrisChunkMeshAttributes.AO, 36, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false) + .addElement(IrisChunkMeshAttributes.POSITION_MATERIAL_MESH, 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 4, false, true) + .addElement(IrisChunkMeshAttributes.COLOR_SHADE, 8, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false) + .addElement(IrisChunkMeshAttributes.BLOCK_TEXTURE, 12, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) + .addElement(IrisChunkMeshAttributes.LIGHT_TEXTURE, 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, true) + .addElement(IrisChunkMeshAttributes.MID_TEX_COORD, 20, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) + .addElement(IrisChunkMeshAttributes.TANGENT, 24, IrisGlVertexAttributeFormat.BYTE, 4, true, false) + .addElement(IrisChunkMeshAttributes.NORMAL, 28, IrisGlVertexAttributeFormat.BYTE, 3, true, false) + .addElement(IrisChunkMeshAttributes.BLOCK_ID, 32, IrisGlVertexAttributeFormat.SHORT, 2, false, false) + .addElement(IrisChunkMeshAttributes.MID_BLOCK, 36, IrisGlVertexAttributeFormat.BYTE, 3, false, false) + .addElement(ChunkMeshAttribute.VERTEX_DATA, 40, IrisGlVertexAttributeFormat.BYTE, 0, false, false) .build(); private static final int POSITION_MAX_VALUE = 65536; @@ -33,7 +31,7 @@ public class XHFPModelVertexType implements ChunkVertexType { private static final float MODEL_ORIGIN = 8.0f; private static final float MODEL_RANGE = 32.0f; - private static final float MODEL_SCALE = 32.0f; + private static final float MODEL_SCALE = MODEL_RANGE / POSITION_MAX_VALUE; private static final float MODEL_SCALE_INV = POSITION_MAX_VALUE / MODEL_RANGE; @@ -49,41 +47,19 @@ public ChunkVertexEncoder getEncoder() { return new XHFPTerrainVertex(); } - public static int encodePosition(float value) { - return (int) ((MODEL_ORIGIN + value) * (POSITION_MAX_VALUE / MODEL_SCALE)); - } - - protected static int encodeDrawParameters(Material material, int sectionIndex) { - return (((sectionIndex & 0xFF) << 8) | ((material.bits() & 0xFF) << 0)); - } - - protected static int encodeColor(int color) { - if (BlockRenderingSettings.INSTANCE.shouldUseSeparateAo()) { - return ColorABGR.withAlpha(color, 0x00); - } - - var brightness = ColorU8.byteToNormalizedFloat(ColorABGR.unpackAlpha(color)); - - int r = ColorU8.normalizedFloatToByte(ColorU8.byteToNormalizedFloat(ColorABGR.unpackRed(color)) * brightness); - int g = ColorU8.normalizedFloatToByte(ColorU8.byteToNormalizedFloat(ColorABGR.unpackGreen(color)) * brightness); - int b = ColorU8.normalizedFloatToByte(ColorU8.byteToNormalizedFloat(ColorABGR.unpackBlue(color)) * brightness); - - - return ColorABGR.pack(r, g, b, 0x00); + static short encodeBlockTexture(float value) { + return (short) (Math.min(0.99999997F, value) * TEXTURE_MAX_VALUE); } - protected static int encodeLight(int light) { - int block = (light >> 4) & 0xF; - int sky = (light >> 20) & 0xF; - - return ((block << 0) | (sky << 4)); + static float decodeBlockTexture(short raw) { + return (raw & 0xFFFF) * TEXTURE_SCALE; } - static short encodeTexture(float value) { - return (short) (Math.min(0.99999997F, value) * TEXTURE_MAX_VALUE); + static short encodePosition(float v) { + return (short) ((MODEL_ORIGIN + v) * MODEL_SCALE_INV); } - static float decodeBlockTexture(short raw) { - return (raw & 0xFFFF) * TEXTURE_SCALE; + static float decodePosition(short raw) { + return (raw & 0xFFFF) * MODEL_SCALE - MODEL_ORIGIN; } } diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPTerrainVertex.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPTerrainVertex.java index 1e25384621..c29be96ab5 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPTerrainVertex.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPTerrainVertex.java @@ -2,26 +2,18 @@ import me.jellysquid.mods.sodium.client.render.chunk.terrain.material.Material; import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexEncoder; -import net.caffeinemc.mods.sodium.api.util.ColorABGR; -import net.caffeinemc.mods.sodium.api.util.ColorU8; import net.coderbot.iris.compat.sodium.impl.block_context.BlockContextHolder; import net.coderbot.iris.compat.sodium.impl.block_context.ContextAwareVertexWriter; import net.coderbot.iris.vertices.NormI8; -import net.minecraft.util.Mth; -import org.joml.Vector2f; import org.joml.Vector3f; import net.coderbot.iris.vertices.ExtendedDataHelper; import net.coderbot.iris.vertices.NormalHelper; import org.lwjgl.system.MemoryUtil; import static net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.STRIDE; -import static net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.encodeColor; -import static net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.encodeDrawParameters; -import static net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.encodeLight; -import static net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.encodePosition; -import static net.coderbot.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.encodeTexture; public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareVertexWriter { + private final QuadViewTerrain.QuadViewTerrainUnsafe quad = new QuadViewTerrain.QuadViewTerrainUnsafe(); private final Vector3f normal = new Vector3f(); private BlockContextHolder contextHolder; @@ -57,20 +49,6 @@ public void copyQuadAndFlipNormal() { this.flush(); }*/ - private final Vector3f[] posHolder = new Vector3f[] { - new Vector3f(), - new Vector3f(), - new Vector3f(), - new Vector3f() - }; - - private final Vector2f[] uvHolder = new Vector2f[] { - new Vector2f(), - new Vector2f(), - new Vector2f(), - new Vector2f() - }; - @Override public void iris$setContextHolder(BlockContextHolder holder) { this.contextHolder = holder; @@ -83,24 +61,27 @@ public void flipUpcomingQuadNormal() { @Override public long write(long ptr, - Material material, Vertex vertex, int sectionIndex) { + Material material, Vertex vertex, int chunkId) { uSum += vertex.u; vSum += vertex.v; + vertexCount++; - posHolder[vertexCount].set(vertex.x, vertex.y, vertex.z); - uvHolder[vertexCount].set(vertex.u, vertex.v); + MemoryUtil.memPutShort(ptr + 0L, XHFPModelVertexType.encodePosition(vertex.x)); + MemoryUtil.memPutShort(ptr + 2L, XHFPModelVertexType.encodePosition(vertex.y)); + MemoryUtil.memPutShort(ptr + 4L, XHFPModelVertexType.encodePosition(vertex.z)); + MemoryUtil.memPutByte(ptr + 6L, (byte) material.bits()); + MemoryUtil.memPutByte(ptr + 7L, (byte) chunkId); - vertexCount++; + MemoryUtil.memPutInt(ptr + 8, vertex.color); - MemoryUtil.memPutInt(ptr + 0, (encodePosition(vertex.x) << 0) | (encodePosition(vertex.y) << 16)); - MemoryUtil.memPutInt(ptr + 4, (encodePosition(vertex.z) << 0) | (encodeDrawParameters(material, sectionIndex) << 16)); - MemoryUtil.memPutInt(ptr + 8, (encodeColor(vertex.color) << 0) | (encodeLight(vertex.light) << 24)); - MemoryUtil.memPutInt(ptr + 12, (encodeTexture(vertex.u) << 0) | (encodeTexture(vertex.v) << 16)); + MemoryUtil.memPutShort(ptr + 12, XHFPModelVertexType.encodeBlockTexture(vertex.u)); + MemoryUtil.memPutShort(ptr + 14, XHFPModelVertexType.encodeBlockTexture(vertex.v)); - MemoryUtil.memPutShort(ptr + 28, contextHolder.blockId); - MemoryUtil.memPutShort(ptr + 30, contextHolder.renderType); - MemoryUtil.memPutInt(ptr + 32, contextHolder.ignoreMidBlock ? 0 : ExtendedDataHelper.computeMidBlock(vertex.x, vertex.y, vertex.z, contextHolder.localPosX, contextHolder.localPosY, contextHolder.localPosZ)); - MemoryUtil.memPutInt(ptr + 36, vertex.color); + MemoryUtil.memPutInt(ptr + 16, vertex.light); + + MemoryUtil.memPutShort(ptr + 32, contextHolder.blockId); + MemoryUtil.memPutShort(ptr + 34, contextHolder.renderType); + MemoryUtil.memPutInt(ptr + 36, contextHolder.ignoreMidBlock ? 0 : ExtendedDataHelper.computeMidBlock(vertex.x, vertex.y, vertex.z, contextHolder.localPosX, contextHolder.localPosY, contextHolder.localPosZ)); if (vertexCount == 4) { vertexCount = 0; @@ -117,7 +98,7 @@ public long write(long ptr, // fragile patching that is not yet possible. // // As a temporary solution, the normalized shorts have been replaced with regular floats, but this takes up - // an extra 4 bytes per vertex. + // an extra 4 bytes per vertex. // NB: Be careful with the math here! A previous bug was caused by midU going negative as a short, which // was sign-extended into midTexCoord, causing midV to have garbage (likely NaN data). If you're touching @@ -135,18 +116,18 @@ public long write(long ptr, uSum *= 0.25f; vSum *= 0.25f; - short midU = XHFPModelVertexType.encodeTexture(uSum); - short midV = XHFPModelVertexType.encodeTexture(vSum); + short midU = XHFPModelVertexType.encodeBlockTexture(uSum); + short midV = XHFPModelVertexType.encodeBlockTexture(vSum); - MemoryUtil.memPutShort(ptr + 16, midU); - MemoryUtil.memPutShort(ptr + 16 - STRIDE, midU); - MemoryUtil.memPutShort(ptr + 16 - STRIDE * 2, midU); - MemoryUtil.memPutShort(ptr + 16 - STRIDE * 3, midU); + MemoryUtil.memPutShort(ptr + 20, midU); + MemoryUtil.memPutShort(ptr + 20 - STRIDE, midU); + MemoryUtil.memPutShort(ptr + 20 - STRIDE * 2, midU); + MemoryUtil.memPutShort(ptr + 20 - STRIDE * 3, midU); - MemoryUtil.memPutShort(ptr + 18, midV); - MemoryUtil.memPutShort(ptr + 18 - STRIDE, midV); - MemoryUtil.memPutShort(ptr + 18 - STRIDE * 2, midV); - MemoryUtil.memPutShort(ptr + 18 - STRIDE * 3, midV); + MemoryUtil.memPutShort(ptr + 22, midV); + MemoryUtil.memPutShort(ptr + 22 - STRIDE, midV); + MemoryUtil.memPutShort(ptr + 22 - STRIDE * 2, midV); + MemoryUtil.memPutShort(ptr + 22 - STRIDE * 3, midV); uSum = 0; vSum = 0; @@ -155,26 +136,28 @@ public long write(long ptr, // Implementation based on the algorithm found here: // https://github.com/IrisShaders/ShaderDoc/blob/master/vertex-format-extensions.md#surface-normal-vector + quad.setup(ptr, STRIDE); if (flipUpcomingNormal) { - NormalHelper.computeFaceNormalFlipped(normal, posHolder); + NormalHelper.computeFaceNormalFlipped(normal, quad); flipUpcomingNormal = false; } else { - NormalHelper.computeFaceNormalArray(normal, posHolder); + NormalHelper.computeFaceNormal(normal, quad); } - int packedNormal = NormI8.pack(normal); - MemoryUtil.memPutInt(ptr + 24, packedNormal); - MemoryUtil.memPutInt(ptr + 24 - STRIDE, packedNormal); - MemoryUtil.memPutInt(ptr + 24 - STRIDE * 2, packedNormal); - MemoryUtil.memPutInt(ptr + 24 - STRIDE * 3, packedNormal); - int tangent = NormalHelper.computeTangentArray(normal.x, normal.y, normal.z, posHolder, uvHolder); - MemoryUtil.memPutInt(ptr + 20, tangent); - MemoryUtil.memPutInt(ptr + 20 - STRIDE, tangent); - MemoryUtil.memPutInt(ptr + 20 - STRIDE * 2, tangent); - MemoryUtil.memPutInt(ptr + 20 - STRIDE * 3, tangent); + MemoryUtil.memPutInt(ptr + 28, packedNormal); + MemoryUtil.memPutInt(ptr + 28 - STRIDE, packedNormal); + MemoryUtil.memPutInt(ptr + 28 - STRIDE * 2, packedNormal); + MemoryUtil.memPutInt(ptr + 28 - STRIDE * 3, packedNormal); + + int tangent = NormalHelper.computeTangent(normal.x, normal.y, normal.z, quad); + + MemoryUtil.memPutInt(ptr + 24, tangent); + MemoryUtil.memPutInt(ptr + 24 - STRIDE, tangent); + MemoryUtil.memPutInt(ptr + 24 - STRIDE * 2, tangent); + MemoryUtil.memPutInt(ptr + 24 - STRIDE * 3, tangent); } return ptr + STRIDE; diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinChunkMeshAttribute.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinChunkMeshAttribute.java index 2feb6e8fc1..1131b34869 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinChunkMeshAttribute.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinChunkMeshAttribute.java @@ -25,22 +25,31 @@ public class MixinChunkMeshAttribute { static { int baseOrdinal = $VALUES.length; + IrisChunkMeshAttributes.POSITION_MATERIAL_MESH + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("POSITION_MATERIAL_MESH", baseOrdinal); + IrisChunkMeshAttributes.COLOR_SHADE + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("COLOR_SHADE", baseOrdinal + 1); + IrisChunkMeshAttributes.BLOCK_TEXTURE + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("BLOCK_TEXTURE", baseOrdinal + 2); + IrisChunkMeshAttributes.LIGHT_TEXTURE + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("LIGHT_TEXTURE", baseOrdinal + 3); IrisChunkMeshAttributes.NORMAL - = ChunkMeshAttributeAccessor.createChunkMeshAttribute("NORMAL", baseOrdinal); - IrisChunkMeshAttributes.AO - = ChunkMeshAttributeAccessor.createChunkMeshAttribute("AO", baseOrdinal + 1); + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("NORMAL", baseOrdinal + 4); IrisChunkMeshAttributes.TANGENT - = ChunkMeshAttributeAccessor.createChunkMeshAttribute("TANGENT", baseOrdinal + 2); + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("TANGENT", baseOrdinal + 5); IrisChunkMeshAttributes.MID_TEX_COORD - = ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_TEX_COORD", baseOrdinal + 3); + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_TEX_COORD", baseOrdinal + 6); IrisChunkMeshAttributes.BLOCK_ID - = ChunkMeshAttributeAccessor.createChunkMeshAttribute("BLOCK_ID", baseOrdinal + 4); + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("BLOCK_ID", baseOrdinal + 7); IrisChunkMeshAttributes.MID_BLOCK - = ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_BLOCK", baseOrdinal + 5); + = ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_BLOCK", baseOrdinal + 8); $VALUES = ArrayUtils.addAll($VALUES, + IrisChunkMeshAttributes.POSITION_MATERIAL_MESH, + IrisChunkMeshAttributes.COLOR_SHADE, + IrisChunkMeshAttributes.BLOCK_TEXTURE, + IrisChunkMeshAttributes.LIGHT_TEXTURE, IrisChunkMeshAttributes.NORMAL, - IrisChunkMeshAttributes.AO, IrisChunkMeshAttributes.TANGENT, IrisChunkMeshAttributes.MID_TEX_COORD, IrisChunkMeshAttributes.BLOCK_ID, diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinGlVertexFormatBuilder.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinGlVertexFormatBuilder.java index c8d60cfaca..0d5214a68e 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinGlVertexFormatBuilder.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinGlVertexFormatBuilder.java @@ -4,14 +4,23 @@ import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexAttributeFormat; import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexFormat; import net.coderbot.iris.compat.sodium.impl.vertex_format.IrisChunkMeshAttributes; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; import java.util.EnumMap; @Mixin(GlVertexFormat.Builder.class) -public class MixinGlVertexFormatBuilder { +public class MixinGlVertexFormatBuilder> { + @Shadow + @Final + private int stride; + @Shadow + @Final + private EnumMap attributes; private static final GlVertexAttribute EMPTY = new GlVertexAttribute(GlVertexAttributeFormat.FLOAT, 0, false, 0, 0, false); @@ -23,14 +32,23 @@ public class MixinGlVertexFormatBuilder { Object value = map.get(key); if (value == null) { - if (key == IrisChunkMeshAttributes.NORMAL || key == IrisChunkMeshAttributes.AO || key == IrisChunkMeshAttributes.TANGENT - || key == IrisChunkMeshAttributes.MID_TEX_COORD || key == IrisChunkMeshAttributes.BLOCK_ID - || key == IrisChunkMeshAttributes.MID_BLOCK) { - // Missing these attributes is acceptable and will be handled properly. - return EMPTY; - } + // Missing these attributes is acceptable and will be handled properly. + return EMPTY; } return value; } + + /** + * @author + * @reason + */ + @Overwrite(remap = false) + private GlVertexFormat.Builder addElement(T type, GlVertexAttribute attribute) { + if (this.attributes.put(type, attribute) != null) { + throw new IllegalStateException("Generic attribute " + type.name() + " already defined in vertex format"); + } else { + return (GlVertexFormat.Builder) (Object) this; + } + } } diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinRegionChunkRenderer.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinRegionChunkRenderer.java index f3507a2879..3ab5cf1e6a 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinRegionChunkRenderer.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/MixinRegionChunkRenderer.java @@ -19,6 +19,9 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; +import java.util.Arrays; +import java.util.List; + @Mixin(DefaultChunkRenderer.class) public abstract class MixinRegionChunkRenderer extends ShaderChunkRenderer { public MixinRegionChunkRenderer(RenderDevice device, ChunkVertexType vertexType) { @@ -31,20 +34,26 @@ public MixinRegionChunkRenderer(RenderDevice device, ChunkVertexType vertexType) return TessellationBinding.forVertexBuffer(buffer, attributes); } - attributes = ArrayUtils.addAll(attributes, - new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.MID_BLOCK, - vertexFormat.getAttribute(IrisChunkMeshAttributes.MID_BLOCK)), - new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.BLOCK_ID, - vertexFormat.getAttribute(IrisChunkMeshAttributes.BLOCK_ID)), - new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.MID_TEX_COORD, - vertexFormat.getAttribute(IrisChunkMeshAttributes.MID_TEX_COORD)), - new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.TANGENT, - vertexFormat.getAttribute(IrisChunkMeshAttributes.TANGENT)), - new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.AO, - vertexFormat.getAttribute(IrisChunkMeshAttributes.AO)), - new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.NORMAL, - vertexFormat.getAttribute(IrisChunkMeshAttributes.NORMAL)) - ); + attributes = new GlVertexAttributeBinding[]{ + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_POSITION_ID, + vertexFormat.getAttribute(IrisChunkMeshAttributes.POSITION_MATERIAL_MESH)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_COLOR, + vertexFormat.getAttribute(IrisChunkMeshAttributes.COLOR_SHADE)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_BLOCK_TEXTURE, + vertexFormat.getAttribute(IrisChunkMeshAttributes.BLOCK_TEXTURE)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_LIGHT_TEXTURE, + vertexFormat.getAttribute(IrisChunkMeshAttributes.LIGHT_TEXTURE)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.MID_BLOCK, + vertexFormat.getAttribute(IrisChunkMeshAttributes.MID_BLOCK)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.BLOCK_ID, + vertexFormat.getAttribute(IrisChunkMeshAttributes.BLOCK_ID)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.MID_TEX_COORD, + vertexFormat.getAttribute(IrisChunkMeshAttributes.MID_TEX_COORD)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.TANGENT, + vertexFormat.getAttribute(IrisChunkMeshAttributes.TANGENT)), + new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.NORMAL, + vertexFormat.getAttribute(IrisChunkMeshAttributes.NORMAL)) + }; return TessellationBinding.forVertexBuffer(buffer, attributes); }