summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/texcompress_etc.c
blob: 78af13a27c83d47b11d6bfbe5191de796ac83d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
/*
 * Copyright (C) 2011 LunarG, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file texcompress_etc.c
 * GL_OES_compressed_ETC1_RGB8_texture support.
 * Supported ETC2 texture formats are:
 * GL_COMPRESSED_RGB8_ETC2
 * GL_COMPRESSED_SRGB8_ETC2
 * GL_COMPRESSED_RGBA8_ETC2_EAC
 * GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
 * GL_COMPRESSED_R11_EAC
 * GL_COMPRESSED_RG11_EAC
 */

#include <stdbool.h>
#include "mfeatures.h"
#include "texcompress.h"
#include "texcompress_etc.h"
#include "texstore.h"
#include "macros.h"
#include "swrast/s_context.h"
#include "format_unpack.h"

struct etc2_block {
   int distance;
   uint64_t pixel_indices[2];
   const int *modifier_tables[2];
   bool flipped;
   bool is_ind_mode;
   bool is_diff_mode;
   bool is_t_mode;
   bool is_h_mode;
   bool is_planar_mode;
   uint8_t base_colors[3][3];
   uint8_t paint_colors[4][3];
   uint8_t base_codeword;
   uint8_t multiplier;
   uint8_t table_index;
};

static const int etc2_distance_table[8] = {
   3, 6, 11, 16, 23, 32, 41, 64 };

static const int etc2_modifier_tables[16][8] = {
   {  -3,   -6,   -9,  -15,   2,   5,   8,   14},
   {  -3,   -7,  -10,  -13,   2,   6,   9,   12},
   {  -2,   -5,   -8,  -13,   1,   4,   7,   12},
   {  -2,   -4,   -6,  -13,   1,   3,   5,   12},
   {  -3,   -6,   -8,  -12,   2,   5,   7,   11},
   {  -3,   -7,   -9,  -11,   2,   6,   8,   10},
   {  -4,   -7,   -8,  -11,   3,   6,   7,   10},
   {  -3,   -5,   -8,  -11,   2,   4,   7,   10},
   {  -2,   -6,   -8,  -10,   1,   5,   7,    9},
   {  -2,   -5,   -8,  -10,   1,   4,   7,    9},
   {  -2,   -4,   -8,  -10,   1,   3,   7,    9},
   {  -2,   -5,   -7,  -10,   1,   4,   6,    9},
   {  -3,   -4,   -7,  -10,   2,   3,   6,    9},
   {  -1,   -2,   -3,  -10,   0,   1,   2,    9},
   {  -4,   -6,   -8,   -9,   3,   5,   7,    8},
   {  -3,   -5,   -7,   -9,   2,   4,   6,    8},
};

/* define etc1_parse_block and etc. */
#define UINT8_TYPE GLubyte
#define TAG(x) x
#include "texcompress_etc_tmp.h"
#undef TAG
#undef UINT8_TYPE

GLboolean
_mesa_texstore_etc1_rgb8(TEXSTORE_PARAMS)
{
   /* GL_ETC1_RGB8_OES is only valid in glCompressedTexImage2D */
   ASSERT(0);

   return GL_FALSE;
}

void
_mesa_fetch_texel_2d_f_etc1_rgb8(const struct swrast_texture_image *texImage,
                                 GLint i, GLint j, GLint k, GLfloat *texel)
{
   struct etc1_block block;
   GLubyte dst[3];
   const GLubyte *src;

   src = (const GLubyte *) texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;

   etc1_parse_block(&block, src);
   etc1_fetch_texel(&block, i % 4, j % 4, dst);

   texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
   texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
   texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
   texel[ACOMP] = 1.0f;
}

