Example usage for android.opengl GLES20 glDeleteShader

List of usage examples for android.opengl GLES20 glDeleteShader

Introduction

In this page you can find the example usage for android.opengl GLES20 glDeleteShader.

Prototype

public static native void glDeleteShader(int shader);

Source Link

Usage

From source file:Main.java

public static int loadShader(String vss, String fss) {
    int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vshader, vss);
    GLES20.glCompileShader(vshader);//from   ww w .j  ava 2  s  .c  om
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e("Shader", "Could not compile vshader");
        Log.v("Shader", "Could not compile vshader:" + GLES20.glGetShaderInfoLog(vshader));
        GLES20.glDeleteShader(vshader);
        vshader = 0;
    }

    int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fshader, fss);
    GLES20.glCompileShader(fshader);
    GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e("Shader", "Could not compile fshader");
        Log.v("Shader", "Could not compile fshader:" + GLES20.glGetShaderInfoLog(fshader));
        GLES20.glDeleteShader(fshader);
        fshader = 0;
    }

    int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vshader);
    GLES20.glAttachShader(program, fshader);
    GLES20.glLinkProgram(program);

    return program;
}

From source file:Main.java

private static int compileShader(int target, String source, int[] output) {
    output[0] = GLES20.glCreateShader(target);

    //   const GLchar *str = src.c_str();
    GLES20.glShaderSource(output[0], source);
    GLES20.glCompileShader(output[0]);//  w  w w  .  j a  va  2  s  .co  m
    checkGLError("Compile shader");
    int[] status = new int[1];
    GLES20.glGetShaderiv(output[0], GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, "Failed to compile shader: " + GLES20.glGetShaderInfoLog(output[0]));
        GLES20.glDeleteShader(output[0]);
    }
    return status[0];
}

From source file:Main.java

public static int loadShader(int type, String shaderCode) {
    int shaderHandle = GLES20.glCreateShader(type);

    if (shaderHandle != 0) {
        GLES20.glShaderSource(shaderHandle, shaderCode);
        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.v(TAG, "Shader fail info: " + GLES20.glGetShaderInfoLog(shaderHandle));
            GLES20.glDeleteShader(shaderHandle);
            shaderHandle = 0;//from  w  w  w.  j ava 2 s .  c  o m
        }
    }

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

From source file:Main.java

public static int createShader(int type, String source) {
    String typeString;//w w  w  . jav a2 s  .c o m
    if (type == GLES20.GL_VERTEX_SHADER)
        typeString = "vertex";
    else if (type == GLES20.GL_FRAGMENT_SHADER)
        typeString = "fragment";
    else
        throw new RuntimeException("Unknown shader type");

    int sh = GLES20.glCreateShader(type);
    if (sh <= 0) {
        throw new RuntimeException("Could not create " + typeString + " shader");
    }
    GLES20.glShaderSource(sh, source);
    GLES20.glCompileShader(sh);
    int[] status = new int[1];
    GLES20.glGetShaderiv(sh, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] <= 0) {
        String message = GLES20.glGetShaderInfoLog(sh);
        GLES20.glDeleteShader(sh);
        throw new RuntimeException("Could not compile " + typeString + " shader: " + message);
    }

    return sh;
}

From source file:Main.java

/**
 * 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./* ww w  .  j  a  v  a  2 s. c om*/
 */
public static int loadGLShader(Context con, int type, int resId) {
    String code = readRawTextFile(con, 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;
}

From source file:Main.java

/**
 * Compiles the provided shader source./*from www .  j a  va 2s.  com*/
 *
 * @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;
}

From source file:Main.java

public static int loadProgram(final String strVSource, final String strFSource) {
    int iVShader;
    int iFShader;
    int iProgId;//from  w w  w .j  a  v  a  2 s . c o  m
    int[] link = new int[1];
    iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER);
    if (iVShader == 0) {
        Log.d("Load Program", "Vertex Shader Failed");
        return 0;
    }
    iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER);
    if (iFShader == 0) {
        Log.d("Load Program", "Fragment Shader Failed");
        return 0;
    }

    iProgId = GLES20.glCreateProgram();

    GLES20.glAttachShader(iProgId, iVShader);
    GLES20.glAttachShader(iProgId, iFShader);

    GLES20.glLinkProgram(iProgId);

    GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0);
    if (link[0] <= 0) {
        Log.d("Load Program", "Linking Failed");
        return 0;
    }
    GLES20.glDeleteShader(iVShader);
    GLES20.glDeleteShader(iFShader);
    return iProgId;
}

From source file:Main.java

static int initShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    if (shader != 0) {
        GLES20.glShaderSource(shader, source);
        GLES20.glCompileShader(shader);//from  w ww.j av  a  2s . c om

        int[] glStatusVar = { GLES20.GL_FALSE };
        GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, glStatusVar, 0);
        if (glStatusVar[0] == GLES20.GL_FALSE) {
            Log.e(LOGTAG, "Could NOT compile shader " + shaderType + " : " + GLES20.glGetShaderInfoLog(shader));
            GLES20.glDeleteShader(shader);
            shader = 0;
        }

    }

    return shader;
}

From source file:Main.java

public static int createProgram(String vertSrc, String fragSrc, String[] attributeNames, int[] attributeBinding,
        String[] uniformNames, int[] uniformBinding) {

    int program = GLES20.glCreateProgram();

    int status = 1;
    int[] vertSh = new int[1];
    int[] fragSh = new int[1];
    status *= compileShader(GLES20.GL_VERTEX_SHADER, vertSrc, vertSh);
    status *= compileShader(GLES20.GL_FRAGMENT_SHADER, fragSrc, fragSh);
    checkGLError("Compiling shaders");

    GLES20.glAttachShader(program, vertSh[0]);
    checkGLError("Attach shader");
    GLES20.glAttachShader(program, fragSh[0]);
    checkGLError("Attach shader fragment");

    //Bind attributes
    for (int i = 0; i < attributeNames.length; i++) {
        GLES20.glBindAttribLocation(program, attributeBinding[i], attributeNames[i]);
        checkGLError("Bind attribute: " + attributeNames[i]);
    }//from   www  . ja v  a 2 s .co m
    status *= linkProgram(program);

    status *= validateProgram(program);

    //location of uniforms
    if (status > 0) {
        for (int i = 0; i < uniformNames.length; i++) {
            //         if (uniformsLocations.at(i).first.length()) {
            int loc = GLES20.glGetUniformLocation(program, uniformNames[i]);
            checkGLError("glGetUniformLocation - " + uniformNames[i]);
            if (loc < 0)
                Log.e(TAG, "Bad uniform " + uniformNames[i]);
            uniformBinding[i] = loc;
        }
    } else {
        GLES20.glDeleteProgram(program);
        program = 0;
    }

    if (vertSh[0] > 0) {
        GLES20.glDeleteShader(vertSh[0]);
        GLES20.glDetachShader(program, vertSh[0]);
    }
    if (fragSh[0] > 0) {
        GLES20.glDeleteShader(fragSh[0]);
        GLES20.glDetachShader(program, fragSh[0]);
    }
    checkGLError("Shaders deleted");
    return program;
}

From source file:Main.java

public static int buildShader(int type, String shaderSource) {
    final int shader = GLES20.glCreateShader(type);
    if (shader == 0) {
        return 0;
    }/*from w w w.j  ava  2  s .c om*/

    GLES20.glShaderSource(shader, shaderSource);
    GLES20.glCompileShader(shader);

    int[] status = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        return 0;
    }

    return shader;
}