First pass at setup/begin pass support (#1335)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2026-01-29 07:53:04 -08:00
1 parent 9d90ff137c
commit 3cdd934987
4 files changed
+175 -11

No files matched your search

@@ -84,6 +84,7 @@ import org.joml.Vector4f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL42;
import java.util.ArrayList;
import java.util.Collections;
@@ -121,6 +122,8 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
private ImmutableList<ClearPass> shadowClearPasses;
private ImmutableList<ClearPass> shadowClearPassesFull;
private final ComputeProgram[] setup;
private final CompositeRenderer beginRenderer;
private final CompositeRenderer prepareRenderer;
@Nullable
@@ -312,8 +315,6 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
this.flippedBeforeShadow = ImmutableSet.of();
BufferFlipper flipper = new BufferFlipper();
this.centerDepthSampler = new CenterDepthSampler(() -> getRenderTargets().getDepthTexture(), programs.getPackDirectives().getCenterDepthHalfLife());
@@ -330,18 +331,26 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
PatchedShaderPrinter.resetPrintState();
final ProgramBuildContext beginBuildContext = new ProgramBuildContext(renderTargets, customTextureManager.getNoiseTexture(), updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureManager.getCustomTextureIdMap(TextureStage.BEGIN), customUniforms, customImages, customTextureManager.getIrisCustomTextures(), this);
final ProgramBuildContext prepareBuildContext = new ProgramBuildContext(renderTargets, customTextureManager.getNoiseTexture(), updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureManager.getCustomTextureIdMap(TextureStage.PREPARE), customUniforms, customImages, customTextureManager.getIrisCustomTextures(), this);
final ProgramBuildContext deferredBuildContext = new ProgramBuildContext(renderTargets, customTextureManager.getNoiseTexture(), updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureManager.getCustomTextureIdMap(TextureStage.DEFERRED), customUniforms, customImages, customTextureManager.getIrisCustomTextures(), this);
final ProgramBuildContext compositeBuildContext = new ProgramBuildContext(renderTargets, customTextureManager.getNoiseTexture(), updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureManager.getCustomTextureIdMap(TextureStage.COMPOSITE_AND_FINAL), customUniforms, customImages, customTextureManager.getIrisCustomTextures(), this);
this.prepareRenderer = new CompositeRenderer(programs.getPrepare(), programs.getPrepareCompute(), flipper, prepareBuildContext, programs.getPackDirectives().getExplicitFlips("prepare_pre"), prepareTransformFutures, "prepare");
this.setup = createSetupComputes(programs.getSetup());
this.beginRenderer = new CompositeRenderer(programs.getBegin(), programs.getBeginCompute(), flipper, beginBuildContext, programs.getPackDirectives().getExplicitFlips("begin_pre"), null, "begin", TextureStage.BEGIN);
this.flippedBeforeShadow = flipper.snapshot();
this.prepareRenderer = new CompositeRenderer(programs.getPrepare(), programs.getPrepareCompute(), flipper, prepareBuildContext, programs.getPackDirectives().getExplicitFlips("prepare_pre"), prepareTransformFutures, "prepare", TextureStage.PREPARE);
flippedAfterPrepare = flipper.snapshot();
this.deferredRenderer = new CompositeRenderer(programs.getDeferred(), programs.getDeferredCompute(), flipper, deferredBuildContext, programs.getPackDirectives().getExplicitFlips("deferred_pre"), deferredTransformFutures, "deferred");
this.deferredRenderer = new CompositeRenderer(programs.getDeferred(), programs.getDeferredCompute(), flipper, deferredBuildContext, programs.getPackDirectives().getExplicitFlips("deferred_pre"), deferredTransformFutures, "deferred", TextureStage.DEFERRED);
flippedAfterTranslucent = flipper.snapshot();
this.compositeRenderer = new CompositeRenderer(programs.getComposite(), programs.getCompositeCompute(), flipper, compositeBuildContext, programs.getPackDirectives().getExplicitFlips("composite_pre"), compositeTransformFutures, "composite");
this.compositeRenderer = new CompositeRenderer(programs.getComposite(), programs.getCompositeCompute(), flipper, compositeBuildContext, programs.getPackDirectives().getExplicitFlips("composite_pre"), compositeTransformFutures, "composite", TextureStage.COMPOSITE_AND_FINAL);
this.finalPassRenderer = new FinalPassRenderer(programs, compositeBuildContext, flipper.snapshot(), this.compositeRenderer.getFlippedAtLeastOnceFinal(), finalTransformFuture, "final");
// [(textured=false,lightmap=false), (textured=true,lightmap=false), (textured=true,lightmap=true)]
@@ -466,6 +475,25 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
this.clearPassesFull = ClearPassCreator.createClearPasses(renderTargets, true, programs.getPackDirectives().getRenderTargetDirectives());
this.clearPasses = ClearPassCreator.createClearPasses(renderTargets, false, programs.getPackDirectives().getRenderTargetDirectives());
boolean hasSetup = false;
for (ComputeProgram program : setup) {
if (program != null) {
if (!hasSetup) {
hasSetup = true;
renderTargets.onFullClear();
final Vector4f fogColor = new Vector4f(1.0F, 1.0F, 1.0F, 1.0F);
clearPassesFull.forEach(clearPass -> clearPass.execute(fogColor));
}
program.use();
customUniforms.push(program);
program.dispatch(1, 1);
}
}
if (hasSetup) {
ComputeProgram.unbind();
RenderSystem.memoryBarrier(GL42.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL42.GL_TEXTURE_FETCH_BARRIER_BIT);
}
// Terrain pipeline sampler/image factory setup follows.
Supplier<ImmutableSet<Integer>> flipped = () -> isBeforeTranslucent ? flippedAfterPrepare : flippedAfterTranslucent;
@@ -958,12 +986,22 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
// Destroy the composite rendering pipeline
//
// This destroys all the loaded composite programs as well.
beginRenderer.destroy();
prepareRenderer.destroy();
compositeRenderer.destroy();
deferredRenderer.destroy();
finalPassRenderer.destroy();
centerDepthSampler.destroy();
// Destroy setup compute programs
if (setup != null) {
for (ComputeProgram compute : setup) {
if (compute != null) {
compute.destroy();
}
}
}
// Destroy shadow compute programs
if (shadowComputes != null) {
for (ComputeProgram compute : shadowComputes) {
@@ -1086,6 +1124,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
main.framebufferHeight, depthBufferFormat, packDirectives);
if (changed) {
beginRenderer.recalculateSizes();
prepareRenderer.recalculateSizes();
deferredRenderer.recalculateSizes();
compositeRenderer.recalculateSizes();
@@ -1106,6 +1145,21 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
if (ssboHolder != null) {
ssboHolder.hasResizedScreen(main.framebufferWidth, main.framebufferHeight);
}
// Re-run setup computes after resize (SETUP dispatches with 1,1 workgroups)
boolean ranSetup = false;
for (ComputeProgram program : setup) {
if (program != null) {
ranSetup = true;
program.use();
customUniforms.push(program);
program.dispatch(1, 1);
}
}
if (ranSetup) {
ComputeProgram.unbind();
RenderSystem.memoryBarrier(GL42.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL42.GL_TEXTURE_FETCH_BARRIER_BIT);
}
}
final ImmutableList<ClearPass> passes;
@@ -1192,6 +1246,60 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
return programs;
}
private ComputeProgram[] createSetupComputes(ComputeSource[] compute) {
ComputeProgram[] programs = new ComputeProgram[compute.length];
for (int i = 0; i < programs.length; i++) {
ComputeSource source = compute[i];
if (source == null || !source.getSource().isPresent()) {
continue;
} else {
ProgramBuilder builder;
try {
String transformed = TransformPatcher.patchCompute(source.getName(), source.getSource().orElse(null), TextureStage.SETUP, getTextureMap());
PatchedShaderPrinter.debugPatchedShaders(source.getName() + "_compute", null, null, null, transformed);
builder = ProgramBuilder.beginCompute(source.getName(), transformed, IrisSamplers.COMPOSITE_RESERVED_TEXTURE_UNITS);
} catch (RuntimeException e) {
throw new RuntimeException("Shader compilation failed for setup compute " + source.getName() + "!", e);
}
CommonUniforms.addDynamicUniforms(builder, FogMode.OFF);
this.customUniforms.assignTo(builder);
ImmutableSet<Integer> empty = ImmutableSet.of();
Supplier<ImmutableSet<Integer>> flipped = () -> empty;
ProgramSamplers.CustomTextureSamplerInterceptor customTextureSamplerInterceptor =
ProgramSamplers.customTextureSamplerInterceptor(builder, customTextureManager.getCustomTextureIdMap(TextureStage.SETUP));
IrisSamplers.addRenderTargetSamplers(customTextureSamplerInterceptor, flipped, renderTargets, true, this);
IrisSamplers.addCustomImages(customTextureSamplerInterceptor, customImages);
IrisSamplers.addCustomTextures(customTextureSamplerInterceptor, customTextureManager.getIrisCustomTextures());
IrisImages.addRenderTargetImages(builder, flipped, renderTargets);
IrisImages.addCustomImages(builder, customImages);
IrisSamplers.addNoiseSampler(customTextureSamplerInterceptor, customTextureManager.getNoiseTexture());
IrisSamplers.addCompositeSamplers(customTextureSamplerInterceptor, renderTargets);
if (IrisSamplers.hasShadowSamplers(customTextureSamplerInterceptor)) {
if (shadowRenderTargets != null) {
IrisSamplers.addShadowSamplers(customTextureSamplerInterceptor, shadowRenderTargets, null, true);
IrisImages.addShadowColorImages(builder, shadowRenderTargets, null);
}
}
programs[i] = builder.buildCompute();
this.customUniforms.mapholderToPass(builder, programs[i]);
programs[i].setWorkGroupInfo(source.getWorkGroupRelative(), source.getWorkGroups(), null);
}
}
return programs;
}
@Override
public void beginHand() {
// We need to copy the current depth texture so that depthtex2 can contain the depth values for
@@ -1314,6 +1422,10 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
image.clear();
}
isRenderingFullScreenPass = true;
beginRenderer.renderAll();
isRenderingFullScreenPass = false;
setPhase(WorldRenderingPhase.SKY);
// Render our horizon box before actual sky rendering to avoid being broken by mods that do weird things
@@ -125,7 +125,11 @@ public class TransformPatcher {
}
public static Map<PatchShaderType, String> patchComposite(String vertex, String geometry, String fragment) {
return transform(vertex, geometry, fragment, new TextureStageParameters(Patch.COMPOSITE, TextureStage.COMPOSITE_AND_FINAL, null));
return patchComposite(vertex, geometry, fragment, TextureStage.COMPOSITE_AND_FINAL, null);
}
public static Map<PatchShaderType, String> patchComposite(String vertex, String geometry, String fragment, TextureStage stage, Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap) {
return transform(vertex, geometry, fragment, new TextureStageParameters(Patch.COMPOSITE, stage, textureMap));
}
public static String patchCompute(String name, String compute, TextureStage stage, Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap) {
@@ -65,6 +65,7 @@ public class CompositeRenderer {
@Nullable private final Set<GlImage> customImages;
@Nullable private final Object2ObjectMap<String, TextureAccess> irisCustomTextures;
@Nullable private final WorldRenderingPipeline pipeline;
private final TextureStage textureStage;
@Getter
private final ImmutableSet<Integer> flippedAtLeastOnceFinal;
@@ -75,10 +76,10 @@ public class CompositeRenderer {
Object2ObjectMap<String, TextureAccess> customTextureIds, ImmutableMap<Integer, Boolean> explicitPreFlips,
CustomUniforms customUniforms,
Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> precomputedTransformFutures) {
this(sources, computes, bufferFlipper, new ProgramBuildContext(renderTargets, noiseTexture, updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureIds, customUniforms, null, null, null), explicitPreFlips, precomputedTransformFutures, "unknown");
this(sources, computes, bufferFlipper, new ProgramBuildContext(renderTargets, noiseTexture, updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureIds, customUniforms, null, null, null), explicitPreFlips, precomputedTransformFutures, "unknown", TextureStage.COMPOSITE_AND_FINAL);
}
public CompositeRenderer(ProgramSource[] sources, ComputeSource[][] computes, BufferFlipper bufferFlipper, ProgramBuildContext context, ImmutableMap<Integer, Boolean> explicitPreFlips, Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> precomputedTransformFutures, String stageName) {
public CompositeRenderer(ProgramSource[] sources, ComputeSource[][] computes, BufferFlipper bufferFlipper, ProgramBuildContext context, ImmutableMap<Integer, Boolean> explicitPreFlips, Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> precomputedTransformFutures, String stageName, TextureStage textureStage) {
this.noiseTexture = context.noiseTexture();
this.updateNotifier = context.updateNotifier();
this.centerDepthSampler = context.centerDepthSampler();
@@ -88,6 +89,7 @@ public class CompositeRenderer {
this.customImages = context.customImages();
this.irisCustomTextures = context.irisCustomTextures();
this.pipeline = context.pipeline();
this.textureStage = textureStage;
final ImmutableList.Builder<Pass> passes = ImmutableList.builder();
final ImmutableSet.Builder<Integer> flippedAtLeastOnce = new ImmutableSet.Builder<>();
@@ -171,7 +173,7 @@ public class CompositeRenderer {
OpenGlHelper.func_153171_g/*glBindFramebuffer*/(GL30.GL_READ_FRAMEBUFFER, 0);
}
private static Map<PatchShaderType, String> getTransformed(ProgramSource source, Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> transformFutures, int index, String stageName) {
private Map<PatchShaderType, String> getTransformed(ProgramSource source, Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> transformFutures, int index, String stageName) {
if (transformFutures != null) {
final CompletableFuture<Map<PatchShaderType, String>> future = transformFutures.get(index);
if (future != null) {
@@ -187,7 +189,7 @@ public class CompositeRenderer {
}
// Fallback: transform synchronously
return TransformPatcher.patchComposite(source.getVertexSource().orElseThrow(NullPointerException::new), source.getGeometrySource().orElse(null), source.getFragmentSource().orElseThrow(NullPointerException::new));
return TransformPatcher.patchComposite(source.getVertexSource().orElseThrow(NullPointerException::new), source.getGeometrySource().orElse(null), source.getFragmentSource().orElseThrow(NullPointerException::new), this.textureStage, pipeline != null ? pipeline.getTextureMap() : null);
}
public void recalculateSizes() {
@@ -399,7 +401,7 @@ public class CompositeRenderer {
ProgramBuilder builder;
try {
String transformed = TransformPatcher.patchCompute(source.getName(), source.getSource().orElse(null), TextureStage.COMPOSITE_AND_FINAL, pipeline != null ? pipeline.getTextureMap() : null);
String transformed = TransformPatcher.patchCompute(source.getName(), source.getSource().orElse(null), this.textureStage, pipeline != null ? pipeline.getTextureMap() : null);
PatchedShaderPrinter.debugPatchedShaders(source.getName() + "_compute", null, null, null, transformed);
builder = ProgramBuilder.beginCompute(source.getName(), transformed, IrisSamplers.COMPOSITE_RESERVED_TEXTURE_UNITS);
} catch (RuntimeException e) {
@@ -20,6 +20,9 @@ public class ProgramSet {
private final ProgramSource[] shadowcomp;
private final ComputeSource[][] shadowCompCompute;
private final ComputeSource[] setup;
private final ProgramSource[] begin;
private final ComputeSource[][] beginCompute;
private final ProgramSource[] prepare;
private final ComputeSource[][] prepareCompute;
@@ -82,6 +85,14 @@ public class ProgramSet {
this.shadowCompCompute[i] = readComputeArray(directory, sourceProvider, "shadowcomp" + ((i == 0) ? "" : i));
}
this.setup = readComputeProgramArray(directory, sourceProvider, "setup");
this.begin = readProgramArray(directory, sourceProvider, "begin", shaderProperties);
this.beginCompute = new ComputeSource[begin.length][];
for (int i = 0; i < begin.length; i++) {
this.beginCompute[i] = readComputeArray(directory, sourceProvider, "begin" + ((i == 0) ? "" : i));
}
this.prepare = readProgramArray(directory, sourceProvider, "prepare", shaderProperties);
this.prepareCompute = new ComputeSource[prepare.length][];
for (int i = 0; i < prepare.length; i++) {
@@ -162,6 +173,18 @@ public class ProgramSet {
return programs;
}
private ComputeSource[] readComputeProgramArray(AbsolutePackPath directory, Function<AbsolutePackPath, String> sourceProvider, String name) {
ComputeSource[] programs = new ComputeSource[100];
for (int i = 0; i < programs.length; i++) {
String suffix = i == 0 ? "" : Integer.toString(i);
programs[i] = readComputeSource(directory, sourceProvider, name + suffix, this);
}
return programs;
}
private ComputeSource[] readComputeArray(AbsolutePackPath directory,
Function<AbsolutePackPath, String> sourceProvider, String name) {
ComputeSource[] programs = new ComputeSource[27];
@@ -187,6 +210,7 @@ public class ProgramSet {
programs.add(shadow);
programs.addAll(Arrays.asList(shadowcomp));
programs.addAll(Arrays.asList(begin));
programs.addAll(Arrays.asList(prepare));
programs.addAll (Arrays.asList(
@@ -212,6 +236,16 @@ public class ProgramSet {
computes.addAll(Arrays.asList(computeSources));
}
for (ComputeSource computeSource : setup) {
if (computeSource != null) {
computes.add(computeSource);
}
}
for (ComputeSource[] computeSources : beginCompute) {
computes.addAll(Arrays.asList(computeSources));
}
Collections.addAll(computes, finalCompute);
Collections.addAll(computes, shadowCompute);
@@ -263,6 +297,18 @@ public class ProgramSet {
return shadowcomp;
}
public ComputeSource[] getSetup() {
return setup;
}
public ProgramSource[] getBegin() {
return begin;
}
public ComputeSource[][] getBeginCompute() {
return beginCompute;
}
public ProgramSource[] getPrepare() {
return prepare;
}