/**
 * Decode texture data in format `MESA_FORMAT_ETC1_RGB8` to
 * `MESA_FORMAT_ABGR8888`.
 *
 * The size of the source data must be a multiple of the ETC1 block size,
 * which is 8, even if the texture image's dimensions are not aligned to 4.
 * From the GL_OES_compressed_ETC1_RGB8_texture spec:
 *   The texture is described as a number of 4x4 pixel blocks. If the
 *   texture (or a particular mip-level) is smaller than 4 pixels in
 *   any dimension (such as a 2x2 or a 8x1 texture), the texture is
 *   found in the upper left part of the block(s), and the rest of the
 *   pixels are not used. For instance, a texture of size 4x2 will be
 *   placed in the upper half of a 4x4 block, and the lower half of the
 *   pixels in the block will not be accessed.
 *
 * \param src_width in pixels
 * \param src_height in pixels
 * \param dst_stride in bytes
 */
void
_mesa_etc1_unpack_rgba8888(uint8_t *dst_row,
                           unsigned dst_stride,
                           const uint8_t *src_row,
                           unsigned src_stride,
                           unsigned src_width,
                           unsigned src_height)
{
   etc1_unpack_rgba8888(dst_row, dst_stride,
                        src_row, src_stride,
                        src_width, src_height);
}

static uint8_t
etc2_base_color1_t_mode(const uint8_t *in, GLuint index)
{
   uint8_t R1a = 0, x = 0;
   /* base col 1 = extend_4to8bits( (R1a << 2) | R1b, G1, B1) */
   switch(index) {
   case 0:
      R1a = (in[0] >> 3) & 0x3;
      x = ((R1a << 2) | (in[0] & 0x3));
      break;
   case 1:
      x = ((in[1] >> 4) & 0xf);
      break;
   case 2:
      x = (in[1] & 0xf);
      break;
   default:
      /* invalid index */
      break;
   }
   return ((x << 4) | (x & 0xf));
}

static uint8_t
etc2_base_color2_t_mode(const uint8_t *in, GLuint index)
{
   uint8_t x = 0;
   /*extend 4to8bits(R2, G2, B2)*/
   switch(index) {
   case 0:
      x = ((in[2] >> 4) & 0xf );
      break;
   case 1:
      x = (in[2] & 0xf);
      break;
   case 2:
      x = ((in[3] >> 4) & 0xf);
      break;
   default:
      /* invalid index */
      break;
   }
   return ((x << 4) | (x & 0xf));
}

static uint8_t
etc2_base_color1_h_mode(const uint8_t *in, GLuint index)
{
   uint8_t x = 0;
   /* base col 1 = extend 4to8bits(R1, (G1a << 1) | G1b, (B1a << 3) | B1b) */
   switch(index) {
   case 0:
      x = ((in[0] >> 3) & 0xf);
      break;
   case 1:
      x = (((in[0] & 0x7) << 1) | ((in[1] >> 4) & 0x1));
      break;
   case 2:
      x = ((in[1] & 0x8) |
           (((in[1] & 0x3) << 1) | ((in[2] >> 7) & 0x1)));
      break;
   default:
      /* invalid index */
      break;
   }
   return ((x << 4) | (x & 0xf));
 }

static uint8_t
etc2_base_color2_h_mode(const uint8_t *in, GLuint index)
{
   uint8_t x = 0;
   /* base col 2 = extend 4to8bits(R2, G2, B2) */
   switch(index) {
   case 0:
      x = ((in[2] >> 3) & 0xf );
      break;
   case 1:
      x = (((in[2] & 0x7) << 1) | ((in[3] >> 7) & 0x1));
      break;
   case 2:
      x = ((in[3] >> 3) & 0xf);
      break;
   default:
      /* invalid index */
      break;
   }
   return ((x << 4) | (x & 0xf));
 }

static uint8_t
etc2_base_color_o_planar(const uint8_t *in, GLuint index)
{
   GLuint tmp;
   switch(index) {
   case 0:
      tmp = ((in[0] >> 1) & 0x3f); /* RO */
      return ((tmp << 2) | (tmp >> 4));
   case 1:
      tmp = (((in[0] & 0x1) << 6) | /* GO1 */
             ((in[1] >> 1) & 0x3f)); /* GO2 */
      return ((tmp << 1) | (tmp >> 6));
   case 2:
      tmp = (((in[1] & 0x1) << 5) | /* BO1 */
             (in[2] & 0x18) | /* BO2 */
             (((in[2] & 0x3) << 1) | ((in[3] >> 7) & 0x1))); /* BO3 */
      return ((tmp << 2) | (tmp >> 4));
    default:
      /* invalid index */
      return 0;
   }
}

