[3d] add meshcommand (#19472)

This commit is contained in:
Arnold 2019-03-07 17:30:11 +08:00 committed by minggo
parent 0138db855f
commit 8caade49e6
18 changed files with 93 additions and 438 deletions

View File

@ -35,6 +35,8 @@ BillBoard::BillBoard()
: _mode(Mode::VIEW_POINT_ORIENTED)
, _modeDirty(false)
{
_trianglesCommand.setTransparent(true);
_trianglesCommand.set3D(true);
Node::setAnchorPoint(Vec2(0.5f,0.5f));
}
@ -229,8 +231,6 @@ void BillBoard::draw(Renderer *renderer, const Mat4 &/*transform*/, uint32_t fla
//FIXME: frustum culling here
flags |= Node::FLAGS_RENDER_AS_3D;
_trianglesCommand.init(0, _texture, _blendFunc, _polyInfo.triangles, _modelViewTransform, flags);
_trianglesCommand.setTransparent(true);
_trianglesCommand.set3D(true);
setMVPMatrixUniform(); //update uniform
renderer->addCommand(&_trianglesCommand);
}

View File

@ -34,7 +34,7 @@
#include "base/CCRef.h"
#include "base/CCVector.h"
#include "math/CCMath.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCMeshCommand.h"
NS_CC_BEGIN
@ -76,8 +76,8 @@ public:
const std::string& getId() const { return _id; }
/**primitive type setter & getter*/
CustomCommand::PrimitiveType getPrimitiveType() const { return _primitiveType; }
void setPrimitiveType(CustomCommand::PrimitiveType primitive) { _primitiveType = primitive; }
MeshCommand::PrimitiveType getPrimitiveType() const { return _primitiveType; }
void setPrimitiveType(MeshCommand::PrimitiveType primitive) { _primitiveType = primitive; }
CC_CONSTRUCTOR_ACCESS:
MeshIndexData() = default;
@ -88,7 +88,7 @@ protected:
MeshVertexData* _vertexData = nullptr; //vertex buffer, weak ref
AABB _aabb; // original aabb of the submesh
std::string _id; //id
CustomCommand::PrimitiveType _primitiveType = CustomCommand::PrimitiveType::TRIANGLE;
MeshCommand::PrimitiveType _primitiveType = MeshCommand::PrimitiveType::TRIANGLE;
friend class MeshVertexData;
friend class Sprite3D;

View File

@ -29,7 +29,6 @@
#include "base/ccTypes.h"
#include "platform/CCPlatformMacros.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCGroupCommand.h"
#include "renderer/CCCallbackCommand.h"
#include "2d/CCNode.h"
#include "renderer/backend/ProgramState.h"

View File

@ -121,14 +121,9 @@ bool Terrain::initProperties()
void Terrain::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
{
_groupCommand.init(_globalZOrder);
_beforeDraw.init(-1); //to ensure callbackcommand run before others
_afterDraw.init(100000.0);
renderer->addCommand(&_groupCommand);
renderer->pushGroup(_groupCommand.getRenderQueueID());
renderer->addCommand(&_beforeDraw);
auto modelMatrix = getNodeToWorldTransform();
@ -229,7 +224,6 @@ void Terrain::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform,
renderer->addCommand(&_afterDraw);
renderer->popGroup();
}
bool Terrain::initHeightMap(const std::string& heightMap)
@ -1092,8 +1086,8 @@ Terrain::Chunk::Chunk(Terrain *terrain)
_command.init(_terrain->_globalZOrder);
_command.setTransparent(false);
_command.set3D(true);
_command.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
_command.setDrawType(CustomCommand::DrawType::ELEMENT);
_command.setPrimitiveType(MeshCommand::PrimitiveType::TRIANGLE);
_command.setDrawType(MeshCommand::DrawType::ELEMENT);
auto &pipelineDescriptor = _command.getPipelineDescriptor();
pipelineDescriptor.blendDescriptor.blendEnabled = false;
}

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "2d/CCNode.h"
#include "2d/CCCamera.h"
#include "renderer/CCTexture2D.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCCallbackCommand.h"
#include "renderer/CCGroupCommand.h"
#include "renderer/CCRenderState.h"
@ -271,7 +271,7 @@ private:
std::vector<Triangle> _trianglesList;
backend::Buffer *_buffer = nullptr;
CustomCommand _command;
MeshCommand _command;
};
/**
@ -530,7 +530,6 @@ protected:
StateBlock _stateBlock;
StateBlock _stateBlockOld;
private:
GroupCommand _groupCommand;
CallbackCommand _beforeDraw;
CallbackCommand _afterDraw;
backend::VertexLayout _vertexLayout;

View File

@ -301,9 +301,7 @@ bool Material::parseSampler(backend::ProgramState* programState, Properties* sam
auto textureName = samplerProperties->getId();
auto location = programState->getUniformLocation(textureName);
//TODO arnold: slot may be incorrect
//programState->setTexture(location, 0, texture->getBackendTexture());
if (!location)
{
CCLOG("warning: failed to find texture uniform location %s when parsing material", textureName);

View File

@ -44,21 +44,12 @@ NS_CC_BEGIN
MeshCommand::MeshCommand()
: _displayColor(1.0f, 1.0f, 1.0f, 1.0f)
, _matrixPalette(nullptr)
, _matrixPaletteSize(0)
, _materialID(0)
, _vao(0)
, _material(nullptr)
, _glProgramState(nullptr)
, _stateBlock(nullptr)
, _textureID(0)
#if CC_ENABLE_CACHE_TEXTURE_DATA
, _rendererRecreatedListener(nullptr)
: _rendererRecreatedListener(nullptr)
#endif
{
_type = RenderCommand::Type::MESH_COMMAND;
_is3D = true;
#if CC_ENABLE_CACHE_TEXTURE_DATA
// listen the event that renderer was recreated on Android/WP8
_rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(MeshCommand::listenRendererRecreated, this));
@ -66,272 +57,22 @@ MeshCommand::MeshCommand()
#endif
}
void MeshCommand::init(float globalZOrder,
Material* material,
GLuint vertexBuffer,
GLuint indexBuffer,
GLenum primitive,
GLenum indexFormat,
ssize_t indexCount,
const cocos2d::Mat4 &mv,
uint32_t flags)
void MeshCommand::init(float globalZOrder)
{
CCASSERT(material, "material cannot be null");
RenderCommand::init(globalZOrder, mv, flags);
_globalOrder = globalZOrder;
_material = material;
_vertexBuffer = vertexBuffer;
_indexBuffer = indexBuffer;
_primitive = primitive;
_indexFormat = indexFormat;
_indexCount = indexCount;
_mv.set(mv);
_is3D = true;
}
void MeshCommand::init(float globalZOrder,
GLuint textureID,
GLProgramState* glProgramState,
RenderState::StateBlock* stateBlock,
GLuint vertexBuffer,
GLuint indexBuffer,
GLenum primitive,
GLenum indexFormat,
ssize_t indexCount,
const cocos2d::Mat4& mv,
uint32_t flags)
{
CCASSERT(glProgramState, "GLProgramState cannot be null");
CCASSERT(stateBlock, "StateBlock cannot be null");
CCASSERT(!_material, "cannot init with GLProgramState if previously inited without GLProgramState");
RenderCommand::init(globalZOrder, mv, flags);
_globalOrder = globalZOrder;
_textureID = textureID;
// weak ref
_glProgramState = glProgramState;
_stateBlock = stateBlock;
_vertexBuffer = vertexBuffer;
_indexBuffer = indexBuffer;
_primitive = primitive;
_indexFormat = indexFormat;
_indexCount = indexCount;
_mv.set(mv);
_is3D = true;
}
void MeshCommand::setDisplayColor(const Vec4& color)
{
CCASSERT(!_material, "If using material, you should set the color as a uniform: use u_color");
_displayColor = color;
}
void MeshCommand::setMatrixPalette(const Vec4* matrixPalette)
{
CCASSERT(!_material, "If using material, you should set the color as a uniform: use u_matrixPalette");
_matrixPalette = matrixPalette;
}
void MeshCommand::setMatrixPaletteSize(int size)
{
CCASSERT(!_material, "If using material, you should set the color as a uniform: use u_matrixPalette with its size");
_matrixPaletteSize = size;
CustomCommand::init(globalZOrder);
}
MeshCommand::~MeshCommand()
{
releaseVAO();
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_rendererRecreatedListener);
#endif
}
void MeshCommand::applyRenderState()
{
CCASSERT(!_material, "Must not be called when using materials");
CCASSERT(_stateBlock, "StateBlock must be non null");
// blend and texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _textureID);
auto &pipelineDescriptor = getPipelineDescriptor();
_stateBlock->bind(&pipelineDescriptor);
}
void MeshCommand::genMaterialID(GLuint texID, void* glProgramState, GLuint vertexBuffer, GLuint indexBuffer, BlendFunc blend)
{
int intArray[7] = {0};
intArray[0] = (int)texID;
*(int**)&intArray[1] = (int*) glProgramState;
intArray[3] = (int) vertexBuffer;
intArray[4] = (int) indexBuffer;
intArray[5] = (int) blend.src;
intArray[6] = (int) blend.dst;
_materialID = XXH32((const void*)intArray, sizeof(intArray), 0);
}
uint32_t MeshCommand::getMaterialID() const
{
return _materialID;
}
void MeshCommand::preBatchDraw()
{
// Do nothing if using material since each pass needs to bind its own VAO
if (!_material)
{
if (Configuration::getInstance()->supportsShareableVAO() && _vao == 0)
buildVAO();
if (_vao)
{
glBindVertexArray(_vao);
}
else
{
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
// FIXME: Assumes that all the passes in the Material share the same Vertex Attribs
// GLProgramState* programState = _material
// ? _material->_currentTechnique->_passes.at(0)->getGLProgramState()
// : _glProgramState;
// programState->applyAttributes();
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
}
}
}
void MeshCommand::batchDraw()
{
if (_material)
{
for(const auto& pass: _material->_currentTechnique->_passes)
{
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
}
}
else
{
// _glProgramState->applyGLProgram(_mv);
// set render state
applyRenderState();
// Draw
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
}
}
void MeshCommand::postBatchDraw()
{
// when using material, unbind is after draw
if (!_material)
{
if (_vao)
{
glBindVertexArray(0);
}
else
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// restore the default state since we don't know
// if the next command will need the default state or not
//TODO arnold
//RenderState::StateBlock::restoreGlobalState(0);
}
}
void MeshCommand::execute()
{
// Draw without VAO
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
if (_material)
{
for(const auto& pass: _material->_currentTechnique->_passes)
{
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
}
}
else
{
// set render state
// _glProgramState->apply(_mv);
applyRenderState();
// Draw
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void MeshCommand::buildVAO()
{
// FIXME: Assumes that all the passes in the Material share the same Vertex Attribs
// GLProgramState* programState = (_material != nullptr)
// ? _material->_currentTechnique->_passes.at(0)->getGLProgramState()
// : _glProgramState;
//
// releaseVAO();
// glGenVertexArrays(1, &_vao);
// glBindVertexArray(_vao);
// glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
// auto flags = programState->getVertexAttribsFlags();
// for (int i = 0; flags > 0; i++) {
// int flag = 1 << i;
// if (flag & flags)
// glEnableVertexAttribArray(i);
// flags &= ~flag;
// }
// programState->applyAttributes(false);
//
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
//
// glBindVertexArray(0);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void MeshCommand::releaseVAO()
{
if (_vao)
{
glDeleteVertexArrays(1, &_vao);
_vao = 0;
glBindVertexArray(0);
}
}
#if CC_ENABLE_CACHE_TEXTURE_DATA
void MeshCommand::listenRendererRecreated(EventCustom* event)
{
_vao = 0;
}
#endif
NS_CC_END

View File

@ -29,6 +29,9 @@
#include <unordered_map>
#include "renderer/CCRenderCommand.h"
#include "renderer/CCRenderState.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Types.h"
#include "renderer/CCCustomCommand.h"
#include "math/CCMath.h"
NS_CC_BEGIN
@ -39,78 +42,35 @@ class EventCustom;
class Material;
//it is a common mesh
class CC_DLL MeshCommand : public RenderCommand
class CC_DLL MeshCommand : public CustomCommand
{
public:
//using PrimitiveType = backend::PrimitiveType;
/**
Buffer usage of vertex/index buffer. If the contents is not updated every frame,
then use STATIC, other use DYNAMIC.
*/
using BufferUsage = backend::BufferUsage;
/**
The index format determine the size for index data. U_SHORT is enough for most
cases.
*/
using IndexFormat = backend::IndexFormat;
MeshCommand();
virtual ~MeshCommand();
void init(float globalZOrder, Material* material, GLuint vertexBuffer, GLuint indexBuffer, GLenum primitive, GLenum indexFormat, ssize_t indexCount, const Mat4 &mv, uint32_t flags);
/**
Init function. The render command will be in 2D mode.
@param globalZOrder GlobalZOrder of the render command.
*/
void init(float globalZOrder);
void init(float globalZOrder, GLuint textureID, GLProgramState* glProgramState, RenderState::StateBlock* stateBlock, GLuint vertexBuffer, GLuint indexBuffer, GLenum primitive, GLenum indexFormat, ssize_t indexCount, const Mat4 &mv, uint32_t flags);
void setDisplayColor(const Vec4& color);
void setMatrixPalette(const Vec4* matrixPalette);
void setMatrixPaletteSize(int size);
void setLightMask(unsigned int lightmask);
void execute();
//used for batch
void preBatchDraw();
void batchDraw();
void postBatchDraw();
void genMaterialID(GLuint texID, void* glProgramState, GLuint vertexBuffer, GLuint indexBuffer, BlendFunc blend);
uint32_t getMaterialID() const;
#if CC_ENABLE_CACHE_TEXTURE_DATA
void listenRendererRecreated(EventCustom* event);
#endif
protected:
//build & release vao
void buildVAO();
void releaseVAO();
// apply renderstate, not used when using material
void applyRenderState();
Vec4 _displayColor; // in order to support tint and fade in fade out
// used for skin
const Vec4* _matrixPalette;
int _matrixPaletteSize;
uint32_t _materialID; //material ID
GLuint _vao; //use vao if possible
GLuint _vertexBuffer;
GLuint _indexBuffer;
GLenum _primitive;
GLenum _indexFormat;
ssize_t _indexCount;
// States, default value all false
// ModelView transform
Mat4 _mv;
// Mode A: Material
// weak ref
Material* _material;
// Mode B: StateBlock
// weak ref
GLProgramState* _glProgramState;
RenderState::StateBlock* _stateBlock;
GLuint _textureID;
#if CC_ENABLE_CACHE_TEXTURE_DATA
EventListenerCustom* _rendererRecreatedListener;

