State.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include <stdint.h>
  22. #include <vector>
  23. #include "../Macro.h"
  24. #include "../Types.h"
  25. RENDERER_BEGIN
  26. class VertexBuffer;
  27. class IndexBuffer;
  28. class Program;
  29. class Texture;
  30. /**
  31. * @addtogroup gfx
  32. * @{
  33. */
  34. /**
  35. * State class save the GL states used in one draw call.
  36. * It will be used to compare between the previous states and the current states, so that we know which GL states should be updated.
  37. * User shouldn't be using this Class directly. Instead, they should set the states in Pass of material system.
  38. * @see `Pass`
  39. */
  40. struct State final
  41. {
  42. /**
  43. * Constructor
  44. */
  45. State();
  46. ~State();
  47. /**
  48. * Reset all states to default values
  49. */
  50. void reset();
  51. /**
  52. @name Blend
  53. @{
  54. */
  55. /**
  56. * Indicates blending enabled or not
  57. */
  58. bool blend;
  59. /**
  60. * Indicates the RGB blend equation and alpha blend equation settings are separate or not
  61. */
  62. bool blendSeparation;
  63. /**
  64. * The blending color
  65. */
  66. uint32_t blendColor;
  67. /**
  68. * The blending equation
  69. */
  70. BlendOp blendEq;
  71. /**
  72. * The blending equation for alpha channel
  73. */
  74. BlendOp blendAlphaEq;
  75. /**
  76. * The blending source factor
  77. */
  78. BlendFactor blendSrc;
  79. /**
  80. * The blending destination factor
  81. */
  82. BlendFactor blendDst;
  83. /**
  84. * The blending source factor for alpha
  85. */
  86. BlendFactor blendSrcAlpha;
  87. /**
  88. * The blending destination factor for alpha
  89. */
  90. BlendFactor blendDstAlpha;
  91. /**
  92. end of Blend
  93. @}
  94. */
  95. /**
  96. @name Depth
  97. @{
  98. */
  99. /**
  100. * Indicates depth test enabled or not
  101. */
  102. bool depthTest;
  103. /**
  104. * Indicates depth writing enabled or not
  105. */
  106. bool depthWrite;
  107. /**
  108. * The depth comparison function
  109. */
  110. DepthFunc depthFunc;
  111. /**
  112. end of Depth
  113. @}
  114. */
  115. /**
  116. @name Stencil
  117. @{
  118. */
  119. /**
  120. * Indicates stencil test enabled or not
  121. */
  122. bool stencilTest;
  123. /**
  124. * Indicates stencil test function for front and back is set separatly or not
  125. */
  126. bool stencilSeparation;
  127. /**
  128. * The front face function for stencil test
  129. */
  130. StencilFunc stencilFuncFront;
  131. /**
  132. * The front reference value for stencil test
  133. */
  134. int32_t stencilRefFront;
  135. /**
  136. * The front mask value to AND with the reference value and store the result into stencil buffer when test is done
  137. */
  138. uint32_t stencilMaskFront;
  139. /**
  140. * The function to use when the stencil test fails for front face
  141. */
  142. StencilOp stencilFailOpFront;
  143. /**
  144. * The function to use when the stencil test passes but the depth test fails for front face
  145. */
  146. StencilOp stencilZFailOpFront;
  147. /**
  148. * The function to use when both the stencil and the depth test pass for front face.
  149. * Or when the stencil test passes and there is no depth buffer or depth testing is disabled.
  150. */
  151. StencilOp stencilZPassOpFront;
  152. /**
  153. * The bit mask to enable or disable writing of individual bits in the front stencil planes
  154. */
  155. uint32_t stencilWriteMaskFront;
  156. /**
  157. * The back face function for stencil test
  158. */
  159. StencilFunc stencilFuncBack;
  160. /**
  161. * The back reference value for stencil test
  162. */
  163. int32_t stencilRefBack;
  164. /**
  165. * The back mask value to AND with the reference value and store the result into stencil buffer when test is done
  166. */
  167. uint32_t stencilMaskBack;
  168. /**
  169. * The function to use when the stencil test fails for back face
  170. */
  171. StencilOp stencilFailOpBack;
  172. /**
  173. * The function to use when the stencil test passes but the depth test fails for back face
  174. */
  175. StencilOp stencilZFailOpBack;
  176. /**
  177. * The function to use when both the stencil and the depth test pass for back face.
  178. * Or when the stencil test passes and there is no depth buffer or depth testing is disabled.
  179. */
  180. StencilOp stencilZPassOpBack;
  181. /**
  182. * The bit mask to enable or disable writing of individual bits in the back stencil planes
  183. */
  184. uint32_t stencilWriteMaskBack;
  185. /**
  186. end of Stencil
  187. @}
  188. */
  189. /**
  190. * Specifies whether front-facing or back-facing polygons are candidates for culling.
  191. */
  192. CullMode cullMode;
  193. /**
  194. * Specifies the primitive type for rendering
  195. */
  196. PrimitiveType primitiveType;
  197. int32_t maxStream;
  198. /**
  199. * Specifies the vertex buffer
  200. */
  201. void setVertexBuffer(size_t index, VertexBuffer* vertBuf);
  202. /**
  203. * Gets the vertex buffer
  204. */
  205. VertexBuffer* getVertexBuffer(size_t index) const;
  206. /**
  207. * Specifies the vertex buffer offset
  208. */
  209. void setVertexBufferOffset(size_t index, int32_t offset);
  210. /**
  211. * Gets the vertex buffer offset
  212. */
  213. int32_t getVertexBufferOffset(size_t index) const;
  214. /**
  215. * Specifies the index buffer
  216. */
  217. void setIndexBuffer(IndexBuffer* indexBuf);
  218. /**
  219. * Gets the index buffer
  220. */
  221. IndexBuffer* getIndexBuffer() const;
  222. /**
  223. * Sets the texture for specific texture unit
  224. */
  225. void setTexture(size_t index, Texture* texture);
  226. /**
  227. * Gets the specific texture unit
  228. */
  229. Texture* getTexture(size_t index) const;
  230. /**
  231. * Gets all texture units
  232. */
  233. const std::vector<Texture*>& getTextureUnits() const { return _textureUnits; }
  234. /**
  235. * Sets the program used for rendering
  236. */
  237. void setProgram(Program* program);
  238. /**
  239. * Gets the program used for rendering
  240. */
  241. Program* getProgram() const;
  242. private:
  243. std::vector<VertexBuffer*> _vertexBuffers;
  244. std::vector<int32_t> _vertexBufferOffsets;
  245. IndexBuffer *_indexBuffer = nullptr;
  246. std::vector<Texture*> _textureUnits;
  247. Program *_program = nullptr;
  248. };
  249. // end of gfx group
  250. /// @}
  251. RENDERER_END