Skip to content

Commit

Permalink
Willing and able
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Jozufozu committed Apr 30, 2024
1 parent 96164f7 commit 96bd163
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ private IndirectPrograms(Map<PipelineProgramKey, GlProgram> pipeline, Map<Instan

private static List<String> getExtensions(GlslVersion glslVersion) {
List<String> 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");
}
Expand Down Expand Up @@ -105,8 +111,8 @@ private static CompilationHarness<InstanceType<?>> 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)
Expand All @@ -127,8 +133,8 @@ private static CompilationHarness<ResourceLocation> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jozufozu.flywheel.backend.compile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -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<String> EXTENSIONS = getExtensions(GlCompat.MAX_GLSL_VERSION);

@Nullable
private static InstancingPrograms instance;

Expand All @@ -23,14 +27,22 @@ private InstancingPrograms(Map<PipelineProgramKey, GlProgram> pipeline) {
this.pipeline = pipeline;
}

private static List<String> getExtensions(GlslVersion glslVersion) {
List<String> extensions = new ArrayList<>();
if (glslVersion.compareTo(GlslVersion.V330) < 0) {
extensions.add("GL_ARB_shader_bit_encoding");
}
return extensions;
}

static void reload(ShaderSources sources, ImmutableList<PipelineProgramKey> pipelineKeys, List<SourceComponent> vertexComponents, List<SourceComponent> fragmentComponents) {
if (!GlCompat.SUPPORTS_INSTANCING) {
return;
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static CompilationHarness<PipelineProgramKey> 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)
Expand All @@ -55,7 +55,7 @@ static CompilationHarness<PipelineProgramKey> 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))
Expand Down Expand Up @@ -91,8 +91,4 @@ static CompilationHarness<PipelineProgramKey> create(ShaderSources sources, Pipe
})
.harness(pipeline.compilerMarker(), sources);
}

static CompilationHarness<PipelineProgramKey> create(ShaderSources sources, Pipeline pipeline, List<SourceComponent> vertexComponents, List<SourceComponent> fragmentComponents) {
return create(sources, pipeline, vertexComponents, fragmentComponents, Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public ShaderCompiler<K> enableExtensions(Collection<String> extensions) {
});
}

public ShaderCompiler<K> requireExtensions(Collection<String> 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<SourceComponent>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 96bd163

Please sign in to comment.