Skip to content

Commit

Permalink
Refactor Quad, BlockPos, and QuadProvider API
Browse files Browse the repository at this point in the history
Also optimize static models
  • Loading branch information
ah-OOG-ah committed Mar 1, 2024
1 parent da8f700 commit 346a305
Show file tree
Hide file tree
Showing 49 changed files with 701 additions and 525 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ plugins {
id("com.gtnewhorizons.gtnhconvention")
}

minecraft {
extraRunJvmArguments.add("-Dangelica.enableTestBlocks=true")
}

tasks.test {
useJUnitPlatform()
testLogging {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/gtnewhorizons/angelica/api/BlockPos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.gtnewhorizons.angelica.api;

import com.gtnewhorizons.angelica.compat.mojang.BlockPosImpl;
import net.minecraft.util.MathHelper;
import net.minecraftforge.common.util.ForgeDirection;

import java.math.RoundingMode;

import static com.google.common.math.IntMath.log2;

public interface BlockPos {

int SIZE_BITS_X = 1 + log2(MathHelper.roundUpToPowerOfTwo(30000000), RoundingMode.UNNECESSARY);
int SIZE_BITS_Z = SIZE_BITS_X;
int SIZE_BITS_Y = 64 - SIZE_BITS_X - SIZE_BITS_Z;
long BITS_X = (1L << SIZE_BITS_X) - 1L;
long BITS_Y = (1L << SIZE_BITS_Y) - 1L;
long BITS_Z = (1L << SIZE_BITS_Z) - 1L;
int BIT_SHIFT_Z = SIZE_BITS_Y;
int BIT_SHIFT_X = SIZE_BITS_Y + SIZE_BITS_Z;

int getX();
int getY();
int getZ();
BlockPosImpl offset(ForgeDirection d);
BlockPosImpl down();
BlockPosImpl up();
long asLong();

static long asLong(int x, int y, int z) {
long l = 0L;
l |= ((long) x & BITS_X) << BIT_SHIFT_X;
l |= ((long) y & BITS_Y) << 0;
l |= ((long) z & BITS_Z) << BIT_SHIFT_Z;
return l;
}

public static int unpackLongX(long packedPos) {
return (int)(packedPos << 64 - BIT_SHIFT_X - SIZE_BITS_X >> 64 - SIZE_BITS_X);
}

public static int unpackLongY(long packedPos) {
return (int)(packedPos << 64 - SIZE_BITS_Y >> 64 - SIZE_BITS_Y);
}

public static int unpackLongZ(long packedPos) {
return (int)(packedPos << 64 - BIT_SHIFT_Z - SIZE_BITS_Z >> 64 - SIZE_BITS_Z);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gtnewhorizons.angelica.api;

public interface MutableBlockPos extends BlockPos {

MutableBlockPos set(int x, int y, int z);

MutableBlockPos set(long packedPos);
}
22 changes: 17 additions & 5 deletions src/main/java/com/gtnewhorizons/angelica/api/QuadProvider.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.gtnewhorizons.angelica.api;

import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.utils.ObjectPooler;
import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;

import java.util.List;
import java.util.Random;
import java.util.function.Supplier;

public interface QuadProvider {

Expand All @@ -23,9 +21,23 @@ public interface QuadProvider {
*/
default int getColor(IBlockAccess world, BlockPos pos, Block block, int meta, Random random) {

final int cin = block.colorMultiplier(world, pos.x, pos.y, pos.z);
final int cin = block.colorMultiplier(world, pos.getX(), pos.getY(), pos.getZ());
return (0xFF << 24) | ((cin & B_MASK) << 16) | (cin & G_MASK) | ((cin & R_MASK) >>> 16);
}

List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, int color, ObjectPooler<Quad> quadPool);
/**
* If you need to allocate new quads, set this to true. If true, the quads returned by {@link #getQuads} are
* recycled, and you should not keep a reference to them. Example: stone can return a static list every time, but a
* modded block which adds or removes quads based on location would need dynamic quads.
*/
default boolean isDynamic() {
return false;
}

/**
* Provide quads to render. If you need new quads, they should be obtained with the passed supplier and
* {@link #isDynamic} overridden to return true. If so, all quads in the list are recycled and references to them
* should not be kept.
*/
List<QuadView> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, int color, Supplier<QuadView> quadPool);
}
22 changes: 22 additions & 0 deletions src/main/java/com/gtnewhorizons/angelica/api/QuadView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gtnewhorizons.angelica.api;

import me.jellysquid.mods.sodium.client.model.quad.ModelQuadViewMutable;
import me.jellysquid.mods.sodium.client.render.pipeline.BlockRenderer;
import net.minecraftforge.common.util.ForgeDirection;

import javax.annotation.Nullable;

public interface QuadView extends ModelQuadViewMutable {

boolean isShade();
boolean isDeleted();
ForgeDirection getFace();
void copyFrom(QuadView src);
void setRaw(int[] data, boolean shade, @Nullable ForgeDirection face, int colorIndex, int flags);
int[] getRawVertexes();

/**
* Present for compatibility with the Tesselator, not recommended for general use.
*/
void setState(int[] rawBuffer, int offset, BlockRenderer.Flags flags, int drawMode, float offsetX, float offsetY, float offsetZ);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.gtnewhorizons.angelica.client.renderer;

import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.api.QuadView;
import com.gtnewhorizons.angelica.compat.mojang.BlockPosImpl;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormat;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import me.jellysquid.mods.sodium.client.model.quad.Quad;
import com.gtnewhorizons.angelica.glsm.stacks.Vector3dStack;
import com.gtnewhorizons.angelica.mixins.interfaces.ITessellatorInstance;
import com.gtnewhorizons.angelica.utils.ObjectPooler;
Expand Down Expand Up @@ -39,15 +40,15 @@ public class CapturingTessellator extends Tessellator implements ITessellatorIns
private static final MethodHandle sRawBufferSize;
private static final MethodHandle gRawBufferSize;
private final BlockRenderer.Flags FLAGS = new BlockRenderer.Flags(true, true, true, true);
private final ObjectPooler<Quad> quadBuf = new ObjectPooler<>(Quad::new);
private final List<Quad> collectedQuads = new ObjectArrayList<>();
private final ObjectPooler<QuadView> quadBuf = new ObjectPooler<>(Quad::new);
private final List<QuadView> collectedQuads = new ObjectArrayList<>();

// Any offset we need to the Tesselator's offset!
private final BlockPos offset = new BlockPos();
private final BlockPosImpl offset = new BlockPosImpl();

private final Vector3dStack storedTranslation = new Vector3dStack();

public void setOffset(BlockPos pos) {
public void setOffset(BlockPosImpl pos) {
this.offset.set(pos);
}
public void resetOffset() {
Expand Down Expand Up @@ -93,10 +94,10 @@ public int draw() {


for(int quadI = 0; quadI < this.vertexCount / verticesPerPrimitive; quadI++) {
final Quad quad = quadBuf.getInstance();
final QuadView quad = quadBuf.getInstance();
quad.setState(this.rawBuffer, quadI * (verticesPerPrimitive * 8), FLAGS, this.drawMode, -offset.x, -offset.y, -offset.z);

if(quad.deleted) {
if(quad.isDeleted()) {
quadBuf.releaseInstance(quad);
} else {
this.collectedQuads.add(quad);
Expand All @@ -115,7 +116,7 @@ public void discard() {
}


public List<Quad> getQuads() {
public List<QuadView> getQuads() {
return collectedQuads;
}

Expand All @@ -127,7 +128,7 @@ public void clearQuads() {
this.collectedQuads.clear();
}

public static ByteBuffer quadsToBuffer(List<Quad> quads, VertexFormat format) {
public static ByteBuffer quadsToBuffer(List<QuadView> quads, VertexFormat format) {
if(!format.canWriteQuads()) {
throw new IllegalStateException("Vertex format has no quad writer: " + format);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/gtnewhorizons/angelica/common/BlockTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.gtnewhorizons.angelica.common;

import com.gtnewhorizons.angelica.api.BlockPos;
import com.gtnewhorizons.angelica.api.QuadProvider;
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.api.QuadView;
import com.gtnewhorizons.angelica.models.json.JsonModel;
import com.gtnewhorizons.angelica.models.json.Loader;
import com.gtnewhorizons.angelica.models.json.Variant;
import com.gtnewhorizons.angelica.utils.ObjectPooler;
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
Expand All @@ -19,6 +18,7 @@

import java.util.List;
import java.util.Random;
import java.util.function.Supplier;

public class BlockTest extends Block implements QuadProvider {

Expand Down Expand Up @@ -48,7 +48,7 @@ public class BlockTest extends Block implements QuadProvider {
false
),
};
private static final List<Quad> EMPTY = ObjectImmutableList.of();
private static final List<QuadView> EMPTY = ObjectImmutableList.of();
private static final JsonModel[] model = new JsonModel[4];

public BlockTest() {
Expand Down Expand Up @@ -78,7 +78,7 @@ public void registerBlockIcons(IIconRegister reg) {
}

@Override
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, int color, ObjectPooler<Quad> quadPool) {
public List<QuadView> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, int color, Supplier<QuadView> quadPool) {

if (meta < 2 || meta > 5) meta = 2;

Expand Down
102 changes: 0 additions & 102 deletions src/main/java/com/gtnewhorizons/angelica/compat/mojang/BlockPos.java

This file was deleted.

Loading

0 comments on commit 346a305

Please sign in to comment.