shaderc_glsl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright 2011-2020 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. 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. bx::printf("Error: %s\n", log);
  53. glslopt_shader_delete(shader);
  54. glslopt_cleanup(ctx);
  55. return false;
  56. }
  57. const char* optimizedShader = glslopt_get_output(shader);
  58. // Trim all directives.
  59. while ('#' == *optimizedShader)
  60. {
  61. optimizedShader = bx::strFindNl(optimizedShader).getPtr();
  62. }
  63. {
  64. char* code = const_cast<char*>(optimizedShader);
  65. strReplace(code, "gl_FragDepthEXT", "gl_FragDepth");
  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, "attribute", 9)
  95. || 0 == bx::strCmp(qualifier, "varying", 7)
  96. || 0 == bx::strCmp(qualifier, "in", 2)
  97. || 0 == bx::strCmp(qualifier, "out", 3)
  98. )
  99. {
  100. // skip attributes and varyings.
  101. parse.set(eol.getPtr() + 1, parse.getTerm() );
  102. continue;
  103. }
  104. if (0 == bx::strCmp(qualifier, "flat", 4)
  105. || 0 == bx::strCmp(qualifier, "smooth", 6)
  106. || 0 == bx::strCmp(qualifier, "noperspective", 13)
  107. || 0 == bx::strCmp(qualifier, "centroid", 8)
  108. )
  109. {
  110. // skip interpolation qualifiers
  111. parse.set(eol.getPtr() + 1, parse.getTerm() );
  112. continue;
  113. }
  114. if (0 == bx::strCmp(parse, "tmpvar", 6) )
  115. {
  116. // skip temporaries
  117. parse.set(eol.getPtr() + 1, parse.getTerm() );
  118. continue;
  119. }
  120. if (0 != bx::strCmp(qualifier, "uniform", 7) )
  121. {
  122. // end if there is no uniform keyword.
  123. parse.clear();
  124. continue;
  125. }
  126. bx::StringView precision;
  127. bx::StringView typen = nextWord(parse);
  128. if (0 == bx::strCmp(typen, "lowp", 4)
  129. || 0 == bx::strCmp(typen, "mediump", 7)
  130. || 0 == bx::strCmp(typen, "highp", 5) )
  131. {
  132. precision = typen;
  133. typen = nextWord(parse);
  134. }
  135. BX_UNUSED(precision);
  136. char uniformType[256];
  137. if (0 == bx::strCmp(typen, "sampler", 7) )
  138. {
  139. bx::strCopy(uniformType, BX_COUNTOF(uniformType), "int");
  140. }
  141. else
  142. {
  143. bx::strCopy(uniformType, BX_COUNTOF(uniformType), typen);
  144. }
  145. bx::StringView name = nextWord(parse);
  146. uint8_t num = 1;
  147. bx::StringView array = bx::strSubstr(parse, 0, 1);
  148. if (0 == bx::strCmp(array, "[", 1) )
  149. {
  150. parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
  151. uint32_t tmp;
  152. bx::fromString(&tmp, parse);
  153. num = uint8_t(tmp);
  154. }
  155. Uniform un;
  156. un.type = nameToUniformTypeEnum(uniformType);
  157. if (UniformType::Count != un.type)
  158. {
  159. un.name.assign(name.getPtr(), name.getTerm());
  160. BX_TRACE("name: %s (type %d, num %d)", un.name.c_str(), un.type, num);
  161. un.num = num;
  162. un.regIndex = 0;
  163. un.regCount = num;
  164. uniforms.push_back(un);
  165. }
  166. parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), parse.getTerm() ) ) );
  167. }
  168. }
  169. }
  170. else
  171. {
  172. const bx::StringView optShader(optimizedShader);
  173. bx::StringView parse = bx::strFind(optimizedShader, "struct xlatMtlShaderUniform {");
  174. bx::StringView end = parse;
  175. if (!parse.isEmpty() )
  176. {
  177. parse.set(parse.getPtr() + bx::strLen("struct xlatMtlShaderUniform {"), optShader.getTerm() );
  178. end = bx::strFind(parse, "};");
  179. }
  180. while ( parse.getPtr() < end.getPtr()
  181. && !parse.isEmpty() )
  182. {
  183. parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm() );
  184. const bx::StringView eol = bx::strFind(parse, ';');
  185. if (!eol.isEmpty() )
  186. {
  187. const char* typen = parse.getPtr();
  188. char uniformType[256];
  189. parse = bx::strWord(parse);
  190. bx::strCopy(uniformType, parse.getLength()+1, typen);
  191. parse.set(parse.getPtr()+parse.getLength(),optShader.getTerm());
  192. const char* name = bx::strLTrimSpace(parse).getPtr();
  193. parse.set(name, optShader.getTerm() );
  194. char uniformName[256];
  195. uint8_t num = 1;
  196. bx::StringView array = bx::strFind(bx::StringView(name, int32_t(eol.getPtr()-parse.getPtr() ) ), "[");
  197. if (!array.isEmpty() )
  198. {
  199. bx::strCopy(uniformName, int32_t(array.getPtr()-name+1), name);
  200. char arraySize[32];
  201. bx::StringView arrayEnd = bx::strFind(bx::StringView(array.getPtr(), int32_t(eol.getPtr()-array.getPtr() ) ), "]");
  202. bx::strCopy(arraySize, int32_t(arrayEnd.getPtr()-array.getPtr() ), array.getPtr()+1);
  203. uint32_t tmp;
  204. bx::fromString(&tmp, arraySize);
  205. num = uint8_t(tmp);
  206. }
  207. else
  208. {
  209. bx::strCopy(uniformName, int32_t(eol.getPtr()-name+1), name);
  210. }
  211. Uniform un;
  212. un.type = nameToUniformTypeEnum(uniformType);
  213. if (UniformType::Count != un.type)
  214. {
  215. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  216. un.name = uniformName;
  217. un.num = num;
  218. un.regIndex = 0;
  219. un.regCount = num;
  220. uniforms.push_back(un);
  221. }
  222. parse = eol.getPtr() + 1;
  223. }
  224. }
  225. bx::StringView mainEntry("xlatMtlShaderOutput xlatMtlMain (");
  226. parse = bx::strFind(optimizedShader, mainEntry);
  227. end = parse;
  228. if (!parse.isEmpty())
  229. {
  230. parse.set(parse.getPtr() + mainEntry.getLength(), optShader.getTerm());
  231. end = bx::strFind(parse, "{");
  232. }
  233. while (parse.getPtr() < end.getPtr()
  234. && !parse.isEmpty())
  235. {
  236. parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm());
  237. const bx::StringView textureNameMark("[[texture(");
  238. const bx::StringView textureName = bx::strFind(parse, textureNameMark);
  239. if (!textureName.isEmpty())
  240. {
  241. Uniform un;
  242. un.type = nameToUniformTypeEnum("int"); // int for sampler
  243. const char* varNameEnd = textureName.getPtr() - 1;
  244. parse.set(parse.getPtr(), varNameEnd - 1);
  245. const char* varNameBeg = parse.getPtr();
  246. for (int ii = parse.getLength() - 1; 0 <= ii; --ii)
  247. {
  248. if (varNameBeg[ii] == ' ')
  249. {
  250. parse.set(varNameBeg + ii + 1, varNameEnd);
  251. break;
  252. }
  253. }
  254. char uniformName[256];
  255. bx::strCopy(uniformName, parse.getLength() + 1, parse);
  256. un.name = uniformName;
  257. const char* regIndexBeg = textureName.getPtr() + textureNameMark.getLength();
  258. bx::StringView regIndex = bx::strFind(regIndexBeg, ")");
  259. regIndex.set(regIndexBeg, regIndex.getPtr());
  260. uint32_t tmp;
  261. bx::fromString(&tmp, regIndex);
  262. un.regIndex = uint16_t(tmp);
  263. un.num = 1;
  264. un.regCount = 1;
  265. uniforms.push_back(un);
  266. parse = regIndex.getPtr() + 1;
  267. }
  268. else
  269. {
  270. parse = textureName;
  271. }
  272. }
  273. }
  274. uint16_t count = (uint16_t)uniforms.size();
  275. bx::write(_writer, count);
  276. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  277. {
  278. const Uniform& un = *it;
  279. uint8_t nameSize = (uint8_t)un.name.size();
  280. bx::write(_writer, nameSize);
  281. bx::write(_writer, un.name.c_str(), nameSize);
  282. uint8_t uniformType = uint8_t(un.type);
  283. bx::write(_writer, uniformType);
  284. bx::write(_writer, un.num);
  285. bx::write(_writer, un.regIndex);
  286. bx::write(_writer, un.regCount);
  287. bx::write(_writer, un.texComponent);
  288. bx::write(_writer, un.texDimension);
  289. BX_TRACE("%s, %s, %d, %d, %d"
  290. , un.name.c_str()
  291. , getUniformTypeName(un.type)
  292. , un.num
  293. , un.regIndex
  294. , un.regCount
  295. );
  296. }
  297. uint32_t shaderSize = (uint32_t)bx::strLen(optimizedShader);
  298. bx::write(_writer, shaderSize);
  299. bx::write(_writer, optimizedShader, shaderSize);
  300. uint8_t nul = 0;
  301. bx::write(_writer, nul);
  302. if (_options.disasm )
  303. {
  304. std::string disasmfp = _options.outputFilePath + ".disasm";
  305. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  306. }
  307. glslopt_shader_delete(shader);
  308. glslopt_cleanup(ctx);
  309. return true;
  310. }
  311. } // namespace glsl
  312. bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  313. {
  314. return glsl::compile(_options, _version, _code, _writer);
  315. }
  316. } // namespace bgfx