Hi,
After using two FBO’s, my frame rate has dropped from the usual vsynch’d 60fps to 30fps. This is the code:
blurTargetA.bind();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setShader(null);
batch.begin();
worldMap.drawMap(debug, batch, regions, camX, camY, endX , endY, WORLDWIDTH, WORLDHEIGHT);
batch.flush();
batch.setShader(blurShader);
// pass 1 - horizontal blur
blurShader.setUniformf("dir", 1f, 0f);
blurShader.setUniformf("radius", 1 * MAX_BLUR);
blurTargetB.bind();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
fboRegion.setTexture(blurTargetA.getColorBufferTexture());
batch.setColor(1,1,1,1); // VIP!
batch.draw(fboRegion, 0, 0, SCREENWIDTH, SCREENHEIGHT);
batch.flush();
FrameBuffer.unbind();
blurShader.setUniformf("dir", 0f, 1f);
blurShader.setUniformf("radius", 1 * MAX_BLUR);
fboRegion.setTexture(blurTargetB.getColorBufferTexture());
batch.setProjectionMatrix(camera.combined); // bind to main camera projection - VIP!!!
batch.draw(fboRegion, (camX*16),(camY*16));//, 320,480);draw to position x400,y400
batch.setShader(null);
FBO’s are created in my create method:
blurTargetA = new FrameBuffer(Format.RGBA8888, SCREENWIDTH, SCREENHEIGHT, false);
blurTargetB = new FrameBuffer(Format.RGBA8888, SCREENWIDTH, SCREENHEIGHT, false);
fboRegion = new TextureRegion(blurTargetA.getColorBufferTexture());
fboRegion.flip(false, true);
Is there any optimizations that could be made to the above although I think it looks ok, if I set the shader to null instead of blurshader, I get the 60fps back, so looking more likely the shader is the cause of it:
varying vec4 vColor;
varying vec2 vTexCoord;
//declare uniforms
uniform sampler2D u_texture;
uniform float resolution;
uniform float radius;
uniform vec2 dir;
void main() {
//this will be our RGBA sum
vec4 sum = vec4(0.0);
//our original texcoord for this fragment
vec2 tc = vTexCoord;
//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius/resolution;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dir.x;
float vstep = dir.y;
//apply blurring, using a 9-tap filter with predefined gaussian weights
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
//discard alpha for our simple demo, multiply by vertex color and return
//gl_FragColor = vColor * vec4(sum.rgb, 1);
vec4 texColor = texture2D(u_texture, vTexCoord);
gl_FragColor = vColor * vec4(sum.rgb, texColor.a);
}
Appreciate any advice. Maybe sampling to half screen width and height? Possibly pass texture chords into vertex shader so no real computing in frag shader…
Thanks
PS - the 30fps is on my laptop which has a crap intel hd4000 GPU.