1 /**************************************************************************
2  *
3  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
4  * Copyright (c) 2008 VMware, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  **************************************************************************/
25 
26 #include "util/format/u_format.h"
27 #include "util/format/u_format_bptc.h"
28 #include "u_format_pack.h"
29 #include "util/format_srgb.h"
30 #include "util/u_math.h"
31 
32 #include "util/format/texcompress_bptc_tmp.h"
33 
34 void
util_format_bptc_rgba_unorm_unpack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)35 util_format_bptc_rgba_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
36                                                const uint8_t *restrict src_row, unsigned src_stride,
37                                                unsigned width, unsigned height)
38 {
39   decompress_rgba_unorm(width, height,
40                         src_row, src_stride,
41                         dst_row, dst_stride);
42 }
43 
44 void
util_format_bptc_rgba_unorm_pack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)45 util_format_bptc_rgba_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
46                                              const uint8_t *restrict src_row, unsigned src_stride,
47                                              unsigned width, unsigned height)
48 {
49    compress_rgba_unorm(width, height,
50                        src_row, src_stride,
51                        dst_row, dst_stride);
52 }
53 
54 void
util_format_bptc_rgba_unorm_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)55 util_format_bptc_rgba_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
56                                               const uint8_t *restrict src_row, unsigned src_stride,
57                                               unsigned width, unsigned height)
58 {
59    uint8_t *temp_block;
60    temp_block = malloc(width * height * 4 * sizeof(uint8_t));
61    decompress_rgba_unorm(width, height,
62                          src_row, src_stride,
63                          temp_block, width * 4 * sizeof(uint8_t));
64    /* Direct call to row unpack instead of util_format_rgba_unpack_rect()
65     * to avoid table lookup that would pull in all unpack symbols.
66     */
67    for (int y = 0; y < height; y++) {
68       util_format_r8g8b8a8_unorm_unpack_rgba_float((char *)dst_row + dst_stride * y,
69                                                     temp_block + 4 * width * y,
70                                                     width);
71    }
72    free((void *) temp_block);
73 }
74 
75 void
util_format_bptc_rgba_unorm_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)76 util_format_bptc_rgba_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
77                                             const float *restrict src_row, unsigned src_stride,
78                                             unsigned width, unsigned height)
79 {
80    uint8_t *temp_block;
81    temp_block = malloc(width * height * 4 * sizeof(uint8_t));
82    /* Direct call to row unpack instead of util_format_rgba_unpack_rect()
83     * to avoid table lookup that would pull in all unpack symbols.
84     */
85    for (int y = 0; y < height; y++) {
86       util_format_r32g32b32a32_float_unpack_rgba_8unorm(
87                         temp_block + 4 * width * y,
88                         (uint8_t *)src_row + src_stride * y,
89                         width);
90    }
91    compress_rgba_unorm(width, height,
92                        temp_block, width * 4 * sizeof(uint8_t),
93                        dst_row, dst_stride);
94    free((void *) temp_block);
95 }
96 
97 void
util_format_bptc_rgba_unorm_fetch_rgba(void * restrict dst,const uint8_t * restrict src,unsigned width,unsigned height)98 util_format_bptc_rgba_unorm_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
99                                              unsigned width, unsigned height)
100 {
101    uint8_t temp_block[4];
102 
103    fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
104                                temp_block, (width % 4) + (height % 4) * 4);
105 
106    util_format_read_4(PIPE_FORMAT_R8G8B8A8_UNORM,
107                       dst, 4 * sizeof(float),
108                       temp_block, 4 * sizeof(uint8_t),
109                       0, 0, 1, 1);
110 }
111 
112 void
util_format_bptc_srgba_unpack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)113 util_format_bptc_srgba_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
114                                           const uint8_t *restrict src_row, unsigned src_stride,
115                                           unsigned width, unsigned height)
116 {
117    decompress_rgba_unorm(width, height,
118                          src_row, src_stride,
119                          dst_row, dst_stride);
120 }
121 
122 void
util_format_bptc_srgba_pack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)123 util_format_bptc_srgba_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
124                                         const uint8_t *restrict src_row, unsigned src_stride,
125                                         unsigned width, unsigned height)
126 {
127    compress_rgba_unorm(width, height,
128                        src_row, src_stride,
129                        dst_row, dst_stride);
130 }
131 
132 void
util_format_bptc_srgba_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)133 util_format_bptc_srgba_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
134                                          const uint8_t *restrict src_row, unsigned src_stride,
135                                          unsigned width, unsigned height)
136 {
137    uint8_t *temp_block;
138    temp_block = malloc(width * height * 4 * sizeof(uint8_t));
139    decompress_rgba_unorm(width, height,
140                          src_row, src_stride,
141                          temp_block, width * 4 * sizeof(uint8_t));
142 
143    /* Direct call to row unpack instead of util_format_rgba_unpack_rect()
144     * to avoid table lookup that would pull in all unpack symbols.
145     */
146    for (int y = 0; y < height; y++) {
147       util_format_r8g8b8a8_srgb_unpack_rgba_float((char *)dst_row + dst_stride * y,
148                                                   temp_block + width * 4 * y,
149                                                   width);
150    }
151 
152    free((void *) temp_block);
153 }
154 
155 void
util_format_bptc_srgba_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)156 util_format_bptc_srgba_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
157                                        const float *restrict src_row, unsigned src_stride,
158                                        unsigned width, unsigned height)
159 {
160    compress_rgb_float(width, height,
161                       src_row, src_stride,
162                       dst_row, dst_stride,
163                       true);
164 }
165 
166 void
util_format_bptc_srgba_fetch_rgba(void * restrict dst,const uint8_t * restrict src,unsigned width,unsigned height)167 util_format_bptc_srgba_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
168                                         unsigned width, unsigned height)
169 {
170    uint8_t temp_block[4];
171 
172    fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
173                                temp_block, (width % 4) + (height % 4) * 4);
174    util_format_r8g8b8a8_srgb_fetch_rgba(dst, temp_block, 0, 0);
175 }
176 
177 void
util_format_bptc_rgb_float_unpack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)178 util_format_bptc_rgb_float_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
179                                               const uint8_t *restrict src_row, unsigned src_stride,
180                                               unsigned width, unsigned height)
181 {
182    float *temp_block;
183    temp_block = malloc(width * height * 4 * sizeof(float));
184    decompress_rgb_float(width, height,
185                         src_row, src_stride,
186                         temp_block, width * 4 * sizeof(float),
187                         true);
188    /* Direct call to row unpack instead of util_format_rgba_unpack_rect()
189     * to avoid table lookup that would pull in all unpack symbols.
190     */
191    for (int y = 0; y < height; y++) {
192       util_format_r32g32b32a32_float_unpack_rgba_8unorm(
193           dst_row + dst_stride * y,
194           (const uint8_t *)temp_block + width * 4 * sizeof(float) * y,
195           width);
196    }
197    free((void *) temp_block);
198 }
199 
200 void
util_format_bptc_rgb_float_pack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)201 util_format_bptc_rgb_float_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
202                                             const uint8_t *restrict src_row, unsigned src_stride,
203                                             unsigned width, unsigned height)
204 {
205    compress_rgba_unorm(width, height,
206                        src_row, src_stride,
207                        dst_row, dst_stride);
208 }
209 
210 void
util_format_bptc_rgb_float_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)211 util_format_bptc_rgb_float_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
212                                              const uint8_t *restrict src_row, unsigned src_stride,
213                                              unsigned width, unsigned height)
214 {
215    decompress_rgb_float(width, height,
216                         src_row, src_stride,
217                         dst_row, dst_stride,
218                         true);
219 }
220 
221 void
util_format_bptc_rgb_float_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)222 util_format_bptc_rgb_float_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
223                                            const float *restrict src_row, unsigned src_stride,
224                                            unsigned width, unsigned height)
225 {
226    compress_rgb_float(width, height,
227                       src_row, src_stride,
228                       dst_row, dst_stride,
229                       true);
230 }
231 
232 void
util_format_bptc_rgb_float_fetch_rgba(void * restrict dst,const uint8_t * restrict src,unsigned width,unsigned height)233 util_format_bptc_rgb_float_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
234                                             unsigned width, unsigned height)
235 {
236    fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
237                               dst, (width % 4) + (height % 4) * 4, true);
238 }
239 
240 void
util_format_bptc_rgb_ufloat_unpack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)241 util_format_bptc_rgb_ufloat_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
242                                                const uint8_t *restrict src_row, unsigned src_stride,
243                                                unsigned width, unsigned height)
244 {
245    float *temp_block;
246    temp_block = malloc(width * height * 4 * sizeof(float));
247    decompress_rgb_float(width, height,
248                         src_row, src_stride,
249                         temp_block, width * 4 * sizeof(float),
250                         false);
251    /* Direct call to row unpack instead of util_format_rgba_unpack_8unorm()
252     * to avoid table lookup that would pull in all unpack symbols.
253     */
254    for (int y = 0; y < height; y++) {
255       util_format_r32g32b32a32_float_unpack_rgba_8unorm(dst_row + dst_stride * y,
256                                                         (void *)(temp_block + 4 * width * y),
257                                                         width);
258    }
259    free((void *) temp_block);
260 }
261 
262 void
util_format_bptc_rgb_ufloat_pack_rgba_8unorm(uint8_t * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)263 util_format_bptc_rgb_ufloat_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
264                                              const uint8_t *restrict src_row, unsigned src_stride,
265                                              unsigned width, unsigned height)
266 {
267    compress_rgba_unorm(width, height,
268                        src_row, src_stride,
269                        dst_row, dst_stride);
270 }
271 
272 void
util_format_bptc_rgb_ufloat_unpack_rgba_float(void * restrict dst_row,unsigned dst_stride,const uint8_t * restrict src_row,unsigned src_stride,unsigned width,unsigned height)273 util_format_bptc_rgb_ufloat_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
274                                               const uint8_t *restrict src_row, unsigned src_stride,
275                                               unsigned width, unsigned height)
276 {
277    decompress_rgb_float(width, height,
278                         src_row, src_stride,
279                         dst_row, dst_stride,
280                         false);
281 }
282 
283 void
util_format_bptc_rgb_ufloat_pack_rgba_float(uint8_t * restrict dst_row,unsigned dst_stride,const float * restrict src_row,unsigned src_stride,unsigned width,unsigned height)284 util_format_bptc_rgb_ufloat_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
285                                             const float *restrict src_row, unsigned src_stride,
286                                             unsigned width, unsigned height)
287 {
288    compress_rgb_float(width, height,
289                       src_row, src_stride,
290                       dst_row, dst_stride,
291                       false);
292 }
293 
294 void
util_format_bptc_rgb_ufloat_fetch_rgba(void * restrict dst,const uint8_t * restrict src,unsigned width,unsigned height)295 util_format_bptc_rgb_ufloat_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
296                                              unsigned width, unsigned height)
297 {
298    fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
299                               dst, (width % 4) + (height % 4) * 4, false);
300 }
301