shaderc_glsl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2011-2022 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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, "textureLodEXT", "texture2DLod");
  65. strReplace(code, "textureGradEXT", "texture2DGrad");
  66. strReplace(code, "texture2DLodARB", "texture2DLod");
  67. strReplace(code, "texture2DLodEXT", "texture2DLod");
  68. strReplace(code, "texture2DGradARB", "texture2DGrad");
  69. strReplace(code, "texture2DGradEXT", "texture2DGrad");
  70. strReplace(code, "textureCubeLodARB", "textureCubeLod");
  71. strReplace(code, "textureCubeLodEXT", "textureCubeLod");
  72. strReplace(code, "textureCubeGradARB", "textureCubeGrad");
  73. strReplace(code, "textureCubeGradEXT", "textureCubeGrad");
  74. strReplace(code, "texture2DProjLodARB", "texture2DProjLod");
  75. strReplace(code, "texture2DProjLodEXT", "texture2DProjLod");
  76. strReplace(code, "texture2DProjGradARB", "texture2DProjGrad");
  77. strReplace(code, "texture2DProjGradEXT", "texture2DProjGrad");
  78. strReplace(code, "shadow2DARB", "shadow2D");
  79. strReplace(code, "shadow2DEXT", "shadow2D");
  80. strReplace(code, "shadow2DProjARB", "shadow2DProj");
  81. strReplace(code, "shadow2DProjEXT", "shadow2DProj");
  82. }
  83. UniformArray uniforms;
  84. if (target != kGlslTargetMetal)
  85. {
  86. bx::StringView parse(optimizedShader);
  87. while (!parse.isEmpty() )
  88. {
  89. parse = bx::strLTrimSpace(parse);
  90. bx::StringView eol = bx::strFind(parse, ';');
  91. if (!eol.isEmpty() )
  92. {
  93. bx::StringView qualifier = nextWord(parse);
  94. if (0 == bx::strCmp(qualifier, "precision", 9) )
  95. {
  96. // skip precision
  97. parse.set(eol.getPtr() + 1, parse.getTerm() );
  98. continue;
  99. }
  100. if (0 == bx::strCmp(qualifier, "attribute", 9)
  101. || 0 == bx::strCmp(qualifier, "varying", 7)
  102. || 0 == bx::strCmp(qualifier, "in", 2)
  103. || 0 == bx::strCmp(qualifier, "out", 3)
  104. )
  105. {
  106. // skip attributes and varyings.
  107. parse.set(eol.getPtr() + 1, parse.getTerm() );
  108. continue;
  109. }
  110. if (0 == bx::strCmp(qualifier, "flat", 4)
  111. || 0 == bx::strCmp(qualifier, "smooth", 6)
  112. || 0 == bx::strCmp(qualifier, "noperspective", 13)
  113. || 0 == bx::strCmp(qualifier, "centroid", 8)
  114. )
  115. {
  116. // skip interpolation qualifiers
  117. parse.set(eol.getPtr() + 1, parse.getTerm() );
  118. continue;
  119. }
  120. if (0 == bx::strCmp(parse, "tmpvar", 6) )
  121. {
  122. // skip temporaries
  123. parse.set(eol.getPtr() + 1, parse.getTerm() );
  124. continue;
  125. }
  126. if (0 != bx::strCmp(qualifier, "uniform", 7) )
  127. {
  128. // end if there is no uniform keyword.
  129. parse.clear();
  130. continue;
  131. }
  132. bx::StringView precision;
  133. bx::StringView typen = nextWord(parse);
  134. if (0 == bx::strCmp(typen, "lowp", 4)
  135. || 0 == bx::strCmp(typen, "mediump", 7)
  136. || 0 == bx::strCmp(typen, "highp", 5) )
  137. {
  138. precision = typen;
  139. typen = nextWord(parse);
  140. }
  141. BX_UNUSED(precision);
  142. char uniformType[256];
  143. if (0 == bx::strCmp(typen, "sampler", 7)
  144. || 0 == bx::strCmp(typen, "isampler", 8)
  145. || 0 == bx::strCmp(typen, "usampler", 8) )
  146. {
  147. bx::strCopy(uniformType, BX_COUNTOF(uniformType), "int");
  148. }
  149. else
  150. {
  151. bx::strCopy(uniformType, BX_COUNTOF(uniformType), typen);
  152. }
  153. bx::StringView name = nextWord(parse);
  154. uint8_t num = 1;
  155. bx::StringView array = bx::strSubstr(parse, 0, 1);
  156. if (0 == bx::strCmp(array, "[", 1) )
  157. {
  158. parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
  159. uint32_t tmp;
  160. bx::fromString(&tmp, parse);
  161. num = uint8_t(tmp);
  162. }
  163. Uniform un;
  164. un.type = nameToUniformTypeEnum(uniformType);
  165. if (UniformType::Count != un.type)
  166. {
  167. un.name.assign(name.getPtr(), name.getTerm());
  168. BX_TRACE("name: %s (type %d, num %d)", un.name.c_str(), un.type, num);
  169. un.num = num;
  170. un.regIndex = 0;
  171. un.regCount = num;
  172. uniforms.push_back(un);
  173. }
  174. parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), parse.getTerm() ) ) );
  175. }
  176. }
  177. }
  178. else
  179. {
  180. const bx::StringView optShader(optimizedShader);
  181. bx::StringView parse = bx::strFind(optimizedShader, "struct xlatMtlShaderUniform {");
  182. bx::StringView end = parse;
  183. if (!parse.isEmpty() )
  184. {
  185. parse.set(parse.getPtr() + bx::strLen("struct xlatMtlShaderUniform {"), optShader.getTerm() );
  186. end = bx::strFind(parse, "};");
  187. }
  188. while ( parse.getPtr() < end.getPtr()
  189. && !parse.isEmpty() )
  190. {
  191. parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm() );
  192. const bx::StringView eol = bx::strFind(parse, ';');
  193. if (!eol.isEmpty() )
  194. {
  195. const char* typen = parse.getPtr();
  196. char uniformType[256];
  197. parse = bx::strWord(parse);
  198. bx::strCopy(uniformType, parse.getLength()+1, typen);
  199. parse.set(parse.getPtr()+parse.getLength(),optShader.getTerm());
  200. const char* name = bx::strLTrimSpace(parse).getPtr();
  201. parse.set(name, optShader.getTerm() );
  202. char uniformName[256];
  203. uint8_t num = 1;
  204. bx::StringView array = bx::strFind(bx::StringView(name, int32_t(eol.getPtr()-parse.getPtr() ) ), "[");
  205. if (!array.isEmpty() )
  206. {
  207. bx::strCopy(uniformName, int32_t(array.getPtr()-name+1), name);
  208. char arraySize[32];
  209. bx::StringView arrayEnd = bx::strFind(bx::StringView(array.getPtr(), int32_t(eol.getPtr()-array.getPtr() ) ), "]");
  210. bx::strCopy(arraySize, int32_t(arrayEnd.getPtr()-array.getPtr() ), array.getPtr()+1);
  211. uint32_t tmp;
  212. bx::fromString(&tmp, arraySize);
  213. num = uint8_t(tmp);
  214. }
  215. else
  216. {
  217. bx::strCopy(uniformName, int32_t(eol.getPtr()-name+1), name);
  218. }
  219. Uniform un;
  220. un.type = nameToUniformTypeEnum(uniformType);
  221. if (UniformType::Count != un.type)
  222. {
  223. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  224. un.name = uniformName;
  225. un.num = num;
  226. un.regIndex = 0;
  227. un.regCount = num;
  228. uniforms.push_back(un);
  229. }
  230. parse = eol.getPtr() + 1;
  231. }
  232. }
  233. bx::StringView mainEntry("xlatMtlShaderOutput xlatMtlMain (");
  234. parse = bx::strFind(optimizedShader, mainEntry);
  235. end = parse;
  236. if (!parse.isEmpty())
  237. {
  238. parse.set(parse.getPtr() + mainEntry.getLength(), optShader.getTerm());
  239. end = bx::strFind(parse, "{");
  240. }
  241. while (parse.getPtr() < end.getPtr()
  242. && !parse.isEmpty())
  243. {
  244. parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm());
  245. const bx::StringView textureNameMark("[[texture(");
  246. const bx::StringView textureName = bx::strFind(parse, textureNameMark);
  247. if (!textureName.isEmpty())
  248. {
  249. Uniform un;
  250. un.type = nameToUniformTypeEnum("int"); // int for sampler
  251. const char* varNameEnd = textureName.getPtr() - 1;
  252. parse.set(parse.getPtr(), varNameEnd - 1);
  253. const char* varNameBeg = parse.getPtr();
  254. for (int ii = parse.getLength() - 1; 0 <= ii; --ii)
  255. {
  256. if (varNameBeg[ii] == ' ')
  257. {
  258. parse.set(varNameBeg + ii + 1, varNameEnd);
  259. break;
  260. }
  261. }
  262. char uniformName[256];
  263. bx::strCopy(uniformName, parse.getLength() + 1, parse);
  264. un.name = uniformName;
  265. const char* regIndexBeg = textureName.getPtr() + textureNameMark.getLength();
  266. bx::StringView regIndex = bx::strFind(regIndexBeg, ")");
  267. regIndex.set(regIndexBeg, regIndex.getPtr());
  268. uint32_t tmp;
  269. bx::fromString(&tmp, regIndex);
  270. un.regIndex = uint16_t(tmp);
  271. un.num = 1;
  272. un.regCount = 1;
  273. uniforms.push_back(un);
  274. parse = regIndex.getPtr() + 1;
  275. }
  276. else
  277. {
  278. parse = textureName;
  279. }
  280. }
  281. }
  282. bx::ErrorAssert err;
  283. uint16_t count = (uint16_t)uniforms.size();
  284. bx::write(_writer, count, &err);
  285. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  286. {
  287. const Uniform& un = *it;
  288. uint8_t nameSize = (uint8_t)un.name.size();
  289. bx::write(_writer, nameSize, &err);
  290. bx::write(_writer, un.name.c_str(), nameSize, &err);
  291. uint8_t uniformType = uint8_t(un.type);
  292. bx::write(_writer, uniformType, &err);
  293. bx::write(_writer, un.num, &err);
  294. bx::write(_writer, un.regIndex, &err);
  295. bx::write(_writer, un.regCount, &err);
  296. bx::write(_writer, un.texComponent, &err);
  297. bx::write(_writer, un.texDimension, &err);
  298. bx::write(_writer, un.texFormat, &err);
  299. BX_TRACE("%s, %s, %d, %d, %d"
  300. , un.name.c_str()
  301. , getUniformTypeName(un.type)
  302. , un.num
  303. , un.regIndex
  304. , un.regCount
  305. );
  306. }
  307. uint32_t shaderSize = (uint32_t)bx::strLen(optimizedShader);
  308. bx::write(_writer, shaderSize, &err);
  309. bx::write(_writer, optimizedShader, shaderSize, &err);
  310. uint8_t nul = 0;
  311. bx::write(_writer, nul, &err);
  312. if (_options.disasm )
  313. {
  314. std::string disasmfp = _options.outputFilePath + ".disasm";
  315. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  316. }
  317. glslopt_shader_delete(shader);
  318. glslopt_cleanup(ctx);
  319. return true;
  320. }
  321. } // namespace glsl
  322. bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  323. {
  324. return glsl::compile(_options, _version, _code, _writer);
  325. }
  326. } // namespace bgfx