CmPass.h 22 KB

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