Skip to content

Commit 0bd0a51

Browse files
committed
[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.
1 parent cf26dcd commit 0bd0a51

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

orx-compositor/src/commonMain/kotlin/Compositor.kt

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@ import org.openrndr.extra.parameters.BooleanParameter
1212
import org.openrndr.extra.parameters.Description
1313
import kotlin.jvm.JvmRecord
1414

15-
fun RenderTarget.deepDestroy() {
16-
val cbcopy = colorAttachments.map { it }
17-
val dbcopy = depthBuffer
18-
detachDepthBuffer()
19-
detachColorAttachments()
20-
cbcopy.forEach {
21-
when (it) {
22-
is ColorBufferAttachment -> it.colorBuffer.destroy()
23-
is CubemapAttachment -> it.cubemap.destroy()
24-
is ArrayTextureAttachment -> it.arrayTexture.destroy()
25-
is ArrayCubemapAttachment -> it.arrayCubemap.destroy()
26-
else -> {}
27-
}
28-
}
29-
dbcopy?.destroy()
30-
destroy()
31-
}
3215

3316
enum class LayerType {
3417
LAYER,
@@ -185,7 +168,7 @@ open class Layer internal constructor(
185168
private fun createLayerTarget(
186169
activeRenderTarget: RenderTarget, drawer: Drawer, bufferMultisample: BufferMultisample
187170
) {
188-
layerTarget?.deepDestroy()
171+
layerTarget?.destroy()
189172
layerTarget = renderTarget(
190173
activeRenderTarget.width, activeRenderTarget.height,
191174
activeRenderTarget.contentScale, bufferMultisample
@@ -348,37 +331,31 @@ class ColorBufferCache(val width: Int, val height: Int) {
348331
}
349332
}
350333

351-
class Composite : Layer(LayerType.LAYER) {
352-
334+
class Composite(val session: Session?) : Layer(LayerType.LAYER), AutoCloseable {
353335
private var cache = ColorBufferCache(RenderTarget.active.width, RenderTarget.active.height)
354336
fun draw(drawer: Drawer) {
355337

338+
session?.push()
356339
if (cache.width != RenderTarget.active.width || cache.height != RenderTarget.active.height) {
357340
cache.destroy()
358341
cache = ColorBufferCache(RenderTarget.active.width, RenderTarget.active.height)
359342
}
360-
361343
drawLayer(drawer, cache)
344+
session?.pop()
345+
}
346+
347+
override fun close() {
348+
session?.close()
362349
}
363350
}
364351

365352
/**
366353
* create a layered composition
367354
*/
368-
fun compose(function: Layer.() -> Unit): Composite {
369-
val root = Composite()
355+
fun compose(function: Composite.() -> Unit): Composite {
356+
val session = Session.active.fork()
357+
val root = Composite(session)
370358
root.function()
359+
session.pop()
371360
return root
372-
}
373-
374-
class Compositor : Extension {
375-
override var enabled: Boolean = true
376-
var composite = Composite()
377-
378-
override fun afterDraw(drawer: Drawer, program: Program) {
379-
drawer.isolated {
380-
drawer.defaults()
381-
composite.draw(drawer)
382-
}
383-
}
384-
}
361+
}

0 commit comments

Comments
 (0)