Stay inline

- Inline all unnecessary context stuff
- Move ContextShaders into backend.compile
- Instancers are parameterized by a nullable VisualEmbedding
  - I don't like this, but the code currently compiles, so I want a
    checkpoint before I try my next idea
This commit is contained in:
Jozufozu 2024-02-26 14:50:58 -08:00
parent e55d506511
commit 2c3b4c54ca
35 changed files with 159 additions and 369 deletions

View file

@ -11,7 +11,6 @@ import com.jozufozu.flywheel.api.visualization.VisualizationManager;
import com.jozufozu.flywheel.backend.Backends;
import com.jozufozu.flywheel.backend.ShaderIndices;
import com.jozufozu.flywheel.backend.compile.FlwPrograms;
import com.jozufozu.flywheel.backend.context.ContextShaders;
import com.jozufozu.flywheel.backend.engine.uniform.Uniforms;
import com.jozufozu.flywheel.config.BackendArgument;
import com.jozufozu.flywheel.config.FlwCommands;
@ -128,7 +127,6 @@ public class Flywheel {
CutoutShaders.init();
FogShaders.init();
StandardMaterialShaders.init();
ContextShaders.init();
ShaderIndices.init();

View file

@ -12,7 +12,7 @@ import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.api.task.Plan;
import com.jozufozu.flywheel.api.task.TaskExecutor;
import com.jozufozu.flywheel.api.visualization.EmbeddedLevel;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import net.minecraft.client.Camera;
import net.minecraft.core.BlockPos;
@ -34,7 +34,7 @@ public interface Engine {
*/
<I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model, RenderStage stage);
<I extends Instance> Instancer<I> instancer(EmbeddedLevel world, InstanceType<I> type, Model model, RenderStage stage);
<I extends Instance> Instancer<I> instancer(VisualEmbedding world, InstanceType<I> type, Model model, RenderStage stage);
/**
* Create a plan that will be executed every frame.

View file

@ -1,11 +0,0 @@
package com.jozufozu.flywheel.api.visualization;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.phys.AABB;
public interface EmbeddedLevel {
void transform(PoseStack stack);
AABB boundingBox();
}

View file

@ -0,0 +1,14 @@
package com.jozufozu.flywheel.api.visualization;
import org.joml.Matrix3fc;
import org.joml.Matrix4fc;
import net.minecraft.world.phys.AABB;
public interface VisualEmbedding {
Matrix4fc pose();
Matrix3fc normal();
AABB boundingBox();
}

View file

@ -3,7 +3,6 @@ package com.jozufozu.flywheel.api.visualization;
import org.jetbrains.annotations.ApiStatus;
import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.backend.context.Context;
import net.minecraft.core.Vec3i;
@ -24,13 +23,6 @@ public interface VisualizationContext {
*/
Vec3i renderOrigin();
/**
* Create a new {@link VisualizationContext} with the given {@link Context} and render origin.
*
* @param context The new context.
* @param renderOrigin The new render origin.
* @return A new {@link VisualizationContext} for use with child visuals.
*/
@ApiStatus.Experimental
VisualizationContext embed(EmbeddedLevel world);
VisualizationContext embed(VisualEmbedding world);
}

View file

@ -0,0 +1,6 @@
package com.jozufozu.flywheel.backend.compile;
import net.minecraft.resources.ResourceLocation;
public record ContextShader(ResourceLocation vertexShader, ResourceLocation fragmentShader) {
}

View file

@ -0,0 +1,23 @@
package com.jozufozu.flywheel.backend.compile;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.internal.InternalFlywheelApi;
import com.jozufozu.flywheel.api.registry.Registry;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
public class ContextShaders {
public static final Registry<ContextShader> REGISTRY = InternalFlywheelApi.INSTANCE.createRegistry();
public static final ContextShader DEFAULT = REGISTRY.registerAndGet(new ContextShader(Flywheel.rl("internal/context/default.vert"), Flywheel.rl("internal/context/default.frag")));
public static final ContextShader CRUMBLING = REGISTRY.registerAndGet(new ContextShader(Flywheel.rl("internal/context/crumbling.vert"), Flywheel.rl("internal/context/crumbling.frag")));
public static final ContextShader EMBEDDED = REGISTRY.registerAndGet(new ContextShader(Flywheel.rl("internal/context/embedded.vert"), Flywheel.rl("internal/context/embedded.frag")));
public static ContextShader forEmbedding(@Nullable VisualEmbedding level) {
if (level == null) {
return DEFAULT;
} else {
return EMBEDDED;
}
}
}

