CCVertexAttribBinding.cpp 7.2 KB

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