Path: blob/21.2-virgl/src/util/format/u_format_rgtc.c
7233 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/u_math.h"28#include "util/rgtc.h"2930void31util_format_rgtc1_unorm_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)32{33util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);34dst[1] = 0;35dst[2] = 0;36dst[3] = 255;37}3839void40util_format_rgtc1_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)41{42const unsigned bw = 4, bh = 4, comps = 4;43unsigned x, y, i, j;44unsigned block_size = 8;4546for(y = 0; y < height; y += bh) {47const uint8_t *src = src_row;48for(x = 0; x < width; x += bw) {49for(j = 0; j < bh; ++j) {50for(i = 0; i < bw; ++i) {51uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*comps;52util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);53dst[1] = 0;54dst[2] = 0;55dst[3] = 255;56}57}58src += block_size;59}60src_row += src_stride;61}62}6364void65util_format_rgtc1_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row,66unsigned src_stride, unsigned width, unsigned height)67{68const unsigned bw = 4, bh = 4, bytes_per_block = 8;69unsigned x, y, i, j;7071for(y = 0; y < height; y += bh) {72uint8_t *dst = dst_row;73for(x = 0; x < width; x += bw) {74uint8_t tmp[4][4]; /* [bh][bw][comps] */75for(j = 0; j < bh; ++j) {76for(i = 0; i < bw; ++i) {77tmp[j][i] = src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4];78}79}80util_format_unsigned_encode_rgtc_ubyte(dst, tmp, 4, 4);81dst += bytes_per_block;82}83dst_row += dst_stride / sizeof(*dst_row);84}85}8687void88util_format_rgtc1_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)89{90unsigned x, y, i, j;91int block_size = 8;92for(y = 0; y < height; y += 4) {93const uint8_t *src = src_row;94for(x = 0; x < width; x += 4) {95for(j = 0; j < 4; ++j) {96for(i = 0; i < 4; ++i) {97float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);98uint8_t tmp_r;99util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);100dst[0] = ubyte_to_float(tmp_r);101dst[1] = 0.0;102dst[2] = 0.0;103dst[3] = 1.0;104}105}106src += block_size;107}108src_row += src_stride;109}110}111112void113util_format_rgtc1_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)114{115const unsigned bw = 4, bh = 4, bytes_per_block = 8;116unsigned x, y, i, j;117118for(y = 0; y < height; y += bh) {119uint8_t *dst = dst_row;120for(x = 0; x < width; x += bw) {121uint8_t tmp[4][4]; /* [bh][bw][comps] */122for(j = 0; j < bh; ++j) {123for(i = 0; i < bw; ++i) {124tmp[j][i] = float_to_ubyte(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]);125}126}127util_format_unsigned_encode_rgtc_ubyte(dst, tmp, 4, 4);128dst += bytes_per_block;129}130dst_row += dst_stride / sizeof(*dst_row);131}132}133134void135util_format_rgtc1_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)136{137float *dst = in_dst;138uint8_t tmp_r;139util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);140dst[0] = ubyte_to_float(tmp_r);141dst[1] = 0.0;142dst[2] = 0.0;143dst[3] = 1.0;144}145146void147util_format_rgtc1_snorm_fetch_rgba_8unorm(UNUSED uint8_t *restrict dst, UNUSED const uint8_t *restrict src,148UNUSED unsigned i, UNUSED unsigned j)149{150fprintf(stderr,"%s\n", __func__);151}152153void154util_format_rgtc1_snorm_unpack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,155UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,156UNUSED unsigned width, UNUSED unsigned height)157{158fprintf(stderr,"%s\n", __func__);159}160161void162util_format_rgtc1_snorm_pack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,163UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,164UNUSED unsigned width, UNUSED unsigned height)165{166fprintf(stderr,"%s\n", __func__);167}168169void170util_format_rgtc1_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)171{172const unsigned bw = 4, bh = 4, bytes_per_block = 8;173unsigned x, y, i, j;174175for(y = 0; y < height; y += bh) {176int8_t *dst = (int8_t *)dst_row;177for(x = 0; x < width; x += bw) {178int8_t tmp[4][4]; /* [bh][bw][comps] */179for(j = 0; j < bh; ++j) {180for(i = 0; i < bw; ++i) {181tmp[j][i] = float_to_byte_tex(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]);182}183}184util_format_signed_encode_rgtc_ubyte(dst, tmp, 4, 4);185dst += bytes_per_block;186}187dst_row += dst_stride / sizeof(*dst_row);188}189}190191void192util_format_rgtc1_snorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)193{194unsigned x, y, i, j;195int block_size = 8;196for(y = 0; y < height; y += 4) {197const int8_t *src = (int8_t *)src_row;198for(x = 0; x < width; x += 4) {199for(j = 0; j < 4; ++j) {200for(i = 0; i < 4; ++i) {201float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);202int8_t tmp_r;203util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);204dst[0] = byte_to_float_tex(tmp_r);205dst[1] = 0.0;206dst[2] = 0.0;207dst[3] = 1.0;208}209}210src += block_size;211}212src_row += src_stride;213}214}215216void217util_format_rgtc1_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)218{219float *dst = in_dst;220int8_t tmp_r;221util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);222dst[0] = byte_to_float_tex(tmp_r);223dst[1] = 0.0;224dst[2] = 0.0;225dst[3] = 1.0;226}227228229void230util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)231{232util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);233util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 1, 2);234dst[2] = 0;235dst[3] = 255;236}237238void239util_format_rgtc2_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)240{241const unsigned bw = 4, bh = 4, comps = 4;242unsigned x, y, i, j;243unsigned block_size = 16;244245for(y = 0; y < height; y += bh) {246const uint8_t *src = src_row;247for(x = 0; x < width; x += bw) {248for(j = 0; j < bh; ++j) {249for(i = 0; i < bw; ++i) {250uint8_t *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*comps;251util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);252util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 1, 2);253dst[2] = 0;254dst[3] = 255;255}256}257src += block_size;258}259src_row += src_stride;260}261}262263void264util_format_rgtc2_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)265{266const unsigned bw = 4, bh = 4, bytes_per_block = 16;267unsigned x, y, i, j;268269for(y = 0; y < height; y += bh) {270uint8_t *dst = dst_row;271for(x = 0; x < width; x += bw) {272uint8_t tmp_r[4][4]; /* [bh][bw] */273uint8_t tmp_g[4][4]; /* [bh][bw] */274for(j = 0; j < bh; ++j) {275for(i = 0; i < bw; ++i) {276tmp_r[j][i] = src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4];277tmp_g[j][i] = src_row[((y + j)*src_stride/sizeof(*src_row) + (x + i)*4) + 1];278}279}280util_format_unsigned_encode_rgtc_ubyte(dst, tmp_r, 4, 4);281util_format_unsigned_encode_rgtc_ubyte(dst + 8, tmp_g, 4, 4);282dst += bytes_per_block;283}284dst_row += dst_stride / sizeof(*dst_row);285}286}287288void289util_format_rxtc2_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height, unsigned chan2off)290{291const unsigned bw = 4, bh = 4, bytes_per_block = 16;292unsigned x, y, i, j;293294for(y = 0; y < height; y += bh) {295uint8_t *dst = dst_row;296for(x = 0; x < width; x += bw) {297uint8_t tmp_r[4][4]; /* [bh][bw][comps] */298uint8_t tmp_g[4][4]; /* [bh][bw][comps] */299for(j = 0; j < bh; ++j) {300for(i = 0; i < bw; ++i) {301tmp_r[j][i] = float_to_ubyte(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]);302tmp_g[j][i] = float_to_ubyte(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4 + chan2off]);303}304}305util_format_unsigned_encode_rgtc_ubyte(dst, tmp_r, 4, 4);306util_format_unsigned_encode_rgtc_ubyte(dst + 8, tmp_g, 4, 4);307dst += bytes_per_block;308}309dst_row += dst_stride / sizeof(*dst_row);310}311}312313void314util_format_rgtc2_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)315{316util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 1);317}318319void320util_format_rgtc2_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)321{322unsigned x, y, i, j;323int block_size = 16;324for(y = 0; y < height; y += 4) {325const uint8_t *src = src_row;326for(x = 0; x < width; x += 4) {327for(j = 0; j < 4; ++j) {328for(i = 0; i < 4; ++i) {329float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);330uint8_t tmp_r, tmp_g;331util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);332util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);333dst[0] = ubyte_to_float(tmp_r);334dst[1] = ubyte_to_float(tmp_g);335dst[2] = 0.0;336dst[3] = 1.0;337}338}339src += block_size;340}341src_row += src_stride;342}343}344345void346util_format_rgtc2_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)347{348float *dst = in_dst;349uint8_t tmp_r, tmp_g;350util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);351util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);352dst[0] = ubyte_to_float(tmp_r);353dst[1] = ubyte_to_float(tmp_g);354dst[2] = 0.0;355dst[3] = 1.0;356}357358359void360util_format_rgtc2_snorm_fetch_rgba_8unorm(UNUSED uint8_t *restrict dst, UNUSED const uint8_t *restrict src,361UNUSED unsigned i, UNUSED unsigned j)362{363fprintf(stderr,"%s\n", __func__);364}365366void367util_format_rgtc2_snorm_unpack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,368UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,369UNUSED unsigned width, UNUSED unsigned height)370{371fprintf(stderr,"%s\n", __func__);372}373374void375util_format_rgtc2_snorm_pack_rgba_8unorm(UNUSED uint8_t *restrict dst_row, UNUSED unsigned dst_stride,376UNUSED const uint8_t *restrict src_row, UNUSED unsigned src_stride,377UNUSED unsigned width, UNUSED unsigned height)378{379fprintf(stderr,"%s\n", __func__);380}381382void383util_format_rgtc2_snorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride, const uint8_t *restrict src_row, unsigned src_stride, unsigned width, unsigned height)384{385unsigned x, y, i, j;386int block_size = 16;387for(y = 0; y < height; y += 4) {388const int8_t *src = (int8_t *)src_row;389for(x = 0; x < width; x += 4) {390for(j = 0; j < 4; ++j) {391for(i = 0; i < 4; ++i) {392float *dst = (float *)((uint8_t *)dst_row + (y + j)*dst_stride + (x + i)*16);393int8_t tmp_r, tmp_g;394util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);395util_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);396dst[0] = byte_to_float_tex(tmp_r);397dst[1] = byte_to_float_tex(tmp_g);398dst[2] = 0.0;399dst[3] = 1.0;400}401}402src += block_size;403}404src_row += src_stride;405}406}407408void409util_format_rxtc2_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height, unsigned chan2off)410{411const unsigned bw = 4, bh = 4, bytes_per_block = 16;412unsigned x, y, i, j;413414for(y = 0; y < height; y += bh) {415int8_t *dst = (int8_t *)dst_row;416for(x = 0; x < width; x += bw) {417int8_t tmp_r[4][4]; /* [bh][bw][comps] */418int8_t tmp_g[4][4]; /* [bh][bw][comps] */419for(j = 0; j < bh; ++j) {420for(i = 0; i < bw; ++i) {421tmp_r[j][i] = float_to_byte_tex(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4]);422tmp_g[j][i] = float_to_byte_tex(src_row[(y + j)*src_stride/sizeof(*src_row) + (x + i)*4 + chan2off]);423}424}425util_format_signed_encode_rgtc_ubyte(dst, tmp_r, 4, 4);426util_format_signed_encode_rgtc_ubyte(dst + 8, tmp_g, 4, 4);427dst += bytes_per_block;428}429dst_row += dst_stride / sizeof(*dst_row);430}431}432433void434util_format_rgtc2_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride, const float *restrict src_row, unsigned src_stride, unsigned width, unsigned height)435{436util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 1);437}438439void440util_format_rgtc2_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)441{442float *dst = in_dst;443int8_t tmp_r, tmp_g;444util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);445util_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2);446dst[0] = byte_to_float_tex(tmp_r);447dst[1] = byte_to_float_tex(tmp_g);448dst[2] = 0.0;449dst[3] = 1.0;450}451452453454