From 1a98292b922c7d98c1a435e754dd2e433a0a376a Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 27 Apr 2020 03:36:34 +0900 Subject: [PATCH] using Coroutines instead of Threadpool but the artefact is still there :( --- .idea/libraries/KotlinJavaRuntime.xml | 3 ++ .idea/libraries/lib.xml | 7 ----- .../terrarum/tests/WorldgenNoiseSandbox.kt | 29 ++++++++++--------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml index 9fbfb0da4..0eb1ce283 100644 --- a/.idea/libraries/KotlinJavaRuntime.xml +++ b/.idea/libraries/KotlinJavaRuntime.xml @@ -4,6 +4,9 @@ + + + diff --git a/.idea/libraries/lib.xml b/.idea/libraries/lib.xml index c4f1a8eef..010e938cc 100644 --- a/.idea/libraries/lib.xml +++ b/.idea/libraries/lib.xml @@ -4,24 +4,17 @@ - - - - - - - \ No newline at end of file diff --git a/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt b/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt index 1f7592d88..d0b77c0d3 100644 --- a/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt +++ b/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt @@ -14,7 +14,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShaderProgram import com.sudoplay.joise.Joise import com.sudoplay.joise.module.* -import net.torvald.UnsafeHelper import net.torvald.UnsafePtr import net.torvald.random.HQRNG import net.torvald.terrarum.concurrent.* @@ -24,10 +23,10 @@ import net.torvald.terrarum.modulebasegame.worldgenerator.BiomegenParams import net.torvald.terrarum.modulebasegame.worldgenerator.TerragenParams import net.torvald.terrarum.modulebasegame.worldgenerator.shake import net.torvald.terrarum.worlddrawer.toRGBA -import java.util.concurrent.Future import kotlin.math.cos import kotlin.math.sin import kotlin.random.Random +import kotlinx.coroutines.* const val WIDTH = 768 const val HEIGHT = 512 @@ -50,7 +49,7 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { private val RNG = HQRNG() private var seed = 10000L - private var generationDone = false + private var initialGenDone = false private var generateKeyLatched = false private var generationTimeInMeasure = false @@ -76,7 +75,7 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { private var generationTime = 0f override fun render() { - if (!generationDone) { + if (!initialGenDone) { joise = getNoiseGenerator(seed) renderNoise() } @@ -96,13 +95,14 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { renderNoise() } + val coroutineExecFinished = (coroutineJobs.fold(true) { acc, it -> acc and it.isCompleted }) // check if generation is done - if (threadExecFinished) { + if (coroutineExecFinished) { generateKeyLatched = false } // finish time measurement - if (threadExecFinished && generationTimeInMeasure) { + if (coroutineExecFinished && generationTimeInMeasure) { generationTimeInMeasure = false val time = System.nanoTime() - generationStartTime generationTime = time / 1000000000f @@ -129,10 +129,6 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { private val sampleOffset = WIDTH / 8.0 - private val threadExecFuture = Array?>(ThreadExecutor.threadCount) { null } - private val threadExecFinished: Boolean - get() = threadExecFuture.fold(true) { acc, future -> acc && (future?.isDone ?: true) } - private val testColSet = arrayOf( Color(0xff0000ff.toInt()), Color(0xffff00ff.toInt()), @@ -165,7 +161,10 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { } } - private val xSlices = (0 until WIDTH).sliceEvenly(ThreadExecutor.threadCount) + //private val xSlices = (0 until WIDTH).sliceEvenly(ThreadExecutor.threadCount) + private val xSlices = (0 until WIDTH).sliceEvenly(WIDTH / 8) + + private lateinit var coroutineJobs: List private fun renderNoise() { generationStartTime = System.nanoTime() @@ -201,11 +200,13 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { } } - runnables.forEachIndexed { index, function -> + /*runnables.forEachIndexed { index, function -> threadExecFuture[index] = ThreadExecutor.submit(function) - } + }*/ - generationDone = true + coroutineJobs = runnables.map { r -> GlobalScope.launch { r() } } + + initialGenDone = true } override fun dispose() {