SpriteKit shader crash iOS 9: SKDefaultShading, gl

2019-04-05 00:38发布

Just installed the iOS 9 open beta (version 3) and now I'm having loads of problems with SpriteKit shaders. On iOS 8 the following code worked just fine:

_fontShader = [SKShader shaderWithFileNamed:@"TheShader"]; // TODO: iOS9 compatibility issues here
_fontUniform = [SKUniform uniformWithName:@"labelBase" float:0];
[self.fontShader addUniform:self.fontUniform]; // TODO: iOS9 compatibility issues here

_fontEffects = [SKEffectNode node];
self.fontEffects.shader = self.fontShader; // TODO: iOS9 compatibility issues here
self.fontEffects.shouldEnableEffects = YES;
self.fontEffects.shouldCenterFilter = NO;
self.fontEffects.shouldRasterize = YES;
[self addChild:self.fontEffects];

Edit: the file "TheShader.fsh" looks like this:

float yPos = gl_FragCoord.y - labelBase; // iOS 9 Compatibility issues here
float gradient = 0.35*(yPos / u_sprite_size.y); // ranges from 0 at base to 0.35 at top

vec4 color = SKDefaultShading(); // the current label color (iOS 9 Compatibility issues here)
color = vec4(gradient + color.r, gradient + color.g, gradient + color.b, color.a);
color.rgb *= color.a; // set background to alpha 0

gl_FragColor = color;

On iOS 9 the console is spewing out a pile of warnings in this format:

2015-07-12 22:43:17.717 ReconInForce[599:110531] Jet: Error Domain=MTLLibraryErrorDomain Code=3 "Compilation failed: 

program_source:8:18: error: use of undeclared identifier 'gl_FragCoord'
    float yPos = gl_FragCoord.y - labelBase[0];
                 ^
program_source:11:18: error: use of undeclared identifier 'SKDefaultShading'
    vec4 color = SKDefaultShading(); // the current label color
             ^
" UserInfo=0x158c084b0 {NSLocalizedDescription=Compilation failed: 

program_source:8:18: error: use of undeclared identifier 'gl_FragCoord'
    float yPos = gl_FragCoord.y - labelBase[0];
             ^
program_source:11:18: error: use of undeclared identifier 'SKDefaultShading'
    vec4 color = SKDefaultShading(); // the current label color
             ^
}

Seems like iOS9 is not supporting SKDefaultShading and gl_FragCoord. How do I find the local pixel dimension without using gl_FragCoord?

The console continues to repeat this forever, slowing down the device. Here's a screenshot: enter image description here

If I comment out the lines above that say "iOS9 compatibility issues here" then the problem resolves itself, but then I don't have any shaders.

Anyone else having this issue? Much help would be appreciated.

4条回答
一纸荒年 Trace。
2楼-- · 2019-04-05 00:49

Try to add parameter 'PrefersOpenGL' of type boolean set to 'YES' in info.plist. This will disable Metal

查看更多
Bombasti
3楼-- · 2019-04-05 00:54

My custom SKShader also stopped working in ios9 on metal devices.

I was able to fix it using v_tex_coord to get the pixel position, and custom uniform to send in the sprite size (There seems to be a bug where u_sprite_size isn't being passed in.)

[SKUniform uniformWithName:@"u_fixed_size" floatVector2:GLKVector2Make(n.calculateAccumulatedFrame.size.width, n.calculateAccumulatedFrame.size.height)]
查看更多
乱世女痞
4楼-- · 2019-04-05 01:09

I had this problem too with iOS9. gl_FragCoord stopped working on real devices, but works in the iOS simulator.

Work around.

Create a node with shader

let shader = SKShader(fileNamed: "polka.fsh")
let uniform = SKUniform(name: "u_spriteSize", floatVector2: GLKVector2Make(Float(200), Float(200)))
shader.uniforms.append(uniform)

Update the uniform

if let uniform = shader.uniformNamed("u_spriteSize") {
    let size = frame.size
    uniform.floatVector2Value = GLKVector2Make(Float(size.width * screenScale), Float(size.height * screenScale))
}

Shader

vec2 xy = v_tex_coord * u_spriteSize - u_polkaCenter; // new code
//vec2 xy = gl_FragCoord.xy - u_polkaCenter;  // old code that worked before iOS9
查看更多
\"骚年 ilove
5楼-- · 2019-04-05 01:14

I came here after seeing the message use of undeclared identifier 'gl_FragCoord' but eventually I could solve my problem by replacing

gl_FragCoord.xy / u_sprite_size.xy

with

v_tex_coord

查看更多
登录 后发表回答