Skip to content

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

@rapatski

Description

@rapatski

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions