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. #include "OgreRenderSystem.h"
  30. #include "CmRenderSystemManager.h"
  31. namespace Ogre {
  32. //-----------------------------------------------------------------------
  33. D3D9HLSLProgram::CmdEntryPoint D3D9HLSLProgram::msCmdEntryPoint;
  34. D3D9HLSLProgram::CmdTarget D3D9HLSLProgram::msCmdTarget;
  35. D3D9HLSLProgram::CmdPreprocessorDefines D3D9HLSLProgram::msCmdPreprocessorDefines;
  36. D3D9HLSLProgram::CmdColumnMajorMatrices D3D9HLSLProgram::msCmdColumnMajorMatrices;
  37. D3D9HLSLProgram::CmdOptimisation D3D9HLSLProgram::msCmdOptimisation;
  38. D3D9HLSLProgram::CmdMicrocode D3D9HLSLProgram::msCmdMicrocode;
  39. D3D9HLSLProgram::CmdAssemblerCode D3D9HLSLProgram::msCmdAssemblerCode;
  40. class _OgreD3D9Export HLSLIncludeHandler : public ID3DXInclude
  41. {
  42. public:
  43. HLSLIncludeHandler(HighLevelGpuProgram* sourceProgram)
  44. : mProgram(sourceProgram) {}
  45. ~HLSLIncludeHandler() {}
  46. STDMETHOD(Open)(D3DXINCLUDE_TYPE IncludeType,
  47. LPCSTR pFileName,
  48. LPCVOID pParentData,
  49. LPCVOID *ppData,
  50. UINT *pByteLen
  51. )
  52. {
  53. // TODO PORT - I'm not sure what to do with this. It will probably break something in its current state.
  54. // find & load source code
  55. //DataStreamPtr stream =
  56. // ResourceGroupManager::getSingleton().openResource(
  57. // String(pFileName), mProgram->getGroup(), true, mProgram);
  58. //String source = stream->getAsString();
  59. //// copy into separate c-string
  60. //// Note - must NOT copy the null terminator, otherwise this will terminate
  61. //// the entire program string!
  62. //*pByteLen = static_cast<UINT>(source.length());
  63. //char* pChar = new char[*pByteLen];
  64. //memcpy(pChar, source.c_str(), *pByteLen);
  65. //*ppData = pChar;
  66. return S_OK;
  67. }
  68. STDMETHOD(Close)(LPCVOID pData)
  69. {
  70. char* pChar = (char*)pData;
  71. delete [] pChar;
  72. return S_OK;
  73. }
  74. protected:
  75. HighLevelGpuProgram* mProgram;
  76. };
  77. //-----------------------------------------------------------------------
  78. //-----------------------------------------------------------------------
  79. void D3D9HLSLProgram::loadFromSource(void)
  80. {
  81. // Populate preprocessor defines
  82. String stringBuffer;
  83. vector<D3DXMACRO>::type defines;
  84. const D3DXMACRO* pDefines = 0;
  85. if (!mPreprocessorDefines.empty())
  86. {
  87. stringBuffer = mPreprocessorDefines;
  88. // Split preprocessor defines and build up macro array
  89. D3DXMACRO macro;
  90. String::size_type pos = 0;
  91. while (pos != String::npos)
  92. {
  93. macro.Name = &stringBuffer[pos];
  94. macro.Definition = 0;
  95. String::size_type start_pos=pos;
  96. // Find delims
  97. pos = stringBuffer.find_first_of(";,=", pos);
  98. if(start_pos==pos)
  99. {
  100. if(pos==stringBuffer.length())
  101. {
  102. break;
  103. }
  104. pos++;
  105. continue;
  106. }
  107. if (pos != String::npos)
  108. {
  109. // Check definition part
  110. if (stringBuffer[pos] == '=')
  111. {
  112. // Setup null character for macro name
  113. stringBuffer[pos++] = '\0';
  114. macro.Definition = &stringBuffer[pos];
  115. pos = stringBuffer.find_first_of(";,", pos);
  116. }
  117. else
  118. {
  119. // No definition part, define as "1"
  120. macro.Definition = "1";
  121. }
  122. if (pos != String::npos)
  123. {
  124. // Setup null character for macro name or definition
  125. stringBuffer[pos++] = '\0';
  126. }
  127. }
  128. else
  129. {
  130. macro.Definition = "1";
  131. }
  132. if(strlen(macro.Name)>0)
  133. {
  134. defines.push_back(macro);
  135. }
  136. else
  137. {
  138. break;
  139. }
  140. }
  141. // Add NULL terminator
  142. macro.Name = 0;
  143. macro.Definition = 0;
  144. defines.push_back(macro);
  145. pDefines = &defines[0];
  146. }
  147. // Populate compile flags
  148. DWORD compileFlags = 0;
  149. if (mColumnMajorMatrices)
  150. compileFlags |= D3DXSHADER_PACKMATRIX_COLUMNMAJOR;
  151. else
  152. compileFlags |= D3DXSHADER_PACKMATRIX_ROWMAJOR;
  153. #if OGRE_DEBUG_MODE
  154. compileFlags |= D3DXSHADER_DEBUG;
  155. #endif
  156. switch (mOptimisationLevel)
  157. {
  158. case OPT_DEFAULT:
  159. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  160. break;
  161. case OPT_NONE:
  162. compileFlags |= D3DXSHADER_SKIPOPTIMIZATION;
  163. break;
  164. case OPT_0:
  165. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL0;
  166. break;
  167. case OPT_1:
  168. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  169. break;
  170. case OPT_2:
  171. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL2;
  172. break;
  173. case OPT_3:
  174. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL3;
  175. break;
  176. }
  177. LPD3DXBUFFER errors = 0;
  178. // include handler
  179. HLSLIncludeHandler includeHandler(this);
  180. // Compile & assemble into microcode
  181. HRESULT hr = D3DXCompileShader(
  182. mSource.c_str(),
  183. static_cast<UINT>(mSource.length()),
  184. pDefines,
  185. &includeHandler,
  186. mEntryPoint.c_str(),
  187. mTarget.c_str(),
  188. compileFlags,
  189. &mpMicroCode,
  190. &errors,
  191. &mpConstTable);
  192. if (FAILED(hr))
  193. {
  194. String message = "Cannot assemble D3D9 high-level shader ";
  195. if( errors )
  196. {
  197. message += String(" Errors:\n") + static_cast<const char*>(errors->GetBufferPointer());
  198. errors->Release();
  199. }
  200. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, message,
  201. "D3D9HLSLProgram::loadFromSource");
  202. }
  203. }
  204. //-----------------------------------------------------------------------
  205. void D3D9HLSLProgram::createLowLevelImpl(void)
  206. {
  207. if (!mCompileError)
  208. {
  209. // Create a low-level program, give it the same name as us
  210. mAssemblerProgram =
  211. D3D9GpuProgramManager::createProgramFromString(
  212. "",// dummy source, since we'll be using microcode
  213. mType,
  214. mTarget);
  215. static_cast<D3D9GpuProgram*>(mAssemblerProgram.get())->setExternalMicrocode(mpMicroCode);
  216. }
  217. }
  218. //-----------------------------------------------------------------------
  219. void D3D9HLSLProgram::unloadHighLevelImpl(void)
  220. {
  221. SAFE_RELEASE(mpMicroCode);
  222. SAFE_RELEASE(mpConstTable);
  223. }
  224. //-----------------------------------------------------------------------
  225. void D3D9HLSLProgram::buildConstantDefinitions() const
  226. {
  227. // Derive parameter names from const table
  228. assert(mpConstTable && "Program not loaded!");
  229. // Get contents of the constant table
  230. D3DXCONSTANTTABLE_DESC desc;
  231. HRESULT hr = mpConstTable->GetDesc(&desc);
  232. createParameterMappingStructures(true);
  233. if (FAILED(hr))
  234. {
  235. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  236. "Cannot retrieve constant descriptions from HLSL program.",
  237. "D3D9HLSLProgram::buildParameterNameMap");
  238. }
  239. // Iterate over the constants
  240. for (unsigned int i = 0; i < desc.Constants; ++i)
  241. {
  242. // Recursively descend through the structure levels
  243. processParamElement(NULL, "", i);
  244. }
  245. }
  246. //-----------------------------------------------------------------------
  247. void D3D9HLSLProgram::processParamElement(D3DXHANDLE parent, String prefix,
  248. unsigned int index) const
  249. {
  250. D3DXHANDLE hConstant = mpConstTable->GetConstant(parent, index);
  251. // Since D3D HLSL doesn't deal with naming of array and struct parameters
  252. // automatically, we have to do it by hand
  253. D3DXCONSTANT_DESC desc;
  254. unsigned int numParams = 1;
  255. HRESULT hr = mpConstTable->GetConstantDesc(hConstant, &desc, &numParams);
  256. if (FAILED(hr))
  257. {
  258. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  259. "Cannot retrieve constant description from HLSL program.",
  260. "D3D9HLSLProgram::processParamElement");
  261. }
  262. String paramName = desc.Name;
  263. // trim the odd '$' which appears at the start of the names in HLSL
  264. if (paramName.at(0) == '$')
  265. paramName.erase(paramName.begin());
  266. // Also trim the '[0]' suffix if it exists, we will add our own indexing later
  267. if (StringUtil::endsWith(paramName, "[0]", false))
  268. {
  269. paramName.erase(paramName.size() - 3);
  270. }
  271. if (desc.Class == D3DXPC_STRUCT)
  272. {
  273. // work out a new prefix for nested members, if it's an array, we need an index
  274. prefix = prefix + paramName + ".";
  275. // Cascade into struct
  276. for (unsigned int i = 0; i < desc.StructMembers; ++i)
  277. {
  278. processParamElement(hConstant, prefix, i);
  279. }
  280. }
  281. else
  282. {
  283. // Process params
  284. if (desc.Type == D3DXPT_FLOAT || desc.Type == D3DXPT_INT || desc.Type == D3DXPT_BOOL)
  285. {
  286. size_t paramIndex = desc.RegisterIndex;
  287. String name = prefix + paramName;
  288. GpuConstantDefinition def;
  289. def.logicalIndex = paramIndex;
  290. // populate type, array size & element size
  291. populateDef(desc, def);
  292. if (def.isFloat())
  293. {
  294. def.physicalIndex = mFloatLogicalToPhysical->bufferSize;
  295. OGRE_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  296. mFloatLogicalToPhysical->map.insert(
  297. GpuLogicalIndexUseMap::value_type(paramIndex,
  298. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  299. mFloatLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
  300. mConstantDefs->floatBufferSize = mFloatLogicalToPhysical->bufferSize;
  301. }
  302. else
  303. {
  304. def.physicalIndex = mIntLogicalToPhysical->bufferSize;
  305. OGRE_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  306. mIntLogicalToPhysical->map.insert(
  307. GpuLogicalIndexUseMap::value_type(paramIndex,
  308. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  309. mIntLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
  310. mConstantDefs->intBufferSize = mIntLogicalToPhysical->bufferSize;
  311. }
  312. mConstantDefs->map.insert(GpuConstantDefinitionMap::value_type(name, def));
  313. // Now deal with arrays
  314. mConstantDefs->generateConstantDefinitionArrayEntries(name, def);
  315. }
  316. }
  317. }
  318. //-----------------------------------------------------------------------
  319. void D3D9HLSLProgram::populateDef(D3DXCONSTANT_DESC& d3dDesc, GpuConstantDefinition& def) const
  320. {
  321. def.arraySize = d3dDesc.Elements;
  322. switch(d3dDesc.Type)
  323. {
  324. case D3DXPT_INT:
  325. switch(d3dDesc.Columns)
  326. {
  327. case 1:
  328. def.constType = GCT_INT1;
  329. break;
  330. case 2:
  331. def.constType = GCT_INT2;
  332. break;
  333. case 3:
  334. def.constType = GCT_INT3;
  335. break;
  336. case 4:
  337. def.constType = GCT_INT4;
  338. break;
  339. } // columns
  340. break;
  341. case D3DXPT_FLOAT:
  342. switch(d3dDesc.Class)
  343. {
  344. case D3DXPC_MATRIX_COLUMNS:
  345. case D3DXPC_MATRIX_ROWS:
  346. {
  347. int firstDim, secondDim;
  348. firstDim = d3dDesc.RegisterCount / d3dDesc.Elements;
  349. if (d3dDesc.Class == D3DXPC_MATRIX_ROWS)
  350. {
  351. secondDim = d3dDesc.Columns;
  352. }
  353. else
  354. {
  355. secondDim = d3dDesc.Rows;
  356. }
  357. switch(firstDim)
  358. {
  359. case 2:
  360. switch(secondDim)
  361. {
  362. case 2:
  363. def.constType = GCT_MATRIX_2X2;
  364. def.elementSize = 8; // HLSL always packs
  365. break;
  366. case 3:
  367. def.constType = GCT_MATRIX_2X3;
  368. def.elementSize = 8; // HLSL always packs
  369. break;
  370. case 4:
  371. def.constType = GCT_MATRIX_2X4;
  372. def.elementSize = 8;
  373. break;
  374. } // columns
  375. break;
  376. case 3:
  377. switch(secondDim)
  378. {
  379. case 2:
  380. def.constType = GCT_MATRIX_3X2;
  381. def.elementSize = 12; // HLSL always packs
  382. break;
  383. case 3:
  384. def.constType = GCT_MATRIX_3X3;
  385. def.elementSize = 12; // HLSL always packs
  386. break;
  387. case 4:
  388. def.constType = GCT_MATRIX_3X4;
  389. def.elementSize = 12;
  390. break;
  391. } // columns
  392. break;
  393. case 4:
  394. switch(secondDim)
  395. {
  396. case 2:
  397. def.constType = GCT_MATRIX_4X2;
  398. def.elementSize = 16; // HLSL always packs
  399. break;
  400. case 3:
  401. def.constType = GCT_MATRIX_4X3;
  402. def.elementSize = 16; // HLSL always packs
  403. break;
  404. case 4:
  405. def.constType = GCT_MATRIX_4X4;
  406. def.elementSize = 16;
  407. break;
  408. } // secondDim
  409. break;
  410. } // firstDim
  411. }
  412. break;
  413. case D3DXPC_SCALAR:
  414. case D3DXPC_VECTOR:
  415. switch(d3dDesc.Columns)
  416. {
  417. case 1:
  418. def.constType = GCT_FLOAT1;
  419. break;
  420. case 2:
  421. def.constType = GCT_FLOAT2;
  422. break;
  423. case 3:
  424. def.constType = GCT_FLOAT3;
  425. break;
  426. case 4:
  427. def.constType = GCT_FLOAT4;
  428. break;
  429. } // columns
  430. break;
  431. }
  432. default:
  433. // not mapping samplers, don't need to take the space
  434. break;
  435. };
  436. // D3D9 pads to 4 elements
  437. def.elementSize = GpuConstantDefinition::getElementSize(def.constType, true);
  438. }
  439. LPD3DXBUFFER D3D9HLSLProgram::getMicroCode()
  440. {
  441. return mpMicroCode;
  442. }
  443. //-----------------------------------------------------------------------
  444. D3D9HLSLProgram::D3D9HLSLProgram()
  445. : HighLevelGpuProgram()
  446. , mTarget()
  447. , mEntryPoint()
  448. , mPreprocessorDefines()
  449. , mColumnMajorMatrices(true)
  450. , mpMicroCode(NULL), mpConstTable(NULL)
  451. , mOptimisationLevel(OPT_DEFAULT)
  452. {
  453. // TODO PORT - I'm not sure if this is needed but I won't be doing things this way
  454. // if (createParamDictionary("D3D9HLSLProgram"))
  455. // {
  456. // setupBaseParamDictionary();
  457. // ParamDictionary* dict = getParamDictionary();
  458. // dict->addParameter(ParameterDef("entry_point",
  459. // "The entry point for the HLSL program.",
  460. // PT_STRING),&msCmdEntryPoint);
  461. // dict->addParameter(ParameterDef("target",
  462. // "Name of the assembler target to compile down to.",
  463. // PT_STRING),&msCmdTarget);
  464. // dict->addParameter(ParameterDef("preprocessor_defines",
  465. // "Preprocessor defines use to compile the program.",
  466. // PT_STRING),&msCmdPreprocessorDefines);
  467. // dict->addParameter(ParameterDef("column_major_matrices",
  468. // "Whether matrix packing in column-major order.",
  469. // PT_BOOL),&msCmdColumnMajorMatrices);
  470. //dict->addParameter(ParameterDef("optimisation_level",
  471. // "The optimisation level to use.",
  472. // PT_STRING),&msCmdOptimisation);
  473. //dict->addParameter(ParameterDef("micro_code",
  474. // "the micro code.",
  475. // PT_STRING),&msCmdMicrocode);
  476. //dict->addParameter(ParameterDef("assemble_code",
  477. // "the assemble code.",
  478. // PT_STRING),&msCmdAssemblerCode);
  479. // }
  480. }
  481. //-----------------------------------------------------------------------
  482. D3D9HLSLProgram::~D3D9HLSLProgram()
  483. {
  484. unloadHighLevel();
  485. }
  486. //-----------------------------------------------------------------------
  487. bool D3D9HLSLProgram::isSupported(void) const
  488. {
  489. if (mCompileError || !isRequiredCapabilitiesSupported())
  490. return false;
  491. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  492. return rs->getCapabilities()->isShaderProfileSupported(mTarget);
  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. }