CCGLProgramCache.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /****************************************************************************
  2. Copyright (c) 2011 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 NO 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/CCGLProgramCache.h"
  25. #include "renderer/CCGLProgram.h"
  26. #include "renderer/ccShaders.h"
  27. #include "base/ccMacros.h"
  28. #include "base/CCConfiguration.h"
  29. #include "base/CCEventListenerCustom.h"
  30. #include "base/CCDirector.h"
  31. #include "base/CCEventDispatcher.h"
  32. NS_CC_BEGIN
  33. enum {
  34. kShaderType_PositionTextureColor,
  35. kShaderType_PositionTextureColor_noMVP,
  36. kShaderType_PositionTextureColorAlphaTest,
  37. kShaderType_PositionTextureColorAlphaTestNoMV,
  38. kShaderType_PositionColor,
  39. kShaderType_PositionColorTextureAsPointsize,
  40. kShaderType_PositionColor_noMVP,
  41. kShaderType_PositionTexture,
  42. kShaderType_PositionTexture_uColor,
  43. kShaderType_PositionTextureA8Color,
  44. kShaderType_Position_uColor,
  45. kShaderType_PositionLengthTextureColor,
  46. kShaderType_LabelDistanceFieldNormal,
  47. kShaderType_LabelDistanceFieldGlow,
  48. kShaderType_UIGrayScale,
  49. kShaderType_LabelNormal,
  50. kShaderType_LabelOutline,
  51. kShaderType_3DPosition,
  52. kShaderType_3DPositionTex,
  53. kShaderType_3DSkinPositionTex,
  54. kShaderType_3DPositionNormal,
  55. kShaderType_3DPositionNormalTex,
  56. kShaderType_3DSkinPositionNormalTex,
  57. kShaderType_3DPositionBumpedNormalTex,
  58. kShaderType_3DSkinPositionBumpedNormalTex,
  59. kShaderType_3DParticleTex,
  60. kShaderType_3DParticleColor,
  61. kShaderType_3DSkyBox,
  62. kShaderType_3DTerrain,
  63. kShaderType_CameraClear,
  64. // ETC1 ALPHA supports.
  65. kShaderType_ETC1ASPositionTextureColor,
  66. kShaderType_ETC1ASPositionTextureColor_noMVP,
  67. kShaderType_ETC1ASPositionTextureGray,
  68. kShaderType_ETC1ASPositionTextureGray_noMVP,
  69. kShaderType_LayerRadialGradient,
  70. kShaderType_MAX,
  71. };
  72. static GLProgramCache *_sharedGLProgramCache = nullptr;
  73. GLProgramCache* GLProgramCache::getInstance()
  74. {
  75. if (!_sharedGLProgramCache) {
  76. _sharedGLProgramCache = new (std::nothrow) GLProgramCache();
  77. if (!_sharedGLProgramCache->init())
  78. {
  79. CC_SAFE_DELETE(_sharedGLProgramCache);
  80. }
  81. }
  82. return _sharedGLProgramCache;
  83. }
  84. void GLProgramCache::destroyInstance()
  85. {
  86. CC_SAFE_RELEASE_NULL(_sharedGLProgramCache);
  87. }
  88. // FIXME: deprecated
  89. GLProgramCache* GLProgramCache::sharedShaderCache()
  90. {
  91. return GLProgramCache::getInstance();
  92. }
  93. // FIXME: deprecated
  94. void GLProgramCache::purgeSharedShaderCache()
  95. {
  96. GLProgramCache::destroyInstance();
  97. }
  98. GLProgramCache::GLProgramCache()
  99. : _programs()
  100. {
  101. }
  102. GLProgramCache::~GLProgramCache()
  103. {
  104. for(auto& program : _programs) {
  105. program.second->release();
  106. }
  107. CCLOGINFO("deallocing GLProgramCache: %p", this);
  108. }
  109. bool GLProgramCache::init()
  110. {
  111. loadDefaultGLPrograms();
  112. auto listener = EventListenerCustom::create(Configuration::CONFIG_FILE_LOADED, [this](EventCustom* /*event*/){
  113. reloadDefaultGLProgramsRelativeToLights();
  114. });
  115. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, -1);
  116. return true;
  117. }
  118. void GLProgramCache::loadDefaultGLPrograms()
  119. {
  120. // Position Texture Color shader
  121. GLProgram *p = new (std::nothrow) GLProgram();
  122. loadDefaultGLProgram(p, kShaderType_PositionTextureColor);
  123. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, p);
  124. // Position Texture Color without MVP shader
  125. p = new (std::nothrow) GLProgram();
  126. loadDefaultGLProgram(p, kShaderType_PositionTextureColor_noMVP);
  127. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, p);
  128. // Position Texture Color alpha test
  129. p = new (std::nothrow) GLProgram();
  130. loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTest);
  131. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST, p);
  132. // Position Texture Color alpha test
  133. p = new (std::nothrow) GLProgram();
  134. loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTestNoMV);
  135. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV, p);
  136. //
  137. // Position, Color shader
  138. //
  139. p = new (std::nothrow) GLProgram();
  140. loadDefaultGLProgram(p, kShaderType_PositionColor);
  141. _programs.emplace(GLProgram::SHADER_NAME_POSITION_COLOR, p);
  142. // Position, Color, PointSize shader
  143. p = new (std::nothrow) GLProgram();
  144. loadDefaultGLProgram(p, kShaderType_PositionColorTextureAsPointsize);
  145. _programs.emplace(GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE, p);
  146. //
  147. // Position, Color shader no MVP
  148. //
  149. p = new (std::nothrow) GLProgram();
  150. loadDefaultGLProgram(p, kShaderType_PositionColor_noMVP);
  151. _programs.emplace(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP, p);
  152. //
  153. // Position Texture shader
  154. //
  155. p = new (std::nothrow) GLProgram();
  156. loadDefaultGLProgram(p, kShaderType_PositionTexture);
  157. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE, p);
  158. //
  159. // Position, Texture attribs, 1 Color as uniform shader
  160. //
  161. p = new (std::nothrow) GLProgram();
  162. loadDefaultGLProgram(p, kShaderType_PositionTexture_uColor);
  163. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR, p);
  164. //
  165. // Position Texture A8 Color shader
  166. //
  167. p = new (std::nothrow) GLProgram();
  168. loadDefaultGLProgram(p, kShaderType_PositionTextureA8Color);
  169. _programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR, p);
  170. //
  171. // Position and 1 color passed as a uniform (to simulate glColor4ub )
  172. //
  173. p = new (std::nothrow) GLProgram();
  174. loadDefaultGLProgram(p, kShaderType_Position_uColor);
  175. _programs.emplace(GLProgram::SHADER_NAME_POSITION_U_COLOR, p);
  176. //
  177. // Position, Length(TexCoords, Color (used by Draw Node basically )
  178. //
  179. p = new (std::nothrow) GLProgram();
  180. loadDefaultGLProgram(p, kShaderType_PositionLengthTextureColor);
  181. _programs.emplace(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR, p);
  182. p = new (std::nothrow) GLProgram();
  183. loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldNormal);
  184. _programs.emplace(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL, p);
  185. p = new (std::nothrow) GLProgram();
  186. loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldGlow);
  187. _programs.emplace(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW, p);
  188. p = new (std::nothrow) GLProgram();
  189. loadDefaultGLProgram(p, kShaderType_UIGrayScale);
  190. _programs.emplace(GLProgram::SHADER_NAME_POSITION_GRAYSCALE, p);
  191. p = new (std::nothrow) GLProgram();
  192. loadDefaultGLProgram(p, kShaderType_LabelNormal);
  193. _programs.emplace(GLProgram::SHADER_NAME_LABEL_NORMAL, p);
  194. p = new (std::nothrow) GLProgram();
  195. loadDefaultGLProgram(p, kShaderType_LabelOutline);
  196. _programs.emplace(GLProgram::SHADER_NAME_LABEL_OUTLINE, p);
  197. p = new (std::nothrow) GLProgram();
  198. loadDefaultGLProgram(p, kShaderType_3DPosition);
  199. _programs.emplace(GLProgram::SHADER_3D_POSITION, p);
  200. p = new (std::nothrow) GLProgram();
  201. loadDefaultGLProgram(p, kShaderType_3DPositionTex);
  202. _programs.emplace(GLProgram::SHADER_3D_POSITION_TEXTURE, p);
  203. p = new (std::nothrow) GLProgram();
  204. loadDefaultGLProgram(p, kShaderType_3DSkinPositionTex);
  205. _programs.emplace(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE, p);
  206. p = new (std::nothrow) GLProgram();
  207. loadDefaultGLProgram(p, kShaderType_3DPositionNormal);
  208. _programs.emplace(GLProgram::SHADER_3D_POSITION_NORMAL, p);
  209. p = new (std::nothrow) GLProgram();
  210. loadDefaultGLProgram(p, kShaderType_3DPositionNormalTex);
  211. _programs.emplace(GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE, p);
  212. p = new (std::nothrow) GLProgram();
  213. loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
  214. _programs.emplace(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE, p);
  215. p = new (std::nothrow) GLProgram();
  216. loadDefaultGLProgram(p, kShaderType_3DPositionBumpedNormalTex);
  217. _programs.emplace(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE, p);
  218. p = new (std::nothrow) GLProgram();
  219. loadDefaultGLProgram(p, kShaderType_3DSkinPositionBumpedNormalTex);
  220. _programs.emplace(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE, p);
  221. p = new (std::nothrow) GLProgram();
  222. loadDefaultGLProgram(p, kShaderType_3DParticleColor);
  223. _programs.emplace(GLProgram::SHADER_3D_PARTICLE_COLOR, p);
  224. p = new (std::nothrow) GLProgram();
  225. loadDefaultGLProgram(p, kShaderType_3DParticleTex);
  226. _programs.emplace(GLProgram::SHADER_3D_PARTICLE_TEXTURE, p);
  227. p = new (std::nothrow) GLProgram();
  228. loadDefaultGLProgram(p, kShaderType_3DSkyBox);
  229. _programs.emplace(GLProgram::SHADER_3D_SKYBOX, p);
  230. p = new (std::nothrow) GLProgram();
  231. loadDefaultGLProgram(p, kShaderType_3DTerrain);
  232. _programs.emplace(GLProgram::SHADER_3D_TERRAIN, p);
  233. p = new (std::nothrow) GLProgram();
  234. loadDefaultGLProgram(p, kShaderType_CameraClear);
  235. _programs.emplace(GLProgram::SHADER_CAMERA_CLEAR, p);
  236. /// ETC1 ALPHA supports.
  237. p = new(std::nothrow) GLProgram();
  238. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureColor);
  239. _programs.emplace(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR, p);
  240. p = new(std::nothrow) GLProgram();
  241. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureColor_noMVP);
  242. _programs.emplace(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP, p);
  243. /// ETC1 Gray supports.
  244. p = new(std::nothrow) GLProgram();
  245. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureGray);
  246. _programs.emplace(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY, p);
  247. p = new(std::nothrow) GLProgram();
  248. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureGray_noMVP);
  249. _programs.emplace(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP, p);
  250. p = new(std::nothrow) GLProgram();
  251. loadDefaultGLProgram(p, kShaderType_LayerRadialGradient);
  252. _programs.emplace(GLProgram::SHADER_LAYER_RADIAL_GRADIENT, p);
  253. }
  254. void GLProgramCache::reloadDefaultGLPrograms()
  255. {
  256. // reset all programs and reload them
  257. // Position Texture Color shader
  258. GLProgram *p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
  259. p->reset();
  260. loadDefaultGLProgram(p, kShaderType_PositionTextureColor);
  261. // Position Texture Color without MVP shader
  262. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP);
  263. p->reset();
  264. loadDefaultGLProgram(p, kShaderType_PositionTextureColor_noMVP);
  265. // Position Texture Color alpha test
  266. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST);
  267. p->reset();
  268. loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTest);
  269. // Position Texture Color alpha test
  270. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
  271. p->reset();
  272. loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTestNoMV);
  273. //
  274. // Position, Color shader
  275. //
  276. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR);
  277. p->reset();
  278. loadDefaultGLProgram(p, kShaderType_PositionColor);
  279. // Position, Color, PointSize shader
  280. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE);
  281. p->reset();
  282. loadDefaultGLProgram(p, kShaderType_PositionColorTextureAsPointsize);
  283. //
  284. // Position, Color shader no MVP
  285. //
  286. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP);
  287. loadDefaultGLProgram(p, kShaderType_PositionColor_noMVP);
  288. //
  289. // Position Texture shader
  290. //
  291. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
  292. p->reset();
  293. loadDefaultGLProgram(p, kShaderType_PositionTexture);
  294. //
  295. // Position, Texture attribs, 1 Color as uniform shader
  296. //
  297. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR);
  298. p->reset();
  299. loadDefaultGLProgram(p, kShaderType_PositionTexture_uColor);
  300. //
  301. // Position Texture A8 Color shader
  302. //
  303. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR);
  304. p->reset();
  305. loadDefaultGLProgram(p, kShaderType_PositionTextureA8Color);
  306. //
  307. // Position and 1 color passed as a uniform (to simulate glColor4ub )
  308. //
  309. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
  310. p->reset();
  311. loadDefaultGLProgram(p, kShaderType_Position_uColor);
  312. //
  313. // Position, Length(TexCoords, Color (used by Draw Node basically )
  314. //
  315. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
  316. p->reset();
  317. loadDefaultGLProgram(p, kShaderType_PositionLengthTextureColor);
  318. p = getGLProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL);
  319. p->reset();
  320. loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldNormal);
  321. p = getGLProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW);
  322. p->reset();
  323. loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldGlow);
  324. p = getGLProgram(GLProgram::SHADER_NAME_POSITION_GRAYSCALE);
  325. p->reset();
  326. loadDefaultGLProgram(p, kShaderType_UIGrayScale);
  327. p = getGLProgram(GLProgram::SHADER_NAME_LABEL_NORMAL);
  328. p->reset();
  329. loadDefaultGLProgram(p, kShaderType_LabelNormal);
  330. p = getGLProgram(GLProgram::SHADER_NAME_LABEL_OUTLINE);
  331. p->reset();
  332. loadDefaultGLProgram(p, kShaderType_LabelOutline);
  333. p = getGLProgram(GLProgram::SHADER_3D_POSITION);
  334. p->reset();
  335. loadDefaultGLProgram(p, kShaderType_3DPosition);
  336. p = getGLProgram(GLProgram::SHADER_3D_POSITION_TEXTURE);
  337. p->reset();
  338. loadDefaultGLProgram(p, kShaderType_3DPositionTex);
  339. p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE);
  340. p->reset();
  341. loadDefaultGLProgram(p, kShaderType_3DSkinPositionTex);
  342. p = getGLProgram(GLProgram::SHADER_3D_POSITION_NORMAL);
  343. p->reset();
  344. loadDefaultGLProgram(p, kShaderType_3DPositionNormal);
  345. p = getGLProgram(GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE);
  346. p->reset();
  347. loadDefaultGLProgram(p, kShaderType_3DPositionNormalTex);
  348. p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE);
  349. p->reset();
  350. loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
  351. p = getGLProgram(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE);
  352. p->reset();
  353. loadDefaultGLProgram(p, kShaderType_3DPositionBumpedNormalTex);
  354. p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE);
  355. p->reset();
  356. loadDefaultGLProgram(p, kShaderType_3DSkinPositionBumpedNormalTex);
  357. p = getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
  358. p->reset();
  359. loadDefaultGLProgram(p, kShaderType_3DParticleTex);
  360. p = getGLProgram(GLProgram::SHADER_3D_PARTICLE_COLOR);
  361. p->reset();
  362. loadDefaultGLProgram(p, kShaderType_3DParticleColor);
  363. p = getGLProgram(GLProgram::SHADER_3D_SKYBOX);
  364. p->reset();
  365. loadDefaultGLProgram(p, kShaderType_3DSkyBox);
  366. p = getGLProgram(GLProgram::SHADER_3D_TERRAIN);
  367. p->reset();
  368. loadDefaultGLProgram(p, kShaderType_3DTerrain);
  369. p = getGLProgram(GLProgram::SHADER_CAMERA_CLEAR);
  370. p->reset();
  371. loadDefaultGLProgram(p, kShaderType_CameraClear);
  372. // ETC1 ALPHA supports.
  373. p = getGLProgram(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR);
  374. p->reset();
  375. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureColor);
  376. p = getGLProgram(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP);
  377. p->reset();
  378. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureColor_noMVP);
  379. // ETC1 Gray supports.
  380. p = getGLProgram(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY);
  381. p->reset();
  382. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureGray);
  383. p = getGLProgram(GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP);
  384. p->reset();
  385. loadDefaultGLProgram(p, kShaderType_ETC1ASPositionTextureGray_noMVP);
  386. p = getGLProgram(GLProgram::SHADER_LAYER_RADIAL_GRADIENT);
  387. loadDefaultGLProgram(p, kShaderType_LayerRadialGradient);
  388. _programs.emplace(GLProgram::SHADER_LAYER_RADIAL_GRADIENT, p);
  389. }
  390. void GLProgramCache::reloadDefaultGLProgramsRelativeToLights()
  391. {
  392. GLProgram *p = getGLProgram(GLProgram::SHADER_3D_POSITION_NORMAL);
  393. p->reset();
  394. loadDefaultGLProgram(p, kShaderType_3DPositionNormal);
  395. p = getGLProgram(GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE);
  396. p->reset();
  397. loadDefaultGLProgram(p, kShaderType_3DPositionNormalTex);
  398. p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE);
  399. p->reset();
  400. loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
  401. p = getGLProgram(GLProgram::SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE);
  402. p->reset();
  403. loadDefaultGLProgram(p, kShaderType_3DPositionBumpedNormalTex);
  404. p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE);
  405. p->reset();
  406. loadDefaultGLProgram(p, kShaderType_3DSkinPositionBumpedNormalTex);
  407. }
  408. void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
  409. {
  410. switch (type) {
  411. case kShaderType_PositionTextureColor:
  412. p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag);
  413. break;
  414. case kShaderType_PositionTextureColor_noMVP:
  415. p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag);
  416. break;
  417. case kShaderType_PositionTextureColorAlphaTest:
  418. p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
  419. break;
  420. case kShaderType_PositionTextureColorAlphaTestNoMV:
  421. p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag);
  422. break;
  423. case kShaderType_PositionColor:
  424. p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag);
  425. break;
  426. case kShaderType_PositionColorTextureAsPointsize:
  427. p->initWithByteArrays(ccPositionColorTextureAsPointsize_vert ,ccPositionColor_frag);
  428. break;
  429. case kShaderType_PositionColor_noMVP:
  430. p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag);
  431. break;
  432. case kShaderType_PositionTexture:
  433. p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag);
  434. break;
  435. case kShaderType_PositionTexture_uColor:
  436. p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag);
  437. break;
  438. case kShaderType_PositionTextureA8Color:
  439. p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag);
  440. break;
  441. case kShaderType_Position_uColor:
  442. p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag);
  443. p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
  444. break;
  445. case kShaderType_PositionLengthTextureColor:
  446. p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag);
  447. break;
  448. case kShaderType_LabelDistanceFieldNormal:
  449. p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag);
  450. break;
  451. case kShaderType_LabelDistanceFieldGlow:
  452. p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag);
  453. break;
  454. case kShaderType_UIGrayScale:
  455. p->initWithByteArrays(ccPositionTextureColor_noMVP_vert,
  456. ccPositionTexture_GrayScale_frag);
  457. break;
  458. case kShaderType_LabelNormal:
  459. p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag);
  460. break;
  461. case kShaderType_LabelOutline:
  462. p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag);
  463. break;
  464. case kShaderType_3DPosition:
  465. p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_Color_frag);
  466. break;
  467. case kShaderType_3DPositionTex:
  468. p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_ColorTex_frag);
  469. break;
  470. case kShaderType_3DSkinPositionTex:
  471. p->initWithByteArrays(cc3D_SkinPositionTex_vert, cc3D_ColorTex_frag);
  472. break;
  473. case kShaderType_3DPositionNormal:
  474. {
  475. std::string def = getShaderMacrosForLight();
  476. p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormal_frag)).c_str());
  477. }
  478. break;
  479. case kShaderType_3DPositionNormalTex:
  480. {
  481. std::string def = getShaderMacrosForLight();
  482. p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
  483. }
  484. break;
  485. case kShaderType_3DSkinPositionNormalTex:
  486. {
  487. std::string def = getShaderMacrosForLight();
  488. p->initWithByteArrays((def + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
  489. }
  490. break;
  491. case kShaderType_3DPositionBumpedNormalTex:
  492. {
  493. std::string def = getShaderMacrosForLight();
  494. std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
  495. p->initWithByteArrays((def + normalMapDef + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + normalMapDef + std::string(cc3D_ColorNormalTex_frag)).c_str());
  496. }
  497. break;
  498. case kShaderType_3DSkinPositionBumpedNormalTex:
  499. {
  500. std::string def = getShaderMacrosForLight();
  501. std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
  502. p->initWithByteArrays((def + normalMapDef + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + normalMapDef + std::string(cc3D_ColorNormalTex_frag)).c_str());
  503. }
  504. break;
  505. case kShaderType_3DParticleTex:
  506. {
  507. p->initWithByteArrays(cc3D_Particle_vert, cc3D_Particle_tex_frag);
  508. }
  509. break;
  510. case kShaderType_3DParticleColor:
  511. p->initWithByteArrays(cc3D_Particle_vert, cc3D_Particle_color_frag);
  512. break;
  513. case kShaderType_3DSkyBox:
  514. p->initWithByteArrays(cc3D_Skybox_vert, cc3D_Skybox_frag);
  515. break;
  516. case kShaderType_3DTerrain:
  517. p->initWithByteArrays(cc3D_Terrain_vert, cc3D_Terrain_frag);
  518. break;
  519. case kShaderType_CameraClear:
  520. p->initWithByteArrays(ccCameraClearVert, ccCameraClearFrag);
  521. break;
  522. /// ETC1 ALPHA supports.
  523. case kShaderType_ETC1ASPositionTextureColor:
  524. p->initWithByteArrays(ccPositionTextureColor_vert, ccETC1ASPositionTextureColor_frag);
  525. break;
  526. case kShaderType_ETC1ASPositionTextureColor_noMVP:
  527. p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccETC1ASPositionTextureColor_frag);
  528. break;
  529. /// ETC1 GRAY supports.
  530. case kShaderType_ETC1ASPositionTextureGray:
  531. p->initWithByteArrays(ccPositionTextureColor_vert, ccETC1ASPositionTextureGray_frag);
  532. break;
  533. case kShaderType_ETC1ASPositionTextureGray_noMVP:
  534. p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccETC1ASPositionTextureGray_frag);
  535. break;
  536. case kShaderType_LayerRadialGradient:
  537. p->initWithByteArrays(ccPosition_vert, ccShader_LayerRadialGradient_frag);
  538. break;
  539. default:
  540. CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__);
  541. return;
  542. }
  543. p->link();
  544. p->updateUniforms();
  545. CHECK_GL_ERROR_DEBUG();
  546. }
  547. GLProgram* GLProgramCache::getGLProgram(const std::string &key)
  548. {
  549. auto it = _programs.find(key);
  550. if( it != _programs.end() )
  551. return it->second;
  552. return nullptr;
  553. }
  554. void GLProgramCache::addGLProgram(GLProgram* program, const std::string &key)
  555. {
  556. // release old one
  557. auto prev = getGLProgram(key);
  558. if( prev == program )
  559. return;
  560. _programs.erase(key);
  561. CC_SAFE_RELEASE_NULL(prev);
  562. if (program)
  563. program->retain();
  564. _programs[key] = program;
  565. }
  566. std::string GLProgramCache::getShaderMacrosForLight() const
  567. {
  568. GLchar def[256];
  569. auto conf = Configuration::getInstance();
  570. snprintf(def, sizeof(def)-1, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
  571. "\n#define MAX_POINT_LIGHT_NUM %d \n"
  572. "\n#define MAX_SPOT_LIGHT_NUM %d \n",
  573. conf->getMaxSupportDirLightInShader(),
  574. conf->getMaxSupportPointLightInShader(),
  575. conf->getMaxSupportSpotLightInShader());
  576. return std::string(def);
  577. }
  578. NS_CC_END