axmol/core/renderer/backend/opengl/BufferGL.cpp

158 lines
4.5 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
https://axis-project.github.io/
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#include "BufferGL.h"
#include <cassert>
#include "base/CCDirector.h"
#include "base/CCEventType.h"
#include "base/CCEventDispatcher.h"
#include "renderer/backend/opengl/MacrosGL.h"
2019-11-23 20:27:39 +08:00
CC_BACKEND_BEGIN
2021-12-25 10:04:45 +08:00
namespace
{
GLenum toGLUsage(const BufferUsage& usage)
{
switch (usage)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case BufferUsage::STATIC:
return GL_STATIC_DRAW;
case BufferUsage::DYNAMIC:
return GL_DYNAMIC_DRAW;
default:
return GL_DYNAMIC_DRAW;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
} // namespace
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
BufferGL::BufferGL(std::size_t size, BufferType type, BufferUsage usage) : Buffer(size, type, usage)
2019-11-23 20:27:39 +08:00
{
glGenBuffers(1, &_buffer);
#if CC_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
_backToForegroundListener =
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { this->reloadBuffer(); });
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
BufferGL::~BufferGL()
{
if (_buffer)
glDeleteBuffers(1, &_buffer);
#if CC_ENABLE_CACHE_TEXTURE_DATA
CC_SAFE_DELETE_ARRAY(_data);
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
void BufferGL::usingDefaultStoredData(bool needDefaultStoredData)
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
_needDefaultStoredData = needDefaultStoredData;
#endif
}
#if CC_ENABLE_CACHE_TEXTURE_DATA
void BufferGL::reloadBuffer()
{
glGenBuffers(1, &_buffer);
2021-12-25 10:04:45 +08:00
if (!_needDefaultStoredData)
2019-11-23 20:27:39 +08:00
return;
_bufferAlreadyFilled = true;
updateData(_data, _bufferAllocated);
}
void BufferGL::fillBuffer(void* data, std::size_t offset, std::size_t size)
{
2021-12-25 10:04:45 +08:00
if (_bufferAlreadyFilled || !_needDefaultStoredData || BufferUsage::STATIC != _usage)
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
if (_data == nullptr)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_data = new char[_bufferAllocated];
2019-11-23 20:27:39 +08:00
}
memcpy(_data + offset, data, size);
}
#endif
void BufferGL::updateData(void* data, std::size_t size)
{
assert(size && size <= _size);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_buffer)
{
if (BufferType::VERTEX == _type)
{
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glBufferData(GL_ARRAY_BUFFER, size, data, toGLUsage(_usage));
}
else
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, toGLUsage(_usage));
}
CHECK_GL_ERROR_DEBUG();
_bufferAllocated = size;
#if CC_ENABLE_CACHE_TEXTURE_DATA
fillBuffer(data, 0, size);
#endif
}
}
void BufferGL::updateSubData(void* data, std::size_t offset, std::size_t size)
{
CCASSERT(_bufferAllocated != 0, "updateData should be invoke before updateSubData");
CCASSERT(offset + size <= _bufferAllocated, "buffer size overflow");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_buffer)
{
CHECK_GL_ERROR_DEBUG();
if (BufferType::VERTEX == _type)
{
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
}
else
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffer);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, data);
}
#if CC_ENABLE_CACHE_TEXTURE_DATA
fillBuffer(data, offset, size);
#endif
CHECK_GL_ERROR_DEBUG();
}
}
CC_BACKEND_END