BsShader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. Vector<String> gpuVariableNames;
  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. * Multiple GPU variables may be mapped to a single parameter in which case the first variable actually found in the program
  154. * will be used while others will be ignored.
  155. *
  156. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  157. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  158. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  159. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  160. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  161. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  162. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  163. *
  164. * @note Mapping multiple GPU variables to a single parameter is useful when you are defining a shader that supports techniques across different render
  165. * systems where GPU variable names for the same parameters might differ.
  166. */
  167. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, UINT32 rendererSemantic = 0);
  168. /**
  169. * @brief Unregister a parameter with the specified name.
  170. */
  171. void removeParameter(const String& name);
  172. /**
  173. * @brief Changes parameters of a parameter block with the specified name.
  174. *
  175. * @param name Name of the parameter block. This should correspond with the name specified in the GPU program code.
  176. * @param shared If parameter block is marked as shared it will not be automatically created by the Material. You will need
  177. * to create it elsewhere and then assign it manually.
  178. * @param usage Specified how often do we plan on modifying the buffer, which determines how is the buffer internally stored
  179. * for best performance.
  180. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter block in the renderer. The actual value of the
  181. * semantic depends on the current Renderer and its supported list of semantics. Elements with a renderer semantic
  182. * will not have their parameter block automatically created (similar to "shared" argument), but instead a Renderer will
  183. * create an assign it instead. Be aware that renderers have strict policies on what and how are parameters stored in the
  184. * 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.
  185. * Value of 0 signifies the parameter block is not used by the renderer.
  186. */
  187. void setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, UINT32 rendererSemantic = 0);
  188. /**
  189. * @brief Returns type of the parameter with the specified name. Throws exception if
  190. * the parameter doesn't exist.
  191. */
  192. GpuParamType getParamType(const String& name) const;
  193. /**
  194. * @brief Returns description for a data parameter with the specified name. Throws exception if
  195. * the parameter doesn't exist.
  196. */
  197. const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const;
  198. /**
  199. * @brief Returns description for an object parameter with the specified name. Throws exception if
  200. * the parameter doesn't exist.
  201. */
  202. const SHADER_OBJECT_PARAM_DESC& getObjectParamDesc(const String& name) const;
  203. /**
  204. * @brief Checks if the parameter with the specified name exists, and is a data parameter.
  205. */
  206. bool hasDataParam(const String& name) const;
  207. /**
  208. * @brief Checks if the parameter with the specified name exists, and is an object parameter.
  209. */
  210. bool hasObjectParam(const String& name) const;
  211. /**
  212. * @brief Returns a map of all data parameters in the shader.
  213. *
  214. * @note Internal method.
  215. */
  216. const Map<String, SHADER_DATA_PARAM_DESC>& _getDataParams() const { return mDataParams; }
  217. /**
  218. * @brief Returns a map of all object parameters in the shader.
  219. *
  220. * @note Internal method.
  221. */
  222. const Map<String, SHADER_OBJECT_PARAM_DESC>& _getObjectParams() const { return mObjectParams; }
  223. /**
  224. * @brief Returns a map of all parameter blocks.
  225. *
  226. * @note Internal method.
  227. */
  228. const Map<String, SHADER_PARAM_BLOCK_DESC>& _getParamBlocks() const { return mParamBlocks; }
  229. static bool isSampler(GpuParamObjectType type);
  230. static bool isTexture(GpuParamObjectType type);
  231. static bool isBuffer(GpuParamObjectType type);
  232. /**
  233. * @brief Returns an empty shader object with the specified name. Caller must register
  234. * techniques with the shader before using it in a Material.
  235. */
  236. static ShaderPtr create(const String& name);
  237. /************************************************************************/
  238. /* CORE PROXY */
  239. /************************************************************************/
  240. /**
  241. * @brief Checks is the core dirty flag set. This is used by external systems
  242. * to know when internal data has changed and core thread potentially needs to be notified.
  243. */
  244. bool _isCoreDirty(ShaderDirtyFlag flag) const { return (mCoreDirtyFlags & (UINT32)flag) != 0; }
  245. /**
  246. * @brief Marks the core dirty flag as clean.
  247. */
  248. void _markCoreClean(ShaderDirtyFlag flag) { mCoreDirtyFlags &= ~(UINT32)flag; }
  249. /**
  250. * @brief Gets the currently active proxy of this material.
  251. */
  252. ShaderProxyPtr _getActiveProxy() const { return mActiveProxy; }
  253. /**
  254. * @brief Sets an active proxy for this material.
  255. */
  256. void _setActiveProxy(const ShaderProxyPtr& proxy) { mActiveProxy = proxy; }
  257. /**
  258. * @brief Creates a new core proxy from the currently set shader data. Core proxies ensure
  259. * that the core thread has all the necessary data, while avoiding the need
  260. * to manage Shader itself on the core thread.
  261. *
  262. * @note Sim thread only.
  263. * You generally need to update the core thread with a new proxy whenever core
  264. * dirty flag is set.
  265. */
  266. ShaderProxyPtr _createProxy();
  267. private:
  268. Shader(const String& name);
  269. /**
  270. * @brief Marks the core data as dirty.
  271. */
  272. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  273. private:
  274. String mName;
  275. QueueSortType mQueueSortType;
  276. UINT32 mQueuePriority;
  277. bool mSeparablePasses;
  278. Vector<TechniquePtr> mTechniques;
  279. UINT32 mCoreDirtyFlags;
  280. Map<String, SHADER_DATA_PARAM_DESC> mDataParams;
  281. Map<String, SHADER_OBJECT_PARAM_DESC> mObjectParams;
  282. Map<String, SHADER_PARAM_BLOCK_DESC> mParamBlocks;
  283. ShaderProxyPtr mActiveProxy;
  284. /************************************************************************/
  285. /* RTTI */
  286. /************************************************************************/
  287. public:
  288. friend class ShaderRTTI;
  289. static RTTITypeBase* getRTTIStatic();
  290. virtual RTTITypeBase* getRTTI() const;
  291. };
  292. }