Skip to content

Commit b0b88df

Browse files
committed
first implementation of MemoryStack integrated in the glfw window loop
1 parent 869ee1b commit b0b88df

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/main/kotlin/uno/glfw/GlfwWindow.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.lwjgl.opengl.GL
1717
import org.lwjgl.opengl.GL43C
1818
import org.lwjgl.opengl.GLUtil
1919
import org.lwjgl.system.Callback
20+
import org.lwjgl.system.MemoryStack
2021
import org.lwjgl.system.MemoryUtil.*
2122
import org.lwjgl.vulkan.VkInstance
2223
import uno.kotlin.first
@@ -433,15 +434,23 @@ open class GlfwWindow(var handle: GlfwWindowHandle) {
433434

434435
var autoSwap = true
435436

436-
inline fun loop(block: () -> Unit) = loop({ isOpen }, block)
437+
inline fun loop(block: (MemoryStack) -> Unit) = loop({ isOpen }, block)
437438

438-
inline fun loop(condition: () -> Boolean, block: () -> Unit) {
439+
/**
440+
* The `stack` passed to `block` will be automatically a stack frame in place
441+
* (i.e. it has been pushed exactly once, without popping).
442+
* So you can do any allocation on that frame without pushing/popping further
443+
* It's the user choice to pass it down the stacktrace to avoid TLS
444+
*/
445+
inline fun loop(condition: () -> Boolean, block: (MemoryStack) -> Unit) {
439446
while (condition()) {
440447
glfwPollEvents()
441-
block()
448+
val stack = MemoryStack.stackGet()
449+
block(stack.push())
442450
if (autoSwap)
443451
glfwSwapBuffers(handle)
444-
appBuffer.reset()
452+
stack.pop()
453+
appBuffer.reset() // TODO delete
445454
}
446455
}
447456

0 commit comments

Comments
 (0)