From 433129d5a04852ce677ccdf87757b7d64bb3b9cd Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 22 Feb 2024 12:31:29 -0600 Subject: [PATCH] Underlay - Clamp overlay from 0 to 10 rather than 3 to 10 to fix everything appearing washed out - Fix normalized short backed float vectors being unpacked incorrectly - Set overlay in shadow instance component to fix shadows appearing red - Indicative of the ivec issue being present for divisor 0 vertex attributes --- .../compile/component/IndirectComponent.java | 38 +++++++++++++++---- .../flywheel/flywheel/instance/shadow.vert | 1 + .../flywheel/flywheel/internal/common.frag | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/component/IndirectComponent.java b/src/main/java/com/jozufozu/flywheel/backend/compile/component/IndirectComponent.java index 7d7e31ccf..78d8f88a1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/component/IndirectComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/component/IndirectComponent.java @@ -207,7 +207,7 @@ public class IndirectComponent implements SourceComponent { return unpackUnsignedVector(fieldName, unsignedIntegerRepr, packed, size); } else if (repr instanceof FloatRepr floatRepr) { return unpackFloatVector(fieldName, floatRepr, packed, size); - } + } throw new IllegalArgumentException("Unknown repr " + repr); } @@ -230,10 +230,10 @@ public class IndirectComponent implements SourceComponent { private static GlslExpr unpackFloatVector(String fieldName, FloatRepr floatRepr, GlslStruct packed, int size) { return switch (floatRepr) { - case NORMALIZED_BYTE -> unpackBuiltin(fieldName, packed, size, "unpackSnorm4x8"); - case NORMALIZED_UNSIGNED_BYTE -> unpackBuiltin(fieldName, packed, size, "unpackUnorm4x8"); - case NORMALIZED_SHORT -> unpackBuiltin(fieldName, packed, size, "unpackSnorm2x16"); - case NORMALIZED_UNSIGNED_SHORT -> unpackBuiltin(fieldName, packed, size, "unpackUnorm2x16"); + case NORMALIZED_BYTE -> unpackByteBuiltin(fieldName, packed, size, "unpackSnorm4x8"); + case NORMALIZED_UNSIGNED_BYTE -> unpackByteBuiltin(fieldName, packed, size, "unpackUnorm4x8"); + case NORMALIZED_SHORT -> unpackShortBuiltin(fieldName, packed, size, "unpackSnorm2x16"); + case NORMALIZED_UNSIGNED_SHORT -> unpackShortBuiltin(fieldName, packed, size, "unpackUnorm2x16"); case NORMALIZED_INT -> unpack(fieldName, packed, size, "int", "vec" + size, e -> e.div(2147483647f) .clamp(-1, 1)); case NORMALIZED_UNSIGNED_INT -> @@ -260,7 +260,7 @@ public class IndirectComponent implements SourceComponent { .rsh(bitPos); args.add(perElement.apply(element)); } - return GlslExpr.call(outType + size, args); + return GlslExpr.call(outType, args); } private static GlslExpr unpackShortBacked(String fieldName, GlslStruct packed, int size, String outType, Function perElement) { @@ -296,7 +296,7 @@ public class IndirectComponent implements SourceComponent { return GlslExpr.call(outType, args); } - private static GlslExpr unpackBuiltin(String fieldName, GlslStruct packed, int size, String func) { + private static GlslExpr unpackByteBuiltin(String fieldName, GlslStruct packed, int size, String func) { packed.addField("uint", fieldName); GlslExpr expr = UNPACKING_VARIABLE.access(fieldName) .callFunction(func); @@ -308,6 +308,30 @@ public class IndirectComponent implements SourceComponent { }; } + private static GlslExpr unpackShortBuiltin(String fieldName, GlslStruct packed, int size, String func) { + if (size == 2) { + packed.addField("uint", fieldName); + return UNPACKING_VARIABLE.access(fieldName) + .callFunction(func); + } else { + var name0 = fieldName + "_" + 0; + var name1 = fieldName + "_" + 1; + packed.addField("uint", name0); + packed.addField("uint", name1); + GlslExpr xy = UNPACKING_VARIABLE.access(name0) + .callFunction(func); + + GlslExpr zw = UNPACKING_VARIABLE.access(name1) + .callFunction(func); + + if (size == 3) { + return GlslExpr.call("vec3", List.of(xy.swizzle("xy"), zw.swizzle("x"))); + } else { + return GlslExpr.call("vec4", List.of(xy, zw)); + } + } + } + private static GlslExpr unpackMatrix(String name, GlslStruct packed, MatrixElementType matrix) { var repr = matrix.repr(); diff --git a/src/main/resources/assets/flywheel/flywheel/instance/shadow.vert b/src/main/resources/assets/flywheel/flywheel/instance/shadow.vert index 97649f624..d9c0a6606 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/shadow.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/shadow.vert @@ -7,4 +7,5 @@ void flw_instanceVertex(in FlwInstance i) { flw_vertexTexCoord = (flw_vertexPos.xz - i.entityPosXZ) * 0.5 / i.radius + 0.5; flw_vertexColor.a = i.alpha; + flw_vertexOverlay = ivec2(0, 10); } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common.frag b/src/main/resources/assets/flywheel/flywheel/internal/common.frag index b0cacfb2e..61276cbe8 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/common.frag +++ b/src/main/resources/assets/flywheel/flywheel/internal/common.frag @@ -42,7 +42,7 @@ void _flw_main() { // This can be removed once instancing uses sampler buffers, though // we may need a solution for the internal vertex format. Perhaps // pass as floats and convert to integers in the shader? - ivec2 actualCoord = clamp(flw_fragOverlay, ivec2(3), ivec2(10)); + ivec2 actualCoord = clamp(flw_fragOverlay, 0, 10); vec4 overlayColor = texelFetch(_flw_overlayTex, actualCoord, 0); color.rgb = mix(overlayColor.rgb, color.rgb, overlayColor.a);