CCShaderCache.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * cc.shaderCache is a singleton object that stores manages GL shaders
  24. * @class
  25. * @name cc.shaderCache
  26. */
  27. cc.shaderCache = /** @lends cc.shaderCache# */{
  28. /**
  29. * @public
  30. * @constant
  31. * @type {Number}
  32. */
  33. TYPE_POSITION_TEXTURECOLOR: 0,
  34. /**
  35. * @public
  36. * @constant
  37. * @type {Number}
  38. */
  39. TYPE_POSITION_TEXTURECOLOR_ALPHATEST: 1,
  40. /**
  41. * @public
  42. * @constant
  43. * @type {Number}
  44. */
  45. TYPE_POSITION_COLOR: 2,
  46. /**
  47. * @public
  48. * @constant
  49. * @type {Number}
  50. */
  51. TYPE_POSITION_TEXTURE: 3,
  52. /**
  53. * @public
  54. * @constant
  55. * @type {Number}
  56. */
  57. TYPE_POSITION_TEXTURE_UCOLOR: 4,
  58. /**
  59. * @public
  60. * @constant
  61. * @type {Number}
  62. */
  63. TYPE_POSITION_TEXTURE_A8COLOR: 5,
  64. /**
  65. * @public
  66. * @constant
  67. * @type {Number}
  68. */
  69. TYPE_POSITION_UCOLOR: 6,
  70. /**
  71. * @public
  72. * @constant
  73. * @type {Number}
  74. */
  75. TYPE_POSITION_LENGTH_TEXTURECOLOR: 7,
  76. /**
  77. * @public
  78. * @constant
  79. * @type {Number}
  80. */
  81. TYPE_MAX: 8,
  82. _programs: {},
  83. _init: function () {
  84. this.loadDefaultShaders();
  85. return true;
  86. },
  87. _loadDefaultShader: function (program, type) {
  88. switch (type) {
  89. case this.TYPE_POSITION_TEXTURECOLOR:
  90. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_TEXTURE_COLOR_VERT, cc.SHADER_POSITION_TEXTURE_COLOR_FRAG);
  91. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  92. program.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
  93. program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
  94. break;
  95. case this.TYPE_POSITION_TEXTURECOLOR_ALPHATEST:
  96. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_TEXTURE_COLOR_VERT, cc.SHADER_POSITION_TEXTURE_COLOR_ALPHATEST_FRAG);
  97. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  98. program.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
  99. program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
  100. break;
  101. case this.TYPE_POSITION_COLOR:
  102. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_COLOR_VERT, cc.SHADER_POSITION_COLOR_FRAG);
  103. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  104. program.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
  105. break;
  106. case this.TYPE_POSITION_TEXTURE:
  107. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_TEXTURE_VERT, cc.SHADER_POSITION_TEXTURE_FRAG);
  108. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  109. program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
  110. break;
  111. case this.TYPE_POSITION_TEXTURE_UCOLOR:
  112. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_TEXTURE_UCOLOR_VERT, cc.SHADER_POSITION_TEXTURE_UCOLOR_FRAG);
  113. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  114. program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
  115. break;
  116. case this.TYPE_POSITION_TEXTURE_A8COLOR:
  117. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_TEXTURE_A8COLOR_VERT, cc.SHADER_POSITION_TEXTURE_A8COLOR_FRAG);
  118. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  119. program.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
  120. program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
  121. break;
  122. case this.TYPE_POSITION_UCOLOR:
  123. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_UCOLOR_VERT, cc.SHADER_POSITION_UCOLOR_FRAG);
  124. program.addAttribute("aVertex", cc.VERTEX_ATTRIB_POSITION);
  125. break;
  126. case this.TYPE_POSITION_LENGTH_TEXTURECOLOR:
  127. program.initWithVertexShaderByteArray(cc.SHADER_POSITION_COLOR_LENGTH_TEXTURE_VERT, cc.SHADER_POSITION_COLOR_LENGTH_TEXTURE_FRAG);
  128. program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
  129. program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
  130. program.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
  131. break;
  132. default:
  133. cc.log("cocos2d: cc.shaderCache._loadDefaultShader, error shader type");
  134. return;
  135. }
  136. program.link();
  137. program.updateUniforms();
  138. //cc.checkGLErrorDebug();
  139. },
  140. /**
  141. * loads the default shaders
  142. */
  143. loadDefaultShaders: function () {
  144. // Position Texture Color shader
  145. var program = new cc.GLProgram();
  146. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURECOLOR);
  147. this._programs[cc.SHADER_POSITION_TEXTURECOLOR] = program;
  148. this._programs["ShaderPositionTextureColor"] = program;
  149. // Position Texture Color alpha test
  150. program = new cc.GLProgram();
  151. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURECOLOR_ALPHATEST);
  152. this._programs[cc.SHADER_POSITION_TEXTURECOLORALPHATEST] = program;
  153. this._programs["ShaderPositionTextureColorAlphaTest"] = program;
  154. //
  155. // Position, Color shader
  156. //
  157. program = new cc.GLProgram();
  158. this._loadDefaultShader(program, this.TYPE_POSITION_COLOR);
  159. this._programs[cc.SHADER_POSITION_COLOR] = program;
  160. this._programs["ShaderPositionColor"] = program;
  161. //
  162. // Position Texture shader
  163. //
  164. program = new cc.GLProgram();
  165. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE);
  166. this._programs[cc.SHADER_POSITION_TEXTURE] = program;
  167. this._programs["ShaderPositionTexture"] = program;
  168. //
  169. // Position, Texture attribs, 1 Color as uniform shader
  170. //
  171. program = new cc.GLProgram();
  172. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE_UCOLOR);
  173. this._programs[cc.SHADER_POSITION_TEXTURE_UCOLOR] = program;
  174. this._programs["ShaderPositionTextureUColor"] = program;
  175. //
  176. // Position Texture A8 Color shader
  177. //
  178. program = new cc.GLProgram();
  179. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE_A8COLOR);
  180. this._programs[cc.SHADER_POSITION_TEXTUREA8COLOR] = program;
  181. this._programs["ShaderPositionTextureA8Color"] = program;
  182. //
  183. // Position and 1 color passed as a uniform (to similate glColor4ub )
  184. //
  185. program = new cc.GLProgram();
  186. this._loadDefaultShader(program, this.TYPE_POSITION_UCOLOR);
  187. this._programs[cc.SHADER_POSITION_UCOLOR] = program;
  188. this._programs["ShaderPositionUColor"] = program;
  189. //
  190. // Position, Legth(TexCoords, Color (used by Draw Node basically )
  191. //
  192. program = new cc.GLProgram();
  193. this._loadDefaultShader(program, this.TYPE_POSITION_LENGTH_TEXTURECOLOR);
  194. this._programs[cc.SHADER_POSITION_LENGTHTEXTURECOLOR] = program;
  195. this._programs["ShaderPositionLengthTextureColor"] = program;
  196. },
  197. /**
  198. * reload the default shaders
  199. */
  200. reloadDefaultShaders: function () {
  201. // reset all programs and reload them
  202. // Position Texture Color shader
  203. var program = this.programForKey(cc.SHADER_POSITION_TEXTURECOLOR);
  204. program.reset();
  205. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURECOLOR);
  206. // Position Texture Color alpha test
  207. program = this.programForKey(cc.SHADER_POSITION_TEXTURECOLORALPHATEST);
  208. program.reset();
  209. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURECOLOR_ALPHATEST);
  210. //
  211. // Position, Color shader
  212. //
  213. program = this.programForKey(cc.SHADER_POSITION_COLOR);
  214. program.reset();
  215. this._loadDefaultShader(program, this.TYPE_POSITION_COLOR);
  216. //
  217. // Position Texture shader
  218. //
  219. program = this.programForKey(cc.SHADER_POSITION_TEXTURE);
  220. program.reset();
  221. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE);
  222. //
  223. // Position, Texture attribs, 1 Color as uniform shader
  224. //
  225. program = this.programForKey(cc.SHADER_POSITION_TEXTURE_UCOLOR);
  226. program.reset();
  227. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE_UCOLOR);
  228. //
  229. // Position Texture A8 Color shader
  230. //
  231. program = this.programForKey(cc.SHADER_POSITION_TEXTUREA8COLOR);
  232. program.reset();
  233. this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE_A8COLOR);
  234. //
  235. // Position and 1 color passed as a uniform (to similate glColor4ub )
  236. //
  237. program = this.programForKey(cc.SHADER_POSITION_UCOLOR);
  238. program.reset();
  239. this._loadDefaultShader(program, this.TYPE_POSITION_UCOLOR);
  240. },
  241. /**
  242. * returns a GL program for a given key
  243. * @param {String} key
  244. */
  245. programForKey: function (key) {
  246. return this._programs[key];
  247. },
  248. /**
  249. * returns a GL program for a shader name
  250. * @param {String} shaderName
  251. * @return {cc.GLProgram}
  252. */
  253. getProgram: function (shaderName) {
  254. return this._programs[shaderName];
  255. },
  256. /**
  257. * adds a CCGLProgram to the cache for a given name
  258. * @param {cc.GLProgram} program
  259. * @param {String} key
  260. */
  261. addProgram: function (program, key) {
  262. this._programs[key] = program;
  263. }
  264. };