[orx-compositor] Refactor resource management in Compositor.
Removed `deepDestroy` in favor of `destroy`, simplifying cleanup logic. Introduced `Session` management in `Composite` to handle resource lifecycles properly.
This commit is contained in:
@@ -12,23 +12,6 @@ import org.openrndr.extra.parameters.BooleanParameter
|
|||||||
import org.openrndr.extra.parameters.Description
|
import org.openrndr.extra.parameters.Description
|
||||||
import kotlin.jvm.JvmRecord
|
import kotlin.jvm.JvmRecord
|
||||||
|
|
||||||
fun RenderTarget.deepDestroy() {
|
|
||||||
val cbcopy = colorAttachments.map { it }
|
|
||||||
val dbcopy = depthBuffer
|
|
||||||
detachDepthBuffer()
|
|
||||||
detachColorAttachments()
|
|
||||||
cbcopy.forEach {
|
|
||||||
when (it) {
|
|
||||||
is ColorBufferAttachment -> it.colorBuffer.destroy()
|
|
||||||
is CubemapAttachment -> it.cubemap.destroy()
|
|
||||||
is ArrayTextureAttachment -> it.arrayTexture.destroy()
|
|
||||||
is ArrayCubemapAttachment -> it.arrayCubemap.destroy()
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dbcopy?.destroy()
|
|
||||||
destroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class LayerType {
|
enum class LayerType {
|
||||||
LAYER,
|
LAYER,
|
||||||
@@ -185,7 +168,7 @@ open class Layer internal constructor(
|
|||||||
private fun createLayerTarget(
|
private fun createLayerTarget(
|
||||||
activeRenderTarget: RenderTarget, drawer: Drawer, bufferMultisample: BufferMultisample
|
activeRenderTarget: RenderTarget, drawer: Drawer, bufferMultisample: BufferMultisample
|
||||||
) {
|
) {
|
||||||
layerTarget?.deepDestroy()
|
layerTarget?.destroy()
|
||||||
layerTarget = renderTarget(
|
layerTarget = renderTarget(
|
||||||
activeRenderTarget.width, activeRenderTarget.height,
|
activeRenderTarget.width, activeRenderTarget.height,
|
||||||
activeRenderTarget.contentScale, bufferMultisample
|
activeRenderTarget.contentScale, bufferMultisample
|
||||||
@@ -348,37 +331,31 @@ class ColorBufferCache(val width: Int, val height: Int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Composite : Layer(LayerType.LAYER) {
|
class Composite(val session: Session?) : Layer(LayerType.LAYER), AutoCloseable {
|
||||||
|
|
||||||
private var cache = ColorBufferCache(RenderTarget.active.width, RenderTarget.active.height)
|
private var cache = ColorBufferCache(RenderTarget.active.width, RenderTarget.active.height)
|
||||||
fun draw(drawer: Drawer) {
|
fun draw(drawer: Drawer) {
|
||||||
|
|
||||||
|
session?.push()
|
||||||
if (cache.width != RenderTarget.active.width || cache.height != RenderTarget.active.height) {
|
if (cache.width != RenderTarget.active.width || cache.height != RenderTarget.active.height) {
|
||||||
cache.destroy()
|
cache.destroy()
|
||||||
cache = ColorBufferCache(RenderTarget.active.width, RenderTarget.active.height)
|
cache = ColorBufferCache(RenderTarget.active.width, RenderTarget.active.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
drawLayer(drawer, cache)
|
drawLayer(drawer, cache)
|
||||||
|
session?.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun close() {
|
||||||
|
session?.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create a layered composition
|
* create a layered composition
|
||||||
*/
|
*/
|
||||||
fun compose(function: Layer.() -> Unit): Composite {
|
fun compose(function: Composite.() -> Unit): Composite {
|
||||||
val root = Composite()
|
val session = Session.active.fork()
|
||||||
|
val root = Composite(session)
|
||||||
root.function()
|
root.function()
|
||||||
|
session.pop()
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
class Compositor : Extension {
|
|
||||||
override var enabled: Boolean = true
|
|
||||||
var composite = Composite()
|
|
||||||
|
|
||||||
override fun afterDraw(drawer: Drawer, program: Program) {
|
|
||||||
drawer.isolated {
|
|
||||||
drawer.defaults()
|
|
||||||
composite.draw(drawer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user