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