人。我正处于开始为我的游戏引擎添加实例的地步。这是我做的通用函数,所以我可以传入一个实例缓冲区指针和一些矩阵,以及要绘制的实例列表。但是,在其第一个循环中,缓冲区为NULL,因此它尝试初始化它,但它失败并且HR = E_INVALID_ARGS。我不确定论证的哪一部分是无效的。
目标是每个实例以XMFLOAT4X4的形式传递两个4X4矩阵。
“sizeofXMFLOAT4X4”变量只是一个健全性检查,可以查看它读取的字节数(64,4bytes * 4floats / vec * 4 vec = 64)。
如果有人能够发现这里可能出现的问题而且可以指出它,我会非常感激。如果您需要其他信息,请与我们联系。感谢。
void StaticSceneryListClass::UpdateInstanceBuffer(ID3D11Buffer *pInstanceBuffer, XMFLOAT4X4 VPMat, XMFLOAT4X4 tileMat, vector<StaticSceneryStruct> culledInstanceList)
{
//Pass this the buffer you want to update, the VPMat for the frame, and a list of instances to draw and it will fill the buffer with the stuff and things.
//If the buffer is not initialized yet, it will be initialized too.
//first, create the data needed.
vector<XMFLOAT4X4> instanceMats;
for (int i = 0; i < culledInstanceList.size(); i++)
{
XMFLOAT4X4 WorldMat = xmCon->MatTranspose(xmCon->MatMultiply(culledInstanceList[i].worldMat, tileMat));
XMFLOAT4X4 WVP = xmCon->MatTranspose(xmCon->MatMultiply(WorldMat, VPMat));
instanceMats.push_back(WVP);
instanceMats.push_back(WorldMat);
}
if (pInstanceBuffer == NULL)
{
//buffer not initialized yet, initialize it.
D3D11_BUFFER_DESC instBuffDesc;
ZeroMemory(&instBuffDesc, sizeof(instBuffDesc));
sizeofXMFLOAT4X4 = sizeof(XMFLOAT4X4);
instBuffDesc.Usage = D3D11_USAGE_DYNAMIC;
instBuffDesc.ByteWidth = sizeofXMFLOAT4X4 * 2 * culledInstanceList.size();
instBuffDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instBuffDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
instBuffDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA instData;
ZeroMemory(&instData, sizeof(instData));
instData.pSysMem = &instanceMats[0];
HRESULT hr = graphicsPointers.pdev->CreateBuffer(&instBuffDesc, &instData, &pInstanceBuffer);
if (FAILED(hr))
{
//Fails here with E_INVALID_ARG
MessageBox(NULL, "Failed to create instance buffer!", "StaticSceneryList", MB_OK);
}
}
else
{
//buffer already initialized, need to use resource may and unmap.
D3D11_MAPPED_SUBRESOURCE mappedResource;
ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
// Disable GPU access to the vertex buffer data.
graphicsPointers.pdevcon->Map(pInstanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
// Update the vertex buffer here.
memcpy(mappedResource.pData, &instanceMats[0], sizeof(XMFLOAT4X4) * 2 * culledInstanceList.size());
// Reenable GPU access to the vertex buffer data.
graphicsPointers.pdevcon->Unmap(pInstanceBuffer, 0);
}
UPDATE1:12/2/2017 2:02 CST 我今天早上一直在运行各种调试测试,结果很有趣。我尝试隔离问题,发现传入null数据或缓冲区的新局部指针都没有做任何事情。我打开了调试层,它没有给我创建缓冲区语句的输出。我尝试使用缓冲区描述的参数来查看它会做什么,这些给了我一个错误。我添加了一个catch,如果淘汰列表的大小为0,即使它不是16的倍数。这使它偶尔通过,但它从未实际产生工作缓冲区。事实上,它通常仍然失败... S_OK ?? FAILED(hr)以HR为S_OK而下降,这没有多大意义。当我查看缓冲区指针的内存值时,它会从null变为具有地址,但它不会被“if not null,release并设置为null”块捕获。
这有趣的副作用是它导致了大量的内存泄漏。过程记忆在大约10秒内以接近线性的速率爬升到3gb以上(我在那时关闭程序,它仍然非常敏感)。
当然,我似乎偶尔也会得到通行证,因为如果缓冲区已初始化的代码尚未触发,那么它似乎永远不会发生。
以下是当前更新的代码:
void StaticSceneryListClass::UpdateInstanceBuffer(ID3D11Buffer *pInstanceBuffer, XMFLOAT4X4 VPMat, XMFLOAT4X4 tileMat, vector<StaticSceneryStruct> culledInstanceList)
{
//Pass this the buffer you want to update, the VPMat for the frame, and a list of instances to draw and it will fill the buffer with the stuff and things.
//If the buffer is not initialized yet, it will be initialized too.
//Skip this process if there aren't any instances to draw
if (culledInstanceList.size() > 0)
{
//first, create the data needed.
vector<XMFLOAT4X4> instanceMats;
for (int i = 0; i < culledInstanceList.size(); i++)
{
XMFLOAT4X4 WorldMat = xmCon->MatTranspose(xmCon->MatMultiply(culledInstanceList[i].worldMat, tileMat));
XMFLOAT4X4 WVP = xmCon->MatTranspose(xmCon->MatMultiply(WorldMat, VPMat));
instanceMats.push_back(WVP);
instanceMats.push_back(WorldMat);
}
if (pInstanceBuffer == NULL)
{
//buffer not initialized yet, initialize it.
D3D11_BUFFER_DESC instBuffDesc;
ZeroMemory(&instBuffDesc, sizeof(instBuffDesc));
bytewidth = sizeof(XMFLOAT4X4) * 2 * culledInstanceList.size();
if (bytewidth % 16 != 0)
{
//hasn't fallen into here yet, which makes sense.
MessageBox(NULL, "Instance Buffer Byte Width Not a multiple of 16", "StaticSceneryList", MB_OK);
}
instBuffDesc.Usage = D3D11_USAGE_DYNAMIC;
instBuffDesc.ByteWidth = bytewidth;
instBuffDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instBuffDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
instBuffDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA instData;
ZeroMemory(&instData, sizeof(instData));
instData.pSysMem = &instanceMats[0];
HRESULT hr = graphicsPointers.pdev->CreateBuffer(&instBuffDesc, &instData, &pInstanceBuffer);
//HRESULT hr = graphicsPointers.pdev->CreateBuffer(&instBuffDesc, &instData, &debugInstanceBuffer); //DEBUG
if (FAILED(hr))
{
//Fails here with S_OK lulwut
MessageBox(NULL, "Failed to create instance buffer!", "StaticSceneryList", MB_OK);
if (pInstanceBuffer != NULL)
{
//This doesn't seem to trigger even though the address is not 0x0000etc
pInstanceBuffer->Release();
pInstanceBuffer = NULL;
}
}
else
{
//occasionally gets here but it doesn't seem to stick
MessageBox(NULL, "Successfully created instance buffer.", "StaticSceneryList", MB_OK);
}
}
else
{
//never seems to get to here.
//buffer already initialized, need to use resource may and unmap.
D3D11_MAPPED_SUBRESOURCE mappedResource;
ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
// Disable GPU access to the vertex buffer data.
graphicsPointers.pdevcon->Map(pInstanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
// Update the vertex buffer here.
memcpy(mappedResource.pData, &instanceMats[0], sizeof(XMFLOAT4X4) * 2 * culledInstanceList.size());
// Reenable GPU access to the vertex buffer data.
graphicsPointers.pdevcon->Unmap(pInstanceBuffer, 0);
}
}//if there is anything to draw
答案 0 :(得分:0)
所以经过一堆没有运气的测试之后,我把代码移到了调用这个函数的地方,然后直接用对象而不是间接地编写它,我得到了它,至少使用默认缓冲区。我不知道为什么,但是有一些事情在传递函数调用时会导致它最终为null,因此会发生一些不稳定的事情。
另外,请注意,当您在输入布局中声明四个R32B32G32A32_FLOAT向量系列,然后在顶点着色器中将它们作为四个float4拉入,然后使用float4x4(vec1,vec2,vec3,vec4)将它们组合起来,在将XMFLOAT4X4放入缓冲区数据之前,不要转置它!