2015-05-12 16:43:17 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2015 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-06-09 15:32:12 +08:00
|
|
|
#include "renderer/CCFrameBuffer.h"
|
2015-05-28 17:08:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventType.h"
|
2015-05-12 16:43:17 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2015-06-09 10:59:43 +08:00
|
|
|
namespace experimental{
|
|
|
|
FrameBuffer* FrameBuffer::_defaultFBO = nullptr;
|
|
|
|
std::set<FrameBuffer*> FrameBuffer::_frameBuffers;
|
2015-05-13 14:24:53 +08:00
|
|
|
|
2015-06-09 16:21:50 +08:00
|
|
|
Viewport::Viewport(float left, float bottom, float width, float height)
|
|
|
|
: _left(left)
|
|
|
|
, _bottom(bottom)
|
|
|
|
, _width(width)
|
|
|
|
, _height(height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Viewport::Viewport()
|
|
|
|
{
|
|
|
|
_left = _bottom = 0.f;
|
|
|
|
_width = _height = 1.0f;
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
RenderTargetBase::RenderTargetBase()
|
2015-05-13 14:24:53 +08:00
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
|
2015-05-13 14:24:53 +08:00
|
|
|
}
|
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
RenderTargetBase::~RenderTargetBase()
|
2015-05-13 14:24:53 +08:00
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
|
2015-05-13 14:24:53 +08:00
|
|
|
}
|
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
bool RenderTargetBase::init(unsigned int width, unsigned int height)
|
2015-05-13 17:17:54 +08:00
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
_width = width;
|
|
|
|
_height = height;
|
|
|
|
return true;
|
2015-05-13 17:17:54 +08:00
|
|
|
}
|
|
|
|
|
2015-05-27 16:59:54 +08:00
|
|
|
RenderTarget* RenderTarget::create(unsigned int width, unsigned int height, Texture2D::PixelFormat format/* = Texture2D::PixelFormat::RGBA8888*/)
|
2015-05-12 16:43:17 +08:00
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
auto result = new (std::nothrow) RenderTarget();
|
2015-05-27 16:59:54 +08:00
|
|
|
if(result && result->init(width, height,format))
|
2015-05-12 16:43:17 +08:00
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:59:54 +08:00
|
|
|
bool RenderTarget::init(unsigned int width, unsigned int height, Texture2D::PixelFormat format)
|
2015-05-12 16:43:17 +08:00
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
if(!RenderTargetBase::init(width, height))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-14 10:36:49 +08:00
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
_texture = new (std::nothrow) Texture2D();
|
|
|
|
if(nullptr == _texture) return false;
|
2015-05-28 17:08:36 +08:00
|
|
|
//TODO: FIX me, get the correct bit depth for pixelformat
|
2015-05-14 10:36:49 +08:00
|
|
|
auto dataLen = width * height * 4;
|
|
|
|
auto data = malloc(dataLen);
|
2015-05-14 11:19:13 +08:00
|
|
|
if( nullptr == data) return false;
|
2015-05-14 10:36:49 +08:00
|
|
|
|
|
|
|
memset(data, 0, dataLen);
|
2015-05-27 16:59:54 +08:00
|
|
|
if(_texture->initWithData(data, dataLen, format, width, height, Size(width, height)))
|
2015-05-14 11:19:13 +08:00
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
_texture->autorelease();
|
|
|
|
CC_SAFE_RETAIN(_texture);
|
2015-05-14 11:19:13 +08:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-27 11:40:12 +08:00
|
|
|
CC_SAFE_DELETE(_texture);
|
2015-05-14 11:19:13 +08:00
|
|
|
free(data);
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
_rebuildTextureListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
|
|
|
auto dataLen = _width * _height * 4;
|
|
|
|
auto data = malloc(dataLen);
|
|
|
|
_texture->initWithData(data, dataLen,_texture->getPixelFormat(), _width, _height, Size(_width, _height));
|
|
|
|
free(data);
|
|
|
|
CCLOG("RenderTarget Texture recreated is %d", _texture->getName());
|
|
|
|
});
|
|
|
|
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_rebuildTextureListener, -1);
|
|
|
|
#endif
|
2015-05-27 11:40:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTarget::RenderTarget()
|
|
|
|
: _texture(nullptr)
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
, _rebuildTextureListener(nullptr)
|
|
|
|
#endif
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-04 11:18:35 +08:00
|
|
|
_type = Type::Texture2D;
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderTarget::~RenderTarget()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_texture);
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_rebuildTextureListener);
|
|
|
|
#endif
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-04 11:18:35 +08:00
|
|
|
RenderTargetRenderBuffer::RenderTargetRenderBuffer()
|
|
|
|
: _colorBuffer(0)
|
|
|
|
, _format(GL_RGBA4)
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
, _reBuildRenderBufferListener(nullptr)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
_type = Type::RenderBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTargetRenderBuffer::~RenderTargetRenderBuffer()
|
|
|
|
{
|
|
|
|
if(glIsRenderbuffer(_colorBuffer))
|
|
|
|
{
|
|
|
|
glDeleteRenderbuffers(1, &_colorBuffer);
|
|
|
|
_colorBuffer = 0;
|
|
|
|
}
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_reBuildRenderBufferListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RenderTargetRenderBuffer::init(unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
if(!RenderTargetBase::init(width, height)) return false;
|
|
|
|
GLint oldRenderBuffer(0);
|
|
|
|
glGetIntegerv(GL_RENDERBUFFER_BINDING, &oldRenderBuffer);
|
|
|
|
|
|
|
|
//generate depthStencil
|
|
|
|
glGenRenderbuffers(1, &_colorBuffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, _colorBuffer);
|
|
|
|
//todo: this could have a param
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, _format, width, height);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, oldRenderBuffer);
|
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
_reBuildRenderBufferListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
|
|
|
/** listen the event that renderer was recreated on Android/WP8 */
|
|
|
|
GLint oldRenderBuffer(0);
|
|
|
|
glGetIntegerv(GL_RENDERBUFFER_BINDING, &oldRenderBuffer);
|
|
|
|
|
|
|
|
glGenRenderbuffers(1, &_colorBuffer);
|
|
|
|
//generate depthStencil
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, _colorBuffer);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, _format, _width, _height);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, oldRenderBuffer);
|
|
|
|
CCLOG("RenderTargetRenderBuffer recreated, _colorBuffer is %d", _colorBuffer);
|
|
|
|
});
|
|
|
|
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_reBuildRenderBufferListener, -1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RenderTargetRenderBuffer* RenderTargetRenderBuffer::create(unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
auto result = new (std::nothrow) RenderTargetRenderBuffer();
|
|
|
|
|
|
|
|
if(result && result->init(width, height))
|
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
RenderTargetDepthStencil::RenderTargetDepthStencil()
|
|
|
|
: _depthStencilBuffer(0)
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
, _reBuildDepthStencilListener(nullptr)
|
|
|
|
#endif
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-04 11:18:35 +08:00
|
|
|
_type = Type::RenderBuffer;
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderTargetDepthStencil::~RenderTargetDepthStencil()
|
|
|
|
{
|
|
|
|
if(glIsRenderbuffer(_depthStencilBuffer))
|
|
|
|
{
|
|
|
|
glDeleteRenderbuffers(1, &_depthStencilBuffer);
|
|
|
|
_depthStencilBuffer = 0;
|
|
|
|
}
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_reBuildDepthStencilListener);
|
|
|
|
#endif
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RenderTargetDepthStencil::init(unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
if(!RenderTargetBase::init(width, height)) return false;
|
2015-05-13 14:24:53 +08:00
|
|
|
GLint oldRenderBuffer(0);
|
|
|
|
glGetIntegerv(GL_RENDERBUFFER_BINDING, &oldRenderBuffer);
|
2015-05-12 16:43:17 +08:00
|
|
|
|
|
|
|
//generate depthStencil
|
2015-05-27 11:40:12 +08:00
|
|
|
glGenRenderbuffers(1, &_depthStencilBuffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, _depthStencilBuffer);
|
2015-05-14 10:36:49 +08:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
2015-05-13 14:24:53 +08:00
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, oldRenderBuffer);
|
2015-05-28 17:08:36 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
_reBuildDepthStencilListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
|
|
|
/** listen the event that renderer was recreated on Android/WP8 */
|
|
|
|
GLint oldRenderBuffer(0);
|
|
|
|
glGetIntegerv(GL_RENDERBUFFER_BINDING, &oldRenderBuffer);
|
|
|
|
|
|
|
|
glGenRenderbuffers(1, &_depthStencilBuffer);
|
|
|
|
//generate depthStencil
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, _depthStencilBuffer);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, _width, _height);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, oldRenderBuffer);
|
|
|
|
CCLOG("RenderTargetDepthStencil recreated, _depthStencilBuffer is %d", _depthStencilBuffer);
|
|
|
|
});
|
|
|
|
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_reBuildDepthStencilListener, -1);
|
|
|
|
#endif
|
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RenderTargetDepthStencil* RenderTargetDepthStencil::create(unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
auto result = new (std::nothrow) RenderTargetDepthStencil();
|
|
|
|
|
|
|
|
if(result && result->init(width, height))
|
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
bool FrameBuffer::initWithGLView(GLView* view)
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-04 15:53:13 +08:00
|
|
|
if(view == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-27 11:40:12 +08:00
|
|
|
GLint fbo;
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
|
2015-06-04 15:53:13 +08:00
|
|
|
_fbo = fbo;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
FrameBuffer* FrameBuffer::getOrCreateDefaultFBO(GLView* view)
|
2015-06-04 15:53:13 +08:00
|
|
|
{
|
|
|
|
if(nullptr == _defaultFBO)
|
|
|
|
{
|
2015-06-09 10:59:43 +08:00
|
|
|
auto result = new (std::nothrow) FrameBuffer();
|
2015-06-04 15:53:13 +08:00
|
|
|
|
|
|
|
if(result && result->initWithGLView(view))
|
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
result->_isDefault = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
_defaultFBO = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _defaultFBO;
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
void FrameBuffer::applyDefaultFBO()
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-04 15:53:13 +08:00
|
|
|
if(_defaultFBO)
|
|
|
|
{
|
|
|
|
_defaultFBO->applyFBO();
|
|
|
|
}
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
void FrameBuffer::clearAllFBOs()
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-09 10:59:43 +08:00
|
|
|
for (auto fbo : _frameBuffers)
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
|
|
|
fbo->clearFBO();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
FrameBuffer* FrameBuffer::create(uint8_t fid, unsigned int width, unsigned int height)
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-09 10:59:43 +08:00
|
|
|
auto result = new (std::nothrow) FrameBuffer();
|
2015-05-27 11:40:12 +08:00
|
|
|
if(result && result->init(fid, width, height))
|
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
bool FrameBuffer::init(uint8_t fid, unsigned int width, unsigned int height)
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
|
|
|
_fid = fid;
|
|
|
|
_width = width;
|
|
|
|
_height = height;
|
|
|
|
|
2015-05-28 17:08:36 +08:00
|
|
|
GLint oldfbo;
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldfbo);
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &_fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, oldfbo);
|
|
|
|
|
2015-05-27 11:40:12 +08:00
|
|
|
// _rt = RenderTarget::create(width, height);
|
|
|
|
// if(nullptr == _rt) return false;
|
2015-05-28 17:08:36 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
_dirtyFBOListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
2015-06-04 15:53:13 +08:00
|
|
|
if(isDefaultFBO()) return;
|
2015-05-28 17:08:36 +08:00
|
|
|
GLint oldfbo;
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldfbo);
|
2015-05-27 11:40:12 +08:00
|
|
|
|
2015-05-28 17:08:36 +08:00
|
|
|
glGenFramebuffers(1, &_fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, oldfbo);
|
|
|
|
CCLOG("Recreate FrameBufferObject _fbo is %d", _fbo);
|
|
|
|
_fboBindingDirty = true;
|
|
|
|
});
|
2015-05-27 11:40:12 +08:00
|
|
|
|
2015-05-28 17:08:36 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_dirtyFBOListener, -1);
|
|
|
|
#endif
|
2015-05-13 14:24:53 +08:00
|
|
|
return true;
|
2015-05-12 16:43:17 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
FrameBuffer::FrameBuffer()
|
2015-05-13 17:17:54 +08:00
|
|
|
: _clearColor(Color4F(0, 0, 0, 1))
|
|
|
|
, _clearDepth(1.0)
|
|
|
|
, _clearStencil(0)
|
2015-05-14 11:19:13 +08:00
|
|
|
, _fbo(0)
|
2015-05-27 11:40:12 +08:00
|
|
|
, _rt(nullptr)
|
|
|
|
, _rtDepthStencil(nullptr)
|
2015-05-28 17:08:36 +08:00
|
|
|
, _fboBindingDirty(true)
|
2015-06-04 15:53:13 +08:00
|
|
|
, _isDefault(false)
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
, _dirtyFBOListener(nullptr)
|
|
|
|
#endif
|
2015-05-12 16:43:17 +08:00
|
|
|
{
|
2015-06-09 10:59:43 +08:00
|
|
|
_frameBuffers.insert(this);
|
2015-05-12 16:43:17 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
FrameBuffer::~FrameBuffer()
|
2015-05-12 16:43:17 +08:00
|
|
|
{
|
2015-06-04 15:53:13 +08:00
|
|
|
if(!isDefaultFBO())
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_rt);
|
|
|
|
CC_SAFE_RELEASE_NULL(_rtDepthStencil);
|
|
|
|
glDeleteFramebuffers(1, &_fbo);
|
|
|
|
_fbo = 0;
|
2015-06-09 10:59:43 +08:00
|
|
|
_frameBuffers.erase(this);
|
2015-05-28 17:08:36 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2015-06-04 15:53:13 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_dirtyFBOListener);
|
2015-05-28 17:08:36 +08:00
|
|
|
#endif
|
2015-06-04 15:53:13 +08:00
|
|
|
}
|
2015-05-13 17:17:54 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
void FrameBuffer::clearFBO()
|
2015-05-13 17:17:54 +08:00
|
|
|
{
|
2015-05-28 17:08:36 +08:00
|
|
|
applyFBO();
|
2015-05-13 17:17:54 +08:00
|
|
|
glClearColor(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a);
|
|
|
|
glClearDepth(_clearDepth);
|
|
|
|
glClearStencil(_clearStencil);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
|
|
|
|
applyDefaultFBO();
|
2015-05-12 16:43:17 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
void FrameBuffer::attachRenderTarget(RenderTargetBase* rt)
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-04 15:53:13 +08:00
|
|
|
if(isDefaultFBO())
|
|
|
|
{
|
|
|
|
CCLOG("Can not apply render target to default FBO");
|
|
|
|
return;
|
|
|
|
}
|
2015-05-27 11:40:12 +08:00
|
|
|
CC_ASSERT(rt);
|
|
|
|
if(rt->getWidth() != _width || rt->getHeight() != _height)
|
|
|
|
{
|
|
|
|
CCLOG("Error, attach a render target with different size, Skip.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CC_SAFE_RETAIN(rt);
|
|
|
|
CC_SAFE_RELEASE(_rt);
|
|
|
|
_rt = rt;
|
2015-05-28 17:08:36 +08:00
|
|
|
_fboBindingDirty = true;
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
void FrameBuffer::applyFBO()
|
2015-05-28 17:08:36 +08:00
|
|
|
{
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-05-27 11:40:12 +08:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
|
2015-05-28 17:08:36 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-06-04 15:53:13 +08:00
|
|
|
if(_fboBindingDirty && !isDefaultFBO())
|
2015-05-28 17:08:36 +08:00
|
|
|
{
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-06-04 11:18:35 +08:00
|
|
|
if(RenderTargetBase::Type::Texture2D == _rt->getType())
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _rt->getTexture()->getName(), 0);
|
|
|
|
else
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _rt->getBuffer());
|
2015-05-28 17:08:36 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-06-04 11:18:35 +08:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, nullptr == _rtDepthStencil ? 0 : _rtDepthStencil->getBuffer());
|
2015-05-28 17:08:36 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-06-04 11:18:35 +08:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, nullptr == _rtDepthStencil ? 0 : _rtDepthStencil->getBuffer());
|
2015-05-28 17:08:36 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-06-04 11:18:35 +08:00
|
|
|
CCLOG("FBO is %d _fbo %d color, %d ds", _fbo, RenderTargetBase::Type::Texture2D == _rt->getType() ? _rt->getTexture()->getName() : _rt->getBuffer(), _rtDepthStencil->getBuffer());
|
2015-05-28 17:08:36 +08:00
|
|
|
_fboBindingDirty = false;
|
|
|
|
}
|
|
|
|
if(GL_FRAMEBUFFER_COMPLETE != glCheckFramebufferStatus(GL_FRAMEBUFFER))
|
|
|
|
{
|
|
|
|
CCLOG("FrameBuffer Status Error %d", (int)glCheckFramebufferStatus(GL_FRAMEBUFFER));
|
|
|
|
}
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
void FrameBuffer::attachDepthStencilTarget(RenderTargetDepthStencil* rt)
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
2015-06-04 15:53:13 +08:00
|
|
|
if(isDefaultFBO())
|
|
|
|
{
|
|
|
|
CCLOG("Can not apply depth stencil target to default FBO");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-27 18:16:05 +08:00
|
|
|
if(nullptr != rt && (rt->getWidth() != _width || rt->getHeight() != _height))
|
2015-05-27 11:40:12 +08:00
|
|
|
{
|
|
|
|
CCLOG("Error, attach a render target Depth stencil with different size, Skip.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CC_SAFE_RETAIN(rt);
|
|
|
|
CC_SAFE_RELEASE(_rtDepthStencil);
|
|
|
|
_rtDepthStencil = rt;
|
2015-05-28 17:08:36 +08:00
|
|
|
_fboBindingDirty = true;
|
2015-05-27 11:40:12 +08:00
|
|
|
}
|
2015-06-09 10:59:43 +08:00
|
|
|
} //end of namespace experimental
|
|
|
|
NS_CC_END
|