From bfe1470614493c877fdec6876ba89000e2469319 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 14 Aug 2015 16:09:16 -0400 Subject: [PATCH 1/5] Request programmable GL profile --- core/src/processing/opengl/PSurfaceJOGL.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/core/src/processing/opengl/PSurfaceJOGL.java b/core/src/processing/opengl/PSurfaceJOGL.java index 0764d2ff2..cecfd0d0e 100644 --- a/core/src/processing/opengl/PSurfaceJOGL.java +++ b/core/src/processing/opengl/PSurfaceJOGL.java @@ -195,15 +195,9 @@ public class PSurfaceJOGL implements PSurface { if (profile == null) { if (PJOGL.profile == 2) { try { - if ("arm".equals(System.getProperty("os.arch"))) { - // request at least GL2 or GLES2 - profile = GLProfile.getGL2ES2(); - } else { - // stay compatible with previous versions for now - profile = GLProfile.getGL2ES1(); - } + profile = GLProfile.getGL2ES2(); } catch (GLException ex) { - profile = GLProfile.getMaxFixedFunc(true); + profile = GLProfile.getMaxProgrammable(true); } } else if (PJOGL.profile == 3) { try { From 38930b5f4bb7e160691421579de61edfac94b7ef Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 14 Aug 2015 16:12:32 -0400 Subject: [PATCH 2/5] Fix depth and stencil bits getters --- core/src/processing/opengl/PGL.java | 12 ++---------- core/src/processing/opengl/PJOGL.java | 4 ++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index c0108f0a1..18f178729 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -459,18 +459,10 @@ public abstract class PGL { } - protected int getDepthBits() { - intBuffer.rewind(); - getIntegerv(DEPTH_BITS, intBuffer); - return intBuffer.get(0); - } + abstract protected int getDepthBits(); - protected int getStencilBits() { - intBuffer.rewind(); - getIntegerv(STENCIL_BITS, intBuffer); - return intBuffer.get(0); - } + abstract protected int getStencilBits(); protected boolean getDepthTest() { diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 6a9dfe4d1..ef8386675 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -500,7 +500,7 @@ public class PJOGL extends PGL { protected boolean isFBOBacked() { return super.isFBOBacked() || capabilities.isFBO(); } - +*/ @Override protected int getDepthBits() { @@ -512,7 +512,7 @@ public class PJOGL extends PGL { protected int getStencilBits() { return capabilities.getStencilBits(); } - +/* @Override protected Texture wrapBackTexture(Texture texture) { From eb7469a1f393fccb794adfa70e95fbd91b3c1581 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 14 Aug 2015 17:50:14 -0400 Subject: [PATCH 3/5] Preprocess shaders on MACOSX MACOSX OpenGL core profile requires GLSL 1.30, so we have to rename a few things to make it happy --- core/src/processing/opengl/PGL.java | 85 +++++++++++++++++++-------- core/src/processing/opengl/PJOGL.java | 17 +++--- 2 files changed, 68 insertions(+), 34 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 18f178729..dedf1e663 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -1666,45 +1666,78 @@ public abstract class PGL { } - protected static String[] convertFragmentSource(String[] fragSrc0, - int version0, int version1) { - if (version0 == 120 && version1 == 150) { - String[] fragSrc = new String[fragSrc0.length + 2]; - fragSrc[0] = "#version 150"; + protected static String[] preprocessFragmentSource(String[] fragSrc0, + int version) { + if (version >= 130) { + // We need to replace 'texture' uniform by 'texMap' uniform and + // 'textureXXX()' functions by 'texture()' functions. Order of these + // replacements is important to prevent collisions between these two. + String[] search = new String[] { + "varying", "attibute", + "texture", + "texMap2D", "texMap3D", "texMap2DRect", + "texMapCube", "gl_FragColor" + }; + String[] replace = new String[] { + "in", "in", + "texMap", + "texture", "texture", "texture", "texture", + "fragColor" + }; + + String[] fragSrc = preprocessShaderSource(fragSrc0, search, replace, 2); + fragSrc[0] = "#version " + version; fragSrc[1] = "out vec4 fragColor;"; - for (int i = 0; i < fragSrc0.length; i++) { - String line = fragSrc0[i]; - line = line.replace("varying", "in"); - line = line.replace("attribute", "in"); - line = line.replace("gl_FragColor", "fragColor"); - line = line.replace("texture", "texMap"); - line = line.replace("texMap2D(", "texture("); - line = line.replace("texMap2DRect(", "texture("); - fragSrc[i + 2] = line; - } + return fragSrc; } return fragSrc0; } + protected static String[] preprocessVertexSource(String[] vertSrc0, + int version) { + if (version >= 130) { + // We need to replace 'texture' uniform by 'texMap' uniform and + // 'textureXXX()' functions by 'texture()' functions. Order of these + // replacements is important to prevent collisions between these two. + String[] search = new String[] { + "varying", "attibute", + "texture", + "texMap2D", "texMap3D", "texMap2DRect", "texMapCube" + }; + String[] replace = new String[] { + "out", "in", + "texMap", + "texture", "texture", "texture", "texture" + }; + String[] vertSrc = preprocessShaderSource(vertSrc0, search, replace, 1); + vertSrc[0] = "#version " + version; - protected static String[] convertVertexSource(String[] vertSrc0, - int version0, int version1) { - if (version0 == 120 && version1 == 150) { - String[] vertSrc = new String[vertSrc0.length + 1]; - vertSrc[0] = "#version 150"; - for (int i = 0; i < vertSrc0.length; i++) { - String line = vertSrc0[i]; - line = line.replace("attribute", "in"); - line = line.replace("varying", "out"); - vertSrc[i + 1] = line; - } return vertSrc; } return vertSrc0; } + protected static String[] preprocessShaderSource(String[] src0, + String[] search, + String[] replace, + int offset) { + String[] src = new String[src0.length+offset]; + for (int i = 0; i < src0.length; i++) { + String line = src0[i]; + if (line.contains("#version")) { + line = ""; + } + for (int j = 0; j < search.length; j++) { + line = line.replace(search[j], replace[j]); + } + src[i+offset] = line; + } + return src; + } + + protected int createShader(int shaderType, String source) { int shader = createShader(shaderType); if (shader != 0) { diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index ef8386675..81f472921 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -53,6 +53,7 @@ import com.jogamp.opengl.glu.GLUtessellator; import com.jogamp.opengl.glu.GLUtessellatorCallbackAdapter; import processing.core.PApplet; +import processing.core.PConstants; import processing.core.PGraphics; import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; @@ -1241,9 +1242,9 @@ public class PJOGL extends PGL { @Override protected String[] loadVertexShader(String filename, int version) { - if (2 < profile && version < 150) { + if (PApplet.platform == PConstants.MACOSX) { String[] fragSrc0 = pg.parent.loadStrings(filename); - return convertFragmentSource(fragSrc0, version, 150); + return preprocessFragmentSource(fragSrc0, 130); } else { return pg.parent.loadStrings(filename); } @@ -1252,9 +1253,9 @@ public class PJOGL extends PGL { @Override protected String[] loadFragmentShader(String filename, int version) { - if (2 < profile && version < 150) { + if (PApplet.platform == PConstants.MACOSX) { String[] vertSrc0 = pg.parent.loadStrings(filename); - return convertVertexSource(vertSrc0, version, 150); + return preprocessVertexSource(vertSrc0, 130); } else { return pg.parent.loadStrings(filename); } @@ -1264,9 +1265,9 @@ public class PJOGL extends PGL { @Override protected String[] loadFragmentShader(URL url, int version) { try { - if (2 < profile && version < 150) { + if (PApplet.platform == PConstants.MACOSX) { String[] fragSrc0 = PApplet.loadStrings(url.openStream()); - return convertFragmentSource(fragSrc0, version, 150); + return preprocessFragmentSource(fragSrc0, 130); } else { return PApplet.loadStrings(url.openStream()); } @@ -1280,9 +1281,9 @@ public class PJOGL extends PGL { @Override protected String[] loadVertexShader(URL url, int version) { try { - if (2 < profile && version < 150) { + if (PApplet.platform == PConstants.MACOSX) { String[] vertSrc0 = PApplet.loadStrings(url.openStream()); - return convertVertexSource(vertSrc0, version, 150); + return preprocessVertexSource(vertSrc0, 130); } else { return PApplet.loadStrings(url.openStream()); } From 7f5b5cd1df496874e37b3dae97517f188b361cd7 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 14 Aug 2015 17:51:07 -0400 Subject: [PATCH 4/5] Remove shader compilation warnings on Intel --- core/src/processing/opengl/shaders/LightVert.glsl | 9 +++++---- core/src/processing/opengl/shaders/TexLightVert.glsl | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/processing/opengl/shaders/LightVert.glsl b/core/src/processing/opengl/shaders/LightVert.glsl index 747e0295d..0c351aab7 100644 --- a/core/src/processing/opengl/shaders/LightVert.glsl +++ b/core/src/processing/opengl/shaders/LightVert.glsl @@ -47,7 +47,8 @@ varying vec4 backVertColor; const float zero_float = 0.0; const float one_float = 1.0; -const vec3 zero_vec3 = vec3(0); +const vec3 zero_vec3 = vec3(0.0); +const vec3 minus_one_vec3 = vec3(0.0-1.0); float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) { vec3 lpv = lightPos - vertPos; @@ -59,7 +60,7 @@ float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) { float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) { vec3 lpv = normalize(lightPos - vertPos); - vec3 nln = -one_float * lightNorm; + vec3 nln = minus_one_vec3 * lightNorm; float spotCos = dot(nln, lpv); return spotCos <= minCos ? zero_float : pow(spotCos, spotExp); } @@ -83,7 +84,7 @@ void main() { // Normal vector in eye coordinates vec3 ecNormal = normalize(normalMatrix * normal); - vec3 ecNormalInv = ecNormal * -one_float; + vec3 ecNormalInv = ecNormal * minus_one_vec3; // Light calculations vec3 totalAmbient = vec3(0, 0, 0); @@ -108,7 +109,7 @@ void main() { if (isDir) { falloff = one_float; - lightDir = -one_float * lightNormal[i]; + lightDir = minus_one_vec3 * lightNormal[i]; } else { falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]); lightDir = normalize(lightPos - ecVertex); diff --git a/core/src/processing/opengl/shaders/TexLightVert.glsl b/core/src/processing/opengl/shaders/TexLightVert.glsl index a69c66dc0..478c91ca2 100644 --- a/core/src/processing/opengl/shaders/TexLightVert.glsl +++ b/core/src/processing/opengl/shaders/TexLightVert.glsl @@ -50,7 +50,8 @@ varying vec4 vertTexCoord; const float zero_float = 0.0; const float one_float = 1.0; -const vec3 zero_vec3 = vec3(0); +const vec3 zero_vec3 = vec3(0.0); +const vec3 minus_one_vec3 = vec3(0.0-1.0); float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) { vec3 lpv = lightPos - vertPos; @@ -62,7 +63,7 @@ float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) { float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) { vec3 lpv = normalize(lightPos - vertPos); - vec3 nln = -one_float * lightNorm; + vec3 nln = minus_one_vec3 * lightNorm; float spotCos = dot(nln, lpv); return spotCos <= minCos ? zero_float : pow(spotCos, spotExp); } @@ -86,7 +87,7 @@ void main() { // Normal vector in eye coordinates vec3 ecNormal = normalize(normalMatrix * normal); - vec3 ecNormalInv = ecNormal * -one_float; + vec3 ecNormalInv = ecNormal * minus_one_vec3; // Light calculations vec3 totalAmbient = vec3(0, 0, 0); @@ -111,7 +112,7 @@ void main() { if (isDir) { falloff = one_float; - lightDir = -one_float * lightNormal[i]; + lightDir = minus_one_vec3 * lightNormal[i]; } else { falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]); lightDir = normalize(lightPos - ecVertex); From 91f96a54c5fef4e67fe4e893fd44ebdf1d8ff05c Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 14 Aug 2015 17:53:03 -0400 Subject: [PATCH 5/5] Enabling textures rubs OpenGL the wrong way This is was deprecated with fixed-function pipeline --- core/src/processing/opengl/PJOGL.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 81f472921..29651057b 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -1174,7 +1174,6 @@ public class PJOGL extends PGL { @Override protected void enableTexturing(int target) { - if (profile == 2) enable(target); if (target == TEXTURE_2D) { texturingTargets[0] = true; } else if (target == TEXTURE_RECTANGLE) { @@ -1185,7 +1184,6 @@ public class PJOGL extends PGL { @Override protected void disableTexturing(int target) { - if (profile == 2) disable(target); if (target == TEXTURE_2D) { texturingTargets[0] = false; } else if (target == TEXTURE_RECTANGLE) {