CCGLProgram.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright 2011 Jeff Lamarche
  5. Copyright 2012 Goffredo Marocchi
  6. Copyright (c) 2011 Zynga 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 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. //-------------Vertex Attributes-----------
  25. /**
  26. * @constant
  27. * @type {Number}
  28. */
  29. cc.VERTEX_ATTRIB_POSITION = 0;
  30. /**
  31. * @constant
  32. * @type {Number}
  33. */
  34. cc.VERTEX_ATTRIB_COLOR = 1;
  35. /**
  36. * @constant
  37. * @type {Number}
  38. */
  39. cc.VERTEX_ATTRIB_TEX_COORDS = 2;
  40. /**
  41. * @constant
  42. * @type {Number}
  43. */
  44. cc.VERTEX_ATTRIB_MAX = 3;
  45. //------------Uniforms------------------
  46. /**
  47. * @constant
  48. * @type {Number}
  49. */
  50. cc.UNIFORM_PMATRIX = 0;
  51. /**
  52. * @constant
  53. * @type {Number}
  54. */
  55. cc.UNIFORM_MVMATRIX = 1;
  56. /**
  57. * @constant
  58. * @type {Number}
  59. */
  60. cc.UNIFORM_MVPMATRIX = 2;
  61. /**
  62. * @constant
  63. * @type {Number}
  64. */
  65. cc.UNIFORM_TIME = 3;
  66. /**
  67. * @constant
  68. * @type {Number}
  69. */
  70. cc.UNIFORM_SINTIME = 4;
  71. /**
  72. * @constant
  73. * @type {Number}
  74. */
  75. cc.UNIFORM_COSTIME = 5;
  76. /**
  77. * @constant
  78. * @type {Number}
  79. */
  80. cc.UNIFORM_RANDOM01 = 6;
  81. /**
  82. * @constant
  83. * @type {Number}
  84. */
  85. cc.UNIFORM_SAMPLER = 7;
  86. /**
  87. * @constant
  88. * @type {Number}
  89. */
  90. cc.UNIFORM_MAX = 8;
  91. //------------Shader Name---------------
  92. /**
  93. * @constant
  94. * @type {String}
  95. */
  96. cc.SHADER_POSITION_TEXTURECOLOR = "ShaderPositionTextureColor";
  97. /**
  98. * @constant
  99. * @type {String}
  100. */
  101. cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest";
  102. /**
  103. * @constant
  104. * @type {String}
  105. */
  106. cc.SHADER_POSITION_COLOR = "ShaderPositionColor";
  107. /**
  108. * @constant
  109. * @type {String}
  110. */
  111. cc.SHADER_POSITION_TEXTURE = "ShaderPositionTexture";
  112. /**
  113. * @constant
  114. * @type {String}
  115. */
  116. cc.SHADER_POSITION_TEXTURE_UCOLOR = "ShaderPositionTexture_uColor";
  117. /**
  118. * @constant
  119. * @type {String}
  120. */
  121. cc.SHADER_POSITION_TEXTUREA8COLOR = "ShaderPositionTextureA8Color";
  122. /**
  123. * @constant
  124. * @type {String}
  125. */
  126. cc.SHADER_POSITION_UCOLOR = "ShaderPosition_uColor";
  127. /**
  128. * @constant
  129. * @type {String}
  130. */
  131. cc.SHADER_POSITION_LENGTHTEXTURECOLOR = "ShaderPositionLengthTextureColor";
  132. //------------uniform names----------------
  133. /**
  134. * @constant
  135. * @type {String}
  136. */
  137. cc.UNIFORM_PMATRIX_S = "CC_PMatrix";
  138. /**
  139. * @constant
  140. * @type {String}
  141. */
  142. cc.UNIFORM_MVMATRIX_S = "CC_MVMatrix";
  143. /**
  144. * @constant
  145. * @type {String}
  146. */
  147. cc.UNIFORM_MVPMATRIX_S = "CC_MVPMatrix";
  148. /**
  149. * @constant
  150. * @type {String}
  151. */
  152. cc.UNIFORM_TIME_S = "CC_Time";
  153. /**
  154. * @constant
  155. * @type {String}
  156. */
  157. cc.UNIFORM_SINTIME_S = "CC_SinTime";
  158. /**
  159. * @constant
  160. * @type {String}
  161. */
  162. cc.UNIFORM_COSTIME_S = "CC_CosTime";
  163. /**
  164. * @constant
  165. * @type {String}
  166. */
  167. cc.UNIFORM_RANDOM01_S = "CC_Random01";
  168. /**
  169. * @constant
  170. * @type {String}
  171. */
  172. cc.UNIFORM_SAMPLER_S = "CC_Texture0";
  173. /**
  174. * @constant
  175. * @type {String}
  176. */
  177. cc.UNIFORM_ALPHA_TEST_VALUE_S = "CC_alpha_value";
  178. //------------Attribute names--------------
  179. /**
  180. * @constant
  181. * @type {String}
  182. */
  183. cc.ATTRIBUTE_NAME_COLOR = "a_color";
  184. /**
  185. * @constant
  186. * @type {String}
  187. */
  188. cc.ATTRIBUTE_NAME_POSITION = "a_position";
  189. /**
  190. * @constant
  191. * @type {String}
  192. */
  193. cc.ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
  194. cc.HashUniformEntry = function (value, location, hh) {
  195. this.value = value;
  196. this.location = location;
  197. this.hh = hh || {};
  198. };
  199. /**
  200. * Class that implements a glProgram
  201. * @class
  202. * @extends cc.Class
  203. */
  204. cc.GLProgram = cc.Class.extend({
  205. _glContext: null,
  206. _programObj: null,
  207. _vertShader: null,
  208. _fragShader: null,
  209. _uniforms: null,
  210. _hashForUniforms: null,
  211. _usesTime: false,
  212. // Uniform cache
  213. _updateUniformLocation: function (location, data, bytes) {
  214. if (location == null)
  215. return false;
  216. var updated = true;
  217. var element = null;
  218. for (var i = 0; i < this._hashForUniforms.length; i++)
  219. if (this._hashForUniforms[i].location == location)
  220. element = this._hashForUniforms[i];
  221. if (!element) {
  222. element = new cc.HashUniformEntry();
  223. // key
  224. element.location = location;
  225. // value
  226. element.value = data;
  227. this._hashForUniforms.push(element);
  228. } else {
  229. if (element.value == data)
  230. updated = false;
  231. else
  232. element.value = data;
  233. }
  234. return updated;
  235. },
  236. _description: function () {
  237. return "<CCGLProgram = " + this.toString() + " | Program = " + this._programObj.toString() + ", VertexShader = " +
  238. this._vertShader.toString() + ", FragmentShader = " + this._fragShader.toString() + ">";
  239. },
  240. _compileShader: function (shader, type, source) {
  241. if (!source || !shader)
  242. return false;
  243. //var preStr = (type == this._glContext.VERTEX_SHADER) ? "precision highp float;\n" : "precision mediump float;\n";
  244. source = "precision highp float; \n"
  245. + "uniform mat4 CC_PMatrix; \n"
  246. + "uniform mat4 CC_MVMatrix; \n"
  247. + "uniform mat4 CC_MVPMatrix; \n"
  248. + "uniform vec4 CC_Time; \n"
  249. + "uniform vec4 CC_SinTime; \n"
  250. + "uniform vec4 CC_CosTime; \n"
  251. + "uniform vec4 CC_Random01; \n"
  252. + "//CC INCLUDES END \n \n" + source;
  253. this._glContext.shaderSource(shader, source);
  254. this._glContext.compileShader(shader);
  255. var status = this._glContext.getShaderParameter(shader, this._glContext.COMPILE_STATUS);
  256. if (!status) {
  257. cc.log("cocos2d: ERROR: Failed to compile shader:\n" + this._glContext.getShaderSource(shader));
  258. if (type == this._glContext.VERTEX_SHADER)
  259. cc.log("cocos2d: \n" + this.vertexShaderLog());
  260. else
  261. cc.log("cocos2d: \n" + this.fragmentShaderLog());
  262. }
  263. return ( status == 1 );
  264. },
  265. ctor: function (glContext) {
  266. this._programObj = null;
  267. this._vertShader = null;
  268. this._fragShader = null;
  269. this._uniforms = [];
  270. this._hashForUniforms = [];
  271. this._glContext = glContext || cc.renderContext;
  272. },
  273. destroyProgram: function () {
  274. this._vertShader = null;
  275. this._fragShader = null;
  276. this._uniforms = null;
  277. this._hashForUniforms = null;
  278. this._glContext.deleteProgram(this._programObj);
  279. },
  280. /**
  281. * Initializes the cc.GLProgram with a vertex and fragment with string
  282. * @param {String} vertShaderStr
  283. * @param {String} fragShaderStr
  284. * @return {Boolean}
  285. */
  286. initWithVertexShaderByteArray: function (vertShaderStr, fragShaderStr) {
  287. var locGL = this._glContext;
  288. this._programObj = locGL.createProgram();
  289. //cc.CHECK_GL_ERROR_DEBUG();
  290. this._vertShader = null;
  291. this._fragShader = null;
  292. if (vertShaderStr) {
  293. this._vertShader = locGL.createShader(locGL.VERTEX_SHADER);
  294. if (!this._compileShader(this._vertShader, locGL.VERTEX_SHADER, vertShaderStr)) {
  295. cc.log("cocos2d: ERROR: Failed to compile vertex shader");
  296. }
  297. }
  298. // Create and compile fragment shader
  299. if (fragShaderStr) {
  300. this._fragShader = locGL.createShader(locGL.FRAGMENT_SHADER);
  301. if (!this._compileShader(this._fragShader, locGL.FRAGMENT_SHADER, fragShaderStr)) {
  302. cc.log("cocos2d: ERROR: Failed to compile fragment shader");
  303. }
  304. }
  305. if (this._vertShader)
  306. locGL.attachShader(this._programObj, this._vertShader);
  307. cc.CHECK_GL_ERROR_DEBUG();
  308. if (this._fragShader)
  309. locGL.attachShader(this._programObj, this._fragShader);
  310. this._hashForUniforms.length = 0;
  311. cc.CHECK_GL_ERROR_DEBUG();
  312. return true;
  313. },
  314. /**
  315. * Initializes the cc.GLProgram with a vertex and fragment with string
  316. * @param {String} vertShaderStr
  317. * @param {String} fragShaderStr
  318. * @return {Boolean}
  319. */
  320. initWithString: function (vertShaderStr, fragShaderStr) {
  321. return this.initWithVertexShaderByteArray(vertShaderStr, fragShaderStr);
  322. },
  323. /**
  324. * Initializes the CCGLProgram with a vertex and fragment with contents of filenames
  325. * @param {String} vShaderFilename
  326. * @param {String} fShaderFileName
  327. * @return {Boolean}
  328. */
  329. initWithVertexShaderFilename: function (vShaderFilename, fShaderFileName) {
  330. var fileUtils = cc.FileUtils.getInstance();
  331. var vertexSource = fileUtils.getTextFileData(vShaderFilename);
  332. var fragmentSource = fileUtils.getTextFileData(fShaderFileName);
  333. return this.initWithVertexShaderByteArray(vertexSource, fragmentSource);
  334. },
  335. /**
  336. * Initializes the CCGLProgram with a vertex and fragment with contents of filenames
  337. * @param {String} vShaderFilename
  338. * @param {String} fShaderFileName
  339. * @return {Boolean}
  340. */
  341. init: function (vShaderFilename, fShaderFileName) {
  342. return this.initWithVertexShaderFilename(vShaderFilename, fShaderFileName);
  343. },
  344. /**
  345. * It will add a new attribute to the shader
  346. * @param {String} attributeName
  347. * @param {Number} index
  348. */
  349. addAttribute: function (attributeName, index) {
  350. this._glContext.bindAttribLocation(this._programObj, index, attributeName);
  351. },
  352. /**
  353. * links the glProgram
  354. * @return {Boolean}
  355. */
  356. link: function () {
  357. if(!this._programObj) {
  358. cc.log("cc.GLProgram.link(): Cannot link invalid program");
  359. return false;
  360. }
  361. this._glContext.linkProgram(this._programObj);
  362. if (this._vertShader)
  363. this._glContext.deleteShader(this._vertShader);
  364. if (this._fragShader)
  365. this._glContext.deleteShader(this._fragShader);
  366. this._vertShader = null;
  367. this._fragShader = null;
  368. if (cc.COCOS2D_DEBUG) {
  369. var status = this._glContext.getProgramParameter(this._programObj, this._glContext.LINK_STATUS);
  370. if (!status) {
  371. cc.log("cocos2d: ERROR: Failed to link program: " + this._glContext.getProgramInfoLog(this._programObj));
  372. cc.glDeleteProgram(this._programObj);
  373. this._programObj = null;
  374. return false;
  375. }
  376. }
  377. return true;
  378. },
  379. /**
  380. * it will call glUseProgram()
  381. */
  382. use: function () {
  383. cc.glUseProgram(this._programObj);
  384. },
  385. /**
  386. * It will create 4 uniforms:
  387. * cc.UNIFORM_PMATRIX
  388. * cc.UNIFORM_MVMATRIX
  389. * cc.UNIFORM_MVPMATRIX
  390. * cc.UNIFORM_SAMPLER
  391. */
  392. updateUniforms: function () {
  393. this._uniforms[cc.UNIFORM_PMATRIX] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_PMATRIX_S);
  394. this._uniforms[cc.UNIFORM_MVMATRIX] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_MVMATRIX_S);
  395. this._uniforms[cc.UNIFORM_MVPMATRIX] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_MVPMATRIX_S);
  396. this._uniforms[cc.UNIFORM_TIME] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_TIME_S);
  397. this._uniforms[cc.UNIFORM_SINTIME] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_SINTIME_S);
  398. this._uniforms[cc.UNIFORM_COSTIME] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_COSTIME_S);
  399. this._usesTime = (this._uniforms[cc.UNIFORM_TIME] != null || this._uniforms[cc.UNIFORM_SINTIME] != null || this._uniforms[cc.UNIFORM_COSTIME] != null);
  400. this._uniforms[cc.UNIFORM_RANDOM01] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_RANDOM01_S);
  401. this._uniforms[cc.UNIFORM_SAMPLER] = this._glContext.getUniformLocation(this._programObj, cc.UNIFORM_SAMPLER_S);
  402. this.use();
  403. // Since sample most probably won't change, set it to 0 now.
  404. this.setUniformLocationWith1i(this._uniforms[cc.UNIFORM_SAMPLER], 0);
  405. },
  406. /**
  407. * calls retrieves the named uniform location for this shader program.
  408. * @param {String} name
  409. * @returns {Number}
  410. */
  411. getUniformLocationForName:function(name){
  412. if(!name)
  413. throw "cc.GLProgram.getUniformLocationForName(): uniform name should be non-null";
  414. if(!this._programObj)
  415. throw "cc.GLProgram.getUniformLocationForName(): Invalid operation. Cannot get uniform location when program is not initialized";
  416. return this._glContext.getUniformLocation(this._programObj, name);
  417. },
  418. getUniformMVPMatrix: function () {
  419. return this._uniforms[cc.UNIFORM_MVPMATRIX];
  420. },
  421. getUniformSampler: function () {
  422. return this._uniforms[cc.UNIFORM_SAMPLER];
  423. },
  424. /**
  425. * calls glUniform1i only if the values are different than the previous call for this same shader program.
  426. * @param {WebGLUniformLocation} location
  427. * @param {Number} i1
  428. */
  429. setUniformLocationWith1i: function (location, i1) {
  430. var updated = this._updateUniformLocation(location, i1);
  431. if (updated)
  432. this._glContext.uniform1i(location, i1);
  433. },
  434. /**
  435. * calls glUniform2i only if the values are different than the previous call for this same shader program.
  436. * @param {WebGLUniformLocation} location
  437. * @param {Number} i1
  438. * @param {Number} i2
  439. */
  440. setUniformLocationWith2i:function(location, i1,i2){
  441. var intArray= [i1,i2];
  442. var updated = this._updateUniformLocation(location, intArray);
  443. if( updated )
  444. this._glContext.uniform2i(location, i1, i2);
  445. },
  446. /**
  447. * calls glUniform3i only if the values are different than the previous call for this same shader program.
  448. * @param {WebGLUniformLocation} location
  449. * @param {Number} i1
  450. * @param {Number} i2
  451. * @param {Number} i3
  452. */
  453. setUniformLocationWith3i:function(location, i1, i2, i3){
  454. var intArray = [i1,i2,i3];
  455. var updated = this._updateUniformLocation(location, intArray);
  456. if( updated )
  457. this._glContext.uniform3i(location, i1, i2, i3);
  458. },
  459. /**
  460. * calls glUniform4i only if the values are different than the previous call for this same shader program.
  461. * @param {WebGLUniformLocation} location
  462. * @param {Number} i1
  463. * @param {Number} i2
  464. * @param {Number} i3
  465. * @param {Number} i4
  466. */
  467. setUniformLocationWith4i:function(location, i1, i2, i3, i4){
  468. var intArray = [i1,i2,i3,i4];
  469. var updated = this._updateUniformLocation(location, intArray);
  470. if( updated )
  471. this._glContext.uniform4i(location, i1, i2, i3, i4);
  472. },
  473. /**
  474. * calls glUniform2iv only if the values are different than the previous call for this same shader program.
  475. * @param {WebGLUniformLocation} location
  476. * @param {Int32Array} intArray
  477. * @param {Number} numberOfArrays
  478. */
  479. setUniformLocationWith2iv:function(location, intArray, numberOfArrays){
  480. var updated = this._updateUniformLocation(location, intArray);
  481. if( updated )
  482. this._glContext.uniform2iv(location, intArray);
  483. },
  484. /**
  485. * calls glUniform3iv only if the values are different than the previous call for this same shader program.
  486. * @param {WebGLUniformLocation} location
  487. * @param {Int32Array} intArray
  488. * @param {Number} numberOfArrays
  489. */
  490. setUniformLocationWith3iv:function(location, intArray, numberOfArrays){
  491. var updated = this._updateUniformLocation(location, intArray);
  492. if( updated )
  493. this._glContext.uniform3iv(location, intArray);
  494. },
  495. /**
  496. * calls glUniform4iv only if the values are different than the previous call for this same shader program.
  497. * @param {WebGLUniformLocation} location
  498. * @param {Int32Array} intArray
  499. * @param {Number} numberOfArrays
  500. */
  501. setUniformLocationWith4iv:function(location, intArray, numberOfArrays){
  502. var updated = this._updateUniformLocation(location, intArray);
  503. if( updated )
  504. this._glContext.uniform4iv(location, intArray);
  505. },
  506. /**
  507. * calls glUniform1i only if the values are different than the previous call for this same shader program.
  508. * @param {WebGLUniformLocation} location
  509. * @param {Number} i1
  510. */
  511. setUniformLocationI32: function (location, i1) {
  512. this.setUniformLocationWith1i(arguments[0], arguments[1]);
  513. },
  514. /**
  515. * calls glUniform1f only if the values are different than the previous call for this same shader program.
  516. * @param {WebGLUniformLocation} location
  517. * @param {Number} f1
  518. */
  519. setUniformLocationWith1f: function (location, f1) {
  520. var updated = this._updateUniformLocation(location, f1);
  521. if (updated)
  522. this._glContext.uniform1f(location, f1);
  523. },
  524. /**
  525. * calls glUniform2f only if the values are different than the previous call for this same shader program.
  526. * @param {WebGLUniformLocation} location
  527. * @param {Number} f1
  528. * @param {Number} f2
  529. */
  530. setUniformLocationWith2f: function (location, f1, f2) {
  531. var floats = [f1, f2];
  532. var updated = this._updateUniformLocation(location, floats);
  533. if (updated)
  534. this._glContext.uniform2f(location, f1, f2);
  535. },
  536. /**
  537. * calls glUniform3f only if the values are different than the previous call for this same shader program.
  538. * @param {WebGLUniformLocation} location
  539. * @param {Number} f1
  540. * @param {Number} f2
  541. * @param {Number} f3
  542. */
  543. setUniformLocationWith3f: function (location, f1, f2, f3) {
  544. var floats = [f1, f2, f3];
  545. var updated = this._updateUniformLocation(location, floats);
  546. if (updated)
  547. this._glContext.uniform3f(location, f1, f2, f3);
  548. },
  549. /**
  550. * calls glUniform4f only if the values are different than the previous call for this same shader program.
  551. * @param {WebGLUniformLocation} location
  552. * @param {Number} f1
  553. * @param {Number} f2
  554. * @param {Number} f3
  555. * @param {Number} f4
  556. */
  557. setUniformLocationWith4f: function (location, f1, f2, f3, f4) {
  558. var floats = [f1, f2, f3, f4];
  559. var updated = this._updateUniformLocation(location, floats);
  560. if (updated)
  561. this._glContext.uniform4f(location, f1, f2, f3, f4);
  562. },
  563. /**
  564. * calls glUniform2fv only if the values are different than the previous call for this same shader program.
  565. * @param {WebGLUniformLocation} location
  566. * @param {Float32Array} floatArray
  567. * @param {Number} numberOfArrays
  568. */
  569. setUniformLocationWith2fv: function (location, floatArray, numberOfArrays) {
  570. var updated = this._updateUniformLocation(location, floatArray);
  571. if (updated)
  572. this._glContext.uniform2fv(location, floatArray);
  573. },
  574. /**
  575. * calls glUniform3fv only if the values are different than the previous call for this same shader program.
  576. * @param {WebGLUniformLocation} location
  577. * @param {Float32Array} floatArray
  578. * @param {Number} numberOfArrays
  579. */
  580. setUniformLocationWith3fv: function (location, floatArray, numberOfArrays) {
  581. var updated = this._updateUniformLocation(location, floatArray);
  582. if (updated)
  583. this._glContext.uniform3fv(location, floatArray);
  584. },
  585. /**
  586. * calls glUniform4fv only if the values are different than the previous call for this same shader program.
  587. * @param {WebGLUniformLocation} location
  588. * @param {Float32Array} floatArray
  589. * @param {Number} numberOfArrays
  590. */
  591. setUniformLocationWith4fv: function (location, floatArray, numberOfArrays) {
  592. var updated = this._updateUniformLocation(location, floatArray);
  593. if (updated)
  594. this._glContext.uniform4fv(location, floatArray);
  595. },
  596. /**
  597. * calls glUniformMatrix4fv only if the values are different than the previous call for this same shader program.
  598. * @param {WebGLUniformLocation} location
  599. * @param {Float32Array} matrixArray
  600. * @param {Number} numberOfMatrices
  601. */
  602. setUniformLocationWithMatrix4fv: function (location, matrixArray, numberOfMatrices) {
  603. var updated = this._updateUniformLocation(location, matrixArray);
  604. if (updated)
  605. this._glContext.uniformMatrix4fv(location, false, matrixArray);
  606. },
  607. setUniformLocationF32: function () {
  608. if (arguments.length < 2)
  609. return;
  610. switch (arguments.length) {
  611. case 2:
  612. this.setUniformLocationWith1f(arguments[0], arguments[1]);
  613. break;
  614. case 3:
  615. this.setUniformLocationWith2f(arguments[0], arguments[1], arguments[2]);
  616. break;
  617. case 4:
  618. this.setUniformLocationWith3f(arguments[0], arguments[1], arguments[2], arguments[3]);
  619. break;
  620. case 5:
  621. this.setUniformLocationWith4f(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
  622. break;
  623. }
  624. },
  625. /**
  626. * will update the builtin uniforms if they are different than the previous call for this same shader program.
  627. */
  628. setUniformsForBuiltins: function () {
  629. var matrixP = new cc.kmMat4();
  630. var matrixMV = new cc.kmMat4();
  631. var matrixMVP = new cc.kmMat4();
  632. cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, matrixP);
  633. cc.kmGLGetMatrix(cc.KM_GL_MODELVIEW, matrixMV);
  634. cc.kmMat4Multiply(matrixMVP, matrixP, matrixMV);
  635. this.setUniformLocationWithMatrix4fv(this._uniforms[cc.UNIFORM_PMATRIX], matrixP.mat, 1);
  636. this.setUniformLocationWithMatrix4fv(this._uniforms[cc.UNIFORM_MVMATRIX], matrixMV.mat, 1);
  637. this.setUniformLocationWithMatrix4fv(this._uniforms[cc.UNIFORM_MVPMATRIX], matrixMVP.mat, 1);
  638. if (this._usesTime) {
  639. var director = cc.Director.getInstance();
  640. // This doesn't give the most accurate global time value.
  641. // Cocos2D doesn't store a high precision time value, so this will have to do.
  642. // Getting Mach time per frame per shader using time could be extremely expensive.
  643. var time = director.getTotalFrames() * director.getAnimationInterval();
  644. this.setUniformLocationWith4f(this._uniforms[cc.UNIFORM_TIME], time / 10.0, time, time * 2, time * 4);
  645. this.setUniformLocationWith4f(this._uniforms[cc.UNIFORM_SINTIME], time / 8.0, time / 4.0, time / 2.0, Math.sin(time));
  646. this.setUniformLocationWith4f(this._uniforms[cc.UNIFORM_COSTIME], time / 8.0, time / 4.0, time / 2.0, Math.cos(time));
  647. }
  648. if (this._uniforms[cc.UNIFORM_RANDOM01] != -1)
  649. this.setUniformLocationWith4f(this._uniforms[cc.UNIFORM_RANDOM01], Math.random(), Math.random(), Math.random(), Math.random());
  650. },
  651. /**
  652. * will update the MVP matrix on the MVP uniform if it is different than the previous call for this same shader program.
  653. */
  654. setUniformForModelViewProjectionMatrix: function () {
  655. this._glContext.uniformMatrix4fv(this._uniforms[cc.UNIFORM_MVPMATRIX], false,
  656. cc.getMat4MultiplyValue(cc.projection_matrix_stack.top, cc.modelview_matrix_stack.top));
  657. },
  658. setUniformForModelViewProjectionMatrixWithMat4: function (swapMat4) {
  659. cc.kmMat4Multiply(swapMat4, cc.projection_matrix_stack.top, cc.modelview_matrix_stack.top);
  660. this._glContext.uniformMatrix4fv(this._uniforms[cc.UNIFORM_MVPMATRIX], false, swapMat4.mat);
  661. },
  662. setUniformForModelViewAndProjectionMatrixWithMat4: function () {
  663. this._glContext.uniformMatrix4fv(this._uniforms[cc.UNIFORM_MVMATRIX], false, cc.modelview_matrix_stack.top.mat);
  664. this._glContext.uniformMatrix4fv(this._uniforms[cc.UNIFORM_PMATRIX], false, cc.projection_matrix_stack.top.mat);
  665. },
  666. /**
  667. * returns the vertexShader error log
  668. * @return {String}
  669. */
  670. vertexShaderLog: function () {
  671. return this._glContext.getShaderInfoLog(this._vertShader);
  672. },
  673. /**
  674. * returns the vertexShader error log
  675. * @return {String}
  676. */
  677. getVertexShaderLog: function () {
  678. return this._glContext.getShaderInfoLog(this._vertShader);
  679. },
  680. /**
  681. * returns the fragmentShader error log
  682. * @returns {String}
  683. */
  684. getFragmentShaderLog: function () {
  685. return this._glContext.getShaderInfoLog(this._vertShader);
  686. },
  687. /**
  688. * returns the fragmentShader error log
  689. * @return {String}
  690. */
  691. fragmentShaderLog: function () {
  692. return this._glContext.getShaderInfoLog(this._fragShader);
  693. },
  694. /**
  695. * returns the program error log
  696. * @return {String}
  697. */
  698. programLog: function () {
  699. return this._glContext.getProgramInfoLog(this._programObj);
  700. },
  701. /**
  702. * returns the program error log
  703. * @return {String}
  704. */
  705. getProgramLog: function () {
  706. return this._glContext.getProgramInfoLog(this._programObj);
  707. },
  708. /**
  709. * reload all shaders, this function is designed for android <br/>
  710. * when opengl context lost, so don't call it.
  711. */
  712. reset: function () {
  713. this._vertShader = null;
  714. this._fragShader = null;
  715. this._uniforms.length = 0;
  716. // it is already deallocated by android
  717. //ccGLDeleteProgram(m_uProgram);
  718. this._glContext.deleteProgram(this._programObj);
  719. this._programObj = null;
  720. // Purge uniform hash
  721. for (var i = 0; i < this._hashForUniforms.length; i++) {
  722. this._hashForUniforms[i].value = null;
  723. this._hashForUniforms[i] = null;
  724. }
  725. this._hashForUniforms.length = 0;
  726. },
  727. /**
  728. * get WebGLProgram object
  729. * @return {WebGLProgram}
  730. */
  731. getProgram: function () {
  732. return this._programObj;
  733. },
  734. /**
  735. * Currently JavaScript Bindings (JSB), in some cases, needs to use retain and release. This is a bug in JSB,
  736. * and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB.
  737. * This is a hack, and should be removed once JSB fixes the retain/release bug
  738. */
  739. retain: function () {
  740. },
  741. release: function () {
  742. }
  743. });
  744. /**
  745. * Create a cc.GLProgram object
  746. * @param {String} vShaderFileName
  747. * @param {String} fShaderFileName
  748. * @returns {cc.GLProgram}
  749. */
  750. cc.GLProgram.create = function (vShaderFileName, fShaderFileName) {
  751. var program = new cc.GLProgram();
  752. if (program.init(vShaderFileName, fShaderFileName))
  753. return program;
  754. return null;
  755. };