0

I use FBO to render an image pass blur shader and the fps go down to 20.why? It was ok when I blured it through x-axis direction directly without FBO. It was also ok when I just use FBO to restore an image and draw it to sceen. Is there any thing I shoud pay attention to? Any help would be great! Thanks. Here is my code:

private static final int FB_SIZE = 200;
public SpriteBatch batch;
protected ShaderProgram mShaderA;
protected ShaderProgram mShaderB;
protected Mesh mMeshA;
protected Mesh mMeshB;
RenderSurface  blurTargetB;
RenderSurface  blurTargetC;
Texture texture;
public void init() {
    texture = new Texture(
            Gdx.files.internal("mainMenuBack.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    texture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
    blurTargetB = new RenderSurface(Format.RGBA4444, 1080, 1920, true);
    blurTargetC = new RenderSurface(Format.RGBA4444, 1080, 1920, true);
    if (mMeshA != null)
        mMeshA.dispose();
    mMeshA = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 2,
            ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(
            Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE
                    + "0"));

    mMeshA.setVertices(new float[] { -1f, -1f, 0, 1, 1f, -1f, 1, 1, 1f, 1f,
            1, 0, -1f, 1f, 0, 0 });

    if (mMeshB != null)
        mMeshB.dispose();
    mMeshB = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 2,
            ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(
            Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE
                    + "1"));
    mMeshB.setVertices(new float[] { -1f, -1f, 0, 1, 1f, -1f, 1, 1, 1f, 1f,
            1, 0, -1f, 1f, 0, 0 });

}

public BlurRenderer() {
    mShaderA = createXBlurShader();
    mShaderB = createYBlurShader();
    init();
    batch=new SpriteBatch();
}


public void render() {
    drawAToB();
    drawBToC();
    drawCToSceen();
}



public void drawAToB() {
    blurTargetB.begin(GL20.GL_COLOR_BUFFER_BIT);

    batch.setShader(mShaderA);

    batch.begin();

    batch.draw(texture, 0,0,1080,1920);
    batch.flush();
    blurTargetB.end();
}

public void drawBToC() {
    blurTargetC.begin(GL20.GL_COLOR_BUFFER_BIT);

    batch.draw( blurTargetB.getTexture(), 0, 0);
    batch.flush();
    blurTargetC.end();
}

public void dispose() {
    mMeshA.dispose();
    mMeshB.dispose();
}
public void drawCToSceen() {
    batch.draw( blurTargetC.getTexture(), 0, 0);
    batch.end();
}

public ShaderProgram createXBlurShader() {
    String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
        + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
        + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
        + "uniform float uBlurBufferSize;\n" // 1 / Size of the blur
        + "uniform mat4 u_projTrans;\n" //
        + "varying vec4 v_color;\n" //
        + "varying vec2 v_texCoords;\n" //
        + "varying vec2 vBlurTexCoords[5];\n" // output texture
        + "\n" //
        + "void main()\n" //
        + "{\n" //
        + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
        + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + " vBlurTexCoords[0] = v_texCoords + vec2(-2.0 * uBlurBufferSize, 0.0);\n"
            + " vBlurTexCoords[1] = v_texCoords + vec2(-1.0 * uBlurBufferSize, 0.0);\n"
            + " vBlurTexCoords[2] = v_texCoords;\n"
            + " vBlurTexCoords[3] = v_texCoords + vec2( 1.0 * uBlurBufferSize, 0.0);\n"
            + " vBlurTexCoords[4] = v_texCoords + vec2( 2.0 * uBlurBufferSize, 0.0);\n"
        + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
        + "}\n";
    String fragmentShader = "#ifdef GL_ES\n" //
        + "#define LOWP lowp\n" //
        + "precision mediump float;\n" //
        + "#else\n" //
        + "#define LOWP \n" //
        + "#endif\n" //
        + "varying LOWP vec4 v_color;\n" //
        + "varying vec2 v_texCoords;\n" //
        + "varying vec2 vBlurTexCoords[5];\n" // input texture coords
        + "uniform sampler2D u_texture;\n" //
        + "void main()\n"//
        + "{\n" //
            + "  vec4 sum = vec4(0.0);\n"
            + "  sum += texture2D(u_texture, vBlurTexCoords[0]) * 0.164074;\n"
            + "  sum += texture2D(u_texture, vBlurTexCoords[1]) * 0.216901;\n"
            + "  sum += texture2D(u_texture, vBlurTexCoords[2]) * 0.23805;\n"
            + "  sum += texture2D(u_texture, vBlurTexCoords[3]) * 0.216901;\n"
            + "  sum += texture2D(u_texture, vBlurTexCoords[4]) * 0.164074;\n"
            + "  gl_FragColor = sum;\n"
        + "}";
    ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false) {
        Gdx.app.log("ERROR", shader.getLog());
    }
    return shader;
}
朱立成
  • 41
  • 4
  • What is `RenderSurface`, a wrapper for FrameBuffer? It's expensive to blur a 1080x1920 image, especially on Android! Usually, when doing a Gaussian blur, you can downsample the screen to half or a quarter of the width and height without much quality loss. That also lets you get away with a smaller sampling radius to achieve the same appearance. – Tenfour04 Sep 29 '14 at 12:23
  • Yes. Thats right and I currently find this page which can solve my problem. https://github.com/mattdesl/lwjgl-basics/wiki/OpenGL-ES-Blurs . Thank you very much. – 朱立成 Sep 30 '14 at 02:32

1 Answers1

1

It's expensive to blur a 1080x1920 image, especially on Android! Usually, when doing a Gaussian blur, you can downsample the screen to half or a quarter of the width and height without much quality loss. That also lets you get away with a smaller sampling radius to achieve the same appearance.

朱立成
  • 41
  • 4