How do Flexible vertex formats definitions define the order of the items in the struct?

StackOverflow https://stackoverflow.com/questions/22925277

  •  29-06-2023
  •  | 
  •  

Question

So I am a little confused as to what exactly is defined when I type:

 VertexFormat FVF_FORMAT = VertexFormat.Position | VertexFormat.Diffuse;

From reading the documentation here: http://msdn.microsoft.com/en-us/library/ms889239.aspx

It seems like the FVF format only defines what information is stored in this struct, not the order of the items in the struct. So how does the directx engine know where to find the position and color components?

or is vertexformat just meant to be a way for you to signal to other classes what is required by this vertex?

  struct MY_VERTEX
  {
    VertexFormat FVF_FORMAT = VertexFormat.Position | VertexFormat.Diffuse;


    public float X, Y, Z;
    public int Color;

    public MY_VERTEX(float x, float y, float z, int color)
    {
      this.X = -x;
      this.Y = -y;
      this.Z = z;
      this.Color = color;
    }
Was it helpful?

Solution

Good question, after a few minutes testing, I found DirectX don't care the FVF_FORMAT definition order, it only care the order of your vertex struct, so the following two lines get the same result.

VertexFormat FVF_FORMAT = VertexFormat.Position | VertexFormat.Diffuse;
VertexFormat FVF_FORMAT = VertexFormat.Diffuse | VertexFormat.Position;

Internally, D3DFVF_XYZ was defined before D3DFVF_Diffuse as below(d3d9types.h), so both above two lines will get a value of 0x42. When DirectX process the vertex, it may use bit-wise operation such as &. for example, 0x42(hex) = 01000010(bin), first test with D3D_FVF_XYZ

(0x42 & 0x002) != 0, this bit was set, so D3DFVF_XYZ was present! fetch some data from vertex buffer as vertex position. the same logic will apply to other macros.

#define D3DFVF_XYZ              0x002
#define D3DFVF_XYZRHW           0x004
#define D3DFVF_XYZB1            0x006
#define D3DFVF_XYZB2            0x008
#define D3DFVF_XYZB3            0x00a
#define D3DFVF_XYZB4            0x00c
#define D3DFVF_XYZB5            0x00e
#define D3DFVF_XYZW             0x4002

#define D3DFVF_NORMAL           0x010
#define D3DFVF_PSIZE            0x020
#define D3DFVF_DIFFUSE          0x040

This means you must define the Vertex in following order, first position, and then diffuse(the order depends on the macro definitions above).this is also true when you fill in data of your vertex buffer.

NOTE: The above conclusion was based on my test, and I didn't find any documents say this, you can also test your own code to see whether this is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top