DeviceGraphics.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 <string>
  23. #include <vector>
  24. #include <unordered_map>
  25. #include "base/ccTypes.h"
  26. #include "base/CCRef.h"
  27. #include "math/Vec2.h"
  28. #include "math/Vec3.h"
  29. #include "math/Vec4.h"
  30. #include "math/Mat4.h"
  31. #include "../Macro.h"
  32. #include "../Types.h"
  33. #include "State.h"
  34. RENDERER_BEGIN
  35. class FrameBuffer;
  36. class VertexBuffer;
  37. class IndexBuffer;
  38. class Program;
  39. class Texture;
  40. /**
  41. * @addtogroup gfx
  42. * @{
  43. */
  44. /**
  45. * DeviceGraphics is a direct abstraction for OpenGL APIs.
  46. * This fundamental class takes responsibility for all GL states management and GL call invocations.
  47. */
  48. class DeviceGraphics final : public Ref
  49. {
  50. public:
  51. struct Capacity
  52. {
  53. int maxVextexTextures;
  54. int maxFragUniforms;
  55. int maxTextureUnits;
  56. int maxVertexAttributes;
  57. int maxDrawBuffers;
  58. int maxColorAttatchments;
  59. };
  60. struct Uniform
  61. {
  62. Uniform(const void* v, size_t bytes, UniformElementType elementType_, size_t count);
  63. Uniform(Uniform&& h);
  64. Uniform();
  65. ~Uniform();
  66. Uniform& operator=(Uniform&& h);
  67. void setValue(const void* v, size_t bytes, size_t count = 1);
  68. void* value = nullptr;
  69. size_t bytes = 0;
  70. size_t count = 0;
  71. bool dirty;
  72. UniformElementType elementType;
  73. private:
  74. // Disable copy operator
  75. Uniform& operator=(const Uniform& o);
  76. };
  77. /**
  78. * Returns a shared instance of the director.
  79. */
  80. static DeviceGraphics* getInstance();
  81. /**
  82. * Sets the target FrameBuffer
  83. */
  84. void setFrameBuffer(const FrameBuffer* fb);
  85. /**
  86. * Sets viewport with x, y, width and height
  87. */
  88. void setViewport(int x, int y, int w, int h);
  89. /**
  90. * Sets scissor clipping area
  91. */
  92. void setScissor(int x, int y, int w, int h);
  93. /**
  94. * Clear with flags, including color, depth and stencil,
  95. * you should also specify the color, depth and stencil value to use.
  96. */
  97. void clear(uint8_t flags, Color4F *color, double depth, int32_t stencil);
  98. /**
  99. * Enables blend in GL state
  100. */
  101. void enableBlend();
  102. /**
  103. * Enables depth test in GL state
  104. */
  105. void enableDepthTest();
  106. /**
  107. * Enables depth write in GL state
  108. */
  109. void enableDepthWrite();
  110. /**
  111. * Enables stencil test in GL state
  112. */
  113. void enableStencilTest();
  114. /**
  115. * Sets both the front and back function and reference value for stencil testing
  116. */
  117. void setStencilFunc(StencilFunc func, int ref, unsigned int mask);
  118. /**
  119. * Sets the front function and reference value for stencil testing
  120. */
  121. void setStencilFuncFront(StencilFunc func, int ref, unsigned int mask);
  122. /**
  123. * Sets the back function and reference value for stencil testing
  124. */
  125. void setStencilFuncBack(StencilFunc func, int ref, unsigned int mask);
  126. /**
  127. * Sets both the front and back-facing stencil test actions
  128. */
  129. void setStencilOp(StencilOp failOp, StencilOp zFailOp, StencilOp zPassOp, unsigned int writeMask);
  130. /**
  131. * Sets the front stencil test actions
  132. */
  133. void setStencilOpFront(StencilOp failOp, StencilOp zFailOp, StencilOp zPassOp, unsigned int writeMask);
  134. /**
  135. * Sets the back stencil test actions
  136. */
  137. void setStencilOpBack(StencilOp failOp, StencilOp zFailOp, StencilOp zPassOp, unsigned int writeMask);
  138. /**
  139. * Specifies the depth comparison function, which sets the conditions under which the pixel will be drawn.
  140. */
  141. void setDepthFunc(DepthFunc func);
  142. /**
  143. * Sets the source and destination blending factors with all channel packed into a 32bit unsigned int
  144. */
  145. void setBlendColor(uint32_t rgba);
  146. /**
  147. * Sets the source and destination blending factors with separate color channel values
  148. */
  149. void setBlendColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
  150. /**
  151. * Sets which function is used for blending pixel arithmetic
  152. */
  153. void setBlendFunc(BlendFactor src, BlendFactor dst);
  154. /**
  155. * Sets which function is used for blending pixel arithmetic for RGB and alpha components separately
  156. */
  157. void setBlendFuncSeparate(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, BlendFactor dstAlpha);
  158. /**
  159. * Sets both the RGB blend equation and alpha blend equation to a single equation.
  160. */
  161. void setBlendEquation(BlendOp mode);
  162. /**
  163. * Sets the RGB blend equation and alpha blend equation separately.
  164. */
  165. void setBlendEquationSeparate(BlendOp modeRGB, BlendOp modeAlpha);
  166. /**
  167. * Specifies whether or not front and/or back-facing polygons can be culled.
  168. */
  169. void setCullMode(CullMode mode);
  170. /**
  171. * Sets the vertex buffer
  172. */
  173. void setVertexBuffer(int stream, VertexBuffer* buffer, int start = 0);
  174. /**
  175. * Sets the index buffer
  176. */
  177. void setIndexBuffer(IndexBuffer *buffer);
  178. /**
  179. * Sets a linked program
  180. */
  181. void setProgram(Program *program);
  182. /**
  183. * Sets a texture into a GL texture slot then set to the specified uniform
  184. */
  185. void setTexture(size_t hashName, Texture* texture, int slot);
  186. /**
  187. * Sets textures array into GL texture slots then set to the specified uniform
  188. */
  189. void setTextureArray(size_t hashName, const std::vector<Texture*>& textures, const std::vector<int>& slots);
  190. /**
  191. * Sets a integer to the specified uniform
  192. */
  193. void setUniformi(size_t hashName, int i1);
  194. /**
  195. * Sets two integers to the specified uniform
  196. */
  197. void setUniformi(size_t hashName, int i1, int i2);
  198. /**
  199. * Sets three integers to the specified uniform
  200. */
  201. void setUniformi(size_t hashName, int i1, int i2, int i3);
  202. /**
  203. * Sets four integers to the specified uniform
  204. */
  205. void setUniformi(size_t hashName, int i1, int i2, int i3, int i4);
  206. /**
  207. * Sets a vector of integers to the specified uniform
  208. */
  209. void setUniformiv(size_t hashName, size_t elementCount, const int* value, size_t uniformCount);
  210. /**
  211. * Sets a float to the specified uniform
  212. */
  213. void setUniformf(size_t hashName, float f1);
  214. /**
  215. * Sets two floats to the specified uniform
  216. */
  217. void setUniformf(size_t hashName, float f1, float f2);
  218. /**
  219. * Sets three floats to the specified uniform
  220. */
  221. void setUniformf(size_t hashName, float f1, float f2, float f3);
  222. /**
  223. * Sets four floats to the specified uniform
  224. */
  225. void setUniformf(size_t hashName, float f1, float f2, float f3, float f4);
  226. /**
  227. * Sets a vector of floats to the specified uniform
  228. */
  229. void setUniformfv(size_t hashName, size_t elementCount, const float* value, size_t uniformCount);
  230. /**
  231. * Sets a Vec2 to the specified uniform
  232. */
  233. void setUniformVec2(size_t hashName, const cocos2d::Vec2& value);
  234. /**
  235. * Sets a Vec3 to the specified uniform
  236. */
  237. void setUniformVec3(size_t hashName, const cocos2d::Vec3& value);
  238. /**
  239. * Sets a Vec4 to the specified uniform
  240. */
  241. void setUniformVec4(size_t hashName, const cocos2d::Vec4& value);
  242. /**
  243. * Sets a 2x2 matrix to the specified uniform
  244. */
  245. void setUniformMat2(size_t hashName, float* value);
  246. /**
  247. * Sets a 3x3 matrix to the specified uniform
  248. */
  249. void setUniformMat3(size_t hashName, float* value);
  250. /**
  251. * Sets a 4x4 matrix specified by float pointer to the given uniform
  252. */
  253. void setUniformMat4(size_t hashName, float* value);
  254. /**
  255. * Sets a Mat4 object to the given uniform
  256. */
  257. void setUniformMat4(size_t hashName, const cocos2d::Mat4& value);
  258. /**
  259. * Sets data specified by data pointer, type and bytes to the given uniform
  260. */
  261. void setUniform(size_t hashName, const void* v, size_t bytes, UniformElementType elementType, size_t uniformCount = 1);
  262. /**
  263. * Sets the primitive type for draw calls
  264. */
  265. void setPrimitiveType(PrimitiveType type);
  266. /**
  267. * Draw elements using the current gl states
  268. */
  269. void draw(size_t base, GLsizei count);
  270. /**
  271. * Resets the draw call counter to 0
  272. */
  273. void resetDrawCalls() { _drawCalls = 0; };
  274. /**
  275. * Gets current draw call counts
  276. */
  277. uint32_t getDrawCalls() const { return _drawCalls; };
  278. inline const Capacity& getCapacity() const { return _caps; }
  279. private:
  280. DeviceGraphics();
  281. ~DeviceGraphics();
  282. CC_DISALLOW_COPY_ASSIGN_AND_MOVE(DeviceGraphics);
  283. inline void initStates();
  284. inline void initCaps();
  285. void restoreTexture(uint32_t index);
  286. void restoreIndexBuffer();
  287. inline void commitBlendStates();
  288. inline void commitDepthStates();
  289. inline void commitStencilStates();
  290. inline void commitCullMode();
  291. inline void commitVertexBuffer();
  292. inline void commitTextures();
  293. int _vx;
  294. int _vy;
  295. int _vw;
  296. int _vh;
  297. int _sx;
  298. int _sy;
  299. int _sw;
  300. int _sh;
  301. uint32_t _drawCalls = 0;
  302. int _defaultFbo;
  303. Capacity _caps;
  304. char* _glExtensions;
  305. FrameBuffer *_frameBuffer;
  306. std::vector<int> _enabledAtrributes;
  307. std::vector<int> _newAttributes;
  308. std::unordered_map<size_t, Uniform> _uniforms;
  309. State* _nextState;
  310. State* _currentState;
  311. friend class IndexBuffer;
  312. friend class Texture2D;
  313. };
  314. // end of gfx group
  315. /// @}
  316. RENDERER_END