gfxVertexFormat.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GFXVERTEXFORMAT_H_
  23. #define _GFXVERTEXFORMAT_H_
  24. #ifndef _TVECTOR_H_
  25. #include "core/util/tVector.h"
  26. #endif
  27. #ifndef _GFXENUMS_H_
  28. #include "gfx/gfxEnums.h"
  29. #endif
  30. /// The known Torque vertex element semantics. You can use
  31. /// other semantic strings, but they will be interpreted as
  32. /// a TEXCOORD.
  33. /// @see GFXVertexElement
  34. /// @see GFXVertexFormat
  35. namespace GFXSemantic
  36. {
  37. extern const String POSITION;
  38. extern const String NORMAL;
  39. extern const String BINORMAL;
  40. extern const String TANGENT;
  41. extern const String TANGENTW;
  42. extern const String COLOR;
  43. extern const String TEXCOORD;
  44. }
  45. /// This is a simple wrapper for the platform specific
  46. /// vertex declaration data which is held by the vertex
  47. /// format.
  48. ///
  49. /// If your using it... you probably shouldn't be.
  50. ///
  51. /// @see GFXVertexFormat
  52. class GFXVertexDecl
  53. {
  54. public:
  55. virtual ~GFXVertexDecl() {}
  56. };
  57. /// The element structure helps define the data layout
  58. /// for GFXVertexFormat.
  59. ///
  60. /// @see GFXVertexFormat
  61. ///
  62. class GFXVertexElement
  63. {
  64. friend class GFXVertexFormat;
  65. protected:
  66. /// The stream index when rendering from multiple
  67. /// vertex streams. In most cases this is 0.
  68. U32 mStreamIndex;
  69. /// A valid Torque shader symantic.
  70. /// @see GFXSemantic
  71. String mSemantic;
  72. /// The semantic index is used where there are
  73. /// multiple semantics of the same type. For
  74. /// instance with texcoords.
  75. U32 mSemanticIndex;
  76. /// The element type.
  77. GFXDeclType mType;
  78. public:
  79. /// Default constructor.
  80. GFXVertexElement()
  81. : mStreamIndex( 0 ),
  82. mSemanticIndex( 0 ),
  83. mType( GFXDeclType_Float4 )
  84. {
  85. }
  86. /// Copy constructor.
  87. GFXVertexElement( const GFXVertexElement &elem )
  88. : mStreamIndex( elem.mStreamIndex ),
  89. mSemantic( elem.mSemantic ),
  90. mSemanticIndex( elem.mSemanticIndex ),
  91. mType( elem.mType )
  92. {
  93. }
  94. /// Returns the stream index.
  95. U32 getStreamIndex() const { return mStreamIndex; }
  96. /// Returns the semantic name which is usually a
  97. /// valid Torque semantic.
  98. /// @see GFXSemantic
  99. const String& getSemantic() const { return mSemantic; }
  100. /// Returns the semantic index which is used where there
  101. /// are multiple semantics of the same type. For instance
  102. /// with texcoords.
  103. U32 getSemanticIndex() const { return mSemanticIndex; }
  104. /// Returns the type for the semantic.
  105. GFXDeclType getType() const { return mType; }
  106. /// Returns true of the semantic matches.
  107. bool isSemantic( const String& str ) const { return ( mSemantic == str ); }
  108. /// Returns the size in bytes of the semantic type.
  109. U32 getSizeInBytes() const;
  110. };
  111. /// The vertex format structure usually created via the declare and
  112. /// implement macros.
  113. ///
  114. /// You can use this class directly to create a vertex format, but
  115. /// note that it is expected to live as long as the VB that uses it
  116. /// exists.
  117. ///
  118. /// @see GFXDeclareVertexFormat
  119. /// @see GFXImplementVertexFormat
  120. /// @see GFXVertexElement
  121. ///
  122. class GFXVertexFormat
  123. {
  124. public:
  125. /// Default constructor for an empty format.
  126. GFXVertexFormat();
  127. /// The copy constructor.
  128. GFXVertexFormat( const GFXVertexFormat &format ) { copy( format ); }
  129. /// Copy the other vertex format.
  130. void copy( const GFXVertexFormat &format );
  131. /// Used to append a vertex format to the end of this one.
  132. void append( const GFXVertexFormat &format, U32 streamIndex = -1 );
  133. /// Returns a unique description string for this vertex format.
  134. const String& getDescription() const;
  135. /// Clears all the vertex elements.
  136. void clear();
  137. /// Adds a vertex element to the format.
  138. ///
  139. /// @param semantic A valid Torque semantic string.
  140. /// @param type The element type.
  141. /// @param index The semantic index which is typically only used for texcoords.
  142. ///
  143. void addElement( const String& semantic, GFXDeclType type, U32 index = 0, U32 stream = 0 );
  144. /// Returns true if there is a NORMAL semantic in this vertex format.
  145. bool hasNormal() const;
  146. /// Returns true if there is a TANGENT semantic in this vertex format.
  147. bool hasTangent() const;
  148. /// Returns true if there is a COLOR semantic in this vertex format.
  149. bool hasColor() const;
  150. /// Returns the texture coordinate count by
  151. /// counting the number of TEXCOORD semantics.
  152. U32 getTexCoordCount() const;
  153. /// Returns true if these two formats are equal.
  154. inline bool isEqual( const GFXVertexFormat &format ) const;
  155. /// Returns the total elements in this format.
  156. U32 getElementCount() const { return mElements.size(); }
  157. /// Returns the vertex element by index.
  158. const GFXVertexElement& getElement( U32 index ) const { return mElements[index]; }
  159. /// Returns the size in bytes of the format as described.
  160. U32 getSizeInBytes() const;
  161. /// Returns the hardware specific vertex declaration for this format.
  162. GFXVertexDecl* getDecl() const;
  163. protected:
  164. /// We disable the copy operator.
  165. GFXVertexFormat& operator =( const GFXVertexFormat& ) { return *this; }
  166. /// Recreates the description and state when
  167. /// the format has been modified.
  168. void _updateDirty();
  169. /// Requests the vertex declaration from the GFX device.
  170. void _updateDecl();
  171. /// Set when the element list is changed.
  172. bool mDirty;
  173. /// Is set if there is a NORMAL semantic in this vertex format.
  174. bool mHasNormal;
  175. /// Is true if there is a TANGENT semantic in this vertex format.
  176. bool mHasTangent;
  177. /// Is true if there is a COLOR semantic in this vertex format.
  178. bool mHasColor;
  179. /// The texture coordinate count by counting the
  180. /// number of "TEXCOORD" semantics.
  181. U32 mTexCoordCount;
  182. /// The size in bytes of the vertex format as described.
  183. U32 mSizeInBytes;
  184. /// An interned string which uniquely identifies the format.
  185. String mDescription;
  186. /// The elements of the vertex format.
  187. Vector<GFXVertexElement> mElements;
  188. /// The hardware specific vertex declaration.
  189. GFXVertexDecl *mDecl;
  190. };
  191. inline bool GFXVertexFormat::isEqual( const GFXVertexFormat &format ) const
  192. {
  193. // Comparing the strings works because we know both
  194. // these are interned strings. This saves one comparison
  195. // over the string equality operator.
  196. return getDescription().c_str() == format.getDescription().c_str();
  197. }
  198. /// This template class is usused to initialize the format in
  199. /// the GFXImplement/DeclareVertexFormat macros. You shouldn't
  200. /// need to use it directly in your code.
  201. ///
  202. /// @see GFXVertexFormat
  203. /// @see GFXImplementVertexFormat
  204. ///
  205. template<class T>
  206. class _GFXVertexFormatConstructor : public GFXVertexFormat
  207. {
  208. protected:
  209. void _construct();
  210. public:
  211. _GFXVertexFormatConstructor() { _construct(); }
  212. };
  213. /// Helper template function which returns the correct
  214. /// GFXVertexFormat object for a vertex structure.
  215. /// @see GFXVertexFormat
  216. template<class T> inline const GFXVertexFormat* getGFXVertexFormat();
  217. #ifdef TORQUE_OS_XENON
  218. /// On the Xbox360 we want we want to be sure that verts
  219. /// are on aligned boundariess.
  220. #define GFX_VERTEX_STRUCT __declspec(align(16)) struct
  221. #else
  222. #define GFX_VERTEX_STRUCT struct
  223. #endif
  224. /// The vertex format declaration which is usally placed in your header
  225. /// file. It should be used in conjunction with the implementation macro.
  226. ///
  227. /// @param name The name for the vertex structure.
  228. ///
  229. /// @code
  230. ///
  231. /// // A simple vertex format declaration.
  232. /// GFXDeclareVertexFormat( GFXVertexPCT )
  233. /// {
  234. /// Point3F pos;
  235. /// GFXVertexColor color;
  236. /// Point2F texCoord;
  237. /// }
  238. ///
  239. /// @endcode
  240. ///
  241. /// @see GFXImplementVertexFormat
  242. ///
  243. #define GFXDeclareVertexFormat( name ) \
  244. GFX_VERTEX_STRUCT name; \
  245. extern const GFXVertexFormat _gfxVertexFormat##name; \
  246. template<> inline const GFXVertexFormat* getGFXVertexFormat<name>() { static _GFXVertexFormatConstructor<name> vertexFormat; return &vertexFormat; } \
  247. GFX_VERTEX_STRUCT name \
  248. /// The vertex format implementation which is usally placed in your source
  249. /// file. It should be used in conjunction with the declaration macro.
  250. ///
  251. /// @param name The name of the vertex structure.
  252. ///
  253. /// @code
  254. ///
  255. /// // A simple vertex format implementation.
  256. /// GFXImplementVertexFormat( GFXVertexPCT )
  257. /// {
  258. /// addElement( "POSITION", GFXDeclType_Float3 );
  259. /// addElement( "COLOR", GFXDeclType_Color );
  260. /// addElement( "TEXCOORD", GFXDeclType_Float2, 0 );
  261. /// }
  262. ///
  263. /// @endcode
  264. ///
  265. /// @see GFXDeclareVertexFormat
  266. ///
  267. #define GFXImplementVertexFormat( name ) \
  268. template<> void _GFXVertexFormatConstructor<name>::_construct() \
  269. #endif // _GFXVERTEXFORMAT_H_