ShaderProgramCompilerMain.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/ShaderCompiler/ShaderProgramCompiler.h>
  6. #include <AnKi/Util.h>
  7. using namespace anki;
  8. static const char* USAGE = R"(Usage: %s input_shader_program_file [options]
  9. Options:
  10. -o <name of output> : The name of the output binary
  11. -j <thread count> : Number of threads. Defaults to system's max
  12. -I <include path> : The path of the #include files
  13. )";
  14. class CmdLineArgs
  15. {
  16. public:
  17. HeapAllocator<U8> m_alloc{allocAligned, nullptr};
  18. StringAuto m_inputFname = {m_alloc};
  19. StringAuto m_outFname = {m_alloc};
  20. StringAuto m_includePath = {m_alloc};
  21. U32 m_threadCount = getCpuCoresCount();
  22. };
  23. static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
  24. {
  25. // Parse config
  26. if(argc < 2)
  27. {
  28. return Error::USER_DATA;
  29. }
  30. info.m_inputFname.create(argv[1]);
  31. for(I i = 2; i < argc; i++)
  32. {
  33. if(strcmp(argv[i], "-o") == 0)
  34. {
  35. ++i;
  36. if(i < argc)
  37. {
  38. if(std::strlen(argv[i]) > 0)
  39. {
  40. info.m_outFname.sprintf("%s", argv[i]);
  41. }
  42. else
  43. {
  44. return Error::USER_DATA;
  45. }
  46. }
  47. else
  48. {
  49. return Error::USER_DATA;
  50. }
  51. }
  52. else if(strcmp(argv[i], "-j") == 0)
  53. {
  54. ++i;
  55. if(i < argc)
  56. {
  57. ANKI_CHECK(CString(argv[i]).toNumber(info.m_threadCount));
  58. }
  59. else
  60. {
  61. return Error::USER_DATA;
  62. }
  63. }
  64. else if(strcmp(argv[i], "-I") == 0)
  65. {
  66. ++i;
  67. if(i < argc)
  68. {
  69. if(std::strlen(argv[i]) > 0)
  70. {
  71. info.m_includePath.sprintf("%s", argv[i]);
  72. }
  73. else
  74. {
  75. return Error::USER_DATA;
  76. }
  77. }
  78. else
  79. {
  80. return Error::USER_DATA;
  81. }
  82. }
  83. else
  84. {
  85. return Error::USER_DATA;
  86. }
  87. }
  88. return Error::NONE;
  89. }
  90. static Error work(const CmdLineArgs& info)
  91. {
  92. HeapAllocator<U8> alloc{allocAligned, nullptr};
  93. // Load interface
  94. class FSystem : public ShaderProgramFilesystemInterface
  95. {
  96. public:
  97. CString m_includePath;
  98. U32 m_fileReadCount = 0;
  99. Error readAllTextInternal(CString filename, StringAuto& txt)
  100. {
  101. StringAuto fname(txt.getAllocator());
  102. // The first file is the input file. Don't append the include path to it
  103. if(m_fileReadCount == 0)
  104. {
  105. fname.sprintf("%s", filename.cstr());
  106. }
  107. else
  108. {
  109. fname.sprintf("%s/%s", m_includePath.cstr(), filename.cstr());
  110. }
  111. ++m_fileReadCount;
  112. File file;
  113. ANKI_CHECK(file.open(fname, FileOpenFlag::READ));
  114. ANKI_CHECK(file.readAllText(txt));
  115. return Error::NONE;
  116. }
  117. Error readAllText(CString filename, StringAuto& txt) final
  118. {
  119. const Error err = readAllTextInternal(filename, txt);
  120. if(err)
  121. {
  122. ANKI_LOGE("Failed to read file: %s", filename.cstr());
  123. }
  124. return err;
  125. }
  126. } fsystem;
  127. fsystem.m_includePath = info.m_includePath;
  128. // Threading interface
  129. class TaskManager : public ShaderProgramAsyncTaskInterface
  130. {
  131. public:
  132. ThreadHive* m_hive = nullptr;
  133. HeapAllocator<U8> m_alloc;
  134. void enqueueTask(void (*callback)(void* userData), void* userData)
  135. {
  136. struct Ctx
  137. {
  138. void (*m_callback)(void* userData);
  139. void* m_userData;
  140. HeapAllocator<U8> m_alloc;
  141. };
  142. Ctx* ctx = m_alloc.newInstance<Ctx>();
  143. ctx->m_callback = callback;
  144. ctx->m_userData = userData;
  145. ctx->m_alloc = m_alloc;
  146. m_hive->submitTask(
  147. [](void* userData, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* signalSemaphore) {
  148. Ctx* ctx = static_cast<Ctx*>(userData);
  149. ctx->m_callback(ctx->m_userData);
  150. auto alloc = ctx->m_alloc;
  151. alloc.deleteInstance(ctx);
  152. },
  153. ctx);
  154. }
  155. Error joinTasks()
  156. {
  157. m_hive->waitAllTasks();
  158. return Error::NONE;
  159. }
  160. } taskManager;
  161. taskManager.m_hive =
  162. (info.m_threadCount) ? alloc.newInstance<ThreadHive>(info.m_threadCount, alloc, true) : nullptr;
  163. taskManager.m_alloc = alloc;
  164. // Some dummy caps
  165. GpuDeviceCapabilities caps;
  166. caps.m_gpuVendor = GpuVendor::AMD;
  167. caps.m_minorApiVersion = 1;
  168. caps.m_majorApiVersion = 1;
  169. BindlessLimits limits;
  170. limits.m_bindlessImageCount = 16;
  171. limits.m_bindlessTextureCount = 16;
  172. // Compile
  173. ShaderProgramBinaryWrapper binary(alloc);
  174. ANKI_CHECK(compileShaderProgram(info.m_inputFname, fsystem, nullptr, (info.m_threadCount) ? &taskManager : nullptr,
  175. alloc, caps, limits, binary));
  176. // Store the binary
  177. ANKI_CHECK(binary.serializeToFile(info.m_outFname));
  178. // Cleanup
  179. alloc.deleteInstance(taskManager.m_hive);
  180. return Error::NONE;
  181. }
  182. int main(int argc, char** argv)
  183. {
  184. CmdLineArgs info;
  185. if(parseCommandLineArgs(argc, argv, info))
  186. {
  187. ANKI_LOGE(USAGE, argv[0]);
  188. return 1;
  189. }
  190. if(info.m_outFname.isEmpty())
  191. {
  192. getFilepathFilename(info.m_inputFname, info.m_outFname);
  193. info.m_outFname.append("bin");
  194. }
  195. if(info.m_includePath.isEmpty())
  196. {
  197. info.m_includePath.create("./");
  198. }
  199. if(work(info))
  200. {
  201. ANKI_LOGE("Failed");
  202. return 1;
  203. }
  204. ANKI_LOGI("Done!");
  205. return 0;
  206. }