CreateInputLayout()返回E_INVALIDARG

时间:2014-07-10 12:05:46

标签: directx directx-11

Shader Code 

struct Light
{
    float3 dir;
    float4 ambient;
    float4 diffuse;
};

cbuffer cbPerFrame
{
    Light light;
};

cbuffer cbPerObject
{
    float4x4 WVP;
    float4x4 World;
};

Texture2D ObjTexture;
SamplerState ObjSamplerState;

struct VS_OUTPUT
{
    float4 Pos : SV_POSITION;
    float2 TexCoord : TEXCOORD;
    float3 normal : NORMAL;
    float4 InstancePos : INSTANCEPOS;
};

VS_OUTPUT VS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD, float3 normal : NORMAL , float4 InstancePos : INSTANCEPOS;)
{
    VS_OUTPUT output;

    output.Pos.x+=InstancePos.x;
    output.Pos.y+=InstancePos.y;
    output.Pos.z+=InstancePos.z;

    output.Pos = mul(inPos, WVP);
    output.TexCoord = inTexCoord;

    output.normal = mul(normal, World);
    output.normal = normalize(output.normal);

    return output;
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuse = ObjTexture.Sample( ObjSamplerState, input.TexCoord );

    float3 finalColor;
    finalColor = diffuse * light.ambient;
    finalColor += saturate(dot(light.dir, input.normal) * light.diffuse * diffuse);

    clip(diffuse.a - .10);

    return float4(finalColor, diffuse.a);
}

//// creation

D3D11_INPUT_ELEMENT_DESC layout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
    { "TEXCOORD0", 0, DXGI_FORMAT_R32G32_FLOAT,    0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
    { "NORMAL",  0, DXGI_FORMAT_R32G32B32_FLOAT,  0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0},
    { "INSTANCEPOS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1} 
};


hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);


//Create the Shader Objects
hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

//===============Create the Input Layout==================================
hr = d3d11Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertLayout );

CreateInputLayout正在返回E_INVALIDARG。我试过了,但它没有用。我添加了D3D11_INPUT_PER_INSTANCE_DATA标记,因为我正在使用实例,但是当我删除它时它可以正常工作。

1 个答案:

答案 0 :(得分:0)

你确定着色器编译而且vs_buffer不是nil吗?该分号应该会给您一个编译错误:: INSTANCEPOS;)

布局设置不会显示应失败的无效值,因此不是此处的着色器。考虑到位置格式,你的texcoord偏移应该是12而不是16,你渲染是假的,但技术上有效。