VertexFormat.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include <string>
  22. #include <vector>
  23. #include <unordered_map>
  24. #include "base/CCRef.h"
  25. #include "../Types.h"
  26. RENDERER_BEGIN
  27. /**
  28. * @addtogroup gfx
  29. * @{
  30. */
  31. /**
  32. * The vertex format defines the attributes and their data layout in the VertexBuffer\n
  33. * JS API: gfx.VertexFormat
  34. @code
  35. let vertexFmt = new gfx.VertexFormat([
  36. { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3 },
  37. { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
  38. { name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_FLOAT32, num: 4, normalize: true },
  39. ]);
  40. @endcode
  41. */
  42. class VertexFormat : public Ref
  43. {
  44. public:
  45. /*
  46. * Informations used to define an attribute in vertex data layout.
  47. * @struct Info
  48. */
  49. struct Info
  50. {
  51. /*
  52. * Constructor
  53. * @param[in] name Attribute name
  54. * @param[in] type Data type of each component
  55. * @param[in] num Number of components per attribute
  56. * @param[in] normalized Whether integer data values should be normalized into a certain range when being casted to a float
  57. */
  58. Info(const std::string& name, AttribType type, uint32_t num, bool normalize = false)
  59. : _name(name)
  60. , _num(num)
  61. , _type(type)
  62. , _normalize(normalize)
  63. {
  64. }
  65. std::string _name;
  66. uint32_t _num;
  67. AttribType _type;
  68. bool _normalize;
  69. };
  70. static Info INFO_END;
  71. /*
  72. * Element describes informations of an attribute
  73. * @struct Info
  74. */
  75. struct Element
  76. {
  77. Element()
  78. : offset(0)
  79. , stride(0)
  80. , stream(-1)
  81. , num(0)
  82. , bytes(0)
  83. , type(AttribType::INVALID)
  84. , normalize(false)
  85. {}
  86. inline bool isValid() const
  87. {
  88. return type != AttribType::INVALID;
  89. }
  90. /*
  91. * Name of the attribute
  92. */
  93. std::string name;
  94. /*
  95. * Byte offset in each vertex data
  96. */
  97. size_t offset;
  98. /*
  99. * Specifies the offset in bytes between the beginning of consecutive vertex attributes
  100. */
  101. uint32_t stride;
  102. int32_t stream;
  103. /*
  104. * Number of components per attribute unit
  105. */
  106. uint32_t num;
  107. /*
  108. * Total bytes per attribute unit
  109. */
  110. uint32_t bytes;
  111. /*
  112. * Data type of each component
  113. */
  114. AttribType type;
  115. /*
  116. * Specifies whether integer data values should be normalized into a certain range when being casted to a float
  117. */
  118. bool normalize;
  119. };
  120. /*
  121. * Default constructor
  122. */
  123. VertexFormat();
  124. /**
  125. * Constructor with specific attribute informations
  126. * @param[in] infos Array of all elements informations
  127. */
  128. VertexFormat(const std::vector<Info>& infos);
  129. /*
  130. * Copy constructor
  131. */
  132. VertexFormat(const VertexFormat& o);
  133. /*
  134. * Move constructor
  135. */
  136. VertexFormat(VertexFormat&& o);
  137. VertexFormat& operator=(const VertexFormat& o);
  138. VertexFormat& operator=(VertexFormat&& o);
  139. /**
  140. * Gets all attribute names
  141. */
  142. const std::vector<std::string>& getAttributeNames() const { return _names; };
  143. /**
  144. * Getes an attribute element by name
  145. */
  146. const Element* getElement(size_t hashName) const;
  147. /**
  148. * Gets total byte size of a vertex
  149. */
  150. uint32_t getBytes() const { return _bytes; };
  151. /*
  152. * Builtin VertexFormat with 2d position, uv, color, color0 attributes
  153. */
  154. static VertexFormat* XY_UV_Two_Color;
  155. /*
  156. * Builtin VertexFormat with 2d position, uv, color attributes
  157. */
  158. static VertexFormat* XY_UV_Color;
  159. /*
  160. * Builtin VertexFormat with 2d position, color attributes
  161. */
  162. static VertexFormat* XY_Color;
  163. private:
  164. std::vector<std::string> _names;
  165. std::unordered_map<size_t, Element> _attr2el;
  166. #if GFX_DEBUG > 0
  167. std::vector<Element> _elements;
  168. #endif
  169. uint32_t _bytes;
  170. friend class VertexBuffer;
  171. };
  172. // end of gfx group
  173. /// @}
  174. RENDERER_END