CaffeineMC / sodium

A Minecraft mod designed to improve frame rates and reduce micro-stutter
Other
4.77k stars 811 forks source link

Add Fog Occlusion for particles #2766

Open AnOpenSauceDev opened 1 month ago

AnOpenSauceDev commented 1 month ago

I'm not sure if this is best suited for Sodium, but this PR fixes Minecraft's behavior of rendering particles regardless of fog occlusion. This PR makes Sodium skip particle rendering if a particle falls behind opaque fog and additionally has Fog Occlusion enabled (essentially just re-using the existing fog occlusion logic).

(Render Distance is set to 2.)

Sodium with Fog No Fog No Fog + Particle Fog Occlusion
2024-09-21_23 46 04 despite the fog occluding the particles, they are still having render called. 2024-09-21_23 46 24 disabling terrain fog shows the particles are still being rendered. 2024-09-21_23 46 55 with Fog Occlusion enabled, the particles no longer render past fog distance, or in some cases, outside of render distance

Let me know if I need to fix/rework anything!

jellysquid3 commented 1 month ago

I don't see a problem with the proposed changes as they are written. But has this been measured to be a problem? I've suspected that some servers will spawn many particles outside the render distance, but have not actually observed this to significantly impact performance.

AnOpenSauceDev commented 1 month ago

Particle rendering on thousands of particles can take up a few milliseconds every frame even on my PC, and I assume the time penalty from all the unnecessary rendering would have a worse effect on a lower-end computer. I do have some older hardware lying around, so I might benchmark the performance thoroughly there.

AnOpenSauceDev commented 1 month ago

On a slower laptop I have (AMD 7530U), a somewhat empty Hypixel Skyblock lobby in the End at 2 Render Distance takes about 1.5ms (lowest was 0.8ms, highest was 2.1ms) to render a frame that stays around 3000 particles.

Turning on fog occlusion drops that to the range of 0.4 to 0.6ms, while skipping 28% to 34% of all particles. It seems that at the very least particle-heavy servers do get a noticeable performance increase.

debug class used

douira commented 1 month ago

Interesting that servers send so many particles outside of the fog distance

AnOpenSauceDev commented 1 month ago

The worst-case scenario is that after joining a LAN server hosted on my home PC, the particles I'm spawning with a command block still render at over 100 blocks outside of the render distance (2 again) on the laptop.

No Fog, No Fog Occlusion

2024-09-23_15 01 58

command: particle minecraft:scrape ~ ~15 ~ 0 0 0 30 500 force (with normal, particles won't be sent past 32 blocks regardless of render distance.)

Fog, No Fog Occlusion

2024-09-23_15 02 52

It also seems that particles are rendering over the stars when in fog. This behavior also happens in Vanilla.

Fog + Fog Occlusion

2024-09-23_15 02 40

This is fixed with Particle Fog Occlusion.

AnOpenSauceDev commented 1 month ago

It seems that particles actually do render over the fog itself (w/o Fog Occlusion), but since they use the sky fog color it's not visible until sunrise/sunset. This then causes some ugly transitions with long-distance particles like campfire smoke.

2024-09-26_14 16 38

Side note: particles don't render behind translucent blocks/fluids, but I think this is intended behavior.

Benchmarking

In the somewhat plausible situation someone has buildings outside of render distance with lots of campfires going, performance can increase by almost 2X. (Rendering the same setup from the first image, a 15x15 fill of campfires)

2024-09-26_14 46 49

Rendering the scene inside of the fog distance looks like it has the same performance, so at the very least the occlusion check has a negligible impact.

The performance gains from this are going to be extremely situational, probably only having an effect on large multiplayer servers, some large builds, and mods/datapacks that make use of long-distance effects (i.e. some biomes from Terralith and Better End.), but it could fix some existing visual issues with particles.

Felix14-v2 commented 4 weeks ago

probably only having an effect on large multiplayer servers, some large builds, and mods/datapacks that make use of long-distance effects

Will underwater fog and fog from blindness effect also affect particle culling? This should significantly improve performance in many mini-games related to these effects.

AnOpenSauceDev commented 4 weeks ago

As of that latest commit, Darkness + Blindness now have an effect on culling. (I forgot to use the fog end distance 😔)

Water fog didn't drop the particle count by much after entering it, but I need to test that properly later.

AnOpenSauceDev commented 2 weeks ago

It looks like water fog isn't actually getting rid of particles because of fog, and I have made a few observations:

1) it looks like after the water fog transition stops, almost all of the world is still visible (at least at 4 chunks), but the fog + overlay just makes it much harder to see. Because of this, the particle count won't really change from being in water past the first few seconds after entering it.

2) despite this, no particles will be visible, as particles currently don't appear through translucent blocks/fluids after rendering. This actually causes thousands of particles to be waste time rendering when not submerged in an ocean. I have a few (probably too slow) ideas on how to stop the rendering anyways, but that doesn't apply to Fog Occlusion anymore.

Because of that being an unrelated problem, this feature is complete and working properly. The only extra thing I could do is also factor in height for distance to cull particles in a sphere instead of a cylinder (This didn't really provide much of a boost in render time in my survival world though).