u_format_s3tc.c 24.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**************************************************************************
 *
 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
 * Copyright (c) 2008 VMware, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "u_dl.h"
#include "u_math.h"
#include "u_format.h"
#include "u_format_s3tc.h"


#if defined(_WIN32) || defined(WIN32)
#define DXTN_LIBNAME "dxtn.dll"
33
34
#elif defined(__APPLE__)
#define DXTN_LIBNAME "libtxc_dxtn.dylib"
35
36
37
38
#else
#define DXTN_LIBNAME "libtxc_dxtn.so"
#endif

39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
static void
util_format_dxt1_rgb_fetch_stub(int src_stride,
                                const uint8_t *src,
                                int col, int row,
                                uint8_t *dst)
{
   assert(0);
}


static void
util_format_dxt1_rgba_fetch_stub(int src_stride,
                                 const uint8_t *src,
                                 int col, int row,
                                 uint8_t *dst )
{
   assert(0);
}


static void
util_format_dxt3_rgba_fetch_stub(int src_stride,
                                 const uint8_t *src,
                                 int col, int row,
                                 uint8_t *dst )
{
   assert(0);
}


static void
util_format_dxt5_rgba_fetch_stub(int src_stride,
                                 const uint8_t *src,
                                 int col, int row,
                                 uint8_t *dst )
{
   assert(0);
}


static void
util_format_dxtn_pack_stub(int src_comps,
                           int width, int height,
                           const uint8_t *src,
                           enum util_format_dxtn dst_format,
                           uint8_t *dst,
                           int dst_stride)
{
   assert(0);
}


92
boolean util_format_s3tc_enabled = FALSE;
93

94
95
96
97
util_format_dxtn_fetch_t util_format_dxt1_rgb_fetch = util_format_dxt1_rgb_fetch_stub;
util_format_dxtn_fetch_t util_format_dxt1_rgba_fetch = util_format_dxt1_rgba_fetch_stub;
util_format_dxtn_fetch_t util_format_dxt3_rgba_fetch = util_format_dxt3_rgba_fetch_stub;
util_format_dxtn_fetch_t util_format_dxt5_rgba_fetch = util_format_dxt5_rgba_fetch_stub;
98

99
util_format_dxtn_pack_t util_format_dxtn_pack = util_format_dxtn_pack_stub;
100

101

102
void
103
util_format_s3tc_init(void)
104
{
105
106
107
108
109
110
111
112
113
114
115
   static boolean first_time = TRUE;
   struct util_dl_library *library = NULL;
   util_dl_proc fetch_2d_texel_rgb_dxt1;
   util_dl_proc fetch_2d_texel_rgba_dxt1;
   util_dl_proc fetch_2d_texel_rgba_dxt3;
   util_dl_proc fetch_2d_texel_rgba_dxt5;
   util_dl_proc tx_compress_dxtn;

   if (!first_time)
      return;
   first_time = FALSE;
116

117
118
   if (util_format_s3tc_enabled)
      return;
119

120
   library = util_dl_open(DXTN_LIBNAME);
121
   if (!library) {
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      debug_printf("couldn't open " DXTN_LIBNAME ", software DXTn "
         "compression/decompression unavailable");
      return;
   }

   fetch_2d_texel_rgb_dxt1 =
         util_dl_get_proc_address(library, "fetch_2d_texel_rgb_dxt1");
   fetch_2d_texel_rgba_dxt1 =
         util_dl_get_proc_address(library, "fetch_2d_texel_rgba_dxt1");
   fetch_2d_texel_rgba_dxt3 =
         util_dl_get_proc_address(library, "fetch_2d_texel_rgba_dxt3");
   fetch_2d_texel_rgba_dxt5 =
         util_dl_get_proc_address(library, "fetch_2d_texel_rgba_dxt5");
   tx_compress_dxtn =
         util_dl_get_proc_address(library, "tx_compress_dxtn");

   if (!util_format_dxt1_rgb_fetch ||
       !util_format_dxt1_rgba_fetch ||
       !util_format_dxt3_rgba_fetch ||
       !util_format_dxt5_rgba_fetch ||
       !util_format_dxtn_pack) {
      debug_printf("couldn't reference all symbols in " DXTN_LIBNAME
                   ", software DXTn compression/decompression "
                   "unavailable");
146
      util_dl_close(library);
147
      return;
148
   }
149
150
151
152
153
154
155

   util_format_dxt1_rgb_fetch = (util_format_dxtn_fetch_t)fetch_2d_texel_rgb_dxt1;
   util_format_dxt1_rgba_fetch = (util_format_dxtn_fetch_t)fetch_2d_texel_rgba_dxt1;
   util_format_dxt3_rgba_fetch = (util_format_dxtn_fetch_t)fetch_2d_texel_rgba_dxt3;
   util_format_dxt5_rgba_fetch = (util_format_dxtn_fetch_t)fetch_2d_texel_rgba_dxt5;
   util_format_dxtn_pack = (util_format_dxtn_pack_t)tx_compress_dxtn;
   util_format_s3tc_enabled = TRUE;
156
157
158
159
160
161
162
163
164
165
}


