Skip to content

Improve antialiased framebuffer performance #7794

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

Merged
merged 1 commit into from
May 6, 2025
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
74 changes: 53 additions & 21 deletions src/webgl/p5.Framebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class FramebufferTexture {
return this.framebuffer.height * this.framebuffer.density;
}

update() {
this.framebuffer._update(this.property);
}

rawTexture() {
return this.framebuffer[this.property];
}
Expand All @@ -59,6 +63,8 @@ class Framebuffer {

this._isClipApplied = false;

this.dirty = { colorTexture: false, depthTexture: false };

this.pixels = [];

this.format = settings.format || constants.UNSIGNED_BYTE;
Expand Down Expand Up @@ -1098,6 +1104,49 @@ class Framebuffer {
}
}

/**
* Ensure all readable textures are up-to-date.
* @private
* @property {'colorTexutre'|'depthTexture'} property The property to update
*/
_update(property) {
if (this.dirty[property] && this.antialias) {
const gl = this.gl;
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this.aaFramebuffer);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.framebuffer);
const partsToCopy = {
colorTexture: [gl.COLOR_BUFFER_BIT, this.colorP5Texture.glMagFilter],
};
if (this.useDepth) {
partsToCopy.depthTexture = [
gl.DEPTH_BUFFER_BIT,
this.depthP5Texture.glMagFilter
];
}
const [flag, filter] = partsToCopy[property];
gl.blitFramebuffer(
0,
0,
this.width * this.density,
this.height * this.density,
0,
0,
this.width * this.density,
this.height * this.density,
flag,
filter
);
this.dirty[property] = false;

const activeFbo = this.renderer.activeFramebuffer();
if (activeFbo) {
gl.bindFramebuffer(gl.FRAMEBUFFER, activeFbo._framebufferToBind());
} else {
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
}
}

/**
* Ensures that the framebuffer is ready to be drawn to
*
Expand All @@ -1119,27 +1168,7 @@ class Framebuffer {
*/
_beforeEnd() {
if (this.antialias) {
const gl = this.gl;
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this.aaFramebuffer);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.framebuffer);
const partsToCopy = [
[gl.COLOR_BUFFER_BIT, this.colorP5Texture.glMagFilter]
];
if (this.useDepth) {
partsToCopy.push(
[gl.DEPTH_BUFFER_BIT, this.depthP5Texture.glMagFilter]
);
}
for (const [flag, filter] of partsToCopy) {
gl.blitFramebuffer(
0, 0,
this.width * this.density, this.height * this.density,
0, 0,
this.width * this.density, this.height * this.density,
flag,
filter
);
}
this.dirty = { colorTexture: true, depthTexture: true };
}
}

Expand Down Expand Up @@ -1319,6 +1348,7 @@ class Framebuffer {
* </div>
*/
loadPixels() {
this._update('colorTexture');
const gl = this.gl;
const prevFramebuffer = this.renderer.activeFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
Expand Down Expand Up @@ -1377,6 +1407,7 @@ class Framebuffer {
* @return {Number[]} color of the pixel at `(x, y)` as an array of color values `[R, G, B, A]`.
*/
get(x, y, w, h) {
this._update('colorTexture');
// p5._validateParameters('p5.Framebuffer.get', arguments);
const colorFormat = this._glColorFormat();
if (x === undefined && y === undefined) {
Expand Down Expand Up @@ -1543,6 +1574,7 @@ class Framebuffer {
this.pixels
);
this.colorP5Texture.unbindTexture();
this.dirty.colorTexture = false;

const prevFramebuffer = this.renderer.activeFramebuffer();
if (this.antialias) {
Expand Down
1 change: 1 addition & 0 deletions src/webgl/p5.Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Texture {
// FramebufferTexture instances wrap raw WebGL textures already, which
// don't need any extra updating, as they already live on the GPU
if (this.isFramebufferTexture) {
this.src.update();
return false;
}

Expand Down