BsShader.cpp 7.0 KB

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