Parallel shader transformation and use Transformer instead of Utils (#1191)

This commit is contained in:
Jason Mitchell authored and GitHub committed 2025-12-23 09:11:32 -08:00
1 parent f0d45b0d62
commit 89132f3ef9
16 files changed
+743 -302

No files matched your search

@@ -39,6 +39,8 @@ import org.lwjgl.opengl.GL30;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
@@ -58,17 +60,25 @@ public class CompositeRenderer {
IntSupplier noiseTexture, FrameUpdateNotifier updateNotifier,
CenterDepthSampler centerDepthSampler, BufferFlipper bufferFlipper,
Supplier<ShadowRenderTargets> shadowTargetsSupplier,
Object2ObjectMap<String, IntSupplier> customTextureIds, ImmutableMap<Integer, Boolean> explicitPreFlips, CustomUniforms customUniforms) {
this.noiseTexture = noiseTexture;
this.updateNotifier = updateNotifier;
this.centerDepthSampler = centerDepthSampler;
this.renderTargets = renderTargets;
this.customTextureIds = customTextureIds;
this.customUniforms = customUniforms;
Object2ObjectMap<String, IntSupplier> 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), explicitPreFlips, precomputedTransformFutures, "unknown");
}
public CompositeRenderer(ProgramSource[] sources, ComputeSource[][] computes, BufferFlipper bufferFlipper, ProgramBuildContext context, ImmutableMap<Integer, Boolean> explicitPreFlips, Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> precomputedTransformFutures, String stageName) {
this.noiseTexture = context.noiseTexture();
this.updateNotifier = context.updateNotifier();
this.centerDepthSampler = context.centerDepthSampler();
this.renderTargets = context.renderTargets();
this.customTextureIds = context.customTextureIds();
this.customUniforms = context.customUniforms();
final ImmutableList.Builder<Pass> passes = ImmutableList.builder();
final ImmutableSet.Builder<Integer> flippedAtLeastOnce = new ImmutableSet.Builder<>();
Map<Integer, CompletableFuture<Map<PatchShaderType, String>>> transformFutures = precomputedTransformFutures;
explicitPreFlips.forEach((buffer, shouldFlip) -> {
if (shouldFlip) {
bufferFlipper.flip(buffer);
@@ -84,28 +94,29 @@ public class CompositeRenderer {
if (source == null || !source.isValid()) {
if (computes[i] != null) {
ComputeOnlyPass pass = new ComputeOnlyPass();
pass.computes = createComputes(computes[i], flipped, flippedAtLeastOnceSnapshot, shadowTargetsSupplier);
final ComputeOnlyPass pass = new ComputeOnlyPass();
pass.computes = createComputes(computes[i], flipped, flippedAtLeastOnceSnapshot, context.shadowTargetsSupplier());
passes.add(pass);
}
continue;
}
Pass pass = new Pass();
ProgramDirectives directives = source.getDirectives();
final Pass pass = new Pass();
final ProgramDirectives directives = source.getDirectives();
pass.program = createProgram(source, flipped, flippedAtLeastOnceSnapshot, shadowTargetsSupplier);
pass.computes = createComputes(computes[i], flipped, flippedAtLeastOnceSnapshot, shadowTargetsSupplier);
int[] drawBuffers = directives.getDrawBuffers();
Map<PatchShaderType, String> transformed = getTransformed(source, transformFutures, i, stageName);
pass.program = createProgramFromTransformed(source, transformed, flipped, flippedAtLeastOnceSnapshot, context.shadowTargetsSupplier());
pass.computes = createComputes(computes[i], flipped, flippedAtLeastOnceSnapshot, context.shadowTargetsSupplier());
final int[] drawBuffers = directives.getDrawBuffers();
GlFramebuffer framebuffer = renderTargets.createColorFramebuffer(flipped, drawBuffers);
final GlFramebuffer framebuffer = renderTargets.createColorFramebuffer(flipped, drawBuffers);
int passWidth = 0, passHeight = 0;
// Flip the buffers that this shader wrote to, and set pass width and height
ImmutableMap<Integer, Boolean> explicitFlips = directives.getExplicitFlips();
final ImmutableMap<Integer, Boolean> explicitFlips = directives.getExplicitFlips();
for (int buffer : drawBuffers) {
RenderTarget target = renderTargets.get(buffer);
final RenderTarget target = renderTargets.get(buffer);
if ((passWidth > 0 && passWidth != target.getWidth()) || (passHeight > 0 && passHeight != target.getHeight())) {
throw new IllegalStateException("Pass widths must match");
}
@@ -145,6 +156,25 @@ 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) {
if (transformFutures != null) {
final CompletableFuture<Map<PatchShaderType, String>> future = transformFutures.get(index);
if (future != null) {
try {
final Map<PatchShaderType, String> result = future.join();
if (result != null) {
return result;
}
} catch (CompletionException e) {
throw new RuntimeException("Shader transformation failed for '" + source.getName() + "' in stage '" + stageName + "' (pass " + index + ")", e.getCause() != null ? e.getCause() : e);
}
}
}
// Fallback: transform synchronously
return TransformPatcher.patchComposite(source.getVertexSource().orElseThrow(NullPointerException::new), source.getGeometrySource().orElse(null), source.getFragmentSource().orElseThrow(NullPointerException::new));
}
public void recalculateSizes() {
for (Pass pass : passes) {
if (pass instanceof ComputeOnlyPass) {
@@ -291,13 +321,9 @@ public class CompositeRenderer {
}
// TODO: Don't just copy this from DeferredWorldRenderingPipeline
private Program createProgram(ProgramSource source, ImmutableSet<Integer> flipped, ImmutableSet<Integer> flippedAtLeastOnceSnapshot,
Supplier<ShadowRenderTargets> shadowTargetsSupplier) {
// TODO: Properly handle empty shaders
Map<PatchShaderType, String> transformed = TransformPatcher.patchComposite(
source.getVertexSource().orElseThrow(NullPointerException::new),
source.getGeometrySource().orElse(null),
source.getFragmentSource().orElseThrow(NullPointerException::new));
private Program createProgramFromTransformed(ProgramSource source, Map<PatchShaderType, String> transformed,
ImmutableSet<Integer> flipped, ImmutableSet<Integer> flippedAtLeastOnceSnapshot,
Supplier<ShadowRenderTargets> shadowTargetsSupplier) {
String vertex = transformed.get(PatchShaderType.VERTEX);
String geometry = transformed.get(PatchShaderType.GEOMETRY);
String fragment = transformed.get(PatchShaderType.FRAGMENT);
@@ -40,6 +40,8 @@ import org.lwjgl.opengl.GL30;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
@@ -59,33 +61,38 @@ public class FinalPassRenderer {
private final Object2ObjectMap<String, IntSupplier> customTextureIds;
private final CustomUniforms customUniforms;
// TODO: The length of this argument list is getting a bit ridiculous
public FinalPassRenderer(ProgramSet pack, RenderTargets renderTargets, IntSupplier noiseTexture,
FrameUpdateNotifier updateNotifier, ImmutableSet<Integer> flippedBuffers,
CenterDepthSampler centerDepthSampler,
Supplier<ShadowRenderTargets> shadowTargetsSupplier,
Object2ObjectMap<String, IntSupplier> customTextureIds,
ImmutableSet<Integer> flippedAtLeastOnce, CustomUniforms customUniforms) {
this.updateNotifier = updateNotifier;
this.centerDepthSampler = centerDepthSampler;
this.customTextureIds = customTextureIds;
ImmutableSet<Integer> flippedAtLeastOnce, CustomUniforms customUniforms,
CompletableFuture<Map<PatchShaderType, String>> precomputedTransformFuture) {
this(pack, new ProgramBuildContext(renderTargets, noiseTexture, updateNotifier, centerDepthSampler, shadowTargetsSupplier, customTextureIds, customUniforms), flippedBuffers, flippedAtLeastOnce, precomputedTransformFuture, "final");
}
this.noiseTexture = noiseTexture;
this.renderTargets = renderTargets;
this.customUniforms = customUniforms;
this.finalPass = pack.getCompositeFinal().map(source -> {
Pass pass = new Pass();
ProgramDirectives directives = source.getDirectives();
public FinalPassRenderer(ProgramSet pack, ProgramBuildContext context, ImmutableSet<Integer> flippedBuffers, ImmutableSet<Integer> flippedAtLeastOnce, CompletableFuture<Map<PatchShaderType, String>> precomputedTransformFuture, String stageName) {
this.updateNotifier = context.updateNotifier();
this.centerDepthSampler = context.centerDepthSampler();
this.customTextureIds = context.customTextureIds();
pass.program = createProgram(source, flippedBuffers, flippedAtLeastOnce, shadowTargetsSupplier);
pass.computes = createComputes(pack.getFinalCompute(), flippedBuffers, flippedAtLeastOnce, shadowTargetsSupplier);
this.noiseTexture = context.noiseTexture();
this.renderTargets = context.renderTargets();
this.customUniforms = context.customUniforms();
this.finalPass = pack.getCompositeFinal().filter(ProgramSource::isValid).map(source -> {
final Pass pass = new Pass();
final ProgramDirectives directives = source.getDirectives();
final Map<PatchShaderType, String> transformed = getTransformed(source, precomputedTransformFuture, stageName);
pass.program = createProgramFromTransformed(source, transformed, flippedBuffers, flippedAtLeastOnce, context.shadowTargetsSupplier());
pass.computes = createComputes(pack.getFinalCompute(), flippedBuffers, flippedAtLeastOnce, context.shadowTargetsSupplier());
pass.stageReadsFromAlt = flippedBuffers;
pass.mipmappedBuffers = directives.getMipmappedBuffers();
return pass;
}).orElse(null);
IntList buffersToBeCleared = pack.getPackDirectives().getRenderTargetDirectives().getBuffersToBeCleared();
final IntList buffersToBeCleared = pack.getPackDirectives().getRenderTargetDirectives().getBuffersToBeCleared();
// The name of this method might seem a bit odd here, but we want a framebuffer with color attachments that line
// up with whatever was written last (since we're reading from these framebuffers) instead of trying to create
@@ -128,6 +135,20 @@ public class FinalPassRenderer {
OpenGlHelper.func_153171_g/*glBindFramebuffer*/(GL30.GL_READ_FRAMEBUFFER, 0);
}
private static Map<PatchShaderType, String> getTransformed(ProgramSource source, CompletableFuture<Map<PatchShaderType, String>> precomputedTransformFuture, String stageName) {
if (precomputedTransformFuture != null) {
try {
final Map<PatchShaderType, String> result = precomputedTransformFuture.join();
if (result != null) {
return result;
}
} catch (CompletionException e) {
throw new RuntimeException("Shader transformation failed for '" + source.getName() + "' in stage '" + stageName + "'", e.getCause() != null ? e.getCause() : e);
}
}
return TransformPatcher.patchComposite(source.getVertexSource().orElseThrow(NullPointerException::new), source.getGeometrySource().orElse(null), source.getFragmentSource().orElseThrow(NullPointerException::new));
}
private static final class Pass {
Program program;
ComputeProgram[] computes;
@@ -314,13 +335,9 @@ public class FinalPassRenderer {
}
// TODO: Don't just copy this from DeferredWorldRenderingPipeline
private Program createProgram(ProgramSource source, ImmutableSet<Integer> flipped, ImmutableSet<Integer> flippedAtLeastOnceSnapshot,
Supplier<ShadowRenderTargets> shadowTargetsSupplier) {
// TODO: Properly handle empty shaders
Map<PatchShaderType, String> transformed = TransformPatcher.patchComposite(
source.getVertexSource().orElseThrow(NullPointerException::new),
source.getGeometrySource().orElse(null),
source.getFragmentSource().orElseThrow(NullPointerException::new));
private Program createProgramFromTransformed(ProgramSource source, Map<PatchShaderType, String> transformed,
ImmutableSet<Integer> flipped, ImmutableSet<Integer> flippedAtLeastOnceSnapshot,
Supplier<ShadowRenderTargets> shadowTargetsSupplier) {
final String vertex = transformed.get(PatchShaderType.VERTEX);
final String geometry = transformed.get(PatchShaderType.GEOMETRY);
final String fragment = transformed.get(PatchShaderType.FRAGMENT);
@@ -0,0 +1,23 @@
package net.coderbot.iris.postprocess;
import com.github.bsideup.jabel.Desugar;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.coderbot.iris.rendertarget.RenderTargets;
import net.coderbot.iris.shadows.ShadowRenderTargets;
import net.coderbot.iris.uniforms.FrameUpdateNotifier;
import net.coderbot.iris.uniforms.custom.CustomUniforms;
import org.jetbrains.annotations.NotNull;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
@Desugar
public record ProgramBuildContext(
@NotNull RenderTargets renderTargets,
@NotNull IntSupplier noiseTexture,
@NotNull FrameUpdateNotifier updateNotifier,
@NotNull CenterDepthSampler centerDepthSampler,
@NotNull Supplier<ShadowRenderTargets> shadowTargetsSupplier,
@NotNull Object2ObjectMap<String, IntSupplier> customTextureIds,
@NotNull CustomUniforms customUniforms
) {}