映射动态纹理导致“已映射错误”
我刚刚开始使用 Direct3D11,我正在尝试创建一个动态纹理,我计划每秒使用新数据更新几次。我的问题是,每次使用新数据更新纹理时,我都会从 D3D 调试器收到此错误:
D3D11:错误:ID3D11DeviceContext::Map:此资源已映射! [RESOURCE_MANIPULATION ERROR #2097213: RESOURCE_MAP_ALREADYMAPPED ]
运行应用程序一段时间后,最终会变成 Map 调用的 E_OUTOFMEMORY 错误。
我像这样创建纹理:
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));
并像这样更新其数据:
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 之前我所做的只是创建一个窗口。
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 "resource 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的是,当你开始就你所坚持的事情提出问题时,找到问题就变得更容易了哈哈。
我明白了问题所在。我用的是我的 HR 宏来进行地图调用。
我不知道为什么它会是问题的根源,但我绝对想知道这个宏如何与 Map 调用产生问题。
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.
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.