/*
 * Pixel fetch.
 */

void
util_format_dxt1_rgb_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
166
   util_format_dxt1_rgb_fetch(0, src, i, j, dst);
167
168
169
170
171
}

void
util_format_dxt1_rgba_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
172
   util_format_dxt1_rgba_fetch(0, src, i, j, dst);
173
174
175
176
177
}

void
util_format_dxt3_rgba_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
178
   util_format_dxt3_rgba_fetch(0, src, i, j, dst);
179
180
181
182
183
}

void
util_format_dxt5_rgba_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
184
   util_format_dxt5_rgba_fetch(0, src, i, j, dst);
185
186
187
188
189
}

void
util_format_dxt1_rgb_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
190
191
192
193
194
195
   uint8_t tmp[4];
   util_format_dxt1_rgb_fetch(0, src, i, j, tmp);
   dst[0] = ubyte_to_float(tmp[0]);
   dst[1] = ubyte_to_float(tmp[1]);
   dst[2] = ubyte_to_float(tmp[2]);
   dst[3] = 1.0;
196
197
198
199
200
}

void
util_format_dxt1_rgba_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
201
202
203
204
205
206
   uint8_t tmp[4];
   util_format_dxt1_rgba_fetch(0, src, i, j, tmp);
   dst[0] = ubyte_to_float(tmp[0]);
   dst[1] = ubyte_to_float(tmp[1]);
   dst[2] = ubyte_to_float(tmp[2]);
   dst[3] = ubyte_to_float(tmp[3]);
207
208
209
210
211
}

void
util_format_dxt3_rgba_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
212
213
214
215
216
217
   uint8_t tmp[4];
   util_format_dxt3_rgba_fetch(0, src, i, j, tmp);
   dst[0] = ubyte_to_float(tmp[0]);
   dst[1] = ubyte_to_float(tmp[1]);
   dst[2] = ubyte_to_float(tmp[2]);
   dst[3] = ubyte_to_float(tmp[3]);
218
219
220
221
222
}

void
util_format_dxt5_rgba_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
223
224
225
226
227
228
   uint8_t tmp[4];
   util_format_dxt5_rgba_fetch(0, src, i, j, tmp);
   dst[0] = ubyte_to_float(tmp[0]);
   dst[1] = ubyte_to_float(tmp[1]);
   dst[2] = ubyte_to_float(tmp[2]);
   dst[3] = ubyte_to_float(tmp[3]);
229
230
231
232
233
234
235
236
237
238
}


/*
 * Block decompression.
 */

void
util_format_dxt1_rgb_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
239
240
241
242
243
244
245
246
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               util_format_dxt1_rgb_fetch(0, src, i, j, dst);
247
248
            }
         }
249
         src += 8;
250
      }
251
      src_row += src_stride;
252
   }
253
254
255
256
257
}

void
util_format_dxt1_rgba_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
258
259
260
261
262
263
264
265
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               util_format_dxt1_rgba_fetch(0, src, i, j, dst);
266
267
            }
         }
268
         src += 8;
269
      }
270
      src_row += src_stride;
271
   }
272
273
274
275
276
}

void
util_format_dxt3_rgba_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
277
278
279
280
281
282
283
284
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               util_format_dxt3_rgba_fetch(0, src, i, j, dst);
285
286
            }
         }
287
         src += 16;
288
      }
289
      src_row += src_stride;
290
   }
291
292
293
294
295
}

void
util_format_dxt5_rgba_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
296
297
298
299
300
301
302
303
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               util_format_dxt5_rgba_fetch(0, src, i, j, dst);
304
305
            }
         }
306
         src += 16;
307
      }
308
      src_row += src_stride;
309
   }
