| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Queues;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 7 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 8 | import it.unimi.dsi.fastutil.longs.LongArrayList;
|
| 9 | import it.unimi.dsi.fastutil.longs.LongCollection;
|
| 10 | import it.unimi.dsi.fastutil.longs.LongIterator;
|
| 11 | import it.unimi.dsi.fastutil.longs.LongList;
|
| 12 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 13 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 14 | import it.unimi.dsi.fastutil.longs.LongSets;
|
| 15 | import java.util.Comparator;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Queue;
|
| 18 | import java.util.concurrent.BlockingQueue;
|
| 19 | import java.util.concurrent.CompletableFuture;
|
| 20 | import java.util.concurrent.Future;
|
| 21 | import java.util.concurrent.LinkedBlockingQueue;
|
| 22 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 23 | import java.util.concurrent.atomic.AtomicReference;
|
| 24 | import java.util.function.Consumer;
|
| 25 | import net.fabricmc.api.EnvType;
|
| 26 | import net.fabricmc.api.Environment;
|
| 27 | import net.minecraft.client.Minecraft;
|
| 28 | import net.minecraft.client.renderer.chunk.CompiledSectionMesh;
|
| 29 | import net.minecraft.client.renderer.chunk.SectionMesh;
|
| 30 | import net.minecraft.client.renderer.chunk.SectionRenderDispatcher;
|
| 31 | import net.minecraft.client.renderer.culling.Frustum;
|
| 32 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 33 | import net.minecraft.client.renderer.state.level.ChunkLoadingRenderState;
|
| 34 | import net.minecraft.core.BlockPos;
|
| 35 | import net.minecraft.core.Direction;
|
| 36 | import net.minecraft.core.SectionPos;
|
| 37 | import net.minecraft.core.Direction.Axis;
|
| 38 | import net.minecraft.server.level.ChunkTrackingView;
|
| 39 | import net.minecraft.util.Mth;
|
| 40 | import net.minecraft.util.Util;
|
| 41 | import net.minecraft.util.VisibleForDebug;
|
| 42 | import net.minecraft.world.level.ChunkPos;
|
| 43 | import net.minecraft.world.phys.Vec3;
|
| 44 | import org.joml.Vector3d;
|
| 45 | import org.jspecify.annotations.Nullable;
|
| 46 | import org.slf4j.Logger;
|
| 47 |
|
| 48 | @Environment(EnvType.CLIENT)
|
| 49 | public class SectionOcclusionGraph {
|
| 50 | private static final int HALF_SECTION_SIZE = 8;
|
| 51 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 52 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 53 | private static final int MINIMUM_ADVANCED_CULLING_DISTANCE = 60;
|
| 54 | private static final int MINIMUM_ADVANCED_CULLING_SECTION_DISTANCE = SectionPos.blockToSectionCoord(60);
|
| 55 | private static final double CEILED_SECTION_DIAGONAL = Math.ceil(Math.sqrt(3.0) * 16.0);
|
| 56 | private boolean needsFullUpdate = true;
|
| 57 | private @Nullable Future<?> fullUpdateTask;
|
| 58 | private @Nullable ViewArea viewArea;
|
| 59 | private final AtomicReference<SectionOcclusionGraph.@Nullable GraphState> currentGraph = new AtomicReference<>();
|
| 60 | private final AtomicBoolean needsFrustumUpdate = new AtomicBoolean(false);
|
| 61 | private final LongOpenHashSet emptySections = new LongOpenHashSet();
|
| 62 | private final LongOpenHashSet loadedChunks = new LongOpenHashSet();
|
| 63 | private volatile @Nullable BlockingQueue<SectionRenderDispatcher.RenderSection> nextSectionsToPropagateFrom;
|
| 64 | private double prevCamX = Double.MIN_VALUE;
|
| 65 | private double prevCamY = Double.MIN_VALUE;
|
| 66 | private double prevCamZ = Double.MIN_VALUE;
|
| 67 | private int prevFov = Integer.MAX_VALUE;
|
| 68 | private boolean lastSmartCull = true;
|
| 69 |
|
| 70 | public void waitAndReset(final @Nullable ViewArea viewArea) {
|
| 71 | if (this.fullUpdateTask != null) {
|
| 72 | try {
|
| 73 | this.fullUpdateTask.get();
|
| 74 | this.fullUpdateTask = null;
|
| 75 | } catch (Exception e) {
|
| 76 | LOGGER.warn("Full update failed", e);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | this.viewArea = viewArea;
|
| 81 | if (viewArea != null) {
|
| 82 | this.currentGraph.set(new SectionOcclusionGraph.GraphState(viewArea));
|
| 83 | this.invalidate();
|
| 84 | } else {
|
| 85 | this.currentGraph.set(null);
|
| 86 | this.emptySections.clear();
|
| 87 | this.loadedChunks.clear();
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | public LongCollection expectedChunks() {
|
| 92 | SectionOcclusionGraph.GraphState graphState = this.currentGraph.get();
|
| 93 | return graphState != null ? graphState.storage.sectionsWaitingForChunkLoads.keySet() : LongSets.EMPTY_SET;
|
| 94 | }
|
| 95 |
|
| 96 | public void invalidate() {
|
| 97 | this.needsFullUpdate = true;
|
| 98 | }
|
| 99 |
|
| 100 | public void invalidateIfNeeded(final CameraRenderState camera, final int fov) {
|
| 101 | Vec3 cameraPos = camera.pos;
|
| 102 | double camX = Math.floor(cameraPos.x / 8.0);
|
| 103 | double camY = Math.floor(cameraPos.y / 8.0);
|
| 104 | double camZ = Math.floor(cameraPos.z / 8.0);
|
| 105 | if (camX != this.prevCamX || camY != this.prevCamY || camZ != this.prevCamZ || this.prevFov != fov || this.lastSmartCull != camera.smartCull) {
|
| 106 | this.invalidate();
|
| 107 | }
|
| 108 |
|
| 109 | this.prevCamX = camX;
|
| 110 | this.prevCamY = camY;
|
| 111 | this.prevCamZ = camZ;
|
| 112 | this.prevFov = fov;
|
| 113 | this.lastSmartCull = camera.smartCull;
|
| 114 | }
|
| 115 |
|
| 116 | public void addSectionsInFrustum(
|
| 117 | final Frustum frustum,
|
| 118 | final List<SectionRenderDispatcher.RenderSection> visibleSections,
|
| 119 | final List<SectionRenderDispatcher.RenderSection> nearbyVisibleSection
|
| 120 | ) {
|
| 121 | Frustum offsetFrustum = offsetFrustum(frustum);
|
| 122 | this.currentGraph.get().storage().sectionTree.visitNodes((node, fullyVisible, depth, isClose) -> {
|
| 123 | SectionRenderDispatcher.RenderSection renderSection = node.getSection();
|
| 124 | if (renderSection != null) {
|
| 125 | visibleSections.add(renderSection);
|
| 126 | if (isClose) {
|
| 127 | nearbyVisibleSection.add(renderSection);
|
| 128 | }
|
| 129 | }
|
| 130 | }, offsetFrustum, 32);
|
| 131 | }
|
| 132 |
|
| 133 | public boolean consumeFrustumUpdate() {
|
| 134 | return this.needsFrustumUpdate.compareAndSet(true, false);
|
| 135 | }
|
| 136 |
|
| 137 | public void schedulePropagationFrom(final SectionRenderDispatcher.RenderSection section) {
|
| 138 | BlockingQueue<SectionRenderDispatcher.RenderSection> nextSectionsToPropagateFrom = this.nextSectionsToPropagateFrom;
|
| 139 | if (nextSectionsToPropagateFrom != null) {
|
| 140 | nextSectionsToPropagateFrom.add(section);
|
| 141 | }
|
| 142 |
|
| 143 | BlockingQueue<SectionRenderDispatcher.RenderSection> sectionsToPropagateFrom = this.currentGraph.get().sectionsToPropagateFrom;
|
| 144 | if (sectionsToPropagateFrom != nextSectionsToPropagateFrom) {
|
| 145 | sectionsToPropagateFrom.add(section);
|
| 146 | }
|
| 147 |
|
| 148 | Minecraft.getInstance().levelRenderer.removeTransientBlocksInSection(section.getSectionNode(), section.getSectionMesh().getCompileTaskStartTime());
|
| 149 | }
|
| 150 |
|
| 151 | public void update(final CameraRenderState camera, final int fov, final ChunkLoadingRenderState chunkLoadingRenderState) {
|
| 152 | this.updateLoadedChunks(chunkLoadingRenderState.addedLoadedChunks, chunkLoadingRenderState.removedLoadedChunks);
|
| 153 | this.updateEmptySections(chunkLoadingRenderState.addedEmptySections, chunkLoadingRenderState.removedEmptySections);
|
| 154 | if (!camera.isFrustumCaptured) {
|
| 155 | this.invalidateIfNeeded(camera, fov);
|
| 156 | if (this.needsFullUpdate && (this.fullUpdateTask == null || this.fullUpdateTask.isDone())) {
|
| 157 | this.scheduleFullUpdate(camera);
|
| 158 | }
|
| 159 |
|
| 160 | this.runPartialUpdate(camera, chunkLoadingRenderState.loadedExpectedChunks);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | private void scheduleFullUpdate(final CameraRenderState camera) {
|
| 165 | this.needsFullUpdate = false;
|
| 166 | LongOpenHashSet clonedEmptySections = this.emptySections.clone();
|
| 167 | LongOpenHashSet clonedLoadedChunks = this.loadedChunks.clone();
|
| 168 | this.fullUpdateTask = CompletableFuture.runAsync(() -> {
|
| 169 | SectionOcclusionGraph.GraphState newState = new SectionOcclusionGraph.GraphState(this.viewArea);
|
| 170 | this.nextSectionsToPropagateFrom = newState.sectionsToPropagateFrom;
|
| 171 | Queue<SectionOcclusionGraph.Node> queue = Queues.newArrayDeque();
|
| 172 | this.initializeQueueForFullUpdate(camera.blockPos, queue);
|
| 173 | queue.forEach(node -> newState.storage.sectionToNodeMap.put(node.section, node));
|
| 174 | this.runUpdates(newState.storage, camera.pos, queue, camera.smartCull, node -> {}, clonedEmptySections, clonedLoadedChunks);
|
| 175 | this.currentGraph.set(newState);
|
| 176 | this.nextSectionsToPropagateFrom = null;
|
| 177 | this.needsFrustumUpdate.set(true);
|
| 178 | }, Util.backgroundExecutor());
|
| 179 | }
|
| 180 |
|
| 181 | private void runPartialUpdate(final CameraRenderState camera, final LongSet loadedExpectedChunks) {
|
| 182 | SectionOcclusionGraph.GraphState state = this.currentGraph.get();
|
| 183 | loadedExpectedChunks.forEach(chunkNode -> {
|
| 184 | LongList waitingSections = state.storage.sectionsWaitingForChunkLoads.remove(chunkNode);
|
| 185 | if (waitingSections != null) {
|
| 186 | waitingSections.forEach(sectionNode -> {
|
| 187 | SectionRenderDispatcher.RenderSection section = this.viewArea.getRenderSection(sectionNode);
|
| 188 | if (section != null) {
|
| 189 | this.schedulePropagationFrom(section);
|
| 190 | }
|
| 191 | });
|
| 192 | }
|
| 193 | });
|
| 194 | if (!state.sectionsToPropagateFrom.isEmpty()) {
|
| 195 | Queue<SectionOcclusionGraph.Node> queue = Queues.newArrayDeque();
|
| 196 |
|
| 197 | while (!state.sectionsToPropagateFrom.isEmpty()) {
|
| 198 | SectionRenderDispatcher.RenderSection renderSection = state.sectionsToPropagateFrom.poll();
|
| 199 | SectionOcclusionGraph.Node node = state.storage.sectionToNodeMap.get(renderSection);
|
| 200 | if (node != null && node.section == renderSection) {
|
| 201 | queue.add(node);
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | Frustum offsetFrustum = offsetFrustum(camera.cullFrustum);
|
| 206 | Consumer<SectionRenderDispatcher.RenderSection> onSectionAdded = section -> {
|
| 207 | if (offsetFrustum.isVisible(section.getBoundingBox())) {
|
| 208 | this.needsFrustumUpdate.set(true);
|
| 209 | }
|
| 210 | };
|
| 211 | this.runUpdates(state.storage, camera.pos, queue, camera.smartCull, onSectionAdded, this.emptySections, this.loadedChunks);
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | private void initializeQueueForFullUpdate(final BlockPos cameraPosition, final Queue<SectionOcclusionGraph.Node> queue) {
|
| 216 | long cameraSectionNode = SectionPos.asLong(cameraPosition);
|
| 217 | int cameraSectionY = SectionPos.y(cameraSectionNode);
|
| 218 | SectionRenderDispatcher.RenderSection cameraSection = this.viewArea.getRenderSection(cameraSectionNode);
|
| 219 | if (cameraSection == null) {
|
| 220 | boolean isBelowTheWorld = cameraSectionY < this.viewArea.minSectionY();
|
| 221 | int sectionY = isBelowTheWorld ? this.viewArea.minSectionY() : this.viewArea.maxSectionY();
|
| 222 | int viewDistance = this.viewArea.getViewDistance();
|
| 223 | List<SectionOcclusionGraph.Node> toAdd = Lists.newArrayList();
|
| 224 | int cameraSectionX = SectionPos.x(cameraSectionNode);
|
| 225 | int cameraSectionZ = SectionPos.z(cameraSectionNode);
|
| 226 |
|
| 227 | for (int sectionX = -viewDistance; sectionX <= viewDistance; sectionX++) {
|
| 228 | for (int sectionZ = -viewDistance; sectionZ <= viewDistance; sectionZ++) {
|
| 229 | SectionRenderDispatcher.RenderSection renderSectionAt = this.viewArea
|
| 230 | .getRenderSection(SectionPos.asLong(sectionX + cameraSectionX, sectionY, sectionZ + cameraSectionZ));
|
| 231 | if (renderSectionAt != null && this.isInViewDistance(cameraSectionNode, renderSectionAt.getSectionNode())) {
|
| 232 | Direction sourceDirection = isBelowTheWorld ? Direction.UP : Direction.DOWN;
|
| 233 | SectionOcclusionGraph.Node node = new SectionOcclusionGraph.Node(renderSectionAt, sourceDirection, 0);
|
| 234 | node.setDirections(node.directions, sourceDirection);
|
| 235 | if (sectionX > 0) {
|
| 236 | node.setDirections(node.directions, Direction.EAST);
|
| 237 | } else if (sectionX < 0) {
|
| 238 | node.setDirections(node.directions, Direction.WEST);
|
| 239 | }
|
| 240 |
|
| 241 | if (sectionZ > 0) {
|
| 242 | node.setDirections(node.directions, Direction.SOUTH);
|
| 243 | } else if (sectionZ < 0) {
|
| 244 | node.setDirections(node.directions, Direction.NORTH);
|
| 245 | }
|
| 246 |
|
| 247 | toAdd.add(node);
|
| 248 | }
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | toAdd.sort(Comparator.comparingDouble(c -> cameraPosition.distSqr(SectionPos.of(c.section.getSectionNode()).center())));
|
| 253 | queue.addAll(toAdd);
|
| 254 | } else {
|
| 255 | queue.add(new SectionOcclusionGraph.Node(cameraSection, null, 0));
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | private void runUpdates(
|
| 260 | final SectionOcclusionGraph.GraphStorage storage,
|
| 261 | final Vec3 cameraPos,
|
| 262 | final Queue<SectionOcclusionGraph.Node> queue,
|
| 263 | final boolean smartCull,
|
| 264 | final Consumer<SectionRenderDispatcher.RenderSection> onSectionAdded,
|
| 265 | final LongOpenHashSet emptySections,
|
| 266 | final LongOpenHashSet loadedChunks
|
| 267 | ) {
|
| 268 | SectionPos cameraSectionPos = SectionPos.of(cameraPos);
|
| 269 | long cameraSectionNode = cameraSectionPos.asLong();
|
| 270 | BlockPos cameraSectionCenter = cameraSectionPos.center();
|
| 271 |
|
| 272 | while (!queue.isEmpty()) {
|
| 273 | SectionOcclusionGraph.Node node = queue.poll();
|
| 274 | SectionRenderDispatcher.RenderSection currentSection = node.section;
|
| 275 | long sectionNode = currentSection.getSectionNode();
|
| 276 | long chunkNode = ChunkPos.fromSectionNode(sectionNode);
|
| 277 | if (!loadedChunks.contains(chunkNode)) {
|
| 278 | storage.sectionsWaitingForChunkLoads.computeIfAbsent(chunkNode, var0 -> new LongArrayList()).add(sectionNode);
|
| 279 | } else {
|
| 280 | if (!emptySections.contains(node.section.getSectionNode())) {
|
| 281 | if (storage.sectionTree.add(node.section)) {
|
| 282 | onSectionAdded.accept(node.section);
|
| 283 | }
|
| 284 | } else if (node.section.sectionMesh.compareAndSet(CompiledSectionMesh.UNCOMPILED, CompiledSectionMesh.EMPTY)) {
|
| 285 | node.section.updateUploadTime();
|
| 286 | }
|
| 287 |
|
| 288 | boolean distantFromCamera = Math.abs(SectionPos.x(sectionNode) - cameraSectionPos.x()) > MINIMUM_ADVANCED_CULLING_SECTION_DISTANCE
|
| 289 | || Math.abs(SectionPos.y(sectionNode) - cameraSectionPos.y()) > MINIMUM_ADVANCED_CULLING_SECTION_DISTANCE
|
| 290 | || Math.abs(SectionPos.z(sectionNode) - cameraSectionPos.z()) > MINIMUM_ADVANCED_CULLING_SECTION_DISTANCE;
|
| 291 |
|
| 292 | for (Direction direction : DIRECTIONS) {
|
| 293 | SectionRenderDispatcher.RenderSection renderSectionAt = this.getRelativeFrom(cameraSectionNode, currentSection, direction);
|
| 294 | if (renderSectionAt != null && (!smartCull || !node.hasDirection(direction.getOpposite()))) {
|
| 295 | if (smartCull && node.hasSourceDirections()) {
|
| 296 | SectionMesh sectionMesh = currentSection.getSectionMesh();
|
| 297 | boolean visible = false;
|
| 298 |
|
| 299 | for (int i = 0; i < DIRECTIONS.length; i++) {
|
| 300 | if (node.hasSourceDirection(i) && sectionMesh.facesCanSeeEachother(DIRECTIONS[i].getOpposite(), direction)) {
|
| 301 | visible = true;
|
| 302 | break;
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | if (!visible) {
|
| 307 | continue;
|
| 308 | }
|
| 309 | }
|
| 310 |
|
| 311 | if (smartCull && distantFromCamera) {
|
| 312 | int renderSectionOriginX = SectionPos.sectionToBlockCoord(SectionPos.x(sectionNode));
|
| 313 | int renderSectionOriginY = SectionPos.sectionToBlockCoord(SectionPos.y(sectionNode));
|
| 314 | int renderSectionOriginZ = SectionPos.sectionToBlockCoord(SectionPos.z(sectionNode));
|
| 315 | boolean maxX = direction.getAxis() == Axis.X ? cameraSectionCenter.getX() > renderSectionOriginX : cameraSectionCenter.getX() < renderSectionOriginX;
|
| 316 | boolean maxY = direction.getAxis() == Axis.Y ? cameraSectionCenter.getY() > renderSectionOriginY : cameraSectionCenter.getY() < renderSectionOriginY;
|
| 317 | boolean maxZ = direction.getAxis() == Axis.Z ? cameraSectionCenter.getZ() > renderSectionOriginZ : cameraSectionCenter.getZ() < renderSectionOriginZ;
|
| 318 | Vector3d checkPos = new Vector3d(renderSectionOriginX + (maxX ? 16 : 0), renderSectionOriginY + (maxY ? 16 : 0), renderSectionOriginZ + (maxZ ? 16 : 0));
|
| 319 | Vector3d step = new Vector3d(cameraPos.x, cameraPos.y, cameraPos.z).sub(checkPos).normalize().mul(CEILED_SECTION_DIAGONAL);
|
| 320 | boolean visible = true;
|
| 321 |
|
| 322 | while (checkPos.distanceSquared(cameraPos.x, cameraPos.y, cameraPos.z) > 3600.0) {
|
| 323 | checkPos.add(step);
|
| 324 | if (checkPos.y > this.viewArea.maxY() || checkPos.y < this.viewArea.minY()) {
|
| 325 | break;
|
| 326 | }
|
| 327 |
|
| 328 | SectionRenderDispatcher.RenderSection checkSection = this.viewArea.getRenderSectionAt(BlockPos.containing(checkPos.x, checkPos.y, checkPos.z));
|
| 329 | if (checkSection == null || storage.sectionToNodeMap.get(checkSection) == null) {
|
| 330 | visible = false;
|
| 331 | break;
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | if (!visible) {
|
| 336 | continue;
|
| 337 | }
|
| 338 | }
|
| 339 |
|
| 340 | SectionOcclusionGraph.Node existingNode = storage.sectionToNodeMap.get(renderSectionAt);
|
| 341 | if (existingNode != null) {
|
| 342 | existingNode.addSourceDirection(direction);
|
| 343 | } else {
|
| 344 | SectionOcclusionGraph.Node newNode = new SectionOcclusionGraph.Node(renderSectionAt, direction, node.step + 1);
|
| 345 | newNode.setDirections(node.directions, direction);
|
| 346 | queue.add(newNode);
|
| 347 | storage.sectionToNodeMap.put(renderSectionAt, newNode);
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 |
|
| 355 | private static Frustum offsetFrustum(final Frustum frustum) {
|
| 356 | return new Frustum(frustum).offsetToFullyIncludeCameraCube(8);
|
| 357 | }
|
| 358 |
|
| 359 | private boolean isInViewDistance(final long cameraSectionNode, final long sectionNode) {
|
| 360 | return ChunkTrackingView.isInViewDistance(
|
| 361 | SectionPos.x(cameraSectionNode), SectionPos.z(cameraSectionNode), this.viewArea.getViewDistance(), SectionPos.x(sectionNode), SectionPos.z(sectionNode)
|
| 362 | );
|
| 363 | }
|
| 364 |
|
| 365 | private SectionRenderDispatcher.@Nullable RenderSection getRelativeFrom(
|
| 366 | final long cameraSectionNode, final SectionRenderDispatcher.RenderSection renderSection, final Direction direction
|
| 367 | ) {
|
| 368 | long relative = renderSection.getNeighborSectionNode(direction);
|
| 369 | if (!this.isInViewDistance(cameraSectionNode, relative)) {
|
| 370 | return null;
|
| 371 | } else {
|
| 372 | return Mth.abs(SectionPos.y(cameraSectionNode) - SectionPos.y(relative)) > this.viewArea.getViewDistance() ? null : this.viewArea.getRenderSection(relative);
|
| 373 | }
|
| 374 | }
|
| 375 |
|
| 376 | @VisibleForDebug
|
| 377 | public SectionOcclusionGraph.@Nullable Node getNode(final SectionRenderDispatcher.RenderSection section) {
|
| 378 | return this.currentGraph.get().storage.sectionToNodeMap.get(section);
|
| 379 | }
|
| 380 |
|
| 381 | public void updateEmptySections(final LongOpenHashSet added, final LongOpenHashSet removed) {
|
| 382 | this.emptySections.addAll(added);
|
| 383 | LongIterator iter = removed.longIterator();
|
| 384 |
|
| 385 | while (iter.hasNext()) {
|
| 386 | long sectionNode = iter.nextLong();
|
| 387 | if (this.emptySections.remove(sectionNode)) {
|
| 388 | SectionRenderDispatcher.RenderSection section = this.viewArea.getRenderSection(sectionNode);
|
| 389 | if (section != null) {
|
| 390 | this.schedulePropagationFrom(section);
|
| 391 | }
|
| 392 | }
|
| 393 | }
|
| 394 | }
|
| 395 |
|
| 396 | public void updateLoadedChunks(final LongOpenHashSet added, final LongOpenHashSet removed) {
|
| 397 | this.loadedChunks.addAll(added);
|
| 398 | this.loadedChunks.removeAll(removed);
|
| 399 | }
|
| 400 |
|
| 401 | public Octree getOctree() {
|
| 402 | return this.currentGraph.get().storage.sectionTree;
|
| 403 | }
|
| 404 |
|
| 405 | @Environment(EnvType.CLIENT)
|
| 406 | private record GraphState(SectionOcclusionGraph.GraphStorage storage, BlockingQueue<SectionRenderDispatcher.RenderSection> sectionsToPropagateFrom) {
|
| 407 | private GraphState(final ViewArea viewArea) {
|
| 408 | this(new SectionOcclusionGraph.GraphStorage(viewArea), new LinkedBlockingQueue<>());
|
| 409 | }
|
| 410 | }
|
| 411 |
|
| 412 | @Environment(EnvType.CLIENT)
|
| 413 | private static class GraphStorage {
|
| 414 | public final SectionOcclusionGraph.SectionToNodeMap sectionToNodeMap;
|
| 415 | public final Octree sectionTree;
|
| 416 | public final Long2ObjectMap<LongList> sectionsWaitingForChunkLoads;
|
| 417 |
|
| 418 | public GraphStorage(final ViewArea viewArea) {
|
| 419 | this.sectionToNodeMap = new SectionOcclusionGraph.SectionToNodeMap(viewArea.size());
|
| 420 | this.sectionTree = new Octree(viewArea.getCameraSectionPos(), viewArea.getViewDistance(), viewArea.sectionCount(), viewArea.minY());
|
| 421 | this.sectionsWaitingForChunkLoads = new Long2ObjectOpenHashMap<>();
|
| 422 | }
|
| 423 | }
|
| 424 |
|
| 425 | @Environment(EnvType.CLIENT)
|
| 426 | @VisibleForDebug
|
| 427 | public static class Node {
|
| 428 | @VisibleForDebug
|
| 429 | protected final SectionRenderDispatcher.RenderSection section;
|
| 430 | private byte sourceDirections;
|
| 431 | private byte directions;
|
| 432 | @VisibleForDebug
|
| 433 | public final int step;
|
| 434 |
|
| 435 | private Node(final SectionRenderDispatcher.RenderSection section, final @Nullable Direction sourceDirection, final int step) {
|
| 436 | this.section = section;
|
| 437 | if (sourceDirection != null) {
|
| 438 | this.addSourceDirection(sourceDirection);
|
| 439 | }
|
| 440 |
|
| 441 | this.step = step;
|
| 442 | }
|
| 443 |
|
| 444 | private void setDirections(final byte oldDirections, final Direction direction) {
|
| 445 | this.directions = (byte)(this.directions | oldDirections | 1 << direction.ordinal());
|
| 446 | }
|
| 447 |
|
| 448 | private boolean hasDirection(final Direction direction) {
|
| 449 | return (this.directions & 1 << direction.ordinal()) > 0;
|
| 450 | }
|
| 451 |
|
| 452 | private void addSourceDirection(final Direction direction) {
|
| 453 | this.sourceDirections = (byte)(this.sourceDirections | this.sourceDirections | 1 << direction.ordinal());
|
| 454 | }
|
| 455 |
|
| 456 | @VisibleForDebug
|
| 457 | public boolean hasSourceDirection(final int directionOrdinal) {
|
| 458 | return (this.sourceDirections & 1 << directionOrdinal) > 0;
|
| 459 | }
|
| 460 |
|
| 461 | private boolean hasSourceDirections() {
|
| 462 | return this.sourceDirections != 0;
|
| 463 | }
|
| 464 |
|
| 465 | @Override
|
| 466 | public int hashCode() {
|
| 467 | return Long.hashCode(this.section.getSectionNode());
|
| 468 | }
|
| 469 |
|
| 470 | @Override
|
| 471 | public boolean equals(final Object obj) {
|
| 472 | return obj instanceof SectionOcclusionGraph.Node other ? this.section.getSectionNode() == other.section.getSectionNode() : false;
|
| 473 | }
|
| 474 | }
|
| 475 |
|
| 476 | @Environment(EnvType.CLIENT)
|
| 477 | private static class SectionToNodeMap {
|
| 478 | private final SectionOcclusionGraph.Node[] nodes;
|
| 479 |
|
| 480 | private SectionToNodeMap(final int sectionCount) {
|
| 481 | this.nodes = new SectionOcclusionGraph.Node[sectionCount];
|
| 482 | }
|
| 483 |
|
| 484 | public void put(final SectionRenderDispatcher.RenderSection renderSection, final SectionOcclusionGraph.Node node) {
|
| 485 | this.nodes[renderSection.index] = node;
|
| 486 | }
|
| 487 |
|
| 488 | public SectionOcclusionGraph.@Nullable Node get(final SectionRenderDispatcher.RenderSection renderSection) {
|
| 489 | int index = renderSection.index;
|
| 490 | return index >= 0 && index < this.nodes.length ? this.nodes[index] : null;
|
| 491 | }
|
| 492 | }
|
| 493 | }
|
| 494 |
|