LoginSignup
8
6

More than 5 years have passed since last update.

HLSL のオフラインコンパイル時に気をつけること。

Last updated at Posted at 2014-08-22

オフラインコンパイルして、なんのエラーもなく.csoファイル(コンパイル済みシェーダー)が出力されたのに、それを使おうとしたら、
シェーダーオブジェクト作成時にエラーが出る・・・なぜだ(´・ω・`)

fxcのエラーメッセージにパラメーターの使い方が出てきた。

Strictness and compatibility mode are mutually exclusive:
1> For DX9 compatibility mode, use /Gec
1> For regular DX10 shaders and effects, use regular mode (do not specify /Gec or /Ges)
1> For clean future-proof DX10 shaders and effects, use strict mode (/Ges)

DX9互換モードを指定する場合は、/Gec を記述する。
DX10用のシェーダーを作成する場合は /Gec または /Ges を記述しない。
DX10以降に対応するシェーダーを作成する場合は、strict mode( /Ges ) を記述する。

fxcに与えるオプション。
DX9 /Gec
DX10 /Gec /Ges を記述しない
DX11 /Ges

fxc.exeのバージョンを確認する

製品バージョン:6.3.9600.16384
"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\fxc.exe"
"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\fxc.exe"
"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\d3dcompiler_47.dll"
"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\d3dcompiler_47.dll"

製品バージョン:9.30.9200.16384
"C:\Program Files (x86)\Windows Kits\8.0\bin\x86\fxc.exe"
"C:\Program Files (x86)\Windows Kits\8.0\bin\x64\fxc.exe"
"C:\Program Files (x86)\Windows Kits\8.0\bin\x86\d3dcompiler_46.dll"
"C:\Program Files (x86)\Windows Kits\8.0\bin\x64\d3dcompiler_46.dll"

製品バージョン:9.29.952.3111
"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x64\fxc.exe"
"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86\fxc.exe"
"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Redist\Jun2010_D3DCompiler_43_x64.cab" D3DCompiler_43.dll
"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Redist\Jun2010_D3DCompiler_43_x86.cab" D3DCompiler_43.dll

結論

win7 上で vs2013 を利用し、DirectX SDK (June 2010) のAPIで何も考えずに開発すると、
オフラインコンパイラのバージョンと、ランタイムのバージョンが異なり、エラーが発生します。
お気を付け下さい。

その他の確認

1.最適化を無効にする/Od オプションを付ける
2.フロー制御コンストラクトを優先する /Gfp オプションをつける
3.D3DDisassembleを利用してすべての.cso(コンパイル済みシェーダー)を確認する。CreateXXXShaderがエラーを履くときはこのAPIもうまく機能していない感じがする。
4.動く場合の.csoと動かない場合の.csoをバイナリで比較する。
5.シェーダーコードのフォーマット仕様に関する資料を発見。http://msdn.microsoft.com/en-us/library/ff552891(v=vs.85).aspx

参照

日本語 http://msdn.microsoft.com/ja-jp/library/bb509709(v=vs.85).aspx
英語 http://msdn.microsoft.com/en-us/library/windows/desktop/bb509709(v=vs.85).aspx

fxc.exeに渡すオプションの違いでどれだけ結果が変わるの??

こんなコードで試してみたいと思います。

vs-000.hlsl

struct InputVS
{
    float4  pos         : POSITION;
};

struct OutputVS
{
    float4  pos         : SV_POSITION;
    float4  color       : COLOR0;
};

cbuffer cbSceneParam : register( b0 )
{
    matrix <float, 4, 4> c_mat_WVP;
};

OutputVS main( InputVS input )
{
    OutputVS    output;

    output.pos   =  mul( input.pos, c_mat_WVP );
    output.color =  float4(0,1.0,0,1.0);;

    return output;
}

/Od /Gfp

//
// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.16384
//
//
// Buffer Definitions: 
//
// cbuffer cbSceneParam
// {
//
//   float4x4 c_mat_WVP;                // Offset:    0 Size:    64
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// cbSceneParam                      cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue Format   Used
// -------------------- ----- ------ -------- -------- ------ ------
// POSITION                 0   xyzw        0     NONE  float   xyzw
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue Format   Used
// -------------------- ----- ------ -------- -------- ------ ------
// SV_POSITION              0   xyzw        0      POS  float   xyzw
// COLOR                    0   xyzw        1     NONE  float   xyzw
//
vs_5_0
      dcl_globalFlags refactoringAllowed
      dcl_constantbuffer cb0[4], immediateIndexed
      dcl_input v0.xyzw
      dcl_output_siv o0.xyzw, position
      dcl_output o1.xyzw
      dcl_temps 2
   0: dp4 r0.x, v0.xyzw, cb0[0].xyzw
   1: dp4 r0.y, v0.xyzw, cb0[1].xyzw
   2: dp4 r0.z, v0.xyzw, cb0[2].xyzw
   3: dp4 r0.w, v0.xyzw, cb0[3].xyzw
   4: itof r1.xz, l(0, 0, 0, 0)
   5: mov r1.yw, l(0,1.000000,0,1.000000)
   6: mov o0.xyzw, r0.xyzw
   7: mov o1.xyzw, r1.xyzw
   8: ret 
// Approximately 9 instruction slots used

/Gfp

//
// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.16384
//
//
// Buffer Definitions: 
//
// cbuffer cbSceneParam
// {
//
//   float4x4 c_mat_WVP;                // Offset:    0 Size:    64
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// cbSceneParam                      cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue Format   Used
// -------------------- ----- ------ -------- -------- ------ ------
// POSITION                 0   xyzw        0     NONE  float   xyzw
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue Format   Used
// -------------------- ----- ------ -------- -------- ------ ------
// SV_POSITION              0   xyzw        0      POS  float   xyzw
// COLOR                    0   xyzw        1     NONE  float   xyzw
//
vs_5_0
      dcl_globalFlags refactoringAllowed
      dcl_constantbuffer cb0[4], immediateIndexed
      dcl_input v0.xyzw
      dcl_output_siv o0.xyzw, position
      dcl_output o1.xyzw
   0: dp4 o0.x, v0.xyzw, cb0[0].xyzw
   1: dp4 o0.y, v0.xyzw, cb0[1].xyzw
   2: dp4 o0.z, v0.xyzw, cb0[2].xyzw
   3: dp4 o0.w, v0.xyzw, cb0[3].xyzw
   4: mov o1.xyzw, l(0,1.000000,0,1.000000)
   5: ret 
// Approximately 6 instruction slots used
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6