diff --git a/src/main/java/com/simibubi/create/AllBlockPartials.java b/src/main/java/com/simibubi/create/AllBlockPartials.java index 84f59e743..580b612b7 100644 --- a/src/main/java/com/simibubi/create/AllBlockPartials.java +++ b/src/main/java/com/simibubi/create/AllBlockPartials.java @@ -13,8 +13,8 @@ import com.simibubi.create.content.contraptions.fluids.FluidTransportBehaviour.A import com.simibubi.create.content.contraptions.processing.burner.BlazeBurnerBlock.HeatLevel; import com.simibubi.create.foundation.utility.*; -import com.simibubi.create.foundation.utility.render.BeltBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.BeltBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.block.BlockState; import net.minecraft.client.renderer.model.IBakedModel; diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java index 2996f04d8..b0c50866c 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java @@ -8,8 +8,7 @@ import com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock; import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer; import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.ColorHelper; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBufferCache.Compartment; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java index 140c3f2d5..235d44ee4 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java @@ -10,8 +10,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.MatrixStacker; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import com.simibubi.create.foundation.utility.VecHelper; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/clock/CuckooClockRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/clock/CuckooClockRenderer.java index 648cb310b..66d85ce92 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/clock/CuckooClockRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/clock/CuckooClockRenderer.java @@ -7,8 +7,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.components.clock.CuckooClockTileEntity.Animation; import com.simibubi.create.foundation.utility.AngleHelper; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.block.BlockState; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java index 14b06de7e..004f0e560 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java @@ -7,9 +7,8 @@ import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.base.IRotate; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java index e175c5f03..0fefa1716 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java @@ -9,8 +9,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.components.flywheel.FlywheelBlock.ConnectionState; import com.simibubi.create.foundation.utility.AngleHelper; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.block.BlockState; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneRenderer.java index 6481a47a1..ee20c59b9 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneRenderer.java @@ -4,9 +4,8 @@ import com.simibubi.create.AllBlockPartials; import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; public class MillstoneRenderer extends KineticTileEntityRenderer { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/motor/CreativeMotorRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/motor/CreativeMotorRenderer.java index 1bbd0e558..1789da7a5 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/motor/CreativeMotorRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/motor/CreativeMotorRenderer.java @@ -3,9 +3,8 @@ package com.simibubi.create.content.contraptions.components.motor; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; public class CreativeMotorRenderer extends KineticTileEntityRenderer { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java index 2a17f0849..52c99aec9 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java @@ -12,8 +12,7 @@ import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringRe import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer; import com.simibubi.create.foundation.utility.*; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.block.BlockState; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java index a1ba39595..ea888941f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java @@ -5,8 +5,7 @@ import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.foundation.utility.AngleHelper; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer; diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java index 70a527779..89cbb001f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java @@ -6,8 +6,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.MatrixStacker; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.block.BlockState; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java index 1031e592f..fe809092d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java @@ -4,9 +4,8 @@ import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.foundation.tileEntity.renderer.SmartTileEntityRenderer; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltRenderer.java index db1285a9a..1a28aff67 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltRenderer.java @@ -3,7 +3,6 @@ package com.simibubi.create.content.contraptions.relays.belt; import java.util.Random; import com.mojang.blaze3d.matrix.MatrixStack; -import com.mojang.blaze3d.vertex.IVertexBuilder; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllSpriteShifts; @@ -12,13 +11,10 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack; import com.simibubi.create.foundation.block.render.SpriteShiftEntry; import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer; -import com.simibubi.create.foundation.utility.AngleHelper; -import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.MatrixStacker; -import com.simibubi.create.foundation.utility.render.BeltBuffer; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.BeltBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.ShadowRenderHelper; import net.minecraft.block.BlockState; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java index be421d744..e126f3b08 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java @@ -6,9 +6,8 @@ import com.simibubi.create.content.contraptions.base.IRotate; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.foundation.utility.Iterate; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import net.minecraft.block.Block; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java index 52b54411f..70f8428c5 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java @@ -5,9 +5,8 @@ import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.foundation.utility.Iterate; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.state.properties.BlockStateProperties; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java index 334e5317e..5f22cc9fa 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java @@ -8,8 +8,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.logistics.block.mechanicalArm.ArmTileEntity.Phase; import com.simibubi.create.foundation.utility.*; -import com.simibubi.create.foundation.utility.render.InstancedBuffer; -import com.simibubi.create.foundation.utility.render.RotatingBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.SuperByteBuffer; import net.minecraft.block.BlockState; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/BeltBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/BeltBuffer.java deleted file mode 100644 index e17b33623..000000000 --- a/src/main/java/com/simibubi/create/foundation/utility/render/BeltBuffer.java +++ /dev/null @@ -1,166 +0,0 @@ -package com.simibubi.create.foundation.utility.render; - -import com.mojang.blaze3d.platform.GlStateManager; -import com.simibubi.create.foundation.block.render.SpriteShiftEntry; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.GLAllocation; -import net.minecraft.client.renderer.LightTexture; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.client.renderer.vertex.VertexFormatElement; -import net.minecraft.util.math.BlockPos; -import net.minecraftforge.client.model.pipeline.VertexLighterFlat; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL15; -import org.lwjgl.opengl.GL20; -import org.lwjgl.opengl.GL40; - -import java.nio.Buffer; -import java.nio.ByteBuffer; - -public class BeltBuffer extends InstancedBuffer { - public BeltBuffer(BufferBuilder buf) { - super(buf); - } - - @Override - protected BeltData newInstance() { - return new BeltData(); - } - - @Override - protected int numAttributes() { - return 9; - } - - @Override - protected void finishBufferingInternal() { - int floatSize = VertexFormatElement.Type.FLOAT.getSize(); - int intSize = VertexFormatElement.Type.INT.getSize(); - int stride = floatSize * 16; - - int instanceSize = instanceCount * stride; - - ByteBuffer buffer = GLAllocation.createDirectByteBuffer(instanceSize); - buffer.order(template.order()); - ((Buffer) buffer).limit(instanceSize); - - data.forEach(instanceData -> instanceData.buffer(buffer)); - buffer.rewind(); - - GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, instanceVBO); - GlStateManager.bufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); - - // render position - GL20.glVertexAttribPointer(3, 3, GL11.GL_FLOAT, false, stride, 0); - - // render rotation - GL20.glVertexAttribPointer(4, 3, GL11.GL_FLOAT, false, stride, floatSize * 3L); - - // light map - GL20.glVertexAttribPointer(5, 2, GL11.GL_FLOAT, false, stride, floatSize * 6L); - - // speed - GL20.glVertexAttribPointer(6, 1, GL11.GL_FLOAT, false, stride, floatSize * 8L); - - // uv data - GL20.glVertexAttribPointer(7, 2, GL11.GL_FLOAT, false, stride, floatSize * 9L); - - GL20.glVertexAttribPointer(8, 4, GL11.GL_FLOAT, false, stride, floatSize * 11L); - - GL20.glVertexAttribPointer(9, 1, GL11.GL_FLOAT, false, stride, floatSize * 15L); - - for (int i = 3; i <= numAttributes(); i++) { - GL40.glVertexAttribDivisor(i, 1); - } - - // Deselect (bind to 0) the VBO - GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); - } - - public static class BeltData { - private float x; - private float y; - private float z; - private float rotX; - private float rotY; - private float rotZ; - private int packedLight; - private float rotationalSpeed; - private float sourceU; - private float sourceV; - private float minU; - private float minV; - private float maxU; - private float maxV; - private float scrollMult; - - public BeltData setPosition(BlockPos pos) { - this.x = pos.getX(); - this.y = pos.getY(); - this.z = pos.getZ(); - return this; - } - - public BeltData setRotation(float rotX, float rotY, float rotZ) { - this.rotX = rotX; - this.rotY = rotY; - this.rotZ = rotZ; - return this; - } - - public BeltData setPackedLight(int packedLight) { - this.packedLight = packedLight; - return this; - } - - public BeltData setRotationalSpeed(float rotationalSpeed) { - this.rotationalSpeed = rotationalSpeed; - return this; - } - - public BeltData setScrollTexture(SpriteShiftEntry spriteShift) { - TextureAtlasSprite source = spriteShift.getOriginal(); - TextureAtlasSprite target = spriteShift.getTarget(); - - this.sourceU = source.getMinU(); - this.sourceV = source.getMinV(); - this.minU = target.getMinU(); - this.minV = target.getMinV(); - this.maxU = target.getMaxU(); - this.maxV = target.getMaxV(); - - return this; - } - - public BeltData setScrollMult(float scrollMult) { - this.scrollMult = scrollMult; - return this; - } - - void buffer(ByteBuffer buf) { - float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight); - float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight); - - buf.putFloat(x); - buf.putFloat(y); - buf.putFloat(z); - - buf.putFloat(rotX); - buf.putFloat(rotY); - buf.putFloat(rotZ); - - buf.putFloat(blockLightCoordinates); - buf.putFloat(skyLightCoordinates); - buf.putFloat(rotationalSpeed); - - buf.putFloat(sourceU); - buf.putFloat(sourceV); - buf.putFloat(minU); - buf.putFloat(minV); - buf.putFloat(maxU); - buf.putFloat(maxV); - - buf.putFloat(scrollMult); - } - } -} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java b/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java index 1ee76a574..9b70af249 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java @@ -7,6 +7,9 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.Create; +import com.simibubi.create.foundation.utility.render.instancing.BeltBuffer; +import com.simibubi.create.foundation.utility.render.instancing.InstanceBuffer; +import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer; import com.simibubi.create.foundation.utility.render.shader.Shader; import com.simibubi.create.foundation.utility.render.shader.ShaderCallback; import com.simibubi.create.foundation.utility.render.shader.ShaderHelper; @@ -17,7 +20,6 @@ import net.minecraft.client.renderer.model.IBakedModel; import net.minecraft.client.renderer.texture.Texture; import net.minecraft.inventory.container.PlayerContainer; import net.minecraft.util.Direction; -import net.minecraft.util.math.Vec3d; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.client.event.RenderWorldLastEvent; import net.minecraftforge.fml.common.Mod; @@ -81,7 +83,7 @@ public class FastKineticRenderer { .stream() .flatMap(cache -> cache.asMap().values().stream()) .filter(type -> !type.isEmpty()) - .forEach(InstancedBuffer::render); + .forEach(InstanceBuffer::render); ShaderHelper.useShader(Shader.BELT_INSTANCED, callback); @@ -89,7 +91,7 @@ public class FastKineticRenderer { .stream() .flatMap(cache -> cache.asMap().values().stream()) .filter(type -> !type.isEmpty()) - .forEach(InstancedBuffer::render); + .forEach(InstanceBuffer::render); ShaderHelper.releaseShader(); @@ -221,12 +223,12 @@ public class FastKineticRenderer { public void invalidate() { rotating.values().forEach(cache -> { - cache.asMap().values().forEach(InstancedBuffer::invalidate); + cache.asMap().values().forEach(InstanceBuffer::invalidate); cache.invalidateAll(); }); belts.values().forEach(cache -> { - cache.asMap().values().forEach(InstancedBuffer::invalidate); + cache.asMap().values().forEach(InstanceBuffer::invalidate); cache.invalidateAll(); }); } diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/RotatingBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/RotatingBuffer.java deleted file mode 100644 index 6d80607f0..000000000 --- a/src/main/java/com/simibubi/create/foundation/utility/render/RotatingBuffer.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.simibubi.create.foundation.utility.render; - -import com.mojang.blaze3d.platform.GlStateManager; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.GLAllocation; -import net.minecraft.client.renderer.LightTexture; -import net.minecraft.client.renderer.Vector3f; -import net.minecraft.client.renderer.vertex.VertexFormatElement; -import net.minecraft.util.math.BlockPos; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL15; -import org.lwjgl.opengl.GL20; -import org.lwjgl.opengl.GL40; - -import java.nio.Buffer; -import java.nio.ByteBuffer; - -public class RotatingBuffer extends InstancedBuffer { - public RotatingBuffer(BufferBuilder buf) { - super(buf); - } - - @Override - protected InstanceData newInstance() { - return new InstanceData(); - } - - @Override - protected int numAttributes() { - return 7; - } - - @Override - protected void finishBufferingInternal() { - int floatSize = VertexFormatElement.Type.FLOAT.getSize(); - int stride = floatSize * 10; - - int instanceSize = instanceCount * stride; - - ByteBuffer buffer = GLAllocation.createDirectByteBuffer(instanceSize); - buffer.order(template.order()); - ((Buffer) buffer).limit(instanceSize); - - data.forEach(instanceData -> instanceData.buffer(buffer)); - buffer.rewind(); - - GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, instanceVBO); - GlStateManager.bufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); - - // the render position - GL20.glVertexAttribPointer(3, 3, GL11.GL_FLOAT, false, stride, 0); - - // vertex lighting - GL20.glVertexAttribPointer(4, 2, GL11.GL_FLOAT, false, stride, floatSize * 3L); - - // rotational speed and offset - GL20.glVertexAttribPointer(5, 1, GL11.GL_FLOAT, false, stride, floatSize * 5L); - GL20.glVertexAttribPointer(6, 1, GL11.GL_FLOAT, false, stride, floatSize * 6L); - // rotation axis - GL20.glVertexAttribPointer(7, 3, GL11.GL_FLOAT, false, stride, floatSize * 7L); - - for (int i = 3; i <= numAttributes(); i++) { - GL40.glVertexAttribDivisor(i, 1); - } - - // Deselect (bind to 0) the VBO - GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); - } - - public static class InstanceData { - private float x; - private float y; - private float z; - private int packedLight; - private float rotationalSpeed; - private float rotationOffset; - private float rotationAxisX; - private float rotationAxisY; - private float rotationAxisZ; - - public InstanceData setPackedLight(int packedLight) { - this.packedLight = packedLight; - return this; - } - - public InstanceData setRotationalSpeed(float rotationalSpeed) { - this.rotationalSpeed = rotationalSpeed; - return this; - } - - public InstanceData setRotationOffset(float rotationOffset) { - this.rotationOffset = rotationOffset; - return this; - } - - public InstanceData setPosition(Vector3f pos) { - this.x = pos.getX(); - this.y = pos.getY(); - this.z = pos.getZ(); - return this; - } - - public InstanceData setPosition(BlockPos pos) { - this.x = pos.getX(); - this.y = pos.getY(); - this.z = pos.getZ(); - return this; - } - - public InstanceData setRotationAxis(Vector3f axis) { - this.rotationAxisX = axis.getX(); - this.rotationAxisY = axis.getY(); - this.rotationAxisZ = axis.getZ(); - return this; - } - - public InstanceData setRotationAxis(float rotationAxisX, float rotationAxisY, float rotationAxisZ) { - this.rotationAxisX = rotationAxisX; - this.rotationAxisY = rotationAxisY; - this.rotationAxisZ = rotationAxisZ; - return this; - } - - void buffer(ByteBuffer buf) { - float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight); - float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight); - - buf.putFloat(x); - buf.putFloat(y); - buf.putFloat(z); - buf.putFloat(blockLightCoordinates); - buf.putFloat(skyLightCoordinates); - buf.putFloat(rotationalSpeed); - buf.putFloat(rotationOffset); - buf.putFloat(rotationAxisX); - buf.putFloat(rotationAxisY); - buf.putFloat(rotationAxisZ); - } - } -} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltBuffer.java new file mode 100644 index 000000000..b07c90d81 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltBuffer.java @@ -0,0 +1,107 @@ +package com.simibubi.create.foundation.utility.render.instancing; + +import com.simibubi.create.foundation.block.render.SpriteShiftEntry; +import com.simibubi.create.foundation.utility.render.LightUtil; +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.util.math.BlockPos; +import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; + +import java.nio.ByteBuffer; + +public class BeltBuffer extends InstanceBuffer { + public BeltBuffer(BufferBuilder buf) { + super(buf); + } + + @Override + protected BeltData newInstance() { + return new BeltData(); + } + + @Override + protected VertexFormat getInstanceFormat() { + return BeltData.FORMAT; + } + + public static class BeltData extends InstanceData { + public static VertexFormat FORMAT = new VertexFormat(VEC3, VEC3, VEC2, FLOAT, VEC2, VEC4, FLOAT); + + private float x; + private float y; + private float z; + private float rotX; + private float rotY; + private float rotZ; + private int packedLight; + private float rotationalSpeed; + private float sourceU; + private float sourceV; + private float minU; + private float minV; + private float maxU; + private float maxV; + private float scrollMult; + + public BeltData setPosition(BlockPos pos) { + this.x = pos.getX(); + this.y = pos.getY(); + this.z = pos.getZ(); + return this; + } + + public BeltData setRotation(float rotX, float rotY, float rotZ) { + this.rotX = rotX; + this.rotY = rotY; + this.rotZ = rotZ; + return this; + } + + public BeltData setPackedLight(int packedLight) { + this.packedLight = packedLight; + return this; + } + + public BeltData setRotationalSpeed(float rotationalSpeed) { + this.rotationalSpeed = rotationalSpeed; + return this; + } + + public BeltData setScrollTexture(SpriteShiftEntry spriteShift) { + TextureAtlasSprite source = spriteShift.getOriginal(); + TextureAtlasSprite target = spriteShift.getTarget(); + + this.sourceU = source.getMinU(); + this.sourceV = source.getMinV(); + this.minU = target.getMinU(); + this.minV = target.getMinV(); + this.maxU = target.getMaxU(); + this.maxV = target.getMaxV(); + + return this; + } + + public BeltData setScrollMult(float scrollMult) { + this.scrollMult = scrollMult; + return this; + } + + @Override + public void write(ByteBuffer buf) { + float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight); + float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight); + + putVec3(buf, x, y, z); + + putVec3(buf, rotX, rotY, rotZ); + + putVec2(buf, blockLightCoordinates, skyLightCoordinates); + putFloat(buf, rotationalSpeed); + + putVec2(buf, sourceU, sourceV); + putVec4(buf, minU, minV, maxU, maxV); + + putFloat(buf, scrollMult); + } + } +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/InstancedBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java similarity index 73% rename from src/main/java/com/simibubi/create/foundation/utility/render/InstancedBuffer.java rename to src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java index bd7be51c8..cde07b894 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/InstancedBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java @@ -1,8 +1,9 @@ -package com.simibubi.create.foundation.utility.render; +package com.simibubi.create.foundation.utility.render.instancing; import com.mojang.blaze3d.platform.GlStateManager; import com.simibubi.create.CreateClient; +import com.simibubi.create.foundation.utility.render.TemplateBuffer; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GLAllocation; import net.minecraft.client.renderer.vertex.VertexFormatElement; @@ -13,14 +14,14 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.function.Consumer; -public abstract class InstancedBuffer extends TemplateBuffer { +public abstract class InstanceBuffer extends TemplateBuffer { protected int vao, ebo, invariantVBO, instanceVBO, instanceCount; - protected final ArrayList data = new ArrayList<>(); + protected final ArrayList data = new ArrayList<>(); protected boolean shouldBuild = true; - public InstancedBuffer(BufferBuilder buf) { + public InstanceBuffer(BufferBuilder buf) { super(buf); setupMainData(); } @@ -86,6 +87,8 @@ public abstract class InstancedBuffer extends TemplateBuffer { GL30.glBindVertexArray(0); } + protected abstract VertexFormat getInstanceFormat(); + public int numInstances() { return instanceCount; } @@ -110,14 +113,12 @@ public abstract class InstancedBuffer extends TemplateBuffer { }); } - protected abstract T newInstance(); + protected abstract D newInstance(); - protected abstract int numAttributes(); - - public void setupInstance(Consumer setup) { + public void setupInstance(Consumer setup) { if (!shouldBuild) return; - T instanceData = newInstance(); + D instanceData = newInstance(); setup.accept(instanceData); data.add(instanceData); @@ -129,7 +130,8 @@ public abstract class InstancedBuffer extends TemplateBuffer { GL30.glBindVertexArray(vao); finishBuffering(); - for (int i = 0; i <= numAttributes(); i++) { + int numAttributes = getInstanceFormat().getNumAttributes() + 3; + for (int i = 0; i <= numAttributes; i++) { GL40.glEnableVertexAttribArray(i); } @@ -137,7 +139,7 @@ public abstract class InstancedBuffer extends TemplateBuffer { GL40.glDrawElementsInstanced(GL11.GL_QUADS, count, GL11.GL_UNSIGNED_SHORT, 0, instanceCount); - for (int i = 0; i <= numAttributes(); i++) { + for (int i = 0; i <= numAttributes; i++) { GL40.glDisableVertexAttribArray(i); } @@ -148,11 +150,30 @@ public abstract class InstancedBuffer extends TemplateBuffer { private void finishBuffering() { if (!shouldBuild) return; - finishBufferingInternal(); + VertexFormat instanceFormat = getInstanceFormat(); + + int instanceSize = instanceCount * instanceFormat.getStride(); + + ByteBuffer buffer = GLAllocation.createDirectByteBuffer(instanceSize); + buffer.order(template.order()); + ((Buffer) buffer).limit(instanceSize); + + data.forEach(instanceData -> instanceData.write(buffer)); + buffer.rewind(); + + GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, instanceVBO); + GlStateManager.bufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); + + instanceFormat.informAttributes(3); + + for (int i = 0; i < instanceFormat.getNumAttributes(); i++) { + GL40.glVertexAttribDivisor(i + 3, 1); + } + + // Deselect (bind to 0) the VBO + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); shouldBuild = false; data.clear(); } - - protected abstract void finishBufferingInternal(); } diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceData.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceData.java new file mode 100644 index 000000000..22690d1fc --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceData.java @@ -0,0 +1,30 @@ +package com.simibubi.create.foundation.utility.render.instancing; + +import java.nio.ByteBuffer; + +public abstract class InstanceData { + + public abstract void write(ByteBuffer buf); + + public void putVec4(ByteBuffer buf, float x, float y, float z, float w) { + putFloat(buf, x); + putFloat(buf, y); + putFloat(buf, z); + putFloat(buf, w); + } + + public void putVec3(ByteBuffer buf, float x, float y, float z) { + putFloat(buf, x); + putFloat(buf, y); + putFloat(buf, z); + } + + public void putVec2(ByteBuffer buf, float x, float y) { + putFloat(buf, x); + putFloat(buf, y); + } + + public void putFloat(ByteBuffer buf, float f) { + buf.putFloat(f); + } +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstancedTileEntityRenderer.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstancedTileEntityRenderer.java new file mode 100644 index 000000000..7c4ed9e8e --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstancedTileEntityRenderer.java @@ -0,0 +1,12 @@ +package com.simibubi.create.foundation.utility.render.instancing; + +import net.minecraft.tileentity.TileEntity; + +import java.util.function.Supplier; + +public abstract class InstancedTileEntityRenderer { + + public abstract D getInstanceData(T te); + + public abstract InstanceBuffer getModel(T te); +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingBuffer.java new file mode 100644 index 000000000..7af85110e --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingBuffer.java @@ -0,0 +1,106 @@ +package com.simibubi.create.foundation.utility.render.instancing; + +import com.mojang.blaze3d.platform.GlStateManager; +import com.simibubi.create.foundation.utility.render.LightUtil; +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.GLAllocation; +import net.minecraft.client.renderer.Vector3f; +import net.minecraft.client.renderer.vertex.VertexFormatElement; +import net.minecraft.util.math.BlockPos; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; +import org.lwjgl.opengl.GL20; +import org.lwjgl.opengl.GL40; + +import java.nio.Buffer; +import java.nio.ByteBuffer; + +import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; +import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.FLOAT; + +public class RotatingBuffer extends InstanceBuffer { + public RotatingBuffer(BufferBuilder buf) { + super(buf); + } + + @Override + protected RotatingData newInstance() { + return new RotatingData(); + } + + @Override + protected VertexFormat getInstanceFormat() { + return RotatingData.FORMAT; + } + + public static class RotatingData extends InstanceData { + public static VertexFormat FORMAT = new VertexFormat(VEC3, VEC2, FLOAT, FLOAT, VEC3); + + private float x; + private float y; + private float z; + private int packedLight; + private float rotationalSpeed; + private float rotationOffset; + private float rotationAxisX; + private float rotationAxisY; + private float rotationAxisZ; + + public RotatingData setPackedLight(int packedLight) { + this.packedLight = packedLight; + return this; + } + + public RotatingData setRotationalSpeed(float rotationalSpeed) { + this.rotationalSpeed = rotationalSpeed; + return this; + } + + public RotatingData setRotationOffset(float rotationOffset) { + this.rotationOffset = rotationOffset; + return this; + } + + public RotatingData setPosition(Vector3f pos) { + this.x = pos.getX(); + this.y = pos.getY(); + this.z = pos.getZ(); + return this; + } + + public RotatingData setPosition(BlockPos pos) { + this.x = pos.getX(); + this.y = pos.getY(); + this.z = pos.getZ(); + return this; + } + + public RotatingData setRotationAxis(Vector3f axis) { + this.rotationAxisX = axis.getX(); + this.rotationAxisY = axis.getY(); + this.rotationAxisZ = axis.getZ(); + return this; + } + + public RotatingData setRotationAxis(float rotationAxisX, float rotationAxisY, float rotationAxisZ) { + this.rotationAxisX = rotationAxisX; + this.rotationAxisY = rotationAxisY; + this.rotationAxisZ = rotationAxisZ; + return this; + } + + @Override + public void write(ByteBuffer buf) { + float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight); + float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight); + + putVec3(buf, x, y, z); + + putVec2(buf, blockLightCoordinates, skyLightCoordinates); + putFloat(buf, rotationalSpeed); + putFloat(buf, rotationOffset); + + putVec3(buf, rotationAxisX, rotationAxisY, rotationAxisZ); + } + } +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java new file mode 100644 index 000000000..1a353623e --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java @@ -0,0 +1,37 @@ +package com.simibubi.create.foundation.utility.render.instancing; + +import net.minecraft.client.renderer.vertex.VertexFormatElement; +import org.lwjgl.opengl.GL20; + +public class VertexAttribute { + + public static VertexAttribute MAT4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 16); + public static VertexAttribute VEC4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 4); + public static VertexAttribute VEC3 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 3); + public static VertexAttribute VEC2 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 2); + public static VertexAttribute FLOAT = new VertexAttribute(VertexFormatElement.Type.FLOAT, 1); + + private final VertexFormatElement.Type type; + private final int count; + private final int size; + private final int attributeCount; + + public VertexAttribute(VertexFormatElement.Type type, int count) { + this.type = type; + this.count = count; + this.size = type.getSize() * count; + this.attributeCount = (this.size + 15) / 16; // ceiling division. GLSL vertex attributes can only be 16 bytes wide + } + + public void registerForBuffer(int stride, int indexAcc, int offsetAcc) { + GL20.glVertexAttribPointer(indexAcc, count, type.getGlConstant(), false, stride, offsetAcc); + } + + public int getSize() { + return size; + } + + public int getAttributeCount() { + return attributeCount; + } +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexFormat.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexFormat.java new file mode 100644 index 000000000..392691e3e --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexFormat.java @@ -0,0 +1,37 @@ +package com.simibubi.create.foundation.utility.render.instancing; + +public class VertexFormat { + + private final VertexAttribute[] elements; + + private final int numAttributes; + private final int stride; + + public VertexFormat(VertexAttribute... elements) { + this.elements = elements; + int numAttributes = 0, stride = 0; + for (VertexAttribute element : elements) { + numAttributes += element.getAttributeCount(); + stride += element.getSize(); + } + this.numAttributes = numAttributes; + this.stride = stride; + } + + public int getNumAttributes() { + return numAttributes; + } + + public int getStride() { + return stride; + } + + public void informAttributes(int index) { + int offset = 0; + for (VertexAttribute element : this.elements) { + element.registerForBuffer(stride, index, offset); + index += element.getAttributeCount(); + offset += element.getSize(); + } + } +}