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-09-02 19:56:50 +08:00
|
|
|
#include "yasio/byte_buffer.hpp"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/opengl/UtilsGL.h"
|
2023-09-02 19:56:50 +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
|
2020-02-24 20:59:11 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
#if AX_GLES_PROFILE == 200
|
|
|
|
# define DEF_TO_INT(pointer, index) (*((GLint*)(pointer) + index))
|
|
|
|
# define DEF_TO_FLOAT(pointer, index) (*((GLfloat*)(pointer) + index))
|
|
|
|
static void setUniform(bool isArray, GLuint location, unsigned int size, GLenum uniformType, void* data)
|
2023-08-13 23:58:18 +08:00
|
|
|
{
|
2023-09-02 19:56:50 +08:00
|
|
|
GLsizei count = size;
|
|
|
|
switch (uniformType)
|
|
|
|
{
|
|
|
|
case GL_INT:
|
|
|
|
case GL_BOOL:
|
|
|
|
case GL_SAMPLER_2D:
|
|
|
|
case GL_SAMPLER_CUBE:
|
|
|
|
if (isArray)
|
|
|
|
glUniform1iv(location, count, (GLint*)data);
|
|
|
|
else
|
|
|
|
glUniform1i(location, DEF_TO_INT(data, 0));
|
|
|
|
break;
|
|
|
|
case GL_INT_VEC2:
|
|
|
|
case GL_BOOL_VEC2:
|
|
|
|
if (isArray)
|
|
|
|
glUniform2iv(location, count, (GLint*)data);
|
|
|
|
else
|
|
|
|
glUniform2i(location, DEF_TO_INT(data, 0), DEF_TO_INT(data, 1));
|
|
|
|
break;
|
|
|
|
case GL_INT_VEC3:
|
|
|
|
case GL_BOOL_VEC3:
|
|
|
|
if (isArray)
|
|
|
|
glUniform3iv(location, count, (GLint*)data);
|
|
|
|
else
|
|
|
|
glUniform3i(location, DEF_TO_INT(data, 0), DEF_TO_INT(data, 1), DEF_TO_INT(data, 2));
|
|
|
|
break;
|
|
|
|
case GL_INT_VEC4:
|
|
|
|
case GL_BOOL_VEC4:
|
|
|
|
if (isArray)
|
|
|
|
glUniform4iv(location, count, (GLint*)data);
|
|
|
|
else
|
|
|
|
glUniform4i(location, DEF_TO_INT(data, 0), DEF_TO_INT(data, 1), DEF_TO_INT(data, 2), DEF_TO_INT(data, 4));
|
|
|
|
break;
|
|
|
|
case GL_FLOAT:
|
|
|
|
if (isArray)
|
|
|
|
glUniform1fv(location, count, (GLfloat*)data);
|
|
|
|
else
|
|
|
|
glUniform1f(location, DEF_TO_FLOAT(data, 0));
|
|
|
|
break;
|
|
|
|
case GL_FLOAT_VEC2:
|
|
|
|
if (isArray)
|
|
|
|
glUniform2fv(location, count, (GLfloat*)data);
|
|
|
|
else
|
|
|
|
glUniform2f(location, DEF_TO_FLOAT(data, 0), DEF_TO_FLOAT(data, 1));
|
|
|
|
break;
|
|
|
|
case GL_FLOAT_VEC3:
|
|
|
|
if (isArray)
|
|
|
|
glUniform3fv(location, count, (GLfloat*)data);
|
|
|
|
else
|
|
|
|
glUniform3f(location, DEF_TO_FLOAT(data, 0), DEF_TO_FLOAT(data, 1), DEF_TO_FLOAT(data, 2));
|
|
|
|
break;
|
|
|
|
case GL_FLOAT_VEC4:
|
|
|
|
if (isArray)
|
|
|
|
glUniform4fv(location, count, (GLfloat*)data);
|
|
|
|
else
|
|
|
|
glUniform4f(location, DEF_TO_FLOAT(data, 0), DEF_TO_FLOAT(data, 1), DEF_TO_FLOAT(data, 2),
|
|
|
|
DEF_TO_FLOAT(data, 3));
|
|
|
|
break;
|
|
|
|
case GL_FLOAT_MAT2:
|
|
|
|
glUniformMatrix2fv(location, count, GL_FALSE, (GLfloat*)data);
|
|
|
|
break;
|
|
|
|
case GL_FLOAT_MAT3:
|
|
|
|
glUniformMatrix3fv(location, count, GL_FALSE, (GLfloat*)data);
|
|
|
|
break;
|
|
|
|
case GL_FLOAT_MAT4:
|
|
|
|
glUniformMatrix4fv(location, count, GL_FALSE, (GLfloat*)data);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
AXASSERT(false, "invalidate Uniform data type");
|
|
|
|
break;
|
|
|
|
}
|
2023-08-13 23:58:18 +08:00
|
|
|
}
|
2023-09-02 19:56:50 +08:00
|
|
|
#endif
|
2023-08-13 23:58:18 +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();
|
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
|
2023-09-02 19:56:50 +08:00
|
|
|
|
|
|
|
setBuiltinLocations();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgramGL::~ProgramGL()
|
|
|
|
{
|
2023-09-02 19:56:50 +08:00
|
|
|
clearUniformBuffers();
|
|
|
|
|
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();
|
2023-09-02 19:56:50 +08:00
|
|
|
static_cast<ShaderModuleGL*>(_vertexShaderModule)->compileShader(backend::ShaderStage::VERTEX, _vertexShader);
|
|
|
|
static_cast<ShaderModuleGL*>(_fragmentShaderModule)->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());
|
2023-09-02 19:56:50 +08:00
|
|
|
ax::log("axmol:ERROR: %s: failed to link program: %s ", __FUNCTION__, errorInfo.get());
|
2020-02-12 20:58:25 +08:00
|
|
|
}
|
|
|
|
else
|
2023-09-02 19:56:50 +08:00
|
|
|
ax::log("axmol:ERROR: %s: failed to link program ", __FUNCTION__);
|
2019-11-23 20:27:39 +08:00
|
|
|
glDeleteProgram(_program);
|
|
|
|
_program = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
void ProgramGL::setBuiltinLocations()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-09-02 19:56:50 +08:00
|
|
|
/*--- Builtin Attribs ---*/
|
|
|
|
|
|
|
|
std::fill(_builtinAttributeLocation, _builtinAttributeLocation + Attribute::ATTRIBUTE_MAX, -1);
|
2023-08-17 20:22:24 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// a_position
|
2023-09-02 19:56:50 +08:00
|
|
|
_builtinAttributeLocation[Attribute::POSITION] = getAttributeLocation(ATTRIBUTE_NAME_POSITION);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// a_color
|
2023-09-02 19:56:50 +08:00
|
|
|
_builtinAttributeLocation[Attribute::COLOR] = getAttributeLocation(ATTRIBUTE_NAME_COLOR);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// a_texCoord
|
2023-09-02 19:56:50 +08:00
|
|
|
_builtinAttributeLocation[Attribute::TEXCOORD] = getAttributeLocation(ATTRIBUTE_NAME_TEXCOORD);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-09-23 22:41:30 +08:00
|
|
|
// a_normal
|
2023-09-02 19:56:50 +08:00
|
|
|
_builtinAttributeLocation[Attribute::NORMAL] = getAttributeLocation(ATTRIBUTE_NAME_NORMAL);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
// a_instance
|
|
|
|
_builtinAttributeLocation[Attribute::INSTANCE] = getAttributeLocation(ATTRIBUTE_NAME_INSTANCE);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
/*--- Builtin Uniforms ---*/
|
2022-09-23 22:41:30 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
/// u_MVPMatrix
|
|
|
|
_builtinUniformLocation[Uniform::MVP_MATRIX] = getUniformLocation(UNIFORM_NAME_MVP_MATRIX);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
|
|
|
/// u_tex0
|
2023-09-02 19:56:50 +08:00
|
|
|
_builtinUniformLocation[Uniform::TEXTURE] = getUniformLocation(UNIFORM_NAME_TEXTURE);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
|
|
|
/// u_tex1
|
2023-09-02 19:56:50 +08:00
|
|
|
_builtinUniformLocation[Uniform::TEXTURE1] = getUniformLocation(UNIFORM_NAME_TEXTURE1);
|
2023-09-02 19:47:18 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
/// u_textColor
|
|
|
|
_builtinUniformLocation[Uniform::TEXT_COLOR] = getUniformLocation(UNIFORM_NAME_TEXT_COLOR);
|
|
|
|
|
|
|
|
/// u_effectColor
|
|
|
|
_builtinUniformLocation[Uniform::EFFECT_COLOR] = getUniformLocation(UNIFORM_NAME_EFFECT_COLOR);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
/// u_effectType
|
|
|
|
_builtinUniformLocation[Uniform::EFFECT_TYPE] = getUniformLocation(UNIFORM_NAME_EFFECT_TYPE);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2023-09-02 19:56:50 +08:00
|
|
|
std::string_view name{attrName.get(), static_cast<size_t>(attrNameLen)};
|
|
|
|
info.location = glGetAttribLocation(_program, name.data());
|
|
|
|
info.type = attrType;
|
|
|
|
info.size = UtilsGL::getGLDataTypeSize(attrType) * attrSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2023-09-02 19:56:50 +08:00
|
|
|
_activeAttribs[name] = 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-19 19:18:17 +08:00
|
|
|
_totalBufferSize = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_maxLocation = -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
_activeUniformInfos.clear();
|
2023-09-02 19:56:50 +08:00
|
|
|
|
|
|
|
yasio::basic_byte_buffer<GLchar> buffer; // buffer for name
|
|
|
|
|
|
|
|
// OpenGL UBO: uloc[0]: block_offset, uloc[1]: offset in block
|
|
|
|
|
|
|
|
auto gpuDevice = Device::getInstance();
|
|
|
|
/* Query uniform blocks */
|
|
|
|
clearUniformBuffers();
|
|
|
|
|
|
|
|
#if AX_GLES_PROFILE != 200
|
|
|
|
GLint numblocks{0};
|
|
|
|
glGetProgramiv(_program, GL_ACTIVE_UNIFORM_BLOCKS, &numblocks);
|
|
|
|
|
|
|
|
axstd::pod_vector<GLint> uniformBlcokOffsets(numblocks);
|
|
|
|
for (int blockIndex = 0; blockIndex < numblocks; ++blockIndex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-09-02 19:56:50 +08:00
|
|
|
GLint blockSize{0};
|
|
|
|
glGetActiveUniformBlockiv(_program, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
assert(blockSize > 0); // empty block not allow by GLSL/ESSL
|
|
|
|
|
|
|
|
GLint memberCount{0};
|
|
|
|
glGetActiveUniformBlockiv(_program, blockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &memberCount);
|
|
|
|
assert(memberCount > 0);
|
|
|
|
|
|
|
|
// set bindingIndex at CPU
|
|
|
|
glUniformBlockBinding(_program, blockIndex, blockIndex);
|
|
|
|
|
|
|
|
// create uniform buffer object
|
|
|
|
auto& desc = _uniformBuffers.emplace_back(
|
|
|
|
static_cast<BufferGL*>(gpuDevice->newBuffer(blockSize, BufferType::UNIFORM, BufferUsage::DYNAMIC)),
|
|
|
|
static_cast<int>(_totalBufferSize), blockSize);
|
|
|
|
desc._ubo->updateData(nullptr, blockSize); // ubo data can be nullptr
|
2023-08-13 23:56:58 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// increase _totalBufferSize
|
|
|
|
_totalBufferSize += blockSize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* construct _activeUniformInfos: uniformName-->UniformInfo
|
|
|
|
*/
|
|
|
|
UniformInfo uniform;
|
|
|
|
GLint nameLen = 0;
|
|
|
|
GLint numOfUniforms = 0;
|
|
|
|
glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &numOfUniforms);
|
|
|
|
for (GLint i = 0; i < numOfUniforms; ++i)
|
|
|
|
{
|
|
|
|
buffer.resize_fit(MAX_UNIFORM_NAME_LENGTH + 1);
|
|
|
|
glGetActiveUniform(_program, i, static_cast<GLint>(buffer.size()), &nameLen, &uniform.count, &uniform.type,
|
|
|
|
buffer.data());
|
|
|
|
|
|
|
|
uniform.size = UtilsGL::getGLDataTypeSize(uniform.type);
|
|
|
|
std::string_view uniformFullName{buffer.data(), static_cast<size_t>(nameLen)};
|
|
|
|
std::string_view uniformName{uniformFullName};
|
|
|
|
|
|
|
|
// Try trim uniform name
|
|
|
|
// trim name vs_ub.xxx[0] --> xxx
|
|
|
|
auto bracket = uniformName.find_last_of('[');
|
|
|
|
if (bracket != std::string_view::npos)
|
2023-08-13 23:58:18 +08:00
|
|
|
{
|
2023-09-02 19:56:50 +08:00
|
|
|
buffer[bracket] = '\0';
|
|
|
|
uniformName = uniformName.substr(0, bracket);
|
|
|
|
}
|
|
|
|
auto dot = uniformName.find_last_of('.');
|
|
|
|
if (dot != std::string::npos)
|
|
|
|
uniformName.remove_prefix(dot + 1); // trim uniformName
|
|
|
|
|
|
|
|
#if AX_GLES_PROFILE != 200
|
|
|
|
GLint blockIndex{-1};
|
|
|
|
glGetActiveUniformsiv(_program, 1, reinterpret_cast<const GLuint*>(&i), GL_UNIFORM_BLOCK_INDEX, &blockIndex);
|
|
|
|
if (blockIndex != -1)
|
|
|
|
{ // member of uniform block
|
|
|
|
auto& blockDesc = _uniformBuffers[blockIndex];
|
|
|
|
GLint uniformOffset{-1};
|
|
|
|
glGetActiveUniformsiv(_program, 1, reinterpret_cast<const GLuint*>(&i), GL_UNIFORM_OFFSET, &uniformOffset);
|
|
|
|
uniform.location = blockDesc._location;
|
|
|
|
uniform.bufferOffset = uniformOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // must be samper: sampler2D or samplerCube
|
|
|
|
assert(uniform.type == GL_SAMPLER_2D || uniform.type == GL_SAMPLER_CUBE);
|
|
|
|
uniform.location = glGetUniformLocation(_program, uniformName.data());
|
|
|
|
uniform.bufferOffset = -1;
|
2023-07-19 23:41:16 +08:00
|
|
|
}
|
2023-09-02 19:56:50 +08:00
|
|
|
#else
|
|
|
|
if (uniform.type == GL_SAMPLER_2D || uniform.type == GL_SAMPLER_CUBE)
|
|
|
|
{
|
|
|
|
uniform.location = glGetUniformLocation(_program, uniformName.data());
|
|
|
|
uniform.bufferOffset = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // GLES2.0: GLSL100
|
|
|
|
uniform.location = glGetUniformLocation(_program, uniformFullName.data());
|
|
|
|
uniform.bufferOffset = (uniform.size == 0) ? 0 : _totalBufferSize;
|
|
|
|
_totalBufferSize += uniform.size * uniform.count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-19 19:18:17 +08:00
|
|
|
_activeUniformInfos[uniformName] = uniform;
|
2023-09-02 19:56:50 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_maxLocation = _maxLocation <= uniform.location ? (uniform.location + 1) : _maxLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
void ProgramGL::bindUniformBuffers(const char* buffer, size_t bufferSize)
|
|
|
|
{
|
|
|
|
#if AX_GLES_PROFILE != 200
|
|
|
|
for (GLuint blockIdx = 0; blockIdx < static_cast<GLuint>(_uniformBuffers.size()); ++blockIdx)
|
|
|
|
{
|
|
|
|
auto& desc = _uniformBuffers[blockIdx];
|
|
|
|
desc._ubo->updateData(buffer + desc._location, desc._size);
|
|
|
|
__gl->bindUniformBufferBase(blockIdx, desc._ubo->getHandler());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
for (auto&& iter : _activeUniformInfos)
|
|
|
|
{
|
|
|
|
auto& uniformInfo = iter.second;
|
|
|
|
if (uniformInfo.size <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int elementCount = uniformInfo.count;
|
|
|
|
setUniform(uniformInfo.count > 1, uniformInfo.location, elementCount, uniformInfo.type,
|
|
|
|
(void*)(buffer + uniformInfo.bufferOffset));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramGL::clearUniformBuffers()
|
|
|
|
{
|
|
|
|
if (_uniformBuffers.empty())
|
|
|
|
return;
|
|
|
|
for (auto& desc : _uniformBuffers)
|
|
|
|
delete desc._ubo;
|
|
|
|
_uniformBuffers.clear();
|
|
|
|
}
|
|
|
|
|
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-09-02 19:56:50 +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;
|
|
|
|
}
|
|
|
|
return ""sv;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
UniformLocation ProgramGL::getUniformLocation(backend::Uniform name) const
|
|
|
|
{
|
2023-07-19 19:18:17 +08:00
|
|
|
return _builtinUniformLocation[name];
|
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-09-02 19:56:50 +08:00
|
|
|
auto iter = _activeUniformInfos.find(uniform);
|
|
|
|
if (iter != _activeUniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-09-02 19:56:50 +08:00
|
|
|
uniformLocation.shaderStage = ShaderStage::VERTEX;
|
|
|
|
|
|
|
|
const auto& uniformInfo = iter->second;
|
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-09-02 19:56:50 +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
|
|
|
|
|
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-19 19:18:17 +08:00
|
|
|
return _totalBufferSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|