2016-11-07 46 views
0

我有一个RecyclerView填充整数,指向我使用哪个片段着色器为GLSurfaceView我连接到MediaPlayer。在我GLSurfaceView.Renderer,我把下面的代码:GLSurfaceView.Renderer - 动态更改着色器程序

public void onFragmentShaderChanged(int filterPosition) 
{ 
    mFragmentShader = VideoUtils.getFragmentShader(mContext, filterPosition); 
    GLES20.glDeleteProgram(mProgram); 
    mProgram = createProgram(mVertexShader, VideoUtils.getFragmentShader(mContext, filterPosition)); 
    if (mProgram == 0) { 
     return; 
    } 
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); 
    checkGlError("glGetAttribLocation aPosition"); 
    if (maPositionHandle == -1) { 
     throw new RuntimeException("Could not get attrib location for aPosition"); 
    } 
    maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord"); 
    checkGlError("glGetAttribLocation aTextureCoord"); 
    if (maTextureHandle == -1) { 
     throw new RuntimeException("Could not get attrib location for aTextureCoord"); 
    } 

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
    checkGlError("glGetUniformLocation uMVPMatrix"); 
    if (muMVPMatrixHandle == -1) { 
     throw new RuntimeException("Could not get attrib location for uMVPMatrix"); 
    } 

    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix"); 
    checkGlError("glGetUniformLocation uSTMatrix"); 
    if (muSTMatrixHandle == -1) { 
     throw new RuntimeException("Could not get attrib location for uSTMatrix"); 
    } 
} 

此代码触发,当我点击说RecyclerView的元素,从包含我需要根据位置的片段着色器的原始文件读取,然后用它来删除现有程序并创建一个新程序。在MediaPlayer仍在运行时,我正在执行此操作。

当我打电话这一点,但是,GLSurfaceView变绿权logcat的给了我后:

E/libEGL:打电话的OpenGL ES API没有当前上下文(每个线程记录一次)

设置setEGLContextClientVersion(2)网我setRenderer() has already been called in this thread或类似的东西。

我的问题:

  1. 我可以改变对飞GLSurfaceView.Renderer的计划?
  2. 如果我不能,那么更改渲染器本身,甚至更改渲染器的片段着色器如何?
  3. 如果上述方法不可行,我是否应该使用类似的渲染器重新创建GLSurfaceView,然后使用不同的片元着色器?

回答

0

原来在我的问题中调用了onFragmentShaderChanged()函数在EGL线程之外被调用。

感谢fadden在另一个问题中的回答,我必须改变onDrawFrame()函数中的程序,我观察滤镜位置整数变量的变化,并在变量确实发生变化时用新的碎片着色器创建程序点击。