RNGLContext.java 4.09 KB
Newer Older
1 2
package com.projectseptember.RNGL;

3 4 5 6 7
import android.util.Log;

import static android.opengl.GLES20.*;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
8 9 10 11
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
12
import com.facebook.react.bridge.WritableMap;
13
import com.facebook.react.module.annotations.ReactModule;
14

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
15 16 17
import java.util.HashMap;
import java.util.Map;

18
@ReactModule(name = "RNGLContext")
19 20
public class RNGLContext extends ReactContextBaseJavaModule {

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
21 22 23 24 25 26 27 28
    private static String STATIC_VERT =
    "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));"+
    "}";

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
29
    private Map<Integer, GLShaderData> shaders = new HashMap<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
30
    private Map<Integer, GLFBO> fbos = new HashMap<>();
31
    private Map<Integer, Callback> onCompileCallbacks = new HashMap<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
32

33 34 35 36 37 38 39 40 41
    public RNGLContext (ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "RNGLContext";
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
42
    public GLShaderData getShader (Integer id) {
43 44 45 46 47
        GLShaderData data;
        synchronized (this) {
            data = shaders.get(id);
        }
        return data;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
48 49
    }

50
    @ReactMethod
51
    public void addShader (final Integer id, final ReadableMap config, final Callback onCompile) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
52 53
        final String frag = config.getString("frag");
        final String name = config.getString("name");
54 55 56 57 58 59
        GLShaderData data = new GLShaderData(name, STATIC_VERT, frag);
        synchronized (this) {
            shaders.put(id, data);
            if (onCompile != null) {
                onCompileCallbacks.put(id, onCompile);
            }
60 61 62 63 64
        }
    }

    @ReactMethod
    public void removeShader (final Integer id) {
65 66 67 68
        GLShaderData shader;
        synchronized (this) {
            shader = shaders.remove(id);
        }
69 70 71 72 73 74
        if (shader == null) {
            throw new Error("removeShader("+id+"): shader does not exist");
        }
    }

    public void shaderFailedToCompile(Integer id, GLShaderCompilationFailed e) {
75 76 77 78 79
        Callback onCompile;
        synchronized (this) {
            onCompile = onCompileCallbacks.get(id);
            onCompileCallbacks.remove(id);
        }
80 81 82 83 84 85 86 87 88
        if (onCompile == null) {
            Log.e("RNGLContext", e.getMessage());
        }
        else {
            onCompile.invoke(e.compileError);
        }
    }

    public void shaderSucceedToCompile(Integer id, Map<String, Integer> uniformTypes) {
89 90 91 92 93
        Callback onCompile;
        synchronized (this) {
            onCompile = onCompileCallbacks.get(id);
            onCompileCallbacks.remove(id);
        }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        if (onCompile != null) {
            WritableMap res = Arguments.createMap();
            WritableMap uniforms = Arguments.createMap();
            for (String key : uniformTypes.keySet()) {
                uniforms.putString(key, glTypeString(uniformTypes.get(key)));
            }
            res.putMap("uniforms", uniforms);
            onCompile.invoke(null, res);
        }
    }

    static String glTypeString (int type) {
        switch (type) {
            case GL_FLOAT: return "float";
            case GL_FLOAT_VEC2: return "vec2";
            case GL_FLOAT_VEC3: return "vec3";
            case GL_FLOAT_VEC4: return "vec4";
            case GL_INT: return "int";
            case GL_INT_VEC2: return "ivec2";
            case GL_INT_VEC3: return "ivec3";
            case GL_INT_VEC4: return "ivec4";
            case GL_BOOL: return "bool";
            case GL_BOOL_VEC2: return "bvec2";
            case GL_BOOL_VEC3: return "bvec3";
            case GL_BOOL_VEC4: return "bvec4";
            case GL_FLOAT_MAT2: return "mat2";
            case GL_FLOAT_MAT3: return "mat3";
            case GL_FLOAT_MAT4: return "mat4";
            case GL_SAMPLER_2D: return "sampler2D";
            case GL_SAMPLER_CUBE: return "samplerCube";
        }
        return "";
126 127
    }
}