What is correct way to use #extension in .fsh shader file?
TestShader.fsh:
#extension GL_EXT_shader_framebuffer_fetch : require
void main() {
lowp vec4 destColor = gl_LastFragData[0];
...
}in Swift:
spriteNode.shader = SKShader(fileNamed: "TestShader.fsh")By this way shader compilation fails with
error: invalid preprocessing directive
#extension GL_EXT_shader_framebuffer_fetch : require
error: use of undeclared identifier 'gl_LastFragData'
lowp vec4 destColor = gl_LastFragData[0];Without extension the shader is compiled successfully.
I'm using Xcode 9.2, running on iphone SE iOS11, PrefersOpenGL is not setted in Info.plist.
The documentation fairly strongly states that OpenGL extensions are not supported:
Warning
SKShader does not support OpenGL Extensions. SpriteKit will return an error if you compile a project containing a fragment shader using extensions.The link you provided cites the extension (framebufffer fetch) is used for programmable blending. In SpriteKit, programmatic blending can be achieved by snapshotting a sprite node with SKView.texture( from: sprite ) and updating the sprite's texture with the result.
let sprite = SKSpriteNode( texture: texture, color: nil, size: texture.size )
let sprite.shader = SKShader( ... )
// every loop that the shader is active, update the sprites texture with the shaded result:
let shadedTexture = mySKView.texture( from: sprite )
sprite.texture = shadedTexture