Skip to content

Commit

Permalink
WebGL performance improvements: re-use texture data
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab committed Oct 9, 2018
1 parent 000bd2f commit 64d8e56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/mpeg1.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ MPEG1.prototype.decodePicture = function(skipOutput) {

// Invoke decode callbacks
if (this.destination) {
this.destination.render(this.currentY, this.currentCr, this.currentCb);
this.destination.render(this.currentY, this.currentCr, this.currentCb, true);
}

// If this is a reference picutre then rotate the prediction pointers
Expand Down
26 changes: 19 additions & 7 deletions src/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ var WebGLRenderer = function(options) {
this.height = this.canvas.height;
this.enabled = true;

this.hasTextureData = {};

var contextCreateOptions = {
preserveDrawingBuffer: !!options.preserveDrawingBuffer,
alpha: false,
depth: false,
stencil: false,
antialias: false
antialias: false,
premultipliedAlpha: false
};

this.gl =
Expand All @@ -25,6 +28,8 @@ var WebGLRenderer = function(options) {
var gl = this.gl;
var vertexAttr = null;

gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);

// Init buffers
this.vertexBuffer = gl.createBuffer();
var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
Expand Down Expand Up @@ -143,7 +148,7 @@ WebGLRenderer.prototype.renderProgress = function(progress) {
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};

WebGLRenderer.prototype.render = function(y, cb, cr) {
WebGLRenderer.prototype.render = function(y, cb, cr, isClampedArray) {
if (!this.enabled) {
return;
}
Expand All @@ -157,7 +162,7 @@ WebGLRenderer.prototype.render = function(y, cb, cr) {
// In some browsers WebGL doesn't like Uint8ClampedArrays (this is a bug
// and should be fixed soon-ish), so we have to create a Uint8Array view
// for each plane.
if (this.shouldCreateUnclampedViews) {
if (isClampedArray && this.shouldCreateUnclampedViews) {
y = new Uint8Array(y.buffer),
cb = new Uint8Array(cb.buffer),
cr = new Uint8Array(cr.buffer);
Expand All @@ -176,10 +181,17 @@ WebGLRenderer.prototype.updateTexture = function(unit, texture, w, h, data) {
var gl = this.gl;
gl.activeTexture(unit);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.LUMINANCE, w, h, 0,
gl.LUMINANCE, gl.UNSIGNED_BYTE, data
);

if (this.hasTextureData[unit]) {
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, w, h, gl.LUMINANCE, gl.UNSIGNED_BYTE, data);
}
else {
this.hasTextureData[unit] = true;
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.LUMINANCE, w, h, 0,
gl.LUMINANCE, gl.UNSIGNED_BYTE, data
);
}
}

WebGLRenderer.IsSupported = function() {
Expand Down

0 comments on commit 64d8e56

Please sign in to comment.