Cache Iris Shaders (#2206)

This commit is contained in:
Eclipse-Sol authored and GitHub committed 2026-10-03 20:06:39 -07:00
1 parent 231a2ab216
commit 0a4d48964e
21 files changed
+1354 -554

No files matched your search

@@ -1,5 +1,7 @@
package net.coderbot.iris.celeritas;
import com.gtnewhorizons.angelica.glsm.GLDebug;
import com.gtnewhorizons.angelica.glsm.shader.ProgramBinaryCache;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.blending.BlendModeOverride;
import net.coderbot.iris.gl.blending.BufferBlendOverride;
@@ -13,12 +15,16 @@ import org.embeddedt.embeddium.impl.gl.shader.ShaderType;
import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration;
import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderInterface;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
import org.embeddedt.embeddium.impl.gl.shader.ShaderBindingContext;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.KHRDebug;
import java.util.EnumMap;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;
public class IrisCeleritasChunkProgramOverrides {
private final EnumMap<IrisTerrainPass, GlProgram<IrisCeleritasChunkShaderInterface>> programs = new EnumMap<>(IrisTerrainPass.class);
@@ -56,7 +62,48 @@ public class IrisCeleritasChunkProgramOverrides {
}
@Nullable
private GlProgram<IrisCeleritasChunkShaderInterface> createShader(IrisTerrainPass pass, CeleritasTerrainPipeline pipeline, RenderPassConfiguration<?> configuration) {
private static ProgramBinaryCache.Key cacheKey(CeleritasTerrainPipeline.PassInfo passInfo, ChunkVertexType vertexType) {
if (!ProgramBinaryCache.isEnabled()) return null;
final String vertex = passInfo.sources().get(PatchShaderType.VERTEX).orElse(null);
final String geometry = passInfo.sources().get(PatchShaderType.GEOMETRY).orElse(null);
final String fragment = passInfo.sources().get(PatchShaderType.FRAGMENT).orElse(null);
if (vertex == null || fragment == null) return null;
final ProgramBinaryCache.Key key = ProgramBinaryCache.key();
int attrIndex = 0;
for (var attr : vertexType.getVertexFormat().getAttributes()) {
key.attribute(attr.getName(), attrIndex++);
}
key.stage(ShaderType.VERTEX.id, vertex);
if (geometry != null) key.stage(ShaderType.GEOM.id, geometry);
key.stage(ShaderType.FRAGMENT.id, fragment);
return key;
}
private static final class SavedProgram extends GlProgram<IrisCeleritasChunkShaderInterface> {
SavedProgram(int program, Function<ShaderBindingContext, IrisCeleritasChunkShaderInterface> factory) {
super(program, factory);
}
}
@Nullable
private GlProgram<IrisCeleritasChunkShaderInterface> createShader(IrisTerrainPass pass, CeleritasTerrainPipeline pipeline, ChunkVertexType vertexType) {
final CeleritasTerrainPipeline.PassInfo passInfo = pipeline.getPassInfo(pass);
final String name = "iris:celeritas-chunk-" + pass.getName();
final BlendModeOverride blendOverride = passInfo.blendModeOverride();
final List<BufferBlendOverride> bufferOverrides = passInfo.bufferBlendOverrides();
final Function<ShaderBindingContext, IrisCeleritasChunkShaderInterface> factory = context -> new IrisCeleritasChunkShaderInterface(
((GlObject) context).handle(), context, pipeline, pass.isShadow(), blendOverride, bufferOverrides, pipeline.getCustomUniforms());
final ProgramBinaryCache.Key cacheKey = cacheKey(passInfo, vertexType);
if (cacheKey != null) {
final int saved = ProgramBinaryCache.load(cacheKey);
if (saved != 0) {
GLDebug.nameObject(KHRDebug.GL_PROGRAM, saved, name);
return new SavedProgram(saved, factory);
}
}
final GlShader vertShader = createVertexShader(pass, pipeline);
final GlShader geomShader = createGeometryShader(pass, pipeline);
final GlShader fragShader = createFragmentShader(pass, pipeline);
@@ -69,7 +116,7 @@ public class IrisCeleritasChunkProgramOverrides {
}
try {
final GlProgram.Builder builder = GlProgram.builder("iris:celeritas-chunk-" + pass.getName());
final GlProgram.Builder builder = GlProgram.builder(name);
builder.attachShader(vertShader);
if (geomShader != null) {
@@ -78,17 +125,16 @@ public class IrisCeleritasChunkProgramOverrides {
builder.attachShader(fragShader);
// Bind all attributes from the vertex format (includes base + Iris extended attributes)
final var vertexType =pass.toTerrainPass(configuration).vertexType();
int attrIndex = 0;
for (var attr : vertexType.getVertexFormat().getAttributes()) {
builder.bindAttribute(attr.getName(), attrIndex++);
}
final CeleritasTerrainPipeline.PassInfo passInfo = pipeline.getPassInfo(pass);
final BlendModeOverride blendOverride = passInfo.blendModeOverride();
final List<BufferBlendOverride> bufferOverrides = passInfo.bufferBlendOverrides();
return builder.link(context -> new IrisCeleritasChunkShaderInterface(((GlObject) context).handle(), context, pipeline, pass.isShadow(), blendOverride, bufferOverrides, pipeline.getCustomUniforms()));
final GlProgram<IrisCeleritasChunkShaderInterface> program = builder.link(factory);
if (cacheKey != null) {
ProgramBinaryCache.save(cacheKey, program.handle());
}
return program;
} finally {
vertShader.delete();
if (geomShader != null) geomShader.delete();
@@ -100,13 +146,21 @@ public class IrisCeleritasChunkProgramOverrides {
* Create shaders for all Iris terrain passes.
*/
public void createShaders(CeleritasTerrainPipeline pipeline, RenderPassConfiguration<?> configuration) {
createShaders(pipeline, pass -> pass.toTerrainPass(configuration).vertexType());
}
public void createShaders(CeleritasTerrainPipeline pipeline, ChunkVertexType vertexType) {
createShaders(pipeline, pass -> vertexType);
}
private void createShaders(CeleritasTerrainPipeline pipeline, Function<IrisTerrainPass, ChunkVertexType> vertexTypeOf) {
if (pipeline != null) {
for (IrisTerrainPass pass : IrisTerrainPass.VALUES) {
if (pass.isShadow() && !pipeline.hasShadowPass()) {
this.programs.put(pass, null);
continue;
}
this.programs.put(pass, createShader(pass, pipeline, configuration));
this.programs.put(pass, createShader(pass, pipeline, vertexTypeOf.apply(pass)));
}
} else {
deleteShaders();