Skip to content

Commit 893453f

Browse files
committed
New uniforms handler, new cache and dirty checks
1 parent 3373430 commit 893453f

1 file changed

Lines changed: 130 additions & 23 deletions

File tree

src/renderer/webgl/pipelines/MeshPipeline.js

Lines changed: 130 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,46 @@ var MeshPipeline = new Class({
6565
location: -1
6666
}
6767
]);
68+
config.uniforms = GetFastValue(config, 'uniforms', [
69+
'uViewProjectionMatrix',
70+
'uLightPosition',
71+
'uLightAmbient',
72+
'uLightDiffuse',
73+
'uLightSpecular',
74+
'uCameraPosition',
75+
'uFogColor',
76+
'uFogNear',
77+
'uFogFar',
78+
'uModelMatrix',
79+
'uNormalMatrix',
80+
'uMaterialAmbient',
81+
'uMaterialDiffuse',
82+
'uMaterialSpecular',
83+
'uMaterialShine',
84+
'uTexture'
85+
]);
6886

6987
WebGLPipeline.call(this, config);
7088

7189
this.forceZero = true;
90+
91+
// Cache structure:
92+
93+
// 0 fog near
94+
// 1 fog far
95+
// 2, 3, 4 model material ambient
96+
// 5, 6, 7 model material diffuse
97+
// 8, 9, 10 model material specular
98+
// 11 model material shine
99+
100+
this.dirtyCache = [
101+
-1,
102+
0,
103+
0, 0, 0,
104+
0, 0, 0,
105+
0, 0, 0,
106+
0
107+
];
72108
},
73109

74110
/**
@@ -113,44 +149,115 @@ var MeshPipeline = new Class({
113149
*/
114150
onBind: function (mesh)
115151
{
116-
var program = this.program;
117-
var renderer = this.renderer;
118-
119152
var camera = mesh.camera;
153+
154+
if (camera.dirty)
155+
{
156+
this.setMatrix4fv('uViewProjectionMatrix', false, camera.viewProjectionMatrix.val);
157+
158+
this.set3f('uCameraPosition', camera.x, camera.y, camera.z);
159+
}
160+
120161
var light = mesh.light;
121162

122-
renderer.setMatrix4(program, 'uViewProjectionMatrix', false, camera.viewProjectionMatrix.val);
163+
if (light.isDirty())
164+
{
165+
this.set3f('uLightPosition', light.x, light.y, light.z);
166+
}
167+
168+
if (light.ambient.dirty)
169+
{
170+
this.set3f('uLightAmbient', light.ambient.x, light.ambient.y, light.ambient.z);
171+
}
172+
173+
if (light.diffuse.dirty)
174+
{
175+
this.set3f('uLightDiffuse', light.diffuse.x, light.diffuse.y, light.diffuse.z);
176+
}
177+
178+
if (light.specular.dirty)
179+
{
180+
this.set3f('uLightSpecular', light.specular.x, light.specular.y, light.specular.z);
181+
}
182+
183+
if (mesh.fogColor.dirty)
184+
{
185+
this.set3f('uFogColor', mesh.fogColor.r, mesh.fogColor.g, mesh.fogColor.b);
186+
}
187+
188+
var cache = this.dirtyCache;
189+
var fogNear = mesh.fogNear;
190+
var fogFar = mesh.fogFar;
191+
192+
if (cache[0] !== fogNear)
193+
{
194+
this.set1f('uFogNear', fogNear);
123195

124-
renderer.setFloat3(program, 'uLightPosition', light.x, light.y, light.z);
125-
renderer.setFloat3(program, 'uLightAmbient', light.ambient.x, light.ambient.y, light.ambient.z);
126-
renderer.setFloat3(program, 'uLightDiffuse', light.diffuse.x, light.diffuse.y, light.diffuse.z);
127-
renderer.setFloat3(program, 'uLightSpecular', light.specular.x, light.specular.y, light.specular.z);
196+
cache[0] = fogNear;
197+
}
128198

129-
renderer.setFloat3(program, 'uCameraPosition', camera.x, camera.y, camera.z);
199+
if (cache[1] !== fogFar)
200+
{
201+
this.set1f('uFogFar', fogFar);
130202

131-
renderer.setFloat3(program, 'uFogColor', mesh.fogColor.r, mesh.fogColor.g, mesh.fogColor.b);
132-
renderer.setFloat1(program, 'uFogNear', mesh.forNear);
133-
renderer.setFloat1(program, 'uFogFar', mesh.forFar);
203+
cache[1] = fogFar;
204+
}
205+
206+
this.set1i('uTexture', 0);
134207
},
135208

136209
drawModel: function (mesh, model)
137210
{
138-
var program = this.program;
139-
var renderer = this.renderer;
211+
var cache = this.dirtyCache;
140212

141-
renderer.setMatrix4(program, 'uModelMatrix', false, model.transformMatrix.val);
142-
renderer.setMatrix4(program, 'uNormalMatrix', false, model.normalMatrix.val);
213+
this.setMatrix4fv('uModelMatrix', false, model.transformMatrix.val);
214+
this.setMatrix4fv('uNormalMatrix', false, model.normalMatrix.val);
143215

144-
renderer.setFloat3(program, 'uMaterialAmbient', model.ambient.x, model.ambient.y, model.ambient.z);
145-
renderer.setFloat3(program, 'uMaterialDiffuse', model.diffuse.x, model.diffuse.y, model.diffuse.z);
146-
renderer.setFloat3(program, 'uMaterialSpecular', model.specular.x, model.specular.y, model.specular.z);
147-
renderer.setFloat1(program, 'uMaterialShine', model.shine);
216+
var ambient = model.ambient;
148217

149-
renderer.setTextureZero(model.frame.glTexture);
150-
renderer.setInt1(program, 'uTexture', 0);
218+
if (!ambient.equals(cache[2], cache[3], cache[4]))
219+
{
220+
this.set3f('uMaterialAmbient', ambient.r, ambient.g, ambient.b);
151221

152-
// All the uniforms are finally bound, so let's buffer our data
222+
cache[2] = ambient.r;
223+
cache[3] = ambient.g;
224+
cache[4] = ambient.b;
225+
}
226+
227+
var diffuse = model.diffuse;
228+
229+
if (!diffuse.equals(cache[5], cache[6], cache[7]))
230+
{
231+
this.set3f('uMaterialDiffuse', diffuse.r, diffuse.g, diffuse.b);
232+
233+
cache[5] = diffuse.r;
234+
cache[6] = diffuse.g;
235+
cache[7] = diffuse.b;
236+
}
237+
238+
var specular = model.specular;
239+
240+
if (!specular.equals(cache[8], cache[9], cache[10]))
241+
{
242+
this.set3f('uMaterialSpecular', specular.r, specular.g, specular.b);
153243

244+
cache[8] = specular.r;
245+
cache[9] = specular.g;
246+
cache[10] = specular.b;
247+
}
248+
249+
var shine = model.shine;
250+
251+
if (!shine !== cache[11])
252+
{
253+
this.set1f('uMaterialShine', shine);
254+
255+
cache[11] = specular.b;
256+
}
257+
258+
this.renderer.setTextureZero(model.frame.glTexture);
259+
260+
// All the uniforms are finally bound, so let's buffer our data
154261
var gl = this.gl;
155262

156263
// STATIC because the buffer data doesn't change, the uniforms do

0 commit comments

Comments
 (0)