BsVertexDeclaration.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsColor.h"
  6. #include "BsCoreObject.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup RenderAPI
  10. * @{
  11. */
  12. /** Semantics that are used for identifying the meaning of vertex buffer elements. */
  13. enum VertexElementSemantic
  14. {
  15. VES_POSITION = 1, /**< Position */
  16. VES_BLEND_WEIGHTS = 2, /**< Blend weights */
  17. VES_BLEND_INDICES = 3, /**< Blend indices */
  18. VES_NORMAL = 4, /**< Normal */
  19. VES_COLOR = 5, /**< Color */
  20. VES_TEXCOORD = 6, /**< UV coordinate */
  21. VES_BITANGENT = 7, /**< Bitangent */
  22. VES_TANGENT = 8, /**< Tangent */
  23. VES_POSITIONT = 9, /**< Transformed position */
  24. VES_PSIZE = 10 /**< Point size */
  25. };
  26. /** Types used to identify base types of vertex element contents. */
  27. enum VertexElementType
  28. {
  29. VET_FLOAT1 = 0, /**< 1D floating point value */
  30. VET_FLOAT2 = 1, /**< 2D floating point value */
  31. VET_FLOAT3 = 2, /**< 3D floating point value */
  32. VET_FLOAT4 = 3, /**< 4D floating point value */
  33. VET_COLOR = 4, /**< Color encoded in 32-bits (8-bits per channel). */
  34. VET_SHORT1 = 5, /**< 1D 16-bit signed integer value */
  35. VET_SHORT2 = 6, /**< 2D 16-bit signed integer value */
  36. VET_SHORT4 = 8, /**< 4D 16-bit signed integer value */
  37. VET_UBYTE4 = 9, /**< 4D 8-bit unsigned integer value */
  38. VET_COLOR_ARGB = 10, /**< Color encoded in 32-bits (8-bits per channel) in ARGB order) */
  39. VET_COLOR_ABGR = 11, /**< Color encoded in 32-bits (8-bits per channel) in ABGR order) */
  40. VET_UINT4 = 12, /**< 4D 32-bit unsigned integer value */
  41. VET_INT4 = 13, /**< 4D 32-bit signed integer value */
  42. VET_USHORT1 = 14, /**< 1D 16-bit unsigned integer value */
  43. VET_USHORT2 = 15, /**< 2D 16-bit unsigned integer value */
  44. VET_USHORT4 = 17, /**< 4D 16-bit unsigned integer value */
  45. VET_INT1 = 18, /**< 1D 32-bit signed integer value */
  46. VET_INT2 = 19, /**< 2D 32-bit signed integer value */
  47. VET_INT3 = 20, /**< 3D 32-bit signed integer value */
  48. VET_UINT1 = 21, /**< 1D 32-bit signed integer value */
  49. VET_UINT2 = 22, /**< 2D 32-bit signed integer value */
  50. VET_UINT3 = 23, /**< 3D 32-bit signed integer value */
  51. };
  52. /** Describes a single vertex element in a vertex declaration. */
  53. class BS_CORE_EXPORT VertexElement
  54. {
  55. public:
  56. VertexElement() {}
  57. VertexElement(UINT16 source, UINT32 offset, VertexElementType theType,
  58. VertexElementSemantic semantic, UINT16 index = 0, UINT32 instanceStepRate = 0);
  59. bool operator== (const VertexElement& rhs) const;
  60. bool operator!= (const VertexElement& rhs) const;
  61. /** Returns index of the vertex buffer from which this element is stored. */
  62. UINT16 getStreamIdx() const { return mSource; }
  63. /**
  64. * Returns an offset into the buffer where this vertex is stored. This value might be in bytes but doesn't have to
  65. * be, it's likely to be render API specific.
  66. */
  67. UINT32 getOffset() const { return mOffset; }
  68. /** Gets the base data type of this element. */
  69. VertexElementType getType() const { return mType; }
  70. /** Gets a semantic that describes what this element contains. */
  71. VertexElementSemantic getSemantic() const { return mSemantic; }
  72. /**
  73. * Gets an index of this element. Only relevant when you have multiple elements with the same semantic,
  74. * for example uv0, uv1.
  75. */
  76. UINT16 getSemanticIdx() const { return mIndex; }
  77. /** Returns the size of this element in bytes. */
  78. UINT32 getSize() const;
  79. /**
  80. * Returns at what rate do the vertex elements advance during instanced rendering. Provide zero for default
  81. * behaviour where each vertex receives the next value from the vertex buffer. Provide a value larger than zero
  82. * to ensure vertex data is advanced with every instance, instead of every vertex (for example a value of 1 means
  83. * each instance will retrieve a new value from the vertex buffer, a value of 2 means each second instance will,
  84. * etc.).
  85. */
  86. UINT32 getInstanceStepRate() const { return mInstanceStepRate; }
  87. /** Returns the size of a base element type. */
  88. static UINT32 getTypeSize(VertexElementType etype);
  89. /** Returns the number of values in the provided base element type. For example float4 has four values. */
  90. static UINT16 getTypeCount(VertexElementType etype);
  91. /** Gets packed color vertex element type used by the active render system. */
  92. static VertexElementType getBestColorVertexElementType();
  93. protected:
  94. UINT16 mSource;
  95. UINT32 mOffset;
  96. VertexElementType mType;
  97. VertexElementSemantic mSemantic;
  98. UINT16 mIndex;
  99. UINT32 mInstanceStepRate;
  100. };
  101. /** @cond SPECIALIZATIONS */
  102. BS_ALLOW_MEMCPY_SERIALIZATION(VertexElement);
  103. /** @endcond */
  104. /** Contains information about a vertex declaration. */
  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. /** Get the number of elements in the declaration. */
  112. UINT32 getElementCount() const { return (UINT32)mElementList.size(); }
  113. /** Returns a list of vertex elements in the declaration. */
  114. const List<VertexElement>& getElements() const { return mElementList; }
  115. /** Returns a single vertex element at the specified index. */
  116. const VertexElement* getElement(UINT16 index) const;
  117. /**
  118. * Attempts to find an element by the given semantic and semantic index. If no element can be found null is returned.
  119. */
  120. virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, UINT16 index = 0) const;
  121. /** Returns a list of all elements that use the provided source index. */
  122. virtual List<VertexElement> findElementsBySource(UINT16 source) const;
  123. /** Returns the total size of all vertex elements using the provided source index. */
  124. virtual UINT32 getVertexSize(UINT16 source) const;
  125. protected:
  126. friend class VertexDeclaration;
  127. friend class VertexDeclarationRTTI;
  128. List<VertexElement> mElementList;
  129. };
  130. /**
  131. * Describes a set of vertex elements, used for describing contents of a vertex buffer or inputs to a vertex GPU program.
  132. *
  133. * @note Sim thread.
  134. */
  135. class BS_CORE_EXPORT VertexDeclaration : public IReflectable, public CoreObject
  136. {
  137. public:
  138. virtual ~VertexDeclaration() { }
  139. /** Returns properties describing the vertex declaration. */
  140. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  141. /** Retrieves a core implementation of a vertex declaration usable only from the core thread. */
  142. SPtr<VertexDeclarationCore> getCore() const;
  143. /** @copydoc HardwareBufferManager::createVertexDeclaration */
  144. static SPtr<VertexDeclaration> create(const SPtr<VertexDataDesc>& desc);
  145. protected:
  146. friend class HardwareBufferManager;
  147. VertexDeclaration(const List<VertexElement>& elements);
  148. /** @copydoc CoreObject::createCore */
  149. SPtr<CoreObjectCore> createCore() const override;
  150. protected:
  151. VertexDeclarationProperties mProperties;
  152. /************************************************************************/
  153. /* SERIALIZATION */
  154. /************************************************************************/
  155. public:
  156. friend class VertexDeclarationRTTI;
  157. static RTTITypeBase* getRTTIStatic();
  158. virtual RTTITypeBase* getRTTI() const override;
  159. };
  160. /** Converts a vertex semantic enum to a readable name. */
  161. BS_CORE_EXPORT String toString(const VertexElementSemantic& val);
  162. /** Converts a vertex semantic enum to a readable name. */
  163. BS_CORE_EXPORT WString toWString(const VertexElementSemantic& val);
  164. /** @} */
  165. /** @addtogroup RenderAPI-Internal
  166. * @{
  167. */
  168. /**
  169. * Core thread portion of a VertexDeclaration.
  170. *
  171. * @note Core thread.
  172. */
  173. class BS_CORE_EXPORT VertexDeclarationCore : public CoreObjectCore
  174. {
  175. public:
  176. virtual ~VertexDeclarationCore() { }
  177. /** @copydoc CoreObjectCore::initialize */
  178. void initialize() override;
  179. /** Returns properties describing the vertex declaration. */
  180. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  181. /** Returns an ID unique to this declaration. */
  182. UINT32 getId() const { return mId; }
  183. /**
  184. * Checks can a vertex buffer declared with this declaration be bound to a shader defined with the provided
  185. * declaration.
  186. */
  187. bool isCompatible(const SPtr<VertexDeclarationCore>& shaderDecl);
  188. /**
  189. * Returns a list of vertex elements that the provided shader's vertex declaration expects but aren't present in
  190. * this vertex declaration.
  191. */
  192. Vector<VertexElement> getMissingElements(const SPtr<VertexDeclarationCore>& shaderDecl);
  193. /** @copydoc HardwareBufferCoreManager::createVertexDeclaration */
  194. static SPtr<VertexDeclarationCore> create(const SPtr<VertexDataDesc>& desc);
  195. protected:
  196. friend class HardwareBufferCoreManager;
  197. VertexDeclarationCore(const List<VertexElement>& elements);
  198. VertexDeclarationProperties mProperties;
  199. UINT32 mId;
  200. static UINT32 NextFreeId;
  201. };
  202. /** @} */
  203. }