Debian Codegraph @

libs/crunch/crn_defs.h

raw symbols SCIP
1 #ifndef CRND_INCLUDE_CRN_DEFS_H
2 #define CRND_INCLUDE_CRN_DEFS_H
3
4 // Include crnlib.h (only to bring in some basic CRN-related types).
5 #include "crnlib.h"
6
7 #define CRND_LIB_VERSION 104
8 #define CRND_VERSION_STRING "01.04"
9
10 #ifdef _DEBUG
11 #define CRND_BUILD_DEBUG
12 #else
13 #define CRND_BUILD_RELEASE
14 #endif
15
16 // CRN decompression API
17 namespace crnd {
18 typedef unsigned char uint8;
19 typedef signed char int8;
20 typedef unsigned short uint16;
21 typedef signed short int16;
22 typedef unsigned int uint32;
23 typedef uint32 uint32;
24 typedef unsigned int uint;
25 typedef signed int int32;
26 #ifdef __GNUC__
27 typedef unsigned long long uint64;
28 typedef long long int64;
29 #else
30 typedef unsigned __int64 uint64;
31 typedef signed __int64 int64;
32 #endif
33
34 // The crnd library assumes all allocation blocks have at least CRND_MIN_ALLOC_ALIGNMENT alignment.
35 const uint32 CRND_MIN_ALLOC_ALIGNMENT = sizeof(uint32) * 2U;
36
37 // realloc callback:
38 // Used to allocate, resize, or free memory blocks.
39 // If p is NULL, the realloc function attempts to allocate a block of at least size bytes. Returns NULL on out of memory.
40 // *pActual_size must be set to the actual size of the allocated block, which must be greater than or equal to the requested size.
41 // If p is not NULL, and size is 0, the realloc function frees the specified block, and always returns NULL. *pActual_size should be set to 0.
42 // If p is not NULL, and size is non-zero, the realloc function attempts to resize the specified block:
43 // If movable is false, the realloc function attempts to shrink or expand the block in-place. NULL is returned if the block cannot be resized in place, or if the
44 // underlying heap implementation doesn't support in-place resizing. Otherwise, the pointer to the original block is returned.
45 // If movable is true, it is permissible to move the block's contents if it cannot be resized in place. NULL is returned if the block cannot be resized in place, and there
46 // is not enough memory to relocate the block.
47 // In all cases, *pActual_size must be set to the actual size of the allocated block, whether it was successfully resized or not.
48 typedef void* (*crnd_realloc_func)(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data);
49
50 // msize callback: Returns the size of the memory block in bytes, or 0 if the pointer or block is invalid.
51 typedef size_t (*crnd_msize_func)(void* p, void* pUser_data);
52
53 // crnd_set_memory_callbacks() - Use to override the crnd library's memory allocation functions.
54 // If any input parameters are NULL, the memory callback functions are reset to the default functions.
55 // The default functions call malloc(), free(), _msize(), _expand(), etc.
56 void crnd_set_memory_callbacks(crnd_realloc_func pRealloc, crnd_msize_func pMSize, void* pUser_data);
57
58 struct crn_file_info {
59 inline crn_file_info()
60 : m_struct_size(sizeof(crn_file_info)) {}
61
62 uint32 m_struct_size;
63 uint32 m_actual_data_size;
64 uint32 m_header_size;
65 uint32 m_total_palette_size;
66 uint32 m_tables_size;
67 uint32 m_levels;
68 uint32 m_level_compressed_size[cCRNMaxLevels];
69 uint32 m_color_endpoint_palette_entries;
70 uint32 m_color_selector_palette_entries;
71 uint32 m_alpha_endpoint_palette_entries;
72 uint32 m_alpha_selector_palette_entries;
73 };
74
75 struct crn_texture_info {
76 inline crn_texture_info()
77 : m_struct_size(sizeof(crn_texture_info)) {}
78
79 uint32 m_struct_size;
80 uint32 m_width;
81 uint32 m_height;
82 uint32 m_levels;
83 uint32 m_faces;
84 uint32 m_bytes_per_block;
85 uint32 m_userdata0;
86 uint32 m_userdata1;
87 crn_format m_format;
88 };
89
90 struct crn_level_info {
91 inline crn_level_info()
92 : m_struct_size(sizeof(crn_level_info)) {}
93
94 uint32 m_struct_size;
95 uint32 m_width;
96 uint32 m_height;
97 uint32 m_faces;
98 uint32 m_blocks_x;
99 uint32 m_blocks_y;
100 uint32 m_bytes_per_block;
101 crn_format m_format;
102 };
103
104 // Returns the FOURCC format code corresponding to the specified CRN format.
105 uint32 crnd_crn_format_to_fourcc(crn_format fmt);
106
107 // Returns the fundamental GPU format given a potentially swizzled DXT5 crn_format.
108 crn_format crnd_get_fundamental_dxt_format(crn_format fmt);
109
110 // Returns the size of the crn_format in bits/texel (either 4 or 8).
111 uint32 crnd_get_crn_format_bits_per_texel(crn_format fmt);
112
113 // Returns the number of bytes per DXTn block (8 or 16).
114 uint32 crnd_get_bytes_per_dxt_block(crn_format fmt);
115
116 // Validates the file header, performing a CRC check on it and verifying that there is
117 // e.g. a sane number of levels.
118 // pData/data_size need only include the header, but may be the whole file.
119 // The crn_file_info.m_struct_size field must be set before calling this function.
120 bool crnd_validate_header(const void* pData, uint32 data_size, crn_file_info* pFile_info);
121
122 // Validates the entire file by calling crnd_validate_header, then checking the data CRC
123 // for the whole file.
124 // This is not something you want to be doing much!
125 // The crn_file_info.m_struct_size field must be set before calling this function.
126 bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info);
127
128 // Retrieves texture information from the CRN file.
129 // The crn_texture_info.m_struct_size field must be set before calling this function.
130 bool crnd_get_texture_info(const void* pData, uint32 data_size, crn_texture_info* pTexture_info);
131
132 // Retrieves mipmap level specific information from the CRN file.
133 // The crn_level_info.m_struct_size field must be set before calling this function.
134 bool crnd_get_level_info(const void* pData, uint32 data_size, uint32 level_index, crn_level_info* pLevel_info);
135
136 // Transcode/unpack context handle.
137 typedef void* crnd_unpack_context;
138
139 // crnd_unpack_begin() - Decompresses the texture's decoder tables and endpoint/selector palettes.
140 // Once you call this function, you may call crnd_unpack_level() to unpack one or more mip levels.
141 // Don't call this once per mip level (unless you absolutely must)!
142 // This function allocates enough memory to hold: Huffman decompression tables, and the endpoint/selector palettes (color and/or alpha).
143 // Worst case allocation is approx. 200k, assuming all palettes contain 8192 entries.
144 // pData must point to a buffer holding all of the compressed .CRN file data.
145 // This buffer must be stable until crnd_unpack_end() is called.
146 // Returns NULL if out of memory, or if any of the input parameters are invalid.
147 crnd_unpack_context crnd_unpack_begin(const void* pData, uint32 data_size);
148
149 // Returns a pointer to the compressed .CRN data associated with a crnd_unpack_context.
150 // Returns false if any of the input parameters are invalid.
151 bool crnd_get_data(crnd_unpack_context pContext, const void** ppData, uint32* pData_size);
152
153 // crnd_unpack_level() - Transcodes the specified mipmap level to a destination buffer in cached or write combined memory.
154 // pContext - Context created by a call to crnd_unpack_begin().
155 // ppDst - A pointer to an array of 1 or 6 destination buffer pointers. Cubemaps require an array of 6 pointers, 2D textures require an array of 1 pointer.
156 // dst_size_in_bytes - Optional size of each destination buffer. Only used for debugging - OK to set to UINT32_MAX.
157 // row_pitch_in_bytes - The pitch in bytes from one row of DXT blocks to the next. Must be a multiple of 4.
158 // level_index - mipmap level index, where 0 is the largest/first level.
159 // Returns false if any of the input parameters, or the compressed stream, are invalid.
160 // This function does not allocate any memory.
161 bool crnd_unpack_level(
162 crnd_unpack_context pContext,
163 void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes,
164 uint32 level_index);
165
166 // crnd_unpack_level_segmented() - Unpacks the specified mipmap level from a "segmented" CRN file.
167 // See the crnd_create_segmented_file() API below.
168 // Segmented files allow the user to control where the compressed mipmap data is stored.
169 bool crnd_unpack_level_segmented(
170 crnd_unpack_context pContext,
171 const void* pSrc, uint32 src_size_in_bytes,
172 void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes,
173 uint32 level_index);
174
175 // crnd_unpack_end() - Frees the decompress tables and unpacked palettes associated with the specified unpack context.
176 // Returns false if the context is NULL, or if it points to an invalid context.
177 // This function frees all memory associated with the context.
178 bool crnd_unpack_end(crnd_unpack_context pContext);
179
180 // The following API's allow the user to create "segmented" CRN files. A segmented file contains multiple pieces:
181 // - Base data: Header + compression tables
182 // - Level data: Individual mipmap levels
183 // This allows mipmap levels from multiple CRN files to be tightly packed together into single files.
184
185 // Returns a pointer to the level's compressed data, and optionally returns the level's compressed data size if pSize is not NULL.
186 const void* crnd_get_level_data(const void* pData, uint32 data_size, uint32 level_index, uint32* pSize);
187
188 // Returns the compressed size of the texture's header and compression tables (but no levels).
189 uint32 crnd_get_segmented_file_size(const void* pData, uint32 data_size);
190
191 // Creates a "segmented" CRN texture from a normal CRN texture. The new texture will be created at pBase_data, and will be crnd_get_base_data_size() bytes long.
192 // base_data_size must be >= crnd_get_base_data_size().
193 // The base data will contain the CRN header and compression tables, but no mipmap data.
194 bool crnd_create_segmented_file(const void* pData, uint32 data_size, void* pBase_data, uint base_data_size);
195
196 } // namespace crnd
197
198 // Low-level CRN file header cracking.
199 namespace crnd {
200 template <unsigned int N>
201 struct crn_packed_uint {
202 inline crn_packed_uint() {}
203
204 inline crn_packed_uint(unsigned int val) { *this = val; }
205
206 inline crn_packed_uint(const crn_packed_uint& other) { *this = other; }
207
208 inline crn_packed_uint& operator=(const crn_packed_uint& rhs) {
209 if (this != &rhs)
210 memcpy(m_buf, rhs.m_buf, sizeof(m_buf));
211 return *this;
212 }
213
214 inline crn_packed_uint& operator=(unsigned int val) {
215 //CRND_ASSERT((N == 4U) || (val < (1U << (N * 8U))));
216
217 val <<= (8U * (4U - N));
218
219 for (unsigned int i = 0; i < N; i++) {
220 m_buf[i] = static_cast<unsigned char>(val >> 24U);
221 val <<= 8U;
222 }
223
224 return *this;
225 }
226
227 inline operator unsigned int() const {
228 switch (N) {
229 case 1:
230 return m_buf[0];
231 case 2:
232 return (m_buf[0] << 8U) | m_buf[1];
233 case 3:
234 return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]);
235 default:
236 return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]);
237 }
238 }
239
240 unsigned char m_buf[N];
241 };
242
243 #pragma pack(push)
244 #pragma pack(1)
245 struct crn_palette {
246 crn_packed_uint<3> m_ofs;
247 crn_packed_uint<3> m_size;
248 crn_packed_uint<2> m_num;
249 };
250
251 enum crn_header_flags {
252 // If set, the compressed mipmap level data is not located after the file's base data - it will be separately managed by the user instead.
253 cCRNHeaderFlagSegmented = 1
254 };
255
256 struct crn_header {
257 enum { cCRNSigValue = ('H' << 8) | 'x' };
258
259 crn_packed_uint<2> m_sig;
260 crn_packed_uint<2> m_header_size;
261 crn_packed_uint<2> m_header_crc16;
262
263 crn_packed_uint<4> m_data_size;
264 crn_packed_uint<2> m_data_crc16;
265
266 crn_packed_uint<2> m_width;
267 crn_packed_uint<2> m_height;
268
269 crn_packed_uint<1> m_levels;
270 crn_packed_uint<1> m_faces;
271
272 crn_packed_uint<1> m_format;
273 crn_packed_uint<2> m_flags;
274
275 crn_packed_uint<4> m_reserved;
276 crn_packed_uint<4> m_userdata0;
277 crn_packed_uint<4> m_userdata1;
278
279 crn_palette m_color_endpoints;
280 crn_palette m_color_selectors;
281
282 crn_palette m_alpha_endpoints;
283 crn_palette m_alpha_selectors;
284
285 crn_packed_uint<2> m_tables_size;
286 crn_packed_uint<3> m_tables_ofs;
287
288 // m_level_ofs[] is actually an array of offsets: m_level_ofs[m_levels]
289 crn_packed_uint<4> m_level_ofs[1];
290 };
291
292 const unsigned int cCRNHeaderMinSize = 62U;
293
294 #pragma pack(pop)
295
296 } // namespace crnd
297
298 #endif // CRND_INCLUDE_CRN_DEFS_H