BsShader.cpp 14 KB

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