Skip to content

Commit

Permalink
Memory Allocation Improvements (#11)
Browse files Browse the repository at this point in the history
* Get rid of egregious Parameter overallocation

* Allocate Point instances conditionally by using mutable Vector3d for calculations

* Update minimum GTNHLib version for JOML
  • Loading branch information
Cleptomania authored Feb 16, 2024
1 parent 210ee6f commit 178f8b2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/malisis/core/MalisisCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
modid = MalisisCore.modid,
name = MalisisCore.modname,
version = MalisisCore.version,
dependencies = "required-after:gtnhlib@[0.0.10,)")
dependencies = "required-after:gtnhlib@[0.2.4,)")
public class MalisisCore implements IMalisisMod {

/** Mod ID. */
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/malisis/core/renderer/MalisisRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class MalisisRenderer extends TileEntitySpecialRenderer
/** Current parameters for the shape being rendered. */
protected RenderParameters rp = new RenderParameters();
/** Current parameters for the face being rendered. */
protected RenderParameters params;
protected static RenderParameters params = new RenderParameters();
/** Base brightness of the block. */
protected int baseBrightness;
/** An override texture set by the renderer. */
Expand Down Expand Up @@ -676,7 +676,11 @@ public void drawShape(Shape s, RenderParameters params) {
if (s == null) return;

shape = s;
rp = params != null ? params : new RenderParameters();
if (params != null) {
rp = params;
} else {
rp.reset();
}

// apply transformations
s.applyMatrix();
Expand Down Expand Up @@ -716,7 +720,7 @@ protected void drawFace(Face f, RenderParameters faceParams) {
}

face = f;
params = new RenderParameters();
params.reset();
params.merge(rp);
params.merge(faceParams);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/malisis/core/renderer/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Parameter<T> {
/** Default value. */
private T defaultValue;

/** Current alue. */
/** Current value. */
private T value;

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ public T getValue() {
* Resets the value to its default.
*/
public void reset() {
value = null;
value = defaultValue;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/malisis/core/util/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Vec3;

import org.joml.Vector3d;

/**
*
* @author Ordinastie
Expand Down Expand Up @@ -45,6 +47,12 @@ public Point(double x, double y, double z) {
this.z = z;
}

public Point(Vector3d vector) {
this.x = vector.x;
this.y = vector.y;
this.z = vector.z;
}

/**
* Instantiates a new point.
*
Expand Down
76 changes: 57 additions & 19 deletions src/main/java/net/malisis/core/util/Ray.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraftforge.common.util.ForgeDirection;

import org.apache.commons.lang3.tuple.Pair;
import org.joml.Vector3d;

/**
*
Expand Down Expand Up @@ -67,14 +68,14 @@ public Ray(Vec3 src, Vec3 dest) {
}

/**
* Gets the {@link Point} at the specified distance.
* Gets the point at the specified distance into a {@link Vector3d}.
*
* @param t the distance
* @return the point at the distance t
* @param vector {@link Vector3d} to store the result in
* @param t the distance
*/
public Point getPointAt(double t) {
if (Double.isNaN(t)) return null;
return new Point(origin.x + t * direction.x, origin.y + t * direction.y, origin.z + t * direction.z);
public void getPointAt(Vector3d vector, double t) {
if (Double.isNaN(t)) return;
vector.set(origin.x + t * direction.x, origin.y + t * direction.y, origin.z + t * direction.z);
}

/**
Expand Down Expand Up @@ -123,23 +124,60 @@ public List<Pair<ForgeDirection, Point>> intersect(AxisAlignedBB aabb) {
double iY = intersectY(aabb.maxY);
double iz = intersectZ(aabb.minZ);
double iZ = intersectZ(aabb.maxZ);
Point interx = ix >= 0 ? getPointAt(ix) : null;
Point interX = iX >= 0 ? getPointAt(iX) : null;
Point intery = iy >= 0 ? getPointAt(iy) : null;
Point interY = iY >= 0 ? getPointAt(iY) : null;
Point interz = iz >= 0 ? getPointAt(iz) : null;
Point interZ = iZ >= 0 ? getPointAt(iZ) : null;

List<Pair<ForgeDirection, Point>> list = new ArrayList<>();
if (interx != null && interx.isInside(aabb)) list.add(Pair.of(ForgeDirection.WEST, interx));
if (interX != null && interX.isInside(aabb)) list.add(Pair.of(ForgeDirection.EAST, interX));

if (intery != null && intery.isInside(aabb)) list.add(Pair.of(ForgeDirection.DOWN, intery));
if (interY != null && interY.isInside(aabb)) list.add(Pair.of(ForgeDirection.UP, interY));

if (interz != null && interz.isInside(aabb)) list.add(Pair.of(ForgeDirection.NORTH, interz));
if (interZ != null && interZ.isInside(aabb)) list.add(Pair.of(ForgeDirection.SOUTH, interZ));
Vector3d intersector = new Vector3d();
if (ix >= 0) {
getPointAt(intersector, ix);
if (isPointInsideAABB(intersector, aabb)) {
list.add(Pair.of(ForgeDirection.WEST, new Point(intersector)));
}
}

if (iX >= 0) {
getPointAt(intersector, iX);
if (isPointInsideAABB(intersector, aabb)) {
list.add(Pair.of(ForgeDirection.EAST, new Point(intersector)));
}
}

if (iy >= 0) {
getPointAt(intersector, iy);
if (isPointInsideAABB(intersector, aabb)) {
list.add(Pair.of(ForgeDirection.DOWN, new Point(intersector)));
}
}

if (iY >= 0) {
getPointAt(intersector, iY);
if (isPointInsideAABB(intersector, aabb)) {
list.add(Pair.of(ForgeDirection.UP, new Point(intersector)));
}
}

if (iz >= 0) {
getPointAt(intersector, iz);
if (isPointInsideAABB(intersector, aabb)) {
list.add(Pair.of(ForgeDirection.NORTH, new Point(intersector)));
}
}

if (iZ >= 0) {
getPointAt(intersector, iZ);
if (isPointInsideAABB(intersector, aabb)) {
list.add(Pair.of(ForgeDirection.SOUTH, new Point(intersector)));
}
}

return list;
}

private boolean isPointInsideAABB(Vector3d point, AxisAlignedBB aabb) {
return point.x >= aabb.minX && point.x <= aabb.maxX
&& point.y >= aabb.minY
&& point.y <= aabb.maxY
&& point.z >= aabb.minZ
&& point.z <= aabb.maxZ;
}
}

0 comments on commit 178f8b2

Please sign in to comment.