BsVertexDeclaration.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "Image/BsColor.h"
  6. #include "CoreThread/BsCoreObject.h"
  7. namespace bs
  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. VET_UBYTE4_NORM = 24, /**< 4D 8-bit unsigned integer interpreted as a normalized value in [0, 1] range. */
  52. VET_COUNT, // Keep at end before VET_UNKNOWN
  53. VET_UNKNOWN = 0xffff
  54. };
  55. /** Describes a single vertex element in a vertex declaration. */
  56. class BS_CORE_EXPORT VertexElement
  57. {
  58. public:
  59. VertexElement() {}
  60. VertexElement(UINT16 source, UINT32 offset, VertexElementType theType,
  61. VertexElementSemantic semantic, UINT16 index = 0, UINT32 instanceStepRate = 0);
  62. bool operator== (const VertexElement& rhs) const;
  63. bool operator!= (const VertexElement& rhs) const;
  64. /** Returns index of the vertex buffer from which this element is stored. */
  65. UINT16 getStreamIdx() const { return mSource; }
  66. /**
  67. * Returns an offset into the buffer where this vertex is stored. This value might be in bytes but doesn't have to
  68. * be, it's likely to be render API specific.
  69. */
  70. UINT32 getOffset() const { return mOffset; }
  71. /** Gets the base data type of this element. */
  72. VertexElementType getType() const { return mType; }
  73. /** Gets a semantic that describes what this element contains. */
  74. VertexElementSemantic getSemantic() const { return mSemantic; }
  75. /**
  76. * Gets an index of this element. Only relevant when you have multiple elements with the same semantic,
  77. * for example uv0, uv1.
  78. */
  79. UINT16 getSemanticIdx() const { return mIndex; }
  80. /** Returns the size of this element in bytes. */
  81. UINT32 getSize() const;
  82. /**
  83. * Returns at what rate do the vertex elements advance during instanced rendering. Provide zero for default
  84. * behaviour where each vertex receives the next value from the vertex buffer. Provide a value larger than zero
  85. * to ensure vertex data is advanced with every instance, instead of every vertex (for example a value of 1 means
  86. * each instance will retrieve a new value from the vertex buffer, a value of 2 means each second instance will,
  87. * etc.).
  88. */
  89. UINT32 getInstanceStepRate() const { return mInstanceStepRate; }
  90. /** Returns the size of a base element type. */
  91. static UINT32 getTypeSize(VertexElementType etype);
  92. /** Returns the number of values in the provided base element type. For example float4 has four values. */
  93. static UINT16 getTypeCount(VertexElementType etype);
  94. /** Gets packed color vertex element type used by the active render system. */
  95. static VertexElementType getBestColorVertexElementType();
  96. /** Calculates a hash value for the provided vertex element. */
  97. static size_t getHash(const VertexElement& element);
  98. protected:
  99. UINT16 mSource;
  100. UINT32 mOffset;
  101. VertexElementType mType;
  102. VertexElementSemantic mSemantic;
  103. UINT16 mIndex;
  104. UINT32 mInstanceStepRate;
  105. };
  106. /** @cond SPECIALIZATIONS */
  107. BS_ALLOW_MEMCPY_SERIALIZATION(VertexElement);
  108. /** @endcond */
  109. /** Contains information about a vertex declaration. */
  110. class BS_CORE_EXPORT VertexDeclarationProperties
  111. {
  112. public:
  113. VertexDeclarationProperties(const List<VertexElement>& elements);
  114. bool operator== (const VertexDeclarationProperties& rhs) const;
  115. bool operator!= (const VertexDeclarationProperties& rhs) const;
  116. /** Get the number of elements in the declaration. */
  117. UINT32 getElementCount() const { return (UINT32)mElementList.size(); }
  118. /** Returns a list of vertex elements in the declaration. */
  119. const List<VertexElement>& getElements() const { return mElementList; }
  120. /** Returns a single vertex element at the specified index. */
  121. const VertexElement* getElement(UINT16 index) const;
  122. /**
  123. * Attempts to find an element by the given semantic and semantic index. If no element can be found null is returned.
  124. */
  125. const VertexElement* findElementBySemantic(VertexElementSemantic sem, UINT16 index = 0) const;
  126. /** Returns a list of all elements that use the provided source index. */
  127. List<VertexElement> findElementsBySource(UINT16 source) const;
  128. /** Returns the total size of all vertex elements using the provided source index. */
  129. UINT32 getVertexSize(UINT16 source) const;
  130. protected:
  131. friend class VertexDeclaration;
  132. friend class VertexDeclarationRTTI;
  133. List<VertexElement> mElementList;
  134. };
  135. /**
  136. * Describes a set of vertex elements, used for describing contents of a vertex buffer or inputs to a vertex GPU program.
  137. *
  138. * @note Sim thread.
  139. */
  140. class BS_CORE_EXPORT VertexDeclaration : public IReflectable, public CoreObject
  141. {
  142. public:
  143. virtual ~VertexDeclaration() { }
  144. /** Returns properties describing the vertex declaration. */
  145. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  146. /** Retrieves a core implementation of a vertex declaration usable only from the core thread. */
  147. SPtr<ct::VertexDeclaration> getCore() const;
  148. /** @copydoc HardwareBufferManager::createVertexDeclaration */
  149. static SPtr<VertexDeclaration> create(const SPtr<VertexDataDesc>& desc);
  150. protected:
  151. friend class HardwareBufferManager;
  152. VertexDeclaration(const List<VertexElement>& elements);
  153. /** @copydoc CoreObject::createCore */
  154. SPtr<ct::CoreObject> createCore() const override;
  155. protected:
  156. VertexDeclarationProperties mProperties;
  157. /************************************************************************/
  158. /* SERIALIZATION */
  159. /************************************************************************/
  160. public:
  161. friend class VertexDeclarationRTTI;
  162. static RTTITypeBase* getRTTIStatic();
  163. RTTITypeBase* getRTTI() const override;
  164. };
  165. /** Converts a vertex semantic enum to a readable name. */
  166. BS_CORE_EXPORT String toString(const VertexElementSemantic& val);
  167. /** Converts a vertex semantic enum to a readable name. */
  168. BS_CORE_EXPORT WString toWString(const VertexElementSemantic& val);
  169. /** @} */
  170. namespace ct
  171. {
  172. /** @addtogroup RenderAPI-Internal
  173. * @{
  174. */
  175. /**
  176. * Core thread portion of a bs::VertexDeclaration.
  177. *
  178. * @note Core thread.
  179. */
  180. class BS_CORE_EXPORT VertexDeclaration : public CoreObject
  181. {
  182. public:
  183. virtual ~VertexDeclaration() { }
  184. /** @copydoc CoreObject::initialize */
  185. void initialize() override;
  186. /** Returns properties describing the vertex declaration. */
  187. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  188. /** Returns an ID unique to this declaration. */
  189. UINT32 getId() const { return mId; }
  190. /**
  191. * Checks can a vertex buffer declared with this declaration be bound to a shader defined with the provided
  192. * declaration.
  193. */
  194. bool isCompatible(const SPtr<VertexDeclaration>& shaderDecl);
  195. /**
  196. * Returns a list of vertex elements that the provided shader's vertex declaration expects but aren't present in
  197. * this vertex declaration.
  198. */
  199. Vector<VertexElement> getMissingElements(const SPtr<VertexDeclaration>& shaderDecl);
  200. /** @copydoc HardwareBufferManager::createVertexDeclaration */
  201. static SPtr<VertexDeclaration> create(const SPtr<VertexDataDesc>& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  202. protected:
  203. friend class HardwareBufferManager;
  204. VertexDeclaration(const List<VertexElement>& elements, GpuDeviceFlags deviceMask);
  205. VertexDeclarationProperties mProperties;
  206. UINT32 mId;
  207. static UINT32 NextFreeId;
  208. };
  209. /** @} */
  210. }
  211. }