2

I created a Swift project using an SKScene with SKNode to show the result of a fragment shader. The project was created with iOS 8.x and Xcode 6.x. Since I upgraded to use Xcode 7 and iOS 9 (currently 7.1 and iOS 9.1), the shader does no longer compile at runtime and shows all kind of problems, such as:

program_source:16:7: warning: no previous prototype for function 'simpleRand'
float simpleRand(vec2 n)
      ^ 
program_source:26:31: error: use of undeclared identifier 'PI'
        float rad = val.x * 2.0 * PI * u_frequency[0];
                                  ^ 
program_source:26:36: error: use of undeclared identifier 'u_frequency'
        float rad = val.x * 2.0 * PI * u_frequency[0];

As far as I have managed to figure out, it seems that since iOS 9 / Xcode 7, the default fragment shader language used is Metal and not OpenGL, so my guess is, that the compiler tries to compile Metal code. If that is the case, how and where can I specify, that I want OpenGL ES?

The shader is initiated as:

func setupShader(uniforms: Array<SKUniform>) {
    if let shader = self.noiseShader {
        shader.uniforms = uniforms
        self.shaderContainer = SKSpriteNode(color: UIColor.redColor(), size: self.size)
        if let shaderContainer = self.shaderContainer {
            self.addChild(shaderContainer)
            shaderContainer.position = CGPointMake(self.size.width/2.0, self.size.height/2.0)
            shaderContainer.shader = shader;
        }
    }
}

and this code worked flawlessly prior to Xcode 7 / iOS 9.

Here is some of the shader code I am using:

const float PI = 3.14159265;

float sinNoise(vec2 val) {
    float rad = val.x * 2.0 * PI * u_frequency;
    return sin(rad);
}

///////////////////////////////////////
// Main
///////////////////////////////////////

void main() {
    vec2 position = v_tex_coord; // gets the location of the current pixel in the intervals [0..1] [0..1]

    float vnoise = sinNoise(position) / 2.0;
    float color = vec4(vec3(vnoise + 0.5), 1.0);

    gl_FragColor = color;
}

Important information: The code works perfectly in the simulator (for example for iPad Air 2 with iOS 9.1), but does not runtime compile on the same physical device with that OS.

genpfault
  • 51,148
  • 11
  • 85
  • 139
mjrehder
  • 317
  • 4
  • 11

1 Answers1

0

Ok, I found the answer in another post. You can add an entry to your projects info.plist file:

PrefersOpenGL (BOOL) YES

as shown in an answer to this post: xcode error calling display has no effect

This works, but it does not smell very good. Guess this is a temporary solution and it will break at some point in time.

Community
  • 1
  • 1
mjrehder
  • 317
  • 4
  • 11