CmD3D9HLSLProgram.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 "CmD3D9HLSLProgram.h"
  25. #include "CmGpuProgramManager.h"
  26. #include "CmD3D9GpuProgram.h"
  27. #include "CmException.h"
  28. #include "CmRenderSystem.h"
  29. #include "CmAsyncOp.h"
  30. #include "CmGpuParams.h"
  31. #include "CmD3D9HLSLProgramRTTI.h"
  32. namespace CamelotEngine {
  33. class CM_D3D9_EXPORT HLSLIncludeHandler : public ID3DXInclude
  34. {
  35. public:
  36. HLSLIncludeHandler(HighLevelGpuProgram* sourceProgram)
  37. : mProgram(sourceProgram) {}
  38. ~HLSLIncludeHandler() {}
  39. STDMETHOD(Open)(D3DXINCLUDE_TYPE IncludeType,
  40. LPCSTR pFileName,
  41. LPCVOID pParentData,
  42. LPCVOID *ppData,
  43. UINT *pByteLen
  44. )
  45. {
  46. // TODO PORT - I'm not sure what to do with this. It will probably break something in its current state.
  47. //// find & load source code
  48. //DataStreamPtr stream =
  49. // ResourceGroupManager::getSingleton().openResource(
  50. // String(pFileName), mProgram->getGroup(), true, mProgram);
  51. //String source = stream->getAsString();
  52. //// copy into separate c-string
  53. //// Note - must NOT copy the null terminator, otherwise this will terminate
  54. //// the entire program string!
  55. //*pByteLen = static_cast<UINT>(source.length());
  56. //char* pChar = new char[*pByteLen];
  57. //memcpy(pChar, source.c_str(), *pByteLen);
  58. //*ppData = pChar;
  59. assert(false); // TODO - Include files not supported until I can figure out how to handle them
  60. return S_OK;
  61. }
  62. STDMETHOD(Close)(LPCVOID pData)
  63. {
  64. char* pChar = (char*)pData;
  65. delete [] pChar;
  66. return S_OK;
  67. }
  68. protected:
  69. HighLevelGpuProgram* mProgram;
  70. };
  71. class D3D9HLSLParamParser
  72. {
  73. public:
  74. D3D9HLSLParamParser(LPD3DXCONSTANTTABLE constTable)
  75. :mpConstTable(constTable)
  76. { }
  77. GpuParamDesc buildParameterDescriptions();
  78. private:
  79. void processParameter(GpuParamBlockDesc& blockDesc, D3DXHANDLE parent, String prefix, UINT32 index);
  80. void populateParamMemberDesc(GpuParamMemberDesc& memberDesc, D3DXCONSTANT_DESC& d3dDesc);
  81. private:
  82. LPD3DXCONSTANTTABLE mpConstTable;
  83. GpuParamDesc mParamDesc;
  84. };
  85. GpuParamDesc D3D9HLSLParamParser::buildParameterDescriptions()
  86. {
  87. // Derive parameter names from const table
  88. assert(mpConstTable && "Program not loaded!");
  89. // Get contents of the constant table
  90. D3DXCONSTANTTABLE_DESC desc;
  91. HRESULT hr = mpConstTable->GetDesc(&desc);
  92. if (FAILED(hr))
  93. CM_EXCEPT(InternalErrorException, "Cannot retrieve constant descriptions from HLSL program.");
  94. // DX9 has no concept of parameter blocks so we just put all members in one global block
  95. String name = "CM_INTERNAL_Globals";
  96. mParamDesc.paramBlocks.insert(std::make_pair(name, GpuParamBlockDesc()));
  97. GpuParamBlockDesc& blockDesc = mParamDesc.paramBlocks[name];
  98. blockDesc.name = name;
  99. blockDesc.slot = 0;
  100. blockDesc.blockSize = 0;
  101. // Iterate over the constants
  102. for (UINT32 i = 0; i < desc.Constants; ++i)
  103. {
  104. // Recursively descend through the structure levels
  105. processParameter(blockDesc, NULL, "", i);
  106. }
  107. return mParamDesc;
  108. }
  109. void D3D9HLSLParamParser::processParameter(GpuParamBlockDesc& blockDesc, D3DXHANDLE parent, String prefix, UINT32 index)
  110. {
  111. D3DXHANDLE hConstant = mpConstTable->GetConstant(parent, index);
  112. // Since D3D HLSL doesn't deal with naming of array and struct parameters
  113. // automatically, we have to do it by hand
  114. D3DXCONSTANT_DESC desc;
  115. UINT32 numParams = 1;
  116. HRESULT hr = mpConstTable->GetConstantDesc(hConstant, &desc, &numParams);
  117. if (FAILED(hr))
  118. {
  119. CM_EXCEPT(InternalErrorException, "Cannot retrieve constant description from HLSL program.");
  120. }
  121. String paramName = desc.Name;
  122. // trim the odd '$' which appears at the start of the names in HLSL
  123. if (paramName.at(0) == '$')
  124. paramName.erase(paramName.begin());
  125. // Also trim the '[0]' suffix if it exists, we will add our own indexing later
  126. if (StringUtil::endsWith(paramName, "[0]", false))
  127. paramName.erase(paramName.size() - 3);
  128. if (desc.Class == D3DXPC_STRUCT)
  129. {
  130. // work out a new prefix for nested members, if it's an array, we need an index
  131. prefix = prefix + paramName + ".";
  132. // Cascade into struct
  133. for (UINT32 i = 0; i < desc.StructMembers; ++i)
  134. {
  135. processParameter(blockDesc, hConstant, prefix, i);
  136. }
  137. }
  138. else
  139. {
  140. // Process params
  141. if (desc.Type == D3DXPT_FLOAT || desc.Type == D3DXPT_INT || desc.Type == D3DXPT_BOOL)
  142. {
  143. GpuParamMemberDesc memberDesc;
  144. memberDesc.gpuMemOffset = desc.RegisterIndex;
  145. memberDesc.cpuMemOffset = blockDesc.blockSize;
  146. memberDesc.paramBlockSlot = blockDesc.slot;
  147. memberDesc.arraySize = 1;
  148. String name = prefix + paramName;
  149. memberDesc.name = name;
  150. populateParamMemberDesc(memberDesc, desc);
  151. mParamDesc.params.insert(std::make_pair(name, memberDesc));
  152. blockDesc.blockSize += memberDesc.elementSize * memberDesc.arraySize;
  153. }
  154. else if(desc.Type == D3DXPT_SAMPLER1D || desc.Type == D3DXPT_SAMPLER2D || desc.Type == D3DXPT_SAMPLER3D || desc.Type == D3DXPT_SAMPLERCUBE)
  155. {
  156. GpuParamSpecialDesc samplerDesc;
  157. samplerDesc.name = paramName;
  158. samplerDesc.slot = desc.RegisterIndex;
  159. GpuParamSpecialDesc textureDesc;
  160. textureDesc.name = paramName;
  161. textureDesc.slot = desc.RegisterIndex;
  162. switch(desc.Type)
  163. {
  164. case D3DXPT_SAMPLER1D:
  165. samplerDesc.type = GST_SAMPLER1D;
  166. textureDesc.type = GST_TEXTURE1D;
  167. break;
  168. case D3DXPT_SAMPLER2D:
  169. samplerDesc.type = GST_SAMPLER2D;
  170. textureDesc.type = GST_TEXTURE2D;
  171. break;
  172. case D3DXPT_SAMPLER3D:
  173. samplerDesc.type = GST_SAMPLER3D;
  174. textureDesc.type = GST_TEXTURE3D;
  175. break;
  176. case D3DXPT_SAMPLERCUBE:
  177. samplerDesc.type = GST_SAMPLERCUBE;
  178. textureDesc.type = GST_TEXTURECUBE;
  179. break;
  180. default:
  181. CM_EXCEPT(InternalErrorException, "Invalid sampler type: " + toString(desc.Type) + " for parameter " + paramName);
  182. }
  183. mParamDesc.samplers.insert(std::make_pair(paramName, samplerDesc));
  184. mParamDesc.textures.insert(std::make_pair(paramName, textureDesc));
  185. }
  186. else
  187. {
  188. CM_EXCEPT(InternalErrorException, "Invalid shader parameter type: " + toString(desc.Type) + " for parameter " + paramName);
  189. }
  190. }
  191. }
  192. void D3D9HLSLParamParser::populateParamMemberDesc(GpuParamMemberDesc& memberDesc, D3DXCONSTANT_DESC& d3dDesc)
  193. {
  194. memberDesc.arraySize = d3dDesc.Elements;
  195. switch(d3dDesc.Type)
  196. {
  197. case D3DXPT_INT:
  198. switch(d3dDesc.Columns)
  199. {
  200. case 1:
  201. memberDesc.type = GMT_INT1;
  202. memberDesc.elementSize = 4;
  203. break;
  204. case 2:
  205. memberDesc.type = GMT_INT2;
  206. memberDesc.elementSize = 4;
  207. break;
  208. case 3:
  209. memberDesc.type = GMT_INT3;
  210. memberDesc.elementSize = 4;
  211. break;
  212. case 4:
  213. memberDesc.type = GMT_INT4;
  214. memberDesc.elementSize = 4;
  215. break;
  216. } // columns
  217. break;
  218. case D3DXPT_FLOAT:
  219. switch(d3dDesc.Class)
  220. {
  221. case D3DXPC_MATRIX_COLUMNS:
  222. case D3DXPC_MATRIX_ROWS:
  223. {
  224. int firstDim, secondDim;
  225. firstDim = d3dDesc.RegisterCount / d3dDesc.Elements;
  226. if (d3dDesc.Class == D3DXPC_MATRIX_ROWS)
  227. secondDim = d3dDesc.Columns;
  228. else
  229. secondDim = d3dDesc.Rows;
  230. switch(firstDim)
  231. {
  232. case 2:
  233. switch(secondDim)
  234. {
  235. case 2:
  236. memberDesc.type = GMT_MATRIX_2X2;
  237. memberDesc.elementSize = 8; // HLSL always packs
  238. break;
  239. case 3:
  240. memberDesc.type = GMT_MATRIX_2X3;
  241. memberDesc.elementSize = 8; // HLSL always packs
  242. break;
  243. case 4:
  244. memberDesc.type = GMT_MATRIX_2X4;
  245. memberDesc.elementSize = 8;
  246. break;
  247. } // columns
  248. break;
  249. case 3:
  250. switch(secondDim)
  251. {
  252. case 2:
  253. memberDesc.type = GMT_MATRIX_3X2;
  254. memberDesc.elementSize = 12; // HLSL always packs
  255. break;
  256. case 3:
  257. memberDesc.type = GMT_MATRIX_3X3;
  258. memberDesc.elementSize = 12; // HLSL always packs
  259. break;
  260. case 4:
  261. memberDesc.type = GMT_MATRIX_3X4;
  262. memberDesc.elementSize = 12;
  263. break;
  264. } // columns
  265. break;
  266. case 4:
  267. switch(secondDim)
  268. {
  269. case 2:
  270. memberDesc.type = GMT_MATRIX_4X2;
  271. memberDesc.elementSize = 16; // HLSL always packs
  272. break;
  273. case 3:
  274. memberDesc.type = GMT_MATRIX_4X3;
  275. memberDesc.elementSize = 16; // HLSL always packs
  276. break;
  277. case 4:
  278. memberDesc.type = GMT_MATRIX_4X4;
  279. memberDesc.elementSize = 16;
  280. break;
  281. } // secondDim
  282. break;
  283. } // firstDim
  284. }
  285. break;
  286. case D3DXPC_SCALAR:
  287. case D3DXPC_VECTOR:
  288. switch(d3dDesc.Columns)
  289. {
  290. case 1:
  291. memberDesc.type = GMT_FLOAT1;
  292. memberDesc.elementSize = 4;
  293. break;
  294. case 2:
  295. memberDesc.type = GMT_FLOAT2;
  296. memberDesc.elementSize = 4;
  297. break;
  298. case 3:
  299. memberDesc.type = GMT_FLOAT3;
  300. memberDesc.elementSize = 4;
  301. break;
  302. case 4:
  303. memberDesc.type = GMT_FLOAT4;
  304. memberDesc.elementSize = 4;
  305. break;
  306. } // columns
  307. break;
  308. }
  309. break;
  310. case D3DXPT_BOOL:
  311. memberDesc.type = GMT_BOOL;
  312. memberDesc.elementSize = 1;
  313. break;
  314. default:
  315. break;
  316. };
  317. }
  318. //-----------------------------------------------------------------------
  319. void D3D9HLSLProgram::loadFromSource(void)
  320. {
  321. // Populate preprocessor defines
  322. String stringBuffer;
  323. vector<D3DXMACRO>::type defines;
  324. const D3DXMACRO* pDefines = 0;
  325. if (!mPreprocessorDefines.empty())
  326. {
  327. stringBuffer = mPreprocessorDefines;
  328. // Split preprocessor defines and build up macro array
  329. D3DXMACRO macro;
  330. String::size_type pos = 0;
  331. while (pos != String::npos)
  332. {
  333. macro.Name = &stringBuffer[pos];
  334. macro.Definition = 0;
  335. String::size_type start_pos=pos;
  336. // Find delims
  337. pos = stringBuffer.find_first_of(";,=", pos);
  338. if(start_pos==pos)
  339. {
  340. if(pos==stringBuffer.length())
  341. {
  342. break;
  343. }
  344. pos++;
  345. continue;
  346. }
  347. if (pos != String::npos)
  348. {
  349. // Check definition part
  350. if (stringBuffer[pos] == '=')
  351. {
  352. // Setup null character for macro name
  353. stringBuffer[pos++] = '\0';
  354. macro.Definition = &stringBuffer[pos];
  355. pos = stringBuffer.find_first_of(";,", pos);
  356. }
  357. else
  358. {
  359. // No definition part, define as "1"
  360. macro.Definition = "1";
  361. }
  362. if (pos != String::npos)
  363. {
  364. // Setup null character for macro name or definition
  365. stringBuffer[pos++] = '\0';
  366. }
  367. }
  368. else
  369. {
  370. macro.Definition = "1";
  371. }
  372. if(strlen(macro.Name)>0)
  373. {
  374. defines.push_back(macro);
  375. }
  376. else
  377. {
  378. break;
  379. }
  380. }
  381. // Add NULL terminator
  382. macro.Name = 0;
  383. macro.Definition = 0;
  384. defines.push_back(macro);
  385. pDefines = &defines[0];
  386. }
  387. // Populate compile flags
  388. DWORD compileFlags = 0;
  389. if (mColumnMajorMatrices)
  390. compileFlags |= D3DXSHADER_PACKMATRIX_COLUMNMAJOR;
  391. else
  392. compileFlags |= D3DXSHADER_PACKMATRIX_ROWMAJOR;
  393. #if CM_DEBUG_MODE
  394. compileFlags |= D3DXSHADER_DEBUG;
  395. #endif
  396. switch (mOptimisationLevel)
  397. {
  398. case OPT_DEFAULT:
  399. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  400. break;
  401. case OPT_NONE:
  402. compileFlags |= D3DXSHADER_SKIPOPTIMIZATION;
  403. break;
  404. case OPT_0:
  405. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL0;
  406. break;
  407. case OPT_1:
  408. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  409. break;
  410. case OPT_2:
  411. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL2;
  412. break;
  413. case OPT_3:
  414. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL3;
  415. break;
  416. }
  417. LPD3DXBUFFER errors = 0;
  418. // include handler
  419. HLSLIncludeHandler includeHandler(this);
  420. LPD3DXCONSTANTTABLE constTable;
  421. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  422. // Compile & assemble into microcode
  423. HRESULT hr = D3DXCompileShader(
  424. mSource.c_str(),
  425. static_cast<UINT>(mSource.length()),
  426. pDefines,
  427. &includeHandler,
  428. mEntryPoint.c_str(),
  429. hlslProfile.c_str(),
  430. compileFlags,
  431. &mpMicroCode,
  432. &errors,
  433. &constTable);
  434. if (FAILED(hr))
  435. {
  436. String message = "Cannot assemble D3D9 high-level shader ";
  437. if( errors )
  438. {
  439. message += String(" Errors:\n") + static_cast<const char*>(errors->GetBufferPointer());
  440. errors->Release();
  441. }
  442. CM_EXCEPT(RenderingAPIException, message);
  443. }
  444. hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  445. // Create a low-level program, give it the same name as us
  446. mAssemblerProgram =
  447. GpuProgramManager::instance().createProgram(
  448. "",// dummy source, since we'll be using microcode
  449. "",
  450. hlslProfile,
  451. mType,
  452. GPP_NONE);
  453. static_cast<D3D9GpuProgram*>(mAssemblerProgram.get())->setExternalMicrocode(mpMicroCode);
  454. D3D9HLSLParamParser paramParser(constTable);
  455. mParametersDesc = paramParser.buildParameterDescriptions();
  456. SAFE_RELEASE(constTable);
  457. }
  458. //-----------------------------------------------------------------------
  459. void D3D9HLSLProgram::destroy_internal()
  460. {
  461. SAFE_RELEASE(mpMicroCode);
  462. HighLevelGpuProgram::destroy_internal();
  463. }
  464. //-----------------------------------------------------------------------
  465. LPD3DXBUFFER D3D9HLSLProgram::getMicroCode()
  466. {
  467. return mpMicroCode;
  468. }
  469. //-----------------------------------------------------------------------
  470. D3D9HLSLProgram::D3D9HLSLProgram(const String& source, const String& entryPoint, const String& language,
  471. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  472. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, isAdjacencyInfoRequired)
  473. , mPreprocessorDefines()
  474. , mColumnMajorMatrices(true)
  475. , mpMicroCode(NULL)
  476. , mOptimisationLevel(OPT_DEFAULT)
  477. {
  478. }
  479. //-----------------------------------------------------------------------
  480. D3D9HLSLProgram::~D3D9HLSLProgram()
  481. {
  482. }
  483. //-----------------------------------------------------------------------
  484. bool D3D9HLSLProgram::isSupported(void) const
  485. {
  486. if (!isRequiredCapabilitiesSupported())
  487. return false;
  488. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  489. RenderSystem* rs = CamelotEngine::RenderSystem::instancePtr();
  490. return rs->getCapabilities()->isShaderProfileSupported(hlslProfile);
  491. }
  492. //-----------------------------------------------------------------------
  493. GpuParamsPtr D3D9HLSLProgram::createParameters()
  494. {
  495. GpuParamsPtr params(new GpuParams(mParametersDesc));
  496. params->setTransposeMatrices(mColumnMajorMatrices);
  497. return params;
  498. }
  499. //-----------------------------------------------------------------------
  500. const String& D3D9HLSLProgram::getLanguage(void) const
  501. {
  502. static const String language = "hlsl";
  503. return language;
  504. }
  505. /************************************************************************/
  506. /* SERIALIZATION */
  507. /************************************************************************/
  508. RTTITypeBase* D3D9HLSLProgram::getRTTIStatic()
  509. {
  510. return D3D9HLSLProgramRTTI::instance();
  511. }
  512. RTTITypeBase* D3D9HLSLProgram::getRTTI() const
  513. {
  514. return D3D9HLSLProgram::getRTTIStatic();
  515. }
  516. }