2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
2020-08-29 20:07:13 +08:00
|
|
|
Copyright (c) 2020 c4games.com
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "renderer/backend/ProgramState.h"
|
|
|
|
#include "renderer/backend/ProgramCache.h"
|
|
|
|
#include "renderer/backend/Program.h"
|
|
|
|
#include "renderer/backend/Texture.h"
|
|
|
|
#include "renderer/backend/Types.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
2020-11-14 12:22:51 +08:00
|
|
|
#include "xxhash.h"
|
2020-10-23 16:40:51 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
#include "glsl_optimizer.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
#define MAT3_SIZE 36
|
|
|
|
#define MAT4X3_SIZE 48
|
|
|
|
#define VEC3_SIZE 12
|
|
|
|
#define VEC4_SIZE 16
|
|
|
|
#define BVEC3_SIZE 3
|
|
|
|
#define BVEC4_SIZE 4
|
|
|
|
#define IVEC3_SIZE 12
|
|
|
|
#define IVEC4_SIZE 16
|
|
|
|
|
|
|
|
void convertbVec3TobVec4(const bool* src, bool* dst)
|
|
|
|
{
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
dst[2] = src[2];
|
|
|
|
dst[3] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertiVec3ToiVec4(const int* src, int* dst)
|
|
|
|
{
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
dst[2] = src[2];
|
|
|
|
dst[3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertVec3ToVec4(const float* src, float* dst)
|
|
|
|
{
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
dst[2] = src[2];
|
|
|
|
dst[3] = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//static field
|
|
|
|
std::vector<ProgramState::AutoBindingResolver*> ProgramState::_customAutoBindingResolvers;
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
TextureInfo::TextureInfo(std::vector<uint16_t>&& _slots, std::vector<backend::TextureBackend*>&& _textures)
|
|
|
|
: TextureInfo(std::move(_slots), std::vector<uint16_t>(_slots.size(), 0), std::move(_textures))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureInfo::TextureInfo(std::vector<uint16_t>&& _slots, std::vector<uint16_t>&& _indexs, std::vector<backend::TextureBackend*>&& _textures)
|
|
|
|
: 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 */
|
2019-11-23 20:27:39 +08:00
|
|
|
TextureInfo::TextureInfo(const TextureInfo &other)
|
|
|
|
{
|
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)
|
|
|
|
CC_SAFE_RETAIN(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureInfo::releaseTextures()
|
|
|
|
{
|
|
|
|
for (auto& texture : textures)
|
|
|
|
CC_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();
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
indexs = other.indexs;
|
|
|
|
slots = other.slots;
|
|
|
|
textures = other.textures;
|
|
|
|
retainTextures();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#if CC_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();
|
|
|
|
|
|
|
|
indexs = std::move(other.indexs);
|
|
|
|
slots = std::move(other.slots);
|
|
|
|
textures = std::move(other.textures);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2020-08-29 16:56:48 +08:00
|
|
|
location = other.location;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(program);
|
|
|
|
_program = program;
|
|
|
|
_vertexUniformBufferSize = _program->getUniformBufferSize(ShaderStage::VERTEX);
|
2020-10-23 11:37:52 +08:00
|
|
|
_vertexUniformBuffer = (char*)calloc(1, _vertexUniformBufferSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
_fragmentUniformBufferSize = _program->getUniformBufferSize(ShaderStage::FRAGMENT);
|
2020-10-23 11:37:52 +08:00
|
|
|
_fragmentUniformBuffer = (char*)calloc(1, _fragmentUniformBufferSize);
|
|
|
|
#endif
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
_uniformHashState = XXH32_createState();
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*){
|
|
|
|
this->resetUniforms();
|
|
|
|
});
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::resetUniforms()
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
if(_program == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& uniformLocation = _program->getAllUniformsLocation();
|
|
|
|
for(const auto& uniform : uniformLocation)
|
|
|
|
{
|
|
|
|
auto location = uniform.second;
|
|
|
|
auto mappedLocation = _program->getMappedLocation(location);
|
|
|
|
|
|
|
|
//check if current location had been set before
|
|
|
|
if(_vertexTextureInfos.find(location) != _vertexTextureInfos.end())
|
|
|
|
{
|
|
|
|
_vertexTextureInfos[location].location = mappedLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramState::~ProgramState()
|
|
|
|
{
|
2020-10-23 11:37:52 +08:00
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
XXH32_freeState(_uniformHashState);
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_SAFE_RELEASE(_program);
|
2020-10-23 11:37:52 +08:00
|
|
|
CC_SAFE_FREE(_vertexUniformBuffer);
|
|
|
|
CC_SAFE_FREE(_fragmentUniformBuffer);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramState *ProgramState::clone() const
|
|
|
|
{
|
2020-10-23 11:37:52 +08:00
|
|
|
ProgramState *cp = new ProgramState(_program);
|
2019-11-23 20:27:39 +08:00
|
|
|
cp->_vertexTextureInfos = _vertexTextureInfos;
|
|
|
|
cp->_fragmentTextureInfos = _fragmentTextureInfos;
|
|
|
|
memcpy(cp->_vertexUniformBuffer, _vertexUniformBuffer, _vertexUniformBufferSize);
|
|
|
|
cp->_vertexLayout = _vertexLayout;
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
memcpy(cp->_fragmentUniformBuffer, _fragmentUniformBuffer, _fragmentUniformBufferSize);
|
|
|
|
#endif
|
2020-10-23 11:37:52 +08:00
|
|
|
cp->_uniformID = _uniformID;
|
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(const std::string& uniform) const
|
|
|
|
{
|
|
|
|
return _program->getUniformLocation(uniform);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::setCallbackUniform(const backend::UniformLocation& uniformLocation,const UniformCallback& callback)
|
|
|
|
{
|
2020-10-23 15:07:18 +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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
void ProgramState::convertAndCopyUniformData(const backend::UniformInfo& uniformInfo, const void* srcData, std::size_t srcSize, void* buffer)
|
|
|
|
{
|
|
|
|
auto basicType = static_cast<glslopt_basic_type>(uniformInfo.type);
|
|
|
|
char* convertedData = new char[uniformInfo.size];
|
|
|
|
memset(convertedData, 0, uniformInfo.size);
|
|
|
|
int offset = 0;
|
|
|
|
switch (basicType)
|
|
|
|
{
|
|
|
|
case kGlslTypeFloat:
|
|
|
|
{
|
|
|
|
if(uniformInfo.isMatrix)
|
|
|
|
{
|
|
|
|
for (int i=0; i<uniformInfo.count; i++)
|
|
|
|
{
|
|
|
|
if(offset >= srcSize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
convertMat3ToMat4x3((float*)srcData + offset, (float*)convertedData + i * MAT4X3_SIZE);
|
|
|
|
offset += MAT3_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i=0; i<uniformInfo.count; i++)
|
|
|
|
{
|
|
|
|
if(offset >= srcSize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
convertVec3ToVec4((float*)srcData +offset, (float*)convertedData + i * VEC4_SIZE);
|
|
|
|
offset += VEC3_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kGlslTypeBool:
|
|
|
|
{
|
|
|
|
for (int i=0; i<uniformInfo.count; i++)
|
|
|
|
{
|
|
|
|
if(offset >= srcSize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
convertbVec3TobVec4((bool*)srcData + offset, (bool*)convertedData + i * BVEC4_SIZE);
|
|
|
|
offset += BVEC3_SIZE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kGlslTypeInt:
|
|
|
|
{
|
|
|
|
for (int i=0; i<uniformInfo.count; i++)
|
|
|
|
{
|
|
|
|
if(offset >= srcSize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
convertiVec3ToiVec4((int*)srcData + offset, (int*)convertedData + i * IVEC4_SIZE);
|
|
|
|
offset += IVEC3_SIZE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
CC_ASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((char*)buffer + uniformInfo.location, convertedData, uniformInfo.size);
|
|
|
|
CC_SAFE_DELETE_ARRAY(convertedData);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ProgramState::setVertexUniform(int location, const void* data, std::size_t size, std::size_t offset)
|
|
|
|
{
|
|
|
|
if(location < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//float3 etc in Metal has both sizeof and alignment same as float4, need convert to correct laytout
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
const auto& uniformInfo = _program->getActiveUniformInfo(ShaderStage::VERTEX, location);
|
|
|
|
if(uniformInfo.needConvert)
|
|
|
|
{
|
|
|
|
convertAndCopyUniformData(uniformInfo, data, size, _vertexUniformBuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(_vertexUniformBuffer + location, data, size);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
memcpy(_vertexUniformBuffer + offset, data, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::setFragmentUniform(int location, const void* data, std::size_t size)
|
|
|
|
{
|
|
|
|
if(location < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//float3 etc in Metal has both sizeof and alignment same as float4, need convert to correct laytout
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
const auto& uniformInfo = _program->getActiveUniformInfo(ShaderStage::FRAGMENT, location);
|
|
|
|
if(uniformInfo.needConvert)
|
|
|
|
{
|
|
|
|
convertAndCopyUniformData(uniformInfo, data, size, _fragmentUniformBuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(_fragmentUniformBuffer + location, data, size);
|
|
|
|
}
|
2020-10-23 11:37:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-23 15:27:03 +08:00
|
|
|
void ProgramState::updateUniformID(int uniformID)
|
2020-10-23 11:37:52 +08:00
|
|
|
{
|
2020-10-23 15:27:03 +08:00
|
|
|
if (uniformID == -1) {
|
2020-10-23 11:37:52 +08:00
|
|
|
#ifdef CC_USE_METAL
|
2020-10-23 15:27:03 +08:00
|
|
|
XXH32_reset(_uniformHashState, 0);
|
|
|
|
XXH32_update(_uniformHashState, _vertexUniformBuffer, _vertexUniformBufferSize);
|
|
|
|
XXH32_update(_uniformHashState, _fragmentUniformBuffer, _fragmentUniformBufferSize);
|
|
|
|
_uniformID = XXH32_digest(_uniformHashState);
|
2020-10-23 11:37:52 +08:00
|
|
|
#else
|
2020-10-23 15:27:03 +08:00
|
|
|
_uniformID = XXH32(_vertexUniformBuffer, _vertexUniformBufferSize, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
2020-10-23 15:27:03 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
_uniformID = uniformID;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::setTexture(backend::TextureBackend* texture)
|
|
|
|
{
|
2020-08-29 16:56:48 +08:00
|
|
|
for (int slot = 0; slot < texture->getCount() && slot < CC_META_TEXTURES; ++slot) {
|
|
|
|
auto location = getUniformLocation((backend::Uniform)(backend::Uniform::TEXTURE + slot));
|
|
|
|
setTexture(location, slot, slot, texture);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
void ProgramState::setTexture(const backend::UniformLocation& uniformLocation, uint16_t slot, backend::TextureBackend* texture)
|
|
|
|
{
|
|
|
|
setTexture(uniformLocation, slot, 0, texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::setTexture(const backend::UniformLocation& uniformLocation, uint16_t slot, uint16_t index, backend::TextureBackend* texture)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (uniformLocation.shaderStage)
|
|
|
|
{
|
|
|
|
case backend::ShaderStage::VERTEX:
|
2020-08-29 16:56:48 +08:00
|
|
|
setTexture(uniformLocation.location[0], slot, index, texture, _vertexTextureInfos);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
case backend::ShaderStage::FRAGMENT:
|
2020-08-29 16:56:48 +08:00
|
|
|
setTexture(uniformLocation.location[1], slot, index, texture, _fragmentTextureInfos);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
case backend::ShaderStage::VERTEX_AND_FRAGMENT:
|
2020-08-29 16:56:48 +08:00
|
|
|
setTexture(uniformLocation.location[0], slot, index, texture, _vertexTextureInfos);
|
|
|
|
setTexture(uniformLocation.location[1], slot, index, texture, _fragmentTextureInfos);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
void ProgramState::setTextureArray(const backend::UniformLocation& uniformLocation, std::vector<uint16_t> slots, std::vector<backend::TextureBackend*> textures)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (uniformLocation.shaderStage)
|
|
|
|
{
|
|
|
|
case backend::ShaderStage::VERTEX:
|
2020-08-29 16:56:48 +08:00
|
|
|
setTextureArray(uniformLocation.location[0], std::move(slots), std::move(textures), _vertexTextureInfos);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
case backend::ShaderStage::FRAGMENT:
|
2020-08-29 16:56:48 +08:00
|
|
|
setTextureArray(uniformLocation.location[1], std::move(slots), std::move(textures), _fragmentTextureInfos);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
case backend::ShaderStage::VERTEX_AND_FRAGMENT:
|
2020-08-29 16:56:48 +08:00
|
|
|
setTextureArray(uniformLocation.location[0], std::move(slots), std::move(textures), _vertexTextureInfos);
|
|
|
|
setTextureArray(uniformLocation.location[1], std::move(slots), std::move(textures), _fragmentTextureInfos);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
void ProgramState::setTexture(int location, uint16_t slot, uint16_t index, backend::TextureBackend* texture, std::unordered_map<int, TextureInfo>& textureInfo)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if(location < 0)
|
|
|
|
return;
|
2020-08-29 16:56:48 +08:00
|
|
|
|
|
|
|
auto& info = textureInfo[location];
|
|
|
|
info = { {slot}, {index}, {texture} };
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
info.location = location;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
void ProgramState::setTextureArray(int location, std::vector<uint16_t> 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];
|
|
|
|
info = { std::move(slots), std::move(textures) };
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
info.location = location;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::setParameterAutoBinding(const std::string &uniform, const std::string &autoBinding)
|
|
|
|
{
|
|
|
|
_autoBindings.emplace(uniform, autoBinding);
|
|
|
|
applyAutoBinding(uniform, autoBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::applyAutoBinding(const std::string &uniformName, const std::string &autoBinding)
|
|
|
|
{
|
|
|
|
bool resolved = false;
|
|
|
|
for (const auto resolver : _customAutoBindingResolvers)
|
|
|
|
{
|
|
|
|
resolved = resolver->resolveAutoBinding(this, uniformName, autoBinding);
|
|
|
|
if (resolved) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramState::AutoBindingResolver::AutoBindingResolver()
|
|
|
|
{
|
|
|
|
_customAutoBindingResolvers.emplace_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramState::AutoBindingResolver::~AutoBindingResolver()
|
|
|
|
{
|
|
|
|
auto &list = _customAutoBindingResolvers;
|
|
|
|
list.erase(std::remove(list.begin(), list.end(), this), list.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::getVertexUniformBuffer(char** buffer, std::size_t& size) const
|
|
|
|
{
|
|
|
|
*buffer = _vertexUniformBuffer;
|
|
|
|
size = _vertexUniformBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::getFragmentUniformBuffer(char** buffer, std::size_t& size) const
|
|
|
|
{
|
|
|
|
*buffer = _fragmentUniformBuffer;
|
|
|
|
size = _fragmentUniformBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|
|
|
|
|