Fix diffuse lighting in batching engine

- Complete hack that definitely won't come back to bite me
 - Also slighting change some unsafe writing stuff
This commit is contained in:
Jozufozu 2021-12-22 16:11:56 -08:00
parent 80d41a76af
commit 99a50631cb
7 changed files with 60 additions and 50 deletions

View file

@ -9,9 +9,11 @@ import com.jozufozu.flywheel.backend.RenderLayer;
import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.backend.instancing.TaskEngine;
import com.jozufozu.flywheel.backend.instancing.Engine; import com.jozufozu.flywheel.backend.instancing.Engine;
import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.event.RenderLayerEvent;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Matrix4f;
import net.minecraft.client.Camera; import net.minecraft.client.Camera;
import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.MultiBufferSource;
@ -60,6 +62,15 @@ public class BatchingEngine implements Engine, MultiBufferSource {
stack.popPose(); 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 // TODO: when/if this causes trouble with shaders, try to inject our BufferBuilders
// into the RenderBuffers from context. // into the RenderBuffers from context.
buffers.forEach((type, builder) -> type.end(builder, 0, 0, 0)); buffers.forEach((type, builder) -> type.end(builder, 0, 0, 0));

View file

@ -14,12 +14,12 @@ public abstract class BasicWriterUnsafe<D extends BasicData> extends UnsafeBuffe
@Override @Override
protected void writeInternal(D d) { protected void writeInternal(D d) {
long addr = writePointer; long ptr = writePointer;
MemoryUtil.memPutByte(addr, (byte) (d.blockLight << 4)); MemoryUtil.memPutByte(ptr, (byte) (d.blockLight << 4));
MemoryUtil.memPutByte(addr + 1, (byte) (d.skyLight << 4)); MemoryUtil.memPutByte(ptr + 1, (byte) (d.skyLight << 4));
MemoryUtil.memPutByte(addr + 2, d.r); MemoryUtil.memPutByte(ptr + 2, d.r);
MemoryUtil.memPutByte(addr + 3, d.g); MemoryUtil.memPutByte(ptr + 3, d.g);
MemoryUtil.memPutByte(addr + 4, d.b); MemoryUtil.memPutByte(ptr + 4, d.b);
MemoryUtil.memPutByte(addr + 5, d.a); MemoryUtil.memPutByte(ptr + 5, d.a);
} }
} }

View file

@ -14,10 +14,9 @@ public class ModelWriterUnsafe extends BasicWriterUnsafe<ModelData> {
@Override @Override
protected void writeInternal(ModelData d) { protected void writeInternal(ModelData d) {
super.writeInternal(d); super.writeInternal(d);
long addr = writePointer + 6; long ptr = writePointer + 6;
((WriteUnsafe) (Object) d.model).writeUnsafe(addr); ((WriteUnsafe) (Object) d.model).writeUnsafe(ptr);
addr += 4 * 16; ((WriteUnsafe) (Object) d.normal).writeUnsafe(ptr + 4 * 16);
((WriteUnsafe) (Object) d.normal).writeUnsafe(addr);
} }
} }

View file

@ -13,18 +13,18 @@ public class OrientedWriterUnsafe extends BasicWriterUnsafe<OrientedData> {
@Override @Override
protected void writeInternal(OrientedData d) { protected void writeInternal(OrientedData d) {
long addr = writePointer; long ptr = writePointer;
super.writeInternal(d); super.writeInternal(d);
MemoryUtil.memPutFloat(addr + 6, d.posX); MemoryUtil.memPutFloat(ptr + 6, d.posX);
MemoryUtil.memPutFloat(addr + 10, d.posY); MemoryUtil.memPutFloat(ptr + 10, d.posY);
MemoryUtil.memPutFloat(addr + 14, d.posZ); MemoryUtil.memPutFloat(ptr + 14, d.posZ);
MemoryUtil.memPutFloat(addr + 18, d.pivotX); MemoryUtil.memPutFloat(ptr + 18, d.pivotX);
MemoryUtil.memPutFloat(addr + 22, d.pivotY); MemoryUtil.memPutFloat(ptr + 22, d.pivotY);
MemoryUtil.memPutFloat(addr + 26, d.pivotZ); MemoryUtil.memPutFloat(ptr + 26, d.pivotZ);
MemoryUtil.memPutFloat(addr + 30, d.qX); MemoryUtil.memPutFloat(ptr + 30, d.qX);
MemoryUtil.memPutFloat(addr + 34, d.qY); MemoryUtil.memPutFloat(ptr + 34, d.qY);
MemoryUtil.memPutFloat(addr + 38, d.qZ); MemoryUtil.memPutFloat(ptr + 38, d.qZ);
MemoryUtil.memPutFloat(addr + 42, d.qW); MemoryUtil.memPutFloat(ptr + 42, d.qW);
} }
} }

