From 99a50631cbaa9d937774eb0eaeaef3a23a49ae29 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Wed, 22 Dec 2021 16:11:56 -0800 Subject: [PATCH] Fix diffuse lighting in batching engine - Complete hack that definitely won't come back to bite me - Also slighting change some unsafe writing stuff --- .../instancing/batching/BatchingEngine.java | 11 ++++++ .../core/materials/BasicWriterUnsafe.java | 14 ++++---- .../materials/model/ModelWriterUnsafe.java | 7 ++-- .../oriented/OrientedWriterUnsafe.java | 22 ++++++------ .../flywheel/mixin/matrix/Matrix3fMixin.java | 20 +++++------ .../flywheel/mixin/matrix/Matrix4fMixin.java | 34 +++++++++---------- .../jozufozu/flywheel/util/WriteUnsafe.java | 2 +- 7 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java index 7af1eb14a..c8ebbed58 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java @@ -9,9 +9,11 @@ import com.jozufozu.flywheel.backend.RenderLayer; import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.backend.instancing.Engine; import com.jozufozu.flywheel.event.RenderLayerEvent; +import com.mojang.blaze3d.platform.Lighting; import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.math.Matrix4f; import net.minecraft.client.Camera; import net.minecraft.client.renderer.MultiBufferSource; @@ -60,6 +62,15 @@ public class BatchingEngine implements Engine, MultiBufferSource { stack.popPose(); + // FIXME: this probably breaks some vanilla stuff but it works much better for flywheel + Matrix4f mat = new Matrix4f(); + mat.setIdentity(); + if (event.getWorld().effects().constantAmbientLight()) { + Lighting.setupNetherLevel(mat); + } else { + Lighting.setupLevel(mat); + } + // TODO: when/if this causes trouble with shaders, try to inject our BufferBuilders // into the RenderBuffers from context. buffers.forEach((type, builder) -> type.end(builder, 0, 0, 0)); diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/BasicWriterUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/materials/BasicWriterUnsafe.java index 1ece742b2..0ce2f7a75 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/BasicWriterUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/BasicWriterUnsafe.java @@ -14,12 +14,12 @@ public abstract class BasicWriterUnsafe extends UnsafeBuffe @Override protected void writeInternal(D d) { - long addr = writePointer; - MemoryUtil.memPutByte(addr, (byte) (d.blockLight << 4)); - MemoryUtil.memPutByte(addr + 1, (byte) (d.skyLight << 4)); - MemoryUtil.memPutByte(addr + 2, d.r); - MemoryUtil.memPutByte(addr + 3, d.g); - MemoryUtil.memPutByte(addr + 4, d.b); - MemoryUtil.memPutByte(addr + 5, d.a); + long ptr = writePointer; + MemoryUtil.memPutByte(ptr, (byte) (d.blockLight << 4)); + MemoryUtil.memPutByte(ptr + 1, (byte) (d.skyLight << 4)); + MemoryUtil.memPutByte(ptr + 2, d.r); + MemoryUtil.memPutByte(ptr + 3, d.g); + MemoryUtil.memPutByte(ptr + 4, d.b); + MemoryUtil.memPutByte(ptr + 5, d.a); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelWriterUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelWriterUnsafe.java index ba842aabd..6fac04507 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelWriterUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelWriterUnsafe.java @@ -14,10 +14,9 @@ public class ModelWriterUnsafe extends BasicWriterUnsafe { @Override protected void writeInternal(ModelData d) { super.writeInternal(d); - long addr = writePointer + 6; + long ptr = writePointer + 6; - ((WriteUnsafe) (Object) d.model).writeUnsafe(addr); - addr += 4 * 16; - ((WriteUnsafe) (Object) d.normal).writeUnsafe(addr); + ((WriteUnsafe) (Object) d.model).writeUnsafe(ptr); + ((WriteUnsafe) (Object) d.normal).writeUnsafe(ptr + 4 * 16); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedWriterUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedWriterUnsafe.java index 201faf6eb..f7db693a3 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedWriterUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedWriterUnsafe.java @@ -13,18 +13,18 @@ public class OrientedWriterUnsafe extends BasicWriterUnsafe { @Override protected void writeInternal(OrientedData d) { - long addr = writePointer; + long ptr = writePointer; super.writeInternal(d); - MemoryUtil.memPutFloat(addr + 6, d.posX); - MemoryUtil.memPutFloat(addr + 10, d.posY); - MemoryUtil.memPutFloat(addr + 14, d.posZ); - MemoryUtil.memPutFloat(addr + 18, d.pivotX); - MemoryUtil.memPutFloat(addr + 22, d.pivotY); - MemoryUtil.memPutFloat(addr + 26, d.pivotZ); - MemoryUtil.memPutFloat(addr + 30, d.qX); - MemoryUtil.memPutFloat(addr + 34, d.qY); - MemoryUtil.memPutFloat(addr + 38, d.qZ); - MemoryUtil.memPutFloat(addr + 42, d.qW); + MemoryUtil.memPutFloat(ptr + 6, d.posX); + MemoryUtil.memPutFloat(ptr + 10, d.posY); + MemoryUtil.memPutFloat(ptr + 14, d.posZ); + MemoryUtil.memPutFloat(ptr + 18, d.pivotX); + MemoryUtil.memPutFloat(ptr + 22, d.pivotY); + MemoryUtil.memPutFloat(ptr + 26, d.pivotZ); + MemoryUtil.memPutFloat(ptr + 30, d.qX); + MemoryUtil.memPutFloat(ptr + 34, d.qY); + MemoryUtil.memPutFloat(ptr + 38, d.qZ); + MemoryUtil.memPutFloat(ptr + 42, d.qW); } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java index 1c5449100..fc7dd333c 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java @@ -27,16 +27,16 @@ public abstract class Matrix3fMixin implements WriteUnsafe, WriteSafe { @Shadow protected float m22; @Override - public void writeUnsafe(long addr) { - MemoryUtil.memPutFloat(addr, m00); - MemoryUtil.memPutFloat(addr += 4L, m10); - MemoryUtil.memPutFloat(addr += 4L, m20); - MemoryUtil.memPutFloat(addr += 4L, m01); - MemoryUtil.memPutFloat(addr += 4L, m11); - MemoryUtil.memPutFloat(addr += 4L, m21); - MemoryUtil.memPutFloat(addr += 4L, m02); - MemoryUtil.memPutFloat(addr += 4L, m12); - MemoryUtil.memPutFloat(addr += 4L, m22); + public void writeUnsafe(long ptr) { + MemoryUtil.memPutFloat(ptr, m00); + MemoryUtil.memPutFloat(ptr + 4, m10); + MemoryUtil.memPutFloat(ptr + 8, m20); + MemoryUtil.memPutFloat(ptr + 12, m01); + MemoryUtil.memPutFloat(ptr + 16, m11); + MemoryUtil.memPutFloat(ptr + 20, m21); + MemoryUtil.memPutFloat(ptr + 24, m02); + MemoryUtil.memPutFloat(ptr + 28, m12); + MemoryUtil.memPutFloat(ptr + 32, m22); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java index 33bc6da8f..8d98d502e 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java @@ -34,23 +34,23 @@ public abstract class Matrix4fMixin implements WriteUnsafe, WriteSafe { @Shadow protected float m33; @Override - public void writeUnsafe(long addr) { - MemoryUtil.memPutFloat(addr, m00); - MemoryUtil.memPutFloat(addr += 4L, m10); - MemoryUtil.memPutFloat(addr += 4L, m20); - MemoryUtil.memPutFloat(addr += 4L, m30); - MemoryUtil.memPutFloat(addr += 4L, m01); - MemoryUtil.memPutFloat(addr += 4L, m11); - MemoryUtil.memPutFloat(addr += 4L, m21); - MemoryUtil.memPutFloat(addr += 4L, m31); - MemoryUtil.memPutFloat(addr += 4L, m02); - MemoryUtil.memPutFloat(addr += 4L, m12); - MemoryUtil.memPutFloat(addr += 4L, m22); - MemoryUtil.memPutFloat(addr += 4L, m32); - MemoryUtil.memPutFloat(addr += 4L, m03); - MemoryUtil.memPutFloat(addr += 4L, m13); - MemoryUtil.memPutFloat(addr += 4L, m23); - MemoryUtil.memPutFloat(addr += 4L, m33); + public void writeUnsafe(long ptr) { + MemoryUtil.memPutFloat(ptr, m00); + MemoryUtil.memPutFloat(ptr + 4, m10); + MemoryUtil.memPutFloat(ptr + 8, m20); + MemoryUtil.memPutFloat(ptr + 12, m30); + MemoryUtil.memPutFloat(ptr + 16, m01); + MemoryUtil.memPutFloat(ptr + 20, m11); + MemoryUtil.memPutFloat(ptr + 24, m21); + MemoryUtil.memPutFloat(ptr + 28, m31); + MemoryUtil.memPutFloat(ptr + 32, m02); + MemoryUtil.memPutFloat(ptr + 36, m12); + MemoryUtil.memPutFloat(ptr + 40, m22); + MemoryUtil.memPutFloat(ptr + 44, m32); + MemoryUtil.memPutFloat(ptr + 48, m03); + MemoryUtil.memPutFloat(ptr + 52, m13); + MemoryUtil.memPutFloat(ptr + 56, m23); + MemoryUtil.memPutFloat(ptr + 60, m33); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/util/WriteUnsafe.java b/src/main/java/com/jozufozu/flywheel/util/WriteUnsafe.java index 7d963ee5c..1b4b6e985 100644 --- a/src/main/java/com/jozufozu/flywheel/util/WriteUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/util/WriteUnsafe.java @@ -5,5 +5,5 @@ public interface WriteUnsafe { /** * Write the contents of this object into sequential memory starting at the given address. */ - void writeUnsafe(long addr); + void writeUnsafe(long ptr); }