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