CCRenderState.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. Copyright (c) 2014 GamePlay3D team
  5. http://www.cocos2d-x.org
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. Ideas taken from:
  16. - GamePlay3D: http://gameplay3d.org/
  17. - OGRE3D: http://www.ogre3d.org/
  18. - Qt3D: http://qt-project.org/
  19. ****************************************************************************/
  20. #ifndef __cocos2d_libs__CCRenderState__
  21. #define __cocos2d_libs__CCRenderState__
  22. #include <string>
  23. #include <functional>
  24. #include <cstdint>
  25. #include "platform/CCPlatformMacros.h"
  26. #include "base/CCRef.h"
  27. #include "base/ccTypes.h"
  28. #include "base/CCVector.h"
  29. NS_CC_BEGIN
  30. class Texture2D;
  31. class Pass;
  32. /**
  33. * Defines the rendering state of the graphics device.
  34. */
  35. class CC_DLL RenderState : public Ref
  36. {
  37. friend class Material;
  38. friend class Technique;
  39. friend class Pass;
  40. public:
  41. /**
  42. * Static initializer that is called during game startup.
  43. */
  44. static void initialize();
  45. /**
  46. * Static finalizer that is called during game shutdown.
  47. */
  48. static void finalize();
  49. std::string getName() const;
  50. /** Texture that will use in the CC_Texture0 uniform.
  51. Added to be backwards compatible. Use Samplers from .material instead.
  52. */
  53. void setTexture(Texture2D* texture);
  54. /** Returns the texture that is going to be used for CC_Texture0.
  55. Added to be backwards compatible.
  56. */
  57. Texture2D* getTexture() const;
  58. /**
  59. * Binds the render state for this RenderState and any of its parents, top-down,
  60. * for the given pass.
  61. */
  62. void bind(Pass* pass);
  63. /**
  64. * Returns the topmost RenderState in the hierarchy below the given RenderState.
  65. */
  66. RenderState* getTopmost(RenderState* below);
  67. void setParent(RenderState* parent) { _parent = parent; }
  68. enum Blend
  69. {
  70. BLEND_ZERO = GL_ZERO,
  71. BLEND_ONE = GL_ONE,
  72. BLEND_SRC_COLOR = GL_SRC_COLOR,
  73. BLEND_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
  74. BLEND_DST_COLOR = GL_DST_COLOR,
  75. BLEND_ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
  76. BLEND_SRC_ALPHA = GL_SRC_ALPHA,
  77. BLEND_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
  78. BLEND_DST_ALPHA = GL_DST_ALPHA,
  79. BLEND_ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
  80. BLEND_CONSTANT_ALPHA = GL_CONSTANT_ALPHA,
  81. BLEND_ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA,
  82. BLEND_SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE
  83. };
  84. /**
  85. * Defines the supported depth compare functions.
  86. *
  87. * Depth compare functions specify the comparison that takes place between the
  88. * incoming pixel's depth value and the depth value already in the depth buffer.
  89. * If the compare function passes, the new pixel will be drawn.
  90. *
  91. * The initial depth compare function is DEPTH_LESS.
  92. */
  93. enum DepthFunction
  94. {
  95. DEPTH_NEVER = GL_NEVER,
  96. DEPTH_LESS = GL_LESS,
  97. DEPTH_EQUAL = GL_EQUAL,
  98. DEPTH_LEQUAL = GL_LEQUAL,
  99. DEPTH_GREATER = GL_GREATER,
  100. DEPTH_NOTEQUAL = GL_NOTEQUAL,
  101. DEPTH_GEQUAL = GL_GEQUAL,
  102. DEPTH_ALWAYS = GL_ALWAYS
  103. };
  104. /**
  105. * Defines culling criteria for front-facing, back-facing and both-side
  106. * facets.
  107. */
  108. enum CullFaceSide
  109. {
  110. CULL_FACE_SIDE_BACK = GL_BACK,
  111. CULL_FACE_SIDE_FRONT = GL_FRONT,
  112. CULL_FACE_SIDE_FRONT_AND_BACK = GL_FRONT_AND_BACK
  113. };
  114. /**
  115. * Defines the winding of vertices in faces that are considered front facing.
  116. *
  117. * The initial front face mode is set to FRONT_FACE_CCW.
  118. */
  119. enum FrontFace
  120. {
  121. FRONT_FACE_CW = GL_CW,
  122. FRONT_FACE_CCW = GL_CCW
  123. };
  124. /**
  125. * Defines the supported stencil compare functions.
  126. *
  127. * Stencil compare functions determine if a new pixel will be drawn.
  128. *
  129. * The initial stencil compare function is STENCIL_ALWAYS.
  130. */
  131. enum StencilFunction
  132. {
  133. STENCIL_NEVER = GL_NEVER,
  134. STENCIL_ALWAYS = GL_ALWAYS,
  135. STENCIL_LESS = GL_LESS,
  136. STENCIL_LEQUAL = GL_LEQUAL,
  137. STENCIL_EQUAL = GL_EQUAL,
  138. STENCIL_GREATER = GL_GREATER,
  139. STENCIL_GEQUAL = GL_GEQUAL,
  140. STENCIL_NOTEQUAL = GL_NOTEQUAL
  141. };
  142. /**
  143. * Defines the supported stencil operations to perform.
  144. *
  145. * Stencil operations determine what should happen to the pixel if the
  146. * stencil test fails, passes, or passes but fails the depth test.
  147. *
  148. * The initial stencil operation is STENCIL_OP_KEEP.
  149. */
  150. enum StencilOperation
  151. {
  152. STENCIL_OP_KEEP = GL_KEEP,
  153. STENCIL_OP_ZERO = GL_ZERO,
  154. STENCIL_OP_REPLACE = GL_REPLACE,
  155. STENCIL_OP_INCR = GL_INCR,
  156. STENCIL_OP_DECR = GL_DECR,
  157. STENCIL_OP_INVERT = GL_INVERT,
  158. STENCIL_OP_INCR_WRAP = GL_INCR_WRAP,
  159. STENCIL_OP_DECR_WRAP = GL_DECR_WRAP
  160. };
  161. /**
  162. * Defines a block of fixed-function render states that can be applied to a
  163. * RenderState object.
  164. */
  165. class CC_DLL StateBlock : public Ref
  166. {
  167. friend class RenderState;
  168. friend class Pass;
  169. friend class RenderQueue;
  170. friend class Renderer;
  171. public:
  172. /**
  173. * Creates a new StateBlock with default render state settings.
  174. */
  175. static StateBlock* create();
  176. /** The recommended way to create StateBlocks is by calling `create`.
  177. * Don't use `new` or `delete` on them.
  178. *
  179. */
  180. StateBlock();
  181. ~StateBlock();
  182. /**
  183. * Binds the state in this StateBlock to the renderer.
  184. *
  185. * This method handles both setting and restoring of render states to ensure that
  186. * only the state explicitly defined by this StateBlock is applied to the renderer.
  187. */
  188. void bind();
  189. /**
  190. * Explicitly sets the source and destination used in the blend function for this render state.
  191. *
  192. * Note that the blend function is only applied when blending is enabled.
  193. *
  194. * @param blendFunc Specifies how the blending factors are computed.
  195. */
  196. void setBlendFunc(const BlendFunc& blendFunc);
  197. /**
  198. * Toggles blending.
  199. *
  200. * @param enabled true to enable, false to disable.
  201. */
  202. void setBlend(bool enabled);
  203. /**
  204. * Explicitly sets the source used in the blend function for this render state.
  205. *
  206. * Note that the blend function is only applied when blending is enabled.
  207. *
  208. * @param blend Specifies how the source blending factors are computed.
  209. */
  210. void setBlendSrc(Blend blend);
  211. /**
  212. * Explicitly sets the source used in the blend function for this render state.
  213. *
  214. * Note that the blend function is only applied when blending is enabled.
  215. *
  216. * @param blend Specifies how the destination blending factors are computed.
  217. */
  218. void setBlendDst(Blend blend);
  219. /**
  220. * Explicitly enables or disables backface culling.
  221. *
  222. * @param enabled true to enable, false to disable.
  223. */
  224. void setCullFace(bool enabled);
  225. /**
  226. * Sets the side of the facets to cull.
  227. *
  228. * When not explicitly set, the default is to cull back-facing facets.
  229. *
  230. * @param side The side to cull.
  231. */
  232. void setCullFaceSide(CullFaceSide side);
  233. /**
  234. * Sets the winding for front facing polygons.
  235. *
  236. * By default, counter-clockwise wound polygons are considered front facing.
  237. *
  238. * @param winding The winding for front facing polygons.
  239. */
  240. void setFrontFace(FrontFace winding);
  241. /**
  242. * Toggles depth testing.
  243. *
  244. * By default, depth testing is disabled.
  245. *
  246. * @param enabled true to enable, false to disable.
  247. */
  248. void setDepthTest(bool enabled);
  249. /**
  250. * Toggles depth writing.
  251. *
  252. * @param enabled true to enable, false to disable.
  253. */
  254. void setDepthWrite(bool enabled);
  255. /**
  256. * Sets the depth function to use when depth testing is enabled.
  257. *
  258. * When not explicitly set and when depth testing is enabled, the default
  259. * depth function is DEPTH_LESS.
  260. *
  261. * @param func The depth function.
  262. */
  263. void setDepthFunction(DepthFunction func);
  264. // /**
  265. // * Toggles stencil testing.
  266. // *
  267. // * By default, stencil testing is disabled.
  268. // *
  269. // * @param enabled true to enable, false to disable.
  270. // */
  271. // void setStencilTest(bool enabled);
  272. //
  273. // /**
  274. // * Sets the stencil writing mask.
  275. // *
  276. // * By default, the stencil writing mask is all 1's.
  277. // *
  278. // * @param mask Bit mask controlling writing to individual stencil planes.
  279. // */
  280. // void setStencilWrite(unsigned int mask);
  281. //
  282. // /**
  283. // * Sets the stencil function.
  284. // *
  285. // * By default, the function is set to STENCIL_ALWAYS, the reference value is 0, and the mask is all 1's.
  286. // *
  287. // * @param func The stencil function.
  288. // * @param ref The stencil reference value.
  289. // * @param mask The stencil mask.
  290. // */
  291. // void setStencilFunction(StencilFunction func, int ref, unsigned int mask);
  292. //
  293. // /**
  294. // * Sets the stencil operation.
  295. // *
  296. // * By default, stencil fail, stencil pass/depth fail, and stencil and depth pass are set to STENCIL_OP_KEEP.
  297. // *
  298. // * @param sfail The stencil operation if the stencil test fails.
  299. // * @param dpfail The stencil operation if the stencil test passes, but the depth test fails.
  300. // * @param dppass The stencil operation if both the stencil test and depth test pass.
  301. // */
  302. // void setStencilOperation(StencilOperation sfail, StencilOperation dpfail, StencilOperation dppass);
  303. /**
  304. * Sets a render state from the given name and value strings.
  305. *
  306. * This method attempts to interpret the passed in strings as render state
  307. * name and value. This is normally used when loading render states from
  308. * material files.
  309. *
  310. * @param name Name of the render state to set.
  311. * @param value Value of the specified render state.
  312. */
  313. void setState(const std::string& name, const std::string& value);
  314. uint32_t getHash() const;
  315. bool isDirty() const;
  316. /** StateBlock bits to be used with invalidate */
  317. enum
  318. {
  319. RS_BLEND = (1 << 0),
  320. RS_BLEND_FUNC = (1 << 1),
  321. RS_CULL_FACE = (1 << 2),
  322. RS_DEPTH_TEST = (1 << 3),
  323. RS_DEPTH_WRITE = (1 << 4),
  324. RS_DEPTH_FUNC = (1 << 5),
  325. RS_CULL_FACE_SIDE = (1 << 6),
  326. // RS_STENCIL_TEST = (1 << 7),
  327. // RS_STENCIL_WRITE = (1 << 8),
  328. // RS_STENCIL_FUNC = (1 << 9),
  329. // RS_STENCIL_OP = (1 << 10),
  330. RS_FRONT_FACE = (1 << 11),
  331. RS_ALL_ONES = 0xFFFFFFFF,
  332. };
  333. /**
  334. * Invalidates the default StateBlock.
  335. *
  336. * Only call it if you are calling GL calls directly. Invoke this function
  337. * at the end of your custom draw call.
  338. * This function restores the default render state its defaults values.
  339. * Since this function might call GL calls, it must be called in a GL context is present.
  340. *
  341. * @param stateBits Bitwise-OR of the states that needs to be invalidated
  342. */
  343. static void invalidate(long stateBits);
  344. /**
  345. * Restores the global Render State to the default state
  346. *
  347. * The difference between `invalidate()` and `restore()`, is that `restore()` will
  348. * restore the global Render State based on its current state. Only the
  349. * states that were changed will be restored.
  350. *
  351. * Rule of thumb:
  352. - call `restore()` if you want to restore to the default state after using `StateBlock`.
  353. - call `invalidate()` if you want to restore to the default state after calling manual GL calls.
  354. */
  355. static void restore(long stateOverrideBits);
  356. static StateBlock* _defaultState;
  357. protected:
  358. void bindNoRestore();
  359. static void enableDepthWrite();
  360. void cloneInto(StateBlock* renderState) const;
  361. bool _cullFaceEnabled;
  362. bool _depthTestEnabled;
  363. bool _depthWriteEnabled;
  364. DepthFunction _depthFunction;
  365. bool _blendEnabled;
  366. Blend _blendSrc;
  367. Blend _blendDst;
  368. CullFaceSide _cullFaceSide;
  369. FrontFace _frontFace;
  370. bool _stencilTestEnabled;
  371. unsigned int _stencilWrite;
  372. StencilFunction _stencilFunction;
  373. int _stencilFunctionRef;
  374. unsigned int _stencilFunctionMask;
  375. StencilOperation _stencilOpSfail;
  376. StencilOperation _stencilOpDpfail;
  377. StencilOperation _stencilOpDppass;
  378. long _bits;
  379. mutable uint32_t _hash;
  380. mutable bool _hashDirty;
  381. };
  382. void setStateBlock(StateBlock* state);
  383. StateBlock* getStateBlock() const;
  384. protected:
  385. RenderState();
  386. ~RenderState();
  387. bool init(RenderState* parent);
  388. void cloneInto(RenderState* state) const;
  389. mutable uint32_t _hash;
  390. mutable bool _hashDirty;
  391. /**
  392. * The StateBlock of fixed-function render states that can be applied to the RenderState.
  393. */
  394. mutable StateBlock* _state;
  395. /**
  396. * The RenderState's parent. Weak Reference
  397. */
  398. RenderState* _parent;
  399. // name, for filtering
  400. std::string _name;
  401. Texture2D* _texture;
  402. };
  403. NS_CC_END
  404. #endif /* defined(__cocos2d_libs__CCRenderState__) */