From f8cee11ab808847ae2722e60b1d1e243450cfa8c Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Tue, 14 Jan 2020 18:39:34 +0100 Subject: [PATCH] Add intermediate cache for BoxBlur --- .../kotlin/blur/ApproximateGaussianBlur.kt | 4 ++-- orx-fx/src/main/kotlin/blur/BoxBlur.kt | 22 +++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt b/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt index 1612bf00..85eca199 100644 --- a/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt +++ b/orx-fx/src/main/kotlin/blur/ApproximateGaussianBlur.kt @@ -11,7 +11,7 @@ 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) + data class ColorBufferDescription(val width: Int, val height: Int, val contentScale: Double, val format: ColorFormat, val type: ColorType) /** @@ -51,7 +51,7 @@ class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertex colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type) } - intermediate?.let { + intermediate.let { parameters["blurDirection"] = Vector2(1.0, 0.0) super.apply(source, arrayOf(it)) diff --git a/orx-fx/src/main/kotlin/blur/BoxBlur.kt b/orx-fx/src/main/kotlin/blur/BoxBlur.kt index 74c1bf6e..54106104 100644 --- a/orx-fx/src/main/kotlin/blur/BoxBlur.kt +++ b/orx-fx/src/main/kotlin/blur/BoxBlur.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,8 @@ import org.openrndr.math.Vector2 class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("blur/box-blur.frag"))) { + data class ColorBufferDescription(val width: Int, val height: Int, val contentScale: Double, val format: ColorFormat, val type: ColorType) + /** * The sample window, default is 5 */ @@ -29,7 +28,7 @@ class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode, */ var gain: Double by parameters - private var intermediate: ColorBuffer? = null + private var intermediateCache = mutableMapOf() init { window = 5 @@ -38,17 +37,12 @@ class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode, } override fun apply(source: Array, target: Array) { - intermediate?.let { - if (it.width != target[0].width || it.height != target[0].height) { - intermediate = null - } + val intermediateDescription = ApproximateGaussianBlur.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) } - if (intermediate == null) { - intermediate = colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type) - } - - intermediate?.let { + intermediate.let { parameters["blurDirection"] = Vector2(1.0, 0.0) super.apply(source, arrayOf(it))