axmol/core/renderer/backend/metal/ShaderModuleMTL.mm

292 lines
9.5 KiB
Plaintext
Raw Normal View History

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.
****************************************************************************/
2019-11-23 20:27:39 +08:00
#include "ShaderModuleMTL.h"
#include "DeviceMTL.h"
2023-09-01 01:17:49 +08:00
#include "glsl_optimizer.h"
2019-11-23 20:27:39 +08:00
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
ShaderModuleMTL::ShaderModuleMTL(id<MTLDevice> mtlDevice, ShaderStage stage, std::string_view source)
: ShaderModule(stage)
2019-11-23 20:27:39 +08:00
{
2023-09-01 01:17:49 +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)
{
2023-09-01 01:17:49 +08:00
NSLog(@"Can not translate GLSL shader to metal shader:");
NSLog(@"%s", source.data());
2019-11-23 20:27:39 +08:00
return;
}
2023-09-01 01:17:49 +08:00
const char* metalShader = glslopt_get_output(glslShader);
if (!metalShader)
{
2023-09-01 01:17:49 +08:00
NSLog(@"Can not get metal shader:");
NSLog(@"%s", source.data());
NSLog(@"%s", glslopt_get_log(glslShader));
glslopt_cleanup(ctx);
assert(false);
return;
}
2023-09-01 01:17:49 +08:00
// NSLog(@"%s", metalShader);
2023-09-01 01:17:49 +08:00
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;
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-09-01 01:17:49 +08:00
NSLog(@"%s", glslopt_get_log(glslShader));
glslopt_shader_delete(glslShader);
glslopt_cleanup(ctx);
assert(false);
2019-11-23 20:27:39 +08:00
return;
}
2023-09-01 01:17:49 +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-09-01 01:17:49 +08:00
NSLog(@"%s", glslopt_get_log(glslShader));
2019-11-23 20:27:39 +08:00
assert(false);
}
2023-09-01 01:17:49 +08:00
glslopt_shader_delete(glslShader);
glslopt_cleanup(ctx);
2019-11-23 20:27:39 +08:00
[library release];
}
ShaderModuleMTL::~ShaderModuleMTL()
{
[_mtlFunction release];
}
2023-09-01 01:17:49 +08:00
void ShaderModuleMTL::parseAttibute(id<MTLDevice> mtlDevice, glslopt_shader* shader)
2019-11-23 20:27:39 +08:00
{
2023-09-01 01:17:49 +08:00
const int attributeCount = glslopt_shader_get_input_count(shader);
for (int i = 0; i < attributeCount; i++)
{
2023-09-01 01:17:49 +08:00
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-09-01 01:17:49 +08:00
attributeInfo.attributeName = parName;
attributeInfo.location = location;
_attributeInfo[parName] = attributeInfo;
2019-11-23 20:27:39 +08:00
}
}
2023-09-01 01:17:49 +08:00
void ShaderModuleMTL::parseUniform(id<MTLDevice> mtlDevice, glslopt_shader* shader)
2019-11-23 20:27:39 +08:00
{
2023-09-01 01:17:49 +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)
{
2023-09-01 01:17:49 +08:00
int nextLocation = -1;
const char* parName;
glslopt_basic_type parType;
glslopt_precision parPrec;
int parVecSize, parMatSize, parArrSize, location;
if (i + 1 < uniformCount)
2023-08-31 22:08:50 +08:00
{
2023-09-01 01:17:49 +08:00
glslopt_shader_get_uniform_desc(shader, i + 1, &parName, &parType, &parPrec, &parVecSize, &parMatSize,
&parArrSize, &location);
nextLocation = location;
}
else
{
nextLocation = static_cast<int>(_uniformBufferSize);
2023-07-19 21:12:25 +08:00
}
2023-09-01 01:17:49 +08:00
glslopt_shader_get_uniform_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize,
&location);
2023-07-27 19:13:09 +08:00
2023-09-01 01:17:49 +08:00
parArrSize = (parArrSize > 0) ? parArrSize : 1;
2019-11-23 20:27:39 +08:00
UniformInfo uniform;
2023-09-01 01:17:49 +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
}
}
2023-09-01 01:17:49 +08:00
int ShaderModuleMTL::getUniformLocation(Uniform name) const
2019-11-23 20:27:39 +08:00
{
return _uniformLocation[name];
}
2023-09-01 01:17:49 +08:00
int ShaderModuleMTL::getUniformLocation(std::string_view name) const
2019-11-23 20:27:39 +08:00
{
2023-09-01 01:17:49 +08:00
auto iter = _uniformInfos.find(name);
if (iter != _uniformInfos.end())
2019-11-23 20:27:39 +08:00
{
2023-09-01 01:17:49 +08:00
return iter->second.location;
2019-11-23 20:27:39 +08:00
}
2023-09-01 01:17:49 +08:00
else
return -1;
2019-11-23 20:27:39 +08:00
}
2023-09-01 01:17:49 +08:00
void ShaderModuleMTL::setBuiltinUniformLocation()
2019-11-23 20:27:39 +08:00
{
2023-09-01 01:17:49 +08:00
std::fill(_uniformLocation, _uniformLocation + UNIFORM_MAX, -1);
/// u_mvpMatrix
auto iter = _uniformInfos.find(UNIFORM_NAME_MVP_MATRIX);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::MVP_MATRIX] = iter->second.location;
}
2023-09-01 01:17:49 +08:00
/// u_textColor
iter = _uniformInfos.find(UNIFORM_NAME_TEXT_COLOR);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::TEXT_COLOR] = iter->second.location;
}
2023-09-01 01:17:49 +08:00
/// u_effectColor
iter = _uniformInfos.find(UNIFORM_NAME_EFFECT_COLOR);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::EFFECT_COLOR] = iter->second.location;
}
2023-09-01 01:17:49 +08:00
/// u_effectType
iter = _uniformInfos.find(UNIFORM_NAME_EFFECT_TYPE);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::EFFECT_TYPE] = iter->second.location;
}
/// u_tex0
2023-09-01 01:17:49 +08:00
iter = _uniformInfos.find(UNIFORM_NAME_TEXTURE);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::TEXTURE] = iter->second.location;
}
/// u_tex1
2023-09-01 01:17:49 +08:00
iter = _uniformInfos.find(UNIFORM_NAME_TEXTURE1);
if (iter != _uniformInfos.end())
{
_uniformLocation[Uniform::TEXTURE1] = iter->second.location;
}
2019-11-23 20:27:39 +08:00
}
int ShaderModuleMTL::getAttributeLocation(Attribute name) const
{
return _attributeLocation[name];
}
int ShaderModuleMTL::getAttributeLocation(std::string_view name)
2019-11-23 20:27:39 +08:00
{
auto iter = _attributeInfo.find(name);
if (iter != _attributeInfo.end())
2019-11-23 20:27:39 +08:00
return _attributeInfo[name].location;
else
return -1;
}
2023-09-01 01:17:49 +08:00
void ShaderModuleMTL::setBuiltinAttributeLocation()
{
std::fill(_attributeLocation, _attributeLocation + ATTRIBUTE_MAX, -1);
/// a_position
auto iter = _attributeInfo.find(ATTRIBUTE_NAME_POSITION);
if (iter != _attributeInfo.end())
{
_attributeLocation[Attribute::POSITION] = iter->second.location;
}
/// a_color
iter = _attributeInfo.find(ATTRIBUTE_NAME_COLOR);
if (iter != _attributeInfo.end())
{
_attributeLocation[Attribute::COLOR] = iter->second.location;
}
/// a_texCoord
iter = _attributeInfo.find(ATTRIBUTE_NAME_TEXCOORD);
if (iter != _attributeInfo.end())
{
_attributeLocation[Attribute::TEXCOORD] = iter->second.location;
}
/// a_normal
iter = _attributeInfo.find(ATTRIBUTE_NAME_NORMAL);
if (iter != _attributeInfo.end())
{
_attributeLocation[Attribute::NORMAL] = 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;
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;
}
}
NS_AX_BACKEND_END