Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small bloom render pass optimization #7104

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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