I have a VertexBuffer that I am remapping on a per frame base for a bunch of quads that are constantly updated, sharing the same material\index buffer but have different width/heights. However, currently right now there is a really bad flicker on this geometry.

Although it is flickering, the flicker looks correct. I know it is the vertex buffer mapping because if I recreate the entire VB then it will render fine. However, as an optimization I figured I would just remap it. Does anyone know what the problem is?

The length (width, size) of the vertex buffer is always the same.

One might think it is double buffering, however, it would not be double buffering because it only happens when I map/unmap the buffer, so that leads me to believe that I am setting some parameters wrong on the creation or mapping.

I am using DirectX11, my initialization and remap code are:

Initialization code

  ZeroMemory( &bd, sizeof(bd) );
  bd.Usage = D3D11_USAGE_DYNAMIC;
  bd.ByteWidth = vertCount * vertexTypeWidth;
  bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  //bd.CPUAccessFlags = 0;
  bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

  ZeroMemory( &InitData, sizeof(InitData) );
  InitData.pSysMem = vertices;

  mVertexType = vertexType;

  HRESULT hResult = device->CreateBuffer( &bd, &InitData, &m_pVertexBuffer );

  // This will be S_OK
  if(hResult != S_OK)
     return false;

Remap code

  HRESULT hResult = deviceContext->Map(m_pVertexBuffer, 0,
     D3D11_MAP_WRITE_DISCARD, 0, &resource);

  // This will be S_OK
  if(hResult != S_OK)
     return false;

  resource.pData = vertices;

  deviceContext->Unmap(m_pVertexBuffer, 0);
  • \$\begingroup\$ InitData.pSysMem = (SomeType*)&vertices; \$\endgroup\$
    – user35931
    Oct 9 '13 at 15:54
  • \$\begingroup\$ @IrlanRobson - no, because pSysMem is void *. \$\endgroup\$ Oct 9 '13 at 18:20

When you invoke Map with D3D11_MAP_WRITE_DISCARD, Direct3D considers the whole buffer contents invalid and will replace it with the data that exists inside the memory that the new data buffer points at when invoking Unmap.

Your problem is that instead of copying your data into the memory that Direct3D provides to you when you map the buffer, you instead change the pointer to point somewhere else.

Direct3D does not expect that, nor will care about where the pointer points. It will just assume that you've filled in the buffer that the pData pointer originally pointed at.

The reason for your flicker is because the buffer may contain bogus uninitialized data, as it expects you to fill it in completely.

  • \$\begingroup\$ What you said put me on the right track, mainly the point that I am pointing it to another pointer, rather than copying it over. Once I copied it over, I no longer had the bug. Cheers. \$\endgroup\$
    – judeclarke
    Aug 6 '11 at 23:28

I think the map you used is wrong, look the code below, wish to help you.

for(int i=0;i<LENGTH;i++)
    ptr[i].pos = XMFLOAT3(x,y,0);  // Bottom left.      
  • 1
    \$\begingroup\$ Please work on your answer a little -- try selecting the code then hitting the <.> button, and including some comments. It'd make the answer a lot better. Thanks. \$\endgroup\$
    – Wackidev
    Jun 23 '12 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.