MeshBuffer.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <stdint.h>
  22. #include "../Macro.h"
  23. #include "../gfx/VertexFormat.h"
  24. #include "../gfx/VertexBuffer.h"
  25. #include "../gfx/IndexBuffer.h"
  26. #include "base/CCVector.h"
  27. RENDERER_BEGIN
  28. class ModelBatcher;
  29. /**
  30. * @addtogroup scene
  31. * @{
  32. */
  33. /**
  34. * @brief The buffer which stores mesh render datas, including the vertices data and the indices data.
  35. * It can be used as a global buffer shared by multiple render handles and eventually shared by Models
  36. */
  37. class MeshBuffer
  38. {
  39. public:
  40. /**
  41. * @brief It describes a range of buffer in the global buffer, it contains result when you request the buffer.
  42. */
  43. struct OffsetInfo
  44. {
  45. /** bytes count of the requested buffer */
  46. uint32_t vByte = 0;
  47. /** offset in index buffer */
  48. uint32_t index = 0;
  49. /** offset in vertex buffer */
  50. uint32_t vertex = 0;
  51. };
  52. /**
  53. * @brief Constructor
  54. * @param[in] batcher The ModelBatcher which creates the current buffer
  55. * @param[in] fmt The vertex format of vertex data
  56. */
  57. MeshBuffer(ModelBatcher* batcher, VertexFormat* fmt);
  58. /**
  59. * @brief Destructor
  60. */
  61. ~MeshBuffer();
  62. /**
  63. * @brief Requests a range of buffer for the given count of vertices and indices
  64. * @param[in] vertexCount Requested count of vertices
  65. * @param[in] indexCount Requested count of indices
  66. * @param[out] offset The result indicates the allocated buffer range
  67. */
  68. const OffsetInfo& request(uint32_t vertexCount, uint32_t indexCount);
  69. const OffsetInfo& requestStatic(uint32_t vertexCount, uint32_t indexCount);
  70. /**
  71. * @brief Upload data to GPU memory
  72. */
  73. void uploadData();
  74. /**
  75. * @brief Reset all states.
  76. */
  77. void reset();
  78. /**
  79. * @brief Gets the current byte offset which indicates the start of empty range
  80. * @return Byte offset.
  81. */
  82. uint32_t getByteOffset() const { return _byteOffset; };
  83. /**
  84. * @brief Gets the current vertex start offset since last time updateOffset is invoked
  85. * @return Vertex start.
  86. */
  87. uint32_t getVertexStart() const { return _vertexStart; };
  88. /**
  89. * @brief Gets the current vertex offset, which should equals to total allocated vertex count.
  90. * @return Vertex offset.
  91. */
  92. uint32_t getVertexOffset() const { return _vertexOffset; };
  93. /**
  94. * @brief Gets the current index start offset since last time updateOffset is invoked
  95. * @return Index start.
  96. */
  97. uint32_t getIndexStart() const { return _indexStart; };
  98. /**
  99. * @brief Gets the current index offset, which should equals to total allocated index count.
  100. * @return Index offset.
  101. */
  102. uint32_t getIndexOffset() const { return _indexOffset; };
  103. /**
  104. * @brief Update the current allocated offsets to the start offsets.
  105. */
  106. void updateOffset()
  107. {
  108. _byteStart = _byteOffset;
  109. _vertexStart = _vertexOffset;
  110. _indexStart = _indexOffset;
  111. };
  112. /**
  113. * @brief Gets the vertex buffer.
  114. */
  115. VertexBuffer* getVertexBuffer() const { return _vb; };
  116. /**
  117. * @brief Gets the index buffer.
  118. */
  119. IndexBuffer* getIndexBuffer() const { return _ib; };
  120. /**
  121. * @brief The vertex data storage in memory
  122. */
  123. float* vData = nullptr;
  124. /**
  125. * @brief The index data storage in memory
  126. */
  127. uint16_t* iData = nullptr;
  128. /**
  129. * @brief Vertex format of the vertex data.
  130. */
  131. VertexFormat* _vertexFmt;
  132. static const int INIT_VERTEX_COUNT = 4096;
  133. static const uint8_t VDATA_BYTE = sizeof(float);
  134. static const uint8_t IDATA_BYTE = sizeof(uint16_t);
  135. protected:
  136. void reallocVBuffer();
  137. void reallocIBuffer();
  138. void checkAndSwitchBuffer(uint32_t vertexCount);
  139. void switchBuffer(uint32_t vertexCount);
  140. void updateOffset(uint32_t vertexCount, uint32_t indiceCount, uint32_t byteOffset);
  141. private:
  142. uint32_t _byteStart = 0;
  143. uint32_t _byteOffset = 0;
  144. uint32_t _indexStart = 0;
  145. uint32_t _indexOffset = 0;
  146. uint32_t _vertexStart = 0;
  147. uint32_t _vertexOffset = 0;
  148. uint32_t _bytesPerVertex = 0;
  149. uint32_t _vDataCount = 0;
  150. uint32_t _iDataCount = 0;
  151. uint32_t _oldVDataCount = 0;
  152. uint32_t _oldIDataCount = 0;
  153. bool _dirty = false;
  154. ModelBatcher* _batcher = nullptr;
  155. std::size_t _vbPos = 0;
  156. cocos2d::Vector<VertexBuffer*> _vbArr;
  157. cocos2d::Vector<IndexBuffer*> _ibArr;
  158. VertexBuffer* _vb = nullptr;
  159. IndexBuffer* _ib = nullptr;
  160. OffsetInfo _offsetInfo;
  161. };
  162. // end of scene group
  163. /// @}
  164. RENDERER_END