axmol/core/renderer/backend/ProgramState.cpp

512 lines
16 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
2023-02-09 21:40:38 +08:00
Copyright (c) 2021-2023 Bytedance Inc.
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 "renderer/backend/ProgramState.h"
#include "renderer/backend/Program.h"
#include "renderer/backend/Texture.h"
#include "renderer/backend/Types.h"
#include "base/EventDispatcher.h"
#include "base/EventType.h"
#include "base/Director.h"
2019-11-23 20:27:39 +08:00
#include <algorithm>
#include "glslcc/sgs-spec.h"
2019-11-23 20:27:39 +08:00
NS_AX_BACKEND_BEGIN
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
#define MAT3_SIZE 36
2021-12-25 10:04:45 +08:00
void convertMat3ToMat4x3(const float* src, float* dst)
{
dst[3] = dst[7] = dst[11] = 0.0f;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[4] = src[3];
dst[5] = src[4];
dst[6] = src[5];
dst[8] = src[6];
dst[9] = src[7];
dst[10] = src[8];
}
} // namespace
// static field
2019-11-23 20:27:39 +08:00
std::vector<ProgramState::AutoBindingResolver*> ProgramState::_customAutoBindingResolvers;
2021-09-30 08:16:33 +08:00
TextureInfo::TextureInfo(std::vector<int>&& _slots, std::vector<backend::TextureBackend*>&& _textures)
: TextureInfo(std::move(_slots), std::vector<int>(_slots.size(), 0), std::move(_textures))
2021-12-25 10:04:45 +08:00
{}
2020-08-29 16:56:48 +08:00
2021-09-30 08:16:33 +08:00
TextureInfo::TextureInfo(std::vector<int>&& _slots,
std::vector<int>&& _indexs,
std::vector<backend::TextureBackend*>&& _textures)
2021-12-25 10:04:45 +08:00
: slots(std::move(_slots)), indexs(std::move(_indexs)), textures(std::move(_textures))
2019-11-23 20:27:39 +08:00
{
retainTextures();
}
2020-08-29 16:56:48 +08:00
/* CLASS TextureInfo */
2021-12-25 10:04:45 +08:00
TextureInfo::TextureInfo(const TextureInfo& other)
2019-11-23 20:27:39 +08:00
{
2020-08-29 16:56:48 +08:00
this->assign(other);
}
TextureInfo::TextureInfo(TextureInfo&& other)
{
this->assign(std::move(other));
2019-11-23 20:27:39 +08:00
}
TextureInfo::~TextureInfo()
{
releaseTextures();
}
void TextureInfo::retainTextures()
{
for (auto&& texture : textures)
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(texture);
2019-11-23 20:27:39 +08:00
}
void TextureInfo::releaseTextures()
{
for (auto&& texture : textures)
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(texture);
2020-08-29 22:00:58 +08:00
textures.clear();
2019-11-23 20:27:39 +08:00
}
2020-08-29 16:56:48 +08:00
TextureInfo& TextureInfo::operator=(const TextureInfo& other) noexcept
{
this->assign(other);
return *this;
}
TextureInfo& TextureInfo::operator=(TextureInfo&& other) noexcept
2019-11-23 20:27:39 +08:00
{
2020-08-29 16:56:48 +08:00
this->assign(std::move(other));
return *this;
}
void TextureInfo::assign(const TextureInfo& other)
{
if (this != &other)
2019-11-23 20:27:39 +08:00
{
2020-08-29 20:07:13 +08:00
releaseTextures();
2021-12-25 10:04:45 +08:00
indexs = other.indexs;
slots = other.slots;
2020-08-29 16:56:48 +08:00
textures = other.textures;
retainTextures();
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2020-08-29 16:56:48 +08:00
location = other.location;
2019-11-23 20:27:39 +08:00
#endif
}
}
2020-08-29 16:56:48 +08:00
void TextureInfo::assign(TextureInfo&& other)
2019-11-23 20:27:39 +08:00
{
2020-08-29 16:56:48 +08:00
if (this != &other)
2019-11-23 20:27:39 +08:00
{
2020-08-29 16:56:48 +08:00
releaseTextures();
2021-12-25 10:04:45 +08:00
indexs = std::move(other.indexs);
slots = std::move(other.slots);
2020-08-29 16:56:48 +08:00
textures = std::move(other.textures);
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
location = other.location;
2020-08-29 16:56:48 +08:00
other.location = -1;
2019-11-23 20:27:39 +08:00
#endif
}
}
2020-08-29 16:56:48 +08:00
/* CLASS ProgramState */
2019-11-23 20:27:39 +08:00
ProgramState::ProgramState(Program* program)
{
init(program);
}
bool ProgramState::init(Program* program)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(program);
2021-12-25 10:04:45 +08:00
_program = program;
2022-09-23 22:41:30 +08:00
_vertexLayout = program->getVertexLayout();
_ownVertexLayout = false;
2019-11-23 20:27:39 +08:00
_vertexUniformBufferSize = _program->getUniformBufferSize(ShaderStage::VERTEX);
2021-12-25 10:04:45 +08:00
_vertexUniformBuffer = (char*)calloc(1, _vertexUniformBufferSize);
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2019-11-23 20:27:39 +08:00
_fragmentUniformBufferSize = _program->getUniformBufferSize(ShaderStage::FRAGMENT);
2021-12-25 10:04:45 +08:00
_fragmentUniformBuffer = (char*)calloc(1, _fragmentUniformBufferSize);
2020-10-23 11:37:52 +08:00
#endif
2019-11-23 20:27:39 +08:00
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->resetUniforms(); });
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
updateBatchId();
2019-11-23 20:27:39 +08:00
return true;
}
void ProgramState::updateBatchId()
{
_batchId = _program->isBatchEnabled() ? _program->getProgramId()
: static_cast<uint64_t>(reinterpret_cast<uintptr_t>(this));
}
2019-11-23 20:27:39 +08:00
void ProgramState::resetUniforms()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
if (_program == nullptr)
2019-11-23 20:27:39 +08:00
return;
const auto& uniformLocation = _program->getAllUniformsLocation();
2021-12-25 10:04:45 +08:00
for (const auto& uniform : uniformLocation)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto location = uniform.second;
2019-11-23 20:27:39 +08:00
auto mappedLocation = _program->getMappedLocation(location);
2021-12-25 10:04:45 +08:00
// check if current location had been set before
if (_vertexTextureInfos.find(location) != _vertexTextureInfos.end())
2019-11-23 20:27:39 +08:00
{
_vertexTextureInfos[location].location = mappedLocation;
}
}
#endif
}
ProgramState::~ProgramState()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_program);
AX_SAFE_FREE(_vertexUniformBuffer);
AX_SAFE_FREE(_fragmentUniformBuffer);
2021-12-25 10:04:45 +08:00
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-09-23 22:41:30 +08:00
if (_ownVertexLayout)
2022-09-23 23:25:01 +08:00
AX_SAFE_DELETE(_vertexLayout);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
ProgramState* ProgramState::clone() const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ProgramState* cp = new ProgramState(_program);
cp->_vertexTextureInfos = _vertexTextureInfos;
2019-11-23 20:27:39 +08:00
cp->_fragmentTextureInfos = _fragmentTextureInfos;
memcpy(cp->_vertexUniformBuffer, _vertexUniformBuffer, _vertexUniformBufferSize);
2022-09-23 22:41:30 +08:00
cp->_ownVertexLayout = _ownVertexLayout;
cp->_vertexLayout = !_ownVertexLayout ? _vertexLayout : new VertexLayout(*_vertexLayout);
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2019-11-23 20:27:39 +08:00
memcpy(cp->_fragmentUniformBuffer, _fragmentUniformBuffer, _fragmentUniformBufferSize);
#endif
cp->_batchId = this->_batchId;
2019-11-23 20:27:39 +08:00
return cp;
}
backend::UniformLocation ProgramState::getUniformLocation(backend::Uniform name) const
{
return _program->getUniformLocation(name);
}
backend::UniformLocation ProgramState::getUniformLocation(std::string_view uniform) const
2019-11-23 20:27:39 +08:00
{
return _program->getUniformLocation(uniform);
}
2021-12-25 10:04:45 +08:00
void ProgramState::setCallbackUniform(const backend::UniformLocation& uniformLocation, const UniformCallback& callback)
2019-11-23 20:27:39 +08:00
{
_callbackUniforms[uniformLocation] = callback;
2019-11-23 20:27:39 +08:00
}
void ProgramState::setUniform(const backend::UniformLocation& uniformLocation, const void* data, std::size_t size)
{
switch (uniformLocation.shaderStage)
{
2021-12-25 10:04:45 +08:00
case backend::ShaderStage::VERTEX:
setVertexUniform(uniformLocation.location[0], data, size, uniformLocation.location[1]);
break;
case backend::ShaderStage::FRAGMENT:
setFragmentUniform(uniformLocation.location[1], data, size);
break;
case backend::ShaderStage::VERTEX_AND_FRAGMENT:
setVertexUniform(uniformLocation.location[0], data, size, uniformLocation.location[1]);
setFragmentUniform(uniformLocation.location[1], data, size);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2021-12-25 10:04:45 +08:00
void ProgramState::convertAndCopyUniformData(const backend::UniformInfo& uniformInfo,
const void* srcData,
std::size_t srcSize,
void* buffer)
2019-11-23 20:27:39 +08:00
{
2023-07-27 20:37:06 +08:00
assert(uniformInfo.type == SGS_VERTEXFORMAT_MAT3);
2019-11-23 20:27:39 +08:00
int offset = 0;
2023-07-27 20:37:06 +08:00
float m4x3[12];
for (int i = 0; i < uniformInfo.count; i++)
2019-11-23 20:27:39 +08:00
{
2023-07-27 20:37:06 +08:00
if (offset >= srcSize)
break;
2021-12-25 10:04:45 +08:00
2023-07-27 20:37:06 +08:00
convertMat3ToMat4x3((float*)((uint8_t*)srcData + offset), m4x3);
memcpy((uint8_t*)buffer + uniformInfo.location + i * sizeof(m4x3), m4x3, sizeof(m4x3));
offset += MAT3_SIZE;
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
}
#endif
void ProgramState::setVertexUniform(int location, const void* data, std::size_t size, std::size_t offset)
{
2021-12-25 10:04:45 +08:00
if (location < 0)
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
// float3 etc in Metal has both sizeof and alignment same as float4, need convert to correct laytout
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2019-11-23 20:27:39 +08:00
const auto& uniformInfo = _program->getActiveUniformInfo(ShaderStage::VERTEX, location);
2023-07-27 20:37:06 +08:00
if (uniformInfo.type == SGS_VERTEXFORMAT_MAT3)
2019-11-23 20:27:39 +08:00
{
convertAndCopyUniformData(uniformInfo, data, size, _vertexUniformBuffer);
}
else
{
memcpy(_vertexUniformBuffer + location, data, size);
}
#else
assert(location + offset + size <= _vertexUniformBufferSize);
memcpy(_vertexUniformBuffer + location + offset, data, size);
2019-11-23 20:27:39 +08:00
#endif
}
void ProgramState::setFragmentUniform(int location, const void* data, std::size_t size)
{
2021-12-25 10:04:45 +08:00
if (location < 0)
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
// float3 etc in Metal has both sizeof and alignment same as float4, need convert to correct laytout
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2019-11-23 20:27:39 +08:00
const auto& uniformInfo = _program->getActiveUniformInfo(ShaderStage::FRAGMENT, location);
2023-07-27 20:37:06 +08:00
if (uniformInfo.type == SGS_VERTEXFORMAT_MAT3)
2019-11-23 20:27:39 +08:00
{
convertAndCopyUniformData(uniformInfo, data, size, _fragmentUniformBuffer);
}
else
{
memcpy(_fragmentUniformBuffer + location, data, size);
}
#else
assert(false);
2020-10-23 11:37:52 +08:00
#endif
}
2022-09-23 22:41:30 +08:00
void ProgramState::setVertexAttrib(std::string_view name,
std::size_t index,
VertexFormat format,
std::size_t offset,
bool needToBeNormallized)
2022-09-23 22:41:30 +08:00
{
ensureVertexLayoutMutable();
_vertexLayout->setAttrib(name, index, format, offset, needToBeNormallized);
2022-09-23 22:41:30 +08:00
}
void ProgramState::setVertexStride(uint32_t stride)
{
ensureVertexLayoutMutable();
_vertexLayout->setStride(stride);
}
void ProgramState::validateSharedVertexLayout(VertexLayoutType vlt)
{
if (!_ownVertexLayout && !_vertexLayout->isValid())
_program->setupVertexLayout(vlt);
}
2022-09-23 22:41:30 +08:00
void ProgramState::ensureVertexLayoutMutable()
{
if (!_ownVertexLayout)
{
_vertexLayout = new VertexLayout();
2022-09-23 22:41:30 +08:00
_ownVertexLayout = true;
}
}
VertexLayout* ProgramState::getMutableVertexLayout()
2020-10-23 11:37:52 +08:00
{
if (_ownVertexLayout || !_vertexLayout->isValid())
return _vertexLayout;
_ownVertexLayout = true;
return _vertexLayout = new VertexLayout();
}
void ProgramState::setSharedVertexLayout(VertexLayout* vertexLayout)
{
if (_ownVertexLayout)
delete _vertexLayout;
_ownVertexLayout = false;
_vertexLayout = vertexLayout;
2019-11-23 20:27:39 +08:00
}
void ProgramState::setTexture(backend::TextureBackend* texture)
{
2022-07-16 10:43:05 +08:00
for (int slot = 0; slot < texture->getCount() && slot < AX_META_TEXTURES; ++slot)
2021-12-25 10:04:45 +08:00
{
2020-08-29 16:56:48 +08:00
auto location = getUniformLocation((backend::Uniform)(backend::Uniform::TEXTURE + slot));
setTexture(location, slot, slot, texture);
2019-11-23 20:27:39 +08:00
}
}
2021-09-30 08:16:33 +08:00
void ProgramState::setTexture(const backend::UniformLocation& uniformLocation,
int slot,
backend::TextureBackend* texture)
2020-08-29 16:56:48 +08:00
{
setTexture(uniformLocation, slot, 0, texture);
}
2021-09-30 08:16:33 +08:00
void ProgramState::setTexture(const backend::UniformLocation& uniformLocation,
int slot,
int index,
backend::TextureBackend* texture)
2019-11-23 20:27:39 +08:00
{
switch (uniformLocation.shaderStage)
{
2021-12-25 10:04:45 +08:00
case backend::ShaderStage::VERTEX:
setTexture(uniformLocation.location[0], slot, index, texture, _vertexTextureInfos);
break;
case backend::ShaderStage::FRAGMENT:
setTexture(uniformLocation.location[1], slot, index, texture, _fragmentTextureInfos);
break;
case backend::ShaderStage::VERTEX_AND_FRAGMENT:
setTexture(uniformLocation.location[0], slot, index, texture, _vertexTextureInfos);
setTexture(uniformLocation.location[1], slot, index, texture, _fragmentTextureInfos);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2021-09-30 08:16:33 +08:00
void ProgramState::setTextureArray(const backend::UniformLocation& uniformLocation,
std::vector<int> slots,
std::vector<backend::TextureBackend*> textures)
2019-11-23 20:27:39 +08:00
{
switch (uniformLocation.shaderStage)
{
2021-12-25 10:04:45 +08:00
case backend::ShaderStage::VERTEX:
setTextureArray(uniformLocation.location[0], std::move(slots), std::move(textures), _vertexTextureInfos);
break;
case backend::ShaderStage::FRAGMENT:
setTextureArray(uniformLocation.location[1], std::move(slots), std::move(textures), _fragmentTextureInfos);
break;
case backend::ShaderStage::VERTEX_AND_FRAGMENT:
setTextureArray(uniformLocation.location[0], std::move(slots), std::move(textures), _vertexTextureInfos);
setTextureArray(uniformLocation.location[1], std::move(slots), std::move(textures), _fragmentTextureInfos);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2021-09-30 08:16:33 +08:00
void ProgramState::setTexture(int location,
int slot,
int index,
backend::TextureBackend* texture,
std::unordered_map<int, TextureInfo>& textureInfo)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (location < 0)
2019-11-23 20:27:39 +08:00
return;
2020-08-29 16:56:48 +08:00
auto& info = textureInfo[location];
2021-12-25 10:04:45 +08:00
info = {{slot}, {index}, {texture}};
2020-08-29 16:56:48 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
info.location = location;
#endif
}
2021-09-30 08:16:33 +08:00
void ProgramState::setTextureArray(int location,
std::vector<int> slots,
std::vector<backend::TextureBackend*> textures,
std::unordered_map<int, TextureInfo>& textureInfo)
2019-11-23 20:27:39 +08:00
{
assert(slots.size() == textures.size());
2020-08-29 16:56:48 +08:00
auto& info = textureInfo[location];
2021-12-25 10:04:45 +08:00
info = {std::move(slots), std::move(textures)};
2020-08-29 16:56:48 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
info.location = location;
#endif
}
void ProgramState::setParameterAutoBinding(std::string_view uniform, std::string_view autoBinding)
2019-11-23 20:27:39 +08:00
{
_autoBindings.emplace(uniform, autoBinding);
applyAutoBinding(uniform, autoBinding);
}
void ProgramState::applyAutoBinding(std::string_view uniformName, std::string_view autoBinding)
2019-11-23 20:27:39 +08:00
{
for (const auto resolver : _customAutoBindingResolvers)
{
2021-10-11 12:15:41 +08:00
if (resolver->resolveAutoBinding(this, uniformName, autoBinding))
break;
2019-11-23 20:27:39 +08:00
}
}
ProgramState::AutoBindingResolver::AutoBindingResolver()
{
_customAutoBindingResolvers.emplace_back(this);
}
ProgramState::AutoBindingResolver::~AutoBindingResolver()
{
2021-12-25 10:04:45 +08:00
auto& list = _customAutoBindingResolvers;
2019-11-23 20:27:39 +08:00
list.erase(std::remove(list.begin(), list.end(), this), list.end());
}
void ProgramState::getVertexUniformBuffer(char** buffer, std::size_t& size) const
{
*buffer = _vertexUniformBuffer;
2021-12-25 10:04:45 +08:00
size = _vertexUniformBufferSize;
2019-11-23 20:27:39 +08:00
}
void ProgramState::getFragmentUniformBuffer(char** buffer, std::size_t& size) const
{
*buffer = _fragmentUniformBuffer;
2021-12-25 10:04:45 +08:00
size = _fragmentUniformBufferSize;
2019-11-23 20:27:39 +08:00
}
NS_AX_BACKEND_END