BsGpuProgramManager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsGpuProgramManager.h"
  5. #include "BsRenderSystem.h"
  6. namespace BansheeEngine
  7. {
  8. String sNullLang = "null";
  9. /**
  10. * @brief Null GPU program used in place of GPU programs we cannot create.
  11. * Null programs don't do anything.
  12. */
  13. class NullProgram : public GpuProgram
  14. {
  15. public:
  16. NullProgram()
  17. :GpuProgram("", "", GPT_VERTEX_PROGRAM, GPP_NONE, nullptr)
  18. { }
  19. ~NullProgram() { }
  20. bool isSupported() const { return false; }
  21. const String& getLanguage() const { return sNullLang; }
  22. protected:
  23. void loadFromSource() {}
  24. void buildConstantDefinitions() const
  25. { }
  26. };
  27. /**
  28. * @brief Factory that creates null GPU programs.
  29. */
  30. class NullProgramFactory : public GpuProgramFactory
  31. {
  32. public:
  33. NullProgramFactory() {}
  34. ~NullProgramFactory() {}
  35. const String& getLanguage() const
  36. {
  37. return sNullLang;
  38. }
  39. GpuProgramPtr create(const String& source, const String& entryPoint, GpuProgramType gptype,
  40. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool requiresAdjacencyInformation)
  41. {
  42. return bs_core_ptr<NullProgram, PoolAlloc>();
  43. }
  44. GpuProgramPtr create(GpuProgramType type)
  45. {
  46. return bs_core_ptr<NullProgram, PoolAlloc>();
  47. }
  48. };
  49. GpuProgramManager::GpuProgramManager()
  50. {
  51. mNullFactory = bs_new<NullProgramFactory>();
  52. addFactory(mNullFactory);
  53. }
  54. GpuProgramManager::~GpuProgramManager()
  55. {
  56. bs_delete((NullProgramFactory*)mNullFactory);
  57. }
  58. void GpuProgramManager::addFactory(GpuProgramFactory* factory)
  59. {
  60. mFactories[factory->getLanguage()] = factory;
  61. }
  62. void GpuProgramManager::removeFactory(GpuProgramFactory* factory)
  63. {
  64. FactoryMap::iterator it = mFactories.find(factory->getLanguage());
  65. if (it != mFactories.end() && it->second == factory)
  66. {
  67. mFactories.erase(it);
  68. }
  69. }
  70. GpuProgramFactory* GpuProgramManager::getFactory(const String& language)
  71. {
  72. FactoryMap::iterator i = mFactories.find(language);
  73. if (i == mFactories.end())
  74. i = mFactories.find(sNullLang);
  75. return i->second;
  76. }
  77. bool GpuProgramManager::isLanguageSupported(const String& lang)
  78. {
  79. FactoryMap::iterator i = mFactories.find(lang);
  80. return i != mFactories.end();
  81. }
  82. GpuProgramPtr GpuProgramManager::create(const String& source, const String& entryPoint, const String& language,
  83. GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes,
  84. bool requiresAdjacencyInformation)
  85. {
  86. GpuProgramFactory* factory = getFactory(language);
  87. GpuProgramPtr ret = factory->create(source, entryPoint, gptype, profile, includes, requiresAdjacencyInformation);
  88. ret->_setThisPtr(ret);
  89. ret->initialize();
  90. return ret;
  91. }
  92. GpuProgramPtr GpuProgramManager::createEmpty(const String& language, GpuProgramType type)
  93. {
  94. GpuProgramFactory* factory = getFactory(language);
  95. GpuProgramPtr ret = factory->create(type);
  96. ret->_setThisPtr(ret);
  97. return ret;
  98. }
  99. GpuProgramFactory::~GpuProgramFactory()
  100. {
  101. }
  102. }