CCGLProgram.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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-2016 Chukong Technologies Inc.
  7. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  8. http://www.cocos2d-x.org
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. ****************************************************************************/
  25. #include "renderer/CCGLProgram.h"
  26. #ifndef WIN32
  27. #include <alloca.h>
  28. #endif
  29. #include "base/CCDirector.h"
  30. #include "base/ccUTF8.h"
  31. #include "renderer/ccGLStateCache.h"
  32. #include "platform/CCFileUtils.h"
  33. // helper functions
  34. static void replaceDefines(const std::string& compileTimeDefines, std::string& out)
  35. {
  36. // Replace semicolons with '#define ... \n'
  37. if (!compileTimeDefines.empty())
  38. {
  39. // append ';' if the last char doesn't have one
  40. auto copyDefines = compileTimeDefines;
  41. if (copyDefines[copyDefines.length()-1] != ';')
  42. copyDefines.append(1, ';');
  43. std::string currentDefine;
  44. for (auto itChar: copyDefines)
  45. {
  46. if (itChar == ';')
  47. {
  48. if (!currentDefine.empty())
  49. {
  50. out.append("\n#define " + currentDefine);
  51. currentDefine.clear();
  52. }
  53. }
  54. else
  55. {
  56. currentDefine.append(1, itChar);
  57. }
  58. }
  59. out += "\n";
  60. }
  61. }
  62. NS_CC_BEGIN
  63. const char* GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR = "#ShaderETC1ASPositionTextureColor";
  64. const char* GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP = "#ShaderETC1ASPositionTextureColor_noMVP";
  65. const char* GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY = "#ShaderETC1ASPositionTextureGray";
  66. const char* GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP = "#ShaderETC1ASPositionTextureGray_noMVP";
  67. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR = "ShaderPositionTextureColor";
  68. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP = "ShaderPositionTextureColor_noMVP";
  69. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST = "ShaderPositionTextureColorAlphaTest";
  70. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV = "ShaderPositionTextureColorAlphaTest_NoMV";
  71. const char* GLProgram::SHADER_NAME_POSITION_COLOR = "ShaderPositionColor";
  72. const char* GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE = "ShaderPositionColorTexAsPointsize";
  73. const char* GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP = "ShaderPositionColor_noMVP";
  74. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE = "ShaderPositionTexture";
  75. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR = "ShaderPositionTexture_uColor";
  76. const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR = "ShaderPositionTextureA8Color";
  77. const char* GLProgram::SHADER_NAME_POSITION_U_COLOR = "ShaderPosition_uColor";
  78. const char* GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR = "ShaderPositionLengthTextureColor";
  79. const char* GLProgram::SHADER_NAME_POSITION_GRAYSCALE = "ShaderUIGrayScale";
  80. const char* GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL = "ShaderLabelDFNormal";
  81. const char* GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW = "ShaderLabelDFGlow";
  82. const char* GLProgram::SHADER_NAME_LABEL_NORMAL = "ShaderLabelNormal";
  83. const char* GLProgram::SHADER_NAME_LABEL_OUTLINE = "ShaderLabelOutline";
  84. const char* GLProgram::SHADER_3D_POSITION = "Shader3DPosition";
  85. const char* GLProgram::SHADER_3D_POSITION_TEXTURE = "Shader3DPositionTexture";
  86. const char* GLProgram::SHADER_3D_SKINPOSITION_TEXTURE = "Shader3DSkinPositionTexture";
  87. const char* GLProgram::SHADER_3D_POSITION_NORMAL = "Shader3DPositionNormal";
  88. const char* GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE = "Shader3DPositionNormalTexture";
  89. const char* GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE = "Shader3DSkinPositionNormalTexture";
  90. const char* GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE = "Shader3DPositionBumpedNormalTexture";
  91. const char* GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE = "Shader3DSkinPositionBumpedNormalTexture";
  92. const char* GLProgram::SHADER_3D_PARTICLE_COLOR = "Shader3DParticleColor";
  93. const char* GLProgram::SHADER_3D_PARTICLE_TEXTURE = "Shader3DParticleTexture";
  94. const char* GLProgram::SHADER_3D_SKYBOX = "Shader3DSkybox";
  95. const char* GLProgram::SHADER_3D_TERRAIN = "Shader3DTerrain";
  96. const char* GLProgram::SHADER_CAMERA_CLEAR = "ShaderCameraClear";
  97. const char* GLProgram::SHADER_LAYER_RADIAL_GRADIENT = "ShaderLayerRadialGradient";
  98. // uniform names
  99. const char* GLProgram::UNIFORM_NAME_AMBIENT_COLOR = "CC_AmbientColor";
  100. const char* GLProgram::UNIFORM_NAME_P_MATRIX = "CC_PMatrix";
  101. const char* GLProgram::UNIFORM_NAME_MULTIVIEW_P_MATRIX = "CC_MultiViewPMatrix";
  102. const char* GLProgram::UNIFORM_NAME_MV_MATRIX = "CC_MVMatrix";
  103. const char* GLProgram::UNIFORM_NAME_MVP_MATRIX = "CC_MVPMatrix";
  104. const char* GLProgram::UNIFORM_NAME_MULTIVIEW_MVP_MATRIX = "CC_MultiViewMVPMatrix";
  105. const char* GLProgram::UNIFORM_NAME_NORMAL_MATRIX = "CC_NormalMatrix";
  106. const char* GLProgram::UNIFORM_NAME_TIME = "CC_Time";
  107. const char* GLProgram::UNIFORM_NAME_SIN_TIME = "CC_SinTime";
  108. const char* GLProgram::UNIFORM_NAME_COS_TIME = "CC_CosTime";
  109. const char* GLProgram::UNIFORM_NAME_RANDOM01 = "CC_Random01";
  110. const char* GLProgram::UNIFORM_NAME_SAMPLER0 = "CC_Texture0";
  111. const char* GLProgram::UNIFORM_NAME_SAMPLER1 = "CC_Texture1";
  112. const char* GLProgram::UNIFORM_NAME_SAMPLER2 = "CC_Texture2";
  113. const char* GLProgram::UNIFORM_NAME_SAMPLER3 = "CC_Texture3";
  114. const char* GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE = "CC_alpha_value";
  115. // Attribute names
  116. const char* GLProgram::ATTRIBUTE_NAME_COLOR = "a_color";
  117. const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position";
  118. const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
  119. const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD1 = "a_texCoord1";
  120. const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD2 = "a_texCoord2";
  121. const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD3 = "a_texCoord3";
  122. const char* GLProgram::ATTRIBUTE_NAME_NORMAL = "a_normal";
  123. const char* GLProgram::ATTRIBUTE_NAME_BLEND_WEIGHT = "a_blendWeight";
  124. const char* GLProgram::ATTRIBUTE_NAME_BLEND_INDEX = "a_blendIndex";
  125. const char* GLProgram::ATTRIBUTE_NAME_TANGENT = "a_tangent";
  126. const char* GLProgram::ATTRIBUTE_NAME_BINORMAL = "a_binormal";
  127. static const char * COCOS2D_SHADER_UNIFORMS =
  128. "uniform mat4 CC_PMatrix;\n"
  129. "uniform mat4 CC_MultiViewPMatrix[4];\n"
  130. "uniform mat4 CC_MVMatrix;\n"
  131. "uniform mat4 CC_MVPMatrix;\n"
  132. "uniform mat4 CC_MultiViewMVPMatrix[4];\n"
  133. "uniform mat3 CC_NormalMatrix;\n"
  134. "uniform vec4 CC_Time;\n"
  135. "uniform vec4 CC_SinTime;\n"
  136. "uniform vec4 CC_CosTime;\n"
  137. "uniform vec4 CC_Random01;\n"
  138. "uniform sampler2D CC_Texture0;\n"
  139. "uniform sampler2D CC_Texture1;\n"
  140. "uniform sampler2D CC_Texture2;\n"
  141. "uniform sampler2D CC_Texture3;\n"
  142. "//CC INCLUDES END\n\n";
  143. static const std::string EMPTY_DEFINE;
  144. GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
  145. {
  146. return createWithByteArrays(vShaderByteArray, fShaderByteArray, EMPTY_DEFINE);
  147. }
  148. GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines)
  149. {
  150. return createWithByteArrays(vShaderByteArray, fShaderByteArray, EMPTY_DEFINE, compileTimeDefines);
  151. }
  152. GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeHeaders, const std::string& compileTimeDefines)
  153. {
  154. auto ret = new (std::nothrow) GLProgram();
  155. if(ret && ret->initWithByteArrays(vShaderByteArray, fShaderByteArray, compileTimeHeaders, compileTimeDefines)) {
  156. ret->link();
  157. ret->updateUniforms();
  158. ret->autorelease();
  159. return ret;
  160. }
  161. CC_SAFE_DELETE(ret);
  162. return nullptr;
  163. }
  164. GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename)
  165. {
  166. return createWithFilenames(vShaderFilename, fShaderFilename, EMPTY_DEFINE);
  167. }
  168. GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeDefines)
  169. {
  170. return createWithFilenames(vShaderFilename, fShaderFilename, EMPTY_DEFINE, compileTimeDefines);
  171. }
  172. GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& /*compileTimeHeaders*/, const std::string& compileTimeDefines)
  173. {
  174. auto ret = new (std::nothrow) GLProgram();
  175. if(ret && ret->initWithFilenames(vShaderFilename, fShaderFilename, compileTimeDefines)) {
  176. ret->link();
  177. ret->updateUniforms();
  178. ret->autorelease();
  179. return ret;
  180. }
  181. CC_SAFE_DELETE(ret);
  182. return nullptr;
  183. }
  184. GLProgram::GLProgram()
  185. : _program(0)
  186. , _vertShader(0)
  187. , _fragShader(0)
  188. , _flags()
  189. {
  190. _director = Director::getInstance();
  191. CCASSERT(nullptr != _director, "Director is null when init a GLProgram");
  192. memset(_builtInUniforms, 0, sizeof(_builtInUniforms));
  193. }
  194. GLProgram::~GLProgram()
  195. {
  196. CCLOGINFO("%s %d deallocing GLProgram: %p", __FUNCTION__, __LINE__, this);
  197. clearShader();
  198. if (_program)
  199. {
  200. GL::deleteProgram(_program);
  201. }
  202. clearHashUniforms();
  203. }
  204. bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
  205. {
  206. return initWithByteArrays(vShaderByteArray, fShaderByteArray, EMPTY_DEFINE);
  207. }
  208. bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines)
  209. {
  210. return initWithByteArrays(vShaderByteArray, fShaderByteArray, "", compileTimeDefines);
  211. }
  212. bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeHeaders, const std::string& compileTimeDefines)
  213. {
  214. _program = glCreateProgram();
  215. CHECK_GL_ERROR_DEBUG();
  216. // convert defines here. If we do it in "compileShader" we will do it twice.
  217. // a cache for the defines could be useful, but seems like overkill at this point
  218. std::string replacedDefines = "";
  219. replaceDefines(compileTimeDefines, replacedDefines);
  220. _vertShader = _fragShader = 0;
  221. if (vShaderByteArray)
  222. {
  223. if (!compileShader(&_vertShader, GL_VERTEX_SHADER, vShaderByteArray, compileTimeHeaders, replacedDefines))
  224. {
  225. CCLOG("cocos2d: ERROR: Failed to compile vertex shader");
  226. return false;
  227. }
  228. }
  229. // Create and compile fragment shader
  230. if (fShaderByteArray)
  231. {
  232. if (!compileShader(&_fragShader, GL_FRAGMENT_SHADER, fShaderByteArray, compileTimeHeaders, replacedDefines))
  233. {
  234. CCLOG("cocos2d: ERROR: Failed to compile fragment shader");
  235. return false;
  236. }
  237. }
  238. if (_vertShader)
  239. {
  240. glAttachShader(_program, _vertShader);
  241. }
  242. CHECK_GL_ERROR_DEBUG();
  243. if (_fragShader)
  244. {
  245. glAttachShader(_program, _fragShader);
  246. }
  247. clearHashUniforms();
  248. CHECK_GL_ERROR_DEBUG();
  249. return true;
  250. }
  251. bool GLProgram::initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename)
  252. {
  253. return initWithFilenames(vShaderFilename, fShaderFilename, EMPTY_DEFINE);
  254. }
  255. bool GLProgram::initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeDefines)
  256. {
  257. return initWithFilenames(vShaderFilename, fShaderFilename, EMPTY_DEFINE, compileTimeDefines);
  258. }
  259. bool GLProgram::initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeHeaders, const std::string& compileTimeDefines)
  260. {
  261. auto fileUtils = FileUtils::getInstance();
  262. std::string vertexSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(vShaderFilename));
  263. std::string fragmentSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(fShaderFilename));
  264. return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str(), compileTimeHeaders, compileTimeDefines);
  265. }
  266. void GLProgram::bindPredefinedVertexAttribs()
  267. {
  268. static const struct {
  269. const char *attributeName;
  270. int location;
  271. } attribute_locations[] =
  272. {
  273. {GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION},
  274. {GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR},
  275. {GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD},
  276. {GLProgram::ATTRIBUTE_NAME_TEX_COORD1, GLProgram::VERTEX_ATTRIB_TEX_COORD1},
  277. {GLProgram::ATTRIBUTE_NAME_TEX_COORD2, GLProgram::VERTEX_ATTRIB_TEX_COORD2},
  278. {GLProgram::ATTRIBUTE_NAME_TEX_COORD3, GLProgram::VERTEX_ATTRIB_TEX_COORD3},
  279. {GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::VERTEX_ATTRIB_NORMAL},
  280. };
  281. const int size = sizeof(attribute_locations) / sizeof(attribute_locations[0]);
  282. for(int i=0; i<size;i++) {
  283. glBindAttribLocation(_program, attribute_locations[i].location, attribute_locations[i].attributeName);
  284. }
  285. }
  286. void GLProgram::parseVertexAttribs()
  287. {
  288. //_vertexAttribs.clear();
  289. // Query and store vertex attribute meta-data from the program.
  290. GLint activeAttributes;
  291. GLint length;
  292. glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTES, &activeAttributes);
  293. if(activeAttributes > 0)
  294. {
  295. VertexAttrib attribute;
  296. glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &length);
  297. if(length > 0)
  298. {
  299. GLchar* attribName = (GLchar*) alloca(length + 1);
  300. for(int i = 0; i < activeAttributes; ++i)
  301. {
  302. // Query attribute info.
  303. glGetActiveAttrib(_program, i, length, nullptr, &attribute.size, &attribute.type, attribName);
  304. attribName[length] = '\0';
  305. attribute.name = std::string(attribName);
  306. // Query the pre-assigned attribute location
  307. attribute.index = glGetAttribLocation(_program, attribName);
  308. _vertexAttribs[attribute.name] = attribute;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. GLchar ErrorLog[1024];
  315. glGetProgramInfoLog(_program, sizeof(ErrorLog), nullptr, ErrorLog);
  316. CCLOG("Error linking shader program: '%s'\n", ErrorLog);
  317. }
  318. }
  319. void GLProgram::parseUniforms()
  320. {
  321. //_userUniforms.clear();
  322. // Query and store uniforms from the program.
  323. GLint activeUniforms;
  324. glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &activeUniforms);
  325. if(activeUniforms > 0)
  326. {
  327. GLint length;
  328. glGetProgramiv(_program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length);
  329. if(length > 0)
  330. {
  331. Uniform uniform;
  332. GLchar* uniformName = (GLchar*)alloca(length + 1);
  333. for(int i = 0; i < activeUniforms; ++i)
  334. {
  335. // Query uniform info.
  336. glGetActiveUniform(_program, i, length, nullptr, &uniform.size, &uniform.type, uniformName);
  337. uniformName[length] = '\0';
  338. // Only add uniforms that are not built-in.
  339. // The ones that start with 'CC_' are built-ins
  340. if(strncmp("CC_", uniformName, 3) != 0) {
  341. // remove possible array '[]' from uniform name
  342. if(length > 3)
  343. {
  344. char* c = strrchr(uniformName, '[');
  345. if(c)
  346. {
  347. *c = '\0';
  348. }
  349. }
  350. uniform.name = std::string(uniformName);
  351. uniform.location = glGetUniformLocation(_program, uniformName);
  352. GLenum __gl_error_code = glGetError();
  353. if (__gl_error_code != GL_NO_ERROR)
  354. {
  355. CCLOG("error: 0x%x uniformName: %s", (int)__gl_error_code, uniformName);
  356. }
  357. assert(__gl_error_code == GL_NO_ERROR);
  358. _userUniforms[uniform.name] = uniform;
  359. }
  360. }
  361. }
  362. }
  363. else
  364. {
  365. GLchar ErrorLog[1024];
  366. glGetProgramInfoLog(_program, sizeof(ErrorLog), nullptr, ErrorLog);
  367. CCLOG("Error linking shader program: '%s'\n", ErrorLog);
  368. }
  369. }
  370. Uniform* GLProgram::getUniform(const std::string &name)
  371. {
  372. const auto itr = _userUniforms.find(name);
  373. if( itr != _userUniforms.end())
  374. return &itr->second;
  375. return nullptr;
  376. }
  377. VertexAttrib* GLProgram::getVertexAttrib(const std::string &name)
  378. {
  379. const auto itr = _vertexAttribs.find(name);
  380. if( itr != _vertexAttribs.end())
  381. return &itr->second;
  382. return nullptr;
  383. }
  384. std::string GLProgram::getDescription() const
  385. {
  386. return StringUtils::format("<GLProgram = "
  387. CC_FORMAT_PRINTF_SIZE_T
  388. " | Program = %i, VertexShader = %i, FragmentShader = %i>",
  389. (size_t)this, _program, _vertShader, _fragShader);
  390. }
  391. bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source)
  392. {
  393. return compileShader(shader, type, source, "");
  394. }
  395. bool GLProgram::compileShader(GLuint* shader, GLenum type, const GLchar* source, const std::string& convertedDefines)
  396. {
  397. return compileShader(shader, type, source, "", convertedDefines);
  398. }
  399. bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source, const std::string& compileTimeHeaders, const std::string& convertedDefines)
  400. {
  401. GLint status;
  402. if (!source)
  403. {
  404. return false;
  405. }
  406. std::string headersDef;
  407. if (compileTimeHeaders.empty()) {
  408. #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  409. headersDef = (type == GL_VERTEX_SHADER ? "precision mediump float;\n precision mediump int;\n" : "precision mediump float;\n precision mediump int;\n");
  410. // Bugfix to make shader variables types constant to be understood by the current Android Virtual Devices or Emulators. This will also eliminate the 0x501 and 0x502 OpenGL Errors during emulation.
  411. #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  412. headersDef = "#version 100\n precision mediump float;\n precision mediump int;\n";
  413. #elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
  414. headersDef = (type == GL_VERTEX_SHADER ? "precision highp float;\n precision highp int;\n" : "precision mediump float;\n precision mediump int;\n");
  415. #endif
  416. }else{
  417. headersDef = compileTimeHeaders;
  418. }
  419. const GLchar *sources[] = {
  420. headersDef.c_str(),
  421. COCOS2D_SHADER_UNIFORMS,
  422. convertedDefines.c_str(),
  423. source};
  424. *shader = glCreateShader(type);
  425. glShaderSource(*shader, sizeof(sources)/sizeof(*sources), sources, nullptr);
  426. glCompileShader(*shader);
  427. glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
  428. if (! status)
  429. {
  430. GLsizei length;
  431. glGetShaderiv(*shader, GL_SHADER_SOURCE_LENGTH, &length);
  432. GLchar* src = (GLchar *)malloc(sizeof(GLchar) * length);
  433. glGetShaderSource(*shader, length, nullptr, src);
  434. CCLOG("cocos2d: ERROR: Failed to compile shader:\n%s", src);
  435. if (type == GL_VERTEX_SHADER)
  436. {
  437. CCLOG("cocos2d: %s", getVertexShaderLog().c_str());
  438. }
  439. else
  440. {
  441. CCLOG("cocos2d: %s", getFragmentShaderLog().c_str());
  442. }
  443. free(src);
  444. return false;
  445. }
  446. return (status == GL_TRUE);
  447. }
  448. GLint GLProgram::getAttribLocation(const std::string &attributeName) const
  449. {
  450. return glGetAttribLocation(_program, attributeName.c_str());
  451. }
  452. GLint GLProgram::getUniformLocation(const std::string &attributeName) const
  453. {
  454. return glGetUniformLocation(_program, attributeName.c_str());
  455. }
  456. void GLProgram::bindAttribLocation(const std::string &attributeName, GLuint index) const
  457. {
  458. glBindAttribLocation(_program, index, attributeName.c_str());
  459. }
  460. void GLProgram::updateUniforms()
  461. {
  462. _builtInUniforms[UNIFORM_AMBIENT_COLOR] = glGetUniformLocation(_program, UNIFORM_NAME_AMBIENT_COLOR);
  463. _builtInUniforms[UNIFORM_P_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_P_MATRIX);
  464. _builtInUniforms[UNIFORM_MULTIVIEW_P_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MULTIVIEW_P_MATRIX);
  465. _builtInUniforms[UNIFORM_MV_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MV_MATRIX);
  466. _builtInUniforms[UNIFORM_MVP_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MVP_MATRIX);
  467. _builtInUniforms[UNIFORM_MULTIVIEW_MVP_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MULTIVIEW_MVP_MATRIX);
  468. _builtInUniforms[UNIFORM_NORMAL_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_NORMAL_MATRIX);
  469. _builtInUniforms[UNIFORM_TIME] = glGetUniformLocation(_program, UNIFORM_NAME_TIME);
  470. _builtInUniforms[UNIFORM_SIN_TIME] = glGetUniformLocation(_program, UNIFORM_NAME_SIN_TIME);
  471. _builtInUniforms[UNIFORM_COS_TIME] = glGetUniformLocation(_program, UNIFORM_NAME_COS_TIME);
  472. _builtInUniforms[UNIFORM_RANDOM01] = glGetUniformLocation(_program, UNIFORM_NAME_RANDOM01);
  473. _builtInUniforms[UNIFORM_SAMPLER0] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER0);
  474. _builtInUniforms[UNIFORM_SAMPLER1] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER1);
  475. _builtInUniforms[UNIFORM_SAMPLER2] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER2);
  476. _builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3);
  477. _flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1;
  478. _flags.usesMultiViewP = _builtInUniforms[UNIFORM_MULTIVIEW_P_MATRIX] != -1;
  479. _flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1;
  480. _flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1;
  481. _flags.usesMultiViewMVP = _builtInUniforms[UNIFORM_MULTIVIEW_MVP_MATRIX] != -1;
  482. _flags.usesNormal = _builtInUniforms[UNIFORM_NORMAL_MATRIX] != -1;
  483. _flags.usesTime = (
  484. _builtInUniforms[UNIFORM_TIME] != -1 ||
  485. _builtInUniforms[UNIFORM_SIN_TIME] != -1 ||
  486. _builtInUniforms[UNIFORM_COS_TIME] != -1
  487. );
  488. _flags.usesRandom = _builtInUniforms[UNIFORM_RANDOM01] != -1;
  489. this->use();
  490. // Since sample most probably won't change, set it to 0,1,2,3 now.
  491. if(_builtInUniforms[UNIFORM_SAMPLER0] != -1)
  492. setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER0], 0);
  493. if(_builtInUniforms[UNIFORM_SAMPLER1] != -1)
  494. setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER1], 1);
  495. if(_builtInUniforms[UNIFORM_SAMPLER2] != -1)
  496. setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER2], 2);
  497. if(_builtInUniforms[UNIFORM_SAMPLER3] != -1)
  498. setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER3], 3);
  499. // clear any glErrors created by any not found uniforms
  500. glGetError();
  501. }
  502. bool GLProgram::link()
  503. {
  504. CCASSERT(_program != 0, "Cannot link invalid program");
  505. GLint status = GL_TRUE;
  506. bindPredefinedVertexAttribs();
  507. glLinkProgram(_program);
  508. // Calling glGetProgramiv(...GL_LINK_STATUS...) will force linking of the program at this moment.
  509. // Otherwise, they might be linked when they are used for the first time. (I guess this depends on the driver implementation)
  510. // So it might slow down the "booting" process on certain devices. But, on the other hand it is important to know if the shader
  511. // linked successfully. Some shaders might be downloaded in runtime so, release version should have this check.
  512. // For more info, see Github issue #16231
  513. glGetProgramiv(_program, GL_LINK_STATUS, &status);
  514. if (status == GL_FALSE)
  515. {
  516. CCLOG("cocos2d: ERROR: Failed to link program: %i", _program);
  517. GL::deleteProgram(_program);
  518. _program = 0;
  519. }
  520. else
  521. {
  522. parseVertexAttribs();
  523. parseUniforms();
  524. clearShader();
  525. }
  526. return (status == GL_TRUE);
  527. }
  528. void GLProgram::use()
  529. {
  530. GL::useProgram(_program);
  531. }
  532. static std::string logForOpenGLShader(GLuint shader)
  533. {
  534. GLint logLength = 0;
  535. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
  536. if (logLength < 1)
  537. return "";
  538. char *logBytes = (char*)malloc(sizeof(char) * logLength);
  539. glGetShaderInfoLog(shader, logLength, nullptr, logBytes);
  540. std::string ret(logBytes);
  541. free(logBytes);
  542. return ret;
  543. }
  544. static std::string logForOpenGLProgram(GLuint program)
  545. {
  546. GLint logLength = 0;
  547. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
  548. if (logLength < 1)
  549. return "";
  550. char *logBytes = (char*)malloc(sizeof(char) * logLength);
  551. glGetProgramInfoLog(program, logLength, nullptr, logBytes);
  552. std::string ret(logBytes);
  553. free(logBytes);
  554. return ret;
  555. }
  556. std::string GLProgram::getVertexShaderLog() const
  557. {
  558. return cocos2d::logForOpenGLShader(_vertShader);
  559. }
  560. std::string GLProgram::getFragmentShaderLog() const
  561. {
  562. return cocos2d::logForOpenGLShader(_fragShader);
  563. }
  564. std::string GLProgram::getProgramLog() const
  565. {
  566. return logForOpenGLProgram(_program);
  567. }
  568. // Uniform cache
  569. bool GLProgram::updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes)
  570. {
  571. if (location < 0)
  572. {
  573. return false;
  574. }
  575. bool updated = true;
  576. auto element = _hashForUniforms.find(location);
  577. if (element == _hashForUniforms.end())
  578. {
  579. GLvoid* value = malloc(bytes);
  580. memcpy(value, data, bytes );
  581. _hashForUniforms.emplace(location, std::make_pair(value, bytes));
  582. }
  583. else
  584. {
  585. if (element->second.second < bytes)
  586. {
  587. GLvoid* value = realloc(element->second.first, bytes);
  588. memcpy(value, data, bytes);
  589. _hashForUniforms[location] = std::make_pair(value, bytes);
  590. }
  591. else
  592. {
  593. if (memcmp(element->second.first, data, bytes) == 0)
  594. {
  595. updated = false;
  596. }
  597. else
  598. memcpy(element->second.first, data, bytes);
  599. }
  600. }
  601. return updated;
  602. }
  603. GLint GLProgram::getUniformLocationForName(const char* name) const
  604. {
  605. CCASSERT(name != nullptr, "Invalid uniform name" );
  606. CCASSERT(_program != 0, "Invalid operation. Cannot get uniform location when program is not initialized");
  607. return glGetUniformLocation(_program, name);
  608. }
  609. void GLProgram::setUniformLocationWith1i(GLint location, GLint i1)
  610. {
  611. bool updated = updateUniformLocation(location, &i1, sizeof(i1)*1);
  612. if (updated)
  613. {
  614. glUniform1i( (GLint)location, i1);
  615. }
  616. }
  617. void GLProgram::setUniformLocationWith2i(GLint location, GLint i1, GLint i2)
  618. {
  619. GLint ints[2] = {i1,i2};
  620. bool updated = updateUniformLocation(location, ints, sizeof(ints));
  621. if (updated)
  622. {
  623. glUniform2i( (GLint)location, i1, i2);
  624. }
  625. }
  626. void GLProgram::setUniformLocationWith3i(GLint location, GLint i1, GLint i2, GLint i3)
  627. {
  628. GLint ints[3] = {i1,i2,i3};
  629. bool updated = updateUniformLocation(location, ints, sizeof(ints));
  630. if (updated)
  631. {
  632. glUniform3i( (GLint)location, i1, i2, i3);
  633. }
  634. }
  635. void GLProgram::setUniformLocationWith4i(GLint location, GLint i1, GLint i2, GLint i3, GLint i4)
  636. {
  637. GLint ints[4] = {i1,i2,i3,i4};
  638. bool updated = updateUniformLocation(location, ints, sizeof(ints));
  639. if (updated)
  640. {
  641. glUniform4i( (GLint)location, i1, i2, i3, i4);
  642. }
  643. }
  644. void GLProgram::setUniformLocationWith2iv(GLint location, GLint* ints, unsigned int numberOfArrays)
  645. {
  646. bool updated = updateUniformLocation(location, ints, sizeof(int)*2*numberOfArrays);
  647. if (updated)
  648. {
  649. glUniform2iv( (GLint)location, (GLsizei)numberOfArrays, ints );
  650. }
  651. }
  652. void GLProgram::setUniformLocationWith3iv(GLint location, GLint* ints, unsigned int numberOfArrays)
  653. {
  654. bool updated = updateUniformLocation(location, ints, sizeof(int)*3*numberOfArrays);
  655. if (updated)
  656. {
  657. glUniform3iv( (GLint)location, (GLsizei)numberOfArrays, ints );
  658. }
  659. }
  660. void GLProgram::setUniformLocationWith4iv(GLint location, GLint* ints, unsigned int numberOfArrays)
  661. {
  662. bool updated = updateUniformLocation(location, ints, sizeof(int)*4*numberOfArrays);
  663. if (updated)
  664. {
  665. glUniform4iv( (GLint)location, (GLsizei)numberOfArrays, ints );
  666. }
  667. }
  668. void GLProgram::setUniformLocationWith1f(GLint location, GLfloat f1)
  669. {
  670. bool updated = updateUniformLocation(location, &f1, sizeof(f1)*1);
  671. if (updated)
  672. {
  673. glUniform1f( (GLint)location, f1);
  674. }
  675. }
  676. void GLProgram::setUniformLocationWith2f(GLint location, GLfloat f1, GLfloat f2)
  677. {
  678. GLfloat floats[2] = {f1,f2};
  679. bool updated = updateUniformLocation(location, floats, sizeof(floats));
  680. if (updated)
  681. {
  682. glUniform2f( (GLint)location, f1, f2);
  683. }
  684. }
  685. void GLProgram::setUniformLocationWith3f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3)
  686. {
  687. GLfloat floats[3] = {f1,f2,f3};
  688. bool updated = updateUniformLocation(location, floats, sizeof(floats));
  689. if (updated)
  690. {
  691. glUniform3f( (GLint)location, f1, f2, f3);
  692. }
  693. }
  694. void GLProgram::setUniformLocationWith4f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4)
  695. {
  696. GLfloat floats[4] = {f1,f2,f3,f4};
  697. bool updated = updateUniformLocation(location, floats, sizeof(floats));
  698. if (updated)
  699. {
  700. glUniform4f( (GLint)location, f1, f2, f3,f4);
  701. }
  702. }
  703. void GLProgram::setUniformLocationWith1fv( GLint location, const GLfloat* floats, unsigned int numberOfArrays )
  704. {
  705. bool updated = updateUniformLocation(location, floats, sizeof(float)*numberOfArrays);
  706. if (updated)
  707. {
  708. glUniform1fv( (GLint)location, (GLsizei)numberOfArrays, floats );
  709. }
  710. }
  711. void GLProgram::setUniformLocationWith2fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays)
  712. {
  713. bool updated = updateUniformLocation(location, floats, sizeof(float)*2*numberOfArrays);
  714. if (updated)
  715. {
  716. glUniform2fv( (GLint)location, (GLsizei)numberOfArrays, floats );
  717. }
  718. }
  719. void GLProgram::setUniformLocationWith3fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays)
  720. {
  721. bool updated = updateUniformLocation(location, floats, sizeof(float)*3*numberOfArrays);
  722. if (updated)
  723. {
  724. glUniform3fv( (GLint)location, (GLsizei)numberOfArrays, floats );
  725. }
  726. }
  727. void GLProgram::setUniformLocationWith4fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays)
  728. {
  729. bool updated = updateUniformLocation(location, floats, sizeof(float)*4*numberOfArrays);
  730. if (updated)
  731. {
  732. glUniform4fv( (GLint)location, (GLsizei)numberOfArrays, floats );
  733. }
  734. }
  735. void GLProgram::setUniformLocationWithMatrix2fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices) {
  736. bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*4*numberOfMatrices);
  737. if (updated)
  738. {
  739. glUniformMatrix2fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
  740. }
  741. }
  742. void GLProgram::setUniformLocationWithMatrix3fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices) {
  743. bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*9*numberOfMatrices);
  744. if (updated)
  745. {
  746. glUniformMatrix3fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
  747. }
  748. }
  749. void GLProgram::setUniformLocationWithMatrix4fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices)
  750. {
  751. bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*16*numberOfMatrices);
  752. if (updated)
  753. {
  754. glUniformMatrix4fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
  755. }
  756. }
  757. void GLProgram::setUniformsForBuiltins()
  758. {
  759. setUniformsForBuiltins(_director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW));
  760. }
  761. void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
  762. {
  763. const auto& matrixP = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  764. if (_flags.usesP)
  765. setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_P_MATRIX], matrixP.m, 1);
  766. if (_flags.usesMultiViewP)
  767. {
  768. Mat4 mats[4];
  769. const auto stackSize = std::min<size_t>(_director->getProjectionMatrixStackSize(), 4);
  770. for (size_t i = 0; i < stackSize; ++i) {
  771. mats[i] = _director->getProjectionMatrix(i);
  772. }
  773. setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MULTIVIEW_P_MATRIX], mats[0].m, 4);
  774. }
  775. if (_flags.usesMV)
  776. setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MV_MATRIX], matrixMV.m, 1);
  777. if (_flags.usesMVP)
  778. {
  779. Mat4 matrixMVP = matrixP * matrixMV;
  780. setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MVP_MATRIX], matrixMVP.m, 1);
  781. }
  782. if (_flags.usesMultiViewMVP)
  783. {
  784. Mat4 mats[4];
  785. const auto stackSize = std::min<size_t>(_director->getProjectionMatrixStackSize(), 4);
  786. for (size_t i = 0; i < stackSize; ++i) {
  787. mats[i] = _director->getProjectionMatrix(i) * matrixMV;
  788. }
  789. setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MULTIVIEW_MVP_MATRIX], mats[0].m, 4);
  790. }
  791. if (_flags.usesNormal)
  792. {
  793. Mat4 mvInverse = matrixMV;
  794. mvInverse.m[12] = mvInverse.m[13] = mvInverse.m[14] = 0.0f;
  795. mvInverse.inverse();
  796. mvInverse.transpose();
  797. GLfloat normalMat[9];
  798. normalMat[0] = mvInverse.m[0];normalMat[1] = mvInverse.m[1];normalMat[2] = mvInverse.m[2];
  799. normalMat[3] = mvInverse.m[4];normalMat[4] = mvInverse.m[5];normalMat[5] = mvInverse.m[6];
  800. normalMat[6] = mvInverse.m[8];normalMat[7] = mvInverse.m[9];normalMat[8] = mvInverse.m[10];
  801. setUniformLocationWithMatrix3fv(_builtInUniforms[UNIFORM_NORMAL_MATRIX], normalMat, 1);
  802. }
  803. if (_flags.usesTime) {
  804. // This doesn't give the most accurate global time value.
  805. // Cocos2D doesn't store a high precision time value, so this will have to do.
  806. // Getting Mach time per frame per shader using time could be extremely expensive.
  807. float time = _director->getTotalFrames() * _director->getAnimationInterval();
  808. setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_TIME], time/10.0f, time, time*2, time*4);
  809. setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_SIN_TIME], time/8.0f, time/4.0f, time/2.0f, sinf(time));
  810. setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_COS_TIME], time/8.0f, time/4.0f, time/2.0f, cosf(time));
  811. }
  812. if (_flags.usesRandom)
  813. setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_RANDOM01], CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
  814. }
  815. void GLProgram::reset()
  816. {
  817. _vertShader = _fragShader = 0;
  818. memset(_builtInUniforms, 0, sizeof(_builtInUniforms));
  819. // it is already deallocated by android
  820. //GL::deleteProgram(_program);
  821. _program = 0;
  822. clearHashUniforms();
  823. }
  824. inline void GLProgram::clearShader()
  825. {
  826. if (_vertShader)
  827. {
  828. glDeleteShader(_vertShader);
  829. }
  830. if (_fragShader)
  831. {
  832. glDeleteShader(_fragShader);
  833. }
  834. _vertShader = _fragShader = 0;
  835. }
  836. inline void GLProgram::clearHashUniforms()
  837. {
  838. for (auto e: _hashForUniforms)
  839. {
  840. free(e.second.first);
  841. }
  842. _hashForUniforms.clear();
  843. }
  844. NS_CC_END