diff --git a/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java index dd880b92b..733ed5739 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java @@ -1,12 +1,18 @@ package com.jozufozu.flywheel.backend; +import java.util.function.Supplier; + import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import net.minecraft.util.ResourceLocation; public interface IShaderContext
{ - P getProgram(ResourceLocation loc); + default P getProgram(ResourceLocation loc) { + return this.getProgramSupplier(loc).get(); + } + + Supplier
getProgramSupplier(ResourceLocation loc); /** * Load all programs associated with this context. This might be just one, if the context is very specialized. diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java index c899108b5..9cbfe3d54 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Supplier; import javax.annotation.Nullable; @@ -30,8 +31,8 @@ public abstract class ShaderContext
implements IShaderConte } @Override - public P getProgram(ResourceLocation spec) { - return programs.get(spec).get(); + public Supplier
getProgramSupplier(ResourceLocation spec) {
+ return programs.get(spec);
}
public Program loadAndLink(ProgramSpec spec, @Nullable ProgramState state) {
diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java
index 8d6878dd1..2c8762452 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java
@@ -4,20 +4,15 @@ import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL15;
-public abstract class MappedBuffer implements AutoCloseable {
+public abstract class MappedBuffer extends VecBuffer implements AutoCloseable {
protected boolean mapped;
protected final GlBuffer owner;
- protected ByteBuffer internal;
public MappedBuffer(GlBuffer owner) {
this.owner = owner;
}
- public void setInternal(ByteBuffer internal) {
- this.internal = internal;
- }
-
protected abstract void checkAndMap();
/**
@@ -38,20 +33,13 @@ public abstract class MappedBuffer implements AutoCloseable {
public MappedBuffer putFloatArray(float[] floats) {
checkAndMap();
-
- for (float f : floats) {
- internal.putFloat(f);
- }
-// internal.asFloatBuffer().put(floats);
-// internal.position(internal.position() + floats.length * 4);
-
+ super.putFloatArray(floats);
return this;
}
public MappedBuffer putByteArray(byte[] bytes) {
checkAndMap();
- internal.put(bytes);
-
+ super.putByteArray(bytes);
return this;
}
@@ -62,75 +50,61 @@ public abstract class MappedBuffer implements AutoCloseable {
*/
public MappedBuffer position(int p) {
checkAndMap();
- internal.position(p);
+ super.position(p);
return this;
}
public MappedBuffer putFloat(float f) {
checkAndMap();
- internal.putFloat(f);
+ super.putFloat(f);
return this;
}
public MappedBuffer putInt(int i) {
checkAndMap();
- internal.putInt(i);
+ super.putInt(i);
return this;
}
public MappedBuffer put(byte b) {
checkAndMap();
- internal.put(b);
+ super.put(b);
return this;
}
public MappedBuffer put(ByteBuffer b) {
checkAndMap();
- internal.put(b);
+ super.put(b);
return this;
}
public MappedBuffer putVec4(float x, float y, float z, float w) {
checkAndMap();
- internal.putFloat(x);
- internal.putFloat(y);
- internal.putFloat(z);
- internal.putFloat(w);
-
+ super.putVec4(x, y, z, w);
return this;
}
public MappedBuffer putVec3(float x, float y, float z) {
checkAndMap();
- internal.putFloat(x);
- internal.putFloat(y);
- internal.putFloat(z);
-
+ super.putVec3(x, y, z);
return this;
}
public MappedBuffer putVec2(float x, float y) {
checkAndMap();
- internal.putFloat(x);
- internal.putFloat(y);
-
+ super.putVec2(x, y);
return this;
}
public MappedBuffer putVec3(byte x, byte y, byte z) {
checkAndMap();
- internal.put(x);
- internal.put(y);
- internal.put(z);
-
+ super.putVec3(x, y, z);
return this;
}
public MappedBuffer putVec2(byte x, byte y) {
checkAndMap();
- internal.put(x);
- internal.put(y);
-
+ super.putVec2(x, y);
return this;
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java
new file mode 100644
index 000000000..03540e7fb
--- /dev/null
+++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java
@@ -0,0 +1,116 @@
+package com.jozufozu.flywheel.backend.gl.buffer;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public class VecBuffer {
+
+ protected ByteBuffer internal;
+
+ public VecBuffer() {
+ }
+
+ public VecBuffer(ByteBuffer internal) {
+ this.internal = internal;
+ }
+
+ public static VecBuffer allocate(int bytes) {
+ ByteBuffer buffer = ByteBuffer.allocate(bytes);
+ buffer.order(ByteOrder.nativeOrder());
+ return new VecBuffer(buffer);
+ }
+
+ protected void setInternal(ByteBuffer internal) {
+ this.internal = internal;
+ }
+
+ public ByteBuffer unwrap() {
+ return internal;
+ }
+
+ public VecBuffer rewind() {
+ internal.rewind();
+
+ return this;
+ }
+
+ public VecBuffer putFloatArray(float[] floats) {
+
+ for (float f : floats) {
+ internal.putFloat(f);
+ }
+// internal.asFloatBuffer().put(floats);
+// internal.position(internal.position() + floats.length * 4);
+
+ return this;
+ }
+
+ public VecBuffer putByteArray(byte[] bytes) {
+ internal.put(bytes);
+ return this;
+ }
+
+ /**
+ * Position this buffer relative to the 0-index in GPU memory.
+ *
+ * @return This buffer.
+ */
+ public VecBuffer position(int p) {
+ internal.position(p);
+ return this;
+ }
+
+ public VecBuffer putFloat(float f) {
+ internal.putFloat(f);
+ return this;
+ }
+
+ public VecBuffer putInt(int i) {
+ internal.putInt(i);
+ return this;
+ }
+
+ public VecBuffer put(byte b) {
+ internal.put(b);
+ return this;
+ }
+
+ public VecBuffer put(ByteBuffer b) {
+ internal.put(b);
+ return this;
+ }
+
+ public VecBuffer putVec4(float x, float y, float z, float w) {
+ internal.putFloat(x);
+ internal.putFloat(y);
+ internal.putFloat(z);
+ internal.putFloat(w);
+ return this;
+ }
+
+ public VecBuffer putVec3(float x, float y, float z) {
+ internal.putFloat(x);
+ internal.putFloat(y);
+ internal.putFloat(z);
+ return this;
+ }
+
+ public VecBuffer putVec2(float x, float y) {
+ internal.putFloat(x);
+ internal.putFloat(y);
+ return this;
+ }
+
+ public VecBuffer putVec3(byte x, byte y, byte z) {
+ internal.put(x);
+ internal.put(y);
+ internal.put(z);
+ return this;
+ }
+
+ public VecBuffer putVec2(byte x, byte y) {
+ internal.put(x);
+ internal.put(y);
+ return this;
+ }
+}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java
index cea5c9094..6f1a1a63e 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java
@@ -54,6 +54,10 @@ public class InstanceMaterial {
public static int MAX_ORIGIN_DISTANCE = 100;
- protected final ArrayList context;
+
+ protected final Map context) {
+ this.context = context;
+
+ this.atlasMaterials = new HashMap<>();
+ this.atlasRenderers = new ArrayList<>(Backend.getInstance().allMaterials().size());
+
this.materials = new HashMap<>();
- this.renderers = new ArrayList<>(Backend.getInstance().allMaterials().size());
+ this.renderers = new HashMap<>();
+
this.listeners = new WeakHashSet<>();
-
- for (MaterialSpec> spec : Backend.getInstance().allMaterials()) {
- InstanceMaterial> material = new InstanceMaterial<>(this::getOriginCoordinate, spec);
- materials.put(spec.name, material);
- MaterialRenderer renderer = new MaterialRenderer<>(context.getProgram(spec.getProgramName()), material);
- renderers.add(renderer);
- }
-
}
/**
@@ -72,20 +76,47 @@ public class MaterialManager {
translate.multiplyBackward(viewProjection);
- for (MaterialRenderer material : renderers) {
+ for (MaterialRenderer material : atlasRenderers) {
material.render(layer, translate, camX, camY, camZ, callback);
}
+
+ for (Map.Entry materialRenderer : entry.getValue()) {
+ materialRenderer.render(layer, translate, camX, camY, camZ, callback);
+ }
+ }
}
public void delete() {
- for (InstanceMaterial> material : materials.values()) {
- material.delete();
- }
+ atlasMaterials.values().forEach(InstanceMaterial::delete);
+
+ materials.values().stream().flatMap(m -> m.values().stream()).forEach(InstanceMaterial::delete);
}
@SuppressWarnings("unchecked")
public {
originCoordinate = new BlockPos(cX, cY, cZ);
- materials.values().forEach(InstanceMaterial::clear);
+ materials.values().stream().flatMap(m -> m.values().stream()).forEach(InstanceMaterial::clear);
+ atlasMaterials.values().forEach(InstanceMaterial::clear);
listeners.forEach(OriginShiftListener::onOriginShift);
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialRenderer.java
index 85f4a3581..1a8b8d1da 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialRenderer.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialRenderer.java
@@ -1,5 +1,7 @@
package com.jozufozu.flywheel.backend.instancing;
+import java.util.function.Supplier;
+
import com.jozufozu.flywheel.core.shader.IProgramCallback;
import com.jozufozu.flywheel.core.shader.WorldProgram;
@@ -8,17 +10,21 @@ import net.minecraft.util.math.vector.Matrix4f;
public class MaterialRenderer {
- private final P program;
+ private final Supplier program;
private final InstanceMaterial> material;
- public MaterialRenderer(P program, InstanceMaterial> material) {
- this.program = program;
+ public MaterialRenderer(Supplier programSupplier, InstanceMaterial> material) {
+ this.program = programSupplier;
this.material = material;
}
public void render(RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ, IProgramCallback setup) {
if (!(layer == RenderType.getCutoutMipped())) return;
+ if (material.nothingToRender()) return;
+
+ P program = this.program.get();
+
program.bind();
program.uploadViewProjection(viewProjection);
program.uploadCameraPos(camX, camY, camZ);
diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java
index b50b3e214..405bc4772 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java
@@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.instancing;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
+import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.util.ResourceLocation;
public class MaterialSpec
*/
-public interface IMultiProgram {
+public interface IMultiProgram extends Supplier {
/**
* Get the shader program most suited for the current game state.
diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java
index 2c8b9e65e..f05438baa 100644
--- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java
+++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java
@@ -1,8 +1,6 @@
package com.simibubi.create.content.contraptions.components.structureMovement.glue;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
+import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.instancing.ITickableInstance;
import com.jozufozu.flywheel.backend.instancing.Instancer;
import com.jozufozu.flywheel.backend.instancing.MaterialManager;
@@ -14,23 +12,28 @@ import com.jozufozu.flywheel.core.Materials;
import com.jozufozu.flywheel.core.instancing.ConditionalInstance;
import com.jozufozu.flywheel.core.materials.OrientedData;
import com.simibubi.create.AllItems;
+import com.simibubi.create.Create;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.VecHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Direction;
+import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3d;
public class GlueInstance extends EntityInstance