CCVertexIndexBuffer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "renderer/CCVertexIndexBuffer.h"
  22. #include "base/CCEventType.h"
  23. #include "base/CCEventListenerCustom.h"
  24. #include "base/CCEventDispatcher.h"
  25. #include "base/CCDirector.h"
  26. NS_CC_BEGIN
  27. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  28. bool VertexBuffer::_enableShadowCopy = true;
  29. #else
  30. bool VertexBuffer::_enableShadowCopy = false;
  31. #endif
  32. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  33. bool IndexBuffer::_enableShadowCopy = true;
  34. #else
  35. bool IndexBuffer::_enableShadowCopy = false;
  36. #endif
  37. VertexBuffer* VertexBuffer::create(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
  38. {
  39. auto result = new (std::nothrow) VertexBuffer();
  40. if(result && result->init(sizePerVertex, vertexNumber, usage))
  41. {
  42. result->autorelease();
  43. return result;
  44. }
  45. CC_SAFE_DELETE(result);
  46. return nullptr;
  47. }
  48. VertexBuffer::VertexBuffer()
  49. : _recreateVBOEventListener(nullptr)
  50. , _vbo(0)
  51. , _sizePerVertex(0)
  52. , _vertexNumber(0)
  53. {
  54. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  55. auto callBack = [this](EventCustom* event)
  56. {
  57. this->recreateVBO();
  58. };
  59. _recreateVBOEventListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_RENDERER_RECREATED, callBack);
  60. #endif
  61. }
  62. VertexBuffer::~VertexBuffer()
  63. {
  64. if(glIsBuffer(_vbo))
  65. {
  66. glDeleteBuffers(1, &_vbo);
  67. _vbo = 0;
  68. }
  69. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  70. Director::getInstance()->getEventDispatcher()->removeEventListener(_recreateVBOEventListener);
  71. #endif
  72. }
  73. bool VertexBuffer::init(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
  74. {
  75. if(0 == sizePerVertex || 0 == vertexNumber)
  76. return false;
  77. _sizePerVertex = sizePerVertex;
  78. _vertexNumber = vertexNumber;
  79. _usage = usage;
  80. if(isShadowCopyEnabled())
  81. {
  82. _shadowCopy.resize(sizePerVertex * _vertexNumber);
  83. }
  84. glGenBuffers(1, &_vbo);
  85. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  86. glBufferData(GL_ARRAY_BUFFER, getSize(), nullptr, _usage);
  87. glBindBuffer(GL_ARRAY_BUFFER, 0);
  88. return true;
  89. }
  90. int VertexBuffer::getSizePerVertex() const
  91. {
  92. return _sizePerVertex;
  93. }
  94. int VertexBuffer::getVertexNumber() const
  95. {
  96. return _vertexNumber;
  97. }
  98. bool VertexBuffer::updateVertices(const void* verts, int count, int begin)
  99. {
  100. if(count <= 0 || nullptr == verts) return false;
  101. if(begin < 0)
  102. {
  103. CCLOGERROR("Update vertices with begin = %d, will set begin to 0", begin);
  104. begin = 0;
  105. }
  106. if(count + begin > _vertexNumber)
  107. {
  108. CCLOGERROR("updated vertices exceed the max size of vertex buffer, will set count to _vertexNumber-begin");
  109. count = _vertexNumber - begin;
  110. }
  111. if(isShadowCopyEnabled())
  112. {
  113. memcpy(&_shadowCopy[begin * _sizePerVertex], verts, count * _sizePerVertex);
  114. }
  115. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  116. glBufferSubData(GL_ARRAY_BUFFER, begin * _sizePerVertex, count * _sizePerVertex, verts);
  117. glBindBuffer(GL_ARRAY_BUFFER, 0);
  118. return true;
  119. }
  120. GLuint VertexBuffer::getVBO() const
  121. {
  122. return _vbo;
  123. }
  124. void VertexBuffer::recreateVBO() const
  125. {
  126. CCLOG("come to foreground of VertexBuffer");
  127. glGenBuffers(1, &_vbo);
  128. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  129. const void* buffer = nullptr;
  130. if(isShadowCopyEnabled())
  131. {
  132. buffer = &_shadowCopy[0];
  133. }
  134. CCLOG("recreate IndexBuffer with size %d %d", getSizePerVertex(), _vertexNumber);
  135. glBufferData(GL_ARRAY_BUFFER, _sizePerVertex * _vertexNumber, buffer, _usage);
  136. glBindBuffer(GL_ARRAY_BUFFER, 0);
  137. if(!glIsBuffer(_vbo))
  138. {
  139. CCLOGERROR("recreate VertexBuffer Error");
  140. }
  141. }
  142. int VertexBuffer::getSize() const
  143. {
  144. return _sizePerVertex * _vertexNumber;
  145. }
  146. IndexBuffer* IndexBuffer::create(IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
  147. {
  148. auto result = new (std::nothrow) IndexBuffer();
  149. if(result && result->init(type, number, usage))
  150. {
  151. result->autorelease();
  152. return result;
  153. }
  154. CC_SAFE_DELETE(result);
  155. return nullptr;
  156. }
  157. IndexBuffer::IndexBuffer()
  158. : _vbo(0)
  159. , _type(IndexType::INDEX_TYPE_SHORT_16)
  160. , _indexNumber(0)
  161. , _recreateVBOEventListener(nullptr)
  162. {
  163. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  164. auto callBack = [this](EventCustom* event)
  165. {
  166. this->recreateVBO();
  167. };
  168. _recreateVBOEventListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_RENDERER_RECREATED, callBack);
  169. #endif
  170. }
  171. IndexBuffer::~IndexBuffer()
  172. {
  173. if(glIsBuffer(_vbo))
  174. {
  175. glDeleteBuffers(1, &_vbo);
  176. _vbo = 0;
  177. }
  178. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  179. Director::getInstance()->getEventDispatcher()->removeEventListener(_recreateVBOEventListener);
  180. #endif
  181. }
  182. bool IndexBuffer::init(IndexBuffer::IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
  183. {
  184. if(number <=0 ) return false;
  185. _type = type;
  186. _indexNumber = number;
  187. _usage = usage;
  188. glGenBuffers(1, &_vbo);
  189. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
  190. glBufferData(GL_ELEMENT_ARRAY_BUFFER, getSize(), nullptr, _usage);
  191. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  192. if(isShadowCopyEnabled())
  193. {
  194. _shadowCopy.resize(getSize());
  195. }
  196. return true;
  197. }
  198. IndexBuffer::IndexType IndexBuffer::getType() const
  199. {
  200. return _type;
  201. }
  202. int IndexBuffer::getSizePerIndex() const
  203. {
  204. return IndexType::INDEX_TYPE_SHORT_16 == _type ? 2 : 4;
  205. }
  206. int IndexBuffer::getIndexNumber() const
  207. {
  208. return _indexNumber;
  209. }
  210. bool IndexBuffer::updateIndices(const void* indices, int count, int begin)
  211. {
  212. if(count <= 0 || nullptr == indices) return false;
  213. if(begin < 0)
  214. {
  215. CCLOGERROR("Update indices with begin = %d, will set begin to 0", begin);
  216. begin = 0;
  217. }
  218. if(count + begin > _indexNumber)
  219. {
  220. CCLOGERROR("updated indices exceed the max size of vertex buffer, will set count to _indexNumber-begin");
  221. count = _indexNumber - begin;
  222. }
  223. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
  224. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, begin * getSizePerIndex(), count * getSizePerIndex(), indices);
  225. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  226. if(isShadowCopyEnabled())
  227. {
  228. memcpy(&_shadowCopy[begin * getSizePerIndex()], indices, count * getSizePerIndex());
  229. }
  230. return true;
  231. }
  232. int IndexBuffer::getSize() const
  233. {
  234. return getSizePerIndex() * _indexNumber;
  235. }
  236. GLuint IndexBuffer::getVBO() const
  237. {
  238. return _vbo;
  239. }
  240. void IndexBuffer::recreateVBO() const
  241. {
  242. CCLOG("come to foreground of IndexBuffer");
  243. glGenBuffers(1, &_vbo);
  244. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  245. const void* buffer = nullptr;
  246. if(isShadowCopyEnabled())
  247. {
  248. buffer = &_shadowCopy[0];
  249. }
  250. CCLOG("recreate IndexBuffer with size %d %d ", getSizePerIndex(), _indexNumber);
  251. glBufferData(GL_ARRAY_BUFFER, getSize(), buffer, _usage);
  252. glBindBuffer(GL_ARRAY_BUFFER, 0);
  253. if(!glIsBuffer(_vbo))
  254. {
  255. CCLOGERROR("recreate IndexBuffer Error");
  256. }
  257. }
  258. NS_CC_END