ShaderProgramCompilerMain.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (C) 2009-2020, 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/shader_compiler/ShaderProgramCompiler.h>
  6. #include <anki/Util.h>
  7. using namespace anki;
  8. static const char* USAGE = R"(Usage: %s 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. Error readAllText(CString filename, StringAuto& txt) final
  99. {
  100. StringAuto fname(txt.getAllocator());
  101. fname.sprintf("%s/%s", m_includePath.cstr(), filename.cstr());
  102. File file;
  103. ANKI_CHECK(file.open(fname, FileOpenFlag::READ));
  104. ANKI_CHECK(file.readAllText(txt));
  105. return Error::NONE;
  106. }
  107. } fsystem;
  108. fsystem.m_includePath = info.m_includePath;
  109. // Threading interface
  110. class TaskManager : public ShaderProgramAsyncTaskInterface
  111. {
  112. public:
  113. ThreadHive* m_hive = nullptr;
  114. HeapAllocator<U8> m_alloc;
  115. void enqueueTask(void (*callback)(void* userData), void* userData)
  116. {
  117. struct Ctx
  118. {
  119. void (*m_callback)(void* userData);
  120. void* m_userData;
  121. HeapAllocator<U8> m_alloc;
  122. };
  123. Ctx* ctx = m_alloc.newInstance<Ctx>();
  124. ctx->m_callback = callback;
  125. ctx->m_userData = userData;
  126. ctx->m_alloc = m_alloc;
  127. m_hive->submitTask(
  128. [](void* userData, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* signalSemaphore) {
  129. Ctx* ctx = static_cast<Ctx*>(userData);
  130. ctx->m_callback(ctx->m_userData);
  131. auto alloc = ctx->m_alloc;
  132. alloc.deleteInstance(ctx);
  133. },
  134. ctx);
  135. }
  136. Error joinTasks()
  137. {
  138. m_hive->waitAllTasks();
  139. return Error::NONE;
  140. }
  141. } taskManager;
  142. taskManager.m_hive =
  143. (info.m_threadCount) ? alloc.newInstance<ThreadHive>(info.m_threadCount, alloc, true) : nullptr;
  144. taskManager.m_alloc = alloc;
  145. // Some dummy caps
  146. GpuDeviceCapabilities caps;
  147. caps.m_gpuVendor = GpuVendor::AMD;
  148. caps.m_minorApiVersion = 1;
  149. caps.m_majorApiVersion = 1;
  150. BindlessLimits limits;
  151. limits.m_bindlessImageCount = 16;
  152. limits.m_bindlessTextureCount = 16;
  153. // Compile
  154. ShaderProgramBinaryWrapper binary(alloc);
  155. ANKI_CHECK(compileShaderProgram(info.m_inputFname, fsystem, nullptr, (info.m_threadCount) ? &taskManager : nullptr,
  156. alloc, caps, limits, binary));
  157. // Store the binary
  158. ANKI_CHECK(binary.serializeToFile(info.m_outFname));
  159. // Cleanup
  160. alloc.deleteInstance(taskManager.m_hive);
  161. return Error::NONE;
  162. }
  163. int main(int argc, char** argv)
  164. {
  165. CmdLineArgs info;
  166. if(parseCommandLineArgs(argc, argv, info))
  167. {
  168. ANKI_LOGE(USAGE, argv[0]);
  169. return 1;
  170. }
  171. if(info.m_outFname.isEmpty())
  172. {
  173. info.m_outFname.sprintf("%sbin", info.m_inputFname.cstr());
  174. }
  175. if(info.m_includePath.isEmpty())
  176. {
  177. info.m_includePath.create("./");
  178. }
  179. if(work(info))
  180. {
  181. ANKI_LOGE("Failed");
  182. return 1;
  183. }
  184. return 0;
  185. }