BsShader.h 13 KB

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