Backend into a corner

- Make VisualizationContext, InstancerProvider, VisualEmbedding all
  BackendImplemented
- Add Engine#createVisualizationContext
- Remove Engine#instancer and variants
- VisualEmbeddings are created by backends, and data will be pushed into
  them by visuals.
- VisualEmbedding extends VisualizationContext
- The implementation of VisualEmbedding also implements Environment
- Environment is an internal replacement for Context
- Instancers are keyed by Environments
- Default environment is a noop essentially
This commit is contained in:
Jozufozu 2024-02-26 18:41:55 -08:00
parent 4d216b6672
commit bd70b89621
21 changed files with 229 additions and 181 deletions

View file

@ -6,13 +6,9 @@ import com.jozufozu.flywheel.api.BackendImplemented;
import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.event.RenderContext;
import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance; 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.instance.InstancerProvider;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.task.Plan;
import com.jozufozu.flywheel.api.task.TaskExecutor; import com.jozufozu.flywheel.api.task.TaskExecutor;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import com.jozufozu.flywheel.api.visualization.VisualizationContext;
import net.minecraft.client.Camera; import net.minecraft.client.Camera;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -21,20 +17,12 @@ import net.minecraft.core.Vec3i;
@BackendImplemented @BackendImplemented
public interface Engine { public interface Engine {
/** /**
* Get an instancer for the given instance type, model, and render stage. * Create a visualization context that will render to the given stage.
* *
* <p>Calling this method twice with the same arguments will return the same instancer.</p> * @param stage The stage to render to.
* * @return A new visualization context.
* <p>If you are writing a visual you should probably be using
* {@link InstancerProvider#instancer(InstanceType, Model)}, which will decide the {@code RenderStage}
* based on what type of visual is getting the instancer.</p>
*
* @return An instancer for the given instance type, model, and render stage.
* @see InstancerProvider
*/ */
<I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model, RenderStage stage); VisualizationContext createVisualizationContext(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. * Create a plan that will be executed every frame.

View file

@ -1,10 +1,9 @@
package com.jozufozu.flywheel.api.instance; package com.jozufozu.flywheel.api.instance;
import org.jetbrains.annotations.ApiStatus; import com.jozufozu.flywheel.api.BackendImplemented;
import com.jozufozu.flywheel.api.model.Model; import com.jozufozu.flywheel.api.model.Model;
@ApiStatus.NonExtendable @BackendImplemented
public interface InstancerProvider { public interface InstancerProvider {
/** /**
* Get an instancer for the given instance type rendering the given model. * Get an instancer for the given instance type rendering the given model.

View file

@ -3,12 +3,15 @@ package com.jozufozu.flywheel.api.visualization;
import org.joml.Matrix3fc; import org.joml.Matrix3fc;
import org.joml.Matrix4fc; import org.joml.Matrix4fc;
import net.minecraft.world.phys.AABB; import com.jozufozu.flywheel.api.BackendImplemented;
public interface VisualEmbedding { @BackendImplemented
Matrix4fc pose(); public interface VisualEmbedding extends VisualizationContext {
/**
Matrix3fc normal(); * Set the transformation matrices for the embedding.
*
AABB boundingBox(); * @param pose The model matrix.
* @param normal The normal matrix.
*/
void transforms(Matrix4fc pose, Matrix3fc normal);
} }

View file

@ -2,6 +2,7 @@ package com.jozufozu.flywheel.api.visualization;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import com.jozufozu.flywheel.api.BackendImplemented;
import com.jozufozu.flywheel.api.instance.InstancerProvider; import com.jozufozu.flywheel.api.instance.InstancerProvider;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
@ -9,7 +10,7 @@ import net.minecraft.core.Vec3i;
/** /**
* A context object passed on visual creation. * A context object passed on visual creation.
*/ */
@ApiStatus.NonExtendable @BackendImplemented
public interface VisualizationContext { public interface VisualizationContext {
/** /**
* @return The {@link InstancerProvider} that the visual can use to get instancers to render models. * @return The {@link InstancerProvider} that the visual can use to get instancers to render models.
@ -24,5 +25,5 @@ public interface VisualizationContext {
Vec3i renderOrigin(); Vec3i renderOrigin();
@ApiStatus.Experimental @ApiStatus.Experimental
VisualizationContext embed(VisualEmbedding world); VisualEmbedding createEmbedding();
} }

View file

@ -5,8 +5,10 @@ import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.Instancer; 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.model.Model;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.api.visualization.VisualizationContext;
import net.minecraft.client.Camera; import net.minecraft.client.Camera;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -21,14 +23,13 @@ public abstract class AbstractEngine implements Engine {
sqrMaxOriginDistance = maxOriginDistance * maxOriginDistance; sqrMaxOriginDistance = maxOriginDistance * maxOriginDistance;
} }
@Override public <I extends Instance> Instancer<I> instancer(Environment environment, InstanceType<I> type, Model model, RenderStage stage) {
public <I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model, RenderStage stage) { return getStorage().getInstancer(environment, type, model, stage);
return getStorage().getInstancer(null, type, model, stage);
} }
@Override @Override
public <I extends Instance> Instancer<I> instancer(VisualEmbedding world, InstanceType<I> type, Model model, RenderStage stage) { public VisualizationContext createVisualizationContext(RenderStage stage) {
return getStorage().getInstancer(world, type, model, stage); return new VisualizationContextImpl(stage);
} }
@Override @Override
@ -54,4 +55,29 @@ public abstract class AbstractEngine implements Engine {
} }
protected abstract InstancerStorage<? extends AbstractInstancer<?>> getStorage(); protected abstract InstancerStorage<? extends AbstractInstancer<?>> getStorage();
private class VisualizationContextImpl implements VisualizationContext {
private final InstancerProviderImpl instancerProvider;
private final RenderStage stage;
public VisualizationContextImpl(RenderStage stage) {
instancerProvider = new InstancerProviderImpl(AbstractEngine.this, stage);
this.stage = stage;
}
@Override
public InstancerProvider instancerProvider() {
return instancerProvider;
}
@Override
public Vec3i renderOrigin() {
return AbstractEngine.this.renderOrigin();
}
@Override
public VisualEmbedding createEmbedding() {
return new EmbeddedEnvironment(AbstractEngine.this, stage);
}
}
} }

View file

@ -7,13 +7,11 @@ import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.Instancer; import com.jozufozu.flywheel.api.instance.Instancer;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.lib.util.AtomicBitset; import com.jozufozu.flywheel.lib.util.AtomicBitset;
public abstract class AbstractInstancer<I extends Instance> implements Instancer<I> { public abstract class AbstractInstancer<I extends Instance> implements Instancer<I> {
public final InstanceType<I> type; public final InstanceType<I> type;
@Nullable public final Environment environment;
public final VisualEmbedding embedding;
// Lock for all instances, only needs to be used in methods that may run on the TaskExecutor. // Lock for all instances, only needs to be used in methods that may run on the TaskExecutor.
protected final Object lock = new Object(); protected final Object lock = new Object();
@ -23,9 +21,9 @@ public abstract class AbstractInstancer<I extends Instance> implements Instancer
protected final AtomicBitset changed = new AtomicBitset(); protected final AtomicBitset changed = new AtomicBitset();
protected final AtomicBitset deleted = new AtomicBitset(); protected final AtomicBitset deleted = new AtomicBitset();
protected AbstractInstancer(InstanceType<I> type, @Nullable VisualEmbedding embedding) { protected AbstractInstancer(InstanceType<I> type, Environment environment) {
this.type = type; this.type = type;
this.embedding = embedding; this.environment = environment;
} }
@Override @Override

View file

@ -0,0 +1,81 @@
package com.jozufozu.flywheel.backend.engine;
import org.joml.Matrix3f;
import org.joml.Matrix3fc;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
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.instance.InstancerProvider;
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.gl.shader.GlProgram;
import net.minecraft.core.Vec3i;
public class EmbeddedEnvironment implements Environment, VisualEmbedding {
private final Matrix4f pose = new Matrix4f();
private final Matrix3f normal = new Matrix3f();
private final InstancerProvider instancerProvider;
private final AbstractEngine engine;
private final RenderStage renderStage;
public EmbeddedEnvironment(AbstractEngine engine, RenderStage renderStage) {
this.engine = engine;
this.renderStage = renderStage;
instancerProvider = new InstancerProvider() {
@Override
public <I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model) {
// Kinda cursed usage of anonymous classes here, but it does the job.
return engine.instancer(EmbeddedEnvironment.this, type, model, renderStage);
}
};
}
@Override
public void transforms(Matrix4fc pose, Matrix3fc normal) {
this.pose.set(pose);
this.normal.set(normal);
}
@Override
public ContextShader contextShader() {
return ContextShaders.EMBEDDED;
}
@Override
public void setupDraw(GlProgram drawProgram) {
drawProgram.setVec3("_flw_oneOverLightBoxSize", 1, 1, 1);
drawProgram.setVec3("_flw_lightVolumeMin", 0, 0, 0);
drawProgram.setMat4("_flw_model", pose);
drawProgram.setMat3("_flw_normal", normal);
}
@Override
public void setupCull(GlProgram cullProgram) {
cullProgram.setBool("_flw_useEmbeddedModel", true);
cullProgram.setMat4("_flw_embeddedModel", pose);
}
@Override
public InstancerProvider instancerProvider() {
return instancerProvider;
}
@Override
public Vec3i renderOrigin() {
return Vec3i.ZERO;
}
@Override
public VisualEmbedding createEmbedding() {
return new EmbeddedEnvironment(engine, renderStage);
}
}

View file

@ -0,0 +1,12 @@
package com.jozufozu.flywheel.backend.engine;
import com.jozufozu.flywheel.backend.compile.ContextShader;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
public interface Environment {
ContextShader contextShader();
void setupDraw(GlProgram drawProgram);
void setupCull(GlProgram cullProgram);
}

View file

@ -0,0 +1,27 @@
package com.jozufozu.flywheel.backend.engine;
import com.jozufozu.flywheel.backend.compile.ContextShader;
import com.jozufozu.flywheel.backend.compile.ContextShaders;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
public class GlobalEnvironment implements Environment {
public static final GlobalEnvironment INSTANCE = new GlobalEnvironment();
private GlobalEnvironment() {
}
@Override
public ContextShader contextShader() {
return ContextShaders.DEFAULT;
}
@Override
public void setupDraw(GlProgram drawProgram) {
}
@Override
public void setupCull(GlProgram cullProgram) {
cullProgram.setBool("_flw_useEmbeddedModel", false);
}
}

View file

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

View file

@ -0,0 +1,15 @@
package com.jozufozu.flywheel.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.instance.Instancer;
import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.api.model.Model;
public record InstancerProviderImpl(AbstractEngine engine, RenderStage renderStage) implements InstancerProvider {
@Override
public <I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model) {
return engine.instancer(GlobalEnvironment.INSTANCE, type, model, renderStage);
}
}

View file

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

View file

@ -16,17 +16,14 @@ import java.util.EnumMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.model.Model; 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.ContextShader;
import com.jozufozu.flywheel.backend.compile.ContextShaders;
import com.jozufozu.flywheel.backend.compile.IndirectPrograms; import com.jozufozu.flywheel.backend.compile.IndirectPrograms;
import com.jozufozu.flywheel.backend.engine.Environment;
import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.MaterialRenderState;
import com.jozufozu.flywheel.backend.engine.MeshPool; import com.jozufozu.flywheel.backend.engine.MeshPool;
import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms;
@ -41,8 +38,7 @@ public class IndirectCullingGroup<I extends Instance> {
private static final int DRAW_BARRIER_BITS = GL_SHADER_STORAGE_BARRIER_BIT | GL_COMMAND_BARRIER_BIT; private static final int DRAW_BARRIER_BITS = GL_SHADER_STORAGE_BARRIER_BIT | GL_COMMAND_BARRIER_BIT;
private final InstanceType<I> instanceType; private final InstanceType<I> instanceType;
@Nullable private final Environment environment;
private final VisualEmbedding embedding;
private final long objectStride; private final long objectStride;
private final IndirectBuffers buffers; private final IndirectBuffers buffers;
private final List<IndirectInstancer<?>> instancers = new ArrayList<>(); private final List<IndirectInstancer<?>> instancers = new ArrayList<>();
@ -58,9 +54,9 @@ public class IndirectCullingGroup<I extends Instance> {
private boolean needsDrawSort; private boolean needsDrawSort;
private int instanceCountThisFrame; private int instanceCountThisFrame;
IndirectCullingGroup(InstanceType<I> instanceType, @Nullable VisualEmbedding embedding, IndirectPrograms programs) { IndirectCullingGroup(InstanceType<I> instanceType, Environment environment, IndirectPrograms programs) {
this.instanceType = instanceType; this.instanceType = instanceType;
this.embedding = embedding; this.environment = environment;
objectStride = instanceType.layout() objectStride = instanceType.layout()
.byteSize() + IndirectBuffers.INT_SIZE; .byteSize() + IndirectBuffers.INT_SIZE;
buffers = new IndirectBuffers(objectStride); buffers = new IndirectBuffers(objectStride);
@ -68,7 +64,7 @@ public class IndirectCullingGroup<I extends Instance> {
this.programs = programs; this.programs = programs;
cullProgram = programs.getCullingProgram(instanceType); cullProgram = programs.getCullingProgram(instanceType);
applyProgram = programs.getApplyProgram(); applyProgram = programs.getApplyProgram();
drawProgram = programs.getIndirectProgram(instanceType, ContextShaders.forEmbedding(embedding)); drawProgram = programs.getIndirectProgram(instanceType, environment.contextShader());
} }
public void flushInstancers() { public void flushInstancers() {
@ -130,12 +126,7 @@ public class IndirectCullingGroup<I extends Instance> {
Uniforms.bindFrame(); Uniforms.bindFrame();
cullProgram.bind(); cullProgram.bind();
if (embedding != null) { environment.setupCull(cullProgram);
cullProgram.setBool("_flw_useEmbeddedModel", true);
cullProgram.setMat4("_flw_embeddedModel", embedding.pose());
} else {
cullProgram.setBool("_flw_useEmbeddedModel", false);
}
buffers.bindForCompute(); buffers.bindForCompute();
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
@ -208,12 +199,7 @@ public class IndirectCullingGroup<I extends Instance> {
drawProgram.bind(); drawProgram.bind();
buffers.bindForDraw(); buffers.bindForDraw();
if (embedding != null) { environment.setupDraw(drawProgram);
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(); drawBarrier();

View file

@ -11,16 +11,14 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.api.backend.Engine; import com.jozufozu.flywheel.api.backend.Engine;
import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType; 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.ContextShaders;
import com.jozufozu.flywheel.backend.compile.IndirectPrograms; import com.jozufozu.flywheel.backend.compile.IndirectPrograms;
import com.jozufozu.flywheel.backend.engine.CommonCrumbling; import com.jozufozu.flywheel.backend.engine.CommonCrumbling;
import com.jozufozu.flywheel.backend.engine.Environment;
import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl; import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl;
import com.jozufozu.flywheel.backend.engine.InstancerKey; import com.jozufozu.flywheel.backend.engine.InstancerKey;
import com.jozufozu.flywheel.backend.engine.InstancerStorage; import com.jozufozu.flywheel.backend.engine.InstancerStorage;
@ -62,14 +60,14 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
@Override @Override
protected <I extends Instance> IndirectInstancer<?> create(InstancerKey<I> key) { protected <I extends Instance> IndirectInstancer<?> create(InstancerKey<I> key) {
return new IndirectInstancer<>(key.type(), key.embedding(), key.model()); return new IndirectInstancer<>(key.type(), key.environment(), key.model());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
protected <I extends Instance> void initialize(InstancerKey<I> key, IndirectInstancer<?> instancer) { protected <I extends Instance> void initialize(InstancerKey<I> key, IndirectInstancer<?> instancer) {
var groupKey = new GroupKey<>(key.type(), key.embedding()); var groupKey = new GroupKey<>(key.type(), key.environment());
var group = (IndirectCullingGroup<I>) cullingGroups.computeIfAbsent(groupKey, t -> new IndirectCullingGroup<>(t.type, t.context, programs)); var group = (IndirectCullingGroup<I>) cullingGroups.computeIfAbsent(groupKey, t -> new IndirectCullingGroup<>(t.type, t.environment, programs));
group.add((IndirectInstancer<I>) instancer, key.model(), key.stage(), meshPool); group.add((IndirectInstancer<I>) instancer, key.model(), key.stage(), meshPool);
} }
@ -224,7 +222,7 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
continue; continue;
} }
byType.computeIfAbsent(new GroupKey<>(instancer.type, instancer.embedding), $ -> new Int2ObjectArrayMap<>()) byType.computeIfAbsent(new GroupKey<>(instancer.type, instancer.environment), $ -> new Int2ObjectArrayMap<>())
.computeIfAbsent(progress, $ -> new ArrayList<>()) .computeIfAbsent(progress, $ -> new ArrayList<>())
.add(Pair.of(instancer, impl)); .add(Pair.of(instancer, impl));
} }
@ -232,6 +230,6 @@ public class IndirectDrawManager extends InstancerStorage<IndirectInstancer<?>>
return byType; return byType;
} }
public record GroupKey<I extends Instance>(InstanceType<I> type, @Nullable VisualEmbedding context) { public record GroupKey<I extends Instance>(InstanceType<I> type, Environment environment) {
} }
} }

View file

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

View file

@ -130,12 +130,14 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
continue; continue;
} }
var embedding = shader.embedding(); var environment = shader.environment();
var material = shader.material(); var material = shader.material();
var program = programs.get(shader.instanceType(), ContextShaders.forEmbedding(embedding)); var program = programs.get(shader.instanceType(), environment.contextShader());
program.bind(); program.bind();
environment.setupDraw(program);
uploadMaterialUniform(program, material); uploadMaterialUniform(program, material);
MaterialRenderState.setup(material); MaterialRenderState.setup(material);
@ -153,7 +155,7 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
@Override @Override
protected <I extends Instance> InstancedInstancer<I> create(InstancerKey<I> key) { protected <I extends Instance> InstancedInstancer<I> create(InstancerKey<I> key) {
return new InstancedInstancer<>(key.type(), key.embedding()); return new InstancedInstancer<>(key.type(), key.environment());
} }
@Override @Override
@ -167,7 +169,7 @@ public class InstancedDrawManager extends InstancerStorage<InstancedInstancer<?>
for (var entry : meshes) { for (var entry : meshes) {
var mesh = meshPool.alloc(entry.mesh()); var mesh = meshPool.alloc(entry.mesh());
ShaderState shaderState = new ShaderState(entry.material(), key.type(), key.embedding()); ShaderState shaderState = new ShaderState(entry.material(), key.type(), key.environment());
DrawCall drawCall = new DrawCall(instancer, mesh, shaderState); DrawCall drawCall = new DrawCall(instancer, mesh, shaderState);
drawSet.put(shaderState, drawCall); drawSet.put(shaderState, drawCall);

View file

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

View file

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

View file

@ -1,65 +0,0 @@
package com.jozufozu.flywheel.impl.visualization;
import java.util.function.Supplier;
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.instance.Instancer;
import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.api.model.Model;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.api.visualization.VisualizationContext;
public class InstancerProviderImpl implements InstancerProvider, Supplier<VisualizationContext> {
protected final Engine engine;
protected final RenderStage renderStage;
public InstancerProviderImpl(Engine engine, RenderStage renderStage) {
this.engine = engine;
this.renderStage = renderStage;
}
@Override
public <I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model) {
return engine.instancer(type, model, renderStage);
}
@Override
public VisualizationContext get() {
return new VisualizationContextImpl(this, engine.renderOrigin());
}
public Embedded embed(VisualEmbedding world) {
return new Embedded(engine, world, renderStage);
}
@Override
public String toString() {
return "InstancerProviderImpl[" + "engine=" + engine + ", " + "renderStage=" + renderStage + ']';
}
public static final class Embedded extends InstancerProviderImpl {
private final VisualEmbedding world;
public Embedded(Engine engine, VisualEmbedding world, RenderStage renderStage) {
super(engine, renderStage);
this.world = world;
}
@Override
public <I extends Instance> Instancer<I> instancer(InstanceType<I> type, Model model) {
return engine.instancer(world, type, model, renderStage);
}
public VisualEmbedding world() {
return world;
}
@Override
public String toString() {
return "InstancerProviderImpl.EmbeddedImpl[" + "world=" + world + ", " + "engine=" + engine + ", " + "renderStage=" + renderStage + ']';
}
}
}

View file

@ -1,21 +0,0 @@
package com.jozufozu.flywheel.impl.visualization;
import com.jozufozu.flywheel.api.instance.InstancerProvider;
import com.jozufozu.flywheel.api.visualization.VisualEmbedding;
import com.jozufozu.flywheel.api.visualization.VisualizationContext;
import net.minecraft.core.Vec3i;
/**
* A context object passed on visual creation.
*
* @param instancerProvider The {@link InstancerProvider} that the visual can use to get instancers to render models.
* @param renderOrigin The origin of the renderer as a world position.
* All models render as if this position is (0, 0, 0).
*/
public record VisualizationContextImpl(InstancerProviderImpl instancerProvider, Vec3i renderOrigin) implements VisualizationContext {
@Override
public VisualizationContext embed(VisualEmbedding world) {
return new VisualizationContextImpl(instancerProvider.embed(world), renderOrigin);
}
}

View file

@ -3,6 +3,7 @@ package com.jozufozu.flywheel.impl.visualization;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.function.Supplier;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -22,6 +23,7 @@ import com.jozufozu.flywheel.api.visual.TickableVisual;
import com.jozufozu.flywheel.api.visual.VisualFrameContext; import com.jozufozu.flywheel.api.visual.VisualFrameContext;
import com.jozufozu.flywheel.api.visual.VisualTickContext; import com.jozufozu.flywheel.api.visual.VisualTickContext;
import com.jozufozu.flywheel.api.visualization.VisualManager; import com.jozufozu.flywheel.api.visualization.VisualManager;
import com.jozufozu.flywheel.api.visualization.VisualizationContext;
import com.jozufozu.flywheel.api.visualization.VisualizationLevel; import com.jozufozu.flywheel.api.visualization.VisualizationLevel;
import com.jozufozu.flywheel.api.visualization.VisualizationManager; import com.jozufozu.flywheel.api.visualization.VisualizationManager;
import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.config.FlwConfig;
@ -84,9 +86,9 @@ public class VisualizationManagerImpl implements VisualizationManager {
.createEngine(level); .createEngine(level);
taskExecutor = FlwTaskExecutor.get(); taskExecutor = FlwTaskExecutor.get();
var blockEntitiesStorage = new BlockEntityStorage(new InstancerProviderImpl(engine, RenderStage.AFTER_BLOCK_ENTITIES)); var blockEntitiesStorage = new BlockEntityStorage(provider(engine, RenderStage.AFTER_BLOCK_ENTITIES));
var entitiesStorage = new EntityStorage(new InstancerProviderImpl(engine, RenderStage.AFTER_ENTITIES)); var entitiesStorage = new EntityStorage(provider(engine, RenderStage.AFTER_ENTITIES));
var effectsStorage = new EffectStorage(new InstancerProviderImpl(engine, RenderStage.AFTER_PARTICLES)); var effectsStorage = new EffectStorage(provider(engine, RenderStage.AFTER_PARTICLES));
blockEntities = new VisualManagerImpl<>(blockEntitiesStorage); blockEntities = new VisualManagerImpl<>(blockEntitiesStorage);
entities = new VisualManagerImpl<>(entitiesStorage); entities = new VisualManagerImpl<>(entitiesStorage);
@ -118,6 +120,11 @@ public class VisualizationManagerImpl implements VisualizationManager {
} }
} }
public static Supplier<VisualizationContext> provider(Engine engine, RenderStage stage) {
var context = engine.createVisualizationContext(stage);
return () -> context;
}
private VisualFrameContext createVisualFrameContext(RenderContext ctx) { private VisualFrameContext createVisualFrameContext(RenderContext ctx) {
Vec3i renderOrigin = engine.renderOrigin(); Vec3i renderOrigin = engine.renderOrigin();
var cameraPos = ctx.camera() var cameraPos = ctx.camera()