Using buffers instead of arrays in P3D library as well

This commit is contained in:
codeanticode
2012-03-14 20:24:03 +00:00
parent a71aa9bfd0
commit 6ba65dc2e3
5 changed files with 1228 additions and 901 deletions

View File

@@ -26,6 +26,7 @@ package processing.opengl;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.nativewindow.GraphicsConfigurationFactory;
@@ -428,8 +429,8 @@ public class PGL {
return gl.glGetString(name);
}
public void glGetIntegerv(int name, int[] values, int offset) {
gl.glGetIntegerv(name, values, offset);
public void glGetIntegerv(int name, IntBuffer values) {
gl.glGetIntegerv(name, values);
}
///////////////////////////////////////////////////////////////////////////////////
@@ -497,12 +498,14 @@ public class PGL {
// Textures
public void glGenTextures(int n, int[] ids, int offset) {
gl.glGenTextures(n, ids, offset);
public void glGenTextures(int n, IntBuffer ids) {
ids.limit(n);
gl.glGenTextures(n, ids);
}
public void glDeleteTextures(int n, int[] ids, int offset) {
gl.glDeleteTextures(n, ids, offset);
public void glDeleteTextures(int n, IntBuffer ids) {
ids.limit(n);
gl.glDeleteTextures(n, ids);
}
public void glActiveTexture(int unit) {
@@ -533,12 +536,14 @@ public class PGL {
// Vertex Buffers
public void glGenBuffers(int n, int[] ids, int offset) {
gl.glGenBuffers(n, ids, offset);
public void glGenBuffers(int n, IntBuffer ids) {
ids.limit(n);
gl.glGenBuffers(n, ids);
}
public void glDeleteBuffers(int n, int[] ids, int offset) {
gl.glDeleteBuffers(n, ids, offset);
public void glDeleteBuffers(int n, IntBuffer ids) {
ids.limit(n);
gl.glDeleteBuffers(n, ids);
}
public void glBindBuffer(int target, int id) {
@@ -588,20 +593,24 @@ public class PGL {
// Framebuffers, renderbuffers
public void glGenFramebuffers(int n, int[] ids, int offset) {
gl.glGenFramebuffers(n, ids, offset);
public void glGenFramebuffers(int n, IntBuffer ids) {
ids.limit(n);
gl.glGenFramebuffers(n, ids);
}
public void glDeleteFramebuffers(int n, int[] ids, int offset) {
gl.glDeleteFramebuffers(n, ids, offset);
public void glDeleteFramebuffers(int n, IntBuffer ids) {
ids.limit(n);
gl.glDeleteFramebuffers(n, ids);
}
public void glGenRenderbuffers(int n, int[] ids, int offset) {
gl.glGenRenderbuffers(n, ids, offset);
public void glGenRenderbuffers(int n, IntBuffer ids) {
ids.limit(n);
gl.glGenRenderbuffers(n, ids);
}
public void glDeleteRenderbuffers(int n, int[] ids, int offset) {
gl.glDeleteRenderbuffers(n, ids, offset);
public void glDeleteRenderbuffers(int n, IntBuffer ids) {
ids.limit(n);
gl.glDeleteRenderbuffers(n, ids);
}
public void glBindFramebuffer(int target, int id) {
@@ -696,32 +705,32 @@ public class PGL {
gl2.glUniform4f(loc, value0, value1, value2, value3);
}
public void glUniform1fv(int loc, int count, float[] v, int offset) {
gl2.glUniform1fv(loc, count, v, offset);
public void glUniform1fv(int loc, int count, FloatBuffer v) {
gl2.glUniform1fv(loc, count, v);
}
public void glUniform2fv(int loc, int count, float[] v, int offset) {
gl2.glUniform2fv(loc, count, v, offset);
public void glUniform2fv(int loc, int count, FloatBuffer v) {
gl2.glUniform2fv(loc, count, v);
}
public void glUniform3fv(int loc, int count, float[] v, int offset) {
gl2.glUniform3fv(loc, count, v, offset);
public void glUniform3fv(int loc, int count, FloatBuffer v) {
gl2.glUniform3fv(loc, count, v);
}
public void glUniform4fv(int loc, int count, float[] v, int offset) {
gl2.glUniform4fv(loc, count, v, offset);
public void glUniform4fv(int loc, int count, FloatBuffer v) {
gl2.glUniform4fv(loc, count, v);
}
public void glUniformMatrix2fv(int loc, int count, boolean transpose, float[] mat, int offset) {
gl2.glUniformMatrix2fv(loc, count, transpose, mat, offset);
public void glUniformMatrix2fv(int loc, int count, boolean transpose, FloatBuffer mat) {
gl2.glUniformMatrix2fv(loc, count, transpose, mat);
}
public void glUniformMatrix3fv(int loc, int count, boolean transpose, float[] mat, int offset) {
gl2.glUniformMatrix3fv(loc, count, transpose, mat, offset);
public void glUniformMatrix3fv(int loc, int count, boolean transpose, FloatBuffer mat) {
gl2.glUniformMatrix3fv(loc, count, transpose, mat);
}
public void glUniformMatrix4fv(int loc, int count, boolean transpose, float[] mat, int offset) {
gl2.glUniformMatrix4fv(loc, count, transpose, mat, offset);
public void glUniformMatrix4fv(int loc, int count, boolean transpose, FloatBuffer mat) {
gl2.glUniformMatrix4fv(loc, count, transpose, mat);
}
public void glVertexAttrib1f(int loc, float value) {
@@ -916,6 +925,14 @@ public class PGL {
// Utility functions
public FloatBuffer createFloatBuffer(int size) {
return FloatBuffer.allocate(size);
}
public IntBuffer createIntBuffer(int size) {
return IntBuffer.allocate(size);
}
public boolean contextIsCurrent(Context other) {
return other.same(context);
}
@@ -933,8 +950,8 @@ public class PGL {
}
public void initTexture(int target, int width, int height, int format, int type) {
int[] texels = new int[width * height];
gl.glTexSubImage2D(target, 0, 0, 0, width, height, format, type, IntBuffer.wrap(texels));
IntBuffer texels = createIntBuffer(width * height);
gl.glTexSubImage2D(target, 0, 0, 0, width, height, format, type, texels);
}
public String getShaderLog(int id) {

View File

@@ -24,9 +24,9 @@
package processing.opengl;
import processing.core.*;
import java.io.IOException;
import java.net.URL;
import java.nio.FloatBuffer;
/**
* This class encapsulates a GLSL shader program, including a vertex
@@ -48,6 +48,14 @@ public class PShader {
protected int vertexShader;
protected int fragmentShader;
protected FloatBuffer vec1f;
protected FloatBuffer vec2f;
protected FloatBuffer vec3f;
protected FloatBuffer vec4f;
protected FloatBuffer mat2x2;
protected FloatBuffer mat3x3;
protected FloatBuffer mat4x4;
public PShader() {
parent = null;
pg = null;
@@ -218,49 +226,91 @@ public class PShader {
public void set1FloatVecUniform(int loc, float[] vec) {
if (-1 < loc) {
pgl.glUniform1fv(loc, vec.length, vec, 0);
if (vec1f == null || vec1f.capacity() != vec.length) {
vec1f = pgl.createFloatBuffer(vec.length);
}
vec1f.rewind();
vec1f.put(vec);
vec1f.flip();
pgl.glUniform1fv(loc, vec.length, vec1f);
}
}
public void set2FloatVecUniform(int loc, float[] vec) {
if (-1 < loc) {
pgl.glUniform2fv(loc, vec.length / 2, vec, 0);
if (vec2f == null || vec2f.capacity() != vec.length) {
vec2f = pgl.createFloatBuffer(vec.length);
}
vec2f.rewind();
vec2f.put(vec);
vec2f.flip();
pgl.glUniform2fv(loc, vec.length / 2, vec2f);
}
}
public void set3FloatVecUniform(int loc, float[] vec) {
if (-1 < loc) {
pgl.glUniform3fv(loc, vec.length / 3, vec, 0);
if (vec3f == null || vec3f.capacity() != vec.length) {
vec3f = pgl.createFloatBuffer(vec.length);
}
vec3f.rewind();
vec3f.put(vec);
vec3f.flip();
pgl.glUniform3fv(loc, vec.length / 3, vec3f);
}
}
public void set4FloatVecUniform(int loc, float[] vec) {
if (-1 < loc) {
pgl.glUniform4fv(loc, vec.length / 4, vec, 0);
if (vec4f == null || vec4f.capacity() != vec.length) {
vec4f = pgl.createFloatBuffer(vec.length);
}
vec4f.rewind();
vec4f.put(vec);
vec4f.flip();
pgl.glUniform4fv(loc, vec.length / 4, vec4f);
}
}
public void set2x2MatUniform(int loc, float[] mat) {
if (-1 < loc) {
pgl.glUniformMatrix2fv(loc, 1, false, mat, 0);
if (mat2x2 == null) {
mat2x2 = pgl.createFloatBuffer(2 * 2);
}
mat2x2.rewind();
mat2x2.put(mat);
mat2x2.flip();
pgl.glUniformMatrix2fv(loc, 1, false, mat2x2);
}
}
public void set3x3MatUniform(int loc, float[] mat) {
if (-1 < loc) {
pgl.glUniformMatrix3fv(loc, 1, false, mat, 0);
if (mat3x3 == null) {
mat3x3 = pgl.createFloatBuffer(3 * 3);
}
mat3x3.rewind();
mat3x3.put(mat);
mat3x3.flip();
pgl.glUniformMatrix3fv(loc, 1, false, mat3x3);
}
}
public void set4x4MatUniform(int loc, float[] mat) {
if (-1 < loc) {
pgl.glUniformMatrix4fv(loc, 1, false, mat, 0);
if (mat4x4 == null) {
mat4x4 = pgl.createFloatBuffer(4 * 4);
}
mat4x4.rewind();
mat4x4.put(mat);
mat4x4.flip();
pgl.glUniformMatrix4fv(loc, 1, false, mat4x4);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -27,7 +27,6 @@ import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PGraphics;
import processing.core.PImage;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
@@ -68,6 +67,8 @@ public class PTexture implements PConstants {
protected int[] tempPixels = null;
protected PFramebuffer tempFbo = null;
protected IntBuffer texels;
protected Object bufferSource;
protected LinkedList<BufferData> bufferCache = null;
protected Method disposeBufferMethod;
@@ -845,14 +846,21 @@ public class PTexture implements PConstants {
protected void setTexels(int[] pix, int x, int y, int w, int h) {
setTexels(pix, 0, x, y, w, h);
}
protected void setTexels(int[] pix, int level, int x, int y, int w, int h) {
pgl.glTexSubImage2D(glTarget, level, x, y, w, h, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, IntBuffer.wrap(pix));
}
protected void setTexels(IntBuffer buffer, int x, int y, int w, int h) {
setTexels(buffer, 0, x, y, w, h);
}
}
protected void setTexels(int[] pix, int level, int x, int y, int w, int h) {
if (texels == null || texels.capacity() != width * height) {
texels = pgl.createIntBuffer(width * height);
}
texels.position(0);
texels.limit(pix.length);
texels.put(pix);
texels.flip();
pgl.glTexSubImage2D(glTarget, level, x, y, w, h, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, texels);
}
protected void setTexels(IntBuffer buffer, int level, int x, int y, int w, int h) {
pgl.glTexSubImage2D(glTarget, level, x, y, w, h, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, buffer);