BsShader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Describes a single data (int, Vector2, etc.) shader parameter.
  8. *
  9. * @see Shader::addParameter.
  10. */
  11. struct BS_CORE_EXPORT SHADER_DATA_PARAM_DESC
  12. {
  13. String name;
  14. String gpuVariableName;
  15. GpuParamDataType type;
  16. UINT32 rendererSemantic;
  17. UINT32 arraySize;
  18. UINT32 elementSize;
  19. };
  20. /**
  21. * @brief Describes a single object (texture, sampler state, etc.) shader parameter.
  22. *
  23. * @see Shader::addParameter.
  24. */
  25. struct BS_CORE_EXPORT SHADER_OBJECT_PARAM_DESC
  26. {
  27. String name;
  28. String gpuVariableName;
  29. UINT32 rendererSemantic;
  30. GpuParamObjectType type;
  31. };
  32. /**
  33. * @brief Describes a shader parameter block.
  34. */
  35. struct BS_CORE_EXPORT SHADER_PARAM_BLOCK_DESC
  36. {
  37. String name;
  38. bool shared;
  39. UINT32 rendererSemantic;
  40. GpuParamBlockUsage usage;
  41. };
  42. /**
  43. * @brief Shader represents a collection of techniques. They are used in Materials,
  44. * which can be considered as instances of a Shader. Multiple materials
  45. * may share the same shader but provide different parameters to it.
  46. *
  47. * Shader will always choose the first supported technique based on the current render
  48. * system, render manager and other properties. So make sure to add most important techniques
  49. * first so you make sure they are used if they are supported.
  50. */
  51. class BS_CORE_EXPORT Shader : public Resource
  52. {
  53. public:
  54. /**
  55. * @brief Adds a new technique that supports the provided render system
  56. * and renderer to the shader. It's up to the caller to populate the
  57. * returned object with valid data.
  58. */
  59. TechniquePtr addTechnique(const String& renderSystem, const String& renderer);
  60. /**
  61. * @brief Removes a technique at the specified index.
  62. */
  63. void removeTechnique(UINT32 idx);
  64. /**
  65. * @brief Removes the specified technique.
  66. */
  67. void removeTechnique(TechniquePtr technique);
  68. /**
  69. * @brief Returns the total number of techniques in this shader.
  70. */
  71. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  72. /**
  73. * @brief Gets the best supported technique based on current render and other systems.
  74. * Returns null if not a single technique is supported.
  75. */
  76. TechniquePtr getBestTechnique() const;
  77. /**
  78. * @brief Sets sorting type to use when performing sort in the render queue. Default value is sort front to back
  79. * which causes least overdraw and is preferable. Transparent objects need to be sorted back to front.
  80. * You may also specify no sorting and the elements will be rendered in the order they were added to the
  81. * render queue.
  82. */
  83. void setQueueSortType(QueueSortType sortType);
  84. /**
  85. * @brief Sets a priority that allows you to control in what order are your shaders rendered.
  86. * See "QueuePriority" for a list of initial values. Shaders with higher priority will be
  87. * rendered before shaders with lower priority, and additionally render queue will only sort
  88. * elements within the same priority group.
  89. *
  90. * @note This is useful when you want all your opaque objects to be rendered before you start
  91. * drawing your transparent ones. Or to render your overlays after everything else. Values
  92. * provided in "QueuePriority" are just for general guidance and feel free to increase them
  93. * or decrease them for finer tuning. (e.g. "QueuePriority::Opaque + 1").
  94. */
  95. void setQueuePriority(UINT32 priority);
  96. /**
  97. * @brief Enables or disables separable passes. When separable passes are disabled
  98. * all shader passes will be executed in a sequence one after another. If it is disabled
  99. * the renderer is free to mix and match passes from different objects to achieve best
  100. * performance. (They will still be executed in sequence, but some other object may
  101. * be rendered in-between passes)
  102. *
  103. * @note Shaders with transparency generally can't be separable, while opaque can.
  104. */
  105. void setAllowSeparablePasses(bool enable);
  106. /**
  107. * @brief Returns currently active queue sort type.
  108. *
  109. * @see setQueueSortType
  110. */
  111. QueueSortType getQueueSortType() const { return mQueueSortType; }
  112. /**
  113. * @brief Returns currently active queue priority.
  114. *
  115. * @see setQueuePriority
  116. */
  117. UINT32 getQueuePriority() const { return mQueuePriority; }
  118. /**
  119. * @brief Returns if separable passes are allowed.
  120. *
  121. * @see setAllowSeparablePasses
  122. */
  123. bool getAllowSeparablePasses() const { return mSeparablePasses; }
  124. /**
  125. * @brief Registers a new data (int, Vector2, etc.) parameter you that you may then use
  126. * via Material by providing the parameter name. All parameters internally map to
  127. * variables defined in GPU programs.
  128. *
  129. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  130. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  131. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  132. * @param arraySize (optional) If the parameter is an array, the number of elements in the array. Size of 1 means its not an array.
  133. * @param elementSize (optional) Size of an individual element in the array, in bytes. You only need to set this if you are setting variable
  134. * length parameters, like structs.
  135. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  136. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  137. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  138. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  139. */
  140. void addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, UINT32 arraySize = 1,
  141. UINT32 elementSize = 0, UINT32 rendererSemantic = 0);
  142. /**
  143. * @brief Registers a new object (texture, sampler state, etc.) parameter you that you may then use
  144. * via Material by providing the parameter name. All parameters internally map to variables defined in GPU programs.
  145. *
  146. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  147. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  148. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  149. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  150. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  151. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  152. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  153. */
  154. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, UINT32 rendererSemantic = 0);
  155. /**
  156. * @brief Unregister a parameter with the specified name.
  157. */
  158. void removeParameter(const String& name);
  159. /**
  160. * @brief Changes parameters of a parameter block with the specified name.
  161. *
  162. * @param name Name of the parameter block. This should correspond with the name specified in the GPU program code.
  163. * @param shared If parameter block is marked as shared it will not be automatically created by the Material. You will need
  164. * to create it elsewhere and then assign it manually.
  165. * @param usage Specified how often do we plan on modifying the buffer, which determines how is the buffer internally stored
  166. * for best performance.
  167. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter block in the renderer. The actual value of the
  168. * semantic depends on the current Renderer and its supported list of semantics. Elements with a renderer semantic
  169. * will not have their parameter block automatically created (similar to "shared" argument), but instead a Renderer will
  170. * create an assign it instead. Be aware that renderers have strict policies on what and how are parameters stored in the
  171. * 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.
  172. * Value of 0 signifies the parameter block is not used by the renderer.
  173. */
  174. void setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, UINT32 rendererSemantic = 0);
  175. /**
  176. * @brief Returns type of the parameter with the specified name. Throws exception if
  177. * the parameter doesn't exist.
  178. */
  179. GpuParamType getParamType(const String& name) const;
  180. /**
  181. * @brief Returns description for a data parameter with the specified name. Throws exception if
  182. * the parameter doesn't exist.
  183. */
  184. const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const;
  185. /**
  186. * @brief Returns description for an object parameter with the specified name. Throws exception if
  187. * the parameter doesn't exist.
  188. */
  189. const SHADER_OBJECT_PARAM_DESC& getObjectParamDesc(const String& name) const;
  190. /**
  191. * @brief Checks if the parameter with the specified name exists, and is a data parameter.
  192. */
  193. bool hasDataParam(const String& name) const;
  194. /**
  195. * @brief Checks if the parameter with the specified name exists, and is an object parameter.
  196. */
  197. bool hasObjectParam(const String& name) const;
  198. /**
  199. * @brief Returns a map of all data parameters in the shader.
  200. *
  201. * @note Internal method.
  202. */
  203. const Map<String, SHADER_DATA_PARAM_DESC>& _getDataParams() const { return mDataParams; }
  204. /**
  205. * @brief Returns a map of all object parameters in the shader.
  206. *
  207. * @note Internal method.
  208. */
  209. const Map<String, SHADER_OBJECT_PARAM_DESC>& _getObjectParams() const { return mObjectParams; }
  210. /**
  211. * @brief Returns a map of all parameter blocks.
  212. *
  213. * @note Internal method.
  214. */
  215. const Map<String, SHADER_PARAM_BLOCK_DESC>& _getParamBlocks() const { return mParamBlocks; }
  216. /**
  217. * @brief Checks is the core dirty flag set. This is used by external systems
  218. * to know when internal data has changed and core thread potentially needs to be notified.
  219. */
  220. bool _isCoreDirty() const { return mCoreDirtyFlags != 0; }
  221. /**
  222. * @brief Marks the core dirty flag as clean.
  223. */
  224. void _markCoreClean() { mCoreDirtyFlags = 0; }
  225. static bool isSampler(GpuParamObjectType type);
  226. static bool isTexture(GpuParamObjectType type);
  227. static bool isBuffer(GpuParamObjectType type);
  228. /**
  229. * @brief Returns an empty shader object with the specified name. Caller must register
  230. * techniques with the shader before using it in a Material.
  231. */
  232. static ShaderPtr create(const String& name);
  233. private:
  234. Shader(const String& name);
  235. /**
  236. * @brief Marks the core data as dirty.
  237. */
  238. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  239. private:
  240. String mName;
  241. QueueSortType mQueueSortType;
  242. UINT32 mQueuePriority;
  243. bool mSeparablePasses;
  244. Vector<TechniquePtr> mTechniques;
  245. UINT32 mCoreDirtyFlags;
  246. Map<String, SHADER_DATA_PARAM_DESC> mDataParams;
  247. Map<String, SHADER_OBJECT_PARAM_DESC> mObjectParams;
  248. Map<String, SHADER_PARAM_BLOCK_DESC> mParamBlocks;
  249. /************************************************************************/
  250. /* RTTI */
  251. /************************************************************************/
  252. public:
  253. friend class ShaderRTTI;
  254. static RTTITypeBase* getRTTIStatic();
  255. virtual RTTITypeBase* getRTTI() const;
  256. };
  257. }