transcodeKTX2.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. define(['./defaultValue-fe22d8c0', './Check-6ede7e26', './WebGLConstants-0b1ce7ba', './RuntimeError-ef395448', './createTaskProcessorWorker'], (function (defaultValue, Check, WebGLConstants, RuntimeError, createTaskProcessorWorker) { 'use strict';
  2. /**
  3. * The data type of a pixel.
  4. *
  5. * @enum {number}
  6. * @see PostProcessStage
  7. */
  8. const PixelDatatype = {
  9. UNSIGNED_BYTE: WebGLConstants.WebGLConstants.UNSIGNED_BYTE,
  10. UNSIGNED_SHORT: WebGLConstants.WebGLConstants.UNSIGNED_SHORT,
  11. UNSIGNED_INT: WebGLConstants.WebGLConstants.UNSIGNED_INT,
  12. FLOAT: WebGLConstants.WebGLConstants.FLOAT,
  13. HALF_FLOAT: WebGLConstants.WebGLConstants.HALF_FLOAT_OES,
  14. UNSIGNED_INT_24_8: WebGLConstants.WebGLConstants.UNSIGNED_INT_24_8,
  15. UNSIGNED_SHORT_4_4_4_4: WebGLConstants.WebGLConstants.UNSIGNED_SHORT_4_4_4_4,
  16. UNSIGNED_SHORT_5_5_5_1: WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_5_5_1,
  17. UNSIGNED_SHORT_5_6_5: WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_6_5,
  18. };
  19. /**
  20. @private
  21. */
  22. PixelDatatype.toWebGLConstant = function (pixelDatatype, context) {
  23. switch (pixelDatatype) {
  24. case PixelDatatype.UNSIGNED_BYTE:
  25. return WebGLConstants.WebGLConstants.UNSIGNED_BYTE;
  26. case PixelDatatype.UNSIGNED_SHORT:
  27. return WebGLConstants.WebGLConstants.UNSIGNED_SHORT;
  28. case PixelDatatype.UNSIGNED_INT:
  29. return WebGLConstants.WebGLConstants.UNSIGNED_INT;
  30. case PixelDatatype.FLOAT:
  31. return WebGLConstants.WebGLConstants.FLOAT;
  32. case PixelDatatype.HALF_FLOAT:
  33. return context.webgl2
  34. ? WebGLConstants.WebGLConstants.HALF_FLOAT
  35. : WebGLConstants.WebGLConstants.HALF_FLOAT_OES;
  36. case PixelDatatype.UNSIGNED_INT_24_8:
  37. return WebGLConstants.WebGLConstants.UNSIGNED_INT_24_8;
  38. case PixelDatatype.UNSIGNED_SHORT_4_4_4_4:
  39. return WebGLConstants.WebGLConstants.UNSIGNED_SHORT_4_4_4_4;
  40. case PixelDatatype.UNSIGNED_SHORT_5_5_5_1:
  41. return WebGLConstants.WebGLConstants.UNSIGNED_SHORT_5_5_5_1;
  42. case PixelDatatype.UNSIGNED_SHORT_5_6_5:
  43. return PixelDatatype.UNSIGNED_SHORT_5_6_5;
  44. }
  45. };
  46. /**
  47. @private
  48. */
  49. PixelDatatype.isPacked = function (pixelDatatype) {
  50. return (
  51. pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8 ||
  52. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4 ||
  53. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1 ||
  54. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5
  55. );
  56. };
  57. /**
  58. @private
  59. */
  60. PixelDatatype.sizeInBytes = function (pixelDatatype) {
  61. switch (pixelDatatype) {
  62. case PixelDatatype.UNSIGNED_BYTE:
  63. return 1;
  64. case PixelDatatype.UNSIGNED_SHORT:
  65. case PixelDatatype.UNSIGNED_SHORT_4_4_4_4:
  66. case PixelDatatype.UNSIGNED_SHORT_5_5_5_1:
  67. case PixelDatatype.UNSIGNED_SHORT_5_6_5:
  68. case PixelDatatype.HALF_FLOAT:
  69. return 2;
  70. case PixelDatatype.UNSIGNED_INT:
  71. case PixelDatatype.FLOAT:
  72. case PixelDatatype.UNSIGNED_INT_24_8:
  73. return 4;
  74. }
  75. };
  76. /**
  77. @private
  78. */
  79. PixelDatatype.validate = function (pixelDatatype) {
  80. return (
  81. pixelDatatype === PixelDatatype.UNSIGNED_BYTE ||
  82. pixelDatatype === PixelDatatype.UNSIGNED_SHORT ||
  83. pixelDatatype === PixelDatatype.UNSIGNED_INT ||
  84. pixelDatatype === PixelDatatype.FLOAT ||
  85. pixelDatatype === PixelDatatype.HALF_FLOAT ||
  86. pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8 ||
  87. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4 ||
  88. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1 ||
  89. pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5
  90. );
  91. };
  92. var PixelDatatype$1 = Object.freeze(PixelDatatype);
  93. /**
  94. * The format of a pixel, i.e., the number of components it has and what they represent.
  95. *
  96. * @enum {number}
  97. */
  98. const PixelFormat = {
  99. /**
  100. * A pixel format containing a depth value.
  101. *
  102. * @type {number}
  103. * @constant
  104. */
  105. DEPTH_COMPONENT: WebGLConstants.WebGLConstants.DEPTH_COMPONENT,
  106. /**
  107. * A pixel format containing a depth and stencil value, most often used with {@link PixelDatatype.UNSIGNED_INT_24_8}.
  108. *
  109. * @type {number}
  110. * @constant
  111. */
  112. DEPTH_STENCIL: WebGLConstants.WebGLConstants.DEPTH_STENCIL,
  113. /**
  114. * A pixel format containing an alpha channel.
  115. *
  116. * @type {number}
  117. * @constant
  118. */
  119. ALPHA: WebGLConstants.WebGLConstants.ALPHA,
  120. /**
  121. * A pixel format containing a red channel
  122. *
  123. * @type {number}
  124. * @constant
  125. */
  126. RED: WebGLConstants.WebGLConstants.RED,
  127. /**
  128. * A pixel format containing red and green channels.
  129. *
  130. * @type {number}
  131. * @constant
  132. */
  133. RG: WebGLConstants.WebGLConstants.RG,
  134. /**
  135. * A pixel format containing red, green, and blue channels.
  136. *
  137. * @type {number}
  138. * @constant
  139. */
  140. RGB: WebGLConstants.WebGLConstants.RGB,
  141. /**
  142. * A pixel format containing red, green, blue, and alpha channels.
  143. *
  144. * @type {number}
  145. * @constant
  146. */
  147. RGBA: WebGLConstants.WebGLConstants.RGBA,
  148. /**
  149. * A pixel format containing a luminance (intensity) channel.
  150. *
  151. * @type {number}
  152. * @constant
  153. */
  154. LUMINANCE: WebGLConstants.WebGLConstants.LUMINANCE,
  155. /**
  156. * A pixel format containing luminance (intensity) and alpha channels.
  157. *
  158. * @type {number}
  159. * @constant
  160. */
  161. LUMINANCE_ALPHA: WebGLConstants.WebGLConstants.LUMINANCE_ALPHA,
  162. /**
  163. * A pixel format containing red, green, and blue channels that is DXT1 compressed.
  164. *
  165. * @type {number}
  166. * @constant
  167. */
  168. RGB_DXT1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_S3TC_DXT1_EXT,
  169. /**
  170. * A pixel format containing red, green, blue, and alpha channels that is DXT1 compressed.
  171. *
  172. * @type {number}
  173. * @constant
  174. */
  175. RGBA_DXT1: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT1_EXT,
  176. /**
  177. * A pixel format containing red, green, blue, and alpha channels that is DXT3 compressed.
  178. *
  179. * @type {number}
  180. * @constant
  181. */
  182. RGBA_DXT3: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT3_EXT,
  183. /**
  184. * A pixel format containing red, green, blue, and alpha channels that is DXT5 compressed.
  185. *
  186. * @type {number}
  187. * @constant
  188. */
  189. RGBA_DXT5: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_S3TC_DXT5_EXT,
  190. /**
  191. * A pixel format containing red, green, and blue channels that is PVR 4bpp compressed.
  192. *
  193. * @type {number}
  194. * @constant
  195. */
  196. RGB_PVRTC_4BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
  197. /**
  198. * A pixel format containing red, green, and blue channels that is PVR 2bpp compressed.
  199. *
  200. * @type {number}
  201. * @constant
  202. */
  203. RGB_PVRTC_2BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
  204. /**
  205. * A pixel format containing red, green, blue, and alpha channels that is PVR 4bpp compressed.
  206. *
  207. * @type {number}
  208. * @constant
  209. */
  210. RGBA_PVRTC_4BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
  211. /**
  212. * A pixel format containing red, green, blue, and alpha channels that is PVR 2bpp compressed.
  213. *
  214. * @type {number}
  215. * @constant
  216. */
  217. RGBA_PVRTC_2BPPV1: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
  218. /**
  219. * A pixel format containing red, green, blue, and alpha channels that is ASTC compressed.
  220. *
  221. * @type {number}
  222. * @constant
  223. */
  224. RGBA_ASTC: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_ASTC_4x4_WEBGL,
  225. /**
  226. * A pixel format containing red, green, and blue channels that is ETC1 compressed.
  227. *
  228. * @type {number}
  229. * @constant
  230. */
  231. RGB_ETC1: WebGLConstants.WebGLConstants.COMPRESSED_RGB_ETC1_WEBGL,
  232. /**
  233. * A pixel format containing red, green, and blue channels that is ETC2 compressed.
  234. *
  235. * @type {number}
  236. * @constant
  237. */
  238. RGB8_ETC2: WebGLConstants.WebGLConstants.COMPRESSED_RGB8_ETC2,
  239. /**
  240. * A pixel format containing red, green, blue, and alpha channels that is ETC2 compressed.
  241. *
  242. * @type {number}
  243. * @constant
  244. */
  245. RGBA8_ETC2_EAC: WebGLConstants.WebGLConstants.COMPRESSED_RGBA8_ETC2_EAC,
  246. /**
  247. * A pixel format containing red, green, blue, and alpha channels that is BC7 compressed.
  248. *
  249. * @type {number}
  250. * @constant
  251. */
  252. RGBA_BC7: WebGLConstants.WebGLConstants.COMPRESSED_RGBA_BPTC_UNORM,
  253. };
  254. /**
  255. * @private
  256. */
  257. PixelFormat.componentsLength = function (pixelFormat) {
  258. switch (pixelFormat) {
  259. case PixelFormat.RGB:
  260. return 3;
  261. case PixelFormat.RGBA:
  262. return 4;
  263. case PixelFormat.LUMINANCE_ALPHA:
  264. case PixelFormat.RG:
  265. return 2;
  266. case PixelFormat.ALPHA:
  267. case PixelFormat.RED:
  268. case PixelFormat.LUMINANCE:
  269. return 1;
  270. default:
  271. return 1;
  272. }
  273. };
  274. /**
  275. * @private
  276. */
  277. PixelFormat.validate = function (pixelFormat) {
  278. return (
  279. pixelFormat === PixelFormat.DEPTH_COMPONENT ||
  280. pixelFormat === PixelFormat.DEPTH_STENCIL ||
  281. pixelFormat === PixelFormat.ALPHA ||
  282. pixelFormat === PixelFormat.RED ||
  283. pixelFormat === PixelFormat.RG ||
  284. pixelFormat === PixelFormat.RGB ||
  285. pixelFormat === PixelFormat.RGBA ||
  286. pixelFormat === PixelFormat.LUMINANCE ||
  287. pixelFormat === PixelFormat.LUMINANCE_ALPHA ||
  288. pixelFormat === PixelFormat.RGB_DXT1 ||
  289. pixelFormat === PixelFormat.RGBA_DXT1 ||
  290. pixelFormat === PixelFormat.RGBA_DXT3 ||
  291. pixelFormat === PixelFormat.RGBA_DXT5 ||
  292. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  293. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  294. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  295. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1 ||
  296. pixelFormat === PixelFormat.RGBA_ASTC ||
  297. pixelFormat === PixelFormat.RGB_ETC1 ||
  298. pixelFormat === PixelFormat.RGB8_ETC2 ||
  299. pixelFormat === PixelFormat.RGBA8_ETC2_EAC ||
  300. pixelFormat === PixelFormat.RGBA_BC7
  301. );
  302. };
  303. /**
  304. * @private
  305. */
  306. PixelFormat.isColorFormat = function (pixelFormat) {
  307. return (
  308. pixelFormat === PixelFormat.ALPHA ||
  309. pixelFormat === PixelFormat.RGB ||
  310. pixelFormat === PixelFormat.RGBA ||
  311. pixelFormat === PixelFormat.LUMINANCE ||
  312. pixelFormat === PixelFormat.LUMINANCE_ALPHA
  313. );
  314. };
  315. /**
  316. * @private
  317. */
  318. PixelFormat.isDepthFormat = function (pixelFormat) {
  319. return (
  320. pixelFormat === PixelFormat.DEPTH_COMPONENT ||
  321. pixelFormat === PixelFormat.DEPTH_STENCIL
  322. );
  323. };
  324. /**
  325. * @private
  326. */
  327. PixelFormat.isCompressedFormat = function (pixelFormat) {
  328. return (
  329. pixelFormat === PixelFormat.RGB_DXT1 ||
  330. pixelFormat === PixelFormat.RGBA_DXT1 ||
  331. pixelFormat === PixelFormat.RGBA_DXT3 ||
  332. pixelFormat === PixelFormat.RGBA_DXT5 ||
  333. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  334. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  335. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  336. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1 ||
  337. pixelFormat === PixelFormat.RGBA_ASTC ||
  338. pixelFormat === PixelFormat.RGB_ETC1 ||
  339. pixelFormat === PixelFormat.RGB8_ETC2 ||
  340. pixelFormat === PixelFormat.RGBA8_ETC2_EAC ||
  341. pixelFormat === PixelFormat.RGBA_BC7
  342. );
  343. };
  344. /**
  345. * @private
  346. */
  347. PixelFormat.isDXTFormat = function (pixelFormat) {
  348. return (
  349. pixelFormat === PixelFormat.RGB_DXT1 ||
  350. pixelFormat === PixelFormat.RGBA_DXT1 ||
  351. pixelFormat === PixelFormat.RGBA_DXT3 ||
  352. pixelFormat === PixelFormat.RGBA_DXT5
  353. );
  354. };
  355. /**
  356. * @private
  357. */
  358. PixelFormat.isPVRTCFormat = function (pixelFormat) {
  359. return (
  360. pixelFormat === PixelFormat.RGB_PVRTC_4BPPV1 ||
  361. pixelFormat === PixelFormat.RGB_PVRTC_2BPPV1 ||
  362. pixelFormat === PixelFormat.RGBA_PVRTC_4BPPV1 ||
  363. pixelFormat === PixelFormat.RGBA_PVRTC_2BPPV1
  364. );
  365. };
  366. /**
  367. * @private
  368. */
  369. PixelFormat.isASTCFormat = function (pixelFormat) {
  370. return pixelFormat === PixelFormat.RGBA_ASTC;
  371. };
  372. /**
  373. * @private
  374. */
  375. PixelFormat.isETC1Format = function (pixelFormat) {
  376. return pixelFormat === PixelFormat.RGB_ETC1;
  377. };
  378. /**
  379. * @private
  380. */
  381. PixelFormat.isETC2Format = function (pixelFormat) {
  382. return (
  383. pixelFormat === PixelFormat.RGB8_ETC2 ||
  384. pixelFormat === PixelFormat.RGBA8_ETC2_EAC
  385. );
  386. };
  387. /**
  388. * @private
  389. */
  390. PixelFormat.isBC7Format = function (pixelFormat) {
  391. return pixelFormat === PixelFormat.RGBA_BC7;
  392. };
  393. /**
  394. * @private
  395. */
  396. PixelFormat.compressedTextureSizeInBytes = function (
  397. pixelFormat,
  398. width,
  399. height
  400. ) {
  401. switch (pixelFormat) {
  402. case PixelFormat.RGB_DXT1:
  403. case PixelFormat.RGBA_DXT1:
  404. case PixelFormat.RGB_ETC1:
  405. case PixelFormat.RGB8_ETC2:
  406. return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 8;
  407. case PixelFormat.RGBA_DXT3:
  408. case PixelFormat.RGBA_DXT5:
  409. case PixelFormat.RGBA_ASTC:
  410. case PixelFormat.RGBA8_ETC2_EAC:
  411. return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 16;
  412. case PixelFormat.RGB_PVRTC_4BPPV1:
  413. case PixelFormat.RGBA_PVRTC_4BPPV1:
  414. return Math.floor((Math.max(width, 8) * Math.max(height, 8) * 4 + 7) / 8);
  415. case PixelFormat.RGB_PVRTC_2BPPV1:
  416. case PixelFormat.RGBA_PVRTC_2BPPV1:
  417. return Math.floor(
  418. (Math.max(width, 16) * Math.max(height, 8) * 2 + 7) / 8
  419. );
  420. case PixelFormat.RGBA_BC7:
  421. return Math.ceil(width / 4) * Math.ceil(height / 4) * 16;
  422. default:
  423. return 0;
  424. }
  425. };
  426. /**
  427. * @private
  428. */
  429. PixelFormat.textureSizeInBytes = function (
  430. pixelFormat,
  431. pixelDatatype,
  432. width,
  433. height
  434. ) {
  435. let componentsLength = PixelFormat.componentsLength(pixelFormat);
  436. if (PixelDatatype$1.isPacked(pixelDatatype)) {
  437. componentsLength = 1;
  438. }
  439. return (
  440. componentsLength * PixelDatatype$1.sizeInBytes(pixelDatatype) * width * height
  441. );
  442. };
  443. /**
  444. * @private
  445. */
  446. PixelFormat.alignmentInBytes = function (pixelFormat, pixelDatatype, width) {
  447. const mod =
  448. PixelFormat.textureSizeInBytes(pixelFormat, pixelDatatype, width, 1) % 4;
  449. return mod === 0 ? 4 : mod === 2 ? 2 : 1;
  450. };
  451. /**
  452. * @private
  453. */
  454. PixelFormat.createTypedArray = function (
  455. pixelFormat,
  456. pixelDatatype,
  457. width,
  458. height
  459. ) {
  460. let constructor;
  461. const sizeInBytes = PixelDatatype$1.sizeInBytes(pixelDatatype);
  462. if (sizeInBytes === Uint8Array.BYTES_PER_ELEMENT) {
  463. constructor = Uint8Array;
  464. } else if (sizeInBytes === Uint16Array.BYTES_PER_ELEMENT) {
  465. constructor = Uint16Array;
  466. } else if (
  467. sizeInBytes === Float32Array.BYTES_PER_ELEMENT &&
  468. pixelDatatype === PixelDatatype$1.FLOAT
  469. ) {
  470. constructor = Float32Array;
  471. } else {
  472. constructor = Uint32Array;
  473. }
  474. const size = PixelFormat.componentsLength(pixelFormat) * width * height;
  475. return new constructor(size);
  476. };
  477. /**
  478. * @private
  479. */
  480. PixelFormat.flipY = function (
  481. bufferView,
  482. pixelFormat,
  483. pixelDatatype,
  484. width,
  485. height
  486. ) {
  487. if (height === 1) {
  488. return bufferView;
  489. }
  490. const flipped = PixelFormat.createTypedArray(
  491. pixelFormat,
  492. pixelDatatype,
  493. width,
  494. height
  495. );
  496. const numberOfComponents = PixelFormat.componentsLength(pixelFormat);
  497. const textureWidth = width * numberOfComponents;
  498. for (let i = 0; i < height; ++i) {
  499. const row = i * width * numberOfComponents;
  500. const flippedRow = (height - i - 1) * width * numberOfComponents;
  501. for (let j = 0; j < textureWidth; ++j) {
  502. flipped[flippedRow + j] = bufferView[row + j];
  503. }
  504. }
  505. return flipped;
  506. };
  507. /**
  508. * @private
  509. */
  510. PixelFormat.toInternalFormat = function (pixelFormat, pixelDatatype, context) {
  511. // WebGL 1 require internalFormat to be the same as PixelFormat
  512. if (!context.webgl2) {
  513. return pixelFormat;
  514. }
  515. // Convert pixelFormat to correct internalFormat for WebGL 2
  516. if (pixelFormat === PixelFormat.DEPTH_STENCIL) {
  517. return WebGLConstants.WebGLConstants.DEPTH24_STENCIL8;
  518. }
  519. if (pixelFormat === PixelFormat.DEPTH_COMPONENT) {
  520. if (pixelDatatype === PixelDatatype$1.UNSIGNED_SHORT) {
  521. return WebGLConstants.WebGLConstants.DEPTH_COMPONENT16;
  522. } else if (pixelDatatype === PixelDatatype$1.UNSIGNED_INT) {
  523. return WebGLConstants.WebGLConstants.DEPTH_COMPONENT24;
  524. }
  525. }
  526. if (pixelDatatype === PixelDatatype$1.FLOAT) {
  527. switch (pixelFormat) {
  528. case PixelFormat.RGBA:
  529. return WebGLConstants.WebGLConstants.RGBA32F;
  530. case PixelFormat.RGB:
  531. return WebGLConstants.WebGLConstants.RGB32F;
  532. case PixelFormat.RG:
  533. return WebGLConstants.WebGLConstants.RG32F;
  534. case PixelFormat.RED:
  535. return WebGLConstants.WebGLConstants.R32F;
  536. }
  537. }
  538. if (pixelDatatype === PixelDatatype$1.HALF_FLOAT) {
  539. switch (pixelFormat) {
  540. case PixelFormat.RGBA:
  541. return WebGLConstants.WebGLConstants.RGBA16F;
  542. case PixelFormat.RGB:
  543. return WebGLConstants.WebGLConstants.RGB16F;
  544. case PixelFormat.RG:
  545. return WebGLConstants.WebGLConstants.RG16F;
  546. case PixelFormat.RED:
  547. return WebGLConstants.WebGLConstants.R16F;
  548. }
  549. }
  550. return pixelFormat;
  551. };
  552. var PixelFormat$1 = Object.freeze(PixelFormat);
  553. /**
  554. * Enum containing Vulkan Constant values by name.
  555. *
  556. * 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}.
  557. *
  558. * @enum {number}
  559. * @private
  560. */
  561. const VulkanConstants = {
  562. VK_FORMAT_UNDEFINED: 0,
  563. VK_FORMAT_R4G4_UNORM_PACK8: 1,
  564. VK_FORMAT_R4G4B4A4_UNORM_PACK16: 2,
  565. VK_FORMAT_B4G4R4A4_UNORM_PACK16: 3,
  566. VK_FORMAT_R5G6B5_UNORM_PACK16: 4,
  567. VK_FORMAT_B5G6R5_UNORM_PACK16: 5,
  568. VK_FORMAT_R5G5B5A1_UNORM_PACK16: 6,
  569. VK_FORMAT_B5G5R5A1_UNORM_PACK16: 7,
  570. VK_FORMAT_A1R5G5B5_UNORM_PACK16: 8,
  571. VK_FORMAT_R8_UNORM: 9,
  572. VK_FORMAT_R8_SNORM: 10,
  573. VK_FORMAT_R8_USCALED: 11,
  574. VK_FORMAT_R8_SSCALED: 12,
  575. VK_FORMAT_R8_UINT: 13,
  576. VK_FORMAT_R8_SINT: 14,
  577. VK_FORMAT_R8_SRGB: 15,
  578. VK_FORMAT_R8G8_UNORM: 16,
  579. VK_FORMAT_R8G8_SNORM: 17,
  580. VK_FORMAT_R8G8_USCALED: 18,
  581. VK_FORMAT_R8G8_SSCALED: 19,
  582. VK_FORMAT_R8G8_UINT: 20,
  583. VK_FORMAT_R8G8_SINT: 21,
  584. VK_FORMAT_R8G8_SRGB: 22,
  585. VK_FORMAT_R8G8B8_UNORM: 23,
  586. VK_FORMAT_R8G8B8_SNORM: 24,
  587. VK_FORMAT_R8G8B8_USCALED: 25,
  588. VK_FORMAT_R8G8B8_SSCALED: 26,
  589. VK_FORMAT_R8G8B8_UINT: 27,
  590. VK_FORMAT_R8G8B8_SINT: 28,
  591. VK_FORMAT_R8G8B8_SRGB: 29,
  592. VK_FORMAT_B8G8R8_UNORM: 30,
  593. VK_FORMAT_B8G8R8_SNORM: 31,
  594. VK_FORMAT_B8G8R8_USCALED: 32,
  595. VK_FORMAT_B8G8R8_SSCALED: 33,
  596. VK_FORMAT_B8G8R8_UINT: 34,
  597. VK_FORMAT_B8G8R8_SINT: 35,
  598. VK_FORMAT_B8G8R8_SRGB: 36,
  599. VK_FORMAT_R8G8B8A8_UNORM: 37,
  600. VK_FORMAT_R8G8B8A8_SNORM: 38,
  601. VK_FORMAT_R8G8B8A8_USCALED: 39,
  602. VK_FORMAT_R8G8B8A8_SSCALED: 40,
  603. VK_FORMAT_R8G8B8A8_UINT: 41,
  604. VK_FORMAT_R8G8B8A8_SINT: 42,
  605. VK_FORMAT_R8G8B8A8_SRGB: 43,
  606. VK_FORMAT_B8G8R8A8_UNORM: 44,
  607. VK_FORMAT_B8G8R8A8_SNORM: 45,
  608. VK_FORMAT_B8G8R8A8_USCALED: 46,
  609. VK_FORMAT_B8G8R8A8_SSCALED: 47,
  610. VK_FORMAT_B8G8R8A8_UINT: 48,
  611. VK_FORMAT_B8G8R8A8_SINT: 49,
  612. VK_FORMAT_B8G8R8A8_SRGB: 50,
  613. VK_FORMAT_A8B8G8R8_UNORM_PACK32: 51,
  614. VK_FORMAT_A8B8G8R8_SNORM_PACK32: 52,
  615. VK_FORMAT_A8B8G8R8_USCALED_PACK32: 53,
  616. VK_FORMAT_A8B8G8R8_SSCALED_PACK32: 54,
  617. VK_FORMAT_A8B8G8R8_UINT_PACK32: 55,
  618. VK_FORMAT_A8B8G8R8_SINT_PACK32: 56,
  619. VK_FORMAT_A8B8G8R8_SRGB_PACK32: 57,
  620. VK_FORMAT_A2R10G10B10_UNORM_PACK32: 58,
  621. VK_FORMAT_A2R10G10B10_SNORM_PACK32: 59,
  622. VK_FORMAT_A2R10G10B10_USCALED_PACK32: 60,
  623. VK_FORMAT_A2R10G10B10_SSCALED_PACK32: 61,
  624. VK_FORMAT_A2R10G10B10_UINT_PACK32: 62,
  625. VK_FORMAT_A2R10G10B10_SINT_PACK32: 63,
  626. VK_FORMAT_A2B10G10R10_UNORM_PACK32: 64,
  627. VK_FORMAT_A2B10G10R10_SNORM_PACK32: 65,
  628. VK_FORMAT_A2B10G10R10_USCALED_PACK32: 66,
  629. VK_FORMAT_A2B10G10R10_SSCALED_PACK32: 67,
  630. VK_FORMAT_A2B10G10R10_UINT_PACK32: 68,
  631. VK_FORMAT_A2B10G10R10_SINT_PACK32: 69,
  632. VK_FORMAT_R16_UNORM: 70,
  633. VK_FORMAT_R16_SNORM: 71,
  634. VK_FORMAT_R16_USCALED: 72,
  635. VK_FORMAT_R16_SSCALED: 73,
  636. VK_FORMAT_R16_UINT: 74,
  637. VK_FORMAT_R16_SINT: 75,
  638. VK_FORMAT_R16_SFLOAT: 76,
  639. VK_FORMAT_R16G16_UNORM: 77,
  640. VK_FORMAT_R16G16_SNORM: 78,
  641. VK_FORMAT_R16G16_USCALED: 79,
  642. VK_FORMAT_R16G16_SSCALED: 80,
  643. VK_FORMAT_R16G16_UINT: 81,
  644. VK_FORMAT_R16G16_SINT: 82,
  645. VK_FORMAT_R16G16_SFLOAT: 83,
  646. VK_FORMAT_R16G16B16_UNORM: 84,
  647. VK_FORMAT_R16G16B16_SNORM: 85,
  648. VK_FORMAT_R16G16B16_USCALED: 86,
  649. VK_FORMAT_R16G16B16_SSCALED: 87,
  650. VK_FORMAT_R16G16B16_UINT: 88,
  651. VK_FORMAT_R16G16B16_SINT: 89,
  652. VK_FORMAT_R16G16B16_SFLOAT: 90,
  653. VK_FORMAT_R16G16B16A16_UNORM: 91,
  654. VK_FORMAT_R16G16B16A16_SNORM: 92,
  655. VK_FORMAT_R16G16B16A16_USCALED: 93,
  656. VK_FORMAT_R16G16B16A16_SSCALED: 94,
  657. VK_FORMAT_R16G16B16A16_UINT: 95,
  658. VK_FORMAT_R16G16B16A16_SINT: 96,
  659. VK_FORMAT_R16G16B16A16_SFLOAT: 97,
  660. VK_FORMAT_R32_UINT: 98,
  661. VK_FORMAT_R32_SINT: 99,
  662. VK_FORMAT_R32_SFLOAT: 100,
  663. VK_FORMAT_R32G32_UINT: 101,
  664. VK_FORMAT_R32G32_SINT: 102,
  665. VK_FORMAT_R32G32_SFLOAT: 103,
  666. VK_FORMAT_R32G32B32_UINT: 104,
  667. VK_FORMAT_R32G32B32_SINT: 105,
  668. VK_FORMAT_R32G32B32_SFLOAT: 106,
  669. VK_FORMAT_R32G32B32A32_UINT: 107,
  670. VK_FORMAT_R32G32B32A32_SINT: 108,
  671. VK_FORMAT_R32G32B32A32_SFLOAT: 109,
  672. VK_FORMAT_R64_UINT: 110,
  673. VK_FORMAT_R64_SINT: 111,
  674. VK_FORMAT_R64_SFLOAT: 112,
  675. VK_FORMAT_R64G64_UINT: 113,
  676. VK_FORMAT_R64G64_SINT: 114,
  677. VK_FORMAT_R64G64_SFLOAT: 115,
  678. VK_FORMAT_R64G64B64_UINT: 116,
  679. VK_FORMAT_R64G64B64_SINT: 117,
  680. VK_FORMAT_R64G64B64_SFLOAT: 118,
  681. VK_FORMAT_R64G64B64A64_UINT: 119,
  682. VK_FORMAT_R64G64B64A64_SINT: 120,
  683. VK_FORMAT_R64G64B64A64_SFLOAT: 121,
  684. VK_FORMAT_B10G11R11_UFLOAT_PACK32: 122,
  685. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: 123,
  686. VK_FORMAT_D16_UNORM: 124,
  687. VK_FORMAT_X8_D24_UNORM_PACK32: 125,
  688. VK_FORMAT_D32_SFLOAT: 126,
  689. VK_FORMAT_S8_UINT: 127,
  690. VK_FORMAT_D16_UNORM_S8_UINT: 128,
  691. VK_FORMAT_D24_UNORM_S8_UINT: 129,
  692. VK_FORMAT_D32_SFLOAT_S8_UINT: 130,
  693. VK_FORMAT_BC1_RGB_UNORM_BLOCK: 131,
  694. VK_FORMAT_BC1_RGB_SRGB_BLOCK: 132,
  695. VK_FORMAT_BC1_RGBA_UNORM_BLOCK: 133,
  696. VK_FORMAT_BC1_RGBA_SRGB_BLOCK: 134,
  697. VK_FORMAT_BC2_UNORM_BLOCK: 135,
  698. VK_FORMAT_BC2_SRGB_BLOCK: 136,
  699. VK_FORMAT_BC3_UNORM_BLOCK: 137,
  700. VK_FORMAT_BC3_SRGB_BLOCK: 138,
  701. VK_FORMAT_BC4_UNORM_BLOCK: 139,
  702. VK_FORMAT_BC4_SNORM_BLOCK: 140,
  703. VK_FORMAT_BC5_UNORM_BLOCK: 141,
  704. VK_FORMAT_BC5_SNORM_BLOCK: 142,
  705. VK_FORMAT_BC6H_UFLOAT_BLOCK: 143,
  706. VK_FORMAT_BC6H_SFLOAT_BLOCK: 144,
  707. VK_FORMAT_BC7_UNORM_BLOCK: 145,
  708. VK_FORMAT_BC7_SRGB_BLOCK: 146,
  709. VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: 147,
  710. VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK: 148,
  711. VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK: 149,
  712. VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK: 150,
  713. VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK: 151,
  714. VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK: 152,
  715. VK_FORMAT_EAC_R11_UNORM_BLOCK: 153,
  716. VK_FORMAT_EAC_R11_SNORM_BLOCK: 154,
  717. VK_FORMAT_EAC_R11G11_UNORM_BLOCK: 155,
  718. VK_FORMAT_EAC_R11G11_SNORM_BLOCK: 156,
  719. VK_FORMAT_ASTC_4x4_UNORM_BLOCK: 157,
  720. VK_FORMAT_ASTC_4x4_SRGB_BLOCK: 158,
  721. VK_FORMAT_ASTC_5x4_UNORM_BLOCK: 159,
  722. VK_FORMAT_ASTC_5x4_SRGB_BLOCK: 160,
  723. VK_FORMAT_ASTC_5x5_UNORM_BLOCK: 161,
  724. VK_FORMAT_ASTC_5x5_SRGB_BLOCK: 162,
  725. VK_FORMAT_ASTC_6x5_UNORM_BLOCK: 163,
  726. VK_FORMAT_ASTC_6x5_SRGB_BLOCK: 164,
  727. VK_FORMAT_ASTC_6x6_UNORM_BLOCK: 165,
  728. VK_FORMAT_ASTC_6x6_SRGB_BLOCK: 166,
  729. VK_FORMAT_ASTC_8x5_UNORM_BLOCK: 167,
  730. VK_FORMAT_ASTC_8x5_SRGB_BLOCK: 168,
  731. VK_FORMAT_ASTC_8x6_UNORM_BLOCK: 169,
  732. VK_FORMAT_ASTC_8x6_SRGB_BLOCK: 170,
  733. VK_FORMAT_ASTC_8x8_UNORM_BLOCK: 171,
  734. VK_FORMAT_ASTC_8x8_SRGB_BLOCK: 172,
  735. VK_FORMAT_ASTC_10x5_UNORM_BLOCK: 173,
  736. VK_FORMAT_ASTC_10x5_SRGB_BLOCK: 174,
  737. VK_FORMAT_ASTC_10x6_UNORM_BLOCK: 175,
  738. VK_FORMAT_ASTC_10x6_SRGB_BLOCK: 176,
  739. VK_FORMAT_ASTC_10x8_UNORM_BLOCK: 177,
  740. VK_FORMAT_ASTC_10x8_SRGB_BLOCK: 178,
  741. VK_FORMAT_ASTC_10x10_UNORM_BLOCK: 179,
  742. VK_FORMAT_ASTC_10x10_SRGB_BLOCK: 180,
  743. VK_FORMAT_ASTC_12x10_UNORM_BLOCK: 181,
  744. VK_FORMAT_ASTC_12x10_SRGB_BLOCK: 182,
  745. VK_FORMAT_ASTC_12x12_UNORM_BLOCK: 183,
  746. VK_FORMAT_ASTC_12x12_SRGB_BLOCK: 184,
  747. VK_FORMAT_G8B8G8R8_422_UNORM: 1000156000,
  748. VK_FORMAT_B8G8R8G8_422_UNORM: 1000156001,
  749. VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM: 1000156002,
  750. VK_FORMAT_G8_B8R8_2PLANE_420_UNORM: 1000156003,
  751. VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM: 1000156004,
  752. VK_FORMAT_G8_B8R8_2PLANE_422_UNORM: 1000156005,
  753. VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM: 1000156006,
  754. VK_FORMAT_R10X6_UNORM_PACK16: 1000156007,
  755. VK_FORMAT_R10X6G10X6_UNORM_2PACK16: 1000156008,
  756. VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16: 1000156009,
  757. VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16: 1000156010,
  758. VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16: 1000156011,
  759. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16: 1000156012,
  760. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16: 1000156013,
  761. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16: 1000156014,
  762. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16: 1000156015,
  763. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16: 1000156016,
  764. VK_FORMAT_R12X4_UNORM_PACK16: 1000156017,
  765. VK_FORMAT_R12X4G12X4_UNORM_2PACK16: 1000156018,
  766. VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16: 1000156019,
  767. VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16: 1000156020,
  768. VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16: 1000156021,
  769. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16: 1000156022,
  770. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16: 1000156023,
  771. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16: 1000156024,
  772. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16: 1000156025,
  773. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16: 1000156026,
  774. VK_FORMAT_G16B16G16R16_422_UNORM: 1000156027,
  775. VK_FORMAT_B16G16R16G16_422_UNORM: 1000156028,
  776. VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM: 1000156029,
  777. VK_FORMAT_G16_B16R16_2PLANE_420_UNORM: 1000156030,
  778. VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM: 1000156031,
  779. VK_FORMAT_G16_B16R16_2PLANE_422_UNORM: 1000156032,
  780. VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM: 1000156033,
  781. VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG: 1000054000,
  782. VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG: 1000054001,
  783. VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG: 1000054002,
  784. VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG: 1000054003,
  785. VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG: 1000054004,
  786. VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG: 1000054005,
  787. VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG: 1000054006,
  788. VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG: 1000054007,
  789. VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT: 1000066000,
  790. VK_FORMAT_ASTC_5x4_SFLOAT_BLOCK_EXT: 1000066001,
  791. VK_FORMAT_ASTC_5x5_SFLOAT_BLOCK_EXT: 1000066002,
  792. VK_FORMAT_ASTC_6x5_SFLOAT_BLOCK_EXT: 1000066003,
  793. VK_FORMAT_ASTC_6x6_SFLOAT_BLOCK_EXT: 1000066004,
  794. VK_FORMAT_ASTC_8x5_SFLOAT_BLOCK_EXT: 1000066005,
  795. VK_FORMAT_ASTC_8x6_SFLOAT_BLOCK_EXT: 1000066006,
  796. VK_FORMAT_ASTC_8x8_SFLOAT_BLOCK_EXT: 1000066007,
  797. VK_FORMAT_ASTC_10x5_SFLOAT_BLOCK_EXT: 1000066008,
  798. VK_FORMAT_ASTC_10x6_SFLOAT_BLOCK_EXT: 1000066009,
  799. VK_FORMAT_ASTC_10x8_SFLOAT_BLOCK_EXT: 1000066010,
  800. VK_FORMAT_ASTC_10x10_SFLOAT_BLOCK_EXT: 1000066011,
  801. VK_FORMAT_ASTC_12x10_SFLOAT_BLOCK_EXT: 1000066012,
  802. VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK_EXT: 1000066013,
  803. VK_FORMAT_G8B8G8R8_422_UNORM_KHR: 1000156000,
  804. VK_FORMAT_B8G8R8G8_422_UNORM_KHR: 1000156001,
  805. VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR: 1000156002,
  806. VK_FORMAT_G8_B8R8_2PLANE_420_UNORM_KHR: 1000156003,
  807. VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM_KHR: 1000156004,
  808. VK_FORMAT_G8_B8R8_2PLANE_422_UNORM_KHR: 1000156005,
  809. VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM_KHR: 1000156006,
  810. VK_FORMAT_R10X6_UNORM_PACK16_KHR: 1000156007,
  811. VK_FORMAT_R10X6G10X6_UNORM_2PACK16_KHR: 1000156008,
  812. VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16_KHR: 1000156009,
  813. VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16_KHR: 1000156010,
  814. VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16_KHR: 1000156011,
  815. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16_KHR: 1000156012,
  816. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16_KHR: 1000156013,
  817. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16_KHR: 1000156014,
  818. VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16_KHR: 1000156015,
  819. VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16_KHR: 1000156016,
  820. VK_FORMAT_R12X4_UNORM_PACK16_KHR: 1000156017,
  821. VK_FORMAT_R12X4G12X4_UNORM_2PACK16_KHR: 1000156018,
  822. VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16_KHR: 1000156019,
  823. VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16_KHR: 1000156020,
  824. VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16_KHR: 1000156021,
  825. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16_KHR: 1000156022,
  826. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16_KHR: 1000156023,
  827. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16_KHR: 1000156024,
  828. VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16_KHR: 1000156025,
  829. VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16_KHR: 1000156026,
  830. VK_FORMAT_G16B16G16R16_422_UNORM_KHR: 1000156027,
  831. VK_FORMAT_B16G16R16G16_422_UNORM_KHR: 1000156028,
  832. VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM_KHR: 1000156029,
  833. VK_FORMAT_G16_B16R16_2PLANE_420_UNORM_KHR: 1000156030,
  834. VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM_KHR: 1000156031,
  835. VK_FORMAT_G16_B16R16_2PLANE_422_UNORM_KHR: 1000156032,
  836. VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM_KHR: 1000156033,
  837. };
  838. var VulkanConstants$1 = Object.freeze(VulkanConstants);
  839. ///////////////////////////////////////////////////
  840. // KTX2 Header.
  841. ///////////////////////////////////////////////////
  842. const KHR_SUPERCOMPRESSION_NONE = 0;
  843. // Data Format Descriptor (DFD).
  844. ///////////////////////////////////////////////////
  845. const KHR_DF_KHR_DESCRIPTORTYPE_BASICFORMAT = 0;
  846. const KHR_DF_VENDORID_KHRONOS = 0;
  847. const KHR_DF_VERSION = 2;
  848. const KHR_DF_MODEL_UNSPECIFIED = 0;
  849. const KHR_DF_FLAG_ALPHA_STRAIGHT = 0;
  850. const KHR_DF_TRANSFER_SRGB = 2;
  851. const KHR_DF_PRIMARIES_BT709 = 1;
  852. const KHR_DF_SAMPLE_DATATYPE_SIGNED = 0x40;
  853. // VK FORMAT.
  854. ///////////////////////////////////////////////////
  855. const VK_FORMAT_UNDEFINED = 0;
  856. /**
  857. * Represents an unpacked KTX 2.0 texture container. Data for individual mip levels are stored in
  858. * the `.levels` array, typically compressed in Basis Universal formats. Additional properties
  859. * provide metadata required to process, transcode, and upload these textures.
  860. */
  861. class KTX2Container {
  862. constructor() {
  863. this.vkFormat = VK_FORMAT_UNDEFINED;
  864. this.typeSize = 1;
  865. this.pixelWidth = 0;
  866. this.pixelHeight = 0;
  867. this.pixelDepth = 0;
  868. this.layerCount = 0;
  869. this.faceCount = 1;
  870. this.supercompressionScheme = KHR_SUPERCOMPRESSION_NONE;
  871. this.levels = [];
  872. this.dataFormatDescriptor = [{
  873. vendorId: KHR_DF_VENDORID_KHRONOS,
  874. descriptorType: KHR_DF_KHR_DESCRIPTORTYPE_BASICFORMAT,
  875. descriptorBlockSize: 0,
  876. versionNumber: KHR_DF_VERSION,
  877. colorModel: KHR_DF_MODEL_UNSPECIFIED,
  878. colorPrimaries: KHR_DF_PRIMARIES_BT709,
  879. transferFunction: KHR_DF_TRANSFER_SRGB,
  880. flags: KHR_DF_FLAG_ALPHA_STRAIGHT,
  881. texelBlockDimension: [0, 0, 0, 0],
  882. bytesPlane: [0, 0, 0, 0, 0, 0, 0, 0],
  883. samples: []
  884. }];
  885. this.keyValue = {};
  886. this.globalData = null;
  887. }
  888. }
  889. class BufferReader {
  890. constructor(data, byteOffset, byteLength, littleEndian) {
  891. this._dataView = void 0;
  892. this._littleEndian = void 0;
  893. this._offset = void 0;
  894. this._dataView = new DataView(data.buffer, data.byteOffset + byteOffset, byteLength);
  895. this._littleEndian = littleEndian;
  896. this._offset = 0;
  897. }
  898. _nextUint8() {
  899. const value = this._dataView.getUint8(this._offset);
  900. this._offset += 1;
  901. return value;
  902. }
  903. _nextUint16() {
  904. const value = this._dataView.getUint16(this._offset, this._littleEndian);
  905. this._offset += 2;
  906. return value;
  907. }
  908. _nextUint32() {
  909. const value = this._dataView.getUint32(this._offset, this._littleEndian);
  910. this._offset += 4;
  911. return value;
  912. }
  913. _nextUint64() {
  914. const left = this._dataView.getUint32(this._offset, this._littleEndian);
  915. const right = this._dataView.getUint32(this._offset + 4, this._littleEndian); // TODO(cleanup): Just test this...
  916. // const value = this._littleEndian ? left + (2 ** 32 * right) : (2 ** 32 * left) + right;
  917. const value = left + 2 ** 32 * right;
  918. this._offset += 8;
  919. return value;
  920. }
  921. _nextInt32() {
  922. const value = this._dataView.getInt32(this._offset, this._littleEndian);
  923. this._offset += 4;
  924. return value;
  925. }
  926. _skip(bytes) {
  927. this._offset += bytes;
  928. return this;
  929. }
  930. _scan(maxByteLength, term = 0x00) {
  931. const byteOffset = this._offset;
  932. let byteLength = 0;
  933. while (this._dataView.getUint8(this._offset) !== term && byteLength < maxByteLength) {
  934. byteLength++;
  935. this._offset++;
  936. }
  937. if (byteLength < maxByteLength) this._offset++;
  938. return new Uint8Array(this._dataView.buffer, this._dataView.byteOffset + byteOffset, byteLength);
  939. }
  940. }
  941. // KTX2 Header.
  942. ///////////////////////////////////////////////////
  943. const KTX2_ID = [// '´', 'K', 'T', 'X', '2', '0', 'ª', '\r', '\n', '\x1A', '\n'
  944. 0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a];
  945. /** Decodes an ArrayBuffer to text. */
  946. function decodeText(buffer) {
  947. if (typeof TextDecoder !== 'undefined') {
  948. return new TextDecoder().decode(buffer);
  949. }
  950. return Buffer.from(buffer).toString('utf8');
  951. }
  952. /**
  953. * Parses a KTX 2.0 file, returning an unpacked {@link KTX2Container} instance with all associated
  954. * data. The container's mip levels and other binary data are pointers into the original file, not
  955. * copies, so the original file should not be overwritten after reading.
  956. *
  957. * @param data Bytes of KTX 2.0 file, as Uint8Array or Buffer.
  958. */
  959. function read(data) {
  960. ///////////////////////////////////////////////////
  961. // KTX 2.0 Identifier.
  962. ///////////////////////////////////////////////////
  963. const id = new Uint8Array(data.buffer, data.byteOffset, KTX2_ID.length);
  964. if (id[0] !== KTX2_ID[0] || // '´'
  965. id[1] !== KTX2_ID[1] || // 'K'
  966. id[2] !== KTX2_ID[2] || // 'T'
  967. id[3] !== KTX2_ID[3] || // 'X'
  968. id[4] !== KTX2_ID[4] || // ' '
  969. id[5] !== KTX2_ID[5] || // '2'
  970. id[6] !== KTX2_ID[6] || // '0'
  971. id[7] !== KTX2_ID[7] || // 'ª'
  972. id[8] !== KTX2_ID[8] || // '\r'
  973. id[9] !== KTX2_ID[9] || // '\n'
  974. id[10] !== KTX2_ID[10] || // '\x1A'
  975. id[11] !== KTX2_ID[11] // '\n'
  976. ) {
  977. throw new Error('Missing KTX 2.0 identifier.');
  978. }
  979. const container = new KTX2Container(); ///////////////////////////////////////////////////
  980. // Header.
  981. ///////////////////////////////////////////////////
  982. const headerByteLength = 17 * Uint32Array.BYTES_PER_ELEMENT;
  983. const headerReader = new BufferReader(data, KTX2_ID.length, headerByteLength, true);
  984. container.vkFormat = headerReader._nextUint32();
  985. container.typeSize = headerReader._nextUint32();
  986. container.pixelWidth = headerReader._nextUint32();
  987. container.pixelHeight = headerReader._nextUint32();
  988. container.pixelDepth = headerReader._nextUint32();
  989. container.layerCount = headerReader._nextUint32();
  990. container.faceCount = headerReader._nextUint32();
  991. const levelCount = headerReader._nextUint32();
  992. container.supercompressionScheme = headerReader._nextUint32();
  993. const dfdByteOffset = headerReader._nextUint32();
  994. const dfdByteLength = headerReader._nextUint32();
  995. const kvdByteOffset = headerReader._nextUint32();
  996. const kvdByteLength = headerReader._nextUint32();
  997. const sgdByteOffset = headerReader._nextUint64();
  998. const sgdByteLength = headerReader._nextUint64(); ///////////////////////////////////////////////////
  999. // Level Index.
  1000. ///////////////////////////////////////////////////
  1001. const levelByteLength = levelCount * 3 * 8;
  1002. const levelReader = new BufferReader(data, KTX2_ID.length + headerByteLength, levelByteLength, true);
  1003. for (let i = 0; i < levelCount; i++) {
  1004. container.levels.push({
  1005. levelData: new Uint8Array(data.buffer, data.byteOffset + levelReader._nextUint64(), levelReader._nextUint64()),
  1006. uncompressedByteLength: levelReader._nextUint64()
  1007. });
  1008. } ///////////////////////////////////////////////////
  1009. // Data Format Descriptor (DFD).
  1010. ///////////////////////////////////////////////////
  1011. const dfdReader = new BufferReader(data, dfdByteOffset, dfdByteLength, true);
  1012. const dfd = {
  1013. vendorId: dfdReader._skip(4
  1014. /* totalSize */
  1015. )._nextUint16(),
  1016. descriptorType: dfdReader._nextUint16(),
  1017. versionNumber: dfdReader._nextUint16(),
  1018. descriptorBlockSize: dfdReader._nextUint16(),
  1019. colorModel: dfdReader._nextUint8(),
  1020. colorPrimaries: dfdReader._nextUint8(),
  1021. transferFunction: dfdReader._nextUint8(),
  1022. flags: dfdReader._nextUint8(),
  1023. texelBlockDimension: [dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8()],
  1024. bytesPlane: [dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8()],
  1025. samples: []
  1026. };
  1027. const sampleStart = 6;
  1028. const sampleWords = 4;
  1029. const numSamples = (dfd.descriptorBlockSize / 4 - sampleStart) / sampleWords;
  1030. for (let i = 0; i < numSamples; i++) {
  1031. const sample = {
  1032. bitOffset: dfdReader._nextUint16(),
  1033. bitLength: dfdReader._nextUint8(),
  1034. channelType: dfdReader._nextUint8(),
  1035. samplePosition: [dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8(), dfdReader._nextUint8()],
  1036. sampleLower: -Infinity,
  1037. sampleUpper: Infinity
  1038. };
  1039. if (sample.channelType & KHR_DF_SAMPLE_DATATYPE_SIGNED) {
  1040. sample.sampleLower = dfdReader._nextInt32();
  1041. sample.sampleUpper = dfdReader._nextInt32();
  1042. } else {
  1043. sample.sampleLower = dfdReader._nextUint32();
  1044. sample.sampleUpper = dfdReader._nextUint32();
  1045. }
  1046. dfd.samples[i] = sample;
  1047. }
  1048. container.dataFormatDescriptor.length = 0;
  1049. container.dataFormatDescriptor.push(dfd); ///////////////////////////////////////////////////
  1050. // Key/Value Data (KVD).
  1051. ///////////////////////////////////////////////////
  1052. const kvdReader = new BufferReader(data, kvdByteOffset, kvdByteLength, true);
  1053. while (kvdReader._offset < kvdByteLength) {
  1054. const keyValueByteLength = kvdReader._nextUint32();
  1055. const keyData = kvdReader._scan(keyValueByteLength);
  1056. const key = decodeText(keyData);
  1057. const valueData = kvdReader._scan(keyValueByteLength - keyData.byteLength);
  1058. container.keyValue[key] = key.match(/^ktx/i) ? decodeText(valueData) : valueData; // 4-byte alignment.
  1059. if (kvdReader._offset % 4) kvdReader._skip(4 - kvdReader._offset % 4);
  1060. } ///////////////////////////////////////////////////
  1061. // Supercompression Global Data (SGD).
  1062. ///////////////////////////////////////////////////
  1063. if (sgdByteLength <= 0) return container;
  1064. const sgdReader = new BufferReader(data, sgdByteOffset, sgdByteLength, true);
  1065. const endpointCount = sgdReader._nextUint16();
  1066. const selectorCount = sgdReader._nextUint16();
  1067. const endpointsByteLength = sgdReader._nextUint32();
  1068. const selectorsByteLength = sgdReader._nextUint32();
  1069. const tablesByteLength = sgdReader._nextUint32();
  1070. const extendedByteLength = sgdReader._nextUint32();
  1071. const imageDescs = [];
  1072. for (let i = 0; i < levelCount; i++) {
  1073. imageDescs.push({
  1074. imageFlags: sgdReader._nextUint32(),
  1075. rgbSliceByteOffset: sgdReader._nextUint32(),
  1076. rgbSliceByteLength: sgdReader._nextUint32(),
  1077. alphaSliceByteOffset: sgdReader._nextUint32(),
  1078. alphaSliceByteLength: sgdReader._nextUint32()
  1079. });
  1080. }
  1081. const endpointsByteOffset = sgdByteOffset + sgdReader._offset;
  1082. const selectorsByteOffset = endpointsByteOffset + endpointsByteLength;
  1083. const tablesByteOffset = selectorsByteOffset + selectorsByteLength;
  1084. const extendedByteOffset = tablesByteOffset + tablesByteLength;
  1085. const endpointsData = new Uint8Array(data.buffer, data.byteOffset + endpointsByteOffset, endpointsByteLength);
  1086. const selectorsData = new Uint8Array(data.buffer, data.byteOffset + selectorsByteOffset, selectorsByteLength);
  1087. const tablesData = new Uint8Array(data.buffer, data.byteOffset + tablesByteOffset, tablesByteLength);
  1088. const extendedData = new Uint8Array(data.buffer, data.byteOffset + extendedByteOffset, extendedByteLength);
  1089. container.globalData = {
  1090. endpointCount,
  1091. selectorCount,
  1092. imageDescs,
  1093. endpointsData,
  1094. selectorsData,
  1095. tablesData,
  1096. extendedData
  1097. };
  1098. return container;
  1099. }
  1100. /* global require */
  1101. const faceOrder = [
  1102. "positiveX",
  1103. "negativeX",
  1104. "positiveY",
  1105. "negativeY",
  1106. "positiveZ",
  1107. "negativeZ",
  1108. ];
  1109. // Flags
  1110. const colorModelETC1S = 163;
  1111. const colorModelUASTC = 166;
  1112. let transcoderModule;
  1113. function transcode(parameters, transferableObjects) {
  1114. //>>includeStart('debug', pragmas.debug);
  1115. Check.Check.typeOf.object("transcoderModule", transcoderModule);
  1116. //>>includeEnd('debug');
  1117. const data = parameters.ktx2Buffer;
  1118. const supportedTargetFormats = parameters.supportedTargetFormats;
  1119. let header;
  1120. try {
  1121. header = read(data);
  1122. } catch (e) {
  1123. throw new RuntimeError.RuntimeError("Invalid KTX2 file.");
  1124. }
  1125. if (header.layerCount !== 0) {
  1126. throw new RuntimeError.RuntimeError("KTX2 texture arrays are not supported.");
  1127. }
  1128. if (header.pixelDepth !== 0) {
  1129. throw new RuntimeError.RuntimeError("KTX2 3D textures are unsupported.");
  1130. }
  1131. const dfd = header.dataFormatDescriptor[0];
  1132. const result = new Array(header.levelCount);
  1133. if (
  1134. header.vkFormat === 0x0 &&
  1135. (dfd.colorModel === colorModelETC1S || dfd.colorModel === colorModelUASTC)
  1136. ) {
  1137. // Compressed, initialize transcoder module
  1138. transcodeCompressed(
  1139. data,
  1140. header,
  1141. supportedTargetFormats,
  1142. transcoderModule,
  1143. transferableObjects,
  1144. result
  1145. );
  1146. } else {
  1147. transferableObjects.push(data.buffer);
  1148. parseUncompressed(header, result);
  1149. }
  1150. return result;
  1151. }
  1152. // Parser for uncompressed
  1153. function parseUncompressed(header, result) {
  1154. const internalFormat =
  1155. header.vkFormat === VulkanConstants$1.VK_FORMAT_R8G8B8_SRGB
  1156. ? PixelFormat$1.RGB
  1157. : PixelFormat$1.RGBA;
  1158. let datatype;
  1159. if (header.vkFormat === VulkanConstants$1.VK_FORMAT_R8G8B8A8_UNORM) {
  1160. datatype = PixelDatatype$1.UNSIGNED_BYTE;
  1161. } else if (
  1162. header.vkFormat === VulkanConstants$1.VK_FORMAT_R16G16B16A16_SFLOAT
  1163. ) {
  1164. datatype = PixelDatatype$1.HALF_FLOAT;
  1165. } else if (
  1166. header.vkFormat === VulkanConstants$1.VK_FORMAT_R32G32B32A32_SFLOAT
  1167. ) {
  1168. datatype = PixelDatatype$1.FLOAT;
  1169. }
  1170. for (let i = 0; i < header.levels.length; ++i) {
  1171. const level = {};
  1172. result[i] = level;
  1173. const levelBuffer = header.levels[i].levelData;
  1174. const width = header.pixelWidth >> i;
  1175. const height = header.pixelHeight >> i;
  1176. const faceLength =
  1177. width * height * PixelFormat$1.componentsLength(internalFormat);
  1178. for (let j = 0; j < header.faceCount; ++j) {
  1179. // multiply levelBuffer.byteOffset by the size in bytes of the pixel data type
  1180. const faceByteOffset =
  1181. levelBuffer.byteOffset + faceLength * header.typeSize * j;
  1182. let faceView;
  1183. if (!defaultValue.defined(datatype) || PixelDatatype$1.sizeInBytes(datatype) === 1) {
  1184. faceView = new Uint8Array(
  1185. levelBuffer.buffer,
  1186. faceByteOffset,
  1187. faceLength
  1188. );
  1189. } else if (PixelDatatype$1.sizeInBytes(datatype) === 2) {
  1190. faceView = new Uint16Array(
  1191. levelBuffer.buffer,
  1192. faceByteOffset,
  1193. faceLength
  1194. );
  1195. } else {
  1196. faceView = new Float32Array(
  1197. levelBuffer.buffer,
  1198. faceByteOffset,
  1199. faceLength
  1200. );
  1201. }
  1202. level[faceOrder[j]] = {
  1203. internalFormat: internalFormat,
  1204. datatype: datatype,
  1205. width: width,
  1206. height: height,
  1207. levelBuffer: faceView,
  1208. };
  1209. }
  1210. }
  1211. }
  1212. function transcodeCompressed(
  1213. data,
  1214. header,
  1215. supportedTargetFormats,
  1216. transcoderModule,
  1217. transferableObjects,
  1218. result
  1219. ) {
  1220. const ktx2File = new transcoderModule.KTX2File(data);
  1221. let width = ktx2File.getWidth();
  1222. let height = ktx2File.getHeight();
  1223. const levels = ktx2File.getLevels();
  1224. const hasAlpha = ktx2File.getHasAlpha();
  1225. if (!(width > 0) || !(height > 0) || !(levels > 0)) {
  1226. ktx2File.close();
  1227. ktx2File.delete();
  1228. throw new RuntimeError.RuntimeError("Invalid KTX2 file");
  1229. }
  1230. let internalFormat, transcoderFormat;
  1231. const dfd = header.dataFormatDescriptor[0];
  1232. const BasisFormat = transcoderModule.transcoder_texture_format;
  1233. // Determine target format based on platform support
  1234. if (dfd.colorModel === colorModelETC1S) {
  1235. if (supportedTargetFormats.etc) {
  1236. internalFormat = hasAlpha
  1237. ? PixelFormat$1.RGBA8_ETC2_EAC
  1238. : PixelFormat$1.RGB8_ETC2;
  1239. transcoderFormat = hasAlpha
  1240. ? BasisFormat.cTFETC2_RGBA
  1241. : BasisFormat.cTFETC1_RGB;
  1242. } else if (supportedTargetFormats.etc1 && !hasAlpha) {
  1243. internalFormat = PixelFormat$1.RGB_ETC1;
  1244. transcoderFormat = BasisFormat.cTFETC1_RGB;
  1245. } else if (supportedTargetFormats.s3tc) {
  1246. internalFormat = hasAlpha ? PixelFormat$1.RGBA_DXT5 : PixelFormat$1.RGB_DXT1;
  1247. transcoderFormat = hasAlpha
  1248. ? BasisFormat.cTFBC3_RGBA
  1249. : BasisFormat.cTFBC1_RGB;
  1250. } else if (supportedTargetFormats.pvrtc) {
  1251. internalFormat = hasAlpha
  1252. ? PixelFormat$1.RGBA_PVRTC_4BPPV1
  1253. : PixelFormat$1.RGB_PVRTC_4BPPV1;
  1254. transcoderFormat = hasAlpha
  1255. ? BasisFormat.cTFPVRTC1_4_RGBA
  1256. : BasisFormat.cTFPVRTC1_4_RGB;
  1257. } else if (supportedTargetFormats.astc) {
  1258. internalFormat = PixelFormat$1.RGBA_ASTC;
  1259. transcoderFormat = BasisFormat.cTFASTC_4x4_RGBA;
  1260. } else if (supportedTargetFormats.bc7) {
  1261. internalFormat = PixelFormat$1.RGBA_BC7;
  1262. transcoderFormat = BasisFormat.cTFBC7_RGBA;
  1263. } else {
  1264. throw new RuntimeError.RuntimeError(
  1265. "No transcoding format target available for ETC1S compressed ktx2."
  1266. );
  1267. }
  1268. } else if (dfd.colorModel === colorModelUASTC) {
  1269. if (supportedTargetFormats.astc) {
  1270. internalFormat = PixelFormat$1.RGBA_ASTC;
  1271. transcoderFormat = BasisFormat.cTFASTC_4x4_RGBA;
  1272. } else if (supportedTargetFormats.bc7) {
  1273. internalFormat = PixelFormat$1.RGBA_BC7;
  1274. transcoderFormat = BasisFormat.cTFBC7_RGBA;
  1275. } else if (supportedTargetFormats.s3tc) {
  1276. internalFormat = hasAlpha ? PixelFormat$1.RGBA_DXT5 : PixelFormat$1.RGB_DXT1;
  1277. transcoderFormat = hasAlpha
  1278. ? BasisFormat.cTFBC3_RGBA
  1279. : BasisFormat.cTFBC1_RGB;
  1280. } else if (supportedTargetFormats.etc) {
  1281. internalFormat = hasAlpha
  1282. ? PixelFormat$1.RGBA8_ETC2_EAC
  1283. : PixelFormat$1.RGB8_ETC2;
  1284. transcoderFormat = hasAlpha
  1285. ? BasisFormat.cTFETC2_RGBA
  1286. : BasisFormat.cTFETC1_RGB;
  1287. } else if (supportedTargetFormats.etc1 && !hasAlpha) {
  1288. internalFormat = PixelFormat$1.RGB_ETC1;
  1289. transcoderFormat = BasisFormat.cTFETC1_RGB;
  1290. } else if (supportedTargetFormats.pvrtc) {
  1291. internalFormat = hasAlpha
  1292. ? PixelFormat$1.RGBA_PVRTC_4BPPV1
  1293. : PixelFormat$1.RGB_PVRTC_4BPPV1;
  1294. transcoderFormat = hasAlpha
  1295. ? BasisFormat.cTFPVRTC1_4_RGBA
  1296. : BasisFormat.cTFPVRTC1_4_RGB;
  1297. } else {
  1298. throw new RuntimeError.RuntimeError(
  1299. "No transcoding format target available for UASTC compressed ktx2."
  1300. );
  1301. }
  1302. }
  1303. if (!ktx2File.startTranscoding()) {
  1304. ktx2File.close();
  1305. ktx2File.delete();
  1306. throw new RuntimeError.RuntimeError("startTranscoding() failed");
  1307. }
  1308. for (let i = 0; i < header.levels.length; ++i) {
  1309. const level = {};
  1310. result[i] = level;
  1311. width = header.pixelWidth >> i;
  1312. height = header.pixelHeight >> i;
  1313. // Since supercompressed cubemaps are unsupported, this function
  1314. // does not iterate over KTX2 faces and assumes faceCount = 1.
  1315. const dstSize = ktx2File.getImageTranscodedSizeInBytes(
  1316. i, // level index
  1317. 0, // layer index
  1318. 0, // face index
  1319. transcoderFormat.value
  1320. );
  1321. const dst = new Uint8Array(dstSize);
  1322. const transcoded = ktx2File.transcodeImage(
  1323. dst,
  1324. i, // level index
  1325. 0, // layer index
  1326. 0, // face index
  1327. transcoderFormat.value,
  1328. 0, // get_alpha_for_opaque_formats
  1329. -1, // channel0
  1330. -1 // channel1
  1331. );
  1332. if (!defaultValue.defined(transcoded)) {
  1333. throw new RuntimeError.RuntimeError("transcodeImage() failed.");
  1334. }
  1335. transferableObjects.push(dst.buffer);
  1336. level[faceOrder[0]] = {
  1337. internalFormat: internalFormat,
  1338. width: width,
  1339. height: height,
  1340. levelBuffer: dst,
  1341. };
  1342. }
  1343. ktx2File.close();
  1344. ktx2File.delete();
  1345. return result;
  1346. }
  1347. function initWorker(compiledModule) {
  1348. transcoderModule = compiledModule;
  1349. transcoderModule.initializeBasis();
  1350. self.onmessage = createTaskProcessorWorker(transcode);
  1351. self.postMessage(true);
  1352. }
  1353. function transcodeKTX2(event) {
  1354. const data = event.data;
  1355. // Expect the first message to be to load a web assembly module
  1356. const wasmConfig = data.webAssemblyConfig;
  1357. if (defaultValue.defined(wasmConfig)) {
  1358. // Require and compile WebAssembly module, or use fallback if not supported
  1359. return require([wasmConfig.modulePath], function (mscBasisTranscoder) {
  1360. if (defaultValue.defined(wasmConfig.wasmBinaryFile)) {
  1361. if (!defaultValue.defined(mscBasisTranscoder)) {
  1362. mscBasisTranscoder = self.MSC_TRANSCODER;
  1363. }
  1364. mscBasisTranscoder(wasmConfig).then(function (compiledModule) {
  1365. initWorker(compiledModule);
  1366. });
  1367. } else {
  1368. return mscBasisTranscoder().then(function (transcoder) {
  1369. initWorker(transcoder);
  1370. });
  1371. }
  1372. });
  1373. }
  1374. }
  1375. return transcodeKTX2;
  1376. }));