Skip to content

Commit

Permalink
absolute insanity
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 22, 2024
1 parent ee91a8f commit 37a0e20
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = Window.class, priority = 1010)
public class MixinWindow {
//@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwCreateWindow(IILjava/lang/CharSequence;JJ)J"))
@Unique
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwCreateWindow(IILjava/lang/CharSequence;JJ)J"))
private void iris$enableDebugContext(WindowEventHandler arg, ScreenManager arg2, DisplayData arg3, String string, String string2, CallbackInfo ci) {
if (Iris.getIrisConfig().areDebugOptionsEnabled()) {
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_DEBUG_CONTEXT, GLFW.GLFW_TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class SodiumPrograms {
private boolean hasBlockId;
private boolean hasMidUv;
private boolean hasNormal;
private boolean hasTangent;
private boolean hasMidBlock;

public SodiumPrograms(IrisRenderingPipeline pipeline, ProgramSet programSet, ProgramFallbackResolver resolver,
Expand All @@ -68,7 +67,7 @@ public SodiumPrograms(IrisRenderingPipeline pipeline, ProgramSet programSet, Pro
shaders.put(pass, shader);
}

WorldRenderingSettings.INSTANCE.setVertexFormat(FormatAnalyzer.createFormat(hasBlockId, hasNormal, hasMidUv, hasTangent, hasMidBlock));
WorldRenderingSettings.INSTANCE.setVertexFormat(FormatAnalyzer.createFormat(hasBlockId, hasNormal, hasMidUv, hasMidBlock));
}

private AlphaTest getAlphaTest(Pass pass, ProgramSource source) {
Expand Down Expand Up @@ -173,7 +172,6 @@ private GlProgram<ChunkShaderInterface> buildProgram(GlProgram.Builder builder,
if (!hasMidBlock) hasMidBlock = GL43C.glGetAttribLocation(handle, "at_midBlock") != -1;
if (!hasBlockId) hasBlockId = GL43C.glGetAttribLocation(handle, "mc_Entity") != -1;
if (!hasMidUv) hasMidUv = GL43C.glGetAttribLocation(handle, "mc_midTexCoord") != -1;
if (!hasTangent) hasTangent = GL43C.glGetAttribLocation(handle, "at_tangent") != -1;

return new SodiumShader(pipeline, pass, shader, handle, source.getDirectives().getBlendModeOverride().orElse(null),
createBufferBlendOverrides(source), customUniforms, flipState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ public static void transform(
root.rename("normalMatrix", "iris_NormalMatrix");
root.rename("chunkOffset", "u_RegionOffset");
if (parameters.type == PatchShaderType.VERTEX) {
boolean needsNormal = root.identifierIndex.has("vaNormal") || root.identifierIndex.has("at_tangent");
// _draw_translation replaced with Chunks[_draw_id].offset.xyz
root.replaceReferenceExpressions(t, "vaPosition", "_vert_position + _get_draw_translation(_draw_id)");
root.replaceReferenceExpressions(t, "vaColor", "_vert_color");
root.rename("vaNormal", "iris_Normal");
root.replaceReferenceExpressions(t, "vaNormal", "irs_Normal");
root.replaceReferenceExpressions(t, "at_tangent", "irs_Tangent");

root.replaceReferenceExpressions(t, "vaUV0", "_vert_tex_diffuse_coord");
root.replaceReferenceExpressions(t, "vaUV1", "ivec2(0, 10)");
root.replaceReferenceExpressions(t, "vaUV2", "a_LightAndData.xy");
Expand All @@ -32,7 +35,7 @@ public static void transform(
SodiumTransformer.replaceMidTexCoord(t, tree, root, 1.0f / 32768.0f);
SodiumTransformer.replaceMCEntity(t, tree, root);

SodiumTransformer.injectVertInit(t, tree, root, parameters);
SodiumTransformer.injectVertInit(t, tree, root, parameters, needsNormal);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static void transform(
replaceMidTexCoord(t, tree, root, 1.0f / 32768.0f);
replaceMCEntity(t, tree, root);

boolean needsNormal = root.identifierIndex.has("gl_Normal") || root.identifierIndex.has("at_tangent");

root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix0, "mat4(1.0)");
root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix1, "iris_LightmapTextureMatrix");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_FUNCTIONS, "uniform mat4 iris_LightmapTextureMatrix;");
Expand All @@ -52,9 +54,9 @@ public static void transform(

root.rename("gl_Color", "_vert_color");

if (parameters.type.glShaderType == ShaderType.VERTEX && root.identifierIndex.has("gl_Normal")) {
root.rename("gl_Normal", "iris_Normal");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "in vec3 iris_Normal;");
if (parameters.type.glShaderType == ShaderType.VERTEX && needsNormal) {
root.rename("gl_Normal", "irs_Normal");
root.replaceReferenceExpressions(t, "at_tangent", "irs_Tangent");
}

// TODO: Should probably add the normal matrix as a proper uniform that's
Expand Down Expand Up @@ -94,7 +96,7 @@ public static void transform(
// inject here so that _vert_position is available to the above. (injections
// inject in reverse order if performed piece-wise but in correct order if
// performed as an array of injections)
injectVertInit(t, tree, root, parameters);
injectVertInit(t, tree, root, parameters, needsNormal);
} else {
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
"uniform mat4 iris_ModelViewMatrix;",
Expand All @@ -111,7 +113,7 @@ public static void injectVertInit(
ASTParser t,
TranslationUnit tree,
Root root,
SodiumParameters parameters) {
SodiumParameters parameters, boolean needsNormal) {
tree.parseAndInjectNodes(t, ASTInjectionPoint.BEFORE_DECLARATIONS,
// translated from sodium's chunk_vertex.glsl
"vec3 _vert_position;",
Expand All @@ -131,8 +133,30 @@ public static void injectVertInit(
"const float TEXTURE_FUZZ_AMOUNT = 1.0 / 64.0;",
"const float TEXTURE_GROW_FACTOR = (1.0 - TEXTURE_FUZZ_AMOUNT) / TEXTURE_MAX_COORD;",
"uint _draw_id;",
"vec3 irs_Normal;",
"vec4 irs_Tangent;",
"const uint MATERIAL_USE_MIP_OFFSET = 0u;",
"""
vec3 oct_to_vec3(vec2 e) {
vec2 f = vec2(e.x * 2.0f - 1.0f, e.y * 2.0f - 1.0f);
vec3 n = vec3(f.x, f.y, 1.0f - abs(f.x) - abs(f.y));
float t = clamp(-n.z, 0.0f, 1.0f);
n.x += n.x >= 0.0f ? -t : t;
n.y += n.y >= 0.0f ? -t : t;
return normalize(n);
}
""",
"""
vec4 tangent_decode(vec2 e) {
vec2 oct_compressed = e;
oct_compressed.y = oct_compressed.y * 2 - 1;
float r_sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f;
oct_compressed.y = abs(oct_compressed.y);
vec3 res = oct_to_vec3(oct_compressed.xy);
return vec4(res, r_sign);
}
""",
"""
uvec3 _deinterleave_u20x3(uvec2 data) {
uvec3 hi = (uvec3(data.x) >> uvec3(0u, 10u, 20u)) & 0x3FFu;
uvec3 lo = (uvec3(data.y) >> uvec3(0u, 10u, 20u)) & 0x3FFu;
Expand All @@ -158,6 +182,8 @@ vec2 _get_texcoord_bias() {
"_vert_tex_diffuse_coord = _get_texcoord() + _get_texcoord_bias();" +
"_vert_tex_light_coord = vec2(a_LightAndData.xy);" +
"_vert_color = a_Color;" +
(needsNormal ? "irs_Normal = oct_to_vec3(iris_Normal.xy);" : "") +
(needsNormal ? "irs_Tangent = tangent_decode(iris_Normal.zw);" : "") +
"_draw_id = a_LightAndData[3]; }",

"uvec3 _get_relative_chunk_coord(uint pos) {\n" +
Expand All @@ -171,6 +197,7 @@ vec2 _get_texcoord_bias() {
addIfNotExists(root, t, tree, "a_TexCoord", Type.U32VEC2, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "a_Color", Type.F32VEC4, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "a_LightAndData", Type.U32VEC4, StorageQualifier.StorageType.IN);
if (needsNormal) addIfNotExists(root, t, tree, "iris_Normal", Type.F32VEC4, StorageQualifier.StorageType.IN);
tree.prependMainFunctionBody(t, "_vert_init();");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ 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);
}

public static byte toByte(float v) {
return (byte) ((byte) (v * 127) & 0xFF);
}

/**
* Packs the specified vector components into a 32-bit integer in XYZ ordering with the 8 bits of padding at the
* end.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

import net.irisshaders.iris.vertices.views.QuadView;
import net.irisshaders.iris.vertices.views.TriView;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Vector4f;

import static java.lang.Math.abs;

public abstract class NormalHelper {
private NormalHelper() {
Expand All @@ -37,6 +42,58 @@ public static int invertPackedNormal(int packed) {
return (packed & 0xFF000000) | (iz << 16) | (iy << 8) | ix;
}

public static void octahedronEncode(Vector2f output, float x, float y, float z) {
float nX = x, nY = y, nZ = z;

float invL1 = 1.0f / (Math.abs(nX) + Math.abs(nY) + Math.abs(nZ));
nX *= invL1;
nY *= invL1;
nZ *= invL1;

float oX, oY;
if (nZ >= 0.0f) {
oX = nX;
oY = nY;
} else {
float absNX = Math.abs(nX);
float absNY = Math.abs(nY);
oX = (1.0f - absNY) * (nX >= 0.0f ? 1.0f : -1.0f);
oY = (1.0f - absNX) * (nY >= 0.0f ? 1.0f : -1.0f);
}

oX = oX * 0.5f + 0.5f;
oY = oY * 0.5f + 0.5f;

output.set(oX, oY);
}

private static final float BIAS = 1.0f / 32767.0f;

public static void tangentEncode(Vector2f output, Vector4f tangent) {
octahedronEncode(output, tangent.x, tangent.y, tangent.z);
output.y = Math.max(output.y, BIAS);
output.y = output.y * 0.5f + 0.5f;
output.y = tangent.w >= 0.0f ? output.y : 1 - output.y;
}

static Vector4f octahedron_tangent_decode(Vector2f p_oct) {
Vector2f oct_compressed = new Vector2f(p_oct);
oct_compressed.y = oct_compressed.y * 2 - 1;
float r_sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f;
oct_compressed.y = Math.abs(oct_compressed.y);
Vector3f res = octahedron_decode(oct_compressed.x, oct_compressed.y);
return new Vector4f(res.x, res.y, res.z, r_sign);
}

private static Vector3f octahedron_decode(float inX, float inY) {
Vector2f f = new Vector2f(inX * 2.0f - 1.0f, inY * 2.0f - 1.0f);
Vector3f n = new Vector3f(f.x, f.y, 1.0f - Math.abs(f.x) - Math.abs(f.y));
float t = Mth.clamp(-n.z, 0.0f, 1.0f);
n.x += n.x >= 0 ? -t : t;
n.y += n.y >= 0 ? -t : t;
return n.normalize();
}

/**
* Computes the face normal of the given quad and saves it in the provided non-null vector.
*
Expand Down Expand Up @@ -83,15 +140,9 @@ public static void computeFaceNormalManual(@NotNull Vector3f saveTo,
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);

saveTo.normalize();
}

/**
Expand Down Expand Up @@ -336,7 +387,7 @@ public static int computeTangent(float normalX, float normalY, float normalZ, Tr
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,
public static int computeTangent(Vector4f output, 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) {
float edge1x = x1 - x0;
Expand Down Expand Up @@ -404,6 +455,10 @@ public static int computeTangent(float normalX, float normalY, float normalZ, fl
tangentW = 1.0F;
}

if (output != null) {
output.set(tangentx, tangenty, tangentz, tangentW);
}

return NormI8.pack(tangentx, tangenty, tangentz, tangentW);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void serialize(long src, long dst, int vertexCount) {
int quadCount = vertexCount / 4;
for (int i = 0; i < quadCount; i++) {
int normal = MemoryUtil.memGetInt(src + 32);
int tangent = NormalHelper.computeTangent(NormI8.unpackX(normal), NormI8.unpackY(normal), NormI8.unpackZ(normal), MemoryUtil.memGetFloat(src), MemoryUtil.memGetFloat(src + 4), MemoryUtil.memGetFloat(src + 8), MemoryUtil.memGetFloat(src + 16), MemoryUtil.memGetFloat(src + 20),
int tangent = NormalHelper.computeTangent(null, NormI8.unpackX(normal), NormI8.unpackY(normal), NormI8.unpackZ(normal), MemoryUtil.memGetFloat(src), MemoryUtil.memGetFloat(src + 4), MemoryUtil.memGetFloat(src + 8), MemoryUtil.memGetFloat(src + 16), MemoryUtil.memGetFloat(src + 20),
MemoryUtil.memGetFloat(src + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 4 + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 8 + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 16 + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 20 + EntityVertex.STRIDE),
MemoryUtil.memGetFloat(src + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 4 + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 8 + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 16 + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 20 + EntityVertex.STRIDE + EntityVertex.STRIDE));
float midU = 0, midV = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void serialize(long src, long dst, int vertexCount) {
int quadCount = vertexCount / 4;
for (int i = 0; i < quadCount; i++) {
int normal = MemoryUtil.memGetInt(src + 32);
int tangent = NormalHelper.computeTangent(NormI8.unpackX(normal), NormI8.unpackY(normal), NormI8.unpackZ(normal), MemoryUtil.memGetFloat(src), MemoryUtil.memGetFloat(src + 4), MemoryUtil.memGetFloat(src + 8), MemoryUtil.memGetFloat(src + 16), MemoryUtil.memGetFloat(src + 20),
int tangent = NormalHelper.computeTangent(null, NormI8.unpackX(normal), NormI8.unpackY(normal), NormI8.unpackZ(normal), MemoryUtil.memGetFloat(src), MemoryUtil.memGetFloat(src + 4), MemoryUtil.memGetFloat(src + 8), MemoryUtil.memGetFloat(src + 16), MemoryUtil.memGetFloat(src + 20),
MemoryUtil.memGetFloat(src + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 4 + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 8 + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 16 + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 20 + EntityVertex.STRIDE),
MemoryUtil.memGetFloat(src + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 4 + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 8 + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 16 + EntityVertex.STRIDE + EntityVertex.STRIDE), MemoryUtil.memGetFloat(src + 20 + EntityVertex.STRIDE + EntityVertex.STRIDE));
float midU = 0, midV = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FormatAnalyzer {
classMap.put((byte) 0, ChunkMeshFormats.COMPACT);
}

public static ChunkVertexType createFormat(boolean blockId, boolean normal, boolean midUV, boolean tangent, boolean midBlock) {
public static ChunkVertexType createFormat(boolean blockId, boolean normal, boolean midUV, boolean midBlock) {
byte key = 0;
if (blockId) {
key |= 1;
Expand All @@ -26,9 +26,7 @@ public static ChunkVertexType createFormat(boolean blockId, boolean normal, bool
if (midUV) {
key |= 4;
}
if (tangent) {
key |= 8;
}

if (midBlock) {
key |= 16;
}
Expand All @@ -39,7 +37,7 @@ public static ChunkVertexType createFormat(boolean blockId, boolean normal, bool

int offset = 20; // Normal Sodium stuff

int blockIdOffset, normalOffset, tangentOffset, midUvOffset, midBlockOffset;
int blockIdOffset, normalOffset, midUvOffset, midBlockOffset;

if (blockId) {
blockIdOffset = offset;
Expand All @@ -62,13 +60,6 @@ public static ChunkVertexType createFormat(boolean blockId, boolean normal, bool
midUvOffset = 0;
}

if (tangent) {
tangentOffset = offset;
offset += 4;
} else {
tangentOffset = 0;
}

if (midBlock) {
midBlockOffset = offset;
offset += 4;
Expand All @@ -94,15 +85,13 @@ public static ChunkVertexType createFormat(boolean blockId, boolean normal, bool
VERTEX_FORMAT.addElement(IrisChunkMeshAttributes.MID_TEX_COORD, 12, midUvOffset);
}

if (tangent) {
VERTEX_FORMAT.addElement(IrisChunkMeshAttributes.TANGENT, 13, tangentOffset);
}

if (midBlock) {
VERTEX_FORMAT.addElement(IrisChunkMeshAttributes.MID_BLOCK, 14, midBlockOffset);
}

System.out.println("Created a new format with " + offset + " stride: " + Integer.toBinaryString(key));


return classMap.computeIfAbsent(key, k -> new XHFPModelVertexType(VERTEX_FORMAT.build(), blockIdOffset, normalOffset, tangentOffset, midUvOffset, midBlockOffset));
return classMap.computeIfAbsent(key, k -> new XHFPModelVertexType(VERTEX_FORMAT.build(), blockIdOffset, normalOffset, midUvOffset, midBlockOffset));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class IrisChunkMeshAttributes {
public static final VertexFormatAttribute MID_TEX_COORD = new VertexFormatAttribute("midTexCoord", GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false);
public static final VertexFormatAttribute TANGENT = new VertexFormatAttribute("TANGENT", GlVertexAttributeFormat.BYTE, 4, true, false);
public static final VertexFormatAttribute NORMAL = new VertexFormatAttribute("NORMAL", GlVertexAttributeFormat.BYTE, 3, true, false);
public static final VertexFormatAttribute NORMAL = new VertexFormatAttribute("NORMAL", GlVertexAttributeFormat.BYTE, 4, true, false);
public static final VertexFormatAttribute BLOCK_ID = new VertexFormatAttribute("BLOCK_ID", GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true);
public static final VertexFormatAttribute MID_BLOCK = new VertexFormatAttribute("MID_BLOCK", GlVertexAttributeFormat.BYTE, 4, false, false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ public class XHFPModelVertexType implements ChunkVertexType {
private final GlVertexFormat format;
private final int normalOffset;
private final int blockIdOffset;
private final int tangentOffset;
private final int midBlockOffset;
private final int midUvOffset;

public XHFPModelVertexType(GlVertexFormat format, int blockIdOffset, int normalOffset, int tangentOffset, int midUvOffset, int midBlockOffset) {
public XHFPModelVertexType(GlVertexFormat format, int blockIdOffset, int normalOffset, int midUvOffset, int midBlockOffset) {
this.format = format;
this.blockIdOffset = blockIdOffset;
this.normalOffset = normalOffset;
this.tangentOffset = tangentOffset;
this.midUvOffset = midUvOffset;
this.midBlockOffset = midBlockOffset;
}
Expand All @@ -43,6 +41,6 @@ public GlVertexFormat getVertexFormat() {

@Override
public ChunkVertexEncoder getEncoder() {
return new XHFPTerrainVertex(blockIdOffset, normalOffset, tangentOffset, midUvOffset, midBlockOffset, format.getStride());
return new XHFPTerrainVertex(blockIdOffset, normalOffset, midUvOffset, midBlockOffset, format.getStride());
}
}
Loading

0 comments on commit 37a0e20

Please sign in to comment.