2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.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>
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Director.h"
|
|
|
|
#include "base/EventType.h"
|
|
|
|
#include "base/EventDispatcher.h"
|
2021-07-07 13:52:56 +08:00
|
|
|
#include "renderer/backend/opengl/MacrosGL.h"
|
2023-07-19 23:41:16 +08:00
|
|
|
#include "OpenGLState.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
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);
|
|
|
|
|
2023-07-19 23:41:16 +08:00
|
|
|
switch (_type)
|
|
|
|
{
|
|
|
|
case BufferType::VERTEX:
|
|
|
|
_target = GL_ARRAY_BUFFER;
|
|
|
|
case BufferType::INDEX:
|
|
|
|
_target = GL_ELEMENT_ARRAY_BUFFER;
|
|
|
|
default: // case BufferType::UNIFORM:
|
|
|
|
_target = GL_UNIFORM_BUFFER;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_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)
|
2023-07-19 23:41:16 +08:00
|
|
|
{
|
2023-07-20 17:55:16 +08:00
|
|
|
__gl->bindBuffer(_target, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
glDeleteBuffers(1, &_buffer);
|
2023-07-19 23:41:16 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
AX_SAFE_DELETE_ARRAY(_data);
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferGL::usingDefaultStoredData(bool needDefaultStoredData)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
_needDefaultStoredData = needDefaultStoredData;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-19 23:41:16 +08:00
|
|
|
void BufferGL::fillBuffer(const void* data, std::size_t offset, std::size_t size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
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
|
|
|
|
|
2023-07-19 23:41:16 +08:00
|
|
|
void BufferGL::updateData(const void* data, std::size_t size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
assert(size && size <= _size);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_buffer)
|
|
|
|
{
|
2023-07-20 17:55:16 +08:00
|
|
|
__gl->bindBuffer(_target, _buffer);
|
2023-07-19 23:41:16 +08:00
|
|
|
glBufferData(_target, size, data, toGLUsage(_usage));
|
2019-11-23 20:27:39 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
_bufferAllocated = size;
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2023-07-19 23:41:16 +08:00
|
|
|
if (data)
|
|
|
|
fillBuffer(data, 0, size);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 23:41:16 +08:00
|
|
|
void BufferGL::updateSubData(const void* data, std::size_t offset, std::size_t size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_bufferAllocated != 0, "updateData should be invoke before updateSubData");
|
|
|
|
AXASSERT(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();
|
2023-07-19 23:41:16 +08:00
|
|
|
|
2023-07-20 17:55:16 +08:00
|
|
|
__gl->bindBuffer(_target, _buffer);
|
2023-07-19 23:41:16 +08:00
|
|
|
glBufferSubData(_target, offset, size, data);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
fillBuffer(data, offset, size);
|
|
|
|
#endif
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|