Skip to content

Commit eeabbfe

Browse files
committed
added caching of uniform locations
1 parent 483aab4 commit eeabbfe

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/main.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,18 @@ function addImmediateMode() {
154154
uniform float pointSize;\
155155
varying vec4 color;\
156156
varying vec4 coord;\
157-
varying vec2 pixel;\
158157
void main() {\
159158
color = gl_Color;\
160159
coord = gl_TexCoord;\
161160
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\
162-
pixel = gl_Position.xy / gl_Position.w * 0.5 + 0.5;\
163161
gl_PointSize = pointSize;\
164162
}\
165163
', '\
166164
uniform sampler2D texture;\
167165
uniform float pointSize;\
168166
uniform bool useTexture;\
169-
uniform vec2 windowSize;\
170167
varying vec4 color;\
171168
varying vec4 coord;\
172-
varying vec2 pixel;\
173169
void main() {\
174170
gl_FragColor = color;\
175171
if (useTexture) gl_FragColor *= texture2D(texture, coord.xy);\
@@ -201,7 +197,6 @@ function addImmediateMode() {
201197
if (immediateMode.mode == -1) throw 'mismatched gl.begin() and gl.end() calls';
202198
immediateMode.mesh.compile();
203199
immediateMode.shader.uniforms({
204-
windowSize: [gl.canvas.width, gl.canvas.height],
205200
useTexture: !!gl.getParameter(gl.TEXTURE_BINDING_2D)
206201
}).draw(immediateMode.mesh, immediateMode.mode);
207202
immediateMode.mode = -1;
@@ -381,11 +376,11 @@ function addOtherMethods() {
381376
window.mozRequestAnimationFrame ||
382377
window.webkitRequestAnimationFrame ||
383378
function(callback) { setTimeout(callback, 1000 / 60); };
384-
var time = new Date();
379+
var time = new Date().getTime();
385380
var context = gl;
386381
function update() {
387382
gl = context;
388-
var now = new Date();
383+
var now = new Date().getTime();
389384
if (gl.onupdate) gl.onupdate((now - time) / 1000);
390385
if (gl.ondraw) gl.ondraw();
391386
post(update);

src/shader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function Shader(vertexSource, fragmentSource) {
111111
throw 'link error: ' + gl.getProgramInfoLog(this.program);
112112
}
113113
this.attributes = {};
114+
this.uniformLocations = {};
114115

115116
// Sampler uniforms need to be uploaded using `gl.uniform1i()` instead of `gl.uniform1f()`.
116117
// To do this automatically, we detect and remember all uniform samplers in the source code.
@@ -143,8 +144,9 @@ Shader.prototype = {
143144
gl.useProgram(this.program);
144145

145146
for (var name in uniforms) {
146-
var location = gl.getUniformLocation(this.program, name);
147+
var location = this.uniformLocations[name] || gl.getUniformLocation(this.program, name);
147148
if (!location) continue;
149+
this.uniformLocations[name] = location;
148150
var value = uniforms[name];
149151
if (value instanceof Vector) {
150152
value = [value.x, value.y, value.z];

0 commit comments

Comments
 (0)