次の方法で共有


switch ステートメント (DirectX HLSL)

セレクターの値に応じて、switch 本体内の別のステートメント ブロックに制御を移します。

[Attribute] switch( Selector ) {

  case 0 :

    { StatementBlock; }

  break;

  case 1 :

    { StatementBlock; }

  break;

  case n :

    { StatementBlock; }

  break;

  default :

    { StatementBlock; }

  break;

パラメーター

  • Attribute
    ステートメントのコンパイル方法を制御するパラメーター (省略可能)。属性が指定されていない場合には、コンパイラはハードウェア切り替えを使用するか、一連の if ステートメントを生成します。

    属性 説明
    flatten ステートメントを、それぞれが flatten 属性を持つ一連の if ステートメントとしてコンパイルします。
    branch ステートメントを、それぞれが branch 属性を持つ一連の if ステートメントとしてコンパイルします。
    forcecase ハードウェア内で switch ステートメントを強制します。
    call switch 内の個々の case の本体は、ハードウェア サブルーチン内に移動され、switch は一連のサブルーチン呼び出しとなります。
  • Selector
    変数。中かっこ内にある各 case ステートメントは、それぞれがこの変数をチェックし、SwitchValue が特定の CaseValue と一致するかを調べます。

  • StatementBlock
    1 つまたは複数の ステートメント

解説 

[branch] switch(a) {     case 0:         return 0;      case 1:         return 1;      case 2:         return 3;      default:         return 6;  }

この式は、次の式と同じです。

[branch] if( a == 2 )     return 3; else if( a == 1 )     return 1; else if( a == 0 )     return 0; else     return 6;

次に、forcecase と call フロー制御属性の使用方法の例を示します。

[forcecase] switch(a) {     case 0:         return 0;      case 1:         return 1;      case 2:         return 3;      default:         return 6;  }  [call] switch(a) {     case 0:         return 0;      case 1:         return 1;      case 2:         return 3;      default:         return 6;  }

関連項目

フロー制御