title: Differences of DirectX11 D3D11_MAP
body: <p>I have a vertex buffer that some parts of it need to be updated <em>only once</em> every frame, so I created it as <code>D3D11_USAGE_DYNAMIC</code> with <code>D3D11_CPU_ACCESS_WRITE</code>.</p>
  
  <p>Since there are a lot of vertices (around 150k), I don't want to iterate over the entire buffer, only on the parts that were tagged to update.</p>
  
  <p>To do so, I tag each "region" with a <code>DirtyVertexBuffer</code> flag and then skip untagged regions. To update the buffer I use <code>D3D11_MAP_WRITE_NO_OVERWRITE</code>. The code is:</p>
  
  <pre><code>VertexType *vertices;
  D3D11_MAPPED_SUBRESOURCE mappedResource;
  Vertex *vertex;
  HRESULT result;
  
  result = context-&gt;Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &amp;mappedResource);
  if (FAILED(result))
      return;
  
  vertices = (DirectX::VertexPositionNormalTexture*)mappedResource.pData;
  
  for (auto &amp;material : materials) {
      if ((material.dirty &amp; Material::Dirty::VertexBuffer) == 0) continue;
  
      material.dirty &amp;= ~Material::Dirty::VertexBuffer;
  
      // Update vertex data
  }
  
  // release the access to the buffer
  context-&gt;Unmap(m_vertexBuffer, 0);
  </code></pre>
  
  <p>My question is: Why is <code>D3D_MAP_WRITE_NO_OVERWRITE</code> necessary here, instead of <code>D3D_MAP_WRITE_DISCARD</code>?</p>
  
  <p>If I use the latter, the vertex buffer seems to be "zeroed" (only regions that changed are rendered). <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ff476181%28v=vs.85%29.aspx" rel="nofollow">MSDN</a> says:</p>
  
  <blockquote>
    <p><strong>D3D11_MAP_WRITE_DISCARD</strong></p>
    
    <p>Resource is mapped for writing; the previous contents of the resource will be undefined.</p>
    
    <p><strong>D3D11_MAP_WRITE_NO_OVERWRITE</strong></p>
    
    <p>Resource is mapped for writing; the existing contents of the resource cannot be overwritten (see Remarks). </p>
  </blockquote>
  
  <p>but since I mapped the resource, shouldn't the entire buffer be copied from VRAM to system RAM and then, when I unmap it, go back to the VRAM?</p>