diff options
-rw-r--r-- | src/library_gl.js | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/library_gl.js b/src/library_gl.js index 2ab3afb2..82f05cd3 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -66,6 +66,16 @@ var LibraryGL = { return true; } while (true); return false; + }, + + computeImageSize: function(width, height, sizePerPixel, alignment) { + function roundedToNextMultipleOf(x, y) { + return Math.floor((x + y - 1) / y) * y + } + var plainRowSize = width * sizePerPixel; + var alignedRowSize = roundedToNextMultipleOf(plainRowSize, alignment); + return (height <= 0) ? 0 : + ((height - 1) * alignedRowSize + plainRowSize); } }, @@ -260,17 +270,17 @@ var LibraryGL = { default: throw 'Invalid format (' + format + ') passed to glTexImage2D'; } - pixels = {{{ makeHEAPView('U8', 'pixels', 'pixels+width*height*sizePerPixel') }}}; break; case 0x8363 /* GL_UNSIGNED_SHORT_5_6_5 */: case 0x8033 /* GL_UNSIGNED_SHORT_4_4_4_4 */: case 0x8034 /* GL_UNSIGNED_SHORT_5_5_5_1 */: sizePerPixel = 2; - pixels = {{{ makeHEAPView('U16', 'pixels', 'pixels+width*height*sizePerPixel') }}}; break; default: throw 'Invalid type (' + type + ') passed to glTexImage2D'; } + var bytes = GL.computeImageSize(width, height, sizePerPixel, GL.unpackAlignment); + pixels = {{{ makeHEAPView('U8', 'pixels', 'pixels+bytes') }}}; } else { pixels = null; } @@ -299,17 +309,17 @@ var LibraryGL = { default: throw 'Invalid format (' + format + ') passed to glTexSubImage2D'; } - pixels = {{{ makeHEAPView('U8', 'pixels', 'pixels+width*height*sizePerPixel') }}}; break; case 0x8363 /* GL_UNSIGNED_SHORT_5_6_5 */: case 0x8033 /* GL_UNSIGNED_SHORT_4_4_4_4 */: case 0x8034 /* GL_UNSIGNED_SHORT_5_5_5_1 */: sizePerPixel = 2; - pixels = {{{ makeHEAPView('U16', 'pixels', 'pixels+width*height*sizePerPixel') }}}; break; default: throw 'Invalid type (' + type + ') passed to glTexSubImage2D'; } + var bytes = GL.computeImageSize(width, height, sizePerPixel, GL.unpackAlignment); + pixels = {{{ makeHEAPView('U8', 'pixels', 'pixels+bytes') }}}; } else { pixels = null; } |