Search code examples
objective-copengl-esgpuimage

GPUImageLookupFilter with intensity control - crashing


Following this question, I'm trying to add an intensity property to the GPUImageLookupFilter. Seems to work great when hard-coding the intensity value, but wiring up the objective-c wrapper to the interface is causing it to crash.

In the main() implementation, I updated the gl_FragColor equation to the suggested:

gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), 1.0);
                                                                   // ^ intensity value

That works great. Then I tried adding/updated the following pieces to the obj-c wrapper:

.h

@interface GPUImageLookupFilter : GPUImageTwoInputFilter
{
    GLint intensityUniform;
}

@property(readwrite, nonatomic) CGFloat intensity;

@end

.m

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageLookupFragmentShaderString = SHADER_STRING
(
   // other unchanged declarations here

   uniform float intensity;

   void main()
   {
      // rest of the unchanged main function here
      gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
   }
);
#else
NSString *const kGPUImageLookupFragmentShaderString = SHADER_STRING
(
   // other unchanged declarations here

   uniform float intensity;

   void main()
   {
      // rest of the unchanged main function here
      gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
   }
);
#endif

@implementation GPUImageLookupFilter

@synthesize intensity = _intensity;

#pragma mark -
#pragma mark Initialization and teardown

- (id)init;
{
    intensityUniform = [filterProgram uniformIndex:@"intensity"];
    self.intensity = 1.0;

    if (!(self = [super initWithFragmentShaderFromString:kGPUImageLookupFragmentShaderString]))
    {
        return nil;
    }
    return self;
}

#pragma mark -
#pragma mark Accessors

- (void)setIntensity:(CGFloat)newValue;
{
    _intensity = newValue;
    [self setFloat:_intensity forUniform:intensityUniform program:filterProgram];
}

@end

And it's crashing with:

*** Assertion failure in -[GPUImageLookupFilter initWithVertexShaderFromString:fragmentShaderFromString:], ...GPUImageFilter.m:94
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Filter shader link failed'

Solution

  • Simple fix, looks like I needed to use lowp float instead:

    uniform lowp float intensity;