Preprocess shaders on MACOSX

MACOSX OpenGL core profile requires GLSL 1.30, so we have to rename a
few things to make it happy
This commit is contained in:
Jakub Valtar
2015-08-14 17:50:14 -04:00
parent 38930b5f4b
commit eb7469a1f3
2 changed files with 68 additions and 34 deletions
+59 -26
View File
@@ -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) {
+9 -8
View File
@@ -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());
}