Program.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "Program.h"
  21. #include "GFXUtils.h"
  22. #include <unordered_map>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. namespace {
  26. uint32_t _genID = 0;
  27. std::string logForOpenGLShader(GLuint shader)
  28. {
  29. GLint logLength = 0;
  30. GL_CHECK(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength));
  31. if (logLength < 1)
  32. return "";
  33. char *logBytes = (char*)malloc(sizeof(char) * logLength);
  34. GL_CHECK(glGetShaderInfoLog(shader, logLength, nullptr, logBytes));
  35. std::string ret(logBytes);
  36. free(logBytes);
  37. return ret;
  38. }
  39. std::string logForOpenGLProgram(GLuint program)
  40. {
  41. GLint logLength = 0;
  42. GL_CHECK(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength));
  43. if (logLength < 1)
  44. return "";
  45. char *logBytes = (char*)malloc(sizeof(char) * logLength);
  46. GL_CHECK(glGetProgramInfoLog(program, logLength, nullptr, logBytes));
  47. std::string ret(logBytes);
  48. free(logBytes);
  49. return ret;
  50. }
  51. bool _createShader(GLenum type, const std::string& src, GLuint* outShader)
  52. {
  53. assert(outShader != nullptr);
  54. GLuint shader = glCreateShader(type);
  55. const GLchar* sources[] = { src.c_str() };
  56. GL_CHECK(glShaderSource(shader, 1, sources, nullptr));
  57. GL_CHECK(glCompileShader(shader));
  58. GLint status;
  59. GL_CHECK(glGetShaderiv(shader, GL_COMPILE_STATUS, &status));
  60. if (!status)
  61. {
  62. GLsizei length;
  63. GL_CHECK(glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &length));
  64. GLchar* source = (GLchar *)malloc(sizeof(GLchar) * length);
  65. GL_CHECK(glGetShaderSource(shader, length, nullptr, source));
  66. RENDERER_LOGE("ERROR: Failed to compile shader:\n%s", source);
  67. std::string shaderLog = logForOpenGLShader(shader);
  68. RENDERER_LOGE("%s", shaderLog.c_str());
  69. free(source);
  70. *outShader = 0;
  71. return false;
  72. }
  73. *outShader = shader;
  74. return true;
  75. }
  76. #define DEF_TO_INT(pointer) (*(int*)(pointer))
  77. #define DEF_TO_FLOAT(pointer) (*(float*)(pointer))
  78. void setUniform1i(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  79. {
  80. assert(count == 1);
  81. if (elementType == cocos2d::renderer::UniformElementType::INT)
  82. {
  83. glUniform1i(location, DEF_TO_INT(value));
  84. }
  85. else
  86. {
  87. float floatVal = *((float*)value);
  88. GLint intVal = (GLint)floatVal;
  89. glUniform1i(location, intVal);
  90. }
  91. }
  92. void setUniform1iv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  93. {
  94. glUniform1iv(location, count, (const GLint*)value);
  95. }
  96. void setUniform2iv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  97. {
  98. glUniform2iv(location, count, (const GLint*)value);
  99. }
  100. void setUniform3iv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  101. {
  102. glUniform3iv(location, count, (const GLint*)value);
  103. }
  104. void setUniform4iv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  105. {
  106. glUniform4iv(location, count, (const GLint*)value);
  107. }
  108. void setUniform1f(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  109. {
  110. assert(count == 1);
  111. glUniform1f(location, DEF_TO_FLOAT(value));
  112. }
  113. void setUniform1fv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  114. {
  115. glUniform1fv(location, count, (const GLfloat*)value);
  116. }
  117. void setUniform2fv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  118. {
  119. glUniform2fv(location, count, (const GLfloat*)value);
  120. }
  121. void setUniform3fv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  122. {
  123. glUniform3fv(location, count, (const GLfloat*)value);
  124. }
  125. void setUniform4fv(GLint location, GLsizei count , const void* value, cocos2d::renderer::UniformElementType elementType)
  126. {
  127. glUniform4fv(location, count, (const GLfloat*)value);
  128. }
  129. void setUniformMatrix2fv(GLint location, GLsizei count, const void *value, cocos2d::renderer::UniformElementType elementType)
  130. {
  131. glUniformMatrix2fv(location, count, GL_FALSE, (const GLfloat*)value);
  132. }
  133. void setUniformMatrix3fv(GLint location, GLsizei count, const void *value, cocos2d::renderer::UniformElementType elementType)
  134. {
  135. glUniformMatrix3fv(location, count, GL_FALSE, (const GLfloat*)value);
  136. }
  137. void setUniformMatrix4fv(GLint location, GLsizei count, const void *value, cocos2d::renderer::UniformElementType elementType)
  138. {
  139. glUniformMatrix4fv(location, count, GL_FALSE, (const GLfloat*)value);
  140. }
  141. /**
  142. * _type2uniformCommit
  143. */
  144. std::unordered_map<GLenum, cocos2d::renderer::Program::Uniform::SetUniformCallback> _type2uniformCommit = {
  145. { GL_INT, setUniform1i },
  146. { GL_FLOAT, setUniform1f },
  147. { GL_FLOAT_VEC2, setUniform2fv },
  148. { GL_FLOAT_VEC3, setUniform3fv },
  149. { GL_FLOAT_VEC4, setUniform4fv },
  150. { GL_INT_VEC2, setUniform2iv },
  151. { GL_INT_VEC3, setUniform3iv },
  152. { GL_INT_VEC4, setUniform4iv },
  153. { GL_BOOL, setUniform1i },
  154. { GL_BOOL_VEC2, setUniform2iv },
  155. { GL_BOOL_VEC3, setUniform3iv },
  156. { GL_BOOL_VEC4, setUniform4iv },
  157. { GL_FLOAT_MAT2, setUniformMatrix2fv },
  158. { GL_FLOAT_MAT3, setUniformMatrix3fv },
  159. { GL_FLOAT_MAT4, setUniformMatrix4fv },
  160. { GL_SAMPLER_2D, setUniform1i },
  161. { GL_SAMPLER_CUBE, setUniform1i }
  162. };
  163. /**
  164. * _type2uniformArrayCommit
  165. */
  166. std::unordered_map<GLenum, cocos2d::renderer::Program::Uniform::SetUniformCallback> _type2uniformArrayCommit = {
  167. { GL_INT, setUniform1iv },
  168. { GL_FLOAT, setUniform1fv },
  169. { GL_FLOAT_VEC2, setUniform2fv },
  170. { GL_FLOAT_VEC3, setUniform3fv },
  171. { GL_FLOAT_VEC4, setUniform4fv },
  172. { GL_INT_VEC2, setUniform2iv },
  173. { GL_INT_VEC3, setUniform3iv },
  174. { GL_INT_VEC4, setUniform4iv },
  175. { GL_BOOL, setUniform1iv },
  176. { GL_BOOL_VEC2, setUniform2iv },
  177. { GL_BOOL_VEC3, setUniform3iv },
  178. { GL_BOOL_VEC4, setUniform4iv },
  179. { GL_FLOAT_MAT2, setUniformMatrix2fv },
  180. { GL_FLOAT_MAT3, setUniformMatrix3fv },
  181. { GL_FLOAT_MAT4, setUniformMatrix4fv },
  182. { GL_SAMPLER_2D, setUniform1iv },
  183. { GL_SAMPLER_CUBE, setUniform1iv }
  184. };
  185. } // namespace {
  186. RENDERER_BEGIN
  187. void Program::Uniform::setUniform(const void* value, UniformElementType elementType, size_t uniformCount) const
  188. {
  189. // uniformCount may bigger than size.
  190. if (size >= 1 && size < uniformCount) uniformCount = size;
  191. GLsizei count = size == -1 ? 1 : (GLsizei)uniformCount;
  192. _callback(location, count, value, elementType);
  193. }
  194. Program::Program()
  195. : _device(nullptr)
  196. , _id(0)
  197. , _linked(false)
  198. {
  199. }
  200. Program::~Program()
  201. {
  202. GL_CHECK(glDeleteProgram(_glID));
  203. }
  204. bool Program::init(DeviceGraphics* device, const char* vertSource, const char* fragSource)
  205. {
  206. assert(device);
  207. assert(vertSource);
  208. assert(fragSource);
  209. _device = device;
  210. _vertSource = vertSource;
  211. _fragSource = fragSource;
  212. _id = _genID++;
  213. _linked = false;
  214. return true;
  215. }
  216. void Program::link()
  217. {
  218. if (_linked) {
  219. return;
  220. }
  221. GLuint vertShader;
  222. bool ok = _createShader(GL_VERTEX_SHADER, _vertSource, &vertShader);
  223. if (!ok)
  224. return;
  225. GLuint fragShader;
  226. ok = _createShader(GL_FRAGMENT_SHADER, _fragSource, &fragShader);
  227. if (!ok)
  228. {
  229. glDeleteShader(vertShader);
  230. return;
  231. }
  232. GLuint program = glCreateProgram();
  233. GL_CHECK(glAttachShader(program, vertShader));
  234. GL_CHECK(glAttachShader(program, fragShader));
  235. GL_CHECK(glLinkProgram(program));
  236. GLint status = GL_TRUE;
  237. GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &status));
  238. if (status == GL_FALSE)
  239. {
  240. RENDERER_LOGE("ERROR: Failed to link program: %u", program);
  241. std::string programLog = logForOpenGLProgram(program);
  242. RENDERER_LOGE("%s", programLog.c_str());
  243. glDeleteShader(vertShader);
  244. glDeleteShader(fragShader);
  245. glDeleteProgram(program);
  246. return;
  247. }
  248. glDeleteShader(vertShader);
  249. glDeleteShader(fragShader);
  250. _glID = program;
  251. // parse attribute
  252. GLint numAttributes;
  253. glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &numAttributes);
  254. if (numAttributes > 0)
  255. {
  256. GLint length;
  257. glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &length);
  258. if (length > 0)
  259. {
  260. GLchar* attribName = (GLchar*) malloc(length + 1);
  261. Attribute attribute;
  262. for (GLint i = 0; i < numAttributes; ++i) {
  263. // Query attribute info.
  264. glGetActiveAttrib(program, i, length, nullptr, &attribute.size, &attribute.type, attribName);
  265. attribName[length] = '\0';
  266. attribute.name = attribName;
  267. attribute.hashName = std::hash<std::string>{}(attribName);
  268. // Query the pre-assigned attribute location
  269. attribute.location = glGetAttribLocation(program, attribName);
  270. _attributes.push_back(std::move(attribute));
  271. }
  272. free(attribName);
  273. }
  274. }
  275. // Query and store uniforms from the program.
  276. GLint activeUniforms;
  277. glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &activeUniforms);
  278. if (activeUniforms > 0)
  279. {
  280. GLint length;
  281. glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length);
  282. if (length > 0)
  283. {
  284. GLchar* uniformName = (GLchar*) malloc(length + 1);
  285. Uniform uniform;
  286. for (int i = 0; i < activeUniforms; ++i)
  287. {
  288. // Query uniform info.
  289. GL_CHECK(glGetActiveUniform(program, i, length, nullptr, &uniform.size, &uniform.type, uniformName));
  290. uniformName[length] = '\0';
  291. bool isArray = false;
  292. // remove possible array '[]' from uniform name
  293. if (length > 3)
  294. {
  295. char* c = strrchr(uniformName, '[');
  296. if (c)
  297. {
  298. *c = '\0';
  299. isArray = true;
  300. }
  301. }
  302. uniform.name = uniformName;
  303. uniform.hashName = std::hash<std::string>{}(uniformName);
  304. GL_CHECK(uniform.location = glGetUniformLocation(program, uniformName));
  305. GLenum err = glGetError();
  306. if (err != GL_NO_ERROR)
  307. {
  308. RENDERER_LOGE("error: 0x%x uniformName: %s", (int)err, uniformName);
  309. }
  310. assert(err == GL_NO_ERROR);
  311. if (!isArray)
  312. {
  313. uniform.size = -1;
  314. auto iter = _type2uniformCommit.find(uniform.type);
  315. assert(iter != _type2uniformCommit.end());
  316. uniform._callback = iter->second;
  317. }
  318. else
  319. {
  320. auto iter = _type2uniformArrayCommit.find(uniform.type);
  321. assert(iter != _type2uniformArrayCommit.end());
  322. uniform._callback = iter->second;
  323. }
  324. _uniforms.push_back(std::move(uniform));
  325. }
  326. free(uniformName);
  327. }
  328. }
  329. _linked = true;
  330. }
  331. RENDERER_END