Effect.cpp 14 KB

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