Path: blob/21.2-virgl/src/util/format/u_format_fxt1.c
7132 views
/**************************************************************************1*2* Copyright (C) 1999-2008 Brian Paul All Rights Reserved.3* Copyright (c) 2008 VMware, Inc.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*23**************************************************************************/2425#include "util/format/u_format.h"26#include "util/format/u_format_fxt1.h"27#include "util/format/u_format_pack.h"28#include "util/format_srgb.h"29#include "util/u_math.h"3031#define RCOMP 032#define GCOMP 133#define BCOMP 234#define ACOMP 33536#define FXT1_BLOCK_SIZE 163738static void39fxt1_encode (uint32_t width, uint32_t height, int32_t comps,40const void *source, int32_t srcRowStride,41void *dest, int32_t destRowStride);4243static void44fxt1_decode_1 (const void *texture, int32_t stride,45int32_t i, int32_t j, uint8_t *rgba);4647/***************************************************************************\48* FXT1 encoder49*50* The encoder was built by reversing the decoder,51* and is vaguely based on Texus2 by 3dfx. Note that this code52* is merely a proof of concept, since it is highly UNoptimized;53* moreover, it is sub-optimal due to initial conditions passed54* to Lloyd's algorithm (the interpolation modes are even worse).55\***************************************************************************/565758#define MAX_COMP 4 /* ever needed maximum number of components in texel */59#define MAX_VECT 4 /* ever needed maximum number of base vectors to find */60#define N_TEXELS 32 /* number of texels in a block (always 32) */61#define LL_N_REP 50 /* number of iterations in lloyd's vq */62#define LL_RMS_D 10 /* fault tolerance (maximum delta) */63#define LL_RMS_E 255 /* fault tolerance (maximum error) */64#define ALPHA_TS 2 /* alpha threshold: (255 - ALPHA_TS) deemed opaque */65static const uint32_t zero = 0;66#define ISTBLACK(v) (memcmp(&(v), &zero, sizeof(zero)) == 0)6768/*69* Define a 64-bit unsigned integer type and macros70*/71#if 17273#define FX64_NATIVE 17475typedef uint64_t Fx64;7677#define FX64_MOV32(a, b) a = b78#define FX64_OR32(a, b) a |= b79#define FX64_SHL(a, c) a <<= c8081#else8283#define FX64_NATIVE 08485typedef struct {86uint32_t lo, hi;87} Fx64;8889#define FX64_MOV32(a, b) a.lo = b90#define FX64_OR32(a, b) a.lo |= b9192#define FX64_SHL(a, c) \93do { \94if ((c) >= 32) { \95a.hi = a.lo << ((c) - 32); \96a.lo = 0; \97} else { \98a.hi = (a.hi << (c)) | (a.lo >> (32 - (c))); \99a.lo <<= (c); \100} \101} while (0)102103#endif104105106#define F(i) (float)1 /* can be used to obtain an oblong metric: 0.30 / 0.59 / 0.11 */107#define SAFECDOT 1 /* for paranoids */108109#define MAKEIVEC(NV, NC, IV, B, V0, V1) \110do { \111/* compute interpolation vector */ \112float d2 = 0.0F; \113float rd2; \114\115for (i = 0; i < NC; i++) { \116IV[i] = (V1[i] - V0[i]) * F(i); \117d2 += IV[i] * IV[i]; \118} \119rd2 = (float)NV / d2; \120B = 0; \121for (i = 0; i < NC; i++) { \122IV[i] *= F(i); \123B -= IV[i] * V0[i]; \124IV[i] *= rd2; \125} \126B = B * rd2 + 0.5f; \127} while (0)128129#define CALCCDOT(TEXEL, NV, NC, IV, B, V)\130do { \131float dot = 0.0F; \132for (i = 0; i < NC; i++) { \133dot += V[i] * IV[i]; \134} \135TEXEL = (int32_t)(dot + B); \136if (SAFECDOT) { \137if (TEXEL < 0) { \138TEXEL = 0; \139} else if (TEXEL > NV) { \140TEXEL = NV; \141} \142} \143} while (0)144145146static int32_t147fxt1_bestcol (float vec[][MAX_COMP], int32_t nv,148uint8_t input[MAX_COMP], int32_t nc)149{150int32_t i, j, best = -1;151float err = 1e9; /* big enough */152153for (j = 0; j < nv; j++) {154float e = 0.0F;155for (i = 0; i < nc; i++) {156e += (vec[j][i] - input[i]) * (vec[j][i] - input[i]);157}158if (e < err) {159err = e;160best = j;161}162}163164return best;165}166167168static int32_t169fxt1_worst (float vec[MAX_COMP],170uint8_t input[N_TEXELS][MAX_COMP], int32_t nc, int32_t n)171{172int32_t i, k, worst = -1;173float err = -1.0F; /* small enough */174175for (k = 0; k < n; k++) {176float e = 0.0F;177for (i = 0; i < nc; i++) {178e += (vec[i] - input[k][i]) * (vec[i] - input[k][i]);179}180if (e > err) {181err = e;182worst = k;183}184}185186return worst;187}188189190static int32_t191fxt1_variance (uint8_t input[N_TEXELS / 2][MAX_COMP], int32_t nc)192{193const int n = N_TEXELS / 2;194int32_t i, k, best = 0;195int32_t sx, sx2;196double var, maxvar = -1; /* small enough */197double teenth = 1.0 / n;198199for (i = 0; i < nc; i++) {200sx = sx2 = 0;201for (k = 0; k < n; k++) {202int32_t t = input[k][i];203sx += t;204sx2 += t * t;205}206var = sx2 * teenth - sx * sx * teenth * teenth;207if (maxvar < var) {208maxvar = var;209best = i;210}211}212213return best;214}215216217static int32_t218fxt1_choose (float vec[][MAX_COMP], int32_t nv,219uint8_t input[N_TEXELS][MAX_COMP], int32_t nc, int32_t n)220{221#if 0222/* Choose colors from a grid.223*/224int32_t i, j;225226for (j = 0; j < nv; j++) {227int32_t m = j * (n - 1) / (nv - 1);228for (i = 0; i < nc; i++) {229vec[j][i] = input[m][i];230}231}232#else233/* Our solution here is to find the darkest and brightest colors in234* the 8x4 tile and use those as the two representative colors.235* There are probably better algorithms to use (histogram-based).236*/237int32_t i, j, k;238int32_t minSum = 2000; /* big enough */239int32_t maxSum = -1; /* small enough */240int32_t minCol = 0; /* phoudoin: silent compiler! */241int32_t maxCol = 0; /* phoudoin: silent compiler! */242243struct {244int32_t flag;245int32_t key;246int32_t freq;247int32_t idx;248} hist[N_TEXELS];249int32_t lenh = 0;250251memset(hist, 0, sizeof(hist));252253for (k = 0; k < n; k++) {254int32_t l;255int32_t key = 0;256int32_t sum = 0;257for (i = 0; i < nc; i++) {258key <<= 8;259key |= input[k][i];260sum += input[k][i];261}262for (l = 0; l < n; l++) {263if (!hist[l].flag) {264/* alloc new slot */265hist[l].flag = !0;266hist[l].key = key;267hist[l].freq = 1;268hist[l].idx = k;269lenh = l + 1;270break;271} else if (hist[l].key == key) {272hist[l].freq++;273break;274}275}276if (minSum > sum) {277minSum = sum;278minCol = k;279}280if (maxSum < sum) {281maxSum = sum;282maxCol = k;283}284}285286if (lenh <= nv) {287for (j = 0; j < lenh; j++) {288for (i = 0; i < nc; i++) {289vec[j][i] = (float)input[hist[j].idx][i];290}291}292for (; j < nv; j++) {293for (i = 0; i < nc; i++) {294vec[j][i] = vec[0][i];295}296}297return 0;298}299300for (j = 0; j < nv; j++) {301for (i = 0; i < nc; i++) {302vec[j][i] = ((nv - 1 - j) * input[minCol][i] + j * input[maxCol][i] + (nv - 1) / 2) / (float)(nv - 1);303}304}305#endif306307return !0;308}309310311static int32_t312fxt1_lloyd (float vec[][MAX_COMP], int32_t nv,313uint8_t input[N_TEXELS][MAX_COMP], int32_t nc, int32_t n)314{315/* Use the generalized lloyd's algorithm for VQ:316* find 4 color vectors.317*318* for each sample color319* sort to nearest vector.320*321* replace each vector with the centroid of its matching colors.322*323* repeat until RMS doesn't improve.324*325* if a color vector has no samples, or becomes the same as another326* vector, replace it with the color which is farthest from a sample.327*328* vec[][MAX_COMP] initial vectors and resulting colors329* nv number of resulting colors required330* input[N_TEXELS][MAX_COMP] input texels331* nc number of components in input / vec332* n number of input samples333*/334335int32_t sum[MAX_VECT][MAX_COMP]; /* used to accumulate closest texels */336int32_t cnt[MAX_VECT]; /* how many times a certain vector was chosen */337float error, lasterror = 1e9;338339int32_t i, j, k, rep;340341/* the quantizer */342for (rep = 0; rep < LL_N_REP; rep++) {343/* reset sums & counters */344for (j = 0; j < nv; j++) {345for (i = 0; i < nc; i++) {346sum[j][i] = 0;347}348cnt[j] = 0;349}350error = 0;351352/* scan whole block */353for (k = 0; k < n; k++) {354#if 1355int32_t best = -1;356float err = 1e9; /* big enough */357/* determine best vector */358for (j = 0; j < nv; j++) {359float e = (vec[j][0] - input[k][0]) * (vec[j][0] - input[k][0]) +360(vec[j][1] - input[k][1]) * (vec[j][1] - input[k][1]) +361(vec[j][2] - input[k][2]) * (vec[j][2] - input[k][2]);362if (nc == 4) {363e += (vec[j][3] - input[k][3]) * (vec[j][3] - input[k][3]);364}365if (e < err) {366err = e;367best = j;368}369}370#else371int32_t best = fxt1_bestcol(vec, nv, input[k], nc, &err);372#endif373assert(best >= 0);374/* add in closest color */375for (i = 0; i < nc; i++) {376sum[best][i] += input[k][i];377}378/* mark this vector as used */379cnt[best]++;380/* accumulate error */381error += err;382}383384/* check RMS */385if ((error < LL_RMS_E) ||386((error < lasterror) && ((lasterror - error) < LL_RMS_D))) {387return !0; /* good match */388}389lasterror = error;390391/* move each vector to the barycenter of its closest colors */392for (j = 0; j < nv; j++) {393if (cnt[j]) {394float div = 1.0F / cnt[j];395for (i = 0; i < nc; i++) {396vec[j][i] = div * sum[j][i];397}398} else {399/* this vec has no samples or is identical with a previous vec */400int32_t worst = fxt1_worst(vec[j], input, nc, n);401for (i = 0; i < nc; i++) {402vec[j][i] = input[worst][i];403}404}405}406}407408return 0; /* could not converge fast enough */409}410411412static void413fxt1_quantize_CHROMA (uint32_t *cc,414uint8_t input[N_TEXELS][MAX_COMP])415{416const int32_t n_vect = 4; /* 4 base vectors to find */417const int32_t n_comp = 3; /* 3 components: R, G, B */418float vec[MAX_VECT][MAX_COMP];419int32_t i, j, k;420Fx64 hi; /* high quadword */421uint32_t lohi, lolo; /* low quadword: hi dword, lo dword */422423if (fxt1_choose(vec, n_vect, input, n_comp, N_TEXELS) != 0) {424fxt1_lloyd(vec, n_vect, input, n_comp, N_TEXELS);425}426427FX64_MOV32(hi, 4); /* cc-chroma = "010" + unused bit */428for (j = n_vect - 1; j >= 0; j--) {429for (i = 0; i < n_comp; i++) {430/* add in colors */431FX64_SHL(hi, 5);432FX64_OR32(hi, (uint32_t)(vec[j][i] / 8.0F));433}434}435((Fx64 *)cc)[1] = hi;436437lohi = lolo = 0;438/* right microtile */439for (k = N_TEXELS - 1; k >= N_TEXELS/2; k--) {440lohi <<= 2;441lohi |= fxt1_bestcol(vec, n_vect, input[k], n_comp);442}443/* left microtile */444for (; k >= 0; k--) {445lolo <<= 2;446lolo |= fxt1_bestcol(vec, n_vect, input[k], n_comp);447}448cc[1] = lohi;449cc[0] = lolo;450}451452453static void454fxt1_quantize_ALPHA0 (uint32_t *cc,455uint8_t input[N_TEXELS][MAX_COMP],456uint8_t reord[N_TEXELS][MAX_COMP], int32_t n)457{458const int32_t n_vect = 3; /* 3 base vectors to find */459const int32_t n_comp = 4; /* 4 components: R, G, B, A */460float vec[MAX_VECT][MAX_COMP];461int32_t i, j, k;462Fx64 hi; /* high quadword */463uint32_t lohi, lolo; /* low quadword: hi dword, lo dword */464465/* the last vector indicates zero */466for (i = 0; i < n_comp; i++) {467vec[n_vect][i] = 0;468}469470/* the first n texels in reord are guaranteed to be non-zero */471if (fxt1_choose(vec, n_vect, reord, n_comp, n) != 0) {472fxt1_lloyd(vec, n_vect, reord, n_comp, n);473}474475FX64_MOV32(hi, 6); /* alpha = "011" + lerp = 0 */476for (j = n_vect - 1; j >= 0; j--) {477/* add in alphas */478FX64_SHL(hi, 5);479FX64_OR32(hi, (uint32_t)(vec[j][ACOMP] / 8.0F));480}481for (j = n_vect - 1; j >= 0; j--) {482for (i = 0; i < n_comp - 1; i++) {483/* add in colors */484FX64_SHL(hi, 5);485FX64_OR32(hi, (uint32_t)(vec[j][i] / 8.0F));486}487}488((Fx64 *)cc)[1] = hi;489490lohi = lolo = 0;491/* right microtile */492for (k = N_TEXELS - 1; k >= N_TEXELS/2; k--) {493lohi <<= 2;494lohi |= fxt1_bestcol(vec, n_vect + 1, input[k], n_comp);495}496/* left microtile */497for (; k >= 0; k--) {498lolo <<= 2;499lolo |= fxt1_bestcol(vec, n_vect + 1, input[k], n_comp);500}501cc[1] = lohi;502cc[0] = lolo;503}504505506static void507fxt1_quantize_ALPHA1 (uint32_t *cc,508uint8_t input[N_TEXELS][MAX_COMP])509{510const int32_t n_vect = 3; /* highest vector number in each microtile */511const int32_t n_comp = 4; /* 4 components: R, G, B, A */512float vec[1 + 1 + 1][MAX_COMP]; /* 1.5 extrema for each sub-block */513float b, iv[MAX_COMP]; /* interpolation vector */514int32_t i, j, k;515Fx64 hi; /* high quadword */516uint32_t lohi, lolo; /* low quadword: hi dword, lo dword */517518int32_t minSum;519int32_t maxSum;520int32_t minColL = 0, maxColL = 0;521int32_t minColR = 0, maxColR = 0;522int32_t sumL = 0, sumR = 0;523int32_t nn_comp;524/* Our solution here is to find the darkest and brightest colors in525* the 4x4 tile and use those as the two representative colors.526* There are probably better algorithms to use (histogram-based).527*/528nn_comp = n_comp;529while ((minColL == maxColL) && nn_comp) {530minSum = 2000; /* big enough */531maxSum = -1; /* small enough */532for (k = 0; k < N_TEXELS / 2; k++) {533int32_t sum = 0;534for (i = 0; i < nn_comp; i++) {535sum += input[k][i];536}537if (minSum > sum) {538minSum = sum;539minColL = k;540}541if (maxSum < sum) {542maxSum = sum;543maxColL = k;544}545sumL += sum;546}547548nn_comp--;549}550551nn_comp = n_comp;552while ((minColR == maxColR) && nn_comp) {553minSum = 2000; /* big enough */554maxSum = -1; /* small enough */555for (k = N_TEXELS / 2; k < N_TEXELS; k++) {556int32_t sum = 0;557for (i = 0; i < nn_comp; i++) {558sum += input[k][i];559}560if (minSum > sum) {561minSum = sum;562minColR = k;563}564if (maxSum < sum) {565maxSum = sum;566maxColR = k;567}568sumR += sum;569}570571nn_comp--;572}573574/* choose the common vector (yuck!) */575{576int32_t j1, j2;577int32_t v1 = 0, v2 = 0;578float err = 1e9; /* big enough */579float tv[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */580for (i = 0; i < n_comp; i++) {581tv[0][i] = input[minColL][i];582tv[1][i] = input[maxColL][i];583tv[2][i] = input[minColR][i];584tv[3][i] = input[maxColR][i];585}586for (j1 = 0; j1 < 2; j1++) {587for (j2 = 2; j2 < 4; j2++) {588float e = 0.0F;589for (i = 0; i < n_comp; i++) {590e += (tv[j1][i] - tv[j2][i]) * (tv[j1][i] - tv[j2][i]);591}592if (e < err) {593err = e;594v1 = j1;595v2 = j2;596}597}598}599for (i = 0; i < n_comp; i++) {600vec[0][i] = tv[1 - v1][i];601vec[1][i] = (tv[v1][i] * sumL + tv[v2][i] * sumR) / (sumL + sumR);602vec[2][i] = tv[5 - v2][i];603}604}605606/* left microtile */607cc[0] = 0;608if (minColL != maxColL) {609/* compute interpolation vector */610MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);611612/* add in texels */613lolo = 0;614for (k = N_TEXELS / 2 - 1; k >= 0; k--) {615int32_t texel;616/* interpolate color */617CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);618/* add in texel */619lolo <<= 2;620lolo |= texel;621}622623cc[0] = lolo;624}625626/* right microtile */627cc[1] = 0;628if (minColR != maxColR) {629/* compute interpolation vector */630MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[1]);631632/* add in texels */633lohi = 0;634for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {635int32_t texel;636/* interpolate color */637CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);638/* add in texel */639lohi <<= 2;640lohi |= texel;641}642643cc[1] = lohi;644}645646FX64_MOV32(hi, 7); /* alpha = "011" + lerp = 1 */647for (j = n_vect - 1; j >= 0; j--) {648/* add in alphas */649FX64_SHL(hi, 5);650FX64_OR32(hi, (uint32_t)(vec[j][ACOMP] / 8.0F));651}652for (j = n_vect - 1; j >= 0; j--) {653for (i = 0; i < n_comp - 1; i++) {654/* add in colors */655FX64_SHL(hi, 5);656FX64_OR32(hi, (uint32_t)(vec[j][i] / 8.0F));657}658}659((Fx64 *)cc)[1] = hi;660}661662663static void664fxt1_quantize_HI (uint32_t *cc,665uint8_t input[N_TEXELS][MAX_COMP],666uint8_t reord[N_TEXELS][MAX_COMP], int32_t n)667{668const int32_t n_vect = 6; /* highest vector number */669const int32_t n_comp = 3; /* 3 components: R, G, B */670float b = 0.0F; /* phoudoin: silent compiler! */671float iv[MAX_COMP]; /* interpolation vector */672int32_t i, k;673uint32_t hihi; /* high quadword: hi dword */674675int32_t minSum = 2000; /* big enough */676int32_t maxSum = -1; /* small enough */677int32_t minCol = 0; /* phoudoin: silent compiler! */678int32_t maxCol = 0; /* phoudoin: silent compiler! */679680/* Our solution here is to find the darkest and brightest colors in681* the 8x4 tile and use those as the two representative colors.682* There are probably better algorithms to use (histogram-based).683*/684for (k = 0; k < n; k++) {685int32_t sum = 0;686for (i = 0; i < n_comp; i++) {687sum += reord[k][i];688}689if (minSum > sum) {690minSum = sum;691minCol = k;692}693if (maxSum < sum) {694maxSum = sum;695maxCol = k;696}697}698699hihi = 0; /* cc-hi = "00" */700for (i = 0; i < n_comp; i++) {701/* add in colors */702hihi <<= 5;703hihi |= reord[maxCol][i] >> 3;704}705for (i = 0; i < n_comp; i++) {706/* add in colors */707hihi <<= 5;708hihi |= reord[minCol][i] >> 3;709}710cc[3] = hihi;711cc[0] = cc[1] = cc[2] = 0;712713/* compute interpolation vector */714if (minCol != maxCol) {715MAKEIVEC(n_vect, n_comp, iv, b, reord[minCol], reord[maxCol]);716}717718/* add in texels */719for (k = N_TEXELS - 1; k >= 0; k--) {720int32_t t = k * 3;721uint32_t *kk = (uint32_t *)((char *)cc + t / 8);722int32_t texel = n_vect + 1; /* transparent black */723724if (!ISTBLACK(input[k])) {725if (minCol != maxCol) {726/* interpolate color */727CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);728/* add in texel */729kk[0] |= texel << (t & 7);730}731} else {732/* add in texel */733kk[0] |= texel << (t & 7);734}735}736}737738739static void740fxt1_quantize_MIXED1 (uint32_t *cc,741uint8_t input[N_TEXELS][MAX_COMP])742{743const int32_t n_vect = 2; /* highest vector number in each microtile */744const int32_t n_comp = 3; /* 3 components: R, G, B */745uint8_t vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */746float b, iv[MAX_COMP]; /* interpolation vector */747int32_t i, j, k;748Fx64 hi; /* high quadword */749uint32_t lohi, lolo; /* low quadword: hi dword, lo dword */750751int32_t minSum;752int32_t maxSum;753int32_t minColL = 0, maxColL = -1;754int32_t minColR = 0, maxColR = -1;755756/* Our solution here is to find the darkest and brightest colors in757* the 4x4 tile and use those as the two representative colors.758* There are probably better algorithms to use (histogram-based).759*/760minSum = 2000; /* big enough */761maxSum = -1; /* small enough */762for (k = 0; k < N_TEXELS / 2; k++) {763if (!ISTBLACK(input[k])) {764int32_t sum = 0;765for (i = 0; i < n_comp; i++) {766sum += input[k][i];767}768if (minSum > sum) {769minSum = sum;770minColL = k;771}772if (maxSum < sum) {773maxSum = sum;774maxColL = k;775}776}777}778minSum = 2000; /* big enough */779maxSum = -1; /* small enough */780for (; k < N_TEXELS; k++) {781if (!ISTBLACK(input[k])) {782int32_t sum = 0;783for (i = 0; i < n_comp; i++) {784sum += input[k][i];785}786if (minSum > sum) {787minSum = sum;788minColR = k;789}790if (maxSum < sum) {791maxSum = sum;792maxColR = k;793}794}795}796797/* left microtile */798if (maxColL == -1) {799/* all transparent black */800cc[0] = ~0u;801for (i = 0; i < n_comp; i++) {802vec[0][i] = 0;803vec[1][i] = 0;804}805} else {806cc[0] = 0;807for (i = 0; i < n_comp; i++) {808vec[0][i] = input[minColL][i];809vec[1][i] = input[maxColL][i];810}811if (minColL != maxColL) {812/* compute interpolation vector */813MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);814815/* add in texels */816lolo = 0;817for (k = N_TEXELS / 2 - 1; k >= 0; k--) {818int32_t texel = n_vect + 1; /* transparent black */819if (!ISTBLACK(input[k])) {820/* interpolate color */821CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);822}823/* add in texel */824lolo <<= 2;825lolo |= texel;826}827cc[0] = lolo;828}829}830831/* right microtile */832if (maxColR == -1) {833/* all transparent black */834cc[1] = ~0u;835for (i = 0; i < n_comp; i++) {836vec[2][i] = 0;837vec[3][i] = 0;838}839} else {840cc[1] = 0;841for (i = 0; i < n_comp; i++) {842vec[2][i] = input[minColR][i];843vec[3][i] = input[maxColR][i];844}845if (minColR != maxColR) {846/* compute interpolation vector */847MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[3]);848849/* add in texels */850lohi = 0;851for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {852int32_t texel = n_vect + 1; /* transparent black */853if (!ISTBLACK(input[k])) {854/* interpolate color */855CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);856}857/* add in texel */858lohi <<= 2;859lohi |= texel;860}861cc[1] = lohi;862}863}864865FX64_MOV32(hi, 9 | (vec[3][GCOMP] & 4) | ((vec[1][GCOMP] >> 1) & 2)); /* chroma = "1" */866for (j = 2 * 2 - 1; j >= 0; j--) {867for (i = 0; i < n_comp; i++) {868/* add in colors */869FX64_SHL(hi, 5);870FX64_OR32(hi, vec[j][i] >> 3);871}872}873((Fx64 *)cc)[1] = hi;874}875876877static void878fxt1_quantize_MIXED0 (uint32_t *cc,879uint8_t input[N_TEXELS][MAX_COMP])880{881const int32_t n_vect = 3; /* highest vector number in each microtile */882const int32_t n_comp = 3; /* 3 components: R, G, B */883uint8_t vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */884float b, iv[MAX_COMP]; /* interpolation vector */885int32_t i, j, k;886Fx64 hi; /* high quadword */887uint32_t lohi, lolo; /* low quadword: hi dword, lo dword */888889int32_t minColL = 0, maxColL = 0;890int32_t minColR = 0, maxColR = 0;891#if 0892int32_t minSum;893int32_t maxSum;894895/* Our solution here is to find the darkest and brightest colors in896* the 4x4 tile and use those as the two representative colors.897* There are probably better algorithms to use (histogram-based).898*/899minSum = 2000; /* big enough */900maxSum = -1; /* small enough */901for (k = 0; k < N_TEXELS / 2; k++) {902int32_t sum = 0;903for (i = 0; i < n_comp; i++) {904sum += input[k][i];905}906if (minSum > sum) {907minSum = sum;908minColL = k;909}910if (maxSum < sum) {911maxSum = sum;912maxColL = k;913}914}915minSum = 2000; /* big enough */916maxSum = -1; /* small enough */917for (; k < N_TEXELS; k++) {918int32_t sum = 0;919for (i = 0; i < n_comp; i++) {920sum += input[k][i];921}922if (minSum > sum) {923minSum = sum;924minColR = k;925}926if (maxSum < sum) {927maxSum = sum;928maxColR = k;929}930}931#else932int32_t minVal;933int32_t maxVal;934int32_t maxVarL = fxt1_variance(input, n_comp);935int32_t maxVarR = fxt1_variance(&input[N_TEXELS / 2], n_comp);936937/* Scan the channel with max variance for lo & hi938* and use those as the two representative colors.939*/940minVal = 2000; /* big enough */941maxVal = -1; /* small enough */942for (k = 0; k < N_TEXELS / 2; k++) {943int32_t t = input[k][maxVarL];944if (minVal > t) {945minVal = t;946minColL = k;947}948if (maxVal < t) {949maxVal = t;950maxColL = k;951}952}953minVal = 2000; /* big enough */954maxVal = -1; /* small enough */955for (; k < N_TEXELS; k++) {956int32_t t = input[k][maxVarR];957if (minVal > t) {958minVal = t;959minColR = k;960}961if (maxVal < t) {962maxVal = t;963maxColR = k;964}965}966#endif967968/* left microtile */969cc[0] = 0;970for (i = 0; i < n_comp; i++) {971vec[0][i] = input[minColL][i];972vec[1][i] = input[maxColL][i];973}974if (minColL != maxColL) {975/* compute interpolation vector */976MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);977978/* add in texels */979lolo = 0;980for (k = N_TEXELS / 2 - 1; k >= 0; k--) {981int32_t texel;982/* interpolate color */983CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);984/* add in texel */985lolo <<= 2;986lolo |= texel;987}988989/* funky encoding for LSB of green */990if ((int32_t)((lolo >> 1) & 1) != (((vec[1][GCOMP] ^ vec[0][GCOMP]) >> 2) & 1)) {991for (i = 0; i < n_comp; i++) {992vec[1][i] = input[minColL][i];993vec[0][i] = input[maxColL][i];994}995lolo = ~lolo;996}997998cc[0] = lolo;999}10001001/* right microtile */1002cc[1] = 0;1003for (i = 0; i < n_comp; i++) {1004vec[2][i] = input[minColR][i];1005vec[3][i] = input[maxColR][i];1006}1007if (minColR != maxColR) {1008/* compute interpolation vector */1009MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[3]);10101011/* add in texels */1012lohi = 0;1013for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {1014int32_t texel;1015/* interpolate color */1016CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);1017/* add in texel */1018lohi <<= 2;1019lohi |= texel;1020}10211022/* funky encoding for LSB of green */1023if ((int32_t)((lohi >> 1) & 1) != (((vec[3][GCOMP] ^ vec[2][GCOMP]) >> 2) & 1)) {1024for (i = 0; i < n_comp; i++) {1025vec[3][i] = input[minColR][i];1026vec[2][i] = input[maxColR][i];1027}1028lohi = ~lohi;1029}10301031cc[1] = lohi;1032}10331034FX64_MOV32(hi, 8 | (vec[3][GCOMP] & 4) | ((vec[1][GCOMP] >> 1) & 2)); /* chroma = "1" */1035for (j = 2 * 2 - 1; j >= 0; j--) {1036for (i = 0; i < n_comp; i++) {1037/* add in colors */1038FX64_SHL(hi, 5);1039FX64_OR32(hi, vec[j][i] >> 3);1040}1041}1042((Fx64 *)cc)[1] = hi;1043}104410451046static void1047fxt1_quantize (uint32_t *cc, const uint8_t *lines[], int32_t comps)1048{1049int32_t trualpha;1050uint8_t reord[N_TEXELS][MAX_COMP];10511052uint8_t input[N_TEXELS][MAX_COMP];1053int32_t i, k, l;10541055if (comps == 3) {1056/* make the whole block opaque */1057memset(input, -1, sizeof(input));1058}10591060/* 8 texels each line */1061for (l = 0; l < 4; l++) {1062for (k = 0; k < 4; k++) {1063for (i = 0; i < comps; i++) {1064input[k + l * 4][i] = *lines[l]++;1065}1066}1067for (; k < 8; k++) {1068for (i = 0; i < comps; i++) {1069input[k + l * 4 + 12][i] = *lines[l]++;1070}1071}1072}10731074/* block layout:1075* 00, 01, 02, 03, 08, 09, 0a, 0b1076* 10, 11, 12, 13, 18, 19, 1a, 1b1077* 04, 05, 06, 07, 0c, 0d, 0e, 0f1078* 14, 15, 16, 17, 1c, 1d, 1e, 1f1079*/10801081/* [dBorca]1082* stupidity flows forth from this1083*/1084l = N_TEXELS;1085trualpha = 0;1086if (comps == 4) {1087/* skip all transparent black texels */1088l = 0;1089for (k = 0; k < N_TEXELS; k++) {1090/* test all components against 0 */1091if (!ISTBLACK(input[k])) {1092/* texel is not transparent black */1093memcpy(reord[l], input[k], 4);1094if (reord[l][ACOMP] < (255 - ALPHA_TS)) {1095/* non-opaque texel */1096trualpha = !0;1097}1098l++;1099}1100}1101}11021103#if 01104if (trualpha) {1105fxt1_quantize_ALPHA0(cc, input, reord, l);1106} else if (l == 0) {1107cc[0] = cc[1] = cc[2] = -1;1108cc[3] = 0;1109} else if (l < N_TEXELS) {1110fxt1_quantize_HI(cc, input, reord, l);1111} else {1112fxt1_quantize_CHROMA(cc, input);1113}1114(void)fxt1_quantize_ALPHA1;1115(void)fxt1_quantize_MIXED1;1116(void)fxt1_quantize_MIXED0;1117#else1118if (trualpha) {1119fxt1_quantize_ALPHA1(cc, input);1120} else if (l == 0) {1121cc[0] = cc[1] = cc[2] = ~0u;1122cc[3] = 0;1123} else if (l < N_TEXELS) {1124fxt1_quantize_MIXED1(cc, input);1125} else {1126fxt1_quantize_MIXED0(cc, input);1127}1128(void)fxt1_quantize_ALPHA0;1129(void)fxt1_quantize_HI;1130(void)fxt1_quantize_CHROMA;1131#endif1132}1133113411351136/**1137* Upscale an image by replication, not (typical) stretching.1138* We use this when the image width or height is less than a1139* certain size (4, 8) and we need to upscale an image.1140*/1141static void1142upscale_teximage2d(int32_t inWidth, int32_t inHeight,1143int32_t outWidth, int32_t outHeight,1144int32_t comps, const uint8_t *src, int32_t srcRowStride,1145uint8_t *dest )1146{1147int32_t i, j, k;11481149assert(outWidth >= inWidth);1150assert(outHeight >= inHeight);1151#if 01152assert(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);1153assert((outWidth & 3) == 0);1154assert((outHeight & 3) == 0);1155#endif11561157for (i = 0; i < outHeight; i++) {1158const int32_t ii = i % inHeight;1159for (j = 0; j < outWidth; j++) {1160const int32_t jj = j % inWidth;1161for (k = 0; k < comps; k++) {1162dest[(i * outWidth + j) * comps + k]1163= src[ii * srcRowStride + jj * comps + k];1164}1165}1166}1167}116811691170static void1171fxt1_encode (uint32_t width, uint32_t height, int32_t comps,1172const void *source, int32_t srcRowStride,1173void *dest, int32_t destRowStride)1174{1175uint32_t x, y;1176const uint8_t *data;1177uint32_t *encoded = (uint32_t *)dest;1178void *newSource = NULL;11791180assert(comps == 3 || comps == 4);11811182/* Replicate image if width is not M8 or height is not M4 */1183if ((width & 7) | (height & 3)) {1184int32_t newWidth = (width + 7) & ~7;1185int32_t newHeight = (height + 3) & ~3;1186newSource = malloc(comps * newWidth * newHeight * sizeof(uint8_t));1187if (!newSource)1188return;1189upscale_teximage2d(width, height, newWidth, newHeight,1190comps, (const uint8_t *) source,1191srcRowStride, (uint8_t *) newSource);1192source = newSource;1193width = newWidth;1194height = newHeight;1195srcRowStride = comps * newWidth;1196}11971198data = (const uint8_t *) source;1199destRowStride = (destRowStride - width * 2) / 4;1200for (y = 0; y < height; y += 4) {1201uint32_t offs = 0 + (y + 0) * srcRowStride;1202for (x = 0; x < width; x += 8) {1203const uint8_t *lines[4];1204lines[0] = &data[offs];1205lines[1] = lines[0] + srcRowStride;1206lines[2] = lines[1] + srcRowStride;1207lines[3] = lines[2] + srcRowStride;1208offs += 8 * comps;1209fxt1_quantize(encoded, lines, comps);1210/* 128 bits per 8x4 block */1211encoded += 4;1212}1213encoded += destRowStride;1214}12151216free(newSource);1217}121812191220/***************************************************************************\1221* FXT1 decoder1222*1223* The decoder is based on GL_3DFX_texture_compression_FXT11224* specification and serves as a concept for the encoder.1225\***************************************************************************/122612271228/* lookup table for scaling 5 bit colors up to 8 bits */1229static const uint8_t _rgb_scale_5[] = {12300, 8, 16, 25, 33, 41, 49, 58,123166, 74, 82, 90, 99, 107, 115, 123,1232132, 140, 148, 156, 165, 173, 181, 189,1233197, 206, 214, 222, 230, 239, 247, 2551234};12351236/* lookup table for scaling 6 bit colors up to 8 bits */1237static const uint8_t _rgb_scale_6[] = {12380, 4, 8, 12, 16, 20, 24, 28,123932, 36, 40, 45, 49, 53, 57, 61,124065, 69, 73, 77, 81, 85, 89, 93,124197, 101, 105, 109, 113, 117, 121, 125,1242130, 134, 138, 142, 146, 150, 154, 158,1243162, 166, 170, 174, 178, 182, 186, 190,1244194, 198, 202, 206, 210, 215, 219, 223,1245227, 231, 235, 239, 243, 247, 251, 2551246};124712481249#define CC_SEL(cc, which) (((uint32_t *)(cc))[(which) / 32] >> ((which) & 31))1250#define UP5(c) _rgb_scale_5[(c) & 31]1251#define UP6(c, b) _rgb_scale_6[(((c) & 31) << 1) | ((b) & 1)]1252#define LERP(n, t, c0, c1) (((n) - (t)) * (c0) + (t) * (c1) + (n) / 2) / (n)125312541255static void1256fxt1_decode_1HI (const uint8_t *code, int32_t t, uint8_t *rgba)1257{1258const uint32_t *cc;12591260t *= 3;1261cc = (const uint32_t *)(code + t / 8);1262t = (cc[0] >> (t & 7)) & 7;12631264if (t == 7) {1265rgba[RCOMP] = rgba[GCOMP] = rgba[BCOMP] = rgba[ACOMP] = 0;1266} else {1267uint8_t r, g, b;1268cc = (const uint32_t *)(code + 12);1269if (t == 0) {1270b = UP5(CC_SEL(cc, 0));1271g = UP5(CC_SEL(cc, 5));1272r = UP5(CC_SEL(cc, 10));1273} else if (t == 6) {1274b = UP5(CC_SEL(cc, 15));1275g = UP5(CC_SEL(cc, 20));1276r = UP5(CC_SEL(cc, 25));1277} else {1278b = LERP(6, t, UP5(CC_SEL(cc, 0)), UP5(CC_SEL(cc, 15)));1279g = LERP(6, t, UP5(CC_SEL(cc, 5)), UP5(CC_SEL(cc, 20)));1280r = LERP(6, t, UP5(CC_SEL(cc, 10)), UP5(CC_SEL(cc, 25)));1281}1282rgba[RCOMP] = r;1283rgba[GCOMP] = g;1284rgba[BCOMP] = b;1285rgba[ACOMP] = 255;1286}1287}128812891290static void1291fxt1_decode_1CHROMA (const uint8_t *code, int32_t t, uint8_t *rgba)1292{1293const uint32_t *cc;1294uint32_t kk;12951296cc = (const uint32_t *)code;1297if (t & 16) {1298cc++;1299t &= 15;1300}1301t = (cc[0] >> (t * 2)) & 3;13021303t *= 15;1304cc = (const uint32_t *)(code + 8 + t / 8);1305kk = cc[0] >> (t & 7);1306rgba[BCOMP] = UP5(kk);1307rgba[GCOMP] = UP5(kk >> 5);1308rgba[RCOMP] = UP5(kk >> 10);1309rgba[ACOMP] = 255;1310}131113121313static void1314fxt1_decode_1MIXED (const uint8_t *code, int32_t t, uint8_t *rgba)1315{1316const uint32_t *cc;1317uint32_t col[2][3];1318int32_t glsb, selb;13191320cc = (const uint32_t *)code;1321if (t & 16) {1322t &= 15;1323t = (cc[1] >> (t * 2)) & 3;1324/* col 2 */1325col[0][BCOMP] = (*(const uint32_t *)(code + 11)) >> 6;1326col[0][GCOMP] = CC_SEL(cc, 99);1327col[0][RCOMP] = CC_SEL(cc, 104);1328/* col 3 */1329col[1][BCOMP] = CC_SEL(cc, 109);1330col[1][GCOMP] = CC_SEL(cc, 114);1331col[1][RCOMP] = CC_SEL(cc, 119);1332glsb = CC_SEL(cc, 126);1333selb = CC_SEL(cc, 33);1334} else {1335t = (cc[0] >> (t * 2)) & 3;1336/* col 0 */1337col[0][BCOMP] = CC_SEL(cc, 64);1338col[0][GCOMP] = CC_SEL(cc, 69);1339col[0][RCOMP] = CC_SEL(cc, 74);1340/* col 1 */1341col[1][BCOMP] = CC_SEL(cc, 79);1342col[1][GCOMP] = CC_SEL(cc, 84);1343col[1][RCOMP] = CC_SEL(cc, 89);1344glsb = CC_SEL(cc, 125);1345selb = CC_SEL(cc, 1);1346}13471348if (CC_SEL(cc, 124) & 1) {1349/* alpha[0] == 1 */13501351if (t == 3) {1352/* zero */1353rgba[RCOMP] = rgba[BCOMP] = rgba[GCOMP] = rgba[ACOMP] = 0;1354} else {1355uint8_t r, g, b;1356if (t == 0) {1357b = UP5(col[0][BCOMP]);1358g = UP5(col[0][GCOMP]);1359r = UP5(col[0][RCOMP]);1360} else if (t == 2) {1361b = UP5(col[1][BCOMP]);1362g = UP6(col[1][GCOMP], glsb);1363r = UP5(col[1][RCOMP]);1364} else {1365b = (UP5(col[0][BCOMP]) + UP5(col[1][BCOMP])) / 2;1366g = (UP5(col[0][GCOMP]) + UP6(col[1][GCOMP], glsb)) / 2;1367r = (UP5(col[0][RCOMP]) + UP5(col[1][RCOMP])) / 2;1368}1369rgba[RCOMP] = r;1370rgba[GCOMP] = g;1371rgba[BCOMP] = b;1372rgba[ACOMP] = 255;1373}1374} else {1375/* alpha[0] == 0 */1376uint8_t r, g, b;1377if (t == 0) {1378b = UP5(col[0][BCOMP]);1379g = UP6(col[0][GCOMP], glsb ^ selb);1380r = UP5(col[0][RCOMP]);1381} else if (t == 3) {1382b = UP5(col[1][BCOMP]);1383g = UP6(col[1][GCOMP], glsb);1384r = UP5(col[1][RCOMP]);1385} else {1386b = LERP(3, t, UP5(col[0][BCOMP]), UP5(col[1][BCOMP]));1387g = LERP(3, t, UP6(col[0][GCOMP], glsb ^ selb),1388UP6(col[1][GCOMP], glsb));1389r = LERP(3, t, UP5(col[0][RCOMP]), UP5(col[1][RCOMP]));1390}1391rgba[RCOMP] = r;1392rgba[GCOMP] = g;1393rgba[BCOMP] = b;1394rgba[ACOMP] = 255;1395}1396}139713981399static void1400fxt1_decode_1ALPHA (const uint8_t *code, int32_t t, uint8_t *rgba)1401{1402const uint32_t *cc;1403uint8_t r, g, b, a;14041405cc = (const uint32_t *)code;1406if (CC_SEL(cc, 124) & 1) {1407/* lerp == 1 */1408uint32_t col0[4];14091410if (t & 16) {1411t &= 15;1412t = (cc[1] >> (t * 2)) & 3;1413/* col 2 */1414col0[BCOMP] = (*(const uint32_t *)(code + 11)) >> 6;1415col0[GCOMP] = CC_SEL(cc, 99);1416col0[RCOMP] = CC_SEL(cc, 104);1417col0[ACOMP] = CC_SEL(cc, 119);1418} else {1419t = (cc[0] >> (t * 2)) & 3;1420/* col 0 */1421col0[BCOMP] = CC_SEL(cc, 64);1422col0[GCOMP] = CC_SEL(cc, 69);1423col0[RCOMP] = CC_SEL(cc, 74);1424col0[ACOMP] = CC_SEL(cc, 109);1425}14261427if (t == 0) {1428b = UP5(col0[BCOMP]);1429g = UP5(col0[GCOMP]);1430r = UP5(col0[RCOMP]);1431a = UP5(col0[ACOMP]);1432} else if (t == 3) {1433b = UP5(CC_SEL(cc, 79));1434g = UP5(CC_SEL(cc, 84));1435r = UP5(CC_SEL(cc, 89));1436a = UP5(CC_SEL(cc, 114));1437} else {1438b = LERP(3, t, UP5(col0[BCOMP]), UP5(CC_SEL(cc, 79)));1439g = LERP(3, t, UP5(col0[GCOMP]), UP5(CC_SEL(cc, 84)));1440r = LERP(3, t, UP5(col0[RCOMP]), UP5(CC_SEL(cc, 89)));1441a = LERP(3, t, UP5(col0[ACOMP]), UP5(CC_SEL(cc, 114)));1442}1443} else {1444/* lerp == 0 */14451446if (t & 16) {1447cc++;1448t &= 15;1449}1450t = (cc[0] >> (t * 2)) & 3;14511452if (t == 3) {1453/* zero */1454r = g = b = a = 0;1455} else {1456uint32_t kk;1457cc = (const uint32_t *)code;1458a = UP5(cc[3] >> (t * 5 + 13));1459t *= 15;1460cc = (const uint32_t *)(code + 8 + t / 8);1461kk = cc[0] >> (t & 7);1462b = UP5(kk);1463g = UP5(kk >> 5);1464r = UP5(kk >> 10);1465}1466}1467rgba[RCOMP] = r;1468rgba[GCOMP] = g;1469rgba[BCOMP] = b;1470rgba[ACOMP] = a;1471}147214731474static void1475fxt1_decode_1 (const void *texture, int32_t stride, /* in pixels */1476int32_t i, int32_t j, uint8_t *rgba)1477{1478static void (*decode_1[]) (const uint8_t *, int32_t, uint8_t *) = {1479fxt1_decode_1HI, /* cc-high = "00?" */1480fxt1_decode_1HI, /* cc-high = "00?" */1481fxt1_decode_1CHROMA, /* cc-chroma = "010" */1482fxt1_decode_1ALPHA, /* alpha = "011" */1483fxt1_decode_1MIXED, /* mixed = "1??" */1484fxt1_decode_1MIXED, /* mixed = "1??" */1485fxt1_decode_1MIXED, /* mixed = "1??" */1486fxt1_decode_1MIXED /* mixed = "1??" */1487};14881489const uint8_t *code = (const uint8_t *)texture +1490((j / 4) * (stride / 8) + (i / 8)) * 16;1491int32_t mode = CC_SEL(code, 125);1492int32_t t = i & 7;14931494if (t & 4) {1495t += 12;1496}1497t += (j & 3) * 4;14981499decode_1[mode](code, t, rgba);1500}15011502/*1503* Pixel fetch within a block.1504*/15051506void1507util_format_fxt1_rgb_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)1508{1509fxt1_decode_1(src, 0, i, j, dst);1510}15111512void1513util_format_fxt1_rgba_fetch_rgba_8unorm(uint8_t *restrict dst, const uint8_t *restrict src, unsigned i, unsigned j)1514{1515fxt1_decode_1(src, 0, i, j, dst);1516dst[3] = 0xff;1517}15181519void1520util_format_fxt1_rgb_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)1521{1522float *dst = in_dst;1523uint8_t tmp[4];1524fxt1_decode_1(src, 0, i, j, tmp);1525dst[0] = ubyte_to_float(tmp[0]);1526dst[1] = ubyte_to_float(tmp[1]);1527dst[2] = ubyte_to_float(tmp[2]);1528dst[3] = 1.0;1529}15301531void1532util_format_fxt1_rgba_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, unsigned i, unsigned j)1533{1534float *dst = in_dst;1535uint8_t tmp[4];1536fxt1_decode_1(src, 0, i, j, tmp);1537dst[0] = ubyte_to_float(tmp[0]);1538dst[1] = ubyte_to_float(tmp[1]);1539dst[2] = ubyte_to_float(tmp[2]);1540dst[3] = ubyte_to_float(tmp[3]);1541}15421543/*1544* Block decompression.1545*/15461547static inline void1548util_format_fxtn_rgb_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,1549const uint8_t *restrict src_row, unsigned src_stride,1550unsigned width, unsigned height,1551boolean rgba)1552{1553const unsigned bw = 8, bh = 4, comps = 4;1554unsigned x, y, i, j;1555for (y = 0; y < height; y += bh) {1556const uint8_t *src = src_row;1557for (x = 0; x < width; x += bw) {1558for (j = 0; j < bh; ++j) {1559for (i = 0; i < bw; ++i) {1560uint8_t *dst = dst_row + (y + j) * dst_stride / sizeof(*dst_row) + (x + i) * comps;1561fxt1_decode_1(src, 0, i, j, dst);1562if (!rgba)1563dst[3] = 0xff;1564}1565}1566src += FXT1_BLOCK_SIZE;1567}1568src_row += src_stride;1569}1570}15711572void1573util_format_fxt1_rgb_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,1574const uint8_t *restrict src_row, unsigned src_stride,1575unsigned width, unsigned height)1576{1577util_format_fxtn_rgb_unpack_rgba_8unorm(dst_row, dst_stride,1578src_row, src_stride,1579width, height,1580false);1581}15821583void1584util_format_fxt1_rgba_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,1585const uint8_t *restrict src_row, unsigned src_stride,1586unsigned width, unsigned height)1587{1588util_format_fxtn_rgb_unpack_rgba_8unorm(dst_row, dst_stride,1589src_row, src_stride,1590width, height,1591true);1592}15931594static inline void1595util_format_fxtn_rgb_unpack_rgba_float(float *dst_row, unsigned dst_stride,1596const uint8_t *restrict src_row, unsigned src_stride,1597unsigned width, unsigned height,1598boolean rgba)1599{1600const unsigned bw = 8, bh = 4, comps = 4;1601unsigned x, y, i, j;1602for (y = 0; y < height; y += 4) {1603const uint8_t *src = src_row;1604for (x = 0; x < width; x += 8) {1605for (j = 0; j < bh; ++j) {1606for (i = 0; i < bw; ++i) {1607float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i) * comps;1608uint8_t tmp[4];1609fxt1_decode_1(src, 0, i, j, tmp);1610dst[0] = ubyte_to_float(tmp[0]);1611dst[1] = ubyte_to_float(tmp[1]);1612dst[2] = ubyte_to_float(tmp[2]);1613if (rgba)1614dst[3] = ubyte_to_float(tmp[3]);1615else1616dst[3] = 1.0;1617}1618}1619src += FXT1_BLOCK_SIZE;1620}1621src_row += src_stride;1622}1623}16241625void1626util_format_fxt1_rgb_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,1627const uint8_t *restrict src_row, unsigned src_stride,1628unsigned width, unsigned height)1629{1630util_format_fxtn_rgb_unpack_rgba_float(dst_row, dst_stride,1631src_row, src_stride,1632width, height,1633false);1634}16351636void1637util_format_fxt1_rgba_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,1638const uint8_t *restrict src_row, unsigned src_stride,1639unsigned width, unsigned height)1640{1641util_format_fxtn_rgb_unpack_rgba_float(dst_row, dst_stride,1642src_row, src_stride,1643width, height,1644true);1645}16461647/*1648* Block compression.1649*/16501651void1652util_format_fxt1_rgb_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,1653const uint8_t *restrict src, unsigned src_stride,1654unsigned width, unsigned height)1655{1656/* The encoder for FXT1_RGB wants 24bpp packed rgb, so make a temporary to do that.1657*/1658int temp_stride = width * 3;1659uint8_t *temp = malloc(height * temp_stride);1660if (!temp)1661return;16621663for (int y = 0; y < height; y++) {1664for (int x = 0; x < width; x++) {1665temp[y * temp_stride + x * 3 + 0] = src[x * 4 + 0];1666temp[y * temp_stride + x * 3 + 1] = src[x * 4 + 1];1667temp[y * temp_stride + x * 3 + 2] = src[x * 4 + 2];1668}1669src += src_stride;1670}16711672fxt1_encode(width, height, 3, temp, temp_stride, dst_row, dst_stride);16731674free(temp);1675}16761677void1678util_format_fxt1_rgba_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,1679const uint8_t *restrict src, unsigned src_stride,1680unsigned width, unsigned height)1681{1682fxt1_encode(width, height, 4, src, src_stride, dst_row, dst_stride);1683}16841685void1686util_format_fxt1_rgb_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,1687const float *restrict src, unsigned src_stride,1688unsigned width, unsigned height)1689{1690int temp_stride = width * 4;1691uint8_t *temp = malloc(height * temp_stride);1692if (!temp)1693return;16941695util_format_r8g8b8a8_unorm_pack_rgba_float(temp, temp_stride,1696src, src_stride,1697width, height);16981699util_format_fxt1_rgb_pack_rgba_8unorm(dst_row, dst_stride,1700temp, temp_stride,1701width, height);17021703free(temp);1704}17051706void1707util_format_fxt1_rgba_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,1708const float *restrict src, unsigned src_stride,1709unsigned width, unsigned height)1710{1711int temp_stride = width * 4;1712uint8_t *temp = malloc(height * temp_stride);1713if (!temp)1714return;17151716util_format_r8g8b8a8_unorm_pack_rgba_float(temp, temp_stride,1717src, src_stride,1718width, height);17191720util_format_fxt1_rgba_pack_rgba_8unorm(dst_row, dst_stride,1721temp, temp_stride,1722width, height);17231724free(temp);1725}172617271728