BsGpuProgram.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "BsGpuProgram.h"
  2. #include "BsVector3.h"
  3. #include "BsVector4.h"
  4. #include "BsRenderSystemCapabilities.h"
  5. #include "BsException.h"
  6. #include "BsRenderSystem.h"
  7. #include "BsAsyncOp.h"
  8. #include "BsGpuParams.h"
  9. #include "BsGpuProgInclude.h"
  10. #include "BsGpuProgramManager.h"
  11. #include "BsResources.h"
  12. #include "BsGpuProgramRTTI.h"
  13. namespace BansheeEngine
  14. {
  15. GpuProgramProperties::GpuProgramProperties(const String& source, const String& entryPoint,
  16. GpuProgramType gptype, GpuProgramProfile profile)
  17. :mSource(source), mEntryPoint(entryPoint), mType(gptype), mProfile(profile)
  18. { }
  19. GpuProgramCore::GpuProgramCore(const String& source, const String& entryPoint,
  20. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  21. : mProperties(source, entryPoint, gptype, profile), mIsCompiled(false),
  22. mNeedsAdjacencyInfo(isAdjacencyInfoRequired)
  23. {
  24. mParametersDesc = bs_shared_ptr<GpuParamDesc>();
  25. }
  26. bool GpuProgramCore::isSupported() const
  27. {
  28. if (!isRequiredCapabilitiesSupported())
  29. return false;
  30. RenderSystem* rs = BansheeEngine::RenderSystem::instancePtr();
  31. String profile = rs->getCapabilities()->gpuProgProfileToRSSpecificProfile(getProperties().getProfile());
  32. return rs->getCapabilities()->isShaderProfileSupported(profile);
  33. }
  34. bool GpuProgramCore::isRequiredCapabilitiesSupported() const
  35. {
  36. return true;
  37. }
  38. SPtr<GpuParamsCore> GpuProgramCore::createParameters()
  39. {
  40. return GpuParamsCore::create(mParametersDesc, false);
  41. }
  42. GpuProgram::GpuProgram(const String& source, const String& entryPoint, const String& language,
  43. GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  44. : mProperties(mergeWithIncludes(source, includes), entryPoint, gptype, profile), mLanguage(language),
  45. mNeedsAdjacencyInfo(isAdjacencyInfoRequired)
  46. {
  47. }
  48. bool GpuProgram::isCompiled() const
  49. {
  50. return getCore()->isCompiled();
  51. }
  52. String GpuProgram::getCompileErrorMessage() const
  53. {
  54. return getCore()->getCompileErrorMessage();
  55. }
  56. GpuParamsPtr GpuProgram::createParameters()
  57. {
  58. return GpuParams::create(getCore()->getParamDesc(), getCore()->hasColumnMajorMatrices());
  59. }
  60. GpuParamDescPtr GpuProgram::getParamDesc() const
  61. {
  62. return getCore()->getParamDesc();
  63. }
  64. SPtr<GpuProgramCore> GpuProgram::getCore() const
  65. {
  66. return std::static_pointer_cast<GpuProgramCore>(mCoreSpecific);
  67. }
  68. SPtr<CoreObjectCore> GpuProgram::createCore() const
  69. {
  70. return GpuProgramCoreManager::instance().createInternal(mProperties.getSource(), mProperties.getEntryPoint(),
  71. mLanguage, mProperties.getType(), mProperties.getProfile(), mNeedsAdjacencyInfo);
  72. }
  73. String GpuProgram::mergeWithIncludes(const String& source, const Vector<HGpuProgInclude>* includes)
  74. {
  75. if (includes != nullptr)
  76. {
  77. StringStream stringStream;
  78. for (auto iter = includes->begin(); iter != includes->end(); ++iter)
  79. {
  80. if (*iter != nullptr)
  81. {
  82. stringStream << (*iter)->getString();
  83. }
  84. }
  85. stringStream << source;
  86. return stringStream.str();
  87. }
  88. else
  89. {
  90. return source;
  91. }
  92. }
  93. HGpuProgram GpuProgram::create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  94. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool requiresAdjacency)
  95. {
  96. GpuProgramPtr programPtr = _createPtr(source, entryPoint, language, gptype, profile, includes, requiresAdjacency);
  97. return static_resource_cast<GpuProgram>(gResources()._createResourceHandle(programPtr));
  98. }
  99. GpuProgramPtr GpuProgram::_createPtr(const String& source, const String& entryPoint,
  100. const String& language, GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool requiresAdjacency)
  101. {
  102. return GpuProgramManager::instance().create(source, entryPoint, language, gptype, profile, includes, requiresAdjacency);
  103. }
  104. /************************************************************************/
  105. /* SERIALIZATION */
  106. /************************************************************************/
  107. RTTITypeBase* GpuProgram::getRTTIStatic()
  108. {
  109. return GpuProgramRTTI::instance();
  110. }
  111. RTTITypeBase* GpuProgram::getRTTI() const
  112. {
  113. return GpuProgram::getRTTIStatic();
  114. }
  115. }