Skip to content

Commit

Permalink
[orx-fx] Fix GLES compatibility for FrameBlur and MipBloom
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Aug 14, 2024
1 parent ff04452 commit 5b6f2ec
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 65 deletions.
20 changes: 18 additions & 2 deletions orx-fx/src/commonMain/kotlin/blur/FrameBlur.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(intermediate2!!), clip)

super.apply(arrayOf(source[0], intermediate!!), arrayOf(intermediate!!), clip)
intermediate!!.copyTo(target[0])
pt.apply(intermediate2!!, intermediate!!)
pt.apply(intermediate!!, target[0])
}
}
}
46 changes: 14 additions & 32 deletions orx-fx/src/commonMain/kotlin/blur/MipBloom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,69 +66,51 @@ open class MipBloom<T : Filter>(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<ColorBuffer>()
var sourceCopy: ColorBuffer? = null
var blurred = mutableListOf<ColorBuffer>()

val upscale = BloomUpscale()
val downScale = BloomDownscale()
val combine = BloomCombine()

override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>, 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
|| (intermediates.isNotEmpty() && (!intermediates[0].isEquivalentTo(target[0], ignoreType = true, ignoreFormat = true)))) {
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)
}
}

Expand Down
6 changes: 1 addition & 5 deletions orx-fx/src/shaders/glsl/blur/approximate-gaussian-blur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions orx-fx/src/shaders/glsl/blur/bloom-downscale.frag
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
13 changes: 6 additions & 7 deletions orx-fx/src/shaders/glsl/blur/bloom-upscale.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ float nrand(vec2 n) {
uniform float noiseSeed;
uniform float shape;
uniform float gain;

uniform float noiseGain;

in vec2 v_texCoord0;
Expand All @@ -28,23 +27,23 @@ 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;
}
{
float weight = pow(1.0, 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;
}
{
float weight = pow(2.0, 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;
}

Expand All @@ -53,23 +52,23 @@ 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;
}
{
float weight = pow(4.0, 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;
}
{
float weight = pow(5.0, 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;
}

Expand Down
18 changes: 7 additions & 11 deletions orx-fx/src/shaders/glsl/blur/gaussian-blur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 5b6f2ec

Please sign in to comment.