CmGLSLProgram.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "CmGpuProgram.h"
  25. #include "CmRenderSystem.h"
  26. #include "CmRenderSystemCapabilities.h"
  27. #include "CmGpuProgramManager.h"
  28. #include "CmHighLevelGpuProgramManager.h"
  29. #include "CmException.h"
  30. #include "CmVertexIndexData.h"
  31. #include "CmDebug.h"
  32. #include "CmGLSLProgram.h"
  33. #include "CmGLSLGpuProgram.h"
  34. #include "CmGLSLExtSupport.h"
  35. #include "CmGLSLPreprocessor.h"
  36. #include "CmGLSLParamParser.h"
  37. #include "CmGLSLProgramRTTI.h"
  38. namespace CamelotEngine
  39. {
  40. //---------------------------------------------------------------------------
  41. GLSLProgram::~GLSLProgram()
  42. {
  43. unload_internal();
  44. }
  45. //-----------------------------------------------------------------------
  46. GLSLProgram::GLSLProgram(const String& source, const String& entryPoint, const String& language,
  47. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  48. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, isAdjacencyInfoRequired),
  49. mInputOperationType(DOT_TRIANGLE_LIST),
  50. mOutputOperationType(DOT_TRIANGLE_LIST), mMaxOutputVertices(3)
  51. {
  52. // Manually assign language now since we use it immediately
  53. mSyntaxCode = "glsl";
  54. }
  55. //-----------------------------------------------------------------------
  56. void GLSLProgram::loadFromSource(void)
  57. {
  58. // only create a shader object if glsl is supported
  59. GLenum shaderType = 0x0000;
  60. if (isSupported())
  61. {
  62. checkForGLSLError( "GLSLProgram::loadFromSource", "GL Errors before creating shader object", 0, GLSLOT_SHADER);
  63. switch (mType)
  64. {
  65. case GPT_VERTEX_PROGRAM:
  66. shaderType = GL_VERTEX_SHADER;
  67. break;
  68. case GPT_FRAGMENT_PROGRAM:
  69. shaderType = GL_FRAGMENT_SHADER;
  70. break;
  71. case GPT_GEOMETRY_PROGRAM:
  72. shaderType = GL_GEOMETRY_SHADER;
  73. break;
  74. case GPT_HULL_PROGRAM:
  75. shaderType = GL_TESS_CONTROL_SHADER;
  76. break;
  77. case GPT_DOMAIN_PROGRAM:
  78. shaderType = GL_TESS_EVALUATION_SHADER;
  79. break;
  80. }
  81. }
  82. // Preprocess the GLSL shader in order to get a clean source
  83. CPreprocessor cpp;
  84. // Pass all user-defined macros to preprocessor
  85. if (!mPreprocessorDefines.empty ())
  86. {
  87. String::size_type pos = 0;
  88. while (pos != String::npos)
  89. {
  90. // Find delims
  91. String::size_type endPos = mPreprocessorDefines.find_first_of(";,=", pos);
  92. if (endPos != String::npos)
  93. {
  94. String::size_type macro_name_start = pos;
  95. size_t macro_name_len = endPos - pos;
  96. pos = endPos;
  97. // Check definition part
  98. if (mPreprocessorDefines[pos] == '=')
  99. {
  100. // set up a definition, skip delim
  101. ++pos;
  102. String::size_type macro_val_start = pos;
  103. size_t macro_val_len;
  104. endPos = mPreprocessorDefines.find_first_of(";,", pos);
  105. if (endPos == String::npos)
  106. {
  107. macro_val_len = mPreprocessorDefines.size () - pos;
  108. pos = endPos;
  109. }
  110. else
  111. {
  112. macro_val_len = endPos - pos;
  113. pos = endPos+1;
  114. }
  115. cpp.Define (
  116. mPreprocessorDefines.c_str () + macro_name_start, macro_name_len,
  117. mPreprocessorDefines.c_str () + macro_val_start, macro_val_len);
  118. }
  119. else
  120. {
  121. // No definition part, define as "1"
  122. ++pos;
  123. cpp.Define (
  124. mPreprocessorDefines.c_str () + macro_name_start, macro_name_len, 1);
  125. }
  126. }
  127. else
  128. pos = endPos;
  129. }
  130. }
  131. size_t out_size = 0;
  132. const char *src = mSource.c_str ();
  133. size_t src_len = mSource.size ();
  134. char *out = cpp.Parse (src, src_len, out_size);
  135. if (!out || !out_size)
  136. {
  137. // Failed to preprocess, break out
  138. CM_EXCEPT(RenderingAPIException, "Failed to preprocess shader ");
  139. }
  140. mSource = String (out, out_size);
  141. if (out < src || out > src + src_len)
  142. free (out);
  143. // Add preprocessor extras and main source
  144. if (!mSource.empty())
  145. {
  146. const char *source = mSource.c_str();
  147. mGLHandle = glCreateShaderProgramv(shaderType, 1, &source);
  148. // check for load errors
  149. checkForGLSLError( "GLSLProgram::loadFromSource", "Cannot load GLSL high-level shader source : ", mGLHandle, GLSLOT_PROGRAM, true);
  150. }
  151. mAssemblerProgram = GpuProgramPtr(new GLSLGpuProgram(this, mSource, mEntryPoint, mSyntaxCode, mType, mProfile));
  152. GLSLParamParser paramParser;
  153. paramParser.buildUniformDescriptions(mGLHandle, mParametersDesc);
  154. paramParser.buildVertexDeclaration(mGLHandle, mVertexDeclaration);
  155. }
  156. //---------------------------------------------------------------------------
  157. void GLSLProgram::unload_internal()
  158. {
  159. // We didn't create mAssemblerProgram through a manager, so override this
  160. // implementation so that we don't try to remove it from one. Since getCreator()
  161. // is used, it might target a different matching handle!
  162. mAssemblerProgram = nullptr;
  163. if (isSupported())
  164. glDeleteShader(mGLHandle);
  165. HighLevelGpuProgram::unload_internal();
  166. }
  167. //-----------------------------------------------------------------------
  168. const String& GLSLProgram::getLanguage(void) const
  169. {
  170. static const String language = "glsl";
  171. return language;
  172. }
  173. /************************************************************************/
  174. /* SERIALIZATION */
  175. /************************************************************************/
  176. RTTITypeBase* GLSLProgram::getRTTIStatic()
  177. {
  178. return GLSLProgramRTTI::instance();
  179. }
  180. RTTITypeBase* GLSLProgram::getRTTI() const
  181. {
  182. return GLSLProgram::getRTTIStatic();
  183. }
  184. }