From 069679689f54f2900a3362bf2aced4fcf665e169 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 2 May 2024 12:59:03 -0700 Subject: [PATCH] Shaders and ladders - Compile with progressively lower glsl versions to test which are available. - Properly returns 460 on my machine even with a gl version of 320 - Remove glsl enums below 150 --- .../backend/compile/core/Compilation.java | 2 +- .../flywheel/backend/gl/GlCompat.java | 62 ++++++++++++------- .../flywheel/backend/glsl/GlslVersion.java | 4 -- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java index 7ab8db17f..d61c49133 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compilation.java @@ -134,7 +134,7 @@ public class Compilation { } } - private static boolean compiledSuccessfully(int handle) { + public static boolean compiledSuccessfully(int handle) { return GL20.glGetShaderi(handle, GL20.GL_COMPILE_STATUS) == GL20.GL_TRUE; } } diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/gl/GlCompat.java b/common/src/backend/java/com/jozufozu/flywheel/backend/gl/GlCompat.java index a7fa5c58f..3fa3c54a1 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/gl/GlCompat.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/gl/GlCompat.java @@ -4,12 +4,14 @@ import java.nio.ByteBuffer; import org.lwjgl.PointerBuffer; import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL20C; import org.lwjgl.opengl.GL31C; import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.opengl.KHRShaderSubgroup; import org.lwjgl.system.MemoryStack; +import com.jozufozu.flywheel.backend.compile.core.Compilation; import com.jozufozu.flywheel.backend.glsl.GlslVersion; import com.jozufozu.flywheel.lib.math.MoreMath; @@ -88,28 +90,6 @@ public final class GlCompat { return DRIVER == Driver.AMD || DRIVER == Driver.MESA ? 64 : 32; } - private static GlslVersion maxGlslVersion() { - if (CAPABILITIES.OpenGL46) { - return GlslVersion.V460; - } else if (CAPABILITIES.OpenGL45) { - return GlslVersion.V450; - } else if (CAPABILITIES.OpenGL44) { - return GlslVersion.V440; - } else if (CAPABILITIES.OpenGL43) { - return GlslVersion.V430; - } else if (CAPABILITIES.OpenGL42) { - return GlslVersion.V420; - } else if (CAPABILITIES.OpenGL41) { - return GlslVersion.V410; - } else if (CAPABILITIES.OpenGL40) { - return GlslVersion.V400; - } else if (CAPABILITIES.OpenGL33) { - return GlslVersion.V330; - } else { - return GlslVersion.V150; - } - } - private static boolean isInstancingSupported() { if (CAPABILITIES.OpenGL33) { return true; @@ -131,4 +111,42 @@ public final class GlCompat { && CAPABILITIES.GL_ARB_shading_language_420pack && CAPABILITIES.GL_ARB_vertex_attrib_binding; } + + /** + * Try to compile a shader with progressively lower glsl versions. + * The first version to compile successfully is returned. + * @return The highest glsl version that could be compiled. + */ + private static GlslVersion maxGlslVersion() { + var glslVersions = GlslVersion.values(); + // No need to test glsl 150 as that is guaranteed to be supported by MC. + for (int i = glslVersions.length - 1; i > 0; i--) { + var version = glslVersions[i]; + + if (canCompileVersion(version)) { + return version; + } + } + + return GlslVersion.V150; + } + + private static boolean canCompileVersion(GlslVersion version) { + int handle = GL20.glCreateShader(GL20.GL_VERTEX_SHADER); + + // Compile the simplest possible shader. + var source = """ + #version %d + void main() {} + """.formatted(version.version); + + safeShaderSource(handle, source); + GL20.glCompileShader(handle); + + boolean success = Compilation.compiledSuccessfully(handle); + + GL20.glDeleteShader(handle); + + return success; + } } diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/glsl/GlslVersion.java b/common/src/backend/java/com/jozufozu/flywheel/backend/glsl/GlslVersion.java index e12d29bd5..11453eb07 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/glsl/GlslVersion.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/glsl/GlslVersion.java @@ -1,10 +1,6 @@ package com.jozufozu.flywheel.backend.glsl; public enum GlslVersion { - V110(110), - V120(120), - V130(130), - V140(140), V150(150), V330(330), V400(400),