ktxint.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* -*- tab-width: 4; -*- */
  2. /* vi: set sw=2 ts=4 expandtab: */
  3. /* $Id: e36ad79b5eac8ea237d6a05602c71aadab575519 $ */
  4. /*
  5. * Copyright 2010-2020 The Khronos Group Inc.
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. /*
  9. * Author: Mark Callow from original code by Georg Kolling
  10. */
  11. #ifndef KTXINT_H
  12. #define KTXINT_H
  13. #include <math.h>
  14. /* Define this to include the ETC unpack software in the library. */
  15. #ifndef SUPPORT_SOFTWARE_ETC_UNPACK
  16. /* Include for all GL versions because have seen OpenGL ES 3
  17. * implementaions that do not support ETC1 (ARM Mali emulator v1.0)!
  18. */
  19. #define SUPPORT_SOFTWARE_ETC_UNPACK 1
  20. #endif
  21. #ifndef MAX
  22. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  23. #endif
  24. #define QUOTE(x) #x
  25. #define STR(x) QUOTE(x)
  26. #define KTX2_IDENTIFIER_REF { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }
  27. #define KTX2_HEADER_SIZE (80)
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * @internal
  33. * @brief used to pass GL context capabilites to subroutines.
  34. */
  35. #define _KTX_NO_R16_FORMATS 0x0
  36. #define _KTX_R16_FORMATS_NORM 0x1
  37. #define _KTX_R16_FORMATS_SNORM 0x2
  38. #define _KTX_ALL_R16_FORMATS (_KTX_R16_FORMATS_NORM | _KTX_R16_FORMATS_SNORM)
  39. extern GLint _ktxR16Formats;
  40. extern GLboolean _ktxSupportsSRGB;
  41. /**
  42. * @internal
  43. * @~English
  44. * @brief KTX file header.
  45. *
  46. * See the KTX specification for descriptions.
  47. */
  48. typedef struct KTX_header {
  49. ktx_uint8_t identifier[12];
  50. ktx_uint32_t endianness;
  51. ktx_uint32_t glType;
  52. ktx_uint32_t glTypeSize;
  53. ktx_uint32_t glFormat;
  54. ktx_uint32_t glInternalformat;
  55. ktx_uint32_t glBaseInternalformat;
  56. ktx_uint32_t pixelWidth;
  57. ktx_uint32_t pixelHeight;
  58. ktx_uint32_t pixelDepth;
  59. ktx_uint32_t numberOfArrayElements;
  60. ktx_uint32_t numberOfFaces;
  61. ktx_uint32_t numberOfMipLevels;
  62. ktx_uint32_t bytesOfKeyValueData;
  63. } KTX_header;
  64. /* This will cause compilation to fail if the struct size doesn't match */
  65. typedef int KTX_header_SIZE_ASSERT [sizeof(KTX_header) == KTX_HEADER_SIZE];
  66. /**
  67. * @internal
  68. * @~English
  69. * @brief 32-bit KTX 2 index entry.
  70. */
  71. typedef struct ktxIndexEntry32 {
  72. ktx_uint32_t byteOffset; /*!< Offset of item from start of file. */
  73. ktx_uint32_t byteLength; /*!< Number of bytes of data in the item. */
  74. } ktxIndexEntry32;
  75. /**
  76. * @internal
  77. * @~English
  78. * @brief 64-bit KTX 2 index entry.
  79. */
  80. typedef struct ktxIndexEntry64 {
  81. ktx_uint64_t byteOffset; /*!< Offset of item from start of file. */
  82. ktx_uint64_t byteLength; /*!< Number of bytes of data in the item. */
  83. } ktxIndexEntry64;
  84. /**
  85. * @internal
  86. * @~English
  87. * @brief KTX 2 file header.
  88. *
  89. * See the KTX 2 specification for descriptions.
  90. */
  91. typedef struct KTX_header2 {
  92. ktx_uint8_t identifier[12];
  93. ktx_uint32_t vkFormat;
  94. ktx_uint32_t typeSize;
  95. ktx_uint32_t pixelWidth;
  96. ktx_uint32_t pixelHeight;
  97. ktx_uint32_t pixelDepth;
  98. ktx_uint32_t layerCount;
  99. ktx_uint32_t faceCount;
  100. ktx_uint32_t levelCount;
  101. ktx_uint32_t supercompressionScheme;
  102. ktxIndexEntry32 dataFormatDescriptor;
  103. ktxIndexEntry32 keyValueData;
  104. ktxIndexEntry64 supercompressionGlobalData;
  105. } KTX_header2;
  106. /* This will cause compilation to fail if the struct size doesn't match */
  107. typedef int KTX_header2_SIZE_ASSERT [sizeof(KTX_header2) == KTX2_HEADER_SIZE];
  108. /**
  109. * @internal
  110. * @~English
  111. * @brief KTX 2 level index entry.
  112. */
  113. typedef struct ktxLevelIndexEntry {
  114. ktx_uint64_t byteOffset; /*!< Offset of level from start of file. */
  115. ktx_uint64_t byteLength;
  116. /*!< Number of bytes of compressed image data in the level. */
  117. ktx_uint64_t uncompressedByteLength;
  118. /*!< Number of bytes of uncompressed image data in the level. */
  119. } ktxLevelIndexEntry;
  120. /**
  121. * @internal
  122. * @~English
  123. * @brief Structure for supplemental information about the texture.
  124. *
  125. * _ktxCheckHeader returns supplemental information about the texture in this
  126. * structure that is derived during checking of the file header.
  127. */
  128. typedef struct KTX_supplemental_info
  129. {
  130. ktx_uint8_t compressed;
  131. ktx_uint8_t generateMipmaps;
  132. ktx_uint16_t textureDimension;
  133. } KTX_supplemental_info;
  134. /**
  135. * @internal
  136. * @var ktx_uint8_t KTX_supplemental_info::compressed
  137. * @~English
  138. * @brief KTX_TRUE, if this a compressed texture, KTX_FALSE otherwise?
  139. */
  140. /**
  141. * @internal
  142. * @var ktx_uint8_t KTX_supplemental_info::generateMipmaps
  143. * @~English
  144. * @brief KTX_TRUE, if mipmap generation is required, KTX_FALSE otherwise.
  145. */
  146. /**
  147. * @internal
  148. * @var ktx_uint16_t KTX_supplemental_info::textureDimension
  149. * @~English
  150. * @brief The number of dimensions, 1, 2 or 3, of data in the texture image.
  151. */
  152. /*
  153. * @internal
  154. * CheckHeader1
  155. *
  156. * Reads the KTX file header and performs some sanity checking on the values
  157. */
  158. KTX_error_code ktxCheckHeader1_(KTX_header* pHeader,
  159. KTX_supplemental_info* pSuppInfo);
  160. /*
  161. * @internal
  162. * CheckHeader2
  163. *
  164. * Reads the KTX 2 file header and performs some sanity checking on the values
  165. */
  166. KTX_error_code ktxCheckHeader2_(KTX_header2* pHeader,
  167. KTX_supplemental_info* pSuppInfo);
  168. /*
  169. * SwapEndian16: Swaps endianness in an array of 16-bit values
  170. */
  171. void _ktxSwapEndian16(ktx_uint16_t* pData16, ktx_size_t count);
  172. /*
  173. * SwapEndian32: Swaps endianness in an array of 32-bit values
  174. */
  175. void _ktxSwapEndian32(ktx_uint32_t* pData32, ktx_size_t count);
  176. /*
  177. * SwapEndian32: Swaps endianness in an array of 64-bit values
  178. */
  179. void _ktxSwapEndian64(ktx_uint64_t* pData64, ktx_size_t count);
  180. /*
  181. * UnpackETC: uncompresses an ETC compressed texture image
  182. */
  183. KTX_error_code _ktxUnpackETC(const GLubyte* srcETC, const GLenum srcFormat,
  184. ktx_uint32_t active_width, ktx_uint32_t active_height,
  185. GLubyte** dstImage,
  186. GLenum* format, GLenum* internalFormat, GLenum* type,
  187. GLint R16Formats, GLboolean supportsSRGB);
  188. /*
  189. * Pad nbytes to next multiple of n
  190. */
  191. #define _KTX_PADN(n, nbytes) (ktx_uint32_t)(n * ceilf((float)(nbytes) / n))
  192. /*
  193. * Calculate bytes of of padding needed to reach next multiple of n.
  194. */
  195. /* Equivalent to (n * ceil(nbytes / n)) - nbytes */
  196. #define _KTX_PADN_LEN(n, nbytes) \
  197. (ktx_uint32_t)((n * ceilf((float)(nbytes) / n)) - (nbytes))
  198. /*
  199. * Pad nbytes to next multiple of 4
  200. */
  201. #define _KTX_PAD4(nbytes) _KTX_PADN(4, nbytes)
  202. /*
  203. * Calculate bytes of of padding needed to reach next multiple of 4.
  204. */
  205. #define _KTX_PAD4_LEN(nbytes) _KTX_PADN_LEN(4, nbytes)
  206. /*
  207. * Pad nbytes to next multiple of 8
  208. */
  209. #define _KTX_PAD8(nbytes) _KTX_PADN(8, nbytes)
  210. /*
  211. * Calculate bytes of of padding needed to reach next multiple of 8.
  212. */
  213. #define _KTX_PAD8_LEN(nbytes) _KTX_PADN_LEN(8, nbytes)
  214. /*
  215. * Pad nbytes to KTX_GL_UNPACK_ALIGNMENT
  216. */
  217. #define _KTX_PAD_UNPACK_ALIGN(nbytes) \
  218. _KTX_PADN(KTX_GL_UNPACK_ALIGNMENT, nbytes)
  219. /*
  220. * Calculate bytes of of padding needed to reach KTX_GL_UNPACK_ALIGNMENT.
  221. */
  222. #define _KTX_PAD_UNPACK_ALIGN_LEN(nbytes) \
  223. _KTX_PADN_LEN(KTX_GL_UNPACK_ALIGNMENT, nbytes)
  224. /*
  225. ======================================
  226. Internal utility functions
  227. ======================================
  228. */
  229. void printKTX2Info2(ktxStream* src, KTX_header2* header);
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* KTXINT_H */