Java Code Examples for android.opengl.GLES20#glShaderSource()

The following examples show how to use android.opengl.GLES20#glShaderSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Shader.java    From smartGL with Apache License 2.0 6 votes vote down vote up
private int loadShader(int shaderType, String script) {

		int shader = GLES20.glCreateShader(shaderType);
		Assert.assertTrue(shader != 0);

		GLES20.glShaderSource(shader, script);
		GLES20.glCompileShader(shader);
		int[] compiled = new int[1];
		GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
		if (compiled[0] == 0) {
			Log.e(TAG, "Could not compile shader " + shaderType + " : " + GLES20.glGetShaderInfoLog(shader));
			GLES20.glDeleteShader(shader);
			shader = 0;
			Assert.assertTrue(false);
		}
		return shader;
	}
 
Example 2
Source File: ShaderUtil.java    From justaline-android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a raw text file, saved as a resource, into an OpenGL ES shader.
 *
 * @param type  The type of shader we will be creating.
 * @param resId The resource ID of the raw text file about to be turned into a shader.
 * @return The shader object handler.
 */
static int loadGLShader(String tag, Context context, int type, int resId) {
    String code = readRawTextFile(context, resId);
    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, code);
    GLES20.glCompileShader(shader);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    // If the compilation failed, delete the shader.
    if (compileStatus[0] == 0) {
        Log.e(tag, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        shader = 0;
    }

    if (shader == 0) {
        throw new RuntimeException("Error creating shader.");
    }

    return shader;
}
 
Example 3
Source File: SurfaceTextureRenderer.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e(TAG, "Could not compile shader " + shaderType + ":");
        Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        // TODO: handle this more gracefully
        throw new IllegalStateException("Could not compile shader " + shaderType);
    }
    return shader;
}
 
Example 4
Source File: VideoRendererGui.java    From droidkit-webrtc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void addShaderTo(
    int type, String source, int program) {
  int[] result = new int[] {
      GLES20.GL_FALSE
  };
  int shader = GLES20.glCreateShader(type);
  GLES20.glShaderSource(shader, source);
  GLES20.glCompileShader(shader);
  GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, 0);
  abortUnless(result[0] == GLES20.GL_TRUE,
      GLES20.glGetShaderInfoLog(shader) + ", source: " + source);
  GLES20.glAttachShader(program, shader);
  GLES20.glDeleteShader(shader);

  checkNoGLES2Error();
}
 
Example 5
Source File: SurfaceTextureRender.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        if (DEBUG) {
            Log.e(TAG, "Could not compile shader " + shaderType + ":");
            Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
        }
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example 6
Source File: ShaderUtil.java    From ARCore-Location with MIT License 6 votes vote down vote up
/**
 * Converts a raw text file, saved as a resource, into an OpenGL ES shader.
 *
 * @param type The type of shader we will be creating.
 * @param resId The resource ID of the raw text file about to be turned into a shader.
 * @return The shader object handler.
 */
public static int loadGLShader(String tag, Context context, int type, int resId) {
    String code = readRawTextFile(context, resId);
    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, code);
    GLES20.glCompileShader(shader);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    // If the compilation failed, delete the shader.
    if (compileStatus[0] == 0) {
        Log.e(tag, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        shader = 0;
    }

    if (shader == 0) {
        throw new RuntimeException("Error creating shader.");
    }

    return shader;
}
 
Example 7
Source File: GlUtil.java    From sealrtc-android with MIT License 6 votes vote down vote up
/**
 * Compiles the provided shader source.
 *
 * @return A handle to the shader, or 0 on failure.
 */
public static int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e(TAG, "Could not compile shader " + shaderType + ":");
        Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example 8
Source File: GLDrawer2D.java    From UVCCameraZxing with Apache License 2.0 5 votes vote down vote up
/**
 * load, compile and link shader
 * @param vss source of vertex shader
 * @param fss source of fragment shader
 * @return
 */
public static int loadShader(final String vss, final String fss) {
	if (DEBUG) Log.v(TAG, "loadShader:");
	int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
	GLES20.glShaderSource(vs, vss);
	GLES20.glCompileShader(vs);
	final int[] compiled = new int[1];
	GLES20.glGetShaderiv(vs, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		if (DEBUG) Log.e(TAG, "Failed to compile vertex shader:"
				+ GLES20.glGetShaderInfoLog(vs));
		GLES20.glDeleteShader(vs);
		vs = 0;
	}

	int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
	GLES20.glShaderSource(fs, fss);
	GLES20.glCompileShader(fs);
	GLES20.glGetShaderiv(fs, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		if (DEBUG) Log.w(TAG, "Failed to compile fragment shader:"
			+ GLES20.glGetShaderInfoLog(fs));
		GLES20.glDeleteShader(fs);
		fs = 0;
	}

	final int program = GLES20.glCreateProgram();
	GLES20.glAttachShader(program, vs);
	GLES20.glAttachShader(program, fs);
	GLES20.glLinkProgram(program);

	return program;
}
 
