Ver código fonte

WebGL performance improvements: re-use texture data

phoboslab 7 anos atrás
pai
commit
64d8e56338
2 arquivos alterados com 20 adições e 8 exclusões
  1. 1 1
      src/mpeg1.js
  2. 19 7
      src/webgl.js

+ 1 - 1
src/mpeg1.js Ver arquivo

205
 
205
 
206
 	// Invoke decode callbacks
206
 	// Invoke decode callbacks
207
 	if (this.destination) {
207
 	if (this.destination) {
208
-		this.destination.render(this.currentY, this.currentCr, this.currentCb);
208
+		this.destination.render(this.currentY, this.currentCr, this.currentCb, true);
209
 	}
209
 	}
210
 
210
 
211
 	// If this is a reference picutre then rotate the prediction pointers
211
 	// If this is a reference picutre then rotate the prediction pointers

+ 19 - 7
src/webgl.js Ver arquivo

6
 	this.height = this.canvas.height;
6
 	this.height = this.canvas.height;
7
 	this.enabled = true;
7
 	this.enabled = true;
8
 
8
 
9
+	this.hasTextureData = {};
10
+
9
 	var contextCreateOptions = {
11
 	var contextCreateOptions = {
10
 		preserveDrawingBuffer: !!options.preserveDrawingBuffer,
12
 		preserveDrawingBuffer: !!options.preserveDrawingBuffer,
11
 		alpha: false,
13
 		alpha: false,
12
 		depth: false,
14
 		depth: false,
13
 		stencil: false,
15
 		stencil: false,
14
-		antialias: false
16
+		antialias: false,
17
+		premultipliedAlpha: false
15
 	};
18
 	};
16
 
19
 
17
 	this.gl = 
20
 	this.gl = 
25
 	var gl = this.gl;
28
 	var gl = this.gl;
26
 	var vertexAttr = null;
29
 	var vertexAttr = null;
27
 
30
 
31
+	gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
32
+
28
 	// Init buffers
33
 	// Init buffers
29
 	this.vertexBuffer = gl.createBuffer();
34
 	this.vertexBuffer = gl.createBuffer();
30
 	var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
35
 	var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
143
 	gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
148
 	gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
144
 };
149
 };
145
 
150
 
146
-WebGLRenderer.prototype.render = function(y, cb, cr) {
151
+WebGLRenderer.prototype.render = function(y, cb, cr, isClampedArray) {
147
 	if (!this.enabled) {
152
 	if (!this.enabled) {
148
 		return;
153
 		return;
149
 	}
154
 	}
157
 	// In some browsers WebGL doesn't like Uint8ClampedArrays (this is a bug
162
 	// In some browsers WebGL doesn't like Uint8ClampedArrays (this is a bug
158
 	// and should be fixed soon-ish), so we have to create a Uint8Array view 
163
 	// and should be fixed soon-ish), so we have to create a Uint8Array view 
159
 	// for each plane.
164
 	// for each plane.
160
-	if (this.shouldCreateUnclampedViews) {
165
+	if (isClampedArray && this.shouldCreateUnclampedViews) {
161
 		y = new Uint8Array(y.buffer),
166
 		y = new Uint8Array(y.buffer),
162
 		cb = new Uint8Array(cb.buffer),
167
 		cb = new Uint8Array(cb.buffer),
163
 		cr = new Uint8Array(cr.buffer);	
168
 		cr = new Uint8Array(cr.buffer);	
176
 	var gl = this.gl;
181
 	var gl = this.gl;
177
 	gl.activeTexture(unit);
182
 	gl.activeTexture(unit);
178
 	gl.bindTexture(gl.TEXTURE_2D, texture);
183
 	gl.bindTexture(gl.TEXTURE_2D, texture);
179
-	gl.texImage2D(
180
-		gl.TEXTURE_2D, 0, gl.LUMINANCE, w, h, 0, 
181
-		gl.LUMINANCE, gl.UNSIGNED_BYTE, data
182
-	);
184
+
185
+	if (this.hasTextureData[unit]) {
186
+		gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, w, h, gl.LUMINANCE, gl.UNSIGNED_BYTE, data);
187
+	}
188
+	else {
189
+		this.hasTextureData[unit] = true;
190
+		gl.texImage2D(
191
+			gl.TEXTURE_2D, 0, gl.LUMINANCE, w, h, 0, 
192
+			gl.LUMINANCE, gl.UNSIGNED_BYTE, data
193
+		);
194
+	}
183
 }
195
 }
184
 
196
 
185
 WebGLRenderer.IsSupported = function() {
197
 WebGLRenderer.IsSupported = function() {