From d84a88d78d4a6bdd94695e9c3255aeedbec4083c Mon Sep 17 00:00:00 2001 From: tterrag Date: Sat, 4 Apr 2020 16:21:41 -0400 Subject: [PATCH] Revive forge lighting pipeline, disabled by default for now Remove a lot of light value convolution by using 0..1 for nearly everything Fix a lot of TODOs that are needed for the lighting pipeline to function Potential fix for #6425 --- .../BlockRendererDispatcher.java.patch | 9 +++ .../client/model/pipeline/BlockInfo.java | 14 ++-- .../pipeline/ForgeBlockModelRenderer.java | 16 ++-- .../model/pipeline/VertexBufferConsumer.java | 79 ++++++++++--------- .../model/pipeline/VertexLighterFlat.java | 42 +++++++--- .../model/pipeline/VertexLighterSmoothAo.java | 5 +- .../minecraftforge/common/ForgeConfig.java | 5 ++ 7 files changed, 102 insertions(+), 68 deletions(-) diff --git a/patches/minecraft/net/minecraft/client/renderer/BlockRendererDispatcher.java.patch b/patches/minecraft/net/minecraft/client/renderer/BlockRendererDispatcher.java.patch index 312a84e44..51fd0a60b 100644 --- a/patches/minecraft/net/minecraft/client/renderer/BlockRendererDispatcher.java.patch +++ b/patches/minecraft/net/minecraft/client/renderer/BlockRendererDispatcher.java.patch @@ -1,5 +1,14 @@ --- a/net/minecraft/client/renderer/BlockRendererDispatcher.java +++ b/net/minecraft/client/renderer/BlockRendererDispatcher.java +@@ -32,7 +32,7 @@ + public BlockRendererDispatcher(BlockModelShapes p_i46577_1_, BlockColors p_i46577_2_) { + this.field_175028_a = p_i46577_1_; + this.field_228790_e_ = p_i46577_2_; +- this.field_175027_c = new BlockModelRenderer(this.field_228790_e_); ++ this.field_175027_c = new net.minecraftforge.client.model.pipeline.ForgeBlockModelRenderer(this.field_228790_e_); + this.field_175025_e = new FluidBlockRenderer(); + } + @@ -40,18 +40,26 @@ return this.field_175028_a; } diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java b/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java index 9e9885d74..6f4e9c3c1 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java @@ -22,6 +22,8 @@ package net.minecraftforge.client.model.pipeline; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.color.BlockColors; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; @@ -113,7 +115,7 @@ public class BlockInfo if (s1 == 0 && !t1) s1 = Math.max(0, c - 1); if (s2 == 0 && !t2) s2 = Math.max(0, c - 1); if (s3 == 0 && !t3) s3 = Math.max(0, Math.max(s1, s2) - 1); - return (float) (c + s1 + s2 + s3) * 0x20 / (4 * 0xFFFF); + return (c + s1 + s2 + s3) / (0xF * 4f); } public void updateLightMatrix() @@ -127,9 +129,9 @@ public class BlockInfo BlockPos pos = blockPos.add(x - 1, y - 1, z - 1); BlockState state = world.getBlockState(pos); t[x][y][z] = state.getOpacity(world, pos) < 15; - int brightness = 0x00FF00FF; // FIXME: state.getPackedLightmapCoords(world, pos); - s[x][y][z] = (brightness >> 0x14) & 0xF; - b[x][y][z] = (brightness >> 0x04) & 0xF; + int brightness = WorldRenderer.getCombinedLight(world, pos); + s[x][y][z] = LightTexture.getLightSky(brightness); + b[x][y][z] = LightTexture.getLightBlock(brightness); ao[x][y][z] = state.getAmbientOcclusionLightValue(world, pos); } } @@ -195,12 +197,12 @@ public class BlockInfo public void updateFlatLighting() { full = Block.isOpaque(state.getCollisionShape(world, blockPos)); - packed[0] = 0x00FF00FF; // FIXME: state.getPackedLightmapCoords(world, blockPos); + packed[0] = WorldRenderer.getCombinedLight(world, blockPos); for (Direction side : SIDES) { int i = side.ordinal() + 1; - packed[i] = 0x00FF00FF; // FIXME: state.getPackedLightmapCoords(world, blockPos.offset(side)); + packed[i] = WorldRenderer.getCombinedLight(world, blockPos.offset(side)); } } diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/ForgeBlockModelRenderer.java b/src/main/java/net/minecraftforge/client/model/pipeline/ForgeBlockModelRenderer.java index 9d336ae08..8b05a0df4 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/ForgeBlockModelRenderer.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/ForgeBlockModelRenderer.java @@ -36,7 +36,6 @@ import net.minecraft.world.ILightReader; import net.minecraftforge.client.model.data.IModelData; import net.minecraftforge.common.ForgeConfig; -/* public class ForgeBlockModelRenderer extends BlockModelRenderer { private final ThreadLocal lighterFlat; @@ -54,16 +53,16 @@ public class ForgeBlockModelRenderer extends BlockModelRenderer @Override public boolean renderModelFlat(ILightReader world, IBakedModel model, BlockState state, BlockPos pos, MatrixStack matrixStack, IVertexBuilder buffer, boolean checkSides, Random rand, long seed, int p_228806_11_, IModelData modelData) { - if(ForgeConfig.CLIENT.forgeLightPipelineEnabled.get()) + if(ForgeConfig.CLIENT.experimentalForgeLightPipelineEnabled.get()) { VertexBufferConsumer consumer = consumerFlat.get(); consumer.setBuffer(buffer); - consumer.setOffset(pos); VertexLighterFlat lighter = lighterFlat.get(); lighter.setParent(consumer); + lighter.setTransform(matrixStack.getLast()); - return render(lighter, world, model, state, pos, checkSides, rand, seed, modelData); + return render(lighter, world, model, state, pos, matrixStack, checkSides, rand, seed, modelData); } else { @@ -74,16 +73,16 @@ public class ForgeBlockModelRenderer extends BlockModelRenderer @Override public boolean renderModelSmooth(ILightReader world, IBakedModel model, BlockState state, BlockPos pos, MatrixStack matrixStack, IVertexBuilder buffer, boolean checkSides, Random rand, long seed, int p_228805_11_, IModelData modelData) { - if(ForgeConfig.CLIENT.forgeLightPipelineEnabled.get()) + if(ForgeConfig.CLIENT.experimentalForgeLightPipelineEnabled.get()) { VertexBufferConsumer consumer = consumerSmooth.get(); consumer.setBuffer(buffer); - consumer.setOffset(pos); VertexLighterSmoothAo lighter = lighterSmooth.get(); lighter.setParent(consumer); + lighter.setTransform(matrixStack.getLast()); - return render(lighter, world, model, state, pos, checkSides, rand, seed, modelData); + return render(lighter, world, model, state, pos, matrixStack, checkSides, rand, seed, modelData); } else { @@ -91,7 +90,7 @@ public class ForgeBlockModelRenderer extends BlockModelRenderer } } - public static boolean render(VertexLighterFlat lighter, ILightReader world, IBakedModel model, BlockState state, BlockPos pos, boolean checkSides, Random rand, long seed, IModelData modelData) + public static boolean render(VertexLighterFlat lighter, ILightReader world, IBakedModel model, BlockState state, BlockPos pos, MatrixStack matrixStack, boolean checkSides, Random rand, long seed, IModelData modelData) { lighter.setWorld(world); lighter.setState(state); @@ -129,4 +128,3 @@ public class ForgeBlockModelRenderer extends BlockModelRenderer return !empty; } } -*/ \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/VertexBufferConsumer.java b/src/main/java/net/minecraftforge/client/model/pipeline/VertexBufferConsumer.java index 461e8ccb3..010ea37f6 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/VertexBufferConsumer.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/VertexBufferConsumer.java @@ -20,25 +20,20 @@ package net.minecraftforge.client.model.pipeline; import com.mojang.blaze3d.vertex.IVertexBuilder; -import net.minecraft.client.renderer.BufferBuilder; + import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.client.renderer.vertex.VertexFormat; -import net.minecraft.client.renderer.vertex.VertexFormatElement.Usage; -import net.minecraft.util.math.BlockPos; +import net.minecraft.client.renderer.vertex.VertexFormatElement; import net.minecraft.util.Direction; +import net.minecraft.util.math.MathHelper; /** * Assumes VertexFormatElement is present in the BufferBuilder's vertex format. */ public class VertexBufferConsumer implements IVertexConsumer { - private static final float[] dummyColor = new float[]{ 1, 1, 1, 1 }; - private IVertexBuilder renderer; - private int[] quadData; - private int v = 0; - private BlockPos offset = BlockPos.ZERO; public VertexBufferConsumer() {} @@ -52,49 +47,61 @@ public class VertexBufferConsumer implements IVertexConsumer { return DefaultVertexFormats.BLOCK; // renderer.getVertexFormat(); } + + private int expandTextureCoord(final float c, final int max) + { + return (int) (MathHelper.clamp(c, 0, 1) * max); + } @Override public void put(int e, float... data) { - // TODO - /* VertexFormat format = getVertexFormat(); - if(renderer.isColorDisabled() && format.getElements().get(e).getUsage() == Usage.COLOR) + VertexFormatElement element = format.getElements().get(e); + final float d0 = data.length <= 0 ? 0 : data[0]; + final float d1 = data.length <= 1 ? 0 : data[1]; + final float d2 = data.length <= 2 ? 0 : data[2]; + final float d3 = data.length <= 3 ? 0 : data[3]; + + switch (element.getUsage()) { - data = dummyColor; + case POSITION: + renderer.pos(d0, d1, d2); + break; + case NORMAL: + renderer.normal(d0, d1, d2); + break; + case COLOR: + renderer.color(d0, d1, d2, d3); + break; + case UV: + switch (element.getIndex()) + { + case 0: + renderer.tex(d0, d1); + break; + case 1: + renderer.overlay(expandTextureCoord(d0, 0xF), expandTextureCoord(d1, 0xF)); + break; + case 2: + renderer.lightmap(expandTextureCoord(d0, 0xF0), expandTextureCoord(d1, 0xF0)); + break; + } + break; + default: + case PADDING: + case GENERIC: + break; } - LightUtil.pack(data, quadData, format, v, e); if(e == format.getElements().size() - 1) { - v++; - if(v == 4) - { - renderer.addVertexData(quadData); - renderer.putPosition(offset.getX(), offset.getY(), offset.getZ()); - //Arrays.fill(quadData, 0); - v = 0; - } - } - */ - } - - private void checkVertexFormat() - { - if (quadData == null || getVertexFormat().getSize() != quadData.length) - { - quadData = new int[getVertexFormat().getSize()]; + renderer.endVertex(); } } public void setBuffer(IVertexBuilder buffer) { this.renderer = buffer; - checkVertexFormat(); - } - - public void setOffset(BlockPos offset) - { - this.offset = new BlockPos(offset); } @Override diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java index b386f1cce..7ebb909ce 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java @@ -21,8 +21,11 @@ package net.minecraftforge.client.model.pipeline; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import com.mojang.blaze3d.matrix.MatrixStack; + import net.minecraft.block.BlockState; import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.Vector3f; import net.minecraft.client.renderer.color.BlockColors; import net.minecraft.client.renderer.texture.TextureAtlasSprite; @@ -51,7 +54,8 @@ public class VertexLighterFlat extends QuadGatheringTransformer protected int lightmapIndex = -1; protected VertexFormat baseFormat; - + protected MatrixStack.Entry pose; + public VertexLighterFlat(BlockColors colors) { this.blockInfo = new BlockInfo(colors); @@ -63,6 +67,11 @@ public class VertexLighterFlat extends QuadGatheringTransformer super.setParent(parent); setVertexFormat(parent.getVertexFormat()); } + + public void setTransform(final MatrixStack.Entry pose) + { + this.pose = pose; + } private void updateIndices() { @@ -137,9 +146,9 @@ public class VertexLighterFlat extends QuadGatheringTransformer float[][] color = quadData[colorIndex]; if (dataLength[normalIndex] >= 3 - && (quadData[normalIndex][0][0] != -1 - || quadData[normalIndex][0][1] != -1 - || quadData[normalIndex][0][2] != -1)) + && (quadData[normalIndex][0][0] != 0 + || quadData[normalIndex][0][1] != 0 + || quadData[normalIndex][0][2] != 0)) { normal = quadData[normalIndex]; } @@ -213,22 +222,29 @@ public class VertexLighterFlat extends QuadGatheringTransformer switch(element.getUsage()) { case POSITION: - // position adding moved to VertexBufferConsumer due to x and z not fitting completely into a float - /*float[] pos = new float[4]; - System.arraycopy(position[v], 0, pos, 0, position[v].length); - pos[0] += blockInfo.getBlockPos().getX(); - pos[1] += blockInfo.getBlockPos().getY(); - pos[2] += blockInfo.getBlockPos().getZ();*/ + final net.minecraft.client.renderer.Vector4f pos = new net.minecraft.client.renderer.Vector4f( + position[v][0], position[v][1], position[v][2], 1); + pos.transform(pose.getMatrix()); + + position[v][0] = pos.getX(); + position[v][1] = pos.getY(); + position[v][2] = pos.getZ(); parent.put(e, position[v]); break; case NORMAL: + final net.minecraft.client.renderer.Vector3f norm = new net.minecraft.client.renderer.Vector3f(normal[v]); + norm.transform(pose.getNormal()); + + normal[v][0] = norm.getX(); + normal[v][1] = norm.getY(); + normal[v][2] = norm.getZ(); parent.put(e, normal[v]); break; case COLOR: parent.put(e, color[v]); break; case UV: - if(element.getIndex() == 1) + if(element.getIndex() == 2) { parent.put(e, lightmap[v]); break; @@ -260,8 +276,8 @@ public class VertexLighterFlat extends QuadGatheringTransformer int i = side == null ? 0 : side.ordinal() + 1; int brightness = blockInfo.getPackedLight()[i]; - lightmap[0] = ((float)((brightness >> 0x04) & 0xF) * 0x20) / 0xFFFF; - lightmap[1] = ((float)((brightness >> 0x14) & 0xF) * 0x20) / 0xFFFF; + lightmap[0] = LightTexture.getLightBlock(brightness) / (float) 0xF; + lightmap[1] = LightTexture.getLightSky(brightness) / (float) 0xF; } protected void updateColor(float[] normal, float[] color, float x, float y, float z, float tint, int multiplier) diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java index c33a4ae22..77af9b246 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java @@ -139,10 +139,7 @@ public class VertexLighterSmoothAo extends VertexLighterFlat } l /= s; - - if(l > 15f * 0x20 / 0xFFFF) l = 15f * 0x20 / 0xFFFF; - if(l < 0) l = 0; - + l = MathHelper.clamp(l, 0, 1); return l; } diff --git a/src/main/java/net/minecraftforge/common/ForgeConfig.java b/src/main/java/net/minecraftforge/common/ForgeConfig.java index 53ee41d20..fbb6f1f7a 100644 --- a/src/main/java/net/minecraftforge/common/ForgeConfig.java +++ b/src/main/java/net/minecraftforge/common/ForgeConfig.java @@ -127,6 +127,7 @@ public class ForgeConfig public final BooleanValue alwaysSetupTerrainOffThread; public final BooleanValue forgeLightPipelineEnabled; + public final BooleanValue experimentalForgeLightPipelineEnabled; public final BooleanValue selectiveResourceReloadEnabled; @@ -162,6 +163,10 @@ public class ForgeConfig .comment("Enable the Forge block rendering pipeline - fixes the lighting of custom models.") .translation("forge.configgui.forgeLightPipelineEnabled") .define("forgeLightPipelineEnabled", true); + experimentalForgeLightPipelineEnabled = builder + .comment("EXPERIMENTAL: Enable the Forge block rendering pipeline - fixes the lighting of custom models.") + .translation("forge.configgui.forgeLightPipelineEnabled") + .define("experimentalForgeLightPipelineEnabled", false); selectiveResourceReloadEnabled = builder .comment("When enabled, makes specific reload tasks such as language changing quicker to run.")