Skip to content

Commit

Permalink
Added workaround for missing shader version numbers
Browse files Browse the repository at this point in the history
If the program cannot be compiled, a version number
is added to the code, and the compilation is started
again. This is very pragmatic, and should not be
necessary when shaders are versioned properly.
  • Loading branch information
javagl committed Jan 14, 2017
1 parent 5036ad7 commit 6b5c586
Showing 1 changed file with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public Integer createGlProgram(
{
logger.fine("Creating vertex shader...");

Integer glVertexShader =
createGlShader(GL_VERTEX_SHADER, vertexShaderSource);
Integer glVertexShader = createGlShader(
GL_VERTEX_SHADER, vertexShaderSource);
if (glVertexShader == null)
{
logger.warning("Creating vertex shader FAILED");
Expand All @@ -100,8 +100,8 @@ public Integer createGlProgram(
logger.fine("Creating vertex shader DONE");

logger.fine("Creating fragment shader...");
Integer glFragmentShader =
createGlShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
Integer glFragmentShader = createGlShader(
GL_FRAGMENT_SHADER, fragmentShaderSource);
if (glFragmentShader == null)
{
logger.warning("Creating fragment shader FAILED");
Expand Down Expand Up @@ -182,6 +182,39 @@ public void disable(Iterable<? extends Number> states)
* @return The GL shader
*/
private Integer createGlShader(int shaderType, String shaderSource)
{
Integer glShader = createGlShaderImpl(shaderType, shaderSource);
if (glShader != null)
{
return glShader;
}

// If the shader source code does not contain a #version number,
// then, depending on the com.jogamp.opengl.GLProfile that was
// chosen for the viewer, certain warnings may be treated as
// errors. As a workaround, pragmatically insert a version
// number and try again...
// (Also see https://github.com/javagl/JglTF/issues/12)
if (!shaderSource.contains("#version"))
{
String versionString = "#version 120";
logger.warning("Inserting GLSL version specifier \"" +
versionString + "\" in shader code");
String shaderSourceWithVersion =
versionString + "\n" + shaderSource;
return createGlShaderImpl(shaderType, shaderSourceWithVersion);
}
return null;
}

/**
* Implementation for {@link #createGlShader(int, String)}.
*
* @param shaderType The shader type
* @param shaderSource The shader source code
* @return The GL shader, or <code>null</code> if it cannot be compiled
*/
private Integer createGlShaderImpl(int shaderType, String shaderSource)
{
int glShader = gl.glCreateShader(shaderType);
gl.glShaderSource(
Expand All @@ -195,8 +228,10 @@ private Integer createGlShader(int shaderType, String shaderSource)
return null;
}
return glShader;

}


@Override
public int getUniformLocation(int glProgram, String uniformName)
{
Expand Down

0 comments on commit 6b5c586

Please sign in to comment.