ShaderCompiler.cpp 24 KB

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