OgreD3D9HLSLProgram.cpp 24 KB

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