CmHighLevelGpuProgramManager.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. namespace CamelotEngine {
  26. String sNullLang = "null";
  27. class NullProgram : public HighLevelGpuProgram
  28. {
  29. protected:
  30. /** Internal load implementation, must be implemented by subclasses.
  31. */
  32. void loadFromSource(void) {}
  33. /** Internal method for creating an appropriate low-level program from this
  34. high-level program, must be implemented by subclasses. */
  35. void createLowLevelImpl(void) {}
  36. /// Internal unload implementation, must be implemented by subclasses
  37. void unloadHighLevelImpl(void) {}
  38. /// Populate the passed parameters with name->index map, must be overridden
  39. void populateParameterNames(GpuProgramParametersSharedPtr params)
  40. {
  41. // Skip the normal implementation
  42. // Ensure we don't complain about missing parameter names
  43. params->setIgnoreMissingParams(true);
  44. }
  45. void buildConstantDefinitions() const
  46. {
  47. // do nothing
  48. }
  49. public:
  50. NullProgram()
  51. : HighLevelGpuProgram(){}
  52. ~NullProgram() {}
  53. /// Overridden from GpuProgram - never supported
  54. bool isSupported(void) const { return false; }
  55. /// Overridden from GpuProgram
  56. const String& getLanguage(void) const { return sNullLang; }
  57. /// Overridden from StringInterface
  58. bool setParameter(const String& name, const String& value)
  59. {
  60. // always silently ignore all parameters so as not to report errors on
  61. // unsupported platforms
  62. return true;
  63. }
  64. };
  65. class NullProgramFactory : public HighLevelGpuProgramFactory
  66. {
  67. public:
  68. NullProgramFactory() {}
  69. ~NullProgramFactory() {}
  70. /// Get the name of the language this factory creates programs for
  71. const String& getLanguage(void) const
  72. {
  73. return sNullLang;
  74. }
  75. HighLevelGpuProgram* create(const String& source, const String& entryPoint, GpuProgramProfile profile)
  76. {
  77. return new NullProgram();
  78. }
  79. void destroy(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. bool HighLevelGpuProgramManager::isLanguageSupported(const String& lang)
  125. {
  126. FactoryMap::iterator i = mFactories.find(lang);
  127. return i != mFactories.end();
  128. }
  129. //---------------------------------------------------------------------------
  130. HighLevelGpuProgramPtr HighLevelGpuProgramManager::createProgram(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype, GpuProgramProfile profile)
  131. {
  132. HighLevelGpuProgramPtr ret = HighLevelGpuProgramPtr(getFactory(language)->create(source, entryPoint, profile));
  133. HighLevelGpuProgramPtr prg = ret;
  134. prg->setType(gptype);
  135. prg->setSyntaxCode(language);
  136. return prg;
  137. }
  138. //---------------------------------------------------------------------------
  139. HighLevelGpuProgramFactory::~HighLevelGpuProgramFactory()
  140. {
  141. }
  142. }