1
0

CCVertexIndexBuffer.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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. #include "renderer/CCVertexIndexBuffer.h"
  21. #include "base/CCEventType.h"
  22. #include "base/CCEventListenerCustom.h"
  23. #include "base/CCEventDispatcher.h"
  24. #include "base/CCDirector.h"
  25. NS_CC_BEGIN
  26. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  27. bool VertexBuffer::_enableShadowCopy = true;
  28. #else
  29. bool VertexBuffer::_enableShadowCopy = false;
  30. #endif
  31. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  32. bool IndexBuffer::_enableShadowCopy = true;
  33. #else
  34. bool IndexBuffer::_enableShadowCopy = false;
  35. #endif
  36. VertexBuffer* VertexBuffer::create(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
  37. {
  38. auto result = new (std::nothrow) VertexBuffer();
  39. if(result && result->init(sizePerVertex, vertexNumber, usage))
  40. {
  41. result->autorelease();
  42. return result;
  43. }
  44. CC_SAFE_DELETE(result);
  45. return nullptr;
  46. }
  47. VertexBuffer::VertexBuffer()
  48. : _recreateVBOEventListener(nullptr)
  49. , _vbo(0)
  50. , _sizePerVertex(0)
  51. , _vertexNumber(0)
  52. {
  53. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  54. auto callBack = [this](EventCustom* event)
  55. {
  56. this->recreateVBO();
  57. };
  58. _recreateVBOEventListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_RENDERER_RECREATED, callBack);
  59. #endif
  60. }
  61. VertexBuffer::~VertexBuffer()
  62. {
  63. if(glIsBuffer(_vbo))
  64. {
  65. glDeleteBuffers(1, &_vbo);
  66. _vbo = 0;
  67. }
  68. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  69. Director::getInstance()->getEventDispatcher()->removeEventListener(_recreateVBOEventListener);
  70. #endif
  71. }
  72. bool VertexBuffer::init(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
  73. {
  74. if(0 == sizePerVertex || 0 == vertexNumber)
  75. return false;
  76. _sizePerVertex = sizePerVertex;
  77. _vertexNumber = vertexNumber;
  78. _usage = usage;
  79. if(isShadowCopyEnabled())
  80. {
  81. _shadowCopy.resize(sizePerVertex * _vertexNumber);
  82. }
  83. glGenBuffers(1, &_vbo);
  84. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  85. glBufferData(GL_ARRAY_BUFFER, getSize(), nullptr, _usage);
  86. glBindBuffer(GL_ARRAY_BUFFER, 0);
  87. return true;
  88. }
  89. int VertexBuffer::getSizePerVertex() const
  90. {
  91. return _sizePerVertex;
  92. }
  93. int VertexBuffer::getVertexNumber() const
  94. {
  95. return _vertexNumber;
  96. }
  97. bool VertexBuffer::updateVertices(const void* verts, int count, int begin)
  98. {
  99. if(count <= 0 || nullptr == verts) return false;
  100. if(begin < 0)
  101. {
  102. CCLOGERROR("Update vertices with begin = %d, will set begin to 0", begin);
  103. begin = 0;
  104. }
  105. if(count + begin > _vertexNumber)
  106. {
  107. CCLOGERROR("updated vertices exceed the max size of vertex buffer, will set count to _vertexNumber-begin");
  108. count = _vertexNumber - begin;
  109. }
  110. if(isShadowCopyEnabled())
  111. {
  112. memcpy(&_shadowCopy[begin * _sizePerVertex], verts, count * _sizePerVertex);
  113. }
  114. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  115. glBufferSubData(GL_ARRAY_BUFFER, begin * _sizePerVertex, count * _sizePerVertex, verts);
  116. glBindBuffer(GL_ARRAY_BUFFER, 0);
  117. return true;
  118. }
  119. GLuint VertexBuffer::getVBO() const
  120. {
  121. return _vbo;
  122. }
  123. void VertexBuffer::recreateVBO() const
  124. {
  125. CCLOG("come to foreground of VertexBuffer");
  126. glGenBuffers(1, &_vbo);
  127. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  128. const void* buffer = nullptr;
  129. if(isShadowCopyEnabled())
  130. {
  131. buffer = &_shadowCopy[0];
  132. }
  133. CCLOG("recreate IndexBuffer with size %d %d", getSizePerVertex(), _vertexNumber);
  134. glBufferData(GL_ARRAY_BUFFER, _sizePerVertex * _vertexNumber, buffer, _usage);
  135. glBindBuffer(GL_ARRAY_BUFFER, 0);
  136. if(!glIsBuffer(_vbo))
  137. {
  138. CCLOGERROR("recreate VertexBuffer Error");
  139. }
  140. }
  141. int VertexBuffer::getSize() const
  142. {
  143. return _sizePerVertex * _vertexNumber;
  144. }
  145. IndexBuffer* IndexBuffer::create(IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
  146. {
  147. auto result = new (std::nothrow) IndexBuffer();
  148. if(result && result->init(type, number, usage))
  149. {
  150. result->autorelease();
  151. return result;
  152. }
  153. CC_SAFE_DELETE(result);
  154. return nullptr;
  155. }
  156. IndexBuffer::IndexBuffer()
  157. : _vbo(0)
  158. , _type(IndexType::INDEX_TYPE_SHORT_16)
  159. , _indexNumber(0)
  160. , _recreateVBOEventListener(nullptr)
  161. {
  162. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  163. auto callBack = [this](EventCustom* event)
  164. {
  165. this->recreateVBO();
  166. };
  167. _recreateVBOEventListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_RENDERER_RECREATED, callBack);
  168. #endif
  169. }
  170. IndexBuffer::~IndexBuffer()
  171. {
  172. if(glIsBuffer(_vbo))
  173. {
  174. glDeleteBuffers(1, &_vbo);
  175. _vbo = 0;
  176. }
  177. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  178. Director::getInstance()->getEventDispatcher()->removeEventListener(_recreateVBOEventListener);
  179. #endif
  180. }
  181. bool IndexBuffer::init(IndexBuffer::IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
  182. {
  183. if(number <=0 ) return false;
  184. _type = type;
  185. _indexNumber = number;
  186. _usage = usage;
  187. glGenBuffers(1, &_vbo);
  188. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
  189. glBufferData(GL_ELEMENT_ARRAY_BUFFER, getSize(), nullptr, _usage);
  190. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  191. if(isShadowCopyEnabled())
  192. {
  193. _shadowCopy.resize(getSize());
  194. }
  195. return true;
  196. }
  197. IndexBuffer::IndexType IndexBuffer::getType() const
  198. {
  199. return _type;
  200. }
  201. int IndexBuffer::getSizePerIndex() const
  202. {
  203. return IndexType::INDEX_TYPE_SHORT_16 == _type ? 2 : 4;
  204. }
  205. int IndexBuffer::getIndexNumber() const
  206. {
  207. return _indexNumber;
  208. }
  209. bool IndexBuffer::updateIndices(const void* indices, int count, int begin)
  210. {
  211. if(count <= 0 || nullptr == indices) return false;
  212. if(begin < 0)
  213. {
  214. CCLOGERROR("Update indices with begin = %d, will set begin to 0", begin);
  215. begin = 0;
  216. }
  217. if(count + begin > _indexNumber)
  218. {
  219. CCLOGERROR("updated indices exceed the max size of vertex buffer, will set count to _indexNumber-begin");
  220. count = _indexNumber - begin;
  221. }
  222. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
  223. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, begin * getSizePerIndex(), count * getSizePerIndex(), indices);
  224. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  225. if(isShadowCopyEnabled())
  226. {
  227. memcpy(&_shadowCopy[begin * getSizePerIndex()], indices, count * getSizePerIndex());
  228. }
  229. return true;
  230. }
  231. int IndexBuffer::getSize() const
  232. {
  233. return getSizePerIndex() * _indexNumber;
  234. }
  235. GLuint IndexBuffer::getVBO() const
  236. {
  237. return _vbo;
  238. }
  239. void IndexBuffer::recreateVBO() const
  240. {
  241. CCLOG("come to foreground of IndexBuffer");
  242. glGenBuffers(1, &_vbo);
  243. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  244. const void* buffer = nullptr;
  245. if(isShadowCopyEnabled())
  246. {
  247. buffer = &_shadowCopy[0];
  248. }
  249. CCLOG("recreate IndexBuffer with size %d %d ", getSizePerIndex(), _indexNumber);
  250. glBufferData(GL_ARRAY_BUFFER, getSize(), buffer, _usage);
  251. glBindBuffer(GL_ARRAY_BUFFER, 0);
  252. if(!glIsBuffer(_vbo))
  253. {
  254. CCLOGERROR("recreate IndexBuffer Error");
  255. }
  256. }
  257. NS_CC_END