This PR adds a more efficient and better-looking algorithm for translucency sorting than the old distance-sorting approach, which was both slow (since it ran all the time) and occasionally incorrect (cause distance is measured to the middle of a quad, so a large quad can count as "farther" than a small one it is actually in front of).
The algorithms here are implemented from Douira's master's thesis, Implementation and Analysis of Geometric Algorithms for Translucency Sorting in Minecraft. This implementation is based solely on the algorithms described in the paper, and no code is taken from the two (non-FOSS) Sodium PRs mentioned in it. AI was used to help write local tests and catch bugs, but design decisions and code were all made by me.
The algorithm
When a section is built, its translucent geometry is analyzed once and given the cheapest correct treatment:
Nothing to sort. Three cases: there are no translucent quads (or none that cover a pixel), they all lie in one plane, or each one is an outward face on the section's bounding box. None of these can ever overlap on screen.
One fixed order. If every quad is parallel to the others (say, a flat lake, or stacked water surfaces), a single order is correct from every camera position. For sections of up to 2048 quads we also try the topological sort from Chapter 3 (Sorting With Graphs) of the thesis: build a graph of "A must be drawn before B" and, if it has no cycles, that order is final. The search is depth-first over implicit edges, so a section that does have a cycle is rejected after a few nodes rather than after every pair.
Partition tree (chapter 4 of the thesis). Minecraft geometry almost always sits on block faces, so there's usually a flat plane with no quad crossing it, which the paper calls a free cut. Everything on the camera's side of that plane is drawn after everything on the far side, wherever the camera is on that side. Cutting recursively gives a tree, and reading a draw order off it is one linear pass that writes straight into the index buffer. Best of all, the order only changes when the camera crosses one of the cut planes, so those planes become the trigger for re-sorting instead of a fixed camera distance. A per-axis bucketed index makes the crossing check cheap even with thousands of sections, and a crossing that happens while a section is still being built is caught when the build is uploaded.
Distance sort, the old algorithm, kept only as the global fallback for geometry nothing above can handle.
Every cut plane is a trigger, so the tree is built with as few as it can get away with. Before a group of quads gets cut, it's checked for a cheaper treatment: if the group is parallel, or has ≤256 quads and sorts topologically, it becomes a single leaf with a fixed order and no planes below it. When a multi-cut would only slice a run of parallel quads (like a water surface between walls) into one slab per block, only the cuts that actually carry quads are kept. And a group with no free cut gets the toposort before the intersector handling below, since having no free cut doesn't imply a cycle. On the synthetic test scenes this removes between a third and two thirds of the trigger planes, and readout time falls with the node count.
One heuristic from section 4.2 (Handling of Special Cases) is also implemented, which helps fix bugs (see figure 4.6) in sections where geometry physically crosses itself, like glass panes inside water. No plane can separate intersecting geometry and no order is truly correct (see figure 2.3). The thesis calls this primary intersector detection: find a small set of quads that touches every crossing (greedily, most crossings first), always draw those first, and partition the rest as usual.
Deviations
The thesis also implements dynamic topological sorting with a camera polytope and a triggering structure built from quantized quad normals and interval trees (GFNI); neither is here. Douira's conclusion is that the partition tree supersedes the dynamic graph sort in nearly all cases, so the only triggers are the tree's axis-aligned cut planes, stored exactly rather than quantized. The visibility test for unaligned quads uses bounding-box corners, which never misses an ordering constraint but can report cycles the thesis's center-based test would not, so a few more sections end up in the tree.
Inside the tree this implementation goes further than the paper: a small group is tried for a fixed order before it is cut (the thesis lists this as future work), a run of parallel quads isn't sliced by every free gap (the thesis takes every gap it finds), and a group with no free cut is tried for a fixed order whether or not it intersects. Primary intersector detection drops the paper's "strict minority" guard, since it stopped the split from firing on a recreation of figure 4.6, and picks the cover greedily by most crossings first. The whole-section fixed order also accepts normals within about eleven degrees of parallel so slightly slanted water still sorts statically, which is an approximation the thesis doesn't make.
Not carried over: reuse of unchanged tree nodes across remeshes, compression of leaf index lists (thesis measured no gain), the cumulative-distance heap for triggering distance sorts (the old section-change rule stays, and distance sorting is now extremely rare anyways), and block-face culling for unsorted sections.
Housekeeping
Section metadata gained a flag so the per-frame distance re-sort only visits sections that still need it.
The old quad analyzer is now just a recorder; all the actual analysis lives in TranslucentSorter.
The camera is moved into section space before it is rounded to float, so sorting stays exact far from the origin.
The F3 overlay has a Tree Sort line showing the total trigger planes, sections triggered per second, sort tasks per second and their average time.
This PR adds a more efficient and better-looking algorithm for translucency sorting than the old distance-sorting approach, which was both slow (since it ran all the time) and occasionally incorrect (cause distance is measured to the middle of a quad, so a large quad can count as "farther" than a small one it is actually in front of).
The algorithms here are implemented from Douira's master's thesis, [Implementation and Analysis of Geometric Algorithms for Translucency Sorting in Minecraft](https://douira.dev/assets/document/douira-master-thesis.pdf). This implementation is based solely on the algorithms described in the paper, and no code is taken from the two (non-FOSS) Sodium PRs mentioned in it. AI was used to help write local tests and catch bugs, but design decisions and code were all made by me.
## The algorithm
When a section is built, its translucent geometry is analyzed once and given the cheapest correct treatment:
1. **Nothing to sort.** Three cases: there are no translucent quads (or none that cover a pixel), they all lie in one plane, or each one is an outward face on the section's bounding box. None of these can ever overlap on screen.
2. **One fixed order.** If every quad is parallel to the others (say, a flat lake, or stacked water surfaces), a single order is correct from every camera position. For sections of up to 2048 quads we also try the topological sort from Chapter 3 (Sorting With Graphs) of the thesis: build a graph of "A must be drawn before B" and, if it has no cycles, that order is final. The search is depth-first over implicit edges, so a section that does have a cycle is rejected after a few nodes rather than after every pair.
3. **Partition tree** (chapter 4 of the thesis). Minecraft geometry almost always sits on block faces, so there's usually a flat plane with no quad crossing it, which the paper calls a *free cut*. Everything on the camera's side of that plane is drawn after everything on the far side, wherever the camera is on that side. Cutting recursively gives a tree, and reading a draw order off it is one linear pass that writes straight into the index buffer. Best of all, the order only changes when the camera crosses one of the cut planes, so those planes become the *trigger* for re-sorting instead of a fixed camera distance. A per-axis bucketed index makes the crossing check cheap even with thousands of sections, and a crossing that happens while a section is still being built is caught when the build is uploaded.
4. **Distance sort**, the old algorithm, kept only as the global fallback for geometry nothing above can handle.
Every cut plane is a trigger, so the tree is built with as few as it can get away with. Before a group of quads gets cut, it's checked for a cheaper treatment: if the group is parallel, or has ≤256 quads and sorts topologically, it becomes a single leaf with a fixed order and no planes below it. When a multi-cut would only slice a run of parallel quads (like a water surface between walls) into one slab per block, only the cuts that actually carry quads are kept. And a group with no free cut gets the toposort before the intersector handling below, since having no free cut doesn't imply a cycle. On the synthetic test scenes this removes between a third and two thirds of the trigger planes, and readout time falls with the node count.
One heuristic from section 4.2 (Handling of Special Cases) is also implemented, which helps fix bugs (see figure 4.6) in sections where geometry physically crosses itself, like glass panes inside water. No plane can separate intersecting geometry and no order is truly correct (see figure 2.3). The thesis calls this **primary intersector detection**: find a small set of quads that touches every crossing (greedily, most crossings first), always draw those first, and partition the rest as usual.
## Deviations
The thesis also implements dynamic topological sorting with a camera polytope and a triggering structure built from quantized quad normals and interval trees (GFNI); neither is here. Douira's conclusion is that the partition tree supersedes the dynamic graph sort in nearly all cases, so the only triggers are the tree's axis-aligned cut planes, stored exactly rather than quantized. The visibility test for unaligned quads uses bounding-box corners, which never misses an ordering constraint but can report cycles the thesis's center-based test would not, so a few more sections end up in the tree.
Inside the tree this implementation goes further than the paper: a small group is tried for a fixed order before it is cut (the thesis lists this as future work), a run of parallel quads isn't sliced by every free gap (the thesis takes every gap it finds), and a group with no free cut is tried for a fixed order whether or not it intersects. Primary intersector detection drops the paper's "strict minority" guard, since it stopped the split from firing on a recreation of figure 4.6, and picks the cover greedily by most crossings first. The whole-section fixed order also accepts normals within about eleven degrees of parallel so slightly slanted water still sorts statically, which is an approximation the thesis doesn't make.
Not carried over: reuse of unchanged tree nodes across remeshes, compression of leaf index lists (thesis measured no gain), the cumulative-distance heap for triggering distance sorts (the old section-change rule stays, and distance sorting is now extremely rare anyways), and block-face culling for unsorted sections.
## Housekeeping
- Section metadata gained a flag so the per-frame distance re-sort only visits sections that still need it.
- The old quad analyzer is now just a recorder; all the actual analysis lives in `TranslucentSorter`.
- The camera is moved into section space before it is rounded to float, so sorting stays exact far from the origin.
- The F3 overlay has a `Tree Sort` line showing the total trigger planes, sections triggered per second, sort tasks per second and their average time.
just a heuristic, but easier to make than full quad splitting
if we find intersecting geometry, split it and render the intersecting stuff first
see page 30, "Handling of Special Cases"
A multi-partition node took up every free gap, so a run of parallel quads like a water surface between walls was sliced into one slab per block, and every gap plane was a trigger. When all quads between the planes are parallel, only cuts with quads lying on them are kept and merged slabs become static leaves.
conforms to the thesis's ordering of the fallbacks, since no free cut doesn't imply a cycle, so an acyclic group takes a static order instead of sinking the whole section to distance sorting
The initial order is sorted for the camera at submission, but its planes only enter the index at upload, so a crossing in between was never noticed and the section kept a stale order until the next one. The camera is now recorded at submission and tested against the new planes at upload, for the latest build only, so a stale result cannot disturb a newer in-flight one.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
This PR adds a more efficient and better-looking algorithm for translucency sorting than the old distance-sorting approach, which was both slow (since it ran all the time) and occasionally incorrect (cause distance is measured to the middle of a quad, so a large quad can count as "farther" than a small one it is actually in front of).
The algorithms here are implemented from Douira's master's thesis, Implementation and Analysis of Geometric Algorithms for Translucency Sorting in Minecraft. This implementation is based solely on the algorithms described in the paper, and no code is taken from the two (non-FOSS) Sodium PRs mentioned in it. AI was used to help write local tests and catch bugs, but design decisions and code were all made by me.
The algorithm
When a section is built, its translucent geometry is analyzed once and given the cheapest correct treatment:
Every cut plane is a trigger, so the tree is built with as few as it can get away with. Before a group of quads gets cut, it's checked for a cheaper treatment: if the group is parallel, or has ≤256 quads and sorts topologically, it becomes a single leaf with a fixed order and no planes below it. When a multi-cut would only slice a run of parallel quads (like a water surface between walls) into one slab per block, only the cuts that actually carry quads are kept. And a group with no free cut gets the toposort before the intersector handling below, since having no free cut doesn't imply a cycle. On the synthetic test scenes this removes between a third and two thirds of the trigger planes, and readout time falls with the node count.
One heuristic from section 4.2 (Handling of Special Cases) is also implemented, which helps fix bugs (see figure 4.6) in sections where geometry physically crosses itself, like glass panes inside water. No plane can separate intersecting geometry and no order is truly correct (see figure 2.3). The thesis calls this primary intersector detection: find a small set of quads that touches every crossing (greedily, most crossings first), always draw those first, and partition the rest as usual.
Deviations
The thesis also implements dynamic topological sorting with a camera polytope and a triggering structure built from quantized quad normals and interval trees (GFNI); neither is here. Douira's conclusion is that the partition tree supersedes the dynamic graph sort in nearly all cases, so the only triggers are the tree's axis-aligned cut planes, stored exactly rather than quantized. The visibility test for unaligned quads uses bounding-box corners, which never misses an ordering constraint but can report cycles the thesis's center-based test would not, so a few more sections end up in the tree.
Inside the tree this implementation goes further than the paper: a small group is tried for a fixed order before it is cut (the thesis lists this as future work), a run of parallel quads isn't sliced by every free gap (the thesis takes every gap it finds), and a group with no free cut is tried for a fixed order whether or not it intersects. Primary intersector detection drops the paper's "strict minority" guard, since it stopped the split from firing on a recreation of figure 4.6, and picks the cover greedily by most crossings first. The whole-section fixed order also accepts normals within about eleven degrees of parallel so slightly slanted water still sorts statically, which is an approximation the thesis doesn't make.
Not carried over: reuse of unchanged tree nodes across remeshes, compression of leaf index lists (thesis measured no gain), the cumulative-distance heap for triggering distance sorts (the old section-change rule stays, and distance sorting is now extremely rare anyways), and block-face culling for unsorted sections.
Housekeeping
TranslucentSorter.Tree Sortline showing the total trigger planes, sections triggered per second, sort tasks per second and their average time.