OgreGpuProgram.cpp 16 KB

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