Skip to content

Commit

Permalink
Small bloom render pass optimization (#7104)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Nov 13, 2024
1 parent c21bfb7 commit 1939666
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/extras/render-passes/render-pass-bloom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 35 additions & 26 deletions src/extras/render-passes/render-pass-downsample.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,50 @@ 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;
varying vec2 uv0;
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);
}`
Expand Down

0 comments on commit 1939666

Please sign in to comment.