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
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/


/**
 * @class DaliShader
 * Layer between DreemGL and Dali. Although Dali has a complete api, this
 * class is used to encapsulate the api to simplify how it is embedded into
 * the dreemgl dali platform.
 *
 * Each instance of DaliShader contains a dali.Shader object, and is extended
 * with information about the uniforms and attributes used by the shader.
 */

/**
 * @property dalishader
 * dali.Shader object
 */

define.class(function(require, exports){
        // internal, DaliApi is a static object to access the dali api
        DaliApi = require('./dali_api')

        // Assign a unique id to each dalishader object
        var DaliShader = exports
        DaliShader.GlobalId = 0

        /**
         * @method constructor
         * Create a dali.Shader object
         * You can access the dali.Shader object as this.dalishader
         * @param {string} vertexShader VertexShader code
         * @param {string} fragmentShader FragmentShader code
         */
        this.atConstructor = function(vertexShader, fragmentShader) {
                this.object_type = 'DaliShader'

                var vs = vertexShader;
                var fs = fragmentShader;

                var shaderOptions = {
            vertexShader : vs,
            fragmentShader: fs
        };

                var dali = DaliApi.dali;
                this.id = ++DaliShader.GlobalId;
                this.dalishader = new dali.Shader(shaderOptions);
                this.vertexshader = vertexShader;
                this.fragmentShader = fragmentShader;

                if (DaliApi.emitcode) {
                        // Simplify the shader by removing comments and empty lines
                        vs = this.trimShader(vs);
                        fs = this.trimShader(fs);

                        // #extension lines must have a \n at the end to compile
                        vs = vs.replace(/(\#extension.*)\n/g, '$1\\n\\\n');
                        fs = fs.replace(/(\#extension.*)\n/g, '$1\\n\\\n');

                        // Each line needs a separate DALICODE statement
                        vs = vs.replace(/\n/g, "\nDALICODE: ");
                        fs = fs.replace(/\n/g, "\nDALICODE: ");

                        console.log('DALICODE: var vertexShader' + this.id + ' = "' + vs + '"');
                        console.log('DALICODE: var fragmentShader' + this.id + ' = "' + fs + '"');
                        console.log('DALICODE: var shaderOptions' + this.id + ' = {vertexShader : vertexShader' + this.id + ', fragmentShader: fragmentShader' + this.id + ' };');
                        console.log('DALICODE: var ' + this.name() + ' = new dali.Shader(shaderOptions' + this.id + ');');
                }

        }

    // Internal method to remove comments and empty lines from a shader. This
    // is to prevent issues when the dalicode is replayed.
    this.trimShader = function(code) {
                var str = code;

        // Remove blank lines
        str = str.replace(/\n\n/g, "\\n");

                // Remove comments
                //var str = code.replace(//*.+?*/|//.*(?=[nr])/g, '');
                str = str.replace(/\/\/.*\n/g, '');

                // Remove trailing new line
                if (str[str.length-1] == "\n")
                        str = str.substring(0, str.length-1);

        // Remove new lines
                str = str.replace(/\n/g, "\\n");

                // Create a multi-line string
                str = str.replace(/\\n/g, "\\\n");

                // Add an additional newline for #extension (or else it won't compile)
                str = str.replace(/(\#extension.*)\\\n/g, '$1\n\\\n');

        return str;
}


        this.name = function() {
                return 'dalishader' + this.id;
        }

        this.inspect = function(depth) {
                var obj = {daliShader:this.id, vertex:this.vertexshader.length, fragment:this.fragmentShader.length};
                var util = require('util')
                return util.inspect(obj, {depth: null});
        }

});