CmVertexDeclaration.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmColor.h"
  4. #include "CmCoreObject.h"
  5. namespace CamelotFramework
  6. {
  7. /// Vertex element semantics, used to identify the meaning of vertex buffer contents
  8. enum VertexElementSemantic {
  9. /// Position, 3 reals per vertex
  10. VES_POSITION = 1,
  11. /// Blending weights
  12. VES_BLEND_WEIGHTS = 2,
  13. /// Blending indices
  14. VES_BLEND_INDICES = 3,
  15. /// Normal, 3 reals per vertex
  16. VES_NORMAL = 4,
  17. /// Diffuse colours
  18. VES_COLOR = 5,
  19. /// Specular colours
  20. VES_SPECULAR = 6,
  21. /// Texture coordinates
  22. VES_TEXCOORD = 7,
  23. /// Binormal (Y axis if normal is Z)
  24. VES_BITANGENT = 8,
  25. /// Tangent (X axis if normal is Z)
  26. VES_TANGENT = 9,
  27. /// Transformed vertex position
  28. VES_POSITIONT = 10,
  29. /// Point size
  30. VES_PSIZE = 11
  31. };
  32. /// Vertex element type, used to identify the base types of the vertex contents
  33. enum VertexElementType
  34. {
  35. VET_FLOAT1 = 0,
  36. VET_FLOAT2 = 1,
  37. VET_FLOAT3 = 2,
  38. VET_FLOAT4 = 3,
  39. /// alias to more specific color type - use the current rendersystem's color packing
  40. VET_COLOR = 4,
  41. VET_SHORT1 = 5,
  42. VET_SHORT2 = 6,
  43. VET_SHORT3 = 7,
  44. VET_SHORT4 = 8,
  45. VET_UBYTE4 = 9,
  46. /// D3D style compact color
  47. VET_COLOR_ARGB = 10,
  48. /// GL style compact color
  49. VET_COLOR_ABGR = 11,
  50. VET_UINT4 = 12,
  51. VET_SINT4 = 13
  52. };
  53. /** This class declares the usage of a single vertex buffer as a component
  54. of a complete VertexDeclaration.
  55. @remarks
  56. Several vertex buffers can be used to supply the input geometry for a
  57. rendering operation, and in each case a vertex buffer can be used in
  58. different ways for different operations; the buffer itself does not
  59. define the semantics (position, normal etc), the VertexElement
  60. class does.
  61. */
  62. class CM_EXPORT VertexElement
  63. {
  64. protected:
  65. /// The source vertex buffer
  66. unsigned short mSource;
  67. /// The offset in the buffer that this element starts at
  68. UINT32 mOffset;
  69. /// The type of element
  70. VertexElementType mType;
  71. /// The meaning of the element
  72. VertexElementSemantic mSemantic;
  73. /// Index of the item, only applicable for some elements like texture coords
  74. unsigned short mIndex;
  75. public:
  76. /// Constructor, should not be called directly, only needed because of list
  77. VertexElement() {}
  78. /// Constructor, should not be called directly, call VertexDeclaration::addElement
  79. VertexElement(unsigned short source, UINT32 offset, VertexElementType theType,
  80. VertexElementSemantic semantic, unsigned short index = 0);
  81. /// Gets the vertex buffer index from where this element draws it's values
  82. unsigned short getStreamIdx(void) const { return mSource; }
  83. /// Gets the offset into the buffer where this element starts
  84. UINT32 getOffset(void) const { return mOffset; }
  85. /// Gets the data format of this element
  86. VertexElementType getType(void) const { return mType; }
  87. /// Gets the meaning of this element
  88. VertexElementSemantic getSemantic(void) const { return mSemantic; }
  89. /// Gets the index of this element, only applicable for repeating elements
  90. unsigned short getSemanticIdx(void) const { return mIndex; }
  91. /// Gets the size of this element in bytes
  92. UINT32 getSize(void) const;
  93. /// Utility method for helping to calculate offsets
  94. static UINT32 getTypeSize(VertexElementType etype);
  95. /// Utility method which returns the count of values in a given type
  96. static unsigned short getTypeCount(VertexElementType etype);
  97. /** Simple converter function which will turn a single-value type into a
  98. multi-value type based on a parameter.
  99. */
  100. static VertexElementType multiplyTypeCount(VertexElementType baseType, unsigned short count);
  101. /** Simple converter function which will a type into it's single-value
  102. equivalent - makes switches on type easier.
  103. */
  104. static VertexElementType getBaseType(VertexElementType multiType);
  105. /** Utility method for converting colour from
  106. one packed 32-bit colour type to another.
  107. @param srcType The source type
  108. @param dstType The destination type
  109. @param ptr Read / write value to change
  110. */
  111. static void convertColourValue(VertexElementType srcType,
  112. VertexElementType dstType, UINT32* ptr);
  113. /** Utility method for converting colour to
  114. a packed 32-bit colour type.
  115. @param src source colour
  116. @param dst The destination type
  117. */
  118. static UINT32 convertColourValue(const Color& src,
  119. VertexElementType dst);
  120. /** Utility method to get the most appropriate packed colour vertex element format. */
  121. static VertexElementType getBestColourVertexElementType(void);
  122. inline bool operator== (const VertexElement& rhs) const
  123. {
  124. if (mType != rhs.mType ||
  125. mIndex != rhs.mIndex ||
  126. mOffset != rhs.mOffset ||
  127. mSemantic != rhs.mSemantic ||
  128. mSource != rhs.mSource)
  129. return false;
  130. else
  131. return true;
  132. }
  133. inline bool operator!= (const VertexElement& rhs) const
  134. {
  135. return !(*this == rhs);
  136. }
  137. /** Adjusts a pointer to the base of a vertex to point at this element.
  138. @remarks
  139. This variant is for void pointers, passed as a parameter because we can't
  140. rely on covariant return types.
  141. @param pBase Pointer to the start of a vertex in this buffer.
  142. @param pElem Pointer to a pointer which will be set to the start of this element.
  143. */
  144. inline void baseVertexPointerToElement(void* pBase, void** pElem) const
  145. {
  146. // The only way we can do this is to cast to char* in order to use byte offset
  147. // then cast back to void*.
  148. *pElem = static_cast<void*>(
  149. static_cast<unsigned char*>(pBase) + mOffset);
  150. }
  151. /** Adjusts a pointer to the base of a vertex to point at this element.
  152. @remarks
  153. This variant is for float pointers, passed as a parameter because we can't
  154. rely on covariant return types.
  155. @param pBase Pointer to the start of a vertex in this buffer.
  156. @param pElem Pointer to a pointer which will be set to the start of this element.
  157. */
  158. inline void baseVertexPointerToElement(void* pBase, float** pElem) const
  159. {
  160. // The only way we can do this is to cast to char* in order to use byte offset
  161. // then cast back to float*. However we have to go via void* because casting
  162. // directly is not allowed
  163. *pElem = static_cast<float*>(
  164. static_cast<void*>(
  165. static_cast<unsigned char*>(pBase) + mOffset));
  166. }
  167. /** Adjusts a pointer to the base of a vertex to point at this element.
  168. @remarks
  169. This variant is for RGBA pointers, passed as a parameter because we can't
  170. rely on covariant return types.
  171. @param pBase Pointer to the start of a vertex in this buffer.
  172. @param pElem Pointer to a pointer which will be set to the start of this element.
  173. */
  174. inline void baseVertexPointerToElement(void* pBase, RGBA** pElem) const
  175. {
  176. *pElem = static_cast<RGBA*>(
  177. static_cast<void*>(
  178. static_cast<unsigned char*>(pBase) + mOffset));
  179. }
  180. /** Adjusts a pointer to the base of a vertex to point at this element.
  181. @remarks
  182. This variant is for char pointers, passed as a parameter because we can't
  183. rely on covariant return types.
  184. @param pBase Pointer to the start of a vertex in this buffer.
  185. @param pElem Pointer to a pointer which will be set to the start of this element.
  186. */
  187. inline void baseVertexPointerToElement(void* pBase, unsigned char** pElem) const
  188. {
  189. *pElem = static_cast<unsigned char*>(pBase) + mOffset;
  190. }
  191. /** Adjusts a pointer to the base of a vertex to point at this element.
  192. @remarks
  193. This variant is for UINT16 pointers, passed as a parameter because we can't
  194. rely on covariant return types.
  195. @param pBase Pointer to the start of a vertex in this buffer.
  196. @param pElem Pointer to a pointer which will be set to the start of this element.
  197. */
  198. inline void baseVertexPointerToElement(void* pBase, unsigned short** pElem) const
  199. {
  200. *pElem = static_cast<unsigned short*>(
  201. static_cast<void*>(
  202. static_cast<unsigned char*>(pBase) + mOffset));
  203. }
  204. /**
  205. * @brief Calculates a hash value for this element.
  206. */
  207. size_t calculateHash() const;
  208. };
  209. CM_ALLOW_MEMCPY_SERIALIZATION(VertexElement);
  210. /** This class declares the format of a set of vertex inputs, which
  211. can be issued to the rendering API through a RenderOperation.
  212. @remarks
  213. You should be aware that the ordering and structure of the
  214. VertexDeclaration can be very important on DirectX with older
  215. cards,so if you want to maintain maximum compatibility with
  216. all render systems and all cards you should be careful to follow these
  217. rules:<ol>
  218. <li>VertexElements should be added in the following order, and the order of the
  219. elements within a shared buffer should be as follows:
  220. position, blending weights, normals, diffuse colours, specular colours,
  221. texture coordinates (in order, with no gaps)</li>
  222. <li>You must not have unused gaps in your buffers which are not referenced
  223. by any VertexElement</li>
  224. <li>You must not cause the buffer & offset settings of 2 VertexElements to overlap</li>
  225. </ol>
  226. Whilst GL and more modern graphics cards in D3D will allow you to defy these rules,
  227. sticking to them will ensure that your buffers have the maximum compatibility.
  228. @par
  229. Like the other classes in this functional area, these declarations should be created and
  230. destroyed using the HardwareBufferManager.
  231. */
  232. class CM_EXPORT VertexDeclaration : public IReflectable, public CoreObject
  233. {
  234. public:
  235. /// Defines the list of vertex elements that makes up this declaration
  236. typedef List<VertexElement>::type VertexElementList;
  237. /// Sort routine for vertex elements
  238. static bool vertexElementLess(const VertexElement& e1, const VertexElement& e2);
  239. virtual ~VertexDeclaration();
  240. /** Get the number of elements in the declaration. */
  241. UINT32 getElementCount(void) { return (UINT32)mElementList.size(); }
  242. /** Gets read-only access to the list of vertex elements. */
  243. const VertexElementList& getElements(void) const;
  244. /** Get a single element. */
  245. const VertexElement* getElement(unsigned short index);
  246. /**
  247. * @brief Returns a hash value generated by all of the elements in the declaration.
  248. */
  249. size_t getHash() { return mHash; }
  250. /** Sorts the elements in this list to be compatible with the maximum
  251. number of rendering APIs / graphics cards.
  252. @remarks
  253. Older graphics cards require vertex data to be presented in a more
  254. rigid way, as defined in the main documentation for this class. As well
  255. as the ordering being important, where shared source buffers are used, the
  256. declaration must list all the elements for each source in turn.
  257. */
  258. void sort(void);
  259. /** Remove any gaps in the source buffer list used by this declaration.
  260. @remarks
  261. This is useful if you've modified a declaration and want to remove
  262. any gaps in the list of buffers being used. Note, however, that if this
  263. declaration is already being used with a VertexBufferBinding, you will
  264. need to alter that too. This method is mainly useful when reorganising
  265. buffers based on an altered declaration.
  266. @note
  267. This will cause the vertex declaration to be re-sorted.
  268. */
  269. void closeGapsInSource(void);
  270. /** Gets the index of the highest source value referenced by this declaration. */
  271. unsigned short getMaxSource(void) const;
  272. /** Adds a new VertexElement to this declaration.
  273. @remarks
  274. This method adds a single element (positions, normals etc) to the end of the
  275. vertex declaration. <b>Please read the information in VertexDeclaration about
  276. the importance of ordering and structure for compatibility with older D3D drivers</b>.
  277. @param source The binding index of HardwareVertexBuffer which will provide the source for this element.
  278. See VertexBufferBindingState for full information.
  279. @param offset The offset in bytes where this element is located in the buffer
  280. @param theType The data format of the element (3 floats, a colour etc)
  281. @param semantic The meaning of the data (position, normal, diffuse colour etc)
  282. @param index Optional index for multi-input elements like texture coordinates
  283. @returns A reference to the VertexElement added.
  284. */
  285. virtual const VertexElement& addElement(unsigned short source, UINT32 offset, VertexElementType theType,
  286. VertexElementSemantic semantic, unsigned short index = 0);
  287. /** Inserts a new VertexElement at a given position in this declaration.
  288. @remarks
  289. This method adds a single element (positions, normals etc) at a given position in this
  290. vertex declaration. <b>Please read the information in VertexDeclaration about
  291. the importance of ordering and structure for compatibility with older D3D drivers</b>.
  292. @param source The binding index of HardwareVertexBuffer which will provide the source for this element.
  293. See VertexBufferBindingState for full information.
  294. @param offset The offset in bytes where this element is located in the buffer
  295. @param theType The data format of the element (3 floats, a colour etc)
  296. @param semantic The meaning of the data (position, normal, diffuse colour etc)
  297. @param index Optional index for multi-input elements like texture coordinates
  298. @returns A reference to the VertexElement added.
  299. */
  300. virtual const VertexElement& insertElement(unsigned short atPosition,
  301. unsigned short source, UINT32 offset, VertexElementType theType,
  302. VertexElementSemantic semantic, unsigned short index = 0);
  303. /** Remove the element at the given index from this declaration. */
  304. virtual void removeElement(unsigned short elem_index);
  305. /** Remove the element with the given semantic and usage index.
  306. @remarks
  307. In this case 'index' means the usage index for repeating elements such
  308. as texture coordinates. For other elements this will always be 0 and does
  309. not refer to the index in the vector.
  310. */
  311. virtual void removeElement(VertexElementSemantic semantic, unsigned short index = 0);
  312. /** Remove all elements. */
  313. virtual void removeAllElements(void);
  314. /** Modify an element in-place, params as addElement.
  315. @remarks
  316. <b>Please read the information in VertexDeclaration about
  317. the importance of ordering and structure for compatibility with older D3D drivers</b>.
  318. */
  319. virtual void modifyElement(unsigned short elem_index, unsigned short source, UINT32 offset, VertexElementType theType,
  320. VertexElementSemantic semantic, unsigned short index = 0);
  321. /** Finds a VertexElement with the given semantic, and index if there is more than
  322. one element with the same semantic.
  323. @remarks
  324. If the element is not found, this method returns null.
  325. */
  326. virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, unsigned short index = 0);
  327. /** Based on the current elements, gets the size of the vertex for a given buffer source.
  328. @param source The buffer binding index for which to get the vertex size.
  329. */
  330. /** Gets a list of elements which use a given source.
  331. @remarks
  332. Note that the list of elements is returned by value therefore is separate from
  333. the declaration as soon as this method returns.
  334. */
  335. virtual VertexElementList findElementsBySource(unsigned short source);
  336. /** Gets the vertex size defined by this declaration for a given source. */
  337. virtual UINT32 getVertexSize(unsigned short source);
  338. /** Clones this declaration.
  339. @param mgr Optional HardwareBufferManager to use for creating the clone
  340. (if null, use the current default).
  341. */
  342. virtual VertexDeclarationPtr clone(HardwareBufferManager* mgr = 0);
  343. inline bool operator== (const VertexDeclaration& rhs) const
  344. {
  345. if (mElementList.size() != rhs.mElementList.size())
  346. return false;
  347. VertexElementList::const_iterator i, iend, rhsi, rhsiend;
  348. iend = mElementList.end();
  349. rhsiend = rhs.mElementList.end();
  350. rhsi = rhs.mElementList.begin();
  351. for (i = mElementList.begin(); i != iend && rhsi != rhsiend; ++i, ++rhsi)
  352. {
  353. if ( !(*i == *rhsi) )
  354. return false;
  355. }
  356. return true;
  357. }
  358. inline bool operator!= (const VertexDeclaration& rhs) const
  359. {
  360. return !(*this == rhs);
  361. }
  362. protected:
  363. friend class HardwareBufferManager;
  364. VertexDeclaration();
  365. /**
  366. * @brief Generates a hash value based on all elements in the declaration.
  367. */
  368. void recalculateHash();
  369. protected:
  370. VertexElementList mElementList;
  371. size_t mHash;
  372. /************************************************************************/
  373. /* SERIALIZATION */
  374. /************************************************************************/
  375. public:
  376. friend class VertexDeclarationRTTI;
  377. static RTTITypeBase* getRTTIStatic();
  378. virtual RTTITypeBase* getRTTI() const;
  379. };
  380. }