shaderc_glsl.cpp 9.8 KB

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