cocos构建了一个GLProgram的缓存,毕竟create GL program是一件消耗比较大的工作,且经常存在这种情况:两个绘制工作的shader逻辑一样,只是attribute、uniform的值不同,完全可以直接更新这些变量的值而避免再新建一个Program。

一、数据结构

  1. class CC_DLL GLProgramCache : public Ref
  2. {
  3. // GLProgram的缓存。
  4. // key: 自定义的string,用于唯一辨识。
  5. // value: GLProgram object
  6. std::unordered_map<std::string, GLProgram*> _programs;
  7. };
  1. class CC_DLL GLProgramCache : public Ref
  2. {
  3. public:
  4. void loadDefaultGLPrograms(); // 加载所有内置的shader
  5. void reloadDefaultGLPrograms(); // 重载所有内置的shader
  6. void reloadDefaultGLProgramsRelativeToLights(); // 重载光照相关的内置的shader
  7. // 自定义的GLProgram对象实例都需要手动缓存起来。
  8. // 内置的shader都自动缓存了。
  9. void addGLProgram(GLProgram* program, const std::string &key);
  10. // 获得shader的GLProgram object,这里面已经一些内置的常用Shader了,
  11. GLProgram * getGLProgram(const std::string &key);
  12. }

二、内置Shader

在创建GLProgramCache的时候就加载内置shader的GLProgram Object。

  1. auto glProgramCache = GLProgramCache::getInstance();
  2. // Position Texture Color shader
  3. //
  4. // gl_Position = CC_MVPMatrix * a_position(顶点数据传入的顶点坐标)
  5. // gl_FragColor = texutreColor * a_color(顶点数据传入的color)
  6. //
  7. // kShaderType_PositionTextureColor
  8. // 顶点着色器代码:#include "renderer/ccShader_PositionTextureColor.frag"
  9. // 片段着色器代码:#include "renderer/ccShader_PositionTextureColor.vert"
  10. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR );
  11. // Position Texture Color without MVP shader
  12. //
  13. // gl_Position = CC_PMatrix * a_position
  14. // gl_FragColor = texutreColor * a_color
  15. //
  16. // kShaderType_PositionTextureColor_noMVP
  17. // #include "renderer/ccShader_PositionTextureColor_noMVP.frag"
  18. // #include "renderer/ccShader_PositionTextureColor_noMVP.vert"
  19. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP );
  20. // Position Texture Color alpha test
  21. //
  22. // gl_Position = CC_MVPMatrix * a_position
  23. // gl_FragColor = texutreColor * a_color
  24. //
  25. // kShaderType_PositionTextureColorAlphaTest
  26. // #include "renderer/ccShader_PositionTextureColor.frag"
  27. // #include "renderer/ccShader_PositionTextureColor.vert"
  28. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST );
  29. // Position Texture Color alpha test
  30. //
  31. // gl_Position = CC_PMatrix * a_position
  32. // gl_FragColor = texutreColor * a_color
  33. //
  34. // kShaderType_PositionTextureColorAlphaTestNoMV
  35. // #include "renderer/ccShader_PositionTextureColor_noMVP.frag"
  36. // #include "renderer/ccShader_PositionTextureColor_noMVP.vert"
  37. // loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTestNoMV);
  38. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV );
  39. // Position, Color shader
  40. //
  41. // gl_Position = CC_MVPMatrix * a_position;
  42. // gl_FragColor = a_color;
  43. //
  44. // kShaderType_PositionColor
  45. // #include "renderer/ccShader_PositionColor.frag"
  46. // #include "renderer/ccShader_PositionColor.vert"
  47. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_COLOR );
  48. // Position, Color, PointSize shader
  49. //
  50. // gl_Position = CC_MVPMatrix * a_position;
  51. // gl_PointSize = a_texCoord.x; // 一个顶点的像素大小
  52. // v_fragmentColor = vec4(a_color.rgb * a_color.a * u_alpha, a_color.a * u_alpha);
  53. //
  54. // gl_FragColor = v_fragmentColor;
  55. //
  56. // kShaderType_PositionColorTextureAsPointsize
  57. // #include "renderer/ccShader_PositionColorTextureAsPointsize.vert"
  58. // #include "renderer/ccShader_PositionColor.frag"
  59. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE );
  60. //
  61. // Position, Color shader no MVP
  62. //
  63. // gl_Position = CC_PMatrix * a_position;
  64. // v_fragmentColor = a_color;
  65. //
  66. // gl_FragColor = v_fragmentColor;
  67. //
  68. // kShaderType_PositionColor_noMVP
  69. // #include "renderer/ccShader_PositionTextureColor_noMVP.vert"
  70. // #include "renderer/ccShader_PositionColor.frag"
  71. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP );
  72. //
  73. // Position Texture shader
  74. //
  75. // gl_Position = CC_MVPMatrix * a_position;
  76. //
  77. // gl_FragColor = textureColor;
  78. //
  79. // kShaderType_PositionTexture
  80. // #include "renderer/ccShader_PositionTexture.vert"
  81. // #include "renderer/ccShader_PositionTexture.frag"
  82. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE );
  83. //
  84. // Position, Texture attribs, 1 Color as uniform shader
  85. //
  86. // uniform vec4 u_color;
  87. //
  88. // gl_Position = CC_MVPMatrix * a_position;
  89. //
  90. // gl_FragColor = textureColor * u_color;
  91. //
  92. // kShaderType_PositionTexture_uColor
  93. // #include "renderer/ccShader_PositionTexture_uColor.vert"
  94. // #include "renderer/ccShader_PositionTexture_uColor.frag"
  95. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR );
  96. //
  97. // Position Texture A8 Color shader
  98. //
  99. // gl_Position = CC_MVPMatrix * a_position;
  100. //
  101. // gl_FragColor = vec4( a_color.rgb, a_color.a * textureColor.a );
  102. //
  103. // kShaderType_PositionTextureA8Color
  104. // #include "renderer/ccShader_PositionTextureA8Color.vert"
  105. // #include "renderer/ccShader_PositionTextureA8Color.frag"
  106. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR );
  107. //
  108. // Position and 1 color passed as a uniform (to simulate glColor4ub )
  109. //
  110. // uniform vec4 u_color;
  111. // uniform float u_pointSize;
  112. //
  113. // gl_Position = CC_MVPMatrix * a_position;
  114. // gl_PointSize = u_pointSize;
  115. //
  116. // gl_FragColor = u_color;
  117. //
  118. // kShaderType_Position_uColor
  119. // #include "renderer/ccShader_Position_uColor.vert"
  120. // #include "renderer/ccShader_Position_uColor.frag"
  121. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_U_COLOR );
  122. //
  123. // Position, Length(TexCoords, Color (used by Draw Node basically )
  124. //
  125. // uniform float u_alpha;
  126. //
  127. // gl_Position = CC_MVPMatrix * a_position;
  128. //
  129. // gl_FragColor = vec4(a_color.rgb * a_color.a * u_alpha, a_color.a * u_alpha)
  130. // * step(0.0, 1.0 - length(a_texcoord));
  131. //
  132. // kShaderType_PositionLengthTextureColor
  133. // #include "renderer/ccShader_PositionColorLengthTexture.vert"
  134. // #include "renderer/ccShader_PositionColorLengthTexture.frag"
  135. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR );
  136. //
  137. // uniform vec4 u_textColor;
  138. //
  139. // gl_Position = CC_MVPMatrix * a_position;
  140. //
  141. // float alpha = smoothstep(0.5 - 0.04, 0.5 + 0.04, textureColor.a) * u_textColor.a;
  142. // gl_FragColor = a_color * vec4(u_textColor.rgb, alpha);
  143. //
  144. // kShaderType_LabelDistanceFieldNormal
  145. // #include "renderer/ccShader_Label.vert"
  146. // #include "renderer/ccShader_Label_df.frag"
  147. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL );
  148. //
  149. // uniform vec4 u_effectColor;
  150. // uniform vec4 u_textColor;
  151. //
  152. // gl_Position = CC_MVPMatrix * a_position;
  153. // float alpha = smoothstep(0.5 - 0.04, 0.5 + 0.04, textureColor.a);
  154. //
  155. // // glow
  156. // float mu = smoothstep(0.5, 1.0, sqrt(textureColor.a));
  157. // vec4 color = u_effectColor*(1.0 - alpha) + u_textColor * alpha;
  158. // gl_FragColor = a_color * vec4(color.rgb, max(alpha, mu) * color.a);
  159. //
  160. // kShaderType_LabelDistanceFieldGlow
  161. // #include "renderer/ccShader_Label.vert"
  162. // #include "renderer/ccShader_Label_df_glow.frag"
  163. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW );
  164. //
  165. // gl_Position = CC_MVPMatrix * a_position;
  166. //
  167. // vec4 c = a_color * textureColor;
  168. // gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b);
  169. // gl_FragColor.w = c.w;
  170. //
  171. // kShaderType_UIGrayScale
  172. // #include "renderer/ccShader_PositionTextureColor_noMVP.vert"
  173. // #include "renderer/ccShader_UI_Gray.frag"
  174. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_POSITION_GRAYSCALE );
  175. //
  176. // uniform vec4 u_textColor;
  177. //
  178. // gl_Position = CC_MVPMatrix * a_position;
  179. // gl_FragColor = a_color * vec4(u_textColor.rgb, u_textColor.a * textureColor.a;
  180. //
  181. // kShaderType_LabelNormal
  182. // #include "renderer/ccShader_Label.vert"
  183. // #include "renderer/ccShader_Label_normal.frag"
  184. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_LABEL_NORMAL );
  185. // kShaderType_LabelOutline
  186. // #include "renderer/ccShader_Label.vert"
  187. // #include "renderer/ccShader_Label_outline.frag"
  188. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_LABEL_OUTLINE );
  189. // ETC1 ALPHA supports.
  190. //
  191. // gl_Position = CC_MVPMatrix * a_position;
  192. //
  193. // vec4 texColor = vec4(textureColor.rgb, textureColor1.r);
  194. // texColor.rgb *= texColor.a;
  195. // gl_FragColor = a_color * texColor;
  196. //
  197. // kShaderType_ETC1ASPositionTextureColor
  198. // #include "renderer/ccShader_PositionTextureColor.vert"
  199. // #include "renderer/ccShader_ETC1AS_PositionTextureColor.frag"
  200. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR );
  201. //
  202. // ETC1 ALPHA supports.
  203. //
  204. // gl_Position = CC_PMatrix * a_position;
  205. //
  206. // vec4 texColor = vec4(textureColor.rgb, textureColor1.r);
  207. // texColor.rgb *= texColor.a;
  208. // gl_FragColor = a_color * texColor;
  209. //
  210. // kShaderType_ETC1ASPositionTextureColor_noMVP
  211. // #include "renderer/ccShader_PositionTextureColor_noMVP.vert"
  212. // #include "renderer/ccShader_ETC1AS_PositionTextureColor.frag"
  213. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP );
  214. //
  215. // ETC1 Gray supports.
  216. //
  217. // gl_Position = CC_MVPMatrix * a_position;
  218. //
  219. // vec4 texColor = vec4(textureColor.rgb, textureColor1.r);
  220. // texColor.rgb *= texColor.a;
  221. // texColor = a_color * texColor;
  222. // gl_FragColor.rgb = vec3(0.2126 * texColor.r + 0.7152 * texColor.g + 0.0722 * texColor.b);
  223. // gl_FragColor.a = texColor.a;
  224. //
  225. // kShaderType_ETC1ASPositionTextureGray
  226. // #include "renderer/ccShader_PositionTextureColor.vert"
  227. // #include "renderer/ccShader_ETC1AS_PositionTextureGray.frag"
  228. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY );
  229. //
  230. // ETC1 Gray supports.
  231. //
  232. // gl_Position = CC_PMatrix * a_position;
  233. //
  234. // vec4 texColor = vec4(textureColor.rgb, textureColor1.r);
  235. // texColor.rgb *= texColor.a;
  236. // texColor = a_color * texColor;
  237. // gl_FragColor.rgb = vec3(0.2126 * texColor.r + 0.7152 * texColor.g + 0.0722 * texColor.b);
  238. // gl_FragColor.a = texColor.a;
  239. //
  240. // kShaderType_ETC1ASPositionTextureGray_noMVP
  241. // #include "renderer/ccShader_PositionTextureColor_noMVP.vert"
  242. // #include "renderer/ccShader_ETC1AS_PositionTextureGray.frag"
  243. glProgramCache->getGLProgram( GLProgram::SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP );
  244. //
  245. // uniform vec4 u_startColor;
  246. // uniform vec4 u_endColor;
  247. // uniform vec2 u_center;
  248. // uniform float u_radius;
  249. // uniform float u_expand;
  250. //
  251. // gl_Position = CC_MVPMatrix * a_position;
  252. // gl_FragColor = a_color;
  253. //
  254. // float d = distance(a_position.xy, u_center) / u_radius;
  255. // if (d <= 1.0) {
  256. // if (d <= u_expand) {
  257. // gl_FragColor = u_startColor;
  258. // }
  259. // else {
  260. // gl_FragColor = mix(u_startColor, u_endColor, (d - u_expand) / (1.0 - u_expand));
  261. // }
  262. // }
  263. // else {
  264. // gl_FragColor = vec4( 0.0, 0.0, 0.0, 0.0 );
  265. // }
  266. //
  267. // kShaderType_LayerRadialGradient
  268. // #include "renderer/ccShader_Position.vert"
  269. // #include "renderer/ccShader_LayerRadialGradient.frag"
  270. glProgramCache->getGLProgram( GLProgram::SHADER_LAYER_RADIAL_GRADIENT );
  271. //
  272. // gl_Position = CC_MVPMatrix * a_position;
  273. // gl_FragColor = a_color;
  274. //
  275. // kShaderType_3DPosition
  276. // #include "renderer/ccShader_3D_PositionTex.vert"
  277. // #include "renderer/ccShader_3D_Color.frag"
  278. glProgramCache->getGLProgram( GLProgram::SHADER_3D_POSITION );
  279. //
  280. // gl_Position = CC_MVPMatrix * a_position;
  281. // gl_FragColor = a_color;
  282. //
  283. // kShaderType_3DPositionTex
  284. // #include "renderer/ccShader_3D_PositionTex.vert"
  285. // #include "renderer/ccShader_3D_ColorTex.frag"
  286. glProgramCache->getGLProgram( GLProgram::SHADER_3D_POSITION_TEXTURE );
  287. //
  288. // gl_Position = CC_MVPMatrix * a_position;
  289. // gl_FragColor = a_color;
  290. //
  291. // kShaderType_3DSkinPositionTex
  292. // #include "renderer/ccShader_3D_PositionTex.vert"
  293. // #include "renderer/ccShader_3D_ColorTex.frag"
  294. glProgramCache->getGLProgram( GLProgram::SHADER_3D_SKINPOSITION_TEXTURE );
  295. // loadDefaultGLProgram( p, kShaderType_3DPositionNormal );
  296. glProgramCache->getGLProgram( GLProgram::SHADER_3D_POSITION_NORMAL );
  297. // loadDefaultGLProgram( p, kShaderType_3DPositionNormalTex );
  298. glProgramCache->getGLProgram( GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE );
  299. // loadDefaultGLProgram( p, kShaderType_3DSkinPositionNormalTex );
  300. glProgramCache->getGLProgram( GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE );
  301. // loadDefaultGLProgram( p, kShaderType_3DPositionBumpedNormalTex );
  302. glProgramCache->getGLProgram( GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE );
  303. // loadDefaultGLProgram( p, kShaderType_3DSkinPositionBumpedNormalTex );
  304. glProgramCache->getGLProgram( GLProgram::SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE );
  305. // loadDefaultGLProgram( p, kShaderType_3DParticleColor );
  306. glProgramCache->getGLProgram( GLProgram::SHADER_3D_PARTICLE_COLOR );
  307. // kShaderType_3DParticleTex
  308. // #include "renderer/ccShader_3D_Particle.vert"
  309. // #include "renderer/ccShader_3D_Particle.frag"
  310. glProgramCache->getGLProgram( GLProgram::SHADER_3D_PARTICLE_TEXTURE );
  311. // kShaderType_3DSkyBox
  312. // #include "renderer/ccShader_3D_Skybox.vert"
  313. // #include "renderer/ccShader_3D_Skybox.frag"
  314. glProgramCache->getGLProgram( GLProgram::SHADER_3D_SKYBOX );
  315. // kShaderType_3DTerrain
  316. // #include "renderer/ccShader_3D_Terrain.vert"
  317. // #include "renderer/ccShader_3D_Terrain.frag"
  318. glProgramCache->getGLProgram( GLProgram::SHADER_3D_TERRAIN );
  319. // kShaderType_CameraClear
  320. // #include "renderer/ccShader_CameraClear.vert"
  321. // #include "renderer/ccShader_CameraClear.frag"
  322. glProgramCache->getGLProgram( GLProgram::SHADER_CAMERA_CLEAR );