2017-11-14 1 views
1

Ich mache eine Übung in der Shader-Schule, in der ich eine Funktion implementiere, die die n-te Potenz einer Matrix zurückgibt. Selbst wenn ich pass konstanten Parameter n in die folgenden Funktion:Parameter, der in der GLSL-Funktion übergeben wird, wird nicht als Konstante erkannt

mat2 matrixPower(highp mat2 m, const int n) { 
    if(n==0) { 
     return mat2(1.0); 
    } else { 
     mat2 result = m; 
     for(int i=1; i < n; ++i) { 
      result = result * m; 
     } 
     return result; 
    } 
} 

ich die folgende Fehlermeldung erhalten:

Loop index cannot be compared with non-constant expression 

Wie ist das möglich?

Antwort

1

Das Schlüsselwort const zeigt an, dass n nicht beschreibbar ist, aber es ist immer noch eine Variable und keine Konstante!
n variiert mit dem tatsächlichen Parameter, den der Aufruf an den formalen Parameter übergeben hat.

Hinweis in Appandix A von The OpenGL ES Shading Language 1.0 mögliche Einschränkungen für OpenGL ES 2.0 aufgelistet ar:

for loops are supported but with the following restrictions:

  • condition has the form loop_index relational_operator constant_expression
    where relational_operator is one of: > >= < <= == or !=
Konstante Ausdrücke von The OpenGL ES Shading Language 1.0


In Kapitel 5.10 wird klar gesagt, dass eine konstante Funktion Parameter nicht ein konstanter Ausdruck ist:

A constant expression is one of
- a literal value (e.g., 5 or true)
- a global or local variable qualified as const excluding function parameters
- an expression formed by an operator on operands that are constant expressions, including getting an element of a constant vector or a constant matrix, or a field of a constant structure
- a constructor whose arguments are all constant expressions
- a built-in function call whose arguments are all constant expressions, with the exception of the texture lookup functions.


eine Abhilfe wäre eine Schleife zu schaffen, die von einem konstanten Ausdruck a begrenzt ist nd zu break aus der Schleife durch eine Bedingung:

for (int i = 1; i < 10; ++i) 
{ 
    if (i >= n) 
     break; 
    result = result * m; 
} 
Verwandte Themen