View File

@ -101,17 +101,8 @@ bool Pass::initWithProgramState(Technique* technique, backend::ProgramState *pro
Pass::Pass()
{
//_customCommand.set3D(true);
//TODO: set _customCommand's vertex layout.
//auto& vertexLayout = _customCommand.getPipelineDescriptor().vertexLayout;
//vertexLayout.setAtrribute("a_position", 0, backend::VertexFormat::FLOAT3, 0, false);
//vertexLayout.setAtrribute("a_texCoord", 1, backend::VertexFormat::FLOAT2, 6 * sizeof(float), false);
//vertexLayout.setLayout(8 * sizeof(float), backend::VertexStepMode::VERTEX);
_beforeVisitCmd.func = CC_CALLBACK_0(Pass::onBeforeVisitCmd, this);
_afterVisitCmd.func = CC_CALLBACK_0(Pass::onAfterVisitCmd, this);
}
Pass::~Pass()
@ -152,7 +143,7 @@ void Pass::setProgramState(backend::ProgramState* programState)
CC_SAFE_RELEASE(_programState);
_programState = programState;
CC_SAFE_RETAIN(_programState);
_customCommand.getPipelineDescriptor().programState = _programState;
_meshCommand.getPipelineDescriptor().programState = _programState;
initUniformLocations();
_hashDirty = true;
}
@ -228,14 +219,14 @@ void Pass::initUniformLocations()
//}
void Pass::draw(float globalZOrder, backend::Buffer* vertexBuffer, backend::Buffer* indexBuffer,
CustomCommand::PrimitiveType primitive, CustomCommand::IndexFormat indexFormat,
MeshCommand::PrimitiveType primitive, MeshCommand::IndexFormat indexFormat,
unsigned int indexCount, const Mat4& modelView)
{
_customCommand.init(globalZOrder, BlendFunc::ALPHA_PREMULTIPLIED);
_customCommand.setPrimitiveType(primitive);
_customCommand.setIndexBuffer(indexBuffer, indexFormat);
_customCommand.setVertexBuffer(vertexBuffer);
_customCommand.setIndexDrawInfo(0, indexCount);
_meshCommand.init(globalZOrder);
_meshCommand.setPrimitiveType(primitive);
_meshCommand.setIndexBuffer(indexBuffer, indexFormat);
_meshCommand.setVertexBuffer(vertexBuffer);
_meshCommand.setIndexDrawInfo(0, indexCount);
auto &matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto mvp = matrixP * modelView;
@ -262,7 +253,7 @@ void Pass::draw(float globalZOrder, backend::Buffer* vertexBuffer, backend::Buff
_afterVisitCmd.init(globalZOrder);
renderer->addCommand(&_beforeVisitCmd);
renderer->addCommand(&_customCommand);
renderer->addCommand(&_meshCommand);
renderer->addCommand(&_afterVisitCmd);
}
@ -271,7 +262,7 @@ void Pass::onBeforeVisitCmd()
auto *renderer = Director::getInstance()->getRenderer();
//_oldDepthEnabledState = renderer->getDepthTest();
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
_rendererDepthTestEnabled = renderer->getDepthTest();
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();

View File

@ -33,7 +33,7 @@
#include "platform/CCPlatformMacros.h"
#include "renderer/CCRenderState.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCGroupCommand.h"
#include "renderer/CCCallbackCommand.h"
@ -66,7 +66,7 @@ public:
/** Returns the ProgramState */
backend::ProgramState* getProgramState() const;
backend::VertexLayout* getVertexLayout() { return &(_customCommand.getPipelineDescriptor().vertexLayout); }
backend::VertexLayout* getVertexLayout() { return &(_meshCommand.getPipelineDescriptor().vertexLayout); }
/** Binds the GLProgramState and the RenderState.
This method must be called before call the actual draw call.
@ -75,7 +75,7 @@ public:
//void bind(const Mat4& modelView, bool bindAttributes);
void draw(float globalZOrder, backend::Buffer* vertexBuffer, backend::Buffer* indexBuffer,
CustomCommand::PrimitiveType primitive, CustomCommand::IndexFormat indexFormat,
MeshCommand::PrimitiveType primitive, MeshCommand::IndexFormat indexFormat,
unsigned int indexCount, const Mat4& modelView);
/** Unbinds the Pass.
@ -143,7 +143,7 @@ protected:
Node* getTarget() const;
VertexAttribBinding* _vertexAttribBinding = nullptr;
CustomCommand _customCommand;
MeshCommand _meshCommand;
Technique * _technique = nullptr;
bool _hashDirty = true;
RenderState _renderState;

View File

@ -52,7 +52,7 @@ void RenderState::bindPass(Pass* pass)
assert(pass->_technique && pass->_technique->_material);
auto *technique = pass->_technique;
auto *material = technique->_material;
auto &pipelineDescriptor = pass->_customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = pass->_meshCommand.getPipelineDescriptor();
//need reset all state
//pipelineDescriptor.blendDescriptor.blendEnabled = true;
@ -73,7 +73,7 @@ void RenderState::bindPass(Pass* pass)
void RenderState::unbindPass(Pass* pass)
{
auto &pipelineDescriptor = pass->_customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = pass->_meshCommand.getPipelineDescriptor();
RenderState::StateBlock::restore(0, &pipelineDescriptor);
}

View File

@ -285,34 +285,8 @@ void Renderer::processRenderCommand(RenderCommand* command)
}
break;
case RenderCommand::Type::MESH_COMMAND:
{
flush2D();
auto cmd = static_cast<MeshCommand*>(command);
if (cmd->isSkipBatching() || _lastBatchedMeshCommand == nullptr || _lastBatchedMeshCommand->getMaterialID() != cmd->getMaterialID())
{
flush3D();
if(cmd->isSkipBatching())
{
// XXX: execute() will call bind() and unbind()
// but unbind() shouldn't be call if the next command is a MESH_COMMAND with Material.
// Once most of cocos2d-x moves to Pass/StateBlock, only bind() should be used.
cmd->execute();
}
else
{
cmd->preBatchDraw();
cmd->batchDraw();
_lastBatchedMeshCommand = cmd;
}
}
else
{
// CCGL_DEBUG_INSERT_EVENT_MARKER("RENDERER_MESH_COMMAND");
cmd->batchDraw();
}
}
drawMeshCommand(command);
break;
case RenderCommand::Type::GROUP_COMMAND:
processGroupCommand(static_cast<GroupCommand*>(command));
@ -419,7 +393,6 @@ void Renderer::clean()
// Clear batch commands
_queuedTriangleCommands.clear();
_lastBatchedMeshCommand = nullptr;
}
void Renderer::setDepthTest(bool value)
@ -652,11 +625,11 @@ void Renderer::drawBatchedTriangles()
_triBatchesToDraw[i].indicesToDraw,
_triBatchesToDraw[i].offset * sizeof(_indices[0]));
_commandBuffer->endRenderPass();
_drawnBatches++;
_drawnVertices += _triBatchesToDraw[i].indicesToDraw;
}
/************** 3: Cleanup *************/
_queuedTriangleCommands.clear();
@ -696,6 +669,13 @@ void Renderer::drawCustomCommand(RenderCommand *command)
_commandBuffer->endRenderPass();
}
void Renderer::drawMeshCommand(RenderCommand *command)
{
//MeshCommand and CustomCommand are identical while rendering.
drawCustomCommand(command);
}
void Renderer::flush()
{
flush2D();
@ -709,11 +689,7 @@ void Renderer::flush2D()
void Renderer::flush3D()
{
if (_lastBatchedMeshCommand)
{
_lastBatchedMeshCommand->postBatchDraw();
_lastBatchedMeshCommand = nullptr;
}
//TODO 3d batch rendering
}
void Renderer::flushTriangles()

View File

@ -277,6 +277,7 @@ protected:
inline GroupCommandManager * getGroupCommandManager() const { return _groupCommandManager; }
void drawBatchedTriangles();
void drawCustomCommand(RenderCommand* command);
void drawMeshCommand(RenderCommand* command);
void beginFrame();
void endFrame();
@ -312,7 +313,6 @@ protected:
std::vector<RenderQueue> _renderGroups;
MeshCommand* _lastBatchedMeshCommand = nullptr;
std::vector<TrianglesCommand*> _queuedTriangleCommands;
//for TrianglesCommand

View File

@ -16,9 +16,9 @@ public:
protected:
Buffer(unsigned int size, BufferType type, BufferUsage usage)
: _size(size)
: _usage(usage)
, _type(type)
, _usage(usage)
, _size(size)
{}
virtual ~Buffer() = default;

View File

@ -159,11 +159,11 @@ void Particle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, Par
float depthZ = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]);
_beforeCommand.init(depthZ);
_customCommand.init(depthZ);
_meshCommand.init(depthZ);
_afterCommand.init(depthZ);
_customCommand.setVertexBuffer(_vertexBuffer);
_customCommand.setIndexBuffer(_indexBuffer, CustomCommand::IndexFormat::U_SHORT);
_meshCommand.setVertexBuffer(_vertexBuffer);
_meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
auto &projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
@ -177,10 +177,10 @@ void Particle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, Par
_programState->setUniform(_locColor, &uColor, sizeof(uColor));
_customCommand.setIndexDrawInfo(0, index);
_meshCommand.setIndexDrawInfo(0, index);
renderer->addCommand(&_beforeCommand);
renderer->addCommand(&_customCommand);
renderer->addCommand(&_meshCommand);
renderer->addCommand(&_afterCommand);
}
@ -203,7 +203,7 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleColor_frag);
}
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
auto &layout = pipelineDescriptor.vertexLayout;
@ -216,8 +216,8 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
_customCommand.setTransparent(true);
_customCommand.setSkipBatching(true);
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);
_stateBlock.setDepthTest(true);
_stateBlock.setDepthWrite(false);
@ -233,7 +233,7 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
void Particle3DQuadRender::onBeforeDraw()
{
auto *renderer = Director::getInstance()->getRenderer();
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
_rendererDepthTestEnabled = renderer->getDepthTest();
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
_rendererCullMode = renderer->getCullMode();

View File

@ -29,7 +29,7 @@
#include "renderer/CCRenderState.h"
#include "renderer/backend/Types.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCCallbackCommand.h"
#include "renderer/backend/Buffer.h"
#include "base/CCRef.h"
@ -114,13 +114,13 @@ protected:
void onAfterDraw();
protected:
CustomCommand _customCommand;
MeshCommand _meshCommand;
CallbackCommand _beforeCommand;
CallbackCommand _afterCommand;
Texture2D* _texture = nullptr;
backend::ProgramState* _programState = nullptr;
backend::Buffer* _indexBuffer = nullptr; //index buffer
backend::Buffer* _vertexBuffer = nullptr; // vertex buffer
Texture2D* _texture = nullptr;
backend::ProgramState* _programState = nullptr;
backend::Buffer* _indexBuffer = nullptr; //index buffer
backend::Buffer* _vertexBuffer = nullptr; // vertex buffer
struct posuvcolor
{

View File

@ -666,7 +666,7 @@ void PUBillboardChain::init( const std::string &texFile )
GLsizei stride = sizeof(VertexInfo);
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
auto &layout = pipelineDescriptor.vertexLayout;
@ -679,8 +679,8 @@ void PUBillboardChain::init( const std::string &texFile )
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
_customCommand.setTransparent(true);
_customCommand.setSkipBatching(true);
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);
_stateBlock.setDepthTest(true);
_stateBlock.setDepthWrite(false);
@ -701,13 +701,13 @@ void PUBillboardChain::render( Renderer* renderer, const Mat4 &transform, Partic
updateVertexBuffer(cameraMat);
updateIndexBuffer();
_customCommand.setVertexBuffer(_vertexBuffer);
_customCommand.setIndexBuffer(_indexBuffer, CustomCommand::IndexFormat::U_SHORT);
_meshCommand.setVertexBuffer(_vertexBuffer);
_meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
if (!_vertices.empty() && !_indices.empty())
{
_beforeCommand.init(0.0);
_customCommand.init(0.0, transform, Node::FLAGS_RENDER_AS_3D);
_meshCommand.init(0.0);
_afterCommand.init(0.0);
auto &projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
@ -721,13 +721,13 @@ void PUBillboardChain::render( Renderer* renderer, const Mat4 &transform, Partic
auto uColor = Vec4(1, 1, 1, 1);
_programState->setUniform(_locColor, &uColor, sizeof(uColor));
_stateBlock.bind(&_customCommand.getPipelineDescriptor());
_stateBlock.bind(&_meshCommand.getPipelineDescriptor());
_customCommand.setIndexDrawInfo(0, _indices.size());
_meshCommand.setIndexDrawInfo(0, _indices.size());
renderer->addCommand(&_beforeCommand);
renderer->addCommand(&_customCommand);
renderer->addCommand(&_meshCommand);
renderer->addCommand(&_afterCommand);
}
}
@ -751,7 +751,7 @@ void PUBillboardChain::setBlendFunc(const BlendFunc& blendFunc)
void PUBillboardChain::onBeforeDraw()
{
auto *renderer = Director::getInstance()->getRenderer();
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
_rendererDepthTestEnabled = renderer->getDepthTest();
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
_rendererCullMode = renderer->getCullMode();
@ -764,7 +764,7 @@ void PUBillboardChain::onBeforeDraw()
void PUBillboardChain::onAfterDraw()
{
auto *renderer = Director::getInstance()->getRenderer();
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
renderer->setDepthTest(_rendererDepthTestEnabled);
renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
renderer->setCullMode(_rendererCullMode);

View File

@ -24,12 +24,11 @@
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_PU_PARTICLE_3D_BILLBOARD_CHAIN_H__
#define __CC_PU_PARTICLE_3D_BILLBOARD_CHAIN_H__
#pragma once
#include <vector>
#include "renderer/CCRenderState.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCCallbackCommand.h"
#include "renderer/backend/Buffer.h"
#include "base/CCRef.h"
@ -325,8 +324,7 @@ protected:
Vec2 uv;
Vec4 color;
};
//MeshCommand* _meshCommand = nullptr;
CustomCommand _customCommand;
MeshCommand _meshCommand;
CallbackCommand _beforeCommand;
CallbackCommand _afterCommand;
RenderState::StateBlock _stateBlock;
@ -353,4 +351,3 @@ protected:
};
NS_CC_END
#endif