From 96bd1639701c54704a7acc780ab14b71f1bb8d94 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 29 Apr 2024 23:52:59 -0700 Subject: [PATCH] Willing and able - Refine capabilities checks - To compile with glsl 150: - Instancing needs ARB_shader_bit_encoding - Indirect needs gpu_shader5 and shading_language_420pack - Require extensions instead of just enable, probably doesn't make a difference since we check for their presence first but require aligns with our intent better --- .../backend/compile/IndirectPrograms.java | 14 ++++++++++---- .../backend/compile/InstancingPrograms.java | 14 +++++++++++++- .../backend/compile/PipelineCompiler.java | 8 ++------ .../backend/compile/core/Compilation.java | 6 ++++++ .../flywheel/backend/compile/core/Compile.java | 8 ++++++++ .../jozufozu/flywheel/backend/gl/GlCompat.java | 15 +++++++-------- 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java index 3806b07d7..ff3683f96 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java @@ -54,6 +54,12 @@ private IndirectPrograms(Map pipeline, Map getExtensions(GlslVersion glslVersion) { List extensions = new ArrayList<>(); + if (glslVersion.compareTo(GlslVersion.V400) < 0) { + extensions.add("GL_ARB_gpu_shader5"); + } + if (glslVersion.compareTo(GlslVersion.V420) < 0) { + extensions.add("GL_ARB_shading_language_420pack"); + } if (glslVersion.compareTo(GlslVersion.V430) < 0) { extensions.add("GL_ARB_shader_storage_buffer_object"); } @@ -105,8 +111,8 @@ private static CompilationHarness> createCullingCompiler(ShaderS return CULL.program() .link(CULL.shader(GlCompat.MAX_GLSL_VERSION, ShaderType.COMPUTE) .nameMapper(instanceType -> "culling/" + ResourceUtil.toDebugFileNameNoExtension(instanceType.cullShader())) - .enableExtensions(EXTENSIONS) - .enableExtensions(COMPUTE_EXTENSIONS) + .requireExtensions(EXTENSIONS) + .requireExtensions(COMPUTE_EXTENSIONS) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withResource(CULL_SHADER_API_IMPL) .withComponent(InstanceStructComponent::new) @@ -127,8 +133,8 @@ private static CompilationHarness createUtilCompiler(ShaderSou return UTIL.program() .link(UTIL.shader(GlCompat.MAX_GLSL_VERSION, ShaderType.COMPUTE) .nameMapper(resourceLocation -> "utilities/" + ResourceUtil.toDebugFileNameNoExtension(resourceLocation)) - .enableExtensions(EXTENSIONS) - .enableExtensions(COMPUTE_EXTENSIONS) + .requireExtensions(EXTENSIONS) + .requireExtensions(COMPUTE_EXTENSIONS) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withResource(s -> s)) .harness("utilities", sources); diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java index 8109f872c..c2320af3b 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend.compile; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -9,11 +10,14 @@ import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.backend.gl.GlCompat; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; +import com.jozufozu.flywheel.backend.glsl.GlslVersion; import com.jozufozu.flywheel.backend.glsl.ShaderSources; import com.jozufozu.flywheel.backend.glsl.SourceComponent; import com.jozufozu.flywheel.backend.util.AtomicReferenceCounted; public class InstancingPrograms extends AtomicReferenceCounted { + private static final List EXTENSIONS = getExtensions(GlCompat.MAX_GLSL_VERSION); + @Nullable private static InstancingPrograms instance; @@ -23,6 +27,14 @@ private InstancingPrograms(Map pipeline) { this.pipeline = pipeline; } + private static List getExtensions(GlslVersion glslVersion) { + List extensions = new ArrayList<>(); + if (glslVersion.compareTo(GlslVersion.V330) < 0) { + extensions.add("GL_ARB_shader_bit_encoding"); + } + return extensions; + } + static void reload(ShaderSources sources, ImmutableList pipelineKeys, List vertexComponents, List fragmentComponents) { if (!GlCompat.SUPPORTS_INSTANCING) { return; @@ -30,7 +42,7 @@ static void reload(ShaderSources sources, ImmutableList pipe InstancingPrograms newInstance = null; - var pipelineCompiler = PipelineCompiler.create(sources, Pipelines.INSTANCING, vertexComponents, fragmentComponents); + var pipelineCompiler = PipelineCompiler.create(sources, Pipelines.INSTANCING, vertexComponents, fragmentComponents, EXTENSIONS); try { var pipelineResult = pipelineCompiler.compileAndReportErrors(pipelineKeys); diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java index a7dc27e3a..c969674ef 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/PipelineCompiler.java @@ -37,7 +37,7 @@ static CompilationHarness create(ShaderSources sources, Pipe .nameLowerCase(); return "pipeline/" + pipeline.compilerMarker() + "/" + instance + "_" + context; }) - .enableExtensions(extensions) + .requireExtensions(extensions) .onCompile((key, comp) -> key.contextShader() .onCompile(comp)) .withResource(API_IMPL_VERT) @@ -55,7 +55,7 @@ static CompilationHarness create(ShaderSources sources, Pipe .nameLowerCase(); return "pipeline/" + pipeline.compilerMarker() + "/" + context; }) - .enableExtensions(extensions) + .requireExtensions(extensions) .enableExtension("GL_ARB_conservative_depth") .onCompile((key, comp) -> key.contextShader() .onCompile(comp)) @@ -91,8 +91,4 @@ static CompilationHarness create(ShaderSources sources, Pipe }) .harness(pipeline.compilerMarker(), sources); } - - static CompilationHarness create(ShaderSources sources, Pipeline pipeline, List vertexComponents, List fragmentComponents) { - return create(sources, pipeline, vertexComponents, fragmentComponents, Collections.emptyList()); - } } 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 3fea1ffb7..7ab8db17f 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 @@ -64,6 +64,12 @@ public void enableExtension(String ext) { .append(" : enable\n"); } + public void requireExtension(String ext) { + fullSource.append("#extension ") + .append(ext) + .append(" : require\n"); + } + public void define(String key, String value) { fullSource.append("#define ") .append(key) diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compile.java b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compile.java index b74b71ff0..7642adb37 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compile.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/compile/core/Compile.java @@ -113,6 +113,14 @@ public ShaderCompiler enableExtensions(Collection extensions) { }); } + public ShaderCompiler requireExtensions(Collection extensions) { + return onCompile(($, ctx) -> { + for (String extension : extensions) { + ctx.requireExtension(extension); + } + }); + } + @Nullable private GlShader compile(K key, ShaderCache compiler, SourceLoader loader) { var components = new ArrayList(); 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 fcf56a471..a7fa5c58f 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 @@ -111,25 +111,24 @@ private static GlslVersion maxGlslVersion() { } private static boolean isInstancingSupported() { - if (!CAPABILITIES.OpenGL33) { - return false; + if (CAPABILITIES.OpenGL33) { + return true; } - return true; + return CAPABILITIES.GL_ARB_shader_bit_encoding; } private static boolean isIndirectSupported() { - // The GL requirement cannot be lower because GL_ARB_compute_shader requires at least GL 4.2. - if (!CAPABILITIES.OpenGL42) { - return false; - } if (CAPABILITIES.OpenGL46) { return true; } return CAPABILITIES.GL_ARB_compute_shader && CAPABILITIES.GL_ARB_direct_state_access + && CAPABILITIES.GL_ARB_gpu_shader5 && CAPABILITIES.GL_ARB_multi_bind && CAPABILITIES.GL_ARB_multi_draw_indirect && CAPABILITIES.GL_ARB_shader_draw_parameters - && CAPABILITIES.GL_ARB_shader_storage_buffer_object; + && CAPABILITIES.GL_ARB_shader_storage_buffer_object + && CAPABILITIES.GL_ARB_shading_language_420pack + && CAPABILITIES.GL_ARB_vertex_attrib_binding; } }