gfxVertexFormat.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /// Tell this format it has instancing
  130. void enableInstancing();
  131. /// Copy the other vertex format.
  132. void copy( const GFXVertexFormat &format );
  133. /// Used to append a vertex format to the end of this one.
  134. void append( const GFXVertexFormat &format, U32 streamIndex = -1 );
  135. /// Returns a unique description string for this vertex format.
  136. const String& getDescription() const;
  137. /// Clears all the vertex elements.
  138. void clear();
  139. /// Adds a vertex element to the format.
  140. ///
  141. /// @param semantic A valid Torque semantic string.
  142. /// @param type The element type.
  143. /// @param index The semantic index which is typically only used for texcoords.
  144. ///
  145. void addElement( const String& semantic, GFXDeclType type, U32 index = 0, U32 stream = 0 );
  146. /// Returns true if there is a NORMAL semantic in this vertex format.
  147. bool hasNormal() const;
  148. /// Returns true if there is a TANGENT semantic in this vertex format.
  149. bool hasTangent() const;
  150. /// Returns true if there is a COLOR semantic in this vertex format.
  151. bool hasColor() const;
  152. /// Return true if instancing is used with this vertex format.
  153. bool hasInstancing() const;
  154. /// Returns the texture coordinate count by
  155. /// counting the number of TEXCOORD semantics.
  156. U32 getTexCoordCount() const;
  157. /// Returns true if these two formats are equal.
  158. inline bool isEqual( const GFXVertexFormat &format ) const;
  159. /// Returns the total elements in this format.
  160. U32 getElementCount() const { return mElements.size(); }
  161. /// Returns the vertex element by index.
  162. const GFXVertexElement& getElement( U32 index ) const { return mElements[index]; }
  163. /// Returns the size in bytes of the format as described.
  164. U32 getSizeInBytes() const;
  165. /// Returns the hardware specific vertex declaration for this format.
  166. GFXVertexDecl* getDecl() const;
  167. protected:
  168. /// We disable the copy operator.
  169. GFXVertexFormat& operator =( const GFXVertexFormat& ) { return *this; }
  170. /// Recreates the description and state when
  171. /// the format has been modified.
  172. void _updateDirty();
  173. /// Requests the vertex declaration from the GFX device.
  174. void _updateDecl();
  175. /// Set when the element list is changed.
  176. bool mDirty;
  177. /// Is set if there is a NORMAL semantic in this vertex format.
  178. bool mHasNormal;
  179. /// Is true if there is a TANGENT semantic in this vertex format.
  180. bool mHasTangent;
  181. /// Is true if there is a COLOR semantic in this vertex format.
  182. bool mHasColor;
  183. /// Is instaning used with this vertex format.
  184. bool mHasInstancing;
  185. /// The texture coordinate count by counting the
  186. /// number of "TEXCOORD" semantics.
  187. U32 mTexCoordCount;
  188. /// The size in bytes of the vertex format as described.
  189. U32 mSizeInBytes;
  190. /// An interned string which uniquely identifies the format.
  191. String mDescription;
  192. /// The elements of the vertex format.
  193. Vector<GFXVertexElement> mElements;
  194. /// The hardware specific vertex declaration.
  195. GFXVertexDecl *mDecl;
  196. };
  197. inline bool GFXVertexFormat::isEqual( const GFXVertexFormat &format ) const
  198. {
  199. // Comparing the strings works because we know both
  200. // these are interned strings. This saves one comparison
  201. // over the string equality operator.
  202. return getDescription().c_str() == format.getDescription().c_str();
  203. }
  204. /// This template class is usused to initialize the format in
  205. /// the GFXImplement/DeclareVertexFormat macros. You shouldn't
  206. /// need to use it directly in your code.
  207. ///
  208. /// @see GFXVertexFormat
  209. /// @see GFXImplementVertexFormat
  210. ///
  211. template<class T>
  212. class _GFXVertexFormatConstructor : public GFXVertexFormat
  213. {
  214. protected:
  215. void _construct();
  216. public:
  217. _GFXVertexFormatConstructor() { _construct(); }
  218. };
  219. /// Helper template function which returns the correct
  220. /// GFXVertexFormat object for a vertex structure.
  221. /// @see GFXVertexFormat
  222. template<class T> inline const GFXVertexFormat* getGFXVertexFormat();
  223. #ifdef TORQUE_OS_XENON
  224. /// On the Xbox360 we want we want to be sure that verts
  225. /// are on aligned boundariess.
  226. #define GFX_VERTEX_STRUCT __declspec(align(16)) struct
  227. #else
  228. #define GFX_VERTEX_STRUCT struct
  229. #endif
  230. /// The vertex format declaration which is usally placed in your header
  231. /// file. It should be used in conjunction with the implementation macro.
  232. ///
  233. /// @param name The name for the vertex structure.
  234. ///
  235. /// @code
  236. ///
  237. /// // A simple vertex format declaration.
  238. /// GFXDeclareVertexFormat( GFXVertexPCT )
  239. /// {
  240. /// Point3F pos;
  241. /// GFXVertexColor color;
  242. /// Point2F texCoord;
  243. /// }
  244. ///
  245. /// @endcode
  246. ///
  247. /// @see GFXImplementVertexFormat
  248. ///
  249. #define GFXDeclareVertexFormat( name ) \
  250. GFX_VERTEX_STRUCT name; \
  251. extern const GFXVertexFormat _gfxVertexFormat##name; \
  252. template<> inline const GFXVertexFormat* getGFXVertexFormat<name>() { static _GFXVertexFormatConstructor<name> vertexFormat; return &vertexFormat; } \
  253. GFX_VERTEX_STRUCT name \
  254. /// The vertex format implementation which is usally placed in your source
  255. /// file. It should be used in conjunction with the declaration macro.
  256. ///
  257. /// @param name The name of the vertex structure.
  258. ///
  259. /// @code
  260. ///
  261. /// // A simple vertex format implementation.
  262. /// GFXImplementVertexFormat( GFXVertexPCT )
  263. /// {
  264. /// addElement( "POSITION", GFXDeclType_Float3 );
  265. /// addElement( "COLOR", GFXDeclType_Color );
  266. /// addElement( "TEXCOORD", GFXDeclType_Float2, 0 );
  267. /// }
  268. ///
  269. /// @endcode
  270. ///
  271. /// @see GFXDeclareVertexFormat
  272. ///
  273. #define GFXImplementVertexFormat( name ) \
  274. template<> void _GFXVertexFormatConstructor<name>::_construct() \
  275. #endif // _GFXVERTEXFORMAT_H_