private static void clearEnabledAttributes(final ShaderObjectsStateRecord record, final GL gl) {
   // go through and disable any enabled attributes
   if (!record.enabledAttributes.isEmpty()) {
     for (int i = 0, maxI = record.enabledAttributes.size(); i < maxI; i++) {
       final ShaderVariable var = record.enabledAttributes.get(i);
       if (var.getSize() == 1) {
         if (gl.isGL2()) {
           gl.getGL2().glDisableVertexAttribArrayARB(var.variableID);
         } else {
           if (gl.isGL2ES2()) {
             gl.getGL2ES2().glDisableVertexAttribArray(var.variableID);
           }
         }
       } else {
         for (int j = 0, maxJ = var.getSize(); j < maxJ; j++) {
           if (gl.isGL2()) {
             gl.getGL2().glDisableVertexAttribArrayARB(var.variableID + j);
           } else {
             if (gl.isGL2ES2()) {
               gl.getGL2ES2().glDisableVertexAttribArray(var.variableID + j);
             }
           }
         }
       }
     }
     record.enabledAttributes.clear();
   }
 }
  private static void checkLinkError(final int programId) {
    final GL gl = GLContext.getCurrentGL();

    final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
    final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
    compiled.clear();
    if (gl.isGL2()) {
      gl.getGL2().glGetObjectParameterivARB(programId, GL2ES2.GL_LINK_STATUS, compiled);
    } else {
      if (gl.isGL2ES2()) {
        gl.getGL2ES2().glGetProgramiv(programId, GL2ES2.GL_LINK_STATUS, compiled);
      }
    }
    if (compiled.get(0) == GL.GL_FALSE) {
      if (gl.isGL2()) {
        gl.getGL2().glGetObjectParameterivARB(programId, GL2ES2.GL_INFO_LOG_LENGTH, compiled);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glGetProgramiv(programId, GL2ES2.GL_INFO_LOG_LENGTH, compiled);
        }
      }
      final int length = compiled.get(0);
      String out = null;
      if (length > 0) {
        final ByteBuffer infoLogBuf = context.getDirectNioBuffersSet().getInfoLogBuffer();
        final ByteBuffer infoLog;
        if (length <= infoLogBuf.capacity()) {
          infoLog = infoLogBuf;
          infoLogBuf.rewind().limit(length);
        } else {
          infoLog = BufferUtils.createByteBuffer(length);
        }
        if (gl.isGL2()) {
          gl.getGL2().glGetInfoLogARB(programId, infoLog.limit(), compiled, infoLog);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glGetProgramInfoLog(programId, infoLog.limit(), compiled, infoLog);
          }
        }

        final byte[] infoBytes = new byte[length];
        infoLog.get(infoBytes);
        out = new String(infoBytes);
      }

      logger.severe(out);

      // throw new Ardor3dException("Error linking GLSL shader: " + out);
    }
  }
示例#3
0
  public static boolean createAndLoadShader(
      final GL _gl,
      final IntBuffer shader,
      final int shaderType,
      final int binFormat,
      final java.nio.Buffer bin,
      final PrintStream verboseOut) {
    final GL2ES2 gl = _gl.getGL2ES2();
    int err = gl.glGetError(); // flush previous errors ..
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println("createAndLoadShader: Pre GL Error: 0x" + Integer.toHexString(err));
    }

    createShader(gl, shaderType, shader);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR) {
      throw new GLException(
          "createAndLoadShader: CreateShader failed, GL Error: 0x" + Integer.toHexString(err));
    }

    shaderBinary(gl, shader, binFormat, bin);

    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println(
          "createAndLoadShader: ShaderBinary failed, GL Error: 0x" + Integer.toHexString(err));
    }
    return err == GL.GL_NO_ERROR;
  }
示例#4
0
  public static boolean isProgramStatusValid(final GL _gl, final int programObj, final int name) {
    final GL2ES2 gl = _gl.getGL2ES2();
    final int[] ires = new int[1];
    gl.glGetProgramiv(programObj, name, ires, 0);

    return ires[0] == 1;
  }
示例#5
0
  public static void shaderSource(final GL _gl, final int shader, final CharSequence[] source) {
    final GL2ES2 gl = _gl.getGL2ES2();
    if (!isShaderCompilerAvailable(_gl)) {
      throw new GLException("No compiler is available");
    }

    final int count = (null != source) ? source.length : 0;
    if (count == 0) {
      throw new GLException("No sources specified");
    }

    final IntBuffer lengths = Buffers.newDirectIntBuffer(count);
    for (int i = 0; i < count; i++) {
      lengths.put(i, source[i].length());
    }
    if (source instanceof String[]) {
      // rare case ..
      gl.glShaderSource(shader, count, (String[]) source, lengths);
    } else {
      final String[] tmp = new String[source.length];
      for (int i = source.length - 1; i >= 0; i--) {
        final CharSequence csq = source[i];
        if (csq instanceof String) {
          // if ShaderCode.create(.. mutableStringBuilder == false )
          tmp[i] = (String) csq;
        } else {
          // if ShaderCode.create(.. mutableStringBuilder == true )
          tmp[i] = source[i].toString();
        }
      }
      gl.glShaderSource(shader, count, tmp, lengths);
    }
  }
示例#6
0
 /** Returns true if a hader compiler is available, otherwise false. */
 public static boolean isShaderCompilerAvailable(final GL _gl) {
   final GL2ES2 gl = _gl.getGL2ES2();
   final ProfileInformation info = getProfileInformation(gl);
   if (null == info.shaderCompilerAvailable) {
     if (gl.isGLES2()) {
       boolean queryOK = false;
       try {
         final byte[] param = new byte[1];
         gl.glGetBooleanv(GL2ES2.GL_SHADER_COMPILER, param, 0);
         final int err = gl.glGetError();
         boolean v = GL.GL_NO_ERROR == err && param[0] != (byte) 0x00;
         if (!v) {
           final Set<Integer> bfs = getShaderBinaryFormats(gl);
           if (bfs.size() == 0) {
             // no supported binary formats, hence a compiler must be available!
             v = true;
           }
         }
         info.shaderCompilerAvailable = Boolean.valueOf(v);
         queryOK = true;
       } catch (final GLException gle) {
         System.err.println("Caught exception on thread " + Thread.currentThread().getName());
         gle.printStackTrace();
       }
       if (!queryOK) {
         info.shaderCompilerAvailable = Boolean.valueOf(true);
       }
     } else if (gl.isGL2ES2()) {
       info.shaderCompilerAvailable = new Boolean(true);
     } else {
       throw new GLException("Invalid OpenGL profile");
     }
   }
   return info.shaderCompilerAvailable.booleanValue();
 }
