Skip to content

Commit d93f646

Browse files
committed
Some more buffer fixes
1 parent 3f087fc commit d93f646

File tree

4 files changed

+95
-80
lines changed

4 files changed

+95
-80
lines changed

java/libraries/lwjgl/src/processing/lwjgl/PGL.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,7 @@ public void disableTexturing(int target) {
927927
GL11.glDisable(target);
928928
}
929929

930-
public void initTexture(int target, int width, int height, int format, int type) {
931-
//int[] texels = new int[width * height];
930+
public void initTexture(int target, int width, int height, int format, int type) {
932931
IntBuffer texels = createIntBuffer(width * height);
933932
GL11.glTexSubImage2D(target, 0, 0, 0, width, height, format, type, texels);
934933
}

java/libraries/lwjgl/src/processing/lwjgl/PGraphicsLWJGL.java

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,9 +1414,9 @@ public void beginDraw() {
14141414
//pg.disableLights();
14151415
}
14161416

1417-
inGeo.reset();
1418-
tessGeo.reset();
1419-
texCache.reset();
1417+
inGeo.clear();
1418+
tessGeo.clear();
1419+
texCache.clear();
14201420

14211421
// Each frame starts with textures disabled.
14221422
super.noTexture();
@@ -1956,7 +1956,7 @@ public PShape createShape(int kind, float... p) {
19561956
public void beginShape(int kind) {
19571957
shape = kind;
19581958

1959-
inGeo.reset();
1959+
inGeo.clear();
19601960

19611961
breakShape = false;
19621962
defaultEdges = true;
@@ -2254,8 +2254,8 @@ public void flush() {
22542254
}
22552255
}
22562256

2257-
tessGeo.reset();
2258-
texCache.reset();
2257+
tessGeo.clear();
2258+
texCache.clear();
22592259
}
22602260

22612261

@@ -4307,8 +4307,8 @@ protected void backgroundImpl(PImage image) {
43074307
}
43084308

43094309
protected void backgroundImpl() {
4310-
tessGeo.reset();
4311-
texCache.reset();
4310+
tessGeo.clear();
4311+
texCache.clear();
43124312

43134313
pgl.glClearColor(0, 0, 0, 0);
43144314
pgl.glClear(PGL.GL_DEPTH_BUFFER_BIT);
@@ -6321,7 +6321,7 @@ public void allocate() {
63216321
hasTexture = false;
63226322
}
63236323

6324-
public void reset() {
6324+
public void clear() {
63256325
java.util.Arrays.fill(textures, 0, count, null);
63266326
count = 0;
63276327
hasTexture = false;
@@ -6472,7 +6472,7 @@ public InGeometry(int mode) {
64726472
allocate();
64736473
}
64746474

6475-
public void reset() {
6475+
public void clear() {
64766476
vertexCount = firstVertex = lastVertex = 0;
64776477
edgeCount = firstEdge = lastEdge = 0;
64786478
}
@@ -6490,7 +6490,7 @@ public void allocate() {
64906490
emissive = new int[PGL.DEFAULT_IN_VERTICES];
64916491
shininess = new float[PGL.DEFAULT_IN_VERTICES];
64926492
edges = new int[PGL.DEFAULT_IN_EDGES][3];
6493-
reset();
6493+
clear();
64946494
}
64956495

64966496
public void trim() {
@@ -7197,7 +7197,7 @@ public TessGeometry(int mode) {
71977197
allocate();
71987198
}
71997199

7200-
public void reset() {
7200+
public void clear() {
72017201
firstFillVertex = lastFillVertex = fillVertexCount = 0;
72027202
firstFillIndex = lastFillIndex = fillIndexCount = 0;
72037203

@@ -7207,25 +7207,25 @@ public void reset() {
72077207
firstPointVertex = lastPointVertex = pointVertexCount = 0;
72087208
firstPointIndex = lastPointIndex = pointIndexCount = 0;
72097209

7210-
fillVertices.rewind();
7211-
fillColors.rewind();
7212-
fillNormals.rewind();
7213-
fillTexcoords.rewind();
7214-
fillAmbient.rewind();
7215-
fillSpecular.rewind();
7216-
fillEmissive.rewind();
7217-
fillShininess.rewind();
7218-
fillIndices.rewind();
7219-
7220-
lineVertices.rewind();
7221-
lineColors.rewind();
7222-
lineDirWidths.rewind();
7223-
lineIndices.rewind();
7224-
7225-
pointVertices.rewind();
7226-
pointColors.rewind();
7227-
pointSizes.rewind();
7228-
pointIndices.rewind();
7210+
fillVertices.clear();
7211+
fillColors.clear();
7212+
fillNormals.clear();
7213+
fillTexcoords.clear();
7214+
fillAmbient.clear();
7215+
fillSpecular.clear();
7216+
fillEmissive.clear();
7217+
fillShininess.clear();
7218+
fillIndices.clear();
7219+
7220+
lineVertices.clear();
7221+
lineColors.clear();
7222+
lineDirWidths.clear();
7223+
lineIndices.clear();
7224+
7225+
pointVertices.clear();
7226+
pointColors.clear();
7227+
pointSizes.clear();
7228+
pointIndices.clear();
72297229

72307230
isStroked = false;
72317231
}
@@ -7251,7 +7251,7 @@ public void allocate() {
72517251
pointSizes = pgl.createFloatBuffer(2 * PGL.DEFAULT_TESS_VERTICES);
72527252
pointIndices = pgl.createIntBuffer(PGL.DEFAULT_TESS_VERTICES);
72537253

7254-
reset();
7254+
clear();
72557255
}
72567256

72577257
public void trim() {
@@ -7735,8 +7735,8 @@ protected void prepareLineVerticesForCopy(int start, int count) {
77357735
}
77367736

77377737
protected void prepareLineIndicesForCopy(int start, int count) {
7738-
lineIndices.position(3 * start);
7739-
lineIndices.limit(3 * count);
7738+
lineIndices.position(start);
7739+
lineIndices.limit(count);
77407740
}
77417741

77427742
protected void preparePointVerticesForCopy() {
@@ -7759,8 +7759,8 @@ protected void preparePointVerticesForCopy(int start, int count) {
77597759
}
77607760

77617761
protected void preparePointIndicesForCopy(int start, int count) {
7762-
pointIndices.position(3 * start);
7763-
pointIndices.limit(3 * count);
7762+
pointIndices.position(start);
7763+
pointIndices.limit(count);
77647764
}
77657765

77667766

@@ -7998,18 +7998,20 @@ public void addFillVertices(InGeometry in) {
79987998
float ny = in.normals[index++];
79997999
float nz = in.normals[index ];
80008000

8001-
index = 3 * tessIdx;
80028001
vert[0] = x * mm.m00 + y * mm.m01 + z * mm.m02 + mm.m03;
80038002
vert[1] = x * mm.m10 + y * mm.m11 + z * mm.m12 + mm.m13;
80048003
vert[2] = x * mm.m20 + y * mm.m21 + z * mm.m22 + mm.m23;
8005-
8006-
index = 3 * tessIdx;
8004+
80078005
norm[0] = nx * nm.m00 + ny * nm.m10 + nz * nm.m20;
80088006
norm[1] = nx * nm.m01 + ny * nm.m11 + nz * nm.m21;
80098007
norm[2] = nx * nm.m02 + ny * nm.m12 + nz * nm.m22;
8010-
8008+
80118009
fillVertices.position(3 * tessIdx);
8012-
fillVertices.put(vert);
8010+
try {
8011+
fillVertices.put(vert);
8012+
} catch (java.nio.BufferOverflowException e) {
8013+
PApplet.println("mama mia " + 3 * tessIdx + " " + fillVertices.position() + " " + fillVertices.limit() + " " + fillVertices.capacity() + " " + 3 * fillVertexCount);
8014+
}
80138015

80148016
fillNormals.position(3 * tessIdx);
80158017
fillNormals.put(norm);
@@ -8072,14 +8074,14 @@ public void putLineVertex(InGeometry in, int inIdx0, int inIdx1, int tessIdx, in
80728074
lineVertices.position(3 * tessIdx);
80738075
lineVertices.put(vert);
80748076

8075-
lineVertices.position(4 * tessIdx);
8076-
lineVertices.put(attr);
8077+
lineDirWidths.position(4 * tessIdx);
8078+
lineDirWidths.put(attr);
80778079
} else {
80788080
lineVertices.position(3 * tessIdx);
80798081
lineVertices.put(in.vertices, 3 * inIdx0, 3);
80808082

8081-
lineVertices.position(4 * tessIdx);
8082-
lineVertices.put(in.vertices, 3 * inIdx1, 3);
8083+
lineDirWidths.position(4 * tessIdx);
8084+
lineDirWidths.put(in.vertices, 3 * inIdx1, 3);
80838085
}
80848086

80858087
lineColors.position(tessIdx);

java/libraries/lwjgl/src/processing/lwjgl/PShape3D.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.util.HashSet;
4848
import java.util.Hashtable;
4949

50-
import org.lwjgl.BufferUtils;
5150

5251
// Notes about geometry update in PShape3D.
5352
// 1) When applying a transformation on a group shape
@@ -867,20 +866,15 @@ protected void updateFillColor() {
867866

868867
updateTesselation();
869868

870-
BufferUtils.zeroBuffer(tess.fillColors);
871-
872-
//Arrays.fill(tess.fillColors, 0, tess.fillVertexCount, fillColor);
873-
// int[] temp = new int[tess.fillVertexCount];
874-
// Arrays.fill(temp, 0, tess.fillVertexCount, fillColor);
875-
// tess.fillColors.position(0);
876-
// tess.fillColors.put(temp);
877-
Arrays.fill(tess.fillColors.array(), 0, tess.fillVertexCount, fillColor);
869+
int[] temp = new int[tess.fillVertexCount];
870+
Arrays.fill(temp, 0, tess.fillVertexCount, fillColor);
871+
tess.fillColors.rewind();
872+
tess.fillColors.put(temp);
878873

879874
modifiedFillColors = true;
880875
modified();
881876
}
882-
883-
877+
884878
//////////////////////////////////////////////////////////////
885879

886880
// STROKE COLOR
@@ -990,15 +984,19 @@ protected void updateStrokeColor() {
990984
updateTesselation();
991985

992986
if (0 < tess.lineVertexCount) {
993-
//Arrays.fill(tess.lineColors, 0, tess.lineVertexCount, strokeColor);
994-
Arrays.fill(tess.lineColors.array(), 0, tess.lineVertexCount, strokeColor);
987+
int[] temp = new int[tess.lineVertexCount];
988+
Arrays.fill(temp, 0, tess.lineVertexCount, strokeColor);
989+
tess.lineColors.rewind();
990+
tess.lineColors.put(temp);
995991
modifiedLineColors = true;
996992
modified();
997993
}
998994

999995
if (0 < tess.pointVertexCount) {
1000-
//Arrays.fill(tess.pointColors, 0, tess.pointVertexCount, strokeColor);
1001-
Arrays.fill(tess.pointColors.array(), 0, tess.pointVertexCount, strokeColor);
996+
int[] temp = new int[tess.pointVertexCount];
997+
Arrays.fill(temp, 0, tess.pointVertexCount, strokeColor);
998+
tess.pointColors.rewind();
999+
tess.pointColors.put(temp);
10021000
modifiedPointColors = true;
10031001
modified();
10041002
}
@@ -1117,11 +1115,10 @@ protected void updateTintColor() {
11171115

11181116
updateTesselation();
11191117

1120-
//Arrays.fill(tess.fillColors, 0, tess.pointVertexCount, tintColor);
11211118
int[] temp = new int[tess.fillVertexCount];
11221119
Arrays.fill(temp, 0, tess.fillVertexCount, tintColor);
1123-
tess.fillColors.position(0);
1124-
tess.fillColors.put(temp);
1120+
tess.fillColors.rewind();
1121+
tess.fillColors.put(temp);
11251122

11261123
modifiedFillColors = true;
11271124
modified();
@@ -1180,9 +1177,11 @@ protected void updateAmbientColor() {
11801177
}
11811178

11821179
updateTesselation();
1183-
1184-
//Arrays.fill(tess.fillAmbient, 0, tess.fillVertexCount, ambientColor);
1185-
Arrays.fill(tess.fillAmbient.array(), 0, tess.fillVertexCount, ambientColor);
1180+
1181+
int[] temp = new int[tess.fillVertexCount];
1182+
Arrays.fill(temp, 0, tess.fillVertexCount, ambientColor);
1183+
tess.fillAmbient.rewind();
1184+
tess.fillAmbient.put(temp);
11861185

11871186
modifiedFillAmbient = true;
11881187
modified();
@@ -1243,8 +1242,10 @@ protected void updateSpecularColor() {
12431242

12441243
updateTesselation();
12451244

1246-
//Arrays.fill(tess.fillSpecular, 0, tess.fillVertexCount, specularColor);
1247-
Arrays.fill(tess.fillSpecular.array(), 0, tess.fillVertexCount, specularColor);
1245+
int[] temp = new int[tess.fillVertexCount];
1246+
Arrays.fill(temp, 0, tess.fillVertexCount, specularColor);
1247+
tess.fillSpecular.rewind();
1248+
tess.fillSpecular.put(temp);
12481249

12491250
modifiedFillSpecular = true;
12501251
modified();
@@ -1305,8 +1306,10 @@ protected void updateEmissiveColor() {
13051306

13061307
updateTesselation();
13071308

1308-
//Arrays.fill(tess.fillEmissive, 0, tess.fillVertexCount, emissiveColor);
1309-
Arrays.fill(tess.fillEmissive.array(), 0, tess.fillVertexCount, emissiveColor);
1309+
int[] temp = new int[tess.fillVertexCount];
1310+
Arrays.fill(temp, 0, tess.fillVertexCount, emissiveColor);
1311+
tess.fillEmissive.rewind();
1312+
tess.fillEmissive.put(temp);
13101313

13111314
modifiedFillEmissive = true;
13121315
modified();
@@ -1337,8 +1340,10 @@ protected void updateShininessFactor() {
13371340

13381341
updateTesselation();
13391342

1340-
//Arrays.fill(tess.fillShininess, 0, tess.fillVertexCount, shininess);
1341-
Arrays.fill(tess.fillShininess.array(), 0, tess.fillVertexCount, shininess);
1343+
float[] temp = new float[tess.fillVertexCount];
1344+
Arrays.fill(temp, 0, tess.fillVertexCount, shininess);
1345+
tess.fillShininess.rewind();
1346+
tess.fillShininess.put(temp);
13421347

13431348
modifiedFillShininess = true;
13441349
modified();
@@ -2882,7 +2887,7 @@ protected void aggregate() {
28822887
// level of the shape hierarchy.
28832888
protected void aggregateImpl() {
28842889
if (family == GROUP) {
2885-
tess.reset();
2890+
tess.clear();
28862891

28872892
boolean firstGeom = true;
28882893
boolean firstStroke = true;

java/libraries/lwjgl/src/processing/lwjgl/PTexture.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public class PTexture implements PConstants {
6868
protected int[] tempPixels = null;
6969
protected PFramebuffer tempFbo = null;
7070

71+
protected IntBuffer texels;
72+
7173
protected Object bufferSource;
7274
protected LinkedList<BufferData> bufferCache = null;
7375
protected Method disposeBufferMethod;
@@ -845,14 +847,21 @@ protected void copyTexels(PTexture tex, int x, int y, int w, int h, boolean scal
845847
protected void setTexels(int[] pix, int x, int y, int w, int h) {
846848
setTexels(pix, 0, x, y, w, h);
847849
}
848-
849-
protected void setTexels(int[] pix, int level, int x, int y, int w, int h) {
850-
pgl.glTexSubImage2D(glTarget, level, x, y, w, h, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, IntBuffer.wrap(pix));
851-
}
852850

853851
protected void setTexels(IntBuffer buffer, int x, int y, int w, int h) {
854852
setTexels(buffer, 0, x, y, w, h);
855-
}
853+
}
854+
855+
protected void setTexels(int[] pix, int level, int x, int y, int w, int h) {
856+
if (texels == null || texels.capacity() != width * height) {
857+
texels = pgl.createIntBuffer(width * height);
858+
}
859+
texels.position(0);
860+
texels.limit(pix.length);
861+
texels.put(pix);
862+
texels.flip();
863+
pgl.glTexSubImage2D(glTarget, level, x, y, w, h, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, texels);
864+
}
856865

857866
protected void setTexels(IntBuffer buffer, int level, int x, int y, int w, int h) {
858867
pgl.glTexSubImage2D(glTarget, level, x, y, w, h, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, buffer);

0 commit comments

Comments
 (0)