BsShader.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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::getResourceDependencies(Vector<HResource>& dependencies) const
  166. {
  167. TechniquePtr bestTechnique = getBestTechnique();
  168. if (bestTechnique == nullptr) // No valid technique
  169. return;
  170. UINT32 numPasses = bestTechnique->getNumPasses();
  171. for (UINT32 i = 0; i < numPasses; i++)
  172. {
  173. PassPtr pass = bestTechnique->getPass(i);
  174. HGpuProgram vertProg = pass->getVertexProgram();
  175. if (vertProg != nullptr)
  176. dependencies.push_back(vertProg);
  177. HGpuProgram fragProg = pass->getFragmentProgram();
  178. if (fragProg != nullptr)
  179. dependencies.push_back(fragProg);
  180. HGpuProgram geomProg = pass->getGeometryProgram();
  181. if (geomProg != nullptr)
  182. dependencies.push_back(geomProg);
  183. HGpuProgram domProg = pass->getDomainProgram();
  184. if (domProg != nullptr)
  185. dependencies.push_back(domProg);
  186. HGpuProgram hullProg = pass->getHullProgram();
  187. if (hullProg != nullptr)
  188. dependencies.push_back(hullProg);
  189. HGpuProgram computeProg = pass->getComputeProgram();
  190. if (computeProg != nullptr)
  191. dependencies.push_back(computeProg);
  192. HBlendState blendState = pass->getBlendState();
  193. if (blendState != nullptr)
  194. dependencies.push_back(blendState);
  195. HRasterizerState rasterizerState = pass->getRasterizerState();
  196. if (rasterizerState != nullptr)
  197. dependencies.push_back(rasterizerState);
  198. HDepthStencilState depthStencilState = pass->getDepthStencilState();
  199. if (depthStencilState != nullptr)
  200. dependencies.push_back(depthStencilState);
  201. }
  202. }
  203. void Shader::getCoreDependencies(Vector<SPtr<CoreObject>>& dependencies)
  204. {
  205. for (auto& technique : mTechniques)
  206. dependencies.push_back(technique);
  207. }
  208. bool Shader::isSampler(GpuParamObjectType type)
  209. {
  210. switch(type)
  211. {
  212. case GPOT_SAMPLER1D:
  213. case GPOT_SAMPLER2D:
  214. case GPOT_SAMPLER3D:
  215. case GPOT_SAMPLERCUBE:
  216. case GPOT_SAMPLER2DMS:
  217. return true;
  218. }
  219. return false;
  220. }
  221. bool Shader::isTexture(GpuParamObjectType type)
  222. {
  223. switch(type)
  224. {
  225. case GPOT_TEXTURE1D:
  226. case GPOT_TEXTURE2D:
  227. case GPOT_TEXTURE3D:
  228. case GPOT_TEXTURECUBE:
  229. case GPOT_TEXTURE2DMS:
  230. return true;
  231. }
  232. return false;
  233. }
  234. bool Shader::isBuffer(GpuParamObjectType type)
  235. {
  236. switch(type)
  237. {
  238. case GPOT_BYTE_BUFFER:
  239. case GPOT_STRUCTURED_BUFFER:
  240. case GPOT_RWBYTE_BUFFER:
  241. case GPOT_RWAPPEND_BUFFER:
  242. case GPOT_RWCONSUME_BUFFER:
  243. case GPOT_RWSTRUCTURED_BUFFER:
  244. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  245. case GPOT_RWTYPED_BUFFER:
  246. return true;
  247. }
  248. return false;
  249. }
  250. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  251. {
  252. ShaderPtr newShader = _createPtr(name, desc, techniques);
  253. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  254. }
  255. ShaderPtr Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  256. {
  257. ShaderPtr newShader = bs_core_ptr<Shader, PoolAlloc>(new (bs_alloc<Shader, PoolAlloc>()) Shader(name, desc, techniques));
  258. newShader->_setThisPtr(newShader);
  259. newShader->initialize();
  260. return newShader;
  261. }
  262. ShaderPtr Shader::createEmpty()
  263. {
  264. ShaderPtr newShader = bs_core_ptr<Shader, PoolAlloc>(new (bs_alloc<Shader, PoolAlloc>()) Shader());
  265. newShader->_setThisPtr(newShader);
  266. return newShader;
  267. }
  268. RTTITypeBase* Shader::getRTTIStatic()
  269. {
  270. return ShaderRTTI::instance();
  271. }
  272. RTTITypeBase* Shader::getRTTI() const
  273. {
  274. return Shader::getRTTIStatic();
  275. }
  276. }