diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index b9d813b..54331a9 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -4,6 +4,7 @@ Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2011 Zynga Inc. Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. +Copyright (c) 2021 cocos2d-lua.org http://www.cocos2d-x.org @@ -472,9 +473,14 @@ void Sprite::updateProgramStateTexture() auto programState = _trianglesCommand.getPipelineDescriptor().programState; programState->setTexture(_textureLocation, 0, _texture->getBackendTexture()); auto alphaTexture = _texture->getAlphaTexture(); - if(alphaTexture && alphaTexture->getBackendTexture()) - { - programState->setTexture(_alphaTextureLocation, 1, alphaTexture->getBackendTexture()); + // may ETC1 or user defined shader used u_texture1. + if (_alphaTextureLocation.location[0] != -1 || _alphaTextureLocation.location[1] != -1) { + if(alphaTexture && alphaTexture->getBackendTexture()) { + programState->setTexture(_alphaTextureLocation, 1, alphaTexture->getBackendTexture()); + } else { + // set a default texture into location to make metal render happy. + programState->setTexture(_alphaTextureLocation, 1, _texture->getBackendTexture()); + } } } diff --git a/cocos/renderer/backend/metal/ShaderModuleMTL.mm b/cocos/renderer/backend/metal/ShaderModuleMTL.mm index d0f8291..5e35eba 100644 --- a/cocos/renderer/backend/metal/ShaderModuleMTL.mm +++ b/cocos/renderer/backend/metal/ShaderModuleMTL.mm @@ -1,5 +1,6 @@ /**************************************************************************** Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2021 cocos2d-lua.org http://www.cocos2d-x.org @@ -37,22 +38,16 @@ ShaderModuleMTL::ShaderModuleMTL(id mtlDevice, ShaderStage stage, con glslopt_ctx* ctx = glslopt_initialize(kGlslTargetMetal); glslopt_shader_type shaderType = stage == ShaderStage::VERTEX ? kGlslOptShaderVertex : kGlslOptShaderFragment; glslopt_shader* glslShader = glslopt_optimize(ctx, shaderType, source.c_str(), 0); - if (!glslShader) - { - NSLog(@"Can not translate GLSL shader to metal shader:"); - NSLog(@"%s", source.c_str()); - return; - } - - const char* metalShader = glslopt_get_output(glslShader); - if (!metalShader) - { + if (!glslopt_get_status(glslShader)) { NSLog(@"Can not get metal shader:"); NSLog(@"%s", source.c_str()); + NSLog(@"%s", glslopt_get_log(glslShader)); + glslopt_shader_delete(glslShader); glslopt_cleanup(ctx); return; } + const char* metalShader = glslopt_get_output(glslShader); // NSLog(@"%s", metalShader); parseAttibute(mtlDevice, glslShader); @@ -66,8 +61,7 @@ ShaderModuleMTL::ShaderModuleMTL(id mtlDevice, ShaderStage stage, con id library = [mtlDevice newLibraryWithSource:shader options:nil error:&error]; - if (!library) - { + if (!library) { NSLog(@"Can not compile metal shader: %@", error); NSLog(@"%s", metalShader); glslopt_shader_delete(glslShader); @@ -79,8 +73,7 @@ ShaderModuleMTL::ShaderModuleMTL(id mtlDevice, ShaderStage stage, con _mtlFunction = [library newFunctionWithName:@"xlatMtlMain1"]; else _mtlFunction = [library newFunctionWithName:@"xlatMtlMain2"]; - if (!_mtlFunction) - { + if (!_mtlFunction) { NSLog(@"metal shader is ---------------"); NSLog(@"%s", metalShader); assert(false);