[Metal Compiler Warning] unknown variable

Hi everyone,

Running Xcode 12, I'm developing a standalone WatchOS App using Spritekit.

I'm using a simple Palette-swap shader using a SKShader with several SKAttributes.

The shader seems to work fine, however I get the following message in the console: [Metal Compiler Warning] Warning: Compilation succeeded with: 
program_source:3:19: warning: unused variable 's'
constexpr sampler s(coord::normalized,

I have no idea what this means, there is no variable 's' in my code so I guess this is an issue because GLGS shader code gets compiled to Metal in the background and it goes wrong there? How do I verify this? It seems there is a memory leak associated with this error so I'm prone to solve it. Any help would be appreciated, even if it's just pointing me in a direction. (Like, should I write a new palette shader in Metal?) Thanks in advance!

The function that adds the SKShader (an extension to SKSpriteNode):

Code Block    
func addMultiColorShader(strokeColor: UIColor, colors:[UIColor]) {
        let useColors:[UIColor] = shaderColors(colors)
        var attributes:[SKAttribute] = [SKAttribute(name: "a_size", type: .vectorFloat2),
                                        SKAttribute(name: "a_scale", type: .vectorFloat2),
                                        SKAttribute(name: "a_alpha", type: .float)]
        
        let colorNames:[String] = ["a_color0","a_color1","a_color2","a_color3"]
        for i in 0..<colorNames.count {
            attributes += [SKAttribute(name: colorNames[i], type: .vectorFloat4)]
        }
        let shader = SKShader(fromFile: "SKHMultiColorize", attributes: attributes)
        setShaderAttributes(size: self.size, scale: CGSize(width: self.xScale, height: self.yScale), alpha: self.alpha)
        setShaderColors(strokeColor: strokeColor, color1: useColors[0], color2: useColors[1], color3: useColors[2])
        self.shader = shader
    }

The actual shader code (OpenGL GS):
Code Block
vec2 nearestNeighbor(vec2 loc, vec2 size) {
    vec2 onePixel = vec2(1.0, 1.0) / size;
    vec2 coordinate = floor(loc * size) / size; // round
    return coordinate + (onePixel * 0.5);
}
void main()
{
    vec4 colors[4];
    colors[0] = a_color0;
    colors[1] = a_color1;
    colors[2] = a_color2;
    colors[3] = a_color3;
        
    vec4 currentColor = texture2D(u_texture, nearestNeighbor(v_tex_coord, a_size));
    int colorIndex = int((currentColor.r * 5.1)); 
    vec4 refColor = colors[colorIndex];
    gl_FragColor = refColor * currentColor.a;
}



Answered by peniche in 636196022
We started seeing this warning as well. We're running SpriteKit using SKShader's, but iPhone and iPad only (no WatchOS). For us, the issue started with Xcode12 with devices running iOS 14.0. It seems like there is a Metal compilation happening which did not occur when the same device was running iOS 13 or earlier, and also wasn't happening for the same device with XCode11. We have prefersOpenGL = true in the Info.plist. We'll update if we find a solution, but please keep us posted in you find one.
Accepted Answer
We started seeing this warning as well. We're running SpriteKit using SKShader's, but iPhone and iPad only (no WatchOS). For us, the issue started with Xcode12 with devices running iOS 14.0. It seems like there is a Metal compilation happening which did not occur when the same device was running iOS 13 or earlier, and also wasn't happening for the same device with XCode11. We have prefersOpenGL = true in the Info.plist. We'll update if we find a solution, but please keep us posted in you find one.
Hi @peniche,

Haven't found anything new other than just setting prefersOpenGL to true which prevents the METAL errors from showing up. Will keep you posted if I happen to learn more.

Any news about this? having the same problem (using SKShader too)

Any update on this warning?

[Metal Compiler Warning] unknown variable
 
 
Q