# Performance loss with PGraphics and SVG

**URL:** <https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897>\
**Category:** Coding Questions\
**Created:** [July 22, 2019, 8:25pm UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897 "2019-07-22T20:25:13Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [September 16, 2019, 5:31pm UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897/21 "2019-09-16T17:31:01Z")

</div>

I’m not sure there are many built-in examples of this. Look under `Topics / Textures`. Only the sphere example builds a PShape with texture coordinates I think.

IIRC there’s an issue where you can’t change the texture for the shape later. I think I managed this by using a PGraphics as the texture and drawing video, etc. into that first if you need it to update.

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 16, 2019, 5:40pm UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897/22 "2019-09-16T17:40:01Z")

</div>

yes. i was reaaly whinkin about sending a PGraph for the shader, so i can do whatever i want inside it.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 16, 2019, 6:38pm UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897/23 "2019-09-16T18:38:59Z")

</div>

> [@neilcsmith](#):
>
> IIRC there’s an issue where you can’t change the texture for the shape later.

If I’m thinking of the same issue, there was a recent discussion about using `s.getTessellation()` as a workaround for this.

> [@PShape texture only applies colour not text?](https://discourse.processing.org/t/pshape-texture-only-applies-colour-not-text/13596/10):
>
> Terribly sorry, should have actually shared it from my test sketch – perhaps I forgot the return type? Anyhow, Replace this line: // roundRect.setTexture(textBoxPGraphics); roundRect = addTextureUV(roundRect, textBoxPGraphics); … and add THIS function: /\*\* \* https://github.com/processing/processing/issues/2508#issuecomment-50774212 \*/ PShape addTextureUV(PShape s, PImage img) { PShape s2 = s.getTessellation(); // github.com/processing/processing/issues/2508#issuecomment-52296723 s2.s…

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 18, 2019, 12:14am UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897/24 "2019-09-18T00:14:50Z")

</div>

i’m figuring out that the issue is not with the PShape itself, it’s with the PGraphics itself.  
i’ve dumped all the PShapes, commented color sampling, using ‘live’ vector data and the problem is still the same. it starts with 9.5 fps and keeps dropping to 0.3. it looks like it’s in a giant loop that’s redrawing the vector data all over.  
it’s funny cause when i draw them out of PGraph it runs smoothly

main thread

```auto
ArrayList<Brick> bricks;
PGraphics pg;
int w=2400;
int h=1920;

void setup() {
  size(600, 480, P2D);
  smooth();
  colorMode(HSB, 360, 100, 100); 

  println("building PGraphics");
  pg=createGraphics(w, h, P2D);

  println("building bricks");
  bricks=new ArrayList<Brick>();
  buildBrick();
}

void draw() {
  background(32);

  pg.beginDraw();
  pg.background(0);
  for (Brick b : bricks) {
    //b.update();
    b.display();
  }

  pg.endDraw();
  image(pg, 0, 0, width, height);

  fill(0, 100, 100);
  textSize(20);
  text(frameRate, 10, 30);
}

void buildBrick() {
  original=loadShape("SVG_OUT_03.svg");
  original.disableStyle();
  int numChild=original.getChildCount();
  println(numChild);

  for (int i=0; i<numChild; i++) {
    PShape tempFace=original.getChild(i);
    int numBricks=tempFace.getChildCount();
    println("face: "+i+" bricks:"+numBricks);

    for (int j=0; j<numBricks; j++) {
      PShape temp=tempFace.getChild(j);

      int vCount=temp.getVertexCount();
      ArrayList<PVector> tempVertex=new ArrayList<PVector>();

      for (int k=0; k<vCount; k++) {
        PVector pv=temp.getVertex(k);
        tempVertex.add(pv);
      }

      bricks.add(new Brick(tempVertex));
    }
  }
}

```

the brick object

```auto
class Brick {
  ArrayList<PVector> vertices;
  PVector center;
  int centerX, centerY;
  color c;
  int loc;
  int hue=0, sat=0, bright=100;
  int alpha=0;

  Brick(ArrayList<PVector> v) {
    vertices=v;
    center=new PVector();
    c=color(0, 0, 100);
    getCentroid();
  }

  void getCentroid() {
    for (PVector p : vertices) {
      centerX+=p.x;
      centerY+=p.y;
    }
    centerX/=vertices.size();
    centerY/=vertices.size();

    loc=int(centerX+centerY*pg.width);
  }

  void update() {
    bright=colorMap.get(this);
  }

  void display() {
    
    pg.beginDraw();
    pg.beginShape();
    //pg.fill(hue, sat, bright);
    pg.noFill();
    pg.strokeWeight(1);
    pg.stroke(255);
    //pg.noStroke();
    for (PVector p : vertices) {
      pg.vertex(p.x, p.y);
    }
    pg.endShape(CLOSE);

    pg.strokeWeight(4);
    pg.stroke(255);
    pg.fill(c);
    pg.ellipse(center.x, center.y, 4, 1);

    pg.endDraw();
  }
}
```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [September 18, 2019, 6:17am UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897/25 "2019-09-18T06:17:52Z")

</div>

I just noticed what might be an obvious problem. You’re calling `pg.beginDraw()` and `pg.endDraw()` in every brick display. Just do this once in your main draw method.

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 18, 2019, 5:09pm UTC](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897/26 "2019-09-18T17:09:03Z")

</div>

this works, it’s faster, but still loosing performance after 60 cycles.

[Previous page](https://discourse.processing.org/t/performance-loss-with-pgraphics-and-svg/12897.md?page=1)
