title: How to define a function-like macro with D3DCompile() from C++?
body: <p>Is it possible to define a function-like macro with the <code>const D3D_SHADER_MACRO *pDefines</code> parameter of <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd607324(v=vs.85).aspx" rel="nofollow"><code>D3DCompile()</code></a> ?</p>
  
  <p>Sadly MSDN is not very specific but implies they can be defined with <em>/D</em> option of <em>fxc.exe</em>.</p>
  
  <hr>
  
  <p>I tried with this :</p>
  
  <pre><code>const D3D_SHADER_MACRO dx11Defines[] = {
      {"SIMPLE_MACRO", "42"},
      {"FUNCTION_MACRO(x)", "DoSometing((x).somefield)"},
      {nullptr, nullptr},
  };
  </code></pre>
  
  <p>And usage in HLSL :</p>
  
  <pre><code>float test1 = SIMPLE_MACRO;
  float test2 = FUNCTION_MACRO(data);
  </code></pre>
  
  <p>But the compilation failed :</p>
  
  <pre><code>Test.fx(XX,XX-XX): error X3004: undeclared identifier 'FUNCTION_MACRO'
  </code></pre>
  
  <p>Output of <code>D3DPreprocess()</code> :</p>
  
  <pre><code>float test1 = 42 ;
  float test2 = FUNCTION_MACRO ( data ) ;
  </code></pre>
  
  <hr>
  
  <p>[edit]</p>
  
  <p>I tested the following command-line with the same HLSL code :</p>
  
  <p><code>fxc dtest.fx /Ptest_p.fx /D"SIMPLE_MACRO=42" /D"FUNCTION_MACRO(data)=24"</code></p>
  
  <p>Which output :</p>
  
  <pre><code>float test1 = 42 ;
  float test2 = FUNCTION_MACRO ( data ) ;
  </code></pre>
  
  <hr>
  
  <p>[edit]</p>
  
  <p>I finally solved the problem by using ID3DInclude interface, as suggested by @catflier.
  I generate function-like macro definitions in a temporary buffer when asked for a specific header filename.</p>
  
  <p>I stumbled upon <a href="http://nikvoss.com/2013/03/implementing-id3dinclude/" rel="nofollow">a nice article by Adam Sawicki</a>, that helped me a lot.</p>