我在
上遇到此错误result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
它表示无效的参数
我正在使用directX11并创建一个顶点缓冲类
下面是代码:
bool VertexBuffer::Init(ID3D11Device * device, Shader * shader, float size, bool writable)
{
m_shader = shader;
unsigned long* indices;
HRESULT result;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
m_vertexCount = 4;
m_indexCount = 6;
m_vertices = new VertexType[m_vertexCount];
indices = new unsigned long[m_indexCount];
float halfSize = size / 2.0f;
//Hardcoded quad from here...
//Load vertex array:
m_vertices[0].position = D3DXVECTOR3(-halfSize, -halfSize, 0.0f);
m_vertices[0].uv = D3DXVECTOR2(0.0f, 1.0f);
m_vertices[1].position = D3DXVECTOR3(-halfSize, halfSize, 0.0f);
m_vertices[1].uv = D3DXVECTOR2(0.0f, 0.0f);
m_vertices[2].position = D3DXVECTOR3(halfSize, halfSize, 0.0f);
m_vertices[2].uv = D3DXVECTOR2(1.0f, 0.0f);
m_vertices[3].position = D3DXVECTOR3(halfSize, -halfSize, 0.0f);
m_vertices[3].uv = D3DXVECTOR2(1.0f, 1.0f);
//Load indices:
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;
//To here.
//Setup vertex buffer desc:
vertexBufferDesc.Usage = (writable) ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType)*m_vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = (writable) ? D3D11_CPU_ACCESS_WRITE : D3D11_CPU_ACCESS_READ;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
//Setup vertex data:
vertexData.pSysMem = m_vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
//Create vertex buffer:
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer); // Here's the error im getting
if (FAILED(result))
{
Error(41);
return false;
}
任何想法都错了吗?
答案 0 :(得分:0)
我认为当vertexBufferDesc.CPUAccessFlags
为writable
时,false
设置为无效值。应将其设置为0,因为用法设置为D3D11_USAGE_DEFAULT
。来自D3D11_CPU_ACCESS_FLAG documentation:
<强> D3D11_CPU_ACCESS_READ 强>
资源是可映射的,以便CPU可以读取其内容。使用此标志创建的资源不能设置为管道的输入或输出,必须使用分段用法创建(参见D3D11_USAGE)。