|
1 | 1 | package net.caffeinemc.mods.sodium.client.render.chunk.lists;
|
2 | 2 |
|
| 3 | +import net.caffeinemc.mods.sodium.client.render.chunk.LocalSectionIndex; |
3 | 4 | import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection;
|
4 | 5 | import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionFlags;
|
| 6 | +import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion; |
| 7 | +import net.caffeinemc.mods.sodium.client.render.viewport.CameraTransform; |
| 8 | +import net.caffeinemc.mods.sodium.client.util.iterator.ByteArrayIterator; |
5 | 9 | import net.caffeinemc.mods.sodium.client.util.iterator.ByteIterator;
|
6 | 10 | import net.caffeinemc.mods.sodium.client.util.iterator.ReversibleByteArrayIterator;
|
7 |
| -import net.caffeinemc.mods.sodium.client.util.iterator.ByteArrayIterator; |
8 |
| -import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion; |
| 11 | +import net.minecraft.util.Mth; |
9 | 12 | import org.jetbrains.annotations.Nullable;
|
10 | 13 |
|
11 | 14 | public class ChunkRenderList {
|
@@ -37,6 +40,39 @@ public void reset(int frame) {
|
37 | 40 | this.lastVisibleFrame = frame;
|
38 | 41 | }
|
39 | 42 |
|
| 43 | + // clamping the relative camera position to the region bounds means there can only be very few different distances |
| 44 | + private static final int SORTING_HISTOGRAM_SIZE = RenderRegion.REGION_WIDTH + RenderRegion.REGION_HEIGHT + RenderRegion.REGION_LENGTH - 2; |
| 45 | + |
| 46 | + public void sortSections(CameraTransform transform, int[] sortItems) { |
| 47 | + var cameraX = Mth.clamp((transform.intX >> 4) - this.region.getChunkX(), 0, RenderRegion.REGION_WIDTH - 1); |
| 48 | + var cameraY = Mth.clamp((transform.intY >> 4) - this.region.getChunkY(), 0, RenderRegion.REGION_HEIGHT - 1); |
| 49 | + var cameraZ = Mth.clamp((transform.intZ >> 4) - this.region.getChunkZ(), 0, RenderRegion.REGION_LENGTH - 1); |
| 50 | + |
| 51 | + int[] histogram = new int[SORTING_HISTOGRAM_SIZE]; |
| 52 | + |
| 53 | + for (int i = 0; i < this.sectionsWithGeometryCount; i++) { |
| 54 | + var index = this.sectionsWithGeometry[i] & 0xFF; // makes sure the byte -> int conversion is unsigned |
| 55 | + var x = Math.abs(LocalSectionIndex.unpackX(index) - cameraX); |
| 56 | + var y = Math.abs(LocalSectionIndex.unpackY(index) - cameraY); |
| 57 | + var z = Math.abs(LocalSectionIndex.unpackZ(index) - cameraZ); |
| 58 | + |
| 59 | + var distance = x + y + z; |
| 60 | + histogram[distance]++; |
| 61 | + sortItems[i] = distance << 8 | index; |
| 62 | + } |
| 63 | + |
| 64 | + // prefix sum to calculate indexes |
| 65 | + for (int i = 1; i < SORTING_HISTOGRAM_SIZE; i++) { |
| 66 | + histogram[i] += histogram[i - 1]; |
| 67 | + } |
| 68 | + |
| 69 | + for (int i = 0; i < this.sectionsWithGeometryCount; i++) { |
| 70 | + var item = sortItems[i]; |
| 71 | + var distance = item >>> 8; |
| 72 | + this.sectionsWithGeometry[--histogram[distance]] = (byte) item; |
| 73 | + } |
| 74 | + } |
| 75 | + |
40 | 76 | public void add(RenderSection render) {
|
41 | 77 | if (this.size >= RenderRegion.REGION_SIZE) {
|
42 | 78 | throw new ArrayIndexOutOfBoundsException("Render list is full");
|
|
0 commit comments