BsShader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Material/BsShader.h"
  4. #include "Material/BsTechnique.h"
  5. #include "Error/BsException.h"
  6. #include "Debug/BsDebug.h"
  7. #include "RTTI/BsShaderRTTI.h"
  8. #include "Resources/BsResources.h"
  9. #include "RenderAPI/BsGpuParams.h"
  10. #include "Material/BsPass.h"
  11. #include "RenderAPI/BsSamplerState.h"
  12. #include "Image/BsTexture.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. if (defaultValue != nullptr)
  34. {
  35. desc.defaultValueIdx = (UINT32)dataDefaultValues.size();
  36. UINT32 defaultValueSize = Shader::getDataParamSize(type);
  37. dataDefaultValues.resize(desc.defaultValueIdx + defaultValueSize);
  38. memcpy(&dataDefaultValues[desc.defaultValueIdx], defaultValue, defaultValueSize);
  39. }
  40. else
  41. desc.defaultValueIdx = (UINT32)-1;
  42. dataParams[name] = desc;
  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<bool Core>
  263. Vector<SPtr<typename TShader<Core>::TechniqueType>> TShader<Core>::getCompatibleTechniques(
  264. const ShaderVariation& variation) const
  265. {
  266. Vector<SPtr<TechniqueType>> output;
  267. for (auto& technique : mTechniques)
  268. {
  269. if (technique->isSupported() && technique->getVariation() == variation)
  270. output.push_back(technique);
  271. }
  272. return output;
  273. }
  274. template class TShader < false > ;
  275. template class TShader < true >;
  276. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques, UINT32 id)
  277. :TShader(name, desc, techniques, id)
  278. {
  279. mMetaData = bs_shared_ptr_new<ShaderMetaData>();
  280. }
  281. SPtr<ct::Shader> Shader::getCore() const
  282. {
  283. return std::static_pointer_cast<ct::Shader>(mCoreSpecific);
  284. }
  285. void Shader::setIncludeFiles(const Vector<String>& includes)
  286. {
  287. SPtr<ShaderMetaData> meta = std::static_pointer_cast<ShaderMetaData>(getMetaData());
  288. meta->includes = includes;
  289. }
  290. SPtr<ct::CoreObject> Shader::createCore() const
  291. {
  292. Vector<SPtr<ct::Technique>> techniques;
  293. for (auto& technique : mTechniques)
  294. techniques.push_back(technique->getCore());
  295. ct::Shader* shaderCore = new (bs_alloc<ct::Shader>()) ct::Shader(mName, convertDesc(mDesc), techniques, mId);
  296. SPtr<ct::Shader> shaderCorePtr = bs_shared_ptr<ct::Shader>(shaderCore);
  297. shaderCorePtr->_setThisPtr(shaderCorePtr);
  298. return shaderCorePtr;
  299. }
  300. ct::SHADER_DESC Shader::convertDesc(const SHADER_DESC& desc) const
  301. {
  302. ct::SHADER_DESC output;
  303. output.dataParams = desc.dataParams;
  304. output.textureParams = desc.textureParams;
  305. output.samplerParams = desc.samplerParams;
  306. output.bufferParams = desc.bufferParams;
  307. output.paramBlocks = desc.paramBlocks;
  308. output.dataDefaultValues = desc.dataDefaultValues;
  309. output.samplerDefaultValues.resize(desc.samplerDefaultValues.size());
  310. for (UINT32 i = 0; i < (UINT32)desc.samplerDefaultValues.size(); i++)
  311. {
  312. if (desc.samplerDefaultValues[i] != nullptr)
  313. output.samplerDefaultValues.push_back(desc.samplerDefaultValues[i]->getCore());
  314. else
  315. output.samplerDefaultValues.push_back(nullptr);
  316. }
  317. output.textureDefaultValues.resize(desc.textureDefaultValues.size());
  318. for (UINT32 i = 0; i < (UINT32)desc.textureDefaultValues.size(); i++)
  319. {
  320. if (desc.textureDefaultValues[i].isLoaded())
  321. output.textureDefaultValues.push_back(desc.textureDefaultValues[i]->getCore());
  322. else
  323. output.textureDefaultValues.push_back(nullptr);
  324. }
  325. output.queuePriority = desc.queuePriority;
  326. output.queueSortType = desc.queueSortType;
  327. output.separablePasses = desc.separablePasses;
  328. output.flags = desc.flags;
  329. // Ignoring default values as they are not needed for syncing since
  330. // they're initialized through the material.
  331. return output;
  332. }
  333. void Shader::getCoreDependencies(Vector<CoreObject*>& dependencies)
  334. {
  335. for (auto& technique : mTechniques)
  336. dependencies.push_back(technique.get());
  337. }
  338. bool Shader::isSampler(GpuParamObjectType type)
  339. {
  340. switch(type)
  341. {
  342. case GPOT_SAMPLER1D:
  343. case GPOT_SAMPLER2D:
  344. case GPOT_SAMPLER3D:
  345. case GPOT_SAMPLERCUBE:
  346. case GPOT_SAMPLER2DMS:
  347. return true;
  348. default:
  349. return false;
  350. }
  351. }
  352. bool Shader::isTexture(GpuParamObjectType type)
  353. {
  354. switch(type)
  355. {
  356. case GPOT_TEXTURE1D:
  357. case GPOT_TEXTURE2D:
  358. case GPOT_TEXTURE3D:
  359. case GPOT_TEXTURECUBE:
  360. case GPOT_TEXTURE2DMS:
  361. case GPOT_TEXTURE1DARRAY:
  362. case GPOT_TEXTURE2DARRAY:
  363. case GPOT_TEXTURE2DMSARRAY:
  364. case GPOT_TEXTURECUBEARRAY:
  365. return true;
  366. default:
  367. return false;
  368. }
  369. }
  370. bool Shader::isLoadStoreTexture(GpuParamObjectType type)
  371. {
  372. switch (type)
  373. {
  374. case GPOT_RWTEXTURE1D:
  375. case GPOT_RWTEXTURE2D:
  376. case GPOT_RWTEXTURE3D:
  377. case GPOT_RWTEXTURE2DMS:
  378. case GPOT_RWTEXTURE1DARRAY:
  379. case GPOT_RWTEXTURE2DARRAY:
  380. case GPOT_RWTEXTURE2DMSARRAY:
  381. return true;
  382. default:
  383. return false;
  384. }
  385. }
  386. bool Shader::isBuffer(GpuParamObjectType type)
  387. {
  388. switch(type)
  389. {
  390. case GPOT_BYTE_BUFFER:
  391. case GPOT_STRUCTURED_BUFFER:
  392. case GPOT_RWBYTE_BUFFER:
  393. case GPOT_RWAPPEND_BUFFER:
  394. case GPOT_RWCONSUME_BUFFER:
  395. case GPOT_RWSTRUCTURED_BUFFER:
  396. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  397. case GPOT_RWTYPED_BUFFER:
  398. return true;
  399. default:
  400. return false;
  401. }
  402. }
  403. UINT32 Shader::getDataParamSize(GpuParamDataType type)
  404. {
  405. static const GpuDataParamInfos PARAM_SIZES;
  406. UINT32 idx = (UINT32)type;
  407. if (idx < sizeof(GpuParams::PARAM_SIZES.lookup))
  408. return GpuParams::PARAM_SIZES.lookup[idx].size;
  409. return 0;
  410. }
  411. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  412. {
  413. SPtr<Shader> newShader = _createPtr(name, desc, techniques);
  414. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  415. }
  416. SPtr<Shader> Shader::_createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  417. {
  418. UINT32 id = ct::Shader::mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  419. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  420. SPtr<Shader> newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader(name, desc, techniques, id));
  421. newShader->_setThisPtr(newShader);
  422. newShader->initialize();
  423. return newShader;
  424. }
  425. SPtr<Shader> Shader::createEmpty()
  426. {
  427. SPtr<Shader> newShader = bs_core_ptr<Shader>(new (bs_alloc<Shader>()) Shader());
  428. newShader->_setThisPtr(newShader);
  429. return newShader;
  430. }
  431. RTTITypeBase* Shader::getRTTIStatic()
  432. {
  433. return ShaderRTTI::instance();
  434. }
  435. RTTITypeBase* Shader::getRTTI() const
  436. {
  437. return Shader::getRTTIStatic();
  438. }
  439. RTTITypeBase* ShaderMetaData::getRTTIStatic()
  440. {
  441. return ShaderMetaDataRTTI::instance();
  442. }
  443. RTTITypeBase* ShaderMetaData::getRTTI() const
  444. {
  445. return ShaderMetaData::getRTTIStatic();
  446. }
  447. namespace ct
  448. {
  449. std::atomic<UINT32> Shader::mNextShaderId;
  450. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques,
  451. UINT32 id)
  452. :TShader(name, desc, techniques, id)
  453. {
  454. }
  455. SPtr<Shader> Shader::create(const String& name, const SHADER_DESC& desc,
  456. const Vector<SPtr<Technique>>& techniques)
  457. {
  458. UINT32 id = mNextShaderId.fetch_add(1, std::memory_order_relaxed);
  459. assert(id < std::numeric_limits<UINT32>::max() && "Created too many shaders, reached maximum id.");
  460. Shader* shaderCore = new (bs_alloc<Shader>()) Shader(name, desc, techniques, id);
  461. SPtr<Shader> shaderCorePtr = bs_shared_ptr<Shader>(shaderCore);
  462. shaderCorePtr->_setThisPtr(shaderCorePtr);
  463. shaderCorePtr->initialize();
  464. return shaderCorePtr;
  465. }
  466. }
  467. }