From 193966637f2271f87878283f50bfbf65b8f64f4e Mon Sep 17 00:00:00 2001 From: Martin Valigursky <59932779+mvaligursky@users.noreply.github.com> Date: Wed, 13 Nov 2024 09:38:55 +0000 Subject: [PATCH] Small bloom render pass optimization (#7104) Co-authored-by: Martin Valigursky --- src/extras/render-passes/render-pass-bloom.js | 3 +- .../render-passes/render-pass-downsample.js | 61 +++++++++++-------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/extras/render-passes/render-pass-bloom.js b/src/extras/render-passes/render-pass-bloom.js index 53019903473..6f85693b7b3 100644 --- a/src/extras/render-passes/render-pass-bloom.js +++ b/src/extras/render-passes/render-pass-bloom.js @@ -94,7 +94,8 @@ class RenderPassBloom extends RenderPass { let passSourceTexture = this._sourceTexture; for (let i = 0; i < numPasses; i++) { - const pass = new RenderPassDownsample(device, passSourceTexture); + const fast = i === 0; // fast box downscale for the first pass + const pass = new RenderPassDownsample(device, passSourceTexture, fast); const rt = this.renderTargets[i]; pass.init(rt, { resizeSource: passSourceTexture, diff --git a/src/extras/render-passes/render-pass-downsample.js b/src/extras/render-passes/render-pass-downsample.js index 5d685a621df..e047ef099cb 100644 --- a/src/extras/render-passes/render-pass-downsample.js +++ b/src/extras/render-passes/render-pass-downsample.js @@ -7,10 +7,12 @@ import { RenderPassShaderQuad } from '../../scene/graphics/render-pass-shader-qu * @ignore */ class RenderPassDownsample extends RenderPassShaderQuad { - constructor(device, sourceTexture) { + constructor(device, sourceTexture, fast = false) { super(device); this.sourceTexture = sourceTexture; - this.shader = this.createQuadShader('DownSampleShader', /* glsl */` + this.shader = this.createQuadShader(`DownSampleShader${fast ? 'Fast' : ''}`, /* glsl */` + + ${fast ? '#define FAST' : ''} uniform sampler2D sourceTexture; uniform vec2 sourceInvResolution; @@ -18,30 +20,37 @@ class RenderPassDownsample extends RenderPassShaderQuad { void main() { - float x = sourceInvResolution.x; - float y = sourceInvResolution.y; - - vec3 a = texture2D (sourceTexture, vec2 (uv0.x - 2.0 * x, uv0.y + 2.0 * y)).rgb; - vec3 b = texture2D (sourceTexture, vec2 (uv0.x, uv0.y + 2.0 * y)).rgb; - vec3 c = texture2D (sourceTexture, vec2 (uv0.x + 2.0 * x, uv0.y + 2.0 * y)).rgb; - - vec3 d = texture2D (sourceTexture, vec2 (uv0.x - 2.0 * x, uv0.y)).rgb; - vec3 e = texture2D (sourceTexture, vec2 (uv0.x, uv0.y)).rgb; - vec3 f = texture2D (sourceTexture, vec2 (uv0.x + 2.0 * x, uv0.y)).rgb; - - vec3 g = texture2D (sourceTexture, vec2 (uv0.x - 2.0 * x, uv0.y - 2.0 * y)).rgb; - vec3 h = texture2D (sourceTexture, vec2 (uv0.x, uv0.y - 2.0 * y)).rgb; - vec3 i = texture2D (sourceTexture, vec2 (uv0.x + 2.0 * x, uv0.y - 2.0 * y)).rgb; - - vec3 j = texture2D (sourceTexture, vec2 (uv0.x - x, uv0.y + y)).rgb; - vec3 k = texture2D (sourceTexture, vec2 (uv0.x + x, uv0.y + y)).rgb; - vec3 l = texture2D (sourceTexture, vec2 (uv0.x - x, uv0.y - y)).rgb; - vec3 m = texture2D (sourceTexture, vec2 (uv0.x + x, uv0.y - y)).rgb; - - vec3 value = e * 0.125; - value += (a + c + g + i) * 0.03125; - value += (b + d + f + h) * 0.0625; - value += (j + k + l + m) * 0.125; + vec3 e = texture2D (sourceTexture, vec2 (uv0.x, uv0.y)).rgb; + + #ifdef FAST + vec3 value = e; + #else + + float x = sourceInvResolution.x; + float y = sourceInvResolution.y; + + vec3 a = texture2D (sourceTexture, vec2 (uv0.x - 2.0 * x, uv0.y + 2.0 * y)).rgb; + vec3 b = texture2D (sourceTexture, vec2 (uv0.x, uv0.y + 2.0 * y)).rgb; + vec3 c = texture2D (sourceTexture, vec2 (uv0.x + 2.0 * x, uv0.y + 2.0 * y)).rgb; + + vec3 d = texture2D (sourceTexture, vec2 (uv0.x - 2.0 * x, uv0.y)).rgb; + vec3 f = texture2D (sourceTexture, vec2 (uv0.x + 2.0 * x, uv0.y)).rgb; + + vec3 g = texture2D (sourceTexture, vec2 (uv0.x - 2.0 * x, uv0.y - 2.0 * y)).rgb; + vec3 h = texture2D (sourceTexture, vec2 (uv0.x, uv0.y - 2.0 * y)).rgb; + vec3 i = texture2D (sourceTexture, vec2 (uv0.x + 2.0 * x, uv0.y - 2.0 * y)).rgb; + + vec3 j = texture2D (sourceTexture, vec2 (uv0.x - x, uv0.y + y)).rgb; + vec3 k = texture2D (sourceTexture, vec2 (uv0.x + x, uv0.y + y)).rgb; + vec3 l = texture2D (sourceTexture, vec2 (uv0.x - x, uv0.y - y)).rgb; + vec3 m = texture2D (sourceTexture, vec2 (uv0.x + x, uv0.y - y)).rgb; + + + vec3 value = e * 0.125; + value += (a + c + g + i) * 0.03125; + value += (b + d + f + h) * 0.0625; + value += (j + k + l + m) * 0.125; + #endif gl_FragColor = vec4(value, 1.0); }`