gpt4 book ai didi

c++ - 动态顶点缓冲区创建失败

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:53 25 4
gpt4 key购买 nike

我正在尝试创建一个动态顶点缓冲区,以便我可以使用 ID3D11DeviceContext::Map() 写入缓冲区。但是,每次我尝试创建顶点缓冲区时都会失败。这是我的代码。我在其他项目中成功地做到了这一点,所以我不知道为什么我写的东西不起作用。这是代码:

//create vertex buffer
Vertex* vertices = static_cast<Vertex*>(std::malloc((4000) * sizeof(Vertex)));
for (int i = 0; i < 4000; i++)
{
vertices[i].pos.x = 0.0f;
vertices[i].pos.y = 0.0f;
vertices[i].pos.z = 0.0f;
vertices[i].tex.x = 0.0f;
vertices[i].tex.y = 0.0f;
}
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof( Vertex ) * 4000;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = vertices;
HRESULT hr = Device->CreateBuffer( &bd, &InitData, &VertexBuffer );

std::free(vertices);

把代码弄乱后,我注意到这段代码成功了:

//create vertex buffer
Vertex* vertices = static_cast<Vertex*>(std::malloc((4000) * sizeof(Vertex)));
for (int i = 0; i < 4000; i++)
{
vertices[i].pos.x = 0.0f;
vertices[i].pos.y = 0.0f;
vertices[i].pos.z = 0.0f;
vertices[i].tex.x = 0.0f;
vertices[i].tex.y = 0.0f;
}
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof( Vertex ) * 4000;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = vertices;
HRESULT hr = Device->CreateBuffer( &bd, &InitData, &VertexBuffer );

std::free(vertices);

这段代码也成功了:

//create vertex buffer
Vertex* vertices = static_cast<Vertex*>(std::malloc((4000) * sizeof(Vertex)));
for (int i = 0; i < 4000; i++)
{
vertices[i].pos.x = 0.0f;
vertices[i].pos.y = 0.0f;
vertices[i].pos.z = 0.0f;
vertices[i].tex.x = 0.0f;
vertices[i].tex.y = 0.0f;
}
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( Vertex ) * 4000;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = vertices;
HRESULT hr = Device->CreateBuffer( &bd, &InitData, &VertexBuffer );

std::free(vertices);

似乎只有当 bd.Usage = D3D11_USAGE_DYNAMIC 和 bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE 同时创建顶点缓冲区时才会失败。关于问题是什么的任何想法?

最佳答案

我记得前一段时间遇到过这个问题。正如 MSDN 所说,当您将资源创建为动态资源时,您应该使用 ID3D11DeviceContext::Map() 填充它。哪怕是第一次。因此,当您创建缓冲区时,只需尝试传递 NULL 而不是 &InitData。创建缓冲区后,对其进行映射和填充。据我所知,这解决了我的问题。这是来源。

http://msdn.microsoft.com/en-us/library/windows/desktop/ff476899%28v=vs.85%29.aspx

关于c++ - 动态顶点缓冲区创建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25589346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com