CCRenderer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CC_RENDERER_H_
  22. #define __CC_RENDERER_H_
  23. #include <vector>
  24. #include <stack>
  25. #include "platform/CCPlatformMacros.h"
  26. #include "renderer/CCRenderCommand.h"
  27. #include "renderer/CCGLProgram.h"
  28. #include "platform/CCGL.h"
  29. #if !defined(NDEBUG) && CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  30. /// Basic wrapper for glInsertEventMarkerEXT() depending on the current build settings and platform.
  31. #define CCGL_DEBUG_INSERT_EVENT_MARKER(__message__) glInsertEventMarkerEXT(0, __message__)
  32. /// Basic wrapper for glPushGroupMarkerEXT() depending on the current build settings and platform.
  33. #define CCGL_DEBUG_PUSH_GROUP_MARKER(__message__) glPushGroupMarkerEXT(0, __message__)
  34. /// Basic wrapper for CCGL_DEBUG_POP_GROUP_MARKER() depending on the current build settings and platform.
  35. #define CCGL_DEBUG_POP_GROUP_MARKER() glPopGroupMarkerEXT()
  36. #else
  37. #define CCGL_DEBUG_INSERT_EVENT_MARKER(__message__)
  38. #define CCGL_DEBUG_PUSH_GROUP_MARKER(__message__)
  39. #define CCGL_DEBUG_POP_GROUP_MARKER()
  40. #endif
  41. /**
  42. * @addtogroup renderer
  43. * @{
  44. */
  45. NS_CC_BEGIN
  46. class EventListenerCustom;
  47. class TrianglesCommand;
  48. class MeshCommand;
  49. /** Class that knows how to sort `RenderCommand` objects.
  50. Since the commands that have `z == 0` are "pushed back" in
  51. the correct order, the only `RenderCommand` objects that need to be sorted,
  52. are the ones that have `z < 0` and `z > 0`.
  53. */
  54. class RenderQueue {
  55. public:
  56. /**
  57. RenderCommand will be divided into Queue Groups.
  58. */
  59. enum QUEUE_GROUP
  60. {
  61. /**Objects with globalZ smaller than 0.*/
  62. GLOBALZ_NEG = 0,
  63. /**Opaque 3D objects with 0 globalZ.*/
  64. OPAQUE_3D = 1,
  65. /**Transparent 3D objects with 0 globalZ.*/
  66. TRANSPARENT_3D = 2,
  67. /**2D objects with 0 globalZ.*/
  68. GLOBALZ_ZERO = 3,
  69. /**Objects with globalZ bigger than 0.*/
  70. GLOBALZ_POS = 4,
  71. QUEUE_COUNT = 5,
  72. };
  73. public:
  74. /**Constructor.*/
  75. RenderQueue();
  76. /**Push a renderCommand into current renderqueue.*/
  77. void push_back(RenderCommand* command);
  78. /**Return the number of render commands.*/
  79. ssize_t size() const;
  80. /**Sort the render commands.*/
  81. void sort();
  82. /**Treat sorted commands as an array, access them one by one.*/
  83. RenderCommand* operator[](ssize_t index) const;
  84. /**Clear all rendered commands.*/
  85. void clear();
  86. /**Realloc command queues and reserve with given size. Note: this clears any existing commands.*/
  87. void realloc(size_t reserveSize);
  88. /**Get a sub group of the render queue.*/
  89. std::vector<RenderCommand*>& getSubQueue(QUEUE_GROUP group) { return _commands[group]; }
  90. /**Get the number of render commands contained in a subqueue.*/
  91. ssize_t getSubQueueSize(QUEUE_GROUP group) const { return _commands[group].size(); }
  92. /**Save the current DepthState, CullState, DepthWriteState render state.*/
  93. void saveRenderState();
  94. /**Restore the saved DepthState, CullState, DepthWriteState render state.*/
  95. void restoreRenderState();
  96. protected:
  97. /**The commands in the render queue.*/
  98. std::vector<RenderCommand*> _commands[QUEUE_COUNT];
  99. /**Cull state.*/
  100. bool _isCullEnabled;
  101. /**Depth test enable state.*/
  102. bool _isDepthEnabled;
  103. /**Depth buffer write state.*/
  104. GLboolean _isDepthWrite;
  105. };
  106. //the struct is not used outside.
  107. struct RenderStackElement
  108. {
  109. int renderQueueID;
  110. ssize_t currentIndex;
  111. };
  112. class GroupCommandManager;
  113. /* Class responsible for the rendering in.
  114. Whenever possible prefer to use `TrianglesCommand` objects since the renderer will automatically batch them.
  115. */
  116. class CC_DLL Renderer
  117. {
  118. public:
  119. /**The max number of vertices in a vertex buffer object.*/
  120. static const int VBO_SIZE = 65536;
  121. /**The max number of indices in a index buffer.*/
  122. static const int INDEX_VBO_SIZE = VBO_SIZE * 6 / 4;
  123. /**The rendercommands which can be batched will be saved into a list, this is the reserved size of this list.*/
  124. static const int BATCH_TRIAGCOMMAND_RESERVED_SIZE = 64;
  125. /**Reserved for material id, which means that the command could not be batched.*/
  126. static const int MATERIAL_ID_DO_NOT_BATCH = 0;
  127. /**Constructor.*/
  128. Renderer();
  129. /**Destructor.*/
  130. ~Renderer();
  131. //TODO: manage GLView inside Render itself
  132. void initGLView();
  133. /** Adds a `RenderComamnd` into the renderer */
  134. void addCommand(RenderCommand* command);
  135. /** Adds a `RenderComamnd` into the renderer specifying a particular render queue ID */
  136. void addCommand(RenderCommand* command, int renderQueueID);
  137. /** Pushes a group into the render queue */
  138. void pushGroup(int renderQueueID);
  139. /** Pops a group from the render queue */
  140. void popGroup();
  141. /** Creates a render queue and returns its Id */
  142. int createRenderQueue();
  143. /** Renders into the GLView all the queued `RenderCommand` objects */
  144. void render();
  145. /** Cleans all `RenderCommand`s in the queue */
  146. void clean();
  147. /** Clear GL buffer and screen */
  148. void clear();
  149. /** set color for clear screen */
  150. void setClearColor(const Color4F& clearColor);
  151. /* returns the number of drawn batches in the last frame */
  152. ssize_t getDrawnBatches() const { return _drawnBatches; }
  153. /* RenderCommands (except) TrianglesCommand should update this value */
  154. void addDrawnBatches(ssize_t number) { _drawnBatches += number; };
  155. /* returns the number of drawn triangles in the last frame */
  156. ssize_t getDrawnVertices() const { return _drawnVertices; }
  157. /* RenderCommands (except) TrianglesCommand should update this value */
  158. void addDrawnVertices(ssize_t number) { _drawnVertices += number; };
  159. /* clear draw stats */
  160. void clearDrawStats() { _drawnBatches = _drawnVertices = 0; }
  161. /**
  162. * Enable/Disable depth test
  163. * For 3D object depth test is enabled by default and can not be changed
  164. * For 2D object depth test is disabled by default
  165. */
  166. void setDepthTest(bool enable);
  167. //This will not be used outside.
  168. GroupCommandManager* getGroupCommandManager() const { return _groupCommandManager; }
  169. /** returns whether or not a rectangle is visible or not */
  170. bool checkVisibility(const Mat4& transform, const Size& size);
  171. protected:
  172. //Setup VBO or VAO based on OpenGL extensions
  173. void setupBuffer();
  174. void setupVBOAndVAO();
  175. void setupVBO();
  176. void mapBuffers();
  177. void drawBatchedTriangles();
  178. //Draw the previews queued triangles and flush previous context
  179. void flush();
  180. void flush2D();
  181. void flush3D();
  182. void flushTriangles();
  183. void processRenderCommand(RenderCommand* command);
  184. void visitRenderQueue(RenderQueue& queue);
  185. void fillVerticesAndIndices(const TrianglesCommand* cmd);
  186. /* clear color set outside be used in setGLDefaultValues() */
  187. Color4F _clearColor;
  188. std::stack<int> _commandGroupStack;
  189. std::vector<RenderQueue> _renderGroups;
  190. MeshCommand* _lastBatchedMeshCommand;
  191. std::vector<TrianglesCommand*> _queuedTriangleCommands;
  192. //for TrianglesCommand
  193. V3F_C4B_T2F _verts[VBO_SIZE];
  194. GLushort _indices[INDEX_VBO_SIZE];
  195. GLuint _buffersVAO;
  196. GLuint _buffersVBO[2]; //0: vertex 1: indices
  197. // Internal structure that has the information for the batches
  198. struct TriBatchToDraw {
  199. TrianglesCommand* cmd; // needed for the Material
  200. GLsizei indicesToDraw;
  201. GLsizei offset;
  202. };
  203. // capacity of the array of TriBatches
  204. int _triBatchesToDrawCapacity;
  205. // the TriBatches
  206. TriBatchToDraw* _triBatchesToDraw;
  207. int _filledVertex;
  208. int _filledIndex;
  209. bool _glViewAssigned;
  210. // stats
  211. ssize_t _drawnBatches;
  212. ssize_t _drawnVertices;
  213. //the flag for checking whether renderer is rendering
  214. bool _isRendering;
  215. bool _isDepthTestFor2D;
  216. GroupCommandManager* _groupCommandManager;
  217. #if CC_ENABLE_CACHE_TEXTURE_DATA
  218. EventListenerCustom* _cacheTextureListener;
  219. #endif
  220. };
  221. NS_CC_END
  222. /**
  223. end of support group
  224. @}
  225. */
  226. #endif //__CC_RENDERER_H_