Example 9
Source File: TextureRender.java    From LiveMultimedia with Apache License 2.0 5 votes vote down vote up
private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e(TAG, "Could not compile shader " + shaderType + ":");
        Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example 10
Source File: TextureRenderer.java    From VideoCompressor with Apache License 2.0 5 votes vote down vote up
private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example 11
Source File: GLHelper.java    From PhotoMovie with Apache License 2.0 5 votes vote down vote up
public static int loadShader(String shaderStr, int type) {
    int[] compiled = new int[1];
    int iShader = GLES20.glCreateShader(type);
    checkGlError("");
    GLES20.glShaderSource(iShader, shaderStr);
    checkGlError("");
    GLES20.glCompileShader(iShader);
    checkGlError("");
    GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    checkGlError("");
    if (compiled[0] == 0) {
        throw new RuntimeException("Load Shader Failed Compilation\n" + GLES20.glGetShaderInfoLog(iShader));
    }
    return iShader;
}
 
Example 12
Source File: TextureRender.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e(TAG, "Could not compile shader " + shaderType + ":");
        Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example 13
Source File: GLDrawer2D.java    From AudioVideoPlayerSample with Apache License 2.0 5 votes vote down vote up
/**
 * load, compile and link shader
 * @param vss source of vertex shader
 * @param fss source of fragment shader
 * @return
 */
public static int loadShader(String vss, String fss) {
	if (DEBUG) Log.v(TAG, "loadShader:");
	int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
	GLES20.glShaderSource(vs, vss);
	GLES20.glCompileShader(vs);
	final int[] compiled = new int[1];
	GLES20.glGetShaderiv(vs, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		if (DEBUG) Log.e(TAG, "Failed to compile vertex shader:"
				+ GLES20.glGetShaderInfoLog(vs));
		GLES20.glDeleteShader(vs);
		vs = 0;
	}

	int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
	GLES20.glShaderSource(fs, fss);
	GLES20.glCompileShader(fs);
	GLES20.glGetShaderiv(fs, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		if (DEBUG) Log.w(TAG, "Failed to compile fragment shader:"
			+ GLES20.glGetShaderInfoLog(fs));
		GLES20.glDeleteShader(fs);
		fs = 0;
	}

	final int program = GLES20.glCreateProgram();
	GLES20.glAttachShader(program, vs);
	GLES20.glAttachShader(program, fs);
	GLES20.glLinkProgram(program);

	return program;
}
 
Example 14
Source File: GLUtil.java    From PhotoMovie with Apache License 2.0 5 votes vote down vote up
public static int loadShader(final String strSource, final int iType) {
    int[] compiled = new int[1];
    int iShader = GLES20.glCreateShader(iType);
    GLES20.glShaderSource(iShader, strSource);
    GLES20.glCompileShader(iShader);
    GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.d("Load Shader Failed", "Compilation\n" + GLES20.glGetShaderInfoLog(iShader));
        return 0;
    }
    return iShader;
}
 
Example 15
Source File: GLUtil.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper function to compile a shader.
 *
 * @param shaderType   The shader type.
 * @param shaderSource The shader source code.
 * @return An OpenGL handle to the shader.
 */
public static int compileShader(final int shaderType, final String shaderSource) {
    int shaderHandle = GLES20.glCreateShader(shaderType);

    if (shaderHandle != 0) {
        // Pass in the shader source.
        GLES20.glShaderSource(shaderHandle, shaderSource);

        // Compile the shader.
        GLES20.glCompileShader(shaderHandle);

        // Get the compilation status.
        final int[] compileStatus = new int[1];
        GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

        // If the compilation failed, delete the shader.
        if (compileStatus[0] == 0) {
            Log.e(TAG, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shaderHandle));
            GLES20.glDeleteShader(shaderHandle);
            shaderHandle = 0;
        }
    }

    if (shaderHandle == 0) {
        throw new RuntimeException("Error creating shader.");
    }

    return shaderHandle;
}
 
