CmCgProgram.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 "CmCgProgram.h"
  25. #include "CmGpuProgramManager.h"
  26. #include "CmHighLevelGpuProgramManager.h"
  27. #include "CmDebug.h"
  28. #include "CmException.h"
  29. namespace CamelotEngine {
  30. void checkForCgError(const String& ogreMethod, const String& errorTextPrefix, CGcontext context)
  31. {
  32. CGerror error = cgGetError();
  33. if (error != CG_NO_ERROR)
  34. {
  35. String msg = errorTextPrefix + cgGetErrorString(error);
  36. if (error == CG_COMPILER_ERROR)
  37. {
  38. // Get listing with full compile errors
  39. msg = msg + "\n" + cgGetLastListing(context);
  40. }
  41. CM_EXCEPT(InternalErrorException, msg);
  42. }
  43. }
  44. //-----------------------------------------------------------------------
  45. void CgProgram::selectProfile(void)
  46. {
  47. mSelectedProfile.clear();
  48. mSelectedCgProfile = CG_PROFILE_UNKNOWN;
  49. mSelectedProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  50. GpuProgramManager& gpuMgr = GpuProgramManager::instance();
  51. if (gpuMgr.isSyntaxSupported(mSelectedProfile))
  52. {
  53. mSelectedCgProfile = cgGetProfile(mSelectedProfile.c_str());
  54. // Check for errors
  55. checkForCgError("CgProgram::selectProfile",
  56. "Unable to find CG profile enum for program.", mCgContext);
  57. }
  58. else
  59. mSelectedProfile.clear();
  60. }
  61. //-----------------------------------------------------------------------
  62. void CgProgram::buildArgs(void)
  63. {
  64. vector<String>::type args;
  65. if (!mCompileArgs.empty())
  66. args = StringUtil::split(mCompileArgs);
  67. vector<String>::type::const_iterator i;
  68. if (mSelectedCgProfile == CG_PROFILE_VS_1_1)
  69. {
  70. // Need the 'dcls' argument whenever we use this profile
  71. // otherwise compilation of the assembler will fail
  72. bool dclsFound = false;
  73. for (i = args.begin(); i != args.end(); ++i)
  74. {
  75. if (*i == "dcls")
  76. {
  77. dclsFound = true;
  78. break;
  79. }
  80. }
  81. if (!dclsFound)
  82. {
  83. args.push_back("-profileopts");
  84. args.push_back("dcls");
  85. }
  86. }
  87. // Now split args into that god-awful char** that Cg insists on
  88. freeCgArgs();
  89. mCgArguments = (char**)malloc(sizeof(char*) * (args.size() + 1));
  90. int index = 0;
  91. for (i = args.begin(); i != args.end(); ++i, ++index)
  92. {
  93. mCgArguments[index] = (char*)malloc(sizeof(char) * (i->length() + 1));
  94. strcpy(mCgArguments[index], i->c_str());
  95. }
  96. // Null terminate list
  97. mCgArguments[index] = 0;
  98. }
  99. //-----------------------------------------------------------------------
  100. void CgProgram::freeCgArgs(void)
  101. {
  102. if (mCgArguments)
  103. {
  104. size_t index = 0;
  105. char* current = mCgArguments[index];
  106. while (current)
  107. {
  108. free(current);
  109. mCgArguments[index] = 0;
  110. current = mCgArguments[++index];
  111. }
  112. free(mCgArguments);
  113. mCgArguments = 0;
  114. }
  115. }
  116. //-----------------------------------------------------------------------
  117. void CgProgram::loadFromSource(void)
  118. {
  119. // Create Cg Program
  120. selectProfile();
  121. if (mSelectedCgProfile == CG_PROFILE_UNKNOWN)
  122. {
  123. gDebug().log("Attempted to load Cg program but no supported "
  124. "profile was found.", "RenderSystem");
  125. return;
  126. }
  127. buildArgs();
  128. // TODO PORT - This doesn't load includes
  129. // deal with includes
  130. String sourceToUse = mSource;
  131. //String sourceToUse = resolveCgIncludes(mSource, this, mFilename);
  132. mCgProgram = cgCreateProgram(mCgContext, CG_SOURCE, sourceToUse.c_str(),
  133. mSelectedCgProfile, mEntryPoint.c_str(), const_cast<const char**>(mCgArguments));
  134. // Test
  135. //LogManager::getSingleton().logMessage(cgGetProgramString(mCgProgram, CG_COMPILED_PROGRAM));
  136. // Check for errors
  137. checkForCgError("CgProgram::loadFromSource",
  138. "Unable to compile Cg program", mCgContext);
  139. }
  140. //-----------------------------------------------------------------------
  141. void CgProgram::createLowLevelImpl(void)
  142. {
  143. // ignore any previous error
  144. if (mSelectedCgProfile != CG_PROFILE_UNKNOWN && !mCompileError)
  145. {
  146. if (mSelectedCgProfile == CG_PROFILE_VS_4_0 || mSelectedCgProfile == CG_PROFILE_PS_4_0)
  147. {
  148. String hlslSourceFromCg = cgGetProgramString(mCgProgram, CG_COMPILED_PROGRAM);
  149. // Create a high-level program, give it the same name as us
  150. HighLevelGpuProgramPtr vp =
  151. HighLevelGpuProgramManager::instance().createProgram(
  152. hlslSourceFromCg, "main", "hlsl", mType, mProfile);
  153. vp->load();
  154. mAssemblerProgram = vp;
  155. }
  156. else
  157. {
  158. String shaderAssemblerCode = cgGetProgramString(mCgProgram, CG_COMPILED_PROGRAM);
  159. // Create a low-level program, give it the same name as us
  160. mAssemblerProgram =
  161. GpuProgramManager::instance().createProgram(
  162. shaderAssemblerCode,
  163. mType,
  164. mSelectedProfile);
  165. }
  166. // Shader params need to be forwarded to low level implementation
  167. mAssemblerProgram->setAdjacencyInfoRequired(isAdjacencyInfoRequired());
  168. }
  169. }
  170. //-----------------------------------------------------------------------
  171. void CgProgram::unloadHighLevelImpl(void)
  172. {
  173. // Unload Cg Program
  174. // Lowlevel program will get unloaded elsewhere
  175. if (mCgProgram)
  176. {
  177. cgDestroyProgram(mCgProgram);
  178. checkForCgError("CgProgram::unloadImpl",
  179. "Error while unloading Cg program",
  180. mCgContext);
  181. mCgProgram = 0;
  182. }
  183. }
  184. //-----------------------------------------------------------------------
  185. void CgProgram::buildConstantDefinitions() const
  186. {
  187. // Derive parameter names from Cg
  188. createParameterMappingStructures(true);
  189. if (!mCgProgram)
  190. return;
  191. recurseParams(cgGetFirstParameter(mCgProgram, CG_PROGRAM));
  192. recurseParams(cgGetFirstParameter(mCgProgram, CG_GLOBAL));
  193. }
  194. //---------------------------------------------------------------------
  195. void CgProgram::recurseParams(CGparameter parameter, size_t contextArraySize) const
  196. {
  197. while (parameter != 0)
  198. {
  199. // Look for uniform (non-sampler) parameters only
  200. // Don't bother enumerating unused parameters, especially since they will
  201. // be optimised out and therefore not in the indexed versions
  202. CGtype paramType = cgGetParameterType(parameter);
  203. if (cgGetParameterVariability(parameter) == CG_UNIFORM &&
  204. paramType != CG_SAMPLER1D &&
  205. paramType != CG_SAMPLER2D &&
  206. paramType != CG_SAMPLER3D &&
  207. paramType != CG_SAMPLERCUBE &&
  208. paramType != CG_SAMPLERRECT &&
  209. cgGetParameterDirection(parameter) != CG_OUT &&
  210. cgIsParameterReferenced(parameter))
  211. {
  212. int arraySize;
  213. switch(paramType)
  214. {
  215. case CG_STRUCT:
  216. recurseParams(cgGetFirstStructParameter(parameter));
  217. break;
  218. case CG_ARRAY:
  219. // Support only 1-dimensional arrays
  220. arraySize = cgGetArraySize(parameter, 0);
  221. recurseParams(cgGetArrayParameter(parameter, 0), (size_t)arraySize);
  222. break;
  223. default:
  224. // Normal path (leaf)
  225. String paramName = cgGetParameterName(parameter);
  226. size_t logicalIndex = cgGetParameterResourceIndex(parameter);
  227. // Get the parameter resource, to calculate the physical index
  228. CGresource res = cgGetParameterResource(parameter);
  229. bool isRegisterCombiner = false;
  230. size_t regCombinerPhysicalIndex = 0;
  231. switch (res)
  232. {
  233. case CG_COMBINER_STAGE_CONST0:
  234. // register combiner, const 0
  235. // the index relates to the texture stage; store this as (stage * 2) + 0
  236. regCombinerPhysicalIndex = logicalIndex * 2;
  237. isRegisterCombiner = true;
  238. break;
  239. case CG_COMBINER_STAGE_CONST1:
  240. // register combiner, const 1
  241. // the index relates to the texture stage; store this as (stage * 2) + 1
  242. regCombinerPhysicalIndex = (logicalIndex * 2) + 1;
  243. isRegisterCombiner = true;
  244. break;
  245. default:
  246. // normal constant
  247. break;
  248. }
  249. // Trim the '[0]' suffix if it exists, we will add our own indexing later
  250. if (StringUtil::endsWith(paramName, "[0]", false))
  251. {
  252. paramName.erase(paramName.size() - 3);
  253. }
  254. GpuConstantDefinition def;
  255. def.arraySize = contextArraySize;
  256. mapTypeAndElementSize(paramType, isRegisterCombiner, def);
  257. if (def.constType == GCT_UNKNOWN)
  258. {
  259. gDebug().log("Problem parsing the following Cg Uniform: '" + paramName + "'", "RenderSystem");
  260. // next uniform
  261. parameter = cgGetNextParameter(parameter);
  262. continue;
  263. }
  264. if (isRegisterCombiner)
  265. {
  266. def.physicalIndex = regCombinerPhysicalIndex;
  267. }
  268. else
  269. {
  270. // base position on existing buffer contents
  271. if (def.isFloat())
  272. {
  273. def.physicalIndex = mFloatLogicalToPhysical->bufferSize;
  274. }
  275. else
  276. {
  277. def.physicalIndex = mIntLogicalToPhysical->bufferSize;
  278. }
  279. }
  280. def.logicalIndex = logicalIndex;
  281. mConstantDefs->map.insert(GpuConstantDefinitionMap::value_type(paramName, def));
  282. // Record logical / physical mapping
  283. if (def.isFloat())
  284. {
  285. CM_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  286. mFloatLogicalToPhysical->map.insert(
  287. GpuLogicalIndexUseMap::value_type(logicalIndex,
  288. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  289. mFloatLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
  290. mConstantDefs->floatBufferSize = mFloatLogicalToPhysical->bufferSize;
  291. }
  292. else
  293. {
  294. CM_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  295. mIntLogicalToPhysical->map.insert(
  296. GpuLogicalIndexUseMap::value_type(logicalIndex,
  297. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  298. mIntLogicalToPhysical->bufferSize += def.arraySize * def.elementSize;
  299. mConstantDefs->intBufferSize = mIntLogicalToPhysical->bufferSize;
  300. }
  301. // Deal with array indexing
  302. mConstantDefs->generateConstantDefinitionArrayEntries(paramName, def);
  303. break;
  304. }
  305. }
  306. // Get next
  307. parameter = cgGetNextParameter(parameter);
  308. }
  309. }
  310. //-----------------------------------------------------------------------
  311. void CgProgram::mapTypeAndElementSize(CGtype cgType, bool isRegisterCombiner,
  312. GpuConstantDefinition& def) const
  313. {
  314. if (isRegisterCombiner)
  315. {
  316. // register combiners are the only single-float entries in our buffer
  317. def.constType = GCT_FLOAT1;
  318. def.elementSize = 1;
  319. }
  320. else
  321. {
  322. switch(cgType)
  323. {
  324. case CG_FLOAT:
  325. case CG_FLOAT1:
  326. case CG_HALF:
  327. case CG_HALF1:
  328. def.constType = GCT_FLOAT1;
  329. break;
  330. case CG_FLOAT2:
  331. case CG_HALF2:
  332. def.constType = GCT_FLOAT2;
  333. break;
  334. case CG_FLOAT3:
  335. case CG_HALF3:
  336. def.constType = GCT_FLOAT3;
  337. break;
  338. case CG_FLOAT4:
  339. case CG_HALF4:
  340. def.constType = GCT_FLOAT4;
  341. break;
  342. case CG_FLOAT2x2:
  343. case CG_HALF2x2:
  344. def.constType = GCT_MATRIX_2X2;
  345. break;
  346. case CG_FLOAT2x3:
  347. case CG_HALF2x3:
  348. def.constType = GCT_MATRIX_2X3;
  349. break;
  350. case CG_FLOAT2x4:
  351. case CG_HALF2x4:
  352. def.constType = GCT_MATRIX_2X4;
  353. break;
  354. case CG_FLOAT3x2:
  355. case CG_HALF3x2:
  356. def.constType = GCT_MATRIX_3X2;
  357. break;
  358. case CG_FLOAT3x3:
  359. case CG_HALF3x3:
  360. def.constType = GCT_MATRIX_3X3;
  361. break;
  362. case CG_FLOAT3x4:
  363. case CG_HALF3x4:
  364. def.constType = GCT_MATRIX_3X4;
  365. break;
  366. case CG_FLOAT4x2:
  367. case CG_HALF4x2:
  368. def.constType = GCT_MATRIX_4X2;
  369. break;
  370. case CG_FLOAT4x3:
  371. case CG_HALF4x3:
  372. def.constType = GCT_MATRIX_4X3;
  373. break;
  374. case CG_FLOAT4x4:
  375. case CG_HALF4x4:
  376. def.constType = GCT_MATRIX_4X4;
  377. break;
  378. case CG_INT:
  379. case CG_INT1:
  380. def.constType = GCT_INT1;
  381. break;
  382. case CG_INT2:
  383. def.constType = GCT_INT2;
  384. break;
  385. case CG_INT3:
  386. def.constType = GCT_INT3;
  387. break;
  388. case CG_INT4:
  389. def.constType = GCT_INT4;
  390. break;
  391. default:
  392. def.constType = GCT_UNKNOWN;
  393. break;
  394. }
  395. // Cg pads
  396. def.elementSize = GpuConstantDefinition::getElementSize(def.constType, true);
  397. }
  398. }
  399. //-----------------------------------------------------------------------
  400. CgProgram::CgProgram(CGcontext context)
  401. : HighLevelGpuProgram(),
  402. mCgContext(context), mCgProgram(0),
  403. mSelectedCgProfile(CG_PROFILE_UNKNOWN), mCgArguments(0)
  404. {
  405. }
  406. //-----------------------------------------------------------------------
  407. CgProgram::~CgProgram()
  408. {
  409. freeCgArgs();
  410. unloadHighLevel();
  411. }
  412. //-----------------------------------------------------------------------
  413. bool CgProgram::isSupported(void) const
  414. {
  415. if (mCompileError || !isRequiredCapabilitiesSupported())
  416. return false;
  417. String selectedProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  418. if (GpuProgramManager::instance().isSyntaxSupported(selectedProfile))
  419. return true;
  420. return false;
  421. }
  422. //-----------------------------------------------------------------------
  423. void CgProgram::setProfiles(const vector<String>::type& profiles)
  424. {
  425. mProfiles.clear();
  426. vector<String>::type::const_iterator i, iend;
  427. iend = profiles.end();
  428. for (i = profiles.begin(); i != iend; ++i)
  429. {
  430. mProfiles.push_back(*i);
  431. }
  432. }
  433. //-----------------------------------------------------------------------
  434. String CgProgram::resolveCgIncludes(const String& inSource, Resource* resourceBeingLoaded, const String& fileName)
  435. {
  436. String outSource;
  437. // TODO PORT - Includes are not handled ATM
  438. // output will be at least this big
  439. //outSource.reserve(inSource.length());
  440. //size_t startMarker = 0;
  441. //size_t i = inSource.find("#include");
  442. //while (i != String::npos)
  443. //{
  444. // size_t includePos = i;
  445. // size_t afterIncludePos = includePos + 8;
  446. // size_t newLineBefore = inSource.rfind("\n", includePos);
  447. // // check we're not in a comment
  448. // size_t lineCommentIt = inSource.rfind("//", includePos);
  449. // if (lineCommentIt != String::npos)
  450. // {
  451. // if (newLineBefore == String::npos || lineCommentIt > newLineBefore)
  452. // {
  453. // // commented
  454. // i = inSource.find("#include", afterIncludePos);
  455. // continue;
  456. // }
  457. // }
  458. // size_t blockCommentIt = inSource.rfind("/*", includePos);
  459. // if (blockCommentIt != String::npos)
  460. // {
  461. // size_t closeCommentIt = inSource.rfind("*/", includePos);
  462. // if (closeCommentIt == String::npos || closeCommentIt < blockCommentIt)
  463. // {
  464. // // commented
  465. // i = inSource.find("#include", afterIncludePos);
  466. // continue;
  467. // }
  468. // }
  469. // // find following newline (or EOF)
  470. // size_t newLineAfter = inSource.find("\n", afterIncludePos);
  471. // // find include file string container
  472. // String endDelimeter = "\"";
  473. // size_t startIt = inSource.find("\"", afterIncludePos);
  474. // if (startIt == String::npos || startIt > newLineAfter)
  475. // {
  476. // // try <>
  477. // startIt = inSource.find("<", afterIncludePos);
  478. // if (startIt == String::npos || startIt > newLineAfter)
  479. // {
  480. // CM_EXCEPT(InternalErrorException,
  481. // "Badly formed #include directive (expected \" or <) in file "
  482. // + fileName + ": " + inSource.substr(includePos, newLineAfter-includePos));
  483. // }
  484. // else
  485. // {
  486. // endDelimeter = ">";
  487. // }
  488. // }
  489. // size_t endIt = inSource.find(endDelimeter, startIt+1);
  490. // if (endIt == String::npos || endIt <= startIt)
  491. // {
  492. // CM_EXCEPT(InternalErrorException,
  493. // "Badly formed #include directive (expected " + endDelimeter + ") in file "
  494. // + fileName + ": " + inSource.substr(includePos, newLineAfter-includePos));
  495. // }
  496. // // extract filename
  497. // String filename(inSource.substr(startIt+1, endIt-startIt-1));
  498. // // open included file
  499. // DataStreamPtr resource = ResourceGroupManager::getSingleton().
  500. // openResource(filename, resourceBeingLoaded->getGroup(), true, resourceBeingLoaded);
  501. // // replace entire include directive line
  502. // // copy up to just before include
  503. // if (newLineBefore != String::npos && newLineBefore >= startMarker)
  504. // outSource.append(inSource.substr(startMarker, newLineBefore-startMarker+1));
  505. // size_t lineCount = 0;
  506. // size_t lineCountPos = 0;
  507. //
  508. // // Count the line number of #include statement
  509. // lineCountPos = outSource.find('\n');
  510. // while(lineCountPos != String::npos)
  511. // {
  512. // lineCountPos = outSource.find('\n', lineCountPos+1);
  513. // lineCount++;
  514. // }
  515. // // Add #line to the start of the included file to correct the line count
  516. // outSource.append("#line 1 \"" + filename + "\"\n");
  517. // outSource.append(resource->getAsString());
  518. // // Add #line to the end of the included file to correct the line count
  519. // outSource.append("\n#line " + toString(lineCount) +
  520. // "\"" + fileName + "\"\n");
  521. // startMarker = newLineAfter;
  522. // if (startMarker != String::npos)
  523. // i = inSource.find("#include", startMarker);
  524. // else
  525. // i = String::npos;
  526. //}
  527. //// copy any remaining characters
  528. //outSource.append(inSource.substr(startMarker));
  529. return outSource;
  530. }
  531. //-----------------------------------------------------------------------
  532. const String& CgProgram::getLanguage(void) const
  533. {
  534. static const String language = "cg";
  535. return language;
  536. }
  537. }