D3D11 创建着色器:编码的顶点着色器大小与指定大小不匹配
D3D11 Create Shader: Encoded Vertex Shader size doesn't match specified size
我正在使用 DirectX 11 开发游戏引擎,但在让着色器正确编码时遇到问题。我正在将着色器预编译为 .csh 文件并使用字节代码创建着色器。
我在尝试创建任何着色器时遇到此错误,但对于此示例,我将使用我的 PassThrough 顶点着色器。
D3D11 ERROR: ID3D11Device::CreateVertexShader: Encoded Vertex Shader size doesn't match specified size. [ STATE_CREATION ERROR #166: CREATEVERTEXSHADER_INVALIDSHADERBYTECODE]
着色器:
#include "../VertexLayouts.hlsli"
// structs in included file
struct PASS_THROUGH_VS
{
float3 pos : POSITION;
float2 texCoord : TEXCOORD;
};
struct PASS_THROUGH_PS
{
float4 pos : SV_POSITION;
float2 texCoord : TEXCOORD;
};
PASS_THROUGH_PS main( PASS_THROUGH_VS input )
{
PASS_THROUGH_PS output = (PASS_THROUGH_PS)(0);
output.pos = float4(input.pos, 1);
output.texCoord = input.texCoord;
return output;
}
使用这些设置:
PassThrough_PS properties
在Renderer.h
#include "Vertex Shaders\PassThrough_VS.csh"
在Renderer.cpp
HRESULT hrReturn;
hrReturn = CreateVertexShader(&PassThrough_VS, sizeof(PassThrough_VS), Pass_Through_VS);
if (FAILED(hrReturn)) {}
//return hrReturn;
HRESULT CRenderer::CreateVertexShader(const void* ptrByteCode, SIZE_T szByteCodeLength, eVertexShaderType type)
{
HRESULT hrReturn;
tVertShader newShader = {};
hrReturn = D3Device->CreateVertexShader(&ptrByteCode, szByteCodeLength, nullptr, &(newShader.m_id3dShader)); // WHERE ERROR OCCURS
if (FAILED(hrReturn))
return hrReturn;
newShader.m_ptrByteCode = ptrByteCode;
newShader.m_szByteCodeLength = szByteCodeLength;
D3VertexShaders[type] = newShader;
return hrReturn;
}
问题是您传递的是指向指针的指针,因此您实际上并未将着色器 blob 数据传递给 Direct3D。
HRESULT CRenderer::CreateVertexShader(const void* ptrByteCode, SIZE_T szByteCodeLength, eVertexShaderType type)
{
HRESULT hrReturn;
tVertShader newShader = {};
hrReturn = D3Device->CreateVertexShader(&ptrByteCode, szByteCodeLength,
nullptr, &(newShader.m_id3dShader));
...
正确的代码是:
HRESULT CRenderer::CreateVertexShader(const void* ptrByteCode, SIZE_T szByteCodeLength, eVertexShaderType type)
{
HRESULT hrReturn;
tVertShader newShader = {};
hrReturn = D3Device->CreateVertexShader(ptrByteCode, szByteCodeLength,
nullptr, &(newShader.m_id3dShader));
...
You should take a look at DirectX Tool Kit and the tutorials in particular.
您没有包含有关 tVertShader
类型的任何详细信息,但您很可能遇到引用计数问题。考虑使用 Microsoft::WRL::ComPtr
而不是原始指针来管理 Direct3D COM 对象的生命周期。参见 this article。
我正在使用 DirectX 11 开发游戏引擎,但在让着色器正确编码时遇到问题。我正在将着色器预编译为 .csh 文件并使用字节代码创建着色器。
我在尝试创建任何着色器时遇到此错误,但对于此示例,我将使用我的 PassThrough 顶点着色器。
D3D11 ERROR: ID3D11Device::CreateVertexShader: Encoded Vertex Shader size doesn't match specified size. [ STATE_CREATION ERROR #166: CREATEVERTEXSHADER_INVALIDSHADERBYTECODE]
着色器:
#include "../VertexLayouts.hlsli"
// structs in included file
struct PASS_THROUGH_VS
{
float3 pos : POSITION;
float2 texCoord : TEXCOORD;
};
struct PASS_THROUGH_PS
{
float4 pos : SV_POSITION;
float2 texCoord : TEXCOORD;
};
PASS_THROUGH_PS main( PASS_THROUGH_VS input )
{
PASS_THROUGH_PS output = (PASS_THROUGH_PS)(0);
output.pos = float4(input.pos, 1);
output.texCoord = input.texCoord;
return output;
}
使用这些设置: PassThrough_PS properties
在Renderer.h
#include "Vertex Shaders\PassThrough_VS.csh"
在Renderer.cpp
HRESULT hrReturn;
hrReturn = CreateVertexShader(&PassThrough_VS, sizeof(PassThrough_VS), Pass_Through_VS);
if (FAILED(hrReturn)) {}
//return hrReturn;
HRESULT CRenderer::CreateVertexShader(const void* ptrByteCode, SIZE_T szByteCodeLength, eVertexShaderType type)
{
HRESULT hrReturn;
tVertShader newShader = {};
hrReturn = D3Device->CreateVertexShader(&ptrByteCode, szByteCodeLength, nullptr, &(newShader.m_id3dShader)); // WHERE ERROR OCCURS
if (FAILED(hrReturn))
return hrReturn;
newShader.m_ptrByteCode = ptrByteCode;
newShader.m_szByteCodeLength = szByteCodeLength;
D3VertexShaders[type] = newShader;
return hrReturn;
}
问题是您传递的是指向指针的指针,因此您实际上并未将着色器 blob 数据传递给 Direct3D。
HRESULT CRenderer::CreateVertexShader(const void* ptrByteCode, SIZE_T szByteCodeLength, eVertexShaderType type)
{
HRESULT hrReturn;
tVertShader newShader = {};
hrReturn = D3Device->CreateVertexShader(&ptrByteCode, szByteCodeLength,
nullptr, &(newShader.m_id3dShader));
...
正确的代码是:
HRESULT CRenderer::CreateVertexShader(const void* ptrByteCode, SIZE_T szByteCodeLength, eVertexShaderType type)
{
HRESULT hrReturn;
tVertShader newShader = {};
hrReturn = D3Device->CreateVertexShader(ptrByteCode, szByteCodeLength,
nullptr, &(newShader.m_id3dShader));
...
You should take a look at DirectX Tool Kit and the tutorials in particular.
您没有包含有关 tVertShader
类型的任何详细信息,但您很可能遇到引用计数问题。考虑使用 Microsoft::WRL::ComPtr
而不是原始指针来管理 Direct3D COM 对象的生命周期。参见 this article。