View file

@ -13,7 +13,6 @@ import com.jozufozu.flywheel.backend.ShaderIndices;
import com.jozufozu.flywheel.backend.compile.component.UberShaderComponent;
import com.jozufozu.flywheel.backend.compile.core.CompilerStats;
import com.jozufozu.flywheel.backend.compile.core.SourceLoader;
import com.jozufozu.flywheel.backend.context.ContextShader;
import com.jozufozu.flywheel.backend.glsl.ShaderSources;
import com.jozufozu.flywheel.backend.glsl.SourceComponent;
import com.jozufozu.flywheel.backend.glsl.generate.FnSignature;
@ -58,7 +57,7 @@ public final class FlwPrograms {
private static ImmutableList<PipelineProgramKey> createPipelineKeys() {
ImmutableList.Builder<PipelineProgramKey> builder = ImmutableList.builder();
for (ContextShader contextShader : ContextShader.REGISTRY) {
for (ContextShader contextShader : ContextShaders.REGISTRY) {
for (InstanceType<?> instanceType : InstanceType.REGISTRY) {
builder.add(new PipelineProgramKey(instanceType, contextShader));
}

View file

@ -11,7 +11,6 @@ import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.backend.compile.component.IndirectComponent;
import com.jozufozu.flywheel.backend.compile.core.CompilationHarness;
import com.jozufozu.flywheel.backend.compile.core.Compile;
import com.jozufozu.flywheel.backend.context.ContextShader;
import com.jozufozu.flywheel.backend.gl.GlCompat;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
import com.jozufozu.flywheel.backend.gl.shader.ShaderType;

View file

@ -7,7 +7,6 @@ import org.jetbrains.annotations.Nullable;
import com.google.common.collect.ImmutableList;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.backend.context.ContextShader;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
import com.jozufozu.flywheel.backend.glsl.ShaderSources;
import com.jozufozu.flywheel.backend.glsl.SourceComponent;

View file

@ -1,7 +1,6 @@
package com.jozufozu.flywheel.backend.compile;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.backend.context.ContextShader;
/**
* Represents the entire context of a program's usage.

View file

@ -1,18 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.backend.engine.textures.TextureSource;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
public interface Context {
ContextShader contextShader();
/**
* Prepare the shader for rendering with the given material and textures.
*
* @param material The material about to be rendered.
* @param shader The shader to prepare.
* @param textureSource Source of the textures to use.
*/
void prepare(Material material, GlProgram shader, TextureSource textureSource);
}

View file

@ -1,14 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import com.jozufozu.flywheel.api.internal.InternalFlywheelApi;
import com.jozufozu.flywheel.api.registry.Registry;
import net.minecraft.resources.ResourceLocation;
public interface ContextShader {
static Registry<ContextShader> REGISTRY = InternalFlywheelApi.INSTANCE.createRegistry();
ResourceLocation vertexShader();
ResourceLocation fragmentShader();
}

View file

@ -1,18 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import org.jetbrains.annotations.ApiStatus;
import com.jozufozu.flywheel.Flywheel;
public class ContextShaders {
public static final SimpleContextShader DEFAULT = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("internal/context/default.vert"), Flywheel.rl("internal/context/default.frag")));
public static final SimpleContextShader CRUMBLING = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("internal/context/crumbling.vert"), Flywheel.rl("internal/context/crumbling.frag")));
public static final SimpleContextShader EMBEDDED = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("internal/context/embedded.vert"), Flywheel.rl("internal/context/embedded.frag")));
private ContextShaders() {
}
@ApiStatus.Internal
public static void init() {
}
}

View file

@ -1,9 +0,0 @@
package com.jozufozu.flywheel.backend.context;
public final class Contexts {
public static final Context DEFAULT = SimpleContext.builder(ContextShaders.DEFAULT)
.build();
private Contexts() {
}
}

View file

