shaderc_glsl.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "shaderc.h"
  6. #include "glsl_optimizer.h"
  7. namespace bgfx { namespace glsl
  8. {
  9. static bool compile(bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  10. {
  11. char ch = char(tolower(_cmdLine.findOption('\0', "type")[0]) );
  12. const glslopt_shader_type type = ch == 'f'
  13. ? kGlslOptShaderFragment
  14. : (ch == 'c' ? kGlslOptShaderCompute : kGlslOptShaderVertex);
  15. glslopt_target target = kGlslTargetOpenGL;
  16. switch (_version)
  17. {
  18. case BX_MAKEFOURCC('M', 'T', 'L', 0):
  19. target = kGlslTargetMetal;
  20. break;
  21. case 2:
  22. target = kGlslTargetOpenGLES20;
  23. break;
  24. case 3:
  25. target = kGlslTargetOpenGLES30;
  26. break;
  27. default:
  28. target = kGlslTargetOpenGL;
  29. break;
  30. }
  31. glslopt_ctx* ctx = glslopt_initialize(target);
  32. glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
  33. if (!glslopt_get_status(shader) )
  34. {
  35. const char* log = glslopt_get_log(shader);
  36. int32_t source = 0;
  37. int32_t line = 0;
  38. int32_t column = 0;
  39. int32_t start = 0;
  40. int32_t end = INT32_MAX;
  41. bool found = false
  42. || 3 == sscanf(log, "%u:%u(%u):", &source, &line, &column)
  43. || 2 == sscanf(log, "(%u,%u):", &line, &column)
  44. ;
  45. if (found
  46. && 0 != line)
  47. {
  48. start = bx::uint32_imax(1, line-10);
  49. end = start + 20;
  50. }
  51. printCode(_code.c_str(), line, start, end, column);
  52. fprintf(stderr, "Error: %s\n", log);
  53. glslopt_cleanup(ctx);
  54. return false;
  55. }
  56. const char* optimizedShader = glslopt_get_output(shader);
  57. // Trim all directives.
  58. while ('#' == *optimizedShader)
  59. {
  60. optimizedShader = bx::strnl(optimizedShader);
  61. }
  62. {
  63. char* code = const_cast<char*>(optimizedShader);
  64. strReplace(code, "gl_FragDepthEXT", "gl_FragDepth");
  65. strReplace(code, "texture2DLodARB", "texture2DLod");
  66. strReplace(code, "texture2DLodEXT", "texture2DLod");
  67. strReplace(code, "texture2DGradARB", "texture2DGrad");
  68. strReplace(code, "texture2DGradEXT", "texture2DGrad");
  69. strReplace(code, "textureCubeLodARB", "textureCubeLod");
  70. strReplace(code, "textureCubeLodEXT", "textureCubeLod");
  71. strReplace(code, "textureCubeGradARB", "textureCubeGrad");
  72. strReplace(code, "textureCubeGradEXT", "textureCubeGrad");
  73. strReplace(code, "texture2DProjLodARB", "texture2DProjLod");
  74. strReplace(code, "texture2DProjLodEXT", "texture2DProjLod");
  75. strReplace(code, "texture2DProjGradARB", "texture2DProjGrad");
  76. strReplace(code, "texture2DProjGradEXT", "texture2DProjGrad");
  77. strReplace(code, "shadow2DARB", "shadow2D");
  78. strReplace(code, "shadow2DEXT", "shadow2D");
  79. strReplace(code, "shadow2DProjARB", "shadow2DProj");
  80. strReplace(code, "shadow2DProjEXT", "shadow2DProj");
  81. }
  82. UniformArray uniforms;
  83. if (target != kGlslTargetMetal)
  84. {
  85. const char* parse = optimizedShader;
  86. while (NULL != parse
  87. && *parse != '\0')
  88. {
  89. parse = bx::strws(parse);
  90. const char* eol = bx::strFind(parse, ';');
  91. if (NULL != eol)
  92. {
  93. const char* qualifier = parse;
  94. parse = bx::strws(bx::strword(parse) );
  95. if (0 == bx::strCmp(qualifier, "attribute", 9)
  96. || 0 == bx::strCmp(qualifier, "varying", 7)
  97. || 0 == bx::strCmp(qualifier, "in", 2)
  98. || 0 == bx::strCmp(qualifier, "out", 3)
  99. )
  100. {
  101. // skip attributes and varyings.
  102. parse = eol + 1;
  103. continue;
  104. }
  105. if (0 != bx::strCmp(qualifier, "uniform", 7) )
  106. {
  107. // end if there is no uniform keyword.
  108. parse = NULL;
  109. continue;
  110. }
  111. const char* precision = NULL;
  112. const char* typen = parse;
  113. if (0 == bx::strCmp(typen, "lowp", 4)
  114. || 0 == bx::strCmp(typen, "mediump", 7)
  115. || 0 == bx::strCmp(typen, "highp", 5) )
  116. {
  117. precision = typen;
  118. typen = parse = bx::strws(bx::strword(parse) );
  119. }
  120. BX_UNUSED(precision);
  121. char uniformType[256];
  122. parse = bx::strword(parse);
  123. if (0 == bx::strCmp(typen, "sampler", 7) )
  124. {
  125. bx::strCopy(uniformType, BX_COUNTOF(uniformType), "int");
  126. }
  127. else
  128. {
  129. bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
  130. }
  131. const char* name = parse = bx::strws(parse);
  132. char uniformName[256];
  133. uint8_t num = 1;
  134. const char* array = bx::strFind(name, "[", int32_t(eol-parse) );
  135. if (NULL != array)
  136. {
  137. bx::strCopy(uniformName, int32_t(array-name+1), name);
  138. char arraySize[32];
  139. const char* end = bx::strFind(array, "]", int32_t(eol-array) );
  140. bx::strCopy(arraySize, int32_t(end-array), array+1);
  141. num = uint8_t(atoi(arraySize) );
  142. }
  143. else
  144. {
  145. bx::strCopy(uniformName, int32_t(eol-name+1), name);
  146. }
  147. Uniform un;
  148. un.type = nameToUniformTypeEnum(uniformType);
  149. if (UniformType::Count != un.type)
  150. {
  151. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  152. un.name = uniformName;
  153. un.num = num;
  154. un.regIndex = 0;
  155. un.regCount = num;
  156. uniforms.push_back(un);
  157. }
  158. parse = eol + 1;
  159. }
  160. }
  161. }
  162. else
  163. {
  164. const char* parse = bx::strFind(optimizedShader, "struct xlatMtlShaderUniform {");
  165. const char* end = parse;
  166. if (NULL != parse)
  167. {
  168. parse += bx::strLen("struct xlatMtlShaderUniform {");
  169. end = bx::strFind(parse, "};");
  170. }
  171. while ( parse < end
  172. && *parse != '\0')
  173. {
  174. parse = bx::strws(parse);
  175. const char* eol = bx::strFind(parse, ';');
  176. if (NULL != eol)
  177. {
  178. const char* typen = parse;
  179. char uniformType[256];
  180. parse = bx::strword(parse);
  181. bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
  182. const char* name = parse = bx::strws(parse);
  183. char uniformName[256];
  184. uint8_t num = 1;
  185. const char* array = bx::strFind(name, "[", int32_t(eol-parse) );
  186. if (NULL != array)
  187. {
  188. bx::strCopy(uniformName, int32_t(array-name+1), name);
  189. char arraySize[32];
  190. const char* arrayEnd = bx::strFind(array, "]", int32_t(eol-array) );
  191. bx::strCopy(arraySize, int32_t(arrayEnd-array), array+1);
  192. num = uint8_t(atoi(arraySize) );
  193. }
  194. else
  195. {
  196. bx::strCopy(uniformName, int32_t(eol-name+1), name);
  197. }
  198. Uniform un;
  199. un.type = nameToUniformTypeEnum(uniformType);
  200. if (UniformType::Count != un.type)
  201. {
  202. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  203. un.name = uniformName;
  204. un.num = num;
  205. un.regIndex = 0;
  206. un.regCount = num;
  207. uniforms.push_back(un);
  208. }
  209. parse = eol + 1;
  210. }
  211. }
  212. }
  213. uint16_t count = (uint16_t)uniforms.size();
  214. bx::write(_writer, count);
  215. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  216. {
  217. const Uniform& un = *it;
  218. uint8_t nameSize = (uint8_t)un.name.size();
  219. bx::write(_writer, nameSize);
  220. bx::write(_writer, un.name.c_str(), nameSize);
  221. uint8_t uniformType = uint8_t(un.type);
  222. bx::write(_writer, uniformType);
  223. bx::write(_writer, un.num);
  224. bx::write(_writer, un.regIndex);
  225. bx::write(_writer, un.regCount);
  226. BX_TRACE("%s, %s, %d, %d, %d"
  227. , un.name.c_str()
  228. , getUniformTypeName(un.type)
  229. , un.num
  230. , un.regIndex
  231. , un.regCount
  232. );
  233. }
  234. uint32_t shaderSize = (uint32_t)bx::strLen(optimizedShader);
  235. bx::write(_writer, shaderSize);
  236. bx::write(_writer, optimizedShader, shaderSize);
  237. uint8_t nul = 0;
  238. bx::write(_writer, nul);
  239. if (_cmdLine.hasArg('\0', "disasm") )
  240. {
  241. std::string disasmfp = _cmdLine.findOption('o');
  242. disasmfp += ".disasm";
  243. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  244. }
  245. glslopt_cleanup(ctx);
  246. return true;
  247. }
  248. } // namespace glsl
  249. bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  250. {
  251. return glsl::compile(_cmdLine, _version, _code, _writer);
  252. }
  253. } // namespace bgfx