diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java index 529051f11..897d132bc 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java @@ -4,10 +4,10 @@ import java.util.List; import org.lwjgl.opengl.GL20; -import com.jozufozu.flywheel.backend.loading.Program; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.core.shader.extension.IProgramExtension; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.vector.Matrix4f; @@ -18,8 +18,8 @@ public class ContraptionProgram extends WorldProgram { protected int uLightVolume; - public ContraptionProgram(Program program, List extensions) { - super(program, extensions); + public ContraptionProgram(ResourceLocation name, int handle, List extensions) { + super(name, handle, extensions); uLightBoxSize = getUniformLocation("uLightBoxSize"); uLightBoxMin = getUniformLocation("uLightBoxMin"); uModel = getUniformLocation("uModel"); diff --git a/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java b/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java index 60ab31a97..c17329ee9 100644 --- a/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java +++ b/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java @@ -2,10 +2,17 @@ package com.simibubi.create.foundation.render; import java.util.stream.Stream; +import org.lwjgl.opengl.GL46; + import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.FileResolution; +import com.jozufozu.flywheel.backend.ResourceUtil; import com.jozufozu.flywheel.backend.SpecMetaRegistry; -import com.jozufozu.flywheel.backend.gl.shader.ShaderType; -import com.jozufozu.flywheel.backend.loading.ModelTemplate; +import com.jozufozu.flywheel.backend.pipeline.IShaderPipeline; +import com.jozufozu.flywheel.backend.pipeline.ITemplate; +import com.jozufozu.flywheel.backend.pipeline.InstancingTemplate; +import com.jozufozu.flywheel.backend.pipeline.OneShotTemplate; +import com.jozufozu.flywheel.backend.pipeline.WorldShaderPipeline; import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.event.GatherContextEvent; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionProgram; @@ -26,16 +33,18 @@ public class CreateContexts { SpecMetaRegistry.register(RainbowDebugStateProvider.INSTANCE); - CWORLD = backend.register(contraptionContext(backend)); - STRUCTURE = backend.register(contraptionContext(backend) - .withSpecStream(() -> Stream.of(AllProgramSpecs.STRUCTURE)) - .withTemplateFactory(ModelTemplate::new)); + CWORLD = backend.register(contraptionContext(backend, InstancingTemplate.INSTANCE)); + STRUCTURE = backend.register(contraptionContext(backend, OneShotTemplate.INSTANCE) + .withSpecStream(() -> Stream.of(AllProgramSpecs.STRUCTURE))); } - private static WorldContext contraptionContext(Backend backend) { - return new WorldContext<>(backend, ContraptionProgram::new) - .withName(CONTRAPTION) - .withBuiltin(ShaderType.FRAGMENT, CONTRAPTION, "/builtin.frag") - .withBuiltin(ShaderType.VERTEX, CONTRAPTION, "/builtin.vert"); + private static WorldContext contraptionContext(Backend backend, ITemplate template) { + + FileResolution header = backend.sources.resolveFile(ResourceUtil.subPath(CONTRAPTION, ".glsl")); + + IShaderPipeline worldPipeline = new WorldShaderPipeline<>(backend.sources, ContraptionProgram::new, template, header); + + return new WorldContext<>(backend, worldPipeline) + .withName(CONTRAPTION); } } diff --git a/src/main/resources/assets/create/flywheel/shaders/belt.vert b/src/main/resources/assets/create/flywheel/shaders/belt.vert index be5d51a43..d953208f7 100644 --- a/src/main/resources/assets/create/flywheel/shaders/belt.vert +++ b/src/main/resources/assets/create/flywheel/shaders/belt.vert @@ -1,11 +1,9 @@ #define PI 3.1415926538 -#flwbuiltins -#flwinclude <"flywheel:core/quaternion.glsl"> -#flwinclude <"flywheel:core/matutils.glsl"> -#flwinclude <"flywheel:core/diffuse.glsl"> +#use "flywheel:core/quaternion.glsl" +#use "flywheel:core/matutils.glsl" +#use "flywheel:core/diffuse.glsl" -#[InstanceData] struct Belt { vec2 light; vec4 color; @@ -18,10 +16,11 @@ struct Belt { float scrollMult; }; -#flwinclude <"flywheel:data/modelvertex.glsl"> -#flwinclude <"flywheel:data/blockfragment.glsl"> +#use "flywheel:data/modelvertex.glsl" +#use "flywheel:block.frag" -BlockFrag FLWMain(Vertex v, Belt instance) { +#if defined(VERTEX_SHADER) +BlockFrag vertex(Vertex v, Belt instance) { vec3 rotated = rotateVertexByQuat(v.pos - .5, instance.rotation) + instance.pos + .5; vec4 worldPos = vec4(rotated, 1.); @@ -49,3 +48,4 @@ BlockFrag FLWMain(Vertex v, Belt instance) { return b; } +#endif diff --git a/src/main/resources/assets/create/flywheel/shaders/context/contraption.glsl b/src/main/resources/assets/create/flywheel/shaders/context/contraption.glsl new file mode 100644 index 000000000..5b561790a --- /dev/null +++ b/src/main/resources/assets/create/flywheel/shaders/context/contraption.glsl @@ -0,0 +1,67 @@ +#use "flywheel:context/fog.glsl" + +varying vec3 BoxCoord; + +uniform sampler3D uLightVolume; + +uniform sampler2D uBlockAtlas; +uniform sampler2D uLightMap; + +uniform vec3 uLightBoxSize; +uniform vec3 uLightBoxMin; +uniform mat4 uModel; + +uniform float uTime; +uniform mat4 uViewProjection; +uniform vec3 uCameraPos; + +uniform vec2 uWindowSize; + +void FLWFinalizeNormal(inout vec3 normal) { + mat3 m; + m[0] = uModel[0].xyz; + m[1] = uModel[1].xyz; + m[2] = uModel[2].xyz; + normal = m * normal; +} + +#if defined(VERTEX_SHADER) + +void FLWFinalizeWorldPos(inout vec4 worldPos) { + worldPos = uModel * worldPos; + + BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; + + #if defined(USE_FOG) + FragDistance = length(worldPos.xyz); + #endif + + gl_Position = uViewProjection * worldPos; +} + +#elif defined(FRAGMENT_SHADER) +#use "flywheel:core/lightutil.glsl" + +vec4 FLWBlockTexture(vec2 texCoords) { + return texture2D(uBlockAtlas, texCoords); +} + +void FLWFinalizeColor(vec4 color) { + #if defined(USE_FOG) + float a = color.a; + float fog = clamp(FLWFogFactor(), 0., 1.); + + color = mix(uFogColor, color, fog); + color.a = a; + #endif + + gl_FragColor = color; +} + +vec4 FLWLight(vec2 lightCoords) { + lightCoords = max(lightCoords, texture3D(uLightVolume, BoxCoord).rg); + + return texture2D(uLightMap, shiftLight(lightCoords)); +} + +#endif diff --git a/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag b/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag deleted file mode 100644 index 919e64ceb..000000000 --- a/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag +++ /dev/null @@ -1,31 +0,0 @@ -#flwinclude <"flywheel:context/world/fog.glsl"> -#flwinclude <"flywheel:core/lightutil.glsl"> - -varying vec3 BoxCoord; -varying vec2 BoxLight; -uniform sampler3D uLightVolume; - -uniform sampler2D uBlockAtlas; -uniform sampler2D uLightMap; - -vec4 FLWBlockTexture(vec2 texCoords) { - return texture2D(uBlockAtlas, texCoords); -} - -void FLWFinalizeColor(vec4 color) { - #if defined(USE_FOG) - float a = color.a; - float fog = clamp(FLWFogFactor(), 0., 1.); - - color = mix(uFogColor, color, fog); - color.a = a; - #endif - - gl_FragColor = color; -} - -vec4 FLWLight(vec2 lightCoords) { - lightCoords = max(lightCoords, texture3D(uLightVolume, BoxCoord).rg); - - return texture2D(uLightMap, shiftLight(lightCoords)); -} diff --git a/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.vert b/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.vert deleted file mode 100644 index 3d82348ea..000000000 --- a/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.vert +++ /dev/null @@ -1,34 +0,0 @@ -#if defined(USE_FOG) -varying float FragDistance; -#endif - -varying vec3 BoxCoord; - -uniform vec3 uLightBoxSize; -uniform vec3 uLightBoxMin; -uniform mat4 uModel; - -uniform float uTime; -uniform mat4 uViewProjection; -uniform vec3 uCameraPos; - -void FLWFinalizeWorldPos(inout vec4 worldPos) { - worldPos = uModel * worldPos; - - BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; - - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz); - #endif - - gl_Position = uViewProjection * worldPos; -} - -void FLWFinalizeNormal(inout vec3 normal) { - mat3 m; - m[0] = uModel[0].xyz; - m[1] = uModel[1].xyz; - m[2] = uModel[2].xyz; - normal = m * normal; -} - diff --git a/src/main/resources/assets/create/flywheel/shaders/contraption_actor.vert b/src/main/resources/assets/create/flywheel/shaders/contraption_actor.vert index 25ff4980a..5de47885d 100644 --- a/src/main/resources/assets/create/flywheel/shaders/contraption_actor.vert +++ b/src/main/resources/assets/create/flywheel/shaders/contraption_actor.vert @@ -1,11 +1,9 @@ #define PI 3.1415926538 -#flwbuiltins -#flwinclude <"flywheel:core/matutils.glsl"> -#flwinclude <"flywheel:core/quaternion.glsl"> -#flwinclude <"flywheel:core/diffuse.glsl"> +#use "flywheel:core/matutils.glsl" +#use "flywheel:core/quaternion.glsl" +#use "flywheel:core/diffuse.glsl" -#[InstanceData] struct Actor { vec3 pos; vec2 light; @@ -16,10 +14,11 @@ struct Actor { float speed; }; -#flwinclude <"flywheel:data/modelvertex.glsl"> -#flwinclude <"flywheel:data/blockfragment.glsl"> +#use "flywheel:data/modelvertex.glsl" +#use "flywheel:block.frag" -BlockFrag FLWMain(Vertex v, Actor instance) { +#if defined(VERTEX_SHADER) +BlockFrag vertex(Vertex v, Actor instance) { float degrees = instance.offset + uTime * instance.speed / 20.; //float angle = fract(degrees / 360.) * PI * 2.; @@ -45,3 +44,4 @@ BlockFrag FLWMain(Vertex v, Actor instance) { return b; } +#endif diff --git a/src/main/resources/assets/create/flywheel/shaders/contraption_structure.vert b/src/main/resources/assets/create/flywheel/shaders/contraption_structure.vert index 02e90fb66..66db67682 100644 --- a/src/main/resources/assets/create/flywheel/shaders/contraption_structure.vert +++ b/src/main/resources/assets/create/flywheel/shaders/contraption_structure.vert @@ -1,10 +1,8 @@ #define PI 3.1415926538 -#flwbuiltins -#flwinclude <"flywheel:core/matutils.glsl"> -#flwinclude <"flywheel:core/diffuse.glsl"> +#use "flywheel:core/matutils.glsl" +#use "flywheel:core/diffuse.glsl" -#[VertexData] struct Vertex { vec3 pos; vec3 normal; @@ -13,9 +11,10 @@ struct Vertex { vec2 modelLight; }; -#flwinclude <"flywheel:data/blockfragment.glsl"> +#use "flywheel:block.frag" -BlockFrag FLWMain(Vertex v) { +#if defined(VERTEX_SHADER) +BlockFrag vertex(Vertex v) { vec4 worldPos = vec4(v.pos, 1.); vec3 norm = v.normal; @@ -35,3 +34,4 @@ BlockFrag FLWMain(Vertex v) { return b; } +#endif diff --git a/src/main/resources/assets/create/flywheel/shaders/flap.vert b/src/main/resources/assets/create/flywheel/shaders/flap.vert index ef39637df..721be4ed1 100644 --- a/src/main/resources/assets/create/flywheel/shaders/flap.vert +++ b/src/main/resources/assets/create/flywheel/shaders/flap.vert @@ -1,11 +1,9 @@ #define PI 3.1415926538 -#flwbuiltins -#flwinclude <"flywheel:core/matutils.glsl"> -#flwinclude <"flywheel:core/quaternion.glsl"> -#flwinclude <"flywheel:core/diffuse.glsl"> +#use "flywheel:core/matutils.glsl" +#use "flywheel:core/quaternion.glsl" +#use "flywheel:core/diffuse.glsl" -#[InstanceData] struct Flap { vec3 instancePos; vec2 light; @@ -17,9 +15,10 @@ struct Flap { float flapness; }; -#flwinclude <"flywheel:data/modelvertex.glsl"> -#flwinclude <"flywheel:data/blockfragment.glsl"> +#use "flywheel:data/modelvertex.glsl" +#use "flywheel:block.frag" +#if defined(VERTEX_SHADER) float toRad(float degrees) { return fract(degrees / 360.) * PI * 2.; @@ -38,7 +37,7 @@ float getFlapAngle(float flapness, float intensity, float scale) { return degrees; } -BlockFrag FLWMain(Vertex v, Flap flap) { +BlockFrag vertex(Vertex v, Flap flap) { float flapAngle = getFlapAngle(flap.flapness, flap.intensity, flap.flapScale); vec4 orientation = quat(vec3(0., 1., 0.), -flap.horizontalAngle); @@ -64,3 +63,4 @@ BlockFrag FLWMain(Vertex v, Flap flap) { #endif return b; } +#endif diff --git a/src/main/resources/assets/create/flywheel/shaders/rotating.vert b/src/main/resources/assets/create/flywheel/shaders/rotating.vert index 5acfdbd3f..a6eb6a062 100644 --- a/src/main/resources/assets/create/flywheel/shaders/rotating.vert +++ b/src/main/resources/assets/create/flywheel/shaders/rotating.vert @@ -1,10 +1,8 @@ #define PI 3.1415926538 -#flwbuiltins -#flwinclude <"flywheel:core/matutils.glsl"> -#flwinclude <"flywheel:core/diffuse.glsl"> +#use "flywheel:core/matutils.glsl" +#use "flywheel:core/diffuse.glsl" -#[InstanceData] struct Rotating { vec2 light; vec4 color; @@ -14,8 +12,8 @@ struct Rotating { vec3 axis; }; -#flwinclude <"flywheel:data/modelvertex.glsl"> -#flwinclude <"flywheel:data/blockfragment.glsl"> +#use "flywheel:data/modelvertex.glsl" +#use "flywheel:block.frag" mat4 kineticRotation(float offset, float speed, vec3 axis) { float degrees = offset + uTime * speed * 3./10.; @@ -24,7 +22,8 @@ mat4 kineticRotation(float offset, float speed, vec3 axis) { return rotate(axis, angle); } -BlockFrag FLWMain(Vertex v, Rotating instance) { +#if defined(VERTEX_SHADER) +BlockFrag vertex(Vertex v, Rotating instance) { mat4 spin = kineticRotation(instance.offset, instance.speed, instance.axis); vec4 worldPos = spin * vec4(v.pos - .5, 1.); @@ -50,3 +49,4 @@ BlockFrag FLWMain(Vertex v, Rotating instance) { return b; } +#endif