title: DirectX 11 How Do Vertex And Pixel Shaders Work
body: <p>I am reading the book, "Practical Rendering and Computation with Direct3D 11" and looking at the DirectX 11 Sample:</p>
  
  <p>CPP:
  <a href="https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial02/Tutorial02.cpp" rel="nofollow noreferrer">https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial02/Tutorial02.cpp</a></p>
  
  <p>HLSL:
  <a href="https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial02/Tutorial02.fx" rel="nofollow noreferrer">https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial02/Tutorial02.fx</a></p>
  
  <p><strong>What is this?</strong></p>
  
  <pre><code>D3D11_INPUT_ELEMENT_DESC layout[] =
   {
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
   };
  </code></pre>
  
  <p><strong>More importantly the Shader -- Where in the program does it set this "Pos"?</strong></p>
  
  <pre><code>float4 VS( float4 Pos : POSITION ) : SV_POSITION
  {
      return Pos;
  }
  </code></pre>
  
  <p>I can only see that you are binding the vertex data via:</p>
  
  <pre><code>// Create vertex buffer
  SimpleVertex vertices[] =
  {
      XMFLOAT3( 0.0f, 0.5f, 0.5f ),
      XMFLOAT3( 0.5f, -0.5f, 0.5f ),
      XMFLOAT3( -0.5f, -0.5f, 0.5f ),
  };
  D3D11_BUFFER_DESC bd;
  ZeroMemory( &amp;bd, sizeof(bd) );
  bd.Usage = D3D11_USAGE_DEFAULT;
  bd.ByteWidth = sizeof( SimpleVertex ) * 3;
  bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  bd.CPUAccessFlags = 0;
  D3D11_SUBRESOURCE_DATA InitData;
  ZeroMemory( &amp;InitData, sizeof(InitData) );
  InitData.pSysMem = vertices;
  hr = g_pd3dDevice-&gt;CreateBuffer( &amp;bd, &amp;InitData, &amp;g_pVertexBuffer );
  if( FAILED( hr ) )
      return hr;
  
  // Set vertex buffer
  UINT stride = sizeof( SimpleVertex );
  UINT offset = 0;
  g_pImmediateContext-&gt;IASetVertexBuffers( 0, 1, &amp;g_pVertexBuffer, &amp;stride, &amp;offset );
  
  // Set primitive topology
  g_pImmediateContext-&gt;IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  </code></pre>
  
  <p>And then you are setting the shaders and then drawing the 3 verticies:</p>
  
  <pre><code>// Render a triangle
  g_pImmediateContext-&gt;VSSetShader( g_pVertexShader, nullptr, 0 );
  g_pImmediateContext-&gt;PSSetShader( g_pPixelShader, nullptr, 0 );
  g_pImmediateContext-&gt;Draw( 3, 0 );
  </code></pre>
  
  <p>This all makes sense.  But I am confused on what this Pos input is, how it is generated, and why is it used?  Also, there are other examples that do the same thing ... (sample 7) where they used postion and textures, but I can not see where this information is coming from.  For example...</p>
  
  <pre><code>struct VS_INPUT
  {
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD0;
  };
  </code></pre>
  
  <p>Thank you for your time!</p>