COpenGLSLMaterialRenderer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. // This file was originally written by William Finlayson. I (Nikolaus
  5. // Gebhardt) did some minor modifications and changes to it and integrated it
  6. // into Irrlicht. Thanks a lot to William for his work on this and that he gave
  7. // me his permission to add it into Irrlicht using the zlib license.
  8. // After Irrlicht 0.12, Michael Zoech did some improvements to this renderer, I
  9. // merged this into Irrlicht 0.14, thanks to him for his work.
  10. #include "IrrCompileConfig.h"
  11. #ifdef _IRR_COMPILE_WITH_OPENGL_
  12. #include "COpenGLSLMaterialRenderer.h"
  13. #include "IGPUProgrammingServices.h"
  14. #include "IShaderConstantSetCallBack.h"
  15. #include "IMaterialRendererServices.h"
  16. #include "IVideoDriver.h"
  17. #include "os.h"
  18. #include "COpenGLDriver.h"
  19. namespace irr
  20. {
  21. namespace video
  22. {
  23. //! Constructor
  24. COpenGLSLMaterialRenderer::COpenGLSLMaterialRenderer(video::COpenGLDriver* driver,
  25. s32& outMaterialTypeNr, const c8* vertexShaderProgram,
  26. const c8* vertexShaderEntryPointName,
  27. E_VERTEX_SHADER_TYPE vsCompileTarget,
  28. const c8* pixelShaderProgram,
  29. const c8* pixelShaderEntryPointName,
  30. E_PIXEL_SHADER_TYPE psCompileTarget,
  31. const c8* geometryShaderProgram,
  32. const c8* geometryShaderEntryPointName,
  33. E_GEOMETRY_SHADER_TYPE gsCompileTarget,
  34. scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType,
  35. u32 verticesOut,
  36. IShaderConstantSetCallBack* callback,
  37. video::IMaterialRenderer* baseMaterial,
  38. s32 userData)
  39. : Driver(driver), CallBack(callback), BaseMaterial(baseMaterial),
  40. Program(0), Program2(0), UserData(userData)
  41. {
  42. #ifdef _DEBUG
  43. setDebugName("COpenGLSLMaterialRenderer");
  44. #endif
  45. //entry points must always be main, and the compile target isn't selectable
  46. //it is fine to ignore what has been asked for, as the compiler should spot anything wrong
  47. //just check that GLSL is available
  48. if (BaseMaterial)
  49. BaseMaterial->grab();
  50. if (CallBack)
  51. CallBack->grab();
  52. if (!Driver->queryFeature(EVDF_ARB_GLSL))
  53. return;
  54. init(outMaterialTypeNr, vertexShaderProgram, pixelShaderProgram, geometryShaderProgram);
  55. }
  56. //! constructor only for use by derived classes who want to
  57. //! create a fall back material for example.
  58. COpenGLSLMaterialRenderer::COpenGLSLMaterialRenderer(COpenGLDriver* driver,
  59. IShaderConstantSetCallBack* callback,
  60. IMaterialRenderer* baseMaterial, s32 userData)
  61. : Driver(driver), CallBack(callback), BaseMaterial(baseMaterial),
  62. Program(0), Program2(0), UserData(userData)
  63. {
  64. if (BaseMaterial)
  65. BaseMaterial->grab();
  66. if (CallBack)
  67. CallBack->grab();
  68. }
  69. //! Destructor
  70. COpenGLSLMaterialRenderer::~COpenGLSLMaterialRenderer()
  71. {
  72. if (CallBack)
  73. CallBack->drop();
  74. if (Program)
  75. {
  76. GLhandleARB shaders[8];
  77. GLint count;
  78. Driver->extGlGetAttachedObjects(Program, 8, &count, shaders);
  79. // avoid bugs in some drivers, which return larger numbers
  80. count=core::min_(count,8);
  81. for (GLint i=0; i<count; ++i)
  82. Driver->extGlDeleteObject(shaders[i]);
  83. Driver->extGlDeleteObject(Program);
  84. Program = 0;
  85. }
  86. if (Program2)
  87. {
  88. GLuint shaders[8];
  89. GLint count;
  90. Driver->extGlGetAttachedShaders(Program2, 8, &count, shaders);
  91. // avoid bugs in some drivers, which return larger numbers
  92. count=core::min_(count,8);
  93. for (GLint i=0; i<count; ++i)
  94. Driver->extGlDeleteShader(shaders[i]);
  95. Driver->extGlDeleteProgram(Program2);
  96. Program2 = 0;
  97. }
  98. UniformInfo.clear();
  99. if (BaseMaterial)
  100. BaseMaterial->drop();
  101. }
  102. void COpenGLSLMaterialRenderer::init(s32& outMaterialTypeNr,
  103. const c8* vertexShaderProgram,
  104. const c8* pixelShaderProgram,
  105. const c8* geometryShaderProgram,
  106. scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType,
  107. u32 verticesOut)
  108. {
  109. outMaterialTypeNr = -1;
  110. if (!createProgram())
  111. return;
  112. #if defined(GL_ARB_vertex_shader) && defined (GL_ARB_fragment_shader)
  113. if (vertexShaderProgram)
  114. if (!createShader(GL_VERTEX_SHADER_ARB, vertexShaderProgram))
  115. return;
  116. if (pixelShaderProgram)
  117. if (!createShader(GL_FRAGMENT_SHADER_ARB, pixelShaderProgram))
  118. return;
  119. #endif
  120. #if defined(GL_ARB_geometry_shader4) || defined(GL_EXT_geometry_shader4) || defined(GL_NV_geometry_program4) || defined(GL_NV_geometry_shader4)
  121. if (geometryShaderProgram && Driver->queryFeature(EVDF_GEOMETRY_SHADER))
  122. {
  123. if (!createShader(GL_GEOMETRY_SHADER_EXT, geometryShaderProgram))
  124. return;
  125. #if defined(GL_ARB_geometry_shader4) || defined(GL_EXT_geometry_shader4) || defined(GL_NV_geometry_shader4)
  126. if (Program2)
  127. {
  128. Driver->extGlProgramParameteri((GLhandleARB)Program2, GL_GEOMETRY_INPUT_TYPE_EXT, Driver->primitiveTypeToGL(inType));
  129. Driver->extGlProgramParameteri((GLhandleARB)Program2, GL_GEOMETRY_OUTPUT_TYPE_EXT, Driver->primitiveTypeToGL(outType));
  130. if (verticesOut==0)
  131. Driver->extGlProgramParameteri((GLhandleARB)Program2, GL_GEOMETRY_VERTICES_OUT_EXT, Driver->MaxGeometryVerticesOut);
  132. else
  133. Driver->extGlProgramParameteri((GLhandleARB)Program2, GL_GEOMETRY_VERTICES_OUT_EXT, core::min_(verticesOut, Driver->MaxGeometryVerticesOut));
  134. }
  135. else
  136. {
  137. Driver->extGlProgramParameteri(Program, GL_GEOMETRY_INPUT_TYPE_EXT, Driver->primitiveTypeToGL(inType));
  138. Driver->extGlProgramParameteri(Program, GL_GEOMETRY_OUTPUT_TYPE_EXT, Driver->primitiveTypeToGL(outType));
  139. if (verticesOut==0)
  140. Driver->extGlProgramParameteri(Program, GL_GEOMETRY_VERTICES_OUT_EXT, Driver->MaxGeometryVerticesOut);
  141. else
  142. Driver->extGlProgramParameteri(Program, GL_GEOMETRY_VERTICES_OUT_EXT, core::min_(verticesOut, Driver->MaxGeometryVerticesOut));
  143. }
  144. #elif defined(GL_NV_geometry_program4)
  145. if (verticesOut==0)
  146. Driver->extGlProgramVertexLimit(GL_GEOMETRY_PROGRAM_NV, Driver->MaxGeometryVerticesOut);
  147. else
  148. Driver->extGlProgramVertexLimit(GL_GEOMETRY_PROGRAM_NV, core::min_(verticesOut, Driver->MaxGeometryVerticesOut));
  149. #endif
  150. }
  151. #endif
  152. if (!linkProgram())
  153. return;
  154. // register myself as new material
  155. outMaterialTypeNr = Driver->addMaterialRenderer(this);
  156. }
  157. bool COpenGLSLMaterialRenderer::OnRender(IMaterialRendererServices* service,
  158. E_VERTEX_TYPE vtxtype)
  159. {
  160. // call callback to set shader constants
  161. if (CallBack && (Program||Program2))
  162. CallBack->OnSetConstants(this, UserData);
  163. return true;
  164. }
  165. void COpenGLSLMaterialRenderer::OnSetMaterial(const video::SMaterial& material,
  166. const video::SMaterial& lastMaterial,
  167. bool resetAllRenderstates,
  168. video::IMaterialRendererServices* services)
  169. {
  170. if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
  171. {
  172. if (Program2)
  173. Driver->extGlUseProgram(Program2);
  174. else if (Program)
  175. Driver->extGlUseProgramObject(Program);
  176. if (BaseMaterial)
  177. BaseMaterial->OnSetMaterial(material, material, true, this);
  178. }
  179. //let callback know used material
  180. if (CallBack)
  181. CallBack->OnSetMaterial(material);
  182. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  183. Driver->setActiveTexture(i, material.getTexture(i));
  184. Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
  185. }
  186. void COpenGLSLMaterialRenderer::OnUnsetMaterial()
  187. {
  188. if (Program)
  189. Driver->extGlUseProgramObject(0);
  190. if (Program2)
  191. Driver->extGlUseProgram(0);
  192. if (BaseMaterial)
  193. BaseMaterial->OnUnsetMaterial();
  194. }
  195. //! Returns if the material is transparent.
  196. bool COpenGLSLMaterialRenderer::isTransparent() const
  197. {
  198. return BaseMaterial ? BaseMaterial->isTransparent() : false;
  199. }
  200. bool COpenGLSLMaterialRenderer::createProgram()
  201. {
  202. if (Driver->Version>=200)
  203. Program2 = Driver->extGlCreateProgram();
  204. else
  205. Program = Driver->extGlCreateProgramObject();
  206. return true;
  207. }
  208. bool COpenGLSLMaterialRenderer::createShader(GLenum shaderType, const char* shader)
  209. {
  210. if (Program2)
  211. {
  212. GLuint shaderHandle = Driver->extGlCreateShader(shaderType);
  213. Driver->extGlShaderSource(shaderHandle, 1, &shader, NULL);
  214. Driver->extGlCompileShader(shaderHandle);
  215. GLint status = 0;
  216. #ifdef GL_VERSION_2_0
  217. Driver->extGlGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &status);
  218. #endif
  219. if (status != GL_TRUE)
  220. {
  221. os::Printer::log("GLSL shader failed to compile", ELL_ERROR);
  222. // check error message and log it
  223. GLint maxLength=0;
  224. GLint length;
  225. #ifdef GL_VERSION_2_0
  226. Driver->extGlGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH,
  227. &maxLength);
  228. #endif
  229. if (maxLength)
  230. {
  231. GLchar *infoLog = new GLchar[maxLength];
  232. Driver->extGlGetShaderInfoLog(shaderHandle, maxLength, &length, infoLog);
  233. os::Printer::log(reinterpret_cast<const c8*>(infoLog), ELL_ERROR);
  234. delete [] infoLog;
  235. }
  236. return false;
  237. }
  238. Driver->extGlAttachShader(Program2, shaderHandle);
  239. }
  240. else
  241. {
  242. GLhandleARB shaderHandle = Driver->extGlCreateShaderObject(shaderType);
  243. Driver->extGlShaderSourceARB(shaderHandle, 1, &shader, NULL);
  244. Driver->extGlCompileShaderARB(shaderHandle);
  245. GLint status = 0;
  246. #ifdef GL_ARB_shader_objects
  247. Driver->extGlGetObjectParameteriv(shaderHandle, GL_OBJECT_COMPILE_STATUS_ARB, &status);
  248. #endif
  249. if (!status)
  250. {
  251. os::Printer::log("GLSL shader failed to compile", ELL_ERROR);
  252. // check error message and log it
  253. GLint maxLength=0;
  254. GLsizei length;
  255. #ifdef GL_ARB_shader_objects
  256. Driver->extGlGetObjectParameteriv(shaderHandle,
  257. GL_OBJECT_INFO_LOG_LENGTH_ARB, &maxLength);
  258. #endif
  259. if (maxLength)
  260. {
  261. GLcharARB *infoLog = new GLcharARB[maxLength];
  262. Driver->extGlGetInfoLog(shaderHandle, maxLength, &length, infoLog);
  263. os::Printer::log(reinterpret_cast<const c8*>(infoLog), ELL_ERROR);
  264. delete [] infoLog;
  265. }
  266. return false;
  267. }
  268. Driver->extGlAttachObject(Program, shaderHandle);
  269. }
  270. return true;
  271. }
  272. bool COpenGLSLMaterialRenderer::linkProgram()
  273. {
  274. if (Program2)
  275. {
  276. Driver->extGlLinkProgram(Program2);
  277. GLint status = 0;
  278. #ifdef GL_VERSION_2_0
  279. Driver->extGlGetProgramiv(Program2, GL_LINK_STATUS, &status);
  280. #endif
  281. if (!status)
  282. {
  283. os::Printer::log("GLSL shader program failed to link", ELL_ERROR);
  284. // check error message and log it
  285. GLint maxLength=0;
  286. GLsizei length;
  287. #ifdef GL_VERSION_2_0
  288. Driver->extGlGetProgramiv(Program2, GL_INFO_LOG_LENGTH, &maxLength);
  289. #endif
  290. if (maxLength)
  291. {
  292. GLchar *infoLog = new GLchar[maxLength];
  293. Driver->extGlGetProgramInfoLog(Program2, maxLength, &length, infoLog);
  294. os::Printer::log(reinterpret_cast<const c8*>(infoLog), ELL_ERROR);
  295. delete [] infoLog;
  296. }
  297. return false;
  298. }
  299. // get uniforms information
  300. GLint num = 0;
  301. #ifdef GL_VERSION_2_0
  302. Driver->extGlGetProgramiv(Program2, GL_ACTIVE_UNIFORMS, &num);
  303. #endif
  304. if (num == 0)
  305. {
  306. // no uniforms
  307. return true;
  308. }
  309. GLint maxlen = 0;
  310. #ifdef GL_VERSION_2_0
  311. Driver->extGlGetProgramiv(Program2, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxlen);
  312. #endif
  313. if (maxlen == 0)
  314. {
  315. os::Printer::log("GLSL: failed to retrieve uniform information", ELL_ERROR);
  316. return false;
  317. }
  318. // seems that some implementations use an extra null terminator
  319. ++maxlen;
  320. c8 *buf = new c8[maxlen];
  321. UniformInfo.clear();
  322. UniformInfo.reallocate(num);
  323. for (GLint i=0; i < num; ++i)
  324. {
  325. SUniformInfo ui;
  326. memset(buf, 0, maxlen);
  327. GLint size;
  328. Driver->extGlGetActiveUniform(Program2, i, maxlen, 0, &size, &ui.type, reinterpret_cast<GLchar*>(buf));
  329. ui.name = buf;
  330. UniformInfo.push_back(ui);
  331. }
  332. delete [] buf;
  333. }
  334. else
  335. {
  336. Driver->extGlLinkProgramARB(Program);
  337. GLint status = 0;
  338. #ifdef GL_ARB_shader_objects
  339. Driver->extGlGetObjectParameteriv(Program, GL_OBJECT_LINK_STATUS_ARB, &status);
  340. #endif
  341. if (!status)
  342. {
  343. os::Printer::log("GLSL shader program failed to link", ELL_ERROR);
  344. // check error message and log it
  345. GLint maxLength=0;
  346. GLsizei length;
  347. #ifdef GL_ARB_shader_objects
  348. Driver->extGlGetObjectParameteriv(Program,
  349. GL_OBJECT_INFO_LOG_LENGTH_ARB, &maxLength);
  350. #endif
  351. if (maxLength)
  352. {
  353. GLcharARB *infoLog = new GLcharARB[maxLength];
  354. Driver->extGlGetInfoLog(Program, maxLength, &length, infoLog);
  355. os::Printer::log(reinterpret_cast<const c8*>(infoLog), ELL_ERROR);
  356. delete [] infoLog;
  357. }
  358. return false;
  359. }
  360. // get uniforms information
  361. GLint num = 0;
  362. #ifdef GL_ARB_shader_objects
  363. Driver->extGlGetObjectParameteriv(Program, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &num);
  364. #endif
  365. if (num == 0)
  366. {
  367. // no uniforms
  368. return true;
  369. }
  370. GLint maxlen = 0;
  371. #ifdef GL_ARB_shader_objects
  372. Driver->extGlGetObjectParameteriv(Program, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &maxlen);
  373. #endif
  374. if (maxlen == 0)
  375. {
  376. os::Printer::log("GLSL: failed to retrieve uniform information", ELL_ERROR);
  377. return false;
  378. }
  379. // seems that some implementations use an extra null terminator
  380. ++maxlen;
  381. c8 *buf = new c8[maxlen];
  382. UniformInfo.clear();
  383. UniformInfo.reallocate(num);
  384. for (int i=0; i < num; ++i)
  385. {
  386. SUniformInfo ui;
  387. memset(buf, 0, maxlen);
  388. GLint size;
  389. Driver->extGlGetActiveUniformARB(Program, i, maxlen, 0, &size, &ui.type, reinterpret_cast<GLcharARB*>(buf));
  390. ui.name = buf;
  391. UniformInfo.push_back(ui);
  392. }
  393. delete [] buf;
  394. }
  395. return true;
  396. }
  397. void COpenGLSLMaterialRenderer::setBasicRenderStates(const SMaterial& material,
  398. const SMaterial& lastMaterial,
  399. bool resetAllRenderstates)
  400. {
  401. // forward
  402. Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
  403. }
  404. bool COpenGLSLMaterialRenderer::setVertexShaderConstant(const c8* name, const f32* floats, int count)
  405. {
  406. return setPixelShaderConstant(name, floats, count);
  407. }
  408. bool COpenGLSLMaterialRenderer::setVertexShaderConstant(const c8* name, const bool* bools, int count)
  409. {
  410. return setPixelShaderConstant(name, bools, count);
  411. }
  412. bool COpenGLSLMaterialRenderer::setVertexShaderConstant(const c8* name, const s32* ints, int count)
  413. {
  414. return setPixelShaderConstant(name, ints, count);
  415. }
  416. void COpenGLSLMaterialRenderer::setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
  417. {
  418. os::Printer::log("Cannot set constant, please use high level shader call instead.", ELL_WARNING);
  419. }
  420. bool COpenGLSLMaterialRenderer::setPixelShaderConstant(const c8* name, const f32* floats, int count)
  421. {
  422. u32 i;
  423. const u32 num = UniformInfo.size();
  424. for (i=0; i < num; ++i)
  425. {
  426. if (UniformInfo[i].name == name)
  427. break;
  428. }
  429. if (i == num)
  430. return false;
  431. #if defined(GL_VERSION_2_0)||defined(GL_ARB_shader_objects)
  432. GLint Location=0;
  433. if (Program2)
  434. Location=Driver->extGlGetUniformLocation(Program2,name);
  435. else
  436. Location=Driver->extGlGetUniformLocationARB(Program,name);
  437. bool status = true;
  438. switch (UniformInfo[i].type)
  439. {
  440. case GL_FLOAT:
  441. Driver->extGlUniform1fv(Location, count, floats);
  442. break;
  443. case GL_FLOAT_VEC2:
  444. Driver->extGlUniform2fv(Location, count/2, floats);
  445. break;
  446. case GL_FLOAT_VEC3:
  447. Driver->extGlUniform3fv(Location, count/3, floats);
  448. break;
  449. case GL_FLOAT_VEC4:
  450. Driver->extGlUniform4fv(Location, count/4, floats);
  451. break;
  452. case GL_FLOAT_MAT2:
  453. Driver->extGlUniformMatrix2fv(Location, count/4, false, floats);
  454. break;
  455. case GL_FLOAT_MAT3:
  456. Driver->extGlUniformMatrix3fv(Location, count/9, false, floats);
  457. break;
  458. case GL_FLOAT_MAT4:
  459. Driver->extGlUniformMatrix4fv(Location, count/16, false, floats);
  460. break;
  461. case GL_SAMPLER_1D:
  462. case GL_SAMPLER_2D:
  463. case GL_SAMPLER_3D:
  464. case GL_SAMPLER_CUBE:
  465. case GL_SAMPLER_1D_SHADOW:
  466. case GL_SAMPLER_2D_SHADOW:
  467. {
  468. const GLint id = static_cast<GLint>(*floats);
  469. Driver->extGlUniform1iv(Location, 1, &id);
  470. }
  471. break;
  472. default:
  473. status = false;
  474. break;
  475. }
  476. return status;
  477. #else
  478. return false;
  479. #endif
  480. }
  481. bool COpenGLSLMaterialRenderer::setPixelShaderConstant(const c8* name, const bool* bools, int count)
  482. {
  483. u32 i;
  484. const u32 num = UniformInfo.size();
  485. for (i=0; i < num; ++i)
  486. {
  487. if (UniformInfo[i].name == name)
  488. break;
  489. }
  490. if (i == num)
  491. return false;
  492. #if defined(GL_VERSION_2_0)||defined(GL_ARB_shader_objects)
  493. GLint Location=0;
  494. if (Program2)
  495. Location=Driver->extGlGetUniformLocation(Program2,name);
  496. else
  497. Location=Driver->extGlGetUniformLocationARB(Program,name);
  498. bool status = true;
  499. switch (UniformInfo[i].type)
  500. {
  501. case GL_BOOL:
  502. Driver->extGlUniform1iv(Location, count, (GLint*)bools);
  503. break;
  504. case GL_BOOL_VEC2:
  505. Driver->extGlUniform2iv(Location, count/2, (GLint*)bools);
  506. break;
  507. case GL_BOOL_VEC3:
  508. Driver->extGlUniform3iv(Location, count/3, (GLint*)bools);
  509. break;
  510. case GL_BOOL_VEC4:
  511. Driver->extGlUniform4iv(Location, count/4, (GLint*)bools);
  512. break;
  513. default:
  514. status = false;
  515. break;
  516. }
  517. return status;
  518. #else
  519. return false;
  520. #endif
  521. }
  522. bool COpenGLSLMaterialRenderer::setPixelShaderConstant(const c8* name, const s32* ints, int count)
  523. {
  524. u32 i;
  525. const u32 num = UniformInfo.size();
  526. for (i=0; i < num; ++i)
  527. {
  528. if (UniformInfo[i].name == name)
  529. break;
  530. }
  531. if (i == num)
  532. return false;
  533. #if defined(GL_VERSION_2_0)||defined(GL_ARB_shader_objects)
  534. GLint Location=0;
  535. if (Program2)
  536. Location=Driver->extGlGetUniformLocation(Program2,name);
  537. else
  538. Location=Driver->extGlGetUniformLocationARB(Program,name);
  539. bool status = true;
  540. switch (UniformInfo[i].type)
  541. {
  542. case GL_INT:
  543. Driver->extGlUniform1iv(Location, count, ints);
  544. break;
  545. case GL_INT_VEC2:
  546. Driver->extGlUniform2iv(Location, count/2, ints);
  547. break;
  548. case GL_INT_VEC3:
  549. Driver->extGlUniform3iv(Location, count/3, ints);
  550. break;
  551. case GL_INT_VEC4:
  552. Driver->extGlUniform4iv(Location, count/4, ints);
  553. break;
  554. case GL_SAMPLER_1D:
  555. case GL_SAMPLER_2D:
  556. case GL_SAMPLER_3D:
  557. case GL_SAMPLER_CUBE:
  558. case GL_SAMPLER_1D_SHADOW:
  559. case GL_SAMPLER_2D_SHADOW:
  560. Driver->extGlUniform1iv(Location, 1, ints);
  561. break;
  562. default:
  563. status = false;
  564. break;
  565. }
  566. return status;
  567. #else
  568. return false;
  569. #endif
  570. }
  571. void COpenGLSLMaterialRenderer::setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
  572. {
  573. os::Printer::log("Cannot set constant, use high level shader call.", ELL_WARNING);
  574. }
  575. IVideoDriver* COpenGLSLMaterialRenderer::getVideoDriver()
  576. {
  577. return Driver;
  578. }
  579. } // end namespace video
  580. } // end namespace irr
  581. #endif