Path: blob/21.2-virgl/src/util/format/u_format_latc.c
7188 views
/**************************************************************************1*2* Copyright (C) 2011 Red Hat Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included12* in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS15* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR18* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR20* OTHER DEALINGS IN THE SOFTWARE.21*22**************************************************************************/2324#include <stdio.h>25#include "util/format/u_format.h"26#include "util/format/u_format_rgtc.h"27#include "util/format/u_format_latc.h"28#include "util/rgtc.h"29#include "util/u_math.h"3031void32util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)33{34/* Fix warnings here: */35(void) util_format_unsigned_encode_rgtc_ubyte;36(void) util_format_signed_encode_rgtc_ubyte;3738util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);39dst[1] = dst[0];40dst[2] = dst[0];41dst[3] = 255;42}4344void45util_format_latc1_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)46{47util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);48}4950void51util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row,52unsigned src_stride, unsigned width, unsigned height)53{54util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);55}5657void58util_format_latc1_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)59{60unsigned x, y, i, j;61int block_size = 8;6263for(y = 0; y < height; y += 4) {64const uint8_t *src = src_row;65for(x = 0; x < width; x += 4) {66for(j = 0; j < 4; ++j) {67for(i = 0; i < 4; ++i) {68float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);69uint8_t tmp_r;70util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);71dst[0] =72dst[1] =73dst[2] = ubyte_to_float(tmp_r);74dst[3] = 1.0;75}76}77src += block_size;78}79src_row += src_stride;80}81}8283void84util_format_latc1_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)85{86util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);87}8889void90util_format_latc1_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)91{92float *dst = in_dst;93uint8_t tmp_r;9495util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);96dst[0] =97dst[1] =98dst[2] = ubyte_to_float(tmp_r);99dst[3] = 1.0;100}101102void103util_format_latc1_snorm_fetch_rgba_8unorm(UNUSED uint8_t *restrict dst, UNUSED const uint8_t *restrict src,104UNUSED unsigned i, UNUSED unsigned j)105{106fprintf(stderr,"%s\n", __func__);107}108109void110util_format_latc1_snorm_unpack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,111UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,112UNUSED unsigned width, UNUSED unsigned height)113{114fprintf(stderr,"%s\n", __func__);115}116117void118util_format_latc1_snorm_pack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,119UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,120UNUSED unsigned width, UNUSED unsigned height)121{122fprintf(stderr,"%s\n", __func__);123}124125void126util_format_latc1_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)127{128util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);129}130131void132util_format_latc1_snorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)133{134unsigned x, y, i, j;135int block_size = 8;136137for(y = 0; y < height; y += 4) {138const int8_t *src = (int8_t *)src_row;139for(x = 0; x < width; x += 4) {140for(j = 0; j < 4; ++j) {141for(i = 0; i < 4; ++i) {142float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);143int8_t tmp_r;144util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);145dst[0] =146dst[1] =147dst[2] = byte_to_float_tex(tmp_r);148dst[3] = 1.0;149}150}151src += block_size;152}153src_row += src_stride;154}155}156157void158util_format_latc1_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)159{160float *dst = in_dst;161int8_t tmp_r;162163util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);164dst[0] =165dst[1] =166dst[2] = byte_to_float_tex(tmp_r);167dst[3] = 1.0;168}169170171void172util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)173{174util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);175dst[1] = dst[0];176dst[2] = dst[0];177util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 3, 2);178}179180void181util_format_latc2_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)182{183util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);184}185186void187util_format_latc2_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)188{189util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);190}191192void193util_format_latc2_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)194{195util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);196}197198void199util_format_latc2_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)200{201unsigned x, y, i, j;202int block_size = 16;203204for(y = 0; y < height; y += 4) {205const uint8_t *src = src_row;206for(x = 0; x < width; x += 4) {207for(j = 0; j < 4; ++j) {208for(i = 0; i < 4; ++i) {209float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);210uint8_t tmp_r, tmp_g;211util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);212util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);213dst[0] =214dst[1] =215dst[2] = ubyte_to_float(tmp_r);216dst[3] = ubyte_to_float(tmp_g);217}218}219src += block_size;220}221src_row += src_stride;222}223}224225void226util_format_latc2_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)227{228float *dst = in_dst;229uint8_t tmp_r, tmp_g;230231util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);232util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);233dst[0] =234dst[1] =235dst[2] = ubyte_to_float(tmp_r);236dst[3] = ubyte_to_float(tmp_g);237}238239240void241util_format_latc2_snorm_fetch_rgba_8unorm(UNUSED uint8_t *restrict dst, UNUSED const uint8_t *restrict src,242UNUSED unsigned i, UNUSED unsigned j)243{244fprintf(stderr,"%s\n", __func__);245}246247void248util_format_latc2_snorm_unpack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,249UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,250UNUSED unsigned width, UNUSED unsigned height)251{252fprintf(stderr,"%s\n", __func__);253}254255void256util_format_latc2_snorm_pack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,257UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,258UNUSED unsigned width, UNUSED unsigned height)259{260fprintf(stderr,"%s\n", __func__);261}262263void264util_format_latc2_snorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)265{266unsigned x, y, i, j;267int block_size = 16;268269for(y = 0; y < height; y += 4) {270const int8_t *src = (int8_t *)src_row;271for(x = 0; x < width; x += 4) {272for(j = 0; j < 4; ++j) {273for(i = 0; i < 4; ++i) {274float *dst = (float *)(uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16;275int8_t tmp_r, tmp_g;276util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);277util_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);278dst[0] =279dst[1] =280dst[2] = byte_to_float_tex(tmp_r);281dst[3] = byte_to_float_tex(tmp_g);282}283}284src += block_size;285}286src_row += src_stride;287}288}289290void291util_format_latc2_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)292{293util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);294}295296void297util_format_latc2_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)298{299float *dst = in_dst;300int8_t tmp_r, tmp_g;301302util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);303util_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2);304dst[0] =305dst[1] =306dst[2] = byte_to_float_tex(tmp_r);307dst[3] = byte_to_float_tex(tmp_g);308}309310311312