Skip navigation

error: Usage field contains value that is not a member of D3DDECLUSAGE

Aka CreateVertexDeclaration() returns E_FAIL: An undetermined error occurred

Ever run into this error?

[5108] Direct3D9: Decl Validator: X281: (Element Error) (Decl Element [2]) Usage field contains value that is not a member of D3DDECLUSAGE. Value: 0xcc. Aborting decl validation.

First of all, if you’re still only getting the extent of information that DxErrorDescription9() affords you then you’re not debugging properly (though that post is directed at xna users, it still works exactly the same for d3d c++ users)

In any case, to fix this problem. Did you forget to put a D3DDECL_END() vertex declaration at the END of your vertexElements array?

“Vertex data is defined using an array of D3DVERTEXELEMENT9 structures. Use D3DDECL_END to declare the last element in the declaration.”

The D3DDECL_END() macro just gives you an initializer for a D3DVERTEXELEMENT9 structure that signifies the end of the vertex declaration. Say you intend to create a vertex declaration that has a position and a color:

  D3DVERTEXELEMENT9 vertexElements[ 2 ] =
    pos, // defines a POSITION vertex element
    col   // defines a COLOR vertex element
  } ;

  IDirect3DVertexDeclaration9 * Vdecl ;

  // next line FAILS.
  hr = g.gpu->CreateVertexDeclaration( vertexElements, &Vdecl ) ;

Reason CreateVertexDeclaration fails is because its like calling strlen() on a string that doesn’t have a null-terminator at the end. D3D doesn’t know where to stop.

You get the error “Usage field contains value that is not a member of D3DDECLUSAGE.” because the random garbage memory value when D3D tries to read your “third” element (even though you don’t have one!) makes d3d angry and confused.

So you do this:

  D3DVERTEXELEMENT9 vertexElements[ 2 ] =

    // VERY IMPORTANT!  D3D doesn't konw
    // HOW MANY elements you will be specifying
    // in advance, so you TELL IT by passing
    // this SPECIAL D3DVERTEXELEMENT9 object
    // which is basically just like the null
    // terminator at the end of C string.
  } ;

And now it works.

Vertex Declarations in Direct3D9


One Comment

    • Yigal
    • Posted March 13, 2012 at 7:54 am
    • Permalink

    I have encountered some other cases in which CreateVertexDeclaration() returns E_FAIL:
    1. More than 1 element with the same usage and usage index
    2. Blending weights elemanee and more than 1 texture-coordinates element

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: