From f7fac414e3d0a2668b78f6bbdc817ee9dcf70561 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 26 Oct 2017 17:55:21 -0400 Subject: [PATCH 1/6] Only use pre-multiply alpha and y flip for DOM sources. Initialized textures before using texSubImage. --- Source/Renderer/CubeMap.js | 41 +++++++++++--------- Source/Renderer/CubeMapFace.js | 63 ++++++++++++++++++++++++++---- Source/Renderer/Texture.js | 71 +++++++++++++++++++++++++++++----- 3 files changed, 140 insertions(+), 35 deletions(-) diff --git a/Source/Renderer/CubeMap.js b/Source/Renderer/CubeMap.js index e08eccd1c718..d0f966aca165 100644 --- a/Source/Renderer/CubeMap.js +++ b/Source/Renderer/CubeMap.js @@ -33,7 +33,6 @@ define([ 'use strict'; function CubeMap(options) { - options = defaultValue(options, defaultValue.EMPTY_OBJECT); //>>includeStart('debug', pragmas.debug); @@ -121,25 +120,30 @@ define([ gl.activeTexture(gl.TEXTURE0); gl.bindTexture(textureTarget, texture); - function createFace(target, sourceFace) { + function createFace(target, sourceFace, preMultiplyAlpha, flipY) { + // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + if (sourceFace.arrayBufferView) { gl.texImage2D(target, 0, pixelFormat, size, size, 0, pixelFormat, pixelDatatype, sourceFace.arrayBufferView); } else { + // Only valid for DOM-Element uploads + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, preMultiplyAlpha); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); + + // Source: ImageData, HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement gl.texImage2D(target, 0, pixelFormat, pixelFormat, pixelDatatype, sourceFace); } } if (defined(source)) { - // TODO: _gl.pixelStorei(_gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, preMultiplyAlpha); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); - - createFace(gl.TEXTURE_CUBE_MAP_POSITIVE_X, source.positiveX); - createFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, source.negativeX); - createFace(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, source.positiveY); - createFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, source.negativeY); - createFace(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, source.positiveZ); - createFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, source.negativeZ); + createFace(gl.TEXTURE_CUBE_MAP_POSITIVE_X, source.positiveX, preMultiplyAlpha, flipY); + createFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, source.negativeX, preMultiplyAlpha, flipY); + createFace(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, source.positiveY, preMultiplyAlpha, flipY); + createFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, source.negativeY, preMultiplyAlpha, flipY); + createFace(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, source.positiveZ, preMultiplyAlpha, flipY); + createFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, source.negativeZ, preMultiplyAlpha, flipY); } else { gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, pixelFormat, size, size, 0, pixelFormat, pixelDatatype, null); gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, pixelFormat, size, size, 0, pixelFormat, pixelDatatype, null); @@ -163,12 +167,13 @@ define([ this._flipY = flipY; this._sampler = undefined; - this._positiveX = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_POSITIVE_X, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY); - this._negativeX = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_NEGATIVE_X, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY); - this._positiveY = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY); - this._negativeY = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY); - this._positiveZ = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_POSITIVE_Z, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY); - this._negativeZ = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY); + var initialized = defined(source); + this._positiveX = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_POSITIVE_X, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized); + this._negativeX = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_NEGATIVE_X, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized); + this._positiveY = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized); + this._negativeY = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized); + this._positiveZ = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_POSITIVE_Z, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized); + this._negativeZ = new CubeMapFace(gl, texture, textureTarget, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized); this.sampler = defined(options.sampler) ? options.sampler : new Sampler(); } diff --git a/Source/Renderer/CubeMapFace.js b/Source/Renderer/CubeMapFace.js index 83e9a165cd73..aa713036e4b8 100644 --- a/Source/Renderer/CubeMapFace.js +++ b/Source/Renderer/CubeMapFace.js @@ -1,21 +1,25 @@ define([ '../Core/Check', '../Core/defaultValue', + '../Core/defined', '../Core/defineProperties', '../Core/DeveloperError', + '../Core/PixelFormat', './PixelDatatype' ], function( Check, defaultValue, + defined, defineProperties, DeveloperError, + PixelFormat, PixelDatatype) { 'use strict'; /** * @private */ - function CubeMapFace(gl, texture, textureTarget, targetFace, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY) { + function CubeMapFace(gl, texture, textureTarget, targetFace, pixelFormat, pixelDatatype, size, preMultiplyAlpha, flipY, initialized) { this._gl = gl; this._texture = texture; this._textureTarget = textureTarget; @@ -25,6 +29,7 @@ define([ this._size = size; this._preMultiplyAlpha = preMultiplyAlpha; this._flipY = flipY; + this._initialized = initialized; } defineProperties(CubeMapFace.prototype, { @@ -91,15 +96,58 @@ define([ var target = this._textureTarget; // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY); + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + gl.activeTexture(gl.TEXTURE0); gl.bindTexture(target, this._texture); - if (source.arrayBufferView) { - gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); - } else { - gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, this._pixelFormat, this._pixelDatatype, source); + var uploaded = false; + if (!this._initialized) { + if (xOffset === 0 && yOffset === 0 && source.width === this._size && source.height === this._size) { + // initialize the entire texture + if (defined(source.arrayBufferView)) { + gl.texImage2D(this._targetFace, 0, this._pixelFormat, this._size, this._size, 0, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); + } else { + // Only valid for DOM-Element uploads + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY); + + gl.texImage2D(this._targetFace, 0, this._pixelFormat, this._pixelFormat, this._pixelDatatype, source); + } + uploaded = true; + } else { + // initialize the entire texture to zero + var constructor; + var sizeInBytes = PixelDatatype.sizeInBytes(this._pixelDatatype); + if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) { + constructor = Uint8Array; + } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) { + constructor = Uint16Array.BYTES_PER_ELEMENT; + } else if (sizeInBytes === Float32Array.BYTES_PER_ELEMENT && this._pixelDatatype === PixelDatatype.FLOAT) { + constructor = Float32Array; + } else { + constructor = Uint32Array; + } + + var size = PixelFormat.componentsLength(this._pixelFormat) * this._size * this._size; + var bufferView = new constructor(size); + gl.texImage2D(this._targetFace, 0, this._pixelFormat, this._size, this._size, 0, this._pixelFormat, this._pixelDatatype, bufferView); + } + this._initialized = true; + } + + if (!uploaded) { + if (source.arrayBufferView) { + gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); + } else { + // Only valid for DOM-Element uploads + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY); + + // Source: ImageData, HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement + gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, this._pixelFormat, this._pixelDatatype, source); + } } gl.bindTexture(target, null); @@ -160,6 +208,7 @@ define([ gl.bindTexture(target, this._texture); gl.copyTexSubImage2D(this._targetFace, 0, xOffset, yOffset, framebufferXOffset, framebufferYOffset, width, height); gl.bindTexture(target, null); + this._initialized = true; }; return CubeMapFace; diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 2ba7284e05c7..e0bfff62e8b3 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -164,18 +164,20 @@ define([ var preMultiplyAlpha = options.preMultiplyAlpha || pixelFormat === PixelFormat.RGB || pixelFormat === PixelFormat.LUMINANCE; var flipY = defaultValue(options.flipY, true); + var initialized = true; + var gl = context._gl; var textureTarget = gl.TEXTURE_2D; var texture = gl.createTexture(); + // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + gl.activeTexture(gl.TEXTURE0); gl.bindTexture(textureTarget, texture); if (defined(source)) { - // TODO: _gl.pixelStorei(_gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, preMultiplyAlpha); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); - if (defined(source.arrayBufferView)) { // Source: typed array if (isCompressed) { @@ -195,11 +197,16 @@ define([ source.framebuffer._unBind(); } } else { + // Only valid for DOM-Element uploads + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, preMultiplyAlpha); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); + // Source: ImageData, HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement gl.texImage2D(textureTarget, 0, internalFormat, pixelFormat, pixelDatatype, source); } } else { gl.texImage2D(textureTarget, 0, internalFormat, width, height, 0, pixelFormat, pixelDatatype, null); + initialized = false; } gl.bindTexture(textureTarget, null); @@ -224,6 +231,7 @@ define([ this._sizeInBytes = sizeInBytes; this._preMultiplyAlpha = preMultiplyAlpha; this._flipY = flipY; + this._initialized = initialized; this._sampler = undefined; this.sampler = defined(options.sampler) ? options.sampler : new Sampler(); @@ -469,15 +477,57 @@ define([ var target = this._textureTarget; // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY); + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + gl.activeTexture(gl.TEXTURE0); gl.bindTexture(target, this._texture); - if (source.arrayBufferView) { - gl.texSubImage2D(target, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); - } else { - gl.texSubImage2D(target, 0, xOffset, yOffset, this._pixelFormat, this._pixelDatatype, source); + var uploaded = false; + if (!this._initialized) { + if (xOffset === 0 && yOffset === 0 && source.width === this._width && source.height === this._height) { + // initialize the entire texture + if (defined(source.arrayBufferView)) { + gl.texImage2D(target, 0, this._pixelFormat, this._width, this._height, 0, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); + } else { + // Only valid for DOM-Element uploads + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY); + + gl.texImage2D(target, 0, this._pixelFormat, this._pixelFormat, this._pixelDatatype, source); + } + uploaded = true; + } else { + // initialize the entire texture to zero + var constructor; + var sizeInBytes = PixelDatatype.sizeInBytes(this._pixelDatatype); + if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) { + constructor = Uint8Array; + } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) { + constructor = Uint16Array.BYTES_PER_ELEMENT; + } else if (sizeInBytes === Float32Array.BYTES_PER_ELEMENT && this._pixelDatatype === PixelDatatype.FLOAT) { + constructor = Float32Array; + } else { + constructor = Uint32Array; + } + + var size = PixelFormat.componentsLength(this._pixelFormat) * this._width * this._height; + var bufferView = new constructor(size); + gl.texImage2D(target, 0, this._pixelFormat, this._width, this._height, 0, this._pixelFormat, this._pixelDatatype, bufferView); + } + this._initialized = true; + } + + if (!uploaded) { + if (source.arrayBufferView) { + gl.texSubImage2D(target, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); + } else { + // Only valid for DOM-Element uploads + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._preMultiplyAlpha); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, this._flipY); + + gl.texSubImage2D(target, 0, xOffset, yOffset, this._pixelFormat, this._pixelDatatype, source); + } } gl.bindTexture(target, null); @@ -536,6 +586,7 @@ define([ gl.bindTexture(target, this._texture); gl.copyTexSubImage2D(target, 0, xOffset, yOffset, framebufferXOffset, framebufferYOffset, width, height); gl.bindTexture(target, null); + this._initialized = true; }; /** From 95e3f9ac8619d7b6955d11e87e9bc7a5e9357a4a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 26 Oct 2017 17:59:41 -0400 Subject: [PATCH 2/6] Update CHANGES.md. --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 3ff13d7948ab..44458b3dc264 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Change Log * Added `customTags` property to the UrlTemplateImageryProvider to allow custom keywords in the template URL. [#5696](https://github.com/AnalyticalGraphicsInc/cesium/pull/5696) * Improved CZML Reference Properties example [#5754](https://github.com/AnalyticalGraphicsInc/cesium/pull/5754) * Fixed bug with placemarks in imported KML: placemarks with no specified icon would be displayed with default icon. [#5819](https://github.com/AnalyticalGraphicsInc/cesium/issues/5819) +* Fix Firefox WebGL console warnings. [#5912](https://github.com/AnalyticalGraphicsInc/cesium/issues/5912) ### 1.38 - 2017-10-02 From d9f44d087f7437e460b1814fcc83168c1a3a1a2c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 28 Feb 2018 15:05:19 -0500 Subject: [PATCH 3/6] Update pixelStorei from review. --- Source/Renderer/CubeMap.js | 5 ++--- Source/Renderer/CubeMapFace.js | 11 +++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Source/Renderer/CubeMap.js b/Source/Renderer/CubeMap.js index d0f966aca165..00a3fe450861 100644 --- a/Source/Renderer/CubeMap.js +++ b/Source/Renderer/CubeMap.js @@ -122,10 +122,9 @@ define([ function createFace(target, sourceFace, preMultiplyAlpha, flipY) { // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); - if (sourceFace.arrayBufferView) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); gl.texImage2D(target, 0, pixelFormat, size, size, 0, pixelFormat, pixelDatatype, sourceFace.arrayBufferView); } else { // Only valid for DOM-Element uploads diff --git a/Source/Renderer/CubeMapFace.js b/Source/Renderer/CubeMapFace.js index aa713036e4b8..b598e23f7bd3 100644 --- a/Source/Renderer/CubeMapFace.js +++ b/Source/Renderer/CubeMapFace.js @@ -96,8 +96,6 @@ define([ var target = this._textureTarget; // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(target, this._texture); @@ -107,6 +105,9 @@ define([ if (xOffset === 0 && yOffset === 0 && source.width === this._size && source.height === this._size) { // initialize the entire texture if (defined(source.arrayBufferView)) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + gl.texImage2D(this._targetFace, 0, this._pixelFormat, this._size, this._size, 0, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); } else { // Only valid for DOM-Element uploads @@ -117,6 +118,9 @@ define([ } uploaded = true; } else { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + // initialize the entire texture to zero var constructor; var sizeInBytes = PixelDatatype.sizeInBytes(this._pixelDatatype); @@ -139,6 +143,9 @@ define([ if (!uploaded) { if (source.arrayBufferView) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + gl.texSubImage2D(this._targetFace, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); } else { // Only valid for DOM-Element uploads From 09720d045987342cab3e6f169df73db71cda72dc Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 28 Feb 2018 16:02:15 -0500 Subject: [PATCH 4/6] Have PixelFormat create an array buffer of the right size. --- Source/Core/PixelFormat.js | 20 ++++++++++++++++++++ Source/Renderer/CubeMapFace.js | 15 +-------------- Source/Renderer/Texture.js | 15 +-------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Source/Core/PixelFormat.js b/Source/Core/PixelFormat.js index 5c01b6999909..6654608f5b21 100644 --- a/Source/Core/PixelFormat.js +++ b/Source/Core/PixelFormat.js @@ -281,6 +281,26 @@ define([ componentsLength = 1; } return componentsLength * PixelDatatype.sizeInBytes(pixelDatatype) * width * height; + }, + + /** + * @private + */ + createTypedArray : function(pixelFormat, pixelDatatype, width, height) { + var constructor; + var sizeInBytes = PixelDatatype.sizeInBytes(pixelDatatype); + if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) { + constructor = Uint8Array; + } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) { + constructor = Uint16Array; + } else if (sizeInBytes === Float32Array.BYTES_PER_ELEMENT && pixelDatatype === PixelDatatype.FLOAT) { + constructor = Float32Array; + } else { + constructor = Uint32Array; + } + + var size = PixelFormat.componentsLength(pixelFormat) * width * height; + return new constructor(size); } }; diff --git a/Source/Renderer/CubeMapFace.js b/Source/Renderer/CubeMapFace.js index b598e23f7bd3..152cb59c9dfa 100644 --- a/Source/Renderer/CubeMapFace.js +++ b/Source/Renderer/CubeMapFace.js @@ -122,20 +122,7 @@ define([ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); // initialize the entire texture to zero - var constructor; - var sizeInBytes = PixelDatatype.sizeInBytes(this._pixelDatatype); - if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) { - constructor = Uint8Array; - } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) { - constructor = Uint16Array.BYTES_PER_ELEMENT; - } else if (sizeInBytes === Float32Array.BYTES_PER_ELEMENT && this._pixelDatatype === PixelDatatype.FLOAT) { - constructor = Float32Array; - } else { - constructor = Uint32Array; - } - - var size = PixelFormat.componentsLength(this._pixelFormat) * this._size * this._size; - var bufferView = new constructor(size); + var bufferView = PixelFormat.createTypedArray(this._pixelFormat, this._pixelDatatype, this._size, this._size); gl.texImage2D(this._targetFace, 0, this._pixelFormat, this._size, this._size, 0, this._pixelFormat, this._pixelDatatype, bufferView); } this._initialized = true; diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index e0bfff62e8b3..1d1ce44c2f3b 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -499,20 +499,7 @@ define([ uploaded = true; } else { // initialize the entire texture to zero - var constructor; - var sizeInBytes = PixelDatatype.sizeInBytes(this._pixelDatatype); - if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) { - constructor = Uint8Array; - } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) { - constructor = Uint16Array.BYTES_PER_ELEMENT; - } else if (sizeInBytes === Float32Array.BYTES_PER_ELEMENT && this._pixelDatatype === PixelDatatype.FLOAT) { - constructor = Float32Array; - } else { - constructor = Uint32Array; - } - - var size = PixelFormat.componentsLength(this._pixelFormat) * this._width * this._height; - var bufferView = new constructor(size); + var bufferView = PixelFormat.createTypedArray(this._pixelFormat, this._pixelDatatype, this._width, this._height); gl.texImage2D(target, 0, this._pixelFormat, this._width, this._height, 0, this._pixelFormat, this._pixelDatatype, bufferView); } this._initialized = true; From 1bcac56c61c80981af5add01caec482addebdd5d Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 28 Feb 2018 17:40:11 -0500 Subject: [PATCH 5/6] Don't call pixelStorei multiple times in Texture.copyFrom. --- Source/Renderer/Texture.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 1d1ce44c2f3b..3f7d1e6654ca 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -477,8 +477,6 @@ define([ var target = this._textureTarget; // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(target, this._texture); @@ -488,6 +486,8 @@ define([ if (xOffset === 0 && yOffset === 0 && source.width === this._width && source.height === this._height) { // initialize the entire texture if (defined(source.arrayBufferView)) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); gl.texImage2D(target, 0, this._pixelFormat, this._width, this._height, 0, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); } else { // Only valid for DOM-Element uploads @@ -498,6 +498,9 @@ define([ } uploaded = true; } else { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + // initialize the entire texture to zero var bufferView = PixelFormat.createTypedArray(this._pixelFormat, this._pixelDatatype, this._width, this._height); gl.texImage2D(target, 0, this._pixelFormat, this._width, this._height, 0, this._pixelFormat, this._pixelDatatype, bufferView); @@ -507,6 +510,9 @@ define([ if (!uploaded) { if (source.arrayBufferView) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + gl.texSubImage2D(target, 0, xOffset, yOffset, source.width, source.height, this._pixelFormat, this._pixelDatatype, source.arrayBufferView); } else { // Only valid for DOM-Element uploads From 544d08ea34ce7f3358bc613601fb20cbd445efab Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 28 Feb 2018 17:48:08 -0500 Subject: [PATCH 6/6] Fix remaining pixelStorei. --- Source/Renderer/Texture.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 3f7d1e6654ca..9d7387962a41 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -171,14 +171,15 @@ define([ var texture = gl.createTexture(); // TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4); - gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); - gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(textureTarget, texture); if (defined(source)) { if (defined(source.arrayBufferView)) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + // Source: typed array if (isCompressed) { gl.compressedTexImage2D(textureTarget, 0, internalFormat, width, height, 0, source.arrayBufferView); @@ -186,6 +187,9 @@ define([ gl.texImage2D(textureTarget, 0, internalFormat, width, height, 0, pixelFormat, pixelDatatype, source.arrayBufferView); } } else if (defined(source.framebuffer)) { + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); + // Source: framebuffer if (source.framebuffer !== context.defaultFramebuffer) { source.framebuffer._bind();