OgreD3D9HLSLProgram.cpp 17 KB

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