From 79cf2968f4c4eb219dcf35de5b4537e08cae7ff0 Mon Sep 17 00:00:00 2001 From: rdh Date: Wed, 29 Jul 2026 23:48:04 -0400 Subject: [PATCH] correct bilinear interpolation for ambient occlusion Co-authored-by: embeddedt <1+embeddedt@noreply.localhost> --- .../builder/ChunkMeshBufferBuilder.java | 30 +++++++++++++++++++ .../vertex/format/ChunkVertexEncoder.java | 1 + .../format/impl/CompactChunkVertex.java | 6 ++-- .../format/impl/VanillaLikeChunkVertex.java | 4 ++- .../shaders/blocks/block_layer_opaque.fsh | 8 +++++ .../shaders/blocks/block_layer_opaque.vsh | 12 +++++++- .../sodium/shaders/include/chunk_vertex.glsl | 7 +++++ .../terrain_xhfp/XHFPModelVertexType.java | 2 +- 8 files changed, 65 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/builder/ChunkMeshBufferBuilder.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/builder/ChunkMeshBufferBuilder.java index cb9705fe5..2bdb2c223 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/builder/ChunkMeshBufferBuilder.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/builder/ChunkMeshBufferBuilder.java @@ -32,6 +32,8 @@ public class ChunkMeshBufferBuilder { } public void push(ChunkVertexEncoder.Vertex[] vertices, Material material) { + prepareBilinearCorrection(vertices); + var vertexStart = this.count * this.stride; var vertexSize = vertices.length * this.stride; @@ -54,6 +56,34 @@ public class ChunkMeshBufferBuilder { this.count += vertices.length; } + private static void prepareBilinearCorrection(ChunkVertexEncoder.Vertex[] vertices) { + if (vertices.length != 4) { + for (var vertex : vertices) { + vertex.rdhFactor = 0; + } + return; + } + + int color0 = vertices[0].color; + int color1 = vertices[1].color; + int color2 = vertices[2].color; + int color3 = vertices[3].color; + int factor = encodeBilinearCorrection(color3, color1, color0, color2, 0) + | encodeBilinearCorrection(color3, color1, color0, color2, 8) << 8 + | encodeBilinearCorrection(color3, color1, color0, color2, 16) << 16 + | encodeBilinearCorrection(color3, color1, color0, color2, 24) << 24; + + for (var vertex : vertices) { + vertex.rdhFactor = factor; + } + } + + private static int encodeBilinearCorrection(int positive0, int positive1, int negative0, int negative1, int shift) { + int factor = ((positive0 >> shift) & 0xFF) + ((positive1 >> shift) & 0xFF) + - ((negative0 >> shift) & 0xFF) - ((negative1 >> shift) & 0xFF); + return Math.round(factor * (127.0F / 510.0F)) & 0xFF; + } + private void grow(int bytesNeeded) { // Grow by a factor of 2, or by however many bytes more we need, whichever is larger. int newCapacity = Math.max(this.capacity * 2, this.capacity + bytesNeeded); diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/ChunkVertexEncoder.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/ChunkVertexEncoder.java index 79c6f4e86..a9e32974e 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/ChunkVertexEncoder.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/ChunkVertexEncoder.java @@ -10,6 +10,7 @@ public interface ChunkVertexEncoder { public float y; public float z; public int color; + public int rdhFactor; public float u; public float v; public int light; diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/CompactChunkVertex.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/CompactChunkVertex.java index 26fbb047c..92df12b77 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/CompactChunkVertex.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/CompactChunkVertex.java @@ -10,14 +10,15 @@ import java.util.List; import java.util.Map; public class CompactChunkVertex implements ChunkVertexType { - public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(20) + public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(24) .addElement("a_PosId", 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 4, false, true) .addElement("a_Color", 8, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false) .addElement("a_TexCoord", 12, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) .addElement("a_LightCoord", 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, true) + .addElement("a_RdhFactor", 20, GlVertexAttributeFormat.BYTE, 4, true, false) .build(); - public static final int STRIDE = 20; + public static final int STRIDE = 24; private static final int POSITION_MAX_VALUE = 65536; private static final int TEXTURE_MAX_VALUE = 32768; @@ -65,6 +66,7 @@ public class CompactChunkVertex implements ChunkVertexType { LWJGL.memPutShort(ptr + 14, encodeTexture(vertex.v)); LWJGL.memPutInt(ptr + 16, vertex.light); + LWJGL.memPutInt(ptr + 20, vertex.rdhFactor); return ptr + STRIDE; }; diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/VanillaLikeChunkVertex.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/VanillaLikeChunkVertex.java index 8be3d4f82..ddcf948ac 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/VanillaLikeChunkVertex.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/vertex/format/impl/VanillaLikeChunkVertex.java @@ -12,13 +12,14 @@ import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL; * compatible with mods & resource packs that need high precision for models. */ public class VanillaLikeChunkVertex implements ChunkVertexType { - public static final int STRIDE = 28; + public static final int STRIDE = 32; public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE) .addElement("a_PosId", 0, GlVertexAttributeFormat.FLOAT, 3, false, false) .addElement("a_Color", 12, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false) .addElement("a_TexCoord", 16, GlVertexAttributeFormat.FLOAT, 2, false, false) .addElement("a_LightCoord", 24, GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true) + .addElement("a_RdhFactor", 28, GlVertexAttributeFormat.BYTE, 4, true, false) .build(); @Override @@ -51,6 +52,7 @@ public class VanillaLikeChunkVertex implements ChunkVertexType { LWJGL.memPutFloat(ptr + 16, encodeTexture(vertex.u)); LWJGL.memPutFloat(ptr + 20, encodeTexture(vertex.v)); LWJGL.memPutInt(ptr + 24, (encodeDrawParameters(material, sectionIndex) << 0) | (encodeLight(vertex.light) << 16)); + LWJGL.memPutInt(ptr + 28, vertex.rdhFactor); return ptr + STRIDE; }; diff --git a/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.fsh b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.fsh index 63e9d3895..0cc09492a 100644 --- a/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.fsh +++ b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.fsh @@ -3,6 +3,9 @@ #import in vec4 v_Color; +in vec4 v_RdhFactor; +in vec2 v_QuadCoord; +in vec3 v_LightColor; in vec2 v_TexCoord; #if defined(USE_FOG) && defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0 @@ -57,6 +60,11 @@ void main() { #endif vec4 m_color = v_Color; + float correctionWeight = min(v_QuadCoord.x, v_QuadCoord.y) + * (1.0 - max(v_QuadCoord.x, v_QuadCoord.y)); + vec4 correction = v_RdhFactor * correctionWeight; + correction.rgb *= v_LightColor; + m_color += correction; #ifdef USE_VANILLA_COLOR_FORMAT // Apply per-vertex color. AO shade is applied ahead of time on the CPU. diff --git a/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.vsh b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.vsh index b6c0a2808..980aff6c9 100644 --- a/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.vsh +++ b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque.vsh @@ -6,6 +6,9 @@ #import out vec4 v_Color; +out vec4 v_RdhFactor; +out vec2 v_QuadCoord; +out vec3 v_LightColor; out vec2 v_TexCoord; #if defined(USE_FOG) && defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0 @@ -60,9 +63,16 @@ void main() { // Add the light color to the vertex color, and pass the texture coordinates to the fragment shader #ifdef CELERITAS_NO_LIGHTMAP v_Color = _vert_color; + v_LightColor = vec3(1.0); #else - v_Color = _vert_color * _sample_lightmap(u_LightTex, _vert_tex_light_coord); + vec4 lightColor = _sample_lightmap(u_LightTex, _vert_tex_light_coord); + v_Color = _vert_color * lightColor; + v_LightColor = lightColor.rgb; #endif + v_RdhFactor = _vert_rdh_factor * 2.0; + int corner = gl_VertexID & 3; + v_QuadCoord = vec2(corner >= 2 ? 1.0 : 0.0, + corner == 1 || corner == 2 ? 1.0 : 0.0); v_TexCoord = _vert_tex_diffuse_coord; v_MaterialMipBias = _material_mip_bias(_material_params); diff --git a/common/src/main/resources/assets/sodium/shaders/include/chunk_vertex.glsl b/common/src/main/resources/assets/sodium/shaders/include/chunk_vertex.glsl index 3ecab3c40..e95d993ff 100644 --- a/common/src/main/resources/assets/sodium/shaders/include/chunk_vertex.glsl +++ b/common/src/main/resources/assets/sodium/shaders/include/chunk_vertex.glsl @@ -10,6 +10,9 @@ ivec2 _vert_tex_light_coord; // The color of the vertex vec4 _vert_color; +// The "rdh factor" of the vertex, used when applying AO correction +vec4 _vert_rdh_factor; + // The index of the draw command which this vertex belongs to uint _draw_id; @@ -32,6 +35,7 @@ in uvec4 a_PosId; in vec4 a_Color; in vec2 a_TexCoord; in ivec2 a_LightCoord; +in vec4 a_RdhFactor; #if !defined(VERT_POS_SCALE) #error "VERT_POS_SCALE not defined" @@ -46,6 +50,7 @@ void _vert_init() { _vert_tex_diffuse_coord = (a_TexCoord * VERT_TEX_SCALE); _vert_tex_light_coord = a_LightCoord; _vert_color = a_Color; + _vert_rdh_factor = a_RdhFactor; _draw_id = (a_PosId.w >> 8u) & 0xFFu; _material_params = (a_PosId.w >> 0u) & 0xFFu; @@ -57,11 +62,13 @@ in vec3 a_PosId; in vec4 a_Color; in vec2 a_TexCoord; in uint a_LightCoord; +in vec4 a_RdhFactor; void _vert_init() { _vert_position = a_PosId; _vert_tex_diffuse_coord = a_TexCoord; _vert_color = a_Color; + _vert_rdh_factor = a_RdhFactor; uint packed_draw_params = (a_LightCoord & 0xFFFFu); // Vertex Material diff --git a/modern/src/main/shaders_java/net/irisshaders/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java b/modern/src/main/shaders_java/net/irisshaders/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java index 82dd46e42..5b36036c0 100644 --- a/modern/src/main/shaders_java/net/irisshaders/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java +++ b/modern/src/main/shaders_java/net/irisshaders/iris/compat/sodium/impl/vertex_format/terrain_xhfp/XHFPModelVertexType.java @@ -12,7 +12,7 @@ import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType; public class XHFPModelVertexType implements ChunkVertexType { public static final ChunkVertexType BASE_VERTEX_TYPE = ChunkMeshFormats.VANILLA_LIKE; - public static final int STRIDE = 48; + public static final int STRIDE = 52; public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE) .addAllElements(BASE_VERTEX_TYPE.getVertexFormat()) .addElement("mc_midTexCoord", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) -- 2.54.1