static uint8_t
etc2_base_color_h_planar(const uint8_t *in, GLuint index)
{
   GLuint tmp;
   switch(index) {
   case 0:
      tmp = (((in[3] & 0x7c) >> 1) | /* RH1 */
             (in[3] & 0x1));         /* RH2 */
      return ((tmp << 2) | (tmp >> 4));
   case 1:
      tmp = (in[4] >> 1) & 0x7f; /* GH */
      return ((tmp << 1) | (tmp >> 6));
   case 2:
      tmp = (((in[4] & 0x1) << 5) |
             ((in[5] >> 3) & 0x1f)); /* BH */
      return ((tmp << 2) | (tmp >> 4));
   default:
      /* invalid index */
      return 0;
   }
}

static uint8_t
etc2_base_color_v_planar(const uint8_t *in, GLuint index)
{
   GLuint tmp;
   switch(index) {
   case 0:
      tmp = (((in[5] & 0x7) << 0x3) |
             ((in[6] >> 5) & 0x7)); /* RV */
      return ((tmp << 2) | (tmp >> 4));
   case 1:
      tmp = (((in[6] & 0x1f) << 2) |
             ((in[7] >> 6) & 0x3)); /* GV */
      return ((tmp << 1) | (tmp >> 6));
   case 2:
      tmp = in[7] & 0x3f; /* BV */
      return ((tmp << 2) | (tmp >> 4));
   default:
      /* invalid index */
      return 0;
   }
}

static GLint
etc2_get_pixel_index(const struct etc2_block *block, int x, int y)
{
   int bit = ((3 - y) + (3 - x) * 4) * 3;
   int idx = (block->pixel_indices[1] >> bit) & 0x7;
   return idx;
}

static uint8_t
etc2_clamp(int color)
{
   /* CLAMP(color, 0, 255) */
   return (uint8_t) CLAMP(color, 0, 255);
}

static GLushort
etc2_clamp2(int color)
{
   /* CLAMP(color, 0, 2047) */
   return (GLushort) CLAMP(color, 0, 2047);
}

