microsoft / DirectXMesh

DirectXMesh geometry processing library
https://walbourn.github.io/directxmesh/
MIT License
786 stars 152 forks source link

FlexibleVertexFormat.h header added to Utilities #58

Closed walbourn closed 3 years ago

walbourn commented 3 years ago

This PR adds a new utility header to the DirectXMesh repo. This contains helper functions for working with Direct3D 9 legacy FVF codes and Direct3D 9 decls. This is useful for tools parsing through older resources, Xfiles, etc. Also added secure versions of two functions that really need bounds to be SAL annotatable.

This header does not require any legacy DirectX SDK headers, but will only build for the "Win32 Desktop" partition because it uses d3d9.h (i.e. it's not usable in UWP apps. If that's an issue, please note that in this review).

walbourn commented 3 years ago
size_t ComputeVertexSize(uint32_t fvfCode);
size_t ComputeVertexSize(const D3DVERTEXELEMENT9* pDecl, uint32_t stream);
size_t ComputeVertexSize(const D3DVERTEXELEMENT9* pDecl, size_t maxDeclLength, uint32_t stream);
size_t GetDeclLength(const D3DVERTEXELEMENT9* pDecl);
bool CreateDeclFromFVF(uint32_t fvfCode, std::vector<D3DVERTEXELEMENT9>& decl);

If the <d3d11.h> header is included before you include FlexibleVertexFormat.h, you get:

bool CreateInputLayoutFromFVF(uint32_t fvfCode, std::vector<D3D11_INPUT_ELEMENT_DESC>& il)

If the <d3d12.h> header is included before you include FlexibleVertexFormat.h, you get:

bool CreateInputLayoutFromFVF(uint32_t fvfCode, std::vector<D3D12_INPUT_ELEMENT_DESC>& il)
uint32_t ComputeFVF(const D3DVERTEXELEMENT9* pDecl);
uint32_t ComputeFVF(const D3DVERTEXELEMENT9* pDecl, size_t maxDeclLength);

I didn't bother with a ComputeFVF from a Direct3D 11 or Direct3D 12 input layout, but let me know if that's important.

walbourn commented 3 years ago

Test suite updates are here

walbourn commented 3 years ago

Porting table:

D3DX FlexibleVertexFormat.h
D3DXGetFVFVertexSize ComputeVertexSize
D3DXGetDeclVertexSize ComputeVertexSize
D3DXGetDeclLength GetDeclLength
D3DXDeclaratorFromFVF CreateDeclFromFVF
CreateInputLayoutFromFVF
D3DXFVFFromDeclarator ComputeFVF

While my functions are equivalent to these older ones, I have improved them somewhat. They have a few more bounds checks, invalid FVFs return 0 or false in more cases, and the original functions didn't handle D3DFVF_XYZW.