BsShader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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;
  16. template<bool Core>
  17. TSHADER_DESC<Core>::TSHADER_DESC()
  18. :queueSortType(QueueSortType::None), queuePriority(0), 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. bool TShader<Core>::hasParamBlock(const String& name) const
  225. {
  226. auto findIterObject = mDesc.paramBlocks.find(name);
  227. if (findIterObject != mDesc.paramBlocks.end())
  228. return true;
  229. return false;
  230. }
  231. template<bool Core>
  232. typename TShader<Core>::TextureType TShader<Core>::getDefaultTexture(UINT32 index) const
  233. {
  234. if (index < (UINT32)mDesc.textureDefaultValues.size())
  235. return mDesc.textureDefaultValues[index];
  236. return TextureType();
  237. }
  238. template<bool Core>
  239. typename TShader<Core>::SamplerStateType TShader<Core>::getDefaultSampler(UINT32 index) const
  240. {
  241. if (index < (UINT32)mDesc.samplerDefaultValues.size())
  242. return mDesc.samplerDefaultValues[index];
  243. return SamplerStateType();
  244. }
  245. template<bool Core>
  246. UINT8* TShader<Core>::getDefaultValue(UINT32 index) const
  247. {
  248. if (index < (UINT32)mDesc.dataDefaultValues.size())
  249. return (UINT8*)&mDesc.dataDefaultValues[index];
  250. return nullptr;
  251. }
  252. template<bool Core>
  253. Vector<SPtr<typename TShader<Core>::TechniqueType>> TShader<Core>::getCompatibleTechniques() const
  254. {
  255. Vector<SPtr<TechniqueType>> output;
  256. for (auto& technique : mTechniques)
  257. {
  258. if (technique->isSupported())
  259. output.push_back(technique);
  260. }
  261. return output;
  262. }
  263. template class TShader < false > ;
  264. template class TShader < true >;
  265. ShaderCore::ShaderCore(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques, UINT32 id)
  266. :TShader(name, desc, techniques, id)
  267. {
  268. }
  269. SPtr<ShaderCore> ShaderCore::create(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  270. {
  271. UINT32 id = mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  272. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  273. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(name, desc, techniques, id);
  274. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  275. shaderCorePtr->_setThisPtr(shaderCorePtr);
  276. shaderCorePtr->initialize();
  277. return shaderCorePtr;
  278. }
  279. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques, UINT32 id)
  280. :TShader(name, desc, techniques, id)
  281. {
  282. mMetaData = bs_shared_ptr_new<ShaderMetaData>();
  283. }
  284. SPtr<ShaderCore> Shader::getCore() const
  285. {
  286. return std::static_pointer_cast<ShaderCore>(mCoreSpecific);
  287. }
  288. void Shader::setIncludeFiles(const Vector<String>& includes)
  289. {
  290. SPtr<ShaderMetaData> meta = std::static_pointer_cast<ShaderMetaData>(getMetaData());
  291. meta->includes = includes;
  292. }
  293. SPtr<CoreObjectCore> Shader::createCore() const
  294. {
  295. Vector<SPtr<TechniqueCore>> techniques;
  296. for (auto& technique : mTechniques)
  297. techniques.push_back(technique->getCore());
  298. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(mName, convertDesc(mDesc), techniques, mId);
  299. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  300. shaderCorePtr->_setThisPtr(shaderCorePtr);
  301. return shaderCorePtr;
  302. }
  303. SHADER_DESC_CORE Shader::convertDesc(const SHADER_DESC& desc) const
  304. {
  305. SHADER_DESC_CORE output;
  306. output.dataParams = desc.dataParams;
  307. output.textureParams = desc.textureParams;
  308. output.samplerParams = desc.samplerParams;
  309. output.bufferParams = desc.bufferParams;
  310. output.paramBlocks = desc.paramBlocks;
  311. output.queuePriority = desc.queuePriority;
  312. output.queueSortType = desc.queueSortType;
  313. output.separablePasses = desc.separablePasses;
  314. // Ignoring default values as they are not needed for syncing since
  315. // they're initialized through the material.
  316. return output;
  317. }
  318. void Shader::getCoreDependencies(Vector<CoreObject*>& dependencies)
  319. {
  320. for (auto& technique : mTechniques)
  321. dependencies.push_back(technique.get());
  322. }
  323. bool Shader::isSampler(GpuParamObjectType type)
  324. {
  325. switch(type)
  326. {
  327. case GPOT_SAMPLER1D:
  328. case GPOT_SAMPLER2D:
  329. case GPOT_SAMPLER3D:
  330. case GPOT_SAMPLERCUBE:
  331. case GPOT_SAMPLER2DMS:
  332. return true;
  333. default:
  334. return false;
  335. }
  336. }
  337. bool Shader::isTexture(GpuParamObjectType type)
  338. {
  339. switch(type)
  340. {
  341. case GPOT_TEXTURE1D:
  342. case GPOT_TEXTURE2D:
  343. case GPOT_TEXTURE3D:
  344. case GPOT_TEXTURECUBE:
  345. case GPOT_TEXTURE2DMS:
  346. return true;
  347. default:
  348. return false;
  349. }
  350. }
  351. bool Shader::isLoadStoreTexture(GpuParamObjectType type)
  352. {
  353. switch (type)
  354. {
  355. case GPOT_RWTEXTURE1D:
  356. case GPOT_RWTEXTURE2D:
  357. case GPOT_RWTEXTURE3D:
  358. case GPOT_RWTEXTURE2DMS:
  359. return true;
  360. default:
  361. return false;
  362. }
  363. }
  364. bool Shader::isBuffer(GpuParamObjectType type)
  365. {
  366. switch(type)
  367. {
  368. case GPOT_BYTE_BUFFER:
  369. case GPOT_STRUCTURED_BUFFER:
  370. case GPOT_RWBYTE_BUFFER:
  371. case GPOT_RWAPPEND_BUFFER:
  372. case GPOT_RWCONSUME_BUFFER:
  373. case GPOT_RWSTRUCTURED_BUFFER:
  374. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  375. case GPOT_RWTYPED_BUFFER:
  376. return true;
  377. default:
  378. return false;
  379. }
  380. }
  381. UINT32 Shader::getDataParamSize(GpuParamDataType type)
  382. {
  383. static const GpuDataParamInfos PARAM_SIZES;
  384. UINT32 idx = (UINT32)type;
  385. if (idx < sizeof(GpuParams::PARAM_SIZES.lookup))
  386. return GpuParams::PARAM_SIZES.lookup[idx].size;
  387. return 0;
  388. }
  389. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  390. {
  391. SPtr<Shader> newShader = _createPtr(name, desc, techniques);
  392. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  393. }
  394. SPtr<Shader> Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  395. {
  396. UINT32 id = ShaderCore::mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  397. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  398. SPtr<Shader> newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader(name, desc, techniques, id));
  399. newShader->_setThisPtr(newShader);
  400. newShader->initialize();
  401. return newShader;
  402. }
  403. SPtr<Shader> Shader::createEmpty()
  404. {
  405. SPtr<Shader> newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader());
  406. newShader->_setThisPtr(newShader);
  407. return newShader;
  408. }
  409. RTTITypeBase* Shader::getRTTIStatic()
  410. {
  411. return ShaderRTTI::instance();
  412. }
  413. RTTITypeBase* Shader::getRTTI() const
  414. {
  415. return Shader::getRTTIStatic();
  416. }
  417. RTTITypeBase* ShaderMetaData::getRTTIStatic()
  418. {
  419. return ShaderMetaDataRTTI::instance();
  420. }
  421. RTTITypeBase* ShaderMetaData::getRTTI() const
  422. {
  423. return ShaderMetaData::getRTTIStatic();
  424. }
  425. }