mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-25 22:54:28 +00:00
Improve shader support for custom block rendering (#2176)
This commit is contained in:
1 parent
66d2a548fc
commit
fe67a22d5a
12 files changed
+150
-63
No files matched your search
@@ -8,6 +8,7 @@ import com.gtnewhorizons.angelica.rendering.items.ItemRenderListManager;
|
||||
import com.gtnewhorizons.angelica.rendering.tesr.TesrLifecycle;
|
||||
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.uniforms.ItemIdManager;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.ITextureObject;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
@@ -96,6 +97,23 @@ public final class ImmediateExtendedAttribs implements ImmediateExtendedAttribHa
|
||||
return Iris.enabled && Iris.getCurrentPack().isPresent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wantsFaceNormals() {
|
||||
return ItemIdManager.isWorldRenderActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeFaceNormals(int[] rawBuffer, int vertexCount) {
|
||||
for (int v = 0; v < vertexCount; v += 4) {
|
||||
quad.setup(rawBuffer, v, RAW_NORMAL_INDEX);
|
||||
NormalHelper.computeFaceNormal(normal, quad);
|
||||
final int packed = NormI8.pack(normal.x, normal.y, normal.z, 0.0f);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
rawBuffer[(v + i) * VERTEX_SIZE + RAW_NORMAL_INDEX] = packed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(int[] rawBuffer, int vertexCount, int vertsPerPrim, int normalIntIndex, long dstAddr, int dstStride) {
|
||||
final boolean smooth = normalIntIndex >= 0;
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.gtnewhorizons.angelica.glsm.hooks.BatchStateGuard;
|
||||
import com.gtnewhorizons.angelica.glsm.recording.CommandRecorder;
|
||||
import com.gtnewhorizons.angelica.glsm.recording.commands.DisplayListCommand;
|
||||
import com.gtnewhorizons.angelica.shadercompat.ShaderGlint;
|
||||
import com.gtnewhorizons.angelica.rendering.FallingBlockRendering;
|
||||
import com.gtnewhorizons.angelica.rendering.BlockMaterialAttribute;
|
||||
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import net.coderbot.iris.gbuffer_overrides.matching.SpecialCondition;
|
||||
@@ -166,8 +166,8 @@ public final class IrisDisplayListState {
|
||||
private record BlockEntityAttributeCmd(Block block, int metadata) implements DisplayListCommand {
|
||||
@Override
|
||||
public void execute() {
|
||||
if (block == null) FallingBlockRendering.resetEntityAttribute();
|
||||
else FallingBlockRendering.setEntityAttribute(block, metadata);
|
||||
if (block == null) BlockMaterialAttribute.reset();
|
||||
else BlockMaterialAttribute.set(block, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.iris.IrisDisplayListState;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.BlockRenderLayer;
|
||||
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
||||
import net.coderbot.iris.block_rendering.BlockMaterialMapping;
|
||||
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
|
||||
import net.coderbot.iris.gl.shader.ProgramCreator;
|
||||
import net.coderbot.iris.layer.GbufferPrograms;
|
||||
import net.coderbot.iris.pipeline.WorldRenderingPhase;
|
||||
import net.irisshaders.iris.api.v0.IrisApi;
|
||||
import net.minecraft.block.Block;
|
||||
|
||||
/** Gives blocks drawn outside chunk meshing their block.properties id on mc_Entity. */
|
||||
public final class BlockMaterialAttribute {
|
||||
|
||||
private BlockMaterialAttribute() {
|
||||
}
|
||||
|
||||
public static void set(Block block, int metadata) {
|
||||
IrisDisplayListState.recordBlockEntityAttribute(block, metadata);
|
||||
if (!shadersActive()) return;
|
||||
IrisDisplayListState.runUnrecorded(() ->
|
||||
GLStateManager.glVertexAttrib2s(ProgramCreator.MC_ENTITY, (short) blockMaterialId(block, metadata), (short) 0));
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
IrisDisplayListState.recordBlockEntityAttribute(null, 0);
|
||||
if (!shadersActive()) return;
|
||||
IrisDisplayListState.runUnrecorded(() ->
|
||||
GLStateManager.glVertexAttrib2s(ProgramCreator.MC_ENTITY, (short) -1, (short) -1));
|
||||
}
|
||||
|
||||
static boolean shadersActive() {
|
||||
return TessellatorManager.isOnMainThread() && IrisApi.getInstance().isShaderPackInUse();
|
||||
}
|
||||
|
||||
public static boolean skipDirectionalShading() {
|
||||
if (!shadersActive() || !BlockRenderingSettings.INSTANCE.shouldDisableDirectionalShading()
|
||||
|| ((StateAwareTessellator) TessellatorManager.get()).angelica$isCeleritasMeshing()) return false;
|
||||
return switch (GbufferPrograms.getCurrentPhase()) {
|
||||
case TERRAIN_SOLID, TERRAIN_CUTOUT, TERRAIN_CUTOUT_MIPPED, TERRAIN_TRANSLUCENT -> true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
public static int blockMaterialId(Block block, int metadata) {
|
||||
final Reference2ObjectMap<Block, Int2IntMap> blockMetaMatches = BlockRenderingSettings.INSTANCE.getBlockMetaMatches();
|
||||
if (blockMetaMatches == null) return -1;
|
||||
|
||||
final Int2IntMap metaMap = blockMetaMatches.get(block);
|
||||
return metaMap != null ? BlockMaterialMapping.resolveId(metaMap, metadata) : -1;
|
||||
}
|
||||
|
||||
public static WorldRenderingPhase renderingPhase(Block block, boolean fading) {
|
||||
if (fading) return WorldRenderingPhase.TERRAIN_TRANSLUCENT;
|
||||
if (block == null) return WorldRenderingPhase.TERRAIN_CUTOUT;
|
||||
final var overrides = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
|
||||
final BlockRenderLayer override = overrides == null ? null : overrides.get(block);
|
||||
if (override != null) {
|
||||
return switch (override) {
|
||||
case SOLID -> WorldRenderingPhase.TERRAIN_SOLID;
|
||||
case CUTOUT -> WorldRenderingPhase.TERRAIN_CUTOUT;
|
||||
case CUTOUT_MIPPED -> WorldRenderingPhase.TERRAIN_CUTOUT_MIPPED;
|
||||
case TRANSLUCENT -> WorldRenderingPhase.TERRAIN_TRANSLUCENT;
|
||||
};
|
||||
}
|
||||
if (block.getRenderBlockPass() == 1) return WorldRenderingPhase.TERRAIN_TRANSLUCENT;
|
||||
return block.isOpaqueCube() ? WorldRenderingPhase.TERRAIN_SOLID : WorldRenderingPhase.TERRAIN_CUTOUT;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,6 @@
|
||||
package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.iris.IrisDisplayListState;
|
||||
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
||||
import net.coderbot.iris.block_rendering.BlockMaterialMapping;
|
||||
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
|
||||
import net.coderbot.iris.gl.shader.ProgramCreator;
|
||||
import net.irisshaders.iris.api.v0.IrisApi;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
public final class FallingBlockRendering {
|
||||
@@ -25,34 +16,8 @@ public final class FallingBlockRendering {
|
||||
return META_ACCESS.set(world, x, y, z, metadata);
|
||||
}
|
||||
|
||||
public static void setEntityAttribute(Block block, int metadata) {
|
||||
IrisDisplayListState.recordBlockEntityAttribute(block, metadata);
|
||||
if (!shadersActive()) return;
|
||||
IrisDisplayListState.runUnrecorded(() ->
|
||||
GLStateManager.glVertexAttrib2s(ProgramCreator.MC_ENTITY, (short) blockMaterialId(block, metadata), (short) 0));
|
||||
}
|
||||
|
||||
public static void resetEntityAttribute() {
|
||||
IrisDisplayListState.recordBlockEntityAttribute(null, 0);
|
||||
if (!shadersActive()) return;
|
||||
IrisDisplayListState.runUnrecorded(() ->
|
||||
GLStateManager.glVertexAttrib2s(ProgramCreator.MC_ENTITY, (short) -1, (short) -1));
|
||||
}
|
||||
|
||||
private static int blockMaterialId(Block block, int metadata) {
|
||||
final Reference2ObjectMap<Block, Int2IntMap> blockMetaMatches = BlockRenderingSettings.INSTANCE.getBlockMetaMatches();
|
||||
if (blockMetaMatches == null) return -1;
|
||||
|
||||
final Int2IntMap metaMap = blockMetaMatches.get(block);
|
||||
return metaMap != null ? BlockMaterialMapping.resolveId(metaMap, metadata) : -1;
|
||||
}
|
||||
|
||||
public static boolean isActive() {
|
||||
return active && shadersActive();
|
||||
}
|
||||
|
||||
private static boolean shadersActive() {
|
||||
return TessellatorManager.isOnMainThread() && IrisApi.getInstance().isShaderPackInUse();
|
||||
return active && BlockMaterialAttribute.shadersActive();
|
||||
}
|
||||
|
||||
public static boolean skipDirectionalShading() {
|
||||
|
||||
@@ -9,6 +9,9 @@ public interface StateAwareTessellator {
|
||||
int RENDERED_WITH_VANILLA_AO = 0x1;
|
||||
int NO_DIRECTIONAL_SHADING = 0x2;
|
||||
|
||||
/// Explicitly render an unmapped material instead of inheriting the surrounding block's shader ID.
|
||||
short UNMAPPED_SHADER_BLOCK_ID = Short.MIN_VALUE;
|
||||
|
||||
void angelica$setAppliedAo(boolean flag);
|
||||
void angelica$setNoDirectionalShading(boolean flag);
|
||||
|
||||
@@ -17,9 +20,17 @@ public interface StateAwareTessellator {
|
||||
/// Enables per-vertex AO state collection into [#angelica$getVertexStates()].
|
||||
void angelica$setCeleritasMeshing(boolean active);
|
||||
|
||||
default boolean angelica$isCeleritasMeshing() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int[] angelica$getVertexStates();
|
||||
|
||||
int[] angelica$getShaderOverrideBlockIds();
|
||||
|
||||
void angelica$setShaderOverrideBlockId(short blockId);
|
||||
|
||||
default short angelica$getShaderOverrideBlockId() {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
+11
-15
@@ -172,7 +172,6 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
|
||||
final short originalBlockId = blockRenderContext.blockId;
|
||||
|
||||
int stateIdx = 0;
|
||||
int facesAtBaseMaterial = 0;
|
||||
boolean hasCachedMaterial = false;
|
||||
TextureAtlasSprite lastSprite = null;
|
||||
Material lastMaterial = null;
|
||||
@@ -182,6 +181,7 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
|
||||
float uSum = 0, vSum = 0;
|
||||
|
||||
int quadState = -1;
|
||||
boolean hasVertexTransparency = false;
|
||||
|
||||
for (int vIdx = 0; vIdx < 4; vIdx++) {
|
||||
final var vertex = vertices[vIdx];
|
||||
@@ -197,6 +197,7 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
|
||||
vSum += v;
|
||||
|
||||
vertex.color = rawBuffer[ptr++];
|
||||
hasVertexTransparency |= ColorABGR.unpackAlpha(vertex.color) != 255;
|
||||
vertex.vanillaNormal = rawBuffer[ptr++];
|
||||
vertex.light = rawBuffer[ptr++];
|
||||
|
||||
@@ -263,27 +264,22 @@ public class AngelicaChunkBuildContext extends ChunkBuildContext {
|
||||
// Apply RGB block light tint from provider
|
||||
applyBlockLightTint(worldX, worldY, worldZ, vertices);
|
||||
|
||||
final int faceBit = 1 << facing.ordinal();
|
||||
final Material correctMaterial;
|
||||
if ((facesAtBaseMaterial & faceBit) != 0) {
|
||||
if (material == AngelicaRenderPassConfiguration.TRANSLUCENT_MATERIAL && hasVertexTransparency) {
|
||||
correctMaterial = material;
|
||||
} else if (hasCachedMaterial && sprite == lastSprite) {
|
||||
correctMaterial = lastMaterial;
|
||||
} else {
|
||||
if (hasCachedMaterial && sprite == lastSprite) {
|
||||
correctMaterial = lastMaterial;
|
||||
} else {
|
||||
correctMaterial = selectMaterial(material, sprite, isShaderPackOverride, useRenderPassOptimization);
|
||||
lastSprite = sprite;
|
||||
lastMaterial = correctMaterial;
|
||||
hasCachedMaterial = true;
|
||||
}
|
||||
if (correctMaterial == material) {
|
||||
facesAtBaseMaterial |= faceBit;
|
||||
}
|
||||
correctMaterial = selectMaterial(material, sprite, isShaderPackOverride, useRenderPassOptimization);
|
||||
lastSprite = sprite;
|
||||
lastMaterial = correctMaterial;
|
||||
hasCachedMaterial = true;
|
||||
}
|
||||
final var builder = buffers.get(correctMaterial);
|
||||
|
||||
final int shaderOverrideBlockId = shaderOverrideBlockIds[quadIdx * 4];
|
||||
blockRenderContext.blockId = shaderOverrideBlockId != -1 ? (short) shaderOverrideBlockId : originalBlockId;
|
||||
blockRenderContext.blockId = shaderOverrideBlockId == StateAwareTessellator.UNMAPPED_SHADER_BLOCK_ID
|
||||
? -1 : shaderOverrideBlockId != -1 ? (short) shaderOverrideBlockId : originalBlockId;
|
||||
|
||||
if (correctMaterial != material && builder.getEncoder() instanceof IrisExtendedChunkVertexEncoder iris) {
|
||||
iris.setContext(blockRenderContext);
|
||||
|
||||
Reference in new issue
Block a user