示例#7
0
 /**
  * If supported, queries the natively supported shader binary formats using {@link
  * GL2ES2#GL_NUM_SHADER_BINARY_FORMATS} and {@link GL2ES2#GL_SHADER_BINARY_FORMATS} via {@link
  * GL2ES2#glGetIntegerv(int, int[], int)}.
  */
 public static Set<Integer> getShaderBinaryFormats(final GL _gl) {
   final GL2ES2 gl = _gl.getGL2ES2();
   final ProfileInformation info = getProfileInformation(gl);
   if (null == info.shaderBinaryFormats) {
     info.shaderBinaryFormats = new HashSet<Integer>();
     if (gl.isGLES2Compatible()) {
       try {
         final int[] param = new int[1];
         gl.glGetIntegerv(GL2ES2.GL_NUM_SHADER_BINARY_FORMATS, param, 0);
         final int err = gl.glGetError();
         final int numFormats = GL.GL_NO_ERROR == err ? param[0] : 0;
         if (numFormats > 0) {
           final int[] formats = new int[numFormats];
           gl.glGetIntegerv(GL2ES2.GL_SHADER_BINARY_FORMATS, formats, 0);
           for (int i = 0; i < numFormats; i++) {
             info.shaderBinaryFormats.add(Integer.valueOf(formats[i]));
           }
         }
       } catch (final GLException gle) {
         System.err.println("Caught exception on thread " + Thread.currentThread().getName());
         gle.printStackTrace();
       }
     }
   }
   return info.shaderBinaryFormats;
 }
  /**
   * Check for program errors. If an error is detected, program exits.
   *
   * @param compilerState the compiler state for a given shader
   * @param id shader's id
   */
  private static void checkProgramError(
      final int compilerState, final int id, final String shaderName) {
    final GL gl = GLContext.getCurrentGL();

    if (compilerState == GL.GL_FALSE) {
      final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
      final IntBuffer iVal = context.getDirectNioBuffersSet().getSingleIntBuffer();
      iVal.clear();
      if (gl.isGL2()) {
        gl.getGL2().glGetObjectParameterivARB(id, GL2.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glGetProgramiv(id, GL2ES2.GL_INFO_LOG_LENGTH, iVal);
        }
      }
      final int length = iVal.get(0);
      String out = null;

      if (length > 0) {
        final ByteBuffer infoLogBuf = context.getDirectNioBuffersSet().getInfoLogBuffer();
        final ByteBuffer infoLog;
        if (length <= infoLogBuf.capacity()) {
          infoLog = infoLogBuf;
          infoLogBuf.rewind().limit(length);
        } else {
          infoLog = BufferUtils.createByteBuffer(length);
        }
        if (gl.isGL2()) {
          gl.getGL2().glGetInfoLogARB(id, infoLog.limit(), iVal, infoLog);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glGetProgramInfoLog(id, infoLog.limit(), iVal, infoLog);
          }
        }

        final byte[] infoBytes = new byte[length];
        infoLog.get(infoBytes);
        out = new String(infoBytes);
      }

      logger.severe(out);

      final String nameString = shaderName.equals("") ? "" : " [ " + shaderName + " ]";
      throw new Ardor3dException("Error compiling GLSL shader " + nameString + ": " + out);
    }
  }
示例#9
0
  public final void enableState(GL gl, boolean enable, Object ext) {
    final GL2ES2 glsl = gl.getGL2ES2();
    final ShaderState st = (ShaderState) ext;

    if (enable) {
      st.enableVertexAttribArray(glsl, ad);
    } else {
      st.disableVertexAttribArray(glsl, ad);
    }
  }
示例#10
0
  public static boolean isShaderStatusValid(
      final GL _gl, final int shaderObj, final int name, final PrintStream verboseOut) {
    final GL2ES2 gl = _gl.getGL2ES2();
    final int[] ires = new int[1];
    gl.glGetShaderiv(shaderObj, name, ires, 0);

    final boolean res = ires[0] == 1;
    if (!res && null != verboseOut) {
      verboseOut.println("Shader status invalid: " + getShaderInfoLog(gl, shaderObj));
    }
    return res;
  }
示例#11
0
  public static String getProgramInfoLog(final GL _gl, final int programObj) {
    final GL2ES2 gl = _gl.getGL2ES2();
    final int[] infoLogLength = new int[1];
    gl.glGetProgramiv(programObj, GL2ES2.GL_INFO_LOG_LENGTH, infoLogLength, 0);

    if (infoLogLength[0] == 0) {
      return "(no info log)";
    }
    final int[] charsWritten = new int[1];
    final byte[] infoLogBytes = new byte[infoLogLength[0]];
    gl.glGetProgramInfoLog(programObj, infoLogLength[0], charsWritten, 0, infoLogBytes, 0);

    return new String(infoLogBytes, 0, charsWritten[0]);
  }
  /** Removes the tessellation evaluation shader */
  private static void removeTessEvalShader(final GLSLShaderObjectsState state) {
    final GL gl = GLContext.getCurrentGL();

    if (state._tessellationEvaluationShaderID != -1) {
      if (gl.isGL2()) {
        gl.getGL2().glDetachObjectARB(state._programID, state._tessellationEvaluationShaderID);
        gl.getGL2().glDeleteObjectARB(state._tessellationEvaluationShaderID);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glDetachShader(state._programID, state._tessellationEvaluationShaderID);
          gl.getGL2ES2().glDeleteShader(state._tessellationEvaluationShaderID);
        }
      }
    }
  }
