transcodeKTX2.js 44 KB

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