diff --git a/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt b/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt index 8631dc6d..1612bf00 100644 --- a/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt +++ b/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt @@ -1,9 +1,6 @@ package org.openrndr.extra.fx.blur -import org.openrndr.draw.ColorBuffer -import org.openrndr.draw.Filter -import org.openrndr.draw.Shader -import org.openrndr.draw.colorBuffer +import org.openrndr.draw.* import org.openrndr.extra.fx.filterFragmentCode import org.openrndr.math.Vector2 @@ -14,6 +11,9 @@ import org.openrndr.math.Vector2 class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("blur/approximate-gaussian-blur.frag"))) { + data class ColorBufferDescription(val width:Int, val height:Int, val contentScale:Double, val format:ColorFormat, val type:ColorType) + + /** * blur sample window, default value is 5 */ @@ -35,7 +35,8 @@ class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertex var gain: Double by parameters - private var intermediate: ColorBuffer? = null + private var intermediateCache = mutableMapOf() + init { window = 5 @@ -45,14 +46,9 @@ class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertex } override fun apply(source: Array, target: Array) { - intermediate?.let { - if (it.width != target[0].width || it.height != target[0].height) { - intermediate = null - } - } - - if (intermediate == null) { - intermediate = colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type) + val intermediateDescription = ColorBufferDescription(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type) + val intermediate = intermediateCache.getOrPut(intermediateDescription) { + colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type) } intermediate?.let {