From 36d83b6a8e0b2de18765f0c96ae564e109ecbc4d Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 5 Sep 2023 21:34:24 +0700 Subject: [PATCH] Faster forEachRenderRegion --- .../kstarbound/client/ClientChunk.kt | 24 ++------ .../kstarbound/client/ClientWorld.kt | 43 +++++++++++++ .../ru/dbotthepony/kstarbound/world/Chunk.kt | 60 ++++++++----------- .../kstarbound/world/api/IChunkCell.kt | 19 +++++- 4 files changed, 90 insertions(+), 56 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientChunk.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientChunk.kt index 7008cb27..7eaf8f13 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientChunk.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientChunk.kt @@ -95,32 +95,20 @@ class ClientChunk(world: ClientWorld, pos: ChunkPos) : Chunk() + /** + * all intersecting chunks + */ inline fun forEachRenderRegion(pos: ChunkPos, action: (RenderRegion) -> Unit) { var (ix, iy) = pos.tile ix /= renderRegionWidth @@ -188,6 +193,32 @@ class ClientWorld( } } + /** + * cell and cells around + */ + inline fun forEachRenderRegion(pos: IStruct2i, action: (RenderRegion) -> Unit) { + val dx = pos.component1() % renderRegionWidth + val dy = pos.component2() % renderRegionHeight + + if (dx == 1 || dx == renderRegionWidth - 1 || dy == 1 || dy == renderRegionHeight - 1) { + val seen = LongArraySet(8) + + for ((x, y) in ring) { + val ix = (pos.component1() + x) / renderRegionWidth + val iy = (pos.component2() + y) / renderRegionHeight + val index = ix.toLong() shl 32 or iy.toLong() + + if (seen.add(index)) { + renderRegions[index]?.let(action) + } + } + } else { + val ix = pos.component1() / renderRegionWidth + val iy = pos.component2() / renderRegionHeight + renderRegions[ix.toLong() shl 32 or iy.toLong()]?.let(action) + } + } + override fun chunkFactory(pos: ChunkPos): ClientChunk { return ClientChunk(this, pos) } @@ -364,4 +395,16 @@ class ClientWorld( ent!!.think(delta) } } + + companion object { + val ring = listOf( + Vector2i(0, 0), + Vector2i(1, 0), + Vector2i(1, 1), + Vector2i(1, -1), + Vector2i(-1, 0), + Vector2i(-1, 1), + Vector2i(-1, -1), + ) + } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt index b79361eb..35cd9e30 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt @@ -2,16 +2,10 @@ package ru.dbotthepony.kstarbound.world import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet import ru.dbotthepony.kbox2d.api.BodyDef -import ru.dbotthepony.kbox2d.api.FixtureDef -import ru.dbotthepony.kbox2d.collision.shapes.PolygonShape import ru.dbotthepony.kbox2d.dynamics.B2Fixture import ru.dbotthepony.kstarbound.defs.tile.LiquidDefinition import ru.dbotthepony.kstarbound.defs.tile.MaterialModifier import ru.dbotthepony.kstarbound.defs.tile.TileDefinition -import ru.dbotthepony.kstarbound.world.CHUNK_SIZE_BITS -import ru.dbotthepony.kstarbound.world.CHUNK_SIZE -import ru.dbotthepony.kstarbound.world.CHUNK_SIZE_FF -import ru.dbotthepony.kstarbound.world.CHUNK_SIZEd import ru.dbotthepony.kstarbound.world.api.ICellAccess import ru.dbotthepony.kstarbound.world.api.IChunkCell import ru.dbotthepony.kstarbound.world.api.ILiquidState @@ -20,14 +14,11 @@ import ru.dbotthepony.kstarbound.world.api.OffsetCellAccess import ru.dbotthepony.kstarbound.world.api.TileColor import ru.dbotthepony.kstarbound.world.api.TileView import ru.dbotthepony.kstarbound.world.entities.Entity -import ru.dbotthepony.kstarbound.world.phys.RectTileFlooderDepthFirst -import ru.dbotthepony.kstarbound.world.phys.RectTileFlooderSizeFirst import ru.dbotthepony.kvector.arrays.Object2DArray import ru.dbotthepony.kvector.util2d.AABB import ru.dbotthepony.kvector.vector.Vector2d import java.util.* import kotlin.collections.ArrayList -import kotlin.collections.HashSet /** * Чанк мира @@ -109,9 +100,8 @@ abstract class Chunk, This : Chunk() - protected open fun foregroundChanges() { - changeset++ - cellChangeset++ + protected open fun foregroundChanges(cell: Cell) { + cellChanges(cell) tileChangeset++ collisionChangeset++ @@ -119,14 +109,23 @@ abstract class Chunk, This : Chunk Unit) { world.chunkMap[pos.left]?.let(block) world.chunkMap[pos.right]?.let(block) @@ -153,9 +152,9 @@ abstract class Chunk, This : Chunk, This : Chunk, This : Chunk, This : Chunk, This : Chunk, This : Chunk, This : Chunk, This : Chunk, This : Chunk