RNGLContext.java 3.59 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

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

17 18
public class RNGLContext extends ReactContextBaseJavaModule {

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
19 20 21 22 23 24 25 26
    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
27
    private Map<Integer, GLShaderData> shaders = new HashMap<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
28
    private Map<Integer, GLFBO> fbos = new HashMap<>();
29
    private Map<Integer, Callback> onCompileCallbacks = new HashMap<>();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
30

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

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

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
40
    public GLShaderData getShader (Integer id) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
41 42 43
        return shaders.get(id);
    }

44
    @ReactMethod
45
    public void addShader (final Integer id, final ReadableMap config, final Callback onCompile) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
46 47
        final String frag = config.getString("frag");
        final String name = config.getString("name");
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
48
        shaders.put(id, new GLShaderData(name, STATIC_VERT, frag));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        if (onCompile != null) {
            onCompileCallbacks.put(id, onCompile);
        }
    }

    @ReactMethod
    public void removeShader (final Integer id) {
        GLShaderData shader = shaders.remove(id);
        if (shader == null) {
            throw new Error("removeShader("+id+"): shader does not exist");
        }
    }

    public void shaderFailedToCompile(Integer id, GLShaderCompilationFailed e) {
        Callback onCompile = onCompileCallbacks.get(id);
        if (onCompile == null) {
            Log.e("RNGLContext", e.getMessage());
        }
        else {
            onCompile.invoke(e.compileError);
        }
    }

    public void shaderSucceedToCompile(Integer id, Map<String, Integer> uniformTypes) {
        Callback onCompile = onCompileCallbacks.get(id);
        onCompileCallbacks.remove(id);
        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 "";
107 108
    }
}