Example 16
Source File: GLES20Canvas.java    From PhotoMovie with Apache License 2.0 5 votes vote down vote up
private static int loadShader(int type, String shaderCode) {
    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    checkError();
    GLES20.glCompileShader(shader);
    checkError();

    return shader;
}
 
Example 17
Source File: TextureManager.java    From libstreaming with Apache License 2.0 5 votes vote down vote up
private int loadShader(int shaderType, String source) {
	int shader = GLES20.glCreateShader(shaderType);
	checkGlError("glCreateShader type=" + shaderType);
	GLES20.glShaderSource(shader, source);
	GLES20.glCompileShader(shader);
	int[] compiled = new int[1];
	GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		Log.e(TAG, "Could not compile shader " + shaderType + ":");
		Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
		GLES20.glDeleteShader(shader);
		shader = 0;
	}
	return shader;
}
 
Example 18
Source File: GlUtil.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
public static int loadShader(int shaderType, String source) {
  int shader = GLES20.glCreateShader(shaderType);
  checkGlError("glCreateShader type=" + shaderType);
  GLES20.glShaderSource(shader, source);
  GLES20.glCompileShader(shader);
  int[] compiled = new int[1];
  GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
  if (compiled[0] == 0) {
    Log.e(TAG, "Could not compile shader " + shaderType + ":");
    Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
    GLES20.glDeleteShader(shader);
    shader = 0;
  }
  return shader;
}
 
Example 19
Source File: RRGLProgram.java    From RedReader with GNU General Public License v3.0 5 votes vote down vote up
private void compileAndAttachShader(final int type, final String source) {

		switch(type) {
			case GLES20.GL_FRAGMENT_SHADER: if(mFragmentShaderHandle != null) throw new RuntimeException(); break;
			case GLES20.GL_VERTEX_SHADER:   if(mVertexShaderHandle != null) throw new RuntimeException(); break;
			default: throw new RuntimeException("Unknown shader type.");
		}

		final int shaderHandle = GLES20.glCreateShader(type);
		if(shaderHandle == 0) {
			throw new RuntimeException("Error creating shader.");
		}

		GLES20.glShaderSource(shaderHandle, source);
		GLES20.glCompileShader(shaderHandle);

		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

		if(compileStatus[0] == 0) {
			final String log = GLES20.glGetShaderInfoLog(mHandle);
			GLES20.glDeleteShader(shaderHandle);
			throw new RuntimeException(String.format(Locale.US, "Shader compile error: \"%s\".", log));
		}

		GLES20.glAttachShader(mHandle, shaderHandle);

		switch(type) {
			case GLES20.GL_FRAGMENT_SHADER: mFragmentShaderHandle = shaderHandle; break;
			case GLES20.GL_VERTEX_SHADER:   mVertexShaderHandle = shaderHandle; break;
			default: throw new RuntimeException("Unknown shader type.");
		}
	}
 
Example 20
Source File: CameraGLRendererBase.java    From OpenCvFaceDetect with Apache License 2.0 4 votes vote down vote up
private static int loadShader(String vss, String fss) {
    Log.d("CameraGLRendererBase", "loadShader");
    int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vshader, vss);
    GLES20.glCompileShader(vshader);
    int[] status = new int[1];
    GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e("CameraGLRendererBase", "Could not compile vertex shader: "+GLES20.glGetShaderInfoLog(vshader));
        GLES20.glDeleteShader(vshader);
        vshader = 0;
        return 0;
    }

    int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fshader, fss);
    GLES20.glCompileShader(fshader);
    GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e("CameraGLRendererBase", "Could not compile fragment shader:"+GLES20.glGetShaderInfoLog(fshader));
        GLES20.glDeleteShader(vshader);
        GLES20.glDeleteShader(fshader);
        fshader = 0;
        return 0;
    }

    int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vshader);
    GLES20.glAttachShader(program, fshader);
    GLES20.glLinkProgram(program);
    GLES20.glDeleteShader(vshader);
    GLES20.glDeleteShader(fshader);
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e("CameraGLRendererBase", "Could not link shader program: "+GLES20.glGetProgramInfoLog(program));
        program = 0;
        return 0;
    }
    GLES20.glValidateProgram(program);
    GLES20.glGetProgramiv(program, GLES20.GL_VALIDATE_STATUS, status, 0);
    if (status[0] == 0)
    {
        Log.e("CameraGLRendererBase", "Shader program validation error: "+GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
        return 0;
    }

    Log.d("CameraGLRendererBase", "Shader program is built OK");

    return program;
}