2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
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-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "ShaderModuleMTL.h"
|
|
|
|
#include "DeviceMTL.h"
|
|
|
|
|
|
|
|
#include "glsl_optimizer.h"
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// Convert GLSL shader to metal shader
|
2021-12-31 12:12:40 +08:00
|
|
|
// TODO: don't crreate/destroy ctx every time.
|
|
|
|
glslopt_ctx* ctx = glslopt_initialize(kGlslTargetMetal);
|
2019-11-23 20:27:39 +08:00
|
|
|
glslopt_shader_type shaderType = stage == ShaderStage::VERTEX ? kGlslOptShaderVertex : kGlslOptShaderFragment;
|
2021-12-31 12:12:40 +08:00
|
|
|
glslopt_shader* glslShader = glslopt_optimize(ctx, shaderType, source.data(), 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!glslShader)
|
|
|
|
{
|
|
|
|
NSLog(@"Can not translate GLSL shader to metal shader:");
|
2021-12-31 12:12:40 +08:00
|
|
|
NSLog(@"%s", source.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* metalShader = glslopt_get_output(glslShader);
|
|
|
|
if (!metalShader)
|
|
|
|
{
|
|
|
|
NSLog(@"Can not get metal shader:");
|
2021-12-31 12:12:40 +08:00
|
|
|
NSLog(@"%s", source.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
glslopt_cleanup(ctx);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// NSLog(@"%s", metalShader);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
parseAttibute(mtlDevice, glslShader);
|
|
|
|
parseUniform(mtlDevice, glslShader);
|
|
|
|
parseTexture(mtlDevice, glslShader);
|
|
|
|
setBuiltinUniformLocation();
|
|
|
|
setBuiltinAttributeLocation();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
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);
|
|
|
|
glslopt_shader_delete(glslShader);
|
|
|
|
glslopt_cleanup(ctx);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (ShaderStage::VERTEX == stage)
|
|
|
|
_mtlFunction = [library newFunctionWithName:@"xlatMtlMain1"];
|
|
|
|
else
|
|
|
|
_mtlFunction = [library newFunctionWithName:@"xlatMtlMain2"];
|
|
|
|
if (!_mtlFunction)
|
|
|
|
{
|
|
|
|
NSLog(@"metal shader is ---------------");
|
|
|
|
NSLog(@"%s", metalShader);
|
|
|
|
assert(false);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
glslopt_shader_delete(glslShader);
|
|
|
|
glslopt_cleanup(ctx);
|
|
|
|
[library release];
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderModuleMTL::~ShaderModuleMTL()
|
|
|
|
{
|
|
|
|
[_mtlFunction release];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderModuleMTL::parseAttibute(id<MTLDevice> mtlDevice, glslopt_shader* shader)
|
|
|
|
{
|
|
|
|
const int attributeCount = glslopt_shader_get_input_count(shader);
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 0; i < attributeCount; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const char* parName;
|
|
|
|
glslopt_basic_type parType;
|
|
|
|
glslopt_precision parPrec;
|
|
|
|
int parVecSize, parMatSize, parArrSize, location;
|
2021-12-31 12:12:40 +08:00
|
|
|
glslopt_shader_get_input_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
|
|
|
|
&location);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
AttributeBindInfo attributeInfo;
|
|
|
|
attributeInfo.attributeName = parName;
|
2021-12-31 12:12:40 +08:00
|
|
|
attributeInfo.location = location;
|
|
|
|
_attributeInfo[parName] = attributeInfo;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderModuleMTL::parseUniform(id<MTLDevice> mtlDevice, glslopt_shader* shader)
|
|
|
|
{
|
|
|
|
const int uniformCount = glslopt_shader_get_uniform_count(shader);
|
2021-12-31 12:12:40 +08:00
|
|
|
_uniformBufferSize = glslopt_shader_get_uniform_total_size(shader);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (i + 1 < uniformCount)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
glslopt_shader_get_uniform_desc(shader, i + 1, &parName, &parType, &parPrec, &parVecSize, &parMatSize,
|
|
|
|
&parArrSize, &location);
|
2019-11-23 20:27:39 +08:00
|
|
|
nextLocation = location;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextLocation = static_cast<int>(_uniformBufferSize);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
glslopt_shader_get_uniform_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
|
|
|
|
&location);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
parArrSize = (parArrSize > 0) ? parArrSize : 1;
|
|
|
|
UniformInfo uniform;
|
2021-12-31 12:12:40 +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;
|
2019-11-23 20:27:39 +08:00
|
|
|
_activeUniformInfos[location] = uniform;
|
2021-12-31 12:12:40 +08:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-12-31 12:12:40 +08:00
|
|
|
glslopt_shader_get_texture_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
|
|
|
|
&location);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
UniformInfo uniform;
|
2021-12-31 12:12:40 +08:00
|
|
|
uniform.count = parArrSize;
|
|
|
|
uniform.location = location;
|
|
|
|
uniform.isArray = parArrSize > 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
_uniformInfos[parName] = uniform;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|