shaderc_glsl.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2011-2016 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. {
  76. const char* parse = optimizedShader;
  77. while (NULL != parse
  78. && *parse != '\0')
  79. {
  80. parse = bx::strws(parse);
  81. const char* eol = strchr(parse, ';');
  82. if (NULL != eol)
  83. {
  84. const char* qualifier = parse;
  85. parse = bx::strws(bx::strword(parse) );
  86. if (0 == strncmp(qualifier, "attribute", 9)
  87. || 0 == strncmp(qualifier, "varying", 7) )
  88. {
  89. // skip attributes and varyings.
  90. parse = eol + 1;
  91. continue;
  92. }
  93. if (0 != strncmp(qualifier, "uniform", 7) )
  94. {
  95. // end if there is no uniform keyword.
  96. parse = NULL;
  97. continue;
  98. }
  99. const char* precision = NULL;
  100. const char* typen = parse;
  101. if (0 == strncmp(typen, "lowp", 4)
  102. || 0 == strncmp(typen, "mediump", 7)
  103. || 0 == strncmp(typen, "highp", 5) )
  104. {
  105. precision = typen;
  106. typen = parse = bx::strws(bx::strword(parse) );
  107. }
  108. BX_UNUSED(precision);
  109. char uniformType[256];
  110. parse = bx::strword(parse);
  111. if (0 == strncmp(typen, "sampler", 7) )
  112. {
  113. strcpy(uniformType, "int");
  114. }
  115. else
  116. {
  117. bx::strlcpy(uniformType, typen, parse-typen+1);
  118. }
  119. const char* name = parse = bx::strws(parse);
  120. char uniformName[256];
  121. uint8_t num = 1;
  122. const char* array = bx::strnstr(name, "[", eol-parse);
  123. if (NULL != array)
  124. {
  125. bx::strlcpy(uniformName, name, array-name+1);
  126. char arraySize[32];
  127. const char* end = bx::strnstr(array, "]", eol-array);
  128. bx::strlcpy(arraySize, array+1, end-array);
  129. num = uint8_t(atoi(arraySize) );
  130. }
  131. else
  132. {
  133. bx::strlcpy(uniformName, name, eol-name+1);
  134. }
  135. Uniform un;
  136. un.type = nameToUniformTypeEnum(uniformType);
  137. if (UniformType::Count != un.type)
  138. {
  139. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  140. un.name = uniformName;
  141. un.num = num;
  142. un.regIndex = 0;
  143. un.regCount = num;
  144. uniforms.push_back(un);
  145. }
  146. parse = eol + 1;
  147. }
  148. }
  149. }
  150. uint16_t count = (uint16_t)uniforms.size();
  151. bx::write(_writer, count);
  152. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  153. {
  154. const Uniform& un = *it;
  155. uint8_t nameSize = (uint8_t)un.name.size();
  156. bx::write(_writer, nameSize);
  157. bx::write(_writer, un.name.c_str(), nameSize);
  158. uint8_t uniformType = un.type;
  159. bx::write(_writer, uniformType);
  160. bx::write(_writer, un.num);
  161. bx::write(_writer, un.regIndex);
  162. bx::write(_writer, un.regCount);
  163. BX_TRACE("%s, %s, %d, %d, %d"
  164. , un.name.c_str()
  165. , getUniformTypeName(un.type)
  166. , un.num
  167. , un.regIndex
  168. , un.regCount
  169. );
  170. }
  171. uint32_t shaderSize = (uint32_t)strlen(optimizedShader);
  172. bx::write(_writer, shaderSize);
  173. bx::write(_writer, optimizedShader, shaderSize);
  174. uint8_t nul = 0;
  175. bx::write(_writer, nul);
  176. if (_cmdLine.hasArg('\0', "disasm") )
  177. {
  178. std::string disasmfp = _cmdLine.findOption('o');
  179. disasmfp += ".disasm";
  180. writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
  181. }
  182. glslopt_cleanup(ctx);
  183. return true;
  184. }
  185. } // namespace glsl
  186. bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  187. {
  188. return glsl::compile(_cmdLine, _version, _code, _writer);
  189. }
  190. } // namespace bgfx