2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
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-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "ProgramMTL.h"
|
|
|
|
#include "ShaderModuleMTL.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Macros.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2021-12-31 12:12:40 +08:00
|
|
|
namespace
|
|
|
|
{
|
2023-07-15 01:17:15 +08:00
|
|
|
constexpr std::string_view metalSpecificDefine = "#define METAL\n"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ProgramMTL::ProgramMTL(std::string_view vertexShader, std::string_view fragmentShader)
|
|
|
|
: Program(vertexShader, fragmentShader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_vertexShader = static_cast<ShaderModuleMTL*>(ShaderCache::newVertexShaderModule(vertexShader));
|
2023-07-15 01:17:15 +08:00
|
|
|
std::string combinedSource{metalSpecificDefine};
|
|
|
|
combinedSource += fragmentShader;
|
|
|
|
_fragmentShader = static_cast<ShaderModuleMTL*>(ShaderCache::newFragmentShaderModule(std::move(combinedSource)));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(_vertexShader);
|
|
|
|
AX_SAFE_RETAIN(_fragmentShader);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgramMTL::~ProgramMTL()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_vertexShader);
|
|
|
|
AX_SAFE_RELEASE(_fragmentShader);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ProgramMTL::getAttributeLocation(Attribute name) const
|
|
|
|
{
|
|
|
|
return _vertexShader->getAttributeLocation(name);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int ProgramMTL::getAttributeLocation(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _vertexShader->getAttributeLocation(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformLocation ProgramMTL::getUniformLocation(backend::Uniform name) const
|
|
|
|
{
|
|
|
|
UniformLocation uniformLocation;
|
|
|
|
auto vsLocation = _vertexShader->getUniformLocation(name);
|
|
|
|
auto fsLocation = _fragmentShader->getUniformLocation(name);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (vsLocation != -1 && fsLocation != -1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
uniformLocation.shaderStage = ShaderStage::VERTEX_AND_FRAGMENT;
|
|
|
|
uniformLocation.location[0] = vsLocation;
|
|
|
|
uniformLocation.location[1] = fsLocation;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else if (vsLocation != -1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
uniformLocation.shaderStage = ShaderStage::VERTEX;
|
|
|
|
uniformLocation.location[0] = vsLocation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uniformLocation.shaderStage = ShaderStage::FRAGMENT;
|
|
|
|
uniformLocation.location[1] = fsLocation;
|
|
|
|
}
|
|
|
|
return uniformLocation;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UniformLocation ProgramMTL::getUniformLocation(std::string_view uniform) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
UniformLocation uniformLocation;
|
|
|
|
auto vsLocation = _vertexShader->getUniformLocation(uniform);
|
|
|
|
auto fsLocation = _fragmentShader->getUniformLocation(uniform);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (vsLocation != -1 && fsLocation != -1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
uniformLocation.shaderStage = ShaderStage::VERTEX_AND_FRAGMENT;
|
|
|
|
uniformLocation.location[0] = vsLocation;
|
|
|
|
uniformLocation.location[1] = fsLocation;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else if (vsLocation != -1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
uniformLocation.shaderStage = ShaderStage::VERTEX;
|
|
|
|
uniformLocation.location[0] = vsLocation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uniformLocation.shaderStage = ShaderStage::FRAGMENT;
|
|
|
|
uniformLocation.location[1] = fsLocation;
|
|
|
|
}
|
|
|
|
return uniformLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProgramMTL::getMaxVertexLocation() const
|
|
|
|
{
|
|
|
|
return _vertexShader->getMaxLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProgramMTL::getMaxFragmentLocation() const
|
|
|
|
{
|
|
|
|
return _fragmentShader->getMaxLocation();
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:38:41 +08:00
|
|
|
const hlookup::string_map<AttributeBindInfo>& ProgramMTL::getActiveAttributes() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _vertexShader->getAttributeInfo();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// const std::vector<char>& ProgramMTL::cloneUniformBuffer(ShaderStage stage) const
|
2019-11-23 20:27:39 +08:00
|
|
|
//{
|
2021-12-31 12:12:40 +08:00
|
|
|
// switch (stage) {
|
|
|
|
// case ShaderStage::VERTEX:
|
|
|
|
// return _vertexShader->cloneUniformBuffer();
|
|
|
|
// break;
|
|
|
|
// case ShaderStage::FRAGMENT:
|
|
|
|
// return _fragmentShader->cloneUniformBuffer();
|
|
|
|
// default:
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXASSERT(false, "Invalid shader stage.");
|
2021-12-31 12:12:40 +08:00
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
const UniformInfo& ProgramMTL::getActiveUniformInfo(ShaderStage stage, int location) const
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::VERTEX:
|
|
|
|
return _vertexShader->getActiveUniform(location);
|
|
|
|
case ShaderStage::FRAGMENT:
|
|
|
|
return _fragmentShader->getActiveUniform(location);
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid shader stage.");
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t ProgramMTL::getUniformBufferSize(ShaderStage stage) const
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::VERTEX:
|
|
|
|
return _vertexShader->getUniformBufferSize();
|
|
|
|
case ShaderStage::FRAGMENT:
|
|
|
|
return _fragmentShader->getUniformBufferSize();
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid shader stage.");
|
2021-12-31 12:12:40 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
const hlookup::string_map<UniformInfo>& ProgramMTL::getAllActiveUniformInfo(ShaderStage stage) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::VERTEX:
|
|
|
|
return _vertexShader->getAllActiveUniformInfo();
|
|
|
|
case ShaderStage::FRAGMENT:
|
|
|
|
return _fragmentShader->getAllActiveUniformInfo();
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid shader stage.");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|