static void
etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src)
{
   unsigned i;
   GLboolean diffbit = src[3] & 0x2;
   static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 };

   const int R_plus_dR = (src[0] >> 3) + lookup[src[0] & 0x7];
   const int G_plus_dG = (src[1] >> 3) + lookup[src[1] & 0x7];
   const int B_plus_dB = (src[2] >> 3) + lookup[src[2] & 0x7];

   /* Reset the mode flags */
   block->is_ind_mode = false;
   block->is_diff_mode = false;
   block->is_t_mode = false;
   block->is_h_mode = false;
   block->is_planar_mode = false;

   if (!diffbit) {
      /* individual mode */
      block->is_ind_mode = true;

      for (i = 0; i < 3; i++) {
         /* Texture decode algorithm is same for individual mode in etc1
          * & etc2.
          */
         block->base_colors[0][i] = etc1_base_color_ind_hi(src[i]);
         block->base_colors[1][i] = etc1_base_color_ind_lo(src[i]);
      }
   }
   else if (R_plus_dR < 0 || R_plus_dR > 31){
      /* T mode */
      block->is_t_mode = true;

      for(i = 0; i < 3; i++) {
         block->base_colors[0][i] = etc2_base_color1_t_mode(src, i);
         block->base_colors[1][i] = etc2_base_color2_t_mode(src, i);
      }
      /* pick distance */
      block->distance =
         etc2_distance_table[(((src[3] >> 2) & 0x3) << 1) |
                             (src[3] & 0x1)];

      for (i = 0; i < 3; i++) {
         block->paint_colors[0][i] = etc2_clamp(block->base_colors[0][i]);
         block->paint_colors[1][i] = etc2_clamp(block->base_colors[1][i] +
                                                block->distance);
         block->paint_colors[2][i] = etc2_clamp(block->base_colors[1][i]);
         block->paint_colors[3][i] = etc2_clamp(block->base_colors[1][i] -
                                                block->distance);
      }
   }
   else if (G_plus_dG < 0 || G_plus_dG > 31){
      /* H mode */
      block->is_h_mode = true;
      int base_color_1_value, base_color_2_value;

      for(i = 0; i < 3; i++) {
         block->base_colors[0][i] = etc2_base_color1_h_mode(src, i);
         block->base_colors[1][i] = etc2_base_color2_h_mode(src, i);
      }

      base_color_1_value = (block->base_colors[0][0] << 16) +
                           (block->base_colors[0][1] << 8) +
                           block->base_colors[0][2];
      base_color_2_value = (block->base_colors[1][0] << 16) +
                           (block->base_colors[1][1] << 8) +
                           block->base_colors[1][2];
      /* pick distance */
      block->distance =
         etc2_distance_table[(src[3] & 0x4) |
                             ((src[3] & 0x1) << 1) |
                             (base_color_1_value >= base_color_2_value)];

      for (i = 0; i < 3; i++) {
         block->paint_colors[0][i] = etc2_clamp(block->base_colors[0][i] +
                                                block->distance);
         block->paint_colors[1][i] = etc2_clamp(block->base_colors[0][i] -
                                                block->distance);
         block->paint_colors[2][i] = etc2_clamp(block->base_colors[1][i] +
                                                block->distance);
         block->paint_colors[3][i] = etc2_clamp(block->base_colors[1][i] -
                                                block->distance);
      }
   }
   else if (B_plus_dB < 0 || B_plus_dB > 31){
      /* Planar mode */
      block->is_planar_mode = true;

      for (i = 0; i < 3; i++) {
         block->base_colors[0][i] = etc2_base_color_o_planar(src, i);
         block->base_colors[1][i] = etc2_base_color_h_planar(src, i);
         block->base_colors[2][i] = etc2_base_color_v_planar(src, i);
      }
   }
   else if (diffbit) {
      /* differential mode */
      block->is_diff_mode = true;

      for (i = 0; i < 3; i++) {
         /* Texture decode algorithm is same for differential mode in etc1
          * & etc2.
          */
         block->base_colors[0][i] = etc1_base_color_diff_hi(src[i]);
         block->base_colors[1][i] = etc1_base_color_diff_lo(src[i]);
     }
   }

   if (block->is_ind_mode || block->is_diff_mode) {
      /* pick modifier tables. same for etc1 & etc2 textures */
      block->modifier_tables[0] = etc1_modifier_tables[(src[3] >> 5) & 0x7];
      block->modifier_tables[1] = etc1_modifier_tables[(src[3] >> 2) & 0x7];
      block->flipped = (src[3] & 0x1);
   }

   block->pixel_indices[0] =
      (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7];
}

static void
etc2_rgb8_fetch_texel(const struct etc2_block *block,
      int x, int y, uint8_t *dst)
{
   const uint8_t *base_color;
   int modifier, bit, idx, blk;

   /* get pixel index */
   bit = y + x * 4;
   idx = ((block->pixel_indices[0] >> (15 + bit)) & 0x2) |
         ((block->pixel_indices[0] >>      (bit)) & 0x1);

   if (block->is_ind_mode || block->is_diff_mode) {
      /* Use pixel index and subblock to get the modifier */
      blk = (block->flipped) ? (y >= 2) : (x >= 2);
      base_color = block->base_colors[blk];
      modifier = block->modifier_tables[blk][idx];

      dst[0] = etc2_clamp(base_color[0] + modifier);
      dst[1] = etc2_clamp(base_color[1] + modifier);
      dst[2] = etc2_clamp(base_color[2] + modifier);
   }
   else if (block->is_t_mode || block->is_h_mode) {
      /* Use pixel index to pick one of the paint colors */
      dst[0] = block->paint_colors[idx][0];
      dst[1] = block->paint_colors[idx][1];
      dst[2] = block->paint_colors[idx][2];
   }
   else if (block->is_planar_mode) {
      /* {R(x, y) = clamp255((x × (RH − RO) + y × (RV − RO) + 4 × RO + 2) >> 2)
       * {G(x, y) = clamp255((x × (GH − GO) + y × (GV − GO) + 4 × GO + 2) >> 2)
       * {B(x, y) = clamp255((x × (BH − BO) + y × (BV − BO) + 4 × BO + 2) >> 2)
       */
      int red, green, blue;
      red = (x * (block->base_colors[1][0] - block->base_colors[0][0]) +
             y * (block->base_colors[2][0] - block->base_colors[0][0]) +
             4 * block->base_colors[0][0] + 2) >> 2;

      green = (x * (block->base_colors[1][1] - block->base_colors[0][1]) +
               y * (block->base_colors[2][1] - block->base_colors[0][1]) +
               4 * block->base_colors[0][1] + 2) >> 2;

      blue = (x * (block->base_colors[1][2] - block->base_colors[0][2]) +
              y * (block->base_colors[2][2] - block->base_colors[0][2]) +
              4 * block->base_colors[0][2] + 2) >> 2;

      dst[0] = etc2_clamp(red);
      dst[1] = etc2_clamp(green);
      dst[2] = etc2_clamp(blue);
  }
}

