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 #define BPTC_BLOCK_DECODE
33 #include "../../mesa/main/texcompress_bptc_tmp.h"
34 
35 void
util_format_bptc_rgba_unorm_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)36 util_format_bptc_rgba_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
37                                                const uint8_t *src_row, unsigned src_stride,
38                                                unsigned width, unsigned height)
39 {
40   decompress_rgba_unorm(width, height,
41                         src_row, src_stride,
42                         dst_row, dst_stride);
43 }
44 
45 void
util_format_bptc_rgba_unorm_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)46 util_format_bptc_rgba_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
47                                              const uint8_t *src_row, unsigned src_stride,
48                                              unsigned width, unsigned height)
49 {
50    compress_rgba_unorm(width, height,
51                        src_row, src_stride,
52                        dst_row, dst_stride);
53 }
54 
55 void
util_format_bptc_rgba_unorm_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)56 util_format_bptc_rgba_unorm_unpack_rgba_float(void *dst_row, unsigned dst_stride,
57                                               const uint8_t *src_row, unsigned src_stride,
58                                               unsigned width, unsigned height)
59 {
60    uint8_t *temp_block;
61    temp_block = malloc(width * height * 4 * sizeof(uint8_t));
62    decompress_rgba_unorm(width, height,
63                          src_row, src_stride,
64                          temp_block, width * 4 * sizeof(uint8_t));
65    util_format_r8g8b8a8_unorm_unpack_rgba_float(
66                       dst_row, dst_stride,
67                       temp_block, width * 4 * sizeof(uint8_t),
68                       width, height);
69    free((void *) temp_block);
70 }
71 
72 void
util_format_bptc_rgba_unorm_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)73 util_format_bptc_rgba_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
74                                             const float *src_row, unsigned src_stride,
75                                             unsigned width, unsigned height)
76 {
77    uint8_t *temp_block;
78    temp_block = malloc(width * height * 4 * sizeof(uint8_t));
79    util_format_r32g32b32a32_float_unpack_rgba_8unorm(
80                         temp_block, width * 4 * sizeof(uint8_t),
81                         (uint8_t *)src_row, src_stride,
82                         width, height);
83    compress_rgba_unorm(width, height,
84                        temp_block, width * 4 * sizeof(uint8_t),
85                        dst_row, dst_stride);
86    free((void *) temp_block);
87 }
88 
89 void
util_format_bptc_rgba_unorm_fetch_rgba(void * dst,const uint8_t * src,unsigned width,unsigned height)90 util_format_bptc_rgba_unorm_fetch_rgba(void *dst, const uint8_t *src,
91                                              unsigned width, unsigned height)
92 {
93    uint8_t temp_block[4];
94 
95    fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
96                                temp_block, (width % 4) + (height % 4) * 4);
97 
98    util_format_read_4(PIPE_FORMAT_R8G8B8A8_UNORM,
99                       dst, 4 * sizeof(float),
100                       temp_block, 4 * sizeof(uint8_t),
101                       0, 0, 1, 1);
102 }
103 
104 void
util_format_bptc_srgba_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)105 util_format_bptc_srgba_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
106                                           const uint8_t *src_row, unsigned src_stride,
107                                           unsigned width, unsigned height)
108 {
109    decompress_rgba_unorm(width, height,
110                          src_row, src_stride,
111                          dst_row, dst_stride);
112 }
113 
114 void
util_format_bptc_srgba_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)115 util_format_bptc_srgba_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
116                                         const uint8_t *src_row, unsigned src_stride,
117                                         unsigned width, unsigned height)
118 {
119    compress_rgba_unorm(width, height,
120                        src_row, src_stride,
121                        dst_row, dst_stride);
122 }
123 
124 void
util_format_bptc_srgba_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)125 util_format_bptc_srgba_unpack_rgba_float(void *dst_row, unsigned dst_stride,
126                                          const uint8_t *src_row, unsigned src_stride,
127                                          unsigned width, unsigned height)
128 {
129    uint8_t *temp_block;
130    temp_block = malloc(width * height * 4 * sizeof(uint8_t));
131    decompress_rgba_unorm(width, height,
132                          src_row, src_stride,
133                          temp_block, width * 4 * sizeof(uint8_t));
134    util_format_r8g8b8a8_srgb_unpack_rgba_float(dst_row, dst_stride,
135                                                temp_block, width * 4 * sizeof(uint8_t),
136                                                width, height);
137 
138    free((void *) temp_block);
139 }
140 
141 void
util_format_bptc_srgba_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)142 util_format_bptc_srgba_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
143                                        const float *src_row, unsigned src_stride,
144                                        unsigned width, unsigned height)
145 {
146    compress_rgb_float(width, height,
147                       src_row, src_stride,
148                       dst_row, dst_stride,
149                       true);
150 }
151 
152 void
util_format_bptc_srgba_fetch_rgba(void * dst,const uint8_t * src,unsigned width,unsigned height)153 util_format_bptc_srgba_fetch_rgba(void *dst, const uint8_t *src,
154                                         unsigned width, unsigned height)
155 {
156    uint8_t temp_block[4];
157 
158    fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
159                                temp_block, (width % 4) + (height % 4) * 4);
160    util_format_r8g8b8a8_srgb_fetch_rgba(dst, temp_block, 0, 0);
161 }
162 
163 void
util_format_bptc_rgb_float_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)164 util_format_bptc_rgb_float_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
165                                               const uint8_t *src_row, unsigned src_stride,
166                                               unsigned width, unsigned height)
167 {
168    float *temp_block;
169    temp_block = malloc(width * height * 4 * sizeof(float));
170    decompress_rgb_float(width, height,
171                         src_row, src_stride,
172                         temp_block, width * 4 * sizeof(float),
173                         true);
174    util_format_r32g32b32a32_float_unpack_rgba_8unorm(
175                         dst_row, dst_stride,
176                         (const uint8_t *)temp_block, width * 4 * sizeof(float),
177                         width, height);
178    free((void *) temp_block);
179 }
180 
181 void
util_format_bptc_rgb_float_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)182 util_format_bptc_rgb_float_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
183                                             const uint8_t *src_row, unsigned src_stride,
184                                             unsigned width, unsigned height)
185 {
186    compress_rgba_unorm(width, height,
187                        src_row, src_stride,
188                        dst_row, dst_stride);
189 }
190 
191 void
util_format_bptc_rgb_float_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)192 util_format_bptc_rgb_float_unpack_rgba_float(void *dst_row, unsigned dst_stride,
193                                              const uint8_t *src_row, unsigned src_stride,
194                                              unsigned width, unsigned height)
195 {
196    decompress_rgb_float(width, height,
197                         src_row, src_stride,
198                         dst_row, dst_stride,
199                         true);
200 }
201 
202 void
util_format_bptc_rgb_float_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)203 util_format_bptc_rgb_float_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
204                                            const float *src_row, unsigned src_stride,
205                                            unsigned width, unsigned height)
206 {
207    compress_rgb_float(width, height,
208                       src_row, src_stride,
209                       dst_row, dst_stride,
210                       true);
211 }
212 
213 void
util_format_bptc_rgb_float_fetch_rgba(void * dst,const uint8_t * src,unsigned width,unsigned height)214 util_format_bptc_rgb_float_fetch_rgba(void *dst, const uint8_t *src,
215                                             unsigned width, unsigned height)
216 {
217    fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
218                               dst, (width % 4) + (height % 4) * 4, true);
219 }
220 
221 void
util_format_bptc_rgb_ufloat_unpack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)222 util_format_bptc_rgb_ufloat_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
223                                                const uint8_t *src_row, unsigned src_stride,
224                                                unsigned width, unsigned height)
225 {
226    float *temp_block;
227    temp_block = malloc(width * height * 4 * sizeof(float));
228    decompress_rgb_float(width, height,
229                         src_row, src_stride,
230                         temp_block, width * 4 * sizeof(float),
231                         false);
232    util_format_r32g32b32a32_float_unpack_rgba_8unorm(
233                         dst_row, dst_stride,
234                         (const uint8_t *)temp_block, width * 4 * sizeof(float),
235                         width, height);
236    free((void *) temp_block);
237 }
238 
239 void
util_format_bptc_rgb_ufloat_pack_rgba_8unorm(uint8_t * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)240 util_format_bptc_rgb_ufloat_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
241                                              const uint8_t *src_row, unsigned src_stride,
242                                              unsigned width, unsigned height)
243 {
244    compress_rgba_unorm(width, height,
245                        src_row, src_stride,
246                        dst_row, dst_stride);
247 }
248 
249 void
util_format_bptc_rgb_ufloat_unpack_rgba_float(void * dst_row,unsigned dst_stride,const uint8_t * src_row,unsigned src_stride,unsigned width,unsigned height)250 util_format_bptc_rgb_ufloat_unpack_rgba_float(void *dst_row, unsigned dst_stride,
251                                               const uint8_t *src_row, unsigned src_stride,
252                                               unsigned width, unsigned height)
253 {
254    decompress_rgb_float(width, height,
255                         src_row, src_stride,
256                         dst_row, dst_stride,
257                         false);
258 }
259 
260 void
util_format_bptc_rgb_ufloat_pack_rgba_float(uint8_t * dst_row,unsigned dst_stride,const float * src_row,unsigned src_stride,unsigned width,unsigned height)261 util_format_bptc_rgb_ufloat_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
262                                             const float *src_row, unsigned src_stride,
263                                             unsigned width, unsigned height)
264 {
265    compress_rgb_float(width, height,
266                       src_row, src_stride,
267                       dst_row, dst_stride,
268                       false);
269 }
270 
271 void
util_format_bptc_rgb_ufloat_fetch_rgba(void * dst,const uint8_t * src,unsigned width,unsigned height)272 util_format_bptc_rgb_ufloat_fetch_rgba(void *dst, const uint8_t *src,
273                                              unsigned width, unsigned height)
274 {
275    fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
276                               dst, (width % 4) + (height % 4) * 4, false);
277 }
278