310
311
312
313
314
}

void
util_format_dxt1_rgb_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
315
316
317
318
319
320
321
322
323
324
325
326
327
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               uint8_t tmp[4];
               util_format_dxt1_rgb_fetch(0, src, i, j, tmp);
               dst[0] = ubyte_to_float(tmp[0]);
               dst[1] = ubyte_to_float(tmp[1]);
               dst[2] = ubyte_to_float(tmp[2]);
               dst[3] = 1.0;
328
329
            }
         }
330
         src += 8;
331
      }
332
      src_row += src_stride;
333
334
335
336
337
338
   }
}

void
util_format_dxt1_rgba_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
339
340
341
342
343
344
345
346
347
348
349
350
351
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               uint8_t tmp[4];
               util_format_dxt1_rgba_fetch(0, src, i, j, tmp);
               dst[0] = ubyte_to_float(tmp[0]);
               dst[1] = ubyte_to_float(tmp[1]);
               dst[2] = ubyte_to_float(tmp[2]);
               dst[3] = ubyte_to_float(tmp[3]);
352
353
            }
         }
354
         src += 8;
355
      }
356
      src_row += src_stride;
357
358
359
360
361
362
   }
}

void
util_format_dxt3_rgba_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
363
364
365
366
367
368
369
370
371
372
373
374
375
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               uint8_t tmp[4];
               util_format_dxt3_rgba_fetch(0, src, i, j, tmp);
               dst[0] = ubyte_to_float(tmp[0]);
               dst[1] = ubyte_to_float(tmp[1]);
               dst[2] = ubyte_to_float(tmp[2]);
               dst[3] = ubyte_to_float(tmp[3]);
376
377
            }
         }
378
         src += 16;
379
      }
380
      src_row += src_stride;
381
382
383
384
385
386
   }
}

void
util_format_dxt5_rgba_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
387
388
389
390
391
392
393
394
395
396
397
398
399
   unsigned x, y, i, j;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      for(x = 0; x < width; x += 4) {
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
               uint8_t tmp[4];
               util_format_dxt5_rgba_fetch(0, src, i, j, tmp);
               dst[0] = ubyte_to_float(tmp[0]);
               dst[1] = ubyte_to_float(tmp[1]);
               dst[2] = ubyte_to_float(tmp[2]);
               dst[3] = ubyte_to_float(tmp[3]);
400
401
            }
         }
402
         src += 16;
403
      }
404
      src_row += src_stride;
405
406
407
408
409
410
411
412
413
414
415
   }
}


/*
 * Block compression.
 */

void
util_format_dxt1_rgb_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
416
417
418
419
420
421
422
423
424
425
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][3];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 3; ++k) {
                  tmp[j][i][k] = src[(y + j)*src_stride/sizeof(*src) + i*4 + k];
426
427
428
               }
            }
         }
429
430
431
         util_format_dxtn_pack(3, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT1_RGB, dst, dst_stride);
         src += 4*4;
         dst += 8;
432
      }
433
434
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
435
   }
436
437
438
439
440
}

void
util_format_dxt1_rgba_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
441
442
443
444
445
446
447
448
449
450
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][4];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 4; ++k) {
                  tmp[j][i][k] = src[(y + j)*src_stride/sizeof(*src) + i*4 + k];
451
452
453
               }
            }
         }
454
455
456
         util_format_dxtn_pack(4, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT1_RGBA, dst, dst_stride);
         src += 4*4;
         dst += 8;
457
      }
458
459
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
460
   }
461
462
463
464
465
}

void
util_format_dxt3_rgba_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
466
467
468
469
470
471
472
473
474
475
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][4];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 4; ++k) {
                  tmp[j][i][k] = src[(y + j)*src_stride/sizeof(*src) + i*4 + k];
476
477
478
               }
            }
         }
479
480
481
         util_format_dxtn_pack(4, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT3_RGBA, dst, dst_stride);
         src += 4*4;
         dst += 16;
482
      }
483
484
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
485
   }
486
487
488
489
490
}

void
util_format_dxt5_rgba_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
491
492
493
494
495
496
497
498
499
500
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const uint8_t *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][4];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 4; ++k) {
                  tmp[j][i][k] = src[(y + j)*src_stride/sizeof(*src) + i*4 + k];
501
502
503
               }
            }
         }
504
505
506
         util_format_dxtn_pack(4, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT5_RGBA, dst, dst_stride);
         src += 4*4;
         dst += 16;
