1
0

CCGLProgramState.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /****************************************************************************
  2. Copyright 2011 Jeff Lamarche
  3. Copyright 2012 Goffredo Marocchi
  4. Copyright 2012 Ricardo Quesada
  5. Copyright 2012 cocos2d-x.org
  6. Copyright (c) 2013-2017 Chukong Technologies Inc.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "renderer/CCGLProgramState.h"
  25. #include "renderer/CCGLProgram.h"
  26. #include "renderer/CCGLProgramStateCache.h"
  27. #include "renderer/CCGLProgramCache.h"
  28. #include "renderer/ccGLStateCache.h"
  29. #include "renderer/CCTexture2D.h"
  30. #include "base/CCEventCustom.h"
  31. #include "base/CCEventListenerCustom.h"
  32. #include "base/CCEventType.h"
  33. #include "base/CCDirector.h"
  34. #include "base/CCEventDispatcher.h"
  35. #include "2d/CCCamera.h"
  36. NS_CC_BEGIN
  37. // static vector with all the registered custom binding resolvers
  38. std::vector<GLProgramState::AutoBindingResolver*> GLProgramState::_customAutoBindingResolvers;
  39. //
  40. //
  41. // UniformValue
  42. //
  43. //
  44. UniformValue::UniformValue()
  45. : _uniform(nullptr)
  46. , _glprogram(nullptr)
  47. , _type(Type::VALUE)
  48. {
  49. }
  50. UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram)
  51. : _uniform(uniform)
  52. , _glprogram(glprogram)
  53. , _type(Type::VALUE)
  54. {
  55. }
  56. UniformValue::UniformValue(const UniformValue& o)
  57. {
  58. *this = o;
  59. }
  60. UniformValue::~UniformValue()
  61. {
  62. if (_type == Type::CALLBACK_FN)
  63. delete _value.callback;
  64. if (_uniform->type == GL_SAMPLER_2D)
  65. {
  66. CC_SAFE_RELEASE(_value.tex.texture);
  67. }
  68. }
  69. void UniformValue::apply()
  70. {
  71. if (_type == Type::CALLBACK_FN)
  72. {
  73. (*_value.callback)(_glprogram, _uniform);
  74. }
  75. else if (_type == Type::POINTER)
  76. {
  77. switch (_uniform->type) {
  78. case GL_FLOAT:
  79. _glprogram->setUniformLocationWith1fv(_uniform->location, _value.floatv.pointer, _value.floatv.size);
  80. break;
  81. case GL_FLOAT_VEC2:
  82. _glprogram->setUniformLocationWith2fv(_uniform->location, _value.v2f.pointer, _value.v2f.size);
  83. break;
  84. case GL_FLOAT_VEC3:
  85. _glprogram->setUniformLocationWith3fv(_uniform->location, _value.v3f.pointer, _value.v3f.size);
  86. break;
  87. case GL_FLOAT_VEC4:
  88. _glprogram->setUniformLocationWith4fv(_uniform->location, _value.v4f.pointer, _value.v4f.size);
  89. break;
  90. default:
  91. CCASSERT(false, "Unsupported type");
  92. break;
  93. }
  94. }
  95. else /* _type == VALUE */
  96. {
  97. switch (_uniform->type) {
  98. case GL_SAMPLER_2D:
  99. _glprogram->setUniformLocationWith1i(_uniform->location, _value.tex.textureUnit);
  100. GL::bindTexture2DN(_value.tex.textureUnit, _value.tex.textureId);
  101. break;
  102. case GL_SAMPLER_CUBE:
  103. _glprogram->setUniformLocationWith1i(_uniform->location, _value.tex.textureUnit);
  104. GL::bindTextureN(_value.tex.textureUnit, _value.tex.textureId, GL_TEXTURE_CUBE_MAP);
  105. break;
  106. case GL_INT:
  107. _glprogram->setUniformLocationWith1i(_uniform->location, _value.intValue);
  108. break;
  109. case GL_FLOAT:
  110. _glprogram->setUniformLocationWith1f(_uniform->location, _value.floatValue);
  111. break;
  112. case GL_FLOAT_VEC2:
  113. _glprogram->setUniformLocationWith2f(_uniform->location, _value.v2Value[0], _value.v2Value[1]);
  114. break;
  115. case GL_FLOAT_VEC3:
  116. _glprogram->setUniformLocationWith3f(_uniform->location, _value.v3Value[0], _value.v3Value[1], _value.v3Value[2]);
  117. break;
  118. case GL_FLOAT_VEC4:
  119. _glprogram->setUniformLocationWith4f(_uniform->location, _value.v4Value[0], _value.v4Value[1], _value.v4Value[2], _value.v4Value[3]);
  120. break;
  121. case GL_FLOAT_MAT4:
  122. _glprogram->setUniformLocationWithMatrix4fv(_uniform->location, (GLfloat*)&_value.matrixValue, 1);
  123. break;
  124. default:
  125. CCASSERT(false, "Invalid UniformValue");
  126. break;
  127. }
  128. }
  129. }
  130. void UniformValue::setCallback(const std::function<void(GLProgram*, Uniform*)> &callback)
  131. {
  132. // delete previously set callback
  133. // TODO: memory will leak if the user does:
  134. // value->setCallback();
  135. // value->setFloat();
  136. if (_type == Type::CALLBACK_FN)
  137. delete _value.callback;
  138. _value.callback = new (std::nothrow) std::function<void(GLProgram*, Uniform*)>();
  139. *_value.callback = callback;
  140. _type = Type::CALLBACK_FN;
  141. }
  142. void UniformValue::setTexture(GLuint textureId, GLuint textureUnit)
  143. {
  144. //CCASSERT(_uniform->type == GL_SAMPLER_2D, "Wrong type. expecting GL_SAMPLER_2D");
  145. _value.tex.textureId = textureId;
  146. _value.tex.textureUnit = textureUnit;
  147. _value.tex.texture = nullptr;
  148. _type = Type::VALUE;
  149. }
  150. void UniformValue::setTexture(Texture2D* texture, GLuint textureUnit)
  151. {
  152. CCASSERT(texture != nullptr, "texture is nullptr");
  153. if (texture != _value.tex.texture)
  154. {
  155. CC_SAFE_RELEASE(_value.tex.texture);
  156. CC_SAFE_RETAIN(texture);
  157. _value.tex.texture = texture;
  158. _value.tex.textureId = texture->getName();
  159. _value.tex.textureUnit = textureUnit;
  160. _type = Type::VALUE;
  161. }
  162. }
  163. void UniformValue::setInt(int value)
  164. {
  165. CCASSERT(_uniform->type == GL_INT, "Wrong type: expecting GL_INT");
  166. _value.intValue = value;
  167. _type = Type::VALUE;
  168. }
  169. void UniformValue::setFloat(float value)
  170. {
  171. CCASSERT(_uniform->type == GL_FLOAT, "Wrong type: expecting GL_FLOAT");
  172. _value.floatValue = value;
  173. _type = Type::VALUE;
  174. }
  175. void UniformValue::setFloatv(ssize_t size, const float* pointer)
  176. {
  177. CCASSERT(_uniform->type == GL_FLOAT, "Wrong type: expecting GL_FLOAT");
  178. _value.floatv.pointer = (const float*)pointer;
  179. _value.floatv.size = (GLsizei)size;
  180. _type = Type::POINTER;
  181. }
  182. void UniformValue::setVec2(const Vec2& value)
  183. {
  184. CCASSERT(_uniform->type == GL_FLOAT_VEC2, "Wrong type: expecting GL_FLOAT_VEC2");
  185. memcpy(_value.v2Value, &value, sizeof(_value.v2Value));
  186. _type = Type::VALUE;
  187. }
  188. void UniformValue::setVec2v(ssize_t size, const Vec2* pointer)
  189. {
  190. CCASSERT(_uniform->type == GL_FLOAT_VEC2, "Wrong type: expecting GL_FLOAT_VEC2");
  191. _value.v2f.pointer = (const float*)pointer;
  192. _value.v2f.size = (GLsizei)size;
  193. _type = Type::POINTER;
  194. }
  195. void UniformValue::setVec3(const Vec3& value)
  196. {
  197. CCASSERT(_uniform->type == GL_FLOAT_VEC3, "Wrong type: expecting GL_FLOAT_VEC3");
  198. memcpy(_value.v3Value, &value, sizeof(_value.v3Value));
  199. _type = Type::VALUE;
  200. }
  201. void UniformValue::setVec3v(ssize_t size, const Vec3* pointer)
  202. {
  203. CCASSERT(_uniform->type == GL_FLOAT_VEC3, "Wrong type: expecting GL_FLOAT_VEC3");
  204. _value.v3f.pointer = (const float*)pointer;
  205. _value.v3f.size = (GLsizei)size;
  206. _type = Type::POINTER;
  207. }
  208. void UniformValue::setVec4(const Vec4& value)
  209. {
  210. CCASSERT (_uniform->type == GL_FLOAT_VEC4, "Wrong type: expecting GL_FLOAT_VEC4");
  211. memcpy(_value.v4Value, &value, sizeof(_value.v4Value));
  212. _type = Type::VALUE;
  213. }
  214. void UniformValue::setVec4v(ssize_t size, const Vec4* pointer)
  215. {
  216. CCASSERT (_uniform->type == GL_FLOAT_VEC4, "Wrong type: expecting GL_FLOAT_VEC4");
  217. _value.v4f.pointer = (const float*)pointer;
  218. _value.v4f.size = (GLsizei)size;
  219. _type = Type::POINTER;
  220. }
  221. void UniformValue::setMat4(const Mat4& value)
  222. {
  223. CCASSERT(_uniform->type == GL_FLOAT_MAT4, "_uniform's type should be equal GL_FLOAT_MAT4.");
  224. memcpy(_value.matrixValue, &value, sizeof(_value.matrixValue));
  225. _type = Type::VALUE;
  226. }
  227. UniformValue& UniformValue::operator=(const UniformValue& o)
  228. {
  229. _uniform = o._uniform;
  230. _glprogram = o._glprogram;
  231. _type = o._type;
  232. _value = o._value;
  233. if (_uniform->type == GL_SAMPLER_2D)
  234. {
  235. CC_SAFE_RETAIN(_value.tex.texture);
  236. }
  237. return *this;
  238. }
  239. //
  240. //
  241. // VertexAttribValue
  242. //
  243. //
  244. VertexAttribValue::VertexAttribValue()
  245. : _vertexAttrib(nullptr)
  246. , _useCallback(false)
  247. , _enabled(false)
  248. {
  249. }
  250. VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
  251. : _vertexAttrib(vertexAttrib)
  252. , _useCallback(false)
  253. , _enabled(false)
  254. {
  255. }
  256. VertexAttribValue::~VertexAttribValue()
  257. {
  258. if (_useCallback)
  259. delete _value.callback;
  260. }
  261. void VertexAttribValue::apply()
  262. {
  263. if(_enabled) {
  264. if(_useCallback) {
  265. (*_value.callback)(_vertexAttrib);
  266. }
  267. else
  268. {
  269. glVertexAttribPointer(_vertexAttrib->index,
  270. _value.pointer.size,
  271. _value.pointer.type,
  272. _value.pointer.normalized,
  273. _value.pointer.stride,
  274. _value.pointer.pointer);
  275. }
  276. }
  277. }
  278. void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &callback)
  279. {
  280. _value.callback = new (std::nothrow) std::function<void(VertexAttrib*)>();
  281. *_value.callback = callback;
  282. _useCallback = true;
  283. _enabled = true;
  284. }
  285. void VertexAttribValue::setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
  286. {
  287. _value.pointer.size = size;
  288. _value.pointer.type = type;
  289. _value.pointer.normalized = normalized;
  290. _value.pointer.stride = stride;
  291. _value.pointer.pointer = pointer;
  292. _enabled = true;
  293. }
  294. //
  295. //
  296. // GLProgramState
  297. //
  298. //
  299. GLProgramState* GLProgramState::getOrCreateWithGLProgramName(const std::string& glProgramName, Texture2D* texture)
  300. {
  301. if (texture != nullptr && texture->getAlphaTextureName() != 0) {
  302. if (glProgramName == GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR) {
  303. return GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR);
  304. }
  305. else if (glProgramName == GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP) {
  306. return GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP);
  307. }
  308. else if (glProgramName == GLProgram::SHADER_NAME_POSITION_GRAYSCALE) {
  309. return GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP);
  310. }
  311. }
  312. return GLProgramState::getOrCreateWithGLProgramName(glProgramName);
  313. }
  314. GLProgramState* GLProgramState::create(GLProgram *glprogram)
  315. {
  316. GLProgramState* ret = new (std::nothrow) GLProgramState();
  317. if(ret && ret->init(glprogram))
  318. {
  319. ret->autorelease();
  320. return ret;
  321. }
  322. CC_SAFE_DELETE(ret);
  323. return nullptr;
  324. }
  325. GLProgramState* GLProgramState::getOrCreateWithGLProgramName(const std::string& glProgramName )
  326. {
  327. GLProgram *glProgram = GLProgramCache::getInstance()->getGLProgram(glProgramName);
  328. if( glProgram )
  329. return getOrCreateWithGLProgram(glProgram);
  330. CCLOG("cocos2d: warning: GLProgram '%s' not found", glProgramName.c_str());
  331. return nullptr;
  332. }
  333. GLProgramState* GLProgramState::getOrCreateWithGLProgram(GLProgram *glprogram)
  334. {
  335. GLProgramState* ret = GLProgramStateCache::getInstance()->getGLProgramState(glprogram);
  336. return ret;
  337. }
  338. GLProgramState* GLProgramState::getOrCreateWithShaders(const std::string& vertexShader, const std::string& fragShader, const std::string& compileTimeDefines)
  339. {
  340. auto glprogramcache = GLProgramCache::getInstance();
  341. const std::string key = vertexShader + "+" + fragShader + "+" + compileTimeDefines;
  342. auto glprogram = glprogramcache->getGLProgram(key);
  343. if (!glprogram) {
  344. glprogram = GLProgram::createWithFilenames(vertexShader, fragShader, compileTimeDefines);
  345. glprogramcache->addGLProgram(glprogram, key);
  346. }
  347. return create(glprogram);
  348. }
  349. GLProgramState::GLProgramState()
  350. : _uniformAttributeValueDirty(true)
  351. , _textureUnitIndex(4) // first 4 textures unites are reserved for CC_Texture0-3
  352. , _vertexAttribsFlags(0)
  353. , _glprogram(nullptr)
  354. , _nodeBinding(nullptr)
  355. {
  356. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  357. /** listen the event that renderer was recreated on Android/WP8 */
  358. CCLOG("create rendererRecreatedListener for GLProgramState");
  359. _backToForegroundlistener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
  360. [this](EventCustom*)
  361. {
  362. CCLOG("Dirty Uniform and Attributes of GLProgramState");
  363. _uniformAttributeValueDirty = true;
  364. });
  365. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundlistener, -1);
  366. #endif
  367. }
  368. GLProgramState::~GLProgramState()
  369. {
  370. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  371. Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundlistener);
  372. #endif
  373. // _uniforms must be cleared before releasing _glprogram since
  374. // the destructor of UniformValue will call a weak pointer
  375. // which points to the member variable in GLProgram.
  376. _uniforms.clear();
  377. _attributes.clear();
  378. CC_SAFE_RELEASE(_glprogram);
  379. }
  380. GLProgramState* GLProgramState::clone() const
  381. {
  382. auto glprogramstate = new (std::nothrow) GLProgramState();
  383. // copy everything manually, instead of calling init since this is faster
  384. glprogramstate->_glprogram = this->_glprogram;
  385. CC_SAFE_RETAIN(glprogramstate->_glprogram);
  386. glprogramstate->_attributes = this->_attributes;
  387. glprogramstate->_vertexAttribsFlags = this->_vertexAttribsFlags;
  388. // copy uniforms
  389. glprogramstate->_uniformsByName = this->_uniformsByName;
  390. glprogramstate->_uniforms = this->_uniforms;
  391. glprogramstate->_uniformAttributeValueDirty = this->_uniformAttributeValueDirty;
  392. // copy textures
  393. glprogramstate->_textureUnitIndex = this->_textureUnitIndex;
  394. glprogramstate->_boundTextureUnits = this->_boundTextureUnits;
  395. // _nodeBinding is null since a node can only have one state.
  396. // making the null explicit to avoid possible bugs in the future
  397. glprogramstate->_nodeBinding = nullptr;
  398. // copy autobindings... rebound them once a target is set again
  399. glprogramstate->_autoBindings = _autoBindings;
  400. glprogramstate->autorelease();
  401. return glprogramstate;
  402. }
  403. bool GLProgramState::init(GLProgram* glprogram)
  404. {
  405. CCASSERT(glprogram, "invalid shader");
  406. _glprogram = glprogram;
  407. _glprogram->retain();
  408. for(auto &attrib : _glprogram->_vertexAttribs) {
  409. VertexAttribValue value(&attrib.second);
  410. _attributes[attrib.first] = value;
  411. }
  412. for(auto &uniform : _glprogram->_userUniforms) {
  413. UniformValue value(&uniform.second, _glprogram);
  414. _uniforms[uniform.second.location] = std::move(value);
  415. _uniformsByName[uniform.first] = uniform.second.location;
  416. }
  417. return true;
  418. }
  419. void GLProgramState::resetGLProgram()
  420. {
  421. // _uniforms must be cleared before releasing _glprogram since
  422. // the destructor of UniformValue will call a weak pointer
  423. // which points to the member variable in GLProgram.
  424. _uniforms.clear();
  425. _attributes.clear();
  426. CC_SAFE_RELEASE(_glprogram);
  427. _glprogram = nullptr;
  428. // first texture is GL_TEXTURE1
  429. _textureUnitIndex = 1;
  430. _nodeBinding = nullptr;
  431. }
  432. void GLProgramState::apply(const Mat4& modelView)
  433. {
  434. applyGLProgram(modelView);
  435. applyAttributes();
  436. applyUniforms();
  437. }
  438. void GLProgramState::updateUniformsAndAttributes()
  439. {
  440. CCASSERT(_glprogram, "invalid glprogram");
  441. if(_uniformAttributeValueDirty)
  442. {
  443. for(auto& uniformLocation : _uniformsByName)
  444. {
  445. _uniforms[uniformLocation.second]._uniform = _glprogram->getUniform(uniformLocation.first);
  446. }
  447. _vertexAttribsFlags = 0;
  448. for(auto& attributeValue : _attributes)
  449. {
  450. attributeValue.second._vertexAttrib = _glprogram->getVertexAttrib(attributeValue.first);
  451. if(attributeValue.second._enabled)
  452. _vertexAttribsFlags |= 1 << attributeValue.second._vertexAttrib->index;
  453. }
  454. _uniformAttributeValueDirty = false;
  455. }
  456. }
  457. void GLProgramState::applyGLProgram(const Mat4& modelView)
  458. {
  459. CCASSERT(_glprogram, "invalid glprogram");
  460. updateUniformsAndAttributes();
  461. // set shader
  462. _glprogram->use();
  463. _glprogram->setUniformsForBuiltins(modelView);
  464. }
  465. void GLProgramState::applyAttributes(bool applyAttribFlags)
  466. {
  467. // Don't set attributes if they weren't set
  468. // Use Case: Auto-batching
  469. updateUniformsAndAttributes();
  470. if(_vertexAttribsFlags) {
  471. // enable/disable vertex attribs
  472. if (applyAttribFlags)
  473. GL::enableVertexAttribs(_vertexAttribsFlags);
  474. // set attributes
  475. for(auto &attribute : _attributes)
  476. {
  477. attribute.second.apply();
  478. }
  479. }
  480. }
  481. void GLProgramState::applyUniforms()
  482. {
  483. // set uniforms
  484. updateUniformsAndAttributes();
  485. for(auto& uniform : _uniforms) {
  486. uniform.second.apply();
  487. }
  488. }
  489. void GLProgramState::setGLProgram(GLProgram *glprogram)
  490. {
  491. CCASSERT(glprogram, "invalid GLProgram");
  492. if( _glprogram != glprogram) {
  493. resetGLProgram();
  494. init(glprogram);
  495. }
  496. }
  497. uint32_t GLProgramState::getVertexAttribsFlags() const
  498. {
  499. return _vertexAttribsFlags;
  500. }
  501. ssize_t GLProgramState::getVertexAttribCount() const
  502. {
  503. return _attributes.size();
  504. }
  505. UniformValue* GLProgramState::getUniformValue(GLint uniformLocation)
  506. {
  507. updateUniformsAndAttributes();
  508. const auto itr = _uniforms.find(uniformLocation);
  509. if (itr != _uniforms.end())
  510. return &itr->second;
  511. return nullptr;
  512. }
  513. UniformValue* GLProgramState::getUniformValue(const std::string& name)
  514. {
  515. updateUniformsAndAttributes();
  516. const auto itr = _uniformsByName.find(name);
  517. if (itr != _uniformsByName.end())
  518. return &_uniforms[itr->second];
  519. return nullptr;
  520. }
  521. VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string& name)
  522. {
  523. updateUniformsAndAttributes();
  524. const auto itr = _attributes.find(name);
  525. if( itr != _attributes.end())
  526. return &itr->second;
  527. return nullptr;
  528. }
  529. // VertexAttrib Setters
  530. void GLProgramState::setVertexAttribCallback(const std::string& name, const std::function<void(VertexAttrib*)> &callback)
  531. {
  532. VertexAttribValue *v = getVertexAttribValue(name);
  533. if(v) {
  534. v->setCallback(callback);
  535. _vertexAttribsFlags |= 1 << v->_vertexAttrib->index;
  536. }
  537. else
  538. {
  539. CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str());
  540. }
  541. }
  542. void GLProgramState::setVertexAttribPointer(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
  543. {
  544. auto v = getVertexAttribValue(name);
  545. if(v) {
  546. v->setPointer(size, type, normalized, stride, pointer);
  547. _vertexAttribsFlags |= 1 << v->_vertexAttrib->index;
  548. }
  549. else
  550. {
  551. CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str());
  552. }
  553. }
  554. // Uniform Setters
  555. void GLProgramState::setUniformCallback(const std::string& uniformName, const std::function<void(GLProgram*, Uniform*)> &callback)
  556. {
  557. auto v = getUniformValue(uniformName);
  558. if (v)
  559. v->setCallback(callback);
  560. else
  561. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  562. }
  563. void GLProgramState::setUniformCallback(GLint uniformLocation, const std::function<void(GLProgram*, Uniform*)> &callback)
  564. {
  565. auto v = getUniformValue(uniformLocation);
  566. if (v)
  567. v->setCallback(callback);
  568. else
  569. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  570. }
  571. void GLProgramState::setUniformFloat(const std::string& uniformName, float value)
  572. {
  573. auto v = getUniformValue(uniformName);
  574. if (v)
  575. v->setFloat(value);
  576. else
  577. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  578. }
  579. void GLProgramState::setUniformFloat(GLint uniformLocation, float value)
  580. {
  581. auto v = getUniformValue(uniformLocation);
  582. if (v)
  583. v->setFloat(value);
  584. else
  585. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  586. }
  587. void GLProgramState::setUniformInt(const std::string& uniformName, int value)
  588. {
  589. auto v = getUniformValue(uniformName);
  590. if(v)
  591. v->setInt(value);
  592. else
  593. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  594. }
  595. void GLProgramState::setUniformInt(GLint uniformLocation, int value)
  596. {
  597. auto v = getUniformValue(uniformLocation);
  598. if (v)
  599. v->setInt(value);
  600. else
  601. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  602. }
  603. void GLProgramState::setUniformFloatv(const std::string& uniformName, ssize_t size, const float* pointer)
  604. {
  605. auto v = getUniformValue(uniformName);
  606. if (v)
  607. v->setFloatv(size, pointer);
  608. else
  609. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  610. }
  611. void GLProgramState::setUniformFloatv(GLint uniformLocation, ssize_t size, const float* pointer)
  612. {
  613. auto v = getUniformValue(uniformLocation);
  614. if (v)
  615. v->setFloatv(size, pointer);
  616. else
  617. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  618. }
  619. void GLProgramState::setUniformVec2(const std::string& uniformName, const Vec2& value)
  620. {
  621. auto v = getUniformValue(uniformName);
  622. if (v)
  623. v->setVec2(value);
  624. else
  625. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  626. }
  627. void GLProgramState::setUniformVec2(GLint uniformLocation, const Vec2& value)
  628. {
  629. auto v = getUniformValue(uniformLocation);
  630. if (v)
  631. v->setVec2(value);
  632. else
  633. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  634. }
  635. void GLProgramState::setUniformVec2v(const std::string& uniformName, ssize_t size, const Vec2* pointer)
  636. {
  637. auto v = getUniformValue(uniformName);
  638. if (v)
  639. v->setVec2v(size, pointer);
  640. else
  641. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  642. }
  643. void GLProgramState::setUniformVec2v(GLint uniformLocation, ssize_t size, const Vec2* pointer)
  644. {
  645. auto v = getUniformValue(uniformLocation);
  646. if (v)
  647. v->setVec2v(size, pointer);
  648. else
  649. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  650. }
  651. void GLProgramState::setUniformVec3(const std::string& uniformName, const Vec3& value)
  652. {
  653. auto v = getUniformValue(uniformName);
  654. if (v)
  655. v->setVec3(value);
  656. else
  657. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  658. }
  659. void GLProgramState::setUniformVec3(GLint uniformLocation, const Vec3& value)
  660. {
  661. auto v = getUniformValue(uniformLocation);
  662. if (v)
  663. v->setVec3(value);
  664. else
  665. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  666. }
  667. void GLProgramState::setUniformVec3v(const std::string& uniformName, ssize_t size, const Vec3* pointer)
  668. {
  669. auto v = getUniformValue(uniformName);
  670. if (v)
  671. v->setVec3v(size, pointer);
  672. else
  673. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  674. }
  675. void GLProgramState::setUniformVec3v(GLint uniformLocation, ssize_t size, const Vec3* pointer)
  676. {
  677. auto v = getUniformValue(uniformLocation);
  678. if (v)
  679. v->setVec3v(size, pointer);
  680. else
  681. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  682. }
  683. void GLProgramState::setUniformVec4(const std::string& uniformName, const Vec4& value)
  684. {
  685. auto v = getUniformValue(uniformName);
  686. if (v)
  687. v->setVec4(value);
  688. else
  689. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  690. }
  691. void GLProgramState::setUniformVec4(GLint uniformLocation, const Vec4& value)
  692. {
  693. auto v = getUniformValue(uniformLocation);
  694. if (v)
  695. v->setVec4(value);
  696. else
  697. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  698. }
  699. void GLProgramState::setUniformVec4v(const std::string& uniformName, ssize_t size, const Vec4* value)
  700. {
  701. auto v = getUniformValue(uniformName);
  702. if (v)
  703. v->setVec4v(size, value);
  704. else
  705. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  706. }
  707. void GLProgramState::setUniformVec4v(GLint uniformLocation, ssize_t size, const Vec4* pointer)
  708. {
  709. auto v = getUniformValue(uniformLocation);
  710. if (v)
  711. v->setVec4v(size, pointer);
  712. else
  713. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  714. }
  715. void GLProgramState::setUniformMat4(const std::string& uniformName, const Mat4& value)
  716. {
  717. auto v = getUniformValue(uniformName);
  718. if (v)
  719. v->setMat4(value);
  720. else
  721. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  722. }
  723. void GLProgramState::setUniformMat4(GLint uniformLocation, const Mat4& value)
  724. {
  725. auto v = getUniformValue(uniformLocation);
  726. if (v)
  727. v->setMat4(value);
  728. else
  729. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  730. }
  731. // Textures
  732. void GLProgramState::setUniformTexture(const std::string& uniformName, Texture2D *texture)
  733. {
  734. CCASSERT(texture, "Invalid texture");
  735. auto v = getUniformValue(uniformName);
  736. if (v)
  737. {
  738. if (_boundTextureUnits.find(uniformName) != _boundTextureUnits.end())
  739. {
  740. v->setTexture(texture, _boundTextureUnits[uniformName]);
  741. }
  742. else
  743. {
  744. v->setTexture(texture, _textureUnitIndex);
  745. _boundTextureUnits[uniformName] = _textureUnitIndex++;
  746. }
  747. }
  748. else
  749. {
  750. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  751. }
  752. }
  753. void GLProgramState::setUniformTexture(GLint uniformLocation, Texture2D *texture)
  754. {
  755. CCASSERT(texture, "Invalid texture");
  756. auto v = getUniformValue(uniformLocation);
  757. if (v)
  758. {
  759. if (_boundTextureUnits.find(v->_uniform->name) != _boundTextureUnits.end())
  760. {
  761. v->setTexture(texture, _boundTextureUnits[v->_uniform->name]);
  762. }
  763. else
  764. {
  765. v->setTexture(texture, _textureUnitIndex);
  766. _boundTextureUnits[v->_uniform->name] = _textureUnitIndex++;
  767. }
  768. }
  769. else
  770. {
  771. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  772. }
  773. }
  774. void GLProgramState::setUniformTexture(const std::string& uniformName, GLuint textureId)
  775. {
  776. auto v = getUniformValue(uniformName);
  777. if (v)
  778. {
  779. if (_boundTextureUnits.find(uniformName) != _boundTextureUnits.end())
  780. {
  781. v->setTexture(textureId, _boundTextureUnits[uniformName]);
  782. }
  783. else
  784. {
  785. v->setTexture(textureId, _textureUnitIndex);
  786. _boundTextureUnits[uniformName] = _textureUnitIndex++;
  787. }
  788. }
  789. else
  790. {
  791. CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
  792. }
  793. }
  794. void GLProgramState::setUniformTexture(GLint uniformLocation, GLuint textureId)
  795. {
  796. auto v = getUniformValue(uniformLocation);
  797. if (v)
  798. {
  799. if (_boundTextureUnits.find(v->_uniform->name) != _boundTextureUnits.end())
  800. {
  801. v->setTexture(textureId, _boundTextureUnits[v->_uniform->name]);
  802. }
  803. else
  804. {
  805. v->setTexture(textureId, _textureUnitIndex);
  806. _boundTextureUnits[v->_uniform->name] = _textureUnitIndex++;
  807. }
  808. }
  809. else
  810. {
  811. CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
  812. }
  813. }
  814. // Auto bindings
  815. void GLProgramState::setParameterAutoBinding(const std::string& uniformName, const std::string& autoBinding)
  816. {
  817. _autoBindings[uniformName] = autoBinding;
  818. if (_nodeBinding)
  819. applyAutoBinding(uniformName, autoBinding);
  820. }
  821. void GLProgramState::applyAutoBinding(const std::string& uniformName, const std::string& autoBinding)
  822. {
  823. // This code tries to replace GLProgram::setUniformsForBuiltins. But it is unfinished ATM.
  824. // The idea is that users will be able to use variables from cocos2d-x without hardcoding the
  825. // information on GLProgram and other objects.
  826. // Instead, the Cocos2d uniform variables will be callbacks.
  827. // As an example of how bad the current design is, the ModelView matrix is being passed from Node, to the Commands, to the GLProgram.
  828. // Instead, the GLProgramState should obtain it from its target.
  829. bool resolved = false;
  830. for (const auto resolver: _customAutoBindingResolvers)
  831. {
  832. resolved = resolver->resolveAutoBinding(this, _nodeBinding, uniformName, autoBinding);
  833. if (resolved)
  834. break;
  835. }
  836. if (!resolved)
  837. {
  838. // add cocos2d-x variables here like:
  839. // PROJECT_MATRIX
  840. // MODEL_MATRIX
  841. // MODEL_VIEW
  842. // MODEL_VIEW_PROJECTION
  843. // etc...
  844. //
  845. // and remove them from GLProgram::setUniformsForBuiltins
  846. }
  847. }
  848. void GLProgramState::setNodeBinding(Node* target)
  849. {
  850. CCASSERT(target, "must be non-null");
  851. // weak ref
  852. _nodeBinding = target;
  853. for (const auto autobinding: _autoBindings)
  854. applyAutoBinding(autobinding.first, autobinding.second);
  855. }
  856. Node* GLProgramState::getNodeBinding() const
  857. {
  858. return _nodeBinding;
  859. }
  860. //
  861. // MARK: AutoBindingResolver
  862. //
  863. GLProgramState::AutoBindingResolver::AutoBindingResolver()
  864. {
  865. _customAutoBindingResolvers.push_back(this);
  866. }
  867. GLProgramState::AutoBindingResolver::~AutoBindingResolver()
  868. {
  869. std::vector<GLProgramState::AutoBindingResolver*>::iterator itr = std::find(_customAutoBindingResolvers.begin(), _customAutoBindingResolvers.end(), this);
  870. if (itr != _customAutoBindingResolvers.end())
  871. _customAutoBindingResolvers.erase(itr);
  872. }
  873. NS_CC_END