static void
etc2_alpha8_fetch_texel(const struct etc2_block *block,
      int x, int y, uint8_t *dst)
{
   int modifier, alpha, idx;
   /* get pixel index */
   idx = etc2_get_pixel_index(block, x, y);
   modifier = etc2_modifier_tables[block->table_index][idx];
   alpha = block->base_codeword + modifier * block->multiplier;
   dst[3] = etc2_clamp(alpha);
}

static void
etc2_r11_fetch_texel(const struct etc2_block *block,
                     int x, int y, uint8_t *dst)
{
   GLint modifier, idx;
   GLshort color;
   /* Get pixel index */
   idx = etc2_get_pixel_index(block, x, y);
   modifier = etc2_modifier_tables[block->table_index][idx];

   if (block->multiplier != 0)
      /* clamp2(base codeword × 8 + 4 + modifier × multiplier × 8) */
      color = etc2_clamp2(((block->base_codeword << 3) | 0x4)  +
                          ((modifier * block->multiplier) << 3));
   else
      color = etc2_clamp2(((block->base_codeword << 3) | 0x4)  + modifier);

   /* Extend 11 bits color value to 16 bits. OpenGL ES 3.0 specification
    * allows extending the color value to any number of bits. But, an
    * implementation is not allowed to truncate the 11-bit value to less than
    * 11 bits."
    */
   color = (color << 5) | (color >> 6);
   ((GLushort *)dst)[0] = color;
}

static void
etc2_alpha8_parse_block(struct etc2_block *block, const uint8_t *src)
{
   block->base_codeword = src[0];
   block->multiplier = (src[1] >> 4) & 0xf;
   block->table_index = src[1] & 0xf;
   block->pixel_indices[1] = (((uint64_t)src[2] << 40) |
                              ((uint64_t)src[3] << 32) |
                              ((uint64_t)src[4] << 24) |
                              ((uint64_t)src[5] << 16) |
                              ((uint64_t)src[6] << 8)  |
                              ((uint64_t)src[7]));
}

static void
etc2_r11_parse_block(struct etc2_block *block, const uint8_t *src)
{
   /* Parsing logic remains same as for etc2_alpha8_parse_block */
    etc2_alpha8_parse_block(block, src);
}

static void
etc2_rgba8_parse_block(struct etc2_block *block, const uint8_t *src)
{
   /* RGB component is parsed the same way as for MESA_FORMAT_ETC2_RGB8 */
   etc2_rgb8_parse_block(block, src + 8);

   /* Parse Alpha component */
   etc2_alpha8_parse_block(block, src);
}

static void
etc2_rgba8_fetch_texel(const struct etc2_block *block,
      int x, int y, uint8_t *dst)
{
   etc2_rgb8_fetch_texel(block, x, y, dst);
   etc2_alpha8_fetch_texel(block, x, y, dst);
}

