diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/jshader.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/jshader.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/jshader.js | 750 |
1 files changed, 750 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/jshader.js b/js/helper-classes/RDGE/src/core/script/jshader.js new file mode 100644 index 00000000..f28219cf --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/jshader.js | |||
@@ -0,0 +1,750 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /* | ||
8 | this API should be familiar to anyone who has worked with | ||
9 | HLSL effect files. | ||
10 | */ | ||
11 | |||
12 | /* | ||
13 | * A map of types to uniform 'binding' functions | ||
14 | */ | ||
15 | bindMap={}; | ||
16 | bindMap['int'] = function(ctx, a,b) { ctx.uniform1iv(a,b); }; | ||
17 | bindMap['float'] = function(ctx, a,b) { ctx.uniform1fv(a,b); }; | ||
18 | bindMap['vec2'] = function(ctx, a,b) { ctx.uniform2fv(a,b); }; | ||
19 | bindMap['vec3'] = function(ctx, a,b) { ctx.uniform3fv(a,b); }; | ||
20 | bindMap['vec4'] = function(ctx, a,b) { ctx.uniform4fv(a,b); }; | ||
21 | bindMap['mat3'] = function(ctx, a,b) { ctx.uniformMatrix3fv(a,false,b); }; | ||
22 | bindMap['mat4'] = function(ctx, a,b) | ||
23 | { | ||
24 | ctx.uniformMatrix4fv(a,false,b); | ||
25 | g_Engine.getContext().debug.mat4CallCount++; | ||
26 | }; | ||
27 | |||
28 | bindMap['tex2d'] = function(ctx, a,b) | ||
29 | { | ||
30 | ctx.activeTexture(ctx.TEXTURE0+b[0]); | ||
31 | ctx.bindTexture(ctx.TEXTURE_2D, b[1]); | ||
32 | ctx.uniform1iv(a,[b[0]]); | ||
33 | }; | ||
34 | |||
35 | bindMap['texCube']=function(ctx, a,b) | ||
36 | { | ||
37 | ctx.activeTexture(ctx.TEXTURE0+b[0]); | ||
38 | ctx.bindTexture(ctx.TEXTURE_CUBE_MAP, b[1]); | ||
39 | ctx.uniform1iv(a,[b[0]]); | ||
40 | }; | ||
41 | |||
42 | lightDataMap = | ||
43 | [ | ||
44 | function(ctx, loc, lightNode) { ctx.uniform3fv(loc, lightNode.position); }, | ||
45 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightDiffuse); }, | ||
46 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightAmbient); }, | ||
47 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightSpecular); } | ||
48 | ]; | ||
49 | |||
50 | paramTypeNameMapping = null; | ||
51 | |||
52 | jshader = function(addr) { | ||
53 | this.name = addr; | ||
54 | this.def = null; | ||
55 | this.technique = {}; | ||
56 | this.params = {}; | ||
57 | this.compiledShaders = {}; | ||
58 | this.resetRS = false; | ||
59 | this.currentPass = 0; | ||
60 | this.type_jshader = {}; | ||
61 | this.global = {}; | ||
62 | this.renderer = g_Engine.getContext().renderer; | ||
63 | this.ctx = this.renderer.ctx; | ||
64 | |||
65 | // load jshader definition at addr (if provided) | ||
66 | if (addr != undefined && addr != null) { | ||
67 | // a synchronous ajax request | ||
68 | request = new XMLHttpRequest(); | ||
69 | request.open("GET", addr, false); | ||
70 | request.send(null); | ||
71 | this.def = JSON.parse(request.responseText); | ||
72 | } | ||
73 | |||
74 | if (!paramTypeNameMapping) { | ||
75 | var gl = this.ctx; | ||
76 | paramTypeNameMapping = {}; | ||
77 | paramTypeNameMapping[gl.BOOL] = "bool"; | ||
78 | paramTypeNameMapping[gl.INT] = "int"; | ||
79 | paramTypeNameMapping[gl.FLOAT] = "float"; | ||
80 | paramTypeNameMapping[gl.FLOAT_VEC2] = "vec2"; | ||
81 | paramTypeNameMapping[gl.FLOAT_VEC3] = "vec3"; | ||
82 | paramTypeNameMapping[gl.FLOAT_VEC4] = "vec4"; | ||
83 | paramTypeNameMapping[gl.INT_VEC2] = "vec2"; | ||
84 | paramTypeNameMapping[gl.INT_VEC3] = "vec3"; | ||
85 | paramTypeNameMapping[gl.INT_VEC4] = "vec4"; | ||
86 | paramTypeNameMapping[gl.BOOL_VEC2] = "vec2"; | ||
87 | paramTypeNameMapping[gl.BOOL_VEC3] = "vec3"; | ||
88 | paramTypeNameMapping[gl.BOOL_VEC4] = "vec4"; | ||
89 | paramTypeNameMapping[gl.FLOAT_MAT2] = "mat2"; | ||
90 | paramTypeNameMapping[gl.FLOAT_MAT3] = "mat3"; | ||
91 | paramTypeNameMapping[gl.FLOAT_MAT4] = "mat4"; | ||
92 | paramTypeNameMapping[gl.SAMPLER_2D] = "tex2d"; | ||
93 | paramTypeNameMapping[gl.SAMPLER_CUBE] = "texCube"; | ||
94 | } | ||
95 | |||
96 | /* | ||
97 | * private helper functions | ||
98 | */ | ||
99 | this.bindParameters = function(pass) { | ||
100 | var params = pass.defParamsList; // global parameters to start with | ||
101 | var lightParams = pass.lightParams; | ||
102 | var lightContext = pass.lightContext; | ||
103 | var length = params.length; | ||
104 | var idx = 0; | ||
105 | var texArg = new Array(2) | ||
106 | |||
107 | // global parameters | ||
108 | var texUnit = 0; | ||
109 | for (idx = 0; idx < length; ++idx) { | ||
110 | if (params[idx].type == 'tex2d' || params[idx].type == 'texCube') { | ||
111 | texArg[0] = texUnit++; | ||
112 | texArg[1] = params[idx].data[0]; | ||
113 | bindMap[params[idx].type](this.ctx, params[idx].loc, texArg); | ||
114 | } | ||
115 | else { | ||
116 | bindMap[params[idx].type](this.ctx, params[idx].loc, rdgeGlobalParameters[params[idx].name].data); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | // light settings defined by the material | ||
121 | var len = rdgeConstants.MAX_MATERIAL_LIGHTS; | ||
122 | for (var i = 0; i < len; ++i) { | ||
123 | // if there is a context for a light check to see if we have a binding to the light | ||
124 | if (lightContext[i] != null) { | ||
125 | // see if we have parameters to bind to this light | ||
126 | if (lightParams[i]) { | ||
127 | // something is here lets bind it | ||
128 | var numParams = lightParams[i].length; | ||
129 | for (var lp = 0; lp < numParams; ++lp) { | ||
130 | // bind the parameters using the lightDataMap function lookup, dataIndex is the key | ||
131 | lightDataMap[lightParams[i][lp].dataIndex](this.ctx, lightParams[i][lp].loc, lightContext[i]); | ||
132 | } | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | |||
137 | // let locally defined uniforms stomp globally defined uniforms | ||
138 | texUnit = this.renderer.usedTextureUnits; // start adding texture after the default textures | ||
139 | params = pass.paramsList; | ||
140 | length = params.length; | ||
141 | for (idx = 0; idx < length; ++idx) { | ||
142 | if (params[idx].type == 'tex2d' || params[idx].type == 'texCube') { | ||
143 | texArg[0] = texUnit++; | ||
144 | texArg[1] = params[idx].data[0]; | ||
145 | bindMap[params[idx].type](this.ctx, params[idx].loc, texArg); | ||
146 | } | ||
147 | else { | ||
148 | bindMap[params[idx].type](this.ctx, params[idx].loc, params[idx].data); | ||
149 | } | ||
150 | } | ||
151 | }; | ||
152 | |||
153 | /* | ||
154 | * helper function for setting up a texture | ||
155 | */ | ||
156 | createJShaderTexture = function(ctx, param) { | ||
157 | var texHandle = null; | ||
158 | if (typeof param.data == "string") { | ||
159 | texHandle = ctx.canvas.renderer.getTextureByName(param.data, param.wrap, param.repeat, param.mips); | ||
160 | } | ||
161 | else { | ||
162 | texHandle = ctx.canvas.renderer.getTextureByName(param.data.lookUpName, param.wrap, param.repeat, param.mips); | ||
163 | } | ||
164 | |||
165 | return [texHandle]; | ||
166 | } | ||
167 | |||
168 | paramType = function(ctx, name, def, program, technique) { | ||
169 | var texUnit = 0; | ||
170 | |||
171 | // Get the uniform location and store it | ||
172 | this.loc = ctx.getUniformLocation(program, name); | ||
173 | |||
174 | // if the parameter does not exist in the shader cull it from the pass | ||
175 | if (this.loc == null) { | ||
176 | window.console.log("ctx:" + ctx.canvas.id + ", technique: " + technique + ", uniform: " + name + " was not found, jshader param will have no affect"); | ||
177 | //return; | ||
178 | } | ||
179 | |||
180 | var param = def[name]; | ||
181 | this.type = param.type; | ||
182 | |||
183 | // if data was not provided then create default data | ||
184 | if (param.data == undefined) { | ||
185 | switch (param.type) { | ||
186 | case "vec4": this.data = vec4.zero(); break; | ||
187 | case "vec3": this.data = vec3.zero(); break; | ||
188 | case "vec2": this.data = vec2.zero(); break; | ||
189 | case "mat4": this.data = mat4.zero(); break; | ||
190 | case "mat3": this.data = new Array(9); break; | ||
191 | case "mat2": this.data = [0, 0, 0, 0]; break; | ||
192 | case "float": this.data = [0]; break; | ||
193 | case "int": this.data = [0]; break; | ||
194 | case "tex2d": this.data = [ctx.canvas.renderer.getTextureByName("assets/images/white.png")]; break; | ||
195 | case "texCube": this.data = [ctx.canvas.renderer.getTextureByName("assets/images/white.png")]; break; | ||
196 | } | ||
197 | } | ||
198 | else { | ||
199 | if (param.type == 'tex2d' || param.type == 'texCube') { | ||
200 | this.data = createJShaderTexture(ctx, param); | ||
201 | } | ||
202 | else { | ||
203 | this.data = param.data.slice(); | ||
204 | } | ||
205 | } | ||
206 | |||
207 | this.get = function() { | ||
208 | return this.data.slice(); | ||
209 | } | ||
210 | |||
211 | this.set = function(v) { | ||
212 | if (this.type == 'tex2d' || this.type == 'texCube') { | ||