Detect quads rendered by vanilla AO pipeline and only recalculate lighting for those (#1391)

* Detect quads rendered by vanilla AO pipeline and only recalculate lighting for those

* Handle oldLighting correctly
This commit is contained in:
embeddedt authored and GitHub committed 2026-02-15 18:46:58 -08:00
1 parent 534ae6b05c
commit 8380f3853d
7 files changed
+144 -84

No files matched your search

@@ -162,6 +162,7 @@ public enum Mixins implements IMixins {
, "rendering.MixinPlayerManager"
, "rendering.MixinRenderBlocks"
, "rendering.MixinRenderingRegistry"
, "rendering.MixinTessellator"
, "rendering.MixinTileEntity"
, "rendering.MixinTileEntityMobSpawner"
, "rendering.MixinTileEntityRendererDispatcher"
@@ -0,0 +1,12 @@
package com.gtnewhorizons.angelica.rendering;
public interface StateAwareTessellator {
/**
* True if the vertex originated from a RenderBlocks method call with the enableAO flag set.
*/
int RENDERED_WITH_VANILLA_AO = 0x1;
void angelica$setAppliedAo(boolean flag);
int[] angelica$getVertexStates();
}
@@ -3,10 +3,10 @@ package com.gtnewhorizons.angelica.rendering.celeritas;
import com.gtnewhorizons.angelica.AngelicaMod;
import com.gtnewhorizons.angelica.dynamiclights.DynamicLights;
import com.gtnewhorizons.angelica.dynamiclights.IDynamicLightSource;
import com.gtnewhorizons.angelica.rendering.StateAwareTessellator;
import com.gtnewhorizons.angelica.rendering.celeritas.iris.BlockRenderContext;
import com.gtnewhorizons.angelica.rendering.celeritas.iris.IrisExtendedChunkVertexEncoder;
import com.gtnewhorizons.angelica.rendering.celeritas.light.LightDataCache;
import com.gtnewhorizons.angelica.rendering.celeritas.light.QuadLightingHelper;
import com.gtnewhorizons.angelica.rendering.celeritas.light.VanillaDiffuseProvider;
import com.gtnewhorizons.angelica.rendering.celeritas.world.WorldSlice;
import lombok.Getter;
@@ -110,7 +110,8 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
}
@SuppressWarnings("unchecked")
public void copyRawBuffer(int[] rawBuffer, int vertexCount, ChunkBuildBuffers buffers, Material material, boolean isShaderPackOverride,
public void copyRawBuffer(int[] rawBuffer, int vertexCount, int[] vertexStates,
ChunkBuildBuffers buffers, Material material, boolean isShaderPackOverride,
boolean blockAllowsSmoothLighting) {
if (vertexCount == 0) {
return;
@@ -127,8 +128,7 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
final boolean celeritasSmoothLighting = AngelicaMod.options().quality.useCeleritasSmoothLighting;
final boolean shaderActive = IrisApi.getInstance().isShaderPackInUse();
final boolean useAoCalculation = lightPipelineReady && (separateAo || celeritasSmoothLighting || shaderActive);
final boolean stripDiffuse = shaderActive && BlockRenderingSettings.INSTANCE.shouldDisableDirectionalShading();
final boolean shade = false; // Vanilla bakes it, or we remove it - so no need to shade
final boolean shouldApplyDiffuse = !BlockRenderingSettings.INSTANCE.shouldDisableDirectionalShading();
final ChunkColorWriter colorEncoder = separateAo ? ChunkColorWriter.SEPARATE_AO : ChunkColorWriter.EMBEDDIUM;
int ptr = 0;
@@ -140,11 +140,14 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
final int worldX = originX + blockX;
final int worldY = originY + blockY;
final int worldZ = originZ + blockZ;
final boolean isEmissive = useAoCalculation && QuadLightingHelper.isBlockEmissive(blockAccess, worldX, worldY, worldZ);
int stateIdx = 0;
for (int quadIdx = 0; quadIdx < numQuads; quadIdx++) {
float uSum = 0, vSum = 0;
int quadState = -1;
for (int vIdx = 0; vIdx < 4; vIdx++) {
final var vertex = vertices[vIdx];
vertex.x = Float.intBitsToFloat(rawBuffer[ptr++]);
@@ -161,6 +164,8 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
vertex.color = rawBuffer[ptr++];
vertex.vanillaNormal = rawBuffer[ptr++];
vertex.light = rawBuffer[ptr++];
quadState &= vertexStates[stateIdx++];
}
final int trueNormal = QuadUtil.calculateNormal(vertices);
@@ -171,34 +176,16 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
animatedSprites.add(sprite);
}
// Strip vanilla's baked diffuse when shaders want oldLighting=false - multiply by an inversion factor
// instead of attempting to mixin/asm all the places that bake it in
if (stripDiffuse) {
final float inverseDiffuse = VanillaDiffuseProvider.INSTANCE.getInverseDiffuse(facing);
for (int vIdx = 0; vIdx < 4; vIdx++) {
vertices[vIdx].color = VanillaDiffuseProvider.multiplyColor(vertices[vIdx].color, inverseDiffuse);
}
}
if (useAoCalculation) {
final boolean quadIsFullBright = QuadLightingHelper.isQuadFullBright(vertices);
if (isEmissive || quadIsFullBright) {
Arrays.fill(quadLightData.br, 1.0f);
if (isEmissive) {
Arrays.fill(quadLightData.lm, LightDataAccess.FULL_BRIGHT);
} else {
for (int i = 0; i < 4; i++) {
quadLightData.lm[i] = vertices[i].light;
}
}
} else {
quadView.setup(trueNormal, blockX, blockY, blockZ);
final ModelQuadFacing lightFace = quadView.getLightFace();
final LightPipeline pipeline = blockAllowsSmoothLighting ? smoothLightPipeline : flatLightPipeline;
final ModelQuadFacing cullFace = quadView.getCullFace();
pipeline.calculate(quadView, worldX, worldY, worldZ, quadLightData, cullFace, lightFace, shade, true);
}
// If block used vanilla AO, then we can safely compute our own lighting data and ignore what vanilla
// computed. We suppress vanilla emitting the brightness/color in MixinRenderBlocks.
// Otherwise, we need to use the block's lightmap values as they are as the ISBRH may have
// specified its own lightmaps for visual purposes (e.g. fullbright)
if (useAoCalculation && (quadState & StateAwareTessellator.RENDERED_WITH_VANILLA_AO) != 0) {
quadView.setup(trueNormal, blockX, blockY, blockZ);
final ModelQuadFacing lightFace = quadView.getLightFace();
final LightPipeline pipeline = blockAllowsSmoothLighting ? smoothLightPipeline : flatLightPipeline;
final ModelQuadFacing cullFace = quadView.getCullFace();
pipeline.calculate(quadView, worldX, worldY, worldZ, quadLightData, cullFace, lightFace, shouldApplyDiffuse, true);
for (int vIdx = 0; vIdx < 4; vIdx++) {
final var vertex = vertices[vIdx];
@@ -207,6 +194,14 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
vertex.light = quadLightData.lm[vIdx];
}
} else {
if (!shouldApplyDiffuse) {
// Calculate the inverse of the expected diffuse constant and apply it to the color to fake
// diffuse not having been applied
final float inverseDiffuse = VanillaDiffuseProvider.INSTANCE.getInverseDiffuse(facing);
for (int vIdx = 0; vIdx < 4; vIdx++) {
vertices[vIdx].color = VanillaDiffuseProvider.multiplyColor(vertices[vIdx].color, inverseDiffuse);
}
}
for (int vIdx = 0; vIdx < 4; vIdx++) {
vertices[vIdx].trueNormal = trueNormal;
}
@@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.rendering.celeritas;
import com.gtnewhorizon.gtnhlib.blockpos.BlockPos;
import com.gtnewhorizons.angelica.rendering.AngelicaRenderQueue;
import com.gtnewhorizons.angelica.rendering.StateAwareTessellator;
import com.gtnewhorizons.angelica.rendering.TileEntityRenderBoundsRegistry;
import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProvider;
import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHolder;
@@ -249,15 +250,11 @@ public abstract class AngelicaChunkBuilderMeshingTask extends ChunkBuilderTask<C
block.canRenderInPass(pass);
tessellator.startDrawingQuads();
renderBlocks.renderBlockByRenderType(block, x, y, z);
// In modern versions, block models can specify whether to enable AO or not, but here we must
// rely on render ID heuristics to figure out which models would have turned it off
int vanillaRenderId = block.getRenderType();
boolean blockAllowsSmoothLighting = Minecraft.isAmbientOcclusionEnabled() // smooth lighting on
&& block.getLightValue() == 0 // does not emit real block light
&& vanillaRenderId != 1 // is not a "cross" block
&& vanillaRenderId != 40; // is not a double plant block
buildContext.copyRawBuffer(tessellator.rawBuffer, tessellator.vertexCount, buffers, passMaterial,
isShaderPackOverride, blockAllowsSmoothLighting);
&& block.getLightValue() == 0; // does not emit real block light
buildContext.copyRawBuffer(tessellator.rawBuffer, tessellator.vertexCount,
((StateAwareTessellator)tessellator).angelica$getVertexStates(),
buffers, passMaterial, isShaderPackOverride, blockAllowsSmoothLighting);
tessellator.reset();
tessellator.isDrawing = false;
@@ -1,39 +0,0 @@
package com.gtnewhorizons.angelica.rendering.celeritas.light;
import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder;
public final class QuadLightingHelper {
private QuadLightingHelper() {}
// Known emissive brightness values set by mods/vanilla
public static final int FULL_BRIGHT_15 = (15 << 20) | (15 << 4); // 15728880
public static final int FULL_BRIGHT_14 = (14 << 20) | (14 << 4); // 14680288
public static boolean isBlockEmissive(IBlockAccess world, int x, int y, int z) {
final Block block = world.getBlock(x, y, z);
try {
return block.getMixedBrightnessForBlock(EmptyBlockAccess.INSTANCE, x, y, z) == FULL_BRIGHT_15;
} catch (Exception e) {
return false;
}
}
public static boolean isQuadFullBright(ChunkVertexEncoder.Vertex[] vertices) {
final int first = vertices[0].light;
// Must be a known emissive brightness value
if (first != FULL_BRIGHT_15 && first != FULL_BRIGHT_14) {
return false;
}
// All 4 vertices must have the same value
for (int i = 1; i < 4; i++) {
if (vertices[i].light != first) {
return false;
}
}
return true;
}
}