ShaderCompiler.cpp 24 KB

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