OgreD3D9HLSLProgram.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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. */
  24. #include "OgreD3D9HLSLProgram.h"
  25. #include "OgreD3D9GpuProgramManager.h"
  26. #include "OgreStringConverter.h"
  27. #include "OgreD3D9GpuProgram.h"
  28. #include "OgreException.h"
  29. namespace Ogre {
  30. //-----------------------------------------------------------------------
  31. D3D9HLSLProgram::CmdEntryPoint D3D9HLSLProgram::msCmdEntryPoint;
  32. D3D9HLSLProgram::CmdTarget D3D9HLSLProgram::msCmdTarget;
  33. D3D9HLSLProgram::CmdPreprocessorDefines D3D9HLSLProgram::msCmdPreprocessorDefines;
  34. D3D9HLSLProgram::CmdColumnMajorMatrices D3D9HLSLProgram::msCmdColumnMajorMatrices;
  35. D3D9HLSLProgram::CmdOptimisation D3D9HLSLProgram::msCmdOptimisation;
  36. D3D9HLSLProgram::CmdMicrocode D3D9HLSLProgram::msCmdMicrocode;
  37. D3D9HLSLProgram::CmdAssemblerCode D3D9HLSLProgram::msCmdAssemblerCode;
  38. class _OgreD3D9Export HLSLIncludeHandler : public ID3DXInclude
  39. {
  40. public:
  41. HLSLIncludeHandler(HighLevelGpuProgram* sourceProgram)
  42. : mProgram(sourceProgram) {}
  43. ~HLSLIncludeHandler() {}
  44. STDMETHOD(Open)(D3DXINCLUDE_TYPE IncludeType,
  45. LPCSTR pFileName,
  46. LPCVOID pParentData,
  47. LPCVOID *ppData,
  48. UINT *pByteLen
  49. )
  50. {
  51. // TODO PORT - I'm not sure what to do with this. It will probably break something in its current state.
  52. // find & load source code
  53. //DataStreamPtr stream =
  54. // ResourceGroupManager::getSingleton().openResource(
  55. // String(pFileName), mProgram->getGroup(), true, mProgram);
  56. //String source = stream->getAsString();
  57. //// copy into separate c-string
  58. //// Note - must NOT copy the null terminator, otherwise this will terminate
  59. //// the entire program string!
  60. //*pByteLen = static_cast<UINT>(source.length());
  61. //char* pChar = new char[*pByteLen];
  62. //memcpy(pChar, source.c_str(), *pByteLen);
  63. //*ppData = pChar;
  64. return S_OK;
  65. }
  66. STDMETHOD(Close)(LPCVOID pData)
  67. {
  68. char* pChar = (char*)pData;
  69. delete [] pChar;
  70. return S_OK;
  71. }
  72. protected:
  73. HighLevelGpuProgram* mProgram;
  74. };
  75. //-----------------------------------------------------------------------
  76. //-----------------------------------------------------------------------
  77. void D3D9HLSLProgram::loadFromSource(void)
  78. {
  79. // Populate preprocessor defines
  80. String stringBuffer;
  81. vector<D3DXMACRO>::type defines;
  82. const D3DXMACRO* pDefines = 0;
  83. if (!mPreprocessorDefines.empty())
  84. {
  85. stringBuffer = mPreprocessorDefines;
  86. // Split preprocessor defines and build up macro array
  87. D3DXMACRO macro;
  88. String::size_type pos = 0;
  89. while (pos != String::npos)
  90. {
  91. macro.Name = &stringBuffer[pos];
  92. macro.Definition = 0;
  93. String::size_type start_pos=pos;
  94. // Find delims
  95. pos = stringBuffer.find_first_of(";,=", pos);
  96. if(start_pos==pos)
  97. {
  98. if(pos==stringBuffer.length())
  99. {
  100. break;
  101. }
  102. pos++;
  103. continue;
  104. }
  105. if (pos != String::npos)
  106. {
  107. // Check definition part
  108. if (stringBuffer[pos] == '=')
  109. {
  110. // Setup null character for macro name
  111. stringBuffer[pos++] = '\0';
  112. macro.Definition = &stringBuffer[pos];
  113. pos = stringBuffer.find_first_of(";,", pos);
  114. }
  115. else
  116. {
  117. // No definition part, define as "1"
  118. macro.Definition = "1";
  119. }
  120. if (pos != String::npos)
  121. {
  122. // Setup null character for macro name or definition
  123. stringBuffer[pos++] = '\0';
  124. }
  125. }
  126. else
  127. {
  128. macro.Definition = "1";
  129. }
  130. if(strlen(macro.Name)>0)
  131. {
  132. defines.push_back(macro);
  133. }
  134. else
  135. {
  136. break;
  137. }
  138. }
  139. // Add NULL terminator
  140. macro.Name = 0;
  141. macro.Definition = 0;
  142. defines.push_back(macro);
  143. pDefines = &defines[0];
  144. }
  145. // Populate compile flags
  146. DWORD compileFlags = 0;
  147. if (mColumnMajorMatrices)
  148. compileFlags |= D3DXSHADER_PACKMATRIX_COLUMNMAJOR;
  149. else
  150. compileFlags |= D3DXSHADER_PACKMATRIX_ROWMAJOR;
  151. #if OGRE_DEBUG_MODE
  152. compileFlags |= D3DXSHADER_DEBUG;
  153. #endif
  154. switch (mOptimisationLevel)
  155. {
  156. case OPT_DEFAULT:
  157. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  158. break;
  159. case OPT_NONE:
  160. compileFlags |= D3DXSHADER_SKIPOPTIMIZATION;
  161. break;
  162. case OPT_0:
  163. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL0;
  164. break;
  165. case OPT_1:
  166. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  167. break;
  168. case OPT_2:
  169. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL2;
  170. break;
  171. case OPT_3:
  172. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL3;
  173. break;
  174. }
  175. LPD3DXBUFFER errors = 0;
  176. // include handler
  177. HLSLIncludeHandler includeHandler(this);
  178. // Compile & assemble into microcode
  179. HRESULT hr = D3DXCompileShader(
  180. mSource.c_str(),
  181. static_cast<UINT>(mSource.length()),
  182. pDefines,
  183. &includeHandler,
  184. mEntryPoint.c_str(),
  185. mTarget.c_str(),
  186. compileFlags,
  187. &mpMicroCode,
  188. &errors,
  189. &mpConstTable);
  190. if (FAILED(hr))
  191. {
  192. String message = "Cannot assemble D3D9 high-level shader ";
  193. if( errors )
  194. {
  195. message += String(" Errors:\n") + static_cast<const char*>(errors->GetBufferPointer());
  196. errors->Release();
  197. }
  198. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, message,
  199. "D3D9HLSLProgram::loadFromSource");
  200. }
  201. }
  202. //-----------------------------------------------------------------------
  203. void D3D9HLSLProgram::createLowLevelImpl(void)
  204. {
  205. if (!mCompileError)
  206. {
  207. // Create a low-level program, give it the same name as us
  208. mAssemblerProgram =
  209. D3D9GpuProgramManager::createProgramFromString(
  210. "",// dummy source, since we'll be using microcode
  211. mType,
  212. mTarget);
  213. static_cast<D3D9GpuProgram*>(mAssemblerProgram.get())->setExternalMicrocode(mpMicroCode);
  214. }
  215. }
  216. //-----------------------------------------------------------------------
  217. void D3D9HLSLProgram::unloadHighLevelImpl(void)
  218. {
  219. SAFE_RELEASE(mpMicroCode);
  220. SAFE_RELEASE(mpConstTable);
  221. }
  222. //-----------------------------------------------------------------------
  223. void D3D9HLSLProgram::buildConstantDefinitions() const
  224. {
  225. // Derive parameter names from const table
  226. assert(mpConstTable && "Program not loaded!");
  227. // Get contents of the constant table
  228. D3DXCONSTANTTABLE_DESC desc;
  229. HRESULT hr = mpConstTable->GetDesc(&desc);
  230. createParameterMappingStructures(true);
  231. if (FAILED(hr))
  232. {
  233. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  234. "Cannot retrieve constant descriptions from HLSL program.",
  235. "D3D9HLSLProgram::buildParameterNameMap");
  236. }
  237. // Iterate over the constants
  238. for (unsigned int i = 0; i < desc.Constants; ++i)
  239. {
  240. // Recursively descend through the structure levels
  241. processParamElement(NULL, "", i);
  242. }
  243. }
  244. //-----------------------------------------------------------------------
  245. void D3D9HLSLProgram::processParamElement(D3DXHANDLE parent, String prefix,
  246. unsigned int index) const
  247. {
  248. D3DXHANDLE hConstant = mpConstTable->GetConstant(parent, index);
  249. // Since D3D HLSL doesn't deal with naming of array and struct parameters
  250. // automatically, we have to do it by hand
  251. D3DXCONSTANT_DESC desc;
  252. unsigned int numParams = 1;
  253. HRESULT hr = mpConstTable->GetConstantDesc(hConstant, &desc, &numParams);
  254. if (FAILED(hr))
  255. {
  256. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  257. "Cannot retrieve constant description from HLSL program.",
  258. "D3D9HLSLProgram::processParamElement");
  259. }
  260. String paramName = desc.Name;
  261. // trim the odd '$' which appears at the start of the names in HLSL
  262. if (paramName.at(0) == '$')
  263. paramName.erase(paramName.begin());
  264. // Also trim the '[0]' suffix if it exists, we will add our own indexing later
  265. if (StringUtil::endsWith(paramName, "[0]", false))
  266. {
  267. paramName.erase(paramName.size() - 3);
  268. }
  269. if (desc.Class == D3DXPC_STRUCT)
  270. {
  271. // work out a new prefix for nested members, if it's an array, we need an index
  272. prefix = prefix + paramName + ".";
  273. // Cascade into struct
  274. for (unsigned int i = 0; i < desc.StructMembers; ++i)
  275. {
  276. processParamElement(hConstant, prefix, i);
  277. }
  278. }
  279. else
  280. {
  281. // Process params
  282. if (desc.Type == D3DXPT_FLOAT || desc.Type == D3DXPT_INT || desc.Type == D3DXPT_BOOL)
  283. {
  284. size_t paramIndex = desc.RegisterIndex;
  285. String name = prefix + paramName;
  286. GpuConstantDefinition def;
  287. def.logicalIndex = paramIndex;
  288. // populate type, array size & element size
  289. populateDef(desc, def);
  290. if (def.isFloat())
  291. {
  292. def.physicalIndex = mFloatLogicalToPhysical->bufferSize;
  293. OGRE_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  294. mFloatLogicalToPhysical->map.insert(
  295. GpuLogicalIndexUseMap::value_type(paramIndex,
  296. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  297. mFloatLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
  298. mConstantDefs->floatBufferSize = mFloatLogicalToPhysical->bufferSize;
  299. }
  300. else
  301. {
  302. def.physicalIndex = mIntLogicalToPhysical->bufferSize;
  303. OGRE_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  304. mIntLogicalToPhysical->map.insert(
  305. GpuLogicalIndexUseMap::value_type(paramIndex,
  306. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  307. mIntLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
  308. mConstantDefs->intBufferSize = mIntLogicalToPhysical->bufferSize;
  309. }
  310. mConstantDefs->map.insert(GpuConstantDefinitionMap::value_type(name, def));
  311. // Now deal with arrays
  312. mConstantDefs->generateConstantDefinitionArrayEntries(name, def);
  313. }
  314. }
  315. }
  316. //-----------------------------------------------------------------------
  317. void D3D9HLSLProgram::populateDef(D3DXCONSTANT_DESC& d3dDesc, GpuConstantDefinition& def) const
  318. {
  319. def.arraySize = d3dDesc.Elements;
  320. switch(d3dDesc.Type)
  321. {
  322. case D3DXPT_INT:
  323. switch(d3dDesc.Columns)
  324. {
  325. case 1:
  326. def.constType = GCT_INT1;
  327. break;
  328. case 2:
  329. def.constType = GCT_INT2;
  330. break;
  331. case 3:
  332. def.constType = GCT_INT3;
  333. break;
  334. case 4:
  335. def.constType = GCT_INT4;
  336. break;
  337. } // columns
  338. break;
  339. case D3DXPT_FLOAT:
  340. switch(d3dDesc.Class)
  341. {
  342. case D3DXPC_MATRIX_COLUMNS:
  343. case D3DXPC_MATRIX_ROWS:
  344. {
  345. int firstDim, secondDim;
  346. firstDim = d3dDesc.RegisterCount / d3dDesc.Elements;
  347. if (d3dDesc.Class == D3DXPC_MATRIX_ROWS)
  348. {
  349. secondDim = d3dDesc.Columns;
  350. }
  351. else
  352. {
  353. secondDim = d3dDesc.Rows;
  354. }
  355. switch(firstDim)
  356. {
  357. case 2:
  358. switch(secondDim)
  359. {
  360. case 2:
  361. def.constType = GCT_MATRIX_2X2;
  362. def.elementSize = 8; // HLSL always packs
  363. break;
  364. case 3:
  365. def.constType = GCT_MATRIX_2X3;
  366. def.elementSize = 8; // HLSL always packs
  367. break;
  368. case 4:
  369. def.constType = GCT_MATRIX_2X4;
  370. def.elementSize = 8;
  371. break;
  372. } // columns
  373. break;
  374. case 3:
  375. switch(secondDim)
  376. {
  377. case 2:
  378. def.constType = GCT_MATRIX_3X2;
  379. def.elementSize = 12; // HLSL always packs
  380. break;
  381. case 3:
  382. def.constType = GCT_MATRIX_3X3;
  383. def.elementSize = 12; // HLSL always packs
  384. break;
  385. case 4:
  386. def.constType = GCT_MATRIX_3X4;
  387. def.elementSize = 12;
  388. break;
  389. } // columns
  390. break;
  391. case 4:
  392. switch(secondDim)
  393. {
  394. case 2:
  395. def.constType = GCT_MATRIX_4X2;
  396. def.elementSize = 16; // HLSL always packs
  397. break;
  398. case 3:
  399. def.constType = GCT_MATRIX_4X3;
  400. def.elementSize = 16; // HLSL always packs
  401. break;
  402. case 4:
  403. def.constType = GCT_MATRIX_4X4;
  404. def.elementSize = 16;
  405. break;
  406. } // secondDim
  407. break;
  408. } // firstDim
  409. }
  410. break;
  411. case D3DXPC_SCALAR:
  412. case D3DXPC_VECTOR:
  413. switch(d3dDesc.Columns)
  414. {
  415. case 1:
  416. def.constType = GCT_FLOAT1;
  417. break;
  418. case 2:
  419. def.constType = GCT_FLOAT2;
  420. break;
  421. case 3:
  422. def.constType = GCT_FLOAT3;
  423. break;
  424. case 4:
  425. def.constType = GCT_FLOAT4;
  426. break;
  427. } // columns
  428. break;
  429. }
  430. default:
  431. // not mapping samplers, don't need to take the space
  432. break;
  433. };
  434. // D3D9 pads to 4 elements
  435. def.elementSize = GpuConstantDefinition::getElementSize(def.constType, true);
  436. }
  437. LPD3DXBUFFER D3D9HLSLProgram::getMicroCode()
  438. {
  439. return mpMicroCode;
  440. }
  441. //-----------------------------------------------------------------------
  442. D3D9HLSLProgram::D3D9HLSLProgram()
  443. : HighLevelGpuProgram()
  444. , mTarget()
  445. , mEntryPoint()
  446. , mPreprocessorDefines()
  447. , mColumnMajorMatrices(true)
  448. , mpMicroCode(NULL), mpConstTable(NULL)
  449. , mOptimisationLevel(OPT_DEFAULT)
  450. {
  451. // TODO PORT - I'm not sure if this is needed but I won't be doing things this way
  452. // if (createParamDictionary("D3D9HLSLProgram"))
  453. // {
  454. // setupBaseParamDictionary();
  455. // ParamDictionary* dict = getParamDictionary();
  456. // dict->addParameter(ParameterDef("entry_point",
  457. // "The entry point for the HLSL program.",
  458. // PT_STRING),&msCmdEntryPoint);
  459. // dict->addParameter(ParameterDef("target",
  460. // "Name of the assembler target to compile down to.",
  461. // PT_STRING),&msCmdTarget);
  462. // dict->addParameter(ParameterDef("preprocessor_defines",
  463. // "Preprocessor defines use to compile the program.",
  464. // PT_STRING),&msCmdPreprocessorDefines);
  465. // dict->addParameter(ParameterDef("column_major_matrices",
  466. // "Whether matrix packing in column-major order.",
  467. // PT_BOOL),&msCmdColumnMajorMatrices);
  468. //dict->addParameter(ParameterDef("optimisation_level",
  469. // "The optimisation level to use.",
  470. // PT_STRING),&msCmdOptimisation);
  471. //dict->addParameter(ParameterDef("micro_code",
  472. // "the micro code.",
  473. // PT_STRING),&msCmdMicrocode);
  474. //dict->addParameter(ParameterDef("assemble_code",
  475. // "the assemble code.",
  476. // PT_STRING),&msCmdAssemblerCode);
  477. // }
  478. }
  479. //-----------------------------------------------------------------------
  480. D3D9HLSLProgram::~D3D9HLSLProgram()
  481. {
  482. unloadHighLevel();
  483. }
  484. //-----------------------------------------------------------------------
  485. bool D3D9HLSLProgram::isSupported(void) const
  486. {
  487. if (mCompileError || !isRequiredCapabilitiesSupported())
  488. return false;
  489. // TODO PORT - Enable this once rendersystem has a singleton, for now just return true
  490. //RenderSystem* rs = Root::getSingleton().getRenderSystem();
  491. //return rs->getCapabilities()->isShaderProfileSupported(mTarget);
  492. return true;
  493. }
  494. //-----------------------------------------------------------------------
  495. GpuProgramParametersSharedPtr D3D9HLSLProgram::createParameters(void)
  496. {
  497. // Call superclass
  498. GpuProgramParametersSharedPtr params = HighLevelGpuProgram::createParameters();
  499. // Need to transpose matrices if compiled with column-major matrices
  500. params->setTransposeMatrices(mColumnMajorMatrices);
  501. return params;
  502. }
  503. //-----------------------------------------------------------------------
  504. void D3D9HLSLProgram::setTarget(const String& target)
  505. {
  506. mTarget = target;
  507. }
  508. //-----------------------------------------------------------------------
  509. const String& D3D9HLSLProgram::getLanguage(void) const
  510. {
  511. static const String language = "hlsl";
  512. return language;
  513. }
  514. //-----------------------------------------------------------------------
  515. //-----------------------------------------------------------------------
  516. String D3D9HLSLProgram::CmdEntryPoint::doGet(const void *target) const
  517. {
  518. return static_cast<const D3D9HLSLProgram*>(target)->getEntryPoint();
  519. }
  520. void D3D9HLSLProgram::CmdEntryPoint::doSet(void *target, const String& val)
  521. {
  522. static_cast<D3D9HLSLProgram*>(target)->setEntryPoint(val);
  523. }
  524. //-----------------------------------------------------------------------
  525. String D3D9HLSLProgram::CmdTarget::doGet(const void *target) const
  526. {
  527. return static_cast<const D3D9HLSLProgram*>(target)->getTarget();
  528. }
  529. void D3D9HLSLProgram::CmdTarget::doSet(void *target, const String& val)
  530. {
  531. static_cast<D3D9HLSLProgram*>(target)->setTarget(val);
  532. }
  533. //-----------------------------------------------------------------------
  534. String D3D9HLSLProgram::CmdPreprocessorDefines::doGet(const void *target) const
  535. {
  536. return static_cast<const D3D9HLSLProgram*>(target)->getPreprocessorDefines();
  537. }
  538. void D3D9HLSLProgram::CmdPreprocessorDefines::doSet(void *target, const String& val)
  539. {
  540. static_cast<D3D9HLSLProgram*>(target)->setPreprocessorDefines(val);
  541. }
  542. //-----------------------------------------------------------------------
  543. String D3D9HLSLProgram::CmdColumnMajorMatrices::doGet(const void *target) const
  544. {
  545. return StringConverter::toString(static_cast<const D3D9HLSLProgram*>(target)->getColumnMajorMatrices());
  546. }
  547. void D3D9HLSLProgram::CmdColumnMajorMatrices::doSet(void *target, const String& val)
  548. {
  549. static_cast<D3D9HLSLProgram*>(target)->setColumnMajorMatrices(StringConverter::parseBool(val));
  550. }
  551. //-----------------------------------------------------------------------
  552. String D3D9HLSLProgram::CmdOptimisation::doGet(const void *target) const
  553. {
  554. switch(static_cast<const D3D9HLSLProgram*>(target)->getOptimisationLevel())
  555. {
  556. default:
  557. case OPT_DEFAULT:
  558. return "default";
  559. case OPT_NONE:
  560. return "none";
  561. case OPT_0:
  562. return "0";
  563. case OPT_1:
  564. return "1";
  565. case OPT_2:
  566. return "2";
  567. case OPT_3:
  568. return "3";
  569. }
  570. }
  571. void D3D9HLSLProgram::CmdOptimisation::doSet(void *target, const String& val)
  572. {
  573. if (StringUtil::startsWith(val, "default", true))
  574. static_cast<D3D9HLSLProgram*>(target)->setOptimisationLevel(OPT_DEFAULT);
  575. else if (StringUtil::startsWith(val, "none", true))
  576. static_cast<D3D9HLSLProgram*>(target)->setOptimisationLevel(OPT_NONE);
  577. else if (StringUtil::startsWith(val, "0", true))
  578. static_cast<D3D9HLSLProgram*>(target)->setOptimisationLevel(OPT_0);
  579. else if (StringUtil::startsWith(val, "1", true))
  580. static_cast<D3D9HLSLProgram*>(target)->setOptimisationLevel(OPT_1);
  581. else if (StringUtil::startsWith(val, "2", true))
  582. static_cast<D3D9HLSLProgram*>(target)->setOptimisationLevel(OPT_2);
  583. else if (StringUtil::startsWith(val, "3", true))
  584. static_cast<D3D9HLSLProgram*>(target)->setOptimisationLevel(OPT_3);
  585. }
  586. //-----------------------------------------------------------------------
  587. String D3D9HLSLProgram::CmdMicrocode::doGet(const void *target) const
  588. {
  589. D3D9HLSLProgram* program=const_cast<D3D9HLSLProgram*>(static_cast<const D3D9HLSLProgram*>(target));
  590. LPD3DXBUFFER buffer=program->getMicroCode();
  591. if(buffer)
  592. {
  593. char* str =static_cast<Ogre::String::value_type*>(buffer->GetBufferPointer());
  594. size_t size=static_cast<size_t>(buffer->GetBufferSize());
  595. Ogre::String code;
  596. code.assign(str,size);
  597. return code;
  598. }
  599. else
  600. {
  601. return String();
  602. }
  603. }
  604. void D3D9HLSLProgram::CmdMicrocode::doSet(void *target, const String& val)
  605. {
  606. //nothing to do
  607. //static_cast<D3D9HLSLProgram*>(target)->setColumnMajorMatrices(StringConverter::parseBool(val));
  608. }
  609. //-----------------------------------------------------------------------
  610. String D3D9HLSLProgram::CmdAssemblerCode::doGet(const void *target) const
  611. {
  612. D3D9HLSLProgram* program=const_cast<D3D9HLSLProgram*>(static_cast<const D3D9HLSLProgram*>(target));
  613. LPD3DXBUFFER buffer=program->getMicroCode();
  614. if(buffer)
  615. {
  616. CONST DWORD* code =static_cast<CONST DWORD*>(buffer->GetBufferPointer());
  617. LPD3DXBUFFER pDisassembly=0;
  618. HRESULT hr=D3DXDisassembleShader(code,FALSE,"// assemble code from D3D9HLSLProgram\n",&pDisassembly);
  619. if(pDisassembly)
  620. {
  621. char* str =static_cast<Ogre::String::value_type*>(pDisassembly->GetBufferPointer());
  622. size_t size=static_cast<size_t>(pDisassembly->GetBufferSize());
  623. Ogre::String assemble_code;
  624. assemble_code.assign(str,size);
  625. pDisassembly->Release();
  626. return assemble_code;
  627. }
  628. return String();
  629. }
  630. else
  631. {
  632. return String();
  633. }
  634. }
  635. void D3D9HLSLProgram::CmdAssemblerCode::doSet(void *target, const String& val)
  636. {
  637. //nothing to do
  638. //static_cast<D3D9HLSLProgram*>(target)->setColumnMajorMatrices(StringConverter::parseBool(val));
  639. }
  640. }