Skip to content

Commit

Permalink
Packing up
Browse files Browse the repository at this point in the history
- Save 8 bytes per glyph instance by switching uvs to
  FloatRepr.NORMALIZED_UNSIGNED_SHORT
- With vanilla's 256x glyph atlases we could technically save another 4
  bytes, but that would make compat with caxton, which uses 4096x
  atlases, much more difficult later
  • Loading branch information
Jozufozu committed Sep 28, 2024
1 parent 0bb5ecf commit 4b4bddd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.engine_room.flywheel.api.instance.InstanceHandle;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.lib.internal.FlwLibLink;
import dev.engine_room.flywheel.lib.internal.GlyphExtension;
import net.minecraft.client.gui.font.glyphs.BakedGlyph;
import net.minecraft.util.FastColor;

Expand All @@ -15,10 +16,8 @@ public class GlyphInstance extends AbstractInstance {

public final Matrix4f pose = new Matrix4f();

public float u0;
public float u1;
public float v0;
public float v1;
public int us;
public int vs;

public byte red = (byte) 0xFF;
public byte green = (byte) 0xFF;
Expand All @@ -34,10 +33,7 @@ public GlyphInstance(InstanceType<?> type, InstanceHandle handle) {
public GlyphInstance setGlyph(BakedGlyph glyph, float x, float y, boolean italic) {
var glyphReader = FlwLibLink.INSTANCE.getGlyphExtension(glyph);

u0 = glyphReader.flywheel$u0();
u1 = glyphReader.flywheel$u1();
v0 = glyphReader.flywheel$v0();
v1 = glyphReader.flywheel$v1();
setUvs(glyphReader);
float left = glyphReader.flywheel$left();
float right = glyphReader.flywheel$right();
float up = glyphReader.flywheel$up();
Expand All @@ -56,10 +52,7 @@ public GlyphInstance setGlyph(BakedGlyph glyph, float x, float y, boolean italic
public GlyphInstance setEffect(BakedGlyph glyph, float x0, float y0, float x1, float y1, float depth) {
var glyphReader = FlwLibLink.INSTANCE.getGlyphExtension(glyph);

u0 = glyphReader.flywheel$u0();
u1 = glyphReader.flywheel$u1();
v0 = glyphReader.flywheel$v0();
v1 = glyphReader.flywheel$v1();
setUvs(glyphReader);

pose.translate(x0, y0, depth);
pose.scale(x1 - x0, y1 - y0, 1.0f);
Expand Down Expand Up @@ -101,4 +94,14 @@ public GlyphInstance color(byte red, byte green, byte blue) {
this.blue = blue;
return this;
}

private void setUvs(GlyphExtension glyphReader) {
float u0 = glyphReader.flywheel$u0();
float u1 = glyphReader.flywheel$u1();
float v0 = glyphReader.flywheel$v0();
float v1 = glyphReader.flywheel$v1();

us = (int) (u0 * 65536) | ((int) (u1 * 65536) << 16);
vs = (int) (v0 * 65536) | ((int) (v1 * 65536) << 16);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,19 @@ public final class InstanceTypes {
public static final InstanceType<GlyphInstance> GLYPH = SimpleInstanceType.builder(GlyphInstance::new)
.layout(LayoutBuilder.create()
.matrix("pose", FloatRepr.FLOAT, 4)
.vector("u0u1v0v1", FloatRepr.FLOAT, 4)
.vector("u0u1v0v1", FloatRepr.NORMALIZED_UNSIGNED_SHORT, 4)
.vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4)
.vector("light", FloatRepr.UNSIGNED_SHORT, 2)
.build())
.writer((ptr, instance) -> {
ExtraMemoryOps.putMatrix4f(ptr, instance.pose);
MemoryUtil.memPutFloat(ptr + 64, instance.u0);
MemoryUtil.memPutFloat(ptr + 68, instance.u1);
MemoryUtil.memPutFloat(ptr + 72, instance.v0);
MemoryUtil.memPutFloat(ptr + 76, instance.v1);
MemoryUtil.memPutByte(ptr + 80, instance.red);
MemoryUtil.memPutByte(ptr + 81, instance.green);
MemoryUtil.memPutByte(ptr + 82, instance.blue);
MemoryUtil.memPutByte(ptr + 83, instance.alpha);
ExtraMemoryOps.put2x16(ptr + 84, instance.light);
ExtraMemoryOps.put2x16(ptr + 64, instance.us);
ExtraMemoryOps.put2x16(ptr + 68, instance.vs);
MemoryUtil.memPutByte(ptr + 72, instance.red);
MemoryUtil.memPutByte(ptr + 73, instance.green);
MemoryUtil.memPutByte(ptr + 74, instance.blue);
MemoryUtil.memPutByte(ptr + 75, instance.alpha);
ExtraMemoryOps.put2x16(ptr + 76, instance.light);
})
.vertexShader(Flywheel.rl("instance/glyph.vert"))
.cullShader(Flywheel.rl("instance/cull/glyph.glsl"))
Expand Down

0 comments on commit 4b4bddd

Please sign in to comment.