COGLES2MaterialRenderer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Copyright (C) 2013 Patryk Nadrowski
  2. // Heavily based on the OpenGL driver implemented by Nikolaus Gebhardt
  3. // OpenGL ES driver implemented by Christian Stehno and first OpenGL ES 2.0
  4. // driver implemented by Amundis.
  5. // This file is part of the "Irrlicht Engine".
  6. // For conditions of distribution and use, see copyright notice in Irrlicht.h
  7. #include "IrrCompileConfig.h"
  8. #ifdef _IRR_COMPILE_WITH_OGLES2_
  9. #include "COGLES2MaterialRenderer.h"
  10. #include "IGPUProgrammingServices.h"
  11. #include "IShaderConstantSetCallBack.h"
  12. #include "IMaterialRendererServices.h"
  13. #include "IVideoDriver.h"
  14. #include "os.h"
  15. #include "COGLES2Driver.h"
  16. #include "COGLES2MaterialRenderer.h"
  17. namespace irr
  18. {
  19. namespace video
  20. {
  21. //! Constructor
  22. COGLES2MaterialRenderer::COGLES2MaterialRenderer(COGLES2Driver* driver,
  23. s32& outMaterialTypeNr,
  24. const c8* vertexShaderProgram,
  25. const c8* pixelShaderProgram,
  26. IShaderConstantSetCallBack* callback,
  27. E_MATERIAL_TYPE baseMaterial,
  28. s32 userData)
  29. : Driver(driver), CallBack(callback), Alpha(false), Blending(false), FixedBlending(false), Program(0), UserData(userData)
  30. {
  31. #ifdef _DEBUG
  32. setDebugName("COGLES2MaterialRenderer");
  33. #endif
  34. if (baseMaterial == EMT_TRANSPARENT_VERTEX_ALPHA || baseMaterial == EMT_TRANSPARENT_ALPHA_CHANNEL ||
  35. /*baseMaterial == EMT_TRANSPARENT_ALPHA_CHANNEL_REF || */baseMaterial == EMT_NORMAL_MAP_TRANSPARENT_VERTEX_ALPHA ||
  36. baseMaterial == EMT_PARALLAX_MAP_TRANSPARENT_VERTEX_ALPHA)
  37. {
  38. Alpha = true;
  39. }
  40. else if (baseMaterial == EMT_TRANSPARENT_ADD_COLOR || baseMaterial == EMT_NORMAL_MAP_TRANSPARENT_ADD_COLOR ||
  41. baseMaterial == EMT_PARALLAX_MAP_TRANSPARENT_ADD_COLOR)
  42. {
  43. FixedBlending = true;
  44. }
  45. else if (baseMaterial == EMT_ONETEXTURE_BLEND)
  46. Blending = true;
  47. if (CallBack)
  48. CallBack->grab();
  49. init(outMaterialTypeNr, vertexShaderProgram, pixelShaderProgram);
  50. }
  51. //! constructor only for use by derived classes who want to
  52. //! create a fall back material for example.
  53. COGLES2MaterialRenderer::COGLES2MaterialRenderer(COGLES2Driver* driver,
  54. IShaderConstantSetCallBack* callback,
  55. E_MATERIAL_TYPE baseMaterial, s32 userData)
  56. : Driver(driver), CallBack(callback), Alpha(false), Blending(false), FixedBlending(false), Program(0), UserData(userData)
  57. {
  58. if (baseMaterial == EMT_TRANSPARENT_VERTEX_ALPHA || baseMaterial == EMT_TRANSPARENT_ALPHA_CHANNEL ||
  59. /*baseMaterial == EMT_TRANSPARENT_ALPHA_CHANNEL_REF || */baseMaterial == EMT_NORMAL_MAP_TRANSPARENT_VERTEX_ALPHA ||
  60. baseMaterial == EMT_PARALLAX_MAP_TRANSPARENT_VERTEX_ALPHA)
  61. {
  62. Alpha = true;
  63. }
  64. else if (baseMaterial == EMT_TRANSPARENT_ADD_COLOR || baseMaterial == EMT_NORMAL_MAP_TRANSPARENT_ADD_COLOR ||
  65. baseMaterial == EMT_PARALLAX_MAP_TRANSPARENT_ADD_COLOR)
  66. {
  67. FixedBlending = true;
  68. }
  69. else if (baseMaterial == EMT_ONETEXTURE_BLEND)
  70. Blending = true;
  71. if (CallBack)
  72. CallBack->grab();
  73. }
  74. //! Destructor
  75. COGLES2MaterialRenderer::~COGLES2MaterialRenderer()
  76. {
  77. if (CallBack)
  78. CallBack->drop();
  79. if (Program)
  80. {
  81. GLuint shaders[8];
  82. GLint count;
  83. glGetAttachedShaders(Program, 8, &count, shaders);
  84. count=core::min_(count,8);
  85. for (GLint i=0; i<count; ++i)
  86. glDeleteShader(shaders[i]);
  87. glDeleteProgram(Program);
  88. Program = 0;
  89. }
  90. UniformInfo.clear();
  91. }
  92. GLuint COGLES2MaterialRenderer::getProgram() const
  93. {
  94. return Program;
  95. }
  96. void COGLES2MaterialRenderer::init(s32& outMaterialTypeNr,
  97. const c8* vertexShaderProgram,
  98. const c8* pixelShaderProgram,
  99. bool addMaterial)
  100. {
  101. outMaterialTypeNr = -1;
  102. if (!vertexShaderProgram || !pixelShaderProgram)
  103. return;
  104. Program = glCreateProgram();
  105. if (!Program)
  106. return;
  107. if (vertexShaderProgram)
  108. if (!createShader(GL_VERTEX_SHADER, vertexShaderProgram))
  109. return;
  110. if (pixelShaderProgram)
  111. if (!createShader(GL_FRAGMENT_SHADER, pixelShaderProgram))
  112. return;
  113. for ( size_t i = 0; i < EVA_COUNT; ++i )
  114. glBindAttribLocation( Program, i, sBuiltInVertexAttributeNames[i]);
  115. if (!linkProgram())
  116. return;
  117. // register myself as new material
  118. if (addMaterial)
  119. outMaterialTypeNr = Driver->addMaterialRenderer(this);
  120. }
  121. bool COGLES2MaterialRenderer::OnRender(IMaterialRendererServices* service, E_VERTEX_TYPE vtxtype)
  122. {
  123. Driver->setTextureRenderStates(Driver->getCurrentMaterial(), false);
  124. // call callback to set shader constants
  125. if (CallBack && Program)
  126. CallBack->OnSetConstants(this, UserData);
  127. return true;
  128. }
  129. void COGLES2MaterialRenderer::OnSetMaterial(const video::SMaterial& material,
  130. const video::SMaterial& lastMaterial,
  131. bool resetAllRenderstates,
  132. video::IMaterialRendererServices* services)
  133. {
  134. Driver->getBridgeCalls()->setProgram(Program);
  135. Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
  136. if (FixedBlending)
  137. {
  138. Driver->getBridgeCalls()->setBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
  139. Driver->getBridgeCalls()->setBlend(true);
  140. }
  141. else if (Blending)
  142. {
  143. E_BLEND_FACTOR srcFact,dstFact;
  144. E_MODULATE_FUNC modulate;
  145. u32 alphaSource;
  146. unpack_textureBlendFunc(srcFact, dstFact, modulate, alphaSource, material.MaterialTypeParam);
  147. Driver->getBridgeCalls()->setBlendFunc(Driver->getGLBlend(srcFact), Driver->getGLBlend(dstFact));
  148. Driver->getBridgeCalls()->setBlend(true);
  149. }
  150. else
  151. Driver->getBridgeCalls()->setBlend(false);
  152. if (CallBack)
  153. CallBack->OnSetMaterial(material);
  154. }
  155. void COGLES2MaterialRenderer::OnUnsetMaterial()
  156. {
  157. }
  158. //! Returns if the material is transparent.
  159. bool COGLES2MaterialRenderer::isTransparent() const
  160. {
  161. return (Alpha || Blending || FixedBlending);
  162. }
  163. bool COGLES2MaterialRenderer::createShader(GLenum shaderType, const char* shader)
  164. {
  165. if (Program)
  166. {
  167. GLuint shaderHandle = glCreateShader(shaderType);
  168. glShaderSource(shaderHandle, 1, &shader, NULL);
  169. glCompileShader(shaderHandle);
  170. GLint status = 0;
  171. glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &status);
  172. if (status != GL_TRUE)
  173. {
  174. os::Printer::log("GLSL shader failed to compile", ELL_ERROR);
  175. // check error message and log it
  176. GLint maxLength=0;
  177. GLint length;
  178. glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH,
  179. &maxLength);
  180. if (maxLength)
  181. {
  182. GLchar *infoLog = new GLchar[maxLength];
  183. glGetShaderInfoLog(shaderHandle, maxLength, &length, infoLog);
  184. os::Printer::log(reinterpret_cast<const c8*>(infoLog), ELL_ERROR);
  185. delete [] infoLog;
  186. }
  187. return false;
  188. }
  189. glAttachShader(Program, shaderHandle);
  190. }
  191. return true;
  192. }
  193. bool COGLES2MaterialRenderer::linkProgram()
  194. {
  195. if (Program)
  196. {
  197. glLinkProgram(Program);
  198. GLint status = 0;
  199. glGetProgramiv(Program, GL_LINK_STATUS, &status);
  200. if (!status)
  201. {
  202. os::Printer::log("GLSL shader program failed to link", ELL_ERROR);
  203. // check error message and log it
  204. GLint maxLength=0;
  205. GLsizei length;
  206. glGetProgramiv(Program, GL_INFO_LOG_LENGTH, &maxLength);
  207. if (maxLength)
  208. {
  209. GLchar *infoLog = new GLchar[maxLength];
  210. glGetProgramInfoLog(Program, maxLength, &length, infoLog);
  211. os::Printer::log(reinterpret_cast<const c8*>(infoLog), ELL_ERROR);
  212. delete [] infoLog;
  213. }
  214. return false;
  215. }
  216. // get uniforms information
  217. GLint num = 0;
  218. glGetProgramiv(Program, GL_ACTIVE_UNIFORMS, &num);
  219. if (num == 0)
  220. {
  221. // no uniforms
  222. return true;
  223. }
  224. GLint maxlen = 0;
  225. glGetProgramiv(Program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxlen);
  226. if (maxlen == 0)
  227. {
  228. os::Printer::log("GLSL: failed to retrieve uniform information", ELL_ERROR);
  229. return false;
  230. }
  231. // seems that some implementations use an extra null terminator
  232. ++maxlen;
  233. c8 *buf = new c8[maxlen];
  234. UniformInfo.clear();
  235. UniformInfo.reallocate(num);
  236. for (GLint i=0; i < num; ++i)
  237. {
  238. SUniformInfo ui;
  239. memset(buf, 0, maxlen);
  240. GLint size;
  241. glGetActiveUniform(Program, i, maxlen, 0, &size, &ui.type, reinterpret_cast<GLchar*>(buf));
  242. ui.name = buf;
  243. ui.location = glGetUniformLocation(Program, buf);
  244. UniformInfo.push_back(ui);
  245. }
  246. delete [] buf;
  247. }
  248. return true;
  249. }
  250. void COGLES2MaterialRenderer::setBasicRenderStates(const SMaterial& material,
  251. const SMaterial& lastMaterial,
  252. bool resetAllRenderstates)
  253. {
  254. // forward
  255. Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
  256. }
  257. s32 COGLES2MaterialRenderer::getVertexShaderConstantID(const c8* name)
  258. {
  259. return getPixelShaderConstantID(name);
  260. }
  261. s32 COGLES2MaterialRenderer::getPixelShaderConstantID(const c8* name)
  262. {
  263. for (u32 i = 0; i < UniformInfo.size(); ++i)
  264. {
  265. if (UniformInfo[i].name == name)
  266. return i;
  267. }
  268. return -1;
  269. }
  270. void COGLES2MaterialRenderer::setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
  271. {
  272. os::Printer::log("Cannot set constant, please use high level shader call instead.", ELL_WARNING);
  273. }
  274. void COGLES2MaterialRenderer::setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
  275. {
  276. os::Printer::log("Cannot set constant, use high level shader call.", ELL_WARNING);
  277. }
  278. bool COGLES2MaterialRenderer::setVertexShaderConstant(s32 index, const f32* floats, int count)
  279. {
  280. return setPixelShaderConstant(index, floats, count);
  281. }
  282. bool COGLES2MaterialRenderer::setVertexShaderConstant(s32 index, const s32* ints, int count)
  283. {
  284. return setPixelShaderConstant(index, ints, count);
  285. }
  286. bool COGLES2MaterialRenderer::setPixelShaderConstant(s32 index, const f32* floats, int count)
  287. {
  288. if(index < 0 || UniformInfo[index].location < 0)
  289. return false;
  290. bool status = true;
  291. switch (UniformInfo[index].type)
  292. {
  293. case GL_FLOAT:
  294. glUniform1fv(UniformInfo[index].location, count, floats);
  295. break;
  296. case GL_FLOAT_VEC2:
  297. glUniform2fv(UniformInfo[index].location, count/2, floats);
  298. break;
  299. case GL_FLOAT_VEC3:
  300. glUniform3fv(UniformInfo[index].location, count/3, floats);
  301. break;
  302. case GL_FLOAT_VEC4:
  303. glUniform4fv(UniformInfo[index].location, count/4, floats);
  304. break;
  305. case GL_FLOAT_MAT2:
  306. glUniformMatrix2fv(UniformInfo[index].location, count/4, false, floats);
  307. break;
  308. case GL_FLOAT_MAT3:
  309. glUniformMatrix3fv(UniformInfo[index].location, count/9, false, floats);
  310. break;
  311. case GL_FLOAT_MAT4:
  312. glUniformMatrix4fv(UniformInfo[index].location, count/16, false, floats);
  313. break;
  314. case GL_SAMPLER_2D:
  315. case GL_SAMPLER_CUBE:
  316. {
  317. if(floats)
  318. {
  319. const GLint id = (GLint)(*floats);
  320. glUniform1iv(UniformInfo[index].location, 1, &id);
  321. }
  322. else
  323. status = false;
  324. }
  325. break;
  326. default:
  327. status = false;
  328. break;
  329. }
  330. return status;
  331. }
  332. bool COGLES2MaterialRenderer::setPixelShaderConstant(s32 index, const s32* ints, int count)
  333. {
  334. if(index < 0 || UniformInfo[index].location < 0)
  335. return false;
  336. bool status = true;
  337. switch (UniformInfo[index].type)
  338. {
  339. case GL_INT:
  340. case GL_BOOL:
  341. glUniform1iv(UniformInfo[index].location, count, ints);
  342. break;
  343. case GL_INT_VEC2:
  344. case GL_BOOL_VEC2:
  345. glUniform2iv(UniformInfo[index].location, count/2, ints);
  346. break;
  347. case GL_INT_VEC3:
  348. case GL_BOOL_VEC3:
  349. glUniform3iv(UniformInfo[index].location, count/3, ints);
  350. break;
  351. case GL_INT_VEC4:
  352. case GL_BOOL_VEC4:
  353. glUniform4iv(UniformInfo[index].location, count/4, ints);
  354. break;
  355. case GL_SAMPLER_2D:
  356. case GL_SAMPLER_CUBE:
  357. glUniform1iv(UniformInfo[index].location, 1, ints);
  358. break;
  359. default:
  360. status = false;
  361. break;
  362. }
  363. return status;
  364. }
  365. IVideoDriver* COGLES2MaterialRenderer::getVideoDriver()
  366. {
  367. return Driver;
  368. }
  369. } // end namespace video
  370. } // end namespace irr
  371. #endif