# all files / src/gpu/ GLProgram.js

76.88% Statements 123/160
65.22% Branches 45/69
92.31% Functions 12/13
76.88% Lines 123/160

    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
    47
    48
    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
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325

    1×
    1×
    1×

    1×

    48×

    1×

    1×

    24×

    24×

    24×

    66×
    66×

    7×
    7×

    103×
    103×

    103×
    102×

    1×

    303×

    76×

    103×

    103×

    103×

    103×

    884×
    884×

    884×
    290×

    594×

    232×

    232×
    3712×

    232×

    232×
    232×

    39×
    39×

    50×
    50×

    19×
    19×

    100×
    100×

    114×
    114×

    8×
    8×

    6×
    6×

    2×
    2×

    22×
    22×

    2×
    2×
    2×
    2×
    2×
    2×
    32×

    2×

    2×

    594×

    455×
    455×
    11×

    444×

    36×
    36×

    36×

    36×
    35×

    1×

    36×

    36×
    35×

    1×

    36×
    36×
    84×
    84×

    84×
    84×
    52×

    52×
    6×
    6×

    46×

    78×

    78×
    78×

    36×
    116×

    78×
    78×
    78×

    36×

    24×
    24×

    24×
    24×

    24×
    24×
    24×

    24×
    24×

    24×
    24×

    24×
    24×

    24×
    24×

    24×

    24×

    24×
    842×
    842×

    24×
    24×

    24×

    24×
    24×

     import vendor from '../core/vendor';
    import Base from '../core/Base';

    var SHADER_STATE_TO_ENABLE = 1;
    var SHADER_STATE_KEEP_ENABLE = 2;
    var SHADER_STATE_PENDING = 3;

    // Enable attribute operation is global to all programs
    // Here saved the list of all enabled attribute index
    // http://www.mjbshaw.com/2013/03/webgl-fixing-invalidoperation.html
    var enabledAttributeList = {};

    // some util functions
    function addLineNumbers(string) {
        var chunks = string.split('\n');
        for (var i = 0, il = chunks.length; i < il; i ++) {
            // Chrome reports shader errors on lines
            // starting counting from 1
            chunks[i] = (i + 1) + ': ' + chunks[i];
        }
        return chunks.join('\n');
    }

    // Return true or error msg if error happened
    function checkShaderErrorMsg(_gl, shader, shaderString) {
        Iif (!_gl.getShaderParameter(shader, _gl.COMPILE_STATUS)) {
            return [_gl.getShaderInfoLog(shader), addLineNumbers(shaderString)].join('\n');
        }
    }

    var tmpFloat32Array16 = new vendor.Float32Array(16);

    var GLProgram = Base.extend({

        uniformSemantics: {},
        attributes: {}

    }, function () {
        this._locations = {};

        this._textureSlot = 0;

        this._program = null;
    }, {

        bind: function (renderer) {
            this._textureSlot = 0;
            renderer.gl.useProgram(this._program);
        },

        hasUniform: function (symbol) {
            var location = this._locations[symbol];
            return location !== null && location !== undefined;
        },

        useTextureSlot: function (renderer, texture, slot) {
            Eif (texture) {
                renderer.gl.activeTexture(renderer.gl.TEXTURE0 + slot);
                // Maybe texture is not loaded yet;
                if (texture.isRenderable()) {
                    texture.bind(renderer);
                }
                else {
                    // Bind texture to null
                    texture.unbind(renderer);
                }
            }
        },

        currentTextureSlot: function () {
            return this._textureSlot;
        },

        resetTextureSlot: function (slot) {
            this._textureSlot = slot || 0;
        },

        takeCurrentTextureSlot: function (renderer, texture) {
            var textureSlot = this._textureSlot;

            this.useTextureSlot(renderer, texture, textureSlot);

            this._textureSlot++;

            return textureSlot;
        },

        setUniform: function (_gl, type, symbol, value) {
            var locationMap = this._locations;
            var location = locationMap[symbol];
            // Uniform is not existed in the shader
            if (location === null || location === undefined) {
                return false;
            }

            switch (type) {
                case 'm4':
                    Eif (!(value instanceof Float32Array)) {
                        // Use Float32Array is much faster than array when uniformMatrix4fv.
                        for (var i = 0; i < value.length; i++) {
                            tmpFloat32Array16[i] = value[i];
                        }
                        value = tmpFloat32Array16;
                    }
                    _gl.uniformMatrix4fv(location, false, value);
                    break;
                case '2i':
                    _gl.uniform2i(location, value[0], value[1]);
                    break;
                case '2f':
                    _gl.uniform2f(location, value[0], value[1]);
                    break;
                case '3i':
                    _gl.uniform3i(location, value[0], value[1], value[2]);
                    break;
                case '3f':
                    _gl.uniform3f(location, value[0], value[1], value[2]);
                    break;
                case '4i':
                    _gl.uniform4i(location, value[0], value[1], value[2], value[3]);
                    break;
                case '4f':
                    _gl.uniform4f(location, value[0], value[1], value[2], value[3]);
                    break;
                case '1i':
                    _gl.uniform1i(location, value);
                    break;
                case '1f':
                    _gl.uniform1f(location, value);
                    break;
                case '1fv':
                    _gl.uniform1fv(location, value);
                    break;
                case '1iv':
                    _gl.uniform1iv(location, value);
                    break;
                case '2iv':
                    _gl.uniform2iv(location, value);
                    break;
                case '2fv':
                    _gl.uniform2fv(location, value);
                    break;
                case '3iv':
                    _gl.uniform3iv(location, value);
                    break;
                case '3fv':
                    _gl.uniform3fv(location, value);
                    break;
                case '4iv':
                    _gl.uniform4iv(location, value);
                    break;
                case '4fv':
                    _gl.uniform4fv(location, value);
                    break;
                case 'm2':
                case 'm2v':
                    _gl.uniformMatrix2fv(location, false, value);
                    break;
                case 'm3':
                case 'm3v':
                    _gl.uniformMatrix3fv(location, false, value);
                    break;
                case 'm4v':
                    // Raw value
                    Eif (Array.isArray(value) && Array.isArray(value[0])) {
                        var array = new vendor.Float32Array(value.length * 16);
                        var cursor = 0;
                        for (var i = 0; i < value.length; i++) {
                            var item = value[i];
                            for (var j = 0; j < 16; j++) {
                                array[cursor++] = item[j];
                            }
                        }
                        _gl.uniformMatrix4fv(location, false, array);
                    }
                    else {   // ArrayBufferView
                        _gl.uniformMatrix4fv(location, false, value);
                    }
                    break;
            }
            return true;
        },

        setUniformOfSemantic: function (_gl, semantic, val) {
            var semanticInfo = this.uniformSemantics[semantic];
            if (semanticInfo) {
                return this.setUniform(_gl, semanticInfo.type, semanticInfo.symbol, val);
            }
            return false;
        },

        // Used for creating VAO
        // Enable the attributes passed in and disable the rest
        // Example Usage:
        // enableAttributes(renderer, ["position", "texcoords"])
        enableAttributes: function (renderer, attribList, vao) {
            var _gl = renderer.gl;
            var program = this._program;

            var locationMap = this._locations;

            var enabledAttributeListInContext;
            if (vao) {
                enabledAttributeListInContext = vao.__enabledAttributeList;
            }
            else {
                enabledAttributeListInContext = enabledAttributeList[renderer.__uid__];
            }
            Eif (!enabledAttributeListInContext) {
                // In vertex array object context
                // PENDING Each vao object needs to enable attributes again?
                if (vao) {
                    enabledAttributeListInContext
                        = vao.__enabledAttributeList
                        = [];
                }
                else {
                    enabledAttributeListInContext
                        = enabledAttributeList[renderer.__uid__]
                        = [];
                }
            }
            var locationList = [];
            for (var i = 0; i < attribList.length; i++) {
                var symbol = attribList[i];
                Iif (!this.attributes[symbol]) {
                    locationList[i] = -1;
                    continue;
                }
                var location = locationMap[symbol];
                if (location == null) {
                    location = _gl.getAttribLocation(program, symbol);
                    // Attrib location is a number from 0 to ...
                    if (location === -1) {
                        locationList[i] = -1;
                        continue;
                    }
                    locationMap[symbol] = location;
                }
                locationList[i] = location;

                Eif (!enabledAttributeListInContext[location]) {
                    enabledAttributeListInContext[location] = SHADER_STATE_TO_ENABLE;
                }
                else {
                    enabledAttributeListInContext[location] = SHADER_STATE_KEEP_ENABLE;
                }
            }

            for (var i = 0; i < enabledAttributeListInContext.length; i++) {
                switch(enabledAttributeListInContext[i]){
                    case SHADER_STATE_TO_ENABLE:
                        _gl.enableVertexAttribArray(i);
                        enabledAttributeListInContext[i] = SHADER_STATE_PENDING;
                        break;
                    case SHADER_STATE_KEEP_ENABLE:
                        enabledAttributeListInContext[i] = SHADER_STATE_PENDING;
                        break;
                    // Expired
                    case SHADER_STATE_PENDING:
                        _gl.disableVertexAttribArray(i);
                        enabledAttributeListInContext[i] = 0;
                        break;
                }
            }

            return locationList;
        },

        buildProgram: function (_gl, shader, vertexShaderCode, fragmentShaderCode) {
            var vertexShader = _gl.createShader(_gl.VERTEX_SHADER);
            var program = _gl.createProgram();

            _gl.shaderSource(vertexShader, vertexShaderCode);
            _gl.compileShader(vertexShader);

            var fragmentShader = _gl.createShader(_gl.FRAGMENT_SHADER);
            _gl.shaderSource(fragmentShader, fragmentShaderCode);
            _gl.compileShader(fragmentShader);

            var msg = checkShaderErrorMsg(_gl, vertexShader, vertexShaderCode);
            Iif (msg) {
                return msg;
            }
            msg = checkShaderErrorMsg(_gl, fragmentShader, fragmentShaderCode);
            Iif (msg) {
                return msg;
            }

            _gl.attachShader(program, vertexShader);
            _gl.attachShader(program, fragmentShader);
            // Force the position bind to location 0;
            Eif (shader.attributeSemantics['POSITION']) {
                _gl.bindAttribLocation(program, 0, shader.attributeSemantics['POSITION'].symbol);
            }
            else {
                // Else choose an attribute and bind to location 0;
                var keys = Object.keys(this.attributes);
                _gl.bindAttribLocation(program, 0, keys[0]);
            }

            _gl.linkProgram(program);

            Iif (!_gl.getProgramParameter(program, _gl.LINK_STATUS)) {
                return 'Could not link program\n' + 'VALIDATE_STATUS: ' + _gl.getProgramParameter(program, _gl.VALIDATE_STATUS) + ', gl error [' + _gl.getError() + ']';
            }

            // Cache uniform locations
            for (var i = 0; i < shader.uniforms.length; i++) {
                var uniformSymbol = shader.uniforms[i];
                this._locations[uniformSymbol] = _gl.getUniformLocation(program, uniformSymbol);
            }

            _gl.deleteShader(vertexShader);
            _gl.deleteShader(fragmentShader);

            this._program = program;

            // Save code.
            this.vertexCode = vertexShaderCode;
            this.fragmentCode = fragmentShaderCode;
        }
    });

    export default GLProgram;