static void
etc2_unpack_rgb8(uint8_t *dst_row,
                 unsigned dst_stride,
                 const uint8_t *src_row,
                 unsigned src_stride,
                 unsigned width,
                 unsigned height)
{
   const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
   struct etc2_block block;
   unsigned x, y, i, j;

   for (y = 0; y < height; y += bh) {
      const uint8_t *src = src_row;

      for (x = 0; x < width; x+= bw) {
         etc2_rgb8_parse_block(&block, src);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
            for (i = 0; i < bw; i++) {
               etc2_rgb8_fetch_texel(&block, i, j, dst);
               dst[3] = 255;
               dst += comps;
            }
         }

         src += bs;
      }

      src_row += src_stride;
   }
}

static void
etc2_unpack_srgb8(uint8_t *dst_row,
                  unsigned dst_stride,
                  const uint8_t *src_row,
                  unsigned src_stride,
                  unsigned width,
                  unsigned height)
{
   const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
   struct etc2_block block;
   unsigned x, y, i, j;
   uint8_t tmp;

   for (y = 0; y < height; y += bh) {
      const uint8_t *src = src_row;

      for (x = 0; x < width; x+= bw) {
         etc2_rgb8_parse_block(&block, src);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
            for (i = 0; i < bw; i++) {
               etc2_rgb8_fetch_texel(&block, i, j, dst);
               /* Convert to MESA_FORMAT_SARGB8 */
               tmp = dst[0];
               dst[0] = dst[2];
               dst[2] = tmp;
               dst[3] = 255;

               dst += comps;
            }
         }
         src += bs;
       }

      src_row += src_stride;
    }
}

static void
etc2_unpack_rgba8(uint8_t *dst_row,
                  unsigned dst_stride,
                  const uint8_t *src_row,
                  unsigned src_stride,
                  unsigned width,
                  unsigned height)
{
   /* If internalformat is COMPRESSED_RGBA8_ETC2_EAC, each 4 × 4 block of
    * RGBA8888 information is compressed to 128 bits. To decode a block, the
    * two 64-bit integers int64bitAlpha and int64bitColor are calculated.
   */
   const unsigned bw = 4, bh = 4, bs = 16, comps = 4;
   struct etc2_block block;
   unsigned x, y, i, j;

   for (y = 0; y < height; y += bh) {
      const uint8_t *src = src_row;

      for (x = 0; x < width; x+= bw) {
         etc2_rgba8_parse_block(&block, src);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
            for (i = 0; i < bw; i++) {
               etc2_rgba8_fetch_texel(&block, i, j, dst);
               dst += comps;
            }
         }
         src += bs;
       }

      src_row += src_stride;
    }
}

static void
etc2_unpack_srgb8_alpha8(uint8_t *dst_row,
                         unsigned dst_stride,
                         const uint8_t *src_row,
                         unsigned src_stride,
                         unsigned width,
                         unsigned height)
{
   /* If internalformat is COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, each 4 × 4 block
    * of RGBA8888 information is compressed to 128 bits. To decode a block, the
    * two 64-bit integers int64bitAlpha and int64bitColor are calculated.
    */
   const unsigned bw = 4, bh = 4, bs = 16, comps = 4;
   struct etc2_block block;
   unsigned x, y, i, j;
   uint8_t tmp;

   for (y = 0; y < height; y += bh) {
      const uint8_t *src = src_row;

      for (x = 0; x < width; x+= bw) {
         etc2_rgba8_parse_block(&block, src);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
            for (i = 0; i < bw; i++) {
               etc2_rgba8_fetch_texel(&block, i, j, dst);

               /* Convert to MESA_FORMAT_SARGB8 */
               tmp = dst[0];
               dst[0] = dst[2];
               dst[2] = tmp;
               dst[3] = dst[3];

               dst += comps;
            }
         }
         src += bs;
       }

      src_row += src_stride;
    }
}

static void
etc2_unpack_r11(uint8_t *dst_row,
                unsigned dst_stride,
                const uint8_t *src_row,
                unsigned src_stride,
                unsigned width,
                unsigned height)
{
   /* If internalformat is COMPRESSED_R11_EAC, each 4 × 4 block of
      color information is compressed to 64 bits.
   */
   const unsigned bw = 4, bh = 4, bs = 8, comps = 1, comp_size = 2;
   struct etc2_block block;
   unsigned x, y, i, j;

   for (y = 0; y < height; y += bh) {
      const uint8_t *src = src_row;

      for (x = 0; x < width; x+= bw) {
         etc2_r11_parse_block(&block, src);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps * comp_size;
            for (i = 0; i < bw; i++) {
               etc2_r11_fetch_texel(&block, i, j, dst);
               dst += comps * comp_size;
            }
         }
         src += bs;
       }

      src_row += src_stride;
    }
}

