slightly faster bilinear interpolation #33

Merged
embeddedt merged 4 commits from rdh/celeritas:ao-better into stonecutter 2026-08-18 20:00:03 -04:00
5 changed files with 19 additions and 10 deletions

No files matched your search

@@ -32,7 +32,9 @@ public class ChunkMeshBufferBuilder {
}
public void push(ChunkVertexEncoder.Vertex[] vertices, Material material) {
postprocessVertices(vertices);
if (this.encoder.supportsBilinearCorrection()) {
postprocessVertices(vertices);
}
var vertexStart = this.count * this.stride;
var vertexSize = vertices.length * this.stride;
@@ -5,6 +5,10 @@ import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
public interface ChunkVertexEncoder {
long write(long ptr, Material material, Vertex vertex, int sectionIndex);
default boolean supportsBilinearCorrection() {
return true;
}
class Vertex {
public float x;
public float y;
@@ -5,7 +5,6 @@
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
@@ -61,11 +60,10 @@ void main() {
vec4 m_color = v_Color;
// Apply correction factor to make the resulting color very close to what true bilinear interpolation would obtain
// min(x, y) * (1 - max(x, y)) == min(x, y) - (x * y)
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;
- (v_QuadCoord.x * v_QuadCoord.y);
m_color += v_RdhFactor * correctionWeight;
#ifdef USE_VANILLA_COLOR_FORMAT
// Apply per-vertex color. AO shade is applied ahead of time on the CPU.
@@ -8,7 +8,6 @@
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
@@ -61,15 +60,15 @@ void main() {
gl_Position = u_ProjectionMatrix * u_ModelViewMatrix * vec4(position, 1.0);
// Add the light color to the vertex color, and pass the texture coordinates to the fragment shader
v_RdhFactor = _vert_rdh_factor * 2.0;
#ifdef CELERITAS_NO_LIGHTMAP
v_Color = _vert_color;
v_LightColor = vec3(1.0);
#else
vec4 lightColor = _sample_lightmap(u_LightTex, _vert_tex_light_coord);
v_Color = _vert_color * lightColor;
v_LightColor = lightColor.rgb;
v_RdhFactor.rgb *= 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);
@@ -144,6 +144,11 @@ public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkV
this.ignoreMidBlock = false;
}
@Override
public boolean supportsBilinearCorrection() {
return false;
}
@Override
public long write(long ptr,
Material material, Vertex vertex, int chunkId) {
@@ -151,6 +156,7 @@ public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkV
vSum += vertex.v;
vertexCount++;
vertex.rdhFactor = 0;
this.baseEncoder.write(ptr, material, vertex, chunkId);
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET + 2, renderType);