BsShader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 ShaderBase
  60. {
  61. public:
  62. virtual ~ShaderBase() { }
  63. /**
  64. * @brief Adds a new technique that supports the provided render system
  65. * and renderer to the shader. It's up to the caller to populate the
  66. * returned object with valid data.
  67. */
  68. TechniquePtr addTechnique(const String& renderSystem, const String& renderer);
  69. /**
  70. * @brief Removes a technique at the specified index.
  71. */
  72. void removeTechnique(UINT32 idx);
  73. /**
  74. * @brief Removes the specified technique.
  75. */
  76. void removeTechnique(TechniquePtr technique);
  77. /**
  78. * @brief Returns the total number of techniques in this shader.
  79. */
  80. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  81. /**
  82. * @brief Gets the best supported technique based on current render and other systems.
  83. * Returns null if not a single technique is supported.
  84. */
  85. TechniquePtr getBestTechnique() const;
  86. /**
  87. * @brief Sets sorting type to use when performing sort in the render queue. Default value is sort front to back
  88. * which causes least overdraw and is preferable. Transparent objects need to be sorted back to front.
  89. * You may also specify no sorting and the elements will be rendered in the order they were added to the
  90. * render queue.
  91. */
  92. void setQueueSortType(QueueSortType sortType);
  93. /**
  94. * @brief Sets a priority that allows you to control in what order are your shaders rendered.
  95. * See "QueuePriority" for a list of initial values. Shaders with higher priority will be
  96. * rendered before shaders with lower priority, and additionally render queue will only sort
  97. * elements within the same priority group.
  98. *
  99. * @note This is useful when you want all your opaque objects to be rendered before you start
  100. * drawing your transparent ones. Or to render your overlays after everything else. Values
  101. * provided in "QueuePriority" are just for general guidance and feel free to increase them
  102. * or decrease them for finer tuning. (e.g. "QueuePriority::Opaque + 1").
  103. */
  104. void setQueuePriority(UINT32 priority);
  105. /**
  106. * @brief Enables or disables separable passes. When separable passes are disabled
  107. * all shader passes will be executed in a sequence one after another. If it is disabled
  108. * the renderer is free to mix and match passes from different objects to achieve best
  109. * performance. (They will still be executed in sequence, but some other object may
  110. * be rendered in-between passes)
  111. *
  112. * @note Shaders with transparency generally can't be separable, while opaque can.
  113. */
  114. void setAllowSeparablePasses(bool enable);
  115. /**
  116. * @brief Returns currently active queue sort type.
  117. *
  118. * @see setQueueSortType
  119. */
  120. QueueSortType getQueueSortType() const { return mQueueSortType; }
  121. /**
  122. * @brief Returns currently active queue priority.
  123. *
  124. * @see setQueuePriority
  125. */
  126. UINT32 getQueuePriority() const { return mQueuePriority; }
  127. /**
  128. * @brief Returns if separable passes are allowed.
  129. *
  130. * @see setAllowSeparablePasses
  131. */
  132. bool getAllowSeparablePasses() const { return mSeparablePasses; }
  133. /**
  134. * @brief Registers a new data (int, Vector2, etc.) parameter you that you may then use
  135. * via Material by providing the parameter name. All parameters internally map to
  136. * variables defined in GPU programs.
  137. *
  138. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  139. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  140. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  141. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  142. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  143. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  144. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  145. * @param arraySize (optional) If the parameter is an array, the number of elements in the array. Size of 1 means its not an array.
  146. * @param elementSize (optional) Size of an individual element in the array, in bytes. You only need to set this if you are setting variable
  147. * length parameters, like structs.
  148. */
  149. void addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, UINT32 rendererSemantic = 0,
  150. UINT32 arraySize = 1, UINT32 elementSize = 0);
  151. /**
  152. * @brief Registers a new object (texture, sampler state, etc.) parameter you that you may then use
  153. * via Material by providing the parameter name. All parameters internally map to variables defined in GPU programs.
  154. * Multiple GPU variables may be mapped to a single parameter in which case the first variable actually found in the program
  155. * will be used while others will be ignored.
  156. *
  157. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  158. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  159. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  160. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter in the renderer. The actual value of the semantic
  161. * depends on the current Renderer and its supported list of semantics. Elements with renderer semantics should not be updated
  162. * by the user, and will be updated by the renderer. These semantics will also be used to determine if a shader is compatible
  163. * with a specific renderer or not. Value of 0 signifies the parameter is not used by the renderer.
  164. *
  165. * @note Mapping multiple GPU variables to a single parameter is useful when you are defining a shader that supports techniques across different render
  166. * systems where GPU variable names for the same parameters might differ.
  167. */
  168. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, UINT32 rendererSemantic = 0);
  169. /**
  170. * @brief Unregister a parameter with the specified name.
  171. */
  172. void removeParameter(const String& name);
  173. /**
  174. * @brief Changes parameters of a parameter block with the specified name.
  175. *
  176. * @param name Name of the parameter block. This should correspond with the name specified in the GPU program code.
  177. * @param shared If parameter block is marked as shared it will not be automatically created by the Material. You will need
  178. * to create it elsewhere and then assign it manually.
  179. * @param usage Specified how often do we plan on modifying the buffer, which determines how is the buffer internally stored
  180. * for best performance.
  181. * @param rendererSemantic (optional) Semantic that allows you to specify the use of this parameter block in the renderer. The actual value of the
  182. * semantic depends on the current Renderer and its supported list of semantics. Elements with a renderer semantic
  183. * will not have their parameter block automatically created (similar to "shared" argument), but instead a Renderer will
  184. * create an assign it instead. Be aware that renderers have strict policies on what and how are parameters stored in the
  185. * 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.
  186. * Value of 0 signifies the parameter block is not used by the renderer.
  187. */
  188. void setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage, UINT32 rendererSemantic = 0);
  189. /**
  190. * @brief Returns type of the parameter with the specified name. Throws exception if
  191. * the parameter doesn't exist.
  192. */
  193. GpuParamType getParamType(const String& name) const;
  194. /**
  195. * @brief Returns description for a data parameter with the specified name. Throws exception if
  196. * the parameter doesn't exist.
  197. */
  198. const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const;
  199. /**
  200. * @brief Returns description for an object parameter with the specified name. Throws exception if
  201. * the parameter doesn't exist.
  202. */
  203. const SHADER_OBJECT_PARAM_DESC& getObjectParamDesc(const String& name) const;
  204. /**
  205. * @brief Checks if the parameter with the specified name exists, and is a data parameter.
  206. */
  207. bool hasDataParam(const String& name) const;
  208. /**
  209. * @brief Checks if the parameter with the specified name exists, and is an object parameter.
  210. */
  211. bool hasObjectParam(const String& name) const;
  212. /**
  213. * @brief Returns a map of all data parameters in the shader.
  214. */
  215. const Map<String, SHADER_DATA_PARAM_DESC>& getDataParams() const { return mDataParams; }
  216. /**
  217. * @brief Returns a map of all object parameters in the shader.
  218. */
  219. const Map<String, SHADER_OBJECT_PARAM_DESC>& getObjectParams() const { return mObjectParams; }
  220. /**
  221. * @brief Returns a map of all parameter blocks.
  222. */
  223. const Map<String, SHADER_PARAM_BLOCK_DESC>& getParamBlocks() const { return mParamBlocks; }
  224. protected:
  225. ShaderBase(const String& name);
  226. /**
  227. * @copydoc CoreObject::markCoreDirty
  228. */
  229. virtual void _markCoreDirty() { }
  230. QueueSortType mQueueSortType;
  231. UINT32 mQueuePriority;
  232. bool mSeparablePasses;
  233. Map<String, SHADER_DATA_PARAM_DESC> mDataParams;
  234. Map<String, SHADER_OBJECT_PARAM_DESC> mObjectParams;
  235. Map<String, SHADER_PARAM_BLOCK_DESC> mParamBlocks;
  236. Vector<TechniquePtr> mTechniques;
  237. String mName;
  238. };
  239. /**
  240. * @copydoc ShaderBase
  241. */
  242. class BS_CORE_EXPORT ShaderCore : public CoreObjectCore, public ShaderBase
  243. {
  244. public:
  245. /**
  246. * @copydoc Shader::create
  247. */
  248. static SPtr<ShaderCore> create(const String& name);
  249. protected:
  250. friend class Shader;
  251. ShaderCore(const String& name);
  252. /**
  253. * @copydoc CoreObjectCore::syncToCore
  254. */
  255. void syncToCore(const CoreSyncData& data);
  256. };
  257. /**
  258. * @copydoc ShaderBase
  259. */
  260. class BS_CORE_EXPORT Shader : public Resource, public ShaderBase
  261. {
  262. public:
  263. /**
  264. * @brief Retrieves an implementation of a shader usable only from the
  265. * core thread.
  266. */
  267. SPtr<ShaderCore> getCore() const;
  268. static bool isSampler(GpuParamObjectType type);
  269. static bool isTexture(GpuParamObjectType type);
  270. static bool isBuffer(GpuParamObjectType type);
  271. /**
  272. * @brief Returns an empty shader object with the specified name. Caller must register
  273. * techniques with the shader before using it in a Material.
  274. */
  275. static ShaderPtr create(const String& name);
  276. private:
  277. Shader(const String& name);
  278. /**
  279. * @copydoc CoreObject::createCore
  280. */
  281. SPtr<CoreObjectCore> createCore() const;
  282. /**
  283. * @copydoc CoreObject::markCoreDirty
  284. */
  285. void _markCoreDirty();
  286. /**
  287. * @copydoc CoreObject::syncToCore
  288. */
  289. CoreSyncData syncToCore(FrameAlloc* allocator);
  290. private:
  291. /************************************************************************/
  292. /* RTTI */
  293. /************************************************************************/
  294. public:
  295. friend class ShaderRTTI;
  296. static RTTITypeBase* getRTTIStatic();
  297. virtual RTTITypeBase* getRTTI() const;
  298. };
  299. }