diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/jshader.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/jshader.js | 272 |
1 files changed, 131 insertions, 141 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/jshader.js b/js/helper-classes/RDGE/src/core/script/jshader.js index ceb67536..22180344 100755 --- a/js/helper-classes/RDGE/src/core/script/jshader.js +++ b/js/helper-classes/RDGE/src/core/script/jshader.js | |||
@@ -4,42 +4,44 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | // RDGE namespaces | ||
8 | var RDGE = RDGE || {}; | ||
9 | |||
7 | /* | 10 | /* |
8 | this API should be familiar to anyone who has worked with | 11 | this API should be familiar to anyone who has worked with HLSL effect files. |
9 | HLSL effect files. | ||
10 | */ | 12 | */ |
11 | 13 | ||
12 | /* | 14 | /* |
13 | * A map of types to uniform 'binding' functions | 15 | * A map of types to uniform 'binding' functions |
14 | */ | 16 | */ |
15 | bindMap={}; | 17 | RDGE.bindMap={}; |
16 | bindMap['int'] = function(ctx, a,b) { ctx.uniform1iv(a,b); }; | 18 | RDGE.bindMap['int'] = function(ctx, a,b) { ctx.uniform1iv(a,b); }; |
17 | bindMap['float'] = function(ctx, a,b) { ctx.uniform1fv(a,b); }; | 19 | RDGE.bindMap['float'] = function(ctx, a,b) { ctx.uniform1fv(a,b); }; |
18 | bindMap['vec2'] = function(ctx, a,b) { ctx.uniform2fv(a,b); }; | 20 | RDGE.bindMap['vec2'] = function(ctx, a,b) { ctx.uniform2fv(a,b); }; |
19 | bindMap['vec3'] = function(ctx, a,b) { ctx.uniform3fv(a,b); }; | 21 | RDGE.bindMap['vec3'] = function(ctx, a,b) { ctx.uniform3fv(a,b); }; |
20 | bindMap['vec4'] = function(ctx, a,b) { ctx.uniform4fv(a,b); }; | 22 | RDGE.bindMap['vec4'] = function(ctx, a,b) { ctx.uniform4fv(a,b); }; |
21 | bindMap['mat3'] = function(ctx, a,b) { ctx.uniformMatrix3fv(a,false,b); }; | 23 | RDGE.bindMap['mat3'] = function(ctx, a,b) { ctx.uniformMatrix3fv(a,false,b); }; |
22 | bindMap['mat4'] = function(ctx, a,b) | 24 | RDGE.bindMap['mat4'] = function(ctx, a,b) |
23 | { | 25 | { |
24 | ctx.uniformMatrix4fv(a,false,b); | 26 | ctx.uniformMatrix4fv(a,false,b); |
25 | g_Engine.getContext().debug.mat4CallCount++; | 27 | RDGE.globals.engine.getContext().debug.mat4CallCount++; |
26 | }; | 28 | }; |
27 | 29 | ||
28 | bindMap['tex2d'] = function(ctx, a,b) | 30 | RDGE.bindMap['tex2d'] = function(ctx, a,b) |
29 | { | 31 | { |
30 | ctx.activeTexture(ctx.TEXTURE0+b[0]); | 32 | ctx.activeTexture(ctx.TEXTURE0+b[0]); |
31 | ctx.bindTexture(ctx.TEXTURE_2D, b[1]); | 33 | ctx.bindTexture(ctx.TEXTURE_2D, b[1]); |
32 | ctx.uniform1iv(a,[b[0]]); | 34 | ctx.uniform1iv(a,[b[0]]); |
33 | }; | 35 | }; |
34 | 36 | ||
35 | bindMap['texCube']=function(ctx, a,b) | 37 | RDGE.bindMap['texCube']=function(ctx, a,b) |
36 | { | 38 | { |
37 | ctx.activeTexture(ctx.TEXTURE0+b[0]); | 39 | ctx.activeTexture(ctx.TEXTURE0+b[0]); |
38 | ctx.bindTexture(ctx.TEXTURE_CUBE_MAP, b[1]); | 40 | ctx.bindTexture(ctx.TEXTURE_CUBE_MAP, b[1]); |
39 | ctx.uniform1iv(a,[b[0]]); | 41 | ctx.uniform1iv(a,[b[0]]); |
40 | }; | 42 | }; |
41 | 43 | ||
42 | lightDataMap = | 44 | RDGE.lightDataMap = |
43 | [ | 45 | [ |
44 | function(ctx, loc, lightNode) { ctx.uniform3fv(loc, lightNode.position); }, | 46 | function(ctx, loc, lightNode) { ctx.uniform3fv(loc, lightNode.position); }, |
45 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightDiffuse); }, | 47 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightDiffuse); }, |
@@ -47,9 +49,9 @@ lightDataMap = | |||
47 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightSpecular); } | 49 | function(ctx, loc, lightNode) { ctx.uniform4fv(loc, lightNode.lightSpecular); } |
48 | ]; | 50 | ]; |
49 | 51 | ||
50 | paramTypeNameMapping = null; | 52 | RDGE.paramTypeNameMapping = null; |
51 | 53 | ||
52 | jshader = function(addr) { | 54 | RDGE.jshader = function (addr) { |
53 | this.name = addr; | 55 | this.name = addr; |
54 | this.def = null; | 56 | this.def = null; |
55 | this.technique = {}; | 57 | this.technique = {}; |
@@ -59,7 +61,7 @@ jshader = function(addr) { | |||
59 | this.currentPass = 0; | 61 | this.currentPass = 0; |
60 | this.type_jshader = {}; | 62 | this.type_jshader = {}; |
61 | this.global = {}; | 63 | this.global = {}; |
62 | this.renderer = g_Engine.getContext().renderer; | 64 | this.renderer = RDGE.globals.engine.getContext().renderer; |
63 | this.ctx = this.renderer.ctx; | 65 | this.ctx = this.renderer.ctx; |
64 | 66 | ||
65 | // load jshader definition at addr (if provided) | 67 | // load jshader definition at addr (if provided) |
@@ -71,32 +73,32 @@ jshader = function(addr) { | |||
71 | this.def = JSON.parse(request.responseText); | 73 | this.def = JSON.parse(request.responseText); |
72 | } | 74 | } |
73 | 75 | ||
74 | if (!paramTypeNameMapping) { | 76 | if (!RDGE.paramTypeNameMapping) { |
75 | var gl = this.ctx; | 77 | var gl = this.ctx; |
76 | paramTypeNameMapping = {}; | 78 | RDGE.paramTypeNameMapping = {}; |
77 | paramTypeNameMapping[gl.BOOL] = "bool"; | 79 | RDGE.paramTypeNameMapping[gl.BOOL] = "bool"; |
78 | paramTypeNameMapping[gl.INT] = "int"; | 80 | RDGE.paramTypeNameMapping[gl.INT] = "int"; |
79 | paramTypeNameMapping[gl.FLOAT] = "float"; | 81 | RDGE.paramTypeNameMapping[gl.FLOAT] = "float"; |
80 | paramTypeNameMapping[gl.FLOAT_VEC2] = "vec2"; | 82 | RDGE.paramTypeNameMapping[gl.FLOAT_VEC2] = "vec2"; |
81 | paramTypeNameMapping[gl.FLOAT_VEC3] = "vec3"; | 83 | RDGE.paramTypeNameMapping[gl.FLOAT_VEC3] = "vec3"; |
82 | paramTypeNameMapping[gl.FLOAT_VEC4] = "vec4"; | 84 | RDGE.paramTypeNameMapping[gl.FLOAT_VEC4] = "vec4"; |
83 | paramTypeNameMapping[gl.INT_VEC2] = "vec2"; | 85 | RDGE.paramTypeNameMapping[gl.INT_VEC2] = "vec2"; |
84 | paramTypeNameMapping[gl.INT_VEC3] = "vec3"; | 86 | RDGE.paramTypeNameMapping[gl.INT_VEC3] = "vec3"; |
85 | paramTypeNameMapping[gl.INT_VEC4] = "vec4"; | 87 | RDGE.paramTypeNameMapping[gl.INT_VEC4] = "vec4"; |
86 | paramTypeNameMapping[gl.BOOL_VEC2] = "vec2"; | 88 | RDGE.paramTypeNameMapping[gl.BOOL_VEC2] = "vec2"; |
87 | paramTypeNameMapping[gl.BOOL_VEC3] = "vec3"; | 89 | RDGE.paramTypeNameMapping[gl.BOOL_VEC3] = "vec3"; |
88 | paramTypeNameMapping[gl.BOOL_VEC4] = "vec4"; | 90 | RDGE.paramTypeNameMapping[gl.BOOL_VEC4] = "vec4"; |
89 | paramTypeNameMapping[gl.FLOAT_MAT2] = "mat2"; | 91 | RDGE.paramTypeNameMapping[gl.FLOAT_MAT2] = "mat2"; |
90 | paramTypeNameMapping[gl.FLOAT_MAT3] = "mat3"; | 92 | RDGE.paramTypeNameMapping[gl.FLOAT_MAT3] = "mat3"; |
91 | paramTypeNameMapping[gl.FLOAT_MAT4] = "mat4"; | 93 | RDGE.paramTypeNameMapping[gl.FLOAT_MAT4] = "mat4"; |
92 | paramTypeNameMapping[gl.SAMPLER_2D] = "tex2d"; | 94 | RDGE.paramTypeNameMapping[gl.SAMPLER_2D] = "tex2d"; |
93 | paramTypeNameMapping[gl.SAMPLER_CUBE] = "texCube"; | 95 | RDGE.paramTypeNameMapping[gl.SAMPLER_CUBE] = "texCube"; |
94 | } | 96 | } |
95 | 97 | ||
96 | /* | 98 | /* |
97 | * private helper functions | 99 | * private helper functions |
98 | */ | 100 | */ |
99 | this.bindParameters = function(pass) { | 101 | this.bindParameters = function (pass) { |
100 | var params = pass.defParamsList; // global parameters to start with | 102 | var params = pass.defParamsList; // global parameters to start with |
101 | var lightParams = pass.lightParams; | 103 | var lightParams = pass.lightParams; |
102 | var lightContext = pass.lightContext; | 104 | var lightContext = pass.lightContext; |
@@ -110,15 +112,15 @@ jshader = function(addr) { | |||
110 | if (params[idx].type == 'tex2d' || params[idx].type == 'texCube') { | 112 | if (params[idx].type == 'tex2d' || params[idx].type == 'texCube') { |
111 | texArg[0] = texUnit++; | 113 | texArg[0] = texUnit++; |
112 | texArg[1] = params[idx].data[0]; | 114 | texArg[1] = params[idx].data[0]; |
113 | bindMap[params[idx].type](this.ctx, params[idx].loc, texArg); | 115 | RDGE.bindMap[params[idx].type](this.ctx, params[idx].loc, texArg); |
114 | } | 116 | } |
115 | else { | 117 | else { |
116 | bindMap[params[idx].type](this.ctx, params[idx].loc, rdgeGlobalParameters[params[idx].name].data); | 118 | RDGE.bindMap[params[idx].type](this.ctx, params[idx].loc, RDGE.rdgeGlobalParameters[params[idx].name].data); |
117 | } | 119 | } |
118 | } | 120 | } |
119 | 121 | ||
120 | // light settings defined by the material | 122 | // light settings defined by the material |
121 | var len = rdgeConstants.MAX_MATERIAL_LIGHTS; | 123 | var len = RDGE.rdgeConstants.MAX_MATERIAL_LIGHTS; |
122 | for (var i = 0; i < len; ++i) { | 124 | 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 | 125 | // if there is a context for a light check to see if we have a binding to the light |
124 | if (lightContext[i] != null) { | 126 | if (lightContext[i] != null) { |
@@ -128,7 +130,7 @@ jshader = function(addr) { | |||
128 | var numParams = lightParams[i].length; | 130 | var numParams = lightParams[i].length; |
129 | for (var lp = 0; lp < numParams; ++lp) { | 131 | for (var lp = 0; lp < numParams; ++lp) { |
130 | // bind the parameters using the lightDataMap function lookup, dataIndex is the key | 132 | // 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]); | 133 | RDGE.lightDataMap[lightParams[i][lp].dataIndex](this.ctx, lightParams[i][lp].loc, lightContext[i]); |
132 | } | 134 | } |
133 | } | 135 | } |
134 | } | 136 | } |
@@ -142,10 +144,10 @@ jshader = function(addr) { | |||
142 | if (params[idx].type == 'tex2d' || params[idx].type == 'texCube') { | 144 | if (params[idx].type == 'tex2d' || params[idx].type == 'texCube') { |
143 | texArg[0] = texUnit++; | 145 | texArg[0] = texUnit++; |
144 | texArg[1] = params[idx].data[0]; | 146 | texArg[1] = params[idx].data[0]; |
145 | bindMap[params[idx].type](this.ctx, params[idx].loc, texArg); | 147 | RDGE.bindMap[params[idx].type](this.ctx, params[idx].loc, texArg); |
146 | } | 148 | } |
147 | else { | 149 | else { |
148 | bindMap[params[idx].type](this.ctx, params[idx].loc, params[idx].data); | 150 | RDGE.bindMap[params[idx].type](this.ctx, params[idx].loc, params[idx].data); |
149 | } | 151 | } |
150 | } | 152 | } |
151 | }; | 153 | }; |
@@ -153,7 +155,7 @@ jshader = function(addr) { | |||
153 | /* | 155 | /* |
154 | * helper function for setting up a texture | 156 | * helper function for setting up a texture |
155 | */ | 157 | */ |
156 | createJShaderTexture = function(ctx, param) { | 158 | createJShaderTexture = function (ctx, param) { |
157 | var texHandle = null; | 159 | var texHandle = null; |
158 | if (typeof param.data == "string") { | 160 | if (typeof param.data == "string") { |
159 | texHandle = ctx.canvas.renderer.getTextureByName(param.data, param.wrap, param.repeat, param.mips); | 161 | texHandle = ctx.canvas.renderer.getTextureByName(param.data, param.wrap, param.repeat, param.mips); |
@@ -163,9 +165,9 @@ jshader = function(addr) { | |||
163 | } |