@galacean/engine-rhi-webgl 1.4.14 → 1.5.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -7,9 +7,7 @@ var engineMath = require('@galacean/engine-math');
7
7
 
8
8
  /**
9
9
  * Smoothing plug-in.
10
- * */ /**
11
- * @deprecated
12
- */ var GLCompressedTextureInternalFormat = /*#__PURE__*/ function(GLCompressedTextureInternalFormat) {
10
+ * */ var GLCompressedTextureInternalFormat = /*#__PURE__*/ function(GLCompressedTextureInternalFormat) {
13
11
  // astc
14
12
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_4X4_KHR"] = 37808] = "RGBA_ASTC_4X4_KHR";
15
13
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_5X4_KHR"] = 37809] = "RGBA_ASTC_5X4_KHR";
@@ -62,6 +60,11 @@ var engineMath = require('@galacean/engine-math');
62
60
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT1_EXT"] = 33777] = "RGBA_S3TC_DXT1_EXT";
63
61
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT3_EXT"] = 33778] = "RGBA_S3TC_DXT3_EXT";
64
62
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT5_EXT"] = 33779] = "RGBA_S3TC_DXT5_EXT";
63
+ // s3tc sRGB
64
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_S3TC_DXT1_EXT"] = 35916] = "SRGB_S3TC_DXT1_EXT";
65
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_S3TC_DXT1_EXT"] = 35917] = "SRGB_ALPHA_S3TC_DXT1_EXT";
66
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_S3TC_DXT3_EXT"] = 35918] = "SRGB_ALPHA_S3TC_DXT3_EXT";
67
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_S3TC_DXT5_EXT"] = 35919] = "SRGB_ALPHA_S3TC_DXT5_EXT";
65
68
  // bptc
66
69
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_BPTC_UNORM_EXT"] = 36492] = "RGBA_BPTC_UNORM_EXT";
67
70
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_BPTC_UNORM_EXT"] = 36493] = "SRGB_ALPHA_BPTC_UNORM_EXT";
@@ -351,7 +354,7 @@ var GLBuffer = /*#__PURE__*/ function() {
351
354
  var cap = this.capabilityList;
352
355
  var isWebGL2 = this.rhi.isWebGL2;
353
356
  var requireExtension = this.rhi.requireExtension.bind(this.rhi);
354
- var shaderVertexID = engineCore.GLCapabilityType.shaderVertexID, standardDerivatives = engineCore.GLCapabilityType.standardDerivatives, shaderTextureLod = engineCore.GLCapabilityType.shaderTextureLod, elementIndexUint = engineCore.GLCapabilityType.elementIndexUint, depthTexture = engineCore.GLCapabilityType.depthTexture, vertexArrayObject = engineCore.GLCapabilityType.vertexArrayObject, instancedArrays = engineCore.GLCapabilityType.instancedArrays, multipleSample = engineCore.GLCapabilityType.multipleSample, drawBuffers = engineCore.GLCapabilityType.drawBuffers, blendMinMax = engineCore.GLCapabilityType.blendMinMax, astc = engineCore.GLCapabilityType.astc, astc_webkit = engineCore.GLCapabilityType.astc_webkit, etc = engineCore.GLCapabilityType.etc, etc_webkit = engineCore.GLCapabilityType.etc_webkit, etc1 = engineCore.GLCapabilityType.etc1, etc1_webkit = engineCore.GLCapabilityType.etc1_webkit, pvrtc = engineCore.GLCapabilityType.pvrtc, pvrtc_webkit = engineCore.GLCapabilityType.pvrtc_webkit, s3tc = engineCore.GLCapabilityType.s3tc, s3tc_webkit = engineCore.GLCapabilityType.s3tc_webkit, bptc = engineCore.GLCapabilityType.bptc, textureFloat = engineCore.GLCapabilityType.textureFloat, textureHalfFloat = engineCore.GLCapabilityType.textureHalfFloat, textureFloatLinear = engineCore.GLCapabilityType.textureFloatLinear, textureHalfFloatLinear = engineCore.GLCapabilityType.textureHalfFloatLinear, WEBGL_colorBufferFloat = engineCore.GLCapabilityType.WEBGL_colorBufferFloat, colorBufferFloat = engineCore.GLCapabilityType.colorBufferFloat, colorBufferHalfFloat = engineCore.GLCapabilityType.colorBufferHalfFloat, textureFilterAnisotropic = engineCore.GLCapabilityType.textureFilterAnisotropic, fragDepth = engineCore.GLCapabilityType.fragDepth;
357
+ var shaderVertexID = engineCore.GLCapabilityType.shaderVertexID, standardDerivatives = engineCore.GLCapabilityType.standardDerivatives, shaderTextureLod = engineCore.GLCapabilityType.shaderTextureLod, elementIndexUint = engineCore.GLCapabilityType.elementIndexUint, depthTexture = engineCore.GLCapabilityType.depthTexture, vertexArrayObject = engineCore.GLCapabilityType.vertexArrayObject, instancedArrays = engineCore.GLCapabilityType.instancedArrays, multipleSample = engineCore.GLCapabilityType.multipleSample, drawBuffers = engineCore.GLCapabilityType.drawBuffers, blendMinMax = engineCore.GLCapabilityType.blendMinMax, astc = engineCore.GLCapabilityType.astc, astc_webkit = engineCore.GLCapabilityType.astc_webkit, etc = engineCore.GLCapabilityType.etc, etc_webkit = engineCore.GLCapabilityType.etc_webkit, etc1 = engineCore.GLCapabilityType.etc1, etc1_webkit = engineCore.GLCapabilityType.etc1_webkit, pvrtc = engineCore.GLCapabilityType.pvrtc, pvrtc_webkit = engineCore.GLCapabilityType.pvrtc_webkit, s3tc = engineCore.GLCapabilityType.s3tc, s3tc_webkit = engineCore.GLCapabilityType.s3tc_webkit, bptc = engineCore.GLCapabilityType.bptc, s3tc_srgb = engineCore.GLCapabilityType.s3tc_srgb, textureFloat = engineCore.GLCapabilityType.textureFloat, textureHalfFloat = engineCore.GLCapabilityType.textureHalfFloat, textureFloatLinear = engineCore.GLCapabilityType.textureFloatLinear, textureHalfFloatLinear = engineCore.GLCapabilityType.textureHalfFloatLinear, WEBGL_colorBufferFloat = engineCore.GLCapabilityType.WEBGL_colorBufferFloat, colorBufferFloat = engineCore.GLCapabilityType.colorBufferFloat, colorBufferHalfFloat = engineCore.GLCapabilityType.colorBufferHalfFloat, textureFilterAnisotropic = engineCore.GLCapabilityType.textureFilterAnisotropic, fragDepth = engineCore.GLCapabilityType.fragDepth, sRGB = engineCore.GLCapabilityType.sRGB;
355
358
  cap.set(shaderVertexID, isWebGL2);
356
359
  cap.set(standardDerivatives, isWebGL2 || !!requireExtension(standardDerivatives));
357
360
  cap.set(shaderTextureLod, isWebGL2 || !!requireExtension(shaderTextureLod));
@@ -375,7 +378,9 @@ var GLBuffer = /*#__PURE__*/ function() {
375
378
  cap.set(etc1, !!(requireExtension(etc1) || requireExtension(etc1_webkit)));
376
379
  cap.set(pvrtc, !!(requireExtension(pvrtc) || requireExtension(pvrtc_webkit)));
377
380
  cap.set(s3tc, !!(requireExtension(s3tc) || requireExtension(s3tc_webkit)));
381
+ cap.set(s3tc_srgb, !!requireExtension(s3tc_srgb));
378
382
  cap.set(bptc, !!requireExtension(bptc));
383
+ cap.set(sRGB, isWebGL2 || !!requireExtension(sRGB));
379
384
  };
380
385
  /**
381
386
  * If there are extensions that can supplement this ability, smooth out the difference.
@@ -402,7 +407,7 @@ var GLBuffer = /*#__PURE__*/ function() {
402
407
  }
403
408
  };
404
409
  _proto._compatibleAllInterface = function _compatibleAllInterface() {
405
- var depthTexture = engineCore.GLCapabilityType.depthTexture, vertexArrayObject = engineCore.GLCapabilityType.vertexArrayObject, instancedArrays = engineCore.GLCapabilityType.instancedArrays, drawBuffers = engineCore.GLCapabilityType.drawBuffers, textureFilterAnisotropic = engineCore.GLCapabilityType.textureFilterAnisotropic, textureHalfFloat = engineCore.GLCapabilityType.textureHalfFloat, colorBufferHalfFloat = engineCore.GLCapabilityType.colorBufferHalfFloat, WEBGL_colorBufferFloat = engineCore.GLCapabilityType.WEBGL_colorBufferFloat, blendMinMax = engineCore.GLCapabilityType.blendMinMax;
410
+ var depthTexture = engineCore.GLCapabilityType.depthTexture, vertexArrayObject = engineCore.GLCapabilityType.vertexArrayObject, instancedArrays = engineCore.GLCapabilityType.instancedArrays, drawBuffers = engineCore.GLCapabilityType.drawBuffers, textureFilterAnisotropic = engineCore.GLCapabilityType.textureFilterAnisotropic, textureHalfFloat = engineCore.GLCapabilityType.textureHalfFloat, colorBufferHalfFloat = engineCore.GLCapabilityType.colorBufferHalfFloat, WEBGL_colorBufferFloat = engineCore.GLCapabilityType.WEBGL_colorBufferFloat, blendMinMax = engineCore.GLCapabilityType.blendMinMax, sRGB = engineCore.GLCapabilityType.sRGB;
406
411
  var isWebGL2 = this.rhi.isWebGL2;
407
412
  if (!isWebGL2) {
408
413
  this._compatibleInterface(blendMinMax, {
@@ -446,6 +451,10 @@ var GLBuffer = /*#__PURE__*/ function() {
446
451
  this._compatibleInterface(WEBGL_colorBufferFloat, {
447
452
  RGBA32F: "RBGA32F_EXT"
448
453
  });
454
+ this._compatibleInterface(sRGB, {
455
+ SRGB8: "SRGB_EXT",
456
+ SRGB8_ALPHA8: "SRGB_ALPHA_EXT"
457
+ });
449
458
  }
450
459
  this._compatibleInterface(textureFilterAnisotropic, {
451
460
  TEXTURE_MAX_ANISOTROPY_EXT: "TEXTURE_MAX_ANISOTROPY_EXT"
@@ -756,8 +765,15 @@ var GLBuffer = /*#__PURE__*/ function() {
756
765
  /**
757
766
  * Generate multi-level textures based on the 0th level data.
758
767
  */ _proto.generateMipmaps = function generateMipmaps() {
768
+ var texture = this._texture;
769
+ //@ts-ignore
770
+ var mipmap = texture._mipmap;
771
+ if (!engineCore.TextureUtils.supportGenerateMipmaps(texture.format, mipmap, texture.isSRGBColorSpace, this._isWebGL2)) {
772
+ engineCore.Logger.warn("Auto-generating mipmaps for sRGB textures is only supported in [WebGL2 + R8G8B8A8], you must generate mipmaps manually.");
773
+ return;
774
+ }
759
775
  // @todo (1x1).generateMipmap() will flash back in uc.
760
- if (this._texture.width !== 1 || this._texture.height !== 1) {
776
+ if (texture.width !== 1 || texture.height !== 1) {
761
777
  this._bind();
762
778
  this._gl.generateMipmap(this._target);
763
779
  }
@@ -808,7 +824,7 @@ var GLBuffer = /*#__PURE__*/ function() {
808
824
  * @param mipLevel - Set mip level the data want to get from
809
825
  */ _proto._getPixelBuffer = function _getPixelBuffer(face, x, y, width, height, mipLevel, out) {
810
826
  var gl = this._gl;
811
- var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
827
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, readFormat = _this__formatDetail.readFormat, alignment = _this__formatDetail.alignment;
812
828
  gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
813
829
  if (mipLevel > 0 && !this._isWebGL2) {
814
830
  mipLevel = 0;
@@ -819,15 +835,44 @@ var GLBuffer = /*#__PURE__*/ function() {
819
835
  } else {
820
836
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, mipLevel);
821
837
  }
822
- gl.readPixels(x, y, width, height, baseFormat, dataType, out);
838
+ gl.pixelStorei(gl.PACK_ALIGNMENT, alignment);
839
+ // Base format is different from read format in webgl1.0 with sRGB
840
+ gl.readPixels(x, y, width, height, readFormat != null ? readFormat : baseFormat, dataType, out);
823
841
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
824
842
  };
843
+ _proto._getReadFrameBuffer = function _getReadFrameBuffer() {
844
+ var frameBuffer = this._rhi._readFrameBuffer;
845
+ if (!frameBuffer) {
846
+ this._rhi._readFrameBuffer = frameBuffer = this._gl.createFramebuffer();
847
+ }
848
+ return frameBuffer;
849
+ };
850
+ _proto._validate = function _validate(texture, rhi) {
851
+ var format = texture.format, width = texture.width, height = texture.height;
852
+ // Validate sRGB format
853
+ // @ts-ignore
854
+ var isSRGBColorSpace = texture._isSRGBColorSpace;
855
+ if (isSRGBColorSpace && !engineCore.TextureUtils.supportSRGB(format)) {
856
+ engineCore.Logger.warn("Only support sRGB color space in RGB8 or RGBA8 or some compressed texture format");
857
+ // @ts-ignore
858
+ texture._isSRGBColorSpace = false;
859
+ }
860
+ var isWebGL2 = rhi.isWebGL2;
861
+ // Validate mipmap
862
+ // @ts-ignore
863
+ var mipmap = texture._mipmap;
864
+ if (mipmap && !engineCore.TextureUtils.supportMipmaps(width, height, isWebGL2)) {
865
+ engineCore.Logger.warn("Non-power-2 texture is not supported for mipmap in WebGL1, and has automatically downgraded to non-mipmap");
866
+ /** @ts-ignore */ texture._mipmap = false;
867
+ /** @ts-ignore */ texture._mipmapCount = texture._getMipmapCount();
868
+ }
869
+ };
825
870
  _proto._setWrapMode = function _setWrapMode(value, pname) {
826
871
  var gl = this._gl;
827
872
  var isWebGL2 = this._isWebGL2;
828
873
  var target = this._target;
829
874
  var _this__texture = this._texture, width = _this__texture.width, height = _this__texture.height;
830
- if (!isWebGL2 && value !== engineCore.TextureWrapMode.Clamp && (!GLTexture._isPowerOf2(width) || !GLTexture._isPowerOf2(height))) {
875
+ if (!isWebGL2 && value !== engineCore.TextureWrapMode.Clamp && (!engineMath.MathUtil.isPowerOf2(width) || !engineMath.MathUtil.isPowerOf2(height))) {
831
876
  engineCore.Logger.warn("non-power-2 texture is not supported for REPEAT or MIRRORED_REPEAT in WebGL1,and has automatically downgraded to CLAMP_TO_EDGE");
832
877
  value = engineCore.TextureWrapMode.Clamp;
833
878
  }
@@ -843,111 +888,134 @@ var GLBuffer = /*#__PURE__*/ function() {
843
888
  break;
844
889
  }
845
890
  };
846
- _proto._getReadFrameBuffer = function _getReadFrameBuffer() {
847
- var frameBuffer = this._rhi._readFrameBuffer;
848
- if (!frameBuffer) {
849
- this._rhi._readFrameBuffer = frameBuffer = this._gl.createFramebuffer();
850
- }
851
- return frameBuffer;
852
- };
853
- /** @internal */ GLTexture._isPowerOf2 = function _isPowerOf2(v) {
854
- return (v & v - 1) === 0;
855
- };
856
891
  /**
857
892
  * Get more texture info from TextureFormat.
858
893
  * @internal
859
- */ GLTexture._getFormatDetail = function _getFormatDetail(format, gl, isWebGL2) {
894
+ */ GLTexture._getFormatDetail = function _getFormatDetail(format, isSRGBColorSpace, gl, isWebGL2) {
860
895
  switch(format){
861
896
  case engineCore.TextureFormat.R8G8B8:
862
897
  return {
863
- internalFormat: isWebGL2 ? gl.RGB8 : gl.RGB,
864
- baseFormat: gl.RGB,
898
+ internalFormat: isSRGBColorSpace ? gl.SRGB8 : isWebGL2 ? gl.RGB8 : gl.RGB,
899
+ baseFormat: isSRGBColorSpace ? isWebGL2 ? gl.RGB : gl.SRGB8 : gl.RGB,
900
+ readFormat: gl.RGB,
865
901
  dataType: gl.UNSIGNED_BYTE,
866
- isCompressed: false
902
+ isCompressed: false,
903
+ alignment: 1
867
904
  };
868
905
  case engineCore.TextureFormat.R8G8B8A8:
869
906
  return {
870
- internalFormat: isWebGL2 ? gl.RGBA8 : gl.RGBA,
871
- baseFormat: gl.RGBA,
907
+ internalFormat: isSRGBColorSpace ? gl.SRGB8_ALPHA8 : isWebGL2 ? gl.RGBA8 : gl.RGBA,
908
+ baseFormat: isSRGBColorSpace ? isWebGL2 ? gl.RGBA : gl.SRGB8_ALPHA8 : gl.RGBA,
909
+ readFormat: gl.RGBA,
872
910
  dataType: gl.UNSIGNED_BYTE,
873
- isCompressed: false
911
+ isCompressed: false,
912
+ alignment: 4
874
913
  };
875
914
  case engineCore.TextureFormat.R4G4B4A4:
876
915
  return {
877
916
  internalFormat: isWebGL2 ? gl.RGBA4 : gl.RGBA,
878
917
  baseFormat: gl.RGBA,
879
918
  dataType: gl.UNSIGNED_SHORT_4_4_4_4,
880
- isCompressed: false
919
+ isCompressed: false,
920
+ alignment: 2
881
921
  };
882
922
  case engineCore.TextureFormat.R5G5B5A1:
883
923
  return {
884
924
  internalFormat: isWebGL2 ? gl.RGB5_A1 : gl.RGBA,
885
925
  baseFormat: gl.RGBA,
886
926
  dataType: gl.UNSIGNED_SHORT_5_5_5_1,
887
- isCompressed: false
927
+ isCompressed: false,
928
+ alignment: 2
888
929
  };
889
930
  case engineCore.TextureFormat.R5G6B5:
890
931
  return {
891
932
  internalFormat: isWebGL2 ? gl.RGB565 : gl.RGB,
892
933
  baseFormat: gl.RGB,
893
934
  dataType: gl.UNSIGNED_SHORT_5_6_5,
894
- isCompressed: false
935
+ isCompressed: false,
936
+ alignment: 2
895
937
  };
896
938
  case engineCore.TextureFormat.Alpha8:
897
939
  return {
898
940
  internalFormat: gl.ALPHA,
899
941
  baseFormat: gl.ALPHA,
900
942
  dataType: gl.UNSIGNED_BYTE,
901
- isCompressed: false
943
+ isCompressed: false,
944
+ alignment: 1
902
945
  };
903
946
  case engineCore.TextureFormat.LuminanceAlpha:
904
947
  return {
905
948
  internalFormat: gl.LUMINANCE_ALPHA,
906
949
  baseFormat: gl.LUMINANCE_ALPHA,
907
950
  dataType: gl.UNSIGNED_BYTE,
908
- isCompressed: false
909
- };
910
- case engineCore.TextureFormat.R11G11B10_UFloat:
911
- return {
912
- internalFormat: isWebGL2 ? gl.R11F_G11F_B10F : gl.NONE,
913
- baseFormat: gl.RGB,
914
- dataType: gl.FLOAT,
915
- isCompressed: false
951
+ isCompressed: false,
952
+ alignment: 2
916
953
  };
917
954
  case engineCore.TextureFormat.R16G16B16A16:
918
955
  return {
919
956
  internalFormat: isWebGL2 ? gl.RGBA16F : gl.RGBA,
920
957
  baseFormat: gl.RGBA,
921
958
  dataType: gl.HALF_FLOAT,
922
- isCompressed: false
959
+ isCompressed: false,
960
+ alignment: 8
923
961
  };
924
962
  case engineCore.TextureFormat.R32G32B32A32:
925
963
  return {
926
964
  internalFormat: isWebGL2 ? gl.RGBA32F : gl.RGBA,
927
965
  baseFormat: gl.RGBA,
928
966
  dataType: gl.FLOAT,
929
- isCompressed: false
967
+ isCompressed: false,
968
+ alignment: 8
969
+ };
970
+ // Only WebGL2 support
971
+ case engineCore.TextureFormat.R11G11B10_UFloat:
972
+ return {
973
+ internalFormat: gl.R11F_G11F_B10F,
974
+ baseFormat: gl.RGB,
975
+ dataType: gl.FLOAT,
976
+ isCompressed: false,
977
+ alignment: 4
930
978
  };
979
+ // Only WebGL2 support
931
980
  case engineCore.TextureFormat.R32G32B32A32_UInt:
932
981
  return {
933
- internalFormat: isWebGL2 ? gl.RGBA32UI : gl.NONE,
982
+ internalFormat: gl.RGBA32UI,
934
983
  baseFormat: gl.RGBA_INTEGER,
935
984
  dataType: gl.UNSIGNED_INT,
936
- isCompressed: false
985
+ isCompressed: false,
986
+ alignment: 8
987
+ };
988
+ // Only WebGL2 support
989
+ case engineCore.TextureFormat.R8:
990
+ return {
991
+ internalFormat: gl.R8,
992
+ baseFormat: gl.RED,
993
+ dataType: gl.UNSIGNED_BYTE,
994
+ isCompressed: false,
995
+ alignment: 1
996
+ };
997
+ // Only WebGL2 support
998
+ case engineCore.TextureFormat.R8G8:
999
+ return {
1000
+ internalFormat: gl.RG8,
1001
+ baseFormat: gl.RG,
1002
+ dataType: gl.UNSIGNED_BYTE,
1003
+ isCompressed: false,
1004
+ alignment: 2
937
1005
  };
938
1006
  case engineCore.TextureFormat.BC1:
939
1007
  return {
940
- internalFormat: GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
1008
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB_S3TC_DXT1_EXT : GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
941
1009
  isCompressed: true
942
1010
  };
943
1011
  case engineCore.TextureFormat.BC3:
944
1012
  return {
945
- internalFormat: GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT,
1013
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB_ALPHA_S3TC_DXT5_EXT : GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT,
946
1014
  isCompressed: true
947
1015
  };
948
1016
  case engineCore.TextureFormat.BC7:
949
1017
  return {
950
- internalFormat: GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT,
1018
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB_ALPHA_BPTC_UNORM_EXT : GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT,
951
1019
  isCompressed: true
952
1020
  };
953
1021
  case engineCore.TextureFormat.ETC1_RGB:
@@ -957,7 +1025,7 @@ var GLBuffer = /*#__PURE__*/ function() {
957
1025
  };
958
1026
  case engineCore.TextureFormat.ETC2_RGB:
959
1027
  return {
960
- internalFormat: GLCompressedTextureInternalFormat.RGB8_ETC2,
1028
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ETC2 : GLCompressedTextureInternalFormat.RGB8_ETC2,
961
1029
  isCompressed: true
962
1030
  };
963
1031
  case engineCore.TextureFormat.ETC2_RGBA5:
@@ -967,7 +1035,7 @@ var GLBuffer = /*#__PURE__*/ function() {
967
1035
  };
968
1036
  case engineCore.TextureFormat.ETC2_RGBA8:
969
1037
  return {
970
- internalFormat: GLCompressedTextureInternalFormat.RGBA8_ETC2_EAC,
1038
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ETC2_EAC : GLCompressedTextureInternalFormat.RGBA8_ETC2_EAC,
971
1039
  isCompressed: true
972
1040
  };
973
1041
  case engineCore.TextureFormat.PVRTC_RGB2:
@@ -992,7 +1060,7 @@ var GLBuffer = /*#__PURE__*/ function() {
992
1060
  };
993
1061
  case engineCore.TextureFormat.ASTC_4x4:
994
1062
  return {
995
- internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_4X4_KHR,
1063
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_4X4_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_4X4_KHR,
996
1064
  isCompressed: true
997
1065
  };
998
1066
  case engineCore.TextureFormat.ASTC_5x5:
@@ -1052,6 +1120,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1052
1120
  isCompressed: false,
1053
1121
  attachment: gl.DEPTH_STENCIL_ATTACHMENT
1054
1122
  };
1123
+ // Only WebGL2 support
1055
1124
  case engineCore.TextureFormat.Depth24:
1056
1125
  return {
1057
1126
  internalFormat: gl.DEPTH_COMPONENT24,
@@ -1060,6 +1129,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1060
1129
  isCompressed: false,
1061
1130
  attachment: gl.DEPTH_ATTACHMENT
1062
1131
  };
1132
+ // Only WebGL2 support
1063
1133
  case engineCore.TextureFormat.Depth32:
1064
1134
  return {
1065
1135
  internalFormat: gl.DEPTH_COMPONENT32F,
@@ -1068,6 +1138,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1068
1138
  isCompressed: false,
1069
1139
  attachment: gl.DEPTH_ATTACHMENT
1070
1140
  };
1141
+ // Only WebGL2 support
1071
1142
  case engineCore.TextureFormat.Depth32Stencil8:
1072
1143
  return {
1073
1144
  internalFormat: gl.DEPTH32F_STENCIL8,
@@ -1154,38 +1225,6 @@ var GLBuffer = /*#__PURE__*/ function() {
1154
1225
  }
1155
1226
  };
1156
1227
  /**
1157
- * Check whether the corresponding texture format is supported.
1158
- * @internal
1159
- */ GLTexture._supportTextureFormat = function _supportTextureFormat(format, rhi) {
1160
- switch(format){
1161
- case engineCore.TextureFormat.R16G16B16A16:
1162
- if (!rhi.canIUse(engineCore.GLCapabilityType.textureHalfFloat)) {
1163
- return false;
1164
- }
1165
- break;
1166
- case engineCore.TextureFormat.R32G32B32A32:
1167
- if (!rhi.canIUse(engineCore.GLCapabilityType.textureFloat)) {
1168
- return false;
1169
- }
1170
- break;
1171
- case engineCore.TextureFormat.Depth16:
1172
- case engineCore.TextureFormat.Depth24Stencil8:
1173
- case engineCore.TextureFormat.Depth:
1174
- case engineCore.TextureFormat.DepthStencil:
1175
- if (!rhi.canIUse(engineCore.GLCapabilityType.depthTexture)) {
1176
- return false;
1177
- }
1178
- break;
1179
- case engineCore.TextureFormat.R11G11B10_UFloat:
1180
- case engineCore.TextureFormat.R32G32B32A32_UInt:
1181
- case engineCore.TextureFormat.Depth24:
1182
- case engineCore.TextureFormat.Depth32:
1183
- case engineCore.TextureFormat.Depth32Stencil8:
1184
- return rhi.isWebGL2;
1185
- }
1186
- return true;
1187
- };
1188
- /**
1189
1228
  * @internal
1190
1229
  */ GLTexture._supportRenderBufferColorFormat = function _supportRenderBufferColorFormat(format, rhi) {
1191
1230
  var isSupported = true;
@@ -1332,10 +1371,13 @@ var GLBuffer = /*#__PURE__*/ function() {
1332
1371
  * MRT + Cube + [,MSAA]
1333
1372
  * MRT + MSAA
1334
1373
  */ for(var i = 0, n = _colorTextures.length; i < n; i++){
1335
- var format = _colorTextures[i]._format;
1374
+ var _colorTextures_i = _colorTextures[i], format = _colorTextures_i.format, isSRGBColorSpace = _colorTextures_i.isSRGBColorSpace;
1336
1375
  if (!GLTexture._supportRenderBufferColorFormat(format, rhi)) {
1337
1376
  throw new Error("TextureFormat is not supported:" + engineCore.TextureFormat[format] + " in RenderTarget");
1338
1377
  }
1378
+ if (isSRGBColorSpace && format === engineCore.TextureFormat.R8G8B8) {
1379
+ throw new Error("If you want to use sRGB color space, only R8G8B8A8 format is supported in RenderTarget");
1380
+ }
1339
1381
  }
1340
1382
  if (!isDepthTexture && !GLTexture._supportRenderBufferDepthFormat(_depth, rhi)) {
1341
1383
  throw new Error("TextureFormat is not supported:" + engineCore.TextureFormat[_depth] + " in RenderTarget");
@@ -1540,17 +1582,10 @@ var GLBuffer = /*#__PURE__*/ function() {
1540
1582
  function GLTexture2D(rhi, texture2D) {
1541
1583
  var _this;
1542
1584
  _this = GLTexture1.call(this, rhi, texture2D, rhi.gl.TEXTURE_2D) || this, /** Backward compatible with WebGL1.0. */ _this._compressedMipFilled = 0;
1543
- /** @ts-ignore */ var format = texture2D.format, _mipmap = texture2D._mipmap, width = texture2D.width, height = texture2D.height;
1585
+ _this._validate(texture2D, rhi);
1586
+ var format = texture2D.format, isSRGBColorSpace = texture2D.isSRGBColorSpace;
1544
1587
  var isWebGL2 = _this._isWebGL2;
1545
- /** @ts-ignore */ if (!GLTexture._supportTextureFormat(format, rhi)) {
1546
- throw new Error("Texture format is not supported:" + engineCore.TextureFormat[format]);
1547
- }
1548
- if (_mipmap && !isWebGL2 && (!GLTexture._isPowerOf2(width) || !GLTexture._isPowerOf2(height))) {
1549
- engineCore.Logger.warn("non-power-2 texture is not supported for mipmap in WebGL1,and has automatically downgraded to non-mipmap");
1550
- /** @ts-ignore */ texture2D._mipmap = false;
1551
- /** @ts-ignore */ texture2D._mipmapCount = texture2D._getMipmapCount();
1552
- }
1553
- _this._formatDetail = GLTexture._getFormatDetail(format, _this._gl, isWebGL2);
1588
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, isWebGL2);
1554
1589
  _this._formatDetail.isCompressed && !isWebGL2 || _this._init(false);
1555
1590
  return _this;
1556
1591
  }
@@ -1560,16 +1595,15 @@ var GLBuffer = /*#__PURE__*/ function() {
1560
1595
  */ _proto.setPixelBuffer = function setPixelBuffer(colorBuffer, mipLevel, x, y, width, height) {
1561
1596
  if (mipLevel === void 0) mipLevel = 0;
1562
1597
  var gl = this._gl;
1563
- var isWebGL2 = this._isWebGL2;
1564
- var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, isCompressed = _this__formatDetail.isCompressed;
1598
+ var formatDetail = this._formatDetail;
1565
1599
  var mipWidth = Math.max(1, this._texture.width >> mipLevel);
1566
1600
  var mipHeight = Math.max(1, this._texture.height >> mipLevel);
1567
1601
  width = width || mipWidth - x;
1568
1602
  height = height || mipHeight - y;
1569
1603
  this._bind();
1570
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1571
- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1572
- if (isCompressed) {
1604
+ if (formatDetail.isCompressed) {
1605
+ var isWebGL2 = this._isWebGL2;
1606
+ var internalFormat = formatDetail.internalFormat;
1573
1607
  var mipBit = 1 << mipLevel;
1574
1608
  if (isWebGL2 || this._compressedMipFilled & mipBit) {
1575
1609
  gl.compressedTexSubImage2D(this._target, mipLevel, x, y, width, height, internalFormat, colorBuffer);
@@ -1578,6 +1612,10 @@ var GLBuffer = /*#__PURE__*/ function() {
1578
1612
  this._compressedMipFilled |= mipBit;
1579
1613
  }
1580
1614
  } else {
1615
+ var baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType;
1616
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1617
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1618
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1581
1619
  gl.texSubImage2D(this._target, mipLevel, x, y, width, height, baseFormat, dataType, colorBuffer);
1582
1620
  }
1583
1621
  };
@@ -1613,15 +1651,10 @@ var GLBuffer = /*#__PURE__*/ function() {
1613
1651
  function GLTexture2DArray(rhi, texture2DArray) {
1614
1652
  var _this;
1615
1653
  _this = GLTexture1.call(this, rhi, texture2DArray, rhi.gl.TEXTURE_2D_ARRAY) || this;
1616
- var format = texture2DArray.format, width = texture2DArray.width, height = texture2DArray.height, length = texture2DArray.length, mipmapCount = texture2DArray.mipmapCount;
1617
- if (!_this._isWebGL2) {
1618
- throw new Error("Texture2D Array is not supported in WebGL1.0");
1619
- }
1620
- /** @ts-ignore */ if (!GLTexture._supportTextureFormat(format, rhi)) {
1621
- throw new Error("Texture format is not supported:" + engineCore.TextureFormat[format]);
1622
- }
1654
+ _this._validate(texture2DArray, rhi);
1655
+ var format = texture2DArray.format, width = texture2DArray.width, height = texture2DArray.height, length = texture2DArray.length, mipmapCount = texture2DArray.mipmapCount, isSRGBColorSpace = texture2DArray.isSRGBColorSpace;
1623
1656
  _this._bind();
1624
- _this._formatDetail = GLTexture._getFormatDetail(format, _this._gl, true);
1657
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, true);
1625
1658
  _this._gl.texStorage3D(_this._target, mipmapCount, _this._formatDetail.internalFormat, width, height, length);
1626
1659
  return _this;
1627
1660
  }
@@ -1630,13 +1663,15 @@ var GLBuffer = /*#__PURE__*/ function() {
1630
1663
  * {@inheritDoc IPlatformTexture2DArray.setPixelBuffer}
1631
1664
  */ _proto.setPixelBuffer = function setPixelBuffer(offsetIndex, colorBuffer, mipLevel, x, y, width, height, length) {
1632
1665
  var _this = this, target = _this._target, gl = _this._gl;
1633
- var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, isCompressed = _this__formatDetail.isCompressed;
1666
+ var formatDetail = this._formatDetail;
1667
+ var internalFormat = formatDetail.internalFormat, baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType, isCompressed = formatDetail.isCompressed;
1634
1668
  width = width || Math.max(1, this._texture.width >> mipLevel) - x;
1635
1669
  height = height || Math.max(1, this._texture.height >> mipLevel) - y;
1636
1670
  length = length || this._texture.length;
1637
1671
  this._bind();
1638
1672
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1639
1673
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1674
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1640
1675
  if (isCompressed) {
1641
1676
  gl.compressedTexSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, internalFormat, colorBuffer);
1642
1677
  } else {
@@ -1661,11 +1696,23 @@ var GLBuffer = /*#__PURE__*/ function() {
1661
1696
  if (formatDetail.isCompressed) {
1662
1697
  throw new Error("Unable to read compressed texture");
1663
1698
  }
1699
+ gl.pixelStorei(gl.PACK_ALIGNMENT, formatDetail.alignment);
1664
1700
  gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
1665
1701
  gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, this._glTexture, mipLevel, elementIndex);
1666
1702
  gl.readPixels(x, y, width, height, formatDetail.baseFormat, formatDetail.dataType, out);
1667
1703
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1668
1704
  };
1705
+ _proto._validate = function _validate(texture, rhi) {
1706
+ var format = texture.format;
1707
+ // Validate sRGB format
1708
+ // @ts-ignore
1709
+ var isSRGBColorSpace = texture._isSRGBColorSpace;
1710
+ if (isSRGBColorSpace && !engineCore.TextureUtils.supportSRGB(format)) {
1711
+ engineCore.Logger.warn("Only support sRGB color space in RGB8 or RGBA8 or some compressed texture format");
1712
+ // @ts-ignore
1713
+ texture._isSRGBColorSpace = false;
1714
+ }
1715
+ };
1669
1716
  return GLTexture2DArray;
1670
1717
  }(GLTexture);
1671
1718
 
@@ -1683,17 +1730,10 @@ var GLBuffer = /*#__PURE__*/ function() {
1683
1730
  0,
1684
1731
  0
1685
1732
  ];
1686
- /** @ts-ignore */ var format = textureCube.format, _mipmap = textureCube._mipmap, size = textureCube.width;
1733
+ _this._validate(textureCube, rhi);
1734
+ var format = textureCube.format, isSRGBColorSpace = textureCube.isSRGBColorSpace;
1687
1735
  var isWebGL2 = _this._isWebGL2;
1688
- /** @ts-ignore */ if (!GLTexture._supportTextureFormat(format, rhi)) {
1689
- throw new Error("Texture format is not supported:" + engineCore.TextureFormat[format]);
1690
- }
1691
- if (_mipmap && !isWebGL2 && !GLTexture._isPowerOf2(size)) {
1692
- engineCore.Logger.warn("non-power-2 texture is not supported for mipmap in WebGL1,and has automatically downgraded to non-mipmap");
1693
- /** @ts-ignore */ textureCube._mipmap = false;
1694
- /** @ts-ignore */ textureCube._mipmapCount = textureCube._getMipmapCount();
1695
- }
1696
- _this._formatDetail = GLTexture._getFormatDetail(format, _this._gl, isWebGL2);
1736
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, isWebGL2);
1697
1737
  _this._formatDetail.isCompressed && !isWebGL2 || _this._init(true);
1698
1738
  return _this;
1699
1739
  }
@@ -1703,13 +1743,15 @@ var GLBuffer = /*#__PURE__*/ function() {
1703
1743
  */ _proto.setPixelBuffer = function setPixelBuffer(face, colorBuffer, mipLevel, x, y, width, height) {
1704
1744
  var gl = this._gl;
1705
1745
  var isWebGL2 = this._isWebGL2;
1706
- var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, isCompressed = _this__formatDetail.isCompressed;
1746
+ var formatDetail = this._formatDetail;
1747
+ var internalFormat = formatDetail.internalFormat, baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType, isCompressed = formatDetail.isCompressed;
1707
1748
  var mipSize = Math.max(1, this._texture.width >> mipLevel);
1708
1749
  width = width || mipSize - x;
1709
1750
  height = height || mipSize - y;
1710
1751
  this._bind();
1711
1752
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1712
1753
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1754
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1713
1755
  if (isCompressed) {
1714
1756
  var mipBit = 1 << mipLevel;
1715
1757
  if (isWebGL2 || this._compressedFaceFilled[face] & mipBit) {