BsShader.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "BsShader.h"
  2. #include "BsTechnique.h"
  3. #include "BsException.h"
  4. #include "BsDebug.h"
  5. #include "BsShaderRTTI.h"
  6. #include "BsResources.h"
  7. #include "BsFrameAlloc.h"
  8. #include "BsPass.h"
  9. namespace BansheeEngine
  10. {
  11. SHADER_DESC::SHADER_DESC()
  12. :queuePriority(0), queueSortType(QueueSortType::None), separablePasses(false)
  13. {
  14. }
  15. void SHADER_DESC::addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type,
  16. StringID rendererSemantic, UINT32 arraySize, UINT32 elementSize)
  17. {
  18. if(type == GPDT_STRUCT && elementSize <= 0)
  19. BS_EXCEPT(InvalidParametersException, "You need to provide a non-zero element size for a struct parameter.")
  20. SHADER_DATA_PARAM_DESC desc;
  21. desc.name = name;
  22. desc.gpuVariableName = gpuVariableName;
  23. desc.type = type;
  24. desc.arraySize = arraySize;
  25. desc.rendererSemantic = rendererSemantic;
  26. desc.elementSize = elementSize;
  27. dataParams[name] = desc;
  28. objectParams.erase(name);
  29. }
  30. void SHADER_DESC::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic)
  31. {
  32. auto iterFind = objectParams.find(name);
  33. if (iterFind == objectParams.end())
  34. {
  35. SHADER_OBJECT_PARAM_DESC desc;
  36. desc.name = name;
  37. desc.type = type;
  38. desc.rendererSemantic = rendererSemantic;
  39. desc.gpuVariableNames.push_back(gpuVariableName);
  40. objectParams[name] = desc;
  41. }
  42. else
  43. {
  44. SHADER_OBJECT_PARAM_DESC& desc = iterFind->second;
  45. if (desc.type != type || desc.rendererSemantic != rendererSemantic)
  46. BS_EXCEPT(InvalidParametersException, "Shader parameter with the name \"" + name + "\" already exists with different properties.");
  47. Vector<String>& gpuVariableNames = desc.gpuVariableNames;
  48. bool found = false;
  49. for (UINT32 i = 0; i < (UINT32)gpuVariableNames.size(); i++)
  50. {
  51. if (gpuVariableNames[i] == gpuVariableName)
  52. {
  53. found = true;
  54. break;
  55. }
  56. }
  57. if (!found)
  58. gpuVariableNames.push_back(gpuVariableName);
  59. }
  60. dataParams.erase(name);
  61. }
  62. void SHADER_DESC::setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, StringID rendererSemantic)
  63. {
  64. SHADER_PARAM_BLOCK_DESC desc;
  65. desc.name = name;
  66. desc.shared = shared;
  67. desc.usage = usage;
  68. desc.rendererSemantic = rendererSemantic;
  69. paramBlocks[name] = desc;
  70. }
  71. ShaderBase::ShaderBase(const String& name, const SHADER_DESC& desc)
  72. :mName(name), mDesc(desc)
  73. {
  74. }
  75. GpuParamType ShaderBase::getParamType(const String& name) const
  76. {
  77. auto findIterData = mDesc.dataParams.find(name);
  78. if (findIterData != mDesc.dataParams.end())
  79. return GPT_DATA;
  80. auto findIterObject = mDesc.objectParams.find(name);
  81. if (findIterObject != mDesc.objectParams.end())
  82. return GPT_OBJECT;
  83. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  84. }
  85. const SHADER_DATA_PARAM_DESC& ShaderBase::getDataParamDesc(const String& name) const
  86. {
  87. auto findIterData = mDesc.dataParams.find(name);
  88. if (findIterData != mDesc.dataParams.end())
  89. return findIterData->second;
  90. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  91. }
  92. const SHADER_OBJECT_PARAM_DESC& ShaderBase::getObjectParamDesc(const String& name) const
  93. {
  94. auto findIterObject = mDesc.objectParams.find(name);
  95. if (findIterObject != mDesc.objectParams.end())
  96. return findIterObject->second;
  97. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  98. }
  99. bool ShaderBase::hasDataParam(const String& name) const
  100. {
  101. auto findIterData = mDesc.dataParams.find(name);
  102. if (findIterData != mDesc.dataParams.end())
  103. return true;
  104. return false;
  105. }
  106. bool ShaderBase::hasObjectParam(const String& name) const
  107. {
  108. auto findIterObject = mDesc.objectParams.find(name);
  109. if (findIterObject != mDesc.objectParams.end())
  110. return true;
  111. return false;
  112. }
  113. template<bool Core>
  114. TShader<Core>::TShader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueType>>& techniques)
  115. :ShaderBase(name, desc), mTechniques(techniques)
  116. { }
  117. template<bool Core>
  118. TShader<Core>::~TShader()
  119. { }
  120. template<bool Core>
  121. SPtr<typename TShader<Core>::TechniqueType> TShader<Core>::getBestTechnique() const
  122. {
  123. for (auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  124. {
  125. if ((*iter)->isSupported())
  126. {
  127. return *iter;
  128. }
  129. }
  130. return nullptr;
  131. // TODO - Low priority. Instead of returning null use an extremely simple technique that will be supported almost everywhere as a fallback.
  132. }
  133. template class TShader < false > ;
  134. template class TShader < true >;
  135. ShaderCore::ShaderCore(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  136. :TShader(name, desc, techniques)
  137. {
  138. }
  139. SPtr<ShaderCore> ShaderCore::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  140. {
  141. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(name, desc, techniques);
  142. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore, GenAlloc>(shaderCore);
  143. shaderCorePtr->_setThisPtr(shaderCorePtr);
  144. shaderCorePtr->initialize();
  145. return shaderCorePtr;
  146. }
  147. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  148. :TShader(name, desc, techniques)
  149. {
  150. }
  151. SPtr<ShaderCore> Shader::getCore() const
  152. {
  153. return std::static_pointer_cast<ShaderCore>(mCoreSpecific);
  154. }
  155. SPtr<CoreObjectCore> Shader::createCore() const
  156. {
  157. Vector<SPtr<TechniqueCore>> techniques;
  158. for (auto& technique : mTechniques)
  159. techniques.push_back(technique->getCore());
  160. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(mName, mDesc, techniques);
  161. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore, GenAlloc>(shaderCore);
  162. shaderCorePtr->_setThisPtr(shaderCorePtr);
  163. return shaderCorePtr;
  164. }
  165. void Shader::getCoreDependencies(Vector<SPtr<CoreObject>>& dependencies)
  166. {
  167. for (auto& technique : mTechniques)
  168. dependencies.push_back(technique);
  169. }
  170. bool Shader::isSampler(GpuParamObjectType type)
  171. {
  172. switch(type)
  173. {
  174. case GPOT_SAMPLER1D:
  175. case GPOT_SAMPLER2D:
  176. case GPOT_SAMPLER3D:
  177. case GPOT_SAMPLERCUBE:
  178. case GPOT_SAMPLER2DMS:
  179. return true;
  180. }
  181. return false;
  182. }
  183. bool Shader::isTexture(GpuParamObjectType type)
  184. {
  185. switch(type)
  186. {
  187. case GPOT_TEXTURE1D:
  188. case GPOT_TEXTURE2D:
  189. case GPOT_TEXTURE3D:
  190. case GPOT_TEXTURECUBE:
  191. case GPOT_TEXTURE2DMS:
  192. return true;
  193. }
  194. return false;
  195. }
  196. bool Shader::isBuffer(GpuParamObjectType type)
  197. {
  198. switch(type)
  199. {
  200. case GPOT_BYTE_BUFFER:
  201. case GPOT_STRUCTURED_BUFFER:
  202. case GPOT_RWBYTE_BUFFER:
  203. case GPOT_RWAPPEND_BUFFER:
  204. case GPOT_RWCONSUME_BUFFER:
  205. case GPOT_RWSTRUCTURED_BUFFER:
  206. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  207. case GPOT_RWTYPED_BUFFER:
  208. return true;
  209. }
  210. return false;
  211. }
  212. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  213. {
  214. ShaderPtr newShader = _createPtr(name, desc, techniques);
  215. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  216. }
  217. ShaderPtr Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  218. {
  219. ShaderPtr newShader = bs_core_ptr<Shader, PoolAlloc>(new (bs_alloc<Shader, PoolAlloc>()) Shader(name, desc, techniques));
  220. newShader->_setThisPtr(newShader);
  221. newShader->initialize();
  222. return newShader;
  223. }
  224. ShaderPtr Shader::createEmpty()
  225. {
  226. ShaderPtr newShader = bs_core_ptr<Shader, PoolAlloc>(new (bs_alloc<Shader, PoolAlloc>()) Shader());
  227. newShader->_setThisPtr(newShader);
  228. return newShader;
  229. }
  230. RTTITypeBase* Shader::getRTTIStatic()
  231. {
  232. return ShaderRTTI::instance();
  233. }
  234. RTTITypeBase* Shader::getRTTI() const
  235. {
  236. return Shader::getRTTIStatic();
  237. }
  238. }