SkSLMain.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include <fstream>
  8. #include "src/sksl/SkSLCompiler.h"
  9. #include "src/sksl/SkSLFileOutputStream.h"
  10. // Given the path to a file (e.g. src/gpu/effects/GrFooFragmentProcessor.fp) and the expected
  11. // filename prefix and suffix (e.g. "Gr" and ".fp"), returns the "base name" of the
  12. // file (in this case, 'FooFragmentProcessor'). If no match, returns the empty string.
  13. static SkSL::String base_name(const char* fpPath, const char* prefix, const char* suffix) {
  14. SkSL::String result;
  15. const char* end = fpPath + strlen(fpPath);
  16. const char* fileName = end;
  17. // back up until we find a slash
  18. while (fileName != fpPath && '/' != *(fileName - 1) && '\\' != *(fileName - 1)) {
  19. --fileName;
  20. }
  21. if (!strncmp(fileName, prefix, strlen(prefix)) &&
  22. !strncmp(end - strlen(suffix), suffix, strlen(suffix))) {
  23. result.append(fileName + strlen(prefix), end - fileName - strlen(prefix) - strlen(suffix));
  24. }
  25. return result;
  26. }
  27. /**
  28. * Very simple standalone executable to facilitate testing.
  29. */
  30. int main(int argc, const char** argv) {
  31. if (argc != 3) {
  32. printf("usage: skslc <input> <output>\n");
  33. exit(1);
  34. }
  35. SkSL::Program::Kind kind;
  36. SkSL::String input(argv[1]);
  37. if (input.endsWith(".vert")) {
  38. kind = SkSL::Program::kVertex_Kind;
  39. } else if (input.endsWith(".frag")) {
  40. kind = SkSL::Program::kFragment_Kind;
  41. } else if (input.endsWith(".geom")) {
  42. kind = SkSL::Program::kGeometry_Kind;
  43. } else if (input.endsWith(".fp")) {
  44. kind = SkSL::Program::kFragmentProcessor_Kind;
  45. } else if (input.endsWith(".stage")) {
  46. kind = SkSL::Program::kPipelineStage_Kind;
  47. } else {
  48. printf("input filename must end in '.vert', '.frag', '.geom', '.fp', or '.stage'\n");
  49. exit(1);
  50. }
  51. std::ifstream in(argv[1]);
  52. std::string stdText((std::istreambuf_iterator<char>(in)),
  53. std::istreambuf_iterator<char>());
  54. SkSL::String text(stdText.c_str());
  55. if (in.rdstate()) {
  56. printf("error reading '%s'\n", argv[1]);
  57. exit(2);
  58. }
  59. SkSL::Program::Settings settings;
  60. settings.fArgs.insert(std::make_pair("gpImplementsDistanceVector", 1));
  61. SkSL::String name(argv[2]);
  62. if (name.endsWith(".spirv")) {
  63. SkSL::FileOutputStream out(argv[2]);
  64. SkSL::Compiler compiler;
  65. if (!out.isValid()) {
  66. printf("error writing '%s'\n", argv[2]);
  67. exit(4);
  68. }
  69. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
  70. if (!program || !compiler.toSPIRV(*program, out)) {
  71. printf("%s", compiler.errorText().c_str());
  72. exit(3);
  73. }
  74. if (!out.close()) {
  75. printf("error writing '%s'\n", argv[2]);
  76. exit(4);
  77. }
  78. } else if (name.endsWith(".glsl")) {
  79. SkSL::FileOutputStream out(argv[2]);
  80. SkSL::Compiler compiler;
  81. if (!out.isValid()) {
  82. printf("error writing '%s'\n", argv[2]);
  83. exit(4);
  84. }
  85. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
  86. if (!program || !compiler.toGLSL(*program, out)) {
  87. printf("%s", compiler.errorText().c_str());
  88. exit(3);
  89. }
  90. if (!out.close()) {
  91. printf("error writing '%s'\n", argv[2]);
  92. exit(4);
  93. }
  94. } else if (name.endsWith(".metal")) {
  95. SkSL::FileOutputStream out(argv[2]);
  96. SkSL::Compiler compiler;
  97. if (!out.isValid()) {
  98. printf("error writing '%s'\n", argv[2]);
  99. exit(4);
  100. }
  101. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
  102. if (!program || !compiler.toMetal(*program, out)) {
  103. printf("%s", compiler.errorText().c_str());
  104. exit(3);
  105. }
  106. if (!out.close()) {
  107. printf("error writing '%s'\n", argv[2]);
  108. exit(4);
  109. }
  110. } else if (name.endsWith(".h")) {
  111. SkSL::FileOutputStream out(argv[2]);
  112. SkSL::Compiler compiler(SkSL::Compiler::kPermitInvalidStaticTests_Flag);
  113. if (!out.isValid()) {
  114. printf("error writing '%s'\n", argv[2]);
  115. exit(4);
  116. }
  117. settings.fReplaceSettings = false;
  118. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
  119. if (!program || !compiler.toH(*program, base_name(argv[1], "Gr", ".fp"), out)) {
  120. printf("%s", compiler.errorText().c_str());
  121. exit(3);
  122. }
  123. if (!out.close()) {
  124. printf("error writing '%s'\n", argv[2]);
  125. exit(4);
  126. }
  127. } else if (name.endsWith(".cpp")) {
  128. SkSL::FileOutputStream out(argv[2]);
  129. SkSL::Compiler compiler(SkSL::Compiler::kPermitInvalidStaticTests_Flag);
  130. if (!out.isValid()) {
  131. printf("error writing '%s'\n", argv[2]);
  132. exit(4);
  133. }
  134. settings.fReplaceSettings = false;
  135. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
  136. if (!program || !compiler.toCPP(*program, base_name(argv[1], "Gr", ".fp"), out)) {
  137. printf("%s", compiler.errorText().c_str());
  138. exit(3);
  139. }
  140. if (!out.close()) {
  141. printf("error writing '%s'\n", argv[2]);
  142. exit(4);
  143. }
  144. } else {
  145. printf("expected output filename to end with '.spirv', '.glsl', '.cpp', '.h', or '.metal'");
  146. exit(1);
  147. }
  148. }