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 ff3683f96..2dea078ab 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 @@ -53,7 +53,7 @@ public class IndirectPrograms extends AtomicReferenceCounted { } private static List getExtensions(GlslVersion glslVersion) { - List extensions = new ArrayList<>(); + var extensions = ImmutableList.builder(); if (glslVersion.compareTo(GlslVersion.V400) < 0) { extensions.add("GL_ARB_gpu_shader5"); } @@ -66,15 +66,18 @@ public class IndirectPrograms extends AtomicReferenceCounted { if (glslVersion.compareTo(GlslVersion.V460) < 0) { extensions.add("GL_ARB_shader_draw_parameters"); } - return extensions; + return extensions.build(); } private static List getComputeExtensions(GlslVersion glslVersion) { - List extensions = new ArrayList<>(); + var extensions = ImmutableList.builder(); + + extensions.addAll(EXTENSIONS); + if (glslVersion.compareTo(GlslVersion.V430) < 0) { extensions.add("GL_ARB_compute_shader"); } - return extensions; + return extensions.build(); } static void reload(ShaderSources sources, ImmutableList pipelineKeys, List vertexComponents, List fragmentComponents) { @@ -86,12 +89,12 @@ public class IndirectPrograms extends AtomicReferenceCounted { var pipelineCompiler = PipelineCompiler.create(sources, Pipelines.INDIRECT, vertexComponents, fragmentComponents, EXTENSIONS); var cullingCompiler = createCullingCompiler(sources); - var applyCompiler = createUtilCompiler(sources); + var utilCompiler = createUtilCompiler(sources); try { var pipelineResult = pipelineCompiler.compileAndReportErrors(pipelineKeys); var cullingResult = cullingCompiler.compileAndReportErrors(createCullingKeys()); - var utils = applyCompiler.compileAndReportErrors(List.of(APPLY_SHADER_MAIN, SCATTER_SHADER_MAIN)); + var utils = utilCompiler.compileAndReportErrors(List.of(APPLY_SHADER_MAIN, SCATTER_SHADER_MAIN)); if (pipelineResult != null && cullingResult != null && utils != null) { newInstance = new IndirectPrograms(pipelineResult, cullingResult, utils.get(APPLY_SHADER_MAIN), utils.get(SCATTER_SHADER_MAIN)); @@ -102,16 +105,18 @@ public class IndirectPrograms extends AtomicReferenceCounted { pipelineCompiler.delete(); cullingCompiler.delete(); - applyCompiler.delete(); + utilCompiler.delete(); setInstance(newInstance); } + /** + * A compiler for cull shaders, parameterized by the instance type. + */ private static CompilationHarness> createCullingCompiler(ShaderSources sources) { return CULL.program() .link(CULL.shader(GlCompat.MAX_GLSL_VERSION, ShaderType.COMPUTE) .nameMapper(instanceType -> "culling/" + ResourceUtil.toDebugFileNameNoExtension(instanceType.cullShader())) - .requireExtensions(EXTENSIONS) .requireExtensions(COMPUTE_EXTENSIONS) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withResource(CULL_SHADER_API_IMPL) @@ -119,21 +124,17 @@ public class IndirectPrograms extends AtomicReferenceCounted { .withResource(InstanceType::cullShader) .withComponent(SsboInstanceComponent::new) .withResource(CULL_SHADER_MAIN)) - .postLink((key, program) -> { - program.setUniformBlockBinding("_FlwFrameUniforms", Uniforms.FRAME_INDEX); - program.setUniformBlockBinding("_FlwFogUniforms", Uniforms.FOG_INDEX); - program.setUniformBlockBinding("_FlwOptionsUniforms", Uniforms.OPTIONS_INDEX); - program.setUniformBlockBinding("_FlwPlayerUniforms", Uniforms.PLAYER_INDEX); - program.setUniformBlockBinding("_FlwLevelUniforms", Uniforms.LEVEL_INDEX); - }) + .postLink((key, program) -> Uniforms.setUniformBlockBindings(program)) .harness("culling", sources); } + /** + * A compiler for utility shaders, directly compiles the shader at the resource location specified by the parameter. + */ private static CompilationHarness createUtilCompiler(ShaderSources sources) { return UTIL.program() .link(UTIL.shader(GlCompat.MAX_GLSL_VERSION, ShaderType.COMPUTE) .nameMapper(resourceLocation -> "utilities/" + ResourceUtil.toDebugFileNameNoExtension(resourceLocation)) - .requireExtensions(EXTENSIONS) .requireExtensions(COMPUTE_EXTENSIONS) .define("_FLW_SUBGROUP_SIZE", GlCompat.SUBGROUP_SIZE) .withResource(s -> s)) 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 c2320af3b..0d9b3de7c 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 @@ -28,11 +28,11 @@ public class InstancingPrograms extends AtomicReferenceCounted { } private static List getExtensions(GlslVersion glslVersion) { - List extensions = new ArrayList<>(); + var extensions = ImmutableList.builder(); if (glslVersion.compareTo(GlslVersion.V330) < 0) { extensions.add("GL_ARB_shader_bit_encoding"); } - return extensions; + return extensions.build(); } static void reload(ShaderSources sources, ImmutableList pipelineKeys, List vertexComponents, List fragmentComponents) { 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 c969674ef..09fbe049b 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 @@ -71,11 +71,7 @@ public final class PipelineCompiler { program.bindAttribLocation("_flw_aNormal", 5); }) .postLink((key, program) -> { - program.setUniformBlockBinding("_FlwFrameUniforms", Uniforms.FRAME_INDEX); - program.setUniformBlockBinding("_FlwFogUniforms", Uniforms.FOG_INDEX); - program.setUniformBlockBinding("_FlwOptionsUniforms", Uniforms.OPTIONS_INDEX); - program.setUniformBlockBinding("_FlwPlayerUniforms", Uniforms.PLAYER_INDEX); - program.setUniformBlockBinding("_FlwLevelUniforms", Uniforms.LEVEL_INDEX); + Uniforms.setUniformBlockBindings(program); program.bind(); diff --git a/common/src/backend/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java b/common/src/backend/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java index 8fa7e0115..057e506ed 100644 --- a/common/src/backend/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java +++ b/common/src/backend/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.backend.engine.uniform; import com.jozufozu.flywheel.api.event.RenderContext; +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; public final class Uniforms { public static final int FRAME_INDEX = 0; @@ -9,6 +10,12 @@ public final class Uniforms { public static final int PLAYER_INDEX = 3; public static final int LEVEL_INDEX = 4; + public static final String FRAME_BLOCK_NAME = "_FlwFrameUniforms"; + public static final String FOG_BLOCK_NAME = "_FlwFogUniforms"; + public static final String OPTIONS_BLOCK_NAME = "_FlwOptionsUniforms"; + public static final String PLAYER_BLOCK_NAME = "_FlwPlayerUniforms"; + public static final String LEVEL_BLOCK_NAME = "_FlwLevelUniforms"; + private static final UniformBuffer[] ALL_BUFFERS = { FrameUniforms.BUFFER, FogUniforms.BUFFER, OptionsUniforms.BUFFER, PlayerUniforms.BUFFER, LevelUniforms.BUFFER }; private Uniforms() { @@ -32,6 +39,14 @@ public final class Uniforms { } } + public static void setUniformBlockBindings(GlProgram program) { + program.setUniformBlockBinding(FRAME_BLOCK_NAME, FRAME_INDEX); + program.setUniformBlockBinding(FOG_BLOCK_NAME, FOG_INDEX); + program.setUniformBlockBinding(OPTIONS_BLOCK_NAME, OPTIONS_INDEX); + program.setUniformBlockBinding(PLAYER_BLOCK_NAME, PLAYER_INDEX); + program.setUniformBlockBinding(LEVEL_BLOCK_NAME, LEVEL_INDEX); + } + public static void onReloadLevelRenderer() { deleteAll(); }