示例#13
0
 /**
  * Performs {@link GL2ES2#glValidateProgram(int)}
  *
  * <p>One shall only call this method while debugging and only if all required resources by the
  * shader are set.
  *
  * <p>Note: It is possible that a working shader program will fail validation. This has been
  * experienced on NVidia APX2500 and Tegra2.
  *
  * @see GL2ES2#glValidateProgram(int)
  */
 public static boolean isProgramExecStatusValid(
     final GL _gl, final int programObj, final PrintStream verboseOut) {
   final GL2ES2 gl = _gl.getGL2ES2();
   gl.glValidateProgram(programObj);
   if (!isProgramStatusValid(gl, programObj, GL2ES2.GL_VALIDATE_STATUS)) {
     if (null != verboseOut) {
       verboseOut.println(
           "Program validation failed: "
               + programObj
               + "\n\t"
               + getProgramInfoLog(gl, programObj));
     }
     return false;
   }
   return true;
 }
示例#14
0
 public static boolean isProgramLinkStatusValid(
     final GL _gl, final int programObj, final PrintStream verboseOut) {
   final GL2ES2 gl = _gl.getGL2ES2();
   if (!gl.glIsProgram(programObj)) {
     if (null != verboseOut) {
       verboseOut.println("Program name invalid: " + programObj);
     }
     return false;
   }
   if (!isProgramStatusValid(gl, programObj, GL2ES2.GL_LINK_STATUS)) {
     if (null != verboseOut) {
       verboseOut.println(
           "Program link failed: " + programObj + "\n\t" + getProgramInfoLog(gl, programObj));
     }
     return false;
   }
   return true;
 }
示例#15
0
  public final void syncData(GL gl, boolean enable, boolean force, Object ext) {
    if (enable) {
      final GL2ES2 glsl = gl.getGL2ES2();
      final ShaderState st = (ShaderState) ext;

      st.vertexAttribPointer(glsl, ad);
      /**
       * Due to probable application VBO switching, this might not make any sense ..
       *
       * <p>if(force) { st.vertexAttribPointer(glsl, ad); } else if(st.getAttribLocation(glsl, ad)
       * >= 0) { final int[] qi = new int[1]; glsl.glGetVertexAttribiv(ad.getLocation(),
       * GL2ES2.GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, qi, 0); if(ad.getVBOName() != qi[0]) {
       * System.err.println("XXX1: "+ad.getName()+", vbo ad "+ad.getVBOName()+", gl "+qi[0]+",
       * "+ad); st.vertexAttribPointer(glsl, ad); } else { System.err.println("XXX0:
       * "+ad.getName()+", vbo ad "+ad.getVBOName()+", gl "+qi[0]+", "+ad); } }
       */
    }
  }
示例#16
0
  public static void shaderBinary(
      final GL _gl, final IntBuffer shaders, final int binFormat, final java.nio.Buffer bin) {
    final GL2ES2 gl = _gl.getGL2ES2();
    if (getShaderBinaryFormats(gl).size() <= 0) {
      throw new GLException("No binary formats are supported");
    }

    final int shaderNum = shaders.remaining();
    if (shaderNum <= 0) {
      throw new GLException("No shaders specified");
    }
    if (null == bin) {
      throw new GLException("Null shader binary");
    }
    final int binLength = bin.remaining();
    if (0 >= binLength) {
      throw new GLException("Empty shader binary (remaining == 0)");
    }
    gl.glShaderBinary(shaderNum, shaders, binFormat, bin, binLength);
  }
