Add translucency layer targets and depth-sorted composite

This commit is contained in:
Ruling-0 committed 2026-11-08 12:15:36 -06:00
1 parent 12840f8953
commit 433d08bc5f
4 files changed
+538

No files matched your search

@@ -0,0 +1,298 @@
package com.gtnewhorizons.angelica.rendering.translucency;
import com.gtnewhorizons.angelica.glsm.GLSMAttribStack;
import com.gtnewhorizons.angelica.glsm.GLSMBlend;
import com.gtnewhorizons.angelica.glsm.GLSMDepthStencilAlpha;
import com.gtnewhorizons.angelica.glsm.GLSMDraws;
import com.gtnewhorizons.angelica.glsm.GLSMFramebuffer;
import com.gtnewhorizons.angelica.glsm.GLSMPrograms;
import com.gtnewhorizons.angelica.glsm.GLSMQueries;
import com.gtnewhorizons.angelica.glsm.GLSMRasterizer;
import com.gtnewhorizons.angelica.glsm.GLSMTextures;
import com.gtnewhorizons.angelica.glsm.GLSMVertexInput;
import com.gtnewhorizons.angelica.glsm.RenderSystem;
import com.gtnewhorizons.angelica.glsm.StateSet;
import com.gtnewhorizons.angelica.glsm.hooks.BatchStateGuard;
import com.gtnewhorizons.angelica.glsm.texture.DepthBufferFormat;
import com.gtnewhorizons.angelica.glsm.texture.TextureInfoCache;
import com.gtnewhorizons.angelica.proxy.ClientProxy;
import com.gtnewhorizons.angelica.render.PanoramaRenderer;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.pipeline.FixedFunctionWorldRenderingPipeline;
import net.coderbot.iris.rendertarget.IRenderTargetExt;
import net.minecraft.client.Minecraft;
import net.minecraft.client.shader.Framebuffer;
import org.embeddedt.embeddium.impl.gl.shader.GlProgram;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL30;
import java.nio.ByteBuffer;
/// Draws each [TranslucentLayer] into its own target, seeded with the main depth, and composites the drawn layers over
/// the main framebuffer per pixel, farthest first. Layers hold premultiplied color with coverage alpha (GLSM layer
/// blending), and the composite writes the nearest layer depth.
public final class TranslucencyLayers {
public static final TranslucencyLayers INSTANCE = new TranslucencyLayers(new MinecraftTarget());
private static final int LAYERS = TranslucentLayer.values().length;
private static final int FIRST_LAYER_UNIT = 1;
interface MainTarget {
int framebuffer();
int depthTexture();
int width();
int height();
int depthVersion();
}
private final MainTarget main;
private final Target[] layers = new Target[LAYERS];
private Target depthSnapshot;
private int width;
private int height;
private int depthVersion = -1;
private DepthBufferFormat depthFormat;
private boolean active;
private int drawnMask;
private TranslucentLayer open;
private int savedDraw;
private int savedRead;
private GlProgram<Object> program;
private int maskUniform;
private int vao;
private StateSet prepareState;
private StateSet compositeState;
TranslucencyLayers(MainTarget main) {
this.main = main;
}
public void beginFrame() {
beginFrame(ClientProxy.options().performance.translucencyMode.composites() && Iris.enabled
&& Iris.getPipelineManager().getPipelineNullable() instanceof FixedFunctionWorldRenderingPipeline);
}
void beginFrame(boolean active) {
this.active = active;
this.drawnMask = 0;
}
public boolean active() {
return active;
}
public void begin(TranslucentLayer layer) {
if (open != null) throw new IllegalStateException("Translucency layer " + open + " is still open");
BatchStateGuard.beforeChange();
savedDraw = GLSMFramebuffer.getDrawFramebuffer();
savedRead = GLSMFramebuffer.getReadFramebuffer();
syncSize();
final Target target = layer(layer);
final int bit = 1 << layer.ordinal();
if ((drawnMask & bit) == 0) {
drawnMask |= bit;
prepare(target);
}
target.framebuffer.bind();
open = layer;
GLSMBlend.setLayerBlending(true);
}
public void end() {
BatchStateGuard.beforeChange();
GLSMBlend.setLayerBlending(false);
GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, savedDraw);
GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, savedRead);
open = null;
}
public boolean redirectsMainBind() {
return open != null;
}
public void bindOpenLayer() {
layers[open.ordinal()].framebuffer.bind();
}
public void composite() {
if (drawnMask == 0) return;
BatchStateGuard.beforeChange();
final int draw = GLSMFramebuffer.getDrawFramebuffer();
final int read = GLSMFramebuffer.getReadFramebuffer();
final int activeProgram = GLSMPrograms.getActiveProgram();
final int activeVao = GLSMQueries.glGetInteger(GL30.GL_VERTEX_ARRAY_BINDING);
if (program == null) initProgram();
syncSize();
if (drawnMask == 0) return;
final Target snapshot = depthSnapshot();
final int depth = GLSMAttribStack.pushState(compositeState);
try {
GLSMRasterizer.disableScissorTest();
blit(main.framebuffer(), snapshot.framebuffer.getId(), GL11.GL_DEPTH_BUFFER_BIT);
GLSMFramebuffer.glBindFramebuffer(GL30.GL_FRAMEBUFFER, main.framebuffer());
GLSMRasterizer.glViewport(0, 0, width, height);
GLSMBlend.enableBlend();
GLSMBlend.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ZERO, GL11.GL_ONE);
GLSMRasterizer.disableCull();
GLSMDepthStencilAlpha.enableDepthTest();
GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_ALWAYS);
GLSMDepthStencilAlpha.glDepthMask(true);
bindTexture(0, snapshot.depth);
for (int i = 0; i < LAYERS; i++) {
if ((drawnMask & (1 << i)) == 0) continue;
bindTexture(FIRST_LAYER_UNIT + i, layers[i].color);
bindTexture(FIRST_LAYER_UNIT + LAYERS + i, layers[i].depth);
}
GLSMPrograms.glUseProgram(program.handle());
GLSMPrograms.glUniform1i(maskUniform, drawnMask);
GLSMVertexInput.glBindVertexArray(vao);
GLSMDraws.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
} finally {
GLSMVertexInput.glBindVertexArray(activeVao);
GLSMPrograms.glUseProgram(activeProgram);
GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, draw);
GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, read);
GLSMAttribStack.popStateTo(depth);
drawnMask = 0;
}
}
public void destroy() {
destroyTargets();
if (program != null) {
program.delete();
GLSMVertexInput.glDeleteVertexArrays(vao);
program = null;
}
}
private void initProgram() {
program = PanoramaRenderer.loadProgram("angelica:translucency_composite", "angelica:panorama_blur", "angelica:translucency_composite", ctx -> ctx);
final int handle = program.handle();
GLSMPrograms.glUseProgram(handle);
GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(handle, "u_Depth"), 0);
for (int i = 0; i < LAYERS; i++) {
GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(handle, "u_Layer" + i), FIRST_LAYER_UNIT + i);
GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(handle, "u_LayerDepth" + i), FIRST_LAYER_UNIT + LAYERS + i);
}
maskUniform = GLSMPrograms.glGetUniformLocation(handle, "u_LayerMask");
GLSMPrograms.glUseProgram(0);
vao = GLSMVertexInput.glGenVertexArrays();
compositeState = StateSet.forMask(GL11.GL_ALL_ATTRIB_BITS);
}
private void prepare(Target target) {
if (prepareState == null) prepareState = StateSet.forMask(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_SCISSOR_BIT);
final int depth = GLSMAttribStack.pushState(prepareState);
try {
GLSMRasterizer.disableScissorTest();
GLSMBlend.glColorMask(true, true, true, true);
target.framebuffer.bind();
GLSMBlend.glClearColor(0f, 0f, 0f, 0f);
GLSMFramebuffer.glClear(GL11.GL_COLOR_BUFFER_BIT);
blit(main.framebuffer(), target.framebuffer.getId(), depthFormat.isCombinedStencil() ? GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT : GL11.GL_DEPTH_BUFFER_BIT);
} finally {
GLSMAttribStack.popStateTo(depth);
}
}
private void blit(int from, int to, int mask) {
GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, from);
GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, to);
GLSMFramebuffer.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, mask, GL11.GL_NEAREST);
}
private static void bindTexture(int unit, int texture) {
GLSMTextures.glActiveTexture(GL13.GL_TEXTURE0 + unit);
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, texture);
}
private void syncSize() {
if (main.width() == width && main.height() == height && main.depthVersion() == depthVersion) return;
destroyTargets();
width = main.width();
height = main.height();
depthVersion = main.depthVersion();
depthFormat = DepthBufferFormat.fromGlEnumOrDefault(TextureInfoCache.INSTANCE.getInfo(main.depthTexture()).getInternalFormat());
}
private Target layer(TranslucentLayer layer) {
final int i = layer.ordinal();
if (layers[i] == null) layers[i] = new Target(width, height, depthFormat, true);
return layers[i];
}
private Target depthSnapshot() {
if (depthSnapshot == null) depthSnapshot = new Target(width, height, depthFormat, false);
return depthSnapshot;
}
private void destroyTargets() {
for (int i = 0; i < LAYERS; i++) {
if (layers[i] != null) layers[i].destroy();
layers[i] = null;
}
if (depthSnapshot != null) depthSnapshot.destroy();
depthSnapshot = null;
drawnMask = 0;
}
private static final class Target {
final int color;
final int depth;
final GlFramebuffer framebuffer;
Target(int width, int height, DepthBufferFormat depthFormat, boolean withColor) {
depth = texture(width, height, depthFormat.getGlInternalFormat(), depthFormat.getGlType(), depthFormat.getGlFormat());
framebuffer = new GlFramebuffer();
framebuffer.addDepthAttachment(depth);
if (withColor) {
color = texture(width, height, GL11.GL_RGBA8, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE);
framebuffer.addColorAttachment(0, color);
framebuffer.drawBuffers(new int[] { 0 });
framebuffer.readBuffer(0);
} else {
color = 0;
framebuffer.noDrawBuffers();
RenderSystem.readBuffer(framebuffer.getId(), GL11.GL_NONE);
}
}
private static int texture(int width, int height, int internalFormat, int format, int type) {
final int previous = GLSMQueries.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
final int texture = GLSMTextures.glGenTextures();
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GLSMTextures.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GLSMTextures.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, (ByteBuffer) null);
GLSMTextures.glBindTexture(GL11.GL_TEXTURE_2D, previous);
return texture;
}
void destroy() {
framebuffer.destroy();
if (color != 0) GLSMTextures.glDeleteTextures(color);
GLSMTextures.glDeleteTextures(depth);
}
}
private static final class MinecraftTarget implements MainTarget {
private static Framebuffer fb() {
return Minecraft.getMinecraft().getFramebuffer();
}
@Override public int framebuffer() { return fb().framebufferObject; }
@Override public int depthTexture() { return ((IRenderTargetExt) fb()).iris$getDepthTextureId(); }
@Override public int width() { return fb().framebufferWidth; }
@Override public int height() { return fb().framebufferHeight; }
@Override public int depthVersion() { return ((IRenderTargetExt) fb()).iris$getDepthBufferVersion(); }
}
}
@@ -0,0 +1,9 @@
package com.gtnewhorizons.angelica.rendering.translucency;
public enum TranslucentLayer {
TERRAIN,
BLOCK_ENTITIES,
PARTICLES,
WEATHER,
CLOUDS
}