Bläddra i källkod

WebGL performance improvements: re-use texture data

phoboslab 7 år sedan
förälder
incheckning
64d8e56338
2 ändrade filer med 20 tillägg och 8 borttagningar
  1. 1 1
      src/mpeg1.js
  2. 19 7
      src/webgl.js

+ 1 - 1
src/mpeg1.js Visa fil

@@ -205,7 +205,7 @@ MPEG1.prototype.decodePicture = function(skipOutput) {
205 205
 
206 206
 	// Invoke decode callbacks
207 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 211
 	// If this is a reference picutre then rotate the prediction pointers

+ 19 - 7
src/webgl.js Visa fil

@@ -6,12 +6,15 @@ var WebGLRenderer = function(options) {
6 6
 	this.height = this.canvas.height;
7 7
 	this.enabled = true;
8 8
 
9
+	this.hasTextureData = {};
10
+
9 11
 	var contextCreateOptions = {
10 12
 		preserveDrawingBuffer: !!options.preserveDrawingBuffer,
11 13
 		alpha: false,
12 14
 		depth: false,
13 15
 		stencil: false,
14
-		antialias: false
16
+		antialias: false,
17
+		premultipliedAlpha: false
15 18
 	};
16 19
 
17 20
 	this.gl = 
@@ -25,6 +28,8 @@ var WebGLRenderer = function(options) {
25 28
 	var gl = this.gl;
26 29
 	var vertexAttr = null;
27 30
 
31
+	gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
32
+
28 33
 	// Init buffers
29 34
 	this.vertexBuffer = gl.createBuffer();
30 35
 	var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
@@ -143,7 +148,7 @@ WebGLRenderer.prototype.renderProgress = function(progress) {
143 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 152
 	if (!this.enabled) {
148 153
 		return;
149 154
 	}
@@ -157,7 +162,7 @@ WebGLRenderer.prototype.render = function(y, cb, cr) {
157 162
 	// In some browsers WebGL doesn't like Uint8ClampedArrays (this is a bug
158 163
 	// and should be fixed soon-ish), so we have to create a Uint8Array view 
159 164
 	// for each plane.
160
-	if (this.shouldCreateUnclampedViews) {
165
+	if (isClampedArray && this.shouldCreateUnclampedViews) {
161 166
 		y = new Uint8Array(y.buffer),
162 167
 		cb = new Uint8Array(cb.buffer),
163 168
 		cr = new Uint8Array(cr.buffer);	
@@ -176,10 +181,17 @@ WebGLRenderer.prototype.updateTexture = function(unit, texture, w, h, data) {
176 181
 	var gl = this.gl;
177 182
 	gl.activeTexture(unit);
178 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 197
 WebGLRenderer.IsSupported = function() {