2
0

BsShader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 bs
  14. {
  15. template<bool Core>
  16. TSHADER_DESC<Core>::TSHADER_DESC()
  17. :queueSortType(QueueSortType::None), queuePriority(0), separablePasses(false), flags(0)
  18. {
  19. }
  20. template<bool Core>
  21. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type,
  22. StringID rendererSemantic, UINT32 arraySize, UINT32 elementSize, UINT8* defaultValue)
  23. {
  24. if(type == GPDT_STRUCT && elementSize <= 0)
  25. BS_EXCEPT(InvalidParametersException, "You need to provide a non-zero element size for a struct parameter.")
  26. SHADER_DATA_PARAM_DESC desc;
  27. desc.name = name;
  28. desc.gpuVariableName = gpuVariableName;
  29. desc.type = type;
  30. desc.arraySize = arraySize;
  31. desc.rendererSemantic = rendererSemantic;
  32. desc.elementSize = elementSize;
  33. dataParams[name] = desc;
  34. if (defaultValue != nullptr)
  35. {
  36. desc.defaultValueIdx = (UINT32)dataDefaultValues.size();
  37. UINT32 defaultValueSize = Shader::getDataParamSize(type);
  38. dataDefaultValues.resize(desc.defaultValueIdx + defaultValueSize);
  39. memcpy(&dataDefaultValues[desc.defaultValueIdx], defaultValue, defaultValueSize);
  40. }
  41. else
  42. desc.defaultValueIdx = (UINT32)-1;
  43. }
  44. template<bool Core>
  45. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic)
  46. {
  47. UINT32 defaultValueIdx = (UINT32)-1;
  48. addParameterInternal(name, gpuVariableName, type, rendererSemantic, defaultValueIdx);
  49. }
  50. template<bool Core>
  51. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, const SamplerStateType& defaultValue, StringID rendererSemantic)
  52. {
  53. UINT32 defaultValueIdx = (UINT32)-1;
  54. if (Shader::isSampler(type) && defaultValue != nullptr)
  55. {
  56. defaultValueIdx = (UINT32)samplerDefaultValues.size();
  57. samplerDefaultValues.push_back(defaultValue);
  58. }
  59. addParameterInternal(name, gpuVariableName, type, rendererSemantic, defaultValueIdx);
  60. }
  61. template<bool Core>
  62. void TSHADER_DESC<Core>::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, const TextureType& defaultValue, StringID rendererSemantic)
  63. {
  64. UINT32 defaultValueIdx = (UINT32)-1;
  65. if (Shader::isTexture(type) && defaultValue != nullptr)
  66. {
  67. defaultValueIdx = (UINT32)textureDefaultValues.size();
  68. textureDefaultValues.push_back(defaultValue);
  69. }
  70. addParameterInternal(name, gpuVariableName, type, rendererSemantic, defaultValueIdx);
  71. }
  72. template<bool Core>
  73. void TSHADER_DESC<Core>::addParameterInternal(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic, UINT32 defaultValueIdx)
  74. {
  75. Map<String, SHADER_OBJECT_PARAM_DESC>* DEST_LOOKUP[] = { &textureParams, &bufferParams, &samplerParams };
  76. UINT32 destIdx = 0;
  77. if (Shader::isBuffer(type))
  78. destIdx = 1;
  79. else if (Shader::isSampler(type))
  80. destIdx = 2;
  81. Map<String, SHADER_OBJECT_PARAM_DESC>& paramsMap = *DEST_LOOKUP[destIdx];
  82. auto iterFind = paramsMap.find(name);
  83. if (iterFind == paramsMap.end())
  84. {
  85. SHADER_OBJECT_PARAM_DESC desc;
  86. desc.name = name;
  87. desc.type = type;
  88. desc.rendererSemantic = rendererSemantic;
  89. desc.gpuVariableNames.push_back(gpuVariableName);
  90. desc.defaultValueIdx = defaultValueIdx;
  91. paramsMap[name] = desc;
  92. }
  93. else
  94. {
  95. SHADER_OBJECT_PARAM_DESC& desc = iterFind->second;
  96. // If same name but different properties, we ignore this param
  97. if (desc.type != type || desc.rendererSemantic != rendererSemantic)
  98. return;
  99. Vector<String>& gpuVariableNames = desc.gpuVariableNames;
  100. bool found = false;
  101. for (UINT32 i = 0; i < (UINT32)gpuVariableNames.size(); i++)
  102. {
  103. if (gpuVariableNames[i] == gpuVariableName)
  104. {
  105. found = true;
  106. break;
  107. }
  108. }
  109. if (!found)
  110. gpuVariableNames.push_back(gpuVariableName);
  111. }
  112. }
  113. template<bool Core>
  114. void TSHADER_DESC<Core>::setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, StringID rendererSemantic)
  115. {
  116. SHADER_PARAM_BLOCK_DESC desc;
  117. desc.name = name;
  118. desc.shared = shared;
  119. desc.usage = usage;
  120. desc.rendererSemantic = rendererSemantic;
  121. paramBlocks[name] = desc;
  122. }
  123. template struct TSHADER_DESC<false>;
  124. template struct TSHADER_DESC<true>;
  125. template<bool Core>
  126. TShader<Core>::TShader(const String& name, const TSHADER_DESC<Core>& desc, const Vector<SPtr<TechniqueType>>& techniques, UINT32 id)
  127. :mName(name), mDesc(desc), mTechniques(techniques), mId(id)
  128. { }
  129. template<bool Core>
  130. TShader<Core>::~TShader()
  131. { }
  132. template<bool Core>
  133. GpuParamType TShader<Core>::getParamType(const String& name) const
  134. {
  135. auto findIterData = mDesc.dataParams.find(name);
  136. if (findIterData != mDesc.dataParams.end())
  137. return GPT_DATA;
  138. auto findIterTexture = mDesc.textureParams.find(name);
  139. if (findIterTexture != mDesc.textureParams.end())
  140. return GPT_TEXTURE;
  141. auto findIterBuffer = mDesc.bufferParams.find(name);
  142. if (findIterBuffer != mDesc.bufferParams.end())
  143. return GPT_BUFFER;
  144. auto findIterSampler = mDesc.samplerParams.find(name);
  145. if (findIterSampler != mDesc.samplerParams.end())
  146. return GPT_SAMPLER;
  147. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  148. return GPT_DATA;
  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. static SHADER_DATA_PARAM_DESC dummy;
  158. return dummy;
  159. }
  160. template<bool Core>
  161. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getTextureParamDesc(const String& name) const
  162. {
  163. auto findIterObject = mDesc.textureParams.find(name);
  164. if (findIterObject != mDesc.textureParams.end())
  165. return findIterObject->second;
  166. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  167. static SHADER_OBJECT_PARAM_DESC dummy;
  168. return dummy;
  169. }
  170. template<bool Core>
  171. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getSamplerParamDesc(const String& name) const
  172. {
  173. auto findIterObject = mDesc.samplerParams.find(name);
  174. if (findIterObject != mDesc.samplerParams.end())
  175. return findIterObject->second;
  176. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  177. static SHADER_OBJECT_PARAM_DESC dummy;
  178. return dummy;
  179. }
  180. template<bool Core>
  181. const SHADER_OBJECT_PARAM_DESC& TShader<Core>::getBufferParamDesc(const String& name) const
  182. {
  183. auto findIterObject = mDesc.bufferParams.find(name);
  184. if (findIterObject != mDesc.bufferParams.end())
  185. return findIterObject->second;
  186. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  187. static SHADER_OBJECT_PARAM_DESC dummy;
  188. return dummy;
  189. }
  190. template<bool Core>
  191. bool TShader<Core>::hasDataParam(const String& name) const
  192. {
  193. auto findIterData = mDesc.dataParams.find(name);
  194. if (findIterData != mDesc.dataParams.end())
  195. return true;
  196. return false;
  197. }
  198. template<bool Core>
  199. bool TShader<Core>::hasTextureParam(const String& name) const
  200. {
  201. auto findIterObject = mDesc.textureParams.find(name);
  202. if (findIterObject != mDesc.textureParams.end())
  203. return true;
  204. return false;
  205. }
  206. template<bool Core>
  207. bool TShader<Core>::hasSamplerParam(const String& name) const
  208. {
  209. auto findIterObject = mDesc.samplerParams.find(name);
  210. if (findIterObject != mDesc.samplerParams.end())
  211. return true;
  212. return false;
  213. }
  214. template<bool Core>
  215. bool TShader<Core>::hasBufferParam(const String& name) const
  216. {
  217. auto findIterObject = mDesc.bufferParams.find(name);
  218. if (findIterObject != mDesc.bufferParams.end())
  219. return true;
  220. return false;
  221. }
  222. template<bool Core>
  223. bool TShader<Core>::hasParamBlock(const String& name) const
  224. {
  225. auto findIterObject = mDesc.paramBlocks.find(name);
  226. if (findIterObject != mDesc.paramBlocks.end())
  227. return true;
  228. return false;
  229. }
  230. template<bool Core>
  231. typename TShader<Core>::TextureType TShader<Core>::getDefaultTexture(UINT32 index) const
  232. {
  233. if (index < (UINT32)mDesc.textureDefaultValues.size())
  234. return mDesc.textureDefaultValues[index];
  235. return TextureType();
  236. }
  237. template<bool Core>
  238. typename TShader<Core>::SamplerStateType TShader<Core>::getDefaultSampler(UINT32 index) const
  239. {
  240. if (index < (UINT32)mDesc.samplerDefaultValues.size())
  241. return mDesc.samplerDefaultValues[index];
  242. return SamplerStateType();
  243. }
  244. template<bool Core>
  245. UINT8* TShader<Core>::getDefaultValue(UINT32 index) const
  246. {
  247. if (index < (UINT32)mDesc.dataDefaultValues.size())
  248. return (UINT8*)&mDesc.dataDefaultValues[index];
  249. return nullptr;
  250. }
  251. template<bool Core>
  252. Vector<SPtr<typename TShader<Core>::TechniqueType>> TShader<Core>::getCompatibleTechniques() const
  253. {
  254. Vector<SPtr<TechniqueType>> output;
  255. for (auto& technique : mTechniques)
  256. {
  257. if (technique->isSupported())
  258. output.push_back(technique);
  259. }
  260. return output;
  261. }
  262. template class TShader < false > ;
  263. template class TShader < true >;
  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<ct::ShaderCore> Shader::getCore() const
  270. {
  271. return std::static_pointer_cast<ct::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<ct::CoreObjectCore> Shader::createCore() const
  279. {
  280. Vector<SPtr<ct::TechniqueCore>> techniques;
  281. for (auto& technique : mTechniques)
  282. techniques.push_back(technique->getCore());
  283. ct::ShaderCore* shaderCore = new (bs_alloc<ct::ShaderCore>()) ct::ShaderCore(mName, convertDesc(mDesc), techniques, mId);
  284. SPtr<ct::ShaderCore> shaderCorePtr = bs_shared_ptr<ct::ShaderCore>(shaderCore);
  285. shaderCorePtr->_setThisPtr(shaderCorePtr);
  286. return shaderCorePtr;
  287. }
  288. ct::SHADER_DESC Shader::convertDesc(const SHADER_DESC& desc) const
  289. {
  290. ct::SHADER_DESC 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. default:
  319. return false;
  320. }
  321. }
  322. bool Shader::isTexture(GpuParamObjectType type)
  323. {
  324. switch(type)
  325. {
  326. case GPOT_TEXTURE1D:
  327. case GPOT_TEXTURE2D:
  328. case GPOT_TEXTURE3D:
  329. case GPOT_TEXTURECUBE:
  330. case GPOT_TEXTURE2DMS:
  331. return true;
  332. default:
  333. return false;
  334. }
  335. }
  336. bool Shader::isLoadStoreTexture(GpuParamObjectType type)
  337. {
  338. switch (type)
  339. {
  340. case GPOT_RWTEXTURE1D:
  341. case GPOT_RWTEXTURE2D:
  342. case GPOT_RWTEXTURE3D:
  343. case GPOT_RWTEXTURE2DMS:
  344. return true;
  345. default:
  346. return false;
  347. }
  348. }
  349. bool Shader::isBuffer(GpuParamObjectType type)
  350. {
  351. switch(type)
  352. {
  353. case GPOT_BYTE_BUFFER:
  354. case GPOT_STRUCTURED_BUFFER:
  355. case GPOT_RWBYTE_BUFFER:
  356. case GPOT_RWAPPEND_BUFFER:
  357. case GPOT_RWCONSUME_BUFFER:
  358. case GPOT_RWSTRUCTURED_BUFFER:
  359. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  360. case GPOT_RWTYPED_BUFFER:
  361. return true;
  362. default:
  363. return false;
  364. }
  365. }
  366. UINT32 Shader::getDataParamSize(GpuParamDataType type)
  367. {
  368. static const GpuDataParamInfos PARAM_SIZES;
  369. UINT32 idx = (UINT32)type;
  370. if (idx < sizeof(GpuParams::PARAM_SIZES.lookup))
  371. return GpuParams::PARAM_SIZES.lookup[idx].size;
  372. return 0;
  373. }
  374. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  375. {
  376. SPtr<Shader> newShader = _createPtr(name, desc, techniques);
  377. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  378. }
  379. SPtr<Shader> Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  380. {
  381. UINT32 id = ct::ShaderCore::mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  382. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  383. SPtr<Shader> newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader(name, desc, techniques, id));
  384. newShader->_setThisPtr(newShader);
  385. newShader->initialize();
  386. return newShader;
  387. }
  388. SPtr<Shader> Shader::createEmpty()
  389. {
  390. SPtr<Shader> newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader());
  391. newShader->_setThisPtr(newShader);
  392. return newShader;
  393. }
  394. RTTITypeBase* Shader::getRTTIStatic()
  395. {
  396. return ShaderRTTI::instance();
  397. }
  398. RTTITypeBase* Shader::getRTTI() const
  399. {
  400. return Shader::getRTTIStatic();
  401. }
  402. RTTITypeBase* ShaderMetaData::getRTTIStatic()
  403. {
  404. return ShaderMetaDataRTTI::instance();
  405. }
  406. RTTITypeBase* ShaderMetaData::getRTTI() const
  407. {
  408. return ShaderMetaData::getRTTIStatic();
  409. }
  410. namespace ct
  411. {
  412. std::atomic<UINT32> ShaderCore::mNextShaderId;
  413. ShaderCore::ShaderCore(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueCore>>& techniques,
  414. UINT32 id)
  415. :TShader(name, desc, techniques, id)
  416. {
  417. }
  418. SPtr<ShaderCore> ShaderCore::create(const String& name, const SHADER_DESC& desc,
  419. const Vector<SPtr<TechniqueCore>>& techniques)
  420. {
  421. UINT32 id = mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  422. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  423. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(name, desc, techniques, id);
  424. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore>(shaderCore);
  425. shaderCorePtr->_setThisPtr(shaderCorePtr);
  426. shaderCorePtr->initialize();
  427. return shaderCorePtr;
  428. }
  429. }
  430. }