GLShadersRegistry.m 1.2 KB
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#import <UIKit/UIKit.h>

#import "RCTBridgeModule.h"
#import "RCTConvert.h"
#import "RCTLog.h"
#import "GLShadersRegistry.h"


@implementation GLShadersRegistry
{
  NSMutableDictionary *_shaders;
  EAGLContext *_context;
}

GLShadersRegistry *GLShadersRegistry_instance; // FIXME is that the proper way to do singleton?

RCT_EXPORT_MODULE();

+ (GLShader*) getShader: (NSNumber *)ctxid
{
  return [GLShadersRegistry_instance shaders][ctxid];
}

- (instancetype)init
{
  self = [super init];
  if (self) {
    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    if (!_context) {
      RCTLogError(@"Failed to initialize OpenGLES 2.0 context");
    }
    _shaders = @{}.mutableCopy;
    GLShadersRegistry_instance = self;
  }
  return self;
}

static NSString* fullViewportVert = @"attribute vec2 position;varying vec2 uv;void main() {gl_Position = vec4(position,0.0,1.0);uv = vec2(0.5, 0.5) * (position+vec2(1.0, 1.0));}";

RCT_EXPORT_METHOD(register:(nonnull NSNumber *)id withConfig:(NSDictionary *)config) {
  NSString *frag = [RCTConvert NSString:config[@"frag"]];
  GLShader *shader = [[GLShader alloc] initWithContext:_context withVert:fullViewportVert withFrag:frag];
  _shaders[id] = shader;
}

@end