ShaderCompiler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Context.h"
  24. #include "File.h"
  25. #include "FileSystem.h"
  26. #include "GraphicsDefs.h"
  27. #include "List.h"
  28. #include "Mutex.h"
  29. #include "ProcessUtils.h"
  30. #include "ShaderParser.h"
  31. #include "StringUtils.h"
  32. #include "Thread.h"
  33. #include "XMLFile.h"
  34. #include <cstdio>
  35. #include <cstring>
  36. #include <windows.h>
  37. #include <d3dcompiler.h>
  38. #include <mojoshader.h>
  39. #include "DebugNew.h"
  40. using namespace Urho3D;
  41. struct Parameter
  42. {
  43. Parameter()
  44. {
  45. }
  46. Parameter(const String& name, unsigned reg, unsigned regCount) :
  47. name_(name),
  48. register_(reg),
  49. regCount_(regCount)
  50. {
  51. }
  52. bool operator < (const Parameter& rhs) const
  53. {
  54. if (register_ != rhs.register_)
  55. return register_ < rhs.register_;
  56. else if (name_ != rhs.name_)
  57. return name_ < rhs.name_;
  58. else
  59. return regCount_ < rhs.regCount_;
  60. }
  61. bool operator > (const Parameter& rhs) const
  62. {
  63. if (register_ != rhs.register_)
  64. return register_ > rhs.register_;
  65. else if (name_ != rhs.name_)
  66. return name_ > rhs.name_;
  67. else
  68. return regCount_ > rhs.regCount_;
  69. }
  70. bool operator == (const Parameter& rhs) const { return register_ == rhs.register_ && name_ == rhs.name_ && regCount_ == rhs.regCount_; }
  71. bool operator != (const Parameter& rhs) const { return register_ != rhs.register_ || name_ != rhs.name_ || regCount_ != rhs.regCount_; }
  72. String name_;
  73. unsigned register_;
  74. unsigned regCount_;
  75. };
  76. struct CompiledVariation
  77. {
  78. ShaderType type_;
  79. String name_;
  80. String outFileName_;
  81. Vector<String> defines_;
  82. Vector<String> defineValues_;
  83. PODVector<unsigned char> byteCode_;
  84. Vector<Parameter> constants_;
  85. Vector<Parameter> textureUnits_;
  86. String errorMsg_;
  87. };
  88. SharedPtr<Context> context_(new Context());
  89. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  90. String inDir_;
  91. String outDir_;
  92. Vector<String> defines_;
  93. Vector<String> defineValues_;
  94. String variationName_;
  95. volatile bool compileFailed_ = false;
  96. bool useSM3_ = false;
  97. bool compileVariation_ = false;
  98. bool compileVS_ = true;
  99. bool compilePS_ = true;
  100. List<CompiledVariation> variations_;
  101. List<CompiledVariation*> workList_;
  102. Mutex workMutex_;
  103. String hlslCode_;
  104. int main(int argc, char** argv);
  105. void Run(const Vector<String>& arguments);
  106. void CompileShader(const String& fileName);
  107. void CompileVariation(CompiledVariation* variation);
  108. void CopyStrippedCode(PODVector<unsigned char>& dest, void* src, unsigned srcSize);
  109. class WorkerThread : public RefCounted, public Thread
  110. {
  111. public:
  112. void ThreadFunction()
  113. {
  114. for (;;)
  115. {
  116. CompiledVariation* workItem = 0;
  117. {
  118. MutexLock lock(workMutex_);
  119. if (!workList_.Empty())
  120. {
  121. workItem = workList_.Front();
  122. workList_.Erase(workList_.Begin());
  123. }
  124. }
  125. if (!workItem)
  126. return;
  127. // If compile(s) failed, just empty the list, but do not compile more
  128. if (!compileFailed_)
  129. CompileVariation(workItem);
  130. }
  131. }
  132. };
  133. class IncludeHandler : public ID3DInclude
  134. {
  135. public:
  136. STDMETHOD(Open)(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
  137. {
  138. String fileName = inDir_ + String((const char*)pFileName);
  139. File file(context_, fileName);
  140. if (!file.IsOpen())
  141. return E_FAIL;
  142. unsigned fileSize = file.GetSize();
  143. void* fileData = new unsigned char[fileSize];
  144. *pBytes = fileSize;
  145. *ppData = fileData;
  146. file.Read(fileData, fileSize);
  147. return S_OK;
  148. }
  149. STDMETHOD(Close)(LPCVOID pData)
  150. {
  151. delete[] (unsigned char*)pData;
  152. return S_OK;
  153. }
  154. };
  155. int main(int argc, char** argv)
  156. {
  157. Vector<String> arguments;
  158. #ifdef WIN32
  159. arguments = ParseArguments(GetCommandLineW());
  160. #else
  161. arguments = ParseArguments(argc, argv);
  162. #endif
  163. Run(arguments);
  164. return 0;
  165. }
  166. void Run(const Vector<String>& arguments)
  167. {
  168. if (arguments.Size() < 1)
  169. {
  170. ErrorExit(
  171. "ShaderCompiler <definitionfile> <outputpath> [options]\n\n"
  172. "Options:\n"
  173. "-tVS|PS Compile only vertex or pixel shaders, by default compile both\n"
  174. "-vX Compile only the shader variation X\n"
  175. "-dX Add a define. Add SM3 to compile for Shader Model 3\n\n"
  176. "Shader binaries will be output into the same directory as the definition file.\n"
  177. "Specify a wildcard to compile multiple shaders from the directory.\n"
  178. );
  179. }
  180. String path, file, extension;
  181. SplitPath(arguments[0], path, file, extension);
  182. inDir_ = AddTrailingSlash(path);
  183. outDir_ = AddTrailingSlash(arguments[1]);
  184. for (unsigned i = 1; i < arguments.Size(); ++i)
  185. {
  186. if (arguments[i][0] == '-' && arguments[i].Length() > 1)
  187. {
  188. char option = arguments[i][1];
  189. String arg = arguments[i].Substring(2);
  190. switch (option)
  191. {
  192. case 'd':
  193. {
  194. Vector<String> nameAndValue = arg.Split('=');
  195. if (nameAndValue.Size() == 2)
  196. {
  197. defines_.Push(nameAndValue[0]);
  198. defineValues_.Push(nameAndValue[1]);
  199. if (nameAndValue[0] == "SM3" && ToInt(nameAndValue[1]) > 0)
  200. useSM3_ = true;
  201. }
  202. else
  203. {
  204. defines_.Push(arg);
  205. defineValues_.Push("1");
  206. if (arg == "SM3")
  207. useSM3_ = true;
  208. }
  209. }
  210. break;
  211. case 't':
  212. if (arg.ToLower() == "vs")
  213. {
  214. compileVS_ = true;
  215. compilePS_ = false;
  216. }
  217. else if (arg.ToLower() == "ps")
  218. {
  219. compileVS_ = false;
  220. compilePS_ = true;
  221. }
  222. break;
  223. case 'v':
  224. compileVariation_ = true;
  225. variationName_ = arg;
  226. break;
  227. }
  228. }
  229. }
  230. if (!file.StartsWith("*"))
  231. CompileShader(arguments[0]);
  232. else
  233. {
  234. Vector<String> shaderFiles;
  235. fileSystem_->ScanDir(shaderFiles, inDir_, file + extension, SCAN_FILES, false);
  236. for (unsigned i = 0; i < shaderFiles.Size(); ++i)
  237. CompileShader(inDir_ + shaderFiles[i]);
  238. }
  239. }
  240. void CompileShader(const String& fileName)
  241. {
  242. String file = GetFileName(fileName);
  243. XMLFile doc(context_);
  244. File source(context_);
  245. source.Open(fileName);
  246. if (!doc.Load(source))
  247. ErrorExit("Could not open input file " + fileName);
  248. XMLElement shaders = doc.GetRoot("shaders");
  249. if (!shaders)
  250. ErrorExit("No shaders element in " + source.GetName());
  251. if (compileVS_)
  252. {
  253. ShaderParser vsParser;
  254. if (!vsParser.Parse(VS, shaders, defines_, defineValues_))
  255. ErrorExit("VS: " + vsParser.GetErrorMessage());
  256. const HashMap<String, unsigned>& combinations = vsParser.GetAllCombinations();
  257. for (HashMap<String, unsigned>::ConstIterator i = combinations.Begin(); i != combinations.End(); ++i)
  258. {
  259. if (!compileVariation_ || i->first_ == variationName_)
  260. {
  261. ShaderCombination src;
  262. CompiledVariation compile;
  263. vsParser.GetCombination(src, i->first_);
  264. compile.type_ = VS;
  265. compile.name_ = file;
  266. compile.outFileName_ = outDir_ + file;
  267. if (!src.name_.Empty())
  268. {
  269. compile.name_ += "_" + src.name_;
  270. compile.outFileName_ += "_" + src.name_;
  271. }
  272. compile.outFileName_ += useSM3_ ? ".vs3" : ".vs2";
  273. compile.defines_ = src.defines_;
  274. compile.defineValues_ = src.defineValues_;
  275. variations_.Push(compile);
  276. workList_.Push(&variations_.Back());
  277. }
  278. }
  279. }
  280. if (compilePS_)
  281. {
  282. ShaderParser psParser;
  283. if (!psParser.Parse(PS, shaders, defines_, defineValues_))
  284. ErrorExit("PS: " + psParser.GetErrorMessage());
  285. const HashMap<String, unsigned>& combinations = psParser.GetAllCombinations();
  286. for (HashMap<String, unsigned>::ConstIterator i = combinations.Begin(); i != combinations.End(); ++i)
  287. {
  288. if (!compileVariation_ || i->first_ == variationName_)
  289. {
  290. ShaderCombination src;
  291. CompiledVariation compile;
  292. psParser.GetCombination(src, i->first_);
  293. compile.type_ = PS;
  294. compile.name_ = file;
  295. compile.outFileName_ = outDir_ + file;
  296. if (!src.name_.Empty())
  297. {
  298. compile.name_ += "_" + src.name_;
  299. compile.outFileName_ += "_" + src.name_;
  300. }
  301. compile.outFileName_ += useSM3_ ? ".ps3" : ".ps2";
  302. compile.defines_ = src.defines_;
  303. compile.defineValues_ = src.defineValues_;
  304. variations_.Push(compile);
  305. workList_.Push(&variations_.Back());
  306. }
  307. }
  308. }
  309. if (variations_.Empty())
  310. {
  311. PrintLine("No variations to compile");
  312. return;
  313. }
  314. // Load the shader source code
  315. {
  316. String inputFileName = inDir_ + file + ".hlsl";
  317. File hlslFile(context_, inputFileName);
  318. if (!hlslFile.IsOpen())
  319. ErrorExit("Could not open input file " + inputFileName);
  320. hlslCode_.Clear();
  321. while (!hlslFile.IsEof())
  322. hlslCode_ += hlslFile.ReadLine() + "\n";
  323. }
  324. if (!compileVariation_)
  325. {
  326. // Create and start worker threads. Use all logical CPUs except one to not lock up the computer completely
  327. unsigned numWorkerThreads = GetNumLogicalCPUs() - 1;
  328. if (!numWorkerThreads)
  329. numWorkerThreads = 1;
  330. Vector<SharedPtr<WorkerThread> > workerThreads;
  331. workerThreads.Resize(numWorkerThreads);
  332. for (unsigned i = 0; i < workerThreads.Size(); ++i)
  333. {
  334. workerThreads[i] = new WorkerThread();
  335. workerThreads[i]->Start();
  336. }
  337. // This will wait until the thread functions have stopped
  338. for (unsigned i = 0; i < workerThreads.Size(); ++i)
  339. workerThreads[i]->Stop();
  340. }
  341. else
  342. {
  343. WorkerThread dummyThread;
  344. dummyThread.ThreadFunction();
  345. }
  346. // Check that all shaders compiled
  347. for (List<CompiledVariation>::Iterator i = variations_.Begin(); i != variations_.End(); ++i)
  348. {
  349. if (!i->errorMsg_.Empty())
  350. {
  351. if (i->type_ == VS)
  352. ErrorExit("Failed to compile vertex shader " + i->name_ + ": " + i->errorMsg_);
  353. else
  354. ErrorExit("Failed to compile pixel shader " + i->name_ + ": " + i->errorMsg_);
  355. }
  356. }
  357. }
  358. void CompileVariation(CompiledVariation* variation)
  359. {
  360. IncludeHandler includeHandler;
  361. PODVector<D3D_SHADER_MACRO> macros;
  362. // Insert variation-specific and global defines
  363. for (unsigned i = 0; i < variation->defines_.Size(); ++i)
  364. {
  365. D3D_SHADER_MACRO macro;
  366. macro.Name = variation->defines_[i].CString();
  367. macro.Definition = variation->defineValues_[i].CString();
  368. macros.Push(macro);
  369. }
  370. for (unsigned i = 0; i < defines_.Size(); ++i)
  371. {
  372. D3D_SHADER_MACRO macro;
  373. macro.Name = defines_[i].CString();
  374. macro.Definition = defineValues_[i].CString();
  375. macros.Push(macro);
  376. }
  377. D3D_SHADER_MACRO endMacro;
  378. endMacro.Name = 0;
  379. endMacro.Definition = 0;
  380. macros.Push(endMacro);
  381. LPD3DBLOB shaderCode = NULL;
  382. LPD3DBLOB errorMsgs = NULL;
  383. // Set the profile, entrypoint and flags according to the shader being compiled
  384. String profile;
  385. String entryPoint;
  386. unsigned flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
  387. if (variation->type_ == VS)
  388. {
  389. entryPoint = "VS";
  390. if (!useSM3_)
  391. profile = "vs_2_0";
  392. else
  393. profile = "vs_3_0";
  394. }
  395. else
  396. {
  397. entryPoint = "PS";
  398. if (!useSM3_)
  399. profile = "ps_2_0";
  400. else
  401. {
  402. profile = "ps_3_0";
  403. flags |= D3DCOMPILE_PREFER_FLOW_CONTROL;
  404. }
  405. }
  406. // Compile using D3DCompiler
  407. HRESULT hr = D3DCompile(hlslCode_.CString(), hlslCode_.Length(), NULL, &macros.Front(), &includeHandler,
  408. entryPoint.CString(), profile.CString(), flags, 0, &shaderCode, &errorMsgs);
  409. if (FAILED(hr))
  410. {
  411. variation->errorMsg_ = String((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
  412. compileFailed_ = true;
  413. }
  414. else
  415. {
  416. BYTE const *const bufData = static_cast<BYTE *>(shaderCode->GetBufferPointer());
  417. SIZE_T const bufSize = shaderCode->GetBufferSize();
  418. MOJOSHADER_parseData const *parseData = MOJOSHADER_parse("bytecode", bufData, bufSize, NULL, 0, NULL, 0, NULL, NULL, NULL);
  419. CopyStrippedCode(variation->byteCode_, shaderCode->GetBufferPointer(), shaderCode->GetBufferSize());
  420. if (!variation->name_.Empty())
  421. PrintLine("Compiled shader variation " + variation->name_ + ", code size " + String(variation->byteCode_.Size()));
  422. else
  423. PrintLine("Compiled base shader variation, code size " + String(variation->byteCode_.Size()));
  424. // Print warnings if any
  425. if (errorMsgs && errorMsgs->GetBufferSize())
  426. {
  427. String warning((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
  428. PrintLine("WARNING: " + warning);
  429. }
  430. for(int i = 0; i < parseData->symbol_count; i++)
  431. {
  432. MOJOSHADER_symbol const& symbol = parseData->symbols[i];
  433. String name(symbol.name);
  434. unsigned const reg = symbol.register_index;
  435. unsigned const regCount = symbol.register_count;
  436. // Check if the parameter is a constant or a texture sampler
  437. bool const isSampler = (name[0] == 's');
  438. name = name.Substring(1);
  439. if (isSampler)
  440. {
  441. // Skip if it's a G-buffer sampler
  442. if (name.Find("Buffer") == String::NPOS)
  443. {
  444. Parameter newTextureUnit(name, reg, 1);
  445. variation->textureUnits_.Push(newTextureUnit);
  446. }
  447. }
  448. else
  449. {
  450. Parameter newParam(name, reg, regCount);
  451. variation->constants_.Push(newParam);
  452. }
  453. }
  454. MOJOSHADER_freeParseData(parseData);
  455. File outFile(context_);
  456. if (!outFile.Open(variation->outFileName_, FILE_WRITE))
  457. {
  458. variation->errorMsg_ = "Could not open output file " + variation->outFileName_;
  459. compileFailed_ = true;
  460. }
  461. else
  462. {
  463. outFile.WriteFileID("USHD");
  464. outFile.WriteShort((unsigned short)variation->type_);
  465. outFile.WriteShort(useSM3_ ? 3 : 2);
  466. outFile.WriteUInt(variation->constants_.Size());
  467. for (unsigned i = 0; i < variation->constants_.Size(); ++i)
  468. {
  469. outFile.WriteString(variation->constants_[i].name_);
  470. outFile.WriteUByte(variation->constants_[i].register_);
  471. outFile.WriteUByte(variation->constants_[i].regCount_);
  472. }
  473. outFile.WriteUInt(variation->textureUnits_.Size());
  474. for (unsigned i = 0; i < variation->textureUnits_.Size(); ++i)
  475. {
  476. outFile.WriteString(variation->textureUnits_[i].name_);
  477. outFile.WriteUByte(variation->textureUnits_[i].register_);
  478. }
  479. unsigned dataSize = variation->byteCode_.Size();
  480. outFile.WriteUInt(dataSize);
  481. if (dataSize)
  482. outFile.Write(&variation->byteCode_[0], dataSize);
  483. }
  484. }
  485. if (shaderCode)
  486. shaderCode->Release();
  487. if (errorMsgs)
  488. errorMsgs->Release();
  489. }
  490. void CopyStrippedCode(PODVector<unsigned char>& dest, void* src, unsigned srcSize)
  491. {
  492. unsigned const D3DSIO_COMMENT = 0xFFFE;
  493. unsigned* srcWords = (unsigned*)src;
  494. unsigned srcWordSize = srcSize >> 2;
  495. dest.Clear();
  496. for (unsigned i = 0; i < srcWordSize; ++i)
  497. {
  498. unsigned opcode = srcWords[i] & 0xffff;
  499. unsigned paramLength = (srcWords[i] & 0x0f000000) >> 24;
  500. unsigned commentLength = srcWords[i] >> 16;
  501. // For now, skip comment only at fixed position to prevent false positives
  502. if (i == 1 && opcode == D3DSIO_COMMENT)
  503. {
  504. // Skip the comment
  505. i += commentLength;
  506. }
  507. else
  508. {
  509. // Not a comment, copy the data
  510. unsigned char* srcBytes = (unsigned char*)(srcWords + i);
  511. dest.Push(*srcBytes++);
  512. dest.Push(*srcBytes++);
  513. dest.Push(*srcBytes++);
  514. dest.Push(*srcBytes++);
  515. }
  516. }
  517. }