BsShader.cpp 8.8 KB

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