u_format_etc.c revision 5c31eb78e5fa4c4cfcc82df3c9a2c34fcd4b402c
1#include "pipe/p_compiler.h"
2#include "util/u_debug.h"
3#include "util/u_math.h"
4#include "u_format_etc.h"
5
6/* define etc1_parse_block and etc. */
7#define UINT8_TYPE uint8_t
8#define TAG(x) x
9#include "../../../mesa/main/texcompress_etc_tmp.h"
10#undef TAG
11#undef UINT8_TYPE
12
13void
14util_format_etc1_rgb8_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
15{
16   const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
17   struct etc1_block block;
18   unsigned x, y, i, j;
19
20   for (y = 0; y < height; y += bh) {
21      const uint8_t *src = src_row;
22
23      for (x = 0; x < width; x+= bw) {
24         etc1_parse_block(&block, src);
25
26         for (j = 0; j < bh; j++) {
27            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
28            for (i = 0; i < bw; i++) {
29               etc1_fetch_texel(&block, i, j, dst);
30               dst[3] = 255;
31               dst += comps;
32            }
33         }
34
35         src += bs;
36      }
37
38      src_row += src_stride;
39   }
40}
41
42void
43util_format_etc1_rgb8_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
44{
45   assert(0);
46}
47
48void
49util_format_etc1_rgb8_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
50{
51   const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
52   struct etc1_block block;
53   unsigned x, y, i, j;
54
55   for (y = 0; y < height; y += bh) {
56      const uint8_t *src = src_row;
57
58      for (x = 0; x < width; x+= bw) {
59         etc1_parse_block(&block, src);
60
61         for (j = 0; j < bh; j++) {
62            float *dst = dst_row + (y + j) * dst_stride / sizeof(*dst_row) + x * comps;
63            uint8_t tmp[3];
64
65            for (i = 0; i < bw; i++) {
66               etc1_fetch_texel(&block, i, j, tmp);
67               dst[0] = ubyte_to_float(tmp[0]);
68               dst[1] = ubyte_to_float(tmp[1]);
69               dst[2] = ubyte_to_float(tmp[2]);
70               dst[3] = 1.0f;
71               dst += comps;
72            }
73         }
74
75         src += bs;
76      }
77
78      src_row += src_stride;
79   }
80}
81
82void
83util_format_etc1_rgb8_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
84{
85   assert(0);
86}
87
88void
89util_format_etc1_rgb8_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
90{
91   const unsigned bw = 4, bh = 4;
92   struct etc1_block block;
93   uint8_t tmp[3];
94
95   assert(i < bw && j < bh);
96
97   etc1_parse_block(&block, src);
98   etc1_fetch_texel(&block, i, j, tmp);
99
100   dst[0] = ubyte_to_float(tmp[0]);
101   dst[1] = ubyte_to_float(tmp[1]);
102   dst[2] = ubyte_to_float(tmp[2]);
103   dst[3] = 1.0f;
104}
105