Skip to content

Commit 5b71fe7

Browse files
committed
Do not use stack to allocate high capacity buffers
1 parent 26e310e commit 5b71fe7

File tree

22 files changed

+1307
-1224
lines changed
  • chapter-03/src/main/java/org/lwjglb/engine/graph
  • chapter-04/src/main/java/org/lwjglb/engine/graph
  • chapter-05/src/main/java/org/lwjglb/engine/graph
  • chapter-06/src/main/java/org/lwjglb/engine/graph
  • chapter-07/src/main/java/org/lwjglb/engine/graph
  • chapter-08/src/main/java/org/lwjglb/engine/graph
  • chapter-09/src/main/java/org/lwjglb/engine/graph
  • chapter-10/src/main/java/org/lwjglb/engine/graph
  • chapter-11/src/main/java/org/lwjglb/engine/graph
  • chapter-12/src/main/java/org/lwjglb/engine/graph
  • chapter-13/src/main/java/org/lwjglb/engine/graph
  • chapter-14/src/main/java/org/lwjglb/engine/graph
  • chapter-15/src/main/java/org/lwjglb/engine/graph
  • chapter-16/src/main/java/org/lwjglb/engine/graph
  • chapter-17/src/main/java/org/lwjglb/engine/graph
  • chapter-18/src/main/java/org/lwjglb/engine/graph
  • chapter-19/src/main/java/org/lwjglb/engine/graph
  • chapter-20/src/main/java/org/lwjglb/engine/graph
  • chapter-21/src/main/java/org/lwjglb/engine/graph

22 files changed

+1307
-1224
lines changed

chapter-03/src/main/java/org/lwjglb/engine/graph/Mesh.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.lwjglb.engine.graph;
22

33
import org.lwjgl.opengl.GL30;
4-
import org.lwjgl.system.MemoryStack;
4+
import org.lwjgl.system.*;
55

