Consider RenderLayer when compiling shaders

- Disabled cutout logic in other layers
This commit is contained in:
Jozufozu 2022-01-01 15:19:49 -08:00
parent 1f5f2a5476
commit c196ed5d78
10 changed files with 43 additions and 32 deletions

View file

@ -7,7 +7,7 @@ import net.minecraft.resources.ResourceLocation;
public interface ShaderContext<P extends GlProgram> { public interface ShaderContext<P extends GlProgram> {
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. * Load all programs associated with this context. This might be just one, if the context is very specialized.

View file

@ -8,7 +8,8 @@ import com.jozufozu.flywheel.api.MaterialGroup;
import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.Instanced;
import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.backend.Backend; 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.ModelAllocator;
import com.jozufozu.flywheel.backend.model.ModelPool; import com.jozufozu.flywheel.backend.model.ModelPool;
import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.Formats;
@ -36,7 +37,7 @@ public class InstancedMaterialGroup<P extends WorldProgram> implements MaterialG
this.owner = owner; this.owner = owner;
this.type = type; this.type = type;
if (Backend.getInstance().compat.onAMDWindows()) { if (Backend.getInstance().compat.onAMDWindows()) {
this.allocator = ImmediateAllocator.INSTANCE; this.allocator = FallbackAllocator.INSTANCE;
} else { } else {
this.allocator = new ModelPool(Formats.POS_TEX_NORMAL, 2048); this.allocator = new ModelPool(Formats.POS_TEX_NORMAL, 2048);
} }
@ -52,14 +53,14 @@ public class InstancedMaterialGroup<P extends WorldProgram> 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(); type.setupRenderState();
Textures.bindActiveTextures(); Textures.bindActiveTextures();
renderAll(viewProjection, camX, camY, camZ); renderAll(viewProjection, camX, camY, camZ, layer);
type.clearRenderState(); 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... // initialize all uninitialized instancers...
for (InstancedMaterial<?> material : materials.values()) { for (InstancedMaterial<?> material : materials.values()) {
for (GPUInstancer<?> instancer : material.uninitialized) { for (GPUInstancer<?> instancer : material.uninitialized) {
@ -78,7 +79,7 @@ public class InstancedMaterialGroup<P extends WorldProgram> implements MaterialG
if (material.nothingToRender()) continue; if (material.nothingToRender()) continue;
P program = owner.context.getProgram(entry.getKey() P program = owner.context.getProgram(entry.getKey()
.getProgramSpec(), Formats.POS_TEX_NORMAL); .getProgramSpec(), Formats.POS_TEX_NORMAL, layer);
program.bind(); program.bind();
program.uploadViewProjection(viewProjection); program.uploadViewProjection(viewProjection);

View file

@ -95,7 +95,7 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
viewProjection = event.viewProjection; 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.ELEMENT_ARRAY_BUFFER.unbind();
GlBufferType.ARRAY_BUFFER.unbind(); GlBufferType.ARRAY_BUFFER.unbind();

View file

@ -2,9 +2,8 @@ package com.jozufozu.flywheel.backend.model;
import com.jozufozu.flywheel.core.model.Model; import com.jozufozu.flywheel.core.model.Model;
public class ImmediateAllocator implements ModelAllocator { public enum FallbackAllocator implements ModelAllocator {
INSTANCE;
public static final ImmediateAllocator INSTANCE = new ImmediateAllocator();
@Override @Override
public BufferedModel alloc(Model model, Callback allocationCallback) { public BufferedModel alloc(Model model, Callback allocationCallback) {

View file

@ -9,9 +9,11 @@ import java.util.stream.Stream;
import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.Instanced;
import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.RenderLayer;
import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderContext;
import com.jozufozu.flywheel.backend.source.ShaderLoadingException; import com.jozufozu.flywheel.backend.source.ShaderLoadingException;
import com.jozufozu.flywheel.core.pipeline.CachingCompiler; 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.pipeline.PipelineCompiler;
import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.core.shader.WorldProgram;
import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec;
@ -58,14 +60,14 @@ public class WorldContext<P extends WorldProgram> implements ShaderContext<P> {
} }
@Override @Override
public P getProgram(ResourceLocation loc, VertexType vertexType) { public P getProgram(ResourceLocation loc, VertexType vertexType, RenderLayer layer) {
ProgramSpec spec = programs.get(loc); ProgramSpec spec = programs.get(loc);
if (spec == null) { if (spec == null) {
throw new NullPointerException("Cannot compile shader because '" + loc + "' is not recognized."); 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 @Override

View file

@ -1,5 +1,6 @@
package com.jozufozu.flywheel.core.crumbling; 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.InstancedMaterialGroup;
import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine; import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine;
import com.jozufozu.flywheel.util.Textures; import com.jozufozu.flywheel.util.Textures;
@ -18,7 +19,7 @@ public class CrumblingGroup<P extends CrumblingProgram> extends InstancedMateria
} }
@Override @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(); type.setupRenderState();
int renderTex = RenderSystem.getShaderTexture(0); int renderTex = RenderSystem.getShaderTexture(0);
@ -35,7 +36,7 @@ public class CrumblingGroup<P extends CrumblingProgram> extends InstancedMateria
RenderSystem.setShaderTexture(4, breakingTex); RenderSystem.setShaderTexture(4, breakingTex);
Textures.bindActiveTextures(); Textures.bindActiveTextures();
renderAll(viewProjection, camX, camY, camZ); renderAll(viewProjection, camX, camY, camZ, layer);
CrumblingRenderer._currentLayer.clearRenderState(); CrumblingRenderer._currentLayer.clearRenderState();
} }

View file

@ -3,9 +3,7 @@ package com.jozufozu.flywheel.core.pipeline;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
import com.jozufozu.flywheel.core.shader.spec.ProgramSpec;
/** /**
* Lazily compiles shader programs, caching the results. * Lazily compiles shader programs, caching the results.
@ -23,12 +21,11 @@ public class CachingCompiler<P extends GlProgram> {
/** /**
* Get or compile a spec to the given vertex type, accounting for all game state conditions specified by the spec. * 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 context The context of compilation.
* @param vertexType The VertexType to target.
* @return A compiled GlProgram. * @return A compiled GlProgram.
*/ */
public P getProgram(ProgramSpec spec, VertexType vertexType) { public P getProgram(CompilationContext context) {
return cache.computeIfAbsent(new CompilationContext(vertexType, spec, spec.getCurrentStateID()), this.pipeline::compile); return cache.computeIfAbsent(context, this.pipeline::compile);
} }
public void invalidate() { public void invalidate() {

View file

@ -3,17 +3,23 @@ package com.jozufozu.flywheel.core.pipeline;
import java.util.Objects; import java.util.Objects;
import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.RenderLayer;
import com.jozufozu.flywheel.backend.source.SourceFile; import com.jozufozu.flywheel.backend.source.SourceFile;
import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec;
/** /**
* Represents the entire context of a program's usage. * Represents the entire context of a program's usage.
* *
* @param layer
* @param vertexType The vertexType the program should be adapted for. * @param vertexType The vertexType the program should be adapted for.
* @param spec The generic program name. * @param spec The generic program name.
* @param ctx An ID representing the state at the time of usage. * @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() { public SourceFile getFile() {
return spec().getSource(); return spec().getSource();
@ -25,11 +31,11 @@ public record CompilationContext(VertexType vertexType, ProgramSpec spec, long c
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
CompilationContext that = (CompilationContext) o; CompilationContext that = (CompilationContext) o;
// override for instance equality on vertexType // 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 @Override
public int hashCode() { public int hashCode() {
return Objects.hash(vertexType, spec, ctx); return Objects.hash(layer, vertexType, spec, ctx);
} }
} }

View file

@ -7,6 +7,7 @@ import javax.annotation.Nullable;
import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.Backend; 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.GlShader;
import com.jozufozu.flywheel.backend.gl.shader.ShaderType; import com.jozufozu.flywheel.backend.gl.shader.ShaderType;
import com.jozufozu.flywheel.backend.source.FileResolution; import com.jozufozu.flywheel.backend.source.FileResolution;
@ -27,20 +28,22 @@ public class ShaderCompiler {
private final List<String> defines; private final List<String> defines;
private final RenderLayer layer;
public final VertexType vertexType; public final VertexType vertexType;
public final SourceFile mainFile; public final SourceFile mainFile;
private final List<SourceFile> files = new ArrayList<>(); private final List<SourceFile> files = new ArrayList<>();
public ShaderCompiler(CompilationContext usage, Template<?> template, FileResolution header) { public ShaderCompiler(CompilationContext context, Template<?> template, FileResolution header) {
this.name = usage.spec().name; this.name = context.spec().name;
this.template = template; this.template = template;
this.header = header; this.header = header;
this.mainFile = usage.getFile(); this.mainFile = context.getFile();
this.defines = usage.spec() this.defines = context.spec()
.getDefines(usage.ctx()); .getDefines(context.ctx());
this.vertexType = usage.vertexType(); this.vertexType = context.vertexType();
layer = context.layer();
} }
public GlShader compile(ShaderType type) { public GlShader compile(ShaderType type) {
@ -56,6 +59,10 @@ public class ShaderCompiler {
.append(type.define) // special case shader type declaration .append(type.define) // special case shader type declaration
.append('\n'); .append('\n');
if (layer == RenderLayer.CUTOUT) {
finalSource.append("#define ALPHA_DISCARD 0.1\n");
}
for (String def : defines) { for (String def : defines) {
finalSource.append("#define ") finalSource.append("#define ")
.append(def) .append(def)

View file

@ -21,8 +21,6 @@ vec4 FLWVertex(inout Vertex v) {
#elif defined(FRAGMENT_SHADER) #elif defined(FRAGMENT_SHADER)
#use "flywheel:core/lightutil.glsl" #use "flywheel:core/lightutil.glsl"
#define ALPHA_DISCARD 0.1
// optimize discard usage // optimize discard usage
#if defined(ALPHA_DISCARD) #if defined(ALPHA_DISCARD)
#if defined(GL_ARB_conservative_depth) #if defined(GL_ARB_conservative_depth)