PixelFormat-e6d821ed.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './WebGLConstants-4c11ee5f'], function (exports, when, WebGLConstants) { 'use strict';
  24. /**
  25. * Describes a compressed texture and contains a compressed texture buffer.
  26. * @alias CompressedTextureBuffer
  27. * @constructor
  28. *
  29. * @param {PixelFormat} internalFormat The pixel format of the compressed texture.
  30. * @param {Number} width The width of the texture.
  31. * @param {Number} height The height of the texture.
  32. * @param {Uint8Array} buffer The compressed texture buffer.
  33. */
  34. function CompressedTextureBuffer(internalFormat, width, height, buffer) {
  35. this._format = internalFormat;
  36. this._width = width;
  37. this._height = height;
  38. this._buffer = buffer;
  39. }
  40. Object.defineProperties(CompressedTextureBuffer.prototype, {
  41. /**
  42. * The format of the compressed texture.
  43. * @type PixelFormat
  44. * @readonly
  45. * @memberof CompressedTextureBuffer.prototype
  46. */
  47. internalFormat : {
  48. get : function() {
  49. return this._format;
  50. }
  51. },
  52. /**
  53. * The width of the texture.
  54. * @type Number
  55. * @readonly
  56. * @memberof CompressedTextureBuffer.prototype
  57. */
  58. width : {
  59. get : function() {
  60. return this._width;
  61. }
  62. },
  63. /**
  64. * The height of the texture.
  65. * @type Number
  66. * @readonly
  67. * @memberof CompressedTextureBuffer.prototype
  68. */
  69. height : {
  70. get : function() {
  71. return this._height;
  72. }
  73. },
  74. /**
  75. * The compressed texture buffer.
  76. * @type Uint8Array
  77. * @readonly
  78. * @memberof CompressedTextureBuffer.prototype
  79. */
  80. bufferView : {
  81. get : function() {
  82. return this._buffer;
  83. }
  84. }
  85. });
  86. /**
  87. * Creates a shallow clone of a compressed texture buffer.
  88. *
  89. * @param {CompressedTextureBuffer} object The compressed texture buffer to be cloned.
  90. * @return {CompressedTextureBuffer} A shallow clone of the compressed texture buffer.
  91. */
  92. CompressedTextureBuffer.clone = function(object) {
  93. if (!when.defined(object)) {
  94. return undefined;
  95. }
  96. return new CompressedTextureBuffer(object._format, object._width, object._height, object._buffer);
  97. };
  98. /**
  99. * Creates a shallow clone of this compressed texture buffer.
  100. *
  101. * @return {CompressedTextureBuffer} A shallow clone of the compressed texture buffer.
  102. */
  103. CompressedTextureBuffer.prototype.clone = function() {
  104. return CompressedTextureBuffer.clone(this);
  105. };
  106. /**
  107. * @private
  108. */
  109. var PixelDatatype = {
  110. UNSIGNED_BYTE : WebGLConstants.WebGLConstants.UNSIGNED_BYTE,
  111. UNSIGNED_SHORT : WebGLConstants.WebGLConstants.UNSIGNED_SHORT,
  112. UNSIGNED_INT : WebGLConstants.WebGLConstants.UNSIGNED_INT,
  113. FLOAT : WebGLConstants.WebGLConstants.FLOAT,
  114. HALF_FLOAT : WebGLConstants.WebGLConstants.HALF_FLOAT_OES,
  115. UNSIGNED_INT_24_8 : WebGLConstants.WebGLConstants.UNSIGNED_INT_24_8,
  116. UNSIGNED_SHORT_4_4_4_4 : WebGLConstants.WebGLConstants.UNSIGNED_SHORT_4_4_4_4,
  117. UNSIGNED_SHORT_5_5_5_1 : WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_5_5_1,
  118. UNSIGNED_SHORT_5_6_5 : WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_6_5,
  119. isPacked : function(pixelDatatype) {
  120. return pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8 ||
  121. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4 ||
  122. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1 ||
  123. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5;
  124. },
  125. sizeInBytes : function(pixelDatatype) {
  126. switch (pixelDatatype) {
  127. case PixelDatatype.UNSIGNED_BYTE:
  128. return 1;
  129. case PixelDatatype.UNSIGNED_SHORT:
  130. case PixelDatatype.UNSIGNED_SHORT_4_4_4_4:
  131. case PixelDatatype.UNSIGNED_SHORT_5_5_5_1:
  132. case PixelDatatype.UNSIGNED_SHORT_5_6_5:
  133. case PixelDatatype.HALF_FLOAT:
  134. return 2;
  135. case PixelDatatype.UNSIGNED_INT:
  136. case PixelDatatype.FLOAT:
  137. case PixelDatatype.UNSIGNED_INT_24_8:
  138. return 4;
  139. }
  140. },
  141. validate : function(pixelDatatype) {
  142. return ((pixelDatatype === PixelDatatype.UNSIGNED_BYTE) ||
  143. (pixelDatatype === PixelDatatype.UNSIGNED_SHORT) ||
  144. (pixelDatatype === PixelDatatype.UNSIGNED_INT) ||
  145. (pixelDatatype === PixelDatatype.FLOAT) ||
  146. (pixelDatatype === PixelDatatype.HALF_FLOAT) ||
  147. (pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8) ||
  148. (pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4) ||
  149. (pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1) ||
  150. (pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5));
  151. }
  152. };
  153. /**
  154. * The format of a pixel, i.e., the number of components it has and what they represent.
  155. *
  156. * @exports PixelFormat
  157. */
  158. var PixelFormat = {
  159. /**
  160. * A pixel format containing a depth value.
  161. *
  162. * @type {Number}
  163. * @constant
  164. */
  165. DEPTH_COMPONENT : WebGLConstants.WebGLConstants.DEPTH_COMPONENT,
  166. /**
  167. * A pixel format containing a depth and stencil value, most often used with {@link PixelDatatype.UNSIGNED_INT_24_8}.
  168. *
  169. * @type {Number}
  170. * @constant
  171. */
  172. DEPTH_STENCIL : WebGLConstants.WebGLConstants.DEPTH_STENCIL,
  173. /**
  174. * A pixel format containing an alpha channel.
  175. *
  176. * @type {Number}
  177. * @constant
  178. */
  179. ALPHA : WebGLConstants.WebGLConstants.ALPHA,
  180. /**
  181. * A pixel format containing red, green, and blue channels.
  182. *
  183. * @type {Number}
  184. * @constant
  185. */
  186. RGB : WebGLConstants.WebGLConstants.RGB,
  187. /**
  188. * A pixel format containing red, green, blue, and alpha channels.
  189. *
  190. * @type {Number}
  191. * @constant
  192. */
  193. RGBA : WebGLConstants.WebGLConstants.RGBA,
  194. /**
  195. * A pixel format containing a luminance (intensity) channel.
  196. *
  197. * @type {Number}
  198. * @constant
  199. */
  200. LUMINANCE : WebGLConstants.WebGLConstants.LUMINANCE,
  201. /**
  202. * A pixel format containing luminance (intensity) and alpha channels.
  203. *
  204. * @type {Number}
  205. * @constant
  206. */
  207. LUMINANCE_ALPHA : WebGLConstants.WebGLConstants.LUMINANCE_ALPHA,
  208. /**
  209. * A pixel format containing red, green, and blue channels that is DXT1 compressed.
  210. *
  211. * @type {Number}
  212. * @constant
  213. */
  214. RGB_DXT1 : WebGLConstants.WebGLConstants.COMPRESSED_RGB_S3TC_DXT1_EXT,
  215. /**
  216. * A pixel format containing red, green, blue, and alpha channels that is DXT1 compressed.
  217. *
  218. * @type {Number}
  219. * @constant
  220. */
  221. RGBA_DXT1 : WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT1_EXT,
  222. /**
  223. * A pixel format containing red, green, blue, and alpha channels that is DXT3 compressed.
  224. *
  225. * @type {Number}
  226. * @constant
  227. */
  228. RGBA_DXT3 : WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT3_EXT,
  229. /**
  230. * A pixel format containing red, green, blue, and alpha channels that is DXT5 compressed.
  231. *
  232. * @type {Number}
  233. * @constant
  234. */
  235. RGBA_DXT5 : WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT5_EXT,
  236. /**
  237. * A pixel format containing red, green, and blue channels that is PVR 4bpp compressed.
  238. *
  239. * @type {Number}
  240. * @constant
  241. */
  242. RGB_PVRTC_4BPPV1 : WebGLConstants.WebGLConstants.COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
  243. /**
  244. * A pixel format containing red, green, and blue channels that is PVR 2bpp compressed.
  245. *
  246. * @type {Number}
  247. * @constant
  248. */
  249. RGB_PVRTC_2BPPV1 : WebGLConstants.WebGLConstants.COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
  250. /**
  251. * A pixel format containing red, green, blue, and alpha channels that is PVR 4bpp compressed.
  252. *
  253. * @type {Number}
  254. * @constant
  255. */
  256. RGBA_PVRTC_4BPPV1 : WebGLConstants.WebGLConstants.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
  257. /**
  258. * A pixel format containing red, green, blue, and alpha channels that is PVR 2bpp compressed.
  259. *
  260. * @type {Number}
  261. * @constant
  262. */
  263. RGBA_PVRTC_2BPPV1 : WebGLConstants.WebGLConstants.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
  264. /**
  265. * A pixel format containing red, green, and blue channels that is ETC1 compressed.
  266. *
  267. * @type {Number}
  268. * @constant
  269. */
  270. RGB_ETC1 : WebGLConstants.WebGLConstants.COMPRESSED_RGB_ETC1_WEBGL,
  271. /**
  272. * @private
  273. */
  274. componentsLength : function(pixelFormat) {
  275. switch (pixelFormat) {
  276. case PixelFormat.RGB:
  277. return 3;
  278. case PixelFormat.RGBA:
  279. return 4;
  280. case PixelFormat.LUMINANCE_ALPHA:
  281. return 2;
  282. case PixelFormat.ALPHA:
  283. case PixelFormat.LUMINANCE:
  284. return 1;
  285. default:
  286. return 1;
  287. }
  288. },
  289. /**
  290. * @private
  291. */
  292. validate : function(pixelFormat) {
  293. return pixelFormat === PixelFormat.DEPTH_COMPONENT ||
  294. pixelFormat === PixelFormat.DEPTH_STENCIL ||
  295. pixelFormat === PixelFormat.ALPHA ||
  296. pixelFormat === PixelFormat.RGB ||
  297. pixelFormat === PixelFormat.RGBA ||
  298. pixelFormat === PixelFormat.LUMINANCE ||
  299. pixelFormat === PixelFormat.LUMINANCE_ALPHA ||
  300. pixelFormat === PixelFormat.RGB_DXT1 ||
  301. pixelFormat === PixelFormat.RGBA_DXT1 ||
  302. pixelFormat === PixelFormat.RGBA_DXT3 ||
  303. pixelFormat === PixelFormat.RGBA_DXT5 ||
  304. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  305. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  306. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  307. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1 ||
  308. pixelFormat === PixelFormat.RGB_ETC1;
  309. },
  310. /**
  311. * @private
  312. */
  313. isColorFormat : function(pixelFormat) {
  314. return pixelFormat === PixelFormat.ALPHA ||
  315. pixelFormat === PixelFormat.RGB ||
  316. pixelFormat === PixelFormat.RGBA ||
  317. pixelFormat === PixelFormat.LUMINANCE ||
  318. pixelFormat === PixelFormat.LUMINANCE_ALPHA;
  319. },
  320. /**
  321. * @private
  322. */
  323. isDepthFormat : function(pixelFormat) {
  324. return pixelFormat === PixelFormat.DEPTH_COMPONENT ||
  325. pixelFormat === PixelFormat.DEPTH_STENCIL;
  326. },
  327. /**
  328. * @private
  329. */
  330. isCompressedFormat : function(pixelFormat) {
  331. return pixelFormat === PixelFormat.RGB_DXT1 ||
  332. pixelFormat === PixelFormat.RGBA_DXT1 ||
  333. pixelFormat === PixelFormat.RGBA_DXT3 ||
  334. pixelFormat === PixelFormat.RGBA_DXT5 ||
  335. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  336. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  337. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  338. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1 ||
  339. pixelFormat === PixelFormat.RGB_ETC1;
  340. },
  341. /**
  342. * @private
  343. */
  344. isDXTFormat : function(pixelFormat) {
  345. return pixelFormat === PixelFormat.RGB_DXT1 ||
  346. pixelFormat === PixelFormat.RGBA_DXT1 ||
  347. pixelFormat === PixelFormat.RGBA_DXT3 ||
  348. pixelFormat === PixelFormat.RGBA_DXT5;
  349. },
  350. /**
  351. * @private
  352. */
  353. isPVRTCFormat : function(pixelFormat) {
  354. return pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  355. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  356. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  357. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1;
  358. },
  359. /**
  360. * @private
  361. */
  362. isETC1Format : function(pixelFormat) {
  363. return pixelFormat === PixelFormat.RGB_ETC1;
  364. },
  365. /**
  366. * @private
  367. */
  368. compressedTextureSizeInBytes : function(pixelFormat, width, height) {
  369. switch (pixelFormat) {
  370. case PixelFormat.RGB_DXT1:
  371. case PixelFormat.RGBA_DXT1:
  372. case PixelFormat.RGB_ETC1:
  373. return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 8;
  374. case PixelFormat.RGBA_DXT3:
  375. case PixelFormat.RGBA_DXT5:
  376. return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 16;
  377. case PixelFormat.RGB_PVRTC_4BPPV1:
  378. case PixelFormat.RGBA_PVRTC_4BPPV1:
  379. return Math.floor((Math.max(width, 8) * Math.max(height, 8) * 4 + 7) / 8);
  380. case PixelFormat.RGB_PVRTC_2BPPV1:
  381. case PixelFormat.RGBA_PVRTC_2BPPV1:
  382. return Math.floor((Math.max(width, 16) * Math.max(height, 8) * 2 + 7) / 8);
  383. default:
  384. return 0;
  385. }
  386. },
  387. /**
  388. * @private
  389. */
  390. textureSizeInBytes : function(pixelFormat, pixelDatatype, width, height) {
  391. var componentsLength = PixelFormat.componentsLength(pixelFormat);
  392. if (PixelDatatype.isPacked(pixelDatatype)) {
  393. componentsLength = 1;
  394. }
  395. return componentsLength * PixelDatatype.sizeInBytes(pixelDatatype) * width * height;
  396. },
  397. /**
  398. * @private
  399. */
  400. alignmentInBytes : function(pixelFormat, pixelDatatype, width) {
  401. var mod = PixelFormat.textureSizeInBytes(pixelFormat, pixelDatatype, width, 1) % 4;
  402. return mod === 0 ? 4 : (mod === 2 ? 2 : 1);
  403. },
  404. /**
  405. * @private
  406. */
  407. createTypedArray : function(pixelFormat, pixelDatatype, width, height) {
  408. var constructor;
  409. var sizeInBytes = PixelDatatype.sizeInBytes(pixelDatatype);
  410. if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) {
  411. constructor = Uint8Array;
  412. } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) {
  413. constructor = Uint16Array;
  414. } else if (sizeInBytes === Float32Array.BYTES_PER_ELEMENT && pixelDatatype === PixelDatatype.FLOAT) {
  415. constructor = Float32Array;
  416. } else {
  417. constructor = Uint32Array;
  418. }
  419. var size = PixelFormat.componentsLength(pixelFormat) * width * height;
  420. return new constructor(size);
  421. },
  422. /**
  423. * @private
  424. */
  425. flipY : function(bufferView, pixelFormat, pixelDatatype, width, height) {
  426. if (height === 1) {
  427. return bufferView;
  428. }
  429. var flipped = PixelFormat.createTypedArray(pixelFormat, pixelDatatype, width, height);
  430. var numberOfComponents = PixelFormat.componentsLength(pixelFormat);
  431. var textureWidth = width * numberOfComponents;
  432. for (var i = 0; i < height; ++i) {
  433. var row = i * height * numberOfComponents;
  434. var flippedRow = (height - i - 1) * height * numberOfComponents;
  435. for (var j = 0; j < textureWidth; ++j) {
  436. flipped[flippedRow + j] = bufferView[row + j];
  437. }
  438. }
  439. return flipped;
  440. }
  441. };
  442. var PixelFormat$1 = Object.freeze(PixelFormat);
  443. exports.CompressedTextureBuffer = CompressedTextureBuffer;
  444. exports.PixelFormat = PixelFormat$1;
  445. });