PageRenderTime 18ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llrender/llshadermgr.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1104 lines | 856 code | 131 blank | 117 comment | 184 complexity | b30b483738280cdb848d54780ac3fe60 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llshadermgr.cpp
  3. * @brief Shader manager implementation.
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llshadermgr.h"
  28. #include "llfile.h"
  29. #include "llrender.h"
  30. #if LL_DARWIN
  31. #include "OpenGL/OpenGL.h"
  32. #endif
  33. #ifdef LL_RELEASE_FOR_DOWNLOAD
  34. #define UNIFORM_ERRS LL_WARNS_ONCE("Shader")
  35. #else
  36. #define UNIFORM_ERRS LL_ERRS("Shader")
  37. #endif
  38. // Lots of STL stuff in here, using namespace std to keep things more readable
  39. using std::vector;
  40. using std::pair;
  41. using std::make_pair;
  42. using std::string;
  43. LLShaderMgr * LLShaderMgr::sInstance = NULL;
  44. LLShaderMgr::LLShaderMgr()
  45. {
  46. }
  47. LLShaderMgr::~LLShaderMgr()
  48. {
  49. }
  50. // static
  51. LLShaderMgr * LLShaderMgr::instance()
  52. {
  53. if(NULL == sInstance)
  54. {
  55. LL_ERRS("Shaders") << "LLShaderMgr should already have been instantiated by the application!" << LL_ENDL;
  56. }
  57. return sInstance;
  58. }
  59. BOOL LLShaderMgr::attachShaderFeatures(LLGLSLShader * shader)
  60. {
  61. llassert_always(shader != NULL);
  62. LLShaderFeatures *features = & shader->mFeatures;
  63. //////////////////////////////////////
  64. // Attach Vertex Shader Features First
  65. //////////////////////////////////////
  66. // NOTE order of shader object attaching is VERY IMPORTANT!!!
  67. if (features->calculatesAtmospherics)
  68. {
  69. if (features->hasWaterFog)
  70. {
  71. if (!shader->attachObject("windlight/atmosphericsVarsWaterV.glsl"))
  72. {
  73. return FALSE;
  74. }
  75. }
  76. else if (!shader->attachObject("windlight/atmosphericsVarsV.glsl"))
  77. {
  78. return FALSE;
  79. }
  80. }
  81. if (features->calculatesLighting)
  82. {
  83. if (!shader->attachObject("windlight/atmosphericsHelpersV.glsl"))
  84. {
  85. return FALSE;
  86. }
  87. if (features->isSpecular)
  88. {
  89. if (!shader->attachObject("lighting/lightFuncSpecularV.glsl"))
  90. {
  91. return FALSE;
  92. }
  93. if (!features->isAlphaLighting)
  94. {
  95. if (!shader->attachObject("lighting/sumLightsSpecularV.glsl"))
  96. {
  97. return FALSE;
  98. }
  99. }
  100. if (!shader->attachObject("lighting/lightSpecularV.glsl"))
  101. {
  102. return FALSE;
  103. }
  104. }
  105. else
  106. {
  107. if (!shader->attachObject("lighting/lightFuncV.glsl"))
  108. {
  109. return FALSE;
  110. }
  111. if (!features->isAlphaLighting)
  112. {
  113. if (!shader->attachObject("lighting/sumLightsV.glsl"))
  114. {
  115. return FALSE;
  116. }
  117. }
  118. if (!shader->attachObject("lighting/lightV.glsl"))
  119. {
  120. return FALSE;
  121. }
  122. }
  123. }
  124. // NOTE order of shader object attaching is VERY IMPORTANT!!!
  125. if (features->calculatesAtmospherics)
  126. {
  127. if (!shader->attachObject("windlight/atmosphericsV.glsl"))
  128. {
  129. return FALSE;
  130. }
  131. }
  132. if (features->hasSkinning)
  133. {
  134. if (!shader->attachObject("avatar/avatarSkinV.glsl"))
  135. {
  136. return FALSE;
  137. }
  138. }
  139. if (features->hasObjectSkinning)
  140. {
  141. if (!shader->attachObject("avatar/objectSkinV.glsl"))
  142. {
  143. return FALSE;
  144. }
  145. }
  146. ///////////////////////////////////////
  147. // Attach Fragment Shader Features Next
  148. ///////////////////////////////////////
  149. if(features->calculatesAtmospherics)
  150. {
  151. if (features->hasWaterFog)
  152. {
  153. if (!shader->attachObject("windlight/atmosphericsVarsWaterF.glsl"))
  154. {
  155. return FALSE;
  156. }
  157. }
  158. else if (!shader->attachObject("windlight/atmosphericsVarsF.glsl"))
  159. {
  160. return FALSE;
  161. }
  162. }
  163. // NOTE order of shader object attaching is VERY IMPORTANT!!!
  164. if (features->hasGamma)
  165. {
  166. if (!shader->attachObject("windlight/gammaF.glsl"))
  167. {
  168. return FALSE;
  169. }
  170. }
  171. if (features->hasAtmospherics)
  172. {
  173. if (!shader->attachObject("windlight/atmosphericsF.glsl"))
  174. {
  175. return FALSE;
  176. }
  177. }
  178. if (features->hasTransport)
  179. {
  180. if (!shader->attachObject("windlight/transportF.glsl"))
  181. {
  182. return FALSE;
  183. }
  184. // Test hasFullbright and hasShiny and attach fullbright and
  185. // fullbright shiny atmos transport if we split them out.
  186. }
  187. // NOTE order of shader object attaching is VERY IMPORTANT!!!
  188. if (features->hasWaterFog)
  189. {
  190. if (!shader->attachObject("environment/waterFogF.glsl"))
  191. {
  192. return FALSE;
  193. }
  194. }
  195. if (features->hasLighting)
  196. {
  197. if (features->hasWaterFog)
  198. {
  199. if (features->disableTextureIndex)
  200. {
  201. if (features->hasAlphaMask)
  202. {
  203. if (!shader->attachObject("lighting/lightWaterAlphaMaskNonIndexedF.glsl"))
  204. {
  205. return FALSE;
  206. }
  207. }
  208. else
  209. {
  210. if (!shader->attachObject("lighting/lightWaterNonIndexedF.glsl"))
  211. {
  212. return FALSE;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. if (features->hasAlphaMask)
  219. {
  220. if (!shader->attachObject("lighting/lightWaterAlphaMaskF.glsl"))
  221. {
  222. return FALSE;
  223. }
  224. }
  225. else
  226. {
  227. if (!shader->attachObject("lighting/lightWaterF.glsl"))
  228. {
  229. return FALSE;
  230. }
  231. }
  232. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  233. }
  234. }
  235. else
  236. {
  237. if (features->disableTextureIndex)
  238. {
  239. if (features->hasAlphaMask)
  240. {
  241. if (!shader->attachObject("lighting/lightAlphaMaskNonIndexedF.glsl"))
  242. {
  243. return FALSE;
  244. }
  245. }
  246. else
  247. {
  248. if (!shader->attachObject("lighting/lightNonIndexedF.glsl"))
  249. {
  250. return FALSE;
  251. }
  252. }
  253. }
  254. else
  255. {
  256. if (features->hasAlphaMask)
  257. {
  258. if (!shader->attachObject("lighting/lightAlphaMaskF.glsl"))
  259. {
  260. return FALSE;
  261. }
  262. }
  263. else
  264. {
  265. if (!shader->attachObject("lighting/lightF.glsl"))
  266. {
  267. return FALSE;
  268. }
  269. }
  270. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  271. }
  272. }
  273. }
  274. // NOTE order of shader object attaching is VERY IMPORTANT!!!
  275. else if (features->isFullbright)
  276. {
  277. if (features->isShiny && features->hasWaterFog)
  278. {
  279. if (features->disableTextureIndex)
  280. {
  281. if (!shader->attachObject("lighting/lightFullbrightShinyWaterNonIndexedF.glsl"))
  282. {
  283. return FALSE;
  284. }
  285. }
  286. else
  287. {
  288. if (!shader->attachObject("lighting/lightFullbrightShinyWaterF.glsl"))
  289. {
  290. return FALSE;
  291. }
  292. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  293. }
  294. }
  295. else if (features->hasWaterFog)
  296. {
  297. if (features->disableTextureIndex)
  298. {
  299. if (features->hasAlphaMask)
  300. {
  301. if (!shader->attachObject("lighting/lightFullbrightWaterNonIndexedAlphaMaskF.glsl"))
  302. {
  303. return FALSE;
  304. }
  305. }
  306. else if (!shader->attachObject("lighting/lightFullbrightWaterNonIndexedF.glsl"))
  307. {
  308. return FALSE;
  309. }
  310. }
  311. else
  312. {
  313. if (features->hasAlphaMask)
  314. {
  315. if (!shader->attachObject("lighting/lightFullbrightWaterAlphaMaskF.glsl"))
  316. {
  317. return FALSE;
  318. }
  319. }
  320. else if (!shader->attachObject("lighting/lightFullbrightWaterF.glsl"))
  321. {
  322. return FALSE;
  323. }
  324. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  325. }
  326. }
  327. else if (features->isShiny)
  328. {
  329. if (features->disableTextureIndex)
  330. {
  331. if (!shader->attachObject("lighting/lightFullbrightShinyNonIndexedF.glsl"))
  332. {
  333. return FALSE;
  334. }
  335. }
  336. else
  337. {
  338. if (!shader->attachObject("lighting/lightFullbrightShinyF.glsl"))
  339. {
  340. return FALSE;
  341. }
  342. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  343. }
  344. }
  345. else
  346. {
  347. if (features->disableTextureIndex)
  348. {
  349. if (features->hasAlphaMask)
  350. {
  351. if (!shader->attachObject("lighting/lightFullbrightNonIndexedAlphaMaskF.glsl"))
  352. {
  353. return FALSE;
  354. }
  355. }
  356. else
  357. {
  358. if (!shader->attachObject("lighting/lightFullbrightNonIndexedF.glsl"))
  359. {
  360. return FALSE;
  361. }
  362. }
  363. }
  364. else
  365. {
  366. if (features->hasAlphaMask)
  367. {
  368. if (!shader->attachObject("lighting/lightFullbrightAlphaMaskF.glsl"))
  369. {
  370. return FALSE;
  371. }
  372. }
  373. else
  374. {
  375. if (!shader->attachObject("lighting/lightFullbrightF.glsl"))
  376. {
  377. return FALSE;
  378. }
  379. }
  380. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  381. }
  382. }
  383. }
  384. // NOTE order of shader object attaching is VERY IMPORTANT!!!
  385. else if (features->isShiny)
  386. {
  387. if (features->hasWaterFog)
  388. {
  389. if (features->disableTextureIndex)
  390. {
  391. if (!shader->attachObject("lighting/lightShinyWaterNonIndexedF.glsl"))
  392. {
  393. return FALSE;
  394. }
  395. }
  396. else
  397. {
  398. if (!shader->attachObject("lighting/lightShinyWaterF.glsl"))
  399. {
  400. return FALSE;
  401. }
  402. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  403. }
  404. }
  405. else
  406. {
  407. if (features->disableTextureIndex)
  408. {
  409. if (!shader->attachObject("lighting/lightShinyNonIndexedF.glsl"))
  410. {
  411. return FALSE;
  412. }
  413. }
  414. else
  415. {
  416. if (!shader->attachObject("lighting/lightShinyF.glsl"))
  417. {
  418. return FALSE;
  419. }
  420. shader->mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
  421. }
  422. }
  423. }
  424. if (features->mIndexedTextureChannels <= 1)
  425. {
  426. if (!shader->attachObject("objects/nonindexedTextureV.glsl"))
  427. {
  428. return FALSE;
  429. }
  430. }
  431. else
  432. {
  433. if (!shader->attachObject("objects/indexedTextureV.glsl"))
  434. {
  435. return FALSE;
  436. }
  437. }
  438. return TRUE;
  439. }
  440. //============================================================================
  441. // Load Shader
  442. static std::string get_object_log(GLhandleARB ret)
  443. {
  444. std::string res;
  445. //get log length
  446. GLint length;
  447. glGetObjectParameterivARB(ret, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
  448. if (length > 0)
  449. {
  450. //the log could be any size, so allocate appropriately
  451. GLcharARB* log = new GLcharARB[length];
  452. glGetInfoLogARB(ret, length, &length, log);
  453. res = std::string((char *)log);
  454. delete[] log;
  455. }
  456. return res;
  457. }
  458. void LLShaderMgr::dumpObjectLog(GLhandleARB ret, BOOL warns)
  459. {
  460. std::string log = get_object_log(ret);
  461. if ( log.length() > 0 )
  462. {
  463. if (warns)
  464. {
  465. LL_WARNS("ShaderLoading") << log << LL_ENDL;
  466. }
  467. else
  468. {
  469. LL_INFOS("ShaderLoading") << log << LL_ENDL;
  470. }
  471. }
  472. }
  473. GLhandleARB LLShaderMgr::loadShaderFile(const std::string& filename, S32 & shader_level, GLenum type, S32 texture_index_channels)
  474. {
  475. GLenum error = GL_NO_ERROR;
  476. if (gDebugGL)
  477. {
  478. error = glGetError();
  479. if (error != GL_NO_ERROR)
  480. {
  481. LL_WARNS("ShaderLoading") << "GL ERROR entering loadShaderFile(): " << error << LL_ENDL;
  482. }
  483. }
  484. LL_DEBUGS("ShaderLoading") << "Loading shader file: " << filename << " class " << shader_level << LL_ENDL;
  485. if (filename.empty())
  486. {
  487. return 0;
  488. }
  489. //read in from file
  490. LLFILE* file = NULL;
  491. S32 try_gpu_class = shader_level;
  492. S32 gpu_class;
  493. //find the most relevant file
  494. for (gpu_class = try_gpu_class; gpu_class > 0; gpu_class--)
  495. { //search from the current gpu class down to class 1 to find the most relevant shader
  496. std::stringstream fname;
  497. fname << getShaderDirPrefix();
  498. fname << gpu_class << "/" << filename;
  499. LL_DEBUGS("ShaderLoading") << "Looking in " << fname.str() << LL_ENDL;
  500. file = LLFile::fopen(fname.str(), "r"); /* Flawfinder: ignore */
  501. if (file)
  502. {
  503. LL_INFOS("ShaderLoading") << "Loading file: shaders/class" << gpu_class << "/" << filename << " (Want class " << gpu_class << ")" << LL_ENDL;
  504. break; // done
  505. }
  506. }
  507. if (file == NULL)
  508. {
  509. LL_WARNS("ShaderLoading") << "GLSL Shader file not found: " << filename << LL_ENDL;
  510. return 0;
  511. }
  512. //we can't have any lines longer than 1024 characters
  513. //or any shaders longer than 4096 lines... deal - DaveP
  514. GLcharARB buff[1024];
  515. GLcharARB* text[4096];
  516. GLuint count = 0;
  517. F32 version = gGLManager.mGLVersion;
  518. //hack to never use GLSL > 1.20 on OSX
  519. #if LL_DARWIN
  520. version = llmin(version, 2.9f);
  521. #endif
  522. if (version < 2.1f)
  523. {
  524. text[count++] = strdup("#version 110\n");
  525. text[count++] = strdup("#define ATTRIBUTE attribute\n");
  526. text[count++] = strdup("#define VARYING varying\n");
  527. }
  528. else if (version < 3.3f)
  529. {
  530. //set version to 1.20
  531. text[count++] = strdup("#version 120\n");
  532. text[count++] = strdup("#define FXAA_GLSL_120 1\n");
  533. text[count++] = strdup("#define FXAA_FAST_PIXEL_OFFSET 0\n");
  534. text[count++] = strdup("#define ATTRIBUTE attribute\n");
  535. text[count++] = strdup("#define VARYING varying\n");
  536. }
  537. else
  538. {
  539. if (version < 4.f)
  540. {
  541. //set version to 1.30
  542. text[count++] = strdup("#version 130\n");
  543. }
  544. else
  545. { //set version to 400
  546. text[count++] = strdup("#version 400\n");
  547. }
  548. text[count++] = strdup("#define DEFINE_GL_FRAGCOLOR 1\n");
  549. text[count++] = strdup("#define FXAA_GLSL_130 1\n");
  550. text[count++] = strdup("#define ATTRIBUTE in\n");
  551. if (type == GL_VERTEX_SHADER_ARB)
  552. { //"varying" state is "out" in a vertex program, "in" in a fragment program
  553. // ("varying" is deprecated after version 1.20)
  554. text[count++] = strdup("#define VARYING out\n");
  555. }
  556. else
  557. {
  558. text[count++] = strdup("#define VARYING in\n");
  559. }
  560. //backwards compatibility with legacy texture lookup syntax
  561. text[count++] = strdup("#define textureCube texture\n");
  562. text[count++] = strdup("#define texture2DLod textureLod\n");
  563. text[count++] = strdup("#define shadow2D(a,b) vec2(texture(a,b))\n");
  564. }
  565. //copy preprocessor definitions into buffer
  566. for (std::map<std::string,std::string>::iterator iter = mDefinitions.begin(); iter != mDefinitions.end(); ++iter)
  567. {
  568. std::string define = "#define " + iter->first + " " + iter->second + "\n";
  569. text[count++] = (GLcharARB *) strdup(define.c_str());
  570. }
  571. if (texture_index_channels > 0 && type == GL_FRAGMENT_SHADER_ARB)
  572. {
  573. //use specified number of texture channels for indexed texture rendering
  574. /* prepend shader code that looks like this:
  575. uniform sampler2D tex0;
  576. uniform sampler2D tex1;
  577. uniform sampler2D tex2;
  578. .
  579. .
  580. .
  581. uniform sampler2D texN;
  582. VARYING float vary_texture_index;
  583. vec4 diffuseLookup(vec2 texcoord)
  584. {
  585. switch (int(vary_texture_index+0.25))
  586. {
  587. case 0: return texture2D(tex0, texcoord);
  588. case 1: return texture2D(tex1, texcoord);
  589. case 2: return texture2D(tex2, texcoord);
  590. .
  591. .
  592. .
  593. case N: return texture2D(texN, texcoord);
  594. }
  595. return vec4(0,0,0,0);
  596. }
  597. */
  598. //uniform declartion
  599. for (S32 i = 0; i < texture_index_channels; ++i)
  600. {
  601. std::string decl = llformat("uniform sampler2D tex%d;\n", i);
  602. text[count++] = strdup(decl.c_str());
  603. }
  604. if (texture_index_channels > 1)
  605. {
  606. text[count++] = strdup("VARYING float vary_texture_index;\n");
  607. }
  608. text[count++] = strdup("vec4 diffuseLookup(vec2 texcoord)\n");
  609. text[count++] = strdup("{\n");
  610. if (texture_index_channels == 1)
  611. { //don't use flow control, that's silly
  612. text[count++] = strdup("return texture2D(tex0, texcoord);\n");
  613. text[count++] = strdup("}\n");
  614. }
  615. else if (gGLManager.mGLVersion >= 3.f)
  616. {
  617. text[count++] = strdup("\tswitch (int(vary_texture_index+0.25))\n");
  618. text[count++] = strdup("\t{\n");
  619. //switch body
  620. for (S32 i = 0; i < texture_index_channels; ++i)
  621. {
  622. std::string case_str = llformat("\t\tcase %d: return texture2D(tex%d, texcoord);\n", i, i);
  623. text[count++] = strdup(case_str.c_str());
  624. }
  625. text[count++] = strdup("\t}\n");
  626. text[count++] = strdup("\treturn vec4(1,0,1,1);\n");
  627. text[count++] = strdup("}\n");
  628. }
  629. else
  630. {
  631. //switches aren't supported, make block that looks like:
  632. /*
  633. int ti = int(vary_texture_index+0.25);
  634. if (ti == 0) return texture2D(tex0, texcoord);
  635. if (ti == 1) return texture2D(tex1, texcoord);
  636. .
  637. .
  638. .
  639. if (ti == N) return texture2D(texN, texcoord);
  640. */
  641. text[count++] = strdup("int ti = int(vary_texture_index+0.25);\n");
  642. for (S32 i = 0; i < texture_index_channels; ++i)
  643. {
  644. std::string if_str = llformat("if (ti == %d) return texture2D(tex%d, texcoord);\n", i, i);
  645. text[count++] = strdup(if_str.c_str());
  646. }
  647. text[count++] = strdup("\treturn vec4(1,0,1,1);\n");
  648. text[count++] = strdup("}\n");
  649. }
  650. }
  651. //copy file into memory
  652. while( fgets((char *)buff, 1024, file) != NULL && count < LL_ARRAY_SIZE(text) )
  653. {
  654. text[count++] = (GLcharARB *)strdup((char *)buff);
  655. }
  656. fclose(file);
  657. //create shader object
  658. GLhandleARB ret = glCreateShaderObjectARB(type);
  659. if (gDebugGL)
  660. {
  661. error = glGetError();
  662. if (error != GL_NO_ERROR)
  663. {
  664. LL_WARNS("ShaderLoading") << "GL ERROR in glCreateShaderObjectARB: " << error << LL_ENDL;
  665. }
  666. }
  667. //load source
  668. glShaderSourceARB(ret, count, (const GLcharARB**) text, NULL);
  669. if (gDebugGL)
  670. {
  671. error = glGetError();
  672. if (error != GL_NO_ERROR)
  673. {
  674. LL_WARNS("ShaderLoading") << "GL ERROR in glShaderSourceARB: " << error << LL_ENDL;
  675. }
  676. }
  677. //compile source
  678. glCompileShaderARB(ret);
  679. if (gDebugGL)
  680. {
  681. error = glGetError();
  682. if (error != GL_NO_ERROR)
  683. {
  684. LL_WARNS("ShaderLoading") << "GL ERROR in glCompileShaderARB: " << error << LL_ENDL;
  685. }
  686. }
  687. if (error == GL_NO_ERROR)
  688. {
  689. //check for errors
  690. GLint success = GL_TRUE;
  691. glGetObjectParameterivARB(ret, GL_OBJECT_COMPILE_STATUS_ARB, &success);
  692. if (gDebugGL || success == GL_FALSE)
  693. {
  694. error = glGetError();
  695. if (error != GL_NO_ERROR || success == GL_FALSE)
  696. {
  697. //an error occured, print log
  698. LL_WARNS("ShaderLoading") << "GLSL Compilation Error: (" << error << ") in " << filename << LL_ENDL;
  699. dumpObjectLog(ret);
  700. #if LL_WINDOWS
  701. std::stringstream ostr;
  702. //dump shader source for debugging
  703. for (GLuint i = 0; i < count; i++)
  704. {
  705. ostr << i << ": " << text[i];
  706. if (i % 128 == 0)
  707. { //dump every 128 lines
  708. LL_WARNS("ShaderLoading") << "\n" << ostr.str() << llendl;
  709. ostr = std::stringstream();
  710. }
  711. }
  712. LL_WARNS("ShaderLoading") << "\n" << ostr.str() << llendl;
  713. #endif // LL_WINDOWS
  714. ret = 0;
  715. }
  716. }
  717. }
  718. else
  719. {
  720. ret = 0;
  721. }
  722. stop_glerror();
  723. //free memory
  724. for (GLuint i = 0; i < count; i++)
  725. {
  726. free(text[i]);
  727. }
  728. //successfully loaded, save results
  729. if (ret)
  730. {
  731. // Add shader file to map
  732. mShaderObjects[filename] = ret;
  733. shader_level = try_gpu_class;
  734. }
  735. else
  736. {
  737. if (shader_level > 1)
  738. {
  739. shader_level--;
  740. return loadShaderFile(filename,shader_level,type,texture_index_channels);
  741. }
  742. LL_WARNS("ShaderLoading") << "Failed to load " << filename << LL_ENDL;
  743. }
  744. return ret;
  745. }
  746. BOOL LLShaderMgr::linkProgramObject(GLhandleARB obj, BOOL suppress_errors)
  747. {
  748. //check for errors
  749. glLinkProgramARB(obj);
  750. GLint success = GL_TRUE;
  751. glGetObjectParameterivARB(obj, GL_OBJECT_LINK_STATUS_ARB, &success);
  752. if (!suppress_errors && success == GL_FALSE)
  753. {
  754. //an error occured, print log
  755. LL_WARNS("ShaderLoading") << "GLSL Linker Error:" << LL_ENDL;
  756. }
  757. #if LL_DARWIN
  758. // For some reason this absolutely kills the frame rate when VBO's are enabled
  759. if (0)
  760. {
  761. // Force an evaluation of the gl state so the driver can tell if the shader will run in hardware or software
  762. // per Apple's suggestion
  763. LLGLSLShader::sNoFixedFunction = false;
  764. glUseProgramObjectARB(obj);
  765. gGL.begin(LLRender::TRIANGLES);
  766. gGL.vertex3f(0.0f, 0.0f, 0.0f);
  767. gGL.vertex3f(0.0f, 0.0f, 0.0f);
  768. gGL.vertex3f(0.0f, 0.0f, 0.0f);
  769. gGL.end();
  770. gGL.flush();
  771. glUseProgramObjectARB(0);
  772. LLGLSLShader::sNoFixedFunction = true;
  773. // Query whether the shader can or cannot run in hardware
  774. // http://developer.apple.com/qa/qa2007/qa1502.html
  775. GLint vertexGPUProcessing, fragmentGPUProcessing;
  776. CGLContextObj ctx = CGLGetCurrentContext();
  777. CGLGetParameter(ctx, kCGLCPGPUVertexProcessing, &vertexGPUProcessing);
  778. CGLGetParameter(ctx, kCGLCPGPUFragmentProcessing, &fragmentGPUProcessing);
  779. if (!fragmentGPUProcessing || !vertexGPUProcessing)
  780. {
  781. LL_WARNS("ShaderLoading") << "GLSL Linker: Running in Software:" << LL_ENDL;
  782. success = GL_FALSE;
  783. suppress_errors = FALSE;
  784. }
  785. }
  786. #else
  787. std::string log = get_object_log(obj);
  788. LLStringUtil::toLower(log);
  789. if (log.find("software") != std::string::npos)
  790. {
  791. LL_WARNS("ShaderLoading") << "GLSL Linker: Running in Software:" << LL_ENDL;
  792. success = GL_FALSE;
  793. suppress_errors = FALSE;
  794. }
  795. #endif
  796. if (!suppress_errors)
  797. {
  798. dumpObjectLog(obj, !success);
  799. }
  800. return success;
  801. }
  802. BOOL LLShaderMgr::validateProgramObject(GLhandleARB obj)
  803. {
  804. //check program validity against current GL
  805. glValidateProgramARB(obj);
  806. GLint success = GL_TRUE;
  807. glGetObjectParameterivARB(obj, GL_OBJECT_VALIDATE_STATUS_ARB, &success);
  808. if (success == GL_FALSE)
  809. {
  810. LL_WARNS("ShaderLoading") << "GLSL program not valid: " << LL_ENDL;
  811. dumpObjectLog(obj);
  812. }
  813. else
  814. {
  815. dumpObjectLog(obj, FALSE);
  816. }
  817. return success;
  818. }
  819. //virtual
  820. void LLShaderMgr::initAttribsAndUniforms()
  821. {
  822. //MUST match order of enum in LLVertexBuffer.h
  823. mReservedAttribs.push_back("position");
  824. mReservedAttribs.push_back("normal");
  825. mReservedAttribs.push_back("texcoord0");
  826. mReservedAttribs.push_back("texcoord1");
  827. mReservedAttribs.push_back("texcoord2");
  828. mReservedAttribs.push_back("texcoord3");
  829. mReservedAttribs.push_back("diffuse_color");
  830. mReservedAttribs.push_back("emissive");
  831. mReservedAttribs.push_back("binormal");
  832. mReservedAttribs.push_back("weight");
  833. mReservedAttribs.push_back("weight4");
  834. mReservedAttribs.push_back("clothing");
  835. mReservedAttribs.push_back("texture_index");
  836. //matrix state
  837. mReservedUniforms.push_back("modelview_matrix");
  838. mReservedUniforms.push_back("projection_matrix");
  839. mReservedUniforms.push_back("inv_proj");
  840. mReservedUniforms.push_back("modelview_projection_matrix");
  841. mReservedUniforms.push_back("normal_matrix");
  842. mReservedUniforms.push_back("texture_matrix0");
  843. mReservedUniforms.push_back("texture_matrix1");
  844. mReservedUniforms.push_back("texture_matrix2");
  845. mReservedUniforms.push_back("texture_matrix3");
  846. llassert(mReservedUniforms.size() == LLShaderMgr::TEXTURE_MATRIX3+1);
  847. mReservedUniforms.push_back("viewport");
  848. mReservedUniforms.push_back("light_position");
  849. mReservedUniforms.push_back("light_direction");
  850. mReservedUniforms.push_back("light_attenuation");
  851. mReservedUniforms.push_back("light_diffuse");
  852. mReservedUniforms.push_back("light_ambient");
  853. mReservedUniforms.push_back("light_count");
  854. mReservedUniforms.push_back("light");
  855. mReservedUniforms.push_back("light_col");
  856. mReservedUniforms.push_back("far_z");
  857. llassert(mReservedUniforms.size() == LLShaderMgr::MULTI_LIGHT_FAR_Z+1);
  858. mReservedUniforms.push_back("proj_mat");
  859. mReservedUniforms.push_back("proj_near");
  860. mReservedUniforms.push_back("proj_p");
  861. mReservedUniforms.push_back("proj_n");
  862. mReservedUniforms.push_back("proj_origin");
  863. mReservedUniforms.push_back("proj_range");
  864. mReservedUniforms.push_back("proj_ambiance");
  865. mReservedUniforms.push_back("proj_shadow_idx");
  866. mReservedUniforms.push_back("shadow_fade");
  867. mReservedUniforms.push_back("proj_focus");
  868. mReservedUniforms.push_back("proj_lod");
  869. mReservedUniforms.push_back("proj_ambient_lod");
  870. llassert(mReservedUniforms.size() == LLShaderMgr::PROJECTOR_AMBIENT_LOD+1);
  871. mReservedUniforms.push_back("color");
  872. mReservedUniforms.push_back("diffuseMap");
  873. mReservedUniforms.push_back("specularMap");
  874. mReservedUniforms.push_back("bumpMap");
  875. mReservedUniforms.push_back("environmentMap");
  876. mReservedUniforms.push_back("cloude_noise_texture");
  877. mReservedUniforms.push_back("fullbright");
  878. mReservedUniforms.push_back("lightnorm");
  879. mReservedUniforms.push_back("sunlight_color_copy");
  880. mReservedUniforms.push_back("ambient");
  881. mReservedUniforms.push_back("blue_horizon");
  882. mReservedUniforms.push_back("blue_density");
  883. mReservedUniforms.push_back("haze_horizon");
  884. mReservedUniforms.push_back("haze_density");
  885. mReservedUniforms.push_back("cloud_shadow");
  886. mReservedUniforms.push_back("density_multiplier");
  887. mReservedUniforms.push_back("distance_multiplier");
  888. mReservedUniforms.push_back("max_y");
  889. mReservedUniforms.push_back("glow");
  890. mReservedUniforms.push_back("cloud_color");
  891. mReservedUniforms.push_back("cloud_pos_density1");
  892. mReservedUniforms.push_back("cloud_pos_density2");
  893. mReservedUniforms.push_back("cloud_scale");
  894. mReservedUniforms.push_back("gamma");
  895. mReservedUniforms.push_back("scene_light_strength");
  896. llassert(mReservedUniforms.size() == LLShaderMgr::SCENE_LIGHT_STRENGTH+1);
  897. mReservedUniforms.push_back("center");
  898. mReservedUniforms.push_back("size");
  899. mReservedUniforms.push_back("falloff");
  900. mReservedUniforms.push_back("minLuminance");
  901. mReservedUniforms.push_back("maxExtractAlpha");
  902. mReservedUniforms.push_back("lumWeights");
  903. mReservedUniforms.push_back("warmthWeights");
  904. mReservedUniforms.push_back("warmthAmount");
  905. mReservedUniforms.push_back("glowStrength");
  906. mReservedUniforms.push_back("glowDelta");
  907. llassert(mReservedUniforms.size() == LLShaderMgr::GLOW_DELTA+1);
  908. mReservedUniforms.push_back("minimum_alpha");
  909. mReservedUniforms.push_back("shadow_matrix");
  910. mReservedUniforms.push_back("env_mat");
  911. mReservedUniforms.push_back("shadow_clip");
  912. mReservedUniforms.push_back("sun_wash");
  913. mReservedUniforms.push_back("shadow_noise");
  914. mReservedUniforms.push_back("blur_size");
  915. mReservedUniforms.push_back("ssao_radius");
  916. mReservedUniforms.push_back("ssao_max_radius");
  917. mReservedUniforms.push_back("ssao_factor");
  918. mReservedUniforms.push_back("ssao_factor_inv");
  919. mReservedUniforms.push_back("ssao_effect_mat");
  920. mReservedUniforms.push_back("screen_res");
  921. mReservedUniforms.push_back("near_clip");
  922. mReservedUniforms.push_back("shadow_offset");
  923. mReservedUniforms.push_back("shadow_bias");
  924. mReservedUniforms.push_back("spot_shadow_bias");
  925. mReservedUniforms.push_back("spot_shadow_offset");
  926. mReservedUniforms.push_back("sun_dir");
  927. mReservedUniforms.push_back("shadow_res");
  928. mReservedUniforms.push_back("proj_shadow_res");
  929. mReservedUniforms.push_back("depth_cutoff");
  930. mReservedUniforms.push_back("norm_cutoff");
  931. llassert(mReservedUniforms.size() == LLShaderMgr::DEFERRED_NORM_CUTOFF+1);
  932. mReservedUniforms.push_back("tc_scale");
  933. mReservedUniforms.push_back("rcp_screen_res");
  934. mReservedUniforms.push_back("rcp_frame_opt");
  935. mReservedUniforms.push_back("rcp_frame_opt2");
  936. mReservedUniforms.push_back("focal_distance");
  937. mReservedUniforms.push_back("blur_constant");
  938. mReservedUniforms.push_back("tan_pixel_angle");
  939. mReservedUniforms.push_back("magnification");
  940. mReservedUniforms.push_back("max_cof");
  941. mReservedUniforms.push_back("res_scale");
  942. mReservedUniforms.push_back("depthMap");
  943. mReservedUniforms.push_back("shadowMap0");
  944. mReservedUniforms.push_back("shadowMap1");
  945. mReservedUniforms.push_back("shadowMap2");
  946. mReservedUniforms.push_back("shadowMap3");
  947. mReservedUniforms.push_back("shadowMap4");
  948. mReservedUniforms.push_back("shadowMap5");
  949. llassert(mReservedUniforms.size() == LLShaderMgr::DEFERRED_SHADOW5+1);
  950. mReservedUniforms.push_back("normalMap");
  951. mReservedUniforms.push_back("positionMap");
  952. mReservedUniforms.push_back("diffuseRect");
  953. mReservedUniforms.push_back("specularRect");
  954. mReservedUniforms.push_back("noiseMap");
  955. mReservedUniforms.push_back("lightFunc");
  956. mReservedUniforms.push_back("lightMap");
  957. mReservedUniforms.push_back("bloomMap");
  958. mReservedUniforms.push_back("projectionMap");
  959. llassert(mReservedUniforms.size() == END_RESERVED_UNIFORMS);
  960. std::set<std::string> dupe_check;
  961. for (U32 i = 0; i < mReservedUniforms.size(); ++i)
  962. {
  963. if (dupe_check.find(mReservedUniforms[i]) != dupe_check.end())
  964. {
  965. llerrs << "Duplicate reserved uniform name found: " << mReservedUniforms[i] << llendl;
  966. }
  967. dupe_check.insert(mReservedUniforms[i]);
  968. }
  969. }