I just starting using Direct3D11 and I am trying to create a dynamic texture that I plan to update with new data several times a second. My issue is that every time I update the texture with the new data, I get this error from the D3D Debugger:
D3D11: ERROR: ID3D11DeviceContext::Map: This resource is already mapped! [RESOURCE_MANIPULATION ERROR #2097213: RESOURCE_MAP_ALREADYMAPPED ]
Which eventually turns into an E_OUTOFMEMORY Error from the Map call after running the application for a while.
I am creating my texture like this:
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 updating its data like this:
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);
Before I create the texture and map the data to it, all I have done in my program is initialize the Direct3D devices. I am completely stumped on what I am doing wrong. I have been trying to read as much as I can on msdn about this but nothing has helped me figure out what is going wrong. According to msdn, I am taking to proper steps:
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.
I am fairly certain I have filling out the texture description correctly too.
Could the issue be stemming from my initialization of my d3d devices? Am I misusing the map function? Did I not provide enough background information? Any ideas on whats going on?
Additional Information: I get the same "r开发者_开发知识库esource already mapped" error when I try to map data to a constant buffer for my vertex shader. Also, I tested it out on a different computer and got the same mapping problems.
Update: I downloaded some tutorial code for D3D11 and I took my texture creation and mapping code and plopped it in right after the tutorial code finished initializing d3d. I just wanted to see if I would get a mapping error. As expected, I did not get a "resource already mapped" error. Furthermore, when I copy the d3d initialization code from the tutorial and replace it with mine, I still get the resource already mapped error which even confuses me even further because all I do before initializing d3d is create a window.
Its funny how when you start asking questions about something your stuck on, it becomes easier to find the problem haha.
I figured out what the issue was. It was my HR macro that I was surrounding the map call with.
#define HR(x)
{
HRESULT hr = (x);
if(FAILED(x))
{
DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true);
}
}
I have no idea why it would the root of the problem, but I definitely would like know how this macro would have issue with the Map call.
精彩评论