D3D11创建着色器:编码的顶点着色器大小与指定的大小不匹配

时间:2017-01-06 04:18:04

标签: c++ rendering directx-11 vertex-shader

我正在使用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;
}

1 个答案:

答案 0 :(得分:0)

问题是你正在传递一个指向指针的指针,所以你实际上并没有将着色器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));
    ...    
  

您应该特别关注DirectX Tool Kittutorials

您没有包含有关tVertShader类型的任何详细信息,但您可能会引用计数问题。考虑使用Microsoft::WRL::ComPtr而不是原始指针来管理Direct3D COM对象的生命周期。请参阅this article