u_format_rgtc.c revision 8d62b2aca99ba67f794dd682ed1ec49dc8826390
1/************************************************************************** 2 * 3 * Copyright (C) 2011 Red Hat Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included 13 * in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 * 22 **************************************************************************/ 23 24#include <stdio.h> 25#include "u_math.h" 26#include "u_format.h" 27#include "u_format_rgtc.h" 28 29static void u_format_unsigned_encode_rgtc_chan(uint8_t *blkaddr, uint8_t srccolors[4][4], 30 int numxpixels, int numypixels); 31 32static void u_format_unsigned_fetch_texel_rgtc(unsigned srcRowStride, const uint8_t *pixdata, 33 unsigned i, unsigned j, uint8_t *value, unsigned comps); 34 35static void u_format_signed_encode_rgtc_chan(int8_t *blkaddr, int8_t srccolors[4][4], 36 int numxpixels, int numypixels); 37 38static void u_format_signed_fetch_texel_rgtc(unsigned srcRowStride, const int8_t *pixdata, 39 unsigned i, unsigned j, int8_t *value, unsigned comps); 40 41void 42util_format_rgtc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 43{ 44 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1); 45} 46 47void 48util_format_rgtc1_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 49{ 50 const unsigned bw = 4, bh = 4, comps = 4; 51 unsigned x, y, i, j; 52 unsigned block_size = 8; 53 54 for(y = 0; y < height; y += bh) { 55 const uint8_t *src = src_row; 56 for(x = 0; x < width; x += bw) { 57 for(j = 0; j < bh; ++j) { 58 for(i = 0; i < bw; ++i) { 59 uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*comps; 60 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1); 61 } 62 } 63 src += block_size; 64 } 65 src_row += src_stride; 66 } 67} 68 69void 70util_format_rgtc1_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, 71 unsigned src_stride, unsigned width, unsigned height) 72{ 73 const unsigned bw = 4, bh = 4, bytes_per_block = 8; 74 unsigned x, y, i, j; 75 76 for(y = 0; y < height; y += bh) { 77 uint8_t *dst = dst_row; 78 for(x = 0; x < width; x += bw) { 79 uint8_t tmp[4][4]; /* [bh][bw][comps] */ 80 for(j = 0; j < bh; ++j) { 81 for(i = 0; i < bw; ++i) { 82 tmp[j][i] = src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]; 83 } 84 } 85 u_format_unsigned_encode_rgtc_chan(dst, tmp, 4, 4); 86 dst += bytes_per_block; 87 } 88 dst_row += dst_stride / sizeof(*dst_row); 89 } 90} 91 92void 93util_format_rgtc1_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 94{ 95 unsigned x, y, i, j; 96 int block_size = 8; 97 for(y = 0; y < height; y += 4) { 98 const uint8_t *src = src_row; 99 for(x = 0; x < width; x += 4) { 100 for(j = 0; j < 4; ++j) { 101 for(i = 0; i < 4; ++i) { 102 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4; 103 uint8_t tmp_r; 104 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1); 105 dst[0] = ubyte_to_float(tmp_r); 106 dst[1] = 0.0; 107 dst[2] = 0.0; 108 dst[3] = 1.0; 109 } 110 } 111 src += block_size; 112 } 113 src_row += src_stride; 114 } 115} 116 117void 118util_format_rgtc1_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height) 119{ 120 const unsigned bw = 4, bh = 4, bytes_per_block = 8; 121 unsigned x, y, i, j; 122 123 for(y = 0; y < height; y += bh) { 124 uint8_t *dst = dst_row; 125 for(x = 0; x < width; x += bw) { 126 uint8_t tmp[4][4]; /* [bh][bw][comps] */ 127 for(j = 0; j < bh; ++j) { 128 for(i = 0; i < bw; ++i) { 129 tmp[j][i] = float_to_ubyte(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]); 130 } 131 } 132 u_format_unsigned_encode_rgtc_chan(dst, tmp, 4, 4); 133 dst += bytes_per_block; 134 } 135 dst_row += dst_stride / sizeof(*dst_row); 136 } 137} 138 139void 140util_format_rgtc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 141{ 142 uint8_t tmp_r; 143 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1); 144 dst[0] = ubyte_to_float(tmp_r); 145 dst[1] = 0.0; 146 dst[2] = 0.0; 147 dst[3] = 1.0; 148} 149 150void 151util_format_rgtc1_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 152{ 153 fprintf(stderr,"%s\n", __func__); 154} 155 156void 157util_format_rgtc1_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 158{ 159 fprintf(stderr,"%s\n", __func__); 160} 161 162void 163util_format_rgtc1_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 164{ 165 fprintf(stderr,"%s\n", __func__); 166} 167 168void 169util_format_rgtc1_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height) 170{ 171 const unsigned bw = 4, bh = 4, bytes_per_block = 8; 172 unsigned x, y, i, j; 173 174 for(y = 0; y < height; y += bh) { 175 int8_t *dst = (int8_t *)dst_row; 176 for(x = 0; x < width; x += bw) { 177 int8_t tmp[4][4]; /* [bh][bw][comps] */ 178 for(j = 0; j < bh; ++j) { 179 for(i = 0; i < bw; ++i) { 180 tmp[j][i] = float_to_byte_tex(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]); 181 } 182 } 183 u_format_signed_encode_rgtc_chan(dst, tmp, 4, 4); 184 dst += bytes_per_block; 185 } 186 dst_row += dst_stride / sizeof(*dst_row); 187 } 188} 189 190void 191util_format_rgtc1_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 192{ 193 unsigned x, y, i, j; 194 int block_size = 8; 195 for(y = 0; y < height; y += 4) { 196 const int8_t *src = (int8_t *)src_row; 197 for(x = 0; x < width; x += 4) { 198 for(j = 0; j < 4; ++j) { 199 for(i = 0; i < 4; ++i) { 200 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4; 201 int8_t tmp_r; 202 u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1); 203 dst[0] = byte_to_float_tex(tmp_r); 204 dst[1] = 0.0; 205 dst[2] = 0.0; 206 dst[3] = 1.0; 207 } 208 } 209 src += block_size; 210 } 211 src_row += src_stride; 212 } 213} 214 215void 216util_format_rgtc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 217{ 218 int8_t tmp_r; 219 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1); 220 dst[0] = byte_to_float_tex(tmp_r); 221 dst[1] = 0.0; 222 dst[2] = 0.0; 223 dst[3] = 1.0; 224} 225 226 227void 228util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 229{ 230 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2); 231 u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 1, 2); 232} 233 234void 235util_format_rgtc2_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 236{ 237 const unsigned bw = 4, bh = 4, comps = 4; 238 unsigned x, y, i, j; 239 unsigned block_size = 16; 240 241 for(y = 0; y < height; y += bh) { 242 const uint8_t *src = src_row; 243 for(x = 0; x < width; x += bw) { 244 for(j = 0; j < bh; ++j) { 245 for(i = 0; i < bw; ++i) { 246 uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*comps; 247 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2); 248 u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 1, 2); 249 250 } 251 } 252 src += block_size; 253 } 254 src_row += src_stride; 255 } 256} 257 258void 259util_format_rgtc2_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 260{ 261 const unsigned bw = 4, bh = 4, bytes_per_block = 16; 262 unsigned x, y, i, j; 263 264 for(y = 0; y < height; y += bh) { 265 uint8_t *dst = dst_row; 266 for(x = 0; x < width; x += bw) { 267 uint8_t tmp_r[4][4]; /* [bh][bw] */ 268 uint8_t tmp_g[4][4]; /* [bh][bw] */ 269 for(j = 0; j < bh; ++j) { 270 for(i = 0; i < bw; ++i) { 271 tmp_r[j][i] = src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]; 272 tmp_g[j][i] = src_row[((y + j)*src_stride/sizeof(*src_row) + (x + i)*4) + 1]; 273 } 274 } 275 u_format_unsigned_encode_rgtc_chan(dst, tmp_r, 4, 4); 276 u_format_unsigned_encode_rgtc_chan(dst + 8, tmp_g, 4, 4); 277 dst += bytes_per_block; 278 } 279 dst_row += dst_stride / sizeof(*dst_row); 280 } 281} 282 283void 284util_format_rgtc2_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height) 285{ 286 const unsigned bw = 4, bh = 4, bytes_per_block = 16; 287 unsigned x, y, i, j; 288 289 for(y = 0; y < height; y += bh) { 290 uint8_t *dst = dst_row; 291 for(x = 0; x < width; x += bw) { 292 uint8_t tmp_r[4][4]; /* [bh][bw][comps] */ 293 uint8_t tmp_g[4][4]; /* [bh][bw][comps] */ 294 for(j = 0; j < bh; ++j) { 295 for(i = 0; i < bw; ++i) { 296 tmp_r[j][i] = float_to_ubyte(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]); 297 tmp_g[j][i] = float_to_ubyte(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4 + 1]); 298 } 299 } 300 u_format_unsigned_encode_rgtc_chan(dst, tmp_r, 4, 4); 301 u_format_unsigned_encode_rgtc_chan(dst + 8, tmp_g, 4, 4); 302 dst += bytes_per_block; 303 } 304 dst_row += dst_stride / sizeof(*dst_row); 305 } 306} 307 308void 309util_format_rgtc2_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 310{ 311 unsigned x, y, i, j; 312 int block_size = 16; 313 for(y = 0; y < height; y += 4) { 314 const uint8_t *src = src_row; 315 for(x = 0; x < width; x += 4) { 316 for(j = 0; j < 4; ++j) { 317 for(i = 0; i < 4; ++i) { 318 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4; 319 uint8_t tmp_r, tmp_g; 320 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2); 321 u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2); 322 dst[0] = ubyte_to_float(tmp_r); 323 dst[1] = ubyte_to_float(tmp_g); 324 dst[2] = 0.0; 325 dst[3] = 1.0; 326 } 327 } 328 src += block_size; 329 } 330 src_row += src_stride; 331 } 332} 333 334void 335util_format_rgtc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 336{ 337 uint8_t tmp_r, tmp_g; 338 u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2); 339 u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2); 340 dst[0] = ubyte_to_float(tmp_r); 341 dst[1] = ubyte_to_float(tmp_g); 342 dst[2] = 0.0; 343 dst[3] = 1.0; 344} 345 346 347void 348util_format_rgtc2_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 349{ 350 fprintf(stderr,"%s\n", __func__); 351} 352 353void 354util_format_rgtc2_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 355{ 356 fprintf(stderr,"%s\n", __func__); 357} 358 359void 360util_format_rgtc2_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 361{ 362 fprintf(stderr,"%s\n", __func__); 363} 364 365void 366util_format_rgtc2_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height) 367{ 368 unsigned x, y, i, j; 369 int block_size = 16; 370 for(y = 0; y < height; y += 4) { 371 const int8_t *src = (int8_t *)src_row; 372 for(x = 0; x < width; x += 4) { 373 for(j = 0; j < 4; ++j) { 374 for(i = 0; i < 4; ++i) { 375 float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4; 376 int8_t tmp_r, tmp_g; 377 u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2); 378 u_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2); 379 dst[0] = byte_to_float_tex(tmp_r); 380 dst[1] = byte_to_float_tex(tmp_g); 381 dst[2] = 0.0; 382 dst[3] = 1.0; 383 } 384 } 385 src += block_size; 386 } 387 src_row += src_stride; 388 } 389} 390 391void 392util_format_rgtc2_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height) 393{ 394 const unsigned bw = 4, bh = 4, bytes_per_block = 16; 395 unsigned x, y, i, j; 396 397 for(y = 0; y < height; y += bh) { 398 int8_t *dst = (int8_t *)dst_row; 399 for(x = 0; x < width; x += bw) { 400 int8_t tmp_r[4][4]; /* [bh][bw][comps] */ 401 int8_t tmp_g[4][4]; /* [bh][bw][comps] */ 402 for(j = 0; j < bh; ++j) { 403 for(i = 0; i < bw; ++i) { 404 tmp_r[j][i] = float_to_byte_tex(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]); 405 tmp_g[j][i] = float_to_byte_tex(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4 + 1]); 406 } 407 } 408 u_format_signed_encode_rgtc_chan(dst, tmp_r, 4, 4); 409 u_format_signed_encode_rgtc_chan(dst + 8, tmp_g, 4, 4); 410 dst += bytes_per_block; 411 } 412 dst_row += dst_stride / sizeof(*dst_row); 413 } 414} 415 416void 417util_format_rgtc2_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 418{ 419 int8_t tmp_r, tmp_g; 420 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2); 421 u_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2); 422 dst[0] = byte_to_float_tex(tmp_r); 423 dst[1] = byte_to_float_tex(tmp_g); 424 dst[2] = 0.0; 425 dst[3] = 1.0; 426} 427 428 429#define TAG(x) u_format_unsigned_##x 430#define TYPE uint8_t 431#define T_MIN 0 432#define T_MAX 255 433 434#include "../../../mesa/main/texcompress_rgtc_tmp.h" 435 436#undef TYPE 437#undef TAG 438#undef T_MIN 439#undef T_MAX 440 441 442#define TAG(x) u_format_signed_##x 443#define TYPE int8_t 444#define T_MIN (int8_t)-128 445#define T_MAX (int8_t)127 446 447#include "../../../mesa/main/texcompress_rgtc_tmp.h" 448 449#undef TYPE 450#undef TAG 451#undef T_MIN 452#undef T_MAX 453