2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
2022-04-25 19:15:46 +08:00
|
|
|
Copyright (c) 2020 C4games Ltd.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
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 "ProgramGL.h"
|
|
|
|
#include "ShaderModuleGL.h"
|
|
|
|
#include "renderer/backend/Types.h"
|
2021-07-07 13:52:56 +08:00
|
|
|
#include "renderer/backend/opengl/MacrosGL.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Director.h"
|
|
|
|
#include "base/EventDispatcher.h"
|
|
|
|
#include "base/EventType.h"
|
2022-10-13 21:44:42 +08:00
|
|
|
#include "base/axstd.h"
|
2023-07-18 00:10:57 +08:00
|
|
|
#include "yasio/byte_buffer.hpp"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/opengl/UtilsGL.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2020-02-24 20:59:11 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ProgramGL::ProgramGL(std::string_view vertexShader, std::string_view fragmentShader)
|
2021-12-25 10:04:45 +08:00
|
|
|
: Program(vertexShader, fragmentShader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_vertexShaderModule = static_cast<ShaderModuleGL*>(ShaderCache::newVertexShaderModule(_vertexShader));
|
2019-11-23 20:27:39 +08:00
|
|
|
_fragmentShaderModule = static_cast<ShaderModuleGL*>(ShaderCache::newFragmentShaderModule(_fragmentShader));
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(_vertexShaderModule);
|
|
|
|
AX_SAFE_RETAIN(_fragmentShaderModule);
|
2019-11-23 20:27:39 +08:00
|
|
|
compileProgram();
|
|
|
|
computeUniformInfos();
|
|
|
|
computeLocations();
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& uniform : _activeUniformInfos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto location = uniform.second.location;
|
2019-11-23 20:27:39 +08:00
|
|
|
_originalUniformLocations[uniform.first] = location;
|
2021-12-25 10:04:45 +08:00
|
|
|
_mapToCurrentActiveLocation[location] = location;
|
|
|
|
_mapToOriginalLocation[location] = location;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_backToForegroundListener =
|
|
|
|
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { this->reloadProgram(); });
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramGL::~ProgramGL()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_vertexShaderModule);
|
|
|
|
AX_SAFE_RELEASE(_fragmentShaderModule);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_program)
|
|
|
|
glDeleteProgram(_program);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
void ProgramGL::reloadProgram()
|
|
|
|
{
|
|
|
|
_activeUniformInfos.clear();
|
|
|
|
_mapToCurrentActiveLocation.clear();
|
|
|
|
_mapToOriginalLocation.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
static_cast<ShaderModuleGL*>(_vertexShaderModule)
|
2023-07-17 20:51:28 +08:00
|
|
|
->compileShader(backend::ShaderStage::VERTEX, _vertexShader);
|
2021-12-25 10:04:45 +08:00
|
|
|
static_cast<ShaderModuleGL*>(_fragmentShaderModule)
|
2023-07-17 20:51:28 +08:00
|
|
|
->compileShader(backend::ShaderStage::FRAGMENT, _fragmentShader);
|
2019-11-23 20:27:39 +08:00
|
|
|
compileProgram();
|
|
|
|
computeUniformInfos();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& uniform : _activeUniformInfos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto location = _originalUniformLocations[uniform.first];
|
|
|
|
_mapToCurrentActiveLocation[location] = uniform.second.location;
|
2019-11-23 20:27:39 +08:00
|
|
|
_mapToOriginalLocation[uniform.second.location] = location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ProgramGL::compileProgram()
|
|
|
|
{
|
|
|
|
if (_vertexShaderModule == nullptr || _fragmentShaderModule == nullptr)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto vertShader = _vertexShaderModule->getShader();
|
|
|
|
auto fragShader = _fragmentShaderModule->getShader();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
assert(vertShader != 0 && fragShader != 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (vertShader == 0 || fragShader == 0)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_program = glCreateProgram();
|
|
|
|
if (!_program)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
glAttachShader(_program, vertShader);
|
|
|
|
glAttachShader(_program, fragShader);
|
2020-02-12 20:58:25 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
glLinkProgram(_program);
|
2020-02-12 20:58:25 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
GLint status = 0;
|
|
|
|
glGetProgramiv(_program, GL_LINK_STATUS, &status);
|
|
|
|
if (GL_FALSE == status)
|
|
|
|
{
|
2020-02-12 20:58:25 +08:00
|
|
|
GLint errorInfoLen = 0;
|
|
|
|
glGetProgramiv(_program, GL_INFO_LOG_LENGTH, &errorInfoLen);
|
2022-07-07 09:30:48 +08:00
|
|
|
if (errorInfoLen > 1)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-10-13 21:44:42 +08:00
|
|
|
auto errorInfo = axstd::make_unique_for_overwrite<char[]>(static_cast<size_t>(errorInfoLen));
|
|
|
|
glGetProgramInfoLog(_program, errorInfoLen, NULL, errorInfo.get());
|
|
|
|
log("cocos2d: ERROR: %s: failed to link program: %s ", __FUNCTION__, errorInfo.get());
|
2020-02-12 20:58:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
log("cocos2d: ERROR: %s: failed to link program ", __FUNCTION__);
|
2019-11-23 20:27:39 +08:00
|
|
|
glDeleteProgram(_program);
|
|
|
|
_program = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramGL::computeLocations()
|
|
|
|
{
|
|
|
|
std::fill(_builtinAttributeLocation, _builtinAttributeLocation + ATTRIBUTE_MAX, -1);
|
2021-12-25 10:04:45 +08:00
|
|
|
// std::fill(_builtinUniformLocation, _builtinUniformLocation + UNIFORM_MAX, -1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// a_position
|
2022-09-24 10:42:11 +08:00
|
|
|
auto location = glGetAttribLocation(_program, ATTRIBUTE_NAME_POSITION.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinAttributeLocation[Attribute::POSITION] = location;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// a_color
|
2022-09-24 10:42:11 +08:00
|
|
|
location = glGetAttribLocation(_program, ATTRIBUTE_NAME_COLOR.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinAttributeLocation[Attribute::COLOR] = location;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// a_texCoord
|
2022-09-24 10:42:11 +08:00
|
|
|
location = glGetAttribLocation(_program, ATTRIBUTE_NAME_TEXCOORD.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinAttributeLocation[Attribute::TEXCOORD] = location;
|
|
|
|
|
2022-09-23 22:41:30 +08:00
|
|
|
// a_normal
|
2022-09-24 10:42:11 +08:00
|
|
|
location = glGetAttribLocation(_program, ATTRIBUTE_NAME_NORMAL.data());
|
2022-09-23 22:41:30 +08:00
|
|
|
_builtinAttributeLocation[Attribute::NORMAL] = location;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// u_MVPMatrix
|
2023-07-17 20:51:28 +08:00
|
|
|
|
|
|
|
location = _ubo.getUniformLocationByName(UNIFORM_NAME_MVP_MATRIX.data()).location[0];
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinUniformLocation[Uniform::MVP_MATRIX].location[0] = location;
|
2021-12-25 10:04:45 +08:00
|
|
|
_builtinUniformLocation[Uniform::MVP_MATRIX].location[1] =
|
|
|
|
_activeUniformInfos[UNIFORM_NAME_MVP_MATRIX].bufferOffset;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// u_textColor
|
2023-07-17 20:51:28 +08:00
|
|
|
location = _ubo.getUniformLocationByName(UNIFORM_NAME_TEXT_COLOR.data()).location[0];
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinUniformLocation[Uniform::TEXT_COLOR].location[0] = location;
|
2021-12-25 10:04:45 +08:00
|
|
|
_builtinUniformLocation[Uniform::TEXT_COLOR].location[1] =
|
|
|
|
_activeUniformInfos[UNIFORM_NAME_TEXT_COLOR].bufferOffset;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// u_effectColor
|
2023-07-17 20:51:28 +08:00
|
|
|
location = _ubo.getUniformLocationByName(UNIFORM_NAME_EFFECT_COLOR.data()).location[0];
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinUniformLocation[Uniform::EFFECT_COLOR].location[0] = location;
|
2021-12-25 10:04:45 +08:00
|
|
|
_builtinUniformLocation[Uniform::EFFECT_COLOR].location[1] =
|
|
|
|
_activeUniformInfos[UNIFORM_NAME_EFFECT_COLOR].bufferOffset;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// u_effectType
|
2023-07-17 20:51:28 +08:00
|
|
|
location = _ubo.getUniformLocationByName(UNIFORM_NAME_EFFECT_TYPE.data()).location[0];
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinUniformLocation[Uniform::EFFECT_TYPE].location[0] = location;
|
2021-12-25 10:04:45 +08:00
|
|
|
_builtinUniformLocation[Uniform::EFFECT_TYPE].location[1] =
|
|
|
|
_activeUniformInfos[UNIFORM_NAME_EFFECT_TYPE].bufferOffset;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 00:39:20 +08:00
|
|
|
/// u_tex0
|
2022-09-24 10:42:11 +08:00
|
|
|
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXTURE.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinUniformLocation[Uniform::TEXTURE].location[0] = location;
|
|
|
|
|
2022-07-05 00:39:20 +08:00
|
|
|
/// u_tex1
|
2022-09-24 10:42:11 +08:00
|
|
|
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXTURE1.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
_builtinUniformLocation[Uniform::TEXTURE1].location[0] = location;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool ProgramGL::getAttributeLocation(std::string_view attributeName, unsigned int& location) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
GLint loc = glGetAttribLocation(_program, attributeName.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (-1 == loc)
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("axmol: %s: can not find vertex attribute of %s", __FUNCTION__, attributeName.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
location = GLuint(loc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:38:41 +08:00
|
|
|
const hlookup::string_map<AttributeBindInfo>& ProgramGL::getActiveAttributes() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-09-24 11:38:41 +08:00
|
|
|
if (!_program || !_activeAttribs.empty())
|
|
|
|
return _activeAttribs;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
GLint numOfActiveAttributes = 0;
|
|
|
|
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTES, &numOfActiveAttributes);
|
|
|
|
|
|
|
|
if (numOfActiveAttributes <= 0)
|
2022-09-24 11:38:41 +08:00
|
|
|
return _activeAttribs;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-09-24 11:38:41 +08:00
|
|
|
_activeAttribs.reserve(numOfActiveAttributes);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-13 21:44:42 +08:00
|
|
|
int MAX_ATTRIBUTE_NAME_LENGTH = 255;
|
|
|
|
auto attrName = axstd::make_unique_for_overwrite<char[]>(MAX_ATTRIBUTE_NAME_LENGTH + 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
GLint attrNameLen = 0;
|
|
|
|
GLenum attrType;
|
|
|
|
GLint attrSize;
|
|
|
|
backend::AttributeBindInfo info;
|
|
|
|
|
|
|
|
for (int i = 0; i < numOfActiveAttributes; i++)
|
|
|
|
{
|
2022-10-13 21:44:42 +08:00
|
|
|
glGetActiveAttrib(_program, i, MAX_ATTRIBUTE_NAME_LENGTH, &attrNameLen, &attrSize, &attrType, attrName.get());
|
2019-11-23 20:27:39 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2022-10-13 21:44:42 +08:00
|
|
|
info.attributeName = std::string(attrName.get(), attrName.get() + attrNameLen);
|
2021-12-25 10:04:45 +08:00
|
|
|
info.location = glGetAttribLocation(_program, info.attributeName.c_str());
|
|
|
|
info.type = attrType;
|
|
|
|
info.size = UtilsGL::getGLDataTypeSize(attrType) * attrSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2022-09-24 11:38:41 +08:00
|
|
|
_activeAttribs[info.attributeName] = info;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-09-24 11:38:41 +08:00
|
|
|
return _activeAttribs;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramGL::computeUniformInfos()
|
|
|
|
{
|
|
|
|
if (!_program)
|
2020-10-29 19:13:36 +08:00
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2023-07-17 20:51:28 +08:00
|
|
|
_ubo.setProgram(_program);
|
|
|
|
_ubo.bindUniformBlock(UniformBlockStage::ALL_STAGES);
|
|
|
|
_ubo.generateUniformOffsets(UniformBlockStage::ALL_STAGES);
|
|
|
|
_ubo.allocateUBOBuffer();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
GLint numOfUniforms = 0;
|
|
|
|
glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &numOfUniforms);
|
|
|
|
if (!numOfUniforms)
|
2020-10-29 19:13:36 +08:00
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
UniformInfo uniform;
|
2021-12-25 10:04:45 +08:00
|
|
|
GLint length = 0;
|
2023-07-17 20:51:28 +08:00
|
|
|
_totalFragBufferSize = 0;
|
|
|
|
_totalVertBufferSize = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_maxLocation = -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
_activeUniformInfos.clear();
|
2020-10-29 19:13:36 +08:00
|
|
|
GLchar uniformName[MAX_UNIFORM_NAME_LENGTH + 1];
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = 0; i < numOfUniforms; ++i)
|
|
|
|
{
|
2023-07-17 20:51:28 +08:00
|
|
|
uniform.isUBO = false;
|
|
|
|
uniform.isFragment = false;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
glGetActiveUniform(_program, i, MAX_UNIFORM_NAME_LENGTH, &length, &uniform.count, &uniform.type, uniformName);
|
|
|
|
uniformName[length] = '\0';
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2023-07-17 20:51:28 +08:00
|
|
|
if (memcmp(uniformName, "vs_ub.", 5 * sizeof(GLchar)) == 0 ||
|
|
|
|
memcmp(uniformName, "fs_ub.", 5 * sizeof(GLchar)) == 0)
|
|
|
|
{
|
|
|
|
if (uniformName[0] == 'f')
|
|
|
|
uniform.isFragment = true;
|
|
|
|
|
|
|
|
while (uniformName[0] != '.')
|
|
|
|
for (int i = 0; i < MAX_UNIFORM_NAME_LENGTH; i++)
|
|
|
|
uniformName[i] = uniformName[i + 1];
|
|
|
|
if (uniformName[0] == '.')
|
|
|
|
for (int i = 0; i < MAX_UNIFORM_NAME_LENGTH; i++)
|
|
|
|
uniformName[i] = uniformName[i + 1];
|
|
|
|
|
|
|
|
uniform.isUBO = true;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (length > 3)
|
|
|
|
{
|
|
|
|
char* c = strrchr(uniformName, '[');
|
|
|
|
if (c)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
*c = '\0';
|
2019-11-23 20:27:39 +08:00
|
|
|
uniform.isArray = true;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
uniform.location = glGetUniformLocation(_program, uniformName);
|
|
|
|
uniform.size = UtilsGL::getGLDataTypeSize(uniform.type);
|
2023-07-18 00:10:57 +08:00
|
|
|
uniform.bufferOffset = (uniform.size == 0u) ? 0 : static_cast<unsigned>(uniform.isFragment ? _totalFragBufferSize : _totalVertBufferSize);
|
2023-07-17 20:51:28 +08:00
|
|
|
if (_activeUniformInfos.find(uniformName) == _activeUniformInfos.end())
|
|
|
|
_activeUniformInfos[uniformName] = uniform;
|
|
|
|
(uniform.isFragment ? _totalFragBufferSize : _totalVertBufferSize) += uniform.size * uniform.count;
|
2019-11-23 20:27:39 +08:00
|
|
|
_maxLocation = _maxLocation <= uniform.location ? (uniform.location + 1) : _maxLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:51:28 +08:00
|
|
|
void ProgramGL::generateUniformBlockBuffers()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int ProgramGL::getAttributeLocation(Attribute name) const
|
|
|
|
{
|
|
|
|
return _builtinAttributeLocation[name];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int ProgramGL::getAttributeLocation(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return glGetAttribLocation(_program, name.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-17 20:51:28 +08:00
|
|
|
inline std::string_view mapLocationEnumToUBO(backend::Uniform name)
|
|
|
|
{
|
|
|
|
switch (name)
|
|
|
|
{
|
|
|
|
case Uniform::MVP_MATRIX:
|
|
|
|
return UNIFORM_NAME_MVP_MATRIX;
|
|
|
|
break;
|
|
|
|
case Uniform::TEXTURE:
|
|
|
|
return UNIFORM_NAME_TEXTURE;
|
|
|
|
break;
|
|
|
|
case Uniform::TEXTURE1:
|
|
|
|
return UNIFORM_NAME_TEXTURE1;
|
|
|
|
break;
|
|
|
|
case Uniform::TEXTURE2:
|
|
|
|
return UNIFORM_NAME_TEXTURE2;
|
|
|
|
break;
|
|
|
|
case Uniform::TEXTURE3:
|
|
|
|
return UNIFORM_NAME_TEXTURE3;
|
|
|
|
break;
|
|
|
|
case Uniform::TEXT_COLOR:
|
|
|
|
return UNIFORM_NAME_TEXT_COLOR;
|
|
|
|
break;
|
|
|
|
case Uniform::EFFECT_COLOR:
|
|
|
|
return UNIFORM_NAME_EFFECT_COLOR;
|
|
|
|
break;
|
|
|
|
case Uniform::EFFECT_TYPE:
|
|
|
|
return UNIFORM_NAME_EFFECT_TYPE;
|
|
|
|
break;
|
|
|
|
}
|
2023-07-18 00:10:57 +08:00
|
|
|
return ""sv;
|
2023-07-17 20:51:28 +08:00
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
UniformLocation ProgramGL::getUniformLocation(backend::Uniform name) const
|
|
|
|
{
|
2023-07-17 20:51:28 +08:00
|
|
|
auto loc = _builtinUniformLocation[name];
|
|
|
|
if (loc.location[0] == UNIFORM_BLOCK_IDENTIFIER)
|
|
|
|
return _ubo.getUniformLocationByName(mapLocationEnumToUBO(name).data());
|
|
|
|
else
|
|
|
|
return loc;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UniformLocation ProgramGL::getUniformLocation(std::string_view uniform) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
UniformLocation uniformLocation;
|
2023-07-17 20:51:28 +08:00
|
|
|
if (_ubo.uniformOffsetsByName.find(uniform.data()) != _ubo.uniformOffsetsByName.end())
|
|
|
|
{
|
|
|
|
uniformLocation = _ubo.uniformOffsetsByName.at(uniform.data());
|
|
|
|
}
|
|
|
|
else if (_activeUniformInfos.find(uniform) != _activeUniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto& uniformInfo = _activeUniformInfos.at(uniform);
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
uniformLocation.location[0] = _mapToOriginalLocation.at(uniformInfo.location);
|
|
|
|
#else
|
|
|
|
uniformLocation.location[0] = uniformInfo.location;
|
|
|
|
#endif
|
|
|
|
uniformLocation.location[1] = uniformInfo.bufferOffset;
|
|
|
|
}
|
2023-07-17 20:51:28 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return uniformLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProgramGL::getMaxVertexLocation() const
|
|
|
|
{
|
|
|
|
return _maxLocation;
|
|
|
|
}
|
|
|
|
int ProgramGL::getMaxFragmentLocation() const
|
|
|
|
{
|
|
|
|
return _maxLocation;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
int ProgramGL::getMappedLocation(int location) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_mapToCurrentActiveLocation.find(location) != _mapToCurrentActiveLocation.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
return _mapToCurrentActiveLocation.at(location);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProgramGL::getOriginalLocation(int location) const
|
|
|
|
{
|
|
|
|
if (_mapToOriginalLocation.find(location) != _mapToOriginalLocation.end())
|
|
|
|
return _mapToOriginalLocation.at(location);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const UniformInfo& ProgramGL::getActiveUniformInfo(ShaderStage stage, int location) const
|
|
|
|
{
|
2020-02-28 12:10:27 +08:00
|
|
|
static const UniformInfo s_emptyInfo{};
|
|
|
|
return s_emptyInfo;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
const hlookup::string_map<UniformInfo>& ProgramGL::getAllActiveUniformInfo(ShaderStage stage) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _activeUniformInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t ProgramGL::getUniformBufferSize(ShaderStage stage) const
|
|
|
|
{
|
2023-07-17 20:51:28 +08:00
|
|
|
return stage == ShaderStage::VERTEX ? _totalVertBufferSize : _totalFragBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformBlockDescriptor::bindUniformBlock(GLint _program, UniformBlockStage _stage)
|
|
|
|
{
|
|
|
|
this->stage = _stage;
|
|
|
|
|
|
|
|
const char* blockName = stage == UniformBlockStage::VERTEX ? "vs_ub" : "fs_ub";
|
|
|
|
blockIndex = glGetUniformBlockIndex(_program, blockName);
|
|
|
|
|
|
|
|
bindingPoint = (GLint)stage - 1;
|
|
|
|
glUniformBlockBinding(_program, blockIndex, bindingPoint);
|
|
|
|
|
|
|
|
glGetActiveUniformBlockiv(_program, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
|
|
|
|
|
|
|
|
if (blockSize == -1)
|
|
|
|
{
|
|
|
|
stage = UniformBlockStage::UBO_NOT_FOUND;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
glGetActiveUniformBlockiv(_program, blockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &numUniforms);
|
|
|
|
|
|
|
|
uniformIndices = new GLint[numUniforms];
|
|
|
|
glGetActiveUniformBlockiv(_program, blockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, uniformIndices);
|
|
|
|
|
|
|
|
uniformOffsets = new GLint[numUniforms];
|
|
|
|
glGetActiveUniformsiv(_program, numUniforms, reinterpret_cast<GLuint*>(uniformIndices), GL_UNIFORM_OFFSET,
|
|
|
|
uniformOffsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformBlockDescriptor::~UniformBlockDescriptor()
|
|
|
|
{
|
|
|
|
AX_SAFE_DELETE(uniformIndices);
|
|
|
|
AX_SAFE_DELETE(uniformOffsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformBlockHandler::generateUniformOffsets(UniformBlockStage _stage)
|
|
|
|
{
|
|
|
|
if (_stage == UniformBlockStage::ALL_STAGES)
|
|
|
|
{
|
|
|
|
generateUniformOffsets(UniformBlockStage::VERTEX);
|
|
|
|
generateUniformOffsets(UniformBlockStage::FRAGMENT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto&& ubo = _stage == UniformBlockStage::VERTEX ? _vertexUBO : _fragmentUBO;
|
|
|
|
|
|
|
|
if (ubo.stage == UniformBlockStage::UBO_NOT_FOUND)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vertBlockSize = _stage == UniformBlockStage::VERTEX ? ubo.blockSize : vertBlockSize;
|
|
|
|
fragBlockSize = _stage == UniformBlockStage::FRAGMENT ? ubo.blockSize : fragBlockSize;
|
|
|
|
|
2023-07-18 00:10:57 +08:00
|
|
|
yasio::basic_byte_buffer<GLchar> buffer;
|
|
|
|
for (GLint i = 0; i < ubo.numUniforms; ++i)
|
2023-07-17 20:51:28 +08:00
|
|
|
{
|
|
|
|
GLint uniformOffset = ubo.uniformOffsets[i];
|
|
|
|
|
2023-07-18 00:10:57 +08:00
|
|
|
buffer.resize(MAX_UNIFORM_NAME_LENGTH, std::true_type{});
|
2023-07-17 20:51:28 +08:00
|
|
|
|
|
|
|
GLint index = ubo.uniformIndices[i];
|
|
|
|
|
2023-07-18 00:10:57 +08:00
|
|
|
GLsizei cch{};
|
|
|
|
GLenum dataType{};
|
|
|
|
GLint arrayCount{};
|
|
|
|
glGetActiveUniform(_program, ubo.uniformIndices[i], MAX_UNIFORM_NAME_LENGTH, &cch, &arrayCount,
|
|
|
|
&dataType,
|
|
|
|
buffer.data());
|
|
|
|
std::string_view uniformName{buffer.data(), static_cast<size_t>(cch)};
|
2023-07-17 20:51:28 +08:00
|
|
|
|
2023-07-18 00:10:57 +08:00
|
|
|
auto dot = uniformName.find_last_of('.');
|
|
|
|
if (dot != std::string::npos)
|
|
|
|
uniformName.remove_prefix(dot + 1);
|
2023-07-17 20:51:28 +08:00
|
|
|
|
|
|
|
auto loc = UniformLocation{};
|
|
|
|
loc.location[0] = UNIFORM_BLOCK_IDENTIFIER;
|
|
|
|
loc.location[1] = uniformOffset / sizeof(float);
|
|
|
|
loc.shaderStage = _stage == UniformBlockStage::VERTEX ? ShaderStage::VERTEX : ShaderStage::FRAGMENT;
|
|
|
|
|
2023-07-18 00:10:57 +08:00
|
|
|
uniformOffsetsByName.emplace(std::string{uniformName}, loc);
|
2023-07-17 20:51:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformBlockHandler::allocateUBOBuffer()
|
|
|
|
{
|
|
|
|
if (_UBOVertBuffer || _UBOFragBuffer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int totalStagesSize = vertBlockSize + fragBlockSize;
|
|
|
|
int32_t* dummy = new int32_t[totalStagesSize];
|
|
|
|
memset(dummy, 0, totalStagesSize);
|
|
|
|
|
|
|
|
_UBOVertBuffer =
|
|
|
|
(BufferGL*)Device::getInstance()->newBuffer(vertBlockSize, BufferType::UNIFORM, BufferUsage::DYNAMIC);
|
|
|
|
_UBOVertBuffer->updateData(dummy, vertBlockSize);
|
|
|
|
|
|
|
|
_UBOFragBuffer =
|
|
|
|
(BufferGL*)Device::getInstance()->newBuffer(vertBlockSize, BufferType::UNIFORM, BufferUsage::DYNAMIC);
|
|
|
|
_UBOFragBuffer->updateData(dummy, vertBlockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformLocation UniformBlockHandler::getUniformLocationByName(std::string name) const
|
|
|
|
{
|
|
|
|
auto it = uniformOffsetsByName.find(name);
|
|
|
|
if (it != uniformOffsetsByName.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformBlockHandler::~UniformBlockHandler()
|
|
|
|
{
|
|
|
|
AX_SAFE_RELEASE(_UBOVertBuffer);
|
|
|
|
AX_SAFE_RELEASE(_UBOFragBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformBlockHandler::bindUniformBlock(UniformBlockStage _stage)
|
|
|
|
{
|
|
|
|
switch (_stage)
|
|
|
|
{
|
|
|
|
case UniformBlockStage::VERTEX:
|
|
|
|
_vertexUBO.bindUniformBlock(_program, UniformBlockStage::VERTEX);
|
|
|
|
break;
|
|
|
|
case UniformBlockStage::FRAGMENT:
|
|
|
|
_fragmentUBO.bindUniformBlock(_program, UniformBlockStage::FRAGMENT);
|
|
|
|
break;
|
|
|
|
case UniformBlockStage::ALL_STAGES:
|
|
|
|
_vertexUBO.bindUniformBlock(_program, UniformBlockStage::VERTEX);
|
|
|
|
_fragmentUBO.bindUniformBlock(_program, UniformBlockStage::FRAGMENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformBlockHandler::bindUniformBlocks()
|
|
|
|
{
|
|
|
|
bindUniformBlock(UniformBlockStage::ALL_STAGES);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|