123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- #ifndef __CC_RENDERER_H_
- #define __CC_RENDERER_H_
- #include <vector>
- #include <stack>
- #include "platform/CCPlatformMacros.h"
- #include "renderer/CCRenderCommand.h"
- #include "renderer/CCGLProgram.h"
- #include "platform/CCGL.h"
- #if !defined(NDEBUG) && CC_TARGET_PLATFORM == CC_PLATFORM_IOS
- #define CCGL_DEBUG_INSERT_EVENT_MARKER(__message__) glInsertEventMarkerEXT(0, __message__)
- #define CCGL_DEBUG_PUSH_GROUP_MARKER(__message__) glPushGroupMarkerEXT(0, __message__)
- #define CCGL_DEBUG_POP_GROUP_MARKER() glPopGroupMarkerEXT()
- #else
- #define CCGL_DEBUG_INSERT_EVENT_MARKER(__message__)
- #define CCGL_DEBUG_PUSH_GROUP_MARKER(__message__)
- #define CCGL_DEBUG_POP_GROUP_MARKER()
- #endif
- NS_CC_BEGIN
- class EventListenerCustom;
- class TrianglesCommand;
- class MeshCommand;
- class RenderQueue {
- public:
-
- enum QUEUE_GROUP
- {
-
- GLOBALZ_NEG = 0,
-
- OPAQUE_3D = 1,
-
- TRANSPARENT_3D = 2,
-
- GLOBALZ_ZERO = 3,
-
- GLOBALZ_POS = 4,
- QUEUE_COUNT = 5,
- };
- public:
-
- RenderQueue();
-
- void push_back(RenderCommand* command);
-
- ssize_t size() const;
-
- void sort();
-
- RenderCommand* operator[](ssize_t index) const;
-
- void clear();
-
- void realloc(size_t reserveSize);
-
- std::vector<RenderCommand*>& getSubQueue(QUEUE_GROUP group) { return _commands[group]; }
-
- ssize_t getSubQueueSize(QUEUE_GROUP group) const { return _commands[group].size(); }
-
- void saveRenderState();
-
- void restoreRenderState();
-
- protected:
-
- std::vector<RenderCommand*> _commands[QUEUE_COUNT];
-
-
- bool _isCullEnabled;
-
- bool _isDepthEnabled;
-
- GLboolean _isDepthWrite;
- };
- struct RenderStackElement
- {
- int renderQueueID;
- ssize_t currentIndex;
- };
- class GroupCommandManager;
- class CC_DLL Renderer
- {
- public:
-
- static const int VBO_SIZE = 65536;
-
- static const int INDEX_VBO_SIZE = VBO_SIZE * 6 / 4;
-
- static const int BATCH_TRIAGCOMMAND_RESERVED_SIZE = 64;
-
- static const int MATERIAL_ID_DO_NOT_BATCH = 0;
-
- Renderer();
-
- ~Renderer();
-
- void initGLView();
-
- void addCommand(RenderCommand* command);
-
- void addCommand(RenderCommand* command, int renderQueue);
-
- void pushGroup(int renderQueueID);
-
- void popGroup();
-
- int createRenderQueue();
-
- void render();
-
- void clean();
-
- void clear();
-
- void setClearColor(const Color4F& clearColor);
-
- ssize_t getDrawnBatches() const { return _drawnBatches; }
-
- void addDrawnBatches(ssize_t number) { _drawnBatches += number; };
-
- ssize_t getDrawnVertices() const { return _drawnVertices; }
-
- void addDrawnVertices(ssize_t number) { _drawnVertices += number; };
-
- void clearDrawStats() { _drawnBatches = _drawnVertices = 0; }
-
- void setDepthTest(bool enable);
-
-
- GroupCommandManager* getGroupCommandManager() const { return _groupCommandManager; }
-
- bool checkVisibility(const Mat4& transform, const Size& size);
- protected:
-
- void setupBuffer();
- void setupVBOAndVAO();
- void setupVBO();
- void mapBuffers();
- void drawBatchedTriangles();
-
- void flush();
-
- void flush2D();
-
- void flush3D();
- void flushTriangles();
- void processRenderCommand(RenderCommand* command);
- void visitRenderQueue(RenderQueue& queue);
- void fillVerticesAndIndices(const TrianglesCommand* cmd);
-
- Color4F _clearColor;
- std::stack<int> _commandGroupStack;
-
- std::vector<RenderQueue> _renderGroups;
- MeshCommand* _lastBatchedMeshCommand;
- std::vector<TrianglesCommand*> _queuedTriangleCommands;
-
- V3F_C4B_T2F _verts[VBO_SIZE];
- GLushort _indices[INDEX_VBO_SIZE];
- GLuint _buffersVAO;
- GLuint _buffersVBO[2];
-
- struct TriBatchToDraw {
- TrianglesCommand* cmd;
- GLsizei indicesToDraw;
- GLsizei offset;
- };
-
- int _triBatchesToDrawCapacity;
-
- TriBatchToDraw* _triBatchesToDraw;
- int _filledVertex;
- int _filledIndex;
- bool _glViewAssigned;
-
- ssize_t _drawnBatches;
- ssize_t _drawnVertices;
-
- bool _isRendering;
-
- bool _isDepthTestFor2D;
-
- GroupCommandManager* _groupCommandManager;
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
- EventListenerCustom* _cacheTextureListener;
- #endif
- };
- NS_CC_END
- #endif
|