1
0

CCRenderer.h 9.1 KB

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