shaderc_glsl.cpp 4.9 KB

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