66
import java.nio.FloatBuffer;
77
import java.util.*;
@@ -15,26 +15,26 @@ public class Mesh {
1515
private List<Integer> vboIdList;
1616

1717
public Mesh(float[] positions, int numVertices) {
18-
try (MemoryStack stack = MemoryStack.stackPush()) {
19-
this.numVertices = numVertices;
20-
vboIdList = new ArrayList<>();
21-
22-
vaoId = glGenVertexArrays();
23-
glBindVertexArray(vaoId);
24-
25-
// Positions VBO
26-
int vboId = glGenBuffers();
27-
vboIdList.add(vboId);
28-
FloatBuffer positionsBuffer = stack.callocFloat(positions.length);
29-
positionsBuffer.put(0, positions);
30-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
31-
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
32-
glEnableVertexAttribArray(0);
33-
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
34-
35-
glBindBuffer(GL_ARRAY_BUFFER, 0);
36-
glBindVertexArray(0);
37-
}
18+
this.numVertices = numVertices;
19+
vboIdList = new ArrayList<>();
20+
21+
vaoId = glGenVertexArrays();
22+
glBindVertexArray(vaoId);
23+
24+
// Positions VBO
25+
int vboId = glGenBuffers();
26+
vboIdList.add(vboId);
27+
FloatBuffer positionsBuffer = MemoryUtil.memCallocFloat(positions.length);
28+
positionsBuffer.put(0, positions);
29+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
30+
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
31+
glEnableVertexAttribArray(0);
32+
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
33+
34+
glBindBuffer(GL_ARRAY_BUFFER, 0);
35+
glBindVertexArray(0);
36+
37+
MemoryUtil.memFree(positionsBuffer);
3838
}
3939

4040
public void cleanup() {

chapter-04/src/main/java/org/lwjglb/engine/graph/Mesh.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.lwjglb.engine.graph;
22

33
import org.lwjgl.opengl.GL30;
4-
import org.lwjgl.system.MemoryStack;
4+
import org.lwjgl.system.*;
55

66
import java.nio.*;
77
import java.util.*;
@@ -15,44 +15,46 @@ public class Mesh {
1515
private List<Integer> vboIdList;
1616

1717
public Mesh(float[] positions, float[] colors, int[] indices) {
18-
try (MemoryStack stack = MemoryStack.stackPush()) {
19-
numVertices = indices.length;
20-
vboIdList = new ArrayList<>();
18+
numVertices = indices.length;
19+
vboIdList = new ArrayList<>();
2120

22-
vaoId = glGenVertexArrays();
23-
glBindVertexArray(vaoId);
21+
vaoId = glGenVertexArrays();
22+
glBindVertexArray(vaoId);
2423

25-
// Positions VBO
26-
int vboId = glGenBuffers();
27-
vboIdList.add(vboId);
28-
FloatBuffer positionsBuffer = stack.callocFloat(positions.length);
29-
positionsBuffer.put(0, positions);
30-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
31-
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
32-
glEnableVertexAttribArray(0);
33-
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
24+
// Positions VBO
25+
int vboId = glGenBuffers();
26+
vboIdList.add(vboId);
27+
FloatBuffer positionsBuffer = MemoryUtil.memCallocFloat(positions.length);
28+
positionsBuffer.put(0, positions);
29+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
30+
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
31+
glEnableVertexAttribArray(0);
32+
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
3433

35-
// Color VBO
36-
vboId = glGenBuffers();
37-
vboIdList.add(vboId);
38-
FloatBuffer colorsBuffer = stack.callocFloat(colors.length);
39-
colorsBuffer.put(0, colors);
40-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
41-
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
42-
glEnableVertexAttribArray(1);
43-
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
34+
// Color VBO
35+
vboId = glGenBuffers();
36+
vboIdList.add(vboId);
37+
FloatBuffer colorsBuffer = MemoryUtil.memCallocFloat(colors.length);
38+
colorsBuffer.put(0, colors);
39+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
40+
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
41+
glEnableVertexAttribArray(1);
42+
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
4443

45-
// Index VBO
46-
vboId = glGenBuffers();
47-
vboIdList.add(vboId);
48-
IntBuffer indicesBuffer = stack.callocInt(indices.length);
49-
indicesBuffer.put(0, indices);
50-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
51-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
44+
// Index VBO
45+
vboId = glGenBuffers();
46+
vboIdList.add(vboId);
47+
IntBuffer indicesBuffer = MemoryUtil.memCallocInt(indices.length);
48+
indicesBuffer.put(0, indices);
49+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
50+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
5251

53-
glBindBuffer(GL_ARRAY_BUFFER, 0);
54-
glBindVertexArray(0);
55-
}
52+
glBindBuffer(GL_ARRAY_BUFFER, 0);
53+
glBindVertexArray(0);
54+
55+
MemoryUtil.memFree(positionsBuffer);
56+
MemoryUtil.memFree(colorsBuffer);
57+
MemoryUtil.memFree(indicesBuffer);
5658
}
5759

5860
public void cleanup() {

chapter-05/src/main/java/org/lwjglb/engine/graph/Mesh.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.lwjglb.engine.graph;
22

33
import org.lwjgl.opengl.GL30;
4-
import org.lwjgl.system.MemoryStack;
4+
import org.lwjgl.system.*;
55

66
import java.nio.*;
77
import java.util.*;
@@ -15,44 +15,46 @@ public class Mesh {
1515
private List<Integer> vboIdList;
1616

1717
public Mesh(float[] positions, float[] colors, int[] indices) {
18-
try (MemoryStack stack = MemoryStack.stackPush()) {
19-
numVertices = indices.length;
20-
vboIdList = new ArrayList<>();
18+
numVertices = indices.length;
19+
vboIdList = new ArrayList<>();
2120

22-
vaoId = glGenVertexArrays();
23-
glBindVertexArray(vaoId);
21+
vaoId = glGenVertexArrays();
22+
glBindVertexArray(vaoId);
2423

25-
// Positions VBO
26-
int vboId = glGenBuffers();
27-
vboIdList.add(vboId);
28-
FloatBuffer positionsBuffer = stack.callocFloat(positions.length);
29-
positionsBuffer.put(0, positions);
30-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
31-
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
32-
glEnableVertexAttribArray(0);
33-
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
24+
// Positions VBO
25+
int vboId = glGenBuffers();
26+
vboIdList.add(vboId);
27+
FloatBuffer positionsBuffer = MemoryUtil.memCallocFloat(positions.length);
28+
positionsBuffer.put(0, positions);
29+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
30+
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
31+
glEnableVertexAttribArray(0);
32+
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
3433

35-
// Color VBO
36-
vboId = glGenBuffers();
37-
vboIdList.add(vboId);
38-
FloatBuffer colorsBuffer = stack.callocFloat(colors.length);
39-
colorsBuffer.put(0, colors);
40-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
41-
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
42-
glEnableVertexAttribArray(1);
43-
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
34+
// Color VBO
35+
vboId = glGenBuffers();
36+
vboIdList.add(vboId);
37+
FloatBuffer colorsBuffer = MemoryUtil.memCallocFloat(colors.length);
38+
colorsBuffer.put(0, colors);
39+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
40+
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
41+
glEnableVertexAttribArray(1);
42+
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
4443

45-
// Index VBO
46-
vboId = glGenBuffers();
47-
vboIdList.add(vboId);
48-
IntBuffer indicesBuffer = stack.callocInt(indices.length);
49-
indicesBuffer.put(0, indices);
50-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
51-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
44+
// Index VBO
45+
vboId = glGenBuffers();
46+
vboIdList.add(vboId);
47+
IntBuffer indicesBuffer = MemoryUtil.memCallocInt(indices.length);
48+
indicesBuffer.put(0, indices);
49+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
50+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
5251

53-
glBindBuffer(GL_ARRAY_BUFFER, 0);
54-
glBindVertexArray(0);
55-
}
52+
glBindBuffer(GL_ARRAY_BUFFER, 0);
53+
glBindVertexArray(0);
54+
55+
MemoryUtil.memFree(positionsBuffer);
56+
MemoryUtil.memFree(colorsBuffer);
57+
MemoryUtil.memFree(indicesBuffer);
5658
}
5759

5860
public void cleanup() {

chapter-06/src/main/java/org/lwjglb/engine/graph/Mesh.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.lwjglb.engine.graph;
22

33
import org.lwjgl.opengl.GL30;
4-
import org.lwjgl.system.MemoryStack;
4+
import org.lwjgl.system.*;
55

66
import java.nio.*;
77
import java.util.*;
@@ -15,44 +15,46 @@ public class Mesh {
1515
private List<Integer> vboIdList;
1616

1717
public Mesh(float[] positions, float[] colors, int[] indices) {
18-
try (MemoryStack stack = MemoryStack.stackPush()) {
19-
numVertices = indices.length;
20-
vboIdList = new ArrayList<>();
18+
numVertices = indices.length;
19+
vboIdList = new ArrayList<>();
2120

22-
vaoId = glGenVertexArrays();
23-
glBindVertexArray(vaoId);
21+
vaoId = glGenVertexArrays();
22+
glBindVertexArray(vaoId);
2423

25-
// Positions VBO
26-
int vboId = glGenBuffers();
27-
vboIdList.add(vboId);
28-
FloatBuffer positionsBuffer = stack.callocFloat(positions.length);
29-
positionsBuffer.put(0, positions);
30-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
31-
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
32-
glEnableVertexAttribArray(0);
33-
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
24+
// Positions VBO
25+
int vboId = glGenBuffers();
26+
vboIdList.add(vboId);
27+
FloatBuffer positionsBuffer = MemoryUtil.memCallocFloat(positions.length);
28+
positionsBuffer.put(0, positions);
29+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
30+
glBufferData(GL_ARRAY_BUFFER, positionsBuffer, GL_STATIC_DRAW);
31+
glEnableVertexAttribArray(0);
32+
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
3433

35-
// Color VBO
36-
vboId = glGenBuffers();
37-
vboIdList.add(vboId);
38-
FloatBuffer colorsBuffer = stack.callocFloat(colors.length);
39-
colorsBuffer.put(0, colors);
40-
glBindBuffer(GL_ARRAY_BUFFER, vboId);
41-
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
42-
glEnableVertexAttribArray(1);
43-
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
34+
// Color VBO
35+
vboId = glGenBuffers();
36+
vboIdList.add(vboId);
37+
FloatBuffer colorsBuffer = MemoryUtil.memCallocFloat(colors.length);
38+
colorsBuffer.put(0, colors);
39+
glBindBuffer(GL_ARRAY_BUFFER, vboId);
40+
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
41+
glEnableVertexAttribArray(1);
42+
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
4443

45-
// Index VBO
46-
vboId = glGenBuffers();
47-
vboIdList.add(vboId);
48-
IntBuffer indicesBuffer = stack.callocInt(indices.length);
49-
indicesBuffer.put(0, indices);
50-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
51-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
44+
// Index VBO
45+
vboId = glGenBuffers();
46+
vboIdList.add(vboId);
47+
IntBuffer indicesBuffer = MemoryUtil.memCallocInt(indices.length);
48+
indicesBuffer.put(0, indices);
49+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
50+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
5251

53-
glBindBuffer(GL_ARRAY_BUFFER, 0);
54-
glBindVertexArray(0);
55-
}
52+
glBindBuffer(GL_ARRAY_BUFFER, 0);
53+
glBindVertexArray(0);
54+
55+
MemoryUtil.memFree(positionsBuffer);
56+
MemoryUtil.memFree(colorsBuffer);
57+
MemoryUtil.memFree(indicesBuffer);
5658
}
5759

5860
public void cleanup() {

0 commit comments

Comments
 (0)