dotnet/Silk.NET

Creating the input layout fails

jjb-pro opened this issue · 0 comments

Hello,
I am using DirectX 11 for my engine, and I am encountering apparently random errors during input layout creation.

My shader code

#pragma pack_matrix(row_major)

// for vertex shader
cbuffer perObjectBuffer : register(b0)
{
    float4x4 worldMatrix;
    float4x4 viewProjectionMatrix;
    float4x4 worldViewProjectionMatrix;
    float4x4 shadowMatrix;
    float4x4 viewInverseMatrix;
};

struct VSInput
{
    float3 LocalPosition : POSITION;
    float3 Normal : NORMAL;
    float2 TextureCoordinate : TEXCOORD;
};

struct PSInput
{
    float4 ClipPosition : SV_POSITION;
    float3 LocalPosition : TEXCOORD;
};

PSInput vs_main(VSInput input)
{
    PSInput output;
    
    output.ClipPosition = mul(float4(input.LocalPosition, 1.0), worldViewProjectionMatrix).xyww;
    output.LocalPosition = input.LocalPosition;
    
    return output;
}

SamplerState objSamplerState : SAMPLER : register(s0);
TextureCube mainTexture : TEXTURE : register(t0);

float4 ps_main(PSInput input) : SV_Target
{
    return mainTexture.Sample(objSamplerState, input.LocalPosition);
}

Observed Errors

System.ArgumentException: Value does not fall within the expected range. when calling Device.CreateInputLayout:

SilkMarshal.ThrowHResult
 (
    _renderContext.Device.CreateInputLayout
    (
       in _inputElementDescriptions[0],
       (uint)_inputElementDescriptions.Length,
       vertexCode.GetBufferPointer(),
       vertexCode.GetBufferSize(),
       ref _inputLayout
    )
);

ID3D11Device::CreateInputLayout: "The provided input signature expects to read an element with SemanticName/Index: 'NORMAL'/0, but the declaration doesn't provide a matching name." even if the shader contains the semantic NORMAL.

When renaming the NORMAL semantic to NORMA, the error changes the reference to TEXCOORD. Occasionally, the shaders compile and render correctly without changes. Also, sometimes I get errors about duplicate semantics (e.g. Element[3] and Element[2405181685762] have the same Semantic ( 0)) that don't exist.

I use my engine once in WinUI 3 and once in the Silk.NET window. With WinUI 3, the problem hardly ever occurs on my device. On another, it occurs every time. With the Silk.NET window it also occurs randomly. Only after I added XAudio 2 (i.e. I create an instance of the audio context), the problem occurs much more often.

These issues do not only occur with this shader. If you replace the current shader with another one, the same bugs occur.

I have added parts of my Shader class (Shader.txt), which is largely based on the Silk.NET sample.