Why does my program freeze when I draw new PShapes for the first time?

I'm making an infinite terrain generator (github). I have decided to split up my world into 32x32 chunks, each of which is stored as both a heightmap and a PShape. When I cross a chunk boundary, I generate new chunks on the edge of my vision range, and I also update the level of detail of some chunks. As I am generating about 50 new PShapes with a total of at least 10,000 vertices, I put the generation in a new thread. The problem is, my program still freezes for about a second after generating the new chunks.

I did some profiling, and found that the majority (about 60-70%) of the time is spent in ArrayList.remove(), mostly in this call stack:

[my rendering method]
 PApplet.shape()
  PGraphics.shape()
   PShapeOpenGL.draw()
    PShapeOpenGL.updateGeometry()
     PShapeOpenGL.initBuffers()
      PShapeOpenGL.initPolyBuffers()
       VertexBuffer.<init>()
        VertexBuffer.<init>()
         VertexBuffer.create()
          PGraphicsOpenGL$GLResourceVertexBuffer.<init>()
           PGraphicsOpenGL$GLResourceVertexBuffer.drainRefQueueBounded()
            PGraphicsOpenGL$GLResourceVertexBuffer.dispose()
             ArrayList.remove()

From this, I assume that there's more initialization done at first draw—stuff that needs to be done other than just adding all the child shapes. I tried drawing it on a temporary PGraphics in the generation thread with things like:

createGraphics(1, 1, P3D).shape(shape);

but those all resulted in the error:

GLException: GL-Error 0x502 while creating mutable storage for target 0x8892 -> buffer 1 of size 4096 with data null

Has anybody experienced anything similar? Anything I can do to fix this, or is this a bug with Processing?

Tagged:
Sign In or Register to comment.