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 "ShaderModuleMTL.h"
|
|
|
|
#include "DeviceMTL.h"
|
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
#include "glsl_optimizer.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ShaderModuleMTL::ShaderModuleMTL(id<MTLDevice> mtlDevice, ShaderStage stage, std::string_view source)
|
|
|
|
: ShaderModule(stage)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-07-15 00:21:23 +08:00
|
|
|
// Convert GLSL shader to metal shader
|
|
|
|
// TODO: don't crreate/destroy ctx every time.
|
|
|
|
glslopt_ctx* ctx = glslopt_initialize(kGlslTargetMetal);
|
|
|
|
glslopt_shader_type shaderType = stage == ShaderStage::VERTEX ? kGlslOptShaderVertex : kGlslOptShaderFragment;
|
|
|
|
glslopt_shader* glslShader = glslopt_optimize(ctx, shaderType, source.data(), 0);
|
|
|
|
if (!glslShader)
|
|
|
|
{
|
|
|
|
NSLog(@"Can not translate GLSL shader to metal shader:");
|
|
|
|
NSLog(@"%s", source.data());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* metalShader = glslopt_get_output(glslShader);
|
|
|
|
if (!metalShader)
|
|
|
|
{
|
|
|
|
NSLog(@"Can not get metal shader:");
|
|
|
|
NSLog(@"%s", source.data());
|
|
|
|
NSLog(@"%s", glslopt_get_log(glslShader));
|
|
|
|
glslopt_cleanup(ctx);
|
2023-02-09 23:07:36 +08:00
|
|
|
assert(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
// NSLog(@"%s", metalShader);
|
|
|
|
|
|
|
|
parseAttibute(mtlDevice, glslShader);
|
|
|
|
parseUniform(mtlDevice, glslShader);
|
|
|
|
parseTexture(mtlDevice, glslShader);
|
|
|
|
setBuiltinUniformLocation();
|
|
|
|
setBuiltinAttributeLocation();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NSString* shader = [NSString stringWithUTF8String:metalShader];
|
|
|
|
NSError* error;
|
2021-12-31 12:12:40 +08:00
|
|
|
id<MTLLibrary> library = [mtlDevice newLibraryWithSource:shader options:nil error:&error];
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!library)
|
|
|
|
{
|
|
|
|
NSLog(@"Can not compile metal shader: %@", error);
|
|
|
|
NSLog(@"%s", metalShader);
|
2023-07-15 00:21:23 +08:00
|
|
|
NSLog(@"%s", glslopt_get_log(glslShader));
|
|
|
|
glslopt_shader_delete(glslShader);
|
|
|
|
glslopt_cleanup(ctx);
|
2023-02-09 23:07:36 +08:00
|
|
|
assert(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
if (ShaderStage::VERTEX == stage)
|
|
|
|
_mtlFunction = [library newFunctionWithName:@"xlatMtlMain1"];
|
|
|
|
else
|
|
|
|
_mtlFunction = [library newFunctionWithName:@"xlatMtlMain2"];
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_mtlFunction)
|
|
|
|
{
|
|
|
|
NSLog(@"metal shader is ---------------");
|
|
|
|
NSLog(@"%s", metalShader);
|
2023-07-15 00:21:23 +08:00
|
|
|
NSLog(@"%s", glslopt_get_log(glslShader));
|
2019-11-23 20:27:39 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
glslopt_shader_delete(glslShader);
|
|
|
|
glslopt_cleanup(ctx);
|
2019-11-23 20:27:39 +08:00
|
|
|
[library release];
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderModuleMTL::~ShaderModuleMTL()
|
|
|
|
{
|
|
|
|
[_mtlFunction release];
|
|
|
|
}
|
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
void ShaderModuleMTL::parseAttibute(id<MTLDevice> mtlDevice, glslopt_shader* shader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-07-15 00:21:23 +08:00
|
|
|
const int attributeCount = glslopt_shader_get_input_count(shader);
|
|
|
|
for (int i = 0; i < attributeCount; i++)
|
|
|
|
{
|
|
|
|
const char* parName;
|
|
|
|
glslopt_basic_type parType;
|
|
|
|
glslopt_precision parPrec;
|
|
|
|
int parVecSize, parMatSize, parArrSize, location;
|
|
|
|
glslopt_shader_get_input_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
|
|
|
|
&location);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
AttributeBindInfo attributeInfo;
|
2023-07-15 00:21:23 +08:00
|
|
|
attributeInfo.attributeName = parName;
|
|
|
|
attributeInfo.location = location;
|
|
|
|
_attributeInfo[parName] = attributeInfo;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
void ShaderModuleMTL::parseUniform(id<MTLDevice> mtlDevice, glslopt_shader* shader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-07-15 00:21:23 +08:00
|
|
|
const int uniformCount = glslopt_shader_get_uniform_count(shader);
|
|
|
|
_uniformBufferSize = glslopt_shader_get_uniform_total_size(shader);
|
|
|
|
|
|
|
|
for (int i = 0; i < uniformCount; ++i)
|
|
|
|
{
|
|
|
|
int nextLocation = -1;
|
|
|
|
const char* parName;
|
|
|
|
glslopt_basic_type parType;
|
|
|
|
glslopt_precision parPrec;
|
|
|
|
int parVecSize, parMatSize, parArrSize, location;
|
|
|
|
if (i + 1 < uniformCount)
|
|
|
|
{
|
|
|
|
glslopt_shader_get_uniform_desc(shader, i + 1, &parName, &parType, &parPrec, &parVecSize, &parMatSize,
|
|
|
|
&parArrSize, &location);
|
|
|
|
nextLocation = location;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextLocation = static_cast<int>(_uniformBufferSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
glslopt_shader_get_uniform_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
|
|
|
|
&location);
|
|
|
|
|
|
|
|
parArrSize = (parArrSize > 0) ? parArrSize : 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
UniformInfo uniform;
|
2023-07-15 00:21:23 +08:00
|
|
|
uniform.count = parArrSize;
|
|
|
|
uniform.location = location;
|
|
|
|
uniform.isArray = parArrSize;
|
|
|
|
uniform.size = nextLocation - location;
|
|
|
|
uniform.bufferOffset = location;
|
|
|
|
uniform.needConvert = (parVecSize == 3) ? true : false;
|
|
|
|
uniform.type = static_cast<unsigned int>(parType);
|
|
|
|
uniform.isMatrix = (parMatSize > 1) ? true : false;
|
|
|
|
_uniformInfos[parName] = uniform;
|
|
|
|
_activeUniformInfos[location] = uniform;
|
|
|
|
_maxLocation = _maxLocation < location ? (location + 1) : _maxLocation;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ShaderModuleMTL::getUniformLocation(Uniform name) const
|
|
|
|
{
|
|
|
|
return _uniformLocation[name];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int ShaderModuleMTL::getUniformLocation(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto iter = _uniformInfos.find(name);
|
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return iter->second.location;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderModuleMTL::setBuiltinUniformLocation()
|
|
|
|
{
|
|
|
|
std::fill(_uniformLocation, _uniformLocation + UNIFORM_MAX, -1);
|
2021-12-31 12:12:40 +08:00
|
|
|
/// u_mvpMatrix
|
2019-11-23 20:27:39 +08:00
|
|
|
auto iter = _uniformInfos.find(UNIFORM_NAME_MVP_MATRIX);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_uniformLocation[Uniform::MVP_MATRIX] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
/// u_textColor
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _uniformInfos.find(UNIFORM_NAME_TEXT_COLOR);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_uniformLocation[Uniform::TEXT_COLOR] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
/// u_effectColor
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _uniformInfos.find(UNIFORM_NAME_EFFECT_COLOR);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_uniformLocation[Uniform::EFFECT_COLOR] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
/// u_effectType
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _uniformInfos.find(UNIFORM_NAME_EFFECT_TYPE);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_uniformLocation[Uniform::EFFECT_TYPE] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-05 00:39:20 +08:00
|
|
|
/// u_tex0
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _uniformInfos.find(UNIFORM_NAME_TEXTURE);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_uniformLocation[Uniform::TEXTURE] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-05 00:39:20 +08:00
|
|
|
/// u_tex1
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _uniformInfos.find(UNIFORM_NAME_TEXTURE1);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _uniformInfos.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_uniformLocation[Uniform::TEXTURE1] = iter->second.location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ShaderModuleMTL::getAttributeLocation(Attribute name) const
|
|
|
|
{
|
|
|
|
return _attributeLocation[name];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int ShaderModuleMTL::getAttributeLocation(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto iter = _attributeInfo.find(name);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
return _attributeInfo[name].location;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderModuleMTL::setBuiltinAttributeLocation()
|
|
|
|
{
|
|
|
|
std::fill(_attributeLocation, _attributeLocation + ATTRIBUTE_MAX, -1);
|
2021-12-31 12:12:40 +08:00
|
|
|
/// a_position
|
2019-11-23 20:27:39 +08:00
|
|
|
auto iter = _attributeInfo.find(ATTRIBUTE_NAME_POSITION);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_attributeLocation[Attribute::POSITION] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
/// a_color
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _attributeInfo.find(ATTRIBUTE_NAME_COLOR);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_attributeLocation[Attribute::COLOR] = iter->second.location;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
/// a_texCoord
|
2019-11-23 20:27:39 +08:00
|
|
|
iter = _attributeInfo.find(ATTRIBUTE_NAME_TEXCOORD);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (iter != _attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_attributeLocation[Attribute::TEXCOORD] = iter->second.location;
|
|
|
|
}
|
2022-09-23 22:41:30 +08:00
|
|
|
|
|
|
|
/// a_normal
|
|
|
|
iter = _attributeInfo.find(ATTRIBUTE_NAME_NORMAL);
|
|
|
|
if (iter != _attributeInfo.end())
|
|
|
|
{
|
|
|
|
_attributeLocation[Attribute::NORMAL] = iter->second.location;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-15 00:21:23 +08:00
|
|
|
void ShaderModuleMTL::parseTexture(id<MTLDevice> mtlDevice, glslopt_shader* shader)
|
|
|
|
{
|
|
|
|
const int textureCount = glslopt_shader_get_texture_count(shader);
|
|
|
|
for (int i = 0; i < textureCount; ++i)
|
|
|
|
{
|
|
|
|
const char* parName;
|
|
|
|
glslopt_basic_type parType;
|
|
|
|
glslopt_precision parPrec;
|
|
|
|
int parVecSize, parMatSize, parArrSize, location;
|
|
|
|
glslopt_shader_get_texture_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
|
|
|
|
&location);
|
|
|
|
|
|
|
|
UniformInfo uniform;
|
|
|
|
uniform.count = parArrSize;
|
|
|
|
uniform.location = location;
|
|
|
|
uniform.isArray = parArrSize > 0;
|
|
|
|
_uniformInfos[parName] = uniform;
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|