Skip to content

Commit b6e1b01

Browse files
committed
moved lazy initalization of default shader and VBO to flushBuffer() and getShader()
1 parent 20742ba commit b6e1b01

File tree

1 file changed

+71
-42
lines changed

1 file changed

+71
-42
lines changed

core/src/processing/opengl/PGraphics2DX.java

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2019 The Processing Foundation
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation, version 2.1.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
123
package processing.opengl;
224

325
import static processing.core.PApplet.println;
@@ -13,18 +35,29 @@
1335
import processing.core.PShape;
1436
import processing.core.PShapeSVG;
1537

16-
// Super fast OpenGL 2D renderer by Miles Fogle:
17-
// https://github.com/hazmatsuitor
18-
19-
//for testing purposes, I found it easier to create a separate class and avoid
20-
//touching existing code for now, rather than directly editing PGraphics2D/PGraphicsOpenGL
21-
//if this code becomes the new P2D implementation, then it will be properly migrated/integrated
22-
23-
//NOTE: this implementation doesn't use some of Processing's OpenGL wrappers
24-
//(e.g. PShader, Texture) because I found it more convenient to handle them manually
25-
//it could probably be made to use those classes with a bit of elbow grease and a spot of guidance
26-
//but it may not be worth it - I doubt it would reduce complexity much, if at all
27-
//(if there are reasons we need to use those classes, let me know)
38+
/**
39+
* Super fast OpenGL 2D renderer originally contributed by Miles Fogle:
40+
* https://github.com/hazmatsuitor
41+
*
42+
* It speeds-up rendering of 2D geometry by essentially two key optimizations: packing all the
43+
* vertex data in a single VBO, and using a custom stroke tessellator (see StrokeRenderer class
44+
* at the end). There are a number of other, less critical optimizations, for example using a single
45+
* shader for textured and non-textured geometry and a depth algorithm that allows stacking a large
46+
* number of 2D shapes without z-fighting (so occlusion is based on drawing order).
47+
*
48+
* Some notes from Miles:
49+
*
50+
* for testing purposes, I found it easier to create a separate class and avoid
51+
* touching existing code for now, rather than directly editing PGraphics2D/PGraphicsOpenGL
52+
* if this code becomes the new P2D implementation, then it will be properly migrated/integrated
53+
54+
* NOTE: this implementation doesn't use some of Processing's OpenGL wrappers
55+
* (e.g. PShader, Texture) because I found it more convenient to handle them manually
56+
* it could probably be made to use those classes with a bit of elbow grease and a spot of guidance
57+
* but it may not be worth it - I doubt it would reduce complexity much, if at all
58+
* (if there are reasons we need to use those classes, let me know)
59+
*
60+
*/
2861

2962
//TODO: track debug performance stats
3063
public final class PGraphics2DX extends PGraphicsOpenGL {
@@ -361,8 +394,6 @@ public void texture(PImage image) {
361394
return;
362395
}
363396

364-
init();
365-
366397
Texture t = currentPG.getTexture(image);
367398
texWidth = t.width;
368399
texHeight = t.height;
@@ -1290,14 +1321,13 @@ protected void end2D() {
12901321
public void filter(PShader shader) {
12911322
// The filter method needs to use the geometry-generation in the base class.
12921323
// We could re-implement it here, but this is easier.
1293-
// if (!useParentImpl) {
1294-
// useOldP2D();
1295-
// super.filter(shader);
1296-
// useNewP2D();
1297-
// } else {
1298-
// super.filter(shader);
1299-
// }
1300-
super.filter(shader);
1324+
if (!useParentImpl) {
1325+
useOldP2D();
1326+
super.filter(shader);
1327+
useNewP2D();
1328+
} else {
1329+
super.filter(shader);
1330+
}
13011331
}
13021332

13031333

@@ -1501,24 +1531,6 @@ public void lightSpecular(float v1, float v2, float v3) {
15011531
// PRIVATE IMPLEMENTATION
15021532

15031533

1504-
//superclass does lazy initialization, so we need to as well
1505-
private void init() {
1506-
if (initialized) return;
1507-
initialized = true;
1508-
1509-
String[] vertSource = pgl.loadVertexShader(defP2DShaderVertURL);
1510-
String[] fragSource = pgl.loadFragmentShader(defP2DShaderFragURL);
1511-
twoShader = new PShader(parent, vertSource, fragSource);
1512-
loadShaderLocs(twoShader);
1513-
defTwoShader = twoShader;
1514-
1515-
//generate vbo
1516-
IntBuffer vboBuff = IntBuffer.allocate(1);
1517-
pgl.genBuffers(1, vboBuff);
1518-
vbo = vboBuff.get(0);
1519-
}
1520-
1521-
15221534
//maxVerts can be tweaked for memory/performance trade-off
15231535
//in my testing, performance seems to plateau after around 6000 (= 2000*3)
15241536
//memory usage should be around ~165kb for 6000 verts
@@ -1631,9 +1643,14 @@ private void flushBuffer() {
16311643
return;
16321644
}
16331645

1634-
init();
1646+
if (vbo == 0) {
1647+
// Generate vbo
1648+
IntBuffer vboBuff = IntBuffer.allocate(1);
1649+
pgl.genBuffers(1, vboBuff);
1650+
vbo = vboBuff.get(0);
1651+
}
16351652

1636-
//upload vertex data
1653+
// Upload vertex data
16371654
pgl.bindBuffer(PGL.ARRAY_BUFFER, vbo);
16381655
pgl.bufferData(PGL.ARRAY_BUFFER, usedVerts * vertSize,
16391656
FloatBuffer.wrap(vertexData), PGL.DYNAMIC_DRAW);
@@ -1666,6 +1683,9 @@ private boolean checkShaderLocs(PShader shader) {
16661683
transformLoc = shader.getUniformLoc("transformMatrix");
16671684
}
16681685
int texScaleLoc = shader.getUniformLoc("texScale");
1686+
if (texScaleLoc == -1) {
1687+
texScaleLoc = shader.getUniformLoc("texOffset");
1688+
}
16691689
return positionLoc != -1 && colorLoc != -1 && texCoordLoc != -1 &&
16701690
texFactorLoc != -1 && transformLoc != -1 && texScaleLoc != -1;
16711691
}
@@ -1684,12 +1704,21 @@ private void loadShaderLocs(PShader shader) {
16841704
transformLoc = shader.getUniformLoc("transformMatrix");
16851705
}
16861706
texScaleLoc = shader.getUniformLoc("texScale");
1707+
if (texScaleLoc == -1) {
1708+
texScaleLoc = shader.getUniformLoc("texOffset");
1709+
}
16871710
}
16881711

16891712

16901713
private PShader getShader() {
16911714
PShader shader;
16921715
if (twoShader == null) {
1716+
if (defTwoShader == null) {
1717+
String[] vertSource = pgl.loadVertexShader(defP2DShaderVertURL);
1718+
String[] fragSource = pgl.loadFragmentShader(defP2DShaderFragURL);
1719+
defTwoShader = new PShader(parent, vertSource, fragSource);
1720+
loadShaderLocs(defTwoShader);
1721+
}
16931722
shader = defTwoShader;
16941723
} else {
16951724
shader = twoShader;

0 commit comments

Comments
 (0)