OgreD3D9HLSLProgram.cpp 24 KB

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