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

PGraphics loadPixels() seems to not set/check .loaded flag in P2D #6214

Open
rapatski opened this issue Aug 26, 2021 · 1 comment
Open

PGraphics loadPixels() seems to not set/check .loaded flag in P2D #6214

rapatski opened this issue Aug 26, 2021 · 1 comment

Comments

@rapatski
Copy link

rapatski commented Aug 26, 2021

Description

Whenever loadPixels() is used on a PGraphics object in a P2D context, it seems to literally download the texture every time, regardless of whether it's been called/downloaded before. In the standard renderer, it seems like subsequent loadPixels() calls do not result in numerous texture downloads.

Expected Behavior

I was expecting the loadPixels() function to check the isLoaded() / loaded flag before deciding on whether the pixel array has to be recreated or not.

Current Behavior

In P2D, calling loadPixels on a large PGraphics texture numerous times a frame absolutely wrecks the frame rate, as it should if it recreates/downloads the buffer every time. The standard renderer seems to ignore subsequent loadPixels() calls to the same PGraphics texture. On standard PImage files this issue does not exist. Only PGraphics objects passed into / converted to PImage objects, see example below.

Steps to Reproduce

PGraphics pg;

void setup()
{
  size(500, 500, P2D);
  frameRate(60);

  //pg = createGraphics(width, height); // 60fps - works as expected; PImage.loadPixels() call in sampleColour ignored 
  pg = createGraphics(width, height, P2D); // 2fps - performance seemingly wrecked by pg.loadPixels() in sampleColour() 
}

void draw()
{
  surface.setTitle("FPS: "+nf(frameRate, 1, 1));

  /*
  This example replicates the PImage.get() behaviour
  creating a 'sampleColour' method to illustrate the issue
  */
  
  // update offscreen pg & prepare texture for sampling
  pg.beginDraw();
  pg.background(0);
  pg.fill(255);
  pg.ellipse(pg.width/2, pg.height/2, pg.width, pg.height);
  pg.endDraw();
  
  pg.loadPixels(); // safeguard for removing/commenting the loadPixels in the sampleColour function below

  int numX = 20;
  int numY = 20;

  for(int i = 0; i < numX; i++)
    for(int j = 0; j < numY; j++)
    {
      float normX = (i+0.5f)/float(numX);
      float normY = (j+0.5f)/float(numY);
      color c = sampleColour(pg, normX, normY);
      
      fill(c);
      rectMode(CENTER);
      rect(normX*width, normY*height, width/numX, height/numY);
    }
}

color sampleColour(PImage img, float normX, float normY)
{
  img.loadPixels(); // with this uncommented, this function wrecks the sketch in P2D - but not in the standard renderer
  return img.pixels[int(normY * img.height) * img.width + int(normX * img.width)];
}

Your Environment

  • Processing version: Both 3.5.4 and 4.0b1
  • Operating System and OS version: macOS Big Sur 11.4 (20F71)
  • Other information:
@rapatski rapatski changed the title PGraphics loadPixels() seems to not set .loaded flag in P2D PGraphics loadPixels() seems to not set/check .loaded flag in P2D Aug 26, 2021
@rapatski
Copy link
Author

Updated the replication example code as to not run into another PGraphics/P2D issue documented separately here: benfry/processing4#641

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant