shaderc_glsl.cpp 4.8 KB

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