OgreGLSLProgram.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreGpuProgram.h"
  25. #include "OgreRenderSystem.h"
  26. #include "OgreRenderSystemCapabilities.h"
  27. #include "OgreStringConverter.h"
  28. #include "OgreGpuProgramManager.h"
  29. #include "OgreHighLevelGpuProgramManager.h"
  30. #include "OgreException.h"
  31. #include "OgreGLSLProgram.h"
  32. #include "OgreGLSLGpuProgram.h"
  33. #include "OgreGLSLExtSupport.h"
  34. #include "OgreGLSLLinkProgramManager.h"
  35. #include "OgreGLSLPreprocessor.h"
  36. namespace Ogre {
  37. //---------------------------------------------------------------------------
  38. GLSLProgram::~GLSLProgram()
  39. {
  40. unload();
  41. }
  42. //-----------------------------------------------------------------------
  43. void GLSLProgram::loadFromSource(void)
  44. {
  45. // only create a shader object if glsl is supported
  46. if (isSupported())
  47. {
  48. checkForGLSLError( "GLSLProgram::loadFromSource", "GL Errors before creating shader object", 0 );
  49. // create shader object
  50. GLenum shaderType = 0x0000;
  51. switch (mType)
  52. {
  53. case GPT_VERTEX_PROGRAM:
  54. shaderType = GL_VERTEX_SHADER_ARB;
  55. break;
  56. case GPT_FRAGMENT_PROGRAM:
  57. shaderType = GL_FRAGMENT_SHADER_ARB;
  58. break;
  59. case GPT_GEOMETRY_PROGRAM:
  60. shaderType = GL_GEOMETRY_SHADER_EXT;
  61. break;
  62. }
  63. mGLHandle = glCreateShaderObjectARB(shaderType);
  64. checkForGLSLError( "GLSLProgram::loadFromSource", "Error creating GLSL shader object", 0 );
  65. }
  66. // Preprocess the GLSL shader in order to get a clean source
  67. CPreprocessor cpp;
  68. // Pass all user-defined macros to preprocessor
  69. if (!mPreprocessorDefines.empty ())
  70. {
  71. String::size_type pos = 0;
  72. while (pos != String::npos)
  73. {
  74. // Find delims
  75. String::size_type endPos = mPreprocessorDefines.find_first_of(";,=", pos);
  76. if (endPos != String::npos)
  77. {
  78. String::size_type macro_name_start = pos;
  79. size_t macro_name_len = endPos - pos;
  80. pos = endPos;
  81. // Check definition part
  82. if (mPreprocessorDefines[pos] == '=')
  83. {
  84. // set up a definition, skip delim
  85. ++pos;
  86. String::size_type macro_val_start = pos;
  87. size_t macro_val_len;
  88. endPos = mPreprocessorDefines.find_first_of(";,", pos);
  89. if (endPos == String::npos)
  90. {
  91. macro_val_len = mPreprocessorDefines.size () - pos;
  92. pos = endPos;
  93. }
  94. else
  95. {
  96. macro_val_len = endPos - pos;
  97. pos = endPos+1;
  98. }
  99. cpp.Define (
  100. mPreprocessorDefines.c_str () + macro_name_start, macro_name_len,
  101. mPreprocessorDefines.c_str () + macro_val_start, macro_val_len);
  102. }
  103. else
  104. {
  105. // No definition part, define as "1"
  106. ++pos;
  107. cpp.Define (
  108. mPreprocessorDefines.c_str () + macro_name_start, macro_name_len, 1);
  109. }
  110. }
  111. else
  112. pos = endPos;
  113. }
  114. }
  115. size_t out_size = 0;
  116. const char *src = mSource.c_str ();
  117. size_t src_len = mSource.size ();
  118. char *out = cpp.Parse (src, src_len, out_size);
  119. if (!out || !out_size)
  120. // Failed to preprocess, break out
  121. OGRE_EXCEPT (Exception::ERR_RENDERINGAPI_ERROR,
  122. "Failed to preprocess shader ",
  123. __FUNCTION__);
  124. mSource = String (out, out_size);
  125. if (out < src || out > src + src_len)
  126. free (out);
  127. // Add preprocessor extras and main source
  128. if (!mSource.empty())
  129. {
  130. const char *source = mSource.c_str();
  131. glShaderSourceARB(mGLHandle, 1, &source, NULL);
  132. // check for load errors
  133. checkForGLSLError( "GLSLProgram::loadFromSource", "Cannot load GLSL high-level shader source : ", 0 );
  134. }
  135. compile();
  136. }
  137. //---------------------------------------------------------------------------
  138. bool GLSLProgram::compile(const bool checkErrors)
  139. {
  140. if (checkErrors)
  141. {
  142. logObjectInfo("GLSL compiling: ", mGLHandle);
  143. }
  144. glCompileShaderARB(mGLHandle);
  145. // check for compile errors
  146. glGetObjectParameterivARB(mGLHandle, GL_OBJECT_COMPILE_STATUS_ARB, &mCompiled);
  147. // force exception if not compiled
  148. if (checkErrors)
  149. {
  150. checkForGLSLError( "GLSLProgram::compile", "Cannot compile GLSL high-level shader : ", mGLHandle, !mCompiled, !mCompiled );
  151. if (mCompiled)
  152. {
  153. logObjectInfo("GLSL compiled : ", mGLHandle);
  154. }
  155. }
  156. return (mCompiled == 1);
  157. }
  158. //-----------------------------------------------------------------------
  159. void GLSLProgram::createLowLevelImpl(void)
  160. {
  161. mAssemblerProgram = GpuProgramPtr(OGRE_NEW GLSLGpuProgram( this ));
  162. }
  163. //---------------------------------------------------------------------------
  164. void GLSLProgram::unloadImpl()
  165. {
  166. // We didn't create mAssemblerProgram through a manager, so override this
  167. // implementation so that we don't try to remove it from one. Since getCreator()
  168. // is used, it might target a different matching handle!
  169. mAssemblerProgram = nullptr;
  170. unloadHighLevel();
  171. }
  172. //-----------------------------------------------------------------------
  173. void GLSLProgram::unloadHighLevelImpl(void)
  174. {
  175. if (isSupported())
  176. {
  177. glDeleteObjectARB(mGLHandle);
  178. }
  179. }
  180. //-----------------------------------------------------------------------
  181. void GLSLProgram::populateParameterNames(GpuProgramParametersSharedPtr params)
  182. {
  183. getConstantDefinitions();
  184. params->_setNamedConstants(mConstantDefs);
  185. // Don't set logical / physical maps here, as we can't access parameters by logical index in GLHL.
  186. }
  187. //-----------------------------------------------------------------------
  188. void GLSLProgram::buildConstantDefinitions() const
  189. {
  190. // We need an accurate list of all the uniforms in the shader, but we
  191. // can't get at them until we link all the shaders into a program object.
  192. // Therefore instead, parse the source code manually and extract the uniforms
  193. createParameterMappingStructures(true);
  194. GLSLLinkProgramManager::getSingleton().extractConstantDefs(
  195. mSource, *mConstantDefs.get(), "");
  196. // Also parse any attached sources
  197. for (GLSLProgramContainer::const_iterator i = mAttachedGLSLPrograms.begin();
  198. i != mAttachedGLSLPrograms.end(); ++i)
  199. {
  200. GLSLProgram* childShader = *i;
  201. GLSLLinkProgramManager::getSingleton().extractConstantDefs(
  202. childShader->getSource(), *mConstantDefs.get(), "");
  203. }
  204. }
  205. //-----------------------------------------------------------------------
  206. GLSLProgram::GLSLProgram()
  207. : HighLevelGpuProgram(),
  208. mInputOperationType(RenderOperation::OT_TRIANGLE_LIST),
  209. mOutputOperationType(RenderOperation::OT_TRIANGLE_LIST), mMaxOutputVertices(3)
  210. {
  211. // Manually assign language now since we use it immediately
  212. mSyntaxCode = "glsl";
  213. }
  214. //---------------------------------------------------------------------
  215. bool GLSLProgram::getPassSurfaceAndLightStates(void) const
  216. {
  217. // scenemanager should pass on light & material state to the rendersystem
  218. return true;
  219. }
  220. //---------------------------------------------------------------------
  221. bool GLSLProgram::getPassTransformStates(void) const
  222. {
  223. // scenemanager should pass on transform state to the rendersystem
  224. return true;
  225. }
  226. //-----------------------------------------------------------------------
  227. void GLSLProgram::attachChildShader(const String& name)
  228. {
  229. // is the name valid and already loaded?
  230. // check with the high level program manager to see if it was loaded
  231. // TODO PORT - I'm not sure what is this for but I don't support it
  232. //HighLevelGpuProgramPtr hlProgram = HighLevelGpuProgramManager::getSingleton().getByName(name);
  233. //if (!hlProgram.isNull())
  234. //{
  235. // if (hlProgram->getSyntaxCode() == "glsl")
  236. // {
  237. // // make sure attached program source gets loaded and compiled
  238. // // don't need a low level implementation for attached shader objects
  239. // // loadHighLevelImpl will only load the source and compile once
  240. // // so don't worry about calling it several times
  241. // GLSLProgram* childShader = static_cast<GLSLProgram*>(hlProgram.getPointer());
  242. // // load the source and attach the child shader only if supported
  243. // if (isSupported())
  244. // {
  245. // childShader->loadHighLevelImpl();
  246. // // add to the container
  247. // mAttachedGLSLPrograms.push_back( childShader );
  248. // mAttachedShaderNames += name + " ";
  249. // }
  250. // }
  251. //}
  252. }
  253. //-----------------------------------------------------------------------
  254. void GLSLProgram::attachToProgramObject( const GLhandleARB programObject )
  255. {
  256. // attach child objects
  257. GLSLProgramContainerIterator childprogramcurrent = mAttachedGLSLPrograms.begin();
  258. GLSLProgramContainerIterator childprogramend = mAttachedGLSLPrograms.end();
  259. while (childprogramcurrent != childprogramend)
  260. {
  261. GLSLProgram* childShader = *childprogramcurrent;
  262. // bug in ATI GLSL linker : modules without main function must be recompiled each time
  263. // they are linked to a different program object
  264. // don't check for compile errors since there won't be any
  265. // *** minor inconvenience until ATI fixes there driver
  266. childShader->compile(false);
  267. childShader->attachToProgramObject( programObject );
  268. ++childprogramcurrent;
  269. }
  270. glAttachObjectARB( programObject, mGLHandle );
  271. checkForGLSLError( "GLSLProgram::attachToProgramObject",
  272. "Error attaching shader object to GLSL Program Object", programObject );
  273. }
  274. //-----------------------------------------------------------------------
  275. void GLSLProgram::detachFromProgramObject( const GLhandleARB programObject )
  276. {
  277. glDetachObjectARB(programObject, mGLHandle);
  278. checkForGLSLError( "GLSLProgram::detachFromProgramObject",
  279. "Error detaching shader object from GLSL Program Object", programObject );
  280. // attach child objects
  281. GLSLProgramContainerIterator childprogramcurrent = mAttachedGLSLPrograms.begin();
  282. GLSLProgramContainerIterator childprogramend = mAttachedGLSLPrograms.end();
  283. while (childprogramcurrent != childprogramend)
  284. {
  285. GLSLProgram* childShader = *childprogramcurrent;
  286. childShader->detachFromProgramObject( programObject );
  287. ++childprogramcurrent;
  288. }
  289. }
  290. //-----------------------------------------------------------------------
  291. const String& GLSLProgram::getLanguage(void) const
  292. {
  293. static const String language = "glsl";
  294. return language;
  295. }
  296. //-----------------------------------------------------------------------
  297. RenderOperation::OperationType parseOperationType(const String& val)
  298. {
  299. if (val == "point_list")
  300. {
  301. return RenderOperation::OT_POINT_LIST;
  302. }
  303. else if (val == "line_list")
  304. {
  305. return RenderOperation::OT_LINE_LIST;
  306. }
  307. else if (val == "line_strip")
  308. {
  309. return RenderOperation::OT_LINE_STRIP;
  310. }
  311. else if (val == "triangle_strip")
  312. {
  313. return RenderOperation::OT_TRIANGLE_STRIP;
  314. }
  315. else if (val == "triangle_fan")
  316. {
  317. return RenderOperation::OT_TRIANGLE_FAN;
  318. }
  319. else
  320. {
  321. //Triangle list is the default fallback. Keep it this way?
  322. return RenderOperation::OT_TRIANGLE_LIST;
  323. }
  324. }
  325. //-----------------------------------------------------------------------
  326. String operationTypeToString(RenderOperation::OperationType val)
  327. {
  328. switch (val)
  329. {
  330. case RenderOperation::OT_POINT_LIST:
  331. return "point_list";
  332. break;
  333. case RenderOperation::OT_LINE_LIST:
  334. return "line_list";
  335. break;
  336. case RenderOperation::OT_LINE_STRIP:
  337. return "line_strip";
  338. break;
  339. case RenderOperation::OT_TRIANGLE_STRIP:
  340. return "triangle_strip";
  341. break;
  342. case RenderOperation::OT_TRIANGLE_FAN:
  343. return "triangle_fan";
  344. break;
  345. case RenderOperation::OT_TRIANGLE_LIST:
  346. default:
  347. return "triangle_list";
  348. break;
  349. }
  350. }
  351. }