From 1dee5db9a111c0aac92a2420359f38d044fbddeb Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Wed, 21 Feb 2024 12:37:46 -0600 Subject: [PATCH] Debugging broken overlay/lighting - Break instance overlay/light back into 2 layout elements - Write light directly as an int - Make light a short backed float element - Clamp overlay coords in common.frag to avoid explosions - Explosions: integer vertex attributes sometimes blow up to massive sizes, greater than Short.MAX_VALUE which should not be possible. - Reorganize some texture/uniform binding code --- .../jozufozu/flywheel/backend/InternalVertex.java | 4 ++-- .../jozufozu/flywheel/backend/engine/MeshPool.java | 13 +++++++------ .../backend/engine/instancing/InstancingEngine.java | 2 +- .../backend/engine/textures/TextureBinder.java | 9 +++++---- .../flywheel/lib/instance/InstanceTypes.java | 12 ++++++------ .../assets/flywheel/flywheel/instance/oriented.vert | 4 ++-- .../flywheel/flywheel/instance/transformed.vert | 4 ++-- .../assets/flywheel/flywheel/internal/common.frag | 9 ++++++++- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/InternalVertex.java b/src/main/java/com/jozufozu/flywheel/backend/InternalVertex.java index f95e9d431..1eb861140 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/InternalVertex.java +++ b/src/main/java/com/jozufozu/flywheel/backend/InternalVertex.java @@ -4,9 +4,9 @@ import java.util.List; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.layout.FloatRepr; +import com.jozufozu.flywheel.api.layout.IntegerRepr; import com.jozufozu.flywheel.api.layout.Layout; import com.jozufozu.flywheel.api.layout.LayoutBuilder; -import com.jozufozu.flywheel.api.layout.UnsignedIntegerRepr; import com.jozufozu.flywheel.api.vertex.VertexView; import com.jozufozu.flywheel.backend.engine.LayoutAttributes; import com.jozufozu.flywheel.backend.gl.array.VertexAttribute; @@ -19,7 +19,7 @@ public final class InternalVertex { .vector("position", FloatRepr.FLOAT, 3) .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) .vector("tex", FloatRepr.FLOAT, 2) - .vector("overlay_light", UnsignedIntegerRepr.UNSIGNED_SHORT, 4) + .vector("overlay_light", IntegerRepr.SHORT, 4) .vector("normal", FloatRepr.NORMALIZED_BYTE, 3) .build(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/MeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/MeshPool.java index c204ae65c..40c5956d2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/MeshPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/MeshPool.java @@ -207,12 +207,13 @@ public class MeshPool { } public void setup(GlVertexArray vao) { - if (boundTo.add(vao)) { - MeshPool.this.indexPool.bind(vao); - vao.bindVertexBuffer(0, MeshPool.this.vbo.handle(), byteIndex, InternalVertex.STRIDE); - vao.bindAttributes(0, 0, InternalVertex.ATTRIBUTES); - } - } + if (!boundTo.add(vao)) { + return; + } + MeshPool.this.indexPool.bind(vao); + vao.bindVertexBuffer(0, MeshPool.this.vbo.handle(), byteIndex, InternalVertex.STRIDE); + vao.bindAttributes(0, 0, InternalVertex.ATTRIBUTES); + } public void acquire() { referenceCount++; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java index eed31cffc..74e05798a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java @@ -64,6 +64,7 @@ public class InstancingEngine extends AbstractEngine { } try (var state = GlStateTracker.getRestoreState()) { + Uniforms.bindForDraw(); render(drawSet); } } @@ -104,7 +105,6 @@ public class InstancingEngine extends AbstractEngine { var program = programs.get(shader.instanceType(), context.contextShader()); program.bind(); - Uniforms.bindForDraw(); uploadMaterialUniform(program, material); context.prepare(material, program, textures); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java index 6b7c2c2e9..eabe4c679 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java @@ -48,14 +48,15 @@ public class TextureBinder { public static void bindLightAndOverlay() { var gameRenderer = Minecraft.getInstance().gameRenderer; - gameRenderer.overlayTexture() - .setupOverlayColor(); - gameRenderer.lightTexture() - .turnOnLightLayer(); GlTextureUnit.T1.makeActive(); + gameRenderer.overlayTexture() + .setupOverlayColor(); RenderSystem.bindTexture(RenderSystem.getShaderTexture(1)); + GlTextureUnit.T2.makeActive(); + gameRenderer.lightTexture() + .turnOnLightLayer(); RenderSystem.bindTexture(RenderSystem.getShaderTexture(2)); } diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java index cfb69cbb5..df6c92fd0 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java @@ -14,7 +14,8 @@ public final class InstanceTypes { public static final InstanceType TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new) .layout(LayoutBuilder.create() .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) - .vector("overlay_light", IntegerRepr.SHORT, 4) + .vector("overlay", IntegerRepr.SHORT, 2) + .vector("light", FloatRepr.UNSIGNED_SHORT, 2) .matrix("pose", FloatRepr.FLOAT, 4) .matrix("normal", FloatRepr.FLOAT, 3) .build()) @@ -24,8 +25,7 @@ public final class InstanceTypes { MemoryUtil.memPutByte(ptr + 2, instance.b); MemoryUtil.memPutByte(ptr + 3, instance.a); MemoryUtil.memPutInt(ptr + 4, instance.overlay); - MemoryUtil.memPutShort(ptr + 8, instance.blockLight); - MemoryUtil.memPutShort(ptr + 10, instance.skyLight); + MemoryUtil.memPutInt(ptr + 8, (int) instance.blockLight | (int) instance.skyLight << 16); MatrixMath.writeUnsafe(instance.model, ptr + 12); MatrixMath.writeUnsafe(instance.normal, ptr + 76); }) @@ -36,7 +36,8 @@ public final class InstanceTypes { public static final InstanceType ORIENTED = SimpleInstanceType.builder(OrientedInstance::new) .layout(LayoutBuilder.create() .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) - .vector("overlay_light", IntegerRepr.SHORT, 4) + .vector("overlay", IntegerRepr.SHORT, 2) + .vector("light", FloatRepr.UNSIGNED_SHORT, 2) .vector("position", FloatRepr.FLOAT, 3) .vector("pivot", FloatRepr.FLOAT, 3) .vector("rotation", FloatRepr.FLOAT, 4) @@ -47,8 +48,7 @@ public final class InstanceTypes { MemoryUtil.memPutByte(ptr + 2, instance.b); MemoryUtil.memPutByte(ptr + 3, instance.a); MemoryUtil.memPutInt(ptr + 4, instance.overlay); - MemoryUtil.memPutShort(ptr + 8, instance.blockLight); - MemoryUtil.memPutShort(ptr + 10, instance.skyLight); + MemoryUtil.memPutInt(ptr + 8, (int) instance.blockLight | (int) instance.skyLight << 16); MemoryUtil.memPutFloat(ptr + 12, instance.posX); MemoryUtil.memPutFloat(ptr + 16, instance.posY); MemoryUtil.memPutFloat(ptr + 20, instance.posZ); diff --git a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert index a6905deb3..dc77d53d9 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert @@ -4,6 +4,6 @@ void flw_instanceVertex(in FlwInstance i) { flw_vertexPos = vec4(rotateByQuaternion(flw_vertexPos.xyz - i.pivot, i.rotation) + i.pivot + i.position, 1.0); flw_vertexNormal = rotateByQuaternion(flw_vertexNormal, i.rotation); flw_vertexColor = i.color; - flw_vertexOverlay = i.overlay_light.xy; - flw_vertexLight = i.overlay_light.zw / 15.0; + flw_vertexOverlay = i.overlay; + flw_vertexLight = i.light / 15.; } diff --git a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert index f4a94bd0f..2bf7de822 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert @@ -2,6 +2,6 @@ void flw_instanceVertex(in FlwInstance i) { flw_vertexPos = i.pose * flw_vertexPos; flw_vertexNormal = i.normal * flw_vertexNormal; flw_vertexColor = i.color; - flw_vertexOverlay = i.overlay_light.xy; - flw_vertexLight = i.overlay_light.zw / 15.0; + flw_vertexOverlay = i.overlay; + flw_vertexLight = i.light / 15.; } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common.frag b/src/main/resources/assets/flywheel/flywheel/internal/common.frag index 661ae8d95..b0cacfb2e 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/common.frag +++ b/src/main/resources/assets/flywheel/flywheel/internal/common.frag @@ -37,7 +37,14 @@ void _flw_main() { } if (flw_material.useOverlay) { - vec4 overlayColor = texelFetch(_flw_overlayTex, flw_fragOverlay, 0); + // Need to clamp the overlay texture coords to sane coordinates because integer vertex attributes explode on + // some drivers for some draw calls. + // 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)); + + vec4 overlayColor = texelFetch(_flw_overlayTex, actualCoord, 0); color.rgb = mix(overlayColor.rgb, color.rgb, overlayColor.a); }