shaderc_glsl.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2011-2017 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_cleanup(ctx);
  54. return false;
  55. }
  56. const char* optimizedShader = glslopt_get_output(shader);
  57. // Trim all directives.
  58. while ('#' == *optimizedShader)
  59. {
  60. optimizedShader = bx::strnl(optimizedShader);
  61. }
  62. {
  63. char* code = const_cast<char*>(optimizedShader);
  64. strReplace(code, "gl_FragDepthEXT", "gl_FragDepth");
  65. strReplace(code, "texture2DLodARB", "texture2DLod");
  66. strReplace(code, "texture2DLodEXT", "texture2DLod");
  67. strReplace(code, "texture2DGradARB", "texture2DGrad");
  68. strReplace(code, "texture2DGradEXT", "texture2DGrad");
  69. strReplace(code, "textureCubeLodARB", "textureCubeLod");
  70. strReplace(code, "textureCubeLodEXT", "textureCubeLod");
  71. strReplace(code, "textureCubeGradARB", "textureCubeGrad");
  72. strReplace(code, "textureCubeGradEXT", "textureCubeGrad");
  73. strReplace(code, "texture2DProjLodARB", "texture2DProjLod");
  74. strReplace(code, "texture2DProjLodEXT", "texture2DProjLod");
  75. strReplace(code, "texture2DProjGradARB", "texture2DProjGrad");
  76. strReplace(code, "texture2DProjGradEXT", "texture2DProjGrad");
  77. strReplace(code, "shadow2DARB", "shadow2D");
  78. strReplace(code, "shadow2DEXT", "shadow2D");
  79. strReplace(code, "shadow2DProjARB", "shadow2DProj");
  80. strReplace(code, "shadow2DProjEXT", "shadow2DProj");
  81. }
  82. UniformArray uniforms;
  83. if (target != kGlslTargetMetal)
  84. {
  85. const char* parse = optimizedShader;
  86. while (NULL != parse
  87. && *parse != '\0')
  88. {
  89. parse = bx::strws(parse);
  90. const char* eol = bx::strFind(parse, ';');
  91. if (NULL != eol)
  92. {
  93. const char* qualifier = parse;
  94. parse = bx::strws(bx::strword(parse) );
  95. if (0 == bx::strCmp(qualifier, "attribute", 9)
  96. || 0 == bx::strCmp(qualifier, "varying", 7)
  97. || 0 == bx::strCmp(qualifier, "in", 2)
  98. || 0 == bx::strCmp(qualifier, "out", 3)
  99. )
  100. {
  101. // skip attributes and varyings.
  102. parse = eol + 1;
  103. continue;
  104. }
  105. if (0 == bx::strCmp(parse, "tmpvar", 6) )
  106. {
  107. // skip temporaries
  108. parse = eol + 1;
  109. continue;
  110. }
  111. if (0 != bx::strCmp(qualifier, "uniform", 7) )
  112. {
  113. // end if there is no uniform keyword.
  114. parse = NULL;
  115. continue;
  116. }
  117. const char* precision = NULL;
  118. const char* typen = parse;
  119. if (0 == bx::strCmp(typen, "lowp", 4)
  120. || 0 == bx::strCmp(typen, "mediump", 7)
  121. || 0 == bx::strCmp(typen, "highp", 5) )
  122. {
  123. precision = typen;
  124. typen = parse = bx::strws(bx::strword(parse) );
  125. }
  126. BX_UNUSED(precision);
  127. char uniformType[256];
  128. parse = bx::strword(parse);
  129. if (0 == bx::strCmp(typen, "sampler", 7) )
  130. {
  131. bx::strCopy(uniformType, BX_COUNTOF(uniformType), "int");
  132. }
  133. else
  134. {
  135. bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
  136. }
  137. const char* name = parse = bx::strws(parse);
  138. char uniformName[256];
  139. uint8_t num = 1;
  140. const char* array = bx::strFind(bx::StringView(name, int32_t(eol-parse) ), "[");
  141. if (NULL != array)
  142. {
  143. bx::strCopy(uniformName, int32_t(array-name+1), name);
  144. char arraySize[32];
  145. const char* end = bx::strFind(bx::StringView(array, int32_t(eol-array) ), "]");
  146. bx::strCopy(arraySize, int32_t(end-array), array+1);
  147. num = uint8_t(atoi(arraySize) );
  148. }
  149. else
  150. {
  151. bx::strCopy(uniformName, int32_t(eol-name+1), name);
  152. }
  153. Uniform un;
  154. un.type = nameToUniformTypeEnum(uniformType);
  155. if (UniformType::Count != un.type)
  156. {
  157. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  158. un.name = uniformName;
  159. un.num = num;
  160. un.regIndex = 0;
  161. un.regCount = num;
  162. uniforms.push_back(un);
  163. }
  164. parse = eol + 1;
  165. }
  166. }
  167. }
  168. else
  169. {
  170. const char* parse = bx::strFind(optimizedShader, "struct xlatMtlShaderUniform {");
  171. const char* end = parse;
  172. if (NULL != parse)
  173. {
  174. parse += bx::strLen("struct xlatMtlShaderUniform {");
  175. end = bx::strFind(parse, "};");
  176. }
  177. while ( parse < end
  178. && *parse != '\0')
  179. {
  180. parse = bx::strws(parse);
  181. const char* eol = bx::strFind(parse, ';');
  182. if (NULL != eol)
  183. {
  184. const char* typen = parse;
  185. char uniformType[256];
  186. parse = bx::strword(parse);
  187. bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
  188. const char* name = parse = bx::strws(parse);
  189. char uniformName[256];
  190. uint8_t num = 1;
  191. const char* array = bx::strFind(bx::StringView(name, int32_t(eol-parse) ), "[");
  192. if (NULL != array)
  193. {
  194. bx::strCopy(uniformName, int32_t(array-name+1), name);
  195. char arraySize[32];
  196. const char* arrayEnd = bx::strFind(bx::StringView(array, int32_t(eol-array) ), "]");
  197. bx::strCopy(arraySize, int32_t(arrayEnd-array), array+1);
  198. num = uint8_t(atoi(arraySize) );
  199. }
  200. else
  201. {
  202. bx::strCopy(uniformName, int32_t(eol-name+1), name);
  203. }
  204. Uniform un;
  205. un.type = nameToUniformTypeEnum(uniformType);
  206. if (UniformType::Count != un.type)
  207. {
  208. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  209. un.name = uniformName;
  210. un.num = num;
  211. un.regIndex = 0;
  212. un.regCount = num;
  213. uniforms.push_back(un);
  214. }
  215. parse = eol + 1;
  216. }
  217. }
  218. }
  219. uint16_t count = (uint16_t)uniforms.size();
  220. bx::write(_writer, count);
  221. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  222. {
  223. const Uniform& un = *it;
  224. uint8_t nameSize = (uint8_t)un.name.size();
  225. bx::write(_writer, nameSize);
  226. bx::write(_writer, un.name.c_str(), nameSize);
  227. uint8_t uniformType = uint8_t(un.type);
  228. bx::write(_writer, uniformType);
  229. bx::write(_writer, un.num);
  230. bx::write(_writer, un.regIndex);
  231. bx::write(_writer, un.regCount);
  232. BX_TRACE("%s, %s, %d, %d, %d"
  233. , un.name.c_str()
  234. , getUniformTypeName(un.type)
  235. , un.num
  236. , un.regIndex
  237. , un.regCount
  238. );
  239. }
  240. uint32_t shaderSize = (uint32_t)bx::strLen(optimizedShader);
  241. bx::write(_writer, shaderSize);
  242. bx::write(_writer, optimizedShader, shaderSize);
  243. uint8_t nul = 0;
  244. bx::write(_writer, nul);
  245. if (_options.disasm )
  246. {
  247. std::string disasmfp = _options.outputFilePath + ".disasm";
  248. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  249. }
  250. glslopt_cleanup(ctx);
  251. return true;
  252. }
  253. } // namespace glsl
  254. bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  255. {
  256. return glsl::compile(_options, _version, _code, _writer);
  257. }
  258. } // namespace bgfx