BsShader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsShader.h"
  4. #include "BsTechnique.h"
  5. #include "BsException.h"
  6. #include "BsDebug.h"
  7. #include "BsShaderRTTI.h"
  8. #include "BsResources.h"
  9. #include "BsGpuParams.h"
  10. #include "BsFrameAlloc.h"
  11. #include "BsPass.h"
  12. #include "BsSamplerState.h"
  13. namespace BansheeEngine
  14. {
  15. std::atomic<UINT32> ShaderCore::mNextShaderId = 0;
  16. template<bool Core>
  17. TSHADER_DESC<Core>::TSHADER_DESC()
  18. :queuePriority(0), queueSortType(QueueSortType::None), separablePasses(false), flags(0)
  19. {
  20. }
  21. template<bool Core>
  22. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type,
  23. StringID rendererSemantic, UINT32 arraySize, UINT32 elementSize, UINT8* defaultValue)
  24. {
  25. if(type == GPDT_STRUCT && elementSize <= 0)
  26. BS_EXCEPT(InvalidParametersException, "You need to provide a non-zero element size for a struct parameter.")
  27. SHADER_DATA_PARAM_DESC desc;
  28. desc.name = name;
  29. desc.gpuVariableName = gpuVariableName;
  30. desc.type = type;
  31. desc.arraySize = arraySize;
  32. desc.rendererSemantic = rendererSemantic;
  33. desc.elementSize = elementSize;
  34. dataParams[name] = desc;
  35. if (defaultValue != nullptr)
  36. {
  37. desc.defaultValueIdx = (UINT32)dataDefaultValues.size();
  38. UINT32 defaultValueSize = Shader::getDataParamSize(type);
  39. dataDefaultValues.resize(desc.defaultValueIdx + defaultValueSize);
  40. memcpy(&dataDefaultValues[desc.defaultValueIdx], defaultValue, defaultValueSize);
  41. }
  42. else
  43. desc.defaultValueIdx = (UINT32)-1;
  44. }
  45. template<bool Core>
  46. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic)
  47. {
  48. UINT32 defaultValueIdx = (UINT32)-1;
  49. addParameterInternal(name, gpuVariableName, type, rendererSemantic, defaultValueIdx);
  50. }
  51. template<bool Core>
  52. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, const SamplerStateType& defaultValue, StringID rendererSemantic)
  53. {
  54. UINT32 defaultValueIdx = (UINT32)-1;
  55. if (Shader::isSampler(type) && defaultValue != nullptr)
  56. {
  57. defaultValueIdx = (UINT32)samplerDefaultValues.size();
  58. samplerDefaultValues.push_back(defaultValue);
  59. }
  60. addParameterInternal(name, gpuVariableName, type, rendererSemantic, defaultValueIdx);
  61. }
  62. template<bool Core>
  63. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, const TextureType& defaultValue, StringID rendererSemantic)
  64. {
  65. UINT32 defaultValueIdx = (UINT32)-1;
  66. if (Shader::isTexture(type) && defaultValue != nullptr)
  67. {
  68. defaultValueIdx = (UINT32)textureDefaultValues.size();
  69. textureDefaultValues.push_back(defaultValue);
  70. }
  71. addParameterInternal(name, gpuVariableName, type, rendererSemantic, defaultValueIdx);
  72. }
  73. template<bool Core>
  74. void TSHADER_DESC<Core>::addParameterInternal(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic, UINT32 defaultValueIdx)
  75. {
  76. Map<String, SHADER_OBJECT_PARAM_DESC>* DEST_LOOKUP[] = { &textureParams, &bufferParams, &samplerParams };
  77. UINT32 destIdx = 0;
  78. if (Shader::isBuffer(type))
  79. destIdx = 1;
  80. else if (Shader::isSampler(type))
  81. destIdx = 2;
  82. Map<String, SHADER_OBJECT_PARAM_DESC>& paramsMap = *DEST_LOOKUP[destIdx];
  83. auto iterFind = paramsMap.find(name);
  84. if (iterFind == paramsMap.end())
  85. {
  86. SHADER_OBJECT_PARAM_DESC desc;
  87. desc.name = name;
  88. desc.type = type;
  89. desc.rendererSemantic = rendererSemantic;
  90. desc.gpuVariableNames.push_back(gpuVariableName);
  91. desc.defaultValueIdx = defaultValueIdx;
  92. paramsMap[name] = desc;
  93. }
  94. else
  95. {
  96. SHADER_OBJECT_PARAM_DESC& desc = iterFind->second;
  97. // If same name but different properties, we ignore this param
  98. if (desc.type != type || desc.rendererSemantic != rendererSemantic)
  99. return;
  100. Vector<String>& gpuVariableNames = desc.gpuVariableNames;
  101. bool found = false;
  102. for (UINT32 i = 0; i < (UINT32)gpuVariableNames.size(); i++)
  103. {
  104. if (gpuVariableNames[i] == gpuVariableName)
  105. {
  106. found = true;
  107. break;
  108. }
  109. }
  110. if (!found)
  111. gpuVariableNames.push_back(gpuVariableName);
  112. }
  113. }
  114. template<bool Core>
  115. void TSHADER_DESC<Core>::setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, StringID rendererSemantic)
  116. {
  117. SHADER_PARAM_BLOCK_DESC desc;
  118. desc.name = name;
  119. desc.shared = shared;
  120. desc.usage = usage;
  121. desc.rendererSemantic = rendererSemantic;
  122. paramBlocks[name] = desc;
  123. }
  124. template struct TSHADER_DESC<false>;
  125. template struct TSHADER_DESC<true>;
  126. template<bool Core>
  127. TShader<Core>::TShader(const String& name, const TSHADER_DESC<Core>& desc, const Vector<SPtr<TechniqueType>>& techniques, UINT32 id)
  128. :mName(name), mDesc(desc), mTechniques(techniques), mId(id)
  129. { }
  130. template<bool Core>
  131. TShader<Core>::~TShader()
  132. { }
  133. template<bool Core>
  134. GpuParamType TShader<Core>::getParamType(const String& name) const
  135. {
  136. auto findIterData = mDesc.dataParams.find(name);
  137. if (findIterData != mDesc.dataParams.end())
  138. return GPT_DATA;
  139. auto findIterTexture = mDesc.textureParams.find(name);
  140. if (findIterTexture != mDesc.textureParams.end())
  141. return GPT_TEXTURE;
  142. auto findIterBuffer = mDesc.bufferParams.find(name);
  143. if (findIterBuffer != mDesc.bufferParams.end())
  144. return GPT_BUFFER;
  145. auto findIterSampler = mDesc.samplerParams.find(name);
  146. if (findIterSampler != mDesc.samplerParams.end())
  147. return GPT_SAMPLER;
  148. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  149. }
  150. template<bool Core>
  151. const SHADER_DATA_PARAM_DESC& TShader<Core>::getDataParamDesc(const String& name) const
  152. {
  153. auto findIterData = mDesc.dataParams.find(name);
  154. if (findIterData != mDesc.dataParams.end())
  155. return findIterData->second;
  156. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  157. }
  158. template<bool Core>
  159. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getTextureParamDesc(const String& name) const
  160. {
  161. auto findIterObject = mDesc.textureParams.find(name);
  162. if (findIterObject != mDesc.textureParams.end())
  163. return findIterObject->second;
  164. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  165. }
  166. template<bool Core>
  167. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getSamplerParamDesc(const String& name) const
  168. {
  169. auto findIterObject = mDesc.samplerParams.find(name);
  170. if (findIterObject != mDesc.samplerParams.end())
  171. return findIterObject->second;
  172. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  173. }
  174. template<bool Core>
  175. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getBufferParamDesc(const String& name) const
  176. {
  177. auto findIterObject = mDesc.bufferParams.find(name);
  178. if (findIterObject != mDesc.bufferParams.end())
  179. return findIterObject->second;
  180. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  181. }
  182. template<bool Core>
  183. bool TShader<Core>::hasDataParam(const String& name) const
  184. {
  185. auto findIterData = mDesc.dataParams.find(name);
  186. if (findIterData != mDesc.dataParams.end())
  187. return true;
  188. return false;
  189. }
  190. template<bool Core>
  191. bool TShader<Core>::hasTextureParam(const String& name) const
  192. {
  193. auto findIterObject = mDesc.textureParams.find(name);
  194. if (findIterObject != mDesc.textureParams.end())
  195. return true;
  196. return false;
  197. }
  198. template<bool Core>
  199. bool TShader<Core>::hasSamplerParam(const String& name) const
  200. {
  201. auto findIterObject = mDesc.samplerParams.find(name);
  202. if (findIterObject != mDesc.samplerParams.end())
  203. return true;
  204. return false;
  205. }
  206. template<bool Core>
  207. bool TShader<Core>::hasBufferParam(const String& name) const
  208. {
  209. auto findIterObject = mDesc.bufferParams.find(name);
  210. if (findIterObject != mDesc.bufferParams.end())
  211. return true;
  212. return false;
  213. }
  214. template<bool Core>
  215. typename TShader<Core>::TextureType TShader<Core>::getDefaultTexture(UINT32 index) const
  216. {
  217. if (index < (UINT32)mDesc.textureDefaultValues.size())
  218. return mDesc.textureDefaultValues[index];
  219. return TextureType();
  220. }
  221. template<bool Core>
  222. typename TShader<Core>::SamplerStateType TShader<Core>::getDefaultSampler(UINT32 index) const
  223. {
  224. if (index < (UINT32)mDesc.samplerDefaultValues.size())
  225. return mDesc.samplerDefaultValues[index];
  226. return SamplerStateType();
  227. }
  228. template<bool Core>
  229. UINT8* TShader<Core>::getDefaultValue(UINT32 index) const
  230. {
  231. if (index < (UINT32)mDesc.dataDefaultValues.size())
  232. return (UINT8*)&mDesc.dataDefaultValues[index];
  233. return nullptr;
  234. }
  235. template<bool Core>
  236. SPtr<typename TShader<Core>::TechniqueType> TShader<Core>::getBestTechnique() const
  237. {
  238. for (auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  239. {
  240. if ((*iter)->isSupported())
  241. {
  242. return *iter;
  243. }
  244. }
  245. return nullptr;
  246. // TODO - Low priority. Instead of returning null use an extremely simple technique that will be supported almost everywhere as a fallback.
  247. }
  248. template class TShader < false > ;
  249. template class TShader < true >;
  250. ShaderCore::ShaderCore(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques, UINT32 id)
  251. :TShader(name, desc, techniques, id)
  252. {
  253. }
  254. SPtr<ShaderCore> ShaderCore::create(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  255. {
  256. UINT32 id = mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  257. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  258. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(name, desc, techniques, id);
  259. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  260. shaderCorePtr->_setThisPtr(shaderCorePtr);
  261. shaderCorePtr->initialize();
  262. return shaderCorePtr;
  263. }
  264. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques, UINT32 id)
  265. :TShader(name, desc, techniques, id)
  266. {
  267. mMetaData = bs_shared_ptr_new<ShaderMetaData>();
  268. }
  269. SPtr<ShaderCore> Shader::getCore() const
  270. {
  271. return std::static_pointer_cast<ShaderCore>(mCoreSpecific);
  272. }
  273. void Shader::setIncludeFiles(const Vector<String>& includes)
  274. {
  275. SPtr<ShaderMetaData> meta = std::static_pointer_cast<ShaderMetaData>(getMetaData());
  276. meta->includes = includes;
  277. }
  278. SPtr<CoreObjectCore> Shader::createCore() const
  279. {
  280. Vector<SPtr<TechniqueCore>> techniques;
  281. for (auto& technique : mTechniques)
  282. techniques.push_back(technique->getCore());
  283. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(mName, convertDesc(mDesc), techniques, mId);
  284. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  285. shaderCorePtr->_setThisPtr(shaderCorePtr);
  286. return shaderCorePtr;
  287. }
  288. SHADER_DESC_CORE Shader::convertDesc(const SHADER_DESC& desc) const
  289. {
  290. SHADER_DESC_CORE output;
  291. output.dataParams = desc.dataParams;
  292. output.textureParams = desc.textureParams;
  293. output.samplerParams = desc.samplerParams;
  294. output.bufferParams = desc.bufferParams;
  295. output.paramBlocks = desc.paramBlocks;
  296. output.queuePriority = desc.queuePriority;
  297. output.queueSortType = desc.queueSortType;
  298. output.separablePasses = desc.separablePasses;
  299. // Ignoring default values as they are not needed for syncing since
  300. // they're initialized through the material.
  301. return output;
  302. }
  303. void Shader::getCoreDependencies(Vector<CoreObject*>& dependencies)
  304. {
  305. for (auto& technique : mTechniques)
  306. dependencies.push_back(technique.get());
  307. }
  308. bool Shader::isSampler(GpuParamObjectType type)
  309. {
  310. switch(type)
  311. {
  312. case GPOT_SAMPLER1D:
  313. case GPOT_SAMPLER2D:
  314. case GPOT_SAMPLER3D:
  315. case GPOT_SAMPLERCUBE:
  316. case GPOT_SAMPLER2DMS:
  317. return true;
  318. }
  319. return false;
  320. }
  321. bool Shader::isTexture(GpuParamObjectType type)
  322. {
  323. switch(type)
  324. {
  325. case GPOT_TEXTURE1D:
  326. case GPOT_TEXTURE2D:
  327. case GPOT_TEXTURE3D:
  328. case GPOT_TEXTURECUBE:
  329. case GPOT_TEXTURE2DMS:
  330. return true;
  331. }
  332. return false;
  333. }
  334. bool Shader::isBuffer(GpuParamObjectType type)
  335. {
  336. switch(type)
  337. {
  338. case GPOT_BYTE_BUFFER:
  339. case GPOT_STRUCTURED_BUFFER:
  340. case GPOT_RWBYTE_BUFFER:
  341. case GPOT_RWAPPEND_BUFFER:
  342. case GPOT_RWCONSUME_BUFFER:
  343. case GPOT_RWSTRUCTURED_BUFFER:
  344. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  345. case GPOT_RWTYPED_BUFFER:
  346. return true;
  347. }
  348. return false;
  349. }
  350. UINT32 Shader::getDataParamSize(GpuParamDataType type)
  351. {
  352. static const GpuDataParamInfos PARAM_SIZES;
  353. UINT32 idx = (UINT32)type;
  354. if (idx < sizeof(GpuParams::PARAM_SIZES.lookup))
  355. return GpuParams::PARAM_SIZES.lookup[idx].size;
  356. return 0;
  357. }
  358. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  359. {
  360. ShaderPtr newShader = _createPtr(name, desc, techniques);
  361. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  362. }
  363. ShaderPtr Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  364. {
  365. UINT32 id = ShaderCore::mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  366. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  367. ShaderPtr newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader(name, desc, techniques, id));
  368. newShader->_setThisPtr(newShader);
  369. newShader->initialize();
  370. return newShader;
  371. }
  372. ShaderPtr Shader::createEmpty()
  373. {
  374. ShaderPtr newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader());
  375. newShader->_setThisPtr(newShader);
  376. return newShader;
  377. }
  378. RTTITypeBase* Shader::getRTTIStatic()
  379. {
  380. return ShaderRTTI::instance();
  381. }
  382. RTTITypeBase* Shader::getRTTI() const
  383. {
  384. return Shader::getRTTIStatic();
  385. }
  386. RTTITypeBase* ShaderMetaData::getRTTIStatic()
  387. {
  388. return ShaderMetaDataRTTI::instance();
  389. }
  390. RTTITypeBase* ShaderMetaData::getRTTI() const
  391. {
  392. return ShaderMetaData::getRTTIStatic();
  393. }
  394. }