- Redo shader loading
 - Now loads an immutable SourceFile containing some metadata
 - Replace legacy compilation pipeline with improved new one using new api
 - Builtins are defined in one file, now "header"
 - New ErrorReporter/ErrorBuilder methods
 - Fancier shader loading errors
This commit is contained in:
Jozufozu 2021-08-08 22:35:27 -07:00
parent 5555f9e6bb
commit cb47ca8452
10 changed files with 127 additions and 116 deletions

View file

@ -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<IProgramExtension> extensions) {
super(program, extensions);
public ContraptionProgram(ResourceLocation name, int handle, List<IProgramExtension> extensions) {
super(name, handle, extensions);
uLightBoxSize = getUniformLocation("uLightBoxSize");
uLightBoxMin = getUniformLocation("uLightBoxMin");
uModel = getUniformLocation("uModel");

View file

@ -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<ContraptionProgram> 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<ContraptionProgram> contraptionContext(Backend backend, ITemplate template) {
FileResolution header = backend.sources.resolveFile(ResourceUtil.subPath(CONTRAPTION, ".glsl"));
IShaderPipeline<ContraptionProgram> worldPipeline = new WorldShaderPipeline<>(backend.sources, ContraptionProgram::new, template, header);
return new WorldContext<>(backend, worldPipeline)
.withName(CONTRAPTION);
}
}

View file

@ -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

View file

@ -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

View file

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

View file

@ -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;
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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