ShaderCompiler.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "List.h"
  27. #include "Mutex.h"
  28. #include "ProcessUtils.h"
  29. #include "StringUtils.h"
  30. #include "Thread.h"
  31. #include "XMLFile.h"
  32. #include <cstdio>
  33. #include <cstring>
  34. #include <Windows.h>
  35. #include <d3d9.h>
  36. #include <D3DX9Shader.h>
  37. #include "DebugNew.h"
  38. enum ShaderType
  39. {
  40. VS,
  41. PS,
  42. Both
  43. };
  44. struct Parameter
  45. {
  46. Parameter()
  47. {
  48. }
  49. Parameter(const String& name, unsigned index) :
  50. name_(name),
  51. index_(index)
  52. {
  53. }
  54. String name_;
  55. unsigned index_;
  56. };
  57. struct Parameters
  58. {
  59. Vector<Parameter> vsParams_;
  60. Vector<Parameter> psParams_;
  61. Vector<Parameter> textureUnits_;
  62. };
  63. struct Variation
  64. {
  65. Variation()
  66. {
  67. }
  68. Variation(const String& name, bool isOption) :
  69. name_(name),
  70. option_(isOption)
  71. {
  72. }
  73. String name_;
  74. Vector<String> defines_;
  75. Vector<String> excludes_;
  76. Vector<String> includes_;
  77. Vector<String> requires_;
  78. bool option_;
  79. };
  80. struct CompiledVariation
  81. {
  82. ShaderType type_;
  83. String name_;
  84. Vector<String> defines_;
  85. PODVector<unsigned char> byteCode_;
  86. Parameters parameters_;
  87. String errorMsg_;
  88. };
  89. struct Shader
  90. {
  91. Shader(const String& name, ShaderType type) :
  92. name_(name),
  93. type_(type)
  94. {
  95. }
  96. String name_;
  97. ShaderType type_;
  98. Vector<Variation> variations_;
  99. };
  100. SharedPtr<Context> context_(new Context());
  101. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  102. String inDir_;
  103. String inFile_;
  104. String outDir_;
  105. Map<String, unsigned> vsParams_;
  106. Map<String, unsigned> psParams_;
  107. Map<String, unsigned> textureUnits_;
  108. Vector<String> defines_;
  109. bool useSM3_ = false;
  110. volatile bool compileFailed_ = false;
  111. List<CompiledVariation*> workList_;
  112. Mutex workMutex_;
  113. Mutex globalParamMutex_;
  114. String hlslCode_;
  115. int main(int argc, char** argv);
  116. void Run(const Vector<String>& arguments);
  117. void CompileVariations(const Shader& baseShader, XMLElement& shaders);
  118. void Compile(CompiledVariation* variation);
  119. class WorkerThread : public RefCounted, public Thread
  120. {
  121. public:
  122. void ThreadFunction()
  123. {
  124. for (;;)
  125. {
  126. CompiledVariation* workItem = 0;
  127. {
  128. MutexLock lock(workMutex_);
  129. if (!workList_.Empty())
  130. {
  131. workItem = workList_.Front();
  132. workList_.Erase(workList_.Begin());
  133. PrintLine("Compiling shader " + workItem->name_);
  134. }
  135. }
  136. if (!workItem)
  137. return;
  138. // If compile(s) failed, just empty the list, but do not compile more
  139. if (!compileFailed_)
  140. Compile(workItem);
  141. }
  142. }
  143. };
  144. class IncludeHandler : public ID3DXInclude
  145. {
  146. public:
  147. STDMETHOD(Open)(D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
  148. {
  149. String fileName = inDir_ + String((const char*)pFileName);
  150. File file(context_, fileName);
  151. if (!file.IsOpen())
  152. return E_FAIL;
  153. unsigned fileSize = file.GetSize();
  154. void* fileData = new unsigned char[fileSize];
  155. *pBytes = fileSize;
  156. *ppData = fileData;
  157. file.Read(fileData, fileSize);
  158. return S_OK;
  159. }
  160. STDMETHOD(Close)(LPCVOID pData)
  161. {
  162. delete[] (unsigned char*)pData;
  163. return S_OK;
  164. }
  165. };
  166. int main(int argc, char** argv)
  167. {
  168. Vector<String> arguments;
  169. for (int i = 1; i < argc; ++i)
  170. {
  171. String argument(argv[i]);
  172. arguments.Push(GetInternalPath(argument));
  173. }
  174. Run(arguments);
  175. return 0;
  176. }
  177. void Run(const Vector<String>& arguments)
  178. {
  179. if (arguments.Size() < 2)
  180. {
  181. ErrorExit(
  182. "Usage: ShaderCompiler <definitionfile> <outputpath> [SM3] [define1] [define2]\n\n"
  183. "HLSL files will be loaded from definition file directory, and binary code will\n"
  184. "be output to same directory as the output file.\n"
  185. );
  186. }
  187. unsigned pos = arguments[0].FindLast('/');
  188. if (pos != String::NPOS)
  189. {
  190. inDir_ = arguments[0].Substring(0, pos);
  191. inFile_ = arguments[0].Substring(pos + 1);
  192. }
  193. else
  194. {
  195. inFile_ = arguments[0];
  196. }
  197. outDir_ = arguments[1];
  198. inDir_ = AddTrailingSlash(inDir_);
  199. outDir_ = AddTrailingSlash(outDir_);
  200. for (unsigned i = 2; i < arguments.Size(); ++i)
  201. {
  202. String arg = arguments[i].ToUpper();
  203. if (arg == "SM3")
  204. useSM3_ = true;
  205. else if (arg == "SM2")
  206. useSM3_ = false;
  207. defines_.Push(arg);
  208. }
  209. XMLFile doc(context_);
  210. File source(context_);
  211. source.Open(arguments[0]);
  212. if (!doc.Load(source))
  213. ErrorExit("Could not open input file " + arguments[0]);
  214. XMLElement shaders = doc.GetRootElement("shaders");
  215. if (!shaders)
  216. ErrorExit("No shaders element in " + source.GetName());
  217. XMLFile outDoc(context_);
  218. XMLElement outShaders = outDoc.CreateRootElement("shaders");
  219. XMLElement shader = shaders.GetChildElement("shader");
  220. while (shader)
  221. {
  222. bool writeOutput = false;
  223. String source = shader.GetString("name");
  224. ShaderType compileType = Both;
  225. String type = shader.GetString("type");
  226. if ((type == "VS") || (type == "vs"))
  227. compileType = VS;
  228. if ((type == "PS") || (type == "ps"))
  229. compileType = PS;
  230. // If both VS & PS are defined separately, we should only write output once both have been compiled
  231. if (compileType != VS)
  232. writeOutput = true;
  233. Shader baseShader(source, compileType);
  234. XMLElement variation = shader.GetChildElement("");
  235. while (variation)
  236. {
  237. String value = variation.GetName();
  238. if ((value == "variation") || (value == "option"))
  239. {
  240. String name = variation.GetString("name");
  241. Variation newVar(name, value == "option");
  242. String simpleDefine = variation.GetString("define");
  243. if (!simpleDefine.Empty())
  244. newVar.defines_.Push(simpleDefine);
  245. String simpleExclude = variation.GetString("exclude");
  246. if (!simpleExclude.Empty())
  247. newVar.excludes_.Push(simpleExclude);
  248. String simpleInclude = variation.GetString("include");
  249. if (!simpleInclude.Empty())
  250. newVar.includes_.Push(simpleInclude);
  251. String simpleRequire = variation.GetString("require");
  252. if (!simpleRequire.Empty())
  253. newVar.requires_.Push(simpleRequire);
  254. XMLElement define = variation.GetChildElement("define");
  255. while (define)
  256. {
  257. newVar.defines_.Push(define.GetString("name"));
  258. define = define.GetNextElement("define");
  259. }
  260. XMLElement exclude = variation.GetChildElement("exclude");
  261. while (exclude)
  262. {
  263. newVar.excludes_.Push(exclude.GetString("name"));
  264. exclude = exclude.GetNextElement("exclude");
  265. }
  266. XMLElement include = variation.GetChildElement("include");
  267. while (include)
  268. {
  269. newVar.includes_.Push(include.GetString("name"));
  270. include = include.GetNextElement("include");
  271. }
  272. XMLElement require = variation.GetChildElement("require");
  273. while (require)
  274. {
  275. newVar.requires_.Push(require.GetString("name"));
  276. require = require.GetNextElement("require");
  277. }
  278. baseShader.variations_.Push(newVar);
  279. }
  280. variation = variation.GetNextElement();
  281. }
  282. if (baseShader.type_ != Both)
  283. CompileVariations(baseShader, outShaders);
  284. else
  285. {
  286. baseShader.type_ = VS;
  287. CompileVariations(baseShader, outShaders);
  288. baseShader.type_ = PS;
  289. CompileVariations(baseShader, outShaders);
  290. }
  291. if (writeOutput)
  292. {
  293. String outFileName = outDir_ + inDir_ + source + ".xml";
  294. // Add global parameter & texture sampler definitions
  295. {
  296. XMLElement parameters = outShaders.CreateChildElement("vsparameters");
  297. Map<unsigned, Vector<String> > sorted;
  298. for (Map<String, unsigned>::ConstIterator i = vsParams_.Begin(); i != vsParams_.End(); ++i)
  299. sorted[i->second_].Push(i->first_);
  300. for (Map<unsigned, Vector<String> >::ConstIterator i = sorted.Begin(); i != sorted.End(); ++i)
  301. {
  302. for (unsigned j = 0; j < i->second_.Size(); ++j)
  303. {
  304. XMLElement param = parameters.CreateChildElement("parameter");
  305. param.SetString("name", i->second_[j]);
  306. param.SetInt("index", i->first_);
  307. }
  308. }
  309. }
  310. {
  311. XMLElement parameters = outShaders.CreateChildElement("psparameters");
  312. Map<unsigned, Vector<String> > sorted;
  313. for (Map<String, unsigned>::ConstIterator i = psParams_.Begin(); i != psParams_.End(); ++i)
  314. sorted[i->second_].Push(i->first_);
  315. for (Map<unsigned, Vector<String> >::ConstIterator i = sorted.Begin(); i != sorted.End(); ++i)
  316. {
  317. for (unsigned j = 0; j < i->second_.Size(); ++j)
  318. {
  319. XMLElement param = parameters.CreateChildElement("parameter");
  320. param.SetString("name", i->second_[j]);
  321. param.SetInt("index", i->first_);
  322. }
  323. }
  324. }
  325. {
  326. XMLElement parameters = outShaders.CreateChildElement("textureunits");
  327. Map<unsigned, Vector<String> > sorted;
  328. for (Map<String, unsigned>::ConstIterator i = textureUnits_.Begin(); i != textureUnits_.End(); ++i)
  329. sorted[i->second_].Push(i->first_);
  330. for (Map<unsigned, Vector<String> >::ConstIterator i = sorted.Begin(); i != sorted.End(); ++i)
  331. {
  332. for (unsigned j = 0; j < i->second_.Size(); ++j)
  333. {
  334. XMLElement param = parameters.CreateChildElement("parameter");
  335. param.SetString("name", i->second_[j]);
  336. param.SetInt("index", i->first_);
  337. }
  338. }
  339. }
  340. File outFile(context_);
  341. outFile.Open(outFileName, FILE_WRITE);
  342. if (!outDoc.Save(outFile))
  343. ErrorExit("Could not save output file " + outFileName);
  344. }
  345. shader = shader.GetNextElement("shader");
  346. }
  347. }
  348. void CompileVariations(const Shader& baseShader, XMLElement& shaders)
  349. {
  350. unsigned combinations = 1;
  351. PODVector<unsigned> usedCombinations;
  352. bool hasVariations = false;
  353. Map<String, unsigned> nameToIndex;
  354. Vector<CompiledVariation> compiledVariations;
  355. const Vector<Variation>& variations = baseShader.variations_;
  356. if (variations.Size() > 32)
  357. ErrorExit("Maximum amount of variations exceeded");
  358. // Load the shader source code
  359. {
  360. String inputFileName = inDir_ + baseShader.name_ + ".hlsl";
  361. File hlslFile(context_, inputFileName);
  362. if (!hlslFile.IsOpen())
  363. ErrorExit("Could not open input file " + inputFileName);
  364. hlslCode_.Clear();
  365. while (!hlslFile.IsEof())
  366. hlslCode_ += hlslFile.ReadLine() + "\n";
  367. }
  368. for (unsigned i = 0; i < variations.Size(); ++i)
  369. {
  370. combinations *= 2;
  371. nameToIndex[variations[i].name_] = i;
  372. if (!variations[i].option_)
  373. hasVariations = true;
  374. }
  375. for (unsigned i = 0; i < combinations; ++i)
  376. {
  377. unsigned active = i; // Variations/options active on this particular combination
  378. bool variationActive = false;
  379. bool skipThis = false;
  380. // Check for excludes/includes/requires
  381. for (unsigned j = 0; j < variations.Size(); ++j)
  382. {
  383. if ((active >> j) & 1)
  384. {
  385. for (unsigned k = 0; k < variations[j].includes_.Size(); ++k)
  386. {
  387. if (nameToIndex.Find(variations[j].includes_[k]) != nameToIndex.End())
  388. active |= (1 << nameToIndex[variations[j].includes_[k]]);
  389. }
  390. for (unsigned k = 0; k < variations[j].excludes_.Size(); ++k)
  391. {
  392. if (nameToIndex.Find(variations[j].excludes_[k]) != nameToIndex.End())
  393. active &= ~(1 << nameToIndex[variations[j].excludes_[k]]);
  394. }
  395. // If it's a variation, exclude all other variations
  396. if (!variations[j].option_)
  397. {
  398. for (unsigned k = 0; k < variations.Size(); ++k)
  399. {
  400. if ((k != j) && (!variations[k].option_))
  401. active &= ~(1 << k);
  402. }
  403. variationActive = true;
  404. }
  405. for (unsigned k = 0; k < variations[j].requires_.Size(); ++k)
  406. {
  407. bool requireFound = false;
  408. for (unsigned l = 0; l < defines_.Size(); ++l)
  409. {
  410. if (defines_[l] == variations[j].requires_[k])
  411. {
  412. requireFound = true;
  413. break;
  414. }
  415. }
  416. for (unsigned l = 0; l < variations.Size(); ++l)
  417. {
  418. if (((active >> l) & 1) && (l != j))
  419. {
  420. if (variations[l].name_ == variations[j].requires_[k])
  421. {
  422. requireFound = true;
  423. break;
  424. }
  425. for (unsigned m = 0; m < variations[l].defines_.Size(); ++m)
  426. {
  427. if (variations[l].defines_[m] == variations[j].requires_[k])
  428. {
  429. requireFound = true;
  430. break;
  431. }
  432. }
  433. }
  434. if (requireFound)
  435. break;
  436. }
  437. if (!requireFound)
  438. skipThis = true;
  439. }
  440. }
  441. }
  442. // If variations are included, check that one of them is active
  443. if ((hasVariations) && (!variationActive))
  444. continue;
  445. if (skipThis)
  446. continue;
  447. // Check that this combination is unique
  448. bool unique = true;
  449. for (unsigned j = 0; j < usedCombinations.Size(); ++j)
  450. {
  451. if (usedCombinations[j] == active)
  452. {
  453. unique = false;
  454. break;
  455. }
  456. }
  457. if (unique)
  458. {
  459. bool firstSuffix = true;
  460. // Build output shader filename & defines from active variations
  461. String outName = baseShader.name_;
  462. Vector<String> defines;
  463. for (unsigned j = 0; j < variations.Size(); ++j)
  464. {
  465. if (active & (1 << j))
  466. {
  467. if (variations[j].name_.Length())
  468. {
  469. if (firstSuffix)
  470. {
  471. outName = outName + "_" + variations[j].name_;
  472. firstSuffix = false;
  473. }
  474. else
  475. outName = outName + variations[j].name_;
  476. }
  477. for (unsigned k = 0; k < variations[j].defines_.Size(); ++k)
  478. defines.Push(variations[j].defines_[k]);
  479. }
  480. }
  481. CompiledVariation compile;
  482. compile.type_ = baseShader.type_;
  483. compile.name_ = outName;
  484. compile.defines_ = defines;
  485. compiledVariations.Push(compile);
  486. usedCombinations.Push(active);
  487. }
  488. }
  489. // Prepare the work list
  490. // (all variations must be created first, so that vector resize does not change pointers)
  491. for (unsigned i = 0; i < compiledVariations.Size(); ++i)
  492. workList_.Push(&compiledVariations[i]);
  493. // Create and start worker threads
  494. Vector<SharedPtr<WorkerThread> > workerThreads;
  495. workerThreads.Resize(GetNumCPUCores());
  496. for (unsigned i = 0; i < workerThreads.Size(); ++i)
  497. {
  498. workerThreads[i] = new WorkerThread();
  499. workerThreads[i]->Start();
  500. }
  501. // This will wait until the thread functions have stopped
  502. for (unsigned i = 0; i < workerThreads.Size(); ++i)
  503. workerThreads[i]->Stop();
  504. // Build the XML output
  505. for (unsigned i = 0; i < compiledVariations.Size(); ++i)
  506. {
  507. if (!compiledVariations[i].errorMsg_.Empty())
  508. ErrorExit("Failed to compile shader " + compiledVariations[i].name_ + ": " + compiledVariations[i].errorMsg_);
  509. Parameters& params = compiledVariations[i].parameters_;
  510. XMLElement shader = shaders.CreateChildElement("shader");
  511. shader.SetString("name", compiledVariations[i].name_);
  512. switch (baseShader.type_)
  513. {
  514. case VS:
  515. shader.SetString("type", "vs");
  516. for (unsigned j = 0; j < params.vsParams_.Size(); ++j)
  517. {
  518. XMLElement vsParam = shader.CreateChildElement("parameter");
  519. vsParam.SetString("name", params.vsParams_[j].name_);
  520. }
  521. break;
  522. case PS:
  523. shader.SetString("type", "ps");
  524. for (unsigned j = 0; j < params.psParams_.Size(); ++j)
  525. {
  526. XMLElement psParam = shader.CreateChildElement("parameter");
  527. psParam.SetString("name", params.psParams_[j].name_);
  528. }
  529. for (unsigned j = 0; j < params.textureUnits_.Size(); ++j)
  530. {
  531. XMLElement texture = shader.CreateChildElement("textureunit");
  532. texture.SetString("name", params.textureUnits_[j].name_);
  533. }
  534. break;
  535. }
  536. }
  537. }
  538. void Compile(CompiledVariation* variation)
  539. {
  540. IncludeHandler includeHandler;
  541. PODVector<D3DXMACRO> macros;
  542. // Insert variation-specific and global defines
  543. for (unsigned i = 0; i < variation->defines_.Size(); ++i)
  544. {
  545. D3DXMACRO macro;
  546. macro.Name = variation->defines_[i].CString();
  547. macro.Definition = "1";
  548. macros.Push(macro);
  549. }
  550. for (unsigned i = 0; i < defines_.Size(); ++i)
  551. {
  552. D3DXMACRO macro;
  553. macro.Name = defines_[i].CString();
  554. macro.Definition = "1";
  555. macros.Push(macro);
  556. }
  557. D3DXMACRO endMacro;
  558. endMacro.Name = 0;
  559. endMacro.Definition = 0;
  560. macros.Push(endMacro);
  561. LPD3DXBUFFER shaderCode = 0;
  562. LPD3DXBUFFER errorMsgs = 0;
  563. LPD3DXCONSTANTTABLE constantTable = 0;
  564. // Set the profile, entrypoint and flags according to the shader being compiled
  565. String profile;
  566. String extension;
  567. String entryPoint;
  568. unsigned flags = 0;
  569. if (variation->type_ == VS)
  570. {
  571. entryPoint = "VS";
  572. if (!useSM3_)
  573. {
  574. profile = "vs_2_0";
  575. extension = ".vs2";
  576. }
  577. else
  578. {
  579. profile = "vs_3_0";
  580. extension = ".vs3";
  581. }
  582. }
  583. else
  584. {
  585. entryPoint = "PS";
  586. if (!useSM3_)
  587. {
  588. profile = "ps_2_0";
  589. extension = ".ps2";
  590. }
  591. else
  592. {
  593. profile = "ps_3_0";
  594. extension = ".ps3";
  595. flags |= D3DXSHADER_PREFER_FLOW_CONTROL;
  596. }
  597. }
  598. String outFileName = outDir_ + inDir_ + variation->name_ + extension;
  599. // Compile using D3DX
  600. HRESULT hr = D3DXCompileShader(hlslCode_.CString(), hlslCode_.Length(), &macros.Front(), &includeHandler,
  601. entryPoint.CString(), profile.CString(), flags, &shaderCode, &errorMsgs, &constantTable);
  602. if (FAILED(hr))
  603. {
  604. variation->errorMsg_ = String((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
  605. compileFailed_ = true;
  606. }
  607. else
  608. {
  609. unsigned dataSize = shaderCode->GetBufferSize();
  610. if (dataSize)
  611. {
  612. variation->byteCode_.Resize(dataSize);
  613. memcpy(&variation->byteCode_[0], shaderCode->GetBufferPointer(), dataSize);
  614. }
  615. File outFile(context_, outFileName, FILE_WRITE);
  616. if (outFile.IsOpen())
  617. outFile.Write(shaderCode->GetBufferPointer(), dataSize);
  618. else
  619. {
  620. variation->errorMsg_ = "Failed to write output file " + outFileName;
  621. compileFailed_ = true;
  622. }
  623. }
  624. // Parse the constant table for constants and texture units
  625. D3DXCONSTANTTABLE_DESC desc;
  626. constantTable->GetDesc(&desc);
  627. for (unsigned i = 0; i < desc.Constants; ++i)
  628. {
  629. D3DXHANDLE handle = constantTable->GetConstant(NULL, i);
  630. D3DXCONSTANT_DESC constantDesc;
  631. unsigned numElements = 1;
  632. constantTable->GetConstantDesc(handle, &constantDesc, &numElements);
  633. String name(constantDesc.Name);
  634. unsigned index = constantDesc.RegisterIndex;
  635. // Check if the parameter is a constant or a texture sampler
  636. bool isSampler = (name[0] == 's');
  637. name = name.Substring(1);
  638. MutexLock lock(globalParamMutex_);
  639. if (isSampler)
  640. {
  641. // Skip if it's a G-buffer sampler
  642. if (name.Find("Buffer") == String::NPOS)
  643. {
  644. variation->parameters_.textureUnits_.Push(Parameter(name, index));
  645. if (textureUnits_.Find(name) != textureUnits_.End())
  646. {
  647. unsigned oldIndex = textureUnits_[name];
  648. if (oldIndex != index)
  649. ErrorExit("Texture " + name + " bound to several sampler registers");
  650. }
  651. textureUnits_[name] = index;
  652. }
  653. }
  654. else
  655. {
  656. if (variation->type_ == VS)
  657. {
  658. variation->parameters_.vsParams_.Push(Parameter(name, index));
  659. if (vsParams_.Find(name) != vsParams_.End())
  660. {
  661. unsigned oldIndex = vsParams_[name];
  662. if (oldIndex != index)
  663. ErrorExit("Parameter " + name + " bound to several constant registers");
  664. }
  665. vsParams_[name] = index;
  666. }
  667. else
  668. {
  669. variation->parameters_.psParams_.Push(Parameter(name, index));
  670. if (psParams_.Find(name) != psParams_.End())
  671. {
  672. unsigned oldIndex = psParams_[name];
  673. if (oldIndex != index)
  674. ErrorExit("Parameter " + name + " bound to several constant registers");
  675. }
  676. psParams_[name] = index;
  677. }
  678. }
  679. }
  680. if (shaderCode)
  681. shaderCode->Release();
  682. if (constantTable)
  683. constantTable->Release();
  684. if (errorMsgs)
  685. errorMsgs->Release();
  686. }