From cb83147d74dc6b6fb61c19a4404824dd3e6cf8c0 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Sat, 15 Aug 2015 11:03:12 -0400 Subject: [PATCH] Detect GLSL version and preprocess to that --- core/src/processing/opengl/PGL.java | 38 ++++++++++++++------ core/src/processing/opengl/PJOGL.java | 38 ++++++++------------ core/src/processing/opengl/PSurfaceJOGL.java | 6 +--- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 375957693..71d7b3b74 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -1668,7 +1668,16 @@ public abstract class PGL { protected static String[] preprocessFragmentSource(String[] fragSrc0, int version) { - if (version >= 130) { + String[] fragSrc; + + if (version < 130) { + String[] search = { }; + String[] replace = { }; + int offset = 1; + + fragSrc = preprocessShaderSource(fragSrc0, search, replace, offset); + fragSrc[0] = "#version " + version; + } else { // 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. @@ -1684,19 +1693,28 @@ public abstract class PGL { "texture", "texture", "texture", "texture", "fragColor" }; + int offset = 2; - String[] fragSrc = preprocessShaderSource(fragSrc0, search, replace, 2); + fragSrc = preprocessShaderSource(fragSrc0, search, replace, offset); fragSrc[0] = "#version " + version; fragSrc[1] = "out vec4 fragColor;"; - - return fragSrc; } - return fragSrc0; + + return fragSrc; } protected static String[] preprocessVertexSource(String[] vertSrc0, int version) { - if (version >= 130) { + String[] vertSrc; + + if (version < 130) { + String[] search = { }; + String[] replace = { }; + int offset = 1; + + vertSrc = preprocessShaderSource(vertSrc0, search, replace, offset); + vertSrc[0] = "#version " + version; + } else { // 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. @@ -1710,13 +1728,13 @@ public abstract class PGL { "texMap", "texture", "texture", "texture", "texture" }; + int offset = 1; - String[] vertSrc = preprocessShaderSource(vertSrc0, search, replace, 1); + vertSrc = preprocessShaderSource(vertSrc0, search, replace, offset); vertSrc[0] = "#version " + version; - - return vertSrc; } - return vertSrc0; + + return vertSrc; } protected static String[] preprocessShaderSource(String[] src0, diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 29651057b..7d852a854 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -37,6 +37,7 @@ import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; +import com.jogamp.common.util.VersionNumber; import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GL2ES1; @@ -1238,37 +1239,30 @@ public class PJOGL extends PGL { } + private static int getGLSLVersion(GLContext context) { + VersionNumber vn = context.getGLSLVersionNumber(); + return vn.getMajor() * 100 + vn.getMinor(); + } + @Override protected String[] loadVertexShader(String filename, int version) { - if (PApplet.platform == PConstants.MACOSX) { - String[] fragSrc0 = pg.parent.loadStrings(filename); - return preprocessFragmentSource(fragSrc0, 130); - } else { - return pg.parent.loadStrings(filename); - } + String[] fragSrc0 = pg.parent.loadStrings(filename); + return preprocessFragmentSource(fragSrc0, getGLSLVersion(context)); } @Override protected String[] loadFragmentShader(String filename, int version) { - if (PApplet.platform == PConstants.MACOSX) { - String[] vertSrc0 = pg.parent.loadStrings(filename); - return preprocessVertexSource(vertSrc0, 130); - } else { - return pg.parent.loadStrings(filename); - } + String[] vertSrc0 = pg.parent.loadStrings(filename); + return preprocessVertexSource(vertSrc0, getGLSLVersion(context)); } @Override protected String[] loadFragmentShader(URL url, int version) { try { - if (PApplet.platform == PConstants.MACOSX) { - String[] fragSrc0 = PApplet.loadStrings(url.openStream()); - return preprocessFragmentSource(fragSrc0, 130); - } else { - return PApplet.loadStrings(url.openStream()); - } + String[] fragSrc0 = PApplet.loadStrings(url.openStream()); + return preprocessFragmentSource(fragSrc0, getGLSLVersion(context)); } catch (IOException e) { PGraphics.showException("Cannot load fragment shader " + url.getFile()); } @@ -1279,12 +1273,8 @@ public class PJOGL extends PGL { @Override protected String[] loadVertexShader(URL url, int version) { try { - if (PApplet.platform == PConstants.MACOSX) { - String[] vertSrc0 = PApplet.loadStrings(url.openStream()); - return preprocessVertexSource(vertSrc0, 130); - } else { - return PApplet.loadStrings(url.openStream()); - } + String[] vertSrc0 = PApplet.loadStrings(url.openStream()); + return preprocessVertexSource(vertSrc0, getGLSLVersion(context)); } catch (IOException e) { PGraphics.showException("Cannot load vertex shader " + url.getFile()); } diff --git a/core/src/processing/opengl/PSurfaceJOGL.java b/core/src/processing/opengl/PSurfaceJOGL.java index f9d7bbbd2..cecfd0d0e 100644 --- a/core/src/processing/opengl/PSurfaceJOGL.java +++ b/core/src/processing/opengl/PSurfaceJOGL.java @@ -195,11 +195,7 @@ public class PSurfaceJOGL implements PSurface { if (profile == null) { if (PJOGL.profile == 2) { try { - if (PApplet.platform == PConstants.MACOSX) { - profile = GLProfile.getMaxProgrammableCore(true); - } else { - profile = GLProfile.getGL2ES2(); - } + profile = GLProfile.getGL2ES2(); } catch (GLException ex) { profile = GLProfile.getMaxProgrammable(true); }