BsShader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. return GPT_DATA;
  150. }
  151. template<bool Core>
  152. const SHADER_DATA_PARAM_DESC& TShader<Core>::getDataParamDesc(const String& name) const
  153. {
  154. auto findIterData = mDesc.dataParams.find(name);
  155. if (findIterData != mDesc.dataParams.end())
  156. return findIterData->second;
  157. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  158. static SHADER_DATA_PARAM_DESC dummy;
  159. return dummy;
  160. }
  161. template<bool Core>
  162. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getTextureParamDesc(const String& name) const
  163. {
  164. auto findIterObject = mDesc.textureParams.find(name);
  165. if (findIterObject != mDesc.textureParams.end())
  166. return findIterObject->second;
  167. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  168. static SHADER_OBJECT_PARAM_DESC dummy;
  169. return dummy;
  170. }
  171. template<bool Core>
  172. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getSamplerParamDesc(const String& name) const
  173. {
  174. auto findIterObject = mDesc.samplerParams.find(name);
  175. if (findIterObject != mDesc.samplerParams.end())
  176. return findIterObject->second;
  177. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  178. static SHADER_OBJECT_PARAM_DESC dummy;
  179. return dummy;
  180. }
  181. template<bool Core>
  182. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getBufferParamDesc(const String& name) const
  183. {
  184. auto findIterObject = mDesc.bufferParams.find(name);
  185. if (findIterObject != mDesc.bufferParams.end())
  186. return findIterObject->second;
  187. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  188. static SHADER_OBJECT_PARAM_DESC dummy;
  189. return dummy;
  190. }
  191. template<bool Core>
  192. bool TShader<Core>::hasDataParam(const String& name) const
  193. {
  194. auto findIterData = mDesc.dataParams.find(name);
  195. if (findIterData != mDesc.dataParams.end())
  196. return true;
  197. return false;
  198. }
  199. template<bool Core>
  200. bool TShader<Core>::hasTextureParam(const String& name) const
  201. {
  202. auto findIterObject = mDesc.textureParams.find(name);
  203. if (findIterObject != mDesc.textureParams.end())
  204. return true;
  205. return false;
  206. }
  207. template<bool Core>
  208. bool TShader<Core>::hasSamplerParam(const String& name) const
  209. {
  210. auto findIterObject = mDesc.samplerParams.find(name);
  211. if (findIterObject != mDesc.samplerParams.end())
  212. return true;
  213. return false;
  214. }
  215. template<bool Core>
  216. bool TShader<Core>::hasBufferParam(const String& name) const
  217. {
  218. auto findIterObject = mDesc.bufferParams.find(name);
  219. if (findIterObject != mDesc.bufferParams.end())
  220. return true;
  221. return false;
  222. }
  223. template<bool Core>
  224. typename TShader<Core>::TextureType TShader<Core>::getDefaultTexture(UINT32 index) const
  225. {
  226. if (index < (UINT32)mDesc.textureDefaultValues.size())
  227. return mDesc.textureDefaultValues[index];
  228. return TextureType();
  229. }
  230. template<bool Core>
  231. typename TShader<Core>::SamplerStateType TShader<Core>::getDefaultSampler(UINT32 index) const
  232. {
  233. if (index < (UINT32)mDesc.samplerDefaultValues.size())
  234. return mDesc.samplerDefaultValues[index];
  235. return SamplerStateType();
  236. }
  237. template<bool Core>
  238. UINT8* TShader<Core>::getDefaultValue(UINT32 index) const
  239. {
  240. if (index < (UINT32)mDesc.dataDefaultValues.size())
  241. return (UINT8*)&mDesc.dataDefaultValues[index];
  242. return nullptr;
  243. }
  244. template<bool Core>
  245. SPtr<typename TShader<Core>::TechniqueType> TShader<Core>::getBestTechnique() const
  246. {
  247. for (auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  248. {
  249. if ((*iter)->isSupported())
  250. {
  251. return *iter;
  252. }
  253. }
  254. return nullptr;
  255. // TODO - Low priority. Instead of returning null use an extremely simple technique that will be supported almost everywhere as a fallback.
  256. }
  257. template class TShader < false > ;
  258. template class TShader < true >;
  259. ShaderCore::ShaderCore(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques, UINT32 id)
  260. :TShader(name, desc, techniques, id)
  261. {
  262. }
  263. SPtr<ShaderCore> ShaderCore::create(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  264. {
  265. UINT32 id = mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  266. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  267. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(name, desc, techniques, id);
  268. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  269. shaderCorePtr->_setThisPtr(shaderCorePtr);
  270. shaderCorePtr->initialize();
  271. return shaderCorePtr;
  272. }
  273. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques, UINT32 id)
  274. :TShader(name, desc, techniques, id)
  275. {
  276. mMetaData = bs_shared_ptr_new<ShaderMetaData>();
  277. }
  278. SPtr<ShaderCore> Shader::getCore() const
  279. {
  280. return std::static_pointer_cast<ShaderCore>(mCoreSpecific);
  281. }
  282. void Shader::setIncludeFiles(const Vector<String>& includes)
  283. {
  284. SPtr<ShaderMetaData> meta = std::static_pointer_cast<ShaderMetaData>(getMetaData());
  285. meta->includes = includes;
  286. }
  287. SPtr<CoreObjectCore> Shader::createCore() const
  288. {
  289. Vector<SPtr<TechniqueCore>> techniques;
  290. for (auto& technique : mTechniques)
  291. techniques.push_back(technique->getCore());
  292. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(mName, convertDesc(mDesc), techniques, mId);
  293. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  294. shaderCorePtr->_setThisPtr(shaderCorePtr);
  295. return shaderCorePtr;
  296. }
  297. SHADER_DESC_CORE Shader::convertDesc(const SHADER_DESC& desc) const
  298. {
  299. SHADER_DESC_CORE output;
  300. output.dataParams = desc.dataParams;
  301. output.textureParams = desc.textureParams;
  302. output.samplerParams = desc.samplerParams;
  303. output.bufferParams = desc.bufferParams;
  304. output.paramBlocks = desc.paramBlocks;
  305. output.queuePriority = desc.queuePriority;
  306. output.queueSortType = desc.queueSortType;
  307. output.separablePasses = desc.separablePasses;
  308. // Ignoring default values as they are not needed for syncing since
  309. // they're initialized through the material.
  310. return output;
  311. }
  312. void Shader::getCoreDependencies(Vector<CoreObject*>& dependencies)
  313. {
  314. for (auto& technique : mTechniques)
  315. dependencies.push_back(technique.get());
  316. }
  317. bool Shader::isSampler(GpuParamObjectType type)
  318. {
  319. switch(type)
  320. {
  321. case GPOT_SAMPLER1D:
  322. case GPOT_SAMPLER2D:
  323. case GPOT_SAMPLER3D:
  324. case GPOT_SAMPLERCUBE:
  325. case GPOT_SAMPLER2DMS:
  326. return true;
  327. }
  328. return false;
  329. }
  330. bool Shader::isTexture(GpuParamObjectType type)
  331. {
  332. switch(type)
  333. {
  334. case GPOT_TEXTURE1D:
  335. case GPOT_TEXTURE2D:
  336. case GPOT_TEXTURE3D:
  337. case GPOT_TEXTURECUBE:
  338. case GPOT_TEXTURE2DMS:
  339. return true;
  340. }
  341. return false;
  342. }
  343. bool Shader::isBuffer(GpuParamObjectType type)
  344. {
  345. switch(type)
  346. {
  347. case GPOT_BYTE_BUFFER:
  348. case GPOT_STRUCTURED_BUFFER:
  349. case GPOT_RWBYTE_BUFFER:
  350. case GPOT_RWAPPEND_BUFFER:
  351. case GPOT_RWCONSUME_BUFFER:
  352. case GPOT_RWSTRUCTURED_BUFFER:
  353. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  354. case GPOT_RWTYPED_BUFFER:
  355. return true;
  356. }
  357. return false;
  358. }
  359. UINT32 Shader::getDataParamSize(GpuParamDataType type)
  360. {
  361. static const GpuDataParamInfos PARAM_SIZES;
  362. UINT32 idx = (UINT32)type;
  363. if (idx < sizeof(GpuParams::PARAM_SIZES.lookup))
  364. return GpuParams::PARAM_SIZES.lookup[idx].size;
  365. return 0;
  366. }
  367. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  368. {
  369. ShaderPtr newShader = _createPtr(name, desc, techniques);
  370. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  371. }
  372. ShaderPtr Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  373. {
  374. UINT32 id = ShaderCore::mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  375. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  376. ShaderPtr newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader(name, desc, techniques, id));
  377. newShader->_setThisPtr(newShader);
  378. newShader->initialize();
  379. return newShader;
  380. }
  381. ShaderPtr Shader::createEmpty()
  382. {
  383. ShaderPtr newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader());
  384. newShader->_setThisPtr(newShader);
  385. return newShader;
  386. }
  387. RTTITypeBase* Shader::getRTTIStatic()
  388. {
  389. return ShaderRTTI::instance();
  390. }
  391. RTTITypeBase* Shader::getRTTI() const
  392. {
  393. return Shader::getRTTIStatic();
  394. }
  395. RTTITypeBase* ShaderMetaData::getRTTIStatic()
  396. {
  397. return ShaderMetaDataRTTI::instance();
  398. }
  399. RTTITypeBase* ShaderMetaData::getRTTI() const
  400. {
  401. return ShaderMetaData::getRTTIStatic();
  402. }
  403. }