android texture switching

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

android texture switching

Postby triplesky » Sun Aug 20, 2017 7:43 am

So, i have 3 separate textures.
Code: Select all
Texture first,second,third;

rendering them 333 times(for example)
Code: Select all
spriteBatch.begin();
for(int i=0;i<333;i++){
spriteBatch.draw(first);
spriteBatch.draw(second);
spriteBatch.draw(third);
}
batch.end();

So i have 999 rendercalls with 998 textureswitching with default shader. So i write my custom SpriteBatch (call it MyBatch), its mostly a copy-paste copy of original SpriteBatch with minor changes. I remove
Code: Select all
uniform sampler2D u_texture;
now shader uses
Code: Select all
uniform sampler2D textures[3];

so i remove texture switching from batch at all and replace it with binding this 3 textures ...
Code: Select all
first.bind(0...2);
setUniformi("textures[0...2],0...2);

on shader i use color.rgb to determine witch index from sampler2D array to use.
Code: Select all
if(v_color.r>0) {glFragcolor=texture2D(textures[0],v_texCoord;)}
if(v_color.g>0) {glFragcolor=texture2D(textures[1],v_texCoord;)}
if(v_color.b>0) {glFragcolor=texture2D(textures[2],v_texCoord;)}

no on render
Code: Select all
myBatch.begin();
for(int i=0;i<333;i++){
myBatch.setColor(1,0,0,1);
myBatch.draw(first);//no matter what texture to pass, cause i remove lastTexture checks,switchTexture() etc;
myBatch.setColor(0,1,0,1);
myBatch.draw(first);
myBatch.setColor(0,0,1,1);
myBatch.draw(first);
}
batch.end();

Now i render all my 3 textures 999 times with 1 draw call binding textures once before shader.end();
Stress test on desktop shows what i want to see. Using SpriteBatch with 999 render calls and 998 texture switching have 27 fps. Using MyBatch now i have 1 render call and no texture switching at all. And of course 60+ fps. Everything is fine, but on android i have same fps with both batches. Why?
triplesky
 
Posts: 3
Joined: Sun Aug 20, 2017 7:02 am

Re: android texture switching

Postby slebed » Mon Aug 21, 2017 4:36 pm

I believe those if statements are the issue. You should be able to rewrite that code to avoid the need for the conditionals.

Something along the lines of:

Code: Select all
glFragcolor = mix(texture2D(textures[0],v_texCoord),texture2D(textures[1],v_texCoord), v_color.r);
glFragcolor = mix(glFragColor ,v_texCoord),texture2D(textures[2],v_texCoord), v_color.g);


I use something like this in my texture splatting shader. Hope this helps.
slebed
 
Posts: 201
Joined: Fri Dec 28, 2012 3:29 am

Re: android texture switching

Postby triplesky » Tue Aug 22, 2017 9:42 am

slebed, ifstatements is not the reason, i have tried passing int with different indexes so it was
Code: Select all
gl_FragColor=texture2D(textures[index],v_texCoord);
but result is the same. Mixing is drop fps more.
triplesky
 
Posts: 3
Joined: Sun Aug 20, 2017 7:02 am

Re: android texture switching

Postby tenfour04 » Tue Aug 22, 2017 11:34 am

Branching on a varying in the fragment shader is very expensive. You don't see it on desktop because a desktop GPU is generally much more powerful than a phone's.

This is the reason for TextureAtlases.
tenfour04
 
Posts: 1186
Joined: Sat Jun 18, 2011 3:24 pm

Re: android texture switching

Postby triplesky » Tue Aug 22, 2017 1:59 pm

tenfour04, you are right. I have tried to work with only 2 textures(drawing them 400 times first than second), and of course i now have only 2 if statemnets, and i have fps gain on phone (not too much).
triplesky
 
Posts: 3
Joined: Sun Aug 20, 2017 7:02 am


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest