Bug? shapes in P2D

When you run this code it draws a linear line and a ball that jumps around with ‘a’ and ‘d’. The line is drawn in the for loop. Running this works just fine, but if you change the conditional in the for loop to 1250 instead of 1251 the entire thing slows way down. If you continue to reduce the number in the conditional, it gradually starts running faster until somewhere around 300 the lag barely noticeable. I am using shape drawing and vertex because I am drawing non linear functions pixel by pixel, but after troubleshooting found that the same problem happens with just a basic linear line. Also, I found that in this problematic range, if you drop the strokeWeight down to 1, it runs smoothly, but above that 1251 cutoff, any stroke works. Tried running it on 2 different computers and it did not make a difference. This only happens when I run P2D.

int playerY = 500;
int playerX = 500;
float xv = 0;
float yv = 0;
void setup(){
  fullScreen(P2D,SPAN);
}
void draw(){
  frameRate(165);
  background(0);
  fill(255);
  ellipse(playerX,playerY,15,15);
  yv +=0.4;
 
  playerX+=xv;
  playerY+=yv;
 
  beginShape();
   noFill();
   stroke(255);
   strokeWeight(3);
   for (int d = 0; d < 1251; d ++) {       //here
   vertex(d,d);
   }
   endShape();
}

void keyPressed() {
  if (key == 'd'||key == 'a') {
     yv = -10;
    if (key=='d') {
      xv += 10;
    } else {
      xv -= 10;
    }
  }
}
1 Like

Hi @zzabel8,

This is interesting thanks for reporting this! :wink:

Here is a simple example code to test:

void setup() {
  fullScreen(P2D);
}

void draw() {
  background(0);

  beginShape();
  
  noFill();
  stroke(255);
  strokeWeight(3);

  for (int d = 0; d < 1250; d++) {
    vertex(d, d);
  }

  endShape();
  
  fill(255);
  text("FPS: " + frameRate, 50, 50);
}

On my windows machine with d < 1250, the above code runs at 35 fps then progressively drops down around 8 fps…

  • Using d < 1251 runs at 60 fps on average

  • Using d < 1250 and vertex(0, d) drops down to 8fps

  • Using d < 1250 and vertex(d, 0) runs at 60fps

  • Using d < 1250 and curveVertex(d, d); runs at 60fps :exploding_head:

Do someone know what’s going on? I’m sure it’s a low level optimization / threshold or algorithm related to OpenGL since P2D uses that.