BsVertexDeclaration.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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,
  16. VES_BLEND_WEIGHTS = 2,
  17. VES_BLEND_INDICES = 3,
  18. VES_NORMAL = 4,
  19. VES_COLOR = 5,
  20. VES_TEXCOORD = 6,
  21. VES_BITANGENT = 7,
  22. VES_TANGENT = 8,
  23. VES_POSITIONT = 9,
  24. VES_PSIZE = 10
  25. };
  26. /** Types used to identify base types of vertex element contents. */
  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. /** Describes a single vertex element in a vertex declaration. */
  45. class BS_CORE_EXPORT VertexElement
  46. {
  47. public:
  48. VertexElement() {}
  49. VertexElement(UINT16 source, UINT32 offset, VertexElementType theType,
  50. VertexElementSemantic semantic, UINT16 index = 0);
  51. bool operator== (const VertexElement& rhs) const;
  52. bool operator!= (const VertexElement& rhs) const;
  53. /** Returns index of the vertex buffer from which this element is stored. */
  54. UINT16 getStreamIdx() const { return mSource; }
  55. /**
  56. * Returns an offset into the buffer where this vertex is stored. This value might be in bytes but doesn't have to
  57. * be, it's likely to be render API specific.
  58. */
  59. UINT32 getOffset() const { return mOffset; }
  60. /** Gets the base data type of tis element. */
  61. VertexElementType getType() const { return mType; }
  62. /** Gets a semantic that describes what this element contains. */
  63. VertexElementSemantic getSemantic() const { return mSemantic; }
  64. /**
  65. * Gets an index of this element. Only relevant when you have multiple elements with the same semantic,
  66. * e.g. uv0, uv1.
  67. */
  68. UINT16 getSemanticIdx() const { return mIndex; }
  69. /** Returns the size of this element in bytes. */
  70. UINT32 getSize() const;
  71. /** Returns the size of a base element type. */
  72. static UINT32 getTypeSize(VertexElementType etype);
  73. /** Returns the number of values in the provided base element type. e.g. float4 has four values. */
  74. static UINT16 getTypeCount(VertexElementType etype);
  75. /** Gets packed color vertex element type used by the active render system. */
  76. static VertexElementType getBestColorVertexElementType();
  77. protected:
  78. UINT16 mSource;
  79. UINT32 mOffset;
  80. VertexElementType mType;
  81. VertexElementSemantic mSemantic;
  82. UINT16 mIndex;
  83. };
  84. BS_ALLOW_MEMCPY_SERIALIZATION(VertexElement);
  85. /** Contains information about a vertex declaration. */
  86. class BS_CORE_EXPORT VertexDeclarationProperties
  87. {
  88. public:
  89. VertexDeclarationProperties(const List<VertexElement>& elements);
  90. bool operator== (const VertexDeclarationProperties& rhs) const;
  91. bool operator!= (const VertexDeclarationProperties& rhs) const;
  92. /** Get the number of elements in the declaration. */
  93. UINT32 getElementCount() const { return (UINT32)mElementList.size(); }
  94. /** Returns a list of vertex elements in the declaration. */
  95. const List<VertexElement>& getElements() const { return mElementList; }
  96. /** Returns a single vertex element at the specified index. */
  97. const VertexElement* getElement(UINT16 index) const;
  98. /**
  99. * Attempts to find an element by the given semantic and semantic index. If no element can be found null is returned.
  100. */
  101. virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, UINT16 index = 0) const;
  102. /** Returns a list of all elements that use the provided source index. */
  103. virtual List<VertexElement> findElementsBySource(UINT16 source) const;
  104. /** Returns the total size of all vertex elements using the provided source index. */
  105. virtual UINT32 getVertexSize(UINT16 source) const;
  106. protected:
  107. friend class VertexDeclaration;
  108. friend class VertexDeclarationRTTI;
  109. List<VertexElement> mElementList;
  110. };
  111. /** @cond INTERNAL */
  112. /**
  113. * Core thread portion of a VertexDeclaration.
  114. *
  115. * @note Core thread.
  116. */
  117. class BS_CORE_EXPORT VertexDeclarationCore : public CoreObjectCore
  118. {
  119. public:
  120. virtual ~VertexDeclarationCore() { }
  121. /** @copydoc CoreObjectCore::initialize */
  122. void initialize() override;
  123. /** Returns properties describing the vertex declaration. */
  124. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  125. /** Returns an ID unique to this declaration. */
  126. UINT32 getId() const { return mId; }
  127. /**
  128. * Checks can a vertex buffer declared with this declaration be bound to a shader defined with the provided
  129. * declaration.
  130. */
  131. bool isCompatible(const SPtr<VertexDeclarationCore>& shaderDecl);
  132. /**
  133. * Returns a list of vertex elements that the provided shader's vertex declaration expects but aren't present in
  134. * this vertex declaration.
  135. */
  136. Vector<VertexElement> getMissingElements(const SPtr<VertexDeclarationCore>& shaderDecl);
  137. protected:
  138. friend class HardwareBufferCoreManager;
  139. VertexDeclarationCore(const List<VertexElement>& elements);
  140. VertexDeclarationProperties mProperties;
  141. UINT32 mId;
  142. static UINT32 NextFreeId;
  143. };
  144. /** @endcond */
  145. /**
  146. * Describes a set of vertex elements, used for describing contents of a vertex buffer or inputs to a vertex GPU program.
  147. *
  148. * @note Sim thread.
  149. */
  150. class BS_CORE_EXPORT VertexDeclaration : public IReflectable, public CoreObject
  151. {
  152. public:
  153. virtual ~VertexDeclaration() { }
  154. /** Returns properties describing the vertex declaration. */
  155. const VertexDeclarationProperties& getProperties() const { return mProperties; }
  156. /** Retrieves a core implementation of a vertex declaration usable only from the core thread. */
  157. SPtr<VertexDeclarationCore> getCore() const;
  158. /** @copydoc HardwareBufferManager::createVertexDeclaration */
  159. static VertexDeclarationPtr createVertexDeclaration(const List<VertexElement>& elements);
  160. protected:
  161. friend class HardwareBufferManager;
  162. VertexDeclaration(const List<VertexElement>& elements);
  163. /** @copydoc CoreObject::createCore */
  164. SPtr<CoreObjectCore> createCore() const override;
  165. protected:
  166. VertexDeclarationProperties mProperties;
  167. /************************************************************************/
  168. /* SERIALIZATION */
  169. /************************************************************************/
  170. public:
  171. friend class VertexDeclarationRTTI;
  172. static RTTITypeBase* getRTTIStatic();
  173. virtual RTTITypeBase* getRTTI() const override;
  174. };
  175. /** Converts a vertex semantic enum to a readable name. */
  176. BS_CORE_EXPORT String toString(const VertexElementSemantic& val);
  177. /** Converts a vertex semantic enum to a readable name. */
  178. BS_CORE_EXPORT WString toWString(const VertexElementSemantic& val);
  179. /** @} */
  180. }