2013-12-18 10:12:15 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-18 10:12:15 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
|
|
|
2013-11-12 03:54:08 +08:00
|
|
|
#ifndef __CC_RENDERER_H_
|
|
|
|
#define __CC_RENDERER_H_
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <stack>
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCRenderCommand.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCGL.h"
|
2013-11-08 07:48:37 +08:00
|
|
|
|
2015-03-27 11:54:40 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup support
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
class EventListenerCustom;
|
2014-01-22 15:18:27 +08:00
|
|
|
class QuadCommand;
|
2014-08-28 09:58:39 +08:00
|
|
|
class TrianglesCommand;
|
2014-06-23 23:58:45 +08:00
|
|
|
class MeshCommand;
|
2013-12-31 10:54:37 +08:00
|
|
|
|
2014-03-07 08:14:06 +08:00
|
|
|
/** Class that knows how to sort `RenderCommand` objects.
|
|
|
|
Since the commands that have `z == 0` are "pushed back" in
|
|
|
|
the correct order, the only `RenderCommand` objects that need to be sorted,
|
|
|
|
are the ones that have `z < 0` and `z > 0`.
|
2014-01-18 15:10:04 +08:00
|
|
|
*/
|
|
|
|
class RenderQueue {
|
2015-01-27 14:29:09 +08:00
|
|
|
public:
|
2015-03-23 20:30:37 +08:00
|
|
|
/**
|
|
|
|
RenderCommand will be divided into Queue Groups.
|
|
|
|
*/
|
2015-01-27 14:29:09 +08:00
|
|
|
enum QUEUE_GROUP
|
|
|
|
{
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Objects with globalZ smaller than 0.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
GLOBALZ_NEG = 0,
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Opaque 3D objects with 0 globalZ.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
OPAQUE_3D = 1,
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Transparent 3D objects with 0 globalZ.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
TRANSPARENT_3D = 2,
|
2015-03-23 20:30:37 +08:00
|
|
|
/**2D objects with 0 globalZ.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
GLOBALZ_ZERO = 3,
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Objects with globalZ bigger than 0.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
GLOBALZ_POS = 4,
|
2015-01-28 10:32:22 +08:00
|
|
|
QUEUE_COUNT = 5,
|
2015-01-27 14:29:09 +08:00
|
|
|
};
|
2014-01-18 15:10:04 +08:00
|
|
|
|
|
|
|
public:
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Constructor.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
RenderQueue()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Push a renderCommand into current renderqueue.*/
|
2014-01-18 15:10:04 +08:00
|
|
|
void push_back(RenderCommand* command);
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Return the number of render commands.*/
|
2014-01-18 15:10:04 +08:00
|
|
|
ssize_t size() const;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Sort the render commands.*/
|
2014-01-18 15:10:04 +08:00
|
|
|
void sort();
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Treat sorted commands as an array, access them one by one.*/
|
2014-01-19 03:35:27 +08:00
|
|
|
RenderCommand* operator[](ssize_t index) const;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Clear all rendered commands.*/
|
2014-01-18 15:10:04 +08:00
|
|
|
void clear();
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Get a sub group of the render queue.*/
|
2015-01-28 06:41:16 +08:00
|
|
|
inline std::vector<RenderCommand*>& getSubQueue(QUEUE_GROUP group) { return _commands[group]; }
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Get the number of render commands contained in a subqueue.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
inline ssize_t getSubQueueSize(QUEUE_GROUP group) const { return _commands[group].size();}
|
2014-01-18 15:10:04 +08:00
|
|
|
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Save the current DepthState, CullState, DepthWriteState render state.*/
|
2015-01-27 08:37:11 +08:00
|
|
|
void saveRenderState();
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Restore the saved DepthState, CullState, DepthWriteState render state.*/
|
2015-01-27 08:37:11 +08:00
|
|
|
void restoreRenderState();
|
|
|
|
|
2014-01-18 15:10:04 +08:00
|
|
|
protected:
|
2015-03-23 20:30:37 +08:00
|
|
|
/**The commands in the render queue.*/
|
2015-01-27 14:29:09 +08:00
|
|
|
std::vector<std::vector<RenderCommand*>> _commands;
|
2015-01-27 08:37:11 +08:00
|
|
|
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Cull state.*/
|
2015-01-27 08:37:11 +08:00
|
|
|
bool _isCullEnabled;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Depth test enable state.*/
|
2015-01-27 08:37:11 +08:00
|
|
|
bool _isDepthEnabled;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Depth buffer write state.*/
|
2015-01-27 08:37:11 +08:00
|
|
|
GLboolean _isDepthWrite;
|
2014-01-18 15:10:04 +08:00
|
|
|
};
|
2013-11-14 09:31:12 +08:00
|
|
|
|
2015-03-23 20:30:37 +08:00
|
|
|
//the struct is not used outside.
|
2013-11-14 09:31:12 +08:00
|
|
|
struct RenderStackElement
|
|
|
|
{
|
|
|
|
int renderQueueID;
|
2014-01-17 07:02:39 +08:00
|
|
|
ssize_t currentIndex;
|
2013-11-14 09:31:12 +08:00
|
|
|
};
|
|
|
|
|
2014-04-12 13:01:42 +08:00
|
|
|
class GroupCommandManager;
|
|
|
|
|
2014-03-07 08:14:06 +08:00
|
|
|
/* Class responsible for the rendering in.
|
|
|
|
|
|
|
|
Whenever possible prefer to use `QuadCommand` objects since the renderer will automatically batch them.
|
2014-02-08 11:37:44 +08:00
|
|
|
*/
|
2014-07-14 17:02:06 +08:00
|
|
|
class CC_DLL Renderer
|
2013-11-05 01:14:22 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-23 20:30:37 +08:00
|
|
|
/**The max number of vertices in a vertex buffer object.*/
|
2014-09-05 16:01:17 +08:00
|
|
|
static const int VBO_SIZE = 65536;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**The max numer of indices in a index buffer.*/
|
2014-09-05 16:01:17 +08:00
|
|
|
static const int INDEX_VBO_SIZE = VBO_SIZE * 6 / 4;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**The rendercommands which can be batched will be saved into a list, this is the reversed size of this list.*/
|
2014-01-22 18:24:23 +08:00
|
|
|
static const int BATCH_QUADCOMMAND_RESEVER_SIZE = 64;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Reserved for material id, which means that the command could not be batched.*/
|
2014-10-16 14:15:29 +08:00
|
|
|
static const int MATERIAL_ID_DO_NOT_BATCH = 0;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Constructor.*/
|
2013-12-18 09:50:17 +08:00
|
|
|
Renderer();
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Destructor.*/
|
2013-12-18 09:50:17 +08:00
|
|
|
~Renderer();
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
//TODO: manage GLView inside Render itself
|
2013-11-22 08:36:19 +08:00
|
|
|
void initGLView();
|
2014-04-04 12:48:16 +08:00
|
|
|
|
2014-03-07 08:14:06 +08:00
|
|
|
/** Adds a `RenderComamnd` into the renderer */
|
2013-11-16 09:32:29 +08:00
|
|
|
void addCommand(RenderCommand* command);
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Adds a `RenderComamnd` into the renderer specifying a particular render queue ID */
|
2013-11-16 09:32:29 +08:00
|
|
|
void addCommand(RenderCommand* command, int renderQueue);
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Pushes a group into the render queue */
|
2013-11-21 03:05:01 +08:00
|
|
|
void pushGroup(int renderQueueID);
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Pops a group from the render queue */
|
2013-11-21 03:05:01 +08:00
|
|
|
void popGroup();
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Creates a render queue and returns its Id */
|
2013-11-16 03:29:11 +08:00
|
|
|
int createRenderQueue();
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Renders into the GLView all the queued `RenderCommand` objects */
|
2013-11-05 01:14:22 +08:00
|
|
|
void render();
|
|
|
|
|
2014-04-04 12:48:16 +08:00
|
|
|
/** Cleans all `RenderCommand`s in the queue */
|
|
|
|
void clean();
|
|
|
|
|
2015-01-10 06:06:21 +08:00
|
|
|
/** Clear GL buffer and screen */
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/** set color for clear screen */
|
|
|
|
void setClearColor(const Color4F& clearColor);
|
2014-02-08 11:37:44 +08:00
|
|
|
/* returns the number of drawn batches in the last frame */
|
|
|
|
ssize_t getDrawnBatches() const { return _drawnBatches; }
|
|
|
|
/* RenderCommands (except) QuadCommand should update this value */
|
|
|
|
void addDrawnBatches(ssize_t number) { _drawnBatches += number; };
|
|
|
|
/* returns the number of drawn triangles in the last frame */
|
|
|
|
ssize_t getDrawnVertices() const { return _drawnVertices; }
|
|
|
|
/* RenderCommands (except) QuadCommand should update this value */
|
|
|
|
void addDrawnVertices(ssize_t number) { _drawnVertices += number; };
|
2014-09-02 11:12:15 +08:00
|
|
|
/* clear draw stats */
|
|
|
|
void clearDrawStats() { _drawnBatches = _drawnVertices = 0; }
|
2014-02-08 11:37:44 +08:00
|
|
|
|
2015-01-16 08:41:07 +08:00
|
|
|
/**
|
|
|
|
* Enable/Disable depth test
|
|
|
|
* For 3D object depth test is enabled by default and can not be changed
|
|
|
|
* For 2D object depth test is disabled by default
|
|
|
|
*/
|
2015-01-16 08:54:27 +08:00
|
|
|
void setDepthTest(bool enable);
|
2015-01-16 08:41:07 +08:00
|
|
|
|
2015-03-23 20:30:37 +08:00
|
|
|
//This will not be used outside.
|
2014-04-12 13:01:42 +08:00
|
|
|
inline GroupCommandManager* getGroupCommandManager() const { return _groupCommandManager; };
|
2014-04-15 07:46:19 +08:00
|
|
|
|
|
|
|
/** returns whether or not a rectangle is visible or not */
|
2014-05-15 01:07:09 +08:00
|
|
|
bool checkVisibility(const Mat4& transform, const Size& size);
|
2014-04-15 07:46:19 +08:00
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
protected:
|
2013-11-08 07:48:37 +08:00
|
|
|
|
2013-12-05 09:02:02 +08:00
|
|
|
//Setup VBO or VAO based on OpenGL extensions
|
|
|
|
void setupBuffer();
|
2013-11-08 08:50:53 +08:00
|
|
|
void setupVBOAndVAO();
|
2013-12-05 09:02:02 +08:00
|
|
|
void setupVBO();
|
|
|
|
void mapBuffers();
|
2014-10-16 14:15:29 +08:00
|
|
|
void drawBatchedTriangles();
|
2013-11-22 08:36:19 +08:00
|
|
|
void drawBatchedQuads();
|
2014-01-16 06:35:26 +08:00
|
|
|
|
2013-11-09 04:06:39 +08:00
|
|
|
//Draw the previews queued quads and flush previous context
|
|
|
|
void flush();
|
2014-04-07 22:51:32 +08:00
|
|
|
|
2014-06-23 23:58:45 +08:00
|
|
|
void flush2D();
|
|
|
|
|
2014-06-23 19:08:26 +08:00
|
|
|
void flush3D();
|
2015-01-10 06:06:21 +08:00
|
|
|
|
|
|
|
void flushQuads();
|
|
|
|
void flushTriangles();
|
|
|
|
|
2015-01-12 14:20:41 +08:00
|
|
|
void processRenderCommand(RenderCommand* command);
|
2015-01-27 08:37:11 +08:00
|
|
|
void visitRenderQueue(RenderQueue& queue);
|
2013-11-09 04:06:39 +08:00
|
|
|
|
2014-08-28 09:58:39 +08:00
|
|
|
void fillVerticesAndIndices(const TrianglesCommand* cmd);
|
2014-10-16 14:15:29 +08:00
|
|
|
void fillQuads(const QuadCommand* cmd);
|
2015-01-10 06:06:21 +08:00
|
|
|
|
|
|
|
/* clear color set outside be used in setGLDefaultValues() */
|
|
|
|
Color4F _clearColor;
|
|
|
|
|
2013-12-17 15:32:24 +08:00
|
|
|
std::stack<int> _commandGroupStack;
|
2013-11-21 03:05:01 +08:00
|
|
|
|
2013-12-17 15:32:24 +08:00
|
|
|
std::vector<RenderQueue> _renderGroups;
|
2013-11-14 09:31:12 +08:00
|
|
|
|
2014-03-31 13:47:07 +08:00
|
|
|
uint32_t _lastMaterialID;
|
2013-11-08 08:50:53 +08:00
|
|
|
|
2014-06-23 23:58:45 +08:00
|
|
|
MeshCommand* _lastBatchedMeshCommand;
|
2014-08-28 09:58:39 +08:00
|
|
|
std::vector<TrianglesCommand*> _batchedCommands;
|
2014-10-16 14:15:29 +08:00
|
|
|
std::vector<QuadCommand*> _batchQuadCommands;
|
2015-01-10 06:06:21 +08:00
|
|
|
|
2014-10-16 14:15:29 +08:00
|
|
|
//for TrianglesCommand
|
2014-08-27 14:54:35 +08:00
|
|
|
V3F_C4B_T2F _verts[VBO_SIZE];
|
|
|
|
GLushort _indices[INDEX_VBO_SIZE];
|
2014-10-16 14:15:29 +08:00
|
|
|
GLuint _buffersVAO;
|
2013-11-08 08:50:53 +08:00
|
|
|
GLuint _buffersVBO[2]; //0: vertex 1: indices
|
|
|
|
|
2014-08-27 14:54:35 +08:00
|
|
|
int _filledVertex;
|
|
|
|
int _filledIndex;
|
2013-11-22 08:36:19 +08:00
|
|
|
|
2014-10-16 14:15:29 +08:00
|
|
|
//for QuadCommand
|
|
|
|
V3F_C4B_T2F _quadVerts[VBO_SIZE];
|
|
|
|
GLushort _quadIndices[INDEX_VBO_SIZE];
|
|
|
|
GLuint _quadVAO;
|
|
|
|
GLuint _quadbuffersVBO[2]; //0: vertex 1: indices
|
|
|
|
int _numberQuads;
|
|
|
|
|
2013-11-22 08:36:19 +08:00
|
|
|
bool _glViewAssigned;
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
|
|
// stats
|
|
|
|
ssize_t _drawnBatches;
|
|
|
|
ssize_t _drawnVertices;
|
2014-04-11 17:31:35 +08:00
|
|
|
//the flag for checking whether renderer is rendering
|
2014-04-11 16:05:22 +08:00
|
|
|
bool _isRendering;
|
2013-12-31 10:54:37 +08:00
|
|
|
|
2015-01-16 08:41:07 +08:00
|
|
|
bool _isDepthTestFor2D;
|
|
|
|
|
2014-04-12 13:01:42 +08:00
|
|
|
GroupCommandManager* _groupCommandManager;
|
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
EventListenerCustom* _cacheTextureListener;
|
|
|
|
#endif
|
2013-11-05 01:14:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-27 11:54:40 +08:00
|
|
|
/**
|
|
|
|
end of support group
|
|
|
|
@}
|
|
|
|
*/
|
2013-11-12 03:54:08 +08:00
|
|
|
#endif //__CC_RENDERER_H_
|