Skip to content

Commit

Permalink
Merge pull request #8972 from CesiumGS/webgl2-clipping
Browse files Browse the repository at this point in the history
Pass internalFormat to gl.texImage2D for WebGL 2
  • Loading branch information
lilleyse authored Jun 19, 2020
2 parents 46ed0ca + ac20c7e commit 45fa059
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fixed artifact for skinned model when log depth is enabled. [#6447](https://github.com/CesiumGS/cesium/issues/6447)
- Fixed a bug where certain rhumb arc polylines would lead to a crash. [#8787](https://github.com/CesiumGS/cesium/pull/8787)
- Fixed handling of Label's backgroundColor and backgroundPadding option [#8949](https://github.com/CesiumGS/cesium/8949)
- Fixed an error of applying clip planes to globe when enabling WebGL 2.0 [#7712](https://github.com/CesiumGS/cesium/issues/7712)
- Fixed a bug where half-float texture doesn't have correct WebGL 2.0 parameter [#8975](https://github.com/CesiumGS/cesium/pull/8975)

### 1.70.1 - 2020-06-10
Expand Down
51 changes: 51 additions & 0 deletions Source/Core/PixelFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,55 @@ PixelFormat.flipY = function (
return flipped;
};

/**
* @private
*/
PixelFormat.toInternalFormat = function (pixelFormat, pixelDatatype, context) {
// WebGL 1 require internalFormat to be the same as PixelFormat
if (!context.webgl2) {
return pixelFormat;
}

// Convert pixelFormat to correct internalFormat for WebGL 2
if (pixelFormat === PixelFormat.DEPTH_STENCIL) {
return WebGLConstants.DEPTH24_STENCIL8;
}

if (pixelFormat === PixelFormat.DEPTH_COMPONENT) {
if (pixelDatatype === PixelDatatype.UNSIGNED_SHORT) {
return WebGLConstants.DEPTH_COMPONENT16;
} else if (pixelDatatype === PixelDatatype.UNSIGNED_INT) {
return WebGLConstants.DEPTH_COMPONENT24;
}
}

if (pixelDatatype === PixelDatatype.FLOAT) {
switch (pixelFormat) {
case PixelFormat.RGBA:
return WebGLConstants.RGBA32F;
case PixelFormat.RGB:
return WebGLConstants.RGB32F;
case PixelFormat.RG:
return WebGLConstants.RG32F;
case PixelFormat.R:
return WebGLConstants.R32F;
}
}

if (pixelDatatype === PixelDatatype.HALF_FLOAT) {
switch (pixelFormat) {
case PixelFormat.RGBA:
return WebGLConstants.RGBA16F;
case PixelFormat.RGB:
return WebGLConstants.RGB16F;
case PixelFormat.RG:
return WebGLConstants.RG16F;
case PixelFormat.R:
return WebGLConstants.R16F;
}
}

return pixelFormat;
};

export default Object.freeze(PixelFormat);
29 changes: 20 additions & 9 deletions Source/Renderer/CubeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ function CubeMap(options) {
}

var size = width;
var pixelFormat = defaultValue(options.pixelFormat, PixelFormat.RGBA);
var pixelDatatype = defaultValue(
options.pixelDatatype,
PixelDatatype.UNSIGNED_BYTE
);
var pixelFormat = defaultValue(options.pixelFormat, PixelFormat.RGBA);
var internalFormat = PixelFormat.toInternalFormat(
pixelFormat,
pixelDatatype,
context
);

//>>includeStart('debug', pragmas.debug);
if (!defined(width) || !defined(height)) {
Expand Down Expand Up @@ -184,7 +189,7 @@ function CubeMap(options) {
gl.texImage2D(
target,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -201,7 +206,7 @@ function CubeMap(options) {
gl.texImage2D(
target,
0,
pixelFormat,
internalFormat,
pixelFormat,
PixelDatatype.toWebGLConstant(pixelDatatype, context),
sourceFace
Expand Down Expand Up @@ -250,7 +255,7 @@ function CubeMap(options) {
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_POSITIVE_X,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -261,7 +266,7 @@ function CubeMap(options) {
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -272,7 +277,7 @@ function CubeMap(options) {
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -283,7 +288,7 @@ function CubeMap(options) {
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -294,7 +299,7 @@ function CubeMap(options) {
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -305,7 +310,7 @@ function CubeMap(options) {
gl.texImage2D(
gl.TEXTURE_CUBE_MAP_NEGATIVE_Z,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand Down Expand Up @@ -335,6 +340,7 @@ function CubeMap(options) {
texture,
textureTarget,
gl.TEXTURE_CUBE_MAP_POSITIVE_X,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand All @@ -347,6 +353,7 @@ function CubeMap(options) {
texture,
textureTarget,
gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand All @@ -359,6 +366,7 @@ function CubeMap(options) {
texture,
textureTarget,
gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand All @@ -371,6 +379,7 @@ function CubeMap(options) {
texture,
textureTarget,
gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand All @@ -383,6 +392,7 @@ function CubeMap(options) {
texture,
textureTarget,
gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand All @@ -395,6 +405,7 @@ function CubeMap(options) {
texture,
textureTarget,
gl.TEXTURE_CUBE_MAP_NEGATIVE_Z,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand Down
11 changes: 7 additions & 4 deletions Source/Renderer/CubeMapFace.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function CubeMapFace(
texture,
textureTarget,
targetFace,
internalFormat,
pixelFormat,
pixelDatatype,
size,
Expand All @@ -24,8 +25,9 @@ function CubeMapFace(
this._texture = texture;
this._textureTarget = textureTarget;
this._targetFace = targetFace;
this._pixelFormat = pixelFormat;
this._pixelDatatype = pixelDatatype;
this._internalFormat = internalFormat;
this._pixelFormat = pixelFormat;
this._size = size;
this._preMultiplyAlpha = preMultiplyAlpha;
this._flipY = flipY;
Expand Down Expand Up @@ -109,6 +111,7 @@ CubeMapFace.prototype.copyFrom = function (source, xOffset, yOffset) {

var size = this._size;
var pixelFormat = this._pixelFormat;
var internalFormat = this._internalFormat;
var pixelDatatype = this._pixelDatatype;

var preMultiplyAlpha = this._preMultiplyAlpha;
Expand Down Expand Up @@ -145,7 +148,7 @@ CubeMapFace.prototype.copyFrom = function (source, xOffset, yOffset) {
gl.texImage2D(
targetFace,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand All @@ -161,7 +164,7 @@ CubeMapFace.prototype.copyFrom = function (source, xOffset, yOffset) {
gl.texImage2D(
targetFace,
0,
pixelFormat,
internalFormat,
pixelFormat,
PixelDatatype.toWebGLConstant(pixelDatatype, this._context),
source
Expand All @@ -182,7 +185,7 @@ CubeMapFace.prototype.copyFrom = function (source, xOffset, yOffset) {
gl.texImage2D(
targetFace,
0,
pixelFormat,
internalFormat,
size,
size,
0,
Expand Down
59 changes: 10 additions & 49 deletions Source/Renderer/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import CesiumMath from "../Core/Math.js";
import PixelFormat from "../Core/PixelFormat.js";
import WebGLConstants from "../Core/WebGLConstants.js";
import ContextLimits from "./ContextLimits.js";
import MipmapHint from "./MipmapHint.js";
import PixelDatatype from "./PixelDatatype.js";
Expand Down Expand Up @@ -44,54 +43,14 @@ function Texture(options) {
options.pixelDatatype,
PixelDatatype.UNSIGNED_BYTE
);
var internalFormat = pixelFormat;
var internalFormat = PixelFormat.toInternalFormat(
pixelFormat,
pixelDatatype,
context
);

var isCompressed = PixelFormat.isCompressedFormat(internalFormat);

if (context.webgl2) {
if (pixelFormat === PixelFormat.DEPTH_STENCIL) {
internalFormat = WebGLConstants.DEPTH24_STENCIL8;
} else if (pixelFormat === PixelFormat.DEPTH_COMPONENT) {
if (pixelDatatype === PixelDatatype.UNSIGNED_SHORT) {
internalFormat = WebGLConstants.DEPTH_COMPONENT16;
} else if (pixelDatatype === PixelDatatype.UNSIGNED_INT) {
internalFormat = WebGLConstants.DEPTH_COMPONENT24;
}
}

if (pixelDatatype === PixelDatatype.FLOAT) {
switch (pixelFormat) {
case PixelFormat.RGBA:
internalFormat = WebGLConstants.RGBA32F;
break;
case PixelFormat.RGB:
internalFormat = WebGLConstants.RGB32F;
break;
case PixelFormat.RG:
internalFormat = WebGLConstants.RG32F;
break;
case PixelFormat.R:
internalFormat = WebGLConstants.R32F;
break;
}
} else if (pixelDatatype === PixelDatatype.HALF_FLOAT) {
switch (pixelFormat) {
case PixelFormat.RGBA:
internalFormat = WebGLConstants.RGBA16F;
break;
case PixelFormat.RGB:
internalFormat = WebGLConstants.RGB16F;
break;
case PixelFormat.RG:
internalFormat = WebGLConstants.RG16F;
break;
case PixelFormat.R:
internalFormat = WebGLConstants.R16F;
break;
}
}
}

//>>includeStart('debug', pragmas.debug);
if (!defined(width) || !defined(height)) {
throw new DeveloperError(
Expand Down Expand Up @@ -378,6 +337,7 @@ function Texture(options) {
this._textureFilterAnisotropic = context._textureFilterAnisotropic;
this._textureTarget = textureTarget;
this._texture = texture;
this._internalFormat = internalFormat;
this._pixelFormat = pixelFormat;
this._pixelDatatype = pixelDatatype;
this._width = width;
Expand Down Expand Up @@ -699,6 +659,7 @@ Texture.prototype.copyFrom = function (source, xOffset, yOffset) {

var textureWidth = this._width;
var textureHeight = this._height;
var internalFormat = this._internalFormat;
var pixelFormat = this._pixelFormat;
var pixelDatatype = this._pixelDatatype;

Expand Down Expand Up @@ -741,7 +702,7 @@ Texture.prototype.copyFrom = function (source, xOffset, yOffset) {
gl.texImage2D(
target,
0,
pixelFormat,
internalFormat,
textureWidth,
textureHeight,
0,
Expand All @@ -757,7 +718,7 @@ Texture.prototype.copyFrom = function (source, xOffset, yOffset) {
gl.texImage2D(
target,
0,
pixelFormat,
internalFormat,
pixelFormat,
PixelDatatype.toWebGLConstant(pixelDatatype, context),
source
Expand All @@ -778,7 +739,7 @@ Texture.prototype.copyFrom = function (source, xOffset, yOffset) {
gl.texImage2D(
target,
0,
pixelFormat,
internalFormat,
textureWidth,
textureHeight,
0,
Expand Down

0 comments on commit 45fa059

Please sign in to comment.