我正在尝试使用硬件实例化绘制一些多维数据集。
<小时/> 但是它们没有显示出来。所以我调试了我的代码,找到了问题的根源:
在我的VertexShader中,具有每个实例世界矩阵的var的值为:
{0.0f,0.0f,0.0f,0.0f, 0.0f,0.0f,0.0f,0.0f, 0.0f,0.0f,0.0f,0.0f, 0.0f,NaN,NaN,NaN}
最奇怪的是它输出NaN(非数字)。以下是我创建实例缓冲区的方法:
XMMATRIX trans[4];
trans[0] = XMMatrixTranslation(0.0f, 0.0f, 0.0f);
trans[1] = XMMatrixTranslation(0.5f, 0.5f, 0.5f);
trans[2] = XMMatrixTranslation(-0.5, -0.5, 0.5);
trans[3] = XMMatrixTranslation(0.5, -0.5f, 0.5);
//Store world matrices
for (int i = 0; i < 4; i++)
XMStoreFloat4x4(&mIV[i].world, trans[i]);
D3D11_BUFFER_DESC instDesc;
ZeroMemory(&instDesc, sizeof(D3D11_BUFFER_DESC));
instDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instDesc.Usage = D3D11_USAGE_DYNAMIC;
instDesc.ByteWidth = sizeof(XMFLOAT4X4) * 4;
instDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
//Create instanced buffer
HR(mDevice->CreateBuffer(&instDesc, NULL, &mBoxInstB));
以下是我将实例数据映射到缓冲区的方法:
D3D11_MAPPED_SUBRESOURCE mapSub;
mContext->Map(mBoxInstB, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mapSub);
VertexI* idata = reinterpret_cast<VertexI*>(mapSub.pData);
idata = mIV;
mContext->Unmap(mBoxInstB, 0);
感谢您的帮助! 如果您需要更多信息,请在评论中写下来!
答案 0 :(得分:2)
idata = mIV
不会复制任何数据。你想要的是memcpy(mapSub.pData, &mIV[0].world, instDesc.ByteWidth)
假设world
成员是mIV[i]
的全部内容并且在内存中是连续的。