示例#17
0
  public static boolean createAndCompileShader(
      final GL _gl,
      final IntBuffer shader,
      final int shaderType,
      final CharSequence[][] sources,
      final PrintStream verboseOut) {
    final GL2ES2 gl = _gl.getGL2ES2();
    int err = gl.glGetError(); // flush previous errors ..
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println("createAndCompileShader: Pre GL Error: 0x" + Integer.toHexString(err));
    }

    createShader(gl, shaderType, shader);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR) {
      throw new GLException(
          "createAndCompileShader: CreateShader failed, GL Error: 0x" + Integer.toHexString(err));
    }

    shaderSource(gl, shader, sources);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR) {
      throw new GLException(
          "createAndCompileShader: ShaderSource failed, GL Error: 0x" + Integer.toHexString(err));
    }

    compileShader(gl, shader);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println(
          "createAndCompileShader: CompileShader failed, GL Error: 0x" + Integer.toHexString(err));
    }

    return isShaderStatusValid(gl, shader, GL2ES2.GL_COMPILE_STATUS, verboseOut)
        && err == GL.GL_NO_ERROR;
  }
示例#18
0
 public static void deleteShader(final GL _gl, final IntBuffer shaders) {
   final GL2ES2 gl = _gl.getGL2ES2();
   for (int i = shaders.position(); i < shaders.limit(); i++) {
     gl.glDeleteShader(shaders.get(i));
   }
 }
 public void setDepthRange(float start, float end) {
   GL gl = GLContext.getCurrentGL();
   gl.getGL2ES2().glDepthRange(start, end);
 }
