CmPass.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmCommon.h"
  4. #include "CmColor.h"
  5. #include "CmIReflectable.h"
  6. namespace CamelotEngine
  7. {
  8. /** Class defining a single pass of a Technique (of a Material), i.e.
  9. a single rendering call.
  10. @remarks
  11. Rendering can be repeated with many passes for more complex effects.
  12. Each pass is a programmable pass (meaning it does
  13. use either a vertex and fragment program, or both).
  14. */
  15. class CM_EXPORT Pass : public IReflectable
  16. {
  17. protected:
  18. //-------------------------------------------------------------------------
  19. // Blending factors
  20. SceneBlendFactor mSourceBlendFactor;
  21. SceneBlendFactor mDestBlendFactor;
  22. SceneBlendFactor mSourceBlendFactorAlpha;
  23. SceneBlendFactor mDestBlendFactorAlpha;
  24. // Used to determine if separate alpha blending should be used for color and alpha channels
  25. bool mSeparateBlend;
  26. //-------------------------------------------------------------------------
  27. // Blending operations
  28. SceneBlendOperation mBlendOperation;
  29. SceneBlendOperation mAlphaBlendOperation;
  30. // Determines if we should use separate blending operations for color and alpha channels
  31. bool mSeparateBlendOperation;
  32. //-------------------------------------------------------------------------
  33. // Depth buffer settings
  34. bool mDepthCheck;
  35. bool mDepthWrite;
  36. CompareFunction mDepthFunc;
  37. float mDepthBiasConstant;
  38. float mDepthBiasSlopeScale;
  39. float mDepthBiasPerIteration;
  40. // Colour buffer settings
  41. bool mColourWrite;
  42. // Alpha reject settings
  43. CompareFunction mAlphaRejectFunc;
  44. unsigned char mAlphaRejectVal;
  45. bool mAlphaToCoverageEnabled;
  46. //-------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------
  48. // Culling mode
  49. CullingMode mCullMode;
  50. //-------------------------------------------------------------------------
  51. /// Polygon mode
  52. PolygonMode mPolygonMode;
  53. //-------------------------------------------------------------------------
  54. // Vertex program
  55. GpuProgramHandle mVertexProgram;
  56. // Fragment program
  57. GpuProgramHandle mFragmentProgram;
  58. // Geometry program
  59. GpuProgramHandle mGeometryProgram;
  60. // point size, applies when not using per-vertex point size
  61. float mPointSize;
  62. float mPointMinSize;
  63. float mPointMaxSize;
  64. public:
  65. /// Default constructor
  66. Pass();
  67. /// Copy constructor
  68. Pass(const Pass& oth );
  69. /// Operator = overload
  70. Pass& operator=(const Pass& oth);
  71. virtual ~Pass();
  72. /// Returns true if this pass uses a programmable vertex pipeline
  73. bool hasVertexProgram(void) const { return mVertexProgram != nullptr; }
  74. /// Returns true if this pass uses a programmable fragment pipeline
  75. bool hasFragmentProgram(void) const { return mFragmentProgram != nullptr; }
  76. /// Returns true if this pass uses a programmable geometry pipeline
  77. bool hasGeometryProgram(void) const { return mGeometryProgram != nullptr; }
  78. /** Gets the point size of the pass.
  79. @remarks
  80. This property determines what point size is used to render a point
  81. list.
  82. */
  83. float getPointSize(void) const;
  84. /** Sets the point size of this pass.
  85. @remarks
  86. This setting allows you to change the size of points when rendering
  87. a point list, or a list of point sprites. The interpretation of this
  88. command depends on the Pass::setPointSizeAttenuation option - if it
  89. is off (the default), the point size is in screen pixels, if it is on,
  90. it expressed as normalised screen coordinates (1.0 is the height of
  91. the screen) when the point is at the origin.
  92. @note
  93. Some drivers have an upper limit on the size of points they support
  94. - this can even vary between APIs on the same card! Don't rely on
  95. point sizes that cause the point sprites to get very large on screen,
  96. since they may get clamped on some cards. Upper sizes can range from
  97. 64 to 256 pixels.
  98. */
  99. void setPointSize(float ps);
  100. /** Set the minimum point size, when point attenuation is in use. */
  101. void setPointMinSize(float min);
  102. /** Get the minimum point size, when point attenuation is in use. */
  103. float getPointMinSize(void) const;
  104. /** Set the maximum point size, when point attenuation is in use.
  105. @remarks Setting this to 0 indicates the max size supported by the card.
  106. */
  107. void setPointMaxSize(float max);
  108. /** Get the maximum point size, when point attenuation is in use.
  109. @remarks 0 indicates the max size supported by the card.
  110. */
  111. float getPointMaxSize(void) const;
  112. /** Allows very fine control of blending this Pass with the existing contents of the scene.
  113. @remarks
  114. Whereas the texture blending operations seen in the TextureUnitState class are concerned with
  115. blending between texture layers, this blending is about combining the output of the material
  116. as a whole with the existing contents of the rendering target. This blending therefore allows
  117. object transparency and other special effects.
  118. @par
  119. This version of the method allows complete control over the blending operation, by specifying the
  120. source and destination blending factors. The result of the blending operation is:
  121. <span align="center">
  122. final = (texture * sourceFactor) + (pixel * destFactor)
  123. </span>
  124. @par
  125. Each of the factors is specified as one of a number of options, as specified in the SceneBlendFactor
  126. enumerated type.
  127. @param
  128. sourceFactor The source factor in the above calculation, i.e. multiplied by the texture colour components.
  129. @param
  130. destFactor The destination factor in the above calculation, i.e. multiplied by the pixel colour components.
  131. @note
  132. This method is applicable for both the fixed-function and programmable pipelines.
  133. */
  134. void setSceneBlending( const SceneBlendFactor sourceFactor, const SceneBlendFactor destFactor);
  135. /** Allows very fine control of blending this Pass with the existing contents of the scene.
  136. @remarks
  137. Wheras the texture blending operations seen in the TextureUnitState class are concerned with
  138. blending between texture layers, this blending is about combining the output of the material
  139. as a whole with the existing contents of the rendering target. This blending therefore allows
  140. object transparency and other special effects.
  141. @par
  142. This version of the method allows complete control over the blending operation, by specifying the
  143. source and destination blending factors. The result of the blending operation is:
  144. <span align="center">
  145. final = (texture * sourceFactor) + (pixel * destFactor)
  146. </span>
  147. @par
  148. Each of the factors is specified as one of a number of options, as specified in the SceneBlendFactor
  149. enumerated type.
  150. @param
  151. sourceFactor The source factor in the above calculation, i.e. multiplied by the texture colour components.
  152. @param
  153. destFactor The destination factor in the above calculation, i.e. multiplied by the pixel colour components.
  154. @param
  155. sourceFactorAlpha The alpha source factor in the above calculation, i.e. multiplied by the texture alpha component.
  156. @param
  157. destFactorAlpha The alpha destination factor in the above calculation, i.e. multiplied by the pixel alpha component.
  158. @note
  159. This method is applicable for both the fixed-function and programmable pipelines.
  160. */
  161. void setSeparateSceneBlending( const SceneBlendFactor sourceFactor, const SceneBlendFactor destFactor, const SceneBlendFactor sourceFactorAlpha, const SceneBlendFactor destFactorAlpha );
  162. /** Return true if this pass uses separate scene blending */
  163. bool hasSeparateSceneBlending() const;
  164. /** Retrieves the source blending factor for the material (as set using Materiall::setSceneBlending).
  165. */
  166. SceneBlendFactor getSourceBlendFactor() const;
  167. /** Retrieves the destination blending factor for the material (as set using Materiall::setSceneBlending).
  168. */
  169. SceneBlendFactor getDestBlendFactor() const;
  170. /** Retrieves the alpha source blending factor for the material (as set using Materiall::setSeparateSceneBlending).
  171. */
  172. SceneBlendFactor getSourceBlendFactorAlpha() const;
  173. /** Retrieves the alpha destination blending factor for the material (as set using Materiall::setSeparateSceneBlending).
  174. */
  175. SceneBlendFactor getDestBlendFactorAlpha() const;
  176. /** Sets the specific operation used to blend source and destination pixels together.
  177. @remarks
  178. By default this operation is +, which creates this equation
  179. <span align="center">
  180. final = (texture * sourceFactor) + (pixel * destFactor)
  181. </span>
  182. By setting this to something other than SBO_ADD you can change the operation to achieve
  183. a different effect.
  184. @param op The blending operation mode to use for this pass
  185. */
  186. void setSceneBlendingOperation(SceneBlendOperation op);
  187. /** Sets the specific operation used to blend source and destination pixels together.
  188. @remarks
  189. By default this operation is +, which creates this equation
  190. <span align="center">
  191. final = (texture * sourceFactor) + (pixel * destFactor)
  192. </span>
  193. By setting this to something other than SBO_ADD you can change the operation to achieve
  194. a different effect.
  195. This function allows more control over blending since it allows you to select different blending
  196. modes for the color and alpha channels
  197. @param op The blending operation mode to use for color channels in this pass
  198. @param op The blending operation mode to use for alpha channels in this pass
  199. */
  200. void setSeparateSceneBlendingOperation(SceneBlendOperation op, SceneBlendOperation alphaOp);
  201. /** Returns true if this pass uses separate scene blending operations. */
  202. bool hasSeparateSceneBlendingOperations() const;
  203. /** Returns the current blending operation */
  204. SceneBlendOperation getSceneBlendingOperation() const;
  205. /** Returns the current alpha blending operation */
  206. SceneBlendOperation getSceneBlendingOperationAlpha() const;
  207. /** Returns true if this pass has some element of transparency. */
  208. bool isTransparent(void) const;
  209. /** Sets whether or not this pass renders with depth-buffer checking on or not.
  210. @remarks
  211. If depth-buffer checking is on, whenever a pixel is about to be written to the frame buffer
  212. the depth buffer is checked to see if the pixel is in front of all other pixels written at that
  213. point. If not, the pixel is not written.
  214. @par
  215. If depth checking is off, pixels are written no matter what has been rendered before.
  216. Also see setDepthFunction for more advanced depth check configuration.
  217. @see
  218. setDepthFunction
  219. */
  220. void setDepthCheckEnabled(bool enabled);
  221. /** Returns whether or not this pass renders with depth-buffer checking on or not.
  222. @see
  223. setDepthCheckEnabled
  224. */
  225. bool getDepthCheckEnabled(void) const;
  226. /** Sets whether or not this pass renders with depth-buffer writing on or not.
  227. @remarks
  228. If depth-buffer writing is on, whenever a pixel is written to the frame buffer
  229. the depth buffer is updated with the depth value of that new pixel, thus affecting future
  230. rendering operations if future pixels are behind this one.
  231. @par
  232. If depth writing is off, pixels are written without updating the depth buffer Depth writing should
  233. normally be on but can be turned off when rendering static backgrounds or when rendering a collection
  234. of transparent objects at the end of a scene so that they overlap each other correctly.
  235. */
  236. void setDepthWriteEnabled(bool enabled);
  237. /** Returns whether or not this pass renders with depth-buffer writing on or not.
  238. @see
  239. setDepthWriteEnabled
  240. */
  241. bool getDepthWriteEnabled(void) const;
  242. /** Sets the function used to compare depth values when depth checking is on.
  243. @remarks
  244. If depth checking is enabled (see setDepthCheckEnabled) a comparison occurs between the depth
  245. value of the pixel to be written and the current contents of the buffer. This comparison is
  246. normally CMPF_LESS_EQUAL, i.e. the pixel is written if it is closer (or at the same distance)
  247. than the current contents. If you wish you can change this comparison using this method.
  248. */
  249. void setDepthFunction( CompareFunction func );
  250. /** Returns the function used to compare depth values when depth checking is on.
  251. @see
  252. setDepthFunction
  253. */
  254. CompareFunction getDepthFunction(void) const;
  255. /** Sets whether or not colour buffer writing is enabled for this Pass.
  256. @remarks
  257. For some effects, you might wish to turn off the colour write operation
  258. when rendering geometry; this means that only the depth buffer will be
  259. updated (provided you have depth buffer writing enabled, which you
  260. probably will do, although you may wish to only update the stencil
  261. buffer for example - stencil buffer state is managed at the RenderSystem
  262. level only, not the Material since you are likely to want to manage it
  263. at a higher level).
  264. */
  265. void setColourWriteEnabled(bool enabled);
  266. /** Determines if colour buffer writing is enabled for this pass. */
  267. bool getColourWriteEnabled(void) const;
  268. /** Sets the culling mode for this pass based on the 'vertex winding'.
  269. @remarks
  270. A typical way for the rendering engine to cull triangles is based on the 'vertex winding' of
  271. triangles. Vertex winding refers to the direction in which the vertices are passed or indexed
  272. to in the rendering operation as viewed from the camera, and will wither be clockwise or
  273. anticlockwise (that's 'counterclockwise' for you Americans out there ;) The default is
  274. CULL_CLOCKWISE i.e. that only triangles whose vertices are passed/indexed in anticlockwise order
  275. are rendered - this is a common approach and is used in 3D studio models for example. You can
  276. alter this culling mode if you wish but it is not advised unless you know what you are doing.
  277. @par
  278. You may wish to use the CULL_NONE option for mesh data that you cull yourself where the vertex
  279. winding is uncertain.
  280. */
  281. void setCullingMode( CullingMode mode );
  282. /** Returns the culling mode for geometry rendered with this pass. See setCullingMode for more information.
  283. */
  284. CullingMode getCullingMode(void) const;
  285. /** Sets the type of polygon rendering required
  286. @note
  287. The default shading method is Solid
  288. */
  289. void setPolygonMode( PolygonMode mode );
  290. /** Returns the type of light shading to be used.
  291. */
  292. PolygonMode getPolygonMode(void) const;
  293. /** Sets the depth bias to be used for this material.
  294. @remarks
  295. When polygons are coplanar, you can get problems with 'depth fighting' where
  296. the pixels from the two polys compete for the same screen pixel. This is particularly
  297. a problem for decals (polys attached to another surface to represent details such as
  298. bulletholes etc.).
  299. @par
  300. A way to combat this problem is to use a depth bias to adjust the depth buffer value
  301. used for the decal such that it is slightly higher than the true value, ensuring that
  302. the decal appears on top. There are two aspects to the biasing, a constant
  303. bias value and a slope-relative biasing value, which varies according to the
  304. maximum depth slope relative to the camera, ie:
  305. <pre>finalBias = maxSlope * slopeScaleBias + constantBias</pre>
  306. Note that slope scale bias, whilst more accurate, may be ignored by old hardware.
  307. @param constantBias The constant bias value, expressed as a factor of the
  308. minimum observable depth
  309. @param slopeScaleBias The slope-relative bias value, expressed as a factor
  310. of the depth slope
  311. */
  312. void setDepthBias(float constantBias, float slopeScaleBias = 0.0f);
  313. /** Retrieves the const depth bias value as set by setDepthBias. */
  314. float getDepthBiasConstant(void) const;
  315. /** Retrieves the slope-scale depth bias value as set by setDepthBias. */
  316. float getDepthBiasSlopeScale(void) const;
  317. /** Sets a factor which derives an additional depth bias from the number
  318. of times a pass is iterated.
  319. @remarks
  320. The Final depth bias will be the constant depth bias as set through
  321. setDepthBias, plus this value times the iteration number.
  322. */
  323. void setIterationDepthBias(float biasPerIteration);
  324. /** Gets a factor which derives an additional depth bias from the number
  325. of times a pass is iterated.
  326. */
  327. float getIterationDepthBias() const;
  328. /** Sets the way the pass will have use alpha to totally reject pixels from the pipeline.
  329. @remarks
  330. The default is CMPF_ALWAYS_PASS i.e. alpha is not used to reject pixels.
  331. @param func The comparison which must pass for the pixel to be written.
  332. @param value 1 byte value against which alpha values will be tested(0-255)
  333. @param alphaToCoverageEnabled Whether to enable alpha to coverage support
  334. @note
  335. This option applies in both the fixed function and the programmable pipeline.
  336. */
  337. void setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverageEnabled = false);
  338. /** Sets the alpha reject function. See setAlphaRejectSettings for more information.
  339. */
  340. void setAlphaRejectFunction(CompareFunction func);
  341. /** Gets the alpha reject value. See setAlphaRejectSettings for more information.
  342. */
  343. void setAlphaRejectValue(unsigned char val);
  344. /** Gets the alpha reject function. See setAlphaRejectSettings for more information.
  345. */
  346. CompareFunction getAlphaRejectFunction(void) const { return mAlphaRejectFunc; }
  347. /** Gets the alpha reject value. See setAlphaRejectSettings for more information.
  348. */
  349. unsigned char getAlphaRejectValue(void) const { return mAlphaRejectVal; }
  350. /** Sets whether to use alpha to coverage (A2C) when blending alpha rejected values.
  351. @remarks
  352. Alpha to coverage performs multisampling on the edges of alpha-rejected
  353. textures to produce a smoother result. It is only supported when multisampling
  354. is already enabled on the render target, and when the hardware supports
  355. alpha to coverage (see RenderSystemCapabilities).
  356. */
  357. void setAlphaToCoverageEnabled(bool enabled);
  358. /** Gets whether to use alpha to coverage (A2C) when blending alpha rejected values.
  359. */
  360. bool isAlphaToCoverageEnabled() const { return mAlphaToCoverageEnabled; }
  361. /** Sets the details of the vertex program to use.
  362. */
  363. void setVertexProgram(GpuProgramHandle gpuProgram);
  364. /** Gets the vertex program used by this pass, only available after _load(). */
  365. const GpuProgramHandle& getVertexProgram(void) const;
  366. /** Sets the details of the fragment program to use.
  367. */
  368. void setFragmentProgram(GpuProgramHandle gpuProgram);
  369. /** Gets the fragment program used by this pass, only available after _load(). */
  370. const GpuProgramHandle& getFragmentProgram(void) const;
  371. /** Sets the details of the geometry program to use.
  372. */
  373. void setGeometryProgram(GpuProgramHandle gpuProgram);
  374. /** Gets the geometry program used by this pass, only available after _load(). */
  375. const GpuProgramHandle& getGeometryProgram(void) const;
  376. /************************************************************************/
  377. /* RTTI */
  378. /************************************************************************/
  379. public:
  380. friend class PassRTTI;
  381. static RTTITypeBase* getRTTIStatic();
  382. virtual RTTITypeBase* getRTTI() const;
  383. };
  384. }