shaderc_glsl.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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
  8. {
  9. bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _gles, const std::string& _code, bx::WriterI* _writer)
  10. {
  11. char ch = 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 (_gles)
  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. if (3 == sscanf(log, "%u:%u(%u):", &source, &line, &column)
  42. && 0 != line)
  43. {
  44. start = bx::uint32_imax(1, line-10);
  45. end = start + 20;
  46. }
  47. printCode(_code.c_str(), line, start, end);
  48. fprintf(stderr, "Error: %s\n", log);
  49. glslopt_cleanup(ctx);
  50. return false;
  51. }
  52. const char* optimizedShader = glslopt_get_output(shader);
  53. // Trim all directives.
  54. while ('#' == *optimizedShader)
  55. {
  56. optimizedShader = bx::strnl(optimizedShader);
  57. }
  58. if (0 != _gles)
  59. {
  60. char* code = const_cast<char*>(optimizedShader);
  61. strReplace(code, "gl_FragDepthEXT", "gl_FragDepth");
  62. strReplace(code, "texture2DLodEXT", "texture2DLod");
  63. strReplace(code, "texture2DProjLodEXT", "texture2DProjLod");
  64. strReplace(code, "textureCubeLodEXT", "textureCubeLod");
  65. strReplace(code, "texture2DGradEXT", "texture2DGrad");
  66. strReplace(code, "texture2DProjGradEXT", "texture2DProjGrad");
  67. strReplace(code, "textureCubeGradEXT", "textureCubeGrad");
  68. strReplace(code, "shadow2DEXT", "shadow2D");
  69. strReplace(code, "shadow2DProjEXT", "shadow2DProj");
  70. }
  71. UniformArray uniforms;
  72. {
  73. const char* parse = optimizedShader;
  74. while (NULL != parse
  75. && *parse != '\0')
  76. {
  77. parse = bx::strws(parse);
  78. const char* eol = strchr(parse, ';');
  79. if (NULL != eol)
  80. {
  81. const char* qualifier = parse;
  82. parse = bx::strws(bx::strword(parse) );
  83. if (0 == strncmp(qualifier, "attribute", 9)
  84. || 0 == strncmp(qualifier, "varying", 7) )
  85. {
  86. // skip attributes and varyings.
  87. parse = eol + 1;
  88. continue;
  89. }
  90. if (0 != strncmp(qualifier, "uniform", 7) )
  91. {
  92. // end if there is no uniform keyword.
  93. parse = NULL;
  94. continue;
  95. }
  96. const char* precision = NULL;
  97. const char* typen = parse;
  98. if (0 == strncmp(typen, "lowp", 4)
  99. || 0 == strncmp(typen, "mediump", 7)
  100. || 0 == strncmp(typen, "highp", 5) )
  101. {
  102. precision = typen;
  103. typen = parse = bx::strws(bx::strword(parse) );
  104. }
  105. BX_UNUSED(precision);
  106. char uniformType[256];
  107. parse = bx::strword(parse);
  108. if (0 == strncmp(typen, "sampler", 7) )
  109. {
  110. strcpy(uniformType, "int");
  111. }
  112. else
  113. {
  114. bx::strlcpy(uniformType, typen, parse-typen+1);
  115. }
  116. const char* name = parse = bx::strws(parse);
  117. char uniformName[256];
  118. uint8_t num = 1;
  119. const char* array = bx::strnstr(name, "[", eol-parse);
  120. if (NULL != array)
  121. {
  122. bx::strlcpy(uniformName, name, array-name+1);
  123. char arraySize[32];
  124. const char* end = bx::strnstr(array, "]", eol-array);
  125. bx::strlcpy(arraySize, array+1, end-array);
  126. num = atoi(arraySize);
  127. }
  128. else
  129. {
  130. bx::strlcpy(uniformName, name, eol-name+1);
  131. }
  132. Uniform un;
  133. un.type = nameToUniformTypeEnum(uniformType);
  134. if (UniformType::Count != un.type)
  135. {
  136. BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
  137. un.name = uniformName;
  138. un.num = num;
  139. un.regIndex = 0;
  140. un.regCount = num;
  141. uniforms.push_back(un);
  142. }
  143. parse = eol + 1;
  144. }
  145. }
  146. }
  147. uint16_t count = (uint16_t)uniforms.size();
  148. bx::write(_writer, count);
  149. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  150. {
  151. const Uniform& un = *it;
  152. uint8_t nameSize = (uint8_t)un.name.size();
  153. bx::write(_writer, nameSize);
  154. bx::write(_writer, un.name.c_str(), nameSize);
  155. uint8_t uniformType = un.type;
  156. bx::write(_writer, uniformType);
  157. bx::write(_writer, un.num);
  158. bx::write(_writer, un.regIndex);
  159. bx::write(_writer, un.regCount);
  160. BX_TRACE("%s, %s, %d, %d, %d"
  161. , un.name.c_str()
  162. , getUniformTypeName(un.type)
  163. , un.num
  164. , un.regIndex
  165. , un.regCount
  166. );
  167. }
  168. uint32_t shaderSize = (uint32_t)strlen(optimizedShader);
  169. bx::write(_writer, shaderSize);
  170. bx::write(_writer, optimizedShader, shaderSize);
  171. uint8_t nul = 0;
  172. bx::write(_writer, nul);
  173. glslopt_cleanup(ctx);
  174. return true;
  175. }
  176. } // namespace bgfx