transcodeKTX2.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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/main/LICENSE.md for full licensing details.
  22. */
  23. define(['./defaultValue-81eec7ed', './RuntimeError-8952249c', './WebGLConstants-508b9636', './createTaskProcessorWorker'], (function (defaultValue, RuntimeError, WebGLConstants, createTaskProcessorWorker) { 'use strict';
  24. /**
  25. * The data type of a pixel.
  26. *
  27. * @enum {Number}
  28. * @see PostProcessStage
  29. */
  30. const PixelDatatype = {
  31. UNSIGNED_BYTE: WebGLConstants.WebGLConstants.UNSIGNED_BYTE,
  32. UNSIGNED_SHORT: WebGLConstants.WebGLConstants.UNSIGNED_SHORT,
  33. UNSIGNED_INT: WebGLConstants.WebGLConstants.UNSIGNED_INT,
  34. FLOAT: WebGLConstants.WebGLConstants.FLOAT,
  35. HALF_FLOAT: WebGLConstants.WebGLConstants.HALF_FLOAT_OES,
  36. UNSIGNED_INT_24_8: WebGLConstants.WebGLConstants.UNSIGNED_INT_24_8,
  37. UNSIGNED_SHORT_4_4_4_4: WebGLConstants.WebGLConstants.UNSIGNED_SHORT_4_4_4_4,
  38. UNSIGNED_SHORT_5_5_5_1: WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_5_5_1,
  39. UNSIGNED_SHORT_5_6_5: WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_6_5,
  40. };
  41. /**
  42. @private
  43. */
  44. PixelDatatype.toWebGLConstant = function (pixelDatatype, context) {
  45. switch (pixelDatatype) {
  46. case PixelDatatype.UNSIGNED_BYTE:
  47. return WebGLConstants.WebGLConstants.UNSIGNED_BYTE;
  48. case PixelDatatype.UNSIGNED_SHORT:
  49. return WebGLConstants.WebGLConstants.UNSIGNED_SHORT;
  50. case PixelDatatype.UNSIGNED_INT:
  51. return WebGLConstants.WebGLConstants.UNSIGNED_INT;
  52. case PixelDatatype.FLOAT:
  53. return WebGLConstants.WebGLConstants.FLOAT;
  54. case PixelDatatype.HALF_FLOAT:
  55. return context.webgl2
  56. ? WebGLConstants.WebGLConstants.HALF_FLOAT
  57. : WebGLConstants.WebGLConstants.HALF_FLOAT_OES;
  58. case PixelDatatype.UNSIGNED_INT_24_8:
  59. return WebGLConstants.WebGLConstants.UNSIGNED_INT_24_8;
  60. case PixelDatatype.UNSIGNED_SHORT_4_4_4_4:
  61. return WebGLConstants.WebGLConstants.UNSIGNED_SHORT_4_4_4_4;
  62. case PixelDatatype.UNSIGNED_SHORT_5_5_5_1:
  63. return WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_5_5_1;
  64. case PixelDatatype.UNSIGNED_SHORT_5_6_5:
  65. return PixelDatatype.UNSIGNED_SHORT_5_6_5;
  66. }
  67. };
  68. /**
  69. @private
  70. */
  71. PixelDatatype.isPacked = function (pixelDatatype) {
  72. return (
  73. pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8 ||
  74. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4 ||
  75. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1 ||
  76. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5
  77. );
  78. };
  79. /**
  80. @private
  81. */
  82. PixelDatatype.sizeInBytes = function (pixelDatatype) {
  83. switch (pixelDatatype) {
  84. case PixelDatatype.UNSIGNED_BYTE:
  85. return 1;
  86. case PixelDatatype.UNSIGNED_SHORT:
  87. case PixelDatatype.UNSIGNED_SHORT_4_4_4_4:
  88. case PixelDatatype.UNSIGNED_SHORT_5_5_5_1:
  89. case PixelDatatype.UNSIGNED_SHORT_5_6_5:
  90. case PixelDatatype.HALF_FLOAT:
  91. return 2;
  92. case PixelDatatype.UNSIGNED_INT:
  93. case PixelDatatype.FLOAT:
  94. case PixelDatatype.UNSIGNED_INT_24_8:
  95. return 4;
  96. }
  97. };
  98. /**
  99. @private
  100. */
  101. PixelDatatype.validate = function (pixelDatatype) {
  102. return (
  103. pixelDatatype === PixelDatatype.UNSIGNED_BYTE ||
  104. pixelDatatype === PixelDatatype.UNSIGNED_SHORT ||
  105. pixelDatatype === PixelDatatype.UNSIGNED_INT ||
  106. pixelDatatype === PixelDatatype.FLOAT ||
  107. pixelDatatype === PixelDatatype.HALF_FLOAT ||
  108. pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8 ||
  109. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4 ||
  110. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1 ||
  111. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5
  112. );
  113. };
  114. var PixelDatatype$1 = Object.freeze(PixelDatatype);
  115. /**
  116. * The format of a pixel, i.e., the number of components it has and what they represent.
  117. *
  118. * @enum {Number}
  119. */
  120. const PixelFormat = {
  121. /**
  122. * A pixel format containing a depth value.
  123. *
  124. * @type {Number}
  125. * @constant
  126. */
  127. DEPTH_COMPONENT: WebGLConstants.WebGLConstants.DEPTH_COMPONENT,
  128. /**
  129. * A pixel format containing a depth and stencil value, most often used with {@link PixelDatatype.UNSIGNED_INT_24_8}.
  130. *
  131. * @type {Number}
  132. * @constant
  133. */
  134. DEPTH_STENCIL: WebGLConstants.WebGLConstants.DEPTH_STENCIL,
  135. /**
  136. * A pixel format containing an alpha channel.
  137. *
  138. * @type {Number}
  139. * @constant
  140. */
  141. ALPHA: WebGLConstants.WebGLConstants.ALPHA,
  142. /**
  143. * A pixel format containing red, green, and blue channels.
  144. *
  145. * @type {Number}
  146. * @constant
  147. */
  148. RGB: WebGLConstants.WebGLConstants.RGB,
  149. /**
  150. * A pixel format containing red, green, blue, and alpha channels.
  151. *
  152. * @type {Number}
  153. * @constant
  154. */
  155. RGBA: WebGLConstants.WebGLConstants.RGBA,
  156. /**
  157. * A pixel format containing a luminance (intensity) channel.
  158. *
  159. * @type {Number}
  160. * @constant
  161. */
  162. LUMINANCE: WebGLConstants.WebGLConstants.LUMINANCE,
  163. /**
  164. * A pixel format containing luminance (intensity) and alpha channels.
  165. *
  166. * @type {Number}
  167. * @constant
  168. */
  169. LUMINANCE_ALPHA: WebGLConstants.WebGLConstants.LUMINANCE_ALPHA,
  170. /**
  171. * A pixel format containing red, green, and blue channels that is DXT1 compressed.
  172. *
  173. * @type {Number}
  174. * @constant
  175. */
  176. RGB_DXT1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_S3TC_DXT1_EXT,
  177. /**
  178. * A pixel format containing red, green, blue, and alpha channels that is DXT1 compressed.
  179. *
  180. * @type {Number}
  181. * @constant
  182. */
  183. RGBA_DXT1: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT1_EXT,
  184. /**
  185. * A pixel format containing red, green, blue, and alpha channels that is DXT3 compressed.
  186. *
  187. * @type {Number}
  188. * @constant
  189. */
  190. RGBA_DXT3: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT3_EXT,
  191. /**
  192. * A pixel format containing red, green, blue, and alpha channels that is DXT5 compressed.
  193. *
  194. * @type {Number}
  195. * @constant
  196. */
  197. RGBA_DXT5: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT5_EXT,
  198. /**
  199. * A pixel format containing red, green, and blue channels that is PVR 4bpp compressed.
  200. *
  201. * @type {Number}
  202. * @constant
  203. */
  204. RGB_PVRTC_4BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
  205. /**
  206. * A pixel format containing red, green, and blue channels that is PVR 2bpp compressed.
  207. *
  208. * @type {Number}
  209. * @constant
  210. */
  211. RGB_PVRTC_2BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
  212. /**
  213. * A pixel format containing red, green, blue, and alpha channels that is PVR 4bpp compressed.
  214. *
  215. * @type {Number}
  216. * @constant
  217. */
  218. RGBA_PVRTC_4BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
  219. /**
  220. * A pixel format containing red, green, blue, and alpha channels that is PVR 2bpp compressed.
  221. *
  222. * @type {Number}
  223. * @constant
  224. */
  225. RGBA_PVRTC_2BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
  226. /**
  227. * A pixel format containing red, green, blue, and alpha channels that is ASTC compressed.
  228. *
  229. * @type {Number}
  230. * @constant
  231. */
  232. RGBA_ASTC: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_ASTC_4x4_WEBGL,
  233. /**
  234. * A pixel format containing red, green, and blue channels that is ETC1 compressed.
  235. *
  236. * @type {Number}
  237. * @constant
  238. */
  239. RGB_ETC1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_ETC1_WEBGL,
  240. /**
  241. * A pixel format containing red, green, and blue channels that is ETC2 compressed.
  242. *
  243. * @type {Number}
  244. * @constant
  245. */
  246. RGB8_ETC2: WebGLConstants.WebGLConstants.COMPRESSED_RGB8_ETC2,
  247. /**
  248. * A pixel format containing red, green, blue, and alpha channels that is ETC2 compressed.
  249. *
  250. * @type {Number}
  251. * @constant
  252. */
  253. RGBA8_ETC2_EAC: WebGLConstants.WebGLConstants.COMPRESSED_RGBA8_ETC2_EAC,
  254. /**
  255. * A pixel format containing red, green, blue, and alpha channels that is BC7 compressed.
  256. *
  257. * @type {Number}
  258. * @constant
  259. */
  260. RGBA_BC7: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_BPTC_UNORM,
  261. };
  262. /**
  263. * @private
  264. */
  265. PixelFormat.componentsLength = function (pixelFormat) {
  266. switch (pixelFormat) {
  267. case PixelFormat.RGB:
  268. return 3;
  269. case PixelFormat.RGBA:
  270. return 4;
  271. case PixelFormat.LUMINANCE_ALPHA:
  272. return 2;
  273. case PixelFormat.ALPHA:
  274. case PixelFormat.LUMINANCE:
  275. return 1;
  276. default:
  277. return 1;
  278. }
  279. };
  280. /**
  281. * @private
  282. */
  283. PixelFormat.validate = function (pixelFormat) {
  284. return (
  285. pixelFormat === PixelFormat.DEPTH_COMPONENT ||
  286. pixelFormat === PixelFormat.DEPTH_STENCIL ||
  287. pixelFormat === PixelFormat.ALPHA ||
  288. pixelFormat === PixelFormat.RGB ||
  289. pixelFormat === PixelFormat.RGBA ||
  290. pixelFormat === PixelFormat.LUMINANCE ||
  291. pixelFormat === PixelFormat.LUMINANCE_ALPHA ||
  292. pixelFormat === PixelFormat.RGB_DXT1 ||
  293. pixelFormat === PixelFormat.RGBA_DXT1 ||
  294. pixelFormat === PixelFormat.RGBA_DXT3 ||
  295. pixelFormat === PixelFormat.RGBA_DXT5 ||
  296. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  297. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  298. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  299. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1 ||
  300. pixelFormat === PixelFormat.RGBA_ASTC ||
  301. pixelFormat === PixelFormat.RGB_ETC1 ||
  302. pixelFormat === PixelFormat.RGB8_ETC2 ||
  303. pixelFormat === PixelFormat.RGBA8_ETC2_EAC ||
  304. pixelFormat === PixelFormat.RGBA_BC7
  305. );
  306. };
  307. /**
  308. * @private
  309. */
  310. PixelFormat.isColorFormat = function (pixelFormat) {
  311. return (
  312. pixelFormat === PixelFormat.ALPHA ||
  313. pixelFormat === PixelFormat.RGB ||
  314. pixelFormat === PixelFormat.RGBA ||
  315. pixelFormat === PixelFormat.LUMINANCE ||
  316. pixelFormat === PixelFormat.LUMINANCE_ALPHA
  317. );
  318. };
  319. /**
  320. * @private
  321. */
  322. PixelFormat.isDepthFormat = function (pixelFormat) {
  323. return (
  324. pixelFormat === PixelFormat.DEPTH_COMPONENT ||
  325. pixelFormat === PixelFormat.DEPTH_STENCIL
  326. );
  327. };
  328. /**
  329. * @private
  330. */
  331. PixelFormat.isCompressedFormat = function (pixelFormat) {
  332. return (
  333. pixelFormat === PixelFormat.RGB_DXT1 ||
  334. pixelFormat === PixelFormat.RGBA_DXT1 ||
  335. pixelFormat === PixelFormat.RGBA_DXT3 ||
  336. pixelFormat === PixelFormat.RGBA_DXT5 ||
  337. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  338. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  339. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  340. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1 ||
  341. pixelFormat === PixelFormat.RGBA_ASTC ||
  342. pixelFormat === PixelFormat.RGB_ETC1 ||
  343. pixelFormat === PixelFormat.RGB8_ETC2 ||
  344. pixelFormat === PixelFormat.RGBA8_ETC2_EAC ||
  345. pixelFormat === PixelFormat.RGBA_BC7
  346. );
  347. };
  348. /**
  349. * @private
  350. */
  351. PixelFormat.isDXTFormat = function (pixelFormat) {
  352. return (
  353. pixelFormat === PixelFormat.RGB_DXT1 ||
  354. pixelFormat === PixelFormat.RGBA_DXT1 ||
  355. pixelFormat === PixelFormat.RGBA_DXT3 ||
  356. pixelFormat === PixelFormat.RGBA_DXT5
  357. );
  358. };
  359. /**
  360. * @private
  361. */
  362. PixelFormat.isPVRTCFormat = function (pixelFormat) {
  363. return (
  364. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  365. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  366. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  367. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1
  368. );
  369. };
  370. /**
  371. * @private
  372. */
  373. PixelFormat.isASTCFormat = function (pixelFormat) {
  374. return pixelFormat === PixelFormat.RGBA_ASTC;
  375. };
  376. /**
  377. * @private
  378. */
  379. PixelFormat.isETC1Format = function (pixelFormat) {
  380. return pixelFormat === PixelFormat.RGB_ETC1;
  381. };
  382. /**
  383. * @private
  384. */
  385. PixelFormat.isETC2Format = function (pixelFormat) {
  386. return (
  387. pixelFormat === PixelFormat.RGB8_ETC2 ||
  388. pixelFormat === PixelFormat.RGBA8_ETC2_EAC
  389. );
  390. };
  391. /**
  392. * @private
  393. */
  394. PixelFormat.isBC7Format = function (pixelFormat) {
  395. return pixelFormat === PixelFormat.RGBA_BC7;
  396. };
  397. /**
  398. * @private
  399. */
  400. PixelFormat.compressedTextureSizeInBytes = function (
  401. pixelFormat,
  402. width,
  403. height
  404. ) {
  405. switch (pixelFormat) {
  406. case PixelFormat.RGB_DXT1:
  407. case PixelFormat.RGBA_DXT1:
  408. case PixelFormat.RGB_ETC1:
  409. case PixelFormat.RGB8_ETC2:
  410. return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 8;
  411. case PixelFormat.RGBA_DXT3:
  412. case PixelFormat.RGBA_DXT5:
  413. case PixelFormat.RGBA_ASTC:
  414. case PixelFormat.RGBA8_ETC2_EAC:
  415. return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 16;
  416. case PixelFormat.RGB_PVRTC_4BPPV1:
  417. case PixelFormat.RGBA_PVRTC_4BPPV1:
  418. return Math.floor((Math.max(width, 8) * Math.max(height, 8) * 4 + 7) / 8);
  419. case PixelFormat.RGB_PVRTC_2BPPV1:
  420. case PixelFormat.RGBA_PVRTC_2BPPV1:
  421. return Math.floor(
  422. (Math.max(width, 16) * Math.max(height, 8) * 2 + 7) / 8
  423. );
  424. case PixelFormat.RGBA_BC7:
  425. return Math.ceil(width / 4) * Math.ceil(height / 4) * 16;
  426. default:
  427. return 0;
  428. }
  429. };
  430. /**
  431. * @private
  432. */
  433. PixelFormat.textureSizeInBytes = function (
  434. pixelFormat,
  435. pixelDatatype,
  436. width,
  437. height
  438. ) {
  439. let componentsLength = PixelFormat.componentsLength(pixelFormat);
  440. if (PixelDatatype$1.isPacked(pixelDatatype)) {
  441. componentsLength = 1;
  442. }
  443. return (
  444. componentsLength * PixelDatatype$1.sizeInBytes(pixelDatatype) * width * height
  445. );
  446. };
  447. /**
  448. * @private
  449. */
  450. PixelFormat.alignmentInBytes = function (pixelFormat, pixelDatatype, width) {
  451. const mod =
  452. PixelFormat.textureSizeInBytes(pixelFormat, pixelDatatype, width, 1) % 4;
  453. return mod === 0 ? 4 : mod === 2 ? 2 : 1;
  454. };
  455. /**
  456. * @private
  457. */
  458. PixelFormat.createTypedArray = function (
  459. pixelFormat,
  460. pixelDatatype,
  461. width,
  462. height
  463. ) {
  464. let constructor;
  465. const sizeInBytes = PixelDatatype$1.sizeInBytes(pixelDatatype);
  466. if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) {
  467. constructor = Uint8Array;
  468. } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) {
  469. constructor = Uint16Array;
  470. } else if (
  471. sizeInBytes === Float32Array.BYTES_PER_ELEMENT &&
  472. pixelDatatype === PixelDatatype$1.FLOAT
  473. ) {
  474. constructor = Float32Array;
  475. } else {
  476. constructor = Uint32Array;
  477. }
  478. const size = PixelFormat.componentsLength(pixelFormat) * width * height;
  479. return new constructor(size);
  480. };
  481. /**
  482. * @private
  483. */
  484. PixelFormat.flipY = function (
  485. bufferView,
  486. pixelFormat,
  487. pixelDatatype,
  488. width,
  489. height
  490. ) {
  491. if (height === 1) {
  492. return bufferView;
  493. }
  494. const flipped = PixelFormat.createTypedArray(
  495. pixelFormat,
  496. pixelDatatype,
  497. width,
  498. height
  499. );
  500. const numberOfComponents = PixelFormat.componentsLength(pixelFormat);
  501. const textureWidth = width * numberOfComponents;
  502. for (let i = 0; i < height; ++i) {
  503. const row = i * width * numberOfComponents;
  504. const flippedRow = (height - i - 1) * width * numberOfComponents;
  505. for (let j = 0; j < textureWidth; ++j) {
  506. flipped[flippedRow + j] = bufferView[row + j];
  507. }
  508. }
  509. return flipped;
  510. };
  511. /**
  512. * @private
  513. */
  514. PixelFormat.toInternalFormat = function (pixelFormat, pixelDatatype, context) {
  515. // WebGL 1 require internalFormat to be the same as PixelFormat
  516. if (!context.webgl2) {
  517. return pixelFormat;
  518. }
  519. // Convert pixelFormat to correct internalFormat for WebGL 2
  520. if (pixelFormat === PixelFormat.DEPTH_STENCIL) {
  521. return WebGLConstants.WebGLConstants.DEPTH24_STENCIL8;
  522. }
  523. if (pixelFormat === PixelFormat.DEPTH_COMPONENT) {
  524. if (pixelDatatype === PixelDatatype$1.UNSIGNED_SHORT) {
  525. return WebGLConstants.WebGLConstants.DEPTH_COMPONENT16;
  526. } else if (pixelDatatype === PixelDatatype$1.UNSIGNED_INT) {
  527. return WebGLConstants.WebGLConstants.DEPTH_COMPONENT24;
  528. }
  529. }
  530. if (pixelDatatype === PixelDatatype$1.FLOAT) {
  531. switch (pixelFormat) {
  532. case PixelFormat.RGBA:
  533. return WebGLConstants.WebGLConstants.RGBA32F;
  534. case PixelFormat.RGB:
  535. return WebGLConstants.WebGLConstants.RGB32F;
  536. case PixelFormat.RG:
  537. return WebGLConstants.WebGLConstants.RG32F;
  538. case PixelFormat.R:
  539. return WebGLConstants.WebGLConstants.R32F;
  540. }
  541. }
  542. if (pixelDatatype === PixelDatatype$1.HALF_FLOAT) {
  543. switch (pixelFormat) {
  544. case PixelFormat.RGBA:
  545. return WebGLConstants.WebGLConstants.RGBA16F;
  546. case PixelFormat.RGB:
  547. return WebGLConstants.WebGLConstants.RGB16F;
  548. case PixelFormat.RG:
  549. return WebGLConstants.WebGLConstants.RG16F;
  550. case PixelFormat.R:
  551. return WebGLConstants.WebGLConstants.R16F;
  552. }
  553. }
  554. return pixelFormat;
  555. };
  556. var PixelFormat$1 = Object.freeze(PixelFormat);
  557. /**
  558. * Enum containing Vulkan Constant values by name.
  559. *
  560. * These match the constants from the {@link https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#formats-definition|Vulkan 1.2 specification}.
  561. *
  562. * @enum {Number}
  563. * @private
  564. */
  565. const VulkanConstants = {
  566. VK_FORMAT_UNDEFINED: 0,
  567. VK_FORMAT_R4G4_UNORM_PACK8: 1,
  568. VK_FORMAT_R4G4B4A4_UNORM_PACK16: 2,
  569. VK_FORMAT_B4G4R4A4_UNORM_PACK16: 3,
  570. VK_FORMAT_R5G6B5_UNORM_PACK16: 4,
  571. VK_FORMAT_B5G6R5_UNORM_PACK16: 5,
  572. VK_FORMAT_R5G5B5A1_UNORM_PACK16: 6,
  573. VK_FORMAT_B5G5R5A1_UNORM_PACK16: 7,
  574. VK_FORMAT_A1R5G5B5_UNORM_PACK16: 8,
  575. VK_FORMAT_R8_UNORM: 9,
  576. VK_FORMAT_R8_SNORM: 10,
  577. VK_FORMAT_R8_USCALED: 11,
  578. VK_FORMAT_R8_SSCALED: 12,
  579. VK_FORMAT_R8_UINT: 13,
  580. VK_FORMAT_R8_SINT: 14,
  581. VK_FORMAT_R8_SRGB: 15,
  582. VK_FORMAT_R8G8_UNORM: 16,
  583. VK_FORMAT_R8G8_SNORM: 17,
  584. VK_FORMAT_R8G8_USCALED: 18,
  585. VK_FORMAT_R8G8_SSCALED: 19,
  586. VK_FORMAT_R8G8_UINT: 20,
  587. VK_FORMAT_R8G8_SINT: 21,
  588. VK_FORMAT_R8G8_SRGB: 22,
  589. VK_FORMAT_R8G8B8_UNORM: 23,
  590. VK_FORMAT_R8G8B8_SNORM: 24,
  591. VK_FORMAT_R8G8B8_USCALED: 25,
  592. VK_FORMAT_R8G8B8_SSCALED: 26,
  593. VK_FORMAT_R8G8B8_UINT: 27,
  594. VK_FORMAT_R8G8B8_SINT: 28,
  595. VK_FORMAT_R8G8B8_SRGB: 29,
  596. VK_FORMAT_B8G8R8_UNORM: 30,
  597. VK_FORMAT_B8G8R8_SNORM: 31,
  598. VK_FORMAT_B8G8R8_USCALED: 32,
  599. VK_FORMAT_B8G8R8_SSCALED: 33,
  600. VK_FORMAT_B8G8R8_UINT: 34,
  601. VK_FORMAT_B8G8R8_SINT: 35,
  602. VK_FORMAT_B8G8R8_SRGB: 36,
  603. VK_FORMAT_R8G8B8A8_UNORM: 37,
  604. VK_FORMAT_R8G8B8A8_SNORM: 38,
  605. VK_FORMAT_R8G8B8A8_USCALED: 39,
  606. VK_FORMAT_R8G8B8A8_SSCALED: 40,
  607. VK_FORMAT_R8G8B8A8_UINT: 41,
  608. VK_FORMAT_R8G8B8A8_SINT: 42,
  609. VK_FORMAT_R8G8B8A8_SRGB: 43,
  610. VK_FORMAT_B8G8R8A8_UNORM: 44,
  611. VK_FORMAT_B8G8R8A8_SNORM: 45,
  612. VK_FORMAT_B8G8R8A8_USCALED: 46,
  613. VK_FORMAT_B8G8R8A8_SSCALED: 47,
  614. VK_FORMAT_B8G8R8A8_UINT: 48,
  615. VK_FORMAT_B8G8R8A8_SINT: 49,
  616. VK_FORMAT_B8G8R8A8_SRGB: 50,
  617. VK_FORMAT_A8B8G8R8_UNORM_PACK32: 51,
  618. VK_FORMAT_A8B8G8R8_SNORM_PACK32: 52,
  619. VK_FORMAT_A8B8G8R8_USCALED_PACK32: 53,
  620. VK_FORMAT_A8B8G8R8_SSCALED_PACK32: 54,
  621. VK_FORMAT_A8B8G8R8_UINT_PACK32: 55,
  622. VK_FORMAT_A8B8G8R8_SINT_PACK32: 56,
  623. VK_FORMAT_A8B8G8R8_SRGB_PACK32: 57,
  624. VK_FORMAT_A2R10G10B10_UNORM_PACK32: 58,
  625. VK_FORMAT_A2R10G10B10_SNORM_PACK32: 59,
  626. VK_FORMAT_A2R10G10B10_USCALED_PACK32: 60,
  627. VK_FORMAT_A2R10G10B10_SSCALED_PACK32: 61,
  628. VK_FORMAT_A2R10G10B10_UINT_PACK32: 62,
  629. VK_FORMAT_A2R10G10B10_SINT_PACK32: 63,
  630. VK_FORMAT_A2B10G10R10_UNORM_PACK32: 64,
  631. VK_FORMAT_A2B10G10R10_SNORM_PACK32: 65,
  632. VK_FORMAT_A2B10G10R10_USCALED_PACK32: 66,
  633. VK_FORMAT_A2B10G10R10_SSCALED_PACK32: 67,
  634. VK_FORMAT_A2B10G10R10_UINT_PACK32: 68,
  635. VK_FORMAT_A2B10G10R10_SINT_PACK32: 69,
  636. VK_FORMAT_R16_UNORM: 70,
  637. VK_FORMAT_R16_SNORM: 71,
  638. VK_FORMAT_R16_USCALED: 72,
  639. VK_FORMAT_R16_SSCALED: 73,
  640. VK_FORMAT_R16_UINT: 74,
  641. VK_FORMAT_R16_SINT: 75,
  642. VK_FORMAT_R16_SFLOAT: 76,
  643. VK_FORMAT_R16G16_UNORM: 77,
  644. VK_FORMAT_R16G16_SNORM: 78,
  645. VK_FORMAT_R16G16_USCALED: 79,
  646. VK_FORMAT_R16G16_SSCALED: 80,
  647. VK_FORMAT_R16G16_UINT: 81,
  648. VK_FORMAT_R16G16_SINT: 82,
  649. VK_FORMAT_R16G16_SFLOAT: 83,
  650. VK_FORMAT_R16G16B16_UNORM: 84,
  651. VK_FORMAT_R16G16B16_SNORM: 85,
  652. VK_FORMAT_R16G16B16_USCALED: 86,
  653. VK_FORMAT_R16G16B16_SSCALED: 87,
  654. VK_FORMAT_R16G16B16_UINT: 88,
  655. VK_FORMAT_R16G16B16_SINT: 89,
  656. VK_FORMAT_R16G16B16_SFLOAT: 90,
  657. VK_FORMAT_R16G16B16A16_UNORM: 91,
  658. VK_FORMAT_R16G16B16A16_SNORM: 92,
  659. VK_FORMAT_R16G16B16A16_USCALED: 93,
  660. VK_FORMAT_R16G16B16A16_SSCALED: 94,
  661. VK_FORMAT_R16G16B16A16_UINT: 95,
  662. VK_FORMAT_R16G16B16A16_SINT: 96,
  663. VK_FORMAT_R16G16B16A16_SFLOAT: 97,
  664. VK_FORMAT_R32_UINT: 98,
  665. VK_FORMAT_R32_SINT: 99,
  666. VK_FORMAT_R32_SFLOAT: 100,
  667. VK_FORMAT_R32G32_UINT: 101,
  668. VK_FORMAT_R32G32_SINT: 102,
  669. VK_FORMAT_R32G32_SFLOAT: 103,
  670. VK_FORMAT_R32G32B32_UINT: 104,
  671. VK_FORMAT_R32G32B32_SINT: 105,
  672. VK_FORMAT_R32G32B32_SFLOAT: 106,
  673. VK_FORMAT_R32G32B32A32_UINT: 107,
  674. VK_FORMAT_R32G32B32A32_SINT: 108,
  675. VK_FORMAT_R32G32B32A32_SFLOAT: 109,
  676. VK_FORMAT_R64_UINT: 110,
  677. VK_FORMAT_R64_SINT: 111,
  678. VK_FORMAT_R64_SFLOAT: 112,
  679. VK_FORMAT_R64G64_UINT: 113,
  680. VK_FORMAT_R64G64_SINT: 114,
  681. VK_FORMAT_R64G64_SFLOAT: 115,
  682. VK_FORMAT_R64G64B64_UINT: 116,
  683. VK_FORMAT_R64G64B64_SINT: 117,
  684. VK_FORMAT_R64G64B64_SFLOAT: 118,
  685. VK_FORMAT_R64G64B64A64_UINT: 119,
  686. VK_FORMAT_R64G64B64A64_SINT: 120,
  687. VK_FORMAT_R64G64B64A64_SFLOAT: 121,
  688. VK_FORMAT_B10G11R11_UFLOAT_PACK32: 122,
  689. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: 123,
  690. VK_FORMAT_D16_UNORM: 124,
  691. VK_FORMAT_X8_D24_UNORM_PACK32: 125,
  692. VK_FORMAT_D32_SFLOAT: 126,
  693. VK_FORMAT_S8_UINT: 127,
  694. VK_FORMAT_D16_UNORM_S8_UINT: 128,
  695. VK_FORMAT_D24_UNORM_S8_UINT: 129,
  696. VK_FORMAT_D32_SFLOAT_S8_UINT: 130,
  697. VK_FORMAT_BC1_RGB_UNORM_BLOCK: 131,
  698. VK_FORMAT_BC1_RGB_SRGB_BLOCK: 132,
  699. VK_FORMAT_BC1_RGBA_UNORM_BLOCK: 133,
  700. VK_FORMAT_BC1_RGBA_SRGB_BLOCK: 134,
  701. VK_FORMAT_BC2_UNORM_BLOCK: 135,
  702. VK_FORMAT_BC2_SRGB_BLOCK: 136,
  703. VK_FORMAT_BC3_UNORM_BLOCK: 137,
  704. VK_FORMAT_BC3_SRGB_BLOCK: 138,
  705. VK_FORMAT_BC4_UNORM_BLOCK: 139,
  706. VK_FORMAT_BC4_SNORM_BLOCK: 140,
  707. VK_FORMAT_BC5_UNORM_BLOCK: 141,
  708. VK_FORMAT_BC5_SNORM_BLOCK: 142,
  709. VK_FORMAT_BC6H_UFLOAT_BLOCK: 143,
  710. VK_FORMAT_BC6H_SFLOAT_BLOCK: 144,
  711. VK_FORMAT_BC7_UNORM_BLOCK: 145,
  712. VK_FORMAT_BC7_SRGB_BLOCK: 146,
  713. VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: 147,
  714. VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK: 148,
  715. VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK: 149,
  716. VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK: 150,
  717. VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK: 151,
  718. VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK: 152,
  719. VK_FORMAT_EAC_R11_UNORM_BLOCK: 153,
  720. VK_FORMAT_EAC_R11_SNORM_BLOCK: 154,
  721. VK_FORMAT_EAC_R11G11_UNORM_BLOCK: 155,
  722. VK_FORMAT_EAC_R11G11_SNORM_BLOCK: 156,
  723. VK_FORMAT_ASTC_4x4_UNORM_BLOCK: 157,
  724. VK_FORMAT_ASTC_4x4_SRGB_BLOCK: 158,
  725. VK_FORMAT_ASTC_5x4_UNORM_BLOCK: 159,
  726. VK_FORMAT_ASTC_5x4_SRGB_BLOCK: 160,
  727. VK_FORMAT_ASTC_5x5_UNORM_BLOCK: 161,
  728. VK_FORMAT_ASTC_5x5_SRGB_BLOCK: 162,
  729. VK_FORMAT_ASTC_6x5_UNORM_BLOCK: 163,
  730. VK_FORMAT_ASTC_6x5_SRGB_BLOCK: 164,
  731. VK_FORMAT_ASTC_6x6_UNORM_BLOCK: 165,
  732. VK_FORMAT_ASTC_6x6_SRGB_BLOCK: 166,
  733. VK_FORMAT_ASTC_8x5_UNORM_BLOCK: 167,
  734. VK_FORMAT_ASTC_8x5_SRGB_BLOCK: 168,
  735. VK_FORMAT_ASTC_8x6_UNORM_BLOCK: 169,
  736. VK_FORMAT_ASTC_8x6_SRGB_BLOCK: 170,
  737. VK_FORMAT_ASTC_8x8_UNORM_BLOCK: 171,
  738. VK_FORMAT_ASTC_8x8_SRGB_BLOCK: 172,
  739. VK_FORMAT_ASTC_10x5_UNORM_BLOCK: 173,
  740. VK_FORMAT_ASTC_10x5_SRGB_BLOCK: 174,
  741. VK_FORMAT_ASTC_10x6_UNORM_BLOCK: 175,
  742. VK_FORMAT_ASTC_10x6_SRGB_BLOCK: 176,
  743. VK_FORMAT_ASTC_10x8_UNORM_BLOCK: 177,
  744. VK_FORMAT_ASTC_10x8_SRGB_BLOCK: 178,
  745. VK_FORMAT_ASTC_10x10_UNORM_BLOCK: 179,
  746. VK_FORMAT_ASTC_10x10_SRGB_BLOCK: 180,
  747. VK_FORMAT_ASTC_12x10_UNORM_BLOCK: 181,
  748. VK_FORMAT_ASTC_12x10_SRGB_BLOCK: 182,
  749. VK_FORMAT_ASTC_12x12_UNORM_BLOCK: 183,
  750. VK_FORMAT_ASTC_12x12_SRGB_BLOCK: 184,
  751. VK_FORMAT_G8B8G8R8_422_UNORM: 1000156000,
  752. VK_FORMAT_B8G8R8G8_422_UNORM: 1000156001,
  753. VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM: 1000156002,
  754. VK_FORMAT_G8_B8R8_2PLANE_420_UNORM: 1000156003,
  755. VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM: 1000156004,
  756. VK_FORMAT_G8_B8R8_2PLANE_422_UNORM: 1000156005,
  757. VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM: 1000156006,
  758. VK_FORMAT_R10X6_UNORM_PACK16: 1000156007,
  759. VK_FORMAT_R10X6G10X6_UNORM_2PACK16: 1000156008,
  760. VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16: 1000156009,
  761. VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16: 1000156010,
  762. VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16: 1000156011,
  763. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16: 1000156012,
  764. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16: 1000156013,
  765. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16: 1000156014,
  766. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16: 1000156015,
  767. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16: 1000156016,
  768. VK_FORMAT_R12X4_UNORM_PACK16: 1000156017,
  769. VK_FORMAT_R12X4G12X4_UNORM_2PACK16: 1000156018,
  770. VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16: 1000156019,
  771. VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16: 1000156020,
  772. VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16: 1000156021,
  773. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16: 1000156022,
  774. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16: 1000156023,
  775. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16: 1000156024,
  776. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16: 1000156025,
  777. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16: 1000156026,
  778. VK_FORMAT_G16B16G16R16_422_UNORM: 1000156027,
  779. VK_FORMAT_B16G16R16G16_422_UNORM: 1000156028,
  780. VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM: 1000156029,
  781. VK_FORMAT_G16_B16R16_2PLANE_420_UNORM: 1000156030,
  782. VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM: 1000156031,
  783. VK_FORMAT_G16_B16R16_2PLANE_422_UNORM: 1000156032,
  784. VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM: 1000156033,
  785. VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG: 1000054000,
  786. VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG: 1000054001,
  787. VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG: 1000054002,
  788. VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG: 1000054003,
  789. VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG: 1000054004,
  790. VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG: 1000054005,
  791. VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG: 1000054006,
  792. VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG: 1000054007,
  793. VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT: 1000066000,
  794. VK_FORMAT_ASTC_5x4_SFLOAT_BLOCK_EXT: 1000066001,
  795. VK_FORMAT_ASTC_5x5_SFLOAT_BLOCK_EXT: 1000066002,
  796. VK_FORMAT_ASTC_6x5_SFLOAT_BLOCK_EXT: 1000066003,
  797. VK_FORMAT_ASTC_6x6_SFLOAT_BLOCK_EXT: 1000066004,
  798. VK_FORMAT_ASTC_8x5_SFLOAT_BLOCK_EXT: 1000066005,
  799. VK_FORMAT_ASTC_8x6_SFLOAT_BLOCK_EXT: 1000066006,
  800. VK_FORMAT_ASTC_8x8_SFLOAT_BLOCK_EXT: 1000066007,
  801. VK_FORMAT_ASTC_10x5_SFLOAT_BLOCK_EXT: 1000066008,
  802. VK_FORMAT_ASTC_10x6_SFLOAT_BLOCK_EXT: 1000066009,
  803. VK_FORMAT_ASTC_10x8_SFLOAT_BLOCK_EXT: 1000066010,
  804. VK_FORMAT_ASTC_10x10_SFLOAT_BLOCK_EXT: 1000066011,
  805. VK_FORMAT_ASTC_12x10_SFLOAT_BLOCK_EXT: 1000066012,
  806. VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK_EXT: 1000066013,
  807. VK_FORMAT_G8B8G8R8_422_UNORM_KHR: 1000156000,
  808. VK_FORMAT_B8G8R8G8_422_UNORM_KHR: 1000156001,
  809. VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR: 1000156002,
  810. VK_FORMAT_G8_B8R8_2PLANE_420_UNORM_KHR: 1000156003,
  811. VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM_KHR: 1000156004,
  812. VK_FORMAT_G8_B8R8_2PLANE_422_UNORM_KHR: 1000156005,
  813. VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM_KHR: 1000156006,
  814. VK_FORMAT_R10X6_UNORM_PACK16_KHR: 1000156007,
  815. VK_FORMAT_R10X6G10X6_UNORM_2PACK16_KHR: 1000156008,
  816. VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16_KHR: 1000156009,
  817. VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16_KHR: 1000156010,
  818. VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16_KHR: 1000156011,
  819. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16_KHR: 1000156012,
  820. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16_KHR: 1000156013,
  821. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16_KHR: 1000156014,
  822. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16_KHR: 1000156015,
  823. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16_KHR: 1000156016,
  824. VK_FORMAT_R12X4_UNORM_PACK16_KHR: 1000156017,
  825. VK_FORMAT_R12X4G12X4_UNORM_2PACK16_KHR: 1000156018,
  826. VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16_KHR: 1000156019,
  827. VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16_KHR: 1000156020,
  828. VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16_KHR: 1000156021,
  829. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16_KHR: 1000156022,
  830. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16_KHR: 1000156023,
  831. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16_KHR: 1000156024,
  832. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16_KHR: 1000156025,
  833. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16_KHR: 1000156026,
  834. VK_FORMAT_G16B16G16R16_422_UNORM_KHR: 1000156027,
  835. VK_FORMAT_B16G16R16G16_422_UNORM_KHR: 1000156028,
  836. VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM_KHR: 1000156029,
  837. VK_FORMAT_G16_B16R16_2PLANE_420_UNORM_KHR: 1000156030,
  838. VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM_KHR: 1000156031,
  839. VK_FORMAT_G16_B16R16_2PLANE_422_UNORM_KHR: 1000156032,
  840. VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM_KHR: 1000156033,
  841. };
  842. var VulkanConstants$1 = Object.freeze(VulkanConstants);
  843. /* This file is automatically rebuilt by the Cesium build process. */
  844. const e=[171,75,84,88,32,50,48,187,13,10,26,10];var n,i,s,a,r,o,l,f;!function(t){t[t.NONE=0]="NONE",t[t.BASISLZ=1]="BASISLZ",t[t.ZSTD=2]="ZSTD",t[t.ZLIB=3]="ZLIB";}(n||(n={})),function(t){t[t.BASICFORMAT=0]="BASICFORMAT";}(i||(i={})),function(t){t[t.UNSPECIFIED=0]="UNSPECIFIED",t[t.ETC1S=163]="ETC1S",t[t.UASTC=166]="UASTC";}(s||(s={})),function(t){t[t.UNSPECIFIED=0]="UNSPECIFIED",t[t.SRGB=1]="SRGB";}(a||(a={})),function(t){t[t.UNSPECIFIED=0]="UNSPECIFIED",t[t.LINEAR=1]="LINEAR",t[t.SRGB=2]="SRGB",t[t.ITU=3]="ITU",t[t.NTSC=4]="NTSC",t[t.SLOG=5]="SLOG",t[t.SLOG2=6]="SLOG2";}(r||(r={})),function(t){t[t.ALPHA_STRAIGHT=0]="ALPHA_STRAIGHT",t[t.ALPHA_PREMULTIPLIED=1]="ALPHA_PREMULTIPLIED";}(o||(o={})),function(t){t[t.RGB=0]="RGB",t[t.RRR=3]="RRR",t[t.GGG=4]="GGG",t[t.AAA=15]="AAA";}(l||(l={})),function(t){t[t.RGB=0]="RGB",t[t.RGBA=3]="RGBA",t[t.RRR=4]="RRR",t[t.RRRG=5]="RRRG";}(f||(f={}));class U{constructor(){this.vkFormat=0,this.typeSize=1,this.pixelWidth=0,this.pixelHeight=0,this.pixelDepth=0,this.layerCount=0,this.faceCount=1,this.supercompressionScheme=n.NONE,this.levels=[],this.dataFormatDescriptor=[{vendorId:0,descriptorType:i.BASICFORMAT,versionNumber:2,descriptorBlockSize:40,colorModel:s.UNSPECIFIED,colorPrimaries:a.SRGB,transferFunction:a.SRGB,flags:o.ALPHA_STRAIGHT,texelBlockDimension:{x:4,y:4,z:1,w:1},bytesPlane:[],samples:[]}],this.keyValue={},this.globalData=null;}}class c{constructor(t,e,n,i){this._dataView=new DataView(t.buffer,t.byteOffset+e,n),this._littleEndian=i,this._offset=0;}_nextUint8(){const t=this._dataView.getUint8(this._offset);return this._offset+=1,t}_nextUint16(){const t=this._dataView.getUint16(this._offset,this._littleEndian);return this._offset+=2,t}_nextUint32(){const t=this._dataView.getUint32(this._offset,this._littleEndian);return this._offset+=4,t}_nextUint64(){const t=this._dataView.getUint32(this._offset,this._littleEndian)+2**32*this._dataView.getUint32(this._offset+4,this._littleEndian);return this._offset+=8,t}_skip(t){return this._offset+=t,this}_scan(t,e=0){const n=this._offset;let i=0;for(;this._dataView.getUint8(this._offset)!==e&&i<t;)i++,this._offset++;return i<t&&this._offset++,new Uint8Array(this._dataView.buffer,this._dataView.byteOffset+n,i)}}function _(t){return "undefined"!=typeof TextDecoder?(new TextDecoder).decode(t):Buffer.from(t).toString("utf8")}function p(t){const n=new Uint8Array(t.buffer,t.byteOffset,e.length);if(n[0]!==e[0]||n[1]!==e[1]||n[2]!==e[2]||n[3]!==e[3]||n[4]!==e[4]||n[5]!==e[5]||n[6]!==e[6]||n[7]!==e[7]||n[8]!==e[8]||n[9]!==e[9]||n[10]!==e[10]||n[11]!==e[11])throw new Error("Missing KTX 2.0 identifier.");const i=new U,s=17*Uint32Array.BYTES_PER_ELEMENT,a=new c(t,e.length,s,!0);i.vkFormat=a._nextUint32(),i.typeSize=a._nextUint32(),i.pixelWidth=a._nextUint32(),i.pixelHeight=a._nextUint32(),i.pixelDepth=a._nextUint32(),i.layerCount=a._nextUint32(),i.faceCount=a._nextUint32();const r=a._nextUint32();i.supercompressionScheme=a._nextUint32();const o=a._nextUint32(),l=a._nextUint32(),f=a._nextUint32(),h=a._nextUint32(),g=a._nextUint64(),p=a._nextUint64(),x=new c(t,e.length+s,3*r*8,!0);for(let e=0;e<r;e++)i.levels.push({levelData:new Uint8Array(t.buffer,t.byteOffset+x._nextUint64(),x._nextUint64()),uncompressedByteLength:x._nextUint64()});const u=new c(t,o,l,!0),y={vendorId:u._skip(4)._nextUint16(),descriptorType:u._nextUint16(),versionNumber:u._nextUint16(),descriptorBlockSize:u._nextUint16(),colorModel:u._nextUint8(),colorPrimaries:u._nextUint8(),transferFunction:u._nextUint8(),flags:u._nextUint8(),texelBlockDimension:{x:u._nextUint8()+1,y:u._nextUint8()+1,z:u._nextUint8()+1,w:u._nextUint8()+1},bytesPlane:[u._nextUint8(),u._nextUint8(),u._nextUint8(),u._nextUint8(),u._nextUint8(),u._nextUint8(),u._nextUint8(),u._nextUint8()],samples:[]},D=(y.descriptorBlockSize/4-6)/4;for(let t=0;t<D;t++)y.samples[t]={bitOffset:u._nextUint16(),bitLength:u._nextUint8(),channelID:u._nextUint8(),samplePosition:[u._nextUint8(),u._nextUint8(),u._nextUint8(),u._nextUint8()],sampleLower:u._nextUint32(),sampleUpper:u._nextUint32()};i.dataFormatDescriptor.length=0,i.dataFormatDescriptor.push(y);const b=new c(t,f,h,!0);for(;b._offset<h;){const t=b._nextUint32(),e=b._scan(t),n=_(e),s=b._scan(t-e.byteLength);i.keyValue[n]=n.match(/^ktx/i)?_(s):s,b._offset%4&&b._skip(4-b._offset%4);}if(p<=0)return i;const d=new c(t,g,p,!0),B=d._nextUint16(),w=d._nextUint16(),A=d._nextUint32(),S=d._nextUint32(),m=d._nextUint32(),L=d._nextUint32(),I=[];for(let t=0;t<r;t++)I.push({imageFlags:d._nextUint32(),rgbSliceByteOffset:d._nextUint32(),rgbSliceByteLength:d._nextUint32(),alphaSliceByteOffset:d._nextUint32(),alphaSliceByteLength:d._nextUint32()});const R=g+d._offset,E=R+A,T=E+S,O=T+m,P=new Uint8Array(t.buffer,t.byteOffset+R,A),C=new Uint8Array(t.buffer,t.byteOffset+E,S),F=new Uint8Array(t.buffer,t.byteOffset+T,m),G=new Uint8Array(t.buffer,t.byteOffset+O,L);return i.globalData={endpointCount:B,selectorCount:w,imageDescs:I,endpointsData:P,selectorsData:C,tablesData:F,extendedData:G},i}
  845. /* global require */
  846. const faceOrder = [
  847. "positiveX",
  848. "negativeX",
  849. "positiveY",
  850. "negativeY",
  851. "positiveZ",
  852. "negativeZ",
  853. ];
  854. // Flags
  855. const colorModelETC1S = 163;
  856. const colorModelUASTC = 166;
  857. let transcoderModule;
  858. function transcode(parameters, transferableObjects) {
  859. //>>includeStart('debug', pragmas.debug);
  860. RuntimeError.Check.typeOf.object("transcoderModule", transcoderModule);
  861. //>>includeEnd('debug');
  862. const data = parameters.ktx2Buffer;
  863. const supportedTargetFormats = parameters.supportedTargetFormats;
  864. let header;
  865. try {
  866. header = p(data);
  867. } catch (e) {
  868. throw new RuntimeError.RuntimeError("Invalid KTX2 file.");
  869. }
  870. if (header.layerCount !== 0) {
  871. throw new RuntimeError.RuntimeError("KTX2 texture arrays are not supported.");
  872. }
  873. if (header.pixelDepth !== 0) {
  874. throw new RuntimeError.RuntimeError("KTX2 3D textures are unsupported.");
  875. }
  876. const dfd = header.dataFormatDescriptor[0];
  877. const result = new Array(header.levelCount);
  878. if (
  879. header.vkFormat === 0x0 &&
  880. (dfd.colorModel === colorModelETC1S || dfd.colorModel === colorModelUASTC)
  881. ) {
  882. // Compressed, initialize transcoder module
  883. transcodeCompressed(
  884. data,
  885. header,
  886. supportedTargetFormats,
  887. transcoderModule,
  888. transferableObjects,
  889. result
  890. );
  891. } else {
  892. transferableObjects.push(data.buffer);
  893. parseUncompressed(header, result);
  894. }
  895. return result;
  896. }
  897. // Parser for uncompressed
  898. function parseUncompressed(header, result) {
  899. const internalFormat =
  900. header.vkFormat === VulkanConstants$1.VK_FORMAT_R8G8B8_SRGB
  901. ? PixelFormat$1.RGB
  902. : PixelFormat$1.RGBA;
  903. let datatype;
  904. if (header.vkFormat === VulkanConstants$1.VK_FORMAT_R8G8B8A8_UNORM) {
  905. datatype = PixelDatatype$1.UNSIGNED_BYTE;
  906. } else if (
  907. header.vkFormat === VulkanConstants$1.VK_FORMAT_R16G16B16A16_SFLOAT
  908. ) {
  909. datatype = PixelDatatype$1.HALF_FLOAT;
  910. } else if (
  911. header.vkFormat === VulkanConstants$1.VK_FORMAT_R32G32B32A32_SFLOAT
  912. ) {
  913. datatype = PixelDatatype$1.FLOAT;
  914. }
  915. for (let i = 0; i < header.levels.length; ++i) {
  916. const level = {};
  917. result[i] = level;
  918. const levelBuffer = header.levels[i].levelData;
  919. const width = header.pixelWidth >> i;
  920. const height = header.pixelHeight >> i;
  921. const faceLength =
  922. width * height * PixelFormat$1.componentsLength(internalFormat);
  923. for (let j = 0; j < header.faceCount; ++j) {
  924. // multiply levelBuffer.byteOffset by the size in bytes of the pixel data type
  925. const faceByteOffset =
  926. levelBuffer.byteOffset + faceLength * header.typeSize * j;
  927. let faceView;
  928. if (!defaultValue.defined(datatype) || PixelDatatype$1.sizeInBytes(datatype) === 1) {
  929. faceView = new Uint8Array(
  930. levelBuffer.buffer,
  931. faceByteOffset,
  932. faceLength
  933. );
  934. } else if (PixelDatatype$1.sizeInBytes(datatype) === 2) {
  935. faceView = new Uint16Array(
  936. levelBuffer.buffer,
  937. faceByteOffset,
  938. faceLength
  939. );
  940. } else {
  941. faceView = new Float32Array(
  942. levelBuffer.buffer,
  943. faceByteOffset,
  944. faceLength
  945. );
  946. }
  947. level[faceOrder[j]] = {
  948. internalFormat: internalFormat,
  949. datatype: datatype,
  950. width: width,
  951. height: height,
  952. levelBuffer: faceView,
  953. };
  954. }
  955. }
  956. }
  957. function transcodeCompressed(
  958. data,
  959. header,
  960. supportedTargetFormats,
  961. transcoderModule,
  962. transferableObjects,
  963. result
  964. ) {
  965. const ktx2File = new transcoderModule.KTX2File(data);
  966. let width = ktx2File.getWidth();
  967. let height = ktx2File.getHeight();
  968. const levels = ktx2File.getLevels();
  969. const hasAlpha = ktx2File.getHasAlpha();
  970. if (!(width > 0) || !(height > 0) || !(levels > 0)) {
  971. ktx2File.close();
  972. ktx2File.delete();
  973. throw new RuntimeError.RuntimeError("Invalid KTX2 file");
  974. }
  975. let internalFormat, transcoderFormat;
  976. const dfd = header.dataFormatDescriptor[0];
  977. const BasisFormat = transcoderModule.transcoder_texture_format;
  978. // Determine target format based on platform support
  979. if (dfd.colorModel === colorModelETC1S) {
  980. if (supportedTargetFormats.etc) {
  981. internalFormat = hasAlpha
  982. ? PixelFormat$1.RGBA8_ETC2_EAC
  983. : PixelFormat$1.RGB8_ETC2;
  984. transcoderFormat = hasAlpha
  985. ? BasisFormat.cTFETC2_RGBA
  986. : BasisFormat.cTFETC1_RGB;
  987. } else if (supportedTargetFormats.etc1 && !hasAlpha) {
  988. internalFormat = PixelFormat$1.RGB_ETC1;
  989. transcoderFormat = BasisFormat.cTFETC1_RGB;
  990. } else if (supportedTargetFormats.s3tc) {
  991. internalFormat = hasAlpha ? PixelFormat$1.RGBA_DXT5 : PixelFormat$1.RGB_DXT1;
  992. transcoderFormat = hasAlpha
  993. ? BasisFormat.cTFBC3_RGBA
  994. : BasisFormat.cTFBC1_RGB;
  995. } else if (supportedTargetFormats.pvrtc) {
  996. internalFormat = hasAlpha
  997. ? PixelFormat$1.RGBA_PVRTC_4BPPV1
  998. : PixelFormat$1.RGB_PVRTC_4BPPV1;
  999. transcoderFormat = hasAlpha
  1000. ? BasisFormat.cTFPVRTC1_4_RGBA
  1001. : BasisFormat.cTFPVRTC1_4_RGB;
  1002. } else if (supportedTargetFormats.astc) {
  1003. internalFormat = PixelFormat$1.RGBA_ASTC;
  1004. transcoderFormat = BasisFormat.cTFASTC_4x4_RGBA;
  1005. } else if (supportedTargetFormats.bc7) {
  1006. internalFormat = PixelFormat$1.RGBA_BC7;
  1007. transcoderFormat = BasisFormat.cTFBC7_RGBA;
  1008. } else {
  1009. throw new RuntimeError.RuntimeError(
  1010. "No transcoding format target available for ETC1S compressed ktx2."
  1011. );
  1012. }
  1013. } else if (dfd.colorModel === colorModelUASTC) {
  1014. if (supportedTargetFormats.astc) {
  1015. internalFormat = PixelFormat$1.RGBA_ASTC;
  1016. transcoderFormat = BasisFormat.cTFASTC_4x4_RGBA;
  1017. } else if (supportedTargetFormats.bc7) {
  1018. internalFormat = PixelFormat$1.RGBA_BC7;
  1019. transcoderFormat = BasisFormat.cTFBC7_RGBA;
  1020. } else if (supportedTargetFormats.s3tc) {
  1021. internalFormat = hasAlpha ? PixelFormat$1.RGBA_DXT5 : PixelFormat$1.RGB_DXT1;
  1022. transcoderFormat = hasAlpha
  1023. ? BasisFormat.cTFBC3_RGBA
  1024. : BasisFormat.cTFBC1_RGB;
  1025. } else if (supportedTargetFormats.etc) {
  1026. internalFormat = hasAlpha
  1027. ? PixelFormat$1.RGBA8_ETC2_EAC
  1028. : PixelFormat$1.RGB8_ETC2;
  1029. transcoderFormat = hasAlpha
  1030. ? BasisFormat.cTFETC2_RGBA
  1031. : BasisFormat.cTFETC1_RGB;
  1032. } else if (supportedTargetFormats.etc1 && !hasAlpha) {
  1033. internalFormat = PixelFormat$1.RGB_ETC1;
  1034. transcoderFormat = BasisFormat.cTFETC1_RGB;
  1035. } else if (supportedTargetFormats.pvrtc) {
  1036. internalFormat = hasAlpha
  1037. ? PixelFormat$1.RGBA_PVRTC_4BPPV1
  1038. : PixelFormat$1.RGB_PVRTC_4BPPV1;
  1039. transcoderFormat = hasAlpha
  1040. ? BasisFormat.cTFPVRTC1_4_RGBA
  1041. : BasisFormat.cTFPVRTC1_4_RGB;
  1042. } else {
  1043. throw new RuntimeError.RuntimeError(
  1044. "No transcoding format target available for UASTC compressed ktx2."
  1045. );
  1046. }
  1047. }
  1048. if (!ktx2File.startTranscoding()) {
  1049. ktx2File.close();
  1050. ktx2File.delete();
  1051. throw new RuntimeError.RuntimeError("startTranscoding() failed");
  1052. }
  1053. for (let i = 0; i < header.levels.length; ++i) {
  1054. const level = {};
  1055. result[i] = level;
  1056. width = header.pixelWidth >> i;
  1057. height = header.pixelHeight >> i;
  1058. // Since supercompressed cubemaps are unsupported, this function
  1059. // does not iterate over KTX2 faces and assumes faceCount = 1.
  1060. const dstSize = ktx2File.getImageTranscodedSizeInBytes(
  1061. i, // level index
  1062. 0, // layer index
  1063. 0, // face index
  1064. transcoderFormat.value
  1065. );
  1066. const dst = new Uint8Array(dstSize);
  1067. const transcoded = ktx2File.transcodeImage(
  1068. dst,
  1069. i, // level index
  1070. 0, // layer index
  1071. 0, // face index
  1072. transcoderFormat.value,
  1073. 0, // get_alpha_for_opaque_formats
  1074. -1, // channel0
  1075. -1 // channel1
  1076. );
  1077. if (!defaultValue.defined(transcoded)) {
  1078. throw new RuntimeError.RuntimeError("transcodeImage() failed.");
  1079. }
  1080. transferableObjects.push(dst.buffer);
  1081. level[faceOrder[0]] = {
  1082. internalFormat: internalFormat,
  1083. width: width,
  1084. height: height,
  1085. levelBuffer: dst,
  1086. };
  1087. }
  1088. ktx2File.close();
  1089. ktx2File.delete();
  1090. return result;
  1091. }
  1092. function initWorker(compiledModule) {
  1093. transcoderModule = compiledModule;
  1094. transcoderModule.initializeBasis();
  1095. self.onmessage = createTaskProcessorWorker(transcode);
  1096. self.postMessage(true);
  1097. }
  1098. function transcodeKTX2(event) {
  1099. const data = event.data;
  1100. // Expect the first message to be to load a web assembly module
  1101. const wasmConfig = data.webAssemblyConfig;
  1102. if (defaultValue.defined(wasmConfig)) {
  1103. // Require and compile WebAssembly module, or use fallback if not supported
  1104. return require([wasmConfig.modulePath], function (mscBasisTranscoder) {
  1105. if (defaultValue.defined(wasmConfig.wasmBinaryFile)) {
  1106. if (!defaultValue.defined(mscBasisTranscoder)) {
  1107. mscBasisTranscoder = self.MSC_TRANSCODER;
  1108. }
  1109. mscBasisTranscoder(wasmConfig).then(function (compiledModule) {
  1110. initWorker(compiledModule);
  1111. });
  1112. } else {
  1113. return mscBasisTranscoder().then(function (transcoder) {
  1114. initWorker(transcoder);
  1115. });
  1116. }
  1117. });
  1118. }
  1119. }
  1120. return transcodeKTX2;
  1121. }));
  1122. //# sourceMappingURL=transcodeKTX2.js.map