2013-10-10 195 views
0

我正在開發Android/iOS遊戲,需要優化渲染。 遊戲使用戶能夠變形地形,所以我使用地形的灰度圖像(地形中的值1表示固體地面,而0表示無地面),並在其上應用片段着色器(還有一個背景圖片)。這工作非常好,60 fps不變,但問題是,我還需要在地形邊緣呈現邊框。所以爲此,我在變形時模糊邊緣,並在片段着色器中根據地形密度/透明度繪製邊框(邊框爲1x64紋理)。opengl es 2.0 - 優化片段着色器

問題是,當渲染邊框時,我需要做一個動態紋理讀取,將幀速率降低到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; 
    } 
} 
+0

您是否嘗試過移動紋理讀取?這樣可能可以通過GPU優化。 – fen

+0

是的,它甚至更慢:/ – blejzz

+0

我不確定你想通過將密度設置爲highp來完成什麼。 'sampler2D'默認爲'lowp',因此通過將查找結果存儲在'highp'變量中,您不會獲得任何精度;你只是在浪費GPU週期將其轉換爲'highp',然後爲紋理座標創建一個高精度的'vec2'。 –

回答

0

想通了。我修正它的方式是刪除所有分支。現在~60fps的運行。 優化代碼:

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);  
} 
相關問題