BsGLSLGpuProgram.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GLSL/BsGLSLGpuProgram.h"
  4. #include "RenderAPI/BsRenderAPI.h"
  5. #include "Error/BsException.h"
  6. #include "GLSL/BsGLSLParamParser.h"
  7. #include "Managers/BsHardwareBufferManager.h"
  8. #include "Profiling/BsRenderStats.h"
  9. #include "RenderAPI/BsGpuParams.h"
  10. namespace bs { namespace ct
  11. {
  12. UINT32 GLSLGpuProgram::mVertexShaderCount = 0;
  13. UINT32 GLSLGpuProgram::mFragmentShaderCount = 0;
  14. UINT32 GLSLGpuProgram::mGeometryShaderCount = 0;
  15. UINT32 GLSLGpuProgram::mDomainShaderCount = 0;
  16. UINT32 GLSLGpuProgram::mHullShaderCount = 0;
  17. UINT32 GLSLGpuProgram::mComputeShaderCount = 0;
  18. bool checkForGLSLError(const GLuint programObj, String& outErrorMsg)
  19. {
  20. StringStream stream;
  21. GLint linkCompileSuccess = 0;
  22. glGetProgramiv(programObj, GL_LINK_STATUS, &linkCompileSuccess);
  23. GLenum glErr;
  24. bool errorsFound = false;
  25. glErr = glGetError();
  26. while (glErr != GL_NO_ERROR)
  27. {
  28. const char* glerrStr = (const char*)gluErrorString(glErr);
  29. if (glerrStr)
  30. {
  31. if (errorsFound)
  32. stream << "\nPrevious errors: \n";
  33. stream << String(glerrStr) << std::endl;;
  34. }
  35. glErr = glGetError();
  36. errorsFound = true;
  37. }
  38. if ((errorsFound || !linkCompileSuccess) && programObj > 0)
  39. {
  40. GLint infologLength = 0;
  41. glGetProgramiv(programObj, GL_INFO_LOG_LENGTH, &infologLength);
  42. if (infologLength > 0)
  43. {
  44. GLint charsWritten = 0;
  45. GLchar* infoLog = (GLchar*)bs_alloc(sizeof(GLchar)* infologLength);
  46. glGetProgramInfoLog(programObj, infologLength, &charsWritten, infoLog);
  47. stream << "Compile and linker info log: \n";
  48. stream << String(infoLog);
  49. bs_free(infoLog);
  50. }
  51. }
  52. outErrorMsg = stream.str();
  53. return errorsFound || !linkCompileSuccess;
  54. }
  55. GLSLGpuProgram::GLSLGpuProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  56. :GpuProgram(desc, deviceMask), mProgramID(0), mGLHandle(0)
  57. { }
  58. GLSLGpuProgram::~GLSLGpuProgram()
  59. {
  60. if (mIsCompiled && mGLHandle != 0)
  61. {
  62. glDeleteProgram(mGLHandle);
  63. mGLHandle = 0;
  64. }
  65. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  66. }
  67. void GLSLGpuProgram::initialize()
  68. {
  69. static const char* VERSION_LINE = "#version 450\n";
  70. if (!isSupported())
  71. {
  72. mIsCompiled = false;
  73. mCompileError = "Specified program is not supported by the current render system.";
  74. GpuProgram::initialize();
  75. return;
  76. }
  77. GLenum shaderType = 0x0000;
  78. switch (mProperties.getType())
  79. {
  80. case GPT_VERTEX_PROGRAM:
  81. shaderType = GL_VERTEX_SHADER;
  82. mProgramID = ++mVertexShaderCount;
  83. break;
  84. case GPT_FRAGMENT_PROGRAM:
  85. shaderType = GL_FRAGMENT_SHADER;
  86. mProgramID = ++mFragmentShaderCount;
  87. break;
  88. case GPT_GEOMETRY_PROGRAM:
  89. shaderType = GL_GEOMETRY_SHADER;
  90. mProgramID = ++mGeometryShaderCount;
  91. break;
  92. case GPT_HULL_PROGRAM:
  93. shaderType = GL_TESS_CONTROL_SHADER;
  94. mProgramID = ++mDomainShaderCount;
  95. break;
  96. case GPT_DOMAIN_PROGRAM:
  97. shaderType = GL_TESS_EVALUATION_SHADER;
  98. mProgramID = ++mHullShaderCount;
  99. break;
  100. case GPT_COMPUTE_PROGRAM:
  101. shaderType = GL_COMPUTE_SHADER;
  102. mProgramID = ++mComputeShaderCount;
  103. break;
  104. default:
  105. break;
  106. }
  107. // Add preprocessor extras and main source
  108. const String& source = mProperties.getSource();
  109. if (!source.empty())
  110. {
  111. Vector<GLchar*> lines;
  112. const char* versionStr = "#version ";
  113. UINT32 versionStrLen = (UINT32)strlen(versionStr);
  114. UINT32 lineLength = 0;
  115. INT32 versionLine = -1;
  116. for (UINT32 i = 0; i < source.size(); i++)
  117. {
  118. if (source[i] == '\n' || source[i] == '\r')
  119. {
  120. assert(sizeof(source[i]) == sizeof(GLchar));
  121. GLchar* lineData = (GLchar*)bs_stack_alloc(sizeof(GLchar) * (lineLength + 2));
  122. memcpy(lineData, &source[i - lineLength], sizeof(GLchar) * lineLength);
  123. lineData[lineLength] = '\n';
  124. lineData[lineLength + 1] = '\0';
  125. if(versionLine == -1 && lineLength >= versionStrLen)
  126. {
  127. bool isEqual = true;
  128. for (UINT32 j = 0; j < versionStrLen; ++j)
  129. {
  130. if(lineData[j] != versionStr[j])
  131. {
  132. isEqual = false;
  133. break;
  134. }
  135. }
  136. if (isEqual)
  137. versionLine = (INT32)lines.size();
  138. }
  139. lines.push_back(lineData);
  140. lineLength = 0;
  141. }
  142. else
  143. {
  144. lineLength++;
  145. }
  146. }
  147. if (lineLength > 0)
  148. {
  149. UINT32 end = (UINT32)source.size() - 1;
  150. assert(sizeof(source[end]) == sizeof(GLchar));
  151. GLchar* lineData = (GLchar*)bs_stack_alloc(sizeof(GLchar) * (lineLength + 1));
  152. memcpy(lineData, &source[source.size() - lineLength], sizeof(GLchar) * lineLength);
  153. lineData[lineLength] = '\0';
  154. lines.push_back(lineData);
  155. lineLength = 0;
  156. }
  157. int numInsertedLines = 0;
  158. if(versionLine == -1)
  159. {
  160. UINT32 length = (UINT32)strlen(VERSION_LINE) + 1;
  161. GLchar* extraLineData = (GLchar*)bs_stack_alloc(length);
  162. memcpy(extraLineData, VERSION_LINE, length);
  163. lines.insert(lines.begin(), extraLineData);
  164. numInsertedLines++;
  165. }
  166. static const char* EXTRA_LINES[] = { "#define OPENGL\n" };
  167. UINT32 numExtraLines = sizeof(EXTRA_LINES) / sizeof(EXTRA_LINES[0]);
  168. UINT32 extraLineOffset = versionLine != -1 ? versionLine + 1 : 0;
  169. for (UINT32 i = 0; i < numExtraLines; i++)
  170. {
  171. UINT32 length = (UINT32)strlen(EXTRA_LINES[i]) + 1;
  172. GLchar* extraLineData = (GLchar*)bs_stack_alloc(length);
  173. memcpy(extraLineData, EXTRA_LINES[i], length);
  174. lines.insert(lines.begin() + extraLineOffset + numInsertedLines, extraLineData);
  175. numInsertedLines++;
  176. }
  177. StringStream codeStream;
  178. for(auto& entry : lines)
  179. codeStream << entry;
  180. for (INT32 i = numInsertedLines - 1; i >= 0; i--)
  181. bs_stack_free(lines[extraLineOffset + i]);
  182. if (numInsertedLines > 0)
  183. lines.erase(lines.begin() + extraLineOffset, lines.begin() + extraLineOffset + numInsertedLines);
  184. for (auto iter = lines.rbegin(); iter != lines.rend(); ++iter)
  185. bs_stack_free(*iter);
  186. String code = codeStream.str();
  187. const char* codeRaw = code.c_str();
  188. mGLHandle = glCreateShaderProgramv(shaderType, 1, (const GLchar**)&codeRaw);
  189. mCompileError = "";
  190. mIsCompiled = !checkForGLSLError(mGLHandle, mCompileError);
  191. }
  192. if (mIsCompiled)
  193. {
  194. GLSLParamParser paramParser;
  195. paramParser.buildUniformDescriptions(mGLHandle, mProperties.getType(), *mParametersDesc);
  196. if (mProperties.getType() == GPT_VERTEX_PROGRAM)
  197. {
  198. List<VertexElement> elementList = paramParser.buildVertexDeclaration(mGLHandle);
  199. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration(elementList);
  200. }
  201. }
  202. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  203. GpuProgram::initialize();
  204. }
  205. bool GLSLGpuProgram::isSupported() const
  206. {
  207. if (!isRequiredCapabilitiesSupported())
  208. return false;
  209. RenderAPI* rapi = RenderAPI::instancePtr();
  210. return rapi->getCapabilities(0).isShaderProfileSupported("glsl");
  211. }
  212. }}