Add ExtraMemoryOps lib class

- Rename some fields and methods in ColoredLitInstance and FlatLit
This commit is contained in:
PepperCode1 2024-06-29 19:15:00 -07:00
parent 22383fd51e
commit 6017228f4a
9 changed files with 122 additions and 109 deletions

View file

@ -1,8 +1,5 @@
package dev.engine_room.flywheel.api.vertex;
import org.joml.Vector3f;
import org.joml.Vector4f;
/**
* A read only view of a vertex buffer.
*
@ -40,14 +37,6 @@ public interface VertexList {
float normalZ(int index);
default Vector4f getPos(int i, Vector4f dest) {
return dest.set(x(i), y(i), z(i));
}
default Vector3f getNormal(int i, Vector3f dest) {
return dest.set(normalX(i), normalY(i), normalZ(i));
}
default void write(MutableVertexList dst, int srcIndex, int dstIndex) {
dst.x(dstIndex, x(srcIndex));
dst.y(dstIndex, y(srcIndex));

View file

@ -3,7 +3,7 @@ package dev.engine_room.flywheel.backend.engine.uniform;
import org.joml.Matrix4f;
import org.lwjgl.system.MemoryUtil;
import dev.engine_room.flywheel.lib.math.MatrixMath;
import dev.engine_room.flywheel.lib.util.ExtraMemoryOps;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.Level;
@ -46,7 +46,7 @@ class UniformWriter {
}
static long writeMat4(long ptr, Matrix4f mat) {
MatrixMath.writeUnsafe(ptr, mat);
ExtraMemoryOps.putMatrix4f(ptr, mat);
return ptr + 64;
}

View file

@ -2,7 +2,6 @@ package dev.engine_room.flywheel.lib.instance;
import dev.engine_room.flywheel.api.instance.InstanceHandle;
import dev.engine_room.flywheel.api.instance.InstanceType;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.texture.OverlayTexture;
public abstract class ColoredLitInstance extends AbstractInstance implements FlatLit {
@ -11,43 +10,42 @@ public abstract class ColoredLitInstance extends AbstractInstance implements Fla
public byte b = (byte) 0xFF;
public byte a = (byte) 0xFF;
public int packedLight;
public int overlay = OverlayTexture.NO_OVERLAY;
public int light = 0;
public ColoredLitInstance(InstanceType<? extends ColoredLitInstance> type, InstanceHandle handle) {
super(type, handle);
}
public ColoredLitInstance setColor(int color) {
return setColor(color, false);
public ColoredLitInstance color(int color) {
return color(color, false);
}
public ColoredLitInstance setColor(int color, boolean alpha) {
public ColoredLitInstance color(int color, boolean alpha) {
byte r = (byte) ((color >> 16) & 0xFF);
byte g = (byte) ((color >> 8) & 0xFF);
byte b = (byte) (color & 0xFF);
if (alpha) {
byte a = (byte) ((color >> 24) & 0xFF);
return setColor(r, g, b, a);
return color(r, g, b, a);
} else {
return setColor(r, g, b);
return color(r, g, b);
}
}
public ColoredLitInstance setColor(int r, int g, int b) {
return setColor((byte) r, (byte) g, (byte) b);
public ColoredLitInstance color(int r, int g, int b) {
return color((byte) r, (byte) g, (byte) b);
}
public ColoredLitInstance setColor(byte r, byte g, byte b) {
public ColoredLitInstance color(byte r, byte g, byte b) {
this.r = r;
this.g = g;
this.b = b;
return this;
}
public ColoredLitInstance setColor(byte r, byte g, byte b, byte a) {
public ColoredLitInstance color(byte r, byte g, byte b, byte a) {
this.r = r;
this.g = g;
this.b = b;
@ -55,19 +53,14 @@ public abstract class ColoredLitInstance extends AbstractInstance implements Fla
return this;
}
@Override
public ColoredLitInstance light(int blockLight, int skyLight) {
return light(LightTexture.pack(blockLight, skyLight));
}
@Override
public ColoredLitInstance light(int packedLight) {
this.packedLight = packedLight;
return this;
}
public ColoredLitInstance setOverlay(int overlay) {
public ColoredLitInstance overlay(int overlay) {
this.overlay = overlay;
return this;
}
@Override
public ColoredLitInstance light(int light) {
this.light = light;
return this;
}
}

View file

@ -9,18 +9,20 @@ import net.minecraft.client.renderer.LightTexture;
* if they wish to make use of the relighting utilities in {@link AbstractVisual}.
*/
public interface FlatLit extends Instance {
/**
* Set the block and sky light values for this instance.
* @param blockLight Block light value
* @param skyLight Sky light value
* @return {@code this} for chaining
*/
FlatLit light(int blockLight, int skyLight);
/**
* Set the packed light value for this instance.
* @param packedLight Packed block and sky light per {@link LightTexture#pack(int, int)}
* @return {@code this} for chaining
*/
FlatLit light(int packedLight);
/**
* Set the block and sky light values for this instance.
* @param blockLight Block light value
* @param skyLight Sky light value
* @return {@code this} for chaining
*/
default FlatLit light(int blockLight, int skyLight) {
return light(LightTexture.pack(blockLight, skyLight));
}
}

