Display a dynamic texture causing an "already mapped error"

I'm just starting to use Direct3D11, and I'm trying to create a dynamic texture that I plan to update with new data several times per second. My problem is that every time I update the texture with new data, I get this error from the D3D Debugger:

D3D11: ERROR: ID3D11DeviceContext :: Map: this resource is already displayed! [RESOURCE_MANIPULATION ERROR # 2097213: RESOURCE_MAP_ALREADYMAPPED]

Which eventually turns into an E_OUTOFMEMORY error from calling the card after starting the application for a while.

I create my texture as follows:

D3D11_TEXTURE2D_DESC td;
td.ArraySize = 1;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
td.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
td.Height = height;
td.Width  = width;
td.MipLevels = 1;
td.MiscFlags = 0;
td.SampleDesc.Count = 1;
td.SampleDesc.Quality = 0;
td.Usage = D3D11_USAGE_DYNAMIC;

HR(m_device->CreateTexture2D(&td, 0, &texture));

and update its data as follows:

HR(m_deviceContext->Map(texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
    memcpy(mappedData, buffer,rowspan);
    mappedData += mappedResource.RowPitch; 
    buffer += rowspan;
}
m_deviceContext->Unmap(texture, 0);

, , , - Direct3D. , . msdn , , . msdn, :

http://msdn.microsoft.com/en-us/library/ff476905(v=vs.85).aspx#Dynamic

To fill a dynamic texture (one created with D3D11_USAGE_DYNAMIC):
Get a pointer to the texture memory by passing in D3D11_MAP_WRITE_DISCARD when calling               
ID3D11DeviceContext::Map.
Write data to the memory.
Call ID3D11DeviceContext::Unmap when you are finished writing data.

, .

d3d? ? ? , ?

: " ". , .

: D3D11, , d3d. , . , ", ". , d3d , , , , d3d, .

+5
1

, , -, , -.

, . HR, .

#define HR(x)                                                   
{                                                               
    HRESULT hr = (x);                                           
    if(FAILED(x))                                               
    {                                                           
        DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true);  
    }                                                       
}   

, , , Map.

+2

All Articles