From 35768b5ade501a6e17c45e7c4f6f182b8085e215 Mon Sep 17 00:00:00 2001 From: JozsefA Date: Sun, 2 May 2021 14:10:38 -0700 Subject: [PATCH] Step towards arbitrary shader contexts --- .../flywheel/backend/ShaderLoader.java | 8 +++ .../foundation/render/AllProgramSpecs.java | 34 ++++++------ .../assets/create/flywheel/shaders/belt.vert | 4 +- .../shaders/{model.frag => block.frag} | 17 +++--- .../create/flywheel/shaders/contraption.frag | 54 ------------------- .../flywheel/shaders/contraption/builtin.frag | 7 +++ .../{finalize.glsl => builtin.vert} | 0 .../assets/create/flywheel/shaders/flap.vert | 4 +- .../assets/create/flywheel/shaders/model.vert | 4 +- .../create/flywheel/shaders/oriented.vert | 4 +- .../create/flywheel/shaders/rotating.vert | 4 +- .../create/flywheel/shaders/std/builtin.frag | 4 ++ .../std/{finalize.glsl => builtin.vert} | 0 13 files changed, 56 insertions(+), 88 deletions(-) rename src/main/resources/assets/create/flywheel/shaders/{model.frag => block.frag} (74%) delete mode 100644 src/main/resources/assets/create/flywheel/shaders/contraption.frag create mode 100644 src/main/resources/assets/create/flywheel/shaders/contraption/builtin.frag rename src/main/resources/assets/create/flywheel/shaders/contraption/{finalize.glsl => builtin.vert} (100%) create mode 100644 src/main/resources/assets/create/flywheel/shaders/std/builtin.frag rename src/main/resources/assets/create/flywheel/shaders/std/{finalize.glsl => builtin.vert} (100%) diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderLoader.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderLoader.java index abd224f1a..de256a9a4 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderLoader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderLoader.java @@ -46,6 +46,7 @@ public class ShaderLoader { // #flwinclude <"valid_namespace:valid/path_to_file.glsl"> private static final Pattern includePattern = Pattern.compile("#flwinclude <\"([\\w\\d_]+:[\\w\\d_./]+)\">"); + private static boolean debugDumpFile = false; final Map shaderSource = new HashMap<>(); @@ -162,6 +163,13 @@ public class ShaderLoader { if (defines != null) source = defines.process(source); + if (debugDumpFile) { + Backend.log.debug("Finished processing '" + name + "':"); + int i = 1; + for (String s : source.split("\n")) { + Backend.log.debug(String.format("%1$4s: ", i++) + s); + } + } return new GlShader(type, name, source); } diff --git a/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java b/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java index 54836972d..72fc21f90 100644 --- a/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java +++ b/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java @@ -41,7 +41,7 @@ public class AllProgramSpecs { .addAttributes(BasicAttributes.class) .addAttributes(TransformAttributes.class) .setVert(Locations.MODEL_VERT) - .setFrag(Locations.MODEL_FRAG) + .setFrag(Locations.BLOCK) .createProgramSpec()); public static final ProgramSpec ORIENTED = register(ProgramSpec.builder("oriented", new FogSensitiveProgram.SpecLoader<>(BasicProgram::new)) @@ -49,7 +49,7 @@ public class AllProgramSpecs { .addAttributes(BasicAttributes.class) .addAttributes(OrientedAttributes.class) .setVert(Locations.ORIENTED) - .setFrag(Locations.MODEL_FRAG) + .setFrag(Locations.BLOCK) .createProgramSpec()); public static final ProgramSpec ROTATING = register(ProgramSpec.builder("rotating", new FogSensitiveProgram.SpecLoader<>(BasicProgram::new)) @@ -58,7 +58,7 @@ public class AllProgramSpecs { .addAttributes(KineticAttributes.class) .addAttributes(RotatingAttributes.class) .setVert(Locations.ROTATING) - .setFrag(Locations.MODEL_FRAG) + .setFrag(Locations.BLOCK) .createProgramSpec()); public static final ProgramSpec BELT = register(ProgramSpec.builder("belt", new FogSensitiveProgram.SpecLoader<>(BasicProgram::new)) @@ -67,26 +67,27 @@ public class AllProgramSpecs { .addAttributes(KineticAttributes.class) .addAttributes(BeltAttributes.class) .setVert(Locations.BELT) - .setFrag(Locations.MODEL_FRAG) + .setFrag(Locations.BLOCK) .createProgramSpec()); public static final ProgramSpec FLAPS = register(ProgramSpec.builder("flap", new FogSensitiveProgram.SpecLoader<>(BasicProgram::new)) .addAttributes(ModelAttributes.class) .addAttributes(FlapAttributes.class) .setVert(Locations.FLAP) - .setFrag(Locations.MODEL_FRAG) + .setFrag(Locations.BLOCK) .createProgramSpec()); public static final ProgramSpec C_STRUCTURE = register(ProgramSpec.builder("contraption_structure", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) .addAttributes(ContraptionAttributes.class) .setVert(Locations.CONTRAPTION_STRUCTURE) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) + .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static final ProgramSpec C_MODEL = register(ProgramSpec.builder("contraption_model", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) .addAttributes(ModelAttributes.class) .addAttributes(BasicAttributes.class) .addAttributes(TransformAttributes.class) .setVert(Locations.MODEL_VERT) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static final ProgramSpec C_ORIENTED = register(ProgramSpec.builder("contraption_oriented", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) @@ -94,7 +95,7 @@ public class AllProgramSpecs { .addAttributes(BasicAttributes.class) .addAttributes(OrientedAttributes.class) .setVert(Locations.ORIENTED) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static final ProgramSpec C_ROTATING = register(ProgramSpec.builder("contraption_rotating", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) @@ -103,7 +104,7 @@ public class AllProgramSpecs { .addAttributes(KineticAttributes.class) .addAttributes(RotatingAttributes.class) .setVert(Locations.ROTATING) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static final ProgramSpec C_BELT = register(ProgramSpec.builder("contraption_belt", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) @@ -112,31 +113,30 @@ public class AllProgramSpecs { .addAttributes(KineticAttributes.class) .addAttributes(BeltAttributes.class) .setVert(Locations.BELT) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static final ProgramSpec C_FLAPS = register(ProgramSpec.builder("contraption_flap", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) .addAttributes(ModelAttributes.class) .addAttributes(FlapAttributes.class) .setVert(Locations.FLAP) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static final ProgramSpec C_ACTOR = register(ProgramSpec.builder("contraption_actor", new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)) .addAttributes(ModelAttributes.class) .addAttributes(ActorVertexAttributes.class) .setVert(Locations.CONTRAPTION_ACTOR) - .setFrag(Locations.CONTRAPTION) + .setFrag(Locations.BLOCK) + .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); public static class Locations { - public static final ResourceLocation EFFECT_VERT = loc("area_effect.vert"); - public static final ResourceLocation EFFECT_FRAG = loc("area_effect.frag"); - public static final ResourceLocation MODEL_FRAG = loc("model.frag"); + public static final ResourceLocation BLOCK = loc("block.frag"); + public static final ResourceLocation MODEL_VERT = loc("model.vert"); public static final ResourceLocation ORIENTED = loc("oriented.vert"); - public static final ResourceLocation CONTRAPTION = loc("contraption.frag"); public static final ResourceLocation ROTATING = loc("rotating.vert"); public static final ResourceLocation BELT = loc("belt.vert"); @@ -144,6 +144,8 @@ public class AllProgramSpecs { public static final ResourceLocation CONTRAPTION_STRUCTURE = loc("contraption_structure.vert"); public static final ResourceLocation CONTRAPTION_ACTOR = loc("contraption_actor.vert"); + public static final ResourceLocation EFFECT_VERT = loc("area_effect.vert"); + public static final ResourceLocation EFFECT_FRAG = loc("area_effect.frag"); private static ResourceLocation loc(String name) { return new ResourceLocation(Create.ID, name); diff --git a/src/main/resources/assets/create/flywheel/shaders/belt.vert b/src/main/resources/assets/create/flywheel/shaders/belt.vert index b0f066140..6edba4c07 100644 --- a/src/main/resources/assets/create/flywheel/shaders/belt.vert +++ b/src/main/resources/assets/create/flywheel/shaders/belt.vert @@ -35,9 +35,9 @@ varying float FragDistance; #endif #ifdef CONTRAPTION -#flwinclude <"create:contraption/finalize.glsl"> +#flwinclude <"create:contraption/builtin.vert"> #else -#flwinclude <"create:std/finalize.glsl"> +#flwinclude <"create:std/builtin.vert"> #endif void main() { diff --git a/src/main/resources/assets/create/flywheel/shaders/model.frag b/src/main/resources/assets/create/flywheel/shaders/block.frag similarity index 74% rename from src/main/resources/assets/create/flywheel/shaders/model.frag rename to src/main/resources/assets/create/flywheel/shaders/block.frag index e8ffaa00c..0ad89defe 100644 --- a/src/main/resources/assets/create/flywheel/shaders/model.frag +++ b/src/main/resources/assets/create/flywheel/shaders/block.frag @@ -28,19 +28,20 @@ float fogFactor() { float dist = FragDistance * uFogDensity; return 1. / exp2(dist * dist); } -#endif + #endif -vec4 light() { - vec2 lm = Light * 0.9375 + 0.03125; - return texture2D(uLightMap, lm); -} + #ifdef CONTRAPTION + #flwinclude <"create:contraption/builtin.frag"> + #else + #flwinclude <"create:std/builtin.frag"> + #endif void main() { vec4 tex = texture2D(uBlockAtlas, TexCoords); - vec4 color = vec4(tex.rgb * light().rgb * Diffuse, tex.a) * Color; + vec4 color = vec4(tex.rgb * FLWLight(Light, uLightMap).rgb * Diffuse, tex.a) * Color; -#if defined(USE_FOG) + #if defined(USE_FOG) float fog = clamp(fogFactor(), 0., 1.); gl_FragColor = mix(uFogColor, color, fog); @@ -48,4 +49,4 @@ void main() { #else gl_FragColor = color; #endif -} \ No newline at end of file +} diff --git a/src/main/resources/assets/create/flywheel/shaders/contraption.frag b/src/main/resources/assets/create/flywheel/shaders/contraption.frag deleted file mode 100644 index 9049221f1..000000000 --- a/src/main/resources/assets/create/flywheel/shaders/contraption.frag +++ /dev/null @@ -1,54 +0,0 @@ -#version 110 - -varying vec2 TexCoords; -varying vec4 Color; -varying float Diffuse; -varying vec2 Light; - -varying vec3 BoxCoord; - -uniform sampler2D uBlockAtlas; -uniform sampler2D uLightMap; -uniform sampler3D uLightVolume; - -#if defined(USE_FOG) -varying float FragDistance; -uniform vec4 uFogColor; -#endif - -#if defined(USE_FOG_LINEAR) -uniform vec2 uFogRange; - -float fogFactor() { - return (uFogRange.y - FragDistance) / (uFogRange.y - uFogRange.x); -} -#endif - -#ifdef USE_FOG_EXP2 -uniform float uFogDensity; - -float fogFactor() { - float dist = FragDistance * uFogDensity; - return 1. / exp2(dist * dist); -} -#endif - -vec4 light() { - vec2 lm = texture3D(uLightVolume, BoxCoord).rg * 0.9375 + 0.03125; - return texture2D(uLightMap, max(lm, Light)); -} - -void main() { - vec4 tex = texture2D(uBlockAtlas, TexCoords); - - vec4 color = vec4(tex.rgb * light().rgb * Diffuse * Color.rgb, tex.a); - -#if defined(USE_FOG) - float fog = clamp(fogFactor(), 0., 1.); - - gl_FragColor = mix(uFogColor, color, fog); - gl_FragColor.a = color.a; -#else - gl_FragColor = color; -#endif -} \ No newline at end of file diff --git a/src/main/resources/assets/create/flywheel/shaders/contraption/builtin.frag b/src/main/resources/assets/create/flywheel/shaders/contraption/builtin.frag new file mode 100644 index 000000000..eb9014677 --- /dev/null +++ b/src/main/resources/assets/create/flywheel/shaders/contraption/builtin.frag @@ -0,0 +1,7 @@ +varying vec3 BoxCoord; +uniform sampler3D uLightVolume; + +vec4 FLWLight(vec2 lightCoords, sampler2D lightMap) { + vec2 lm = max(lightCoords, texture3D(uLightVolume, BoxCoord).rg); + return texture2D(lightMap, lm * 0.9375 + 0.03125); +} diff --git a/src/main/resources/assets/create/flywheel/shaders/contraption/finalize.glsl b/src/main/resources/assets/create/flywheel/shaders/contraption/builtin.vert similarity index 100% rename from src/main/resources/assets/create/flywheel/shaders/contraption/finalize.glsl rename to src/main/resources/assets/create/flywheel/shaders/contraption/builtin.vert diff --git a/src/main/resources/assets/create/flywheel/shaders/flap.vert b/src/main/resources/assets/create/flywheel/shaders/flap.vert index bd877cc48..037c603c2 100644 --- a/src/main/resources/assets/create/flywheel/shaders/flap.vert +++ b/src/main/resources/assets/create/flywheel/shaders/flap.vert @@ -37,9 +37,9 @@ varying float FragDistance; #endif #ifdef CONTRAPTION -#flwinclude <"create:contraption/finalize.glsl"> +#flwinclude <"create:contraption/builtin.vert"> #else -#flwinclude <"create:std/finalize.glsl"> +#flwinclude <"create:std/builtin.vert"> #endif float toRad(float degrees) { diff --git a/src/main/resources/assets/create/flywheel/shaders/model.vert b/src/main/resources/assets/create/flywheel/shaders/model.vert index c6a146695..e0a62efe7 100644 --- a/src/main/resources/assets/create/flywheel/shaders/model.vert +++ b/src/main/resources/assets/create/flywheel/shaders/model.vert @@ -28,9 +28,9 @@ varying float FragDistance; #endif #ifdef CONTRAPTION -#flwinclude <"create:contraption/finalize.glsl"> +#flwinclude <"create:contraption/builtin.vert"> #else -#flwinclude <"create:std/finalize.glsl"> +#flwinclude <"create:std/builtin.vert"> #endif diff --git a/src/main/resources/assets/create/flywheel/shaders/oriented.vert b/src/main/resources/assets/create/flywheel/shaders/oriented.vert index 79d76cfe9..e88371820 100644 --- a/src/main/resources/assets/create/flywheel/shaders/oriented.vert +++ b/src/main/resources/assets/create/flywheel/shaders/oriented.vert @@ -30,9 +30,9 @@ varying float FragDistance; #endif #ifdef CONTRAPTION -#flwinclude <"create:contraption/finalize.glsl"> +#flwinclude <"create:contraption/builtin.vert"> #else -#flwinclude <"create:std/finalize.glsl"> +#flwinclude <"create:std/builtin.vert"> #endif void main() { diff --git a/src/main/resources/assets/create/flywheel/shaders/rotating.vert b/src/main/resources/assets/create/flywheel/shaders/rotating.vert index e428b7049..d2eaeb63b 100644 --- a/src/main/resources/assets/create/flywheel/shaders/rotating.vert +++ b/src/main/resources/assets/create/flywheel/shaders/rotating.vert @@ -32,9 +32,9 @@ varying float FragDistance; #endif #ifdef CONTRAPTION -#flwinclude <"create:contraption/finalize.glsl"> +#flwinclude <"create:contraption/builtin.vert"> #else -#flwinclude <"create:std/finalize.glsl"> +#flwinclude <"create:std/builtin.vert"> #endif mat4 kineticRotation() { diff --git a/src/main/resources/assets/create/flywheel/shaders/std/builtin.frag b/src/main/resources/assets/create/flywheel/shaders/std/builtin.frag new file mode 100644 index 000000000..d133154cf --- /dev/null +++ b/src/main/resources/assets/create/flywheel/shaders/std/builtin.frag @@ -0,0 +1,4 @@ +vec4 FLWLight(vec2 lightCoords, sampler2D lightMap) { + vec2 lm = lightCoords * 0.9375 + 0.03125; + return texture2D(lightMap, lm); +} diff --git a/src/main/resources/assets/create/flywheel/shaders/std/finalize.glsl b/src/main/resources/assets/create/flywheel/shaders/std/builtin.vert similarity index 100% rename from src/main/resources/assets/create/flywheel/shaders/std/finalize.glsl rename to src/main/resources/assets/create/flywheel/shaders/std/builtin.vert