示例#20
0
 public static void createShader(final GL _gl, final int type, final IntBuffer shaders) {
   final GL2ES2 gl = _gl.getGL2ES2();
   for (int i = shaders.position(); i < shaders.limit(); i++) {
     shaders.put(i, gl.glCreateShader(type));
   }
 }
  protected static void sendToGL(
      final GLSLShaderObjectsState state, final ContextCapabilities caps) {
    final GL gl = GLContext.getCurrentGL();

    if (state.getVertexShader() == null && state.getFragmentShader() == null) {
      logger.warning("Could not find shader resources!" + "(both inputbuffers are null)");
      state._needSendShader = false;
      return;
    }

    if (state._programID == -1) {
      if (gl.isGL2()) {
        state._programID = gl.getGL2().glCreateProgramObjectARB();
      } else {
        if (gl.isGL2ES2()) {
          state._programID = gl.getGL2ES2().glCreateProgram();
        }
      }
    }

    if (state.getVertexShader() != null) {
      if (state._vertexShaderID != -1) {
        removeVertShader(state);
      }
      if (gl.isGL2()) {
        state._vertexShaderID = gl.getGL2().glCreateShaderObjectARB(GL2ES2.GL_VERTEX_SHADER);
      } else {
        if (gl.isGL2ES2()) {
          state._vertexShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_VERTEX_SHADER);
        }
      }

      // Create the sources
      final byte array[] = new byte[state.getVertexShader().limit()];
      state.getVertexShader().rewind();
      state.getVertexShader().get(array);
      if (gl.isGL2()) {
        gl.getGL2()
            .glShaderSourceARB(
                state._vertexShaderID,
                1,
                new String[] {new String(array)},
                new int[] {array.length},
                0);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2()
              .glShaderSource(
                  state._vertexShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        }
      }

      // Compile the vertex shader
      final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
      final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
      compiled.clear();
      if (gl.isGL2()) {
        gl.getGL2().glCompileShaderARB(state._vertexShaderID);
        gl.getGL2()
            .glGetObjectParameterivARB(
                state._vertexShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glCompileShader(state._vertexShaderID);
          gl.getGL2ES2().glGetShaderiv(state._vertexShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
        }
      }
      checkProgramError(compiled.get(0), state._vertexShaderID, state._vertexShaderName);

      // Attach the program
      if (gl.isGL2()) {
        gl.getGL2().glAttachObjectARB(state._programID, state._vertexShaderID);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glAttachShader(state._programID, state._vertexShaderID);
        }
      }
    } else if (state._vertexShaderID != -1) {
      removeVertShader(state);
      state._vertexShaderID = -1;
    }

    if (state.getFragmentShader() != null) {
      if (state._fragmentShaderID != -1) {
        removeFragShader(state);
      }

      if (gl.isGL2()) {
        state._fragmentShaderID = gl.getGL2().glCreateShaderObjectARB(GL2ES2.GL_FRAGMENT_SHADER);
      } else {
        if (gl.isGL2ES2()) {
          state._fragmentShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_FRAGMENT_SHADER);
        }
      }

      // Create the sources
      final byte array[] = new byte[state.getFragmentShader().limit()];
      state.getFragmentShader().rewind();
      state.getFragmentShader().get(array);
      if (gl.isGL2()) {
        gl.getGL2()
            .glShaderSourceARB(
                state._fragmentShaderID,
                1,
                new String[] {new String(array)},
                new int[] {array.length},
                0);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2()
              .glShaderSource(
                  state._fragmentShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        }
      }

      // Compile the fragment shader
      final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
      final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
      compiled.clear();
      if (gl.isGL2()) {
        gl.getGL2().glCompileShaderARB(state._fragmentShaderID);
        gl.getGL2()
            .glGetObjectParameterivARB(
                state._fragmentShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glCompileShader(state._fragmentShaderID);
          gl.getGL2ES2().glGetShaderiv(state._fragmentShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
        }
      }
      checkProgramError(compiled.get(0), state._fragmentShaderID, state._vertexShaderName);

      // Attach the program
      if (gl.isGL2()) {
        gl.getGL2().glAttachObjectARB(state._programID, state._fragmentShaderID);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glAttachShader(state._programID, state._fragmentShaderID);
        }
      }
    } else if (state._fragmentShaderID != -1) {
      removeFragShader(state);
      state._fragmentShaderID = -1;
    }

    if (caps.isGeometryShader4Supported()) {
      if (state.getGeometryShader() != null) {
        if (state._geometryShaderID != -1) {
          removeGeomShader(state);
        }

        if (gl.isGL2()) {
          state._geometryShaderID = gl.getGL2().glCreateShaderObjectARB(GL3.GL_GEOMETRY_SHADER);
        } else {
          if (gl.isGL2ES2()) {
            state._geometryShaderID = gl.getGL2ES2().glCreateShader(GL3.GL_GEOMETRY_SHADER);
          }
        }

        // Create the sources
        final byte array[] = new byte[state.getGeometryShader().limit()];
        state.getGeometryShader().rewind();
        state.getGeometryShader().get(array);
        if (gl.isGL2()) {
          gl.getGL2()
              .glShaderSourceARB(
                  state._geometryShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2()
                .glShaderSource(
                    state._geometryShaderID,
                    1,
                    new String[] {new String(array)},
                    new int[] {array.length},
                    0);
          }
        }

        // Compile the geometry shader
        final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
        final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
        compiled.clear();
        if (gl.isGL2()) {
          gl.getGL2().glCompileShaderARB(state._geometryShaderID);
          gl.getGL2()
              .glGetObjectParameterivARB(
                  state._geometryShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glCompileShader(state._geometryShaderID);
            gl.getGL2ES2()
                .glGetShaderiv(state._geometryShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
          }
        }
        checkProgramError(compiled.get(0), state._geometryShaderID, state._geometryShaderName);

        // Attach the program
        if (gl.isGL2()) {
          gl.getGL2().glAttachObjectARB(state._programID, state._geometryShaderID);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glAttachShader(state._programID, state._geometryShaderID);
          }
        }
      } else if (state._geometryShaderID != -1) {
        removeGeomShader(state);
        state._geometryShaderID = -1;
      }
    }

    if (caps.isTessellationShadersSupported()) {
      if (state.getTessellationControlShader() != null) {
        if (state._tessellationControlShaderID != -1) {
          removeTessControlShader(state);
        }

        if (gl.isGL2()) {
          state._tessellationControlShaderID =
              gl.getGL2().glCreateShaderObjectARB(GL4.GL_TESS_CONTROL_SHADER);
        } else {
          if (gl.isGL2ES2()) {
            state._tessellationControlShaderID =
                gl.getGL2ES2().glCreateShader(GL4.GL_TESS_CONTROL_SHADER);
          }
        }

        // Create the sources
        final byte array[] = new byte[state.getTessellationControlShader().limit()];
        state.getTessellationControlShader().rewind();
        state.getTessellationControlShader().get(array);
        if (gl.isGL2()) {
          gl.getGL2()
              .glShaderSourceARB(
                  state._tessellationControlShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2()
                .glShaderSource(
                    state._tessellationControlShaderID,
                    1,
                    new String[] {new String(array)},
                    new int[] {array.length},
                    0);
          }
        }

        // Compile the tessellation control shader
        final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
        final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
        compiled.clear();
        if (gl.isGL2()) {
          gl.getGL2().glCompileShaderARB(state._tessellationControlShaderID);
          gl.getGL2()
              .glGetObjectParameterivARB(
                  state._tessellationControlShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glCompileShader(state._tessellationControlShaderID);
            gl.getGL2ES2()
                .glGetShaderiv(
                    state._tessellationControlShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
          }
        }
        checkProgramError(
            compiled.get(0),
            state._tessellationControlShaderID,
            state._tessellationControlShaderName);

        // Attach the program
        if (gl.isGL2()) {
          gl.getGL2().glAttachObjectARB(state._programID, state._tessellationControlShaderID);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glAttachShader(state._programID, state._tessellationControlShaderID);
          }
        }
      } else if (state._tessellationControlShaderID != -1) {
        removeTessControlShader(state);
        state._tessellationControlShaderID = -1;
      }
      if (state.getTessellationEvaluationShader() != null) {
        if (state._tessellationEvaluationShaderID != -1) {
          removeTessEvalShader(state);
        }

        if (gl.isGL2()) {
          state._tessellationEvaluationShaderID =
              gl.getGL2().glCreateShaderObjectARB(GL4.GL_TESS_CONTROL_SHADER);
        } else {
          if (gl.isGL2ES2()) {
            state._tessellationEvaluationShaderID =
                gl.getGL2ES2().glCreateShader(GL4.GL_TESS_CONTROL_SHADER);
          }
        }

        // Create the sources
        final byte array[] = new byte[state.getTessellationEvaluationShader().limit()];
        state.getTessellationEvaluationShader().rewind();
        state.getTessellationEvaluationShader().get(array);
        if (gl.isGL2()) {
          gl.getGL2()
              .glShaderSourceARB(
                  state._tessellationEvaluationShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2()
                .glShaderSource(
                    state._tessellationEvaluationShaderID,
                    1,
                    new String[] {new String(array)},
                    new int[] {array.length},
                    0);
          }
        }

        // Compile the tessellation control shader
        final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
        final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
        compiled.clear();
        if (gl.isGL2()) {
          gl.getGL2().glCompileShaderARB(state._tessellationEvaluationShaderID);
          gl.getGL2()
              .glGetObjectParameterivARB(
                  state._tessellationEvaluationShaderID,
                  GL2.GL_OBJECT_COMPILE_STATUS_ARB,
                  compiled);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glCompileShader(state._tessellationEvaluationShaderID);
            gl.getGL2ES2()
                .glGetShaderiv(
                    state._tessellationEvaluationShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
          }
        }
        checkProgramError(
            compiled.get(0),
            state._tessellationEvaluationShaderID,
            state._tessellationEvaluationShaderName);

        // Attach the program
        if (gl.isGL2()) {
          gl.getGL2().glAttachObjectARB(state._programID, state._tessellationEvaluationShaderID);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glAttachShader(state._programID, state._tessellationEvaluationShaderID);
          }
        }
      } else if (state._tessellationEvaluationShaderID != -1) {
        removeTessEvalShader(state);
        state._tessellationEvaluationShaderID = -1;
      }
    }

    if (gl.isGL2()) {
      gl.getGL2().glLinkProgramARB(state._programID);
    } else {
      if (gl.isGL2ES2()) {
        gl.getGL2ES2().glLinkProgram(state._programID);
      }
    }
    checkLinkError(state._programID);
    state.setNeedsRefresh(true);
    state._needSendShader = false;
  }
示例#22
0
 public static void detachShader(final GL _gl, final int program, final IntBuffer shaders) {
   final GL2ES2 gl = _gl.getGL2ES2();
   for (int i = shaders.position(); i < shaders.limit(); i++) {
     gl.glDetachShader(program, shaders.get(i));
   }
 }