2016-11-07 49 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()函數中的程序,我觀察濾鏡位置整數變量的變化,並在變量確實發生變化時用新的碎片着色器創建程序點擊。