mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 02:24:05 +09:00
light parallel failed attempt
This commit is contained in:
@@ -25,7 +25,7 @@ object DefaultConfig {
|
|||||||
jsonObject.addProperty("language", AppLoader.getSysLang())
|
jsonObject.addProperty("language", AppLoader.getSysLang())
|
||||||
jsonObject.addProperty("notificationshowuptime", 4000)
|
jsonObject.addProperty("notificationshowuptime", 4000)
|
||||||
jsonObject.addProperty("multithread", true) // experimental!
|
jsonObject.addProperty("multithread", true) // experimental!
|
||||||
jsonObject.addProperty("multithreadedlight", false) // experimental!
|
jsonObject.addProperty("multithreadedlight", true) // experimental!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.torvald.terrarum.concurrent
|
package net.torvald.terrarum.concurrent
|
||||||
|
|
||||||
import net.torvald.terrarum.Terrarum
|
import net.torvald.terrarum.Terrarum
|
||||||
|
import kotlin.math.absoluteValue
|
||||||
|
|
||||||
typealias RunnableFun = () -> Unit
|
typealias RunnableFun = () -> Unit
|
||||||
/** Int: index of the processing core */
|
/** Int: index of the processing core */
|
||||||
@@ -12,7 +13,7 @@ typealias ThreadableFun = (Int) -> Unit
|
|||||||
object ThreadParallel {
|
object ThreadParallel {
|
||||||
val threadCount = Terrarum.THREADS // modify this to your taste
|
val threadCount = Terrarum.THREADS // modify this to your taste
|
||||||
|
|
||||||
private val pool: Array<Thread?> = Array(threadCount, { null })
|
private val pool: Array<Thread?> = Array(threadCount) { null }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map Runnable object to certain index of the thread pool.
|
* Map Runnable object to certain index of the thread pool.
|
||||||
@@ -189,13 +190,19 @@ object ParallelUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun IntRange.sliceEvenly(slices: Int): List<IntRange> {
|
fun IntProgression.sliceEvenly(slices: Int): List<IntProgression> {
|
||||||
if (this.step != 1) throw UnsupportedOperationException("Sorry, step != 1")
|
if (this.step.absoluteValue != 1) throw UnsupportedOperationException("Sorry, step != +1/-1")
|
||||||
val size = this.last - this.first + 1f
|
val size = (this.last - this.first).absoluteValue + (this.step.toFloat()).absoluteValue
|
||||||
|
|
||||||
return (0 until slices).map {
|
// println(size)
|
||||||
size.div(slices).times(it).roundInt() until
|
|
||||||
size.div(slices).times(it + 1).roundInt()
|
return if (this.first < this.last) (0 until slices).map {
|
||||||
|
this.first + size.div(slices).times(it).roundInt() ..
|
||||||
|
this.first + size.div(slices).times(it + 1).roundInt() - 1
|
||||||
|
}
|
||||||
|
else (0 until slices).map {
|
||||||
|
this.first - size.div(slices).times(it).roundInt() downTo
|
||||||
|
this.first - size.div(slices).times(it + 1).roundInt() + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class NoiseGenerator : ScreenAdapter() {
|
|||||||
|
|
||||||
private val jobsCount: Int
|
private val jobsCount: Int
|
||||||
get() = (IMAGE_SIZE * IMAGE_SIZE) / pixelsInSingleJob
|
get() = (IMAGE_SIZE * IMAGE_SIZE) / pixelsInSingleJob
|
||||||
private val rawPixelsList: List<IntRange>
|
private val rawPixelsList: List<IntProgression>
|
||||||
get() = (0 until IMAGE_SIZE * IMAGE_SIZE).sliceEvenly(jobsCount)
|
get() = (0 until IMAGE_SIZE * IMAGE_SIZE).sliceEvenly(jobsCount)
|
||||||
private fun makeGenFun(seed: Long, index: Int) = { //i: Int ->
|
private fun makeGenFun(seed: Long, index: Int) = { //i: Int ->
|
||||||
val module = ModuleFractal()
|
val module = ModuleFractal()
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import net.torvald.terrarum.blockproperties.Block
|
|||||||
import net.torvald.terrarum.blockproperties.BlockCodex
|
import net.torvald.terrarum.blockproperties.BlockCodex
|
||||||
import net.torvald.terrarum.blockproperties.Fluid
|
import net.torvald.terrarum.blockproperties.Fluid
|
||||||
import net.torvald.terrarum.concurrent.ParallelUtils.sliceEvenly
|
import net.torvald.terrarum.concurrent.ParallelUtils.sliceEvenly
|
||||||
|
import net.torvald.terrarum.concurrent.ThreadParallel
|
||||||
import net.torvald.terrarum.gameactors.ActorWBMovable
|
import net.torvald.terrarum.gameactors.ActorWBMovable
|
||||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
import net.torvald.terrarum.gameactors.ActorWithBody
|
||||||
import net.torvald.terrarum.gameactors.Luminous
|
import net.torvald.terrarum.gameactors.Luminous
|
||||||
@@ -186,6 +187,8 @@ object LightmapRenderer {
|
|||||||
return // quit prematurely
|
return // quit prematurely
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (world.worldIndex == -1) return
|
||||||
|
|
||||||
|
|
||||||
for_x_start = WorldCamera.x / TILE_SIZE // fix for premature lightmap rendering
|
for_x_start = WorldCamera.x / TILE_SIZE // fix for premature lightmap rendering
|
||||||
for_y_start = WorldCamera.y / TILE_SIZE // on topmost/leftmost side
|
for_y_start = WorldCamera.y / TILE_SIZE // on topmost/leftmost side
|
||||||
@@ -251,38 +254,67 @@ object LightmapRenderer {
|
|||||||
AppLoader.measureDebugTime("Renderer.LightTotal") {
|
AppLoader.measureDebugTime("Renderer.LightTotal") {
|
||||||
// Round 2
|
// Round 2
|
||||||
for (y in for_y_end + overscan_open downTo for_y_start) {
|
for (y in for_y_end + overscan_open downTo for_y_start) {
|
||||||
|
// TODO multithread the following for loop duh
|
||||||
for (x in for_x_start - overscan_open..for_x_end) {
|
for (x in for_x_start - overscan_open..for_x_end) {
|
||||||
calculateAndAssign(lightmap, x, y)
|
calculateAndAssign(lightmap, x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Round 3
|
// Round 3
|
||||||
for (y in for_y_end + overscan_open downTo for_y_start) {
|
for (y in for_y_end + overscan_open downTo for_y_start) {
|
||||||
|
// TODO multithread the following for loop duh
|
||||||
for (x in for_x_end + overscan_open downTo for_x_start) {
|
for (x in for_x_end + overscan_open downTo for_x_start) {
|
||||||
calculateAndAssign(lightmap, x, y)
|
calculateAndAssign(lightmap, x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Round 4
|
// Round 4
|
||||||
for (y in for_y_start - overscan_open..for_y_end) {
|
for (y in for_y_start - overscan_open..for_y_end) {
|
||||||
|
// TODO multithread the following for loop duh
|
||||||
for (x in for_x_end + overscan_open downTo for_x_start) {
|
for (x in for_x_end + overscan_open downTo for_x_start) {
|
||||||
calculateAndAssign(lightmap, x, y)
|
calculateAndAssign(lightmap, x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Round 1
|
// Round 1
|
||||||
for (y in for_y_start - overscan_open..for_y_end) {
|
for (y in for_y_start - overscan_open..for_y_end) {
|
||||||
|
// TODO multithread the following for loop duh
|
||||||
for (x in for_x_start - overscan_open..for_x_end) {
|
for (x in for_x_start - overscan_open..for_x_end) {
|
||||||
calculateAndAssign(lightmap, x, y)
|
calculateAndAssign(lightmap, x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Round 2 again
|
|
||||||
/*for (y in for_y_end + overscan_open downTo for_y_start) {
|
|
||||||
for (x in for_x_start - overscan_open..for_x_end) {
|
|
||||||
calculateAndAssign(lightmap, x, y)
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (world.worldIndex != -1) { // to avoid updating on the null world
|
else if (world.worldIndex != -1) { // to avoid updating on the null world
|
||||||
TODO()
|
val roundsY = arrayOf(
|
||||||
|
(for_y_end + overscan_open downTo for_y_start).sliceEvenly(ThreadParallel.threadCount),
|
||||||
|
(for_y_end + overscan_open downTo for_y_start).sliceEvenly(ThreadParallel.threadCount),
|
||||||
|
(for_y_start - overscan_open..for_y_end).sliceEvenly(ThreadParallel.threadCount),
|
||||||
|
(for_y_start - overscan_open..for_y_end).sliceEvenly(ThreadParallel.threadCount)
|
||||||
|
)
|
||||||
|
val roundsX = arrayOf(
|
||||||
|
(for_x_start - overscan_open..for_x_end),
|
||||||
|
(for_x_end + overscan_open downTo for_x_start),
|
||||||
|
(for_x_end + overscan_open downTo for_x_start),
|
||||||
|
(for_x_start - overscan_open..for_x_end)
|
||||||
|
)
|
||||||
|
|
||||||
|
AppLoader.measureDebugTime("Renderer.LightParallelPre") {
|
||||||
|
for (round in 0..roundsY.lastIndex) {
|
||||||
|
roundsY[round].forEachIndexed { index, yRange ->
|
||||||
|
ThreadParallel.map(index, "lightrender-round${round + 1}") {
|
||||||
|
for (y in yRange) {
|
||||||
|
for (x in roundsX[round]) {
|
||||||
|
calculateAndAssign(lightmap, x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AppLoader.measureDebugTime("Renderer.LightParallelRun") {
|
||||||
|
ThreadParallel.startAllWaitForDie()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user