From c2d110e38fb5e4886162bc37c612d96fec98f4b8 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 5 Aug 2021 12:16:17 -0700 Subject: [PATCH] More steps towards actually using the new loader --- .../backend/gl/shader/ShaderType.java | 8 ++-- .../backend/pipeline/GLSLVersion.java | 18 +++++++++ .../flywheel/backend/pipeline/GlShader.java | 39 +++++++++++++++++++ .../flywheel/backend/pipeline/Shader.java | 31 +++++++++++++++ .../flywheel/backend/pipeline/SourceFile.java | 31 ++++++++------- .../backend/pipeline/WorldShaderPipeline.java | 6 ++- .../flywheel/core/shader/WorldProgram.java | 30 +++++++++++--- .../shaders/context/world/builtin.frag | 1 + 8 files changed, 139 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/pipeline/GLSLVersion.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/pipeline/GlShader.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/pipeline/Shader.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderType.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderType.java index 2ac5a3721..a7146ef56 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderType.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderType.java @@ -3,15 +3,17 @@ package com.jozufozu.flywheel.backend.gl.shader; import org.lwjgl.opengl.GL20; public enum ShaderType { - VERTEX("vertex", GL20.GL_VERTEX_SHADER), - FRAGMENT("fragment", GL20.GL_FRAGMENT_SHADER), + VERTEX("vertex", "VERTEX_SHADER", GL20.GL_VERTEX_SHADER), + FRAGMENT("fragment", "FRAGMENT_SHADER", GL20.GL_FRAGMENT_SHADER), ; public final String name; + public final String define; public final int glEnum; - ShaderType(String name, int glEnum) { + ShaderType(String name, String define, int glEnum) { this.name = name; + this.define = define; this.glEnum = glEnum; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/pipeline/GLSLVersion.java b/src/main/java/com/jozufozu/flywheel/backend/pipeline/GLSLVersion.java new file mode 100644 index 000000000..a2dcfab4c --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/pipeline/GLSLVersion.java @@ -0,0 +1,18 @@ +package com.jozufozu.flywheel.backend.pipeline; + +public enum GLSLVersion { + V110(110), + V120(120), + ; + + public final int version; + + GLSLVersion(int version) { + this.version = version; + } + + @Override + public String toString() { + return Integer.toString(version); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/pipeline/GlShader.java b/src/main/java/com/jozufozu/flywheel/backend/pipeline/GlShader.java new file mode 100644 index 000000000..d92ac9c60 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/pipeline/GlShader.java @@ -0,0 +1,39 @@ +package com.jozufozu.flywheel.backend.pipeline; + +import org.lwjgl.opengl.GL20; + +import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.gl.GlObject; +import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; + +public class GlShader extends GlObject { + + public GlShader(ShaderType type, CharSequence source) { + int handle = GL20.glCreateShader(type.glEnum); + + GlCompat.safeShaderSource(handle, source); + + GL20.glCompileShader(handle); + + String log = GL20.glGetShaderInfoLog(handle); + + if (!log.isEmpty()) { + Backend.log.error("Shader compilation log for " + "DUBUG"+ ": " + log); + Backend.log.error(source); + } + //Backend.log.debug(shader.printSource()); + + if (GL20.glGetShaderi(handle, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) { + throw new RuntimeException("Could not compile " + "DEBUG" + ". See log for details."); + } + + + setHandle(handle); + } + + @Override + protected void deleteInternal(int handle) { + + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/pipeline/Shader.java b/src/main/java/com/jozufozu/flywheel/backend/pipeline/Shader.java new file mode 100644 index 000000000..2f44f774f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/pipeline/Shader.java @@ -0,0 +1,31 @@ +package com.jozufozu.flywheel.backend.pipeline; + +import com.jozufozu.flywheel.backend.gl.shader.ShaderType; + +public class Shader { + + private final GLSLVersion version; + private final CharSequence source; + + public Shader(GLSLVersion version, CharSequence source) { + this.version = version; + this.source = source; + } + + public GlShader create(ShaderType type) { + + StringBuilder source = new StringBuilder(); + + source.append("#version ") + .append(version.version) + .append('\n'); + + source.append("#define ") + .append(type.define) + .append('\n'); + + source.append(this.source); + + return new GlShader(type, source); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/pipeline/SourceFile.java b/src/main/java/com/jozufozu/flywheel/backend/pipeline/SourceFile.java index 0ccec9693..73b60d52c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/pipeline/SourceFile.java +++ b/src/main/java/com/jozufozu/flywheel/backend/pipeline/SourceFile.java @@ -37,6 +37,7 @@ public class SourceFile { private final ShaderSources parent; private final String source; + private final CharSequence elided; private final ImmutableList lines; private final IntList lineStarts; @@ -62,12 +63,18 @@ public class SourceFile { this.includes = parseIncludes(); this.functions = parseFunctions(); this.structs = parseStructs(); + + this.elided = createElidedSource(); } public String getSource() { return source; } + public CharSequence getElidedSource() { + return elided; + } + public ShaderSources getParent() { return parent; } @@ -119,26 +126,20 @@ public class SourceFile { return builder.toString(); } - private CharSequence elided = null; + private CharSequence createElidedSource() { + StringBuilder out = new StringBuilder(); - public CharSequence getElidedSource() { - if (elided == null) { - StringBuilder out = new StringBuilder(); + int lastEnd = 0; - int lastEnd = 0; + for (Span elision : elisions) { + out.append(source, lastEnd, elision.getStartPos()); - for (Span elision : elisions) { - out.append(source, lastEnd, elision.getStartPos()); - - lastEnd = elision.getEndPos(); - } - - out.append(source, lastEnd, source.length()); - - elided = out.toString(); + lastEnd = elision.getEndPos(); } - return elided; + out.append(source, lastEnd, source.length()); + + return out; } /** diff --git a/src/main/java/com/jozufozu/flywheel/backend/pipeline/WorldShaderPipeline.java b/src/main/java/com/jozufozu/flywheel/backend/pipeline/WorldShaderPipeline.java index 014cd0b19..ca7f3a074 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/pipeline/WorldShaderPipeline.java +++ b/src/main/java/com/jozufozu/flywheel/backend/pipeline/WorldShaderPipeline.java @@ -5,6 +5,8 @@ import java.util.List; import javax.annotation.Nullable; import com.jozufozu.flywheel.backend.ShaderSources; +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; +import com.jozufozu.flywheel.backend.loading.Program; import com.jozufozu.flywheel.core.shader.IMultiProgram; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; @@ -34,7 +36,9 @@ public class WorldShaderPipeline

{ builder.include(file); - builder.build(); + CharSequence output = builder.build(); + + //Program program = new Program() return null; } diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java b/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java index 60b6589cb..97981d23a 100644 --- a/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java +++ b/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.core.shader; import static org.lwjgl.opengl.GL20.glUniform1f; +import static org.lwjgl.opengl.GL20.glUniform2f; import static org.lwjgl.opengl.GL20.glUniform3f; import java.util.List; @@ -9,21 +10,21 @@ import com.jozufozu.flywheel.backend.loading.Program; import com.jozufozu.flywheel.core.shader.extension.IProgramExtension; import com.jozufozu.flywheel.util.AnimationTickHolder; +import net.minecraft.client.MainWindow; +import net.minecraft.client.Minecraft; import net.minecraft.util.math.vector.Matrix4f; public class WorldProgram extends ExtensibleGlProgram { - protected final int uTime; - protected final int uViewProjection; - protected final int uCameraPos; + protected final int uTime = getUniformLocation("uTime"); + protected final int uViewProjection = getUniformLocation("uViewProjection"); + protected final int uCameraPos = getUniformLocation("uCameraPos"); + protected final int uWindowSize = getUniformLocation("uWindowSize"); protected int uBlockAtlas; protected int uLightMap; public WorldProgram(Program program, List extensions) { super(program, extensions); - uTime = getUniformLocation("uTime"); - uViewProjection = getUniformLocation("uViewProjection"); - uCameraPos = getUniformLocation("uCameraPos"); super.bind(); registerSamplers(); @@ -36,14 +37,30 @@ public class WorldProgram extends ExtensibleGlProgram { } public void uploadViewProjection(Matrix4f viewProjection) { + if (uViewProjection < 0) return; + uploadMatrixUniform(uViewProjection, viewProjection); } + public void uploadWindowSize() { + if (uWindowSize < 0) return; + + MainWindow window = Minecraft.getInstance().getWindow(); + + int height = window.getScreenHeight(); + int width = window.getScreenWidth(); + glUniform2f(uWindowSize, width, height); + } + public void uploadCameraPos(double camX, double camY, double camZ) { + if (uCameraPos < 0) return; + glUniform3f(uCameraPos, (float) camX, (float) camY, (float) camZ); } public void uploadTime(float renderTime) { + if (uTime < 0) return; + glUniform1f(uTime, renderTime); } @@ -51,6 +68,7 @@ public class WorldProgram extends ExtensibleGlProgram { public void bind() { super.bind(); + uploadWindowSize(); uploadTime(AnimationTickHolder.getRenderTime()); } } diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/world/builtin.frag b/src/main/resources/assets/flywheel/flywheel/shaders/context/world/builtin.frag index e8b615c75..831d5a1de 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/world/builtin.frag +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/world/builtin.frag @@ -3,6 +3,7 @@ uniform sampler2D uBlockAtlas; uniform sampler2D uLightMap; +uniform vec2 uWindowSize; vec4 FLWBlockTexture(vec2 texCoords) { return texture2D(uBlockAtlas, texCoords);