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

367 lines
13 KiB
C++
Raw Normal View History

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-08-08 18:02:17 +08:00
https://axys1.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"
#include "renderer/backend/opengl/MacrosGL.h"
2019-11-23 20:27:39 +08:00
#include "base/CCDirector.h"
#include "base/CCEventDispatcher.h"
#include "base/CCEventType.h"
#include "renderer/backend/opengl/UtilsGL.h"
2022-07-07 09:30:48 +08:00
#include "yasio/detail/byte_buffer.hpp"
2019-11-23 20:27:39 +08:00
NS_AX_BACKEND_BEGIN
2021-12-25 10:04:45 +08:00
namespace
{
static const std::string SHADER_PREDEFINE = "#version 100\n precision highp float;\n precision highp int;\n";
2019-11-23 20:27:39 +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
{
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_GLES)
// some device required manually specify the precision qualifiers for vertex shader.
2021-12-25 10:04:45 +08:00
_vertexShaderModule =
static_cast<ShaderModuleGL*>(ShaderCache::newVertexShaderModule(SHADER_PREDEFINE + _vertexShader));
_fragmentShaderModule =
static_cast<ShaderModuleGL*>(ShaderCache::newFragmentShaderModule(SHADER_PREDEFINE + _fragmentShader));
2019-11-23 20:27:39 +08:00
#else
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));
#endif
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)
->compileShader(backend::ShaderStage::VERTEX, SHADER_PREDEFINE + _vertexShader);
static_cast<ShaderModuleGL*>(_fragmentShaderModule)
->compileShader(backend::ShaderStage::FRAGMENT, SHADER_PREDEFINE + _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);
2019-11-23 20:27:39 +08:00
glLinkProgram(_program);
2019-11-23 20:27:39 +08:00
GLint status = 0;
glGetProgramiv(_program, GL_LINK_STATUS, &status);
if (GL_FALSE == status)
{
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-07-07 09:30:48 +08:00
yasio::sbyte_buffer errorInfo{static_cast<size_t>(errorInfoLen), std::true_type{}};
glGetProgramInfoLog(_program, errorInfoLen, NULL, errorInfo.data());
log("cocos2d: ERROR: %s: failed to link program: %s ", __FUNCTION__, errorInfo.data());
}
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
auto location = glGetAttribLocation(_program, ATTRIBUTE_NAME_POSITION);
2019-11-23 20:27:39 +08:00
_builtinAttributeLocation[Attribute::POSITION] = location;
2021-12-25 10:04:45 +08:00
/// a_color
location = glGetAttribLocation(_program, ATTRIBUTE_NAME_COLOR);
2019-11-23 20:27:39 +08:00
_builtinAttributeLocation[Attribute::COLOR] = location;
2021-12-25 10:04:45 +08:00
/// a_texCoord
location = glGetAttribLocation(_program, ATTRIBUTE_NAME_TEXCOORD);
2019-11-23 20:27:39 +08:00
_builtinAttributeLocation[Attribute::TEXCOORD] = location;
2021-12-25 10:04:45 +08:00
/// u_MVPMatrix
location = glGetUniformLocation(_program, UNIFORM_NAME_MVP_MATRIX);
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
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXT_COLOR);
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
2019-11-23 20:27:39 +08:00
location = glGetUniformLocation(_program, UNIFORM_NAME_EFFECT_COLOR);
_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
2019-11-23 20:27:39 +08:00
location = glGetUniformLocation(_program, UNIFORM_NAME_EFFECT_TYPE);
_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
/// u_tex0
2021-12-25 10:04:45 +08:00
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXTURE);
2019-11-23 20:27:39 +08:00
_builtinUniformLocation[Uniform::TEXTURE].location[0] = location;
/// u_tex1
2021-12-25 10:04:45 +08:00
location = glGetUniformLocation(_program, UNIFORM_NAME_TEXTURE1);
2019-11-23 20:27:39 +08:00
_builtinUniformLocation[Uniform::TEXTURE1].location[0] = location;
}
bool ProgramGL::getAttributeLocation(std::string_view attributeName, unsigned int& location) const
2019-11-23 20:27:39 +08:00
{
GLint loc = glGetAttribLocation(_program, attributeName.data());
2019-11-23 20:27:39 +08:00
if (-1 == loc)
{
2022-09-01 19:51:19 +08:00
AXLOG("axys: %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;
}
const hlookup::string_map<AttributeBindInfo> ProgramGL::getActiveAttributes() const
2019-11-23 20:27:39 +08:00
{
hlookup::string_map<AttributeBindInfo> attributes;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (!_program)
return attributes;
2019-11-23 20:27:39 +08:00
GLint numOfActiveAttributes = 0;
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTES, &numOfActiveAttributes);
if (numOfActiveAttributes <= 0)
return attributes;
attributes.reserve(numOfActiveAttributes);
int MAX_ATTRIBUTE_NAME_LENGTH = 256;
std::vector<char> attrName(MAX_ATTRIBUTE_NAME_LENGTH + 1);
GLint attrNameLen = 0;
GLenum attrType;
GLint attrSize;
backend::AttributeBindInfo info;
for (int i = 0; i < numOfActiveAttributes; i++)
{
glGetActiveAttrib(_program, i, MAX_ATTRIBUTE_NAME_LENGTH, &attrNameLen, &attrSize, &attrType, attrName.data());
CHECK_GL_ERROR_DEBUG();
info.attributeName = std::string(attrName.data(), attrName.data() + 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();
attributes[info.attributeName] = info;
}
return attributes;
}
void ProgramGL::computeUniformInfos()
{
if (!_program)
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
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
#define MAX_UNIFORM_NAME_LENGTH 256
UniformInfo uniform;
2021-12-25 10:04:45 +08:00
GLint length = 0;
2019-11-23 20:27:39 +08:00
_totalBufferSize = 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)
{
glGetActiveUniform(_program, i, MAX_UNIFORM_NAME_LENGTH, &length, &uniform.count, &uniform.type, uniformName);
uniformName[length] = '\0';
2021-12-25 10:04:45 +08:00
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);
uniform.bufferOffset = (uniform.size == 0) ? 0 : _totalBufferSize;
2019-11-23 20:27:39 +08:00
_activeUniformInfos[uniformName] = uniform;
_totalBufferSize += uniform.size * uniform.count;
_maxLocation = _maxLocation <= uniform.location ? (uniform.location + 1) : _maxLocation;
}
}
int ProgramGL::getAttributeLocation(Attribute name) const
{
return _builtinAttributeLocation[name];
}
int ProgramGL::getAttributeLocation(std::string_view name) const
2019-11-23 20:27:39 +08:00
{
return glGetAttribLocation(_program, name.data());
2019-11-23 20:27:39 +08:00
}
UniformLocation ProgramGL::getUniformLocation(backend::Uniform name) const
{
2021-12-25 10:04:45 +08:00
return _builtinUniformLocation[name];
2019-11-23 20:27:39 +08:00
}
UniformLocation ProgramGL::getUniformLocation(std::string_view uniform) const
2019-11-23 20:27:39 +08:00
{
UniformLocation uniformLocation;
if (_activeUniformInfos.find(uniform) != _activeUniformInfos.end())
{
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;
}
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
}
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
{
return _totalBufferSize;
}
NS_AX_BACKEND_END