Skip to content

Commit

Permalink
Add some supporting code for using the new sampler system with Sodium
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbot16 committed Jun 27, 2021
1 parent 49d2403 commit 5e5a4f2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;

import com.google.common.collect.ImmutableSet;
Expand All @@ -13,6 +14,7 @@
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.gl.program.Program;
import net.coderbot.iris.gl.program.ProgramBuilder;
import net.coderbot.iris.gl.program.ProgramSamplers;
import net.coderbot.iris.layer.GbufferProgram;
import net.coderbot.iris.mixin.WorldRendererAccessor;
import net.coderbot.iris.postprocess.BufferFlipper;
Expand Down Expand Up @@ -159,10 +161,12 @@ public DeferredWorldRenderingPipeline(ProgramSet programs) {

GlStateManager.activeTexture(GL20C.GL_TEXTURE0);

// TODO: Change this once earlier passes are implemented.
ImmutableSet<Integer> flippedBeforeTerrain = ImmutableSet.of();

createShadowMapRenderer = () -> {
// TODO: The flipped set will need to be changeable if we implement the "prepare" passes.
shadowMapRenderer = new ShadowRenderer(this, programs.getShadow().orElse(null),
programs.getPackDirectives(), () -> ImmutableSet.of(), renderTargets, normals, specular, noise);
programs.getPackDirectives(), () -> flippedBeforeTerrain, renderTargets, normals, specular, noise);
createShadowMapRenderer = () -> {};
};

Expand All @@ -187,6 +191,41 @@ public DeferredWorldRenderingPipeline(ProgramSet programs) {
this.finalPassRenderer = new FinalPassRenderer(programs, renderTargets, noise, updateNotifier, flipper.snapshot(),
centerDepthSampler, shadowMapRendererSupplier);

Supplier<ImmutableSet<Integer>> flipped =
() -> isBeforeTranslucent ? flippedBeforeTranslucent : flippedAfterTranslucent;

IntFunction<ProgramSamplers> createTerrainSamplers = (programId) -> {
ProgramSamplers.Builder builder = ProgramSamplers.builder(programId, IrisSamplers.WORLD_RESERVED_TEXTURE_UNITS);

IrisSamplers.addRenderTargetSamplers(builder, flipped, renderTargets, false);
IrisSamplers.addWorldSamplers(builder, normals, specular);
IrisSamplers.addWorldDepthSamplers(builder, renderTargets);
IrisSamplers.addNoiseSampler(builder, noise);

if (IrisSamplers.hasShadowSamplers(builder)) {
createShadowMapRenderer.run();
IrisSamplers.addShadowSamplers(builder, shadowMapRenderer);
}

return builder.build();
};

IntFunction<ProgramSamplers> createShadowTerrainSamplers = (programId) -> {
ProgramSamplers.Builder builder = ProgramSamplers.builder(programId, IrisSamplers.WORLD_RESERVED_TEXTURE_UNITS);

IrisSamplers.addRenderTargetSamplers(builder, () -> flippedBeforeTerrain, renderTargets, false);
IrisSamplers.addWorldSamplers(builder, normals, specular);
IrisSamplers.addNoiseSampler(builder, noise);

// Only initialize these samplers if the shadow map renderer exists.
// Otherwise, this program shouldn't be used at all?
if (IrisSamplers.hasShadowSamplers(builder) && shadowMapRenderer != null) {
IrisSamplers.addShadowSamplers(builder, shadowMapRenderer);
}

return builder.build();
};

this.basic = programs.getGbuffersBasic().map(this::createPass).orElse(null);
this.textured = programs.getGbuffersTextured().map(this::createPass).orElse(basic);
this.texturedLit = programs.getGbuffersTexturedLit().map(this::createPass).orElse(textured);
Expand Down Expand Up @@ -216,7 +255,7 @@ public DeferredWorldRenderingPipeline(ProgramSet programs) {
this.shadowMapRenderer = new EmptyShadowMapRenderer(programs.getPackDirectives().getShadowDirectives().getResolution());
}

this.sodiumTerrainPipeline = new SodiumTerrainPipeline(programs);
this.sodiumTerrainPipeline = new SodiumTerrainPipeline(programs, createTerrainSamplers, createShadowTerrainSamplers);
}

private void checkWorld() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package net.coderbot.iris.pipeline;

import java.util.Objects;
import java.util.Optional;
import java.util.function.IntFunction;

import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.program.ProgramBuilder;
import net.coderbot.iris.gl.program.ProgramSamplers;
import net.coderbot.iris.gl.program.ProgramUniforms;
import net.coderbot.iris.shaderpack.ProgramSet;
import net.coderbot.iris.shaderpack.ProgramSource;
import net.coderbot.iris.shaderpack.transform.BuiltinUniformReplacementTransformer;
import net.coderbot.iris.shaderpack.transform.StringTransformations;
import net.coderbot.iris.shaderpack.transform.Transformations;
import net.coderbot.iris.uniforms.CommonUniforms;
import net.coderbot.iris.uniforms.FrameUpdateNotifier;
import net.coderbot.iris.uniforms.SamplerUniforms;
import net.coderbot.iris.uniforms.builtin.BuiltinReplacementUniforms;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -27,7 +27,11 @@ public class SodiumTerrainPipeline {
//GlFramebuffer framebuffer;
ProgramSet programSet;

public SodiumTerrainPipeline(ProgramSet programSet) {
private final IntFunction<ProgramSamplers> createTerrainSamplers;
private final IntFunction<ProgramSamplers> createShadowSamplers;

public SodiumTerrainPipeline(ProgramSet programSet, IntFunction<ProgramSamplers> createTerrainSamplers,
IntFunction<ProgramSamplers> createShadowSamplers) {
Optional<ProgramSource> terrainSource = first(programSet.getGbuffersTerrain(), programSet.getGbuffersTexturedLit(), programSet.getGbuffersTextured(), programSet.getGbuffersBasic());
Optional<ProgramSource> translucentSource = first(programSet.getGbuffersWater(), terrainSource);
Optional<ProgramSource> shadowSource = programSet.getShadow();
Expand Down Expand Up @@ -72,6 +76,9 @@ public SodiumTerrainPipeline(ProgramSet programSet) {
if (shadowFragment != null) {
shadowFragment = transformFragmentShader(shadowFragment);
}

this.createTerrainSamplers = createTerrainSamplers;
this.createShadowSamplers = createShadowSamplers;
}

private static String transformVertexShader(String base) {
Expand Down Expand Up @@ -189,6 +196,14 @@ public ProgramUniforms initUniforms(int programId) {
return uniforms.buildUniforms();
}

public ProgramSamplers initTerrainSamplers(int programId) {
return createTerrainSamplers.apply(programId);
}

public ProgramSamplers initShadowSamplers(int programId) {
return createShadowSamplers.apply(programId);
}

/*public void bindFramebuffer() {
this.framebuffer.bind();
}
Expand Down

0 comments on commit 5e5a4f2

Please sign in to comment.