use VAO/VBO to improve performance (#18066)

This commit is contained in:
minggo 2017-07-17 15:16:42 +08:00 committed by GitHub
parent bdfb71b05b
commit 7da188734d
3 changed files with 70 additions and 18 deletions

View File

@ -84,12 +84,26 @@ CameraBackgroundSkyBoxBrush* CameraBackgroundBrush::createSkyboxBrush(const std:
CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
: _depth(0.f)
, _clearColor(GL_FALSE)
, _vao(0)
, _vertexBuffer(0)
, _indexBuffer(0)
{
}
CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
{
glDeleteBuffers(1, &_vertexBuffer);
glDeleteBuffers(1, &_indexBuffer);
_vertexBuffer = 0;
_indexBuffer = 0;
if (Configuration::getInstance()->supportsShareableVAO())
{
glDeleteVertexArrays(1, &_vao);
GL::bindVAO(0);
_vao = 0;
}
}
CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
@ -126,6 +140,43 @@ bool CameraBackgroundDepthBrush::init()
_quad.br.texCoords = Tex2F(1,0);
_quad.tl.texCoords = Tex2F(0,1);
_quad.tr.texCoords = Tex2F(1,1);
auto supportVAO = Configuration::getInstance()->supportsShareableVAO();
if (supportVAO)
{
glGenVertexArrays(1, &_vao);
GL::bindVAO(_vao);
}
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(_quad), &_quad, GL_STATIC_DRAW);
GLshort indices[6] = {0, 1, 2, 3, 2, 1};
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
if (supportVAO)
{
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
// vertices
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_quad.tl.vertices);
// colors
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), &_quad.tl.colors);
// tex coords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_quad.tl.texCoords);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if (supportVAO)
GL::bindVAO(0);
return true;
}
@ -151,14 +202,15 @@ void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)
_glProgramState->setUniformFloat("depth", _depth);
_glProgramState->apply(Mat4::IDENTITY);
GLshort indices[6] = {0, 1, 2, 3, 2, 1};
auto supportVAO = Configuration::getInstance()->supportsShareableVAO();
if (supportVAO)
GL::bindVAO(_vao);
else
{
GL::bindVAO(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
// vertices
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_quad.tl.vertices);
@ -168,8 +220,17 @@ void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)
// tex coords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_quad.tl.texCoords);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
}
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
if (supportVAO)
GL::bindVAO(0);
else
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}

View File

@ -158,6 +158,9 @@ protected:
GLboolean _clearColor;
V3F_C4B_T2F_Quad _quad;
GLuint _vao;
GLuint _vertexBuffer;
GLuint _indexBuffer;
};
/**

View File

@ -54,21 +54,9 @@
if (self = [super init])
{
if (! sharegroup)
{
// multisampling conflict with GLES3, refer to https://github.com/cocos2d/cocos2d-x/issues/17767
if (!multiSampling)
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (! context_)
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
}
else
{
// multisampling conflict with GLES3, refer to https://github.com/cocos2d/cocos2d-x/issues/17767
if (!multiSampling)
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3 sharegroup:sharegroup];
if (!context_)
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:sharegroup];
}
if (!context_ || ![EAGLContext setCurrentContext:context_] )
{