title: DirectX 11: Use UpdateSubresource to update Vertex and Index buffers
body: <p>in my sientific simulation I have got huge data sets (around 600000 vertices) to update in regular time intervals (around 1sec). </p>
  
  <p>For the first data set I create a buffer like follows:</p>
  
  <pre><code>mVertexStride = sizeof(VertexType);
  
  D3D11_BUFFER_DESC VertexBufferDesc;
  ZeroMemory(&amp;VertexBufferDesc, sizeof(VertexBufferDesc));
  VertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  VertexBufferDesc.ByteWidth = sizeof(VertexType) * numOfVertices;
  VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //use as a vertex buffer
  VertexBufferDesc.CPUAccessFlags = 0; 
  VertexBufferDesc.MiscFlags = 0;
  
  D3D11_SUBRESOURCE_DATA InitialSubData;
  ZeroMemory(&amp;InitialSubData, sizeof(InitialSubData));
  InitialSubData.pSysMem = vertices;
  
  HRESULT hr = device-&gt;CreateBuffer(&amp;VertexBufferDesc, &amp;InitialSubData, &amp;mVertexBuffer);
  if (FAILED(hr)) { return false; }
  return true;
  </code></pre>
  
  <p>I'm using similar calls to create an index buffer.</p>
  
  <p>Then after 1 sec I wanna update the entire data set. So I wanna kinda swap the whole vertex and index buffer. The old vertices and indices are discarded while the new ones are supposed to be written to the buffers.</p>
  
  <p>That point gives rise to the actual question. My code looks like this right now:</p>
  
  <pre><code>deviceContext-&gt;UpdateSubresource(mVertexBuffer, 0, NULL, vertices, 0, 0);
  </code></pre>
  
  <p>And for the index buffer</p>
  
  <pre><code>deviceContext-&gt;UpdateSubresource(mIndexBuffer, 0, NULL, indices, 0, 0);
  </code></pre>
  
  <p>That code works (most of the time) but the problem is that the size of the buffer does not expand automatically.</p>
  
  <p>For example first I have a data set with 650000 vertices and then I have one with 700000 vertices. The second set will be filled into the buffer but only shows 650000 vertices. There are visible holes. How do I cope with that? Could not find anything on the web or in the Microsoft help.</p>
  
  <p>Should I use map/unmap even if I'm not updating every frame?</p>
  
  <p>Any help is appreciated!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>In my case UpdateSubresource() seems the wrong approach. What I am doing now is deleting the old buffer and creating a new one. The simulation still performs well, the "swapping" is done in milliseconds. My code looks like follows:</p>
  
  <pre><code>if (mVertexBuffer != NULL)
  {
      mVertexBuffer-&gt;Release();
      mVertexBuffer = NULL;
  }
  
  mVertexStride = sizeof(VertexType);
  
  D3D11_BUFFER_DESC VertexBufferDesc;
  ZeroMemory(&amp;VertexBufferDesc, sizeof(VertexBufferDesc));
  VertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  VertexBufferDesc.ByteWidth = sizeof(VertexType) * numOfVertices;
  VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //use as a vertex buffer
  VertexBufferDesc.CPUAccessFlags = 0; //D3D11_CPU_ACCESS_WRITE; //allow CPU to write in buffer
  VertexBufferDesc.MiscFlags = 0;
  
  D3D11_SUBRESOURCE_DATA InitialSubData;
  ZeroMemory(&amp;InitialSubData, sizeof(InitialSubData));
  InitialSubData.pSysMem = vertices;
  
  HRESULT hr = device-&gt;CreateBuffer(&amp;VertexBufferDesc, &amp;InitialSubData, &amp;mVertexBuffer); // 1a) create input buffer
  if (FAILED(hr)) { return false; }
  return true;
  </code></pre>
  
  <p>Any other suggestions?</p>