From c196ed5d7801c8c54e5297eda1f610cb902e6c20 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 1 Jan 2022 15:19:49 -0800 Subject: [PATCH] Consider RenderLayer when compiling shaders - Disabled cutout logic in other layers --- .../flywheel/backend/ShaderContext.java | 2 +- .../instancing/InstancedMaterialGroup.java | 13 +++++++------ .../instancing/InstancingEngine.java | 2 +- ...eAllocator.java => FallbackAllocator.java} | 5 ++--- .../jozufozu/flywheel/core/WorldContext.java | 6 ++++-- .../core/crumbling/CrumblingGroup.java | 5 +++-- .../core/pipeline/CachingCompiler.java | 9 +++------ .../core/pipeline/CompilationContext.java | 12 +++++++++--- .../core/pipeline/ShaderCompiler.java | 19 +++++++++++++------ .../flywheel/shaders/context/world.glsl | 2 -- 10 files changed, 43 insertions(+), 32 deletions(-) rename src/main/java/com/jozufozu/flywheel/backend/model/{ImmediateAllocator.java => FallbackAllocator.java} (66%) diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java index 4367194eb..1e3d68f8a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java @@ -7,7 +7,7 @@ import net.minecraft.resources.ResourceLocation; public interface ShaderContext

{ - P getProgram(ResourceLocation loc, VertexType vertexType); + P getProgram(ResourceLocation loc, VertexType vertexType, RenderLayer layer); /** * Load all programs associated with this context. This might be just one, if the context is very specialized. diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java index c558213cd..6d243973a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java @@ -8,7 +8,8 @@ import com.jozufozu.flywheel.api.MaterialGroup; import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.model.ImmediateAllocator; +import com.jozufozu.flywheel.backend.RenderLayer; +import com.jozufozu.flywheel.backend.model.FallbackAllocator; import com.jozufozu.flywheel.backend.model.ModelAllocator; import com.jozufozu.flywheel.backend.model.ModelPool; import com.jozufozu.flywheel.core.Formats; @@ -36,7 +37,7 @@ public class InstancedMaterialGroup

implements MaterialG this.owner = owner; this.type = type; if (Backend.getInstance().compat.onAMDWindows()) { - this.allocator = ImmediateAllocator.INSTANCE; + this.allocator = FallbackAllocator.INSTANCE; } else { this.allocator = new ModelPool(Formats.POS_TEX_NORMAL, 2048); } @@ -52,14 +53,14 @@ public class InstancedMaterialGroup

implements MaterialG } } - public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { + public void render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { type.setupRenderState(); Textures.bindActiveTextures(); - renderAll(viewProjection, camX, camY, camZ); + renderAll(viewProjection, camX, camY, camZ, layer); type.clearRenderState(); } - protected void renderAll(Matrix4f viewProjection, double camX, double camY, double camZ) { + protected void renderAll(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { // initialize all uninitialized instancers... for (InstancedMaterial material : materials.values()) { for (GPUInstancer instancer : material.uninitialized) { @@ -78,7 +79,7 @@ public class InstancedMaterialGroup

implements MaterialG if (material.nothingToRender()) continue; P program = owner.context.getProgram(entry.getKey() - .getProgramSpec(), Formats.POS_TEX_NORMAL); + .getProgramSpec(), Formats.POS_TEX_NORMAL, layer); program.bind(); program.uploadViewProjection(viewProjection); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index 41b102cff..6d91b3563 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -95,7 +95,7 @@ public class InstancingEngine

implements Engine { viewProjection = event.viewProjection; } - getGroupsToRender(event.getLayer()).forEach(group -> group.render(viewProjection, camX, camY, camZ)); + getGroupsToRender(event.getLayer()).forEach(group -> group.render(viewProjection, camX, camY, camZ, event.getLayer())); GlBufferType.ELEMENT_ARRAY_BUFFER.unbind(); GlBufferType.ARRAY_BUFFER.unbind(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ImmediateAllocator.java b/src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java similarity index 66% rename from src/main/java/com/jozufozu/flywheel/backend/model/ImmediateAllocator.java rename to src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java index 30240d8d8..7520afff2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ImmediateAllocator.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java @@ -2,9 +2,8 @@ package com.jozufozu.flywheel.backend.model; import com.jozufozu.flywheel.core.model.Model; -public class ImmediateAllocator implements ModelAllocator { - - public static final ImmediateAllocator INSTANCE = new ImmediateAllocator(); +public enum FallbackAllocator implements ModelAllocator { + INSTANCE; @Override public BufferedModel alloc(Model model, Callback allocationCallback) { diff --git a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java index 6b3cd99b0..85be2a530 100644 --- a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java +++ b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java @@ -9,9 +9,11 @@ import java.util.stream.Stream; import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.RenderLayer; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.source.ShaderLoadingException; import com.jozufozu.flywheel.core.pipeline.CachingCompiler; +import com.jozufozu.flywheel.core.pipeline.CompilationContext; import com.jozufozu.flywheel.core.pipeline.PipelineCompiler; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; @@ -58,14 +60,14 @@ public class WorldContext

implements ShaderContext

{ } @Override - public P getProgram(ResourceLocation loc, VertexType vertexType) { + public P getProgram(ResourceLocation loc, VertexType vertexType, RenderLayer layer) { ProgramSpec spec = programs.get(loc); if (spec == null) { throw new NullPointerException("Cannot compile shader because '" + loc + "' is not recognized."); } - return programCache.getProgram(spec, vertexType); + return programCache.getProgram(CompilationContext.create(vertexType, layer, spec)); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java index 19385b5a3..ac27997f3 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.core.crumbling; +import com.jozufozu.flywheel.backend.RenderLayer; import com.jozufozu.flywheel.backend.instancing.instancing.InstancedMaterialGroup; import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine; import com.jozufozu.flywheel.util.Textures; @@ -18,7 +19,7 @@ public class CrumblingGroup

extends InstancedMateria } @Override - public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { + public void render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { type.setupRenderState(); int renderTex = RenderSystem.getShaderTexture(0); @@ -35,7 +36,7 @@ public class CrumblingGroup

extends InstancedMateria RenderSystem.setShaderTexture(4, breakingTex); Textures.bindActiveTextures(); - renderAll(viewProjection, camX, camY, camZ); + renderAll(viewProjection, camX, camY, camZ, layer); CrumblingRenderer._currentLayer.clearRenderState(); } diff --git a/src/main/java/com/jozufozu/flywheel/core/pipeline/CachingCompiler.java b/src/main/java/com/jozufozu/flywheel/core/pipeline/CachingCompiler.java index 728286b90..b29b75c30 100644 --- a/src/main/java/com/jozufozu/flywheel/core/pipeline/CachingCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/core/pipeline/CachingCompiler.java @@ -3,9 +3,7 @@ package com.jozufozu.flywheel.core.pipeline; import java.util.HashMap; import java.util.Map; -import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; /** * Lazily compiles shader programs, caching the results. @@ -23,12 +21,11 @@ public class CachingCompiler

{ /** * Get or compile a spec to the given vertex type, accounting for all game state conditions specified by the spec. * - * @param spec The ProgramSpec to target. - * @param vertexType The VertexType to target. + * @param context The context of compilation. * @return A compiled GlProgram. */ - public P getProgram(ProgramSpec spec, VertexType vertexType) { - return cache.computeIfAbsent(new CompilationContext(vertexType, spec, spec.getCurrentStateID()), this.pipeline::compile); + public P getProgram(CompilationContext context) { + return cache.computeIfAbsent(context, this.pipeline::compile); } public void invalidate() { diff --git a/src/main/java/com/jozufozu/flywheel/core/pipeline/CompilationContext.java b/src/main/java/com/jozufozu/flywheel/core/pipeline/CompilationContext.java index b05da4d35..bc4469574 100644 --- a/src/main/java/com/jozufozu/flywheel/core/pipeline/CompilationContext.java +++ b/src/main/java/com/jozufozu/flywheel/core/pipeline/CompilationContext.java @@ -3,17 +3,23 @@ package com.jozufozu.flywheel.core.pipeline; import java.util.Objects; import com.jozufozu.flywheel.api.vertex.VertexType; +import com.jozufozu.flywheel.backend.RenderLayer; import com.jozufozu.flywheel.backend.source.SourceFile; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; /** * Represents the entire context of a program's usage. * + * @param layer * @param vertexType The vertexType the program should be adapted for. * @param spec The generic program name. * @param ctx An ID representing the state at the time of usage. */ -public record CompilationContext(VertexType vertexType, ProgramSpec spec, long ctx) { +public record CompilationContext(RenderLayer layer, VertexType vertexType, ProgramSpec spec, long ctx) { + + public static CompilationContext create(VertexType vertexType, RenderLayer layer, ProgramSpec spec) { + return new CompilationContext(layer, vertexType, spec, spec.getCurrentStateID()); + } public SourceFile getFile() { return spec().getSource(); @@ -25,11 +31,11 @@ public record CompilationContext(VertexType vertexType, ProgramSpec spec, long c if (o == null || getClass() != o.getClass()) return false; CompilationContext that = (CompilationContext) o; // override for instance equality on vertexType - return ctx == that.ctx && vertexType == that.vertexType && spec.equals(that.spec); + return layer == that.layer && ctx == that.ctx && vertexType == that.vertexType && spec.equals(that.spec); } @Override public int hashCode() { - return Objects.hash(vertexType, spec, ctx); + return Objects.hash(layer, vertexType, spec, ctx); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/pipeline/ShaderCompiler.java b/src/main/java/com/jozufozu/flywheel/core/pipeline/ShaderCompiler.java index 11a451a4f..c2da3e63a 100644 --- a/src/main/java/com/jozufozu/flywheel/core/pipeline/ShaderCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/core/pipeline/ShaderCompiler.java @@ -7,6 +7,7 @@ import javax.annotation.Nullable; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.RenderLayer; import com.jozufozu.flywheel.backend.gl.shader.GlShader; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; import com.jozufozu.flywheel.backend.source.FileResolution; @@ -27,20 +28,22 @@ public class ShaderCompiler { private final List defines; + private final RenderLayer layer; public final VertexType vertexType; public final SourceFile mainFile; private final List files = new ArrayList<>(); - public ShaderCompiler(CompilationContext usage, Template template, FileResolution header) { - this.name = usage.spec().name; + public ShaderCompiler(CompilationContext context, Template template, FileResolution header) { + this.name = context.spec().name; this.template = template; this.header = header; - this.mainFile = usage.getFile(); - this.defines = usage.spec() - .getDefines(usage.ctx()); - this.vertexType = usage.vertexType(); + this.mainFile = context.getFile(); + this.defines = context.spec() + .getDefines(context.ctx()); + this.vertexType = context.vertexType(); + layer = context.layer(); } public GlShader compile(ShaderType type) { @@ -56,6 +59,10 @@ public class ShaderCompiler { .append(type.define) // special case shader type declaration .append('\n'); + if (layer == RenderLayer.CUTOUT) { + finalSource.append("#define ALPHA_DISCARD 0.1\n"); + } + for (String def : defines) { finalSource.append("#define ") .append(def) diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl index 58ad850b6..28e36ab3d 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl @@ -21,8 +21,6 @@ vec4 FLWVertex(inout Vertex v) { #elif defined(FRAGMENT_SHADER) #use "flywheel:core/lightutil.glsl" - -#define ALPHA_DISCARD 0.1 // optimize discard usage #if defined(ALPHA_DISCARD) #if defined(GL_ARB_conservative_depth)