Add intermediate cache for BoxBlur
This commit is contained in:
@@ -11,7 +11,7 @@ import org.openrndr.math.Vector2
|
|||||||
class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
|
class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
|
||||||
filterFragmentCode("blur/approximate-gaussian-blur.frag"))) {
|
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)
|
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)
|
parameters["blurDirection"] = Vector2(1.0, 0.0)
|
||||||
super.apply(source, arrayOf(it))
|
super.apply(source, arrayOf(it))
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package org.openrndr.extra.fx.blur
|
package org.openrndr.extra.fx.blur
|
||||||
|
|
||||||
import org.openrndr.draw.ColorBuffer
|
import org.openrndr.draw.*
|
||||||
import org.openrndr.draw.Filter
|
|
||||||
import org.openrndr.draw.Shader
|
|
||||||
import org.openrndr.draw.colorBuffer
|
|
||||||
import org.openrndr.extra.fx.filterFragmentCode
|
import org.openrndr.extra.fx.filterFragmentCode
|
||||||
|
|
||||||
import org.openrndr.math.Vector2
|
import org.openrndr.math.Vector2
|
||||||
@@ -14,6 +11,8 @@ import org.openrndr.math.Vector2
|
|||||||
class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
|
class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
|
||||||
filterFragmentCode("blur/box-blur.frag"))) {
|
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
|
* The sample window, default is 5
|
||||||
*/
|
*/
|
||||||
@@ -29,7 +28,7 @@ class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
|
|||||||
*/
|
*/
|
||||||
var gain: Double by parameters
|
var gain: Double by parameters
|
||||||
|
|
||||||
private var intermediate: ColorBuffer? = null
|
private var intermediateCache = mutableMapOf<ApproximateGaussianBlur.ColorBufferDescription, ColorBuffer>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
window = 5
|
window = 5
|
||||||
@@ -38,17 +37,12 @@ class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||||
intermediate?.let {
|
val intermediateDescription = ApproximateGaussianBlur.ColorBufferDescription(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
|
||||||
if (it.width != target[0].width || it.height != target[0].height) {
|
val intermediate = intermediateCache.getOrPut(intermediateDescription) {
|
||||||
intermediate = null
|
colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intermediate == null) {
|
intermediate.let {
|
||||||
intermediate = colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
|
|
||||||
}
|
|
||||||
|
|
||||||
intermediate?.let {
|
|
||||||
parameters["blurDirection"] = Vector2(1.0, 0.0)
|
parameters["blurDirection"] = Vector2(1.0, 0.0)
|
||||||
super.apply(source, arrayOf(it))
|
super.apply(source, arrayOf(it))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user