title: Does the D3D11 HLSL compiler pre-calculate matrices?
body: <p>Let's say that we have a geometry shader (model 5.0) in HLSL with a matrix for scaling and a matrix for translation:</p>
  
  <pre><code>float4x4 scale;
  scale[0] = float4( s,   0.0f, 0.0f, 0.0f);
  scale[1] = float4(0.0f,  s,   0.0f, 0.0f);
  scale[2] = float4(0.0f, 0.0f,  s,   0.0f);
  scale[3] = float4(0.0f, 0.0f, 0.0f, 1.0f);
  
  float4x4 translation;
  translation[0] = float4(1.0f, 0.0f, 0.0f, 0.0f);
  translation[1] = float4(0.0f, 1.0f, 0.0f, 0.0f);
  translation[2] = float4(0.0f, 0.0f, 1.0f, 0.0f);
  translation[3] = float4( x,    y,    z,   1.0f);
  </code></pre>
  
  <p>Here <code>s</code> is a scaling coefficient and <code>x</code>, <code>y</code>, <code>z</code> are the amount of translation in each axis. We can combine these matrices into a single matrix by multiplying them:</p>
  
  <pre><code>float4x4 combined;
  combined = mul(scale, translation);
  </code></pre>
  
  <p>However, it's also possible to do this multiplication on paper and then initialize the combined matrix directly. Doing that would yield this result:</p>
  
  <pre><code>float4x4 combined;
  combined[0] = float4( s,   0.0f, 0.0f, 0.0f);
  combined[1] = float4(0.0f,  s,   0.0f, 0.0f);
  combined[2] = float4(0.0f, 0.0f,  s,   0.0f);
  combined[3] = float4( x,    y,    z,   1.0f);
  </code></pre>
  
  <p>The above example is really simple but I have a total of 4 matrices with both scaling, rotation and translation so as you understand the combined matrix will be quite cumbersome to initialize directly.</p>
  
  <p>So if the compiler would do this during compile time, it would be better to initialize the matrices separately for readability reasons, but if it doesn't combine them during compile time but rather during runtime, it would be better do initialize the combined matrix for performance reasons.</p>
  
  <p>Thus, my question is:  </p>
  
  <p><strong>Will the compiler combine these matrices automatically during the compilation?</strong></p>
  
  <p>I'm currently using the <code>D3DCompileFromFile</code> function to compile the shader. I don't know if that function uses the same compiler as the one in Visual Studio, but if it doesn't, and the one in Visual Studio is better, I can compile the shaders during the build instead and then just read the <code>.cso</code> file.</p>
  
  <p>Also, this geometry shader is producing billboards so I can't calculate these matrices on the CPU, as every billboard will have different matrices.</p>