BsShader.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. #include "BsStringID.h"
  5. #include "BsResourceMetaData.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Describes a single data (int, Vector2, etc.) shader parameter.
  10. *
  11. * @see Shader::addParameter.
  12. */
  13. struct SHADER_DATA_PARAM_DESC
  14. {
  15. String name;
  16. String gpuVariableName;
  17. GpuParamDataType type;
  18. StringID rendererSemantic;
  19. UINT32 arraySize;
  20. UINT32 elementSize;
  21. UINT32 defaultValueIdx;
  22. };
  23. /**
  24. * @brief Describes a single object (texture, sampler state, etc.) shader parameter.
  25. *
  26. * @see Shader::addParameter.
  27. */
  28. struct SHADER_OBJECT_PARAM_DESC
  29. {
  30. String name;
  31. Vector<String> gpuVariableNames;
  32. StringID rendererSemantic;
  33. GpuParamObjectType type;
  34. UINT32 defaultValueIdx;
  35. };
  36. /**
  37. * @brief Describes a shader parameter block.
  38. */
  39. struct SHADER_PARAM_BLOCK_DESC
  40. {
  41. String name;
  42. bool shared;
  43. StringID rendererSemantic;
  44. GpuParamBlockUsage usage;
  45. };
  46. /**
  47. * @brief Structure used for initializing a shader.
  48. */
  49. template<bool Core>
  50. struct BS_CORE_EXPORT TSHADER_DESC
  51. {
  52. template<bool Core> struct TTextureType {};
  53. template<> struct TTextureType < false > { typedef HTexture Type; };
  54. template<> struct TTextureType < true > { typedef SPtr<TextureCore> Type; };
  55. template<bool Core> struct TSamplerStateType {};
  56. template<> struct TSamplerStateType < false > { typedef SamplerStatePtr Type; };
  57. template<> struct TSamplerStateType < true > { typedef SPtr<SamplerStateCore> Type; };
  58. typedef typename TTextureType<Core>::Type TextureType;
  59. typedef typename TSamplerStateType<Core>::Type SamplerStateType;
  60. TSHADER_DESC();
  61. /**
  62. * @brief Registers a new data (int, Vector2, etc.) parameter you that you may then use
  63. * via Material by providing the parameter name. All parameters internally map to
  64. * variables defined in GPU programs.
  65. *
  66. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  67. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  68. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  69. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  70. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  71. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  72. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  73. * @param arraySize (optional) If the parameter is an array, the number of elements in the array. Size of 1 means its not an array.
  74. * @param elementSize (optional) Size of an individual element in the array, in bytes. You only need to set this if you are setting variable
  75. * length parameters, like structs.
  76. * @param defaultValue (optional) Pointer to the buffer containing the default value for this parameter (initial value that will be set
  77. * when a material is initialized with this shader). The provided buffer must be of the correct size (depending on the
  78. * element type and array size).
  79. *
  80. * @note If multiple parameters are given with the same name but different types behavior is undefined.
  81. */
  82. void addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, StringID rendererSemantic = StringID::NONE,
  83. UINT32 arraySize = 1, UINT32 elementSize = 0, UINT8* defaultValue = nullptr);
  84. /**
  85. * @brief Registers a new object (texture, sampler state, etc.) parameter you that you may then use
  86. * via Material by providing the parameter name. All parameters internally map to variables defined in GPU programs.
  87. * Multiple GPU variables may be mapped to a single parameter in which case the first variable actually found in the program
  88. * will be used while others will be ignored.
  89. *
  90. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  91. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  92. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  93. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  94. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  95. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  96. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  97. *
  98. * @note If multiple parameters are given with the same name but different types behavior is undefined.
  99. * You are allowed to call this method multiple times in order to map multiple GPU variable names to a single parameter,
  100. * but the default value (if any) will only be recognized on the first call. Mapping multiple GPU variables to a single parameter
  101. * is useful when you are defining a shader that supports techniques across different render systems where GPU variable names for
  102. * the same parameters might differ.
  103. */
  104. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic = StringID::NONE);
  105. /**
  106. * @see SHADER_DESC::addParameter(const String&, const String&, GpuParamObjectType, StringID)
  107. *
  108. * @note Specialized version of addParameter that accepts a default sampler value that will be used for
  109. * initializing the object parameter upon Material creation. Default sampler value is only valid
  110. * if the object type is one of the sampler types.
  111. */
  112. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type,
  113. const SamplerStateType& defaultValue, StringID rendererSemantic = StringID::NONE);
  114. /**
  115. * @see SHADER_DESC::addParameter(const String&, const String&, GpuParamObjectType, StringID)
  116. *
  117. * @note Specialized version of addParameter that accepts a default texture value that will be used for
  118. * initializing the object parameter upon Material creation. Default texture value is only valid
  119. * if the object type is one of the texture types.
  120. */
  121. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type,
  122. const TextureType& defaultValue, StringID rendererSemantic = StringID::NONE);
  123. /**
  124. * @brief Changes parameters of a parameter block with the specified name.
  125. *
  126. * @param name Name of the parameter block. This should correspond with the name specified in the GPU program code.
  127. * @param shared If parameter block is marked as shared it will not be automatically created by the Material. You will need
  128. * to create it elsewhere and then assign it manually.
  129. * @param usage Specified how often do we plan on modifying the buffer, which determines how is the buffer internally stored
  130. * for best performance.
  131. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter block in the renderer. The actual value of the
  132. * semantic depends on the current Renderer and its supported list of semantics. Elements with a renderer semantic
  133. * will not have their parameter block automatically created (similar to "shared" argument), but instead a Renderer will
  134. * create an assign it instead. Be aware that renderers have strict policies on what and how are parameters stored in the
  135. * buffer and you will need to respect them. If you don't respect them your shader will be deemed incompatible and won't be used.
  136. * Value of 0 signifies the parameter block is not used by the renderer.
  137. */
  138. void setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, StringID rendererSemantic = StringID::NONE);
  139. /**
  140. * @brief sorting type to use when performing sort in the render queue. Default value is sort front to back
  141. * which causes least overdraw and is preferable. Transparent objects need to be sorted back to front.
  142. * You may also specify no sorting and the elements will be rendered in the order they were added to the
  143. * render queue.
  144. */
  145. QueueSortType queueSortType;
  146. /**
  147. * @brief Priority that allows you to control in what order are your shaders rendered.
  148. * See "QueuePriority" for a list of initial values. Shaders with higher priority will be
  149. * rendered before shaders with lower priority, and additionally render queue will only sort
  150. * elements within the same priority group.
  151. *
  152. * @note This is useful when you want all your opaque objects to be rendered before you start
  153. * drawing your transparent ones. Or to render your overlays after everything else. Values
  154. * provided in "QueuePriority" are just for general guidance and feel free to increase them
  155. * or decrease them for finer tuning. (e.g. "QueuePriority::Opaque + 1").
  156. */
  157. INT32 queuePriority;
  158. /**
  159. * @brief Enables or disables separable passes. When separable passes are disabled
  160. * all shader passes will be executed in a sequence one after another. If it is disabled
  161. * the renderer is free to mix and match passes from different objects to achieve best
  162. * performance. (They will still be executed in sequence, but some other object may
  163. * be rendered in-between passes)
  164. *
  165. * @note Shaders with transparency generally can't be separable, while opaque can.
  166. */
  167. bool separablePasses;
  168. Map<String, SHADER_DATA_PARAM_DESC> dataParams;
  169. Map<String, SHADER_OBJECT_PARAM_DESC> textureParams;
  170. Map<String, SHADER_OBJECT_PARAM_DESC> bufferParams;
  171. Map<String, SHADER_OBJECT_PARAM_DESC> samplerParams;
  172. Map<String, SHADER_PARAM_BLOCK_DESC> paramBlocks;
  173. Vector<UINT8> dataDefaultValues;
  174. Vector<SamplerStateType> samplerDefaultValues;
  175. Vector<TextureType> textureDefaultValues;
  176. private:
  177. /**
  178. * @copydoc addParameter(const String&, const String&, GpuParamObjectType, StringID)
  179. *
  180. * @note Common method shared by different addParameter overloads.
  181. */
  182. void addParameterInternal(const String& name, const String& gpuVariableName, GpuParamObjectType type, StringID rendererSemantic, UINT32 defaultValueIdx);
  183. };
  184. typedef TSHADER_DESC<true> SHADER_DESC_CORE;
  185. typedef TSHADER_DESC<false> SHADER_DESC;
  186. /**
  187. * @brief Shader represents a collection of techniques. They are used in Materials,
  188. * which can be considered as instances of a Shader. Multiple materials
  189. * may share the same shader but provide different parameters to it.
  190. *
  191. * Shader will always choose the first supported technique based on the current render
  192. * system, render manager and other properties. So make sure to add most important techniques
  193. * first so you make sure they are used if they are supported.
  194. *
  195. * @note Templated version of Shader used for implementing both
  196. * sim and core thread variants.
  197. */
  198. template<bool Core>
  199. class BS_CORE_EXPORT TShader
  200. {
  201. public:
  202. template<bool Core> struct TTechniqueType {};
  203. template<> struct TTechniqueType < false > { typedef Technique Type; };
  204. template<> struct TTechniqueType < true > { typedef TechniqueCore Type; };
  205. typedef typename TTechniqueType<Core>::Type TechniqueType;
  206. typedef typename TSHADER_DESC<Core>::TextureType TextureType;
  207. typedef typename TSHADER_DESC<Core>::SamplerStateType SamplerStateType;
  208. TShader() { }
  209. TShader(const String& name, const TSHADER_DESC<Core>& desc, const Vector<SPtr<TechniqueType>>& techniques);
  210. virtual ~TShader();
  211. /**
  212. * @brief Returns the total number of techniques in this shader.
  213. */
  214. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  215. /**
  216. * @brief Gets the best supported technique based on current render and other systems.
  217. * Returns null if not a single technique is supported.
  218. */
  219. SPtr<TechniqueType> getBestTechnique() const;
  220. /**
  221. * @brief Returns currently active queue sort type.
  222. *
  223. * @see SHADER_DESC::queueSortType
  224. */
  225. QueueSortType getQueueSortType() const { return mDesc.queueSortType; }
  226. /**
  227. * @brief Returns currently active queue priority.
  228. *
  229. * @see SHADER_DESC::queuePriority
  230. */
  231. INT32 getQueuePriority() const { return mDesc.queuePriority; }
  232. /**
  233. * @brief Returns if separable passes are allowed.
  234. *
  235. * @see SHADER_DESC::separablePasses
  236. */
  237. bool getAllowSeparablePasses() const { return mDesc.separablePasses; }
  238. /**
  239. * @brief Returns type of the parameter with the specified name. Throws exception if
  240. * the parameter doesn't exist.
  241. */
  242. GpuParamType getParamType(const String& name) const;
  243. /**
  244. * @brief Returns description for a data parameter with the specified name. Throws exception if
  245. * the parameter doesn't exist.
  246. */
  247. const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const;
  248. /**
  249. * @brief Returns description for a texture parameter with the specified name. Throws exception if
  250. * the parameter doesn't exist.
  251. */
  252. const SHADER_OBJECT_PARAM_DESC& getTextureParamDesc(const String& name) const;
  253. /**
  254. * @brief Returns description for a sampler parameter with the specified name. Throws exception if
  255. * the parameter doesn't exist.
  256. */
  257. const SHADER_OBJECT_PARAM_DESC& getSamplerParamDesc(const String& name) const;
  258. /**
  259. * @brief Returns description for a buffer parameter with the specified name. Throws exception if
  260. * the parameter doesn't exist.
  261. */
  262. const SHADER_OBJECT_PARAM_DESC& getBufferParamDesc(const String& name) const;
  263. /**
  264. * @brief Checks if the parameter with the specified name exists, and is a data parameter.
  265. */
  266. bool hasDataParam(const String& name) const;
  267. /**
  268. * @brief Checks if the parameter with the specified name exists, and is a texture parameter.
  269. */
  270. bool hasTextureParam(const String& name) const;
  271. /**
  272. * @brief Checks if the parameter with the specified name exists, and is a sampler parameter.
  273. */
  274. bool hasSamplerParam(const String& name) const;
  275. /**
  276. * @brief Checks if the parameter with the specified name exists, and is a buffer parameter.
  277. */
  278. bool hasBufferParam(const String& name) const;
  279. /**
  280. * @brief Returns a map of all data parameters in the shader.
  281. */
  282. const Map<String, SHADER_DATA_PARAM_DESC>& getDataParams() const { return mDesc.dataParams; }
  283. /**
  284. * @brief Returns a map of all texture parameters in the shader.
  285. */
  286. const Map<String, SHADER_OBJECT_PARAM_DESC>& getTextureParams() const { return mDesc.textureParams; }
  287. /**
  288. * @brief Returns a map of all buffer parameters in the shader.
  289. */
  290. const Map<String, SHADER_OBJECT_PARAM_DESC>& getBufferParams() const { return mDesc.bufferParams; }
  291. /**
  292. * @brief Returns a map of all sampler parameters in the shader.
  293. */
  294. const Map<String, SHADER_OBJECT_PARAM_DESC>& getSamplerParams() const { return mDesc.samplerParams; }
  295. /**
  296. * @brief Returns a map of all parameter blocks.
  297. */
  298. const Map<String, SHADER_PARAM_BLOCK_DESC>& getParamBlocks() const { return mDesc.paramBlocks; }
  299. /**
  300. * @brief Returns a default texture for a parameter that has the specified
  301. * default value index (retrieved from the parameters descriptor).
  302. */
  303. TextureType getDefaultTexture(UINT32 index) const;
  304. /**
  305. * @brief Returns a default sampler state for a parameter that has the specified
  306. * default value index (retrieved from the parameters descriptor).
  307. */
  308. SamplerStateType getDefaultSampler(UINT32 index) const;
  309. /**
  310. * @brief Returns a pointer to the internal buffer containing the default
  311. * value for a data parameter that has the specified default value index
  312. * (retrieved from the parameters descriptor).
  313. */
  314. UINT8* getDefaultValue(UINT32 index) const;
  315. protected:
  316. String mName;
  317. TSHADER_DESC<Core> mDesc;
  318. Vector<SPtr<TechniqueType>> mTechniques;
  319. };
  320. /**
  321. * @copydoc ShaderBase
  322. */
  323. class BS_CORE_EXPORT ShaderCore : public CoreObjectCore, public TShader<true>
  324. {
  325. public:
  326. /**
  327. * @copydoc Shader::create
  328. */
  329. static SPtr<ShaderCore> create(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques);
  330. protected:
  331. friend class Shader;
  332. ShaderCore(const String& name, const SHADER_DESC_CORE& desc, const Vector<SPtr<TechniqueCore>>& techniques);
  333. };
  334. /**
  335. * @copydoc ShaderBase
  336. */
  337. class BS_CORE_EXPORT Shader : public Resource, public TShader<false>
  338. {
  339. public:
  340. /**
  341. * @brief Retrieves an implementation of a shader usable only from the
  342. * core thread.
  343. */
  344. SPtr<ShaderCore> getCore() const;
  345. /**
  346. * @brief Sets a list include file paths that are referenced by this shader.
  347. *
  348. * @note This is not used directly by the shader as includes are expected to be
  349. * processed during GPU program and state creation, but it may be referenced
  350. * by higher layers for various purposes.
  351. */
  352. void setIncludeFiles(const Vector<String>& includes);
  353. /**
  354. * @brief Checks is the provided object type a sampler.
  355. */
  356. static bool isSampler(GpuParamObjectType type);
  357. /**
  358. * @brief Checks is the provided object type a texture.
  359. */
  360. static bool isTexture(GpuParamObjectType type);
  361. /**
  362. * @brief Checks is the provided object type a buffer.
  363. */
  364. static bool isBuffer(GpuParamObjectType type);
  365. /**
  366. * @brief Returns the size in bytes for a specific data type.
  367. *
  368. * @note Returns 0 for variable size types like structures.
  369. */
  370. static UINT32 getDataParamSize(GpuParamDataType type);
  371. /**
  372. * @brief Creates a new shader resource using the provided descriptor and techniques.
  373. */
  374. static HShader create(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques);
  375. /**
  376. * @brief Creates a new shader object using the provided descriptor and techniques.
  377. *
  378. * @note Internal method.
  379. */
  380. static ShaderPtr _createPtr(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques);
  381. /**
  382. * @brief Returns a shader object but doesn't initialize it.
  383. */
  384. static ShaderPtr createEmpty();
  385. private:
  386. Shader(const String& name, const SHADER_DESC& desc, const Vector<SPtr<Technique>>& techniques);
  387. /**
  388. * @copydoc CoreObject::getCoreDependencies
  389. */
  390. void getCoreDependencies(Vector<SPtr<CoreObject>>& dependencies);
  391. /**
  392. * @copydoc CoreObject::createCore
  393. */
  394. SPtr<CoreObjectCore> createCore() const;
  395. /**
  396. * @brief Converts a sim thread version of the shader descriptor to
  397. * a core thread version.
  398. */
  399. SHADER_DESC_CORE convertDesc(const SHADER_DESC& desc) const;
  400. private:
  401. /************************************************************************/
  402. /* RTTI */
  403. /************************************************************************/
  404. Shader() { }
  405. public:
  406. friend class ShaderRTTI;
  407. static RTTITypeBase* getRTTIStatic();
  408. virtual RTTITypeBase* getRTTI() const;
  409. };
  410. /**
  411. * @brief Shader specific resource meta-data containing information
  412. * about referenced include files.
  413. */
  414. class BS_CORE_EXPORT ShaderMetaData : public ResourceMetaData
  415. {
  416. public:
  417. Vector<String> includes;
  418. /************************************************************************/
  419. /* SERIALIZATION */
  420. /************************************************************************/
  421. public:
  422. friend class ShaderMetaDataRTTI;
  423. static RTTITypeBase* getRTTIStatic();
  424. virtual RTTITypeBase* getRTTI() const;
  425. };
  426. }