Effect.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "Base.h"
  2. #include "Effect.h"
  3. #include "FileSystem.h"
  4. #define OPENGL_ES_DEFINE "#define OPENGL_ES"
  5. namespace gameplay
  6. {
  7. // Cache of unique effects.
  8. static std::map<std::string, Effect*> __effectCache;
  9. static Effect* __currentEffect = NULL;
  10. Effect::Effect() : _program(0)
  11. {
  12. }
  13. Effect::~Effect()
  14. {
  15. // Remove this effect from the cache.
  16. __effectCache.erase(_id);
  17. // Free uniforms.
  18. for (std::map<std::string, Uniform*>::iterator itr = _uniforms.begin(); itr != _uniforms.end(); itr++)
  19. {
  20. SAFE_DELETE(itr->second);
  21. }
  22. if (_program)
  23. {
  24. // If our program object is currently bound, unbind it before we're destroyed.
  25. if (__currentEffect == this)
  26. {
  27. GL_ASSERT( glUseProgram(0) );
  28. __currentEffect = NULL;
  29. }
  30. GL_ASSERT( glDeleteProgram(_program) );
  31. _program = 0;
  32. }
  33. }
  34. Effect* Effect::createFromFile(const char* vshPath, const char* fshPath, const char* defines)
  35. {
  36. GP_ASSERT(vshPath);
  37. GP_ASSERT(fshPath);
  38. // Search the effect cache for an identical effect that is already loaded.
  39. std::string uniqueId = vshPath;
  40. uniqueId += ';';
  41. uniqueId += fshPath;
  42. uniqueId += ';';
  43. if (defines)
  44. {
  45. uniqueId += defines;
  46. }
  47. std::map<std::string, Effect*>::const_iterator itr = __effectCache.find(uniqueId);
  48. if (itr != __effectCache.end())
  49. {
  50. // Found an exiting effect with this id, so increase its ref count and return it.
  51. GP_ASSERT(itr->second);
  52. itr->second->addRef();
  53. return itr->second;
  54. }
  55. // Read source from file.
  56. char* vshSource = FileSystem::readAll(vshPath);
  57. if (vshSource == NULL)
  58. {
  59. GP_ERROR("Failed to read vertex shader from file '%s'.", vshPath);
  60. return NULL;
  61. }
  62. char* fshSource = FileSystem::readAll(fshPath);
  63. if (fshSource == NULL)
  64. {
  65. GP_ERROR("Failed to read fragment shader from file '%s'.", fshPath);
  66. SAFE_DELETE_ARRAY(vshSource);
  67. return NULL;
  68. }
  69. Effect* effect = createFromSource(vshPath, vshSource, fshPath, fshSource, defines);
  70. SAFE_DELETE_ARRAY(vshSource);
  71. SAFE_DELETE_ARRAY(fshSource);
  72. if (effect == NULL)
  73. {
  74. GP_ERROR("Failed to create effect from shaders '%s', '%s'.", vshPath, fshPath);
  75. }
  76. else
  77. {
  78. // Store this effect in the cache.
  79. effect->_id = uniqueId;
  80. __effectCache[uniqueId] = effect;
  81. }
  82. return effect;
  83. }
  84. Effect* Effect::createFromSource(const char* vshSource, const char* fshSource, const char* defines)
  85. {
  86. return createFromSource(NULL, vshSource, NULL, fshSource, defines);
  87. }
  88. Effect* Effect::createFromSource(const char* vshPath, const char* vshSource, const char* fshPath, const char* fshSource, const char* defines)
  89. {
  90. GP_ASSERT(vshSource);
  91. GP_ASSERT(fshSource);
  92. const unsigned int SHADER_SOURCE_LENGTH = 3;
  93. const GLchar* shaderSource[SHADER_SOURCE_LENGTH];
  94. char* infoLog = NULL;
  95. GLuint vertexShader;
  96. GLuint fragmentShader;
  97. GLuint program;
  98. GLint length;
  99. GLint success;
  100. // Compile vertex shader.
  101. std::string definesStr = (defines == NULL) ? "" : defines;
  102. #ifdef OPENGL_ES
  103. if (defines && strlen(defines) != 0)
  104. definesStr += "\n";
  105. definesStr+= OPENGL_ES_DEFINE;
  106. #endif
  107. shaderSource[0] = definesStr.c_str();
  108. shaderSource[1] = "\n";
  109. shaderSource[2] = vshSource;
  110. GL_ASSERT( vertexShader = glCreateShader(GL_VERTEX_SHADER) );
  111. GL_ASSERT( glShaderSource(vertexShader, SHADER_SOURCE_LENGTH, shaderSource, NULL) );
  112. GL_ASSERT( glCompileShader(vertexShader) );
  113. GL_ASSERT( glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success) );
  114. if (success != GL_TRUE)
  115. {
  116. GL_ASSERT( glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &length) );
  117. if (length > 0)
  118. {
  119. infoLog = new char[length];
  120. GL_ASSERT( glGetShaderInfoLog(vertexShader, length, NULL, infoLog) );
  121. infoLog[length-1] = '\0';
  122. }
  123. GP_ERROR("Compile failed for vertex shader '%s' with error '%s'.", vshPath == NULL ? "NULL" : vshPath, infoLog == NULL ? "" : infoLog);
  124. SAFE_DELETE_ARRAY(infoLog);
  125. // Clean up.
  126. GL_ASSERT( glDeleteShader(vertexShader) );
  127. return NULL;
  128. }
  129. // Compile the fragment shader.
  130. shaderSource[2] = fshSource;
  131. GL_ASSERT( fragmentShader = glCreateShader(GL_FRAGMENT_SHADER) );
  132. GL_ASSERT( glShaderSource(fragmentShader, SHADER_SOURCE_LENGTH, shaderSource, NULL) );
  133. GL_ASSERT( glCompileShader(fragmentShader) );
  134. GL_ASSERT( glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success) );
  135. if (success != GL_TRUE)
  136. {
  137. GL_ASSERT( glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &length) );
  138. if (length > 0)
  139. {
  140. infoLog = new char[length];
  141. GL_ASSERT( glGetShaderInfoLog(fragmentShader, length, NULL, infoLog) );
  142. infoLog[length-1] = '\0';
  143. }
  144. GP_ERROR("Compile failed for fragment shader (%s): %s", fshPath == NULL ? "NULL" : fshPath, infoLog == NULL ? "" : infoLog);
  145. SAFE_DELETE_ARRAY(infoLog);
  146. // Clean up.
  147. GL_ASSERT( glDeleteShader(vertexShader) );
  148. GL_ASSERT( glDeleteShader(fragmentShader) );
  149. return NULL;
  150. }
  151. // Link program.
  152. GL_ASSERT( program = glCreateProgram() );
  153. GL_ASSERT( glAttachShader(program, vertexShader) );
  154. GL_ASSERT( glAttachShader(program, fragmentShader) );
  155. GL_ASSERT( glLinkProgram(program) );
  156. GL_ASSERT( glGetProgramiv(program, GL_LINK_STATUS, &success) );
  157. // Delete shaders after linking.
  158. GL_ASSERT( glDeleteShader(vertexShader) );
  159. GL_ASSERT( glDeleteShader(fragmentShader) );
  160. // Check link status.
  161. if (success != GL_TRUE)
  162. {
  163. GL_ASSERT( glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length) );
  164. if (length > 0)
  165. {
  166. infoLog = new char[length];
  167. GL_ASSERT( glGetProgramInfoLog(program, length, NULL, infoLog) );
  168. infoLog[length-1] = '\0';
  169. }
  170. GP_ERROR("Linking program failed (%s,%s): %s", vshPath == NULL ? "NULL" : vshPath, fshPath == NULL ? "NULL" : fshPath, infoLog == NULL ? "" : infoLog);
  171. SAFE_DELETE_ARRAY(infoLog);
  172. // Clean up.
  173. GL_ASSERT( glDeleteProgram(program) );
  174. return NULL;
  175. }
  176. // Create and return the new Effect.
  177. Effect* effect = new Effect();
  178. effect->_program = program;
  179. // Query and store vertex attribute meta-data from the program.
  180. // NOTE: Rather than using glBindAttribLocation to explicitly specify our own
  181. // preferred attribute locations, we're going to query the locations that were
  182. // automatically bound by the GPU. While it can sometimes be convenient to use
  183. // glBindAttribLocation, some vendors actually reserve certain attribute indices
  184. // and thereore using this function can create compatibility issues between
  185. // different hardware vendors.
  186. GLint activeAttributes;
  187. GL_ASSERT( glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &activeAttributes) );
  188. if (activeAttributes > 0)
  189. {
  190. GL_ASSERT( glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &length) );
  191. if (length > 0)
  192. {
  193. GLchar* attribName = new GLchar[length + 1];
  194. GLint attribSize;
  195. GLenum attribType;
  196. GLint attribLocation;
  197. for (int i = 0; i < activeAttributes; ++i)
  198. {
  199. // Query attribute info.
  200. GL_ASSERT( glGetActiveAttrib(program, i, length, NULL, &attribSize, &attribType, attribName) );
  201. attribName[length] = '\0';
  202. // Query the pre-assigned attribute location.
  203. GL_ASSERT( attribLocation = glGetAttribLocation(program, attribName) );
  204. // Assign the vertex attribute mapping for the effect.
  205. effect->_vertexAttributes[attribName] = attribLocation;
  206. }
  207. SAFE_DELETE_ARRAY(attribName);
  208. }
  209. }
  210. // Query and store uniforms from the program.
  211. GLint activeUniforms;
  212. GL_ASSERT( glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &activeUniforms) );
  213. if (activeUniforms > 0)
  214. {
  215. GL_ASSERT( glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length) );
  216. if (length > 0)
  217. {
  218. GLchar* uniformName = new GLchar[length + 1];
  219. GLint uniformSize;
  220. GLenum uniformType;
  221. GLint uniformLocation;
  222. unsigned int samplerIndex = 0;
  223. for (int i = 0; i < activeUniforms; ++i)
  224. {
  225. // Query uniform info.
  226. GL_ASSERT( glGetActiveUniform(program, i, length, NULL, &uniformSize, &uniformType, uniformName) );
  227. uniformName[length] = '\0'; // null terminate
  228. if (uniformSize > 1 && length > 3)
  229. {
  230. // This is an array uniform. I'm stripping array indexers off it since GL does not
  231. // seem to be consistent across different drivers/implementations in how it returns
  232. // array uniforms. On some systems it will return "u_matrixArray", while on others
  233. // it will return "u_matrixArray[0]".
  234. char* c = strrchr(uniformName, '[');
  235. if (c)
  236. {
  237. *c = '\0';
  238. }
  239. }
  240. // Query the pre-assigned uniform location.
  241. GL_ASSERT( uniformLocation = glGetUniformLocation(program, uniformName) );
  242. Uniform* uniform = new Uniform();
  243. uniform->_effect = effect;
  244. uniform->_name = uniformName;
  245. uniform->_location = uniformLocation;
  246. uniform->_type = uniformType;
  247. uniform->_index = uniformType == GL_SAMPLER_2D ? (samplerIndex++) : 0;
  248. effect->_uniforms[uniformName] = uniform;
  249. }
  250. SAFE_DELETE_ARRAY(uniformName);
  251. }
  252. }
  253. return effect;
  254. }
  255. const char* Effect::getId() const
  256. {
  257. return _id.c_str();
  258. }
  259. VertexAttribute Effect::getVertexAttribute(const char* name) const
  260. {
  261. std::map<std::string, VertexAttribute>::const_iterator itr = _vertexAttributes.find(name);
  262. return (itr == _vertexAttributes.end() ? -1 : itr->second);
  263. }
  264. Uniform* Effect::getUniform(const char* name) const
  265. {
  266. std::map<std::string, Uniform*>::const_iterator itr = _uniforms.find(name);
  267. return (itr == _uniforms.end() ? NULL : itr->second);
  268. }
  269. Uniform* Effect::getUniform(unsigned int index) const
  270. {
  271. unsigned int i = 0;
  272. for (std::map<std::string, Uniform*>::const_iterator itr = _uniforms.begin(); itr != _uniforms.end(); itr++, i++)
  273. {
  274. if (i == index)
  275. {
  276. return itr->second;
  277. }
  278. }
  279. return NULL;
  280. }
  281. unsigned int Effect::getUniformCount() const
  282. {
  283. return _uniforms.size();
  284. }
  285. void Effect::setValue(Uniform* uniform, float value)
  286. {
  287. GP_ASSERT(uniform);
  288. GL_ASSERT( glUniform1f(uniform->_location, value) );
  289. }
  290. void Effect::setValue(Uniform* uniform, const float* values, unsigned int count)
  291. {
  292. GP_ASSERT(uniform);
  293. GP_ASSERT(values);
  294. GL_ASSERT( glUniform1fv(uniform->_location, count, values) );
  295. }
  296. void Effect::setValue(Uniform* uniform, int value)
  297. {
  298. GP_ASSERT(uniform);
  299. GL_ASSERT( glUniform1i(uniform->_location, value) );
  300. }
  301. void Effect::setValue(Uniform* uniform, const int* values, unsigned int count)
  302. {
  303. GP_ASSERT(uniform);
  304. GP_ASSERT(values);
  305. GL_ASSERT( glUniform1iv(uniform->_location, count, values) );
  306. }
  307. void Effect::setValue(Uniform* uniform, const Matrix& value)
  308. {
  309. GP_ASSERT(uniform);
  310. GL_ASSERT( glUniformMatrix4fv(uniform->_location, 1, GL_FALSE, value.m) );
  311. }
  312. void Effect::setValue(Uniform* uniform, const Matrix* values, unsigned int count)
  313. {
  314. GP_ASSERT(uniform);
  315. GP_ASSERT(values);
  316. GL_ASSERT( glUniformMatrix4fv(uniform->_location, count, GL_FALSE, (GLfloat*)values) );
  317. }
  318. void Effect::setValue(Uniform* uniform, const Vector2& value)
  319. {
  320. GP_ASSERT(uniform);
  321. GL_ASSERT( glUniform2f(uniform->_location, value.x, value.y) );
  322. }
  323. void Effect::setValue(Uniform* uniform, const Vector2* values, unsigned int count)
  324. {
  325. GP_ASSERT(uniform);
  326. GP_ASSERT(values);
  327. GL_ASSERT( glUniform2fv(uniform->_location, count, (GLfloat*)values) );
  328. }
  329. void Effect::setValue(Uniform* uniform, const Vector3& value)
  330. {
  331. GP_ASSERT(uniform);
  332. GL_ASSERT( glUniform3f(uniform->_location, value.x, value.y, value.z) );
  333. }
  334. void Effect::setValue(Uniform* uniform, const Vector3* values, unsigned int count)
  335. {
  336. GP_ASSERT(uniform);
  337. GP_ASSERT(values);
  338. GL_ASSERT( glUniform3fv(uniform->_location, count, (GLfloat*)values) );
  339. }
  340. void Effect::setValue(Uniform* uniform, const Vector4& value)
  341. {
  342. GP_ASSERT(uniform);
  343. GL_ASSERT( glUniform4f(uniform->_location, value.x, value.y, value.z, value.w) );
  344. }
  345. void Effect::setValue(Uniform* uniform, const Vector4* values, unsigned int count)
  346. {
  347. GP_ASSERT(uniform);
  348. GP_ASSERT(values);
  349. GL_ASSERT( glUniform4fv(uniform->_location, count, (GLfloat*)values) );
  350. }
  351. void Effect::setValue(Uniform* uniform, const Texture::Sampler* sampler)
  352. {
  353. GP_ASSERT(uniform);
  354. GP_ASSERT(uniform->_type == GL_SAMPLER_2D);
  355. GP_ASSERT(sampler);
  356. GL_ASSERT( glActiveTexture(GL_TEXTURE0 + uniform->_index) );
  357. // Bind the sampler - this binds the texture and applies sampler state
  358. const_cast<Texture::Sampler*>(sampler)->bind();
  359. GL_ASSERT( glUniform1i(uniform->_location, uniform->_index) );
  360. }
  361. void Effect::bind()
  362. {
  363. glUseProgram(_program) ;
  364. GLenum test = glGetError();
  365. __currentEffect = this;
  366. }
  367. Effect* Effect::getCurrentEffect()
  368. {
  369. return __currentEffect;
  370. }
  371. Uniform::Uniform() :
  372. _location(-1), _type(0), _index(0)
  373. {
  374. }
  375. Uniform::Uniform(const Uniform& copy)
  376. {
  377. // hidden
  378. }
  379. Uniform::~Uniform()
  380. {
  381. // hidden
  382. }
  383. Effect* Uniform::getEffect() const
  384. {
  385. return _effect;
  386. }
  387. const char* Uniform::getName() const
  388. {
  389. return _name.c_str();
  390. }
  391. const GLenum Uniform::getType() const
  392. {
  393. return _type;
  394. }
  395. }