CmHighLevelGpuProgramManager.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "CmHighLevelGpuProgramManager.h"
  25. #include "CmRenderSystem.h"
  26. namespace CamelotEngine {
  27. String sNullLang = "null";
  28. class NullProgram : public HighLevelGpuProgram
  29. {
  30. protected:
  31. /** Internal load implementation, must be implemented by subclasses.
  32. */
  33. void loadFromSource(void) {}
  34. /// Populate the passed parameters with name->index map, must be overridden
  35. void populateParameterNames(GpuProgramParametersSharedPtr params)
  36. {
  37. // Skip the normal implementation
  38. // Ensure we don't complain about missing parameter names
  39. params->setIgnoreMissingParams(true);
  40. }
  41. void buildConstantDefinitions() const
  42. {
  43. // do nothing
  44. }
  45. public:
  46. NullProgram()
  47. : HighLevelGpuProgram("", "", "", GPT_VERTEX_PROGRAM, GPP_NONE){}
  48. ~NullProgram() {}
  49. /// Overridden from GpuProgram - never supported
  50. bool isSupported(void) const { return false; }
  51. /// Overridden from GpuProgram
  52. const String& getLanguage(void) const { return sNullLang; }
  53. /// Overridden from StringInterface
  54. bool setParameter(const String& name, const String& value)
  55. {
  56. // always silently ignore all parameters so as not to report errors on
  57. // unsupported platforms
  58. return true;
  59. }
  60. };
  61. class NullProgramFactory : public HighLevelGpuProgramFactory
  62. {
  63. public:
  64. NullProgramFactory() {}
  65. ~NullProgramFactory() {}
  66. /// Get the name of the language this factory creates programs for
  67. const String& getLanguage(void) const
  68. {
  69. return sNullLang;
  70. }
  71. HighLevelGpuProgram* create(const String& source, const String& entryPoint, GpuProgramType gptype, GpuProgramProfile profile)
  72. {
  73. return new NullProgram();
  74. }
  75. HighLevelGpuProgram* create()
  76. {
  77. return new NullProgram();
  78. }
  79. void destroy_internal(HighLevelGpuProgram* prog)
  80. {
  81. delete prog;
  82. }
  83. };
  84. //-----------------------------------------------------------------------
  85. HighLevelGpuProgramManager::HighLevelGpuProgramManager()
  86. {
  87. mNullFactory = new NullProgramFactory();
  88. addFactory(mNullFactory);
  89. }
  90. //-----------------------------------------------------------------------
  91. HighLevelGpuProgramManager::~HighLevelGpuProgramManager()
  92. {
  93. delete mNullFactory;
  94. }
  95. //---------------------------------------------------------------------------
  96. void HighLevelGpuProgramManager::addFactory(HighLevelGpuProgramFactory* factory)
  97. {
  98. // deliberately allow later plugins to override earlier ones
  99. mFactories[factory->getLanguage()] = factory;
  100. }
  101. //---------------------------------------------------------------------------
  102. void HighLevelGpuProgramManager::removeFactory(HighLevelGpuProgramFactory* factory)
  103. {
  104. // Remove only if equal to registered one, since it might overridden
  105. // by other plugins
  106. FactoryMap::iterator it = mFactories.find(factory->getLanguage());
  107. if (it != mFactories.end() && it->second == factory)
  108. {
  109. mFactories.erase(it);
  110. }
  111. }
  112. //---------------------------------------------------------------------------
  113. HighLevelGpuProgramFactory* HighLevelGpuProgramManager::getFactory(const String& language)
  114. {
  115. FactoryMap::iterator i = mFactories.find(language);
  116. if (i == mFactories.end())
  117. {
  118. // use the null factory to create programs that will never be supported
  119. i = mFactories.find(sNullLang);
  120. }
  121. return i->second;
  122. }
  123. //---------------------------------------------------------------------------
  124. void HighLevelGpuProgramFactory::destroy(HighLevelGpuProgram* prog)
  125. {
  126. RenderSystem::instancePtr()->queueCommand(boost::bind(&HighLevelGpuProgramFactory::destroy_internal, this, prog));
  127. }
  128. //---------------------------------------------------------------------
  129. bool HighLevelGpuProgramManager::isLanguageSupported(const String& lang)
  130. {
  131. FactoryMap::iterator i = mFactories.find(lang);
  132. return i != mFactories.end();
  133. }
  134. //---------------------------------------------------------------------------
  135. HighLevelGpuProgramPtr HighLevelGpuProgramManager::create(const String& source, const String& entryPoint, const String& language,
  136. GpuProgramType gptype, GpuProgramProfile profile, bool internalCall)
  137. {
  138. HighLevelGpuProgramFactory* factory = getFactory(language);
  139. HighLevelGpuProgramPtr ret = HighLevelGpuProgramPtr(factory->create(source, entryPoint, gptype, profile), boost::bind(&HighLevelGpuProgramFactory::destroy, factory, _1));
  140. ret->initialize(internalCall);
  141. return ret;
  142. }
  143. //---------------------------------------------------------------------------
  144. HighLevelGpuProgramPtr HighLevelGpuProgramManager::create(const String& language, bool internalCall)
  145. {
  146. HighLevelGpuProgramFactory* factory = getFactory(language);
  147. HighLevelGpuProgramPtr ret = HighLevelGpuProgramPtr(factory->create(), boost::bind(&HighLevelGpuProgramFactory::destroy, factory, _1));
  148. ret->initialize(internalCall);
  149. return ret;
  150. }
  151. //---------------------------------------------------------------------------
  152. HighLevelGpuProgramPtr HighLevelGpuProgramManager::createEmpty(const String& language)
  153. {
  154. HighLevelGpuProgramFactory* factory = getFactory(language);
  155. HighLevelGpuProgramPtr ret = HighLevelGpuProgramPtr(factory->create(), boost::bind(&HighLevelGpuProgramFactory::destroy, factory, _1));
  156. return ret;
  157. }
  158. //---------------------------------------------------------------------------
  159. HighLevelGpuProgramFactory::~HighLevelGpuProgramFactory()
  160. {
  161. }
  162. }