我正在为Android/iOS开发一个游戏,需要优化渲染。该游戏使用户能够变形地形,所以我使用一个灰度图像的地形(值1在地形意味着坚实的地面,0表示没有地面),并应用碎片阴影(也有一个背景图像)。这与60 fps常数很好,但问题是,我还需要渲染一个边界的地形边缘。为此,我在变形时模糊边缘,在碎片着色器中,我根据地形密度/透明度绘制边框(边框为1x64纹理)。
问题是,当渲染边框时,我需要做一个动态纹理读取,将帧速率降到20。有什么方法可以优化这个吗?如果我用一个统一的浮点数组来替换边框纹理,它会有帮助吗?还是和从2d纹理中读取一样?
着色器代码:
varying mediump vec2 frag_background_texcoord;
varying mediump vec2 frag_density_texcoord;
varying mediump vec2 frag_terrain_texcoord;
uniform sampler2D density_texture;
uniform sampler2D terrain_texture;
uniform sampler2D mix_texture;
uniform sampler2D background_texture;
void main()
{
lowp vec4 background_color = texture2D(background_texture, frag_background_texcoord);
lowp vec4 terrain_color = texture2D(terrain_texture, frag_terrain_texcoord);
highp float density = texture2D(density_texture, frag_density_texcoord).a;
if(density > 0.5)
{
lowp vec4 mix_color = texture2D(mix_texture, vec2(density, 1.0)); <- dynamic texture read (FPS drops to 20), would replacing this with a uniform float array help (would also need to calculate the index in the array)?
gl_FragColor = mix(terrain_color, mix_color, mix_color.a);
} else
{
gl_FragColor = background_color;
}
}发布于 2013-10-19 11:45:05
弄明白了。我修的方法是移除所有的分支。现在运行到60 now。优化的代码:
varying mediump vec2 frag_background_texcoord;
varying mediump vec2 frag_density_texcoord;
varying mediump vec2 frag_terrain_texcoord;
uniform sampler2D density_texture;
uniform sampler2D terrain_texture;
uniform sampler2D mix_texture;
uniform sampler2D background_texture;
void main()
{
lowp vec4 background_color = texture2D(background_texture, frag_background_texcoord);
lowp vec4 terrain_color = texture2D(terrain_texture, frag_terrain_texcoord);
lowp vec4 mix_color = texture2D(mix_texture, vec2(density, 0.0));
lowp float density = texture2D(density_texture, frag_density_texcoord).a;
gl_FragColor = mix(mix(bg_color, terrain_color, mix_color.r), mix_color, mix_color.a);
}https://stackoverflow.com/questions/19298406
复制相似问题