shaderc_glsl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright 2011-2021 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(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  10. {
  11. char ch = _options.shaderType;
  12. const glslopt_shader_type type = ch == 'f'
  13. ? kGlslOptShaderFragment
  14. : (ch == 'c' ? kGlslOptShaderCompute : kGlslOptShaderVertex);
  15. glslopt_target target = kGlslTargetOpenGL;
  16. if(_version == BX_MAKEFOURCC('M', 'T', 'L', 0))
  17. {
  18. target = kGlslTargetMetal;
  19. } else if(_version < 0x80000000) {
  20. target = kGlslTargetOpenGL;
  21. }
  22. else {
  23. _version &= ~0x80000000;
  24. target = (_version >= 300) ? kGlslTargetOpenGLES30 : kGlslTargetOpenGLES20;
  25. }
  26. glslopt_ctx* ctx = glslopt_initialize(target);
  27. glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
  28. if (!glslopt_get_status(shader) )
  29. {
  30. const char* log = glslopt_get_log(shader);
  31. int32_t source = 0;
  32. int32_t line = 0;
  33. int32_t column = 0;
  34. int32_t start = 0;
  35. int32_t end = INT32_MAX;
  36. bool found = false
  37. || 3 == sscanf(log, "%u:%u(%u):", &source, &line, &column)
  38. || 2 == sscanf(log, "(%u,%u):", &line, &column)
  39. ;
  40. if (found
  41. && 0 != line)
  42. {
  43. start = bx::uint32_imax(1, line-10);
  44. end = start + 20;
  45. }
  46. printCode(_code.c_str(), line, start, end, column);
  47. bx::printf("Error: %s\n", log);
  48. glslopt_shader_delete(shader);
  49. glslopt_cleanup(ctx);
  50. return false;
  51. }
  52. const char* optimizedShader = glslopt_get_output(shader);
  53. std::string out;
  54. // Trim all directives.
  55. while ('#' == *optimizedShader)
  56. {
  57. optimizedShader = bx::strFindNl(optimizedShader).getPtr();
  58. }
  59. out.append(optimizedShader, strlen(optimizedShader));
  60. optimizedShader = out.c_str();
  61. {
  62. char* code = const_cast<char*>(optimizedShader);
  63. strReplace(code, "gl_FragDepthEXT", "gl_FragDepth");
  64. strReplace(code, "texture2DLodARB", "texture2DLod");
  65. strReplace(code, "texture2DLodEXT", "texture2DLod");
  66. strReplace(code, "texture2DGradARB", "texture2DGrad");
  67. strReplace(code, "texture2DGradEXT", "texture2DGrad");
  68. strReplace(code, "textureCubeLodARB", "textureCubeLod");
  69. strReplace(code, "textureCubeLodEXT", "textureCubeLod");
  70. strReplace(code, "textureCubeGradARB", "textureCubeGrad");
  71. strReplace(code, "textureCubeGradEXT", "textureCubeGrad");
  72. strReplace(code, "texture2DProjLodARB", "texture2DProjLod");
  73. strReplace(code, "texture2DProjLodEXT", "texture2DProjLod");
  74. strReplace(code, "texture2DProjGradARB", "texture2DProjGrad");
  75. strReplace(code, "texture2DProjGradEXT", "texture2DProjGrad");
  76. strReplace(code, "shadow2DARB", "shadow2D");
  77. strReplace(code, "shadow2DEXT", "shadow2D");
  78. strReplace(code, "shadow2DProjARB", "shadow2DProj");
  79. strReplace(code, "shadow2DProjEXT", "shadow2DProj");
  80. }
  81. UniformArray uniforms;
  82. if (target != kGlslTargetMetal)
  83. {
  84. bx::StringView parse(optimizedShader);
  85. while (!parse.isEmpty() )
  86. {
  87. parse = bx::strLTrimSpace(parse);
  88. bx::StringView eol = bx::strFind(parse, ';');
  89. if (!eol.isEmpty() )
  90. {
  91. bx::StringView qualifier = nextWord(parse);
  92. if (0 == bx::strCmp(qualifier, "precision", 9) )
  93. {
  94. // skip precision
  95. parse.set(eol.getPtr() + 1, parse.getTerm() );
  96. continue;
  97. }
  98. if (0 == bx::strCmp(qualifier, "attribute", 9)
  99. || 0 == bx::strCmp(qualifier, "varying", 7)
  100. || 0 == bx::strCmp(qualifier, "in", 2)
  101. || 0 == bx::strCmp(qualifier, "out", 3)
  102. )
  103. {
  104. // skip attributes and varyings.
  105. parse.set(eol.getPtr() + 1, parse.getTerm() );
  106. continue;
  107. }
  108. if (0 == bx::strCmp(qualifier, "flat", 4)
  109. || 0 == bx::strCmp(qualifier, "smooth", 6)
  110. || 0 == bx::strCmp(qualifier, "noperspective", 13)
  111. || 0 == bx::strCmp(qualifier, "centroid", 8)
  112. )
  113. {
  114. // skip interpolation qualifiers
  115. parse.set(eol.getPtr() + 1, parse.getTerm() );
  116. continue;
  117. }
  118. if (0 == bx::strCmp(parse, "tmpvar", 6) )
  119. {
  120. // skip temporaries
  121. parse.set(eol.getPtr() + 1, parse.getTerm() );
  122. continue;
  123. }
  124. if (0 != bx::strCmp(qualifier, "uniform", 7) )
  125. {
  126. // end if there is no uniform keyword.
  127. parse.clear();
  128. continue;
  129. }
  130. bx::StringView precision;
  131. bx::StringView typen = nextWord(parse);
  132. if (0 == bx::strCmp(typen, "lowp", 4)
  133. || 0 == bx::strCmp(typen, "mediump", 7)
  134. || 0 == bx::strCmp(typen, "highp", 5) )
  135. {
  136. precision = typen;
  137. typen = nextWord(parse);
  138. }
  139. BX_UNUSED(precision);
  140. char uniformType[256];
  141. if (0 == bx::strCmp(typen, "sampler", 7)
  142. || 0 == bx::strCmp(typen, "isampler", 8)
  143. || 0 == bx::strCmp(typen, "usampler", 8) )
  144. {
  145. bx::strCopy(uniformType, BX_COUNTOF(uniformType), "int");
  146. }
  147. else
  148. {
  149. bx::strCopy(uniformType, BX_COUNTOF(uniformType), typen);
  150. }
  151. bx::StringView name = nextWord(parse);
  152. uint8_t num = 1;
  153. bx::StringView array = bx::strSubstr(parse, 0, 1);
  154. if (0 == bx::strCmp(array, "[", 1) )
  155. {
  156. parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
  157. uint32_t tmp;
  158. bx::fromString(&tmp, parse);
  159. num = uint8_t(tmp);
  160. }
  161. Uniform un;
  162. un.type = nameToUniformTypeEnum(uniformType);
  163. if (UniformType::Count != un.type)
  164. {
  165. un.name.assign(name.getPtr(), name.getTerm());
  166. BX_TRACE("name: %s (type %d, num %d)", un.name.c_str(), un.type, num);
  167. un.num = num;
  168. un.regIndex = 0;
  169. un.regCount = num;
  170. uniforms.push_back(un);
  171. }
  172. parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), parse.getTerm() ) ) );
  173. }
  174. }
  175. }
  176. else
  177. {
  178. const bx::StringView optShader(optimizedShader);
  179. bx::StringView parse = bx::strFind(optimizedShader, "struct xlatMtlShaderUniform {");
  180. bx::StringView end = parse;
  181. if (!parse.isEmpty() )
  182. {
  183. parse.set(parse.getPtr() + bx::strLen("struct xlatMtlShaderUniform {"), optShader.getTerm() );
  184. end = bx::strFind(parse, "};");
  185. }
  186. while ( parse.getPtr() < end.getPtr()
  187. && !parse.isEmpty() )
  188. {
  189. parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm() );
  190. const bx::StringView eol = bx::strFind(parse, ';');
  191. if (!eol.isEmpty() )
  192. {
  193. const char* typen = parse.getPtr();
  194. char uniformType[256];
  195. parse = bx::strWord(parse);
  196. bx::strCopy(uniformType, parse.getLength()+1, typen);
  197. parse.set(parse.getPtr()+parse.getLength(),optShader.getTerm());
  198. const char* name = bx::strLTrimSpace(parse).getPtr();
  199. parse.set(name, optShader.getTerm() );
  200. char uniformName[256];
  201. uint8_t num = 1;
  202. bx::StringView array = bx::strFind(bx::StringView(name, int32_t(eol.getPtr()-parse.getPtr() ) ), "[");
  203. if (!array.isEmpty() )
  204. {
  205. bx::strCopy(uniformName, int32_t(array.getPtr()-name+1), name);
  206. char arraySize[32];
  207. bx::StringView arrayEnd = bx::strFind(bx::StringView(array.getPtr(), int32_t(eol.getPtr()-array.getPtr() ) ), "]");
  208. bx::strCopy(arraySize, int32_t(arrayEnd.getPtr()-array.getPtr() ), array.getPtr()+1);
  209. uint32_t tmp;
  210. bx::fromString(&tmp, arraySize);
  211. num = uint8_t(tmp);
  212. }
  213. else
  214. {
  215. bx::strCopy(uniformName, int32_t(eol.getPtr()-name+1), name);
  216. }
  217. Uniform un;
  218. un.type = nameToUniformTypeEnum(uniformType);
  219. if (UniformType::Count != un.type)
  220. {
  221. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  222. un.name = uniformName;
  223. un.num = num;
  224. un.regIndex = 0;
  225. un.regCount = num;
  226. uniforms.push_back(un);
  227. }
  228. parse = eol.getPtr() + 1;
  229. }
  230. }
  231. bx::StringView mainEntry("xlatMtlShaderOutput xlatMtlMain (");
  232. parse = bx::strFind(optimizedShader, mainEntry);
  233. end = parse;
  234. if (!parse.isEmpty())
  235. {
  236. parse.set(parse.getPtr() + mainEntry.getLength(), optShader.getTerm());
  237. end = bx::strFind(parse, "{");
  238. }
  239. while (parse.getPtr() < end.getPtr()
  240. && !parse.isEmpty())
  241. {
  242. parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm());
  243. const bx::StringView textureNameMark("[[texture(");
  244. const bx::StringView textureName = bx::strFind(parse, textureNameMark);
  245. if (!textureName.isEmpty())
  246. {
  247. Uniform un;
  248. un.type = nameToUniformTypeEnum("int"); // int for sampler
  249. const char* varNameEnd = textureName.getPtr() - 1;
  250. parse.set(parse.getPtr(), varNameEnd - 1);
  251. const char* varNameBeg = parse.getPtr();
  252. for (int ii = parse.getLength() - 1; 0 <= ii; --ii)
  253. {
  254. if (varNameBeg[ii] == ' ')
  255. {
  256. parse.set(varNameBeg + ii + 1, varNameEnd);
  257. break;
  258. }
  259. }
  260. char uniformName[256];
  261. bx::strCopy(uniformName, parse.getLength() + 1, parse);
  262. un.name = uniformName;
  263. const char* regIndexBeg = textureName.getPtr() + textureNameMark.getLength();
  264. bx::StringView regIndex = bx::strFind(regIndexBeg, ")");
  265. regIndex.set(regIndexBeg, regIndex.getPtr());
  266. uint32_t tmp;
  267. bx::fromString(&tmp, regIndex);
  268. un.regIndex = uint16_t(tmp);
  269. un.num = 1;
  270. un.regCount = 1;
  271. uniforms.push_back(un);
  272. parse = regIndex.getPtr() + 1;
  273. }
  274. else
  275. {
  276. parse = textureName;
  277. }
  278. }
  279. }
  280. uint16_t count = (uint16_t)uniforms.size();
  281. bx::write(_writer, count);
  282. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  283. {
  284. const Uniform& un = *it;
  285. uint8_t nameSize = (uint8_t)un.name.size();
  286. bx::write(_writer, nameSize);
  287. bx::write(_writer, un.name.c_str(), nameSize);
  288. uint8_t uniformType = uint8_t(un.type);
  289. bx::write(_writer, uniformType);
  290. bx::write(_writer, un.num);
  291. bx::write(_writer, un.regIndex);
  292. bx::write(_writer, un.regCount);
  293. bx::write(_writer, un.texComponent);
  294. bx::write(_writer, un.texDimension);
  295. bx::write(_writer, un.texFormat);
  296. BX_TRACE("%s, %s, %d, %d, %d"
  297. , un.name.c_str()
  298. , getUniformTypeName(un.type)
  299. , un.num
  300. , un.regIndex
  301. , un.regCount
  302. );
  303. }
  304. uint32_t shaderSize = (uint32_t)bx::strLen(optimizedShader);
  305. bx::write(_writer, shaderSize);
  306. bx::write(_writer, optimizedShader, shaderSize);
  307. uint8_t nul = 0;
  308. bx::write(_writer, nul);
  309. if (_options.disasm )
  310. {
  311. std::string disasmfp = _options.outputFilePath + ".disasm";
  312. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  313. }
  314. glslopt_shader_delete(shader);
  315. glslopt_cleanup(ctx);
  316. return true;
  317. }
  318. } // namespace glsl
  319. bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  320. {
  321. return glsl::compile(_options, _version, _code, _writer);
  322. }
  323. } // namespace bgfx