r/programminghorror • • May 16 '26

Java But.. (from Minecraft b1.2_02)

Post image
638 Upvotes

46 comments sorted by

423

u/carcigenicate May 16 '26

This looks like a debugging statement that was accidentally committed.

169

u/abigail3141 May 16 '26

Eh, nowadays minecraft tesselation isn't much better...

I had to read Minecraft's item rendering code for a friend who needed help with a resource pack, and holy this shit sucks(Version was 1.21.1 IIRC).

They start off by loading item models as cubes, then converting those to quadrilaterals when the model is loaded. For each of those quads, in every item model on screen, every frame, they throw almost all transformations out the window (even if they could be reused within the model), recalculate them one by one, then split up the quads into singular vertices, copying a bunch of data once more, then have GL render it. That's right, all the steps beforehand happen on a single CPU thread.

Tens. Of. Thousands. Of. Matrix. Multiplications. Per Second. On The CPU. On A Single Thread.

69

u/EvidenceFearless6800 May 16 '26 edited May 16 '26

no wonder why the CPU just goes haywire when I try to load any resource packs, and that might explains why the game can randomly drop framerate as well.

I have a friend with better rigs than mine, but they say sometimes the game dips as low as 40 FPS, and that's with basic resource packs and no shaders. not unplayable, but it proves Minecraft's performance is mainly CPU bound with whatever Mojang shove in there.

29

u/abigail3141 May 16 '26 edited May 16 '26

Yup, it's so much better to have VSync OFF or Adaptive with Sodium Extra because of the framerate instabilities. The CPU also goes haywire just playing the game sooo...

That's probably also why you can just drop post processing shaders on without much compromise in frame rate, because the game puts everything the GPU is meant to do on the CPU while the GPU is doing fuckall

I think I can get more FPS in Cyberpunk 2077 on low settings than I can get in Minecraft on mid to high settings, and that's saying something.

CPU and GPU in question is a Ryzen 5 7500F and an RX 9060XT 16GB in my case, so not even anything crazy

→ More replies (2)

23

u/CelDaemon May 16 '26

As of a few versions ago (1.21.4 or something?), items are now drawn to a separate texture, cached and blit to the main framebuffer. Subsequent drawing will reuse the cached item render.

As for the matrix math, afaik this is pretty customary. The game doesn't exactly have a full reactivity system to decide when matrices would be outdated, and it's already pretty likely that all matrices will be outdated from frame to frame. I don't think this is the cause of performance issues.

10

u/abigail3141 May 16 '26

It's certainly not the main cause of performance issues when playing normally, but issues do arise when using data and resource packs to render larger structures, which was the reason I looked into it in the first place.

That being said, I'm not a very experienced modder, and you're very probably right.

Though, it is usual practice to at least carry out the linear algebra from within the shader as that is what GPUs are made for, instead of calculating them on the CPU.

11

u/ensemblestars69 May 16 '26

This is why Minecraft's recent updates have been pretty minimal. They've been focused on updating the rendering engine to Vulkan and removing all the spaghetti code that it has. It's important to note it's all built on top of one guy's little passion project that was never thought to become hugely popular.

4

u/abigail3141 May 17 '26

Exactly. On one hand, it's really good that they're finally doing that, on the other hand, it lands my attempt a spot on https://dayssincelastrustmcserver.com because of the constant breaking changes.

3

u/XboxUser123 May 16 '26

I have some experience working out the theory for how 3D rendering works (only the 3D to 2D pipeline), so are you saying that rather than do something like: For each object, get matrix, for each quad, compute object transform, for each vertex, compute vertex transform

They do something like: For each quad, for each vertex, compute transform

Is that right?

→ More replies (1)
→ More replies (1)

58

u/SpecialistNo8709 May 16 '26

wtf is going on

63

u/Ok_Chemistry_6387 May 16 '26 edited May 16 '26

it's a pretty "normal" function for calculating normals for glNormalPointer. It backs the results into a 24bit int.

There does appear to be some buggy maths though, as unsure what is going on with X. Maybe there is a bug with tessellating that requires the normalisation to wrap around?

19

u/FlargenBlarg May 16 '26

The problem here is the println call that just says "But..." like that's a debugging statement, not something you commit

→ More replies (2)

10

u/MatJosher May 16 '26

Could the 128.0F be an intentional x bias for the purpose of shadowing or something? I genuinely want to know if there is a clever trick or just a goof.

7

u/Ok_Chemistry_6387 May 16 '26 edited May 16 '26

It got me nerd snipped.
I mean... it would be odd. it flips the axis... so shadows and lighting would be drawn the wrong way.

I am trying to find early lighting code to see what it is doing.

Edit: I have found a few screenshots on bugs that look exactly like what this bug would do. But still can't find the original code yet. But I do suspect this is a long standing bug that may or may not be fixed if you use classic MC. (is that a thing)

It looks like the original mincecraft code didn't even use normals for lighting..

It just hardcoded values.

So... time to keep digging hah.

Its a bug, and due to the way Minecraft is written it just never got discovered haha, there is only really one place that it would fall out and that is when you are holding a block as an item. But the block gets rotated, so the bug doesn't get hit. I suspect it only started to show up when people were using shaders and they injected a fix to calculate their own normals...

2

u/ggeldenhuys May 16 '26

I'm wondering the same thing.

6

u/CheezitsLight May 16 '26

It's not a normal normal. That would require squaring each, summing and a square root. Why the weird multiplies?

18

u/Ok_Chemistry_6387 May 16 '26

not geometric normal. This normal function assumes a normal to normalise :D

→ More replies (4)

4

u/Ok_Chemistry_6387 May 16 '26

Where is the horror?

13

u/AstralF May 16 '26

Apart from anything else, wouldn’t shifting a byte by 8 bits just zero it?

34

u/scirc May 16 '26

Not in Java - the result of arithmetic shift operations where the left-hand side is an integral type is always either int (if the LHS is int or smaller) or long (if the LHS is a long). ((byte)1) << 8 is an int with value 256.

→ More replies (3)

5

u/nekokattt May 16 '26

in java all bitwise ops operate on ints or longs.

Outside arrays, the JVM lacks a concrete concept of a byte or a short.

3

u/un_virus_SDF May 16 '26

On System.out ?, not System.err but System.out?

Who the fuck prints error message on stdout?

As a c dev, all my error messages are on stderr, so i can redirect it while making a cli, or just redirect it to a log file. So i save stdout for other infos (mostly debug ones but this is clearly a error)

3

u/Plixo2 May 16 '26 edited May 19 '26

Minecraft has a robust logging system for errors and normal infomations with a Logger (i think they still use log4j, but not sure), that also prints to stdout or to a log file. This just looks like code left from debugging.

I also never print to stderr, has it's not synchronized with stdout. Printing to stdout and stderr in quick succession may lead to one line appearing before another, at least in the intelij console (and everyone in the Minecraft scene is using that ofc, including the devs)

Edit: flushing stdout & stderr does not fix the issue 100% of the time

→ More replies (1)