ShaderCompiler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Set.h"
  31. #include "ShaderParser.h"
  32. #include "StringUtils.h"
  33. #include "Thread.h"
  34. #include "XMLFile.h"
  35. #include <cstdio>
  36. #include <cstring>
  37. #include <windows.h>
  38. #include <d3d9.h>
  39. #include <d3dx9shader.h>
  40. #include "DebugNew.h"
  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 ID3DXInclude
  134. {
  135. public:
  136. STDMETHOD(Open)(D3DXINCLUDE_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> [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_ = inDir_;
  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 Vector<ShaderCombination>& combinations = vsParser.GetCombinations();
  257. for (unsigned i = 0; i < combinations.Size(); ++i)
  258. {
  259. if (!compileVariation_ || combinations[i].name_ == variationName_)
  260. {
  261. CompiledVariation compile;
  262. compile.type_ = VS;
  263. compile.name_ = file;
  264. compile.outFileName_ = inDir_ + file;
  265. if (!combinations[i].name_.Empty())
  266. {
  267. compile.name_ += "_" + combinations[i].name_;
  268. compile.outFileName_ += "_" + combinations[i].name_;
  269. }
  270. compile.outFileName_ += useSM3_ ? ".vs3" : ".vs2";
  271. compile.defines_ = combinations[i].defines_;
  272. compile.defineValues_ = combinations[i].defineValues_;
  273. variations_.Push(compile);
  274. workList_.Push(&variations_.Back());
  275. }
  276. }
  277. }
  278. if (compilePS_)
  279. {
  280. ShaderParser psParser;
  281. if (!psParser.Parse(PS, shaders, defines_, defineValues_))
  282. ErrorExit("PS: " + psParser.GetErrorMessage());
  283. const Vector<ShaderCombination>& combinations = psParser.GetCombinations();
  284. for (unsigned i = 0; i < combinations.Size(); ++i)
  285. {
  286. if (!compileVariation_ || combinations[i].name_ == variationName_)
  287. {
  288. CompiledVariation compile;
  289. compile.type_ = PS;
  290. compile.name_ = file;
  291. compile.outFileName_ = inDir_ + file;
  292. if (!combinations[i].name_.Empty())
  293. {
  294. compile.name_ += "_" + combinations[i].name_;
  295. compile.outFileName_ += "_" + combinations[i].name_;
  296. }
  297. compile.outFileName_ += useSM3_ ? ".ps3" : ".ps2";
  298. compile.defines_ = combinations[i].defines_;
  299. compile.defineValues_ = combinations[i].defineValues_;
  300. variations_.Push(compile);
  301. workList_.Push(&variations_.Back());
  302. }
  303. }
  304. }
  305. if (variations_.Empty())
  306. {
  307. PrintLine("No variations to compile");
  308. return;
  309. }
  310. // Load the shader source code
  311. {
  312. String inputFileName = inDir_ + file + ".hlsl";
  313. File hlslFile(context_, inputFileName);
  314. if (!hlslFile.IsOpen())
  315. ErrorExit("Could not open input file " + inputFileName);
  316. hlslCode_.Clear();
  317. while (!hlslFile.IsEof())
  318. hlslCode_ += hlslFile.ReadLine() + "\n";
  319. }
  320. if (!compileVariation_)
  321. {
  322. // Create and start worker threads. Use all logical CPUs except one to not lock up the computer completely
  323. unsigned numWorkerThreads = GetNumLogicalCPUs() - 1;
  324. if (!numWorkerThreads)
  325. numWorkerThreads = 1;
  326. Vector<SharedPtr<WorkerThread> > workerThreads;
  327. workerThreads.Resize(numWorkerThreads);
  328. for (unsigned i = 0; i < workerThreads.Size(); ++i)
  329. {
  330. workerThreads[i] = new WorkerThread();
  331. workerThreads[i]->Start();
  332. }
  333. // This will wait until the thread functions have stopped
  334. for (unsigned i = 0; i < workerThreads.Size(); ++i)
  335. workerThreads[i]->Stop();
  336. }
  337. else
  338. {
  339. WorkerThread dummyThread;
  340. dummyThread.ThreadFunction();
  341. }
  342. // Check that all shaders compiled
  343. for (List<CompiledVariation>::Iterator i = variations_.Begin(); i != variations_.End(); ++i)
  344. {
  345. if (!i->errorMsg_.Empty())
  346. ErrorExit("Failed to compile shader " + i->name_ + ": " + i->errorMsg_);
  347. }
  348. }
  349. void CompileVariation(CompiledVariation* variation)
  350. {
  351. IncludeHandler includeHandler;
  352. PODVector<D3DXMACRO> macros;
  353. // Insert variation-specific and global defines
  354. for (unsigned i = 0; i < variation->defines_.Size(); ++i)
  355. {
  356. D3DXMACRO macro;
  357. macro.Name = variation->defines_[i].CString();
  358. macro.Definition = variation->defineValues_[i].CString();
  359. macros.Push(macro);
  360. }
  361. for (unsigned i = 0; i < defines_.Size(); ++i)
  362. {
  363. D3DXMACRO macro;
  364. macro.Name = defines_[i].CString();
  365. macro.Definition = defineValues_[i].CString();
  366. macros.Push(macro);
  367. }
  368. D3DXMACRO endMacro;
  369. endMacro.Name = 0;
  370. endMacro.Definition = 0;
  371. macros.Push(endMacro);
  372. LPD3DXBUFFER shaderCode = 0;
  373. LPD3DXBUFFER errorMsgs = 0;
  374. LPD3DXCONSTANTTABLE constantTable = 0;
  375. // Set the profile, entrypoint and flags according to the shader being compiled
  376. String profile;
  377. String entryPoint;
  378. unsigned flags = D3DXSHADER_OPTIMIZATION_LEVEL3;
  379. if (variation->type_ == VS)
  380. {
  381. entryPoint = "VS";
  382. if (!useSM3_)
  383. profile = "vs_2_0";
  384. else
  385. profile = "vs_3_0";
  386. }
  387. else
  388. {
  389. entryPoint = "PS";
  390. if (!useSM3_)
  391. profile = "ps_2_0";
  392. else
  393. {
  394. profile = "ps_3_0";
  395. flags |= D3DXSHADER_PREFER_FLOW_CONTROL;
  396. }
  397. }
  398. // Compile using D3DX
  399. HRESULT hr = D3DXCompileShader(hlslCode_.CString(), hlslCode_.Length(), &macros.Front(), &includeHandler,
  400. entryPoint.CString(), profile.CString(), flags, &shaderCode, &errorMsgs, &constantTable);
  401. if (FAILED(hr))
  402. {
  403. variation->errorMsg_ = String((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
  404. compileFailed_ = true;
  405. }
  406. else
  407. {
  408. CopyStrippedCode(variation->byteCode_, shaderCode->GetBufferPointer(), shaderCode->GetBufferSize());
  409. if (!variation->name_.Empty())
  410. PrintLine("Compiled shader variation " + variation->name_ + ", code size " + String(variation->byteCode_.Size()));
  411. else
  412. PrintLine("Compiled base shader variation, code size " + String(variation->byteCode_.Size()));
  413. // Print warnings if any
  414. if (errorMsgs && errorMsgs->GetBufferSize())
  415. {
  416. String warning((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
  417. PrintLine("WARNING: " + warning);
  418. }
  419. // Parse the constant table for constants and texture units
  420. D3DXCONSTANTTABLE_DESC desc;
  421. constantTable->GetDesc(&desc);
  422. for (unsigned i = 0; i < desc.Constants; ++i)
  423. {
  424. D3DXHANDLE handle = constantTable->GetConstant(NULL, i);
  425. D3DXCONSTANT_DESC constantDesc;
  426. unsigned numElements = 1;
  427. constantTable->GetConstantDesc(handle, &constantDesc, &numElements);
  428. String name(constantDesc.Name);
  429. unsigned reg = constantDesc.RegisterIndex;
  430. unsigned regCount = constantDesc.RegisterCount;
  431. // Check if the parameter is a constant or a texture sampler
  432. bool isSampler = (name[0] == 's');
  433. name = name.Substring(1);
  434. if (isSampler)
  435. {
  436. // Skip if it's a G-buffer sampler
  437. if (name.Find("Buffer") == String::NPOS)
  438. {
  439. Parameter newTextureUnit(name, reg, 1);
  440. variation->textureUnits_.Push(newTextureUnit);
  441. }
  442. }
  443. else
  444. {
  445. Parameter newParam(name, reg, regCount);
  446. variation->constants_.Push(newParam);
  447. }
  448. }
  449. File outFile(context_);
  450. if (!outFile.Open(variation->outFileName_, FILE_WRITE))
  451. {
  452. variation->errorMsg_ = "Could not open output file " + variation->outFileName_;
  453. compileFailed_ = true;
  454. }
  455. else
  456. {
  457. outFile.WriteFileID("USHD");
  458. outFile.WriteShort((unsigned short)variation->type_);
  459. outFile.WriteShort(useSM3_ ? 3 : 2);
  460. outFile.WriteUInt(variation->constants_.Size());
  461. for (unsigned i = 0; i < variation->constants_.Size(); ++i)
  462. {
  463. outFile.WriteString(variation->constants_[i].name_);
  464. outFile.WriteUByte(variation->constants_[i].register_);
  465. outFile.WriteUByte(variation->constants_[i].regCount_);
  466. }
  467. outFile.WriteUInt(variation->textureUnits_.Size());
  468. for (unsigned i = 0; i < variation->textureUnits_.Size(); ++i)
  469. {
  470. outFile.WriteString(variation->textureUnits_[i].name_);
  471. outFile.WriteUByte(variation->textureUnits_[i].register_);
  472. }
  473. unsigned dataSize = variation->byteCode_.Size();
  474. outFile.WriteUInt(dataSize);
  475. if (dataSize)
  476. outFile.Write(&variation->byteCode_[0], dataSize);
  477. }
  478. }
  479. if (shaderCode)
  480. shaderCode->Release();
  481. if (constantTable)
  482. constantTable->Release();
  483. if (errorMsgs)
  484. errorMsgs->Release();
  485. }
  486. void CopyStrippedCode(PODVector<unsigned char>& dest, void* src, unsigned srcSize)
  487. {
  488. unsigned* srcWords = (unsigned*)src;
  489. unsigned srcWordSize = srcSize >> 2;
  490. dest.Clear();
  491. for (unsigned i = 0; i < srcWordSize; ++i)
  492. {
  493. unsigned opcode = srcWords[i] & 0xffff;
  494. unsigned paramLength = (srcWords[i] & 0x0f000000) >> 24;
  495. unsigned commentLength = srcWords[i] >> 16;
  496. // For now, skip comment only at fixed position to prevent false positives
  497. if (i == 1 && opcode == D3DSIO_COMMENT)
  498. {
  499. // Skip the comment
  500. i += commentLength;
  501. }
  502. else
  503. {
  504. // Not a comment, copy the data
  505. unsigned char* srcBytes = (unsigned char*)(srcWords + i);
  506. dest.Push(*srcBytes++);
  507. dest.Push(*srcBytes++);
  508. dest.Push(*srcBytes++);
  509. dest.Push(*srcBytes++);
  510. }
  511. }
  512. }