2012-03-22 91 views
5

我有一個具有以下屬性的片段着色器:glGetUniformLocation返回-1的OpenGL ES(iPhone)

varying highp vec2 coordinate; 
precision mediump float; 

uniform sampler2D videoframe; 
uniform sampler2D videosprite; 
uniform vec4 mask; 
uniform float threshold; 

我得到他們的位置,後來設置它們:

_frame = glGetUniformLocation(_program, "videoframe"); 
_sprite = glGetUniformLocation(_program, "videosprite"); 
_mask = glGetUniformLocation(_program, "mask"); 
_threshold = glGetUniformLocation(_program, "threshold"); 

NSLog(@"%i %i %i %i", _frame, _sprite, _mask, _threshold); 

但是,日誌揭示:0 2 1 -1

從文檔,我看到-1(門檻統一)意味着它失敗。爲什麼失敗? 謝謝

回答

10

GLSL編譯器可以(通常會)優化掉沒有在着色器中使用的任何制服和屬性。您只能查詢活動制服的位置,即在着色器的至少一個分支中使用的位置。

所以我猜threshold變量沒有在着色器代碼中的任何地方使用。但是在這種情況下,無論如何你都不需要它的價值,並且爲位置-1設置統一的值將不會做任何事情。所以你實際上不必擔心這一點。

+0

哇...即使我知道這一點,謝謝你的提醒。這是問題... – 0xSina 2012-03-22 18:31:44