View file

@ -8,7 +8,7 @@ import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.layout.FloatRepr;
import dev.engine_room.flywheel.api.layout.IntegerRepr;
import dev.engine_room.flywheel.api.layout.LayoutBuilder;
import dev.engine_room.flywheel.lib.math.MatrixMath;
import dev.engine_room.flywheel.lib.util.ExtraMemoryOps;
public final class InstanceTypes {
public static final InstanceType<TransformedInstance> TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new)
@ -24,12 +24,10 @@ public final class InstanceTypes {
MemoryUtil.memPutByte(ptr + 1, instance.g);
MemoryUtil.memPutByte(ptr + 2, instance.b);
MemoryUtil.memPutByte(ptr + 3, instance.a);
MemoryUtil.memPutShort(ptr + 4, (short) (instance.overlay & 0xFFFF));
MemoryUtil.memPutShort(ptr + 6, (short) (instance.overlay >> 16 & 0xFFFF));
MemoryUtil.memPutShort(ptr + 8, (short) (instance.packedLight & 0xFFFF));
MemoryUtil.memPutShort(ptr + 10, (short) (instance.packedLight >> 16 & 0xFFFF));
MatrixMath.writeUnsafe(ptr + 12, instance.model);
MatrixMath.writeUnsafe(ptr + 76, instance.normal);
ExtraMemoryOps.put2x16(ptr + 4, instance.overlay);
ExtraMemoryOps.put2x16(ptr + 8, instance.light);
ExtraMemoryOps.putMatrix4f(ptr + 12, instance.model);
ExtraMemoryOps.putMatrix3f(ptr + 76, instance.normal);
})
.vertexShader(Flywheel.rl("instance/transformed.vert"))
.cullShader(Flywheel.rl("instance/cull/transformed.glsl"))
@ -49,20 +47,15 @@ public final class InstanceTypes {
MemoryUtil.memPutByte(ptr + 1, instance.g);
MemoryUtil.memPutByte(ptr + 2, instance.b);
MemoryUtil.memPutByte(ptr + 3, instance.a);
MemoryUtil.memPutShort(ptr + 4, (short) (instance.overlay & 0xFFFF));
MemoryUtil.memPutShort(ptr + 6, (short) (instance.overlay >> 16 & 0xFFFF));
MemoryUtil.memPutShort(ptr + 8, (short) (instance.packedLight & 0xFFFF));
MemoryUtil.memPutShort(ptr + 10, (short) (instance.packedLight >> 16 & 0xFFFF));
ExtraMemoryOps.put2x16(ptr + 4, instance.overlay);
ExtraMemoryOps.put2x16(ptr + 8, instance.light);
MemoryUtil.memPutFloat(ptr + 12, instance.posX);
MemoryUtil.memPutFloat(ptr + 16, instance.posY);
MemoryUtil.memPutFloat(ptr + 20, instance.posZ);
MemoryUtil.memPutFloat(ptr + 24, instance.pivotX);
MemoryUtil.memPutFloat(ptr + 28, instance.pivotY);
MemoryUtil.memPutFloat(ptr + 32, instance.pivotZ);
MemoryUtil.memPutFloat(ptr + 36, instance.rotation.x);
MemoryUtil.memPutFloat(ptr + 40, instance.rotation.y);
MemoryUtil.memPutFloat(ptr + 44, instance.rotation.z);
MemoryUtil.memPutFloat(ptr + 48, instance.rotation.w);
ExtraMemoryOps.putQuaternionf(ptr + 36, instance.rotation);
})
.vertexShader(Flywheel.rl("instance/oriented.vert"))
.cullShader(Flywheel.rl("instance/cull/oriented.glsl"))

