2
0

BsShader.cpp 8.8 KB

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