@ -1,33 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.visualization.EmbeddedLevel;
import com.jozufozu.flywheel.backend.engine.textures.TextureSource;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
import com.mojang.blaze3d.vertex.PoseStack;
public class EmbeddedContext implements Context {
private final EmbeddedLevel world;
public EmbeddedContext(EmbeddedLevel world) {
this.world = world;
}
@Override
public ContextShader contextShader() {
return ContextShaders.EMBEDDED;
}
@Override
public void prepare(Material material, GlProgram shader, TextureSource textureSource) {
var stack = new PoseStack();
world.transform(stack);
// shader.setVec3("create_oneOverLightBoxSize");
// shader.setVec3("create_lightVolumeMin");
shader.setMat4("_flw_model", stack.last()
.pose());
shader.setMat3("_flw_normal", stack.last()
.normal());
}
}

View file

@ -1,59 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.backend.engine.textures.TextureSource;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
public class SimpleContext implements Context {
private final ContextShader contextShader;
private final Preparation preparation;
public SimpleContext(ContextShader contextShader, Preparation preparation) {
this.contextShader = contextShader;
this.preparation = preparation;
}
public static Builder builder(ContextShader contextShader) {
return new Builder(contextShader);
}
@Override
public ContextShader contextShader() {
return contextShader;
}
@Override
public void prepare(Material material, GlProgram shader, TextureSource textureSource) {
preparation.prepare(material, shader, textureSource);
}
@FunctionalInterface
public interface Preparation {
void prepare(Material material, GlProgram shader, TextureSource textureSource);
}
public static class Builder {
private final ContextShader contextShader;
@Nullable
private Preparation preparation;
public Builder(ContextShader contextShader) {
this.contextShader = contextShader;
}
public Builder preparation(Preparation preparation) {
this.preparation = preparation;
return this;
}
public SimpleContext build() {
if (preparation == null) {
preparation = (material, shader, textureSource) -> {
};
}
return new SimpleContext(contextShader, preparation);
}
}
}

View file

@ -1,16 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import net.minecraft.resources.ResourceLocation;
public record SimpleContextShader(ResourceLocation vertexShader,
ResourceLocation fragmentShader) implements ContextShader {
@Override
public ResourceLocation vertexShader() {
return vertexShader;
}
@Override
public ResourceLocation fragmentShader() {
return fragmentShader;
}
}

View file

@ -1,8 +0,0 @@
package com.jozufozu.flywheel.backend.context;
import com.jozufozu.flywheel.api.BackendImplemented;
@BackendImplemented
public interface Texture {
void filter(boolean blur, boolean mipmap);
}

View file