507
      }
508
509
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
510
   }
511
512
513
514
515
}

void
util_format_dxt1_rgb_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
516
517
518
519
520
521
522
523
524
525
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const float *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][3];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 3; ++k) {
                  tmp[j][i][k] = float_to_ubyte(src[(y + j)*src_stride/sizeof(*src) + i*4 + k]);
526
527
528
               }
            }
         }
529
530
531
         util_format_dxtn_pack(3, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT1_RGB, dst, dst_stride);
         src += 4*4;
         dst += 8;
532
      }
533
534
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
535
   }
536
537
538
539
540
}

void
util_format_dxt1_rgba_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
541
542
543
544
545
546
547
548
549
550
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const float *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][4];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 4; ++k) {
                  tmp[j][i][k] = float_to_ubyte(src[(y + j)*src_stride/sizeof(*src) + i*4 + k]);
551
552
553
               }
            }
         }
554
555
556
         util_format_dxtn_pack(4, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT1_RGBA, dst, dst_stride);
         src += 4*4;
         dst += 8;
557
      }
558
559
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
560
   }
561
562
563
564
565
}

void
util_format_dxt3_rgba_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
566
567
568
569
570
571
572
573
574
575
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const float *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][4];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 4; ++k) {
                  tmp[j][i][k] = float_to_ubyte(src[(y + j)*src_stride/sizeof(*src) + i*4 + k]);
576
577
578
               }
            }
         }
579
580
581
         util_format_dxtn_pack(4, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT3_RGBA, dst, dst_stride);
         src += 4*4;
         dst += 16;
582
      }
583
584
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
585
   }
586
587
588
589
590
}

void
util_format_dxt5_rgba_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
591
592
593
594
595
596
597
598
599
600
   unsigned x, y, i, j, k;
   for(y = 0; y < height; y += 4) {
      const float *src = src_row;
      uint8_t *dst = dst_row;
      for(x = 0; x < width; x += 4) {
         uint8_t tmp[4][4][4];
         for(j = 0; j < 4; ++j) {
            for(i = 0; i < 4; ++i) {
               for(k = 0; k < 4; ++k) {
                  tmp[j][i][k] = float_to_ubyte(src[(y + j)*src_stride/sizeof(*src) + i*4 + k]);
601
602
603
               }
            }
         }
604
605
606
         util_format_dxtn_pack(4, 4, 4, &tmp[0][0][0], UTIL_FORMAT_DXT5_RGBA, dst, dst_stride);
         src += 4*4;
         dst += 16;
607
      }
608
609
      src_row += src_stride;
      dst_row += 4*dst_stride/sizeof(*dst_row);
610
   }
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
}


/*
 * SRGB variants.
 *
 * FIXME: shunts to RGB for now
 */

void
util_format_dxt1_srgb_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgb_unpack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgb_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgb_pack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgb_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt1_rgb_fetch_8unorm(dst, src, i, j);
}

void
util_format_dxt1_srgba_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgba_unpack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgba_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgba_pack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgba_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt1_rgba_fetch_8unorm(dst, src, i, j);
}

void
util_format_dxt3_srgba_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt3_rgba_unpack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt3_srgba_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt3_rgba_pack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt3_srgba_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt3_rgba_fetch_8unorm(dst, src, i, j);
}

void
util_format_dxt5_srgba_unpack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt5_rgba_unpack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt5_srgba_pack_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt5_rgba_pack_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt5_srgba_fetch_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt5_rgba_fetch_8unorm(dst, src, i, j);
}

void
util_format_dxt1_srgb_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgb_unpack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgb_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgb_pack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgb_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt1_rgb_fetch_float(dst, src, i, j);
}

void
util_format_dxt1_srgba_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgba_unpack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgba_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt1_rgba_pack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt1_srgba_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt1_rgba_fetch_float(dst, src, i, j);
}

void
util_format_dxt3_srgba_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt3_rgba_unpack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt3_srgba_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt3_rgba_pack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt3_srgba_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt3_rgba_fetch_float(dst, src, i, j);
}

void
util_format_dxt5_srgba_unpack_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt5_rgba_unpack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt5_srgba_pack_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
{
   util_format_dxt5_rgba_pack_float(dst_row, dst_stride, src_row, src_stride, width, height);
}

void
util_format_dxt5_srgba_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
   util_format_dxt5_rgba_fetch_float(dst, src, i, j);
}