Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

  D3D11_BUFFER_DESC bd;
  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;

  D3D11_SUBRESOURCE_DATA InitData;
  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

  D3D11_MAPPED_SUBRESOURCE resource;
  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);
share|improve this question
    
InitData.pSysMem = (SomeType*)&vertices; –  Irlan Robson Oct 9 '13 at 15:54
    
@IrlanRobson - no, because pSysMem is void *. –  Jimmy Shelter Oct 9 '13 at 18:20
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
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. –  judeclarke Aug 6 '11 at 23:28
add comment

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

if(FAILED(pCtx->Map(m_pvertexbuffer[0],0,D3D11_MAP::D3D11_MAP_WRITE_DISCARD,0,&Mapped)))
{
    PostQuitMessage(0);
}
VERTEX_STRUCT *ptr=(VERTEX_STRUCT*)Mapped.pData;
for(int i=0;i<LENGTH;i++)
{
    x=r*cos(2*3.142*i/length);
    y=r*sin(2*3.142*i/length); 
    ptr[i].pos = XMFLOAT3(x,y,0);  // Bottom left.      
    //srand((int)time(0));
    srand((GetTickCount()%65536));
    ptr[i].color.x=(float(rand())/RAND_MAX);
    srand((GetTickCount()%65536)-1000);
    ptr[i].color.y=(float(rand())/RAND_MAX);
    srand((GetTickCount()%65536)-2000);
    ptr[i].color.z=(float(rand())/RAND_MAX);
}
pCtx->Unmap(m_pvertexbuffer[0],0);
share|improve this answer
1  
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. –  Wackidev Jun 23 '12 at 19:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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