Improve upon Texture Filtering Work (#2096)

This commit is contained in:
Eclipse-Sol authored and GitHub committed 2026-09-15 16:00:43 -07:00
1 parent 75639d3b73
commit 26c161f0e8
26 files changed
+595 -261

No files matched your search

@@ -384,6 +384,7 @@ public enum Mixins implements IMixins {
, "celeritas.features.mipmaps.MixinTextureMetadataSectionSerializer"
, "celeritas.features.textures.MixinTextureMap"
, "celeritas.features.textures.MixinTextureAtlasSprite"
, "celeritas.features.textures.MixinStitcherHolder"
, "celeritas.biome_blending.MixinBlockGrass"
, "celeritas.biome_blending.MixinBlockLeaves"
, "celeritas.biome_blending.MixinBlockLiquid"
@@ -165,6 +165,7 @@ 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;
@@ -255,14 +256,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 (hasCachedMaterial && sprite == lastSprite) {
correctMaterial = lastMaterial;
if ((facesAtBaseMaterial & faceBit) != 0) {
correctMaterial = material;
} else {
correctMaterial = selectMaterial(material, sprite, isShaderPackOverride, useRenderPassOptimization);
lastSprite = sprite;
lastMaterial = correctMaterial;
hasCachedMaterial = true;
if (hasCachedMaterial && sprite == lastSprite) {
correctMaterial = lastMaterial;
} else {
correctMaterial = selectMaterial(material, sprite, isShaderPackOverride, useRenderPassOptimization);
lastSprite = sprite;
lastMaterial = correctMaterial;
hasCachedMaterial = true;
}
if (correctMaterial == material) {
facesAtBaseMaterial |= faceBit;
}
}
final var builder = buffers.get(correctMaterial);
@@ -10,6 +10,7 @@ import com.gtnewhorizons.angelica.proxy.ClientProxy;
import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProvider;
import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHolder;
import com.gtnewhorizons.angelica.rendering.culling.GpuCulling;
import me.jellysquid.mods.sodium.client.gui.SodiumGameOptions;
import me.jellysquid.mods.sodium.client.gui.options.named.MultiDrawMode;
import com.gtnewhorizons.angelica.rendering.voxelization.SdlShadowVoxelizationSink;
import com.gtnewhorizons.angelica.rendering.voxelization.ShadowVoxelizer;
@@ -48,7 +49,9 @@ import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.render.shader.ShaderLoader;
import org.embeddedt.embeddium.impl.render.viewport.CameraTransform;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.EXTTextureFilterAnisotropic;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL20;
import java.util.ArrayList;
@@ -61,9 +64,14 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer {
private GlProgram<? extends ChunkShaderInterface> irisProgram;
private boolean usingIrisProgram;
private int rgssSampler;
private boolean rgssSamplerResolved;
private boolean rgssSamplerBound;
private int terrainSampler;
private boolean terrainSamplerResolved;
private int terrainSamplerAnisotropy = -1;
private boolean terrainSamplerNearest;
private int packTerrainSampler;
private boolean packTerrainSamplerResolved;
private int packTerrainSamplerAnisotropy = -1;
private boolean terrainSamplerBound;
private final GpuTerrainCuller culler;
private final ReusableCachedBatch gpuBatch = new ReusableCachedBatch();
@@ -161,6 +169,7 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer {
this.activeProgram = (GlProgram<ChunkShaderInterface>) override;
this.irisProgram = override;
this.usingIrisProgram = true;
bindPackTerrainSampler();
return;
}
}
@@ -169,7 +178,7 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer {
this.usingIrisProgram = false;
this.irisProgram = null;
super.begin(pass);
bindRgssSampler();
bindTerrainSampler();
} finally {
if (Tracy.ENABLED) Tracy.endZone();
}
@@ -177,7 +186,7 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer {
@Override
protected void end(TerrainRenderPass pass) {
unbindRgssSampler();
unbindTerrainSampler();
if (usingIrisProgram && irisProgram != null) {
irisProgram.getInterface().restoreState();
@@ -192,32 +201,73 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer {
super.end(pass);
}
private void bindRgssSampler() {
if (!AngelicaRenderPassConfiguration.isRgssEnabled()) {
return;
}
private void bindTerrainSampler() {
final int anisotropy = SodiumGameOptions.resolvedAnisotropicFiltering();
final boolean nearest = ClientProxy.options().quality.texelSampling.isNearest();
if (!rgssSamplerResolved) {
rgssSamplerResolved = true;
rgssSampler = RenderSystem.genSampler();
if (rgssSampler != 0) {
RenderSystem.samplerParameteri(rgssSampler, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
RenderSystem.samplerParameteri(rgssSampler, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
} else {
AngelicaMod.LOGGER.warn("Sampler objects unavailable; RGSS terrain filtering will fall back to nearest sampling");
if (!terrainSamplerResolved || terrainSamplerAnisotropy != anisotropy
|| terrainSamplerNearest != nearest) {
if (!terrainSamplerResolved) {
terrainSampler = RenderSystem.genSampler();
if (terrainSampler == 0) {
AngelicaMod.LOGGER.warn("Sampler objects unavailable; terrain filtering will fall back to the atlas texture's own parameters");
}
}
terrainSamplerResolved = true;
terrainSamplerAnisotropy = anisotropy;
terrainSamplerNearest = nearest;
configureTerrainSampler(terrainSampler, nearest, anisotropy);
}
if (rgssSampler != 0) {
RenderSystem.bindSamplerToUnit(BLOCK_TEXTURE_UNIT, rgssSampler);
rgssSamplerBound = true;
if (terrainSampler != 0) {
RenderSystem.bindSamplerToUnit(BLOCK_TEXTURE_UNIT, terrainSampler);
terrainSamplerBound = true;
}
}
private void unbindRgssSampler() {
if (rgssSamplerBound) {
private void bindPackTerrainSampler() {
final int anisotropy = SodiumGameOptions.resolvedAnisotropicFiltering();
if (!packTerrainSamplerResolved || packTerrainSamplerAnisotropy != anisotropy) {
if (!packTerrainSamplerResolved) {
packTerrainSampler = RenderSystem.genSampler();
if (packTerrainSampler == 0) {
AngelicaMod.LOGGER.warn("Sampler objects unavailable; shader pack terrain filtering will fall back to the atlas texture's own parameters");
}
}
packTerrainSamplerResolved = true;
packTerrainSamplerAnisotropy = anisotropy;
configureTerrainSampler(packTerrainSampler, true, anisotropy);
}
if (packTerrainSampler != 0) {
RenderSystem.bindSamplerToUnit(BLOCK_TEXTURE_UNIT, packTerrainSampler);
terrainSamplerBound = true;
}
}
private static void configureTerrainSampler(int sampler, boolean nearest, int anisotropy) {
if (sampler == 0) {
return;
}
RenderSystem.samplerParameteri(sampler, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
RenderSystem.samplerParameteri(sampler, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
RenderSystem.samplerParameteri(sampler, GL11.GL_TEXTURE_MIN_FILTER,
nearest ? GL11.GL_NEAREST_MIPMAP_LINEAR : GL11.GL_LINEAR_MIPMAP_LINEAR);
RenderSystem.samplerParameteri(sampler, GL11.GL_TEXTURE_MAG_FILTER,
nearest ? GL11.GL_NEAREST : GL11.GL_LINEAR);
if (SodiumGameOptions.anisotropySupported()) {
RenderSystem.samplerParameteri(sampler,
EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
}
}
private void unbindTerrainSampler() {
if (terrainSamplerBound) {
RenderSystem.bindSamplerToUnit(BLOCK_TEXTURE_UNIT, 0);
rgssSamplerBound = false;
terrainSamplerBound = false;
}
}
@@ -229,10 +279,16 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer {
culler.delete();
}
unbindRgssSampler();
RenderSystem.destroySampler(rgssSampler);
rgssSampler = 0;
rgssSamplerResolved = false;
unbindTerrainSampler();
RenderSystem.destroySampler(terrainSampler);
terrainSampler = 0;
terrainSamplerResolved = false;
terrainSamplerAnisotropy = -1;
terrainSamplerNearest = false;
RenderSystem.destroySampler(packTerrainSampler);
packTerrainSampler = 0;
packTerrainSamplerResolved = false;
packTerrainSamplerAnisotropy = -1;
}
@Override
@@ -5,7 +5,6 @@ import com.gtnewhorizons.angelica.glsm.GLStateManager;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import lombok.Getter;
import me.jellysquid.mods.sodium.client.gui.SodiumGameOptions;
import me.jellysquid.mods.sodium.client.gui.options.named.TextureFilterMode;
import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration;
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.QuadPrimitiveType;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
@@ -38,7 +37,7 @@ public class AngelicaRenderPassConfiguration {
}
public static RenderPassConfiguration<BlockRenderLayer> build(ChunkVertexType vertexType) {
rgssEnabled = SodiumGameOptions.effectiveTextureFilterMode() == TextureFilterMode.RGSS;
rgssEnabled = SodiumGameOptions.effectiveTextureFilterMode().usesRgss();
SOLID_PASS = builderForRenderType(0, true, vertexType)
.name("solid")
@@ -12,4 +12,9 @@ public interface SpriteExtension {
/** One-shot: returns true if marked, then resets. */
boolean celeritas$shouldUpdate();
void celeritas$setMipmapStrategy(MipmapStrategy strategy, boolean explicit, int textureType);
/** Width of the replicated-edge border reserved around this sprite in its stitcher slot. */
int angelica$getGutterWidth();
void angelica$setGutterWidth(int gutter);
}
@@ -26,11 +26,7 @@ public class IrisExtendedChunkVertexEncoder implements ContextAwareChunkVertexEn
private static final int NORMAL_OFFSET = IrisExtendedChunkVertexType.VERTEX_FORMAT.getAttribute("iris_Normal").getPointer();
private static final int MC_ENTITY_OFFSET = IrisExtendedChunkVertexType.VERTEX_FORMAT.getAttribute("mc_Entity").getPointer();
private static final int MID_BLOCK_OFFSET = IrisExtendedChunkVertexType.VERTEX_FORMAT.getAttribute("at_midBlock").getPointer();
private static final int A_TEXCOORD_OFFSET = IrisExtendedChunkVertexType.VERTEX_FORMAT.getAttribute("a_TexCoord").getPointer();
// One unit of UV-space shift toward the quad centroid. Stops bilinear /
// mipmap sampling from bleeding pixels from neighboring atlas cells.
private static final float TEX_CENTROID_BIAS = 1.0f / 32768.0f;
// a_TexCoord used to be here, if something related to it breaks add this back.
private final ChunkVertexEncoder baseEncoder = IrisExtendedChunkVertexType.BASE_TYPE.createEncoder();
private final CeleritasQuadView quad = new CeleritasQuadView();
@@ -133,14 +129,6 @@ public class IrisExtendedChunkVertexEncoder implements ContextAwareChunkVertexEn
memPutInt(ptr + MID_TEX_OFFSET - STRIDE * 2, midUV);
memPutInt(ptr + MID_TEX_OFFSET - STRIDE * 3, midUV);
for (int vIdx = 0; vIdx < 4; vIdx++) {
final long uvBase = ptr - (long) (3 - vIdx) * STRIDE + A_TEXCOORD_OFFSET;
final float vU = memGetFloat(uvBase);
final float vV = memGetFloat(uvBase + 4L);
memPutFloat(uvBase, vU + (vU < midU ? TEX_CENTROID_BIAS : -TEX_CENTROID_BIAS));
memPutFloat(uvBase + 4L, vV + (vV < midV ? TEX_CENTROID_BIAS : -TEX_CENTROID_BIAS));
}
quad.setup(ptr, STRIDE);
NormalHelper.computeFaceNormal(normal, quad);
final int packedNormal = NormI8.pack(normal);
@@ -12,7 +12,7 @@ import static org.joml.Math.clamp;
public final class MipmapGenerator {
private static final float CUTOUT_ALPHA_REF = 0.5f;
private static final float STRICT_COVERAGE_TARGET = 0.35f;
private static final float STRICT_CUTOUT_ALPHA_REF = 0.3f;
private static final float ALPHA_BIAS = 0.025f;
private MipmapGenerator() {
@@ -137,14 +137,14 @@ public final class MipmapGenerator {
* Estimates the fraction of the image that survives, with bilinear filtering modeled by
* supersampling each texel quad on a 4x4 interior grid.
*/
static float alphaTestCoverage(int[] image, int width, float alphaRef, float alphaScale, int border) {
static float alphaTestCoverage(int[] image, int width, float alphaRef, float alphaScale) {
final int height = image.length / width;
final float alphaFactor = alphaScale / 255.0f;
float total = 0.0f;
int quads = 0;
for (int y = border; y < height - border - 1; y++) {
for (int x = border; x < width - border - 1; x++) {
for (int y = 0; y < height - 1; y++) {
for (int x = 0; x < width - 1; x++) {
final int i = x + y * width;
final float a00 = scaledAlpha(image[i], alphaFactor);
final float a10 = scaledAlpha(image[i + 1], alphaFactor);
@@ -190,7 +190,7 @@ public final class MipmapGenerator {
return clamp(0.0f, 1.0f, ColorARGB.unpackAlpha(color) * alphaFactor);
}
static void scaleAlphaToCoverage(int[] image, int width, float desiredCoverage, float alphaRef, int border) {
static void scaleAlphaToCoverage(int[] image, int width, float desiredCoverage, float alphaRef) {
float min = 0.0f;
float max = 4.0f;
float scale = 1.0f;
@@ -198,7 +198,7 @@ public final class MipmapGenerator {
float bestError = Float.POSITIVE_INFINITY;
for (int i = 0; i < 5; i++) {
final float coverage = alphaTestCoverage(image, width, alphaRef, scale, border);
final float coverage = alphaTestCoverage(image, width, alphaRef, scale);
final float error = Math.abs(coverage - desiredCoverage);
if (error < bestError) {
bestError = error;
@@ -225,7 +225,7 @@ public final class MipmapGenerator {
}
public static int[][] generateMipLevels(int mipLevel, int width, int[][] currentMips,
MipmapStrategy strategy, boolean hasTransparentPixel, int border) {
MipmapStrategy strategy, boolean hasTransparentPixel) {
final MipmapStrategy resolved = resolve(strategy, hasTransparentPixel);
final boolean isCutout = isCutoutStrategy(strategy, hasTransparentPixel);
@@ -247,16 +247,8 @@ public final class MipmapGenerator {
return result;
}
final boolean autoResolved = strategy == null || strategy == MipmapStrategy.AUTO;
final boolean strictCoverage = resolved == MipmapStrategy.STRICT_CUTOUT;
final float alphaRef = CUTOUT_ALPHA_REF;
float originalCoverage = 0.0f;
if (isCutout) {
originalCoverage = alphaTestCoverage(base, width, alphaRef, 1.0f, border);
if (strictCoverage || (autoResolved && originalCoverage < STRICT_COVERAGE_TARGET)) {
originalCoverage = Math.max(originalCoverage, STRICT_COVERAGE_TARGET);
}
}
final float cutoutRef = resolved == MipmapStrategy.STRICT_CUTOUT ? STRICT_CUTOUT_ALPHA_REF : CUTOUT_ALPHA_REF;
final float originalCoverage = isCutout ? alphaTestCoverage(base, width, cutoutRef, 1.0f) : 0.0f;
for (int level = 1; level <= mipLevel; level++) {
final int levelWidth = width >> level;
@@ -283,7 +275,7 @@ public final class MipmapGenerator {
}
if (isCutout) {
scaleAlphaToCoverage(result[level], levelWidth, originalCoverage, alphaRef, border >> level);
scaleAlphaToCoverage(result[level], levelWidth, originalCoverage, cutoutRef);
}
}
@@ -0,0 +1,104 @@
package com.gtnewhorizons.angelica.utils;
import net.minecraft.client.renderer.texture.TextureUtil;
public final class SpritePadding {
private static int[][] scratch;
private SpritePadding() {}
/**
* The gutter has to survive being halved once per mip level, and the stitcher slot has to stay a multiple
* of 2^mipmapLevels so every level's origin lands on a texel, so 1 &lt;&lt; mipmapLevels is the only workable
* width. At mip 0 the terrain atlas still needs one texel for the RGSS taps to land in.
*/
public static int gutterFor(int mipmapLevels, boolean terrainAtlas) {
if (mipmapLevels <= 0) {
return terrainAtlas ? 1 : 0;
}
return 1 << mipmapLevels;
}
public static void uploadPadded(int[][] frameData, int width, int height, int originX, int originY, int gutter,
boolean blur, boolean clamp) {
if (gutter <= 0) {
TextureUtil.uploadTextureMipmap(frameData, width, height, originX, originY, blur, clamp);
return;
}
scratch = padFrame(frameData, width, height, gutter, scratch);
TextureUtil.uploadTextureMipmap(scratch, width + 2 * gutter, height + 2 * gutter,
originX - gutter, originY - gutter, blur, clamp);
}
private static int[][] padFrame(int[][] frameData, int width, int height, int gutter, int[][] scratch) {
final int[][] padded = scratch != null && scratch.length == frameData.length ? scratch
: new int[frameData.length][];
for (int level = 0; level < frameData.length; level++) {
final int[] levelData = frameData[level];
if (levelData == null) {
padded[level] = null;
continue;
}
final int levelWidth = width >> level;
final int levelHeight = height >> level;
final int levelGutter = gutter >> level;
final int size = (levelWidth + 2 * levelGutter) * (levelHeight + 2 * levelGutter);
int[] target = padded[level];
if (target == null || target.length < size) {
target = new int[size];
padded[level] = target;
}
System.arraycopy(levelData, 0, target, 0, Math.min(levelData.length, levelWidth * levelHeight));
padClampToEdge(target, levelWidth, levelHeight, levelGutter);
}
return padded;
}
private static void padClampToEdge(int[] data, int width, int height, int border) {
if (border <= 0 || width <= 0 || height <= 0) {
return;
}
final int stride = width + 2 * border;
for (int y = height - 1; y >= 0; y--) {
System.arraycopy(data, y * width, data, border + (y + border) * stride, width);
}
clampBorderToEdge(data, stride, height + 2 * border, border);
}
private static void clampBorderToEdge(int[] image, int paddedWidth, int paddedHeight, int border) {
if (border <= 0) {
return;
}
final int contentWidth = paddedWidth - 2 * border;
final int contentHeight = paddedHeight - 2 * border;
if (contentWidth <= 0 || contentHeight <= 0) {
return;
}
for (int y = 0; y < contentHeight; y++) {
final int row = border + (y + border) * paddedWidth;
final int left = image[row];
final int right = image[row + contentWidth - 1];
for (int i = 1; i <= border; i++) {
image[row - i] = left;
image[row + contentWidth - 1 + i] = right;
}
}
final int top = border * paddedWidth;
final int bottom = (border + contentHeight - 1) * paddedWidth;
for (int i = 1; i <= border; i++) {
System.arraycopy(image, top, image, top - i * paddedWidth, paddedWidth);
System.arraycopy(image, bottom, image, bottom + i * paddedWidth, paddedWidth);
}
}
}