aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/jshader.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/jshader.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/jshader.js750
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>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No 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/*
8this API should be familiar to anyone who has worked with
9HLSL effect files.
10*/
11
12/*
13 * A map of types to uniform 'binding' functions
14 */
15bindMap={};
16bindMap['int'] = function(ctx, a,b) { ctx.uniform1iv(a,b); };
17bindMap['float'] = function(ctx, a,b) { ctx.uniform1fv(a,b); };
18bindMap['vec2'] = function(ctx, a,b) { ctx.uniform2fv(a,b); };
19bindMap['vec3'] = function(ctx, a,b) { ctx.uniform3fv(a,b); };
20bindMap['vec4'] = function(ctx, a,b) { ctx.uniform4fv(a,b); };
21bindMap['mat3'] = function(ctx, a,b) { ctx.uniformMatrix3fv(a,false,b); };
22bindMap['mat4'] = function(ctx, a,b)
23{
24 ctx.uniformMatrix4fv(a,false,b);
25 g_Engine.getContext().debug.mat4CallCount++;
26};
27
28bindMap['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
35bindMap['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
42lightDataMap =
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
50paramTypeNameMapping = null;
51
52jshader = 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') {
213 if (typeof v == "string") {
214 v = ctx.canvas.renderer.getTextureByName