resort some loops to y->z->y

This is because the values are indexed in that way and cpu caching improves performance
This commit is contained in:
Moritz Zwerger 2023-07-29 17:14:45 +02:00
parent a7b10aaa49
commit a278b6813b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 13 additions and 10 deletions

View File

@ -33,8 +33,8 @@ abstract class ChunkHeightmap(protected val chunk: Chunk) : Heightmap {
chunk.lock.lock()
val maxY = (chunk.maxSection + 1) * ProtocolDefinition.SECTION_HEIGHT_Y
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) {
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
trace(x, maxY, z, false)
}
}

View File

@ -127,8 +127,8 @@ class ChunkSkyLight(val light: ChunkLight) {
private fun floodFill() {
val neighbours = this.chunk.neighbours.get() ?: return
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) {
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
floodFill(neighbours, x, z)
}
}

View File

@ -274,8 +274,8 @@ class SectionLight(
}
private fun propagateX(baseY: Int, neighbours: Array<ChunkSection?>) {
for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) {
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) {
val totalY = baseY + y
neighbours[Directions.O_WEST]?.light?.get(ProtocolDefinition.SECTION_MAX_Z, y, z)?.toInt()?.let { light ->
(light and BLOCK_LIGHT_MASK).let { if (it > 1) traceBlockIncrease(0, y, z, it - 1, Directions.EAST) }

View File

@ -84,14 +84,17 @@ class SolidCullSectionPreparer(
val floatOffset = FloatArray(3)
for (y in blocks.minPosition.y..blocks.maxPosition.y) {
val min = blocks.minPosition.array
val max = blocks.maxPosition.array
for (y in min[1]..max[1]) {
position.y = offsetY + y
floatOffset[1] = (position.y - cameraOffset.y).toFloat()
val fastBedrock = y == 0 && isLowestSection && fastBedrock
for (x in blocks.minPosition.x..blocks.maxPosition.x) {
position.x = offsetX + x
floatOffset[0] = (position.x - cameraOffset.x).toFloat()
for (z in blocks.minPosition.z..blocks.maxPosition.z) {
for (z in min[2]..max[2]) {
position.z = offsetZ + z
floatOffset[2] = (position.z - cameraOffset.z).toFloat()
for (x in min[0]..max[0]) {
val baseIndex = (z shl 4) or x
val index = (y shl 8) or baseIndex
blockState = blocks[index] ?: continue
@ -99,8 +102,8 @@ class SolidCullSectionPreparer(
continue
}
light[SELF_LIGHT_INDEX] = sectionLight[index]
position.z = offsetZ + z
floatOffset[2] = (position.z - cameraOffset.z).toFloat()
position.x = offsetX + x
floatOffset[0] = (position.x - cameraOffset.x).toFloat()
val maxHeight = chunk.light.heightmap[baseIndex]
if (position.y >= maxHeight) {