From 5b6f2ec1ff0166b32bfc24169c47bb77cfe294c1 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Wed, 14 Aug 2024 12:11:53 +0200 Subject: [PATCH] [orx-fx] Fix GLES compatibility for FrameBlur and MipBloom --- .../src/commonMain/kotlin/blur/FrameBlur.kt | 20 +++++++- orx-fx/src/commonMain/kotlin/blur/MipBloom.kt | 46 ++++++------------- .../glsl/blur/approximate-gaussian-blur.frag | 6 +-- .../shaders/glsl/blur/bloom-downscale.frag | 16 +++---- .../src/shaders/glsl/blur/bloom-upscale.frag | 13 +++--- .../src/shaders/glsl/blur/gaussian-blur.frag | 18 +++----- 6 files changed, 54 insertions(+), 65 deletions(-) diff --git a/orx-fx/src/commonMain/kotlin/blur/FrameBlur.kt b/orx-fx/src/commonMain/kotlin/blur/FrameBlur.kt index f9dc1d3c..00e5b0e9 100644 --- a/orx-fx/src/commonMain/kotlin/blur/FrameBlur.kt +++ b/orx-fx/src/commonMain/kotlin/blur/FrameBlur.kt @@ -4,6 +4,7 @@ package org.openrndr.extra.fx.blur import org.openrndr.color.ColorRGBa import org.openrndr.draw.* +import org.openrndr.extra.fx.blend.Passthrough import org.openrndr.extra.fx.fx_frame_blur import org.openrndr.extra.fx.mppFilterShader import org.openrndr.extra.parameters.Description @@ -17,7 +18,10 @@ class FrameBlur(val colorType: ColorType = ColorType.FLOAT16) : @DoubleParameter("blend", 0.0, 1.0) var blend: Double by parameters + + val pt = Passthrough() private var intermediate: ColorBuffer? = null + private var intermediate2: ColorBuffer? = null init { blend = 0.5 @@ -32,14 +36,26 @@ class FrameBlur(val colorType: ColorType = ColorType.FLOAT16) : intermediate = null } } + intermediate2?.let { + if (it.isEquivalentTo(target[0], ignoreFormat = true, ignoreLevels = true)) { + it.destroy() + intermediate2 = null + } + } if (intermediate == null) { intermediate = target[0].createEquivalent(type = colorType) intermediate?.fill(ColorRGBa.TRANSPARENT) } + if (intermediate2 == null) { + intermediate2 = target[0].createEquivalent(type = colorType) + intermediate2?.fill(ColorRGBa.TRANSPARENT) + } - super.apply(arrayOf(source[0], intermediate!!), arrayOf(intermediate!!), clip) - intermediate!!.copyTo(target[0]) + super.apply(arrayOf(source[0], intermediate!!), arrayOf(intermediate2!!), clip) + + pt.apply(intermediate2!!, intermediate!!) + pt.apply(intermediate!!, target[0]) } } } diff --git a/orx-fx/src/commonMain/kotlin/blur/MipBloom.kt b/orx-fx/src/commonMain/kotlin/blur/MipBloom.kt index acd49ad7..72ed43b3 100644 --- a/orx-fx/src/commonMain/kotlin/blur/MipBloom.kt +++ b/orx-fx/src/commonMain/kotlin/blur/MipBloom.kt @@ -66,12 +66,8 @@ open class MipBloom(val blur: T) : Filter1to1(mppFilterShader(fx_blo @DoubleParameter("noise seed", 0.0, 1000.0) var noiseSeed: Double = 0.0 - - @BooleanParameter("sRGB") - var sRGB = true - var intermediates = mutableListOf() - var sourceCopy: ColorBuffer? = null + var blurred = mutableListOf() val upscale = BloomUpscale() val downScale = BloomDownscale() @@ -79,17 +75,6 @@ open class MipBloom(val blur: T) : Filter1to1(mppFilterShader(fx_blo override fun apply(source: Array, target: Array, clip: Rectangle?) { require(clip == null) - sourceCopy?.let { - if (!it.isEquivalentTo(source[0], ignoreType = true)) { - it.destroy() - sourceCopy = null - } - } - if (sourceCopy == null) { - sourceCopy = source[0].createEquivalent(type = ColorType.FLOAT16) - } - - source[0].copyTo(sourceCopy!!) upscale.shape = shape if (intermediates.size != passes @@ -97,38 +82,35 @@ open class MipBloom(val blur: T) : Filter1to1(mppFilterShader(fx_blo intermediates.forEach { it.destroy() } + blurred.forEach { + it.destroy() + } intermediates.clear() + blurred.clear() for (pass in 0 until passes) { val tdiv = 1 shl (pass + 1) - val cb = colorBuffer(target[0].width / tdiv, target[0].height / tdiv, type = ColorType.FLOAT16) + val cb = colorBuffer(target[0].width / tdiv, target[0].height / tdiv, type = ColorType.FLOAT32) intermediates.add(cb) + val cbb = colorBuffer(target[0].width / tdiv, target[0].height / tdiv, type = ColorType.FLOAT32) + blurred.add(cbb) } } - - if (sRGB) { - linearize.apply(sourceCopy!!, sourceCopy!!, clip) - } - upscale.noiseGain = noiseGain upscale.noiseSeed = noiseSeed - downScale.apply(sourceCopy!!, intermediates[0], clip) - blur.apply(intermediates[0], intermediates[0], clip) + downScale.apply(source[0], intermediates[0], clip) + blur.apply(intermediates[0], blurred[0], clip) for (pass in 1 until passes) { - downScale.apply(intermediates[pass - 1], intermediates[pass], clip) - blur.apply(intermediates[pass], intermediates[pass], clip) + downScale.apply(blurred[pass - 1], intermediates[pass], clip) + blur.apply(intermediates[pass], blurred[pass], clip) } - upscale.apply(intermediates.toTypedArray(), arrayOf(target[0]), clip) + upscale.apply(blurred.toTypedArray(), arrayOf(target[0]), clip) combine.gain = gain combine.pregain = pregain - combine.apply(arrayOf(sourceCopy!!, target[0]), target, clip) - - if (sRGB) { - delinearize.apply(target[0], target[0], clip) - } + combine.apply(arrayOf(source[0], target[0]), target, clip) } } diff --git a/orx-fx/src/shaders/glsl/blur/approximate-gaussian-blur.frag b/orx-fx/src/shaders/glsl/blur/approximate-gaussian-blur.frag index ae304d9f..8bd52737 100644 --- a/orx-fx/src/shaders/glsl/blur/approximate-gaussian-blur.frag +++ b/orx-fx/src/shaders/glsl/blur/approximate-gaussian-blur.frag @@ -19,11 +19,7 @@ void main() { for (int x = -w; x <= w; ++x) { float lw = exp( float(-(x*x)) / (2.0 * sigma * sigma) ) ; vec2 tc = v_texCoord0 + float(x) * blurDirection * s;// * spread; - #ifndef OR_WEBGL2 - sum += textureLod(tex0, tc, float(sourceLevel)) * lw; - #else - sum += texture(tex0, tc); - #endif + sum += texture(tex0, tc) * lw; weight += lw; } o_color = (sum / weight) * gain; diff --git a/orx-fx/src/shaders/glsl/blur/bloom-downscale.frag b/orx-fx/src/shaders/glsl/blur/bloom-downscale.frag index 68218097..42d3e15b 100644 --- a/orx-fx/src/shaders/glsl/blur/bloom-downscale.frag +++ b/orx-fx/src/shaders/glsl/blur/bloom-downscale.frag @@ -1,6 +1,6 @@ -out vec4 o_output; -in vec2 v_texCoord0; -uniform sampler2D tex0; +highp out vec4 o_output; +highp in vec2 v_texCoord0; +uniform highp sampler2D tex0; // -- based on https://github.com/excess-demogroup/even-laster-engine/blob/a451a89f6bd6d3c6017d5890b92d9f72823bc742/src/shaders/bloom.fra @@ -11,9 +11,9 @@ void main() vec4 offsets = vec4(-diagonalOffsets.xy, +diagonalOffsets.xy) / vec2(textureSize(tex0, 0)).xyxy; float diagonalWeight = 0.2085034734347498; - o_output = textureLod(tex0, v_texCoord0, 0.0) * centerWeight + - textureLod(tex0, v_texCoord0 + offsets.xy, 0.0) * diagonalWeight + - textureLod(tex0, v_texCoord0 + offsets.wx, 0.0) * diagonalWeight + - textureLod(tex0, v_texCoord0 + offsets.zw, 0.0) * diagonalWeight + - textureLod(tex0, v_texCoord0 + offsets.yz, 0.0) * diagonalWeight; + o_output = texture(tex0, v_texCoord0) * centerWeight + + texture(tex0, v_texCoord0 + offsets.xy) * diagonalWeight + + texture(tex0, v_texCoord0 + offsets.wx) * diagonalWeight + + texture(tex0, v_texCoord0 + offsets.zw) * diagonalWeight + + texture(tex0, v_texCoord0 + offsets.yz) * diagonalWeight; } \ No newline at end of file diff --git a/orx-fx/src/shaders/glsl/blur/bloom-upscale.frag b/orx-fx/src/shaders/glsl/blur/bloom-upscale.frag index 573d10d9..1a806d39 100644 --- a/orx-fx/src/shaders/glsl/blur/bloom-upscale.frag +++ b/orx-fx/src/shaders/glsl/blur/bloom-upscale.frag @@ -6,7 +6,6 @@ float nrand(vec2 n) { uniform float noiseSeed; uniform float shape; uniform float gain; - uniform float noiseGain; in vec2 v_texCoord0; @@ -28,7 +27,7 @@ vec4 sampleBloom(vec2 pos, float shape) { vec2 rnd = vec2(nrand(3.0 + 0.0 + pos.xy + noiseSeed), nrand(5.0 + 0.0 + pos.yx - noiseSeed)); rnd = (rnd * 2.0 - 1.0) / vec2(textureSize(tex0, 0)); - sum += textureLod(tex0, pos + rnd * noiseGain, 0.0) * weight; + sum += texture(tex0, pos + rnd * noiseGain) * weight; total += weight; } { @@ -36,7 +35,7 @@ vec4 sampleBloom(vec2 pos, float shape) { vec2 rnd = vec2(nrand(3.0 + 0.0 + pos.xy + noiseSeed), nrand(5.0 + 0.0 + pos.yx - noiseSeed)); rnd = (rnd * 2.0 - 1.0) / vec2(textureSize(tex0, 0)); - sum += textureLod(tex1, pos + rnd * noiseGain, 0.0) * weight; + sum += texture(tex1, pos + rnd * noiseGain, 0.0) * weight; total += weight; } { @@ -44,7 +43,7 @@ vec4 sampleBloom(vec2 pos, float shape) { vec2 rnd = vec2(nrand(3.0 + 0.0 + pos.xy + noiseSeed), nrand(5.0 + 0.0 + pos.yx - noiseSeed)); rnd = (rnd * 2.0 - 1.0) / vec2(textureSize(tex0, 0)); - sum += textureLod(tex2, pos + rnd * noiseGain, 0.0) * weight; + sum += texture(tex2, pos + rnd * noiseGain) * weight; total += weight; } @@ -53,7 +52,7 @@ vec4 sampleBloom(vec2 pos, float shape) { vec2 rnd = vec2(nrand(3.0 + 0.0 + pos.xy + noiseSeed), nrand(5.0 + 0.0 + pos.yx - noiseSeed)); rnd = (rnd * 3.0 - 1.0) / vec2(textureSize(tex0, 0)); - sum += textureLod(tex3, pos + rnd * noiseGain, 0.0) * weight; + sum += texture(tex3, pos + rnd * noiseGain) * weight; total += weight; } { @@ -61,7 +60,7 @@ vec4 sampleBloom(vec2 pos, float shape) { vec2 rnd = vec2(nrand(3.0 + 0.0 + pos.xy + noiseSeed), nrand(5.0 + 0.0 + pos.yx - noiseSeed)); rnd = (rnd * 3.0 - 1.0) / vec2(textureSize(tex0, 0)); - sum += textureLod(tex4, pos + rnd * noiseGain, 0.0) * weight; + sum += texture(tex4, pos + rnd * noiseGain) * weight; total += weight; } { @@ -69,7 +68,7 @@ vec4 sampleBloom(vec2 pos, float shape) { vec2 rnd = vec2(nrand(3.0 + 0.0 + pos.xy + noiseSeed), nrand(5.0 + 0.0 + pos.yx - noiseSeed)); rnd = (rnd * 3.0 - 1.0) / vec2(textureSize(tex0, 0)); - sum += textureLod(tex5, pos + rnd * noiseGain, 0.0) * weight; + sum += texture(tex5, pos + rnd * noiseGain) * weight; total += weight; } diff --git a/orx-fx/src/shaders/glsl/blur/gaussian-blur.frag b/orx-fx/src/shaders/glsl/blur/gaussian-blur.frag index 8ff16558..13cb3d0f 100644 --- a/orx-fx/src/shaders/glsl/blur/gaussian-blur.frag +++ b/orx-fx/src/shaders/glsl/blur/gaussian-blur.frag @@ -6,24 +6,20 @@ uniform float sigma; uniform float spread; uniform float gain; - out vec4 o_color; void main() { - - vec2 s = vec2(textureSize(tex0, 0).xy); - s = vec2(1.0/s.x, 1.0/s.y); - + vec2 s = vec2(textureSize(tex0, 0).xy); + s = vec2(1.0 / s.x, 1.0 / s.y); int w = window; vec4 sum = vec4(0.0, 0.0, 0.0, 0.0); float weight = 0.0; - for (int y = -w; y<= w; ++y) { - for (int x = -w; x<= w; ++x) { - float lw = exp(-float(x*x+y*y) / (2.0 * sigma * sigma)); - sum+=texture(tex0, v_texCoord0 + vec2(x, y) * s * spread) * lw; - weight+=lw; + for (int y = -w; y <= w; ++y) { + for (int x = -w; x <= w; ++x) { + float lw = exp(-float(x * x + y * y) / (2.0 * sigma * sigma)); + sum += texture(tex0, v_texCoord0 + vec2(x, y) * s * 1.0) * lw; + weight += lw; } } - o_color = (sum / weight) * gain; } \ No newline at end of file