From b9352bc3a426188d62f6910eaa812f8ba2180021 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 6 Dec 2021 01:47:52 -0800 Subject: [PATCH] Vanillaization - IModels now accept a VertexConsumer - IRenderState -> RenderType - AbstractInstancer for dealing with InstanceData tidyness - Alter crumbling renderer to match new system - Add hack to keep track of bound atlas textures --- .../java/com/jozufozu/flywheel/Flywheel.java | 4 + .../flywheel/backend/gl/GlTextureUnit.java | 8 ++ .../flywheel/backend/gl/error/GlError.java | 4 +- .../backend/instancing/AbstractInstancer.java | 128 ++++++++++++++++++ .../backend/instancing/GPUInstancer.java | 125 +---------------- .../backend/instancing/InstanceWorld.java | 7 - .../backend/material/MaterialGroupImpl.java | 15 +- .../backend/material/MaterialImpl.java | 9 +- .../backend/material/MaterialManager.java | 20 ++- .../backend/material/MaterialManagerImpl.java | 28 ++-- .../flywheel/backend/model/BufferedModel.java | 3 +- .../flywheel/backend/model/ModelPool.java | 10 +- .../flywheel/backend/state/IRenderState.java | 19 --- .../backend/state/NoCullRenderState.java | 20 --- .../flywheel/backend/state/RenderState.java | 81 ----------- .../backend/state/TextureRenderState.java | 64 --------- .../flywheel/backend/struct/StructType.java | 8 +- .../flywheel/backend/struct/Writeable.java | 17 +++ .../core/crumbling/CrumblingGroup.java | 41 +++++- .../core/crumbling/CrumblingRenderer.java | 32 ++--- .../core/materials/model/ModelType.java | 4 +- .../core/materials/oriented/OrientedType.java | 4 +- .../flywheel/core/model/BakedModelModel.java | 13 +- .../flywheel/core/model/BlockModel.java | 13 +- .../jozufozu/flywheel/core/model/IModel.java | 4 +- .../flywheel/core/model/ModelPart.java | 4 +- .../flywheel/core/model/PartBuilder.java | 14 +- .../core/model/VecBufferConsumer.java | 69 ++++++++++ .../flywheel/core/model/WorldModel.java | 17 ++- .../flywheel/core/shader/WorldProgram.java | 2 +- .../flywheel/event/RenderLayerEvent.java | 5 + .../flywheel/mixin/RenderTexturesMixin.java | 20 +++ .../flywheel/mixin/ShaderInstanceMixin.java | 59 -------- .../flywheel/util/RenderTextures.java | 26 ++++ .../jozufozu/flywheel/util/TextureBinder.java | 23 ++-- .../flywheel/vanilla/ChestInstance.java | 6 +- .../flywheel/vanilla/MinecartInstance.java | 4 +- .../flywheel/vanilla/RenderStates.java | 14 -- .../flywheel/vanilla/ShulkerBoxInstance.java | 5 +- src/main/resources/flywheel.mixins.json | 56 ++++---- 40 files changed, 462 insertions(+), 543 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/state/IRenderState.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/state/NoCullRenderState.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/state/RenderState.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/struct/Writeable.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/RenderTexturesMixin.java delete mode 100644 src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/RenderTextures.java delete mode 100644 src/main/java/com/jozufozu/flywheel/vanilla/RenderStates.java diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 3a34d5989..3f8f8c1cd 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -1,5 +1,8 @@ package com.jozufozu.flywheel; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.config.FlwPackets; @@ -16,6 +19,7 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; public class Flywheel { public static final String ID = "flywheel"; + public static final Logger log = LogManager.getLogger(Flywheel.class); public Flywheel() { FMLJavaModLoadingContext.get() diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlTextureUnit.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlTextureUnit.java index fbd28c63a..c599c3cb4 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlTextureUnit.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlTextureUnit.java @@ -51,4 +51,12 @@ public enum GlTextureUnit { public void makeActive() { GlStateManager._activeTexture(glEnum); } + + public static GlTextureUnit getActive() { + return fromGlEnum(GlStateManager._getActiveTexture()); + } + + public static GlTextureUnit fromGlEnum(int glEnum) { + return GlTextureUnit.values()[glEnum - GL_TEXTURE0]; + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java index 3df53b71b..55355ddbc 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java @@ -5,6 +5,8 @@ import java.util.function.Supplier; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; +import com.jozufozu.flywheel.Flywheel; + import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -42,7 +44,7 @@ public enum GlError { // TODO: build flag? to enable or disable this function GlError poll = GlError.poll(); if (poll != null) { - //throw new GlException(poll, context.get()); + Flywheel.log.error("{}: {}", poll.name(), context.get()); } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java new file mode 100644 index 000000000..c27178984 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java @@ -0,0 +1,128 @@ +package com.jozufozu.flywheel.backend.instancing; + +import java.util.ArrayList; +import java.util.BitSet; + +import com.jozufozu.flywheel.backend.struct.StructType; +import com.jozufozu.flywheel.core.model.IModel; + +public class AbstractInstancer implements Instancer { + + protected final StructType type; + protected final IModel modelData; + protected final ArrayList data = new ArrayList<>(); + + boolean anyToRemove; + boolean anyToUpdate; + + public AbstractInstancer(StructType type, IModel modelData) { + this.type = type; + this.modelData = modelData; + } + + /** + * @return a handle to a new copy of this model. + */ + @Override + public D createInstance() { + D data = type.create(); + data.owner = this; + return _add(data); + } + + /** + * Copy a data from another Instancer to this. + * + * This has the effect of swapping out one model for another. + * @param inOther the data associated with a different model. + */ + @Override + public void stealInstance(D inOther) { + if (inOther.owner == this) return; + + inOther.delete(); + // sike, we want to keep it, changing the owner reference will still delete it in the other + inOther.removed = false; + _add(inOther); + } + + @Override + public void markDirty(InstanceData instanceData) { + anyToUpdate = true; + instanceData.dirty = true; + } + + @Override + public void markRemoval(InstanceData instanceData) { + anyToRemove = true; + instanceData.removed = true; + } + + /** + * Clear all instance data without freeing resources. + */ + public void clear() { + data.clear(); + anyToRemove = true; + } + + protected BitSet getDirtyBitSet() { + final int size = data.size(); + final BitSet dirtySet = new BitSet(size); + + for (int i = 0; i < size; i++) { + D element = data.get(i); + if (element.dirty) { + dirtySet.set(i); + + element.dirty = false; + } + } + return dirtySet; + } + + protected void removeDeletedInstances() { + // Figure out which elements are to be removed. + final int oldSize = this.data.size(); + int removeCount = 0; + final BitSet removeSet = new BitSet(oldSize); + for (int i = 0; i < oldSize; i++) { + final D element = this.data.get(i); + if (element.removed || element.owner != this) { + removeSet.set(i); + removeCount++; + } + } + + final int newSize = oldSize - removeCount; + + // shift surviving elements left over the spaces left by removed elements + for (int i = 0, j = 0; (i < oldSize) && (j < newSize); i++, j++) { + i = removeSet.nextClearBit(i); + + if (i != j) { + D element = data.get(i); + data.set(j, element); + element.dirty = true; + } + } + + anyToUpdate = true; + + data.subList(newSize, oldSize) + .clear(); + + } + + private D _add(D instanceData) { + instanceData.owner = this; + + instanceData.dirty = true; + anyToUpdate = true; + synchronized (data) { + data.add(instanceData); + } + + return instanceData; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java index 364d406c6..d80982c38 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java @@ -1,6 +1,5 @@ package com.jozufozu.flywheel.backend.instancing; -import java.util.ArrayList; import java.util.BitSet; import com.jozufozu.flywheel.backend.Backend; @@ -17,12 +16,10 @@ import com.jozufozu.flywheel.backend.struct.StructWriter; import com.jozufozu.flywheel.core.model.IModel; import com.jozufozu.flywheel.util.AttribUtil; -public class GPUInstancer implements Instancer { +public class GPUInstancer extends AbstractInstancer { private final ModelAllocator modelAllocator; - private final IModel modelData; private final VertexFormat instanceFormat; - private final StructType type; private IBufferedModel model; private GlVertexArray vao; @@ -32,44 +29,12 @@ public class GPUInstancer implements Instancer { private boolean deleted; private boolean initialized; - private final ArrayList data = new ArrayList<>(); - - boolean anyToRemove; - boolean anyToUpdate; - - public GPUInstancer(ModelAllocator modelAllocator, IModel model, StructType type) { + public GPUInstancer(StructType type, IModel model, ModelAllocator modelAllocator) { + super(type, model); this.modelAllocator = modelAllocator; - this.modelData = model; - this.type = type; this.instanceFormat = type.format(); } - /** - * @return a handle to a new copy of this model. - */ - @Override - public D createInstance() { - D data = type.create(); - data.owner = this; - return _add(data); - } - - /** - * Copy a data from another Instancer to this. - * - * This has the effect of swapping out one model for another. - * @param inOther the data associated with a different model. - */ - @Override - public void stealInstance(D inOther) { - if (inOther.owner == this) return; - - inOther.delete(); - // sike, we want to keep it, changing the owner reference will still delete it in the other - inOther.removed = false; - _add(inOther); - } - public void render() { if (invalid()) return; @@ -127,14 +92,6 @@ public class GPUInstancer implements Instancer { return !anyToUpdate && !anyToRemove && glInstanceCount == 0; } - /** - * Clear all instance data without freeing resources. - */ - public void clear() { - data.clear(); - anyToRemove = true; - } - /** * Free acquired resources. All other Instancer methods are undefined behavior after calling delete. */ @@ -149,18 +106,6 @@ public class GPUInstancer implements Instancer { vao.delete(); } - private D _add(D instanceData) { - instanceData.owner = this; - - instanceData.dirty = true; - anyToUpdate = true; - synchronized (data) { - data.add(instanceData); - } - - return instanceData; - } - protected void renderSetup() { if (anyToRemove) { removeDeletedInstances(); @@ -215,7 +160,7 @@ public class GPUInstancer implements Instancer { if (length > 0) { MappedBuffer mapped = instanceVBO.getBuffer(offset, length); - StructWriter writer = type.getWriter(mapped); + StructWriter writer = type.asWriteable().getWriter(mapped); dirtySet.stream() .forEach(i -> { @@ -226,21 +171,6 @@ public class GPUInstancer implements Instancer { } } - private BitSet getDirtyBitSet() { - final int size = data.size(); - final BitSet dirtySet = new BitSet(size); - - for (int i = 0; i < size; i++) { - D element = data.get(i); - if (element.dirty) { - dirtySet.set(i); - - element.dirty = false; - } - } - return dirtySet; - } - private boolean realloc() { int size = this.data.size(); int stride = instanceFormat.getStride(); @@ -250,7 +180,7 @@ public class GPUInstancer implements Instancer { instanceVBO.alloc(glBufferSize); MappedBuffer buffer = instanceVBO.getBuffer(0, glBufferSize); - StructWriter writer = type.getWriter(buffer); + StructWriter writer = type.asWriteable().getWriter(buffer); for (D datum : data) { writer.write(datum); } @@ -265,39 +195,6 @@ public class GPUInstancer implements Instancer { return false; } - private void removeDeletedInstances() { - // Figure out which elements are to be removed. - final int oldSize = this.data.size(); - int removeCount = 0; - final BitSet removeSet = new BitSet(oldSize); - for (int i = 0; i < oldSize; i++) { - final D element = this.data.get(i); - if (element.removed || element.owner != this) { - removeSet.set(i); - removeCount++; - } - } - - final int newSize = oldSize - removeCount; - - // shift surviving elements left over the spaces left by removed elements - for (int i = 0, j = 0; (i < oldSize) && (j < newSize); i++, j++) { - i = removeSet.nextClearBit(i); - - if (i != j) { - D element = data.get(i); - data.set(j, element); - element.dirty = true; - } - } - - anyToUpdate = true; - - data.subList(newSize, oldSize) - .clear(); - - } - private void informAttribDivisors() { int staticAttributes = model.getAttributeCount(); instanceFormat.vertexAttribPointers(staticAttributes); @@ -306,16 +203,4 @@ public class GPUInstancer implements Instancer { Backend.getInstance().compat.instancedArrays.vertexAttribDivisor(i + staticAttributes, 1); } } - - @Override - public void markDirty(InstanceData instanceData) { - anyToUpdate = true; - instanceData.dirty = true; - } - - @Override - public void markRemoval(InstanceData instanceData) { - anyToRemove = true; - instanceData.removed = true; - } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java index 0e454d2f5..4f1d0f8a6 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -9,7 +9,6 @@ import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.util.ChunkIter; -import com.jozufozu.flywheel.util.TextureBinder; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; @@ -100,12 +99,6 @@ public class InstanceWorld { * Draw the given layer. */ public void renderLayer(RenderLayerEvent event) { - event.type.setupRenderState(); - - TextureBinder.bindActiveTextures(); - materialManager.render(event.layer, event.viewProjection, event.camX, event.camY, event.camZ); - - event.type.clearRenderState(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java index 6aae0c6d1..f8c8405c3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java @@ -5,10 +5,12 @@ import java.util.HashMap; import java.util.Map; import com.jozufozu.flywheel.backend.instancing.InstanceData; -import com.jozufozu.flywheel.backend.state.IRenderState; import com.jozufozu.flywheel.core.shader.WorldProgram; +import com.jozufozu.flywheel.util.TextureBinder; import com.mojang.math.Matrix4f; +import net.minecraft.client.renderer.RenderType; + /** * A group of materials all rendered with the same GL state. * @@ -18,15 +20,13 @@ import com.mojang.math.Matrix4f; public class MaterialGroupImpl

implements MaterialGroup { protected final MaterialManagerImpl

owner; - protected final IRenderState state; - private final ArrayList> renderers = new ArrayList<>(); + protected final ArrayList> renderers = new ArrayList<>(); private final Map, MaterialImpl> materials = new HashMap<>(); - public MaterialGroupImpl(MaterialManagerImpl

owner, IRenderState state) { + public MaterialGroupImpl(MaterialManagerImpl

owner) { this.owner = owner; - this.state = state; } /** @@ -41,10 +41,13 @@ public class MaterialGroupImpl

implements MaterialGroup return (MaterialImpl) materials.computeIfAbsent(spec, this::createInstanceMaterial); } - public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { + public void render(RenderType type, Matrix4f viewProjection, double camX, double camY, double camZ) { + type.setupRenderState(); + TextureBinder.bindActiveTextures(); for (MaterialRenderer

renderer : renderers) { renderer.render(viewProjection, camX, camY, camZ); } + type.clearRenderState(); } public void setup(P program) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialImpl.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialImpl.java index ce3fa98d0..acef52c2c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialImpl.java @@ -10,6 +10,7 @@ import com.jozufozu.flywheel.backend.instancing.GPUInstancer; import com.jozufozu.flywheel.backend.instancing.InstanceData; import com.jozufozu.flywheel.backend.instancing.Instancer; import com.jozufozu.flywheel.backend.model.ModelPool; +import com.jozufozu.flywheel.backend.struct.StructType; import com.jozufozu.flywheel.core.model.IModel; /** @@ -20,12 +21,12 @@ public class MaterialImpl implements Material { final ModelPool modelPool; protected final Cache> models; - protected final MaterialSpec spec; + protected final StructType type; public MaterialImpl(MaterialSpec spec) { - this.spec = spec; + this.type = spec.getInstanceType(); - modelPool = new ModelPool(spec.getModelFormat(), spec.getModelFormat().getStride() * 64); + modelPool = new ModelPool(spec.getModelFormat(), 64); this.models = CacheBuilder.newBuilder() .removalListener(notification -> { GPUInstancer instancer = (GPUInstancer) notification.getValue(); @@ -44,7 +45,7 @@ public class MaterialImpl implements Material { @Override public Instancer model(Object key, Supplier modelSupplier) { try { - return models.get(key, () -> new GPUInstancer<>(modelPool, modelSupplier.get(), spec.getInstanceType())); + return models.get(key, () -> new GPUInstancer<>(type, modelSupplier.get(), modelPool)); } catch (ExecutionException e) { throw new RuntimeException("error creating instancer", e); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java index 639bfb1ed..5c53ed7a3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java @@ -1,11 +1,9 @@ package com.jozufozu.flywheel.backend.material; -import com.jozufozu.flywheel.backend.state.IRenderState; import com.jozufozu.flywheel.backend.state.RenderLayer; -import com.jozufozu.flywheel.backend.state.TextureRenderState; +import net.minecraft.client.renderer.RenderType; import net.minecraft.core.Vec3i; -import net.minecraft.world.inventory.InventoryMenu; public interface MaterialManager { @@ -13,34 +11,34 @@ public interface MaterialManager { * Get a material group that will render in the given layer with the given state. * * @param layer The {@link RenderLayer} you want to draw in. - * @param state The {@link IRenderState} you need to draw with. + * @param state The {@link net.minecraft.client.renderer.RenderType} you need to draw with. * @return A material group whose children will */ - MaterialGroup state(RenderLayer layer, IRenderState state); + MaterialGroup state(RenderLayer layer, RenderType state); Vec3i getOriginCoordinate(); - default MaterialGroup solid(IRenderState state) { + default MaterialGroup solid(RenderType state) { return state(RenderLayer.SOLID, state); } - default MaterialGroup cutout(IRenderState state) { + default MaterialGroup cutout(RenderType state) { return state(RenderLayer.CUTOUT, state); } - default MaterialGroup transparent(IRenderState state) { + default MaterialGroup transparent(RenderType state) { return state(RenderLayer.TRANSPARENT, state); } default MaterialGroup defaultSolid() { - return solid(TextureRenderState.get(InventoryMenu.BLOCK_ATLAS)); + return solid(RenderType.solid()); } default MaterialGroup defaultCutout() { - return cutout(TextureRenderState.get(InventoryMenu.BLOCK_ATLAS)); + return cutout(RenderType.cutout()); } default MaterialGroup defaultTransparent() { - return transparent(TextureRenderState.get(InventoryMenu.BLOCK_ATLAS)); + return transparent(RenderType.translucent()); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java index ff32b3eaf..3c49410fd 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java @@ -5,7 +5,6 @@ import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; -import com.jozufozu.flywheel.backend.state.IRenderState; import com.jozufozu.flywheel.backend.state.RenderLayer; import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.shader.WorldProgram; @@ -13,6 +12,7 @@ import com.jozufozu.flywheel.util.WeakHashSet; import com.mojang.math.Matrix4f; import net.minecraft.client.Camera; +import net.minecraft.client.renderer.RenderType; import net.minecraft.core.BlockPos; import net.minecraft.core.Vec3i; import net.minecraft.resources.ResourceLocation; @@ -28,7 +28,7 @@ public class MaterialManagerImpl

implements MaterialMana protected final GroupFactory

groupFactory; protected final boolean ignoreOriginCoordinate; - protected final Map>> layers; + protected final Map>> layers; private final WeakHashSet listeners; @@ -57,12 +57,12 @@ public class MaterialManagerImpl

implements MaterialMana * Get a material group that will render in the given layer with the given state. * * @param layer The {@link RenderLayer} you want to draw in. - * @param state The {@link IRenderState} you need to draw with. + * @param state The {@link RenderType} you need to draw with. * @return A material group whose children will */ @Override - public MaterialGroup state(RenderLayer layer, IRenderState state) { - return layers.get(layer).computeIfAbsent(state, this::createGroup); + public MaterialGroup state(RenderLayer layer, RenderType state) { + return layers.get(layer).computeIfAbsent(state, $ -> groupFactory.create(this)); } /** @@ -83,18 +83,16 @@ public class MaterialManagerImpl

implements MaterialMana viewProjection = translate; } - for (Map.Entry> entry : layers.get(layer).entrySet()) { - IRenderState state = entry.getKey(); + for (Map.Entry> entry : layers.get(layer).entrySet()) { + RenderType state = entry.getKey(); MaterialGroupImpl

group = entry.getValue(); - state.bind(); - group.render(viewProjection, camX, camY, camZ); - state.unbind(); + group.render(state, viewProjection, camX, camY, camZ); } } public void delete() { - for (Map> groups : layers.values()) { + for (Map> groups : layers.values()) { groups.values().forEach(MaterialGroupImpl::delete); } @@ -131,7 +129,7 @@ public class MaterialManagerImpl

implements MaterialMana originCoordinate = new BlockPos(cX, cY, cZ); - for (Map> groups : layers.values()) { + for (Map> groups : layers.values()) { groups.values().forEach(MaterialGroupImpl::clear); } @@ -139,10 +137,6 @@ public class MaterialManagerImpl

implements MaterialMana } } - private MaterialGroupImpl

createGroup(IRenderState state) { - return groupFactory.create(this, state); - } - @FunctionalInterface public interface OriginShiftListener { void onOriginShift(); @@ -150,7 +144,7 @@ public class MaterialManagerImpl

implements MaterialMana @FunctionalInterface public interface GroupFactory

{ - MaterialGroupImpl

create(MaterialManagerImpl

materialManager, IRenderState state); + MaterialGroupImpl

create(MaterialManagerImpl

materialManager); } public static class Builder

{ diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java b/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java index e06f7be4b..c5f552abf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java @@ -10,6 +10,7 @@ import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.core.model.IModel; +import com.jozufozu.flywheel.core.model.VecBufferConsumer; import com.jozufozu.flywheel.util.AttribUtil; public class BufferedModel implements IBufferedModel { @@ -31,7 +32,7 @@ public class BufferedModel implements IBufferedModel { // mirror it in system memory so we can write to it, and upload our model. MappedBuffer buffer = vbo.getBuffer(0, model.size()); - model.buffer(buffer); + model.buffer(new VecBufferConsumer(buffer, model.format())); buffer.flush(); vbo.unbind(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java b/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java index c905ca44f..8f74f8658 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java @@ -11,6 +11,7 @@ import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.core.model.IModel; +import com.jozufozu.flywheel.core.model.VecBufferConsumer; import com.jozufozu.flywheel.util.AttribUtil; public class ModelPool implements ModelAllocator { @@ -31,7 +32,7 @@ public class ModelPool implements ModelAllocator { public ModelPool(VertexFormat format, int initialSize) { this.format = format; - bufferSize = initialSize; + bufferSize = format.getStride() * initialSize; vbo = new MappedGlBuffer(GlBufferType.ARRAY_BUFFER); @@ -115,8 +116,10 @@ public class ModelPool implements ModelAllocator { private void uploadAll() { MappedBuffer buffer = vbo.getBuffer(0, bufferSize); + VecBufferConsumer consumer = new VecBufferConsumer(buffer, format); + for (PooledModel model : models) { - model.model.buffer(buffer); + model.model.buffer(consumer); if (model.callback != null) model.callback.onAlloc(model); } @@ -126,12 +129,13 @@ public class ModelPool implements ModelAllocator { private void uploadPending() { MappedBuffer buffer = vbo.getBuffer(0, bufferSize); + VecBufferConsumer consumer = new VecBufferConsumer(buffer, format); int stride = format.getStride(); for (PooledModel model : pendingUpload) { int pos = model.first * stride; buffer.position(pos); - model.model.buffer(buffer); + model.model.buffer(consumer); if (model.callback != null) model.callback.onAlloc(model); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/state/IRenderState.java b/src/main/java/com/jozufozu/flywheel/backend/state/IRenderState.java deleted file mode 100644 index b24a755f8..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/state/IRenderState.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.jozufozu.flywheel.backend.state; - -import javax.annotation.Nullable; - -import com.jozufozu.flywheel.backend.gl.GlTextureUnit; - -import net.minecraft.resources.ResourceLocation; - -public interface IRenderState { - - void bind(); - - void unbind(); - - @Nullable - default ResourceLocation getTexture(GlTextureUnit textureUnit) { - return null; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/state/NoCullRenderState.java b/src/main/java/com/jozufozu/flywheel/backend/state/NoCullRenderState.java deleted file mode 100644 index dc5f6ff10..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/state/NoCullRenderState.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jozufozu.flywheel.backend.state; - -import com.mojang.blaze3d.platform.GlStateManager; - -public class NoCullRenderState implements IRenderState { - - public static final NoCullRenderState INSTANCE = new NoCullRenderState(); - - protected NoCullRenderState() { } - - @Override - public void bind() { - GlStateManager._disableCull(); - } - - @Override - public void unbind() { - GlStateManager._enableCull(); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/state/RenderState.java b/src/main/java/com/jozufozu/flywheel/backend/state/RenderState.java deleted file mode 100644 index 3bbb5bd37..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/state/RenderState.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.jozufozu.flywheel.backend.state; - -import java.util.EnumMap; -import java.util.Map; -import java.util.Objects; - -import javax.annotation.Nullable; - -import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.backend.gl.GlTextureUnit; - -import net.minecraft.resources.ResourceLocation; - -public class RenderState implements IRenderState { - - private final Map textures; - private final ImmutableList states; - - public RenderState(Map textures, ImmutableList states) { - this.textures = textures; - this.states = states; - } - - @Override - public void bind() { - states.forEach(IRenderState::bind); - } - - @Override - public void unbind() { - states.forEach(IRenderState::unbind); - } - - @Nullable - @Override - public ResourceLocation getTexture(GlTextureUnit textureUnit) { - return textures.get(textureUnit); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - RenderState that = (RenderState) o; - return states.equals(that.states); - } - - @Override - public int hashCode() { - return Objects.hash(states); - } - - public static StateBuilder builder() { - return new StateBuilder(); - } - - public static class StateBuilder { - private final ImmutableList.Builder states = ImmutableList.builder(); - private final Map textures = new EnumMap<>(GlTextureUnit.class); - - public StateBuilder texture(ResourceLocation name) { - return addState(TextureRenderState.get(name)); - } - - public StateBuilder addState(IRenderState state) { - if (state instanceof TextureRenderState) { - TextureRenderState tex = (TextureRenderState) state; - if (textures.put(tex.unit, tex.location) == null) { - states.add(state); - } - } else { - states.add(state); - } - return this; - } - - public RenderState build() { - return new RenderState(textures, states.build()); - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java b/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java deleted file mode 100644 index b7c43aa75..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.jozufozu.flywheel.backend.state; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -import javax.annotation.Nullable; - -import com.jozufozu.flywheel.backend.gl.GlTextureUnit; -import com.jozufozu.flywheel.util.Pair; - -import net.minecraft.client.Minecraft; -import net.minecraft.resources.ResourceLocation; - -public class TextureRenderState implements IRenderState { - private static final Map, TextureRenderState> states = new HashMap<>(); - - public final GlTextureUnit unit; - public final ResourceLocation location; - - private TextureRenderState(GlTextureUnit unit, ResourceLocation location) { - this.unit = unit; - this.location = location; - } - - public static TextureRenderState get(ResourceLocation texture) { - return get(GlTextureUnit.T0, texture); - } - - public static TextureRenderState get(GlTextureUnit unit, ResourceLocation texture) { - return states.computeIfAbsent(Pair.of(unit, texture), p -> new TextureRenderState(p.getFirst(), p.getSecond())); - } - - @Override - public void bind() { - unit.makeActive(); - Minecraft.getInstance().getTextureManager().bindForSetup(location); - } - - @Override - public void unbind() { - - } - - @Nullable - @Override - public ResourceLocation getTexture(GlTextureUnit textureUnit) { - if (textureUnit == unit) return location; - return null; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TextureRenderState that = (TextureRenderState) o; - return location.equals(that.location); - } - - @Override - public int hashCode() { - return Objects.hash(location); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/struct/StructType.java b/src/main/java/com/jozufozu/flywheel/backend/struct/StructType.java index a79bc85f8..fc1d7be41 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/struct/StructType.java +++ b/src/main/java/com/jozufozu/flywheel/backend/struct/StructType.java @@ -1,7 +1,6 @@ package com.jozufozu.flywheel.backend.struct; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; /** * A StructType contains metadata for a specific instance struct that Flywheel can interface with. @@ -19,10 +18,5 @@ public interface StructType { */ VertexFormat format(); - /** - * Create a {@link StructWriter} that will consume instances of S and write them to the given buffer. - * - * @param backing The buffer that the StructWriter will write to. - */ - StructWriter getWriter(VecBuffer backing); + Writeable asWriteable(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/struct/Writeable.java b/src/main/java/com/jozufozu/flywheel/backend/struct/Writeable.java new file mode 100644 index 000000000..869e47ed4 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/struct/Writeable.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.backend.struct; + +import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; + +public interface Writeable extends StructType { + /** + * Create a {@link StructWriter} that will consume instances of S and write them to the given buffer. + * + * @param backing The buffer that the StructWriter will write to. + */ + StructWriter getWriter(VecBuffer backing); + + @Override + default Writeable asWriteable() { + return this; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java index 59614b28a..3c403ebcd 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -1,23 +1,34 @@ package com.jozufozu.flywheel.core.crumbling; -import com.jozufozu.flywheel.backend.gl.GlTextureUnit; import com.jozufozu.flywheel.backend.material.MaterialGroupImpl; import com.jozufozu.flywheel.backend.material.MaterialManagerImpl; -import com.jozufozu.flywheel.backend.state.IRenderState; +import com.jozufozu.flywheel.backend.material.MaterialRenderer; import com.jozufozu.flywheel.core.atlas.AtlasInfo; import com.jozufozu.flywheel.core.atlas.SheetData; +import com.jozufozu.flywheel.util.RenderTextures; +import com.jozufozu.flywheel.util.TextureBinder; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.math.Matrix4f; +import net.minecraft.client.renderer.RenderType; import net.minecraft.resources.ResourceLocation; public class CrumblingGroup

extends MaterialGroupImpl

{ - private final int width; - private final int height; + private int width; + private int height; - public CrumblingGroup(MaterialManagerImpl

owner, IRenderState state) { - super(owner, state); + public CrumblingGroup(MaterialManagerImpl

owner) { + super(owner); + } - ResourceLocation texture = state.getTexture(GlTextureUnit.T0); + @Override + public void render(RenderType type, Matrix4f viewProjection, double camX, double camY, double camZ) { + type.setupRenderState(); + + int renderTex = RenderSystem.getShaderTexture(0); + + ResourceLocation texture = RenderTextures.getShaderTexture(0); if (texture != null) { SheetData atlasData = AtlasInfo.getAtlasData(texture); @@ -27,6 +38,22 @@ public class CrumblingGroup

extends MaterialGroupImp } else { width = height = 256; } + + type.clearRenderState(); + + CrumblingRenderer._currentLayer.setupRenderState(); + + int breakingTex = RenderSystem.getShaderTexture(0); + + RenderSystem.setShaderTexture(0, renderTex); + RenderSystem.setShaderTexture(4, breakingTex); + + TextureBinder.bindActiveTextures(); + for (MaterialRenderer

renderer : renderers) { + renderer.render(viewProjection, camX, camY, camZ); + } + + CrumblingRenderer._currentLayer.clearRenderState(); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java index 14b032e5f..a91049aec 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java @@ -1,14 +1,12 @@ package com.jozufozu.flywheel.core.crumbling; -import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; -import static org.lwjgl.opengl.GL11.glBindTexture; - import java.util.ArrayList; import java.util.List; import java.util.SortedSet; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.gl.GlTextureUnit; +import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.material.MaterialManagerImpl; import com.jozufozu.flywheel.backend.state.RenderLayer; @@ -17,6 +15,7 @@ import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.mixin.LevelRendererAccessor; import com.jozufozu.flywheel.util.Lazy; import com.jozufozu.flywheel.util.Pair; +import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.math.Matrix4f; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; @@ -44,6 +43,8 @@ import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber(Dist.CLIENT) public class CrumblingRenderer { + static RenderType _currentLayer; + private static final Lazy STATE; private static final Lazy.KillSwitch INVALIDATOR; @@ -63,40 +64,31 @@ public class CrumblingRenderer { if (activeStages.isEmpty()) return; State state = STATE.get(); - RenderType layer = ModelBakery.DESTROY_TYPES.get(0); - - InstanceManager renderer = state.instanceManager; + InstanceManager instanceManager = state.instanceManager; + MaterialManagerImpl materials = state.materialManager; TextureManager textureManager = Minecraft.getInstance().getTextureManager(); Camera info = Minecraft.getInstance().gameRenderer.getMainCamera(); - MaterialManagerImpl materials = state.materialManager; - layer.setupRenderState(); - for (Int2ObjectMap.Entry> stage : activeStages.int2ObjectEntrySet()) { - int i = stage.getIntKey(); - AbstractTexture breaking = textureManager.getTexture(ModelBakery.BREAKING_LOCATIONS.get(i)); + _currentLayer = ModelBakery.DESTROY_TYPES.get(stage.getIntKey()); // something about when we call this means that the textures are not ready for use on the first frame they should appear - if (breaking != null) { - stage.getValue().forEach(renderer::add); + if (_currentLayer != null) { + stage.getValue().forEach(instanceManager::add); - renderer.beginFrame(info); + instanceManager.beginFrame(info); - GlTextureUnit.T4.makeActive(); - glBindTexture(GL_TEXTURE_2D, breaking.getId()); materials.render(RenderLayer.SOLID, viewProjection, cameraX, cameraY, cameraZ); - renderer.invalidate(); + instanceManager.invalidate(); } } - layer.clearRenderState(); - GlTextureUnit.T0.makeActive(); AbstractTexture breaking = textureManager.getTexture(ModelBakery.BREAKING_LOCATIONS.get(0)); - if (breaking != null) glBindTexture(GL_TEXTURE_2D, breaking.getId()); + if (breaking != null) RenderSystem.bindTexture(breaking.getId()); } /** diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelType.java b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelType.java index 1297a07e0..6c66b0833 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelType.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelType.java @@ -2,12 +2,12 @@ package com.jozufozu.flywheel.core.materials.model; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.backend.struct.StructType; import com.jozufozu.flywheel.backend.struct.StructWriter; +import com.jozufozu.flywheel.backend.struct.Writeable; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.materials.model.writer.UnsafeModelWriter; -public class ModelType implements StructType { +public class ModelType implements Writeable { @Override public ModelData create() { diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedType.java b/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedType.java index 239968079..1d78185b9 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedType.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedType.java @@ -2,12 +2,12 @@ package com.jozufozu.flywheel.core.materials.oriented; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.backend.struct.StructType; import com.jozufozu.flywheel.backend.struct.StructWriter; +import com.jozufozu.flywheel.backend.struct.Writeable; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.materials.oriented.writer.UnsafeOrientedWriter; -public class OrientedType implements StructType { +public class OrientedType implements Writeable { @Override public OrientedData create() { diff --git a/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java b/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java index 15c3e1704..d9ea916ec 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.core.model; -import static com.jozufozu.flywheel.util.RenderMath.nb; - import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.IntBuffer; @@ -12,10 +10,10 @@ import java.util.Random; import org.lwjgl.system.MemoryStack; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.util.VirtualEmptyModelData; import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.math.Vector3f; import net.minecraft.client.Minecraft; @@ -62,7 +60,7 @@ public class BakedModelModel implements IModel { } @Override - public void buffer(VecBuffer buffer) { + public void buffer(VertexConsumer buffer) { Minecraft mc = Minecraft.getInstance(); @@ -118,9 +116,10 @@ public class BakedModelModel implements IModel { float u = bytebuffer.getFloat(16); float v = bytebuffer.getFloat(20); - buffer.putVec3(f, f1, f2); - buffer.putVec3(nb(normal.x()), nb(normal.y()), nb(normal.z())); - buffer.putVec2(u, v); + buffer.vertex(f, f1, f2); + buffer.normal(normal.x(), normal.y(), normal.z()); + buffer.uv(u, v); + buffer.endVertex(); } } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java index 9d63d913f..efee04d5b 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java @@ -3,13 +3,14 @@ package com.jozufozu.flywheel.core.model; import java.util.Arrays; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.util.BufferBuilderReader; +import com.jozufozu.flywheel.util.RenderMath; import com.jozufozu.flywheel.util.VirtualEmptyModelData; import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.BlockRenderDispatcher; @@ -61,16 +62,18 @@ public class BlockModel implements IModel { } @Override - public void buffer(VecBuffer buffer) { + public void buffer(VertexConsumer buffer) { int vertexCount = vertexCount(); for (int i = 0; i < vertexCount; i++) { - buffer.putVec3(reader.getX(i), reader.getY(i), reader.getZ(i)); + buffer.vertex(reader.getX(i), reader.getY(i), reader.getZ(i)); - buffer.putVec3(reader.getNX(i), reader.getNY(i), reader.getNZ(i)); + buffer.normal(RenderMath.f(reader.getNX(i)), RenderMath.f(reader.getNY(i)), RenderMath.f(reader.getNZ(i))); - buffer.putVec2(reader.getU(i), reader.getV(i)); + buffer.uv(reader.getU(i), reader.getV(i)); + + buffer.endVertex(); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/IModel.java b/src/main/java/com/jozufozu/flywheel/core/model/IModel.java index 75c1be74d..6db3d1135 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/IModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/IModel.java @@ -1,9 +1,9 @@ package com.jozufozu.flywheel.core.model; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.backend.model.ElementBuffer; import com.jozufozu.flywheel.core.QuadConverter; +import com.mojang.blaze3d.vertex.VertexConsumer; /** * A model that can be rendered by flywheel. @@ -35,7 +35,7 @@ public interface IModel { /** * Copy this model into the given buffer. */ - void buffer(VecBuffer buffer); + void buffer(VertexConsumer buffer); /** * @return The number of vertices the model has. diff --git a/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java b/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java index 3574019a6..2ae657e2b 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java @@ -3,8 +3,8 @@ package com.jozufozu.flywheel.core.model; import java.util.List; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.Formats; +import com.mojang.blaze3d.vertex.VertexConsumer; public class ModelPart implements IModel { @@ -33,7 +33,7 @@ public class ModelPart implements IModel { } @Override - public void buffer(VecBuffer buffer) { + public void buffer(VertexConsumer buffer) { for (PartBuilder.CuboidBuilder cuboid : cuboids) { cuboid.buffer(buffer); } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java index 127cbd2d2..e4d811eb2 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Set; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; +import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.math.Matrix3f; import com.mojang.math.Quaternion; import com.mojang.math.Vector3f; @@ -156,7 +157,7 @@ public class PartBuilder { return visibleFaces.size() * 4; } - public void buffer(VecBuffer buffer) { + public void buffer(VertexConsumer buffer) { float sizeX = posX2 - posX1; float sizeY = posY2 - posY1; @@ -231,12 +232,11 @@ public class PartBuilder { } } - - public void quad(VecBuffer buffer, Vector3f[] vertices, float minU, float minV, float maxU, float maxV, Vector3f normal) { - buffer.putVec3(vertices[0].x(), vertices[0].y(), vertices[0].z()).putVec3(nb(normal.x()), nb(normal.y()), nb(normal.z())).putVec2(maxU, minV); - buffer.putVec3(vertices[1].x(), vertices[1].y(), vertices[1].z()).putVec3(nb(normal.x()), nb(normal.y()), nb(normal.z())).putVec2(minU, minV); - buffer.putVec3(vertices[2].x(), vertices[2].y(), vertices[2].z()).putVec3(nb(normal.x()), nb(normal.y()), nb(normal.z())).putVec2(minU, maxV); - buffer.putVec3(vertices[3].x(), vertices[3].y(), vertices[3].z()).putVec3(nb(normal.x()), nb(normal.y()), nb(normal.z())).putVec2(maxU, maxV); + public void quad(VertexConsumer buffer, Vector3f[] vertices, float minU, float minV, float maxU, float maxV, Vector3f normal) { + buffer.vertex(vertices[0].x(), vertices[0].y(), vertices[0].z()).normal(normal.x(), normal.y(), normal.z()).uv(maxU, minV).endVertex(); + buffer.vertex(vertices[1].x(), vertices[1].y(), vertices[1].z()).normal(normal.x(), normal.y(), normal.z()).uv(minU, minV).endVertex(); + buffer.vertex(vertices[2].x(), vertices[2].y(), vertices[2].z()).normal(normal.x(), normal.y(), normal.z()).uv(minU, maxV).endVertex(); + buffer.vertex(vertices[3].x(), vertices[3].y(), vertices[3].z()).normal(normal.x(), normal.y(), normal.z()).uv(maxU, maxV).endVertex(); } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java b/src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java new file mode 100644 index 000000000..221193f06 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java @@ -0,0 +1,69 @@ +package com.jozufozu.flywheel.core.model; + +import static com.jozufozu.flywheel.util.RenderMath.nb; + +import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; +import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; +import com.mojang.blaze3d.vertex.VertexConsumer; + +public class VecBufferConsumer implements VertexConsumer { + + + private final VecBuffer buffer; + private final VertexFormat format; + + public VecBufferConsumer(VecBuffer buffer, VertexFormat format) { + this.buffer = buffer; + this.format = format; + } + + @Override + public VertexConsumer vertex(double v, double v1, double v2) { + buffer.putVec3((float) v, (float) v1, (float) v2); + return this; + } + + @Override + public VertexConsumer color(int i, int i1, int i2, int i3) { + buffer.putColor(i, i1, i2, i3); + return this; + } + + @Override + public VertexConsumer uv(float v, float v1) { + buffer.putVec2(v, v1); + return this; + } + + @Override + public VertexConsumer overlayCoords(int i, int i1) { + return this; + } + + @Override + public VertexConsumer uv2(int i, int i1) { + buffer.putVec2((byte) i, (byte) i1); + return this; + } + + @Override + public VertexConsumer normal(float v, float v1, float v2) { + buffer.putVec3(nb(v), nb(v1), nb(v2)); + return this; + } + + @Override + public void endVertex() { + + } + + @Override + public void defaultColor(int i, int i1, int i2, int i3) { + + } + + @Override + public void unsetDefaultColor() { + + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java b/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java index 32939ff22..d62ef9c26 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java @@ -3,9 +3,10 @@ package com.jozufozu.flywheel.core.model; import java.util.Collection; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.util.BufferBuilderReader; +import com.jozufozu.flywheel.util.RenderMath; +import com.mojang.blaze3d.vertex.VertexConsumer; import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.RenderType; @@ -28,22 +29,24 @@ public class WorldModel implements IModel { } @Override - public void buffer(VecBuffer vertices) { + public void buffer(VertexConsumer vertices) { for (int i = 0; i < vertexCount(); i++) { - vertices.putVec3(reader.getX(i), reader.getY(i), reader.getZ(i)); + vertices.vertex(reader.getX(i), reader.getY(i), reader.getZ(i)); - vertices.putVec3(reader.getNX(i), reader.getNY(i), reader.getNZ(i)); + vertices.normal(RenderMath.f(reader.getNX(i)), RenderMath.f(reader.getNY(i)), RenderMath.f(reader.getNZ(i))); - vertices.putVec2(reader.getU(i), reader.getV(i)); + vertices.uv(reader.getU(i), reader.getV(i)); - vertices.putColor(reader.getR(i), reader.getG(i), reader.getB(i), reader.getA(i)); + vertices.color(reader.getR(i), reader.getG(i), reader.getB(i), reader.getA(i)); int light = reader.getLight(i); byte block = (byte) (LightTexture.block(light) << 4); byte sky = (byte) (LightTexture.sky(light) << 4); - vertices.putVec2(block, sky); + vertices.uv2(block, sky); + + vertices.endVertex(); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java b/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java index 67f7f6703..226fbd28e 100644 --- a/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java +++ b/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java @@ -33,7 +33,7 @@ public class WorldProgram extends ExtensibleGlProgram { protected void registerSamplers() { uBlockAtlas = setSamplerBinding("uBlockAtlas", 0); - uLightMap = setSamplerBinding("uLightMap", 1); + uLightMap = setSamplerBinding("uLightMap", 2); } public void uploadViewProjection(Matrix4f viewProjection) { diff --git a/src/main/java/com/jozufozu/flywheel/event/RenderLayerEvent.java b/src/main/java/com/jozufozu/flywheel/event/RenderLayerEvent.java index 9482f3556..2e1176738 100644 --- a/src/main/java/com/jozufozu/flywheel/event/RenderLayerEvent.java +++ b/src/main/java/com/jozufozu/flywheel/event/RenderLayerEvent.java @@ -69,4 +69,9 @@ public class RenderLayerEvent extends Event { public double getCamZ() { return camZ; } + + @Override + public String toString() { + return "RenderLayerEvent{" + "world=" + world + ", type=" + type + ", stack=" + stack + ", viewProjection=" + viewProjection + ", buffers=" + buffers + ", camX=" + camX + ", camY=" + camY + ", camZ=" + camZ + ", layer=" + layer + '}'; + } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/RenderTexturesMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/RenderTexturesMixin.java new file mode 100644 index 000000000..ac2b09450 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/RenderTexturesMixin.java @@ -0,0 +1,20 @@ +package com.jozufozu.flywheel.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.jozufozu.flywheel.util.RenderTextures; +import com.mojang.blaze3d.systems.RenderSystem; + +import net.minecraft.resources.ResourceLocation; + +@Mixin(RenderSystem.class) +public class RenderTexturesMixin { + + @Inject(method = "_setShaderTexture(ILnet/minecraft/resources/ResourceLocation;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureManager;getTexture(Lnet/minecraft/resources/ResourceLocation;)Lnet/minecraft/client/renderer/texture/AbstractTexture;")) + private static void storeTextureLoc(int pShaderTexture, ResourceLocation pTextureId, CallbackInfo ci) { + RenderTextures._setShaderTexture(pShaderTexture, pTextureId); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java deleted file mode 100644 index 9778874cd..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.jozufozu.flywheel.mixin; - -import java.util.List; -import java.util.Map; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -import com.jozufozu.flywheel.util.TextureBinder; -import com.mojang.blaze3d.pipeline.RenderTarget; -import com.mojang.blaze3d.platform.GlStateManager; -import com.mojang.blaze3d.systems.RenderSystem; - -import net.minecraft.client.renderer.ShaderInstance; -import net.minecraft.client.renderer.texture.AbstractTexture; - -@Mixin(ShaderInstance.class) -public class ShaderInstanceMixin implements TextureBinder { - @Shadow - @Final - private List samplerLocations; - - @Shadow - @Final - private List samplerNames; - - @Shadow - @Final - private Map samplerMap; - - @Override - public void bind() { - int i = GlStateManager._getActiveTexture(); - - for(int j = 0; j < samplerLocations.size(); ++j) { - String s = samplerNames.get(j); - if (samplerMap.get(s) != null) { - RenderSystem.activeTexture('\u84c0' + j); - RenderSystem.enableTexture(); - Object object = this.samplerMap.get(s); - int l = -1; - if (object instanceof RenderTarget) { - l = ((RenderTarget)object).getColorTextureId(); - } else if (object instanceof AbstractTexture) { - l = ((AbstractTexture)object).getId(); - } else if (object instanceof Integer) { - l = (Integer)object; - } - - if (l != -1) { - RenderSystem.bindTexture(l); - } - } - } - - GlStateManager._activeTexture(i); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/util/RenderTextures.java b/src/main/java/com/jozufozu/flywheel/util/RenderTextures.java new file mode 100644 index 000000000..8ea3f12dc --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/RenderTextures.java @@ -0,0 +1,26 @@ +package com.jozufozu.flywheel.util; + +import javax.annotation.Nullable; + +import net.minecraft.resources.ResourceLocation; + +/** + * Helper class to keep track of what texture atlases are bound to what texture units. + * + *

+ * Works with {@link com.jozufozu.flywheel.mixin.RenderTexturesMixin}. + *

+ */ +public class RenderTextures { + + private static final ResourceLocation[] shaderTextures = new ResourceLocation[12]; + + @Nullable + public static ResourceLocation getShaderTexture(int pShaderTexture) { + return shaderTextures[pShaderTexture]; + } + + public static void _setShaderTexture(int pShaderTexture, ResourceLocation pTextureId) { + shaderTextures[pShaderTexture] = pTextureId; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java b/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java index 2c7e816de..ba9c4f64e 100644 --- a/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java +++ b/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java @@ -1,30 +1,29 @@ package com.jozufozu.flywheel.util; +import org.lwjgl.opengl.GL32; + import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.renderer.RenderType; -import net.minecraft.client.renderer.ShaderInstance; /** * This is a silly hack that's needed because flywheel does things too different from vanilla. * *

- * When a {@link RenderType} is setup, the associated textures are passed to the active ShaderInstance, and properly - * bound later on when {@link ShaderInstance#apply()} is called. - * This interface (and {@link com.jozufozu.flywheel.mixin.ShaderInstanceMixin mixin} binds textures to opengl - * without binding the shader. + * When a {@link RenderType} is setup, the associated textures are "bound" within RenderSystem, but not actually + * bound via opengl. This class provides a helper function to forward the bindings to opengl. *

*/ -public interface TextureBinder { +public class TextureBinder { /** * Call this after calling {@link RenderType#setupRenderState()}. */ - static void bindActiveTextures() { - TextureBinder shader = (TextureBinder) RenderSystem.getShader(); - if (shader != null) - shader.bind(); + public static void bindActiveTextures() { + for (int i = 0; i < 12; i++) { + int shaderTexture = RenderSystem.getShaderTexture(i); + RenderSystem.activeTexture(GL32.GL_TEXTURE0 + i); + RenderSystem.bindTexture(shaderTexture); + } } - - void bind(); } diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index a11867494..513dbd4c6 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -7,7 +7,6 @@ import javax.annotation.Nonnull; import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.jozufozu.flywheel.backend.material.MaterialManager; -import com.jozufozu.flywheel.backend.state.TextureRenderState; import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.materials.model.ModelData; import com.jozufozu.flywheel.core.materials.oriented.OrientedData; @@ -17,6 +16,7 @@ import com.mojang.math.Quaternion; import com.mojang.math.Vector3f; import it.unimi.dsi.fastutil.floats.Float2FloatFunction; +import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.Sheets; import net.minecraft.client.resources.model.Material; import net.minecraft.world.level.block.AbstractChestBlock; @@ -111,7 +111,7 @@ public class ChestInstance extends TileE private OrientedData baseInstance() { - return materialManager.solid(TextureRenderState.get(renderMaterial.atlasLocation())) + return materialManager.solid(RenderType.entitySolid(renderMaterial.atlasLocation())) .material(Materials.ORIENTED) .model("base_" + renderMaterial.texture(), this::getBaseModel) .createInstance(); @@ -119,7 +119,7 @@ public class ChestInstance extends TileE private ModelData lidInstance() { - return materialManager.solid(TextureRenderState.get(renderMaterial.atlasLocation())) + return materialManager.solid(RenderType.entitySolid(renderMaterial.atlasLocation())) .material(Materials.TRANSFORMED) .model("lid_" + renderMaterial.texture(), this::getLidModel) .createInstance(); diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java index a7e2ff9ae..7d097f89b 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java @@ -4,7 +4,6 @@ import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.ITickableInstance; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; import com.jozufozu.flywheel.backend.material.MaterialManager; -import com.jozufozu.flywheel.backend.state.TextureRenderState; import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.materials.model.ModelData; import com.jozufozu.flywheel.core.model.IModel; @@ -13,6 +12,7 @@ import com.jozufozu.flywheel.util.AnimationTickHolder; import com.jozufozu.flywheel.util.transform.MatrixTransformStack; import com.mojang.math.Vector3f; +import net.minecraft.client.renderer.RenderType; import net.minecraft.core.Vec3i; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Mth; @@ -147,7 +147,7 @@ public class MinecartInstance extends EntityInstance } private ModelData getBody() { - return materialManager.solid(TextureRenderState.get(MINECART_LOCATION)) + return materialManager.solid(RenderType.entitySolid(MINECART_LOCATION)) .material(Materials.TRANSFORMED) .model(entity.getType(), this::getBodyModel) .createInstance(); diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/RenderStates.java b/src/main/java/com/jozufozu/flywheel/vanilla/RenderStates.java deleted file mode 100644 index 826a3892f..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/RenderStates.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jozufozu.flywheel.vanilla; - -import com.jozufozu.flywheel.backend.state.IRenderState; -import com.jozufozu.flywheel.backend.state.NoCullRenderState; -import com.jozufozu.flywheel.backend.state.RenderState; - -import net.minecraft.client.renderer.Sheets; - -public class RenderStates { - public static final IRenderState SHULKER = RenderState.builder() - .texture(Sheets.SHULKER_SHEET) - .addState(NoCullRenderState.INSTANCE) - .build(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java index 5da600a83..824526074 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java @@ -11,6 +11,7 @@ import com.jozufozu.flywheel.util.transform.MatrixTransformStack; import com.mojang.math.Quaternion; import com.mojang.math.Vector3f; +import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.Sheets; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.core.Direction; @@ -85,14 +86,14 @@ public class ShulkerBoxInstance extends TileEntityInstance