title: SharpDX D3D11 VertexElement Format Mismatch?
body: <p>I have a shader that no longer draws correctly.  It was working in XNA, but had to be rewritten for DX11 and SharpDX and now uses shader model 5.  There are no exceptions and the shader effect compiles fine, no debug-layer messages from the device except for an unrelated(?) default sampler-state complaint.  It is a normal, fullscreen quad doing the simplest texture blit. (Using a random noise texture for testing)</p>
  
  <p><img src="https://i.stack.imgur.com/32cGP.png" alt="Vertex Problem Example"></p>
  
  <p>I am feeding the vertex shader a <code>VertexPositionTexutre</code>, a SharpDX object which contains:</p>
  
  <pre><code>[VertexElement("SV_Position")]
  public Vector3 Position;
  [VertexElement("TEXCOORD0")]
  public Vector2 TextureCoordinate;
  </code></pre>
  
  <p>I suspect there is a mismatch between signatures.  <code>VertexPositionTexture</code> has a <code>Vector3</code> position component defined for "SV_Position".</p>
  
  <p>D3D11 <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647%28v=vs.85%29.aspx" rel="nofollow noreferrer">defines</a> "SV_Position" as a <code>float4</code>.  It is a System-Value constant with a concrete native type.  I fear the shader is eating an extra float beyond the end of the float3 of <code>Vector3</code>.  This would also explain why my UVs are broken on several vertices.</p>
  
  <p>This is my vertex shader:</p>
  
  <pre><code>struct V2P
  {
      float4 vPosition    : SV_Position;
      float2 vTexcoord    : TEXCOORD0;
  };
  
  V2P VSCopy(float4 InPos  : SV_Position,
                      float2 InTex : TEXCOORD0)
  {
      V2P Out = (V2P)0;
  
      // transform the position to the screen
      Out.vPosition = float4(InPos);
      Out.vTexcoord = InTex;
  
      return Out;
  }
  </code></pre>
  
  <p>I have tried to change the input of <code>VSCopy()</code> from <code>float4</code> to <code>float3</code> with no change in result.  Short of recompiling SharpDX <code>VertexPositionTexture</code>, I don't see how to fix this, yet SharpDX fully supports DirectX11 so I must be doing something incorrectly.</p>
  
  <p>I am defining my vertices and setting them as follows:</p>
  
  <pre><code>//using SharpDX.Toolkit.Graphics
  [...]//In the initialization
      verts = new VertexPositionTexture[]
      {
          new VertexPositionTexture(
              new Vector3(1.0f,-1.0f,0),
              new Vector2(1,1)),
          new VertexPositionTexture(
              new Vector3(-1.0f,-1.0f,0),
              new Vector2(0,1)),
          new VertexPositionTexture(
              new Vector3(-1.0f,1.0f,0),
              new Vector2(0,0)),
          new VertexPositionTexture(
              new Vector3(1.0f,1.0f,0),
              new Vector2(1,0))
      }
  
      //vb is a Buffer&lt;VertexPositionTexture&gt;
      short[] indeces = new short[] { 0, 1, 2, 2, 3, 0 };
          vb = SharpDX.Toolkit.Graphics.Buffer.Vertex.New(Game.Instance.GraphicsDevice, verts, SharpDX.Direct3D11.ResourceUsage.Dynamic);
          ib = SharpDX.Toolkit.Graphics.Buffer.Index.New(Game.Instance.GraphicsDevice, indeces, dx11.ResourceUsage.Dynamic);
  
          [...] //In the Render method, called every frame to draw the quad
          Game.Instance.GraphicsDevice.SetVertexBuffer&lt;VertexPositionTexture&gt;(vb, 0);
          Game.Instance.GraphicsDevice.SetIndexBuffer(ib, false);
          Game.Instance.GraphicsDevice.DrawIndexed(PrimitiveType.TriangleList, 6);
  </code></pre>