Skip to content

Commit

Permalink
load frames data asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed Nov 18, 2023
1 parent ed07086 commit 41f7353
Showing 1 changed file with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package solve.rendering.engine.rendering.renderers

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.joml.Matrix4f
import org.joml.Vector2i
import solve.constants.ShadersFrameFragmentPath
Expand All @@ -11,6 +14,7 @@ import solve.rendering.engine.rendering.batch.PrimitiveType
import solve.rendering.engine.rendering.batch.RenderBatch
import solve.rendering.engine.rendering.texture.ArrayTexture
import solve.rendering.engine.rendering.texture.Texture2D
import solve.rendering.engine.rendering.texture.Texture2DData
import solve.rendering.engine.rendering.texture.TextureChannelsType
import solve.rendering.engine.shader.ShaderAttributeType
import solve.rendering.engine.shader.ShaderProgram
Expand All @@ -25,6 +29,8 @@ import kotlin.math.abs
class FramesRenderer(
window: Window
) : Renderer(window) {
private data class LoadedBufferFrameData(val textureData: Texture2DData, val bufferIndex: Int)

override val maxBatchSize = 1000
private var modelsCommonMatrix = Matrix4f().identity()
private var gridWidth = DefaultGridWidth
Expand All @@ -41,6 +47,9 @@ class FramesRenderer(

private var cameraLastGridCellPosition = Vector2i(0)

private val bufferFramesToUpload = mutableListOf<LoadedBufferFrameData>()
private val framesLoadingCoroutineScope = CoroutineScope(Dispatchers.Default)

fun changeModelsCommonMatrix(newMatrix: Matrix4f) {
modelsCommonMatrix = newMatrix
}
Expand Down Expand Up @@ -84,16 +93,28 @@ class FramesRenderer(
}
}


override fun beforeRender() {
uploadLoadedFramesToBuffers()
updateBuffersTextures()
}

private fun uploadLoadedFramesToBuffers() {
bufferFramesToUpload.toList().forEach { frame ->
bufferFramesArrayTexture.uploadTexture(frame.textureData, frame.bufferIndex)
bufferFramesToUpload.remove(frame)
Texture2D.freeData(frame.textureData)
}
}

private fun updateBuffersTextures() {
val cameraGridCellPosition = getCameraGridCellPosition()
if (cameraGridCellPosition != cameraLastGridCellPosition) {
updateBuffersTextures(cameraGridCellPosition)
loadNewTexturesToBuffers(cameraGridCellPosition)
}
cameraLastGridCellPosition = cameraGridCellPosition
}

private fun updateBuffersTextures(cameraGridCellPosition: Vector2i) {
private fun loadNewTexturesToBuffers(cameraGridCellPosition: Vector2i) {
val cameraPosition = Vector2i(cameraGridCellPosition)
cameraPosition.x.coerceIn(0 until gridWidth)
cameraPosition.y.coerceIn(0 until gridHeight)
Expand Down Expand Up @@ -188,15 +209,15 @@ class FramesRenderer(
}

private fun uploadFrameToBuffersArray(frame: ProjectFrame, index: Int) {
val textureData = Texture2D.loadData(frame.imagePath.toString())
if (textureData == null) {
println("The read texture is null!")
return
}

bufferFramesArrayTexture.uploadTexture(textureData, index)
framesLoadingCoroutineScope.launch {
val textureData = Texture2D.loadData(frame.imagePath.toString())
if (textureData == null) {
println("The read texture is null!")
return@launch
}

Texture2D.freeData(textureData)
bufferFramesToUpload.add(LoadedBufferFrameData(textureData, index))
}
}

companion object {
Expand Down

0 comments on commit 41f7353

Please sign in to comment.