Description
Most appropriate sub-area of p5.js?
- Accessibility
- Color
- Core/Environment/Rendering
- Data
- DOM
- Events
- Image
- IO
- Math
- Typography
- Utilities
- WebGL
- Build process
- Unit testing
- Internationalization
- Friendly errors
- Other (specify if possible)
p5.js version
2.0.0 beta 2
Web browser and version
Chrome 132.0.6834.111
Operating system
MacOSX Sequoia 15.2
Steps to reproduce this
Both webGL and canvas renderer performance seems to have dropped between 1.11.3 and 2.0.0 beta 2 significantly. In the attached sketch, I try to draw 1000 particles. In 1.11.3, I'm able to achieve a smooth 60fps. In 2.0, I barely crack 20fps with webGL, 40fps for canvas. In fact in 1.11.3 I can render 3500 particles in webGL still at 60fps. You can comment between the two libraries in the index.html file.
There are some other issues mentioning webGL losing perf from around a year ago #6743 , I didn't see anything more recent but since this seemed like (to me) a common use case it felt worth bringing up again, especially since the canvas renderer appears to be significantly slower as well.
Steps:
- Try to draw 1000 shapes in 1.11.3, then do the same in 2.0.0 beta 2
- Observe the performance difference
Snippet:
webgl: https://editor.p5js.org/aferriss/sketches/_x59zc5AH
canvas: https://editor.p5js.org/aferriss/sketches/lFcjKYZKM
let renderWidth = 2000;
let renderHeight = 2000;
let fbo;
let nParticles = 1000;
let particles = [];
class Particle {
constructor(x, y, z, size) {
this.x = x;
this.y = y;
this.z = z;
this.size = size;
this.color = color(random(0, 255), random(0, 255), random(0, 255));
}
update() {
this.z += 2;
if (this.z > 800) {
this.z = -100;
}
}
draw() {
push();
translate(this.x, this.y, this.z);
rectMode(CENTER);
fill(this.color);
noStroke();
rect(0, 0, this.size, this.size);
pop();
}
}
let fontLoaded = false;
function setup() {
createCanvas(renderWidth/4, renderWidth/4, WEBGL);
let fboOptions = {
width: renderWidth,
height: renderHeight,
depth: true,
alpha: true,
};
fbo = createFramebuffer(fboOptions);
pixelDensity(1);
for (let i = 0; i < nParticles; i++) {
particles.push(
new Particle(
random(-renderWidth / 2, renderWidth / 2),
random(-renderHeight / 2, renderHeight / 2),
random(-100, 800),
random(10, 20)
)
);
}
loadFont("Arial.ttf", font => {
textFont(font);
textSize(20);
textAlign(LEFT, TOP);
fontLoaded = true;
});
}
function draw() {
// render some particles into an fbo
// fbo is not necessary, you can remove and see the same issue
fbo.begin();
push();
clear();
background(255);
for(let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
}
pop();
fbo.end();
image(fbo, -width/2, -height/2, width, height);
// draw fps
if (fontLoaded) {
push();
fill(255, 0, 0);
text("FPS: " + frameRate().toFixed(2), -width/2 + 20, -height/2 + 20);
pop();
}
}