CCVertexAttribBinding.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Copyright (c) 2015-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Original file from GamePlay3D: http://gameplay3d.org
  15. This file was modified to fit the cocos2d-x project
  16. */
  17. #include "renderer/CCVertexAttribBinding.h"
  18. #include "renderer/CCGLProgramState.h"
  19. #include "renderer/ccGLStateCache.h"
  20. #include "platform/CCGL.h"
  21. #include "base/CCConfiguration.h"
  22. #include "3d/CCMeshVertexIndexData.h"
  23. NS_CC_BEGIN
  24. std::string s_attributeNames[] = {
  25. GLProgram::ATTRIBUTE_NAME_POSITION,
  26. GLProgram::ATTRIBUTE_NAME_COLOR,
  27. GLProgram::ATTRIBUTE_NAME_TEX_COORD,
  28. GLProgram::ATTRIBUTE_NAME_TEX_COORD1,
  29. GLProgram::ATTRIBUTE_NAME_TEX_COORD2,
  30. GLProgram::ATTRIBUTE_NAME_TEX_COORD3,
  31. GLProgram::ATTRIBUTE_NAME_NORMAL,
  32. GLProgram::ATTRIBUTE_NAME_BLEND_WEIGHT,
  33. GLProgram::ATTRIBUTE_NAME_BLEND_INDEX,
  34. GLProgram::ATTRIBUTE_NAME_TANGENT,
  35. GLProgram::ATTRIBUTE_NAME_BINORMAL
  36. };
  37. static GLuint __maxVertexAttribs = 0;
  38. static std::vector<VertexAttribBinding*> __vertexAttribBindingCache;
  39. VertexAttribBinding::VertexAttribBinding() :
  40. _handle(0), _meshIndexData(nullptr), _glProgramState(nullptr), _attributes()
  41. {
  42. }
  43. VertexAttribBinding::~VertexAttribBinding()
  44. {
  45. // Delete from the vertex attribute binding cache.
  46. std::vector<VertexAttribBinding*>::iterator itr = std::find(__vertexAttribBindingCache.begin(), __vertexAttribBindingCache.end(), this);
  47. if (itr != __vertexAttribBindingCache.end())
  48. {
  49. __vertexAttribBindingCache.erase(itr);
  50. }
  51. CC_SAFE_RELEASE(_meshIndexData);
  52. CC_SAFE_RELEASE(_glProgramState);
  53. _attributes.clear();
  54. if (_handle)
  55. {
  56. glDeleteVertexArrays(1, &_handle);
  57. _handle = 0;
  58. }
  59. }
  60. VertexAttribBinding* VertexAttribBinding::create(MeshIndexData* meshIndexData, GLProgramState* glProgramState)
  61. {
  62. CCASSERT(meshIndexData && glProgramState, "Invalid MeshIndexData and/or GLProgramState");
  63. // Search for an existing vertex attribute binding that can be used.
  64. VertexAttribBinding* b;
  65. for (size_t i = 0, count = __vertexAttribBindingCache.size(); i < count; ++i)
  66. {
  67. b = __vertexAttribBindingCache[i];
  68. CC_ASSERT(b);
  69. if (b->_meshIndexData == meshIndexData && b->_glProgramState == glProgramState)
  70. {
  71. // Found a match!
  72. return b;
  73. }
  74. }
  75. b = new (std::nothrow) VertexAttribBinding();
  76. if (b && b->init(meshIndexData, glProgramState))
  77. {
  78. b->autorelease();
  79. __vertexAttribBindingCache.push_back(b);
  80. }
  81. return b;
  82. }
  83. bool VertexAttribBinding::init(MeshIndexData* meshIndexData, GLProgramState* glProgramState)
  84. {
  85. CCASSERT(meshIndexData && glProgramState, "Invalid arguments");
  86. // One-time initialization.
  87. if (__maxVertexAttribs == 0)
  88. {
  89. GLint temp;
  90. glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &temp);
  91. __maxVertexAttribs = temp;
  92. if (__maxVertexAttribs <= 0)
  93. {
  94. CCLOGERROR("The maximum number of vertex attributes supported by OpenGL on the current device is 0 or less.");
  95. return false;
  96. }
  97. }
  98. _meshIndexData = meshIndexData;
  99. _meshIndexData->retain();
  100. _glProgramState = glProgramState;
  101. _glProgramState->retain();
  102. auto meshVertexData = meshIndexData->getMeshVertexData();
  103. auto attributeCount = meshVertexData->getMeshVertexAttribCount();
  104. // Parse and set attributes
  105. parseAttributes();
  106. long offset = 0;
  107. for (auto k = 0; k < attributeCount; k++)
  108. {
  109. auto meshattribute = meshVertexData->getMeshVertexAttrib(k);
  110. setVertexAttribPointer(
  111. s_attributeNames[meshattribute.vertexAttrib],
  112. meshattribute.size,
  113. meshattribute.type,
  114. GL_FALSE,
  115. meshVertexData->getVertexBuffer()->getSizePerVertex(),
  116. (GLvoid*)offset);
  117. offset += meshattribute.attribSizeBytes;
  118. }
  119. // VAO hardware
  120. if (Configuration::getInstance()->supportsShareableVAO())
  121. {
  122. glGenVertexArrays(1, &_handle);
  123. GL::bindVAO(_handle);
  124. glBindBuffer(GL_ARRAY_BUFFER, meshVertexData->getVertexBuffer()->getVBO());
  125. auto flags = _vertexAttribsFlags;
  126. for (int i = 0; flags > 0; i++) {
  127. int flag = 1 << i;
  128. if (flag & flags)
  129. glEnableVertexAttribArray(i);
  130. flags &= ~flag;
  131. }
  132. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIndexData->getIndexBuffer()->getVBO());
  133. for(auto &attribute : _attributes)
  134. {
  135. attribute.second.apply();
  136. }
  137. GL::bindVAO(0);
  138. glBindBuffer(GL_ARRAY_BUFFER, 0);
  139. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  140. }
  141. return true;
  142. }
  143. void VertexAttribBinding::bind()
  144. {
  145. if (_handle)
  146. {
  147. // hardware
  148. GL::bindVAO(_handle);
  149. }
  150. else
  151. {
  152. // software
  153. auto meshVertexData = _meshIndexData->getMeshVertexData();
  154. glBindBuffer(GL_ARRAY_BUFFER, meshVertexData->getVertexBuffer()->getVBO());
  155. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _meshIndexData->getIndexBuffer()->getVBO());
  156. // Software mode
  157. GL::enableVertexAttribs(_vertexAttribsFlags);
  158. // set attributes
  159. for(auto &attribute : _attributes)
  160. {
  161. attribute.second.apply();
  162. }
  163. }
  164. }
  165. void VertexAttribBinding::unbind()
  166. {
  167. if (_handle)
  168. {
  169. // Hardware
  170. GL::bindVAO(0);
  171. }
  172. else
  173. {
  174. // Software
  175. glBindBuffer(GL_ARRAY_BUFFER, 0);
  176. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  177. }
  178. }
  179. uint32_t VertexAttribBinding::getVertexAttribsFlags() const
  180. {
  181. return _vertexAttribsFlags;
  182. }
  183. void VertexAttribBinding::parseAttributes()
  184. {
  185. CCASSERT(_glProgramState, "invalid glprogram");
  186. _attributes.clear();
  187. _vertexAttribsFlags = 0;
  188. auto glprogram = _glProgramState->getGLProgram();
  189. for(auto &attrib: glprogram->_vertexAttribs)
  190. {
  191. VertexAttribValue value(&attrib.second);
  192. _attributes[attrib.first] = value;
  193. }
  194. }
  195. VertexAttribValue* VertexAttribBinding::getVertexAttribValue(const std::string& name)
  196. {
  197. const auto itr = _attributes.find(name);
  198. if( itr != _attributes.end())
  199. return &itr->second;
  200. return nullptr;
  201. }
  202. void VertexAttribBinding::setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid* pointer)
  203. {
  204. auto v = getVertexAttribValue(name);
  205. if(v) {
  206. v->setPointer(size, type, normalized, stride, pointer);
  207. _vertexAttribsFlags |= 1 << v->_vertexAttrib->index;
  208. }
  209. else
  210. {
  211. CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str());
  212. }
  213. }
  214. NS_CC_END