View file

@ -7,25 +7,10 @@ import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.lwjgl.system.MemoryUtil;
import com.mojang.blaze3d.vertex.PoseStack;
public final class MatrixMath {
private MatrixMath() {
}
public static PoseStack copyPoseStack(PoseStack stack) {
PoseStack copy = new PoseStack();
copy.last()
.pose()
.set(stack.last()
.pose());
copy.last()
.normal()
.set(stack.last()
.normal());
return copy;
}
public static float transformPositionX(Matrix4f matrix, float x, float y, float z) {
return fma(matrix.m00(), x, fma(matrix.m10(), y, fma(matrix.m20(), z, matrix.m30())));
}
@ -50,37 +35,6 @@ public final class MatrixMath {
return fma(matrix.m02(), x, fma(matrix.m12(), y, matrix.m22() * z));
}
public static void writeUnsafe(long ptr, Matrix4f matrix) {
MemoryUtil.memPutFloat(ptr, matrix.m00());
MemoryUtil.memPutFloat(ptr + 4, matrix.m01());
MemoryUtil.memPutFloat(ptr + 8, matrix.m02());
MemoryUtil.memPutFloat(ptr + 12, matrix.m03());
MemoryUtil.memPutFloat(ptr + 16, matrix.m10());
MemoryUtil.memPutFloat(ptr + 20, matrix.m11());
MemoryUtil.memPutFloat(ptr + 24, matrix.m12());
MemoryUtil.memPutFloat(ptr + 28, matrix.m13());
MemoryUtil.memPutFloat(ptr + 32, matrix.m20());
MemoryUtil.memPutFloat(ptr + 36, matrix.m21());
MemoryUtil.memPutFloat(ptr + 40, matrix.m22());
MemoryUtil.memPutFloat(ptr + 44, matrix.m23());
MemoryUtil.memPutFloat(ptr + 48, matrix.m30());
MemoryUtil.memPutFloat(ptr + 52, matrix.m31());
MemoryUtil.memPutFloat(ptr + 56, matrix.m32());
MemoryUtil.memPutFloat(ptr + 60, matrix.m33());
}
public static void writeUnsafe(long ptr, Matrix3f matrix) {
MemoryUtil.memPutFloat(ptr, matrix.m00());
MemoryUtil.memPutFloat(ptr + 4, matrix.m01());
MemoryUtil.memPutFloat(ptr + 8, matrix.m02());
MemoryUtil.memPutFloat(ptr + 12, matrix.m10());
MemoryUtil.memPutFloat(ptr + 16, matrix.m11());
MemoryUtil.memPutFloat(ptr + 20, matrix.m12());
MemoryUtil.memPutFloat(ptr + 24, matrix.m20());
MemoryUtil.memPutFloat(ptr + 28, matrix.m21());
MemoryUtil.memPutFloat(ptr + 32, matrix.m22());
}
/**
* Extracts the greatest scale factor across all axes from the given matrix.
*

View file

@ -0,0 +1,82 @@
package dev.engine_room.flywheel.lib.util;
import org.joml.Matrix3fc;
import org.joml.Matrix4fc;
import org.joml.Quaternionfc;
import org.joml.Vector2fc;
import org.joml.Vector3fc;
import org.joml.Vector4fc;
import org.lwjgl.system.MemoryUtil;
public final class ExtraMemoryOps {
private ExtraMemoryOps() {
}
public static void put4x8(long ptr, int value) {
MemoryUtil.memPutByte(ptr, (byte) (value & 0xFF));
MemoryUtil.memPutByte(ptr + 1, (byte) (value >>> 8 & 0xFF));
MemoryUtil.memPutByte(ptr + 2, (byte) (value >>> 16 & 0xFF));
MemoryUtil.memPutByte(ptr + 3, (byte) (value >>> 24 & 0xFF));
}
public static void put2x16(long ptr, int value) {
MemoryUtil.memPutShort(ptr, (short) (value & 0xFFFF));
MemoryUtil.memPutShort(ptr + 2, (short) (value >>> 16 & 0xFFFF));
}
public static void putVector2f(long ptr, Vector2fc vector) {
MemoryUtil.memPutFloat(ptr, vector.x());
MemoryUtil.memPutFloat(ptr + 4, vector.y());
}
public static void putVector3f(long ptr, Vector3fc vector) {
MemoryUtil.memPutFloat(ptr, vector.x());
MemoryUtil.memPutFloat(ptr + 4, vector.y());
MemoryUtil.memPutFloat(ptr + 8, vector.z());
}
public static void putVector4f(long ptr, Vector4fc vector) {
MemoryUtil.memPutFloat(ptr, vector.x());
MemoryUtil.memPutFloat(ptr + 4, vector.y());
MemoryUtil.memPutFloat(ptr + 8, vector.z());
MemoryUtil.memPutFloat(ptr + 12, vector.w());
}
public static void putQuaternionf(long ptr, Quaternionfc quaternion) {
MemoryUtil.memPutFloat(ptr, quaternion.x());
MemoryUtil.memPutFloat(ptr + 4, quaternion.y());
MemoryUtil.memPutFloat(ptr + 8, quaternion.z());
MemoryUtil.memPutFloat(ptr + 12, quaternion.w());
}
public static void putMatrix3f(long ptr, Matrix3fc matrix) {
MemoryUtil.memPutFloat(ptr, matrix.m00());
MemoryUtil.memPutFloat(ptr + 4, matrix.m01());
MemoryUtil.memPutFloat(ptr + 8, matrix.m02());
MemoryUtil.memPutFloat(ptr + 12, matrix.m10());
MemoryUtil.memPutFloat(ptr + 16, matrix.m11());
MemoryUtil.memPutFloat(ptr + 20, matrix.m12());
MemoryUtil.memPutFloat(ptr + 24, matrix.m20());
MemoryUtil.memPutFloat(ptr + 28, matrix.m21());
MemoryUtil.memPutFloat(ptr + 32, matrix.m22());
}
public static void putMatrix4f(long ptr, Matrix4fc matrix) {
MemoryUtil.memPutFloat(ptr, matrix.m00());
MemoryUtil.memPutFloat(ptr + 4, matrix.m01());
MemoryUtil.memPutFloat(ptr + 8, matrix.m02());
MemoryUtil.memPutFloat(ptr + 12, matrix.m03());
MemoryUtil.memPutFloat(ptr + 16, matrix.m10());
MemoryUtil.memPutFloat(ptr + 20, matrix.m11());
MemoryUtil.memPutFloat(ptr + 24, matrix.m12());
MemoryUtil.memPutFloat(ptr + 28, matrix.m13());
MemoryUtil.memPutFloat(ptr + 32, matrix.m20());
MemoryUtil.memPutFloat(ptr + 36, matrix.m21());
MemoryUtil.memPutFloat(ptr + 40, matrix.m22());
MemoryUtil.memPutFloat(ptr + 44, matrix.m23());
MemoryUtil.memPutFloat(ptr + 48, matrix.m30());
MemoryUtil.memPutFloat(ptr + 52, matrix.m31());
MemoryUtil.memPutFloat(ptr + 56, matrix.m32());
MemoryUtil.memPutFloat(ptr + 60, matrix.m33());
}
}

View file

@ -109,7 +109,7 @@ public class HitboxComponent implements EntityComponent {
.loadIdentity()
.translate(entityX - bbWidthHalf, entityY + entity.getEyeHeight() - 0.01, entityZ - bbWidthHalf)
.scale(bbWidth, 0.02f, bbWidth)
.setColor(255, 0, 0)
.color(255, 0, 0)
.setChanged();
}
@ -119,7 +119,7 @@ public class HitboxComponent implements EntityComponent {
.loadIdentity()
.translate(entityX, entityY + entity.getEyeHeight(), entityZ)
.rotate(new Quaternionf().rotateTo(0, 1, 0, (float) viewVector.x, (float) viewVector.y, (float) viewVector.z))
.setColor(0, 0, 255)
.color(0, 0, 255)
.setChanged();
}

View file

@ -35,7 +35,7 @@ public class TntMinecartVisual<T extends MinecartTNT> extends MinecartVisual<T>
}
contents.setTransform(stack)
.setOverlay(overlay)
.overlay(overlay)
.setChanged();
}
}