shaderc_glsl.cpp 7.0 KB

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