View file

@ -27,16 +27,16 @@ public abstract class Matrix3fMixin implements WriteUnsafe, WriteSafe {
@Shadow protected float m22; @Shadow protected float m22;
@Override @Override
public void writeUnsafe(long addr) { public void writeUnsafe(long ptr) {
MemoryUtil.memPutFloat(addr, m00); MemoryUtil.memPutFloat(ptr, m00);
MemoryUtil.memPutFloat(addr += 4L, m10); MemoryUtil.memPutFloat(ptr + 4, m10);
MemoryUtil.memPutFloat(addr += 4L, m20); MemoryUtil.memPutFloat(ptr + 8, m20);
MemoryUtil.memPutFloat(addr += 4L, m01); MemoryUtil.memPutFloat(ptr + 12, m01);
MemoryUtil.memPutFloat(addr += 4L, m11); MemoryUtil.memPutFloat(ptr + 16, m11);
MemoryUtil.memPutFloat(addr += 4L, m21); MemoryUtil.memPutFloat(ptr + 20, m21);
MemoryUtil.memPutFloat(addr += 4L, m02); MemoryUtil.memPutFloat(ptr + 24, m02);
MemoryUtil.memPutFloat(addr += 4L, m12); MemoryUtil.memPutFloat(ptr + 28, m12);
MemoryUtil.memPutFloat(addr += 4L, m22); MemoryUtil.memPutFloat(ptr + 32, m22);
} }
@Override @Override

View file

@ -34,23 +34,23 @@ public abstract class Matrix4fMixin implements WriteUnsafe, WriteSafe {
@Shadow protected float m33; @Shadow protected float m33;
@Override @Override
public void writeUnsafe(long addr) { public void writeUnsafe(long ptr) {
MemoryUtil.memPutFloat(addr, m00); MemoryUtil.memPutFloat(ptr, m00);
MemoryUtil.memPutFloat(addr += 4L, m10); MemoryUtil.memPutFloat(ptr + 4, m10);
MemoryUtil.memPutFloat(addr += 4L, m20); MemoryUtil.memPutFloat(ptr + 8, m20);
MemoryUtil.memPutFloat(addr += 4L, m30); MemoryUtil.memPutFloat(ptr + 12, m30);
MemoryUtil.memPutFloat(addr += 4L, m01); MemoryUtil.memPutFloat(ptr + 16, m01);
MemoryUtil.memPutFloat(addr += 4L, m11); MemoryUtil.memPutFloat(ptr + 20, m11);
MemoryUtil.memPutFloat(addr += 4L, m21); MemoryUtil.memPutFloat(ptr + 24, m21);
MemoryUtil.memPutFloat(addr += 4L, m31); MemoryUtil.memPutFloat(ptr + 28, m31);
MemoryUtil.memPutFloat(addr += 4L, m02); MemoryUtil.memPutFloat(ptr + 32, m02);
MemoryUtil.memPutFloat(addr += 4L, m12); MemoryUtil.memPutFloat(ptr + 36, m12);
MemoryUtil.memPutFloat(addr += 4L, m22); MemoryUtil.memPutFloat(ptr + 40, m22);
MemoryUtil.memPutFloat(addr += 4L, m32); MemoryUtil.memPutFloat(ptr + 44, m32);
MemoryUtil.memPutFloat(addr += 4L, m03); MemoryUtil.memPutFloat(ptr + 48, m03);
MemoryUtil.memPutFloat(addr += 4L, m13); MemoryUtil.memPutFloat(ptr + 52, m13);
MemoryUtil.memPutFloat(addr += 4L, m23); MemoryUtil.memPutFloat(ptr + 56, m23);
MemoryUtil.memPutFloat(addr += 4L, m33); MemoryUtil.memPutFloat(ptr + 60, m33);
} }
@Override @Override

View file

@ -5,5 +5,5 @@ public interface WriteUnsafe {
/** /**
* Write the contents of this object into sequential memory starting at the given address. * Write the contents of this object into sequential memory starting at the given address.
*/ */
void writeUnsafe(long addr); void writeUnsafe(long ptr);
} }