diff --git a/cocos/2d/CCCameraBackgroundBrush.cpp b/cocos/2d/CCCameraBackgroundBrush.cpp index ebb3de6917..0d0d7e9ba6 100644 --- a/cocos/2d/CCCameraBackgroundBrush.cpp +++ b/cocos/2d/CCCameraBackgroundBrush.cpp @@ -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); } diff --git a/cocos/2d/CCCameraBackgroundBrush.h b/cocos/2d/CCCameraBackgroundBrush.h index 07f29a898a..bb3458316c 100644 --- a/cocos/2d/CCCameraBackgroundBrush.h +++ b/cocos/2d/CCCameraBackgroundBrush.h @@ -158,6 +158,9 @@ protected: GLboolean _clearColor; V3F_C4B_T2F_Quad _quad; + GLuint _vao; + GLuint _vertexBuffer; + GLuint _indexBuffer; }; /** diff --git a/cocos/platform/ios/CCES2Renderer-ios.m b/cocos/platform/ios/CCES2Renderer-ios.m index dacde8ef41..c1f04ab98e 100644 --- a/cocos/platform/ios/CCES2Renderer-ios.m +++ b/cocos/platform/ios/CCES2Renderer-ios.m @@ -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_] ) {