Skip to content

Enchant glint optimizations - #115

Draft
SocksM wants to merge 11 commits into
Aerhhh:masterfrom
SocksM:enchant-glint-optimizations
Draft

SocksM wants to merge 11 commits into
Aerhhh:masterfrom
SocksM:enchant-glint-optimizations

Conversation

@SocksM

@SocksM SocksM commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

No description provided.

went from:
Dirt + enchant glint benchmark: warmup=3, iterations=10, average=2178,3 ms, min=2154,3 ms, max=2211,7 ms
to:
Dirt + enchant glint benchmark: warmup=3, iterations=10, average=938,2 ms, min=927,8 ms, max=945,8 ms
on my machine
as to not slow down the build process in the docker build for no reason
@SocksM

SocksM commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

the build failing is in the tooling package which i didnt change and gets fixed by #114

Old:
3x3 recipe + tooltip + enchanted item benchmark: warmup=3, iterations=10, average=9597,2 ms, min=9148,7 ms, max=9874,2 ms
New:
3x3 recipe + tooltip + enchanted item benchmark: warmup=3, iterations=10, average=7724,0 ms, min=7662,1 ms, max=7822,7 ms
from:
Dirt + enchant glint benchmark: warmup=3, iterations=10, average=1013,3 ms, min=1005,6 ms, max=1023,2 ms
to:
Dirt + enchant glint benchmark: warmup=3, iterations=10, average=864,0 ms, min=859,5 ms, max=877,0 ms
Comment on lines 124 to -168
@@ -148,37 +190,32 @@ private void sampleGlint(double u, double v, int textureWidth, int textureHeight
double fracX = texX - baseX;
double fracY = texY - baseY;

int topLeftColor = glintTexture.getRGB(leftX, topY);
int topRightColor = glintTexture.getRGB(rightX, topY);
int bottomLeftColor = glintTexture.getRGB(leftX, bottomY);
int bottomRightColor = glintTexture.getRGB(rightX, bottomY);

double weightTopLeft = (1.0 - fracX) * (1.0 - fracY);
double weightTopRight = fracX * (1.0 - fracY);
double weightBottomLeft = (1.0 - fracX) * fracY;
double weightBottomRight = fracX * fracY;

double red = ((topLeftColor >> 16) & 0xFF) * weightTopLeft
+ ((topRightColor >> 16) & 0xFF) * weightTopRight
+ ((bottomLeftColor >> 16) & 0xFF) * weightBottomLeft
+ ((bottomRightColor >> 16) & 0xFF) * weightBottomRight;
double green = ((topLeftColor >> 8) & 0xFF) * weightTopLeft
+ ((topRightColor >> 8) & 0xFF) * weightTopRight
+ ((bottomLeftColor >> 8) & 0xFF) * weightBottomLeft
+ ((bottomRightColor >> 8) & 0xFF) * weightBottomRight;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aerhhh something doesnt add up here because even in the original this will always be 255 for all the rgb values
the + 0.5f doesnt make sense if ur right after that casting to an int and the math.min just causes it to always be the max value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay gg it commented it on the wrong lines for some reason but what i ment to comment on was this

                int outR = (int) (Math.min(1.0f, baseR + addR) * 255.0f + 0.5f);
                int outG = (int) (Math.min(1.0f, baseG + addG) * 255.0f + 0.5f);
                int outB = (int) (Math.min(1.0f, baseB + addB) * 255.0f + 0.5f);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original is fine. Everything there is a normalized float from 0 to 1. baseR is the channel / 255, and addR tops out around 0.6 (1 × 0.8 tint × 0.75 alpha). Math.min(1.0f, …) is just a clamp, so you only get 255 when base + add > 1. The + 0.5f is there because the (int) cast truncates, so adding 0.5 first rounds to the nearest value: 127.5 becomes 128 instead of 127.

Comment on lines +162 to +164
int addR = (int) (sampled[0] * (GLINT_TINT[0] * glintAlpha));
int addG = (int) (sampled[1] * (GLINT_TINT[1] * glintAlpha));
int addB = (int) (sampled[2] * (GLINT_TINT[2] * glintAlpha));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maxing out is in the PR version. sampled and glintAlpha are 0 to 255 ints now, so sampled[0] * tint * glintAlpha ends up 255 times too big. With 100 and 75 you get 100 × 0.5 × 75 = 3750, which Math.min(255, …) clamps to 255. Dividing the add by 255 should fix it: addR = (int) (sampled[0] * GLINT_TINT[0] * glintAlpha / 255f).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants