1
\$\begingroup\$

I am using MonoGame 3.8.1.303 with Windows/DirectX. I am just rendering a TriangleList using DrawIndexedPrimitives, but I am always getting this error when starting my game:

D3D11 ERROR: ID3D11Device::CreateInputLayout: The provided input signature expects to read an element with SemanticName/Index: 'SV_POSITION'/0, but the declaration doesn't provide a matching name. [ STATE_CREATION ERROR #163: CREATEINPUTLAYOUT_MISSINGELEMENT]

Exception thrown at 0x00007FFDAF45CD29 in realtimestrategy.exe: Microsoft C++ exception: _com_error at memory location 0x0000008CF917D388. Exception thrown: 'SharpDX.SharpDXException' in

SharpDX.Direct3D11.dll HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.

This is my vertex declaration:

public struct VertexTypePosTxtBlend : IVertexType
{
    public VertexTypePosTxtBlend(Vector3 pos, Vector2 txtCoord, Color blend)
    {
        this.pos = pos;
        this.txtCoord = txtCoord;
        this.blend = blend;
    }

    public Vector3 pos;
    public Vector2 txtCoord;
    public Color blend;

    public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
    (
        new VertexElement(sizeof(float) * 0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
        new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0),
        new VertexElement(sizeof(float) * 5, VertexElementFormat.Color, VertexElementUsage.Color, 0)
    );

    readonly VertexDeclaration IVertexType.VertexDeclaration => VertexDeclaration;
};

This is my HLSL shader:

float4x4 World;
float4x4 ViewProjection;
texture UserTexture1;
texture UserTexture2;
texture UserTexture3;
texture UserTexture4;

sampler userMap1 = sampler_state
{
    texture = <UserTexture1>;
};

sampler userMap2 = sampler_state
{
    texture = <UserTexture2>;
};

sampler userMap3 = sampler_state
{
    texture = <UserTexture3>;
};

sampler userMap4 = sampler_state
{
    texture = <UserTexture4>;
};

struct VS_INPUT
{
    float3 Position : SV_POSITION0;
    float2 Texcoord : TEXCOORD0;
    float4 Blend : COLOR0;
};

struct VS_OUTPUT
{
    float4 Position : SV_POSITION0;
    float2 Texcoord : TEXCOORD0;
    float4 Blend : COLOR0;
};

VS_OUTPUT vs_main(VS_INPUT Input)
{
    VS_OUTPUT Output;
    Output.Position = mul(Input.Position, mul(World, ViewProjection));
    Output.Texcoord = Input.Texcoord;
    Output.Blend = Input.Blend;
    return Output;
}

float4 ps_main(VS_OUTPUT Input) : SV_TARGET
{
   return tex2D(userMap1, Input.Texcoord) * Input.Blend.r
        + tex2D(userMap2, Input.Texcoord) * Input.Blend.g
        + tex2D(userMap3, Input.Texcoord) * Input.Blend.b
        + tex2D(userMap4, Input.Texcoord) * Input.Blend.a;           
}

technique
{
    pass
    {
        PixelShader = compile ps_4_0_level_9_1 ps_main();
        VertexShader = compile vs_4_0_level_9_1 vs_main();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ POSITION0 is the most correct semantic. Due to shader translation, the SM 3 translation to later shader modles becomes a function of the cross-platform FX compiler(MOJO-shader). Exceptions abound. \$\endgroup\$
    – user122973
    Jan 8, 2023 at 1:42

1 Answer 1

1
\$\begingroup\$

I changed all declarations in the shader from SV_POSITION0 to POSITION0 and it works now. I do not understand why, since in another project SV_POSITION0 was working fine and I also was thinking that SV_POSITION0 was the more correct one for new shader model >=4.0.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ For the new "System-Value Semantics", you need to use SV_Position and not SV_POSITION0. See learn.microsoft.com/en-us/windows/win32/direct3dhlsl/… \$\endgroup\$
    – Adam
    Jan 4, 2023 at 2:22
  • \$\begingroup\$ @Adam: I tried changing POSITION0 to SV_Position, but I get the same error. \$\endgroup\$
    – codymanix
    Jan 4, 2023 at 14:06
  • 1
    \$\begingroup\$ My apologies. I see what's going on now. SV_Position is a standard vertex shader output, and pixel shader input semantic. POSITION is still used for the vertex shader input semantics in D3D11. \$\endgroup\$
    – Adam
    Jan 5, 2023 at 0:39
  • \$\begingroup\$ @Adam: I still do not understand it. why can't I use SV_Position? \$\endgroup\$
    – codymanix
    Jan 6, 2023 at 0:35
  • 1
    \$\begingroup\$ You can use SV_Position, but only in your VS_OUTPUT structure as that's the output from the vertex shader, and the input to the pixel shader. For VS_INPUT, and the vertex declaration, you want to use POSITION. \$\endgroup\$
    – Adam
    Jan 6, 2023 at 21:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .