Skip to content

Commit 0c18512

Browse files
committed
Detect GLSL version and preprocess to that
1 parent a4297c6 commit 0c18512

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

core/src/processing/opengl/PGL.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,16 @@ protected String[] loadVertexShader(URL url, int version) {
16681668

16691669
protected static String[] preprocessFragmentSource(String[] fragSrc0,
16701670
int version) {
1671-
if (version >= 130) {
1671+
String[] fragSrc;
1672+
1673+
if (version < 130) {
1674+
String[] search = { };
1675+
String[] replace = { };
1676+
int offset = 1;
1677+
1678+
fragSrc = preprocessShaderSource(fragSrc0, search, replace, offset);
1679+
fragSrc[0] = "#version " + version;
1680+
} else {
16721681
// We need to replace 'texture' uniform by 'texMap' uniform and
16731682
// 'textureXXX()' functions by 'texture()' functions. Order of these
16741683
// replacements is important to prevent collisions between these two.
@@ -1684,19 +1693,28 @@ protected static String[] preprocessFragmentSource(String[] fragSrc0,
16841693
"texture", "texture", "texture", "texture",
16851694
"fragColor"
16861695
};
1696+
int offset = 2;
16871697

1688-
String[] fragSrc = preprocessShaderSource(fragSrc0, search, replace, 2);
1698+
fragSrc = preprocessShaderSource(fragSrc0, search, replace, offset);
16891699
fragSrc[0] = "#version " + version;
16901700
fragSrc[1] = "out vec4 fragColor;";
1691-
1692-
return fragSrc;
16931701
}
1694-
return fragSrc0;
1702+
1703+
return fragSrc;
16951704
}
16961705

16971706
protected static String[] preprocessVertexSource(String[] vertSrc0,
16981707
int version) {
1699-
if (version >= 130) {
1708+
String[] vertSrc;
1709+
1710+
if (version < 130) {
1711+
String[] search = { };
1712+
String[] replace = { };
1713+
int offset = 1;
1714+
1715+
vertSrc = preprocessShaderSource(vertSrc0, search, replace, offset);
1716+
vertSrc[0] = "#version " + version;
1717+
} else {
17001718
// We need to replace 'texture' uniform by 'texMap' uniform and
17011719
// 'textureXXX()' functions by 'texture()' functions. Order of these
17021720
// replacements is important to prevent collisions between these two.
@@ -1710,13 +1728,13 @@ protected static String[] preprocessVertexSource(String[] vertSrc0,
17101728
"texMap",
17111729
"texture", "texture", "texture", "texture"
17121730
};
1731+
int offset = 1;
17131732

1714-
String[] vertSrc = preprocessShaderSource(vertSrc0, search, replace, 1);
1733+
vertSrc = preprocessShaderSource(vertSrc0, search, replace, offset);
17151734
vertSrc[0] = "#version " + version;
1716-
1717-
return vertSrc;
17181735
}
1719-
return vertSrc0;
1736+
1737+
return vertSrc;
17201738
}
17211739

17221740
protected static String[] preprocessShaderSource(String[] src0,

core/src/processing/opengl/PJOGL.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.nio.FloatBuffer;
3838
import java.nio.IntBuffer;
3939

40+
import com.jogamp.common.util.VersionNumber;
4041
import com.jogamp.opengl.GL;
4142
import com.jogamp.opengl.GL2;
4243
import com.jogamp.opengl.GL2ES1;
@@ -1238,37 +1239,30 @@ protected Object getDerivedFont(Object font, float size) {
12381239
}
12391240

12401241

1242+
private static int getGLSLVersion(GLContext context) {
1243+
VersionNumber vn = context.getGLSLVersionNumber();
1244+
return vn.getMajor() * 100 + vn.getMinor();
1245+
}
1246+
12411247
@Override
12421248
protected String[] loadVertexShader(String filename, int version) {
1243-
if (PApplet.platform == PConstants.MACOSX) {
1244-
String[] fragSrc0 = pg.parent.loadStrings(filename);
1245-
return preprocessFragmentSource(fragSrc0, 130);
1246-
} else {
1247-
return pg.parent.loadStrings(filename);
1248-
}
1249+
String[] fragSrc0 = pg.parent.loadStrings(filename);
1250+
return preprocessFragmentSource(fragSrc0, getGLSLVersion(context));
12491251
}
12501252

12511253

12521254
@Override
12531255
protected String[] loadFragmentShader(String filename, int version) {
1254-
if (PApplet.platform == PConstants.MACOSX) {
1255-
String[] vertSrc0 = pg.parent.loadStrings(filename);
1256-
return preprocessVertexSource(vertSrc0, 130);
1257-
} else {
1258-
return pg.parent.loadStrings(filename);
1259-
}
1256+
String[] vertSrc0 = pg.parent.loadStrings(filename);
1257+
return preprocessVertexSource(vertSrc0, getGLSLVersion(context));
12601258
}
12611259

12621260

12631261
@Override
12641262
protected String[] loadFragmentShader(URL url, int version) {
12651263
try {
1266-
if (PApplet.platform == PConstants.MACOSX) {
1267-
String[] fragSrc0 = PApplet.loadStrings(url.openStream());
1268-
return preprocessFragmentSource(fragSrc0, 130);
1269-
} else {
1270-
return PApplet.loadStrings(url.openStream());
1271-
}
1264+
String[] fragSrc0 = PApplet.loadStrings(url.openStream());
1265+
return preprocessFragmentSource(fragSrc0, getGLSLVersion(context));
12721266
} catch (IOException e) {
12731267
PGraphics.showException("Cannot load fragment shader " + url.getFile());
12741268
}
@@ -1279,12 +1273,8 @@ protected String[] loadFragmentShader(URL url, int version) {
12791273
@Override
12801274
protected String[] loadVertexShader(URL url, int version) {
12811275
try {
1282-
if (PApplet.platform == PConstants.MACOSX) {
1283-
String[] vertSrc0 = PApplet.loadStrings(url.openStream());
1284-
return preprocessVertexSource(vertSrc0, 130);
1285-
} else {
1286-
return PApplet.loadStrings(url.openStream());
1287-
}
1276+
String[] vertSrc0 = PApplet.loadStrings(url.openStream());
1277+
return preprocessVertexSource(vertSrc0, getGLSLVersion(context));
12881278
} catch (IOException e) {
12891279
PGraphics.showException("Cannot load vertex shader " + url.getFile());
12901280
}

core/src/processing/opengl/PSurfaceJOGL.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,7 @@ protected void initGL() {
195195
if (profile == null) {
196196
if (PJOGL.profile == 2) {
197197
try {
198-
if (PApplet.platform == PConstants.MACOSX) {
199-
profile = GLProfile.getMaxProgrammableCore(true);
200-
} else {
201-
profile = GLProfile.getGL2ES2();
202-
}
198+
profile = GLProfile.getGL2ES2();
203199
} catch (GLException ex) {
204200
profile = GLProfile.getMaxProgrammable(true);
205201
}

0 commit comments

Comments
 (0)