mirror of
https://github.com/processing/processing4.git
synced 2026-05-09 12:22:43 +02:00
Updates in the PShader API
This commit is contained in:
@@ -60,28 +60,28 @@ import com.jogamp.opengl.util.AnimatorBase;
|
||||
*/
|
||||
public class PGL {
|
||||
// The two windowing toolkits available to use in JOGL:
|
||||
public static int AWT = 0; // http://jogamp.org/wiki/index.php/Using_JOGL_in_AWT_SWT_and_Swing
|
||||
public static int NEWT = 1; // http://jogamp.org/jogl/doc/NEWT-Overview.html
|
||||
public static final int AWT = 0; // http://jogamp.org/wiki/index.php/Using_JOGL_in_AWT_SWT_and_Swing
|
||||
public static final int NEWT = 1; // http://jogamp.org/jogl/doc/NEWT-Overview.html
|
||||
|
||||
public static int toolkit = AWT;
|
||||
|
||||
/** Size of a short (in bytes). */
|
||||
static final int SIZEOF_SHORT = Short.SIZE / 8;
|
||||
public static final int SIZEOF_SHORT = Short.SIZE / 8;
|
||||
|
||||
/** Size of an int (in bytes). */
|
||||
static final int SIZEOF_INT = Integer.SIZE / 8;
|
||||
public static final int SIZEOF_INT = Integer.SIZE / 8;
|
||||
|
||||
/** Size of a float (in bytes). */
|
||||
static final int SIZEOF_FLOAT = Float.SIZE / 8;
|
||||
public static final int SIZEOF_FLOAT = Float.SIZE / 8;
|
||||
|
||||
/** Size of a byte (in bytes). */
|
||||
static final int SIZEOF_BYTE = Byte.SIZE / 8;
|
||||
public static final int SIZEOF_BYTE = Byte.SIZE / 8;
|
||||
|
||||
/** Size of a vertex index. */
|
||||
static final int SIZEOF_INDEX = SIZEOF_SHORT;
|
||||
public static final int SIZEOF_INDEX = SIZEOF_SHORT;
|
||||
|
||||
/** Type of a vertex index. */
|
||||
static final int INDEX_TYPE = GL.GL_UNSIGNED_SHORT;
|
||||
public static final int INDEX_TYPE = GL.GL_UNSIGNED_SHORT;
|
||||
|
||||
/** Initial sizes for arrays of input and tessellated data. */
|
||||
public static final int DEFAULT_IN_VERTICES = 64;
|
||||
@@ -122,13 +122,13 @@ public class PGL {
|
||||
public static final int MAX_CAPS_JOINS_LENGTH = 5000;
|
||||
|
||||
/** Minimum array size to use arrayCopy method(). **/
|
||||
static protected final int MIN_ARRAYCOPY_SIZE = 2;
|
||||
protected static final int MIN_ARRAYCOPY_SIZE = 2;
|
||||
|
||||
/** Enables/disables mipmap use. **/
|
||||
static protected final boolean MIPMAPS_ENABLED = true;
|
||||
protected static final boolean MIPMAPS_ENABLED = true;
|
||||
|
||||
/** Machine Epsilon for float precision. **/
|
||||
static public float FLOAT_EPS = Float.MIN_VALUE;
|
||||
public static float FLOAT_EPS = Float.MIN_VALUE;
|
||||
// Calculation of the Machine Epsilon for float precision. From:
|
||||
// http://en.wikipedia.org/wiki/Machine_epsilon#Approximation_using_Java
|
||||
static {
|
||||
@@ -145,7 +145,7 @@ public class PGL {
|
||||
* Set to true if the host system is big endian (PowerPC, MIPS, SPARC), false
|
||||
* if little endian (x86 Intel for Mac or PC).
|
||||
*/
|
||||
static public boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
public static boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
|
||||
protected static final String SHADER_PREPROCESSOR_DIRECTIVE = "#ifdef GL_ES\n" +
|
||||
"precision mediump float;\n" +
|
||||
@@ -1642,7 +1642,7 @@ public class PGL {
|
||||
}
|
||||
|
||||
if (texData == null) {
|
||||
texData = ByteBuffer.allocateDirect(texCoords.length * SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
texData = allocateDirectFloatBuffer(texCoords.length);
|
||||
}
|
||||
|
||||
if (0 < tex2DShaderProgram) {
|
||||
@@ -1739,7 +1739,7 @@ public class PGL {
|
||||
}
|
||||
|
||||
if (texData == null) {
|
||||
texData = ByteBuffer.allocateDirect(texCoords.length * SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
texData = allocateDirectFloatBuffer(texCoords.length);
|
||||
}
|
||||
|
||||
if (0 < texRectShaderProgram) {
|
||||
@@ -1830,7 +1830,7 @@ public class PGL {
|
||||
rectVertLoc = glGetAttribLocation(rectShaderProgram, "inVertex");
|
||||
rectColorLoc = glGetUniformLocation(rectShaderProgram, "rectColor");
|
||||
}
|
||||
rectData = ByteBuffer.allocateDirect(rectCoords.length * SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
rectData = allocateDirectFloatBuffer(rectCoords.length);
|
||||
loadedRectShader = true;
|
||||
rectShaderContext = context;
|
||||
}
|
||||
@@ -1927,7 +1927,7 @@ public class PGL {
|
||||
|
||||
|
||||
// bit shifting this might be more efficient
|
||||
static public int nextPowerOfTwo(int val) {
|
||||
public static int nextPowerOfTwo(int val) {
|
||||
int ret = 1;
|
||||
while (ret < val) {
|
||||
ret <<= 1;
|
||||
@@ -1940,7 +1940,7 @@ public class PGL {
|
||||
* Converts input native OpenGL value (RGBA on big endian, ABGR on little
|
||||
* endian) to Java ARGB.
|
||||
*/
|
||||
static public int nativeToJavaARGB(int color) {
|
||||
public static int nativeToJavaARGB(int color) {
|
||||
if (BIG_ENDIAN) { // RGBA to ARGB
|
||||
return (color & 0xff000000) |
|
||||
((color >> 8) & 0x00ffffff);
|
||||
@@ -1959,7 +1959,7 @@ public class PGL {
|
||||
* It also rearranges the elements in the array so that the image is flipped
|
||||
* vertically.
|
||||
*/
|
||||
static public void nativeToJavaARGB(int[] pixels, int width, int height) {
|
||||
public static void nativeToJavaARGB(int[] pixels, int width, int height) {
|
||||
int index = 0;
|
||||
int yindex = (height - 1) * width;
|
||||
for (int y = 0; y < height / 2; y++) {
|
||||
@@ -2018,7 +2018,7 @@ public class PGL {
|
||||
* endian) to Java RGB, so that the alpha component of the result is set
|
||||
* to opaque (255).
|
||||
*/
|
||||
static public int nativeToJavaRGB(int color) {
|
||||
public static int nativeToJavaRGB(int color) {
|
||||
if (BIG_ENDIAN) { // RGBA to ARGB
|
||||
return ((color << 8) & 0xffffff00) | 0xff;
|
||||
} else { // ABGR to ARGB
|
||||
@@ -2035,7 +2035,7 @@ public class PGL {
|
||||
* so that the alpha component of all pixels is set to opaque (255). It also
|
||||
* rearranges the elements in the array so that the image is flipped vertically.
|
||||
*/
|
||||
static public void nativeToJavaRGB(int[] pixels, int width, int height) {
|
||||
public static void nativeToJavaRGB(int[] pixels, int width, int height) {
|
||||
int index = 0;
|
||||
int yindex = (height - 1) * width;
|
||||
for (int y = 0; y < height / 2; y++) {
|
||||
@@ -2087,7 +2087,7 @@ public class PGL {
|
||||
* Converts input Java ARGB value to native OpenGL format (RGBA on big endian,
|
||||
* BGRA on little endian).
|
||||
*/
|
||||
static public int javaToNativeARGB(int color) {
|
||||
public static int javaToNativeARGB(int color) {
|
||||
if (BIG_ENDIAN) { // ARGB to RGBA
|
||||
return ((color >> 24) & 0xff) |
|
||||
((color << 8) & 0xffffff00);
|
||||
@@ -2106,7 +2106,7 @@ public class PGL {
|
||||
* It also rearranges the elements in the array so that the image is flipped
|
||||
* vertically.
|
||||
*/
|
||||
static public void javaToNativeARGB(int[] pixels, int width, int height) {
|
||||
public static void javaToNativeARGB(int[] pixels, int width, int height) {
|
||||
int index = 0;
|
||||
int yindex = (height - 1) * width;
|
||||
for (int y = 0; y < height / 2; y++) {
|
||||
@@ -2165,7 +2165,7 @@ public class PGL {
|
||||
* Converts input Java ARGB value to native OpenGL format (RGBA on big endian,
|
||||
* BGRA on little endian), setting alpha component to opaque (255).
|
||||
*/
|
||||
static public int javaToNativeRGB(int color) {
|
||||
public static int javaToNativeRGB(int color) {
|
||||
if (BIG_ENDIAN) { // ARGB to RGBA
|
||||
return ((color << 8) & 0xffffff00) | 0xff;
|
||||
} else { // ARGB to ABGR
|
||||
@@ -2182,7 +2182,7 @@ public class PGL {
|
||||
* while setting alpha component of all pixels to opaque (255). It also rearranges
|
||||
* the elements in the array so that the image is flipped vertically.
|
||||
*/
|
||||
static public void javaToNativeRGB(int[] pixels, int width, int height) {
|
||||
public static void javaToNativeRGB(int[] pixels, int width, int height) {
|
||||
int index = 0;
|
||||
int yindex = (height - 1) * width;
|
||||
for (int y = 0; y < height / 2; y++) {
|
||||
@@ -2288,6 +2288,21 @@ public class PGL {
|
||||
}
|
||||
|
||||
|
||||
public static ByteBuffer allocateDirectByteBuffer(int size) {
|
||||
return ByteBuffer.allocateDirect(size * SIZEOF_BYTE).order(ByteOrder.nativeOrder());
|
||||
}
|
||||
|
||||
|
||||
public static IntBuffer allocateDirectIntBuffer(int size) {
|
||||
return ByteBuffer.allocateDirect(size * SIZEOF_INT).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
}
|
||||
|
||||
|
||||
public static FloatBuffer allocateDirectFloatBuffer(int size) {
|
||||
return ByteBuffer.allocateDirect(size * SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Java specific stuff
|
||||
|
||||
@@ -2456,7 +2456,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
// If the renderer is 2D, then lights should always be false,
|
||||
// so no need to worry about that.
|
||||
PolyShader shader = getPolyShader(lights, tex != null);
|
||||
shader.start();
|
||||
shader.bind();
|
||||
|
||||
int first = texCache.firstCache[i];
|
||||
int last = texCache.lastCache[i];
|
||||
@@ -2489,7 +2489,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
shader.stop();
|
||||
shader.unbind();
|
||||
}
|
||||
texCache.endRender();
|
||||
unbindPolyBuffers();
|
||||
@@ -2598,7 +2598,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
updateLineBuffers();
|
||||
|
||||
LineShader shader = getLineShader();
|
||||
shader.start();
|
||||
shader.bind();
|
||||
|
||||
IndexCache cache = tessGeo.lineIndexCache;
|
||||
for (int n = 0; n < cache.size; n++) {
|
||||
@@ -2615,7 +2615,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
shader.stop();
|
||||
shader.unbind();
|
||||
unbindLineBuffers();
|
||||
}
|
||||
|
||||
@@ -2696,7 +2696,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
updatePointBuffers();
|
||||
|
||||
PointShader shader = getPointShader();
|
||||
shader.start();
|
||||
shader.bind();
|
||||
|
||||
IndexCache cache = tessGeo.pointIndexCache;
|
||||
for (int n = 0; n < cache.size; n++) {
|
||||
@@ -2713,7 +2713,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
shader.stop();
|
||||
shader.unbind();
|
||||
unbindPointBuffers();
|
||||
}
|
||||
|
||||
@@ -5629,7 +5629,68 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public PShader getShader(int kind) {
|
||||
PShader shader;
|
||||
if (kind == PShader.FLAT) {
|
||||
if (polyFlatShader == null) {
|
||||
if (defPolyFlatShader == null) {
|
||||
defPolyFlatShader = new PolyFlatShader(parent, defPolyFlatShaderVertURL, defPolyNoTexShaderFragURL);
|
||||
}
|
||||
polyFlatShader = defPolyFlatShader;
|
||||
}
|
||||
shader = polyFlatShader;
|
||||
} else if (kind == PShader.LIT) {
|
||||
if (polyLightShader == null) {
|
||||
if (defPolyLightShader == null) {
|
||||
defPolyLightShader = new PolyLightShader(parent, defPolyLightShaderVertURL, defPolyNoTexShaderFragURL);
|
||||
}
|
||||
polyLightShader = defPolyLightShader;
|
||||
}
|
||||
shader = polyLightShader;
|
||||
} else if (kind == PShader.TEXTURED) {
|
||||
if (polyTexShader == null) {
|
||||
if (defPolyTexShader == null) {
|
||||
defPolyTexShader = new PolyTexShader(parent, defPolyTexShaderVertURL, defPolyTexShaderFragURL);
|
||||
}
|
||||
polyTexShader = defPolyTexShader;
|
||||
}
|
||||
shader = polyTexShader;
|
||||
} else if (kind == PShader.FULL) {
|
||||
if (polyFullShader == null) {
|
||||
if (defPolyFullShader == null) {
|
||||
defPolyFullShader = new PolyFullShader(parent, defPolyFullShaderVertURL, defPolyTexShaderFragURL);
|
||||
}
|
||||
polyFullShader = defPolyFullShader;
|
||||
}
|
||||
shader = polyFullShader;
|
||||
} else if (kind == PShader.LINE) {
|
||||
if (lineShader == null) {
|
||||
if (defLineShader == null) {
|
||||
defLineShader = new LineShader(parent, defLineShaderVertURL, defLineShaderFragURL);
|
||||
}
|
||||
lineShader = defLineShader;
|
||||
}
|
||||
shader = lineShader;
|
||||
} else if (kind == PShader.POINT) {
|
||||
if (pointShader == null) {
|
||||
if (defPointShader == null) {
|
||||
defPointShader = new PointShader(parent, defPointShaderVertURL, defPointShaderFragURL);
|
||||
}
|
||||
pointShader = defPointShader;
|
||||
}
|
||||
shader = pointShader;
|
||||
} else {
|
||||
PGraphics.showWarning("Wrong shader type");
|
||||
return null;
|
||||
}
|
||||
shader.setRenderer(this);
|
||||
shader.loadAttributes();
|
||||
shader.loadUniforms();
|
||||
return shader;
|
||||
}
|
||||
|
||||
|
||||
public void defaultShader(int kind) {
|
||||
flush(); // Flushing geometry with a different shader.
|
||||
if (kind == PShader.FLAT) {
|
||||
@@ -5806,8 +5867,8 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
setAttribute(inColorLoc, vboId, size, type, true, stride, offset);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
public void bind() {
|
||||
super.bind();
|
||||
|
||||
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
|
||||
@@ -5830,13 +5891,13 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void unbind() {
|
||||
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
|
||||
|
||||
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
|
||||
|
||||
super.stop();
|
||||
super.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5932,8 +5993,8 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
setAttribute(inShineLoc, vboId, size, type, false, stride, offset);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
public void bind() {
|
||||
super.bind();
|
||||
|
||||
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
|
||||
@@ -5976,7 +6037,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void unbind() {
|
||||
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
|
||||
if (-1 < inNormalLoc) pgl.glDisableVertexAttribArray(inNormalLoc);
|
||||
@@ -5988,7 +6049,7 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
|
||||
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
|
||||
|
||||
super.stop();
|
||||
super.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6065,16 +6126,16 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
setFloatUniform(texcoordOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
public void bind() {
|
||||
super.bind();
|
||||
|
||||
if (-1 < inTexcoordLoc) pgl.glEnableVertexAttribArray(inTexcoordLoc);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void unbind() {
|
||||
if (-1 < inTexcoordLoc) pgl.glDisableVertexAttribArray(inTexcoordLoc);
|
||||
|
||||
super.stop();
|
||||
super.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6151,16 +6212,16 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
setFloatUniform(texcoordOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
public void bind() {
|
||||
super.bind();
|
||||
|
||||
if (-1 < inTexcoordLoc) pgl.glEnableVertexAttribArray(inTexcoordLoc);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void unbind() {
|
||||
if (-1 < inTexcoordLoc) pgl.glDisableVertexAttribArray(inTexcoordLoc);
|
||||
|
||||
super.stop();
|
||||
super.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6223,8 +6284,8 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
setAttribute(inAttribLoc, vboId, size, type, false, stride, offset);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
public void bind() {
|
||||
super.bind();
|
||||
|
||||
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
|
||||
@@ -6262,14 +6323,14 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void unbind() {
|
||||
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
|
||||
if (-1 < inAttribLoc) pgl.glDisableVertexAttribArray(inAttribLoc);
|
||||
|
||||
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
|
||||
|
||||
super.stop();
|
||||
super.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6324,8 +6385,8 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
setAttribute(inPointLoc, vboId, size, type, false, stride, offset);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
public void bind() {
|
||||
super.bind();
|
||||
|
||||
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
|
||||
@@ -6349,14 +6410,14 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void unbind() {
|
||||
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
|
||||
if (-1 < inPointLoc) pgl.glDisableVertexAttribArray(inPointLoc);
|
||||
|
||||
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
|
||||
|
||||
super.stop();
|
||||
super.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,10 @@ public class PShader {
|
||||
protected PGL pgl;
|
||||
protected PGL.Context context; // The context that created this shader.
|
||||
|
||||
public int glProgramObjectID;
|
||||
public int glVertexShaderID;
|
||||
public int glFragmentShaderID;
|
||||
|
||||
protected URL vertexURL;
|
||||
protected URL fragmentURL;
|
||||
|
||||
@@ -64,11 +68,7 @@ public class PShader {
|
||||
protected String vertexShaderSource;
|
||||
protected String fragmentShaderSource;
|
||||
|
||||
protected int programObject;
|
||||
protected int vertexShader;
|
||||
protected int fragmentShader;
|
||||
|
||||
protected boolean active;
|
||||
protected boolean bound;
|
||||
|
||||
protected HashMap<Integer, UniformValue> uniformValues = null;
|
||||
|
||||
@@ -83,11 +83,11 @@ public class PShader {
|
||||
this.vertexFilename = null;
|
||||
this.fragmentFilename = null;
|
||||
|
||||
programObject = 0;
|
||||
vertexShader = 0;
|
||||
fragmentShader = 0;
|
||||
glProgramObjectID = 0;
|
||||
glVertexShaderID = 0;
|
||||
glFragmentShaderID = 0;
|
||||
|
||||
active = false;
|
||||
bound = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -118,9 +118,9 @@ public class PShader {
|
||||
this.vertexFilename = vertFilename;
|
||||
this.fragmentFilename = fragFilename;
|
||||
|
||||
programObject = 0;
|
||||
vertexShader = 0;
|
||||
fragmentShader = 0;
|
||||
glProgramObjectID = 0;
|
||||
glVertexShaderID = 0;
|
||||
glFragmentShaderID = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -134,22 +134,22 @@ public class PShader {
|
||||
this.vertexFilename = null;
|
||||
this.fragmentFilename = null;
|
||||
|
||||
programObject = 0;
|
||||
vertexShader = 0;
|
||||
fragmentShader = 0;
|
||||
glProgramObjectID = 0;
|
||||
glVertexShaderID = 0;
|
||||
glFragmentShaderID = 0;
|
||||
}
|
||||
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
if (vertexShader != 0) {
|
||||
pgMain.finalizeGLSLVertShaderObject(vertexShader, context.code());
|
||||
if (glVertexShaderID != 0) {
|
||||
pgMain.finalizeGLSLVertShaderObject(glVertexShaderID, context.code());
|
||||
}
|
||||
if (fragmentShader != 0) {
|
||||
pgMain.finalizeGLSLFragShaderObject(fragmentShader, context.code());
|
||||
if (glFragmentShaderID != 0) {
|
||||
pgMain.finalizeGLSLFragShaderObject(glFragmentShaderID, context.code());
|
||||
}
|
||||
if (programObject != 0) {
|
||||
pgMain.finalizeGLSLProgramObject(programObject, context.code());
|
||||
if (glProgramObjectID != 0) {
|
||||
pgMain.finalizeGLSLProgramObject(glProgramObjectID, context.code());
|
||||
}
|
||||
} finally {
|
||||
super.finalize();
|
||||
@@ -177,6 +177,34 @@ public class PShader {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes (if needed) and binds the shader program.
|
||||
*/
|
||||
public void bind() {
|
||||
init();
|
||||
pgl.glUseProgram(glProgramObjectID);
|
||||
bound = true;
|
||||
consumeUniforms();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unbinds the shader program.
|
||||
*/
|
||||
public void unbind() {
|
||||
pgl.glUseProgram(0);
|
||||
bound = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the shader is bound, false otherwise.
|
||||
*/
|
||||
public boolean bound() {
|
||||
return bound;
|
||||
}
|
||||
|
||||
|
||||
public void setUniform(String name, int x) {
|
||||
setUniformImpl(name, UniformValue.INT1, new int[] { x });
|
||||
}
|
||||
@@ -294,34 +322,6 @@ public class PShader {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts the execution of the shader program.
|
||||
*/
|
||||
protected void start() {
|
||||
init();
|
||||
pgl.glUseProgram(programObject);
|
||||
active = true;
|
||||
consumeUniforms();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stops the execution of the shader program.
|
||||
*/
|
||||
protected void stop() {
|
||||
pgl.glUseProgram(0);
|
||||
active = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the shader is running, false otherwise.
|
||||
*/
|
||||
protected boolean active() {
|
||||
return active;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the ID location of the attribute parameter given its name.
|
||||
*
|
||||
@@ -330,7 +330,7 @@ public class PShader {
|
||||
*/
|
||||
protected int getAttribLocation(String name) {
|
||||
init();
|
||||
return pgl.glGetAttribLocation(programObject, name);
|
||||
return pgl.glGetAttribLocation(glProgramObjectID, name);
|
||||
}
|
||||
|
||||
|
||||
@@ -342,7 +342,7 @@ public class PShader {
|
||||
*/
|
||||
protected int getUniformLocation(String name) {
|
||||
init();
|
||||
return pgl.glGetUniformLocation(programObject, name);
|
||||
return pgl.glGetUniformLocation(glProgramObjectID, name);
|
||||
}
|
||||
|
||||
|
||||
@@ -575,9 +575,9 @@ public class PShader {
|
||||
|
||||
|
||||
protected void init() {
|
||||
if (programObject == 0 || contextIsOutdated()) {
|
||||
if (glProgramObjectID == 0 || contextIsOutdated()) {
|
||||
context = pgl.getCurrentContext();
|
||||
programObject = pgMain.createGLSLProgramObject(context.code());
|
||||
glProgramObjectID = pgMain.createGLSLProgramObject(context.code());
|
||||
|
||||
boolean hasVert = false;
|
||||
if (vertexFilename != null) {
|
||||
@@ -609,25 +609,25 @@ public class PShader {
|
||||
|
||||
if (vertRes && fragRes) {
|
||||
if (hasVert) {
|
||||
pgl.glAttachShader(programObject, vertexShader);
|
||||
pgl.glAttachShader(glProgramObjectID, glVertexShaderID);
|
||||
}
|
||||
if (hasFrag) {
|
||||
pgl.glAttachShader(programObject, fragmentShader);
|
||||
pgl.glAttachShader(glProgramObjectID, glFragmentShaderID);
|
||||
}
|
||||
pgl.glLinkProgram(programObject);
|
||||
pgl.glLinkProgram(glProgramObjectID);
|
||||
|
||||
int[] linked = new int[1];
|
||||
pgl.glGetProgramiv(programObject, PGL.GL_LINK_STATUS, linked, 0);
|
||||
pgl.glGetProgramiv(glProgramObjectID, PGL.GL_LINK_STATUS, linked, 0);
|
||||
if (linked[0] == PGL.GL_FALSE) {
|
||||
PGraphics.showException("Cannot link shader program:\n" + pgl.glGetProgramInfoLog(programObject));
|
||||
PGraphics.showException("Cannot link shader program:\n" + pgl.glGetProgramInfoLog(glProgramObjectID));
|
||||
}
|
||||
|
||||
pgl.glValidateProgram(programObject);
|
||||
pgl.glValidateProgram(glProgramObjectID);
|
||||
|
||||
int[] validated = new int[1];
|
||||
pgl.glGetProgramiv(programObject, PGL.GL_VALIDATE_STATUS, validated, 0);
|
||||
pgl.glGetProgramiv(glProgramObjectID, PGL.GL_VALIDATE_STATUS, validated, 0);
|
||||
if (validated[0] == PGL.GL_FALSE) {
|
||||
PGraphics.showException("Cannot validate shader program:\n" + pgl.glGetProgramInfoLog(programObject));
|
||||
PGraphics.showException("Cannot validate shader program:\n" + pgl.glGetProgramInfoLog(glProgramObjectID));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -637,13 +637,13 @@ public class PShader {
|
||||
protected boolean contextIsOutdated() {
|
||||
boolean outdated = !pgl.contextIsCurrent(context);
|
||||
if (outdated) {
|
||||
pgMain.removeGLSLProgramObject(programObject, context.code());
|
||||
pgMain.removeGLSLVertShaderObject(vertexShader, context.code());
|
||||
pgMain.removeGLSLFragShaderObject(fragmentShader, context.code());
|
||||
pgMain.removeGLSLProgramObject(glProgramObjectID, context.code());
|
||||
pgMain.removeGLSLVertShaderObject(glVertexShaderID, context.code());
|
||||
pgMain.removeGLSLFragShaderObject(glFragmentShaderID, context.code());
|
||||
|
||||
programObject = 0;
|
||||
vertexShader = 0;
|
||||
fragmentShader = 0;
|
||||
glProgramObjectID = 0;
|
||||
glVertexShaderID = 0;
|
||||
glFragmentShaderID = 0;
|
||||
}
|
||||
return outdated;
|
||||
}
|
||||
@@ -707,15 +707,15 @@ public class PShader {
|
||||
* @param shaderSource a string containing the shader's code
|
||||
*/
|
||||
protected boolean compileVertexShader() {
|
||||
vertexShader = pgMain.createGLSLVertShaderObject(context.code());
|
||||
glVertexShaderID = pgMain.createGLSLVertShaderObject(context.code());
|
||||
|
||||
pgl.glShaderSource(vertexShader, vertexShaderSource);
|
||||
pgl.glCompileShader(vertexShader);
|
||||
pgl.glShaderSource(glVertexShaderID, vertexShaderSource);
|
||||
pgl.glCompileShader(glVertexShaderID);
|
||||
|
||||
int[] compiled = new int[1];
|
||||
pgl.glGetShaderiv(vertexShader, PGL.GL_COMPILE_STATUS, compiled, 0);
|
||||
pgl.glGetShaderiv(glVertexShaderID, PGL.GL_COMPILE_STATUS, compiled, 0);
|
||||
if (compiled[0] == PGL.GL_FALSE) {
|
||||
PGraphics.showException("Cannot compile vertex shader:\n" + pgl.glGetShaderInfoLog(vertexShader));
|
||||
PGraphics.showException("Cannot compile vertex shader:\n" + pgl.glGetShaderInfoLog(glVertexShaderID));
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@@ -727,15 +727,15 @@ public class PShader {
|
||||
* @param shaderSource a string containing the shader's code
|
||||
*/
|
||||
protected boolean compileFragmentShader() {
|
||||
fragmentShader = pgMain.createGLSLFragShaderObject(context.code());
|
||||
glFragmentShaderID = pgMain.createGLSLFragShaderObject(context.code());
|
||||
|
||||
pgl.glShaderSource(fragmentShader, fragmentShaderSource);
|
||||
pgl.glCompileShader(fragmentShader);
|
||||
pgl.glShaderSource(glFragmentShaderID, fragmentShaderSource);
|
||||
pgl.glCompileShader(glFragmentShaderID);
|
||||
|
||||
int[] compiled = new int[1];
|
||||
pgl.glGetShaderiv(fragmentShader, PGL.GL_COMPILE_STATUS, compiled, 0);
|
||||
pgl.glGetShaderiv(glFragmentShaderID, PGL.GL_COMPILE_STATUS, compiled, 0);
|
||||
if (compiled[0] == PGL.GL_FALSE) {
|
||||
PGraphics.showException("Cannot compile fragment shader:\n" + pgl.glGetShaderInfoLog(fragmentShader));
|
||||
PGraphics.showException("Cannot compile fragment shader:\n" + pgl.glGetShaderInfoLog(glFragmentShaderID));
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@@ -754,17 +754,17 @@ public class PShader {
|
||||
|
||||
|
||||
protected void release() {
|
||||
if (vertexShader != 0) {
|
||||
pgMain.deleteGLSLVertShaderObject(vertexShader, context.code());
|
||||
vertexShader = 0;
|
||||
if (glVertexShaderID != 0) {
|
||||
pgMain.deleteGLSLVertShaderObject(glVertexShaderID, context.code());
|
||||
glVertexShaderID = 0;
|
||||
}
|
||||
if (fragmentShader != 0) {
|
||||
pgMain.deleteGLSLFragShaderObject(fragmentShader, context.code());
|
||||
fragmentShader = 0;
|
||||
if (glFragmentShaderID != 0) {
|
||||
pgMain.deleteGLSLFragShaderObject(glFragmentShaderID, context.code());
|
||||
glFragmentShaderID = 0;
|
||||
}
|
||||
if (programObject != 0) {
|
||||
pgMain.deleteGLSLProgramObject(programObject, context.code());
|
||||
programObject = 0;
|
||||
if (glProgramObjectID != 0) {
|
||||
pgMain.deleteGLSLProgramObject(glProgramObjectID, context.code());
|
||||
glProgramObjectID = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3921,7 +3921,7 @@ public class PShapeOpenGL extends PShape {
|
||||
// Rendering fill triangles, which can be lit and textured.
|
||||
if (!renderingFill) {
|
||||
shader = g.getPolyShader(g.lights, tex != null);
|
||||
shader.start();
|
||||
shader.bind();
|
||||
renderingFill = true;
|
||||
}
|
||||
} else {
|
||||
@@ -3933,14 +3933,14 @@ public class PShapeOpenGL extends PShape {
|
||||
tex = null;
|
||||
}
|
||||
|
||||
if (shader != null && shader.active()) {
|
||||
shader.stop();
|
||||
if (shader != null && shader.bound()) {
|
||||
shader.unbind();
|
||||
}
|
||||
|
||||
// If the renderer is 2D, then g.lights should always be false,
|
||||
// so no need to worry about that.
|
||||
shader = g.getPolyShader(g.lights, false);
|
||||
shader.start();
|
||||
shader.bind();
|
||||
|
||||
renderingFill = false;
|
||||
renderingStroke = true;
|
||||
@@ -3972,8 +3972,8 @@ public class PShapeOpenGL extends PShape {
|
||||
pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
if (shader != null && shader.active()) {
|
||||
shader.stop();
|
||||
if (shader != null && shader.bound()) {
|
||||
shader.unbind();
|
||||
}
|
||||
|
||||
if (tex != null) {
|
||||
@@ -4074,7 +4074,7 @@ public class PShapeOpenGL extends PShape {
|
||||
|
||||
protected void renderLines(PGraphicsOpenGL g) {
|
||||
LineShader shader = g.getLineShader();
|
||||
shader.start();
|
||||
shader.bind();
|
||||
|
||||
IndexCache cache = tessGeo.lineIndexCache;
|
||||
for (int n = firstLineIndexCache; n <= lastLineIndexCache; n++) {
|
||||
@@ -4091,7 +4091,7 @@ public class PShapeOpenGL extends PShape {
|
||||
pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
shader.stop();
|
||||
shader.unbind();
|
||||
}
|
||||
|
||||
|
||||
@@ -4168,7 +4168,7 @@ public class PShapeOpenGL extends PShape {
|
||||
|
||||
protected void renderPoints(PGraphicsOpenGL g) {
|
||||
PointShader shader = g.getPointShader();
|
||||
shader.start();
|
||||
shader.bind();
|
||||
|
||||
IndexCache cache = tessGeo.pointIndexCache;
|
||||
for (int n = firstPointIndexCache; n <= lastPointIndexCache; n++) {
|
||||
@@ -4185,7 +4185,7 @@ public class PShapeOpenGL extends PShape {
|
||||
pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
shader.stop();
|
||||
shader.unbind();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user