static void
etc2_unpack_rg11(uint8_t *dst_row,
                 unsigned dst_stride,
                 const uint8_t *src_row,
                 unsigned src_stride,
                 unsigned width,
                 unsigned height)
{
   /* If internalformat is COMPRESSED_RG11_EAC, each 4 × 4 block of
      RG color information is compressed to 128 bits.
   */
   const unsigned bw = 4, bh = 4, bs = 16, comps = 2, comp_size = 2;
   struct etc2_block block;
   unsigned x, y, i, j;

   for (y = 0; y < height; y += bh) {
      const uint8_t *src = src_row;

      for (x = 0; x < width; x+= bw) {
         /* red component */
         etc2_r11_parse_block(&block, src);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride +
                           x * comps * comp_size;
            for (i = 0; i < bw; i++) {
               etc2_r11_fetch_texel(&block, i, j, dst);
               dst += comps * comp_size;
            }
         }
         /* green component */
         etc2_r11_parse_block(&block, src + 8);

         for (j = 0; j < bh; j++) {
            uint8_t *dst = dst_row + (y + j) * dst_stride +
                           x * comps * comp_size;
            for (i = 0; i < bw; i++) {
               etc2_r11_fetch_texel(&block, i, j, dst + comp_size);
               dst += comps * comp_size;
            }
         }
         src += bs;
       }

      src_row += src_stride;
    }
}

/* ETC2 texture formats are valid in glCompressedTexImage2D and
 * glCompressedTexSubImage2D functions */
GLboolean
_mesa_texstore_etc2_rgb8(TEXSTORE_PARAMS)
{
   ASSERT(0);

   return GL_FALSE;
}

GLboolean
_mesa_texstore_etc2_srgb8(TEXSTORE_PARAMS)
{
   ASSERT(0);

   return GL_FALSE;
}

GLboolean
_mesa_texstore_etc2_rgba8_eac(TEXSTORE_PARAMS)
{
   ASSERT(0);

   return GL_FALSE;
}

GLboolean
_mesa_texstore_etc2_srgb8_alpha8_eac(TEXSTORE_PARAMS)
{
   ASSERT(0);

   return GL_FALSE;
}

GLboolean
_mesa_texstore_etc2_r11_eac(TEXSTORE_PARAMS)
{
   ASSERT(0);

   return GL_FALSE;
}

GLboolean
_mesa_texstore_etc2_rg11_eac(TEXSTORE_PARAMS)
{
   ASSERT(0);

   return GL_FALSE;
}

void
_mesa_fetch_texel_2d_f_etc2_rgb8(const struct swrast_texture_image *texImage,
                                 GLint i, GLint j, GLint k, GLfloat *texel)
{
   struct etc2_block block;
   uint8_t dst[3];
   const uint8_t *src;

   src = texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;

   etc2_rgb8_parse_block(&block, src);
   etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst);

   texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
   texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
   texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
   texel[ACOMP] = 1.0f;
}

void
_mesa_fetch_texel_2d_f_etc2_srgb8(const struct swrast_texture_image *texImage,
                                  GLint i, GLint j, GLint k, GLfloat *texel)
{
   struct etc2_block block;
   uint8_t dst[3];
   const uint8_t *src;

   src = texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;

   etc2_rgb8_parse_block(&block, src);
   etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst);

   texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
   texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
   texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
   texel[ACOMP] = 1.0f;
}

void
_mesa_fetch_texel_2d_f_etc2_rgba8_eac(const struct swrast_texture_image *texImage,
                                      GLint i, GLint j, GLint k, GLfloat *texel)
{
   struct etc2_block block;
   uint8_t dst[4];
   const uint8_t *src;

   src = texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   etc2_rgba8_parse_block(&block, src);
   etc2_rgba8_fetch_texel(&block, i % 4, j % 4, dst);

   texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
   texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
   texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
   texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
}

