BsShader.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "BsFrameAlloc.h"
  8. namespace BansheeEngine
  9. {
  10. void SHADER_DESC::addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, UINT32 rendererSemantic, UINT32 arraySize, UINT32 elementSize)
  11. {
  12. if(type == GPDT_STRUCT && elementSize <= 0)
  13. BS_EXCEPT(InvalidParametersException, "You need to provide a non-zero element size for a struct parameter.")
  14. SHADER_DATA_PARAM_DESC desc;
  15. desc.name = name;
  16. desc.gpuVariableName = gpuVariableName;
  17. desc.type = type;
  18. desc.arraySize = arraySize;
  19. desc.rendererSemantic = rendererSemantic;
  20. desc.elementSize = elementSize;
  21. dataParams[name] = desc;
  22. objectParams.erase(name);
  23. }
  24. void SHADER_DESC::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, UINT32 rendererSemantic)
  25. {
  26. auto iterFind = objectParams.find(name);
  27. if (iterFind == objectParams.end())
  28. {
  29. SHADER_OBJECT_PARAM_DESC desc;
  30. desc.name = name;
  31. desc.type = type;
  32. desc.rendererSemantic = rendererSemantic;
  33. desc.gpuVariableNames.push_back(gpuVariableName);
  34. objectParams[name] = desc;
  35. }
  36. else
  37. {
  38. SHADER_OBJECT_PARAM_DESC& desc = iterFind->second;
  39. if (desc.type != type || desc.rendererSemantic != rendererSemantic)
  40. BS_EXCEPT(InvalidParametersException, "Shader parameter with the name \"" + name + "\" already exists with different properties.");
  41. Vector<String>& gpuVariableNames = desc.gpuVariableNames;
  42. bool found = false;
  43. for (UINT32 i = 0; i < (UINT32)gpuVariableNames.size(); i++)
  44. {
  45. if (gpuVariableNames[i] == gpuVariableName)
  46. {
  47. found = true;
  48. break;
  49. }
  50. }
  51. if (!found)
  52. gpuVariableNames.push_back(gpuVariableName);
  53. }
  54. dataParams.erase(name);
  55. }
  56. void SHADER_DESC::setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, UINT32 rendererSemantic)
  57. {
  58. SHADER_PARAM_BLOCK_DESC desc;
  59. desc.name = name;
  60. desc.shared = shared;
  61. desc.usage = usage;
  62. desc.rendererSemantic = rendererSemantic;
  63. paramBlocks[name] = desc;
  64. }
  65. ShaderBase::ShaderBase(const String& name, const SHADER_DESC& desc)
  66. :mName(name), mDesc(desc)
  67. {
  68. }
  69. GpuParamType ShaderBase::getParamType(const String& name) const
  70. {
  71. auto findIterData = mDesc.dataParams.find(name);
  72. if (findIterData != mDesc.dataParams.end())
  73. return GPT_DATA;
  74. auto findIterObject = mDesc.objectParams.find(name);
  75. if (findIterObject != mDesc.objectParams.end())
  76. return GPT_OBJECT;
  77. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  78. }
  79. const SHADER_DATA_PARAM_DESC& ShaderBase::getDataParamDesc(const String& name) const
  80. {
  81. auto findIterData = mDesc.dataParams.find(name);
  82. if (findIterData != mDesc.dataParams.end())
  83. return findIterData->second;
  84. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  85. }
  86. const SHADER_OBJECT_PARAM_DESC& ShaderBase::getObjectParamDesc(const String& name) const
  87. {
  88. auto findIterObject = mDesc.objectParams.find(name);
  89. if (findIterObject != mDesc.objectParams.end())
  90. return findIterObject->second;
  91. BS_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  92. }
  93. bool ShaderBase::hasDataParam(const String& name) const
  94. {
  95. auto findIterData = mDesc.dataParams.find(name);
  96. if (findIterData != mDesc.dataParams.end())
  97. return true;
  98. return false;
  99. }
  100. bool ShaderBase::hasObjectParam(const String& name) const
  101. {
  102. auto findIterObject = mDesc.objectParams.find(name);
  103. if (findIterObject != mDesc.objectParams.end())
  104. return true;
  105. return false;
  106. }
  107. template<bool Core>
  108. TShader<Core>::TShader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueType>>& techniques)
  109. :ShaderBase(name, desc), mTechniques(techniques)
  110. { }
  111. template<bool Core>
  112. TShader<Core>::~TShader()
  113. { }
  114. template<bool Core>
  115. SPtr<typename TShader<Core>::TechniqueType> TShader<Core>::getBestTechnique() const
  116. {
  117. for (auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  118. {
  119. if ((*iter)->isSupported())
  120. {
  121. return *iter;
  122. }
  123. }
  124. return nullptr;
  125. // TODO - Low priority. Instead of returning null use an extremely simple technique that will be supported almost everywhere as a fallback.
  126. }
  127. template class TShader < false > ;
  128. template class TShader < true >;
  129. ShaderCore::ShaderCore(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  130. :TShader(name, desc, techniques)
  131. {
  132. }
  133. SPtr<ShaderCore> ShaderCore::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<TechniqueCore>>& techniques)
  134. {
  135. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(name, desc, techniques);
  136. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore, GenAlloc>(shaderCore);
  137. shaderCorePtr->_setThisPtr(shaderCorePtr);
  138. shaderCorePtr->initialize();
  139. return shaderCorePtr;
  140. }
  141. Shader::Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  142. :TShader(name, desc, techniques)
  143. {
  144. }
  145. SPtr<ShaderCore> Shader::getCore() const
  146. {
  147. return std::static_pointer_cast<ShaderCore>(mCoreSpecific);
  148. }
  149. SPtr<CoreObjectCore> Shader::createCore() const
  150. {
  151. Vector<SPtr<TechniqueCore>> techniques;
  152. for (auto& technique : mTechniques)
  153. techniques.push_back(technique->getCore());
  154. ShaderCore* shaderCore = new (bs_alloc<ShaderCore>()) ShaderCore(mName, mDesc, techniques);
  155. SPtr<ShaderCore> shaderCorePtr = bs_shared_ptr<ShaderCore, GenAlloc>(shaderCore);
  156. shaderCorePtr->_setThisPtr(shaderCorePtr);
  157. return shaderCorePtr;
  158. }
  159. bool Shader::isSampler(GpuParamObjectType type)
  160. {
  161. switch(type)
  162. {
  163. case GPOT_SAMPLER1D:
  164. case GPOT_SAMPLER2D:
  165. case GPOT_SAMPLER3D:
  166. case GPOT_SAMPLERCUBE:
  167. case GPOT_SAMPLER2DMS:
  168. return true;
  169. }
  170. return false;
  171. }
  172. bool Shader::isTexture(GpuParamObjectType type)
  173. {
  174. switch(type)
  175. {
  176. case GPOT_TEXTURE1D:
  177. case GPOT_TEXTURE2D:
  178. case GPOT_TEXTURE3D:
  179. case GPOT_TEXTURECUBE:
  180. case GPOT_TEXTURE2DMS:
  181. return true;
  182. }
  183. return false;
  184. }
  185. bool Shader::isBuffer(GpuParamObjectType type)
  186. {
  187. switch(type)
  188. {
  189. case GPOT_BYTE_BUFFER:
  190. case GPOT_STRUCTURED_BUFFER:
  191. case GPOT_RWBYTE_BUFFER:
  192. case GPOT_RWAPPEND_BUFFER:
  193. case GPOT_RWCONSUME_BUFFER:
  194. case GPOT_RWSTRUCTURED_BUFFER:
  195. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  196. case GPOT_RWTYPED_BUFFER:
  197. return true;
  198. }
  199. return false;
  200. }
  201. HShader Shader::create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques)
  202. {
  203. ShaderPtr newShader = bs_core_ptr<Shader, PoolAlloc>(new (bs_alloc<Shader, PoolAlloc>()) Shader(name, desc, techniques));
  204. newShader->_setThisPtr(newShader);
  205. newShader->initialize();
  206. return static_resource_cast<Shader>(gResources()._createResourceHandle(newShader));
  207. }
  208. ShaderPtr Shader::createEmpty()
  209. {
  210. ShaderPtr newShader = bs_core_ptr<Shader, PoolAlloc>(new (bs_alloc<Shader, PoolAlloc>()) Shader());
  211. newShader->_setThisPtr(newShader);
  212. return newShader;
  213. }
  214. RTTITypeBase* Shader::getRTTIStatic()
  215. {
  216. return ShaderRTTI::instance();
  217. }
  218. RTTITypeBase* Shader::getRTTI() const
  219. {
  220. return Shader::getRTTIStatic();
  221. }
  222. }