OgreGpuProgram.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "OgreGpuProgram.h"
  25. #include "OgreHighLevelGpuProgram.h"
  26. #include "OgreVector3.h"
  27. #include "OgreVector4.h"
  28. #include "OgreRenderSystemCapabilities.h"
  29. #include "OgreStringConverter.h"
  30. #include "OgreException.h"
  31. namespace Ogre
  32. {
  33. //-----------------------------------------------------------------------------
  34. GpuProgram::CmdType GpuProgram::msTypeCmd;
  35. GpuProgram::CmdSyntax GpuProgram::msSyntaxCmd;
  36. GpuProgram::CmdSkeletal GpuProgram::msSkeletalCmd;
  37. GpuProgram::CmdMorph GpuProgram::msMorphCmd;
  38. GpuProgram::CmdPose GpuProgram::msPoseCmd;
  39. GpuProgram::CmdVTF GpuProgram::msVTFCmd;
  40. GpuProgram::CmdManualNamedConstsFile GpuProgram::msManNamedConstsFileCmd;
  41. GpuProgram::CmdAdjacency GpuProgram::msAdjacencyCmd;
  42. //-----------------------------------------------------------------------------
  43. GpuProgram::GpuProgram()
  44. :mType(GPT_VERTEX_PROGRAM), mLoadFromFile(true), mSkeletalAnimation(false),
  45. mMorphAnimation(false), mPoseAnimation(0),
  46. mVertexTextureFetch(false), mNeedsAdjacencyInfo(false),
  47. mCompileError(false), mLoadedManualNamedConstants(false)
  48. {
  49. createParameterMappingStructures();
  50. }
  51. //-----------------------------------------------------------------------------
  52. void GpuProgram::setType(GpuProgramType t)
  53. {
  54. mType = t;
  55. }
  56. //-----------------------------------------------------------------------------
  57. void GpuProgram::setSyntaxCode(const String& syntax)
  58. {
  59. mSyntaxCode = syntax;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void GpuProgram::setSourceFile(const String& filename)
  63. {
  64. mFilename = filename;
  65. mSource.clear();
  66. mLoadFromFile = true;
  67. mCompileError = false;
  68. }
  69. //-----------------------------------------------------------------------------
  70. void GpuProgram::setSource(const String& source)
  71. {
  72. mSource = source;
  73. mFilename.clear();
  74. mLoadFromFile = false;
  75. mCompileError = false;
  76. }
  77. //-----------------------------------------------------------------------------
  78. void GpuProgram::loadImpl(void)
  79. {
  80. // Call polymorphic load
  81. try
  82. {
  83. loadFromSource();
  84. if (!mDefaultParams.isNull())
  85. {
  86. // Keep a reference to old ones to copy
  87. GpuProgramParametersSharedPtr savedParams = mDefaultParams;
  88. // reset params to stop them being referenced in the next create
  89. mDefaultParams.setNull();
  90. // Create new params
  91. mDefaultParams = createParameters();
  92. // Copy old (matching) values across
  93. // Don't use copyConstantsFrom since program may be different
  94. mDefaultParams->copyMatchingNamedConstantsFrom(*savedParams.get());
  95. }
  96. }
  97. catch (const Exception&)
  98. {
  99. // will already have been logged
  100. //LogManager::getSingleton().stream()
  101. // << "Gpu program " << mName << " encountered an error "
  102. // << "during loading and is thus not supported.";
  103. mCompileError = true;
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool GpuProgram::isRequiredCapabilitiesSupported(void) const
  108. {
  109. // TODO PORT- Enable once I port rendersystem. Right now just assume everything is supported and return true
  110. return true;
  111. //const RenderSystemCapabilities* caps =
  112. // Root::getSingleton().getRenderSystem()->getCapabilities();
  113. // // If skeletal animation is being done, we need support for UBYTE4
  114. // if (isSkeletalAnimationIncluded() &&
  115. // !caps->hasCapability(RSC_VERTEX_FORMAT_UBYTE4))
  116. // {
  117. // return false;
  118. // }
  119. //// Vertex texture fetch required?
  120. //if (isVertexTextureFetchRequired() &&
  121. // !caps->hasCapability(RSC_VERTEX_TEXTURE_FETCH))
  122. //{
  123. // return false;
  124. //}
  125. // return true;
  126. }
  127. //-----------------------------------------------------------------------------
  128. bool GpuProgram::isSupported(void) const
  129. {
  130. if (mCompileError || !isRequiredCapabilitiesSupported())
  131. return false;
  132. // TODO PORT - Enable this once I have ported the render system, for now just return true
  133. //RenderSystem* rs = Root::getSingleton().getRenderSystem();
  134. //return rs->getCapabilities()->isShaderProfileSupported(mSyntaxCode);
  135. return true;
  136. }
  137. //---------------------------------------------------------------------
  138. void GpuProgram::createParameterMappingStructures(bool recreateIfExists) const
  139. {
  140. createLogicalParameterMappingStructures(recreateIfExists);
  141. createNamedParameterMappingStructures(recreateIfExists);
  142. }
  143. //---------------------------------------------------------------------
  144. void GpuProgram::createLogicalParameterMappingStructures(bool recreateIfExists) const
  145. {
  146. if (recreateIfExists || mFloatLogicalToPhysical.isNull())
  147. mFloatLogicalToPhysical = GpuLogicalBufferStructPtr(OGRE_NEW GpuLogicalBufferStruct());
  148. if (recreateIfExists || mIntLogicalToPhysical.isNull())
  149. mIntLogicalToPhysical = GpuLogicalBufferStructPtr(OGRE_NEW GpuLogicalBufferStruct());
  150. }
  151. //---------------------------------------------------------------------
  152. void GpuProgram::createNamedParameterMappingStructures(bool recreateIfExists) const
  153. {
  154. if (recreateIfExists || mConstantDefs.isNull())
  155. mConstantDefs = GpuNamedConstantsPtr(OGRE_NEW GpuNamedConstants());
  156. }
  157. //---------------------------------------------------------------------
  158. void GpuProgram::setManualNamedConstantsFile(const String& paramDefFile)
  159. {
  160. mManualNamedConstantsFile = paramDefFile;
  161. mLoadedManualNamedConstants = false;
  162. }
  163. //---------------------------------------------------------------------
  164. void GpuProgram::setManualNamedConstants(const GpuNamedConstants& namedConstants)
  165. {
  166. createParameterMappingStructures();
  167. *mConstantDefs.get() = namedConstants;
  168. mFloatLogicalToPhysical->bufferSize = mConstantDefs->floatBufferSize;
  169. mIntLogicalToPhysical->bufferSize = mConstantDefs->intBufferSize;
  170. mFloatLogicalToPhysical->map.clear();
  171. mIntLogicalToPhysical->map.clear();
  172. // need to set up logical mappings too for some rendersystems
  173. for (GpuConstantDefinitionMap::const_iterator i = mConstantDefs->map.begin();
  174. i != mConstantDefs->map.end(); ++i)
  175. {
  176. const String& name = i->first;
  177. const GpuConstantDefinition& def = i->second;
  178. // only consider non-array entries
  179. if (name.find("[") == String::npos)
  180. {
  181. GpuLogicalIndexUseMap::value_type val(def.logicalIndex,
  182. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, def.variability));
  183. if (def.isFloat())
  184. {
  185. mFloatLogicalToPhysical->map.insert(val);
  186. }
  187. else
  188. {
  189. mIntLogicalToPhysical->map.insert(val);
  190. }
  191. }
  192. }
  193. }
  194. //-----------------------------------------------------------------------------
  195. GpuProgramParametersSharedPtr GpuProgram::createParameters(void)
  196. {
  197. // Default implementation simply returns standard parameters.
  198. GpuProgramParametersSharedPtr ret = GpuProgramParametersSharedPtr(OGRE_NEW GpuProgramParameters());
  199. // set up named parameters, if any
  200. if (!mConstantDefs.isNull() && !mConstantDefs->map.empty())
  201. {
  202. ret->_setNamedConstants(mConstantDefs);
  203. }
  204. // link shared logical / physical map for low-level use
  205. ret->_setLogicalIndexes(mFloatLogicalToPhysical, mIntLogicalToPhysical);
  206. // Copy in default parameters if present
  207. if (!mDefaultParams.isNull())
  208. ret->copyConstantsFrom(*(mDefaultParams.get()));
  209. return ret;
  210. }
  211. //-----------------------------------------------------------------------------
  212. GpuProgramParametersSharedPtr GpuProgram::getDefaultParameters(void)
  213. {
  214. if (mDefaultParams.isNull())
  215. {
  216. mDefaultParams = createParameters();
  217. }
  218. return mDefaultParams;
  219. }
  220. //-----------------------------------------------------------------------------
  221. void GpuProgram::setupBaseParamDictionary(void)
  222. {
  223. // TODO PORT - I'm not really sure what this will do and will it be needed in my port, but its calling a method from Resource, and we don't inherit from it
  224. // ParamDictionary* dict = getParamDictionary();
  225. // dict->addParameter(
  226. // ParameterDef("type", "'vertex_program', 'geometry_program' or 'fragment_program'",
  227. // PT_STRING), &msTypeCmd);
  228. // dict->addParameter(
  229. // ParameterDef("syntax", "Syntax code, e.g. vs_1_1", PT_STRING), &msSyntaxCmd);
  230. // dict->addParameter(
  231. // ParameterDef("includes_skeletal_animation",
  232. // "Whether this vertex program includes skeletal animation", PT_BOOL),
  233. // &msSkeletalCmd);
  234. //dict->addParameter(
  235. // ParameterDef("includes_morph_animation",
  236. // "Whether this vertex program includes morph animation", PT_BOOL),
  237. // &msMorphCmd);
  238. //dict->addParameter(
  239. // ParameterDef("includes_pose_animation",
  240. // "The number of poses this vertex program supports for pose animation", PT_INT),
  241. // &msPoseCmd);
  242. //dict->addParameter(
  243. // ParameterDef("uses_vertex_texture_fetch",
  244. // "Whether this vertex program requires vertex texture fetch support.", PT_BOOL),
  245. // &msVTFCmd);
  246. //dict->addParameter(
  247. // ParameterDef("manual_named_constants",
  248. // "File containing named parameter mappings for low-level programs.", PT_BOOL),
  249. // &msManNamedConstsFileCmd);
  250. //dict->addParameter(
  251. // ParameterDef("uses_adjacency_information",
  252. // "Whether this geometry program requires adjacency information from the input primitives.", PT_BOOL),
  253. // &msAdjacencyCmd);
  254. }
  255. //-----------------------------------------------------------------------
  256. const String& GpuProgram::getLanguage(void) const
  257. {
  258. static const String language = "asm";
  259. return language;
  260. }
  261. //-----------------------------------------------------------------------
  262. //-----------------------------------------------------------------------
  263. String GpuProgram::CmdType::doGet(const void* target) const
  264. {
  265. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  266. if (t->getType() == GPT_VERTEX_PROGRAM)
  267. {
  268. return "vertex_program";
  269. }
  270. else if (t->getType() == GPT_GEOMETRY_PROGRAM)
  271. {
  272. return "geometry_program";
  273. }
  274. else
  275. {
  276. return "fragment_program";
  277. }
  278. }
  279. void GpuProgram::CmdType::doSet(void* target, const String& val)
  280. {
  281. GpuProgram* t = static_cast<GpuProgram*>(target);
  282. if (val == "vertex_program")
  283. {
  284. t->setType(GPT_VERTEX_PROGRAM);
  285. }
  286. else if (val == "geometry_program")
  287. {
  288. t->setType(GPT_GEOMETRY_PROGRAM);
  289. }
  290. else
  291. {
  292. t->setType(GPT_FRAGMENT_PROGRAM);
  293. }
  294. }
  295. //-----------------------------------------------------------------------
  296. String GpuProgram::CmdSyntax::doGet(const void* target) const
  297. {
  298. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  299. return t->getSyntaxCode();
  300. }
  301. void GpuProgram::CmdSyntax::doSet(void* target, const String& val)
  302. {
  303. GpuProgram* t = static_cast<GpuProgram*>(target);
  304. t->setSyntaxCode(val);
  305. }
  306. //-----------------------------------------------------------------------
  307. String GpuProgram::CmdSkeletal::doGet(const void* target) const
  308. {
  309. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  310. return StringConverter::toString(t->isSkeletalAnimationIncluded());
  311. }
  312. void GpuProgram::CmdSkeletal::doSet(void* target, const String& val)
  313. {
  314. GpuProgram* t = static_cast<GpuProgram*>(target);
  315. t->setSkeletalAnimationIncluded(StringConverter::parseBool(val));
  316. }
  317. //-----------------------------------------------------------------------
  318. String GpuProgram::CmdMorph::doGet(const void* target) const
  319. {
  320. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  321. return StringConverter::toString(t->isMorphAnimationIncluded());
  322. }
  323. void GpuProgram::CmdMorph::doSet(void* target, const String& val)
  324. {
  325. GpuProgram* t = static_cast<GpuProgram*>(target);
  326. t->setMorphAnimationIncluded(StringConverter::parseBool(val));
  327. }
  328. //-----------------------------------------------------------------------
  329. String GpuProgram::CmdPose::doGet(const void* target) const
  330. {
  331. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  332. return StringConverter::toString(t->getNumberOfPosesIncluded());
  333. }
  334. void GpuProgram::CmdPose::doSet(void* target, const String& val)
  335. {
  336. GpuProgram* t = static_cast<GpuProgram*>(target);
  337. t->setPoseAnimationIncluded((ushort)StringConverter::parseUnsignedInt(val));
  338. }
  339. //-----------------------------------------------------------------------
  340. String GpuProgram::CmdVTF::doGet(const void* target) const
  341. {
  342. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  343. return StringConverter::toString(t->isVertexTextureFetchRequired());
  344. }
  345. void GpuProgram::CmdVTF::doSet(void* target, const String& val)
  346. {
  347. GpuProgram* t = static_cast<GpuProgram*>(target);
  348. t->setVertexTextureFetchRequired(StringConverter::parseBool(val));
  349. }
  350. //-----------------------------------------------------------------------
  351. String GpuProgram::CmdManualNamedConstsFile::doGet(const void* target) const
  352. {
  353. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  354. return t->getManualNamedConstantsFile();
  355. }
  356. void GpuProgram::CmdManualNamedConstsFile::doSet(void* target, const String& val)
  357. {
  358. GpuProgram* t = static_cast<GpuProgram*>(target);
  359. t->setManualNamedConstantsFile(val);
  360. }
  361. //-----------------------------------------------------------------------
  362. String GpuProgram::CmdAdjacency::doGet(const void* target) const
  363. {
  364. const GpuProgram* t = static_cast<const GpuProgram*>(target);
  365. return StringConverter::toString(t->isAdjacencyInfoRequired());
  366. }
  367. void GpuProgram::CmdAdjacency::doSet(void* target, const String& val)
  368. {
  369. GpuProgram* t = static_cast<GpuProgram*>(target);
  370. t->setAdjacencyInfoRequired(StringConverter::parseBool(val));
  371. }
  372. //-----------------------------------------------------------------------
  373. GpuProgramPtr& GpuProgramPtr::operator=(const HighLevelGpuProgramPtr& r)
  374. {
  375. // Can assign direct
  376. if (pRep == r.getPointer())
  377. return *this;
  378. release();
  379. // lock & copy other mutex pointer
  380. OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
  381. {
  382. OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
  383. OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
  384. pRep = r.getPointer();
  385. pUseCount = r.useCountPointer();
  386. if (pUseCount)
  387. {
  388. ++(*pUseCount);
  389. }
  390. }
  391. else
  392. {
  393. // RHS must be a null pointer
  394. assert(r.isNull() && "RHS must be null if it has no mutex!");
  395. setNull();
  396. }
  397. return *this;
  398. }
  399. }