void
_mesa_fetch_texel_2d_f_etc2_srgb8_alpha8_eac(const struct
                                             swrast_texture_image *texImage,
                                             GLint i, GLint j,
                                             GLint k, GLfloat *texel)
{
   struct etc2_block block;
   uint8_t dst[4];
   const uint8_t *src;

   src = texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   etc2_rgba8_parse_block(&block, src);
   etc2_rgba8_fetch_texel(&block, i % 4, j % 4, dst);

   texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
   texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
   texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
   texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
}

void
_mesa_fetch_texel_2d_f_etc2_r11_eac(const struct swrast_texture_image *texImage,
                                    GLint i, GLint j, GLint k, GLfloat *texel)
{
   struct etc2_block block;
   GLushort dst;
   const uint8_t *src;

   src = texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;

   etc2_r11_parse_block(&block, src);
   etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)&dst);

   texel[RCOMP] = USHORT_TO_FLOAT(dst);
   texel[GCOMP] = 0.0f;
   texel[BCOMP] = 0.0f;
   texel[ACOMP] = 1.0f;
}

void
_mesa_fetch_texel_2d_f_etc2_rg11_eac(const struct
                                     swrast_texture_image *texImage,
                                     GLint i, GLint j,
                                     GLint k, GLfloat *texel)
{
   struct etc2_block block;
   GLushort dst[2];
   const uint8_t *src;

   src = texImage->Map +
      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   /* red component */
   etc2_r11_parse_block(&block, src);
   etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)dst);

   /* green component */
   etc2_r11_parse_block(&block, src + 8);
   etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)(dst + 1));

   texel[RCOMP] = USHORT_TO_FLOAT(dst[0]);
   texel[GCOMP] = USHORT_TO_FLOAT(dst[1]);
   texel[BCOMP] = 0.0f;
   texel[ACOMP] = 1.0f;
}

/**
 * Decode texture data in any one of following formats:
 * `MESA_FORMAT_ETC2_RGB8`
 * `MESA_FORMAT_ETC2_SRGB8`
 * `MESA_FORMAT_ETC2_RGBA8_EAC`
 * `MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC`
 * `MESA_FORMAT_ETC2_R11_EAC`
 * `MESA_FORMAT_ETC2_RG11_EAC`
 *
 * The size of the source data must be a multiple of the ETC2 block size
 * even if the texture image's dimensions are not aligned to 4.
 *
 * \param src_width in pixels
 * \param src_height in pixels
 * \param dst_stride in bytes
 */

void
_mesa_unpack_etc2_format(uint8_t *dst_row,
                         unsigned dst_stride,
                         const uint8_t *src_row,
                         unsigned src_stride,
                         unsigned src_width,
                         unsigned src_height,
                         gl_format format)
{
   if (format == MESA_FORMAT_ETC2_RGB8)
      etc2_unpack_rgb8(dst_row, dst_stride,
                       src_row, src_stride,
                       src_width, src_height);
   else if (format == MESA_FORMAT_ETC2_SRGB8)
      etc2_unpack_srgb8(dst_row, dst_stride,
                        src_row, src_stride,
                        src_width, src_height);
   else if (format == MESA_FORMAT_ETC2_RGBA8_EAC)
      etc2_unpack_rgba8(dst_row, dst_stride,
                        src_row, src_stride,
                        src_width, src_height);
   else if (format == MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC)
      etc2_unpack_srgb8_alpha8(dst_row, dst_stride,
                               src_row, src_stride,
                               src_width, src_height);
   else if (format == MESA_FORMAT_ETC2_R11_EAC)
      etc2_unpack_r11(dst_row, dst_stride,
                      src_row, src_stride,
                      src_width, src_height);
   else if (format == MESA_FORMAT_ETC2_RG11_EAC)
      etc2_unpack_rg11(dst_row, dst_stride,
                       src_row, src_stride,
                       src_width, src_height);
}