Last active
June 11, 2018 07:11
-
-
Save jamieomatthews/5237002 to your computer and use it in GitHub Desktop.
Android GPUImage implementation of GPUImageTwoPassFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.cyberagent.android.gpuimage; | |
import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.CUBE; | |
import static jp.co.cyberagent.android.gpuimage.utils.TextureRotationUtils.TEXTURE_NO_ROTATION; | |
import java.nio.ByteBuffer; | |
import java.nio.ByteOrder; | |
import java.nio.FloatBuffer; | |
import java.nio.IntBuffer; | |
import jp.co.cyberagent.android.gpuimage.utils.TextureRotationUtils; | |
import android.opengl.GLES20; | |
import android.util.Log; | |
public class GPUImageTwoPassFilter1 extends GPUImageFilter { | |
final static String TAG = "GPUImageTwoPassFilter"; | |
//int secondFilterOutputTexture; | |
int secondFilterPositionAttribute; | |
int secondFilterTextureCoordinateAttribute; | |
int secondFilterInputTextureUniform; | |
int secondFilterInputTextureUniform2; | |
int secondFilterProgram; | |
private ByteBuffer mTexture2CoordinatesBuffer; | |
String secondStageVertexShader; | |
String secondStageFragmentShader; | |
private int[] secondFilterFramebuffer; | |
private int[] secondFilterOutputTexture; | |
private int outputTexture; | |
public GPUImageTwoPassFilter1(String firstStageVertexShaderString, String firstStageFragmentShaderString, String secondStageVertexShaderString, String secondStageFragmentShaderString){ | |
super(firstStageVertexShaderString, firstStageFragmentShaderString); | |
Log.d(TAG, "Full Constructor"); | |
secondStageVertexShader = secondStageVertexShaderString; | |
secondStageFragmentShader = secondStageFragmentShaderString; | |
} | |
public GPUImageTwoPassFilter1(String firstStageFragmentShaderString, String secondStageFragmentShaderString){ | |
this(NO_FILTER_VERTEX_SHADER, firstStageFragmentShaderString, NO_FILTER_VERTEX_SHADER, secondStageFragmentShaderString); | |
Log.d(TAG, "Second Constructor"); | |
secondStageVertexShader = NO_FILTER_VERTEX_SHADER; | |
secondStageFragmentShader = secondStageFragmentShaderString; | |
} | |
@Override | |
public void onInit() { | |
super.onInit(); | |
//create the second filter program | |
Log.d("Second Filter Program", "Pre Load"); | |
secondFilterProgram = OpenGlUtils.loadProgram(secondStageVertexShader, secondStageFragmentShader); | |
Log.d("Second Filter Program", secondFilterProgram + ""); | |
secondFilterPositionAttribute = GLES20.glGetAttribLocation(secondFilterProgram, "position"); | |
secondFilterTextureCoordinateAttribute = GLES20.glGetAttribLocation(secondFilterProgram, "inputTextureCoordinate"); | |
secondFilterInputTextureUniform = GLES20.glGetUniformLocation(secondFilterProgram, "inputImageTexture"); | |
secondFilterInputTextureUniform2 = GLES20.glGetUniformLocation(secondFilterProgram, "inputImageTexture2"); | |
//TODO: [GPUImageOpenGLESContext setActiveShaderProgram:secondFilterProgram]; ?? | |
GLES20.glUseProgram(secondFilterProgram); | |
GLES20.glEnableVertexAttribArray(secondFilterPositionAttribute); | |
GLES20.glEnableVertexAttribArray(secondFilterTextureCoordinateAttribute); | |
} | |
@Override | |
public void onDraw(final int textureId, final FloatBuffer cubeBuffer,final FloatBuffer textureBuffer){ | |
//do the first pass, as usual | |
super.onDraw(textureId, cubeBuffer, textureBuffer); | |
//createSecondFilterFBO | |
if(secondFilterFramebuffer == null){ | |
//createFilterFBOOfSize | |
secondFilterFramebuffer = new int[1]; | |
Log.d(TAG, "creating filter framebuffer"); | |
GLES20.glGenFramebuffers(1, secondFilterFramebuffer, 0); | |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, secondFilterFramebuffer[0]); | |
if(secondFilterOutputTexture == null){ | |
Log.d(TAG, "creating second output texture"); | |
secondFilterOutputTexture = new int[1]; | |
GLES20.glGenTextures(1, secondFilterOutputTexture, 0); | |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, secondFilterOutputTexture[0]); | |
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); | |
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); | |
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); | |
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); | |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); | |
} | |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, secondFilterOutputTexture[0]); | |
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null); | |
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, secondFilterOutputTexture[0], 0); | |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); | |
} | |
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER); | |
if(status != GLES20.GL_FRAMEBUFFER_COMPLETE){ | |
Log.d(TAG, "framebuffer failed to create"); | |
} | |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, secondFilterFramebuffer[0]); | |
//GLES20.glViewport(0, 0, width, height); | |
//change the program | |
GLES20.glUseProgram(secondFilterProgram); | |
//clear the old colors | |
GLES20.glClearColor(0, 0, 0, 1); | |
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); | |
GLES20.glActiveTexture(GLES20.GL_TEXTURE3); //ios version is GL_TEXTURE3 | |
//TODO: change this texture to be the correct input texture | |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, secondFilterOutputTexture[0]); | |
GLES20.glUniform1i(secondFilterInputTextureUniform, 3); | |
cubeBuffer.position(0); | |
GLES20.glVertexAttribPointer(secondFilterPositionAttribute, 2, GLES20.GL_FLOAT, false, 0, cubeBuffer); | |
GLES20.glEnableVertexAttribArray(secondFilterPositionAttribute); | |
textureBuffer.position(0); | |
//TODO: change the textureBuffer to be NO ROTATION | |
GLES20.glVertexAttribPointer(secondFilterTextureCoordinateAttribute, 2, GLES20.GL_FLOAT, false, 0, textureBuffer); | |
GLES20.glEnableVertexAttribArray(secondFilterTextureCoordinateAttribute); | |
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); | |
GLES20.glDisableVertexAttribArray(secondFilterPositionAttribute); | |
GLES20.glDisableVertexAttribArray(secondFilterTextureCoordinateAttribute); | |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); | |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); | |
} | |
} |
On how to use the previous texture, look at GPUImageFilterGroup. There in onDraw you see the previousTexture which is used as input for the onDraw method of the filters.
did you get this work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone looking at this code, I'm pretty sure the issue is that I have 2 different textures (the first one is the textureId passed into onDraw, the second is generated separately, represented as secondFilterOutputTexture). I'm not sure how to take the first texture in for a second pass.