我在我的libgdx_project中使用FBO渲染图像过程模糊着色器,fps下降到20.为什么



我使用FBO渲染图像过程模糊着色器,fps下降到20。为什么?当我在没有FBO的情况下直接通过x轴方向模糊它时,这是可以的。当我只是使用FBO来恢复图像并将其绘制到sceen时,这也是可以的。有什么事情我需要注意吗?任何帮助都会很棒!谢谢这是我的代码:

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_ESn" //
        + "#define LOWP lowpn" //
        + "precision mediump float;n" //
        + "#elsen" //
        + "#define LOWP n" //
        + "#endifn" //
        + "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;
}

模糊1080x1920图像的成本很高,尤其是在Android上!通常,在进行高斯模糊时,可以将屏幕缩小到宽度和高度的一半或四分之一,而不会有太多质量损失。这也让您可以通过较小的采样半径来获得相同的外观。

最新更新