ktx.h 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /* -*- tab-width: 4; -*- */
  2. /* vi: set sw=2 ts=4 expandtab: */
  3. #ifndef KTX_H_A55A6F00956F42F3A137C11929827FE1
  4. #define KTX_H_A55A6F00956F42F3A137C11929827FE1
  5. /*
  6. * Copyright 2010-2018 The Khronos Group, Inc.
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * See the accompanying LICENSE.md for licensing details for all files in
  10. * the KTX library and KTX loader tests.
  11. */
  12. /**
  13. * @file
  14. * @~English
  15. *
  16. * @brief Declares the public functions and structures of the
  17. * KTX API.
  18. *
  19. * @author Mark Callow, Edgewise Consulting and while at HI Corporation
  20. * @author Based on original work by Georg Kolling, Imagination Technology
  21. *
  22. * @snippet{doc} version.h API version
  23. */
  24. #include <limits.h>
  25. #include <stdio.h>
  26. #include <stdbool.h>
  27. #include <sys/types.h>
  28. #include <KHR/khr_df.h>
  29. /*
  30. * Don't use khrplatform.h in order not to break apps existing
  31. * before these definitions were needed.
  32. */
  33. #if defined(KHRONOS_STATIC)
  34. #define KTX_API
  35. #elif defined(_WIN32) || defined(__CYGWIN__)
  36. #if !defined(KTX_API)
  37. #if __GNUC__
  38. #define KTX_API __attribute__ ((dllimport))
  39. #elif _MSC_VER
  40. #define KTX_API __declspec(dllimport)
  41. #else
  42. #error "Your compiler's equivalent of dllimport is unknown"
  43. #endif
  44. #endif
  45. #elif defined(__ANDROID__)
  46. #define KTX_API __attribute__((visibility("default")))
  47. #else
  48. #define KTX_API
  49. #endif
  50. #if defined(_WIN32) && !defined(KHRONOS_STATIC)
  51. #if !defined(KTX_APIENTRY)
  52. #define KTX_APIENTRY __stdcall
  53. #endif
  54. #else
  55. #define KTX_APIENTRY
  56. #endif
  57. /* To avoid including <KHR/khrplatform.h> define our own types. */
  58. typedef unsigned char ktx_uint8_t;
  59. typedef bool ktx_bool_t;
  60. #ifdef _MSC_VER
  61. typedef unsigned __int16 ktx_uint16_t;
  62. typedef signed __int16 ktx_int16_t;
  63. typedef unsigned __int32 ktx_uint32_t;
  64. typedef signed __int32 ktx_int32_t;
  65. typedef size_t ktx_size_t;
  66. typedef unsigned __int64 ktx_uint64_t;
  67. typedef signed __int64 ktx_int64_t;
  68. #else
  69. #include <stdint.h>
  70. typedef uint16_t ktx_uint16_t;
  71. typedef int16_t ktx_int16_t;
  72. typedef uint32_t ktx_uint32_t;
  73. typedef int32_t ktx_int32_t;
  74. typedef size_t ktx_size_t;
  75. typedef uint64_t ktx_uint64_t;
  76. typedef int64_t ktx_int64_t;
  77. #endif
  78. /* This will cause compilation to fail if size of uint32 != 4. */
  79. typedef unsigned char ktx_uint32_t_SIZE_ASSERT[sizeof(ktx_uint32_t) == 4];
  80. /*
  81. * This #if allows libktx to be compiled with strict c99. It avoids
  82. * compiler warnings or even errors when a gl.h is already included.
  83. * "Redefinition of (type) is a c11 feature". Obviously this doesn't help if
  84. * gl.h comes after. However nobody has complained about the unguarded typedefs
  85. * since they were introduced so this is unlikely to be a problem in practice.
  86. * Presumably everybody is using platform default compilers not c99 or else
  87. * they are using C++.
  88. */
  89. #if !defined(GL_NO_ERROR)
  90. /*
  91. * To avoid having to including gl.h ...
  92. */
  93. typedef unsigned char GLboolean;
  94. typedef unsigned int GLenum;
  95. typedef int GLint;
  96. typedef int GLsizei;
  97. typedef unsigned int GLuint;
  98. typedef unsigned char GLubyte;
  99. #endif
  100. #ifdef __cplusplus
  101. extern "C" {
  102. #endif
  103. /**
  104. * @~English
  105. * @brief Key string for standard writer metadata.
  106. */
  107. #define KTX_ANIMDATA_KEY "KTXanimData"
  108. /**
  109. * @~English
  110. * @brief Key string for standard orientation metadata.
  111. */
  112. #define KTX_ORIENTATION_KEY "KTXorientation"
  113. /**
  114. * @~English
  115. * @brief Key string for standard swizzle metadata.
  116. */
  117. #define KTX_SWIZZLE_KEY "KTXswizzle"
  118. /**
  119. * @~English
  120. * @brief Key string for standard writer metadata.
  121. */
  122. #define KTX_WRITER_KEY "KTXwriter"
  123. /**
  124. * @~English
  125. * @brief Key string for standard writer supercompression parameter metadata.
  126. */
  127. #define KTX_WRITER_SCPARAMS_KEY "KTXwriterScParams"
  128. /**
  129. * @~English
  130. * @brief Standard KTX 1 format for 1D orientation value.
  131. */
  132. #define KTX_ORIENTATION1_FMT "S=%c"
  133. /**
  134. * @~English
  135. * @brief Standard KTX 1 format for 2D orientation value.
  136. */
  137. #define KTX_ORIENTATION2_FMT "S=%c,T=%c"
  138. /**
  139. * @~English
  140. * @brief Standard KTX 1 format for 3D orientation value.
  141. */
  142. #define KTX_ORIENTATION3_FMT "S=%c,T=%c,R=%c"
  143. /**
  144. * @~English
  145. * @brief Required unpack alignment
  146. */
  147. #define KTX_GL_UNPACK_ALIGNMENT 4
  148. #define KTX_FACESLICE_WHOLE_LEVEL UINT_MAX
  149. #define KTX_TRUE true
  150. #define KTX_FALSE false
  151. /**
  152. * @~English
  153. * @brief Error codes returned by library functions.
  154. */
  155. typedef enum ktx_error_code_e {
  156. KTX_SUCCESS = 0, /*!< Operation was successful. */
  157. KTX_FILE_DATA_ERROR, /*!< The data in the file is inconsistent with the spec. */
  158. KTX_FILE_ISPIPE, /*!< The file is a pipe or named pipe. */
  159. KTX_FILE_OPEN_FAILED, /*!< The target file could not be opened. */
  160. KTX_FILE_OVERFLOW, /*!< The operation would exceed the max file size. */
  161. KTX_FILE_READ_ERROR, /*!< An error occurred while reading from the file. */
  162. KTX_FILE_SEEK_ERROR, /*!< An error occurred while seeking in the file. */
  163. KTX_FILE_UNEXPECTED_EOF, /*!< File does not have enough data to satisfy request. */
  164. KTX_FILE_WRITE_ERROR, /*!< An error occurred while writing to the file. */
  165. KTX_GL_ERROR, /*!< GL operations resulted in an error. */
  166. KTX_INVALID_OPERATION, /*!< The operation is not allowed in the current state. */
  167. KTX_INVALID_VALUE, /*!< A parameter value was not valid. */
  168. KTX_NOT_FOUND, /*!< Requested metadata key or required dynamically loaded GPU function was not found. */
  169. KTX_OUT_OF_MEMORY, /*!< Not enough memory to complete the operation. */
  170. KTX_TRANSCODE_FAILED, /*!< Transcoding of block compressed texture failed. */
  171. KTX_UNKNOWN_FILE_FORMAT, /*!< The file not a KTX file */
  172. KTX_UNSUPPORTED_TEXTURE_TYPE, /*!< The KTX file specifies an unsupported texture type. */
  173. KTX_UNSUPPORTED_FEATURE, /*!< Feature not included in in-use library or not yet implemented. */
  174. KTX_LIBRARY_NOT_LINKED, /*!< Library dependency (OpenGL or Vulkan) not linked into application. */
  175. KTX_DECOMPRESS_LENGTH_ERROR, /*!< Decompressed byte count does not match expected byte size */
  176. KTX_DECOMPRESS_CHECKSUM_ERROR, /*!< Checksum mismatch when decompressing */
  177. KTX_ERROR_MAX_ENUM = KTX_DECOMPRESS_CHECKSUM_ERROR /*!< For safety checks. */
  178. } ktx_error_code_e;
  179. /**
  180. * @deprecated
  181. * @~English
  182. * @brief For backward compatibility
  183. */
  184. #define KTX_error_code ktx_error_code_e
  185. #define KTX_IDENTIFIER_REF { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }
  186. #define KTX_ENDIAN_REF (0x04030201)
  187. #define KTX_ENDIAN_REF_REV (0x01020304)
  188. #define KTX_HEADER_SIZE (64)
  189. /**
  190. * @~English
  191. * @brief Result codes returned by library functions.
  192. */
  193. typedef enum ktx_error_code_e ktxResult;
  194. /**
  195. * @class ktxHashList
  196. * @~English
  197. * @brief Opaque handle to a ktxHashList.
  198. */
  199. typedef struct ktxKVListEntry* ktxHashList;
  200. typedef struct ktxStream ktxStream;
  201. #define KTX_APIENTRYP KTX_APIENTRY *
  202. /**
  203. * @class ktxHashListEntry
  204. * @~English
  205. * @brief Opaque handle to an entry in a @ref ktxHashList.
  206. */
  207. typedef struct ktxKVListEntry ktxHashListEntry;
  208. typedef enum ktxOrientationX {
  209. KTX_ORIENT_X_LEFT = 'l', KTX_ORIENT_X_RIGHT = 'r'
  210. } ktxOrientationX;
  211. typedef enum ktxOrientationY {
  212. KTX_ORIENT_Y_UP = 'u', KTX_ORIENT_Y_DOWN = 'd'
  213. } ktxOrientationY;
  214. typedef enum ktxOrientationZ {
  215. KTX_ORIENT_Z_IN = 'i', KTX_ORIENT_Z_OUT = 'o'
  216. } ktxOrientationZ;
  217. typedef enum class_id {
  218. ktxTexture1_c = 1,
  219. ktxTexture2_c = 2
  220. } class_id;
  221. /**
  222. * @~English
  223. * @brief Struct describing the logical orientation of an image.
  224. */
  225. struct ktxOrientation {
  226. ktxOrientationX x; /*!< Orientation in X */
  227. ktxOrientationY y; /*!< Orientation in Y */
  228. ktxOrientationZ z; /*!< Orientation in Z */
  229. };
  230. #define KTXTEXTURECLASSDEFN \
  231. class_id classId; \
  232. struct ktxTexture_vtbl* vtbl; \
  233. struct ktxTexture_vvtbl* vvtbl; \
  234. struct ktxTexture_protected* _protected; \
  235. ktx_bool_t isArray; \
  236. ktx_bool_t isCubemap; \
  237. ktx_bool_t isCompressed; \
  238. ktx_bool_t generateMipmaps; \
  239. ktx_uint32_t baseWidth; \
  240. ktx_uint32_t baseHeight; \
  241. ktx_uint32_t baseDepth; \
  242. ktx_uint32_t numDimensions; \
  243. ktx_uint32_t numLevels; \
  244. ktx_uint32_t numLayers; \
  245. ktx_uint32_t numFaces; \
  246. struct ktxOrientation orientation; \
  247. ktxHashList kvDataHead; \
  248. ktx_uint32_t kvDataLen; \
  249. ktx_uint8_t* kvData; \
  250. ktx_size_t dataSize; \
  251. ktx_uint8_t* pData;
  252. /**
  253. * @class ktxTexture
  254. * @~English
  255. * @brief Base class representing a texture.
  256. *
  257. * ktxTextures should be created only by one of the provided
  258. * functions and these fields should be considered read-only.
  259. */
  260. typedef struct ktxTexture {
  261. KTXTEXTURECLASSDEFN
  262. } ktxTexture;
  263. /**
  264. * @typedef ktxTexture::classId
  265. * @~English
  266. * @brief Identify the class type.
  267. *
  268. * Since there are no public ktxTexture constructors, this can only have
  269. * values of ktxTexture1_c or ktxTexture2_c.
  270. */
  271. /**
  272. * @typedef ktxTexture::vtbl
  273. * @~English
  274. * @brief Pointer to the class's vtble.
  275. */
  276. /**
  277. * @typedef ktxTexture::vvtbl
  278. * @~English
  279. * @brief Pointer to the class's vtble for Vulkan functions.
  280. *
  281. * A separate vtble is used so this header does not need to include vulkan.h.
  282. */
  283. /**
  284. * @typedef ktxTexture::_protected
  285. * @~English
  286. * @brief Opaque pointer to the class's protected variables.
  287. */
  288. /**
  289. * @typedef ktxTexture::isArray
  290. * @~English
  291. *
  292. * KTX_TRUE if the texture is an array texture, i.e,
  293. * a GL_TEXTURE_*_ARRAY target is to be used.
  294. */
  295. /**
  296. * @typedef ktxTexture::isCubemap
  297. * @~English
  298. *
  299. * KTX_TRUE if the texture is a cubemap or cubemap array.
  300. */
  301. /**
  302. * @typedef ktxTexture::isCubemap
  303. * @~English
  304. *
  305. * KTX_TRUE if the texture's format is a block compressed format.
  306. */
  307. /**
  308. * @typedef ktxTexture::generateMipmaps
  309. * @~English
  310. *
  311. * KTX_TRUE if mipmaps should be generated for the texture by
  312. * ktxTexture_GLUpload() or ktxTexture_VkUpload().
  313. */
  314. /**n
  315. * @typedef ktxTexture::baseWidth
  316. * @~English
  317. * @brief Width of the texture's base level.
  318. */
  319. /**
  320. * @typedef ktxTexture::baseHeight
  321. * @~English
  322. * @brief Height of the texture's base level.
  323. */
  324. /**
  325. * @typedef ktxTexture::baseDepth
  326. * @~English
  327. * @brief Depth of the texture's base level.
  328. */
  329. /**
  330. * @typedef ktxTexture::numDimensions
  331. * @~English
  332. * @brief Number of dimensions in the texture: 1, 2 or 3.
  333. */
  334. /**
  335. * @typedef ktxTexture::numLevels
  336. * @~English
  337. * @brief Number of mip levels in the texture.
  338. *
  339. * Must be 1, if @c generateMipmaps is KTX_TRUE. Can be less than a
  340. * full pyramid but always starts at the base level.
  341. */
  342. /**
  343. * @typedef ktxTexture::numLevels
  344. * @~English
  345. * @brief Number of array layers in the texture.
  346. */
  347. /**
  348. * @typedef ktxTexture::numFaces
  349. * @~English
  350. * @brief Number of faces: 6 for cube maps, 1 otherwise.
  351. */
  352. /**
  353. * @typedef ktxTexture::orientation
  354. * @~English
  355. * @brief Describes the logical orientation of the images in each dimension.
  356. *
  357. * ktxOrientationX for X, ktxOrientationY for Y and ktxOrientationZ for Z.
  358. */
  359. /**
  360. * @typedef ktxTexture::kvDataHead
  361. * @~English
  362. * @brief Head of the hash list of metadata.
  363. */
  364. /**
  365. * @typedef ktxTexture::kvDataLen
  366. * @~English
  367. * @brief Length of the metadata, if it has been extracted in its raw form,
  368. * otherwise 0.
  369. */
  370. /**
  371. * @typedef ktxTexture::kvData
  372. * @~English
  373. * @brief Pointer to the metadata, if it has been extracted in its raw form,
  374. * otherwise NULL.
  375. */
  376. /**
  377. * @typedef ktxTexture::dataSize
  378. * @~English
  379. * @brief Byte length of the texture's uncompressed image data.
  380. */
  381. /**
  382. * @typedef ktxTexture::pData
  383. * @~English
  384. * @brief Pointer to the start of the image data.
  385. */
  386. /**
  387. * @memberof ktxTexture
  388. * @~English
  389. * @brief Signature of function called by the <tt>ktxTexture_Iterate*</tt>
  390. * functions to receive image data.
  391. *
  392. * The function parameters are used to pass values which change for each image.
  393. * Obtain values which are uniform across all images from the @c ktxTexture
  394. * object.
  395. *
  396. * @param [in] miplevel MIP level from 0 to the max level which is
  397. * dependent on the texture size.
  398. * @param [in] face usually 0; for cube maps, one of the 6 cube
  399. * faces in the order +X, -X, +Y, -Y, +Z, -Z,
  400. * 0 to 5.
  401. * @param [in] width width of the image.
  402. * @param [in] height height of the image or, for 1D textures
  403. * textures, 1.
  404. * @param [in] depth depth of the image or, for 1D & 2D
  405. * textures, 1.
  406. * @param [in] faceLodSize number of bytes of data pointed at by
  407. * @p pixels.
  408. * @param [in] pixels pointer to the image data.
  409. * @param [in,out] userdata pointer for the application to pass data to and
  410. * from the callback function.
  411. */
  412. typedef KTX_error_code
  413. (* PFNKTXITERCB)(int miplevel, int face,
  414. int width, int height, int depth,
  415. ktx_uint64_t faceLodSize,
  416. void* pixels, void* userdata);
  417. /* Don't use KTX_APIENTRYP to avoid a Doxygen bug. */
  418. typedef void (KTX_APIENTRY* PFNKTEXDESTROY)(ktxTexture* This);
  419. typedef KTX_error_code
  420. (KTX_APIENTRY* PFNKTEXGETIMAGEOFFSET)(ktxTexture* This, ktx_uint32_t level,
  421. ktx_uint32_t layer,
  422. ktx_uint32_t faceSlice,
  423. ktx_size_t* pOffset);
  424. typedef ktx_size_t
  425. (KTX_APIENTRY* PFNKTEXGETDATASIZEUNCOMPRESSED)(ktxTexture* This);
  426. typedef ktx_size_t
  427. (KTX_APIENTRY* PFNKTEXGETIMAGESIZE)(ktxTexture* This, ktx_uint32_t level);
  428. typedef KTX_error_code
  429. (KTX_APIENTRY* PFNKTEXITERATELEVELS)(ktxTexture* This, PFNKTXITERCB iterCb,
  430. void* userdata);
  431. typedef KTX_error_code
  432. (KTX_APIENTRY* PFNKTEXITERATELOADLEVELFACES)(ktxTexture* This,
  433. PFNKTXITERCB iterCb,
  434. void* userdata);
  435. typedef KTX_error_code
  436. (KTX_APIENTRY* PFNKTEXLOADIMAGEDATA)(ktxTexture* This,
  437. ktx_uint8_t* pBuffer,
  438. ktx_size_t bufSize);
  439. typedef ktx_bool_t
  440. (KTX_APIENTRY* PFNKTEXNEEDSTRANSCODING)(ktxTexture* This);
  441. typedef KTX_error_code
  442. (KTX_APIENTRY* PFNKTEXSETIMAGEFROMMEMORY)(ktxTexture* This,
  443. ktx_uint32_t level,
  444. ktx_uint32_t layer,
  445. ktx_uint32_t faceSlice,
  446. const ktx_uint8_t* src,
  447. ktx_size_t srcSize);
  448. typedef KTX_error_code
  449. (KTX_APIENTRY* PFNKTEXSETIMAGEFROMSTDIOSTREAM)(ktxTexture* This,
  450. ktx_uint32_t level,
  451. ktx_uint32_t layer,
  452. ktx_uint32_t faceSlice,
  453. FILE* src, ktx_size_t srcSize);
  454. typedef KTX_error_code
  455. (KTX_APIENTRY* PFNKTEXWRITETOSTDIOSTREAM)(ktxTexture* This, FILE* dstsstr);
  456. typedef KTX_error_code
  457. (KTX_APIENTRY* PFNKTEXWRITETONAMEDFILE)(ktxTexture* This,
  458. const char* const dstname);
  459. typedef KTX_error_code
  460. (KTX_APIENTRY* PFNKTEXWRITETOMEMORY)(ktxTexture* This,
  461. ktx_uint8_t** bytes, ktx_size_t* size);
  462. typedef KTX_error_code
  463. (KTX_APIENTRY* PFNKTEXWRITETOSTREAM)(ktxTexture* This,
  464. ktxStream* dststr);
  465. /**
  466. * @memberof ktxTexture
  467. * @~English
  468. * @brief Table of virtual ktxTexture methods.
  469. */
  470. struct ktxTexture_vtbl {
  471. PFNKTEXDESTROY Destroy;
  472. PFNKTEXGETIMAGEOFFSET GetImageOffset;
  473. PFNKTEXGETDATASIZEUNCOMPRESSED GetDataSizeUncompressed;
  474. PFNKTEXGETIMAGESIZE GetImageSize;
  475. PFNKTEXITERATELEVELS IterateLevels;
  476. PFNKTEXITERATELOADLEVELFACES IterateLoadLevelFaces;
  477. PFNKTEXNEEDSTRANSCODING NeedsTranscoding;
  478. PFNKTEXLOADIMAGEDATA LoadImageData;
  479. PFNKTEXSETIMAGEFROMMEMORY SetImageFromMemory;
  480. PFNKTEXSETIMAGEFROMSTDIOSTREAM SetImageFromStdioStream;
  481. PFNKTEXWRITETOSTDIOSTREAM WriteToStdioStream;
  482. PFNKTEXWRITETONAMEDFILE WriteToNamedFile;
  483. PFNKTEXWRITETOMEMORY WriteToMemory;
  484. PFNKTEXWRITETOSTREAM WriteToStream;
  485. };
  486. /****************************************************************
  487. * Macros to give some backward compatibility to the previous API
  488. ****************************************************************/
  489. /**
  490. * @~English
  491. * @brief Helper for calling the Destroy virtual method of a ktxTexture.
  492. * @copydoc ktxTexture2.ktxTexture2_Destroy
  493. */
  494. #define ktxTexture_Destroy(This) (This)->vtbl->Destroy(This)
  495. /**
  496. * @~English
  497. * @brief Helper for calling the GetImageOffset virtual method of a
  498. * ktxTexture.
  499. * @copydoc ktxTexture2.ktxTexture2_GetImageOffset
  500. */
  501. #define ktxTexture_GetImageOffset(This, level, layer, faceSlice, pOffset) \
  502. (This)->vtbl->GetImageOffset(This, level, layer, faceSlice, pOffset)
  503. /**
  504. * @~English
  505. * @brief Helper for calling the GetDataSizeUncompressed virtual method of a ktxTexture.
  506. *
  507. * For a ktxTexture1 this will always return the value of This->dataSize.
  508. *
  509. * @copydetails ktxTexture2.ktxTexture2_GetDataSizeUncompressed
  510. */
  511. #define ktxTexture_GetDataSizeUncompressed(This) \
  512. (This)->vtbl->GetDataSizeUncompressed(This)
  513. /**
  514. * @~English
  515. * @brief Helper for calling the GetImageSize virtual method of a ktxTexture.
  516. * @copydoc ktxTexture2.ktxTexture2_GetImageSize
  517. */
  518. #define ktxTexture_GetImageSize(This, level) \
  519. (This)->vtbl->GetImageSize(This, level)
  520. /**
  521. * @~English
  522. * @brief Helper for calling the IterateLevels virtual method of a ktxTexture.
  523. * @copydoc ktxTexture2.ktxTexture2_IterateLevels
  524. */
  525. #define ktxTexture_IterateLevels(This, iterCb, userdata) \
  526. (This)->vtbl->IterateLevels(This, iterCb, userdata)
  527. /**
  528. * @~English
  529. * @brief Helper for calling the IterateLoadLevelFaces virtual method of a
  530. * ktxTexture.
  531. * @copydoc ktxTexture2.ktxTexture2_IterateLoadLevelFaces
  532. */
  533. #define ktxTexture_IterateLoadLevelFaces(This, iterCb, userdata) \
  534. (This)->vtbl->IterateLoadLevelFaces(This, iterCb, userdata)
  535. /**
  536. * @~English
  537. * @brief Helper for calling the LoadImageData virtual method of a ktxTexture.
  538. * @copydoc ktxTexture2.ktxTexture2_LoadImageData
  539. */
  540. #define ktxTexture_LoadImageData(This, pBuffer, bufSize) \
  541. (This)->vtbl->LoadImageData(This, pBuffer, bufSize)
  542. /**
  543. * @~English
  544. * @brief Helper for calling the NeedsTranscoding virtual method of a ktxTexture.
  545. * @copydoc ktxTexture2.ktxTexture2_NeedsTranscoding
  546. */
  547. #define ktxTexture_NeedsTranscoding(This) (This)->vtbl->NeedsTranscoding(This)
  548. /**
  549. * @~English
  550. * @brief Helper for calling the SetImageFromMemory virtual method of a
  551. * ktxTexture.
  552. * @copydoc ktxTexture2.ktxTexture2_SetImageFromMemory
  553. */
  554. #define ktxTexture_SetImageFromMemory(This, level, layer, faceSlice, \
  555. src, srcSize) \
  556. (This)->vtbl->SetImageFromMemory(This, level, layer, faceSlice, src, srcSize)
  557. /**
  558. * @~English
  559. * @brief Helper for calling the SetImageFromStdioStream virtual method of a
  560. * ktxTexture.
  561. * @copydoc ktxTexture2.ktxTexture2_SetImageFromStdioStream
  562. */
  563. #define ktxTexture_SetImageFromStdioStream(This, level, layer, faceSlice, \
  564. src, srcSize) \
  565. (This)->vtbl->SetImageFromStdioStream(This, level, layer, faceSlice, \
  566. src, srcSize)
  567. /**
  568. * @~English
  569. * @brief Helper for calling the WriteToStdioStream virtual method of a
  570. * ktxTexture.
  571. * @copydoc ktxTexture2.ktxTexture2_WriteToStdioStream
  572. */
  573. #define ktxTexture_WriteToStdioStream(This, dstsstr) \
  574. (This)->vtbl->WriteToStdioStream(This, dstsstr)
  575. /**
  576. * @~English
  577. * @brief Helper for calling the WriteToNamedfile virtual method of a
  578. * ktxTexture.
  579. * @copydoc ktxTexture2.ktxTexture2_WriteToNamedFile
  580. */
  581. #define ktxTexture_WriteToNamedFile(This, dstname) \
  582. (This)->vtbl->WriteToNamedFile(This, dstname)
  583. /**
  584. * @~English
  585. * @brief Helper for calling the WriteToMemory virtual method of a ktxTexture.
  586. * @copydoc ktxTexture2.ktxTexture2_WriteToMemory
  587. */
  588. #define ktxTexture_WriteToMemory(This, ppDstBytes, pSize) \
  589. (This)->vtbl->WriteToMemory(This, ppDstBytes, pSize)
  590. /**
  591. * @~English
  592. * @brief Helper for calling the WriteToStream virtual method of a ktxTexture.
  593. * @copydoc ktxTexture2.ktxTexture2_WriteToStream
  594. */
  595. #define ktxTexture_WriteToStream(This, dststr) \
  596. (This)->vtbl->WriteToStream(This, dststr)
  597. /**
  598. * @class ktxTexture1
  599. * @~English
  600. * @brief Class representing a KTX version 1 format texture.
  601. *
  602. * ktxTextures should be created only by one of the ktxTexture_Create*
  603. * functions and these fields should be considered read-only.
  604. */
  605. typedef struct ktxTexture1 {
  606. KTXTEXTURECLASSDEFN
  607. ktx_uint32_t glFormat; /*!< Format of the texture data, e.g., GL_RGB. */
  608. ktx_uint32_t glInternalformat; /*!< Internal format of the texture data,
  609. e.g., GL_RGB8. */
  610. ktx_uint32_t glBaseInternalformat; /*!< Base format of the texture data,
  611. e.g., GL_RGB. */
  612. ktx_uint32_t glType; /*!< Type of the texture data, e.g, GL_UNSIGNED_BYTE.*/
  613. struct ktxTexture1_private* _private; /*!< Private data. */
  614. } ktxTexture1;
  615. /*===========================================================*
  616. * KTX format version 2 *
  617. *===========================================================*/
  618. /**
  619. * @~English
  620. * @brief Enumerators identifying the supercompression scheme.
  621. */
  622. typedef enum ktxSupercmpScheme {
  623. KTX_SS_NONE = 0, /*!< No supercompression. */
  624. KTX_SS_BASIS_LZ = 1, /*!< Basis LZ supercompression. */
  625. KTX_SS_ZSTD = 2, /*!< ZStd supercompression. */
  626. KTX_SS_ZLIB = 3, /*!< ZLIB supercompression. */
  627. KTX_SS_BEGIN_RANGE = KTX_SS_NONE,
  628. KTX_SS_END_RANGE = KTX_SS_ZLIB,
  629. KTX_SS_BEGIN_VENDOR_RANGE = 0x10000,
  630. KTX_SS_END_VENDOR_RANGE = 0x1ffff,
  631. KTX_SS_BEGIN_RESERVED = 0x20000,
  632. KTX_SUPERCOMPRESSION_BASIS = KTX_SS_BASIS_LZ,
  633. /*!< @deprecated Will be removed before v4 release. Use KTX_SS_BASIS_LZ instead. */
  634. KTX_SUPERCOMPRESSION_ZSTD = KTX_SS_ZSTD
  635. /*!< @deprecated Will be removed before v4 release. Use KTX_SS_ZSTD instead. */
  636. } ktxSupercmpScheme;
  637. /**
  638. * @class ktxTexture2
  639. * @~English
  640. * @brief Class representing a KTX version 2 format texture.
  641. *
  642. * ktxTextures should be created only by one of the ktxTexture_Create*
  643. * functions and these fields should be considered read-only.
  644. */
  645. typedef struct ktxTexture2 {
  646. KTXTEXTURECLASSDEFN
  647. ktx_uint32_t vkFormat;
  648. ktx_uint32_t* pDfd;
  649. ktxSupercmpScheme supercompressionScheme;
  650. ktx_bool_t isVideo;
  651. ktx_uint32_t duration;
  652. ktx_uint32_t timescale;
  653. ktx_uint32_t loopcount;
  654. struct ktxTexture2_private* _private; /*!< Private data. */
  655. } ktxTexture2;
  656. /**
  657. * @brief Helper for casting ktxTexture1 and ktxTexture2 to ktxTexture.
  658. *
  659. * Use with caution.
  660. */
  661. #define ktxTexture(t) ((ktxTexture*)t)
  662. /**
  663. * @memberof ktxTexture
  664. * @~English
  665. * @brief Structure for passing texture information to ktxTexture1\_Create() and
  666. * ktxTexture2\_Create().
  667. *
  668. * @sa @ref ktxTexture1::ktxTexture1\_Create() "ktxTexture1_Create()"
  669. * @sa @ref ktxTexture2::ktxTexture2\_Create() "ktxTexture2_Create()"
  670. */
  671. typedef struct
  672. {
  673. ktx_uint32_t glInternalformat; /*!< Internal format for the texture, e.g.,
  674. GL_RGB8. Ignored when creating a
  675. ktxTexture2. */
  676. ktx_uint32_t vkFormat; /*!< VkFormat for texture. Ignored when creating a
  677. ktxTexture1. */
  678. ktx_uint32_t* pDfd; /*!< Pointer to DFD. Used only when creating a
  679. ktxTexture2 and only if vkFormat is
  680. VK_FORMAT_UNDEFINED. */
  681. ktx_uint32_t baseWidth; /*!< Width of the base level of the texture. */
  682. ktx_uint32_t baseHeight; /*!< Height of the base level of the texture. */
  683. ktx_uint32_t baseDepth; /*!< Depth of the base level of the texture. */
  684. ktx_uint32_t numDimensions; /*!< Number of dimensions in the texture, 1, 2
  685. or 3. */
  686. ktx_uint32_t numLevels; /*!< Number of mip levels in the texture. Should be
  687. 1 if @c generateMipmaps is KTX_TRUE; */
  688. ktx_uint32_t numLayers; /*!< Number of array layers in the texture. */
  689. ktx_uint32_t numFaces; /*!< Number of faces: 6 for cube maps, 1 otherwise. */
  690. ktx_bool_t isArray; /*!< Set to KTX_TRUE if the texture is to be an
  691. array texture. Means OpenGL will use a
  692. GL_TEXTURE_*_ARRAY target. */
  693. ktx_bool_t generateMipmaps; /*!< Set to KTX_TRUE if mipmaps should be
  694. generated for the texture when loading
  695. into a 3D API. */
  696. } ktxTextureCreateInfo;
  697. /**
  698. * @memberof ktxTexture
  699. * @~English
  700. * @brief Enum for requesting, or not, allocation of storage for images.
  701. *
  702. * @sa ktxTexture1_Create() and ktxTexture2_Create().
  703. */
  704. typedef enum {
  705. KTX_TEXTURE_CREATE_NO_STORAGE = 0, /*!< Don't allocate any image storage. */
  706. KTX_TEXTURE_CREATE_ALLOC_STORAGE = 1 /*!< Allocate image storage. */
  707. } ktxTextureCreateStorageEnum;
  708. /**
  709. * @memberof ktxTexture
  710. * @~English
  711. * @brief Flags for requesting services during creation.
  712. *
  713. * @sa ktxTexture_CreateFrom*
  714. */
  715. enum ktxTextureCreateFlagBits {
  716. KTX_TEXTURE_CREATE_NO_FLAGS = 0x00,
  717. KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT = 0x01,
  718. /*!< Load the images from the KTX source. */
  719. KTX_TEXTURE_CREATE_RAW_KVDATA_BIT = 0x02,
  720. /*!< Load the raw key-value data instead of
  721. creating a @c ktxHashList from it. */
  722. KTX_TEXTURE_CREATE_SKIP_KVDATA_BIT = 0x04,
  723. /*!< Skip any key-value data. This overrides
  724. the RAW_KVDATA_BIT. */
  725. KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT = 0x08
  726. /*!< Load texture compatible with the rules
  727. of KHR_texture_basisu glTF extension */
  728. };
  729. /**
  730. * @memberof ktxTexture
  731. * @~English
  732. * @brief Type for TextureCreateFlags parameters.
  733. *
  734. * @sa ktxTexture_CreateFrom*()
  735. */
  736. typedef ktx_uint32_t ktxTextureCreateFlags;
  737. /*===========================================================*
  738. * ktxStream
  739. *===========================================================*/
  740. /*
  741. * This is unsigned to allow ktxmemstreams to use the
  742. * full amount of memory available. Platforms will
  743. * limit the size of ktxfilestreams to, e.g, MAX_LONG
  744. * on 32-bit and ktxfilestreams raises errors if
  745. * offset values exceed the limits. This choice may
  746. * need to be revisited if we ever start needing -ve
  747. * offsets.
  748. *
  749. * Should the 2GB file size handling limit on 32-bit
  750. * platforms become a problem, ktxfilestream will have
  751. * to be changed to explicitly handle large files by
  752. * using the 64-bit stream functions.
  753. */
  754. #if defined(_MSC_VER) && defined(_WIN64)
  755. typedef unsigned __int64 ktx_off_t;
  756. #else
  757. typedef off_t ktx_off_t;
  758. #endif
  759. typedef struct ktxMem ktxMem;
  760. typedef struct ktxStream ktxStream;
  761. enum streamType { eStreamTypeFile = 1, eStreamTypeMemory = 2, eStreamTypeCustom = 3 };
  762. /**
  763. * @~English
  764. * @brief type for a pointer to a stream reading function
  765. */
  766. typedef KTX_error_code (*ktxStream_read)(ktxStream* str, void* dst,
  767. const ktx_size_t count);
  768. /**
  769. * @~English
  770. * @brief type for a pointer to a stream skipping function
  771. */
  772. typedef KTX_error_code (*ktxStream_skip)(ktxStream* str,
  773. const ktx_size_t count);
  774. /**
  775. * @~English
  776. * @brief type for a pointer to a stream writing function
  777. */
  778. typedef KTX_error_code (*ktxStream_write)(ktxStream* str, const void *src,
  779. const ktx_size_t size,
  780. const ktx_size_t count);
  781. /**
  782. * @~English
  783. * @brief type for a pointer to a stream position query function
  784. */
  785. typedef KTX_error_code (*ktxStream_getpos)(ktxStream* str, ktx_off_t* const offset);
  786. /**
  787. * @~English
  788. * @brief type for a pointer to a stream position query function
  789. */
  790. typedef KTX_error_code (*ktxStream_setpos)(ktxStream* str, const ktx_off_t offset);
  791. /**
  792. * @~English
  793. * @brief type for a pointer to a stream size query function
  794. */
  795. typedef KTX_error_code (*ktxStream_getsize)(ktxStream* str, ktx_size_t* const size);
  796. /**
  797. * @~English
  798. * @brief Destruct a stream
  799. */
  800. typedef void (*ktxStream_destruct)(ktxStream* str);
  801. /**
  802. * @~English
  803. *
  804. * @brief Interface of ktxStream.
  805. *
  806. * @author Maksim Kolesin
  807. * @author Georg Kolling, Imagination Technology
  808. * @author Mark Callow, HI Corporation
  809. */
  810. struct ktxStream
  811. {
  812. ktxStream_read read; /*!< pointer to function for reading bytes. */
  813. ktxStream_skip skip; /*!< pointer to function for skipping bytes. */
  814. ktxStream_write write; /*!< pointer to function for writing bytes. */
  815. ktxStream_getpos getpos; /*!< pointer to function for getting current position in stream. */
  816. ktxStream_setpos setpos; /*!< pointer to function for setting current position in stream. */
  817. ktxStream_getsize getsize; /*!< pointer to function for querying size. */
  818. ktxStream_destruct destruct; /*!< destruct the stream. */
  819. enum streamType type;
  820. union {
  821. FILE* file; /**< a stdio FILE pointer for a ktxFileStream. */
  822. ktxMem* mem; /**< a pointer to a ktxMem struct for a ktxMemStream. */
  823. struct
  824. {
  825. void* address; /**< pointer to the data. */
  826. void* allocatorAddress; /**< pointer to a memory allocator. */
  827. ktx_size_t size; /**< size of the data. */
  828. } custom_ptr; /**< pointer to a struct for custom streams. */
  829. } data; /**< pointer to the stream data. */
  830. ktx_off_t readpos; /**< used by FileStream for stdin. */
  831. ktx_bool_t closeOnDestruct; /**< Close FILE* or dispose of memory on destruct. */
  832. };
  833. /*
  834. * See the implementation files for the full documentation of the following
  835. * functions.
  836. */
  837. /*
  838. * These four create a ktxTexture1 or ktxTexture2 according to the data
  839. * header, and return a pointer to the base ktxTexture class.
  840. */
  841. KTX_API KTX_error_code KTX_APIENTRY
  842. ktxTexture_CreateFromStdioStream(FILE* stdioStream,
  843. ktxTextureCreateFlags createFlags,
  844. ktxTexture** newTex);
  845. KTX_API KTX_error_code KTX_APIENTRY
  846. ktxTexture_CreateFromNamedFile(const char* const filename,
  847. ktxTextureCreateFlags createFlags,
  848. ktxTexture** newTex);
  849. KTX_API KTX_error_code KTX_APIENTRY
  850. ktxTexture_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
  851. ktxTextureCreateFlags createFlags,
  852. ktxTexture** newTex);
  853. KTX_API KTX_error_code KTX_APIENTRY
  854. ktxTexture_CreateFromStream(ktxStream* stream,
  855. ktxTextureCreateFlags createFlags,
  856. ktxTexture** newTex);
  857. /*
  858. * Returns a pointer to the image data of a ktxTexture object.
  859. */
  860. KTX_API ktx_uint8_t* KTX_APIENTRY
  861. ktxTexture_GetData(ktxTexture* This);
  862. /*
  863. * Returns the pitch of a row of an image at the specified level.
  864. * Similar to the rowPitch in a VkSubResourceLayout.
  865. */
  866. KTX_API ktx_uint32_t KTX_APIENTRY
  867. ktxTexture_GetRowPitch(ktxTexture* This, ktx_uint32_t level);
  868. /*
  869. * Return the element size of the texture's images.
  870. */
  871. KTX_API ktx_uint32_t KTX_APIENTRY
  872. ktxTexture_GetElementSize(ktxTexture* This);
  873. /*
  874. * Returns the size of all the image data of a ktxTexture object in bytes.
  875. */
  876. KTX_API ktx_size_t KTX_APIENTRY
  877. ktxTexture_GetDataSize(ktxTexture* This);
  878. /* Uploads a texture to OpenGL {,ES}. */
  879. KTX_API KTX_error_code KTX_APIENTRY
  880. ktxTexture_GLUpload(ktxTexture* This, GLuint* pTexture, GLenum* pTarget,
  881. GLenum* pGlerror);
  882. /*
  883. * Iterate over the levels or faces in a ktxTexture object.
  884. */
  885. KTX_API KTX_error_code KTX_APIENTRY
  886. ktxTexture_IterateLevelFaces(ktxTexture* This, PFNKTXITERCB iterCb,
  887. void* userdata);
  888. /*
  889. * Create a new ktxTexture1.
  890. */
  891. KTX_API KTX_error_code KTX_APIENTRY
  892. ktxTexture1_Create(ktxTextureCreateInfo* createInfo,
  893. ktxTextureCreateStorageEnum storageAllocation,
  894. ktxTexture1** newTex);
  895. /*
  896. * These four create a ktxTexture1 provided the data is in KTX format.
  897. */
  898. KTX_API KTX_error_code KTX_APIENTRY
  899. ktxTexture1_CreateFromStdioStream(FILE* stdioStream,
  900. ktxTextureCreateFlags createFlags,
  901. ktxTexture1** newTex);
  902. KTX_API KTX_error_code KTX_APIENTRY
  903. ktxTexture1_CreateFromNamedFile(const char* const filename,
  904. ktxTextureCreateFlags createFlags,
  905. ktxTexture1** newTex);
  906. KTX_API KTX_error_code KTX_APIENTRY
  907. ktxTexture1_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
  908. ktxTextureCreateFlags createFlags,
  909. ktxTexture1** newTex);
  910. KTX_API KTX_error_code KTX_APIENTRY
  911. ktxTexture1_CreateFromStream(ktxStream* stream,
  912. ktxTextureCreateFlags createFlags,
  913. ktxTexture1** newTex);
  914. KTX_API ktx_bool_t KTX_APIENTRY
  915. ktxTexture1_NeedsTranscoding(ktxTexture1* This);
  916. /*
  917. * Write a ktxTexture object to a stdio stream in KTX format.
  918. */
  919. KTX_API KTX_error_code KTX_APIENTRY
  920. ktxTexture1_WriteKTX2ToStdioStream(ktxTexture1* This, FILE* dstsstr);
  921. /*
  922. * Write a ktxTexture object to a named file in KTX format.
  923. */
  924. KTX_API KTX_error_code KTX_APIENTRY
  925. ktxTexture1_WriteKTX2ToNamedFile(ktxTexture1* This, const char* const dstname);
  926. /*
  927. * Write a ktxTexture object to a block of memory in KTX format.
  928. */
  929. KTX_API KTX_error_code KTX_APIENTRY
  930. ktxTexture1_WriteKTX2ToMemory(ktxTexture1* This,
  931. ktx_uint8_t** bytes, ktx_size_t* size);
  932. /*
  933. * Write a ktxTexture object to a ktxStream in KTX format.
  934. */
  935. KTX_API KTX_error_code KTX_APIENTRY
  936. ktxTexture1_WriteKTX2ToStream(ktxTexture1* This, ktxStream *dststr);
  937. /*
  938. * Create a new ktxTexture2.
  939. */
  940. KTX_API KTX_error_code KTX_APIENTRY
  941. ktxTexture2_Create(ktxTextureCreateInfo* createInfo,
  942. ktxTextureCreateStorageEnum storageAllocation,
  943. ktxTexture2** newTex);
  944. /*
  945. * Create a new ktxTexture2 as a copy of an existing texture.
  946. */
  947. KTX_API KTX_error_code KTX_APIENTRY
  948. ktxTexture2_CreateCopy(ktxTexture2* orig, ktxTexture2** newTex);
  949. /*
  950. * These four create a ktxTexture2 provided the data is in KTX2 format.
  951. */
  952. KTX_API KTX_error_code KTX_APIENTRY
  953. ktxTexture2_CreateFromStdioStream(FILE* stdioStream,
  954. ktxTextureCreateFlags createFlags,
  955. ktxTexture2** newTex);
  956. KTX_API KTX_error_code KTX_APIENTRY
  957. ktxTexture2_CreateFromNamedFile(const char* const filename,
  958. ktxTextureCreateFlags createFlags,
  959. ktxTexture2** newTex);
  960. KTX_API KTX_error_code KTX_APIENTRY
  961. ktxTexture2_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
  962. ktxTextureCreateFlags createFlags,
  963. ktxTexture2** newTex);
  964. KTX_API KTX_error_code KTX_APIENTRY
  965. ktxTexture2_CreateFromStream(ktxStream* stream,
  966. ktxTextureCreateFlags createFlags,
  967. ktxTexture2** newTex);
  968. KTX_API KTX_error_code KTX_APIENTRY
  969. ktxTexture2_CompressBasis(ktxTexture2* This, ktx_uint32_t quality);
  970. KTX_API KTX_error_code KTX_APIENTRY
  971. ktxTexture2_DeflateZstd(ktxTexture2* This, ktx_uint32_t level);
  972. KTX_API KTX_error_code KTX_APIENTRY
  973. ktxTexture2_DeflateZLIB(ktxTexture2* This, ktx_uint32_t level);
  974. KTX_API void KTX_APIENTRY
  975. ktxTexture2_GetComponentInfo(ktxTexture2* This, ktx_uint32_t* numComponents,
  976. ktx_uint32_t* componentByteLength);
  977. KTX_API ktx_uint32_t KTX_APIENTRY
  978. ktxTexture2_GetNumComponents(ktxTexture2* This);
  979. KTX_API khr_df_transfer_e KTX_APIENTRY
  980. ktxTexture2_GetOETF_e(ktxTexture2* This);
  981. // For backward compatibility
  982. KTX_API ktx_uint32_t KTX_APIENTRY
  983. ktxTexture2_GetOETF(ktxTexture2* This);
  984. KTX_API khr_df_model_e KTX_APIENTRY
  985. ktxTexture2_GetColorModel_e(ktxTexture2* This);
  986. KTX_API ktx_bool_t KTX_APIENTRY
  987. ktxTexture2_GetPremultipliedAlpha(ktxTexture2* This);
  988. KTX_API ktx_bool_t KTX_APIENTRY
  989. ktxTexture2_NeedsTranscoding(ktxTexture2* This);
  990. /**
  991. * @~English
  992. * @brief Flags specifiying UASTC encoding options.
  993. */
  994. typedef enum ktx_pack_uastc_flag_bits_e {
  995. KTX_PACK_UASTC_LEVEL_FASTEST = 0,
  996. /*!< Fastest compression. 43.45dB. */
  997. KTX_PACK_UASTC_LEVEL_FASTER = 1,
  998. /*!< Faster compression. 46.49dB. */
  999. KTX_PACK_UASTC_LEVEL_DEFAULT = 2,
  1000. /*!< Default compression. 47.47dB. */
  1001. KTX_PACK_UASTC_LEVEL_SLOWER = 3,
  1002. /*!< Slower compression. 48.01dB. */
  1003. KTX_PACK_UASTC_LEVEL_VERYSLOW = 4,
  1004. /*!< Very slow compression. 48.24dB. */
  1005. KTX_PACK_UASTC_MAX_LEVEL = KTX_PACK_UASTC_LEVEL_VERYSLOW,
  1006. /*!< Maximum supported quality level. */
  1007. KTX_PACK_UASTC_LEVEL_MASK = 0xF,
  1008. /*!< Mask to extract the level from the other bits. */
  1009. KTX_PACK_UASTC_FAVOR_UASTC_ERROR = 8,
  1010. /*!< Optimize for lowest UASTC error. */
  1011. KTX_PACK_UASTC_FAVOR_BC7_ERROR = 16,
  1012. /*!< Optimize for lowest BC7 error. */
  1013. KTX_PACK_UASTC_ETC1_FASTER_HINTS = 64,
  1014. /*!< Optimize for faster transcoding to ETC1. */
  1015. KTX_PACK_UASTC_ETC1_FASTEST_HINTS = 128,
  1016. /*!< Optimize for fastest transcoding to ETC1. */
  1017. KTX_PACK_UASTC__ETC1_DISABLE_FLIP_AND_INDIVIDUAL = 256
  1018. /*!< Not documented in BasisU code. */
  1019. } ktx_pack_uastc_flag_bits_e;
  1020. typedef ktx_uint32_t ktx_pack_uastc_flags;
  1021. /**
  1022. * @~English
  1023. * @brief Options specifiying ASTC encoding quality levels.
  1024. */
  1025. typedef enum ktx_pack_astc_quality_levels_e {
  1026. KTX_PACK_ASTC_QUALITY_LEVEL_FASTEST = 0,
  1027. /*!< Fastest compression. */
  1028. KTX_PACK_ASTC_QUALITY_LEVEL_FAST = 10,
  1029. /*!< Fast compression. */
  1030. KTX_PACK_ASTC_QUALITY_LEVEL_MEDIUM = 60,
  1031. /*!< Medium compression. */
  1032. KTX_PACK_ASTC_QUALITY_LEVEL_THOROUGH = 98,
  1033. /*!< Slower compression. */
  1034. KTX_PACK_ASTC_QUALITY_LEVEL_EXHAUSTIVE = 100,
  1035. /*!< Very slow compression. */
  1036. KTX_PACK_ASTC_QUALITY_LEVEL_MAX = KTX_PACK_ASTC_QUALITY_LEVEL_EXHAUSTIVE,
  1037. /*!< Maximum supported quality level. */
  1038. } ktx_pack_astc_quality_levels_e;
  1039. /**
  1040. * @~English
  1041. * @brief Options specifiying ASTC encoding block dimensions
  1042. */
  1043. typedef enum ktx_pack_astc_block_dimension_e {
  1044. // 2D formats
  1045. KTX_PACK_ASTC_BLOCK_DIMENSION_4x4, //: 8.00 bpp
  1046. KTX_PACK_ASTC_BLOCK_DIMENSION_5x4, //: 6.40 bpp
  1047. KTX_PACK_ASTC_BLOCK_DIMENSION_5x5, //: 5.12 bpp
  1048. KTX_PACK_ASTC_BLOCK_DIMENSION_6x5, //: 4.27 bpp
  1049. KTX_PACK_ASTC_BLOCK_DIMENSION_6x6, //: 3.56 bpp
  1050. KTX_PACK_ASTC_BLOCK_DIMENSION_8x5, //: 3.20 bpp
  1051. KTX_PACK_ASTC_BLOCK_DIMENSION_8x6, //: 2.67 bpp
  1052. KTX_PACK_ASTC_BLOCK_DIMENSION_10x5, //: 2.56 bpp
  1053. KTX_PACK_ASTC_BLOCK_DIMENSION_10x6, //: 2.13 bpp
  1054. KTX_PACK_ASTC_BLOCK_DIMENSION_8x8, //: 2.00 bpp
  1055. KTX_PACK_ASTC_BLOCK_DIMENSION_10x8, //: 1.60 bpp
  1056. KTX_PACK_ASTC_BLOCK_DIMENSION_10x10, //: 1.28 bpp
  1057. KTX_PACK_ASTC_BLOCK_DIMENSION_12x10, //: 1.07 bpp
  1058. KTX_PACK_ASTC_BLOCK_DIMENSION_12x12, //: 0.89 bpp
  1059. // 3D formats
  1060. KTX_PACK_ASTC_BLOCK_DIMENSION_3x3x3, //: 4.74 bpp
  1061. KTX_PACK_ASTC_BLOCK_DIMENSION_4x3x3, //: 3.56 bpp
  1062. KTX_PACK_ASTC_BLOCK_DIMENSION_4x4x3, //: 2.67 bpp
  1063. KTX_PACK_ASTC_BLOCK_DIMENSION_4x4x4, //: 2.00 bpp
  1064. KTX_PACK_ASTC_BLOCK_DIMENSION_5x4x4, //: 1.60 bpp
  1065. KTX_PACK_ASTC_BLOCK_DIMENSION_5x5x4, //: 1.28 bpp
  1066. KTX_PACK_ASTC_BLOCK_DIMENSION_5x5x5, //: 1.02 bpp
  1067. KTX_PACK_ASTC_BLOCK_DIMENSION_6x5x5, //: 0.85 bpp
  1068. KTX_PACK_ASTC_BLOCK_DIMENSION_6x6x5, //: 0.71 bpp
  1069. KTX_PACK_ASTC_BLOCK_DIMENSION_6x6x6, //: 0.59 bpp
  1070. KTX_PACK_ASTC_BLOCK_DIMENSION_MAX = KTX_PACK_ASTC_BLOCK_DIMENSION_6x6x6
  1071. /*!< Maximum supported blocks. */
  1072. } ktx_pack_astc_block_dimension_e;
  1073. /**
  1074. * @~English
  1075. * @brief Options specifying ASTC encoder profile mode
  1076. * This and function is used later to derive the profile.
  1077. */
  1078. typedef enum ktx_pack_astc_encoder_mode_e {
  1079. KTX_PACK_ASTC_ENCODER_MODE_DEFAULT,
  1080. KTX_PACK_ASTC_ENCODER_MODE_LDR,
  1081. KTX_PACK_ASTC_ENCODER_MODE_HDR,
  1082. KTX_PACK_ASTC_ENCODER_MODE_MAX = KTX_PACK_ASTC_ENCODER_MODE_HDR
  1083. } ktx_pack_astc_encoder_mode_e;
  1084. extern KTX_API const ktx_uint32_t KTX_ETC1S_DEFAULT_COMPRESSION_LEVEL;
  1085. /**
  1086. * @memberof ktxTexture
  1087. * @~English
  1088. * @brief Structure for passing extended parameters to
  1089. * ktxTexture_CompressAstc.
  1090. *
  1091. * Passing a struct initialized to 0 (e.g. " = {0};") will use blockDimension
  1092. * 4x4, mode LDR and qualityLevel FASTEST. Setting qualityLevel to
  1093. * KTX_PACK_ASTC_QUALITY_LEVEL_MEDIUM is recommended.
  1094. */
  1095. typedef struct ktxAstcParams {
  1096. ktx_uint32_t structSize;
  1097. /*!< Size of this struct. Used so library can tell which version
  1098. of struct is being passed.
  1099. */
  1100. ktx_bool_t verbose;
  1101. /*!< If true, prints Astc encoder operation details to
  1102. @c stdout. Not recommended for GUI apps.
  1103. */
  1104. ktx_uint32_t threadCount;
  1105. /*!< Number of threads used for compression. Default is 1.
  1106. */
  1107. /* astcenc params */
  1108. ktx_uint32_t blockDimension;
  1109. /*!< Combinations of block dimensions that astcenc supports
  1110. i.e. 6x6, 8x8, 6x5 etc
  1111. */
  1112. ktx_uint32_t mode;
  1113. /*!< Can be {ldr/hdr} from astcenc
  1114. */
  1115. ktx_uint32_t qualityLevel;
  1116. /*!< astcenc supports -fastest, -fast, -medium, -thorough, -exhaustive
  1117. */
  1118. ktx_bool_t normalMap;
  1119. /*!< Tunes codec parameters for better quality on normal maps
  1120. In this mode normals are compressed to X,Y components
  1121. Discarding Z component, reader will need to generate Z
  1122. component in shaders.
  1123. */
  1124. ktx_bool_t perceptual;
  1125. /*!< The codec should optimize for perceptual error, instead of direct
  1126. RMS error. This aims to improves perceived image quality, but
  1127. typically lowers the measured PSNR score. Perceptual methods are
  1128. currently only available for normal maps and RGB color data.
  1129. */
  1130. char inputSwizzle[4];
  1131. /*!< A swizzle to provide as input to astcenc. It must match the regular
  1132. expression /^[rgba01]{4}$/.
  1133. */
  1134. } ktxAstcParams;
  1135. KTX_API KTX_error_code KTX_APIENTRY
  1136. ktxTexture2_CompressAstcEx(ktxTexture2* This, ktxAstcParams* params);
  1137. KTX_API KTX_error_code KTX_APIENTRY
  1138. ktxTexture2_CompressAstc(ktxTexture2* This, ktx_uint32_t quality);
  1139. /**
  1140. * @memberof ktxTexture2
  1141. * @~English
  1142. * @brief Structure for passing extended parameters to
  1143. * ktxTexture2_CompressBasisEx().
  1144. *
  1145. * If you only want default values, use ktxTexture2_CompressBasis(). Here, at a minimum you
  1146. * must initialize the structure as follows:
  1147. * @code
  1148. * ktxBasisParams params = {0};
  1149. * params.structSize = sizeof(params);
  1150. * params.compressionLevel = KTX_ETC1S_DEFAULT_COMPRESSION_LEVEL;
  1151. * @endcode
  1152. *
  1153. * @e compressionLevel has to be explicitly set because 0 is a valid @e compressionLevel
  1154. * but is not the default used by the BasisU encoder when no value is set. Only the other
  1155. * settings that are to be non-default must be non-zero.
  1156. */
  1157. typedef struct ktxBasisParams {
  1158. ktx_uint32_t structSize;
  1159. /*!< Size of this struct. Used so library can tell which version
  1160. of struct is being passed.
  1161. */
  1162. ktx_bool_t uastc;
  1163. /*!< True to use UASTC base, false to use ETC1S base. */
  1164. ktx_bool_t verbose;
  1165. /*!< If true, prints Basis Universal encoder operation details to
  1166. @c stdout. Not recommended for GUI apps.
  1167. */
  1168. ktx_bool_t noSSE;
  1169. /*!< True to forbid use of the SSE instruction set. Ignored if CPU
  1170. does not support SSE. */
  1171. ktx_uint32_t threadCount;
  1172. /*!< Number of threads used for compression. Default is 1. */
  1173. /* ETC1S params */
  1174. ktx_uint32_t compressionLevel;
  1175. /*!< Encoding speed vs. quality tradeoff. Range is [0,5]. Higher values
  1176. are slower, but give higher quality. There is no default. Callers
  1177. must explicitly set this value. Callers can use
  1178. KTX_ETC1S_DEFAULT_COMPRESSION_LEVEL as a default value.
  1179. Currently this is 2.
  1180. */
  1181. ktx_uint32_t qualityLevel;
  1182. /*!< Compression quality. Range is [1,255]. Lower gives better
  1183. compression/lower quality/faster. Higher gives less compression
  1184. /higher quality/slower. This automatically determines values for
  1185. @c maxEndpoints, @c maxSelectors,
  1186. @c endpointRDOThreshold and @c selectorRDOThreshold
  1187. for the target quality level. Setting these parameters overrides
  1188. the values determined by @c qualityLevel which defaults to
  1189. 128 if neither it nor both of @c maxEndpoints and
  1190. @c maxSelectors have been set.
  1191. @note @e Both of @c maxEndpoints and @c maxSelectors
  1192. must be set for them to have any effect.
  1193. @note qualityLevel will only determine values for
  1194. @c endpointRDOThreshold and @c selectorRDOThreshold
  1195. when its value exceeds 128, otherwise their defaults will be used.
  1196. */
  1197. ktx_uint32_t maxEndpoints;
  1198. /*!< Manually set the max number of color endpoint clusters.
  1199. Range is [1,16128]. Default is 0, unset. If this is set, maxSelectors
  1200. must also be set, otherwise the value will be ignored.
  1201. */
  1202. float endpointRDOThreshold;
  1203. /*!< Set endpoint RDO quality threshold. The default is 1.25. Lower is
  1204. higher quality but less quality per output bit (try [1.0,3.0].
  1205. This will override the value chosen by @c qualityLevel.
  1206. */
  1207. ktx_uint32_t maxSelectors;
  1208. /*!< Manually set the max number of color selector clusters. Range
  1209. is [1,16128]. Default is 0, unset. If this is set, maxEndpoints
  1210. must also be set, otherwise the value will be ignored.
  1211. */
  1212. float selectorRDOThreshold;
  1213. /*!< Set selector RDO quality threshold. The default is 1.5. Lower is
  1214. higher quality but less quality per output bit (try [1.0,3.0]).
  1215. This will override the value chosen by @c qualityLevel.
  1216. */
  1217. char inputSwizzle[4];
  1218. /*!< A swizzle to apply before encoding. It must match the regular
  1219. expression /^[rgba01]{4}$/. If both this and preSwizzle
  1220. are specified ktxTexture_CompressBasisEx will raise
  1221. KTX_INVALID_OPERATION.
  1222. */
  1223. ktx_bool_t normalMap;
  1224. /*!< Tunes codec parameters for better quality on normal maps (no
  1225. selector RDO, no endpoint RDO) and sets the texture's DFD appropriately.
  1226. Only valid for linear textures.
  1227. */
  1228. ktx_bool_t separateRGToRGB_A;
  1229. /*!< @deprecated. This was and is a no-op. 2-component inputs have always been
  1230. automatically separated using an "rrrg" inputSwizzle. @sa inputSwizzle and normalMode.
  1231. */
  1232. ktx_bool_t preSwizzle;
  1233. /*!< If the texture has @c KTXswizzle metadata, apply it before
  1234. compressing. Swizzling, like @c rabb may yield drastically
  1235. different error metrics if done after supercompression.
  1236. */
  1237. ktx_bool_t noEndpointRDO;
  1238. /*!< Disable endpoint rate distortion optimizations. Slightly faster,
  1239. less noisy output, but lower quality per output bit. Default is
  1240. KTX_FALSE.
  1241. */
  1242. ktx_bool_t noSelectorRDO;
  1243. /*!< Disable selector rate distortion optimizations. Slightly faster,
  1244. less noisy output, but lower quality per output bit. Default is
  1245. KTX_FALSE.
  1246. */
  1247. /* UASTC params */
  1248. ktx_pack_uastc_flags uastcFlags;
  1249. /*!< A set of ::ktx_pack_uastc_flag_bits_e controlling UASTC
  1250. encoding. The most important value is the level given in the
  1251. least-significant 4 bits which selects a speed vs quality tradeoff
  1252. as shown in the following table:
  1253. Level/Speed | Quality
  1254. :-----: | :-------:
  1255. KTX_PACK_UASTC_LEVEL_FASTEST | 43.45dB
  1256. KTX_PACK_UASTC_LEVEL_FASTER | 46.49dB
  1257. KTX_PACK_UASTC_LEVEL_DEFAULT | 47.47dB
  1258. KTX_PACK_UASTC_LEVEL_SLOWER | 48.01dB
  1259. KTX_PACK_UASTC_LEVEL_VERYSLOW | 48.24dB
  1260. */
  1261. ktx_bool_t uastcRDO;
  1262. /*!< Enable Rate Distortion Optimization (RDO) post-processing.
  1263. */
  1264. float uastcRDOQualityScalar;
  1265. /*!< UASTC RDO quality scalar (lambda). Lower values yield higher
  1266. quality/larger LZ compressed files, higher values yield lower
  1267. quality/smaller LZ compressed files. A good range to try is [.2,4].
  1268. Full range is [.001,50.0]. Default is 1.0.
  1269. */
  1270. ktx_uint32_t uastcRDODictSize;
  1271. /*!< UASTC RDO dictionary size in bytes. Default is 4096. Lower
  1272. values=faster, but give less compression. Range is [64,65536].
  1273. */
  1274. float uastcRDOMaxSmoothBlockErrorScale;
  1275. /*!< UASTC RDO max smooth block error scale. Range is [1,300].
  1276. Default is 10.0, 1.0 is disabled. Larger values suppress more
  1277. artifacts (and allocate more bits) on smooth blocks.
  1278. */
  1279. float uastcRDOMaxSmoothBlockStdDev;
  1280. /*!< UASTC RDO max smooth block standard deviation. Range is
  1281. [.01,65536.0]. Default is 18.0. Larger values expand the range of
  1282. blocks considered smooth.
  1283. */
  1284. ktx_bool_t uastcRDODontFavorSimplerModes;
  1285. /*!< Do not favor simpler UASTC modes in RDO mode.
  1286. */
  1287. ktx_bool_t uastcRDONoMultithreading;
  1288. /*!< Disable RDO multithreading (slightly higher compression,
  1289. deterministic).
  1290. */
  1291. } ktxBasisParams;
  1292. KTX_API KTX_error_code KTX_APIENTRY
  1293. ktxTexture2_CompressBasisEx(ktxTexture2* This, ktxBasisParams* params);
  1294. /**
  1295. * @~English
  1296. * @brief Enumerators for specifying the transcode target format.
  1297. *
  1298. * For BasisU/ETC1S format, @e Opaque and @e alpha here refer to 2 separate
  1299. * RGB images, a.k.a slices within the BasisU compressed data. For UASTC
  1300. * format they refer to the RGB and the alpha components of the UASTC data. If
  1301. * the original image had only 2 components, R will be in the opaque portion
  1302. * and G in the alpha portion. The R value will be replicated in the RGB
  1303. * components. In the case of BasisU the G value will be replicated in all 3
  1304. * components of the alpha slice. If the original image had only 1 component
  1305. * it's value is replicated in all 3 components of the opaque portion and
  1306. * there is no alpha.
  1307. *
  1308. * @note You should not transcode sRGB encoded data to @c KTX_TTF_BC4_R,
  1309. * @c KTX_TTF_BC5_RG, @c KTX_TTF_ETC2_EAC_R{,G}11, @c KTX_TTF_RGB565,
  1310. * @c KTX_TTF_BGR565 or @c KTX_TTF_RGBA4444 formats as neither OpenGL nor
  1311. * Vulkan support sRGB variants of these. Doing sRGB decoding in the shader
  1312. * will not produce correct results if any texture filtering is being used.
  1313. */
  1314. typedef enum ktx_transcode_fmt_e {
  1315. // Compressed formats
  1316. // ETC1-2
  1317. KTX_TTF_ETC1_RGB = 0,
  1318. /*!< Opaque only. Returns RGB or alpha data, if
  1319. KTX_TF_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS flag is
  1320. specified. */
  1321. KTX_TTF_ETC2_RGBA = 1,
  1322. /*!< Opaque+alpha. EAC_A8 block followed by an ETC1 block. The
  1323. alpha channel will be opaque for textures without an alpha
  1324. channel. */
  1325. // BC1-5, BC7 (desktop, some mobile devices)
  1326. KTX_TTF_BC1_RGB = 2,
  1327. /*!< Opaque only, no punchthrough alpha support yet. Returns RGB
  1328. or alpha data, if KTX_TF_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS
  1329. flag is specified. */
  1330. KTX_TTF_BC3_RGBA = 3,
  1331. /*!< Opaque+alpha. BC4 block with alpha followed by a BC1 block. The
  1332. alpha channel will be opaque for textures without an alpha
  1333. channel. */
  1334. KTX_TTF_BC4_R = 4,
  1335. /*!< One BC4 block. R = opaque.g or alpha.g, if
  1336. KTX_TF_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS flag is
  1337. specified. */
  1338. KTX_TTF_BC5_RG = 5,
  1339. /*!< Two BC4 blocks, R=opaque.g and G=alpha.g The texture should
  1340. have an alpha channel (if not G will be all 255's. For tangent
  1341. space normal maps. */
  1342. KTX_TTF_BC7_RGBA = 6,
  1343. /*!< RGB or RGBA mode 5 for ETC1S, modes 1, 2, 3, 4, 5, 6, 7 for
  1344. UASTC. */
  1345. // PVRTC1 4bpp (mobile, PowerVR devices)
  1346. KTX_TTF_PVRTC1_4_RGB = 8,
  1347. /*!< Opaque only. Returns RGB or alpha data, if
  1348. KTX_TF_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS flag is
  1349. specified. */
  1350. KTX_TTF_PVRTC1_4_RGBA = 9,
  1351. /*!< Opaque+alpha. Most useful for simple opacity maps. If the
  1352. texture doesn't have an alpha channel KTX_TTF_PVRTC1_4_RGB
  1353. will be used instead. Lowest quality of any supported
  1354. texture format. */
  1355. // ASTC (mobile, Intel devices, hopefully all desktop GPU's one day)
  1356. KTX_TTF_ASTC_4x4_RGBA = 10,
  1357. /*!< Opaque+alpha, ASTC 4x4. The alpha channel will be opaque for
  1358. textures without an alpha channel. The transcoder uses
  1359. RGB/RGBA/L/LA modes, void extent, and up to two ([0,47] and
  1360. [0,255]) endpoint precisions. */
  1361. // ATC and FXT1 formats are not supported by KTX2 as there
  1362. // are no equivalent VkFormats.
  1363. KTX_TTF_PVRTC2_4_RGB = 18,
  1364. /*!< Opaque-only. Almost BC1 quality, much faster to transcode
  1365. and supports arbitrary texture dimensions (unlike
  1366. PVRTC1 RGB). */
  1367. KTX_TTF_PVRTC2_4_RGBA = 19,
  1368. /*!< Opaque+alpha. Slower to transcode than cTFPVRTC2_4_RGB.
  1369. Premultiplied alpha is highly recommended, otherwise the
  1370. color channel can leak into the alpha channel on transparent
  1371. blocks. */
  1372. KTX_TTF_ETC2_EAC_R11 = 20,
  1373. /*!< R only (ETC2 EAC R11 unsigned). R = opaque.g or alpha.g, if
  1374. KTX_TF_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS flag is
  1375. specified. */
  1376. KTX_TTF_ETC2_EAC_RG11 = 21,
  1377. /*!< RG only (ETC2 EAC RG11 unsigned), R=opaque.g, G=alpha.g. The
  1378. texture should have an alpha channel (if not G will be all
  1379. 255's. For tangent space normal maps. */
  1380. // Uncompressed (raw pixel) formats
  1381. KTX_TTF_RGBA32 = 13,
  1382. /*!< 32bpp RGBA image stored in raster (not block) order in
  1383. memory, R is first byte, A is last byte. */
  1384. KTX_TTF_RGB565 = 14,
  1385. /*!< 16bpp RGB image stored in raster (not block) order in memory,
  1386. R at bit position 11. */
  1387. KTX_TTF_BGR565 = 15,
  1388. /*!< 16bpp RGB image stored in raster (not block) order in memory,
  1389. R at bit position 0. */
  1390. KTX_TTF_RGBA4444 = 16,
  1391. /*!< 16bpp RGBA image stored in raster (not block) order in memory,
  1392. R at bit position 12, A at bit position 0. */
  1393. // Values for automatic selection of RGB or RGBA depending if alpha
  1394. // present.
  1395. KTX_TTF_ETC = 22,
  1396. /*!< Automatically selects @c KTX_TTF_ETC1_RGB or
  1397. @c KTX_TTF_ETC2_RGBA according to presence of alpha. */
  1398. KTX_TTF_BC1_OR_3 = 23,
  1399. /*!< Automatically selects @c KTX_TTF_BC1_RGB or
  1400. @c KTX_TTF_BC3_RGBA according to presence of alpha. */
  1401. KTX_TTF_NOSELECTION = 0x7fffffff,
  1402. // Old enums for compatibility with code compiled against previous
  1403. // versions of libktx.
  1404. KTX_TF_ETC1 = KTX_TTF_ETC1_RGB,
  1405. //!< @deprecated. Use #KTX_TTF_ETC1_RGB.
  1406. KTX_TF_ETC2 = KTX_TTF_ETC,
  1407. //!< @deprecated. Use #KTX_TTF_ETC.
  1408. KTX_TF_BC1 = KTX_TTF_BC1_RGB,
  1409. //!< @deprecated. Use #KTX_TTF_BC1_RGB.
  1410. KTX_TF_BC3 = KTX_TTF_BC3_RGBA,
  1411. //!< @deprecated. Use #KTX_TTF_BC3_RGBA.
  1412. KTX_TF_BC4 = KTX_TTF_BC4_R,
  1413. //!< @deprecated. Use #KTX_TTF_BC4_R.
  1414. KTX_TF_BC5 = KTX_TTF_BC5_RG,
  1415. //!< @deprecated. Use #KTX_TTF_BC5_RG.
  1416. KTX_TTF_BC7_M6_RGB = KTX_TTF_BC7_RGBA,
  1417. //!< @deprecated. Use #KTX_TTF_BC7_RGBA.
  1418. KTX_TTF_BC7_M5_RGBA = KTX_TTF_BC7_RGBA,
  1419. //!< @deprecated. Use #KTX_TTF_BC7_RGBA.
  1420. KTX_TF_BC7_M6_OPAQUE_ONLY = KTX_TTF_BC7_RGBA,
  1421. //!< @deprecated. Use #KTX_TTF_BC7_RGBA
  1422. KTX_TF_PVRTC1_4_OPAQUE_ONLY = KTX_TTF_PVRTC1_4_RGB
  1423. //!< @deprecated. Use #KTX_TTF_PVRTC1_4_RGB.
  1424. } ktx_transcode_fmt_e;
  1425. /**
  1426. * @~English
  1427. * @brief Flags guiding transcoding of Basis Universal compressed textures.
  1428. */
  1429. typedef enum ktx_transcode_flag_bits_e {
  1430. KTX_TF_PVRTC_DECODE_TO_NEXT_POW2 = 2,
  1431. /*!< PVRTC1: decode non-pow2 ETC1S texture level to the next larger
  1432. power of 2 (not implemented yet, but we're going to support it).
  1433. Ignored if the slice's dimensions are already a power of 2.
  1434. */
  1435. KTX_TF_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS = 4,
  1436. /*!< When decoding to an opaque texture format, if the Basis data has
  1437. alpha, decode the alpha slice instead of the color slice to the
  1438. output texture format. Has no effect if there is no alpha data.
  1439. */
  1440. KTX_TF_HIGH_QUALITY = 32,
  1441. /*!< Request higher quality transcode of UASTC to BC1, BC3, ETC2_EAC_R11 and
  1442. ETC2_EAC_RG11. The flag is unused by other UASTC transcoders.
  1443. */
  1444. } ktx_transcode_flag_bits_e;
  1445. typedef ktx_uint32_t ktx_transcode_flags;
  1446. KTX_API KTX_error_code KTX_APIENTRY
  1447. ktxTexture2_TranscodeBasis(ktxTexture2* This, ktx_transcode_fmt_e fmt,
  1448. ktx_transcode_flags transcodeFlags);
  1449. /*
  1450. * Returns a string corresponding to a KTX error code.
  1451. */
  1452. KTX_API const char* KTX_APIENTRY
  1453. ktxErrorString(KTX_error_code error);
  1454. /*
  1455. * Returns a string corresponding to a supercompression scheme.
  1456. */
  1457. KTX_API const char* KTX_APIENTRY
  1458. ktxSupercompressionSchemeString(ktxSupercmpScheme scheme);
  1459. /*
  1460. * Returns a string corresponding to a transcode target format.
  1461. */
  1462. KTX_API const char* KTX_APIENTRY
  1463. ktxTranscodeFormatString(ktx_transcode_fmt_e format);
  1464. KTX_API KTX_error_code KTX_APIENTRY ktxHashList_Create(ktxHashList** ppHl);
  1465. KTX_API KTX_error_code KTX_APIENTRY
  1466. ktxHashList_CreateCopy(ktxHashList** ppHl, ktxHashList orig);
  1467. KTX_API void KTX_APIENTRY ktxHashList_Construct(ktxHashList* pHl);
  1468. KTX_API void KTX_APIENTRY
  1469. ktxHashList_ConstructCopy(ktxHashList* pHl, ktxHashList orig);
  1470. KTX_API void KTX_APIENTRY ktxHashList_Destroy(ktxHashList* head);
  1471. KTX_API void KTX_APIENTRY ktxHashList_Destruct(ktxHashList* head);
  1472. /*
  1473. * Adds a key-value pair to a hash list.
  1474. */
  1475. KTX_API KTX_error_code KTX_APIENTRY
  1476. ktxHashList_AddKVPair(ktxHashList* pHead, const char* key,
  1477. unsigned int valueLen, const void* value);
  1478. /*
  1479. * Deletes a ktxHashListEntry from a ktxHashList.
  1480. */
  1481. KTX_API KTX_error_code KTX_APIENTRY
  1482. ktxHashList_DeleteEntry(ktxHashList* pHead, ktxHashListEntry* pEntry);
  1483. /*
  1484. * Finds the entry for a key in a ktxHashList and deletes it.
  1485. */
  1486. KTX_API KTX_error_code KTX_APIENTRY
  1487. ktxHashList_DeleteKVPair(ktxHashList* pHead, const char* key);
  1488. /*
  1489. * Looks up a key and returns the ktxHashListEntry.
  1490. */
  1491. KTX_API KTX_error_code KTX_APIENTRY
  1492. ktxHashList_FindEntry(ktxHashList* pHead, const char* key,
  1493. ktxHashListEntry** ppEntry);
  1494. /*
  1495. * Looks up a key and returns the value.
  1496. */
  1497. KTX_API KTX_error_code KTX_APIENTRY
  1498. ktxHashList_FindValue(ktxHashList* pHead, const char* key,
  1499. unsigned int* pValueLen, void** pValue);
  1500. /*
  1501. * Return the next entry in a ktxHashList.
  1502. */
  1503. KTX_API ktxHashListEntry* KTX_APIENTRY
  1504. ktxHashList_Next(ktxHashListEntry* entry);
  1505. /*
  1506. * Sorts a ktxHashList into order of the key codepoints.
  1507. */
  1508. KTX_API KTX_error_code KTX_APIENTRY
  1509. ktxHashList_Sort(ktxHashList* pHead);
  1510. /*
  1511. * Serializes a ktxHashList to a block of memory suitable for
  1512. * writing to a KTX file.
  1513. */
  1514. KTX_API KTX_error_code KTX_APIENTRY
  1515. ktxHashList_Serialize(ktxHashList* pHead,
  1516. unsigned int* kvdLen, unsigned char** kvd);
  1517. /*
  1518. * Creates a hash table from the serialized data read from a
  1519. * a KTX file.
  1520. */
  1521. KTX_API KTX_error_code KTX_APIENTRY
  1522. ktxHashList_Deserialize(ktxHashList* pHead, unsigned int kvdLen, void* kvd);
  1523. /*
  1524. * Get the key from a ktxHashListEntry
  1525. */
  1526. KTX_API KTX_error_code KTX_APIENTRY
  1527. ktxHashListEntry_GetKey(ktxHashListEntry* This,
  1528. unsigned int* pKeyLen, char** ppKey);
  1529. /*
  1530. * Get the value from a ktxHashListEntry
  1531. */
  1532. KTX_API KTX_error_code KTX_APIENTRY
  1533. ktxHashListEntry_GetValue(ktxHashListEntry* This,
  1534. unsigned int* pValueLen, void** ppValue);
  1535. /*===========================================================*
  1536. * Utilities for printing info about a KTX file. *
  1537. *===========================================================*/
  1538. KTX_API KTX_error_code KTX_APIENTRY ktxPrintInfoForStdioStream(FILE* stdioStream);
  1539. KTX_API KTX_error_code KTX_APIENTRY ktxPrintInfoForNamedFile(const char* const filename);
  1540. KTX_API KTX_error_code KTX_APIENTRY ktxPrintInfoForMemory(const ktx_uint8_t* bytes, ktx_size_t size);
  1541. /*===========================================================*
  1542. * Utilities for printing info about a KTX2 file. *
  1543. *===========================================================*/
  1544. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoTextForMemory(const ktx_uint8_t* bytes, ktx_size_t size);
  1545. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoTextForNamedFile(const char* const filename);
  1546. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoTextForStdioStream(FILE* stdioStream);
  1547. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoTextForStream(ktxStream* stream);
  1548. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoJSONForMemory(const ktx_uint8_t* bytes, ktx_size_t size, ktx_uint32_t base_indent, ktx_uint32_t indent_width, bool minified);
  1549. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoJSONForNamedFile(const char* const filename, ktx_uint32_t base_indent, ktx_uint32_t indent_width, bool minified);
  1550. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoJSONForStdioStream(FILE* stdioStream, ktx_uint32_t base_indent, ktx_uint32_t indent_width, bool minified);
  1551. KTX_API KTX_error_code KTX_APIENTRY ktxPrintKTX2InfoJSONForStream(ktxStream* stream, ktx_uint32_t base_indent, ktx_uint32_t indent_width, bool minified);
  1552. #ifdef __cplusplus
  1553. }
  1554. #endif
  1555. /*========================================================================*
  1556. * For backward compatibilty with the V3 & early versions of the V4 APIs. *
  1557. *========================================================================*/
  1558. /**
  1559. * @deprecated Will be dropped before V4 release.
  1560. */
  1561. #define ktx_texture_transcode_fmt_e ktx_transcode_fmt_e
  1562. /**
  1563. * @deprecated Will be dropped before V4 release.
  1564. */
  1565. #define ktx_texture_decode_flags ktx_transcode_flag_bits
  1566. /**
  1567. * @deprecated Will be dropped before V4 release.
  1568. */
  1569. #define ktxTexture_GetSize ktxTexture_GetDatasize
  1570. /**
  1571. @~English
  1572. @page libktx_history Revision History
  1573. No longer updated. Kept to preserve ancient history. For more recent history see the repo log at
  1574. https://github.com/KhronosGroup/KTX-Software. See also the Release Notes in the repo.
  1575. @section v8 Version 4.0
  1576. Added:
  1577. @li Support for KTX Version 2.
  1578. @li Support for encoding and transcoding Basis Universal images in KTX Version 2 files.
  1579. @li Function to print info about a KTX file.
  1580. @section v7 Version 3.0.1
  1581. Fixed:
  1582. @li GitHub issue #159: compile failure with recent Vulkan SDKs.
  1583. @li Incorrect mapping of GL DXT3 and DXT5 formats to Vulkan equivalents.
  1584. @li Incorrect BC4 blocksize.
  1585. @li Missing mapping of PVRTC formats from GL to Vulkan.
  1586. @li Incorrect block width and height calculations for sizes that are not
  1587. a multiple of the block size.
  1588. @li Incorrect KTXorientation key in test images.
  1589. @section v6 Version 3.0
  1590. Added:
  1591. @li new ktxTexture object based API for reading KTX files without an OpenGL context.
  1592. @li Vulkan loader. @#include <ktxvulkan.h> to use it.
  1593. Changed:
  1594. @li ktx.h to not depend on KHR/khrplatform.h and GL{,ES*}/gl{corearb,}.h.
  1595. Applications using OpenGL must now include these files themselves.
  1596. @li ktxLoadTexture[FMN], removing the hack of loading 1D textures as 2D textures
  1597. when the OpenGL context does not support 1D textures.
  1598. KTX_UNSUPPORTED_TEXTURE_TYPE is now returned.
  1599. @section v5 Version 2.0.2
  1600. Added:
  1601. @li Support for cubemap arrays.
  1602. Changed:
  1603. @li New build system
  1604. Fixed:
  1605. @li GitHub issue #40: failure to byte-swap key-value lengths.
  1606. @li GitHub issue #33: returning incorrect target when loading cubemaps.
  1607. @li GitHub PR #42: loading of texture arrays.
  1608. @li GitHub PR #41: compilation error when KTX_OPENGL_ES2=1 defined.
  1609. @li GitHub issue #39: stack-buffer-overflow in toktx
  1610. @li Don't use GL_EXTENSIONS on recent OpenGL versions.
  1611. @section v4 Version 2.0.1
  1612. Added:
  1613. @li CMake build files. Thanks to Pavel Rotjberg for the initial version.
  1614. Changed:
  1615. @li ktxWriteKTXF to check the validity of the type & format combinations
  1616. passed to it.
  1617. Fixed:
  1618. @li Public Bugzilla <a href="http://www.khronos.org/bugzilla/show_bug.cgi?id=999">999</a>: 16-bit luminance texture cannot be written.
  1619. @li compile warnings from compilers stricter than MS Visual C++. Thanks to
  1620. Pavel Rotjberg.
  1621. @section v3 Version 2.0
  1622. Added:
  1623. @li support for decoding ETC2 and EAC formats in the absence of a hardware
  1624. decoder.
  1625. @li support for converting textures with legacy LUMINANCE, LUMINANCE_ALPHA,
  1626. etc. formats to the equivalent R, RG, etc. format with an
  1627. appropriate swizzle, when loading in OpenGL Core Profile contexts.
  1628. @li ktxErrorString function to return a string corresponding to an error code.
  1629. @li tests for ktxLoadTexture[FN] that run under OpenGL ES 3.0 and OpenGL 3.3.
  1630. The latter includes an EGL on WGL wrapper that makes porting apps between
  1631. OpenGL ES and OpenGL easier on Windows.
  1632. @li more texture formats to ktxLoadTexture[FN] and toktx tests.
  1633. Changed:
  1634. @li ktxLoadTexture[FMN] to discover the capabilities of the GL context at
  1635. run time and load textures, or not, according to those capabilities.
  1636. Fixed:
  1637. @li failure of ktxWriteKTXF to pad image rows to 4 bytes as required by the KTX
  1638. format.
  1639. @li ktxWriteKTXF exiting with KTX_FILE_WRITE_ERROR when attempting to write
  1640. more than 1 byte of face-LOD padding.
  1641. Although there is only a very minor API change, the addition of ktxErrorString,
  1642. the functional changes are large enough to justify bumping the major revision
  1643. number.
  1644. @section v2 Version 1.0.1
  1645. Implemented ktxLoadTextureM.
  1646. Fixed the following:
  1647. @li Public Bugzilla <a href="http://www.khronos.org/bugzilla/show_bug.cgi?id=571">571</a>: crash when null passed for pIsMipmapped.
  1648. @li Public Bugzilla <a href="http://www.khronos.org/bugzilla/show_bug.cgi?id=572">572</a>: memory leak when unpacking ETC textures.
  1649. @li Public Bugzilla <a href="http://www.khronos.org/bugzilla/show_bug.cgi?id=573">573</a>: potential crash when unpacking ETC textures with unused padding pixels.
  1650. @li Public Bugzilla <a href="http://www.khronos.org/bugzilla/show_bug.cgi?id=576">576</a>: various small fixes.
  1651. Thanks to Krystian Bigaj for the ktxLoadTextureM implementation and these fixes.
  1652. @section v1 Version 1.0
  1653. Initial release.
  1654. */
  1655. #endif /* KTX_H_A55A6F00956F42F3A137C11929827FE1 */