Add layer and post buffer caching

This commit is contained in:
Edwin Jakobs
2019-01-20 17:30:23 +01:00
parent 6332962972
commit a3385abee0

View File

@@ -4,6 +4,21 @@ import org.openrndr.color.ColorRGBa
import org.openrndr.draw.* import org.openrndr.draw.*
import org.openrndr.math.Matrix44 import org.openrndr.math.Matrix44
private val postBufferCache = mutableListOf<ColorBuffer>()
fun RenderTarget.deepDestroy() {
val cbcopy = colorBuffers.map { it}
val dbcopy = depthBuffer
detachDepthBuffer()
detachColorBuffers()
cbcopy.forEach {
it.destroy()
}
dbcopy?.destroy()
destroy()
}
/** /**
* A single layer representation * A single layer representation
*/ */
@@ -13,42 +28,66 @@ class Layer internal constructor() {
var blendFilter: Pair<Filter, Filter.() -> Unit>? = null var blendFilter: Pair<Filter, Filter.() -> Unit>? = null
val postFilters: MutableList<Pair<Filter, Filter.() -> Unit>> = mutableListOf() val postFilters: MutableList<Pair<Filter, Filter.() -> Unit>> = mutableListOf()
private var layerTarget:RenderTarget? = null
/** /**
* draw the layer * draw the layer
*/ */
fun draw(drawer: Drawer) { fun draw(drawer: Drawer) {
val rt = RenderTarget.active val rt = RenderTarget.active
val layerTarget = renderTarget(rt.width, rt.height) {
val llt = layerTarget
if (llt == null || (llt.width != rt.width || llt.height != rt.height)) {
layerTarget?.deepDestroy()
layerTarget = renderTarget(rt.width, rt.height) {
colorBuffer() colorBuffer()
depthBuffer() depthBuffer()
} }
}
drawer.isolatedWithTarget(layerTarget) { layerTarget?.let { target ->
drawer.isolatedWithTarget(target) {
drawer.background(ColorRGBa.TRANSPARENT) drawer.background(ColorRGBa.TRANSPARENT)
drawFunc() drawFunc()
children.forEach { children.forEach {
it.draw(drawer) it.draw(drawer)
} }
} }
val (tmpTargets, layerPost) = postFilters.let { filters -> if (postFilters.size > 0) {
val targets = List(Math.min(filters.size, 2)) { val sizeMismatch = if (postBufferCache.isNotEmpty()) {
colorBuffer(rt.width, rt.height) postBufferCache[0].width != rt.width || postBufferCache[0].height != rt.height
} else {
false
} }
val result = filters.foldIndexed(layerTarget.colorBuffer(0)) { i, source, filter ->
if (sizeMismatch) {
postBufferCache.forEach { it.destroy() }
postBufferCache.clear()
}
if (postBufferCache.isEmpty()) {
postBufferCache += colorBuffer(rt.width, rt.height)
postBufferCache += colorBuffer(rt.width, rt.height)
}
}
val layerPost = postFilters.let { filters ->
val targets = postBufferCache
val result = filters.foldIndexed(target.colorBuffer(0)) { i, source, filter ->
val target = targets[i % targets.size] val target = targets[i % targets.size]
filter.first.apply(filter.second) filter.first.apply(filter.second)
filter.first.apply(source, target) filter.first.apply(source, target)
target target
} }
Pair(targets, result) result
} }
val lblend = blendFilter val lblend = blendFilter
if (lblend == null) { if (lblend == null) {
drawer.isolatedWithTarget(rt) { drawer.isolatedWithTarget(rt) {
drawer.ortho(rt) //drawer.ortho(rt)
drawer.ortho()
drawer.view = Matrix44.IDENTITY drawer.view = Matrix44.IDENTITY
drawer.model = Matrix44.IDENTITY drawer.model = Matrix44.IDENTITY
drawer.image(layerPost, layerPost.bounds, drawer.bounds) drawer.image(layerPost, layerPost.bounds, drawer.bounds)
@@ -57,16 +96,7 @@ class Layer internal constructor() {
lblend.first.apply(lblend.second) lblend.first.apply(lblend.second)
lblend.first.apply(arrayOf(rt.colorBuffer(0), layerPost), rt.colorBuffer(0)) lblend.first.apply(arrayOf(rt.colorBuffer(0), layerPost), rt.colorBuffer(0))
} }
tmpTargets.forEach {
it.destroy()
} }
layerTarget.colorBuffer(0).destroy()
layerTarget.depthBuffer?.destroy()
layerTarget.detachColorBuffers()
layerTarget.detachDepthBuffer()
layerTarget.destroy()
} }
} }