shaderc_glsl.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2011-2018 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::strnl(optimizedShader);
  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. const char* parse = optimizedShader;
  87. while (NULL != parse
  88. && *parse != '\0')
  89. {
  90. parse = bx::strws(parse);
  91. const char* eol = bx::strFind(parse, ';');
  92. if (NULL != eol)
  93. {
  94. const char* qualifier = parse;
  95. parse = bx::strws(bx::strSkipWord(parse) );
  96. if (0 == bx::strCmp(qualifier, "attribute", 9)
  97. || 0 == bx::strCmp(qualifier, "varying", 7)
  98. || 0 == bx::strCmp(qualifier, "in", 2)
  99. || 0 == bx::strCmp(qualifier, "out", 3)
  100. )
  101. {
  102. // skip attributes and varyings.
  103. parse = eol + 1;
  104. continue;
  105. }
  106. if (0 == bx::strCmp(parse, "tmpvar", 6) )
  107. {
  108. // skip temporaries
  109. parse = eol + 1;
  110. continue;
  111. }
  112. if (0 != bx::strCmp(qualifier, "uniform", 7) )
  113. {
  114. // end if there is no uniform keyword.
  115. parse = NULL;
  116. continue;
  117. }
  118. const char* precision = NULL;
  119. const char* typen = parse;
  120. if (0 == bx::strCmp(typen, "lowp", 4)
  121. || 0 == bx::strCmp(typen, "mediump", 7)
  122. || 0 == bx::strCmp(typen, "highp", 5) )
  123. {
  124. precision = typen;
  125. typen = parse = bx::strws(bx::strSkipWord(parse) );
  126. }
  127. BX_UNUSED(precision);
  128. char uniformType[256];
  129. parse = bx::strSkipWord(parse);
  130. if (0 == bx::strCmp(typen, "sampler", 7) )
  131. {
  132. bx::strCopy(uniformType, BX_COUNTOF(uniformType), "int");
  133. }
  134. else
  135. {
  136. bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
  137. }
  138. const char* name = parse = bx::strws(parse);
  139. char uniformName[256];
  140. uint8_t num = 1;
  141. const char* array = bx::strFind(bx::StringView(name, int32_t(eol-parse) ), "[");
  142. if (NULL != array)
  143. {
  144. bx::strCopy(uniformName, int32_t(array-name+1), name);
  145. char arraySize[32];
  146. const char* end = bx::strFind(bx::StringView(array, int32_t(eol-array) ), "]");
  147. bx::strCopy(arraySize, int32_t(end-array), array+1);
  148. num = uint8_t(atoi(arraySize) );
  149. }
  150. else
  151. {
  152. bx::strCopy(uniformName, int32_t(eol-name+1), name);
  153. }
  154. Uniform un;
  155. un.type = nameToUniformTypeEnum(uniformType);
  156. if (UniformType::Count != un.type)
  157. {
  158. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  159. un.name = uniformName;
  160. un.num = num;
  161. un.regIndex = 0;
  162. un.regCount = num;
  163. uniforms.push_back(un);
  164. }
  165. parse = eol + 1;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. const char* parse = bx::strFind(optimizedShader, "struct xlatMtlShaderUniform {");
  172. const char* end = parse;
  173. if (NULL != parse)
  174. {
  175. parse += bx::strLen("struct xlatMtlShaderUniform {");
  176. end = bx::strFind(parse, "};");
  177. }
  178. while ( parse < end
  179. && *parse != '\0')
  180. {
  181. parse = bx::strws(parse);
  182. const char* eol = bx::strFind(parse, ';');
  183. if (NULL != eol)
  184. {
  185. const char* typen = parse;
  186. char uniformType[256];
  187. parse = bx::strSkipWord(parse);
  188. bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
  189. const char* name = parse = bx::strws(parse);
  190. char uniformName[256];
  191. uint8_t num = 1;
  192. const char* array = bx::strFind(bx::StringView(name, int32_t(eol-parse) ), "[");
  193. if (NULL != array)
  194. {
  195. bx::strCopy(uniformName, int32_t(array-name+1), name);
  196. char arraySize[32];
  197. const char* arrayEnd = bx::strFind(bx::StringView(array, int32_t(eol-array) ), "]");
  198. bx::strCopy(arraySize, int32_t(arrayEnd-array), array+1);
  199. num = uint8_t(atoi(arraySize) );
  200. }
  201. else
  202. {
  203. bx::strCopy(uniformName, int32_t(eol-name+1), name);
  204. }
  205. Uniform un;
  206. un.type = nameToUniformTypeEnum(uniformType);
  207. if (UniformType::Count != un.type)
  208. {
  209. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  210. un.name = uniformName;
  211. un.num = num;
  212. un.regIndex = 0;
  213. un.regCount = num;
  214. uniforms.push_back(un);
  215. }
  216. parse = eol + 1;
  217. }
  218. }
  219. }
  220. uint16_t count = (uint16_t)uniforms.size();
  221. bx::write(_writer, count);
  222. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  223. {
  224. const Uniform& un = *it;
  225. uint8_t nameSize = (uint8_t)un.name.size();
  226. bx::write(_writer, nameSize);
  227. bx::write(_writer, un.name.c_str(), nameSize);
  228. uint8_t uniformType = uint8_t(un.type);
  229. bx::write(_writer, uniformType);
  230. bx::write(_writer, un.num);
  231. bx::write(_writer, un.regIndex);
  232. bx::write(_writer, un.regCount);
  233. BX_TRACE("%s, %s, %d, %d, %d"
  234. , un.name.c_str()
  235. , getUniformTypeName(un.type)
  236. , un.num
  237. , un.regIndex
  238. , un.regCount
  239. );
  240. }
  241. uint32_t shaderSize = (uint32_t)bx::strLen(optimizedShader);
  242. bx::write(_writer, shaderSize);
  243. bx::write(_writer, optimizedShader, shaderSize);
  244. uint8_t nul = 0;
  245. bx::write(_writer, nul);
  246. if (_options.disasm )
  247. {
  248. std::string disasmfp = _options.outputFilePath + ".disasm";
  249. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  250. }
  251. glslopt_shader_delete(shader);
  252. glslopt_cleanup(ctx);
  253. return true;
  254. }
  255. } // namespace glsl
  256. bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  257. {
  258. return glsl::compile(_options, _version, _code, _writer);
  259. }
  260. } // namespace bgfx