我在我的 libgdx_project 中使用 FBO 渲染图像传递模糊着色器,fps 下降到 20。为啥?
Posted
技术标签:
【中文标题】我在我的 libgdx_project 中使用 FBO 渲染图像传递模糊着色器,fps 下降到 20。为啥?【英文标题】:I use FBO to render an image pass blur shader in my libgdx_project and the fps go down to 20.why?我在我的 libgdx_project 中使用 FBO 渲染图像传递模糊着色器,fps 下降到 20。为什么? 【发布时间】:2014-09-29 07:09:35 【问题描述】:我使用 FBO 渲染图像通道模糊着色器,然后 fps 下降到 20。为什么?当我在没有FBO的情况下直接通过x轴方向模糊它时没关系。当我只使用FBO恢复图像并将其绘制到屏幕时也可以。有什么我需要注意的吗?任何帮助都会很棒!谢谢。 这是我的代码:
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;
【问题讨论】:
什么是RenderSurface
,FrameBuffer 的包装器?模糊 1080x1920 图像的成本很高,尤其是在 android 上!通常,在进行高斯模糊时,您可以将屏幕缩小到宽度和高度的一半或四分之一,而不会造成太大的质量损失。这也让您可以使用更小的采样半径来获得相同的外观。
是的。没错,我目前发现这个页面可以解决我的问题。 github.com/mattdesl/lwjgl-basics/wiki/OpenGL-ES-Blurs 。非常感谢。
【参考方案1】:
模糊 1080x1920 图像的成本很高,尤其是在 Android 上!通常,在进行高斯模糊时,您可以将屏幕缩小到宽度和高度的一半或四分之一,而不会造成太大的质量损失。这也让您可以使用更小的采样半径来获得相同的外观。
【讨论】:
以上是关于我在我的 libgdx_project 中使用 FBO 渲染图像传递模糊着色器,fps 下降到 20。为啥?的主要内容,如果未能解决你的问题,请参考以下文章
使用库 github.barteksc.pdfviewer 在我的应用程序中加载 PDF 时出现错误