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);