BsVertexDeclaration.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsColor.h"
  4. #include "BsCoreObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Semantics that are used for identifying the meaning of
  9. * vertex buffer elements.
  10. */
  11. enum VertexElementSemantic
  12. {
  13. VES_POSITION = 1,
  14. VES_BLEND_WEIGHTS = 2,
  15. VES_BLEND_INDICES = 3,
  16. VES_NORMAL = 4,
  17. VES_COLOR = 5,
  18. VES_TEXCOORD = 6,
  19. VES_BITANGENT = 7,
  20. VES_TANGENT = 8,
  21. VES_POSITIONT = 9,
  22. VES_PSIZE = 10
  23. };
  24. /**
  25. * @brief Types used to identify base types of vertex element contents
  26. */
  27. enum VertexElementType
  28. {
  29. VET_FLOAT1 = 0,
  30. VET_FLOAT2 = 1,
  31. VET_FLOAT3 = 2,
  32. VET_FLOAT4 = 3,
  33. VET_COLOR = 4,
  34. VET_SHORT1 = 5,
  35. VET_SHORT2 = 6,
  36. VET_SHORT3 = 7,
  37. VET_SHORT4 = 8,
  38. VET_UBYTE4 = 9,
  39. VET_COLOR_ARGB = 10,
  40. VET_COLOR_ABGR = 11,
  41. VET_UINT4 = 12,
  42. VET_SINT4 = 13
  43. };
  44. /**
  45. * @brief Describes a single vertex element in a vertex declaration.
  46. */
  47. class BS_CORE_EXPORT VertexElement
  48. {
  49. public:
  50. VertexElement() {}
  51. VertexElement(UINT16 source, UINT32 offset, VertexElementType theType,
  52. VertexElementSemantic semantic, UINT16 index = 0);
  53. bool operator== (const VertexElement& rhs) const;
  54. bool operator!= (const VertexElement& rhs) const;
  55. /**
  56. * @brief Returns index of the vertex buffer from which this element is stored.
  57. */
  58. UINT16 getStreamIdx() const { return mSource; }
  59. /**
  60. * @brief Returns an offset into the buffer where this vertex is stored. This value
  61. * might be in bytes but doesn't have to be, it's likely to be render API specific.
  62. */
  63. UINT32 getOffset() const { return mOffset; }
  64. /**
  65. * @brief Gets the base data type of tis element.
  66. */
  67. VertexElementType getType() const { return mType; }
  68. /**
  69. * @brief Gets a semantic that describes what this element contains.
  70. */
  71. VertexElementSemantic getSemantic() const { return mSemantic; }
  72. /**
  73. * @brief Gets an index of this element. Only relevant when you have
  74. * multiple elements with the same semantic, e.g. uv0, uv1.
  75. */
  76. UINT16 getSemanticIdx() const { return mIndex; }
  77. /**
  78. * @brief Returns the size of this element in bytes.
  79. */
  80. UINT32 getSize() const;
  81. /**
  82. * @brief Returns the size of a base element type.
  83. */
  84. static UINT32 getTypeSize(VertexElementType etype);
  85. /**
  86. * @brief Returns the number of values in the provided base element type.
  87. * e.g. float4 has four values.
  88. */
  89. static UINT16 getTypeCount(VertexElementType etype);
  90. /**
  91. * @brief Gets packed color vertex element type used by the active render system.
  92. */
  93. static VertexElementType getBestColorVertexElementType();
  94. protected:
  95. UINT16 mSource;
  96. UINT32 mOffset;
  97. VertexElementType mType;
  98. VertexElementSemantic mSemantic;
  99. UINT16 mIndex;
  100. };
  101. BS_ALLOW_MEMCPY_SERIALIZATION(VertexElement);
  102. /**
  103. * @brief Contains information about a vertex declaration.
  104. */
  105. class BS_CORE_EXPORT VertexDeclarationProperties
  106. {
  107. public:
  108. VertexDeclarationProperties(const List<VertexElement>& elements);
  109. bool operator== (const VertexDeclarationProperties& rhs) const;
  110. bool operator!= (const VertexDeclarationProperties& rhs) const;
  111. /**
  112. * @brief Get the number of elements in the declaration.
  113. */
  114. UINT32 getElementCount() const { return (UINT32)mElementList.size(); }
  115. /**
  116. * @brief Returns a list of vertex elements in the declaration.
  117. */
  118. const List<VertexElement>& getElements() const { return mElementList; }
  119. /**
  120. * @brief Returns a single vertex element at the specified index.
  121. */
  122. const VertexElement* getElement(UINT16 index) const;
  123. /**
  124. * @brief Attempts to find an element by the given semantic and semantic index. If no element
  125. * can be found null is returned.
  126. */
  127. virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, UINT16 index = 0) const;
  128. /**
  129. * @brief Returns a list of all elements that use the provided source index.
  130. */
  131. virtual List<VertexElement> findElementsBySource(UINT16 source) const;
  132. /**
  133. * @brief Returns the total size of all vertex elements using the provided source index.
  134. */
  135. virtual UINT32 getVertexSize(UINT16 source) const;
  136. protected:
  137. friend class VertexDeclaration;
  138. friend class VertexDeclarationRTTI;
  139. List<VertexElement> mElementList;
  140. };
  141. /**
  142. * @brief Core thread portion of a VertexDeclaration.
  143. *
  144. * @see VertexDeclaration.
  145. *
  146. * @note Core thread.
  147. */
  148. class BS_CORE_EXPORT VertexDeclarationCore : public CoreObjectCore
  149. {
  150. public:
  151. virtual ~VertexDeclarationCore() { }
  152. /**
  153. * @copydoc CoreObjectCore::initialize
  154. */
  155. void initialize();
  156. /**
  157. * @brief Returns properties describing the vertex declaration.
  158. */
  159. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  160. /**
  161. * @brief Returns an ID unique to this declaration.
  162. */
  163. UINT32 getId() const { return mId; }
  164. protected:
  165. friend class HardwareBufferCoreManager;
  166. VertexDeclarationCore(const List<VertexElement>& elements);
  167. protected:
  168. VertexDeclarationProperties mProperties;
  169. UINT32 mId;
  170. static UINT32 NextFreeId;
  171. };
  172. /**
  173. * @brief Describes a set of vertex elements, used for describing contents of
  174. * a vertex buffer or inputs to a vertex GPU program.
  175. *
  176. * @note Sim thread.
  177. */
  178. class BS_CORE_EXPORT VertexDeclaration : public IReflectable, public CoreObject
  179. {
  180. public:
  181. virtual ~VertexDeclaration() { }
  182. /**
  183. * @brief Returns properties describing the vertex declaration.
  184. */
  185. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  186. /**
  187. * @brief Retrieves a core implementation of a vertex declaration usable only from the
  188. * core thread.
  189. */
  190. SPtr<VertexDeclarationCore> getCore() const;
  191. /**
  192. * @copydoc HardwareBufferManager::createVertexDeclaration
  193. */
  194. static VertexDeclarationPtr createVertexDeclaration(const List<VertexElement>& elements);
  195. protected:
  196. friend class HardwareBufferManager;
  197. VertexDeclaration(const List<VertexElement>& elements);
  198. /**
  199. * @copydoc CoreObject::createCore
  200. */
  201. SPtr<CoreObjectCore> createCore() const;
  202. protected:
  203. VertexDeclarationProperties mProperties;
  204. /************************************************************************/
  205. /* SERIALIZATION */
  206. /************************************************************************/
  207. public:
  208. friend class VertexDeclarationRTTI;
  209. static RTTITypeBase* getRTTIStatic();
  210. virtual RTTITypeBase* getRTTI() const;
  211. };
  212. }