@ -6,9 +6,7 @@ import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.Instancer;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.api.visualization.EmbeddedLevel;
import com.jozufozu.flywheel.backend.context.Contexts;
import com.jozufozu.flywheel.backend.context.EmbeddedContext;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import net.minecraft.client.Camera;
import net.minecraft.core.BlockPos;
@ -25,12 +23,12 @@ public abstract class AbstractEngine implements Engine {
@Override
public <I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model, RenderStage stage) {
return getStorage().getInstancer(type, Contexts.DEFAULT, model, stage);
return getStorage().getInstancer(null, type, model, stage);
}
@Override
public <I extends Instance> Instancer<I> instancer(EmbeddedLevel world, InstanceType<I> type, Model model, RenderStage stage) {
return getStorage().getInstancer(type, new EmbeddedContext(world), model, stage);
public <I extends Instance> Instancer<I> instancer(VisualEmbedding world, InstanceType<I> type, Model model, RenderStage stage) {
return getStorage().getInstancer(world, type, model, stage);
}
@Override

View file

@ -2,15 +2,18 @@ package com.jozufozu.flywheel.backend.engine;
import java.util.ArrayList;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.Instancer;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.lib.util.AtomicBitset;
public abstract class AbstractInstancer<I extends Instance> implements Instancer<I> {
public final InstanceType<I> type;
public final Context context;
@Nullable
public final VisualEmbedding embedding;
// Lock for all instances, only needs to be used in methods that may run on the TaskExecutor.
protected final Object lock = new Object();
@ -20,9 +23,9 @@ public abstract class AbstractInstancer<I extends Instance> implements Instancer
protected final AtomicBitset changed = new AtomicBitset();
protected final AtomicBitset deleted = new AtomicBitset();
protected AbstractInstancer(InstanceType<I> type, Context context) {
protected AbstractInstancer(InstanceType<I> type, @Nullable VisualEmbedding embedding) {
this.type = type;
this.context = context;
this.embedding = embedding;
}
@Override
@ -38,7 +41,7 @@ public abstract class AbstractInstancer<I extends Instance> implements Instancer
}
@Override
public void stealInstance(I instance) {
public void stealInstance(@Nullable I instance) {
if (instance == null) {
return;
}

View file

@ -1,10 +1,13 @@
package com.jozufozu.flywheel.backend.engine;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
public record InstancerKey<I extends Instance>(InstanceType<I> type, Context context, Model model, RenderStage stage) {
public record InstancerKey<I extends Instance>(@Nullable VisualEmbedding embedding, InstanceType<I> type, Model model,
RenderStage stage) {
}

View file

@ -5,13 +5,15 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.Instancer;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
public abstract class InstancerStorage<N extends AbstractInstancer<?>> {
/**
@ -28,8 +30,8 @@ public abstract class InstancerStorage<N extends AbstractInstancer<?>> {
protected final List<UninitializedInstancer<N, ?>> initializationQueue = new ArrayList<>();
@SuppressWarnings("unchecked")
public <I extends Instance> Instancer<I> getInstancer(InstanceType<I> type, Context context, Model model, RenderStage stage) {
return (Instancer<I>) instancers.computeIfAbsent(new InstancerKey<>(type, context, model, stage), this::createAndDeferInit);
public <I extends Instance> Instancer<I> getInstancer(@Nullable VisualEmbedding level, InstanceType<I> type, Model model, RenderStage stage) {
return (Instancer<I>) instancers.computeIfAbsent(new InstancerKey<>(level, type, model, stage), this::createAndDeferInit);
}
public void delete() {

View file

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.backend.engine.textures;
package com.jozufozu.flywheel.backend.engine;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
@ -8,6 +8,7 @@ import com.mojang.blaze3d.systems.RenderSystem;
import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
public class TextureBinder {
// TODO: some kind of cache eviction when the program changes
@ -69,4 +70,17 @@ public class TextureBinder {
gameRenderer.lightTexture()
.turnOffLightLayer();
}
/**
* Get a built-in texture by its resource location.
*
* @param texture The texture's resource location.
* @return The texture.
*/
public static int byName(ResourceLocation texture) {
return Minecraft.getInstance()
.getTextureManager()
.getTexture(texture)
.getId();
}
}

View file

@ -16,18 +16,20 @@ import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.backend.compile.ContextShader;
import com.jozufozu.flywheel.backend.compile.ContextShaders;
import com.jozufozu.flywheel.backend.compile.IndirectPrograms;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.backend.context.SimpleContextShader;
import com.jozufozu.flywheel.backend.engine.MaterialRenderState;
import com.jozufozu.flywheel.backend.engine.MeshPool;
import com.jozufozu.flywheel.backend.engine.textures.TextureBinder;
import com.jozufozu.flywheel.backend.engine.textures.TextureSource;
import com.jozufozu.flywheel.backend.engine.TextureBinder;
import com.jozufozu.flywheel.backend.engine.uniform.Uniforms;
import com.jozufozu.flywheel.backend.gl.Driver;
import com.jozufozu.flywheel.backend.gl.GlCompat;
@ -40,7 +42,8 @@ public class IndirectCullingGroup<I extends Instance> {
private static final int DRAW_BARRIER_BITS = GL_SHADER_STORAGE_BARRIER_BIT | GL_COMMAND_BARRIER_BIT;
private final InstanceType<I> instanceType;
private final Context context;
@Nullable
private final VisualEmbedding embedding;
private final long objectStride;
private final IndirectBuffers buffers;
private final List<IndirectInstancer<?>> instancers = new ArrayList<>();
@ -56,18 +59,17 @@ public class IndirectCullingGroup<I extends Instance> {
private boolean needsDrawSort;
private int instanceCountThisFrame;
IndirectCullingGroup(InstanceType<I> instanceType, Context context, IndirectPrograms programs) {
IndirectCullingGroup(InstanceType<I> instanceType, @Nullable VisualEmbedding embedding, IndirectPrograms programs) {
this.instanceType = instanceType;
this.context = context;
this.embedding = embedding;
objectStride = instanceType.layout()
.byteSize() + IndirectBuffers.INT_SIZE;
buffers = new IndirectBuffers(objectStride);
this.programs = programs;
// TODO: Culling programs need to be context aware.
cullProgram = programs.getCullingProgram(instanceType);
applyProgram = programs.getApplyProgram();
drawProgram = programs.getIndirectProgram(instanceType, context.contextShader());
drawProgram = programs.getIndirectProgram(instanceType, ContextShaders.forEmbedding(embedding));
}
public void flushInstancers() {
@ -128,6 +130,14 @@ public class IndirectCullingGroup<I extends Instance> {
Uniforms.bindFrame();
cullProgram.bind();
if (embedding != null) {
cullProgram.setBool("_flw_useEmbeddedModel", true);
cullProgram.setMat4("_flw_embeddedModel", embedding.pose());
} else {
cullProgram.setBool("_flw_useEmbeddedModel", false);
}
buffers.bindForCompute();
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glDispatchCompute(GlCompat.getComputeGroupCount(instanceCountThisFrame), 1, 1);
@ -191,7 +201,7 @@ public class IndirectCullingGroup<I extends Instance> {
needsDrawSort = true;
}
public void submit(RenderStage stage, TextureSource textures) {
public void submit(RenderStage stage) {
if (nothingToDo(stage)) {
return;
}
@ -199,6 +209,13 @@ public class IndirectCullingGroup<I extends Instance> {
drawProgram.bind();
buffers.bindForDraw();
if (embedding != null) {
drawProgram.setVec3("_flw_oneOverLightBoxSize", 1, 1, 1);
drawProgram.setVec3("_flw_lightVolumeMin", 0, 0, 0);
drawProgram.setMat4("_flw_model", embedding.pose());
drawProgram.setMat3("_flw_normal", embedding.normal());
}
drawBarrier();
var flwBaseDraw = drawProgram.getUniformLocation("_flw_baseDraw");
@ -206,7 +223,6 @@ public class IndirectCullingGroup<I extends Instance> {
for (var multiDraw : multiDraws.get(stage)) {
glUniform1ui(flwBaseDraw, multiDraw.start);
context.prepare(multiDraw.material, drawProgram, textures);
MaterialRenderState.setup(multiDraw.material);
multiDraw.submit();
@ -214,7 +230,7 @@ public class IndirectCullingGroup<I extends Instance> {
}
}
public GlProgram bindWithContextShader(SimpleContextShader override) {
public GlProgram bindWithContextShader(ContextShader override) {
var program = programs.getIndirectProgram(instanceType, override);
program.bind();

View file

@ -11,21 +11,22 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.backend.Engine;
import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.backend.compile.ContextShaders;
import com.jozufozu.flywheel.backend.compile.IndirectPrograms;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.backend.context.ContextShaders;
import com.jozufozu.flywheel.backend.engine.CommonCrumbling;
import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl;
import com.jozufozu.flywheel.backend.engine.InstancerKey;
import com.jozufozu.flywheel.backend.engine.InstancerStorage;
import com.jozufozu.flywheel.backend.engine.MaterialRenderState;
import com.jozufozu.flywheel.backend.engine.MeshPool;
import com.jozufozu.flywheel.backend.engine.textures.TextureBinder;
import com.jozufozu.flywheel.backend.engine.textures.TextureSource;
import com.jozufozu.flywheel.backend.engine.TextureBinder;
import com.jozufozu.flywheel.backend.engine.uniform.Uniforms;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.backend.gl.array.GlVertexArray;
@ -44,7 +45,6 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
private final StagingBuffer stagingBuffer;
private final MeshPool meshPool;
private final GlVertexArray vertexArray;
private final TextureSource textures = new TextureSource();
private final Map<GroupKey<?>, IndirectCullingGroup<?>> cullingGroups = new HashMap<>();
private final GlBuffer crumblingDrawBuffer = new GlBuffer();
@ -61,13 +61,13 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
@Override
protected <I extends Instance> IndirectInstancer<?> create(InstancerKey<I> key) {
return new IndirectInstancer<>(key.type(), key.context(), key.model());
return new IndirectInstancer<>(key.type(), key.embedding(), key.model());
}
@SuppressWarnings("unchecked")
@Override
protected <I extends Instance> void initialize(InstancerKey<I> key, IndirectInstancer<?> instancer) {
var groupKey = new GroupKey<>(key.type(), key.context());
var groupKey = new GroupKey<>(key.type(), key.embedding());
var group = (IndirectCullingGroup<I>) cullingGroups.computeIfAbsent(groupKey, t -> new IndirectCullingGroup<>(t.type, t.context, programs));
group.add((IndirectInstancer<I>) instancer, key.model(), key.stage(), meshPool);
}
@ -88,7 +88,7 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
Uniforms.bindForDraw();
for (var group : cullingGroups.values()) {
group.submit(stage, textures);
group.submit(stage);
}
MaterialRenderState.reset();
@ -168,7 +168,7 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
.bindWithContextShader(ContextShaders.CRUMBLING);
for (var progressEntry : byProgress.int2ObjectEntrySet()) {
program.setTexture("crumblingTex", textures.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey())));
program.setTexture("crumblingTex", TextureBinder.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey())));
for (var instanceHandlePair : progressEntry.getValue()) {
IndirectInstancer<?> instancer = instanceHandlePair.first();
@ -221,7 +221,7 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
continue;
}
byType.computeIfAbsent(new GroupKey<>(instancer.type, instancer.context), $ -> new Int2ObjectArrayMap<>())
byType.computeIfAbsent(new GroupKey<>(instancer.type, instancer.embedding), $ -> new Int2ObjectArrayMap<>())
.computeIfAbsent(progress, $ -> new ArrayList<>())
.add(Pair.of(instancer, impl));
}
@ -229,6 +229,6 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
return byType;
}
public record GroupKey<I extends Instance>(InstanceType<I> type, Context context) {
public record GroupKey<I extends Instance>(InstanceType<I> type, @Nullable VisualEmbedding context) {
}
}

View file

@ -3,6 +3,7 @@ package com.jozufozu.flywheel.backend.engine.indirect;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector4fc;
import org.lwjgl.system.MemoryUtil;
@ -10,7 +11,7 @@ import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.InstanceWriter;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.backend.engine.AbstractInstancer;
public class IndirectInstancer<I extends Instance> extends AbstractInstancer<I> {
@ -25,7 +26,7 @@ public class IndirectInstancer<I extends Instance> extends AbstractInstancer<I>
private int lastModelIndex = -1;
private long lastStartPos = -1;
public IndirectInstancer(InstanceType<I> type, Context context, Model model) {
public IndirectInstancer(InstanceType<I> type, @Nullable VisualEmbedding context, Model model) {
super(type, context);
this.objectStride = type.layout()
.byteSize() + IndirectBuffers.INT_SIZE;

View file

@ -19,8 +19,8 @@ import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.backend.ShaderIndices;
import com.jozufozu.flywheel.backend.compile.ContextShaders;
import com.jozufozu.flywheel.backend.compile.InstancingPrograms;
import com.jozufozu.flywheel.backend.context.ContextShaders;
import com.jozufozu.flywheel.backend.engine.CommonCrumbling;
import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl;
import com.jozufozu.flywheel.backend.engine.InstancerKey;
@ -28,8 +28,7 @@ import com.jozufozu.flywheel.backend.engine.InstancerStorage;
import com.jozufozu.flywheel.backend.engine.MaterialEncoder;
import com.jozufozu.flywheel.backend.engine.MaterialRenderState;
import com.jozufozu.flywheel.backend.engine.MeshPool;
import com.jozufozu.flywheel.backend.engine.textures.TextureBinder;
import com.jozufozu.flywheel.backend.engine.textures.TextureSource;
import com.jozufozu.flywheel.backend.engine.TextureBinder;
import com.jozufozu.flywheel.backend.engine.uniform.Uniforms;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.backend.gl.GlTextureUnit;
@ -53,7 +52,6 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
*/
private final MeshPool meshPool;
private final GlVertexArray vao;
private final TextureSource textures;
private final TextureBuffer instanceTexture;
public InstancedDrawManager(InstancingPrograms programs) {
@ -62,7 +60,6 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
meshPool = new MeshPool();
vao = GlVertexArray.create();
textures = new TextureSource();
instanceTexture = new TextureBuffer();
meshPool.bind(vao);
@ -133,15 +130,14 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
continue;
}
var context = shader.context();
var embedding = shader.embedding();
var material = shader.material();
var program = programs.get(shader.instanceType(), context.contextShader());
var program = programs.get(shader.instanceType(), ContextShaders.forEmbedding(embedding));
program.bind();
uploadMaterialUniform(program, material);
context.prepare(material, program, textures);
MaterialRenderState.setup(material);
GlTextureUnit.T3.makeActive();
@ -160,7 +156,7 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
@Override
protected <I extends Instance> InstancedInstancer<I> create(InstancerKey<I> key) {
return new InstancedInstancer<>(key.type(), key.context());
return new InstancedInstancer<>(key.type(), key.embedding());
}
@Override
@ -174,7 +170,7 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
for (var entry : meshes) {
var mesh = meshPool.alloc(entry.mesh());
ShaderState shaderState = new ShaderState(entry.material(), key.type(), key.context());
ShaderState shaderState = new ShaderState(entry.material(), key.type(), key.embedding());
DrawCall drawCall = new DrawCall(instancer, mesh, shaderState);
drawSet.put(shaderState, drawCall);
@ -222,7 +218,7 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
continue;
}
program.setTexture("crumblingTex", textures.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey())));
program.setTexture("crumblingTex", TextureBinder.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey())));
GlTextureUnit.T3.makeActive();
program.setSamplerBinding("_flw_instances", 3);

View file

@ -8,7 +8,7 @@ import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.InstanceWriter;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.backend.engine.AbstractInstancer;
import com.jozufozu.flywheel.backend.gl.TextureBuffer;
import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer;
@ -25,7 +25,7 @@ public class InstancedInstancer<I extends Instance> extends AbstractInstancer<I>
private final List<DrawCall> drawCalls = new ArrayList<>();
public InstancedInstancer(InstanceType<I> type, Context context) {
public InstancedInstancer(InstanceType<I> type, @Nullable VisualEmbedding context) {
super(type, context);
var layout = type.layout();
// Align to one texel in the texture buffer

View file

@ -1,8 +1,10 @@
package com.jozufozu.flywheel.backend.engine.instancing;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.backend.context.Context;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
public record ShaderState(Material material, InstanceType<?> instanceType, Context context) {
public record ShaderState(Material material, InstanceType<?> instanceType, @Nullable VisualEmbedding embedding) {
}

View file

@ -1,13 +0,0 @@
package com.jozufozu.flywheel.backend.engine.textures;
import com.jozufozu.flywheel.backend.context.Texture;
/**
* Internal base interface that {@link com.jozufozu.flywheel.backend.gl.shader.GlProgram GlProgram} expects.
*/
public interface IdentifiedTexture extends Texture {
/**
* @return The GL texture id of this texture.
*/
int id();
}

View file

@ -1,78 +0,0 @@
package com.jozufozu.flywheel.backend.engine.textures;
import java.util.HashMap;
import java.util.Map;
import com.jozufozu.flywheel.backend.context.Texture;
import com.jozufozu.flywheel.backend.mixin.LightTextureAccessor;
import com.jozufozu.flywheel.backend.mixin.OverlayTextureAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.resources.ResourceLocation;
public class TextureSource {
private final DirectTexture lightTexture;
private final DirectTexture overlayTexture;
private final Map<ResourceLocation, WrappedTexture> wrappers = new HashMap<>();
public TextureSource() {
var gameRenderer = Minecraft.getInstance().gameRenderer;
this.lightTexture = new DirectTexture(((LightTextureAccessor) gameRenderer.lightTexture()).flywheel$texture()
.getId());
this.overlayTexture = new DirectTexture(((OverlayTextureAccessor) gameRenderer.overlayTexture()).flywheel$texture()
.getId());
}
/**
* Get a built-in texture by its resource location.
*
* @param texture The texture's resource location.
* @return The texture.
*/
public Texture byName(ResourceLocation texture) {
return wrappers.computeIfAbsent(texture, key -> new WrappedTexture(Minecraft.getInstance()
.getTextureManager()
.getTexture(key)));
}
/**
* Get the overlay texture.
*
* @return The overlay texture.
*/
public Texture overlay() {
return overlayTexture;
}
/**
* Get the light texture.
*
* @return The light texture.
*/
public Texture light() {
return lightTexture;
}
public record WrappedTexture(AbstractTexture texture) implements IdentifiedTexture {
@Override
public void filter(boolean blur, boolean mipmap) {
texture.setFilter(blur, mipmap);
}
@Override
public int id() {
return texture.getId();
}
}
public record DirectTexture(int id) implements IdentifiedTexture {
@Override
public void filter(boolean blur, boolean mipmap) {
// no-op
}
}
}

View file

@ -18,9 +18,7 @@ import org.joml.Matrix3fc;
import org.joml.Matrix4fc;
import org.slf4j.Logger;
import com.jozufozu.flywheel.backend.context.Texture;
import com.jozufozu.flywheel.backend.engine.textures.IdentifiedTexture;
import com.jozufozu.flywheel.backend.engine.textures.TextureBinder;
import com.jozufozu.flywheel.backend.engine.TextureBinder;
import com.jozufozu.flywheel.backend.gl.GlObject;
import com.mojang.blaze3d.shaders.ProgramManager;
import com.mojang.logging.LogUtils;
@ -45,19 +43,14 @@ public class GlProgram extends GlObject {
ProgramManager.glUseProgram(0);
}
public void setTexture(String glslName, Texture texture) {
if (!(texture instanceof IdentifiedTexture identified)) {
return;
}
public void setTexture(String glslName, int texture) {
int uniform = getUniformLocation(glslName);
if (uniform < 0) {
return;
}
int id = identified.id();
int binding = TextureBinder.bindTexture(id);
int binding = TextureBinder.bindTexture(texture);
glUniform1i(uniform, binding);
}
@ -122,6 +115,16 @@ public class GlProgram extends GlObject {
glUniformMatrix3fv(uniform, false, matrix.get(new float[9]));
}
public void setBool(String glslName, boolean bool) {
int uniform = getUniformLocation(glslName);
if (uniform < 0) {
return;
}
glUniform1i(uniform, bool ? 1 : 0);
}
/**
* Retrieves the index of the uniform with the given name.
*

View file

@ -9,7 +9,7 @@ import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.Instancer;
import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.api.visualization.EmbeddedLevel;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.api.visualization.VisualizationContext;
public class InstancerProviderImpl implements InstancerProvider, Supplier<VisualizationContext> {
@ -31,7 +31,7 @@ public class InstancerProviderImpl implements InstancerProvider, Supplier<Visual
return new VisualizationContextImpl(this, engine.renderOrigin());
}
public Embedded embed(EmbeddedLevel world) {
public Embedded embed(VisualEmbedding world) {
return new Embedded(engine, world, renderStage);
}
@ -41,9 +41,9 @@ public class InstancerProviderImpl implements InstancerProvider, Supplier<Visual
}
public static final class Embedded extends InstancerProviderImpl {
private final EmbeddedLevel world;
private final VisualEmbedding world;
public Embedded(Engine engine, EmbeddedLevel world, RenderStage renderStage) {
public Embedded(Engine engine, VisualEmbedding world, RenderStage renderStage) {
super(engine, renderStage);
this.world = world;
}
@ -53,7 +53,7 @@ public class InstancerProviderImpl implements InstancerProvider, Supplier<Visual
return engine.instancer(world, type, model, renderStage);
}
public EmbeddedLevel world() {
public VisualEmbedding world() {
return world;
}

View file

@ -1,7 +1,7 @@
package com.jozufozu.flywheel.impl.visualization;
import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.api.visualization.EmbeddedLevel;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.api.visualization.VisualizationContext;
import net.minecraft.core.Vec3i;
@ -15,7 +15,7 @@ import net.minecraft.core.Vec3i;
*/
public record VisualizationContextImpl(InstancerProviderImpl instancerProvider, Vec3i renderOrigin) implements VisualizationContext {
@Override
public VisualizationContext embed(EmbeddedLevel world) {
public VisualizationContext embed(VisualEmbedding world) {
return new VisualizationContextImpl(instancerProvider.embed(world), renderOrigin);
}
}