CCGLProgram.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /****************************************************************************
  2. Copyright 2011 Jeff Lamarche
  3. Copyright 2012 Goffredo Marocchi
  4. Copyright 2012 Ricardo Quesada
  5. Copyright 2012 cocos2d-x.org
  6. Copyright (c) 2013-2016 Chukong Technologies Inc.
  7. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  8. http://www.cocos2d-x.org
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. ****************************************************************************/
  25. #ifndef __CCGLPROGRAM_H__
  26. #define __CCGLPROGRAM_H__
  27. #include <unordered_map>
  28. #include <string>
  29. #include "base/ccMacros.h"
  30. #include "base/CCRef.h"
  31. #include "base/ccTypes.h"
  32. #include "platform/CCGL.h"
  33. #include "math/CCMath.h"
  34. /**
  35. * @addtogroup renderer
  36. * @{
  37. */
  38. NS_CC_BEGIN
  39. class GLProgram;
  40. class Director;
  41. //FIXME: these two typedefs would be deprecated or removed in version 4.0.
  42. typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params);
  43. typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
  44. /**VertexAttrib is a structure to encapsulate data got from glGetActiveAttrib.*/
  45. struct VertexAttrib
  46. {
  47. /**Index of attribute, start from 0.*/
  48. GLuint index;
  49. /**Number of Data type in the attribute, could range from 0-4.*/
  50. GLint size;
  51. /**Data type of the attribute, could be GL_FLOAT, GL_UNSIGNED_BYTE etc.*/
  52. GLenum type;
  53. /**The string name in vertex shader.*/
  54. std::string name;
  55. };
  56. /**Uniform is a structure to encapsulate data got from glGetActiveUniform and glGetUniformLocation.*/
  57. struct Uniform
  58. {
  59. /**The place where the uniform placed, starts from 0.*/
  60. GLint location;
  61. /**Number of data type in attribute.*/
  62. GLint size;
  63. /**Data type of the attribute.*/
  64. GLenum type;
  65. /**String of the uniform name.*/
  66. std::string name;
  67. };
  68. /** GLProgram
  69. Class that implements a glProgram
  70. @since v2.0.0
  71. */
  72. class CC_DLL GLProgram : public Ref
  73. {
  74. friend class GLProgramState;
  75. friend class VertexAttribBinding;
  76. public:
  77. /**Enum the preallocated vertex attribute. */
  78. enum
  79. {
  80. /**Index 0 will be used as Position.*/
  81. VERTEX_ATTRIB_POSITION,
  82. /**Index 1 will be used as Color.*/
  83. VERTEX_ATTRIB_COLOR,
  84. /**Index 2 will be used as Tex coord unit 0.*/
  85. VERTEX_ATTRIB_TEX_COORD,
  86. /**Index 3 will be used as Tex coord unit 1.*/
  87. VERTEX_ATTRIB_TEX_COORD1,
  88. /**Index 4 will be used as Tex coord unit 2.*/
  89. VERTEX_ATTRIB_TEX_COORD2,
  90. /**Index 5 will be used as Tex coord unit 3.*/
  91. VERTEX_ATTRIB_TEX_COORD3,
  92. /**Index 6 will be used as Normal.*/
  93. VERTEX_ATTRIB_NORMAL,
  94. /**Index 7 will be used as Blend weight for hardware skin.*/
  95. VERTEX_ATTRIB_BLEND_WEIGHT,
  96. /**Index 8 will be used as Blend index.*/
  97. VERTEX_ATTRIB_BLEND_INDEX,
  98. /**Index 9 will be used as tangent.*/
  99. VERTEX_ATTRIB_TANGENT,
  100. /**Index 10 will be used as Binormal.*/
  101. VERTEX_ATTRIB_BINORMAL,
  102. VERTEX_ATTRIB_MAX,
  103. // backward compatibility
  104. VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
  105. };
  106. /**Preallocated uniform handle.*/
  107. enum
  108. {
  109. /**Ambient color.*/
  110. UNIFORM_AMBIENT_COLOR,
  111. /**Projection matrix.*/
  112. UNIFORM_P_MATRIX,
  113. /**MultiView Projection matrix.*/
  114. UNIFORM_MULTIVIEW_P_MATRIX,
  115. /**Model view matrix.*/
  116. UNIFORM_MV_MATRIX,
  117. /**Model view projection matrix.*/
  118. UNIFORM_MVP_MATRIX,
  119. /**MultiView Model view projection matrix.*/
  120. UNIFORM_MULTIVIEW_MVP_MATRIX,
  121. /**Normal matrix.*/
  122. UNIFORM_NORMAL_MATRIX,
  123. /**Time.*/
  124. UNIFORM_TIME,
  125. /**sin(Time).*/
  126. UNIFORM_SIN_TIME,
  127. /**cos(Time).*/
  128. UNIFORM_COS_TIME,
  129. /**Random number.*/
  130. UNIFORM_RANDOM01,
  131. /** @{
  132. * Sampler 0-3, used for texture.
  133. */
  134. UNIFORM_SAMPLER0,
  135. UNIFORM_SAMPLER1,
  136. UNIFORM_SAMPLER2,
  137. UNIFORM_SAMPLER3,
  138. /**@}*/
  139. UNIFORM_MAX,
  140. };
  141. /** Flags used by the uniforms */
  142. struct UniformFlags {
  143. unsigned int usesTime:1;
  144. unsigned int usesNormal:1;
  145. unsigned int usesMVP:1;
  146. unsigned int usesMultiViewMVP:1;
  147. unsigned int usesMV:1;
  148. unsigned int usesP:1;
  149. unsigned int usesMultiViewP:1;
  150. unsigned int usesRandom:1;
  151. // handy way to initialize the bitfield
  152. UniformFlags() { memset(this, 0, sizeof(*this)); }
  153. };
  154. /**
  155. @name Built Shader types
  156. @{
  157. */
  158. /** ETC1 ALPHA supports for 2d */
  159. static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR;
  160. static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP;
  161. static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY;
  162. static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP;
  163. /**Built in shader for 2d. Support Position, Texture and Color vertex attribute.*/
  164. static const char* SHADER_NAME_POSITION_TEXTURE_COLOR;
  165. /**Built in shader for 2d. Support Position, Texture and Color vertex attribute, but without multiply vertex by MVP matrix.*/
  166. static const char* SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP;
  167. /**Built in shader for 2d. Support Position, Texture vertex attribute, but include alpha test.*/
  168. static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST;
  169. /**Built in shader for 2d. Support Position, Texture and Color vertex attribute, include alpha test and without multiply vertex by MVP matrix.*/
  170. static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV;
  171. /**Built in shader for 2d. Support Position, Color vertex attribute.*/
  172. static const char* SHADER_NAME_POSITION_COLOR;
  173. /**Built in shader for 2d. Support Position, Color, Texture vertex attribute. texture coordinate will used as point size.*/
  174. static const char* SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE;
  175. /**Built in shader for 2d. Support Position, Color vertex attribute, without multiply vertex by MVP matrix.*/
  176. static const char* SHADER_NAME_POSITION_COLOR_NO_MVP;
  177. /**Built in shader for 2d. Support Position, Texture vertex attribute.*/
  178. static const char* SHADER_NAME_POSITION_TEXTURE;
  179. /**Built in shader for 2d. Support Position, Texture vertex attribute. with a specified uniform as color*/
  180. static const char* SHADER_NAME_POSITION_TEXTURE_U_COLOR;
  181. /**Built in shader for 2d. Support Position, Texture and Color vertex attribute. but alpha will be the multiplication of color attribute and texture.*/
  182. static const char* SHADER_NAME_POSITION_TEXTURE_A8_COLOR;
  183. /**Built in shader for 2d. Support Position, with color specified by a uniform.*/
  184. static const char* SHADER_NAME_POSITION_U_COLOR;
  185. /**Built in shader for draw a sector with 90 degrees with center at bottom left point.*/
  186. static const char* SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR;
  187. /**Built in shader for ui effects */
  188. static const char* SHADER_NAME_POSITION_GRAYSCALE;
  189. /** @{
  190. Built in shader for label and label with effects.
  191. */
  192. static const char* SHADER_NAME_LABEL_NORMAL;
  193. static const char* SHADER_NAME_LABEL_OUTLINE;
  194. static const char* SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL;
  195. static const char* SHADER_NAME_LABEL_DISTANCEFIELD_GLOW;
  196. /**Built in shader used for 3D, support Position vertex attribute, with color specified by a uniform.*/
  197. static const char* SHADER_3D_POSITION;
  198. /**Built in shader used for 3D, support Position and Texture vertex attribute, with color specified by a uniform.*/
  199. static const char* SHADER_3D_POSITION_TEXTURE;
  200. /**
  201. Built in shader used for 3D, support Position (Skeletal animation by hardware skin) and Texture vertex attribute,
  202. with color specified by a uniform.
  203. */
  204. static const char* SHADER_3D_SKINPOSITION_TEXTURE;
  205. /**
  206. Built in shader used for 3D, support Position and Normal vertex attribute, used in lighting. with color specified by a uniform.
  207. */
  208. static const char* SHADER_3D_POSITION_NORMAL;
  209. /**
  210. Built in shader used for 3D, support Position, Normal, Texture vertex attribute, used in lighting. with color specified by a uniform.
  211. */
  212. static const char* SHADER_3D_POSITION_NORMAL_TEXTURE;
  213. /**
  214. Built in shader used for 3D, support Position(skeletal animation by hardware skin), Normal, Texture vertex attribute,
  215. used in lighting. with color specified by a uniform.
  216. */
  217. static const char* SHADER_3D_SKINPOSITION_NORMAL_TEXTURE;
  218. /**
  219. Built in shader used for 3D, support Position, Bumped Normal, Texture vertex attribute, used in lighting. with color specified by a uniform.
  220. */
  221. static const char* SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE;
  222. /**
  223. Built in shader used for 3D, support Position(skeletal animation by hardware skin), Bumped Normal, Texture vertex attribute,
  224. used in lighting. with color specified by a uniform.
  225. */
  226. static const char* SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE;
  227. /**
  228. Built in shader for particles, support Position and Texture, with a color specified by a uniform.
  229. */
  230. static const char* SHADER_3D_PARTICLE_TEXTURE;
  231. /**
  232. Built in shader for particles, support Position, with a color specified by a uniform.
  233. */
  234. static const char* SHADER_3D_PARTICLE_COLOR;
  235. /**
  236. Built in shader for skybox
  237. */
  238. static const char* SHADER_3D_SKYBOX;
  239. /**
  240. Built in shader for terrain
  241. */
  242. static const char* SHADER_3D_TERRAIN;
  243. /**
  244. Built in shader for LayerRadialGradient
  245. */
  246. static const char* SHADER_LAYER_RADIAL_GRADIENT;
  247. /**
  248. Built in shader for camera clear
  249. */
  250. static const char* SHADER_CAMERA_CLEAR;
  251. /**
  252. end of built shader types.
  253. @}
  254. */
  255. /**
  256. @name Built uniform names
  257. @{
  258. */
  259. /**Ambient Color uniform.*/
  260. static const char* UNIFORM_NAME_AMBIENT_COLOR;
  261. /**Projection Matrix uniform.*/
  262. static const char* UNIFORM_NAME_P_MATRIX;
  263. /**MultiView Projection Matrix uniform.*/
  264. static const char* UNIFORM_NAME_MULTIVIEW_P_MATRIX;
  265. /**Model view matrix uniform.*/
  266. static const char* UNIFORM_NAME_MV_MATRIX;
  267. /**Model view projection uniform.*/
  268. static const char* UNIFORM_NAME_MVP_MATRIX;
  269. /**MultiView Model view projection uniform.*/
  270. static const char* UNIFORM_NAME_MULTIVIEW_MVP_MATRIX;
  271. /**Normal matrix uniform.*/
  272. static const char* UNIFORM_NAME_NORMAL_MATRIX;
  273. /**Time uniform.*/
  274. static const char* UNIFORM_NAME_TIME;
  275. /**Sin time uniform.*/
  276. static const char* UNIFORM_NAME_SIN_TIME;
  277. /**Cos time uniform.*/
  278. static const char* UNIFORM_NAME_COS_TIME;
  279. /**Random number uniform.*/
  280. static const char* UNIFORM_NAME_RANDOM01;
  281. /**
  282. @{ Sampler uniform 0-3, used for textures.
  283. */
  284. static const char* UNIFORM_NAME_SAMPLER0;
  285. static const char* UNIFORM_NAME_SAMPLER1;
  286. static const char* UNIFORM_NAME_SAMPLER2;
  287. static const char* UNIFORM_NAME_SAMPLER3;
  288. /**
  289. @}
  290. */
  291. /**Alpha test value uniform.*/
  292. static const char* UNIFORM_NAME_ALPHA_TEST_VALUE;
  293. /**
  294. end of Built uniform names
  295. @}
  296. */
  297. /**
  298. @name Built Attribute names
  299. @{
  300. */
  301. /**Attribute color.*/
  302. static const char* ATTRIBUTE_NAME_COLOR;
  303. /**Attribute position.*/
  304. static const char* ATTRIBUTE_NAME_POSITION;
  305. /**@{ Attribute Texcoord 0-3.*/
  306. static const char* ATTRIBUTE_NAME_TEX_COORD;
  307. static const char* ATTRIBUTE_NAME_TEX_COORD1;
  308. static const char* ATTRIBUTE_NAME_TEX_COORD2;
  309. static const char* ATTRIBUTE_NAME_TEX_COORD3;
  310. /**@}*/
  311. /**Attribute normal.*/
  312. static const char* ATTRIBUTE_NAME_NORMAL;
  313. /**Attribute blend weight.*/
  314. static const char* ATTRIBUTE_NAME_BLEND_WEIGHT;
  315. /**Attribute blend index.*/
  316. static const char* ATTRIBUTE_NAME_BLEND_INDEX;
  317. /**Attribute blend tangent.*/
  318. static const char* ATTRIBUTE_NAME_TANGENT;
  319. /**Attribute blend binormal.*/
  320. static const char* ATTRIBUTE_NAME_BINORMAL;
  321. /**
  322. end of Built Attribute names
  323. @}
  324. */
  325. /**Constructor.*/
  326. GLProgram();
  327. /**Destructor.*/
  328. virtual ~GLProgram();
  329. /** @{
  330. Create or Initializes the GLProgram with a vertex and fragment with bytes array.
  331. * @js initWithString.
  332. * @lua initWithString.
  333. */
  334. static GLProgram* createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
  335. bool initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
  336. static GLProgram* createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines);
  337. bool initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines);
  338. /**
  339. @}
  340. */
  341. /** @{
  342. Create or Initializes the GLProgram with a vertex and fragment with bytes array, with shader headers definition(eg. #version ... or #extension ...), with compileTimeDefines(eg. #define ...).
  343. * @js initWithString.
  344. * @lua initWithString.
  345. */
  346. static GLProgram* createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeHeaders, const std::string& compileTimeDefines);
  347. bool initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeHeaders, const std::string& compileTimeDefines);
  348. /**
  349. @}
  350. */
  351. /** @{
  352. Create or Initializes the GLProgram with a vertex and fragment with contents of filenames.
  353. * @js init
  354. * @lua init
  355. */
  356. static GLProgram* createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
  357. bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
  358. static GLProgram* createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeDefines);
  359. bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeDefines);
  360. /**
  361. @}
  362. */
  363. /** @{
  364. Create or Initializes the GLProgram with a vertex and fragment with contents of filenames, with shader headers definition(eg. #version ... or #extension ...), with compileTimeDefines(eg. #define ...).
  365. * @js init
  366. * @lua init
  367. */
  368. static GLProgram* createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeHeaders, const std::string& compileTimeDefines);
  369. bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeHeaders, const std::string& compileTimeDefines);
  370. /**
  371. @}
  372. */
  373. /**@{ Get the uniform or vertex attribute by string name in shader, return null if it does not exist.*/
  374. Uniform* getUniform(const std::string& name);
  375. VertexAttrib* getVertexAttrib(const std::string& name);
  376. /**@}*/
  377. /** It will add a new attribute to the shader by calling glBindAttribLocation. */
  378. void bindAttribLocation(const std::string& attributeName, GLuint index) const;
  379. /** Calls glGetAttribLocation. */
  380. GLint getAttribLocation(const std::string& attributeName) const;
  381. /** Calls glGetUniformLocation(). */
  382. GLint getUniformLocation(const std::string& attributeName) const;
  383. /** links the glProgram */
  384. bool link();
  385. /** it will call glUseProgram() */
  386. void use();
  387. /** It will create 4 uniforms:
  388. - kUniformPMatrix
  389. - kUniformMVMatrix
  390. - kUniformMVPMatrix
  391. - GLProgram::UNIFORM_SAMPLER
  392. And it will bind "GLProgram::UNIFORM_SAMPLER" to 0
  393. */
  394. void updateUniforms();
  395. /** calls retrieves the named uniform location for this shader program. */
  396. GLint getUniformLocationForName(const char* name) const;
  397. /** calls glUniform1i only if the values are different than the previous call for this same shader program.
  398. * @js setUniformLocationI32
  399. * @lua setUniformLocationI32
  400. */
  401. void setUniformLocationWith1i(GLint location, GLint i1);
  402. /** calls glUniform2i only if the values are different than the previous call for this same shader program. */
  403. void setUniformLocationWith2i(GLint location, GLint i1, GLint i2);
  404. /** calls glUniform3i only if the values are different than the previous call for this same shader program. */
  405. void setUniformLocationWith3i(GLint location, GLint i1, GLint i2, GLint i3);
  406. /** calls glUniform4i only if the values are different than the previous call for this same shader program. */
  407. void setUniformLocationWith4i(GLint location, GLint i1, GLint i2, GLint i3, GLint i4);
  408. /** calls glUniform2iv only if the values are different than the previous call for this same shader program. */
  409. void setUniformLocationWith2iv(GLint location, GLint* ints, unsigned int numberOfArrays);
  410. /** calls glUniform3iv only if the values are different than the previous call for this same shader program. */
  411. void setUniformLocationWith3iv(GLint location, GLint* ints, unsigned int numberOfArrays);
  412. /** calls glUniform4iv only if the values are different than the previous call for this same shader program. */
  413. void setUniformLocationWith4iv(GLint location, GLint* ints, unsigned int numberOfArrays);
  414. /** calls glUniform1f only if the values are different than the previous call for this same shader program.
  415. * In js or lua,please use setUniformLocationF32
  416. * @js NA
  417. */
  418. void setUniformLocationWith1f(GLint location, GLfloat f1);
  419. /** calls glUniform2f only if the values are different than the previous call for this same shader program.
  420. * In js or lua,please use setUniformLocationF32
  421. * @js NA
  422. */
  423. void setUniformLocationWith2f(GLint location, GLfloat f1, GLfloat f2);
  424. /** calls glUniform3f only if the values are different than the previous call for this same shader program.
  425. * In js or lua,please use setUniformLocationF32
  426. * @js NA
  427. */
  428. void setUniformLocationWith3f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3);
  429. /** calls glUniform4f only if the values are different than the previous call for this same shader program.
  430. * In js or lua,please use setUniformLocationF32
  431. * @js NA
  432. */
  433. void setUniformLocationWith4f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4);
  434. /** calls glUniformfv only if the values are different than the previous call for this same shader program. */
  435. void setUniformLocationWith1fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
  436. /** calls glUniform2fv only if the values are different than the previous call for this same shader program. */
  437. void setUniformLocationWith2fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
  438. /** calls glUniform3fv only if the values are different than the previous call for this same shader program. */
  439. void setUniformLocationWith3fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
  440. /** calls glUniform4fv only if the values are different than the previous call for this same shader program. */
  441. void setUniformLocationWith4fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
  442. /** calls glUniformMatrix2fv only if the values are different than the previous call for this same shader program. */
  443. void setUniformLocationWithMatrix2fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices);
  444. /** calls glUniformMatrix3fv only if the values are different than the previous call for this same shader program. */
  445. void setUniformLocationWithMatrix3fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices);
  446. /** calls glUniformMatrix4fv only if the values are different than the previous call for this same shader program. */
  447. void setUniformLocationWithMatrix4fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices);
  448. /**
  449. Update the builtin uniforms if they are different than the previous call for this same shader program.
  450. @param modelView modelView matrix applied to the built in uniform of the shader.
  451. */
  452. void setUniformsForBuiltins(const Mat4 &modelView);
  453. /**
  454. Update the builtin uniforms if they are different than the previous call for this same shader program.
  455. */
  456. void setUniformsForBuiltins();
  457. /** returns the vertexShader error log */
  458. std::string getVertexShaderLog() const;
  459. /** returns the fragmentShader error log */
  460. std::string getFragmentShaderLog() const;
  461. /** returns the program error log */
  462. std::string getProgramLog() const;
  463. /** Reload all shaders, this function is designed for android
  464. when opengl context lost, so don't call it.
  465. */
  466. void reset();
  467. /** returns the OpenGL Program object */
  468. GLuint getProgram() const { return _program; }
  469. /** returns the Uniform flags */
  470. const UniformFlags& getUniformFlags() const { return _flags; }
  471. //DEPRECATED
  472. CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderByteArray(const GLchar* vertexByteArray, const GLchar* fragByteArray)
  473. { return initWithByteArrays(vertexByteArray, fragByteArray); }
  474. CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderFilename(const std::string &vertexFilename, const std::string& fragFilename)
  475. { return initWithFilenames(vertexFilename, fragFilename); }
  476. CC_DEPRECATED_ATTRIBUTE void addAttribute(const std::string &attributeName, GLuint index) const { return bindAttribLocation(attributeName, index); }
  477. protected:
  478. /**
  479. Update the uniform data in location.
  480. @param location The location of the uniform.
  481. @param data Updated data.
  482. @param bytes Data length in bytes to update.
  483. */
  484. bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes);
  485. /**Get a general description of the shader.*/
  486. virtual std::string getDescription() const;
  487. /**Bind the predefined vertex attributes to their specific slot.*/
  488. void bindPredefinedVertexAttribs();
  489. /**Parse user defined Vertex Attributes automatically.*/
  490. void parseVertexAttribs();
  491. /**Parse user defined uniform automatically.*/
  492. void parseUniforms();
  493. /**Compile the shader sources.*/
  494. bool compileShader(GLuint * shader, GLenum type, const GLchar* source, const std::string& compileTimeHeaders, const std::string& convertedDefines);
  495. bool compileShader(GLuint * shader, GLenum type, const GLchar* source, const std::string& convertedDefines);
  496. bool compileShader(GLuint * shader, GLenum type, const GLchar* source);
  497. void clearShader();
  498. void clearHashUniforms();
  499. /**OpenGL handle for program.*/
  500. GLuint _program;
  501. /**OpenGL handle for vertex shader.*/
  502. GLuint _vertShader;
  503. /**OpenGL handle for fragment shader.*/
  504. GLuint _fragShader;
  505. /**Built in uniforms.*/
  506. GLint _builtInUniforms[UNIFORM_MAX];
  507. /**Indicate whether it has a offline shader compiler or not.*/
  508. bool _hasShaderCompiler;
  509. /**User defined Uniforms.*/
  510. std::unordered_map<std::string, Uniform> _userUniforms;
  511. /**User defined vertex attributes.*/
  512. std::unordered_map<std::string, VertexAttrib> _vertexAttribs;
  513. /**Hash value of uniforms for quick access.*/
  514. std::unordered_map<GLint, std::pair<GLvoid*, unsigned int>> _hashForUniforms;
  515. //cached director pointer for calling
  516. Director* _director;
  517. /*needed uniforms*/
  518. UniformFlags _flags;
  519. };
  520. NS_CC_END
  521. /**
  522. end of support group
  523. @}
  524. */
  525. #endif /* __CCGLPROGRAM_H__ */