为什么 ID3D11Device::CreateInputLayout 以 E_INVALIDARG 失败?
Why did ID3D11Device::CreateInputLayout fail with E_INVALIDARG?
我正在使用 DirectX11 用 C++ 编写程序。现在我想从着色器开始,为此我还需要 ID3D11InputLayout。因为我想给出选择以备后用。我有两个要传递给 VertexShader 的顶点结构:
typedef struct _Vertex1
{
XMFLOAT3 Pos;
XMFLOAT4 Color;
} VertexPosColor;
typedef struct _Vertex2
{
XMFLOAT3 Pos;
XMFLOAT3 Normal;
XMFLOAT2 Tex0;
XMFLOAT2 Tex1;
};
不,我正在创建 ID3D11InputLayouts,如下所示:
// Create INPUTLAYOUT 1
D3D11_INPUT_ELEMENT_DESC vertex1Desc[] =
{
{"POSITION", 0U, DXGI_FORMAT_R32G32B32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"COLOR", 0U, DXGI_FORMAT_R32G32B32A32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
};
hr = GInterface::m_d3dDevice->CreateInputLayout(vertex1Desc, 2U, pVertexBlob->GetBufferPointer(), pVertexBlob->GetBufferSize(), &GInterface::m_d3dInputLayouts[0]);
if (FAILED(hr)) {
EErrors::printError(L"Failed to create input layout 1!", L"Pipeline Error", L"CreateInputLayout", EEE_D3D11, hr);
return false;
}
// Create INPUTLAYOUT 2
D3D11_INPUT_ELEMENT_DESC vertex2Desc[] =
{
{"POSITION", 0U, DXGI_FORMAT_R32G32B32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"NORMAL", 0U, DXGI_FORMAT_R32G32B32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"TEXCOORD", 0U, DXGI_FORMAT_R32G32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"TEXCOORD", 1U, DXGI_FORMAT_R32G32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
};
hr = GInterface::m_d3dDevice->CreateInputLayout(vertex2Desc, 4U, pVertexBlob->GetBufferPointer(), pVertexBlob->GetBufferSize(), &GInterface::m_d3dInputLayouts[1]);
if (FAILED(hr)) {
EErrors::printError(L"Failed to create input layout 2!", L"Pipeline Error", L"CreateInputLayout", EEE_D3D11, hr);
return false;
}
第一次调用 CreateInputLayout 函数成功,但第二次 returns 一个 E_INVALIDARG HRESULT。
我不知道为什么...
InputLayouts 的变量是静态的,初始化如下:
ID3D11InputLayout** GInterface::m_d3dInputLayouts = (ID3D11InputLayout**)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3D11InputLayout*) * GInterface::m_d3dInputLayoutsAmount);
非常感谢您的回答,谢谢
相同的着色器字节码似乎作为输入传递给 CreateInputLayout
。因此,预期相同的输入布局与声明的 D3D11_INPUT_ELEMENT_DESC
描述符不匹配。如果您希望使用不同的输入布局,则必须使用匹配的着色器创建每个布局。
我正在使用 DirectX11 用 C++ 编写程序。现在我想从着色器开始,为此我还需要 ID3D11InputLayout。因为我想给出选择以备后用。我有两个要传递给 VertexShader 的顶点结构:
typedef struct _Vertex1
{
XMFLOAT3 Pos;
XMFLOAT4 Color;
} VertexPosColor;
typedef struct _Vertex2
{
XMFLOAT3 Pos;
XMFLOAT3 Normal;
XMFLOAT2 Tex0;
XMFLOAT2 Tex1;
};
不,我正在创建 ID3D11InputLayouts,如下所示:
// Create INPUTLAYOUT 1
D3D11_INPUT_ELEMENT_DESC vertex1Desc[] =
{
{"POSITION", 0U, DXGI_FORMAT_R32G32B32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"COLOR", 0U, DXGI_FORMAT_R32G32B32A32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
};
hr = GInterface::m_d3dDevice->CreateInputLayout(vertex1Desc, 2U, pVertexBlob->GetBufferPointer(), pVertexBlob->GetBufferSize(), &GInterface::m_d3dInputLayouts[0]);
if (FAILED(hr)) {
EErrors::printError(L"Failed to create input layout 1!", L"Pipeline Error", L"CreateInputLayout", EEE_D3D11, hr);
return false;
}
// Create INPUTLAYOUT 2
D3D11_INPUT_ELEMENT_DESC vertex2Desc[] =
{
{"POSITION", 0U, DXGI_FORMAT_R32G32B32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"NORMAL", 0U, DXGI_FORMAT_R32G32B32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"TEXCOORD", 0U, DXGI_FORMAT_R32G32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
{"TEXCOORD", 1U, DXGI_FORMAT_R32G32_FLOAT, 0U, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0U},
};
hr = GInterface::m_d3dDevice->CreateInputLayout(vertex2Desc, 4U, pVertexBlob->GetBufferPointer(), pVertexBlob->GetBufferSize(), &GInterface::m_d3dInputLayouts[1]);
if (FAILED(hr)) {
EErrors::printError(L"Failed to create input layout 2!", L"Pipeline Error", L"CreateInputLayout", EEE_D3D11, hr);
return false;
}
第一次调用 CreateInputLayout 函数成功,但第二次 returns 一个 E_INVALIDARG HRESULT。 我不知道为什么...
InputLayouts 的变量是静态的,初始化如下:
ID3D11InputLayout** GInterface::m_d3dInputLayouts = (ID3D11InputLayout**)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3D11InputLayout*) * GInterface::m_d3dInputLayoutsAmount);
非常感谢您的回答,谢谢
相同的着色器字节码似乎作为输入传递给 CreateInputLayout
。因此,预期相同的输入布局与声明的 D3D11_INPUT_ELEMENT_DESC
描述符不匹配。如果您希望使用不同的输入布局,则必须使用匹配的着色器创建每个布局。