CCGLProgram.js 25 KB

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