Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/1.20/dev' into feat/multi-load…
Browse files Browse the repository at this point in the history
…er-1.21

# Conflicts:
#	forge/src/main/java/dev/engine_room/flywheel/impl/FlywheelForge.java
#	forge/src/main/java/dev/engine_room/flywheel/impl/ForgeFlwConfig.java
  • Loading branch information
IThundxr committed Aug 23, 2024
2 parents a932d5d + 1a8ed8d commit 0919c0f
Show file tree
Hide file tree
Showing 55 changed files with 633 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public interface Backend {
Engine createEngine(LevelAccessor level);

/**
* Get a fallback backend in case this backend is not supported.
* The priority of this backend.
* <p>The backend with the highest priority upon first launch will be chosen as the default backend.
*
* <p>If the selected backend becomes unavailable for whatever reason, the next supported backend
* with a LOWER priority than the selected one will be chosen.
*
* @return The priority of this backend.
*/
Backend findFallback();
int priority();

/**
* Check if this backend is supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ public interface BackendConfig {
/**
* How smooth/accurate our flw_light impl is.
*
* <p>This makes more sense here as a backend-specific config because it's tightly coupled to
* our backend's implementation. 3rd party backend may have different approaches and configurations.
*
* @return The current light smoothness setting.
*/
LightSmoothness lightSmoothness();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class Backends {
*/
public static final Backend INSTANCING = SimpleBackend.builder()
.engineFactory(level -> new EngineImpl(level, new InstancedDrawManager(InstancingPrograms.get()), 256))
.priority(500)
.supported(() -> GlCompat.SUPPORTS_INSTANCING && InstancingPrograms.allLoaded() && !ShadersModHandler.isShaderPackInUse())
.register(Flywheel.rl("instancing"));

Expand All @@ -25,7 +26,7 @@ public final class Backends {
*/
public static final Backend INDIRECT = SimpleBackend.builder()
.engineFactory(level -> new EngineImpl(level, new IndirectDrawManager(IndirectPrograms.get()), 256))
.fallback(() -> Backends.INSTANCING)
.priority(1000)
.supported(() -> GlCompat.SUPPORTS_INDIRECT && IndirectPrograms.allLoaded() && !ShadersModHandler.isShaderPackInUse())
.register(Flywheel.rl("indirect"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import dev.engine_room.flywheel.api.material.CutoutShader;
import dev.engine_room.flywheel.api.material.FogShader;
import dev.engine_room.flywheel.api.material.LightShader;
import dev.engine_room.flywheel.api.material.MaterialShaders;
import dev.engine_room.flywheel.api.registry.Registry;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
Expand All @@ -26,8 +25,6 @@ public final class MaterialShaderIndices {
private static Index fogSources;
@Nullable
private static Index cutoutSources;
@Nullable
private static Index lightSources;

private MaterialShaderIndices() {
}
Expand Down Expand Up @@ -60,13 +57,6 @@ public static Index cutoutSources() {
return cutoutSources;
}

public static Index lightSources() {
if (lightSources == null) {
lightSources = indexFromRegistry(LightShader.REGISTRY, LightShader::source);
}
return lightSources;
}

public static int vertexIndex(MaterialShaders shaders) {
return vertexSources().index(shaders.vertexSource());
}
Expand All @@ -83,10 +73,6 @@ public static int cutoutIndex(CutoutShader cutoutShader) {
return cutoutSources().index(cutoutShader.source());
}

public static int lightIndex(LightShader lightShader) {
return lightSources().index(lightShader.source());
}

private static <T> Index indexFromRegistry(Registry<T> registry, Function<T, ResourceLocation> sourceFunc) {
if (!registry.isFrozen()) {
throw new IllegalStateException("Cannot create index from registry that is not frozen!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.material.LightShader;
import dev.engine_room.flywheel.backend.MaterialShaderIndices;
import dev.engine_room.flywheel.backend.compile.component.UberShaderComponent;
import dev.engine_room.flywheel.backend.compile.core.CompilerStats;
Expand Down Expand Up @@ -46,16 +47,15 @@ static void reload(ResourceManager resourceManager) {
var fragmentMaterialComponent = createFragmentMaterialComponent(loader);
var fogComponent = createFogComponent(loader);
var cutoutComponent = createCutoutComponent(loader);
var lightComponent = createLightComponent(loader);

if (stats.errored() || vertexComponentsHeader == null || fragmentComponentsHeader == null || vertexMaterialComponent == null || fragmentMaterialComponent == null || fogComponent == null || cutoutComponent == null || lightComponent == null) {
if (stats.errored() || vertexComponentsHeader == null || fragmentComponentsHeader == null || vertexMaterialComponent == null || fragmentMaterialComponent == null || fogComponent == null || cutoutComponent == null) {
// Probably means the shader sources are missing.
stats.emitErrorLog();
return;
}

List<SourceComponent> vertexComponents = List.of(vertexComponentsHeader, vertexMaterialComponent);
List<SourceComponent> fragmentComponents = List.of(fragmentComponentsHeader, fragmentMaterialComponent, fogComponent, cutoutComponent, lightComponent);
List<SourceComponent> fragmentComponents = List.of(fragmentComponentsHeader, fragmentMaterialComponent, fogComponent, cutoutComponent);

var pipelineKeys = createPipelineKeys();
InstancingPrograms.reload(sources, pipelineKeys, vertexComponents, fragmentComponents);
Expand All @@ -66,7 +66,9 @@ private static ImmutableList<PipelineProgramKey> createPipelineKeys() {
ImmutableList.Builder<PipelineProgramKey> builder = ImmutableList.builder();
for (ContextShader contextShader : ContextShader.values()) {
for (InstanceType<?> instanceType : InstanceType.REGISTRY) {
builder.add(new PipelineProgramKey(instanceType, contextShader));
for (LightShader light : LightShader.REGISTRY.getAll()) {
builder.add(new PipelineProgramKey(instanceType, contextShader, light));
}
}
}
return builder.build();
Expand Down Expand Up @@ -119,18 +121,4 @@ private static UberShaderComponent createCutoutComponent(SourceLoader loader) {
.switchOn(GlslExpr.variable("_flw_uberCutoutIndex"))
.build(loader);
}

// TODO: Do not uber this component. Shader compile times are very high now
@Nullable
private static UberShaderComponent createLightComponent(SourceLoader loader) {
return UberShaderComponent.builder(Flywheel.rl("light"))
.materialSources(MaterialShaderIndices.lightSources()
.all())
.adapt(FnSignature.create()
.returnType("void")
.name("flw_shaderLight")
.build())
.switchOn(GlslExpr.variable("_flw_uberLightIndex"))
.build(loader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.material.LightShader;
import dev.engine_room.flywheel.backend.compile.component.InstanceStructComponent;
import dev.engine_room.flywheel.backend.compile.component.SsboInstanceComponent;
import dev.engine_room.flywheel.backend.compile.core.CompilationHarness;
Expand Down Expand Up @@ -167,8 +168,8 @@ public static boolean allLoaded() {
return instance != null;
}

public GlProgram getIndirectProgram(InstanceType<?> instanceType, ContextShader contextShader) {
return pipeline.get(new PipelineProgramKey(instanceType, contextShader));
public GlProgram getIndirectProgram(InstanceType<?> instanceType, ContextShader contextShader, LightShader light) {
return pipeline.get(new PipelineProgramKey(instanceType, contextShader, light));
}

public GlProgram getCullingProgram(InstanceType<?> instanceType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.collect.ImmutableList;

import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.material.LightShader;
import dev.engine_room.flywheel.backend.gl.GlCompat;
import dev.engine_room.flywheel.backend.gl.shader.GlProgram;
import dev.engine_room.flywheel.backend.glsl.GlslVersion;
Expand Down Expand Up @@ -78,8 +79,8 @@ public static boolean allLoaded() {
return instance != null;
}

public GlProgram get(InstanceType<?> instanceType, ContextShader contextShader) {
return pipeline.get(new PipelineProgramKey(instanceType, contextShader));
public GlProgram get(InstanceType<?> instanceType, ContextShader contextShader, LightShader light) {
return pipeline.get(new PipelineProgramKey(instanceType, contextShader, light));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ static CompilationHarness<PipelineProgramKey> create(ShaderSources sources, Pipe
.nameMapper(key -> {
var context = key.contextShader()
.nameLowerCase();
return "pipeline/" + pipeline.compilerMarker() + "/" + context;
return "pipeline/" + pipeline.compilerMarker() + "/" + ResourceUtil.toDebugFileNameNoExtension(key.light()
.source()) + "_" + context;
})
.requireExtensions(extensions)
.enableExtension("GL_ARB_conservative_depth")
Expand All @@ -65,6 +66,8 @@ static CompilationHarness<PipelineProgramKey> create(ShaderSources sources, Pipe
.onCompile((key, comp) -> lightSmoothness.onCompile(comp))
.withResource(API_IMPL_FRAG)
.withComponents(fragmentComponents)
.withResource(key -> key.light()
.source())
.withResource(pipeline.fragmentMain()))
.preLink((key, program) -> {
program.bindAttribLocation("_flw_aPos", 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package dev.engine_room.flywheel.backend.compile;

import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.material.LightShader;

/**
* Represents the entire context of a program's usage.
*
* @param instanceType The instance shader to use.
* @param contextShader The context shader to use.
* @param light The light shader to use.
*/
public record PipelineProgramKey(InstanceType<?> instanceType, ContextShader contextShader) {
public record PipelineProgramKey(InstanceType<?> instanceType, ContextShader contextShader, LightShader light) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public void delete() {
public int capacity() {
return top;
}

public long byteCapacity() {
return memoryBlock.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.engine_room.flywheel.api.visualization.VisualType;
import dev.engine_room.flywheel.backend.FlwBackend;
import dev.engine_room.flywheel.backend.engine.embed.Environment;
import dev.engine_room.flywheel.backend.engine.embed.EnvironmentStorage;
import dev.engine_room.flywheel.lib.util.Pair;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
Expand All @@ -40,7 +41,7 @@ public <I extends Instance> Instancer<I> getInstancer(Environment environment, I
return (Instancer<I>) instancers.computeIfAbsent(new InstancerKey<>(environment, type, model, visualType, bias), this::createAndDeferInit);
}

public void flush(LightStorage lightStorage) {
public void flush(LightStorage lightStorage, EnvironmentStorage environmentStorage) {
// Thread safety: flush is called from the render thread after all visual updates have been made,
// so there are no:tm: threads we could be racing with.
for (var instancer : initializationQueue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void setupRender(RenderContext context) {
try (var state = GlStateTracker.getRestoreState()) {
Uniforms.update(context);
environmentStorage.flush();
drawManager.flush(lightStorage);
drawManager.flush(lightStorage, environmentStorage);
}
}

Expand All @@ -107,6 +107,7 @@ public void renderCrumbling(RenderContext context, List<CrumblingBlock> crumblin
public void delete() {
drawManager.delete();
lightStorage.delete();
environmentStorage.delete();
}

public <I extends Instance> Instancer<I> instancer(Environment environment, InstanceType<I> type, Model model, VisualType visualType, int bias) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private static int bitMask(int bitLength, int bitOffset) {
public static int packUberShader(Material material) {
var fog = MaterialShaderIndices.fogIndex(material.fog());
var cutout = MaterialShaderIndices.cutoutIndex(material.cutout());
var light = MaterialShaderIndices.lightIndex(material.light());
return (light & 0x3FF) | (cutout & 0x3FF) << 10 | (fog & 0x3FF) << 20;
return (cutout & 0xFFFF) | (fog & 0xFFFF) << 16;
}

// Packed format:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import net.minecraft.client.renderer.texture.AbstractTexture;

public final class MaterialRenderState {
public static final Comparator<Material> COMPARATOR = Comparator.comparing(Material::texture)
public static final Comparator<Material> COMPARATOR = Comparator.comparing((Material m) -> m.light()
.source())
.thenComparing(Material::texture)
.thenComparing(Material::blur)
.thenComparing(Material::mipmap)
.thenComparing(Material::backfaceCulling)
Expand Down Expand Up @@ -177,4 +179,18 @@ private static void resetWriteMask() {
RenderSystem.depthMask(true);
RenderSystem.colorMask(true, true, true, true);
}

public static boolean materialEquals(Material lhs, Material rhs) {
if (lhs == rhs) {
return true;
}

// Not here because ubershader: useLight, useOverlay, diffuse, shaders, fog shader, and cutout shader
// Everything in the comparator should be here.
return lhs.blur() == rhs.blur() && lhs.mipmap() == rhs.mipmap() && lhs.backfaceCulling() == rhs.backfaceCulling() && lhs.polygonOffset() == rhs.polygonOffset() && lhs.light()
.source()
.equals(rhs.light()
.source()) && lhs.texture()
.equals(rhs.texture()) && lhs.depthTest() == rhs.depthTest() && lhs.transparency() == rhs.transparency() && lhs.writeMask() == rhs.writeMask();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.engine_room.flywheel.backend.compile.ContextShader;
import dev.engine_room.flywheel.backend.engine.EngineImpl;
import dev.engine_room.flywheel.backend.gl.shader.GlProgram;
import dev.engine_room.flywheel.lib.util.ExtraMemoryOps;
import net.minecraft.core.Vec3i;

public class EmbeddedEnvironment implements VisualEmbedding, Environment {
Expand All @@ -31,6 +32,8 @@ public class EmbeddedEnvironment implements VisualEmbedding, Environment {
private final Matrix4f poseComposed = new Matrix4f();
private final Matrix3f normalComposed = new Matrix3f();

public int matrixIndex = 0;

private boolean deleted = false;

public EmbeddedEnvironment(EngineImpl engine, VisualType visualType, Vec3i renderOrigin, @Nullable EmbeddedEnvironment parent) {
Expand Down Expand Up @@ -82,22 +85,24 @@ public ContextShader contextShader() {
}

@Override
public void setupCull(GlProgram program) {
program.setBool(EmbeddingUniforms.USE_MODEL_MATRIX, true);
public void setupDraw(GlProgram program) {
program.setMat4(EmbeddingUniforms.MODEL_MATRIX, poseComposed);
program.setMat3(EmbeddingUniforms.NORMAL_MATRIX, normalComposed);
}

@Override
public void setupDraw(GlProgram program) {
program.setMat4(EmbeddingUniforms.MODEL_MATRIX, poseComposed);
program.setMat3(EmbeddingUniforms.NORMAL_MATRIX, normalComposed);
public int matrixIndex() {
return matrixIndex;
}

public void flush() {
public void flush(long ptr) {
poseComposed.identity();
normalComposed.identity();

composeMatrices(poseComposed, normalComposed);

ExtraMemoryOps.putMatrix4f(ptr, poseComposed);
ExtraMemoryOps.putMatrix3fPadded(ptr + 16 * Float.BYTES, normalComposed);
}

private void composeMatrices(Matrix4f pose, Matrix3f normal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package dev.engine_room.flywheel.backend.engine.embed;

public final class EmbeddingUniforms {
/**
* Only used by cull shaders.
*/
public static final String USE_MODEL_MATRIX = "_flw_useModelMatrix";
public static final String MODEL_MATRIX = "_flw_modelMatrix";
public static final String NORMAL_MATRIX = "_flw_normalMatrix";
public static final String MODEL_MATRIX = "_flw_modelMatrixUniform";
public static final String NORMAL_MATRIX = "_flw_normalMatrixUniform";

private EmbeddingUniforms() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface Environment {
ContextShader contextShader();

void setupCull(GlProgram cullProgram);

void setupDraw(GlProgram drawProgram);

int matrixIndex();
}
Loading

0 comments on commit 0919c0f

Please sign in to comment.