Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not loading frames bugs #216

Merged
merged 5 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import solve.scene.controller.SceneController
import solve.scene.model.VisualizationFrame
import solve.utils.ceilToInt
import java.util.Date
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

class FramesRenderer(
Expand Down Expand Up @@ -57,9 +59,9 @@ class FramesRenderer(
private val framesRatio: Float
get() = framesWidth.toFloat() / framesHeight.toFloat()

private var cameraLastGridCellPosition = getScreenCenterGridCellPosition()
private var cameraLastGridCellPosition = getScreenTopLeftGridCellPosition()

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

private var needToReinitializeBuffers = false
Expand All @@ -78,6 +80,7 @@ class FramesRenderer(
}

this.gridWidth = min(gridWidth, selectedFrames.count())
haveNewFramesSelection = true
}

fun setNewSceneFrames(frames: List<VisualizationFrame>) {
Expand Down Expand Up @@ -113,7 +116,7 @@ class FramesRenderer(
shaderProgram.uploadVector2i(BuffersSizeUniformName, buffersSize)
shaderProgram.uploadInt(TexturesArrayUniformName, 0)
shaderProgram.uploadFloat(TexturesRatioUniformName, framesRatio)
shaderProgram.uploadVector2f(CameraPositionUniformName, getScreenCenterGridCellPosition().toFloatVector())
shaderProgram.uploadVector2f(CameraPositionUniformName, getScreenTopLeftGridCellPosition().toFloatVector())
}

override fun createNewBatch(zIndex: Int) =
Expand All @@ -136,8 +139,8 @@ class FramesRenderer(
}

if (haveNewFramesSelection) {
uploadAllFramesToBuffer()
bufferFramesToUpload.clear()
uploadAllFramesToBuffer()
haveNewFramesSelection = false
enableVirtualization()
}
Expand All @@ -155,7 +158,7 @@ class FramesRenderer(
}

private fun enableVirtualization() {
cameraLastGridCellPosition = getScreenCenterGridCellPosition()
cameraLastGridCellPosition = getScreenTopLeftGridCellPosition()
isVirtualizationEnabled = true
}

Expand All @@ -166,15 +169,19 @@ class FramesRenderer(
}

private fun uploadLoadedFramesToBuffers() {
bufferFramesToUpload.toList().forEach { frame ->
val uploadedFramesIndices = mutableSetOf<Int>()
bufferFramesToUpload.sortedBy { it.time }.forEach { frame ->
bufferFramesToUpload.remove(frame)
bufferFramesArrayTexture?.uploadTexture(frame.textureData, frame.bufferIndex)
if (!uploadedFramesIndices.contains(frame.bufferIndex)) {
bufferFramesArrayTexture?.uploadTexture(frame.textureData, frame.bufferIndex)
uploadedFramesIndices.add(frame.bufferIndex)
}
Texture2D.freeData(frame.textureData)
}
}

private fun updateBuffersTextures() {
val cameraGridCellPosition = getScreenCenterGridCellPosition()
val cameraGridCellPosition = getScreenTopLeftGridCellPosition()
if (cameraGridCellPosition != cameraLastGridCellPosition) {
loadNewTexturesToBuffers(cameraGridCellPosition)
}
Expand All @@ -195,30 +202,26 @@ class FramesRenderer(
val rectHeight: Int
if (gridCellPositionDelta.x != 0) {
rectWidth = abs(gridCellPositionDelta.x)
rectHeight = buffersSize.y
rectHeight = min(buffersSize.y, gridHeight)
} else {
rectHeight = abs(gridCellPositionDelta.y)
rectWidth = buffersSize.x
rectWidth = min(buffersSize.x, gridWidth)
}
val newFramesRect = IntRect(
if (gridCellPositionDelta.x > 0) {
cameraPosition.x + buffersSize.x - gridCellPositionDelta.x
} else {
cameraPosition.x
max(0, cameraPosition.x)
},
if (gridCellPositionDelta.y > 0) {
cameraPosition.y + buffersSize.y - gridCellPositionDelta.y
} else {
cameraPosition.y
max(0, cameraPosition.y)
},
rectWidth,
rectHeight
)
if (newFramesRect.x0 > 0 && newFramesRect.x1 < gridWidth &&
newFramesRect.y0 > 0 && newFramesRect.y1 < gridHeight
) {
loadRectFramesToBuffers(newFramesRect)
}
loadRectFramesToBuffers(newFramesRect)
}

private fun getFramesAtRect(rect: IntRect): List<List<VisualizationFrame>> {
Expand Down Expand Up @@ -246,17 +249,23 @@ class FramesRenderer(
}

val buffersOffset = Vector2i(framesRect.x0 % buffersSize.x, framesRect.y0 % buffersSize.y)
val uploadedBuffersIndices = mutableSetOf<Int>()

for (y in 0 until rectFrames.count()) {
for (x in 0 until rectFrames[y].count()) {
val textureBuffersIndex =
((buffersOffset.y + y) % buffersSize.y) * buffersSize.x + (buffersOffset.x + x) % buffersSize.x
if (uploadedBuffersIndices.contains(textureBuffersIndex)) {
continue
}

uploadFrameToBuffersArray(rectFrames[y][x], textureBuffersIndex)
uploadedBuffersIndices.add(textureBuffersIndex)
}
}
}

private fun getScreenCenterGridCellPosition(): Vector2i {
private fun getScreenTopLeftGridCellPosition(): Vector2i {
val cameraGridCellPosition = Vector2f(window.camera.position.x / framesRatio, window.camera.position.y)
return (cameraGridCellPosition - Vector2f(buffersSize) / 2f).toIntVector()
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/solve/scene/SceneFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import tornadofx.find
* Transforms common application data models into scene data and applies it.
*/
object SceneFacade {
val lastVisualizationKeepSettingsProperty = SimpleObjectProperty(false)
private var lastVisualizationKeepSettings: Boolean
val lastVisualizationKeepSettingsProperty = SimpleObjectProperty(true)
var lastVisualizationKeepSettings: Boolean
get() = lastVisualizationKeepSettingsProperty.value
private set(value) {
lastVisualizationKeepSettingsProperty.value = value
Expand Down
18 changes: 9 additions & 9 deletions src/main/kotlin/solve/scene/view/SceneView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ class SceneView : View() {

override val root = canvas.canvas

private val projectChangedEventHandler = InvalidationListener {
val framesSize = controller.scene.frameSize
val framesSizeVector = Vector2i(framesSize.width.toInt(), framesSize.height.toInt())
canvas.setNewSceneFrames(controller.scene.frames, framesSizeVector)
}
private val framesChangedEventHandler = InvalidationListener {
canvas.setFramesSelection(controller.scene.frames)
private val sceneChangedEventHandler = InvalidationListener {
if (SceneFacade.lastVisualizationKeepSettings) {
canvas.setFramesSelection(controller.scene.frames)
} else {
val framesSize = controller.scene.frameSize
val framesSizeVector = Vector2i(framesSize.width.toInt(), framesSize.height.toInt())
canvas.setNewSceneFrames(controller.scene.frames, framesSizeVector)
}
}

init {
Expand All @@ -51,8 +52,7 @@ class SceneView : View() {
}

private fun addSceneFramesBindings() {
SceneFacade.lastVisualizationKeepSettingsProperty.addListener(projectChangedEventHandler)
controller.sceneProperty.addListener(framesChangedEventHandler)
controller.sceneProperty.addListener(sceneChangedEventHandler)
}

private fun addSceneParamsBindings() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/engine/shaders/frame/frame.geom
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ void main() {

if (isColored) {
int frameID = gs_in[0].frameID;
int frameX = int(mod(frameID, uGridWidth));
int frameX = frameID % uGridWidth;
int frameY = frameID / uGridWidth;

int bufferX = int(mod(frameX, uBuffersSize.x));
int bufferY = int(mod(frameY, uBuffersSize.y));
int bufferX = frameX % uBuffersSize.x;
int bufferY = frameY % uBuffersSize.y;
float texID = float(bufferY * uBuffersSize.x + bufferX);

vec4 initialPosition = vec4(
Expand Down
Loading