From 6b5c586eff954aeca5642cbe3038749f2cd67512 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Sat, 14 Jan 2017 17:48:19 +0100 Subject: [PATCH] Added workaround for missing shader version numbers 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. --- .../jgltf/viewer/jogl/GlContextJogl.java | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/jgltf-viewer-jogl/src/main/java/de/javagl/jgltf/viewer/jogl/GlContextJogl.java b/jgltf-viewer-jogl/src/main/java/de/javagl/jgltf/viewer/jogl/GlContextJogl.java index 5f6a4a64..82ca6214 100644 --- a/jgltf-viewer-jogl/src/main/java/de/javagl/jgltf/viewer/jogl/GlContextJogl.java +++ b/jgltf-viewer-jogl/src/main/java/de/javagl/jgltf/viewer/jogl/GlContextJogl.java @@ -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"); @@ -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"); @@ -182,6 +182,39 @@ public void disable(Iterable 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 null if it cannot be compiled + */ + private Integer createGlShaderImpl(int shaderType, String shaderSource) { int glShader = gl.glCreateShader(shaderType); gl.glShaderSource( @@ -195,8 +228,10 @@ private Integer createGlShader(int shaderType, String shaderSource) return null; } return glShader; + } + @Override public int getUniformLocation(int glProgram, String uniformName) {