mesa: Allow setting GL_TEXTURE_MAX_LEVEL to 0 with GL_TEXTURE_RECTANGLE.
[mesa.git] / src / mesa / main / texcompress_etc.c
1 /*
2 * Copyright (C) 2011 LunarG, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file texcompress_etc.c
26 * GL_OES_compressed_ETC1_RGB8_texture support.
27 * Supported ETC2 texture formats are:
28 * GL_COMPRESSED_RGB8_ETC2
29 * GL_COMPRESSED_SRGB8_ETC2
30 * GL_COMPRESSED_RGBA8_ETC2_EAC
31 * GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
32 * GL_COMPRESSED_R11_EAC
33 * GL_COMPRESSED_RG11_EAC
34 * GL_COMPRESSED_SIGNED_R11_EAC
35 * GL_COMPRESSED_SIGNED_RG11_EAC
36 * MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
37 * MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
38 */
39
40 #include <stdbool.h>
41 #include "texcompress.h"
42 #include "texcompress_etc.h"
43 #include "texstore.h"
44 #include "macros.h"
45 #include "format_unpack.h"
46
47
48 struct etc2_block {
49 int distance;
50 uint64_t pixel_indices[2];
51 const int *modifier_tables[2];
52 bool flipped;
53 bool opaque;
54 bool is_ind_mode;
55 bool is_diff_mode;
56 bool is_t_mode;
57 bool is_h_mode;
58 bool is_planar_mode;
59 uint8_t base_colors[3][3];
60 uint8_t paint_colors[4][3];
61 uint8_t base_codeword;
62 uint8_t multiplier;
63 uint8_t table_index;
64 };
65
66 static const int etc2_distance_table[8] = {
67 3, 6, 11, 16, 23, 32, 41, 64 };
68
69 static const int etc2_modifier_tables[16][8] = {
70 { -3, -6, -9, -15, 2, 5, 8, 14},
71 { -3, -7, -10, -13, 2, 6, 9, 12},
72 { -2, -5, -8, -13, 1, 4, 7, 12},
73 { -2, -4, -6, -13, 1, 3, 5, 12},
74 { -3, -6, -8, -12, 2, 5, 7, 11},
75 { -3, -7, -9, -11, 2, 6, 8, 10},
76 { -4, -7, -8, -11, 3, 6, 7, 10},
77 { -3, -5, -8, -11, 2, 4, 7, 10},
78 { -2, -6, -8, -10, 1, 5, 7, 9},
79 { -2, -5, -8, -10, 1, 4, 7, 9},
80 { -2, -4, -8, -10, 1, 3, 7, 9},
81 { -2, -5, -7, -10, 1, 4, 6, 9},
82 { -3, -4, -7, -10, 2, 3, 6, 9},
83 { -1, -2, -3, -10, 0, 1, 2, 9},
84 { -4, -6, -8, -9, 3, 5, 7, 8},
85 { -3, -5, -7, -9, 2, 4, 6, 8},
86 };
87
88 static const int etc2_modifier_tables_non_opaque[8][4] = {
89 { 0, 8, 0, -8},
90 { 0, 17, 0, -17},
91 { 0, 29, 0, -29},
92 { 0, 42, 0, -42},
93 { 0, 60, 0, -60},
94 { 0, 80, 0, -80},
95 { 0, 106, 0, -106},
96 { 0, 183, 0, -183}
97 };
98
99 /* define etc1_parse_block and etc. */
100 #define UINT8_TYPE GLubyte
101 #define TAG(x) x
102 #include "texcompress_etc_tmp.h"
103 #undef TAG
104 #undef UINT8_TYPE
105
106 GLboolean
107 _mesa_texstore_etc1_rgb8(TEXSTORE_PARAMS)
108 {
109 /* GL_ETC1_RGB8_OES is only valid in glCompressedTexImage2D */
110 ASSERT(0);
111
112 return GL_FALSE;
113 }
114
115
116 /**
117 * Decode texture data in format `MESA_FORMAT_ETC1_RGB8` to
118 * `MESA_FORMAT_ABGR8888`.
119 *
120 * The size of the source data must be a multiple of the ETC1 block size,
121 * which is 8, even if the texture image's dimensions are not aligned to 4.
122 * From the GL_OES_compressed_ETC1_RGB8_texture spec:
123 * The texture is described as a number of 4x4 pixel blocks. If the
124 * texture (or a particular mip-level) is smaller than 4 pixels in
125 * any dimension (such as a 2x2 or a 8x1 texture), the texture is
126 * found in the upper left part of the block(s), and the rest of the
127 * pixels are not used. For instance, a texture of size 4x2 will be
128 * placed in the upper half of a 4x4 block, and the lower half of the
129 * pixels in the block will not be accessed.
130 *
131 * \param src_width in pixels
132 * \param src_height in pixels
133 * \param dst_stride in bytes
134 */
135 void
136 _mesa_etc1_unpack_rgba8888(uint8_t *dst_row,
137 unsigned dst_stride,
138 const uint8_t *src_row,
139 unsigned src_stride,
140 unsigned src_width,
141 unsigned src_height)
142 {
143 etc1_unpack_rgba8888(dst_row, dst_stride,
144 src_row, src_stride,
145 src_width, src_height);
146 }
147
148 static uint8_t
149 etc2_base_color1_t_mode(const uint8_t *in, GLuint index)
150 {
151 uint8_t R1a = 0, x = 0;
152 /* base col 1 = extend_4to8bits( (R1a << 2) | R1b, G1, B1) */
153 switch(index) {
154 case 0:
155 R1a = (in[0] >> 3) & 0x3;
156 x = ((R1a << 2) | (in[0] & 0x3));
157 break;
158 case 1:
159 x = ((in[1] >> 4) & 0xf);
160 break;
161 case 2:
162 x = (in[1] & 0xf);
163 break;
164 default:
165 /* invalid index */
166 break;
167 }
168 return ((x << 4) | (x & 0xf));
169 }
170
171 static uint8_t
172 etc2_base_color2_t_mode(const uint8_t *in, GLuint index)
173 {
174 uint8_t x = 0;
175 /*extend 4to8bits(R2, G2, B2)*/
176 switch(index) {
177 case 0:
178 x = ((in[2] >> 4) & 0xf );
179 break;
180 case 1:
181 x = (in[2] & 0xf);
182 break;
183 case 2:
184 x = ((in[3] >> 4) & 0xf);
185 break;
186 default:
187 /* invalid index */
188 break;
189 }
190 return ((x << 4) | (x & 0xf));
191 }
192
193 static uint8_t
194 etc2_base_color1_h_mode(const uint8_t *in, GLuint index)
195 {
196 uint8_t x = 0;
197 /* base col 1 = extend 4to8bits(R1, (G1a << 1) | G1b, (B1a << 3) | B1b) */
198 switch(index) {
199 case 0:
200 x = ((in[0] >> 3) & 0xf);
201 break;
202 case 1:
203 x = (((in[0] & 0x7) << 1) | ((in[1] >> 4) & 0x1));
204 break;
205 case 2:
206 x = ((in[1] & 0x8) |
207 (((in[1] & 0x3) << 1) | ((in[2] >> 7) & 0x1)));
208 break;
209 default:
210 /* invalid index */
211 break;
212 }
213 return ((x << 4) | (x & 0xf));
214 }
215
216 static uint8_t
217 etc2_base_color2_h_mode(const uint8_t *in, GLuint index)
218 {
219 uint8_t x = 0;
220 /* base col 2 = extend 4to8bits(R2, G2, B2) */
221 switch(index) {
222 case 0:
223 x = ((in[2] >> 3) & 0xf );
224 break;
225 case 1:
226 x = (((in[2] & 0x7) << 1) | ((in[3] >> 7) & 0x1));
227 break;
228 case 2:
229 x = ((in[3] >> 3) & 0xf);
230 break;
231 default:
232 /* invalid index */
233 break;
234 }
235 return ((x << 4) | (x & 0xf));
236 }
237
238 static uint8_t
239 etc2_base_color_o_planar(const uint8_t *in, GLuint index)
240 {
241 GLuint tmp;
242 switch(index) {
243 case 0:
244 tmp = ((in[0] >> 1) & 0x3f); /* RO */
245 return ((tmp << 2) | (tmp >> 4));
246 case 1:
247 tmp = (((in[0] & 0x1) << 6) | /* GO1 */
248 ((in[1] >> 1) & 0x3f)); /* GO2 */
249 return ((tmp << 1) | (tmp >> 6));
250 case 2:
251 tmp = (((in[1] & 0x1) << 5) | /* BO1 */
252 (in[2] & 0x18) | /* BO2 */
253 (((in[2] & 0x3) << 1) | ((in[3] >> 7) & 0x1))); /* BO3 */
254 return ((tmp << 2) | (tmp >> 4));
255 default:
256 /* invalid index */
257 return 0;
258 }
259 }
260
261 static uint8_t
262 etc2_base_color_h_planar(const uint8_t *in, GLuint index)
263 {
264 GLuint tmp;
265 switch(index) {
266 case 0:
267 tmp = (((in[3] & 0x7c) >> 1) | /* RH1 */
268 (in[3] & 0x1)); /* RH2 */
269 return ((tmp << 2) | (tmp >> 4));
270 case 1:
271 tmp = (in[4] >> 1) & 0x7f; /* GH */
272 return ((tmp << 1) | (tmp >> 6));
273 case 2:
274 tmp = (((in[4] & 0x1) << 5) |
275 ((in[5] >> 3) & 0x1f)); /* BH */
276 return ((tmp << 2) | (tmp >> 4));
277 default:
278 /* invalid index */
279 return 0;
280 }
281 }
282
283 static uint8_t
284 etc2_base_color_v_planar(const uint8_t *in, GLuint index)
285 {
286 GLuint tmp;
287 switch(index) {
288 case 0:
289 tmp = (((in[5] & 0x7) << 0x3) |
290 ((in[6] >> 5) & 0x7)); /* RV */
291 return ((tmp << 2) | (tmp >> 4));
292 case 1:
293 tmp = (((in[6] & 0x1f) << 2) |
294 ((in[7] >> 6) & 0x3)); /* GV */
295 return ((tmp << 1) | (tmp >> 6));
296 case 2:
297 tmp = in[7] & 0x3f; /* BV */
298 return ((tmp << 2) | (tmp >> 4));
299 default:
300 /* invalid index */
301 return 0;
302 }
303 }
304
305 static GLint
306 etc2_get_pixel_index(const struct etc2_block *block, int x, int y)
307 {
308 int bit = ((3 - y) + (3 - x) * 4) * 3;
309 int idx = (block->pixel_indices[1] >> bit) & 0x7;
310 return idx;
311 }
312
313 static uint8_t
314 etc2_clamp(int color)
315 {
316 /* CLAMP(color, 0, 255) */
317 return (uint8_t) CLAMP(color, 0, 255);
318 }
319
320 static GLushort
321 etc2_clamp2(int color)
322 {
323 /* CLAMP(color, 0, 2047) */
324 return (GLushort) CLAMP(color, 0, 2047);
325 }
326
327 static GLshort
328 etc2_clamp3(int color)
329 {
330 /* CLAMP(color, -1023, 1023) */
331 return (GLshort) CLAMP(color, -1023, 1023);
332 }
333
334 static void
335 etc2_rgb8_parse_block(struct etc2_block *block,
336 const uint8_t *src,
337 GLboolean punchthrough_alpha)
338 {
339 unsigned i;
340 GLboolean diffbit = false;
341 static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 };
342
343 const int R_plus_dR = (src[0] >> 3) + lookup[src[0] & 0x7];
344 const int G_plus_dG = (src[1] >> 3) + lookup[src[1] & 0x7];
345 const int B_plus_dB = (src[2] >> 3) + lookup[src[2] & 0x7];
346
347 /* Reset the mode flags */
348 block->is_ind_mode = false;
349 block->is_diff_mode = false;
350 block->is_t_mode = false;
351 block->is_h_mode = false;
352 block->is_planar_mode = false;
353
354 if (punchthrough_alpha)
355 block->opaque = src[3] & 0x2;
356 else
357 diffbit = src[3] & 0x2;
358
359 if (!diffbit && !punchthrough_alpha) {
360 /* individual mode */
361 block->is_ind_mode = true;
362
363 for (i = 0; i < 3; i++) {
364 /* Texture decode algorithm is same for individual mode in etc1
365 * & etc2.
366 */
367 block->base_colors[0][i] = etc1_base_color_ind_hi(src[i]);
368 block->base_colors[1][i] = etc1_base_color_ind_lo(src[i]);
369 }
370 }
371 else if (R_plus_dR < 0 || R_plus_dR > 31){
372 /* T mode */
373 block->is_t_mode = true;
374
375 for(i = 0; i < 3; i++) {
376 block->base_colors[0][i] = etc2_base_color1_t_mode(src, i);
377 block->base_colors[1][i] = etc2_base_color2_t_mode(src, i);
378 }
379 /* pick distance */
380 block->distance =
381 etc2_distance_table[(((src[3] >> 2) & 0x3) << 1) |
382 (src[3] & 0x1)];
383
384 for (i = 0; i < 3; i++) {
385 block->paint_colors[0][i] = etc2_clamp(block->base_colors[0][i]);
386 block->paint_colors[1][i] = etc2_clamp(block->base_colors[1][i] +
387 block->distance);
388 block->paint_colors[2][i] = etc2_clamp(block->base_colors[1][i]);
389 block->paint_colors[3][i] = etc2_clamp(block->base_colors[1][i] -
390 block->distance);
391 }
392 }
393 else if (G_plus_dG < 0 || G_plus_dG > 31){
394 int base_color_1_value, base_color_2_value;
395
396 /* H mode */
397 block->is_h_mode = true;
398
399 for(i = 0; i < 3; i++) {
400 block->base_colors[0][i] = etc2_base_color1_h_mode(src, i);
401 block->base_colors[1][i] = etc2_base_color2_h_mode(src, i);
402 }
403
404 base_color_1_value = (block->base_colors[0][0] << 16) +
405 (block->base_colors[0][1] << 8) +
406 block->base_colors[0][2];
407 base_color_2_value = (block->base_colors[1][0] << 16) +
408 (block->base_colors[1][1] << 8) +
409 block->base_colors[1][2];
410 /* pick distance */
411 block->distance =
412 etc2_distance_table[(src[3] & 0x4) |
413 ((src[3] & 0x1) << 1) |
414 (base_color_1_value >= base_color_2_value)];
415
416 for (i = 0; i < 3; i++) {
417 block->paint_colors[0][i] = etc2_clamp(block->base_colors[0][i] +
418 block->distance);
419 block->paint_colors[1][i] = etc2_clamp(block->base_colors[0][i] -
420 block->distance);
421 block->paint_colors[2][i] = etc2_clamp(block->base_colors[1][i] +
422 block->distance);
423 block->paint_colors[3][i] = etc2_clamp(block->base_colors[1][i] -
424 block->distance);
425 }
426 }
427 else if (B_plus_dB < 0 || B_plus_dB > 31) {
428 /* Planar mode */
429 block->is_planar_mode = true;
430
431 /* opaque bit must be set in planar mode */
432 block->opaque = true;
433
434 for (i = 0; i < 3; i++) {
435 block->base_colors[0][i] = etc2_base_color_o_planar(src, i);
436 block->base_colors[1][i] = etc2_base_color_h_planar(src, i);
437 block->base_colors[2][i] = etc2_base_color_v_planar(src, i);
438 }
439 }
440 else if (diffbit || punchthrough_alpha) {
441 /* differential mode */
442 block->is_diff_mode = true;
443
444 for (i = 0; i < 3; i++) {
445 /* Texture decode algorithm is same for differential mode in etc1
446 * & etc2.
447 */
448 block->base_colors[0][i] = etc1_base_color_diff_hi(src[i]);
449 block->base_colors[1][i] = etc1_base_color_diff_lo(src[i]);
450 }
451 }
452
453 if (block->is_ind_mode || block->is_diff_mode) {
454 int table1_idx = (src[3] >> 5) & 0x7;
455 int table2_idx = (src[3] >> 2) & 0x7;
456
457 /* Use same modifier tables as for etc1 textures if opaque bit is set
458 * or if non punchthrough texture format
459 */
460 block->modifier_tables[0] = (block->opaque || !punchthrough_alpha) ?
461 etc1_modifier_tables[table1_idx] :
462 etc2_modifier_tables_non_opaque[table1_idx];
463 block->modifier_tables[1] = (block->opaque || !punchthrough_alpha) ?
464 etc1_modifier_tables[table2_idx] :
465 etc2_modifier_tables_non_opaque[table2_idx];
466
467 block->flipped = (src[3] & 0x1);
468 }
469
470 block->pixel_indices[0] =
471 (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7];
472 }
473
474 static void
475 etc2_rgb8_fetch_texel(const struct etc2_block *block,
476 int x, int y, uint8_t *dst,
477 GLboolean punchthrough_alpha)
478 {
479 const uint8_t *base_color;
480 int modifier, bit, idx, blk;
481
482 /* get pixel index */
483 bit = y + x * 4;
484 idx = ((block->pixel_indices[0] >> (15 + bit)) & 0x2) |
485 ((block->pixel_indices[0] >> (bit)) & 0x1);
486
487 if (block->is_ind_mode || block->is_diff_mode) {
488 /* check for punchthrough_alpha format */
489 if (punchthrough_alpha) {
490 if (!block->opaque && idx == 2) {
491 dst[0] = dst[1] = dst[2] = dst[3] = 0;
492 return;
493 }
494 else
495 dst[3] = 255;
496 }
497
498 /* Use pixel index and subblock to get the modifier */
499 blk = (block->flipped) ? (y >= 2) : (x >= 2);
500 base_color = block->base_colors[blk];
501 modifier = block->modifier_tables[blk][idx];
502
503 dst[0] = etc2_clamp(base_color[0] + modifier);
504 dst[1] = etc2_clamp(base_color[1] + modifier);
505 dst[2] = etc2_clamp(base_color[2] + modifier);
506 }
507 else if (block->is_t_mode || block->is_h_mode) {
508 /* check for punchthrough_alpha format */
509 if (punchthrough_alpha) {
510 if (!block->opaque && idx == 2) {
511 dst[0] = dst[1] = dst[2] = dst[3] = 0;
512 return;
513 }
514 else
515 dst[3] = 255;
516 }
517
518 /* Use pixel index to pick one of the paint colors */
519 dst[0] = block->paint_colors[idx][0];
520 dst[1] = block->paint_colors[idx][1];
521 dst[2] = block->paint_colors[idx][2];
522 }
523 else if (block->is_planar_mode) {
524 /* {R(x, y) = clamp255((x × (RH − RO) + y × (RV − RO) + 4 × RO + 2) >> 2)
525 * {G(x, y) = clamp255((x × (GH − GO) + y × (GV − GO) + 4 × GO + 2) >> 2)
526 * {B(x, y) = clamp255((x × (BH − BO) + y × (BV − BO) + 4 × BO + 2) >> 2)
527 */
528 int red, green, blue;
529 red = (x * (block->base_colors[1][0] - block->base_colors[0][0]) +
530 y * (block->base_colors[2][0] - block->base_colors[0][0]) +
531 4 * block->base_colors[0][0] + 2) >> 2;
532
533 green = (x * (block->base_colors[1][1] - block->base_colors[0][1]) +
534 y * (block->base_colors[2][1] - block->base_colors[0][1]) +
535 4 * block->base_colors[0][1] + 2) >> 2;
536
537 blue = (x * (block->base_colors[1][2] - block->base_colors[0][2]) +
538 y * (block->base_colors[2][2] - block->base_colors[0][2]) +
539 4 * block->base_colors[0][2] + 2) >> 2;
540
541 dst[0] = etc2_clamp(red);
542 dst[1] = etc2_clamp(green);
543 dst[2] = etc2_clamp(blue);
544
545 /* check for punchthrough_alpha format */
546 if (punchthrough_alpha)
547 dst[3] = 255;
548 }
549 }
550
551 static void
552 etc2_alpha8_fetch_texel(const struct etc2_block *block,
553 int x, int y, uint8_t *dst)
554 {
555 int modifier, alpha, idx;
556 /* get pixel index */
557 idx = etc2_get_pixel_index(block, x, y);
558 modifier = etc2_modifier_tables[block->table_index][idx];
559 alpha = block->base_codeword + modifier * block->multiplier;
560 dst[3] = etc2_clamp(alpha);
561 }
562
563 static void
564 etc2_r11_fetch_texel(const struct etc2_block *block,
565 int x, int y, uint8_t *dst)
566 {
567 GLint modifier, idx;
568 GLshort color;
569 /* Get pixel index */
570 idx = etc2_get_pixel_index(block, x, y);
571 modifier = etc2_modifier_tables[block->table_index][idx];
572
573 if (block->multiplier != 0)
574 /* clamp2(base codeword × 8 + 4 + modifier × multiplier × 8) */
575 color = etc2_clamp2(((block->base_codeword << 3) | 0x4) +
576 ((modifier * block->multiplier) << 3));
577 else
578 color = etc2_clamp2(((block->base_codeword << 3) | 0x4) + modifier);
579
580 /* Extend 11 bits color value to 16 bits. OpenGL ES 3.0 specification
581 * allows extending the color value to any number of bits. But, an
582 * implementation is not allowed to truncate the 11-bit value to less than
583 * 11 bits."
584 */
585 color = (color << 5) | (color >> 6);
586 ((GLushort *)dst)[0] = color;
587 }
588
589 static void
590 etc2_signed_r11_fetch_texel(const struct etc2_block *block,
591 int x, int y, uint8_t *dst)
592 {
593 GLint modifier, idx;
594 GLshort color;
595 GLbyte base_codeword = (GLbyte) block->base_codeword;
596
597 if (base_codeword == -128)
598 base_codeword = -127;
599
600 /* Get pixel index */
601 idx = etc2_get_pixel_index(block, x, y);
602 modifier = etc2_modifier_tables[block->table_index][idx];
603
604 if (block->multiplier != 0)
605 /* clamp3(base codeword × 8 + modifier × multiplier × 8) */
606 color = etc2_clamp3((base_codeword << 3) +
607 ((modifier * block->multiplier) << 3));
608 else
609 color = etc2_clamp3((base_codeword << 3) + modifier);
610
611 /* Extend 11 bits color value to 16 bits. OpenGL ES 3.0 specification
612 * allows extending the color value to any number of bits. But, an
613 * implementation is not allowed to truncate the 11-bit value to less than
614 * 11 bits. A negative 11-bit value must first be made positive before bit
615 * replication, and then made negative again
616 */
617 if (color >= 0)
618 color = (color << 5) | (color >> 5);
619 else {
620 color = -color;
621 color = (color << 5) | (color >> 5);
622 color = -color;
623 }
624 ((GLshort *)dst)[0] = color;
625 }
626
627 static void
628 etc2_alpha8_parse_block(struct etc2_block *block, const uint8_t *src)
629 {
630 block->base_codeword = src[0];
631 block->multiplier = (src[1] >> 4) & 0xf;
632 block->table_index = src[1] & 0xf;
633 block->pixel_indices[1] = (((uint64_t)src[2] << 40) |
634 ((uint64_t)src[3] << 32) |
635 ((uint64_t)src[4] << 24) |
636 ((uint64_t)src[5] << 16) |
637 ((uint64_t)src[6] << 8) |
638 ((uint64_t)src[7]));
639 }
640
641 static void
642 etc2_r11_parse_block(struct etc2_block *block, const uint8_t *src)
643 {
644 /* Parsing logic remains same as for etc2_alpha8_parse_block */
645 etc2_alpha8_parse_block(block, src);
646 }
647
648 static void
649 etc2_rgba8_parse_block(struct etc2_block *block, const uint8_t *src)
650 {
651 /* RGB component is parsed the same way as for MESA_FORMAT_ETC2_RGB8 */
652 etc2_rgb8_parse_block(block, src + 8,
653 false /* punchthrough_alpha */);
654 /* Parse Alpha component */
655 etc2_alpha8_parse_block(block, src);
656 }
657
658 static void
659 etc2_rgba8_fetch_texel(const struct etc2_block *block,
660 int x, int y, uint8_t *dst)
661 {
662 etc2_rgb8_fetch_texel(block, x, y, dst,
663 false /* punchthrough_alpha */);
664 etc2_alpha8_fetch_texel(block, x, y, dst);
665 }
666
667 static void
668 etc2_unpack_rgb8(uint8_t *dst_row,
669 unsigned dst_stride,
670 const uint8_t *src_row,
671 unsigned src_stride,
672 unsigned width,
673 unsigned height)
674 {
675 const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
676 struct etc2_block block;
677 unsigned x, y, i, j;
678
679 for (y = 0; y < height; y += bh) {
680 const uint8_t *src = src_row;
681
682 for (x = 0; x < width; x+= bw) {
683 etc2_rgb8_parse_block(&block, src,
684 false /* punchthrough_alpha */);
685
686 for (j = 0; j < bh; j++) {
687 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
688 for (i = 0; i < bw; i++) {
689 etc2_rgb8_fetch_texel(&block, i, j, dst,
690 false /* punchthrough_alpha */);
691 dst[3] = 255;
692 dst += comps;
693 }
694 }
695
696 src += bs;
697 }
698
699 src_row += src_stride;
700 }
701 }
702
703 static void
704 etc2_unpack_srgb8(uint8_t *dst_row,
705 unsigned dst_stride,
706 const uint8_t *src_row,
707 unsigned src_stride,
708 unsigned width,
709 unsigned height)
710 {
711 const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
712 struct etc2_block block;
713 unsigned x, y, i, j;
714 uint8_t tmp;
715
716 for (y = 0; y < height; y += bh) {
717 const uint8_t *src = src_row;
718
719 for (x = 0; x < width; x+= bw) {
720 etc2_rgb8_parse_block(&block, src,
721 false /* punchthrough_alpha */);
722
723 for (j = 0; j < bh; j++) {
724 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
725 for (i = 0; i < bw; i++) {
726 etc2_rgb8_fetch_texel(&block, i, j, dst,
727 false /* punchthrough_alpha */);
728 /* Convert to MESA_FORMAT_B8G8R8A8_SRGB */
729 tmp = dst[0];
730 dst[0] = dst[2];
731 dst[2] = tmp;
732 dst[3] = 255;
733
734 dst += comps;
735 }
736 }
737 src += bs;
738 }
739
740 src_row += src_stride;
741 }
742 }
743
744 static void
745 etc2_unpack_rgba8(uint8_t *dst_row,
746 unsigned dst_stride,
747 const uint8_t *src_row,
748 unsigned src_stride,
749 unsigned width,
750 unsigned height)
751 {
752 /* If internalformat is COMPRESSED_RGBA8_ETC2_EAC, each 4 × 4 block of
753 * RGBA8888 information is compressed to 128 bits. To decode a block, the
754 * two 64-bit integers int64bitAlpha and int64bitColor are calculated.
755 */
756 const unsigned bw = 4, bh = 4, bs = 16, comps = 4;
757 struct etc2_block block;
758 unsigned x, y, i, j;
759
760 for (y = 0; y < height; y += bh) {
761 const uint8_t *src = src_row;
762
763 for (x = 0; x < width; x+= bw) {
764 etc2_rgba8_parse_block(&block, src);
765
766 for (j = 0; j < bh; j++) {
767 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
768 for (i = 0; i < bw; i++) {
769 etc2_rgba8_fetch_texel(&block, i, j, dst);
770 dst += comps;
771 }
772 }
773 src += bs;
774 }
775
776 src_row += src_stride;
777 }
778 }
779
780 static void
781 etc2_unpack_srgb8_alpha8(uint8_t *dst_row,
782 unsigned dst_stride,
783 const uint8_t *src_row,
784 unsigned src_stride,
785 unsigned width,
786 unsigned height)
787 {
788 /* If internalformat is COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, each 4 × 4 block
789 * of RGBA8888 information is compressed to 128 bits. To decode a block, the
790 * two 64-bit integers int64bitAlpha and int64bitColor are calculated.
791 */
792 const unsigned bw = 4, bh = 4, bs = 16, comps = 4;
793 struct etc2_block block;
794 unsigned x, y, i, j;
795 uint8_t tmp;
796
797 for (y = 0; y < height; y += bh) {
798 const uint8_t *src = src_row;
799
800 for (x = 0; x < width; x+= bw) {
801 etc2_rgba8_parse_block(&block, src);
802
803 for (j = 0; j < bh; j++) {
804 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
805 for (i = 0; i < bw; i++) {
806 etc2_rgba8_fetch_texel(&block, i, j, dst);
807
808 /* Convert to MESA_FORMAT_B8G8R8A8_SRGB */
809 tmp = dst[0];
810 dst[0] = dst[2];
811 dst[2] = tmp;
812 dst[3] = dst[3];
813
814 dst += comps;
815 }
816 }
817 src += bs;
818 }
819
820 src_row += src_stride;
821 }
822 }
823
824 static void
825 etc2_unpack_r11(uint8_t *dst_row,
826 unsigned dst_stride,
827 const uint8_t *src_row,
828 unsigned src_stride,
829 unsigned width,
830 unsigned height)
831 {
832 /* If internalformat is COMPRESSED_R11_EAC, each 4 × 4 block of
833 color information is compressed to 64 bits.
834 */
835 const unsigned bw = 4, bh = 4, bs = 8, comps = 1, comp_size = 2;
836 struct etc2_block block;
837 unsigned x, y, i, j;
838
839 for (y = 0; y < height; y += bh) {
840 const uint8_t *src = src_row;
841
842 for (x = 0; x < width; x+= bw) {
843 etc2_r11_parse_block(&block, src);
844
845 for (j = 0; j < bh; j++) {
846 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps * comp_size;
847 for (i = 0; i < bw; i++) {
848 etc2_r11_fetch_texel(&block, i, j, dst);
849 dst += comps * comp_size;
850 }
851 }
852 src += bs;
853 }
854
855 src_row += src_stride;
856 }
857 }
858
859 static void
860 etc2_unpack_rg11(uint8_t *dst_row,
861 unsigned dst_stride,
862 const uint8_t *src_row,
863 unsigned src_stride,
864 unsigned width,
865 unsigned height)
866 {
867 /* If internalformat is COMPRESSED_RG11_EAC, each 4 × 4 block of
868 RG color information is compressed to 128 bits.
869 */
870 const unsigned bw = 4, bh = 4, bs = 16, comps = 2, comp_size = 2;
871 struct etc2_block block;
872 unsigned x, y, i, j;
873
874 for (y = 0; y < height; y += bh) {
875 const uint8_t *src = src_row;
876
877 for (x = 0; x < width; x+= bw) {
878 /* red component */
879 etc2_r11_parse_block(&block, src);
880
881 for (j = 0; j < bh; j++) {
882 uint8_t *dst = dst_row + (y + j) * dst_stride +
883 x * comps * comp_size;
884 for (i = 0; i < bw; i++) {
885 etc2_r11_fetch_texel(&block, i, j, dst);
886 dst += comps * comp_size;
887 }
888 }
889 /* green component */
890 etc2_r11_parse_block(&block, src + 8);
891
892 for (j = 0; j < bh; j++) {
893 uint8_t *dst = dst_row + (y + j) * dst_stride +
894 x * comps * comp_size;
895 for (i = 0; i < bw; i++) {
896 etc2_r11_fetch_texel(&block, i, j, dst + comp_size);
897 dst += comps * comp_size;
898 }
899 }
900 src += bs;
901 }
902
903 src_row += src_stride;
904 }
905 }
906
907 static void
908 etc2_unpack_signed_r11(uint8_t *dst_row,
909 unsigned dst_stride,
910 const uint8_t *src_row,
911 unsigned src_stride,
912 unsigned width,
913 unsigned height)
914 {
915 /* If internalformat is COMPRESSED_SIGNED_R11_EAC, each 4 × 4 block of
916 red color information is compressed to 64 bits.
917 */
918 const unsigned bw = 4, bh = 4, bs = 8, comps = 1, comp_size = 2;
919 struct etc2_block block;
920 unsigned x, y, i, j;
921
922 for (y = 0; y < height; y += bh) {
923 const uint8_t *src = src_row;
924
925 for (x = 0; x < width; x+= bw) {
926 etc2_r11_parse_block(&block, src);
927
928 for (j = 0; j < bh; j++) {
929 uint8_t *dst = dst_row + (y + j) * dst_stride +
930 x * comps * comp_size;
931 for (i = 0; i < bw; i++) {
932 etc2_signed_r11_fetch_texel(&block, i, j, dst);
933 dst += comps * comp_size;
934 }
935 }
936 src += bs;
937 }
938
939 src_row += src_stride;
940 }
941 }
942
943 static void
944 etc2_unpack_signed_rg11(uint8_t *dst_row,
945 unsigned dst_stride,
946 const uint8_t *src_row,
947 unsigned src_stride,
948 unsigned width,
949 unsigned height)
950 {
951 /* If internalformat is COMPRESSED_SIGNED_RG11_EAC, each 4 × 4 block of
952 RG color information is compressed to 128 bits.
953 */
954 const unsigned bw = 4, bh = 4, bs = 16, comps = 2, comp_size = 2;
955 struct etc2_block block;
956 unsigned x, y, i, j;
957
958 for (y = 0; y < height; y += bh) {
959 const uint8_t *src = src_row;
960
961 for (x = 0; x < width; x+= bw) {
962 /* red component */
963 etc2_r11_parse_block(&block, src);
964
965 for (j = 0; j < bh; j++) {
966 uint8_t *dst = dst_row + (y + j) * dst_stride +
967 x * comps * comp_size;
968 for (i = 0; i < bw; i++) {
969 etc2_signed_r11_fetch_texel(&block, i, j, dst);
970 dst += comps * comp_size;
971 }
972 }
973 /* green component */
974 etc2_r11_parse_block(&block, src + 8);
975
976 for (j = 0; j < bh; j++) {
977 uint8_t *dst = dst_row + (y + j) * dst_stride +
978 x * comps * comp_size;
979 for (i = 0; i < bw; i++) {
980 etc2_signed_r11_fetch_texel(&block, i, j, dst + comp_size);
981 dst += comps * comp_size;
982 }
983 }
984 src += bs;
985 }
986
987 src_row += src_stride;
988 }
989 }
990
991 static void
992 etc2_unpack_rgb8_punchthrough_alpha1(uint8_t *dst_row,
993 unsigned dst_stride,
994 const uint8_t *src_row,
995 unsigned src_stride,
996 unsigned width,
997 unsigned height)
998 {
999 const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
1000 struct etc2_block block;
1001 unsigned x, y, i, j;
1002
1003 for (y = 0; y < height; y += bh) {
1004 const uint8_t *src = src_row;
1005
1006 for (x = 0; x < width; x+= bw) {
1007 etc2_rgb8_parse_block(&block, src,
1008 true /* punchthrough_alpha */);
1009 for (j = 0; j < bh; j++) {
1010 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
1011 for (i = 0; i < bw; i++) {
1012 etc2_rgb8_fetch_texel(&block, i, j, dst,
1013 true /* punchthrough_alpha */);
1014 dst += comps;
1015 }
1016 }
1017
1018 src += bs;
1019 }
1020
1021 src_row += src_stride;
1022 }
1023 }
1024
1025 static void
1026 etc2_unpack_srgb8_punchthrough_alpha1(uint8_t *dst_row,
1027 unsigned dst_stride,
1028 const uint8_t *src_row,
1029 unsigned src_stride,
1030 unsigned width,
1031 unsigned height)
1032 {
1033 const unsigned bw = 4, bh = 4, bs = 8, comps = 4;
1034 struct etc2_block block;
1035 unsigned x, y, i, j;
1036 uint8_t tmp;
1037
1038 for (y = 0; y < height; y += bh) {
1039 const uint8_t *src = src_row;
1040
1041 for (x = 0; x < width; x+= bw) {
1042 etc2_rgb8_parse_block(&block, src,
1043 true /* punchthrough_alpha */);
1044 for (j = 0; j < bh; j++) {
1045 uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
1046 for (i = 0; i < bw; i++) {
1047 etc2_rgb8_fetch_texel(&block, i, j, dst,
1048 true /* punchthrough_alpha */);
1049 /* Convert to MESA_FORMAT_B8G8R8A8_SRGB */
1050 tmp = dst[0];
1051 dst[0] = dst[2];
1052 dst[2] = tmp;
1053 dst[3] = dst[3];
1054
1055 dst += comps;
1056 }
1057 }
1058
1059 src += bs;
1060 }
1061
1062 src_row += src_stride;
1063 }
1064 }
1065
1066 /* ETC2 texture formats are valid in glCompressedTexImage2D and
1067 * glCompressedTexSubImage2D functions */
1068 GLboolean
1069 _mesa_texstore_etc2_rgb8(TEXSTORE_PARAMS)
1070 {
1071 ASSERT(0);
1072
1073 return GL_FALSE;
1074 }
1075
1076 GLboolean
1077 _mesa_texstore_etc2_srgb8(TEXSTORE_PARAMS)
1078 {
1079 ASSERT(0);
1080
1081 return GL_FALSE;
1082 }
1083
1084 GLboolean
1085 _mesa_texstore_etc2_rgba8_eac(TEXSTORE_PARAMS)
1086 {
1087 ASSERT(0);
1088
1089 return GL_FALSE;
1090 }
1091
1092 GLboolean
1093 _mesa_texstore_etc2_srgb8_alpha8_eac(TEXSTORE_PARAMS)
1094 {
1095 ASSERT(0);
1096
1097 return GL_FALSE;
1098 }
1099
1100 GLboolean
1101 _mesa_texstore_etc2_r11_eac(TEXSTORE_PARAMS)
1102 {
1103 ASSERT(0);
1104
1105 return GL_FALSE;
1106 }
1107
1108 GLboolean
1109 _mesa_texstore_etc2_signed_r11_eac(TEXSTORE_PARAMS)
1110 {
1111 ASSERT(0);
1112
1113 return GL_FALSE;
1114 }
1115
1116 GLboolean
1117 _mesa_texstore_etc2_rg11_eac(TEXSTORE_PARAMS)
1118 {
1119 ASSERT(0);
1120
1121 return GL_FALSE;
1122 }
1123
1124 GLboolean
1125 _mesa_texstore_etc2_signed_rg11_eac(TEXSTORE_PARAMS)
1126 {
1127 ASSERT(0);
1128
1129 return GL_FALSE;
1130 }
1131
1132 GLboolean
1133 _mesa_texstore_etc2_rgb8_punchthrough_alpha1(TEXSTORE_PARAMS)
1134 {
1135 ASSERT(0);
1136
1137 return GL_FALSE;
1138 }
1139
1140 GLboolean
1141 _mesa_texstore_etc2_srgb8_punchthrough_alpha1(TEXSTORE_PARAMS)
1142 {
1143 ASSERT(0);
1144
1145 return GL_FALSE;
1146 }
1147
1148
1149 /**
1150 * Decode texture data in any one of following formats:
1151 * `MESA_FORMAT_ETC2_RGB8`
1152 * `MESA_FORMAT_ETC2_SRGB8`
1153 * `MESA_FORMAT_ETC2_RGBA8_EAC`
1154 * `MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC`
1155 * `MESA_FORMAT_ETC2_R11_EAC`
1156 * `MESA_FORMAT_ETC2_RG11_EAC`
1157 * `MESA_FORMAT_ETC2_SIGNED_R11_EAC`
1158 * `MESA_FORMAT_ETC2_SIGNED_RG11_EAC`
1159 * `MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1`
1160 * `MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1`
1161 *
1162 * The size of the source data must be a multiple of the ETC2 block size
1163 * even if the texture image's dimensions are not aligned to 4.
1164 *
1165 * \param src_width in pixels
1166 * \param src_height in pixels
1167 * \param dst_stride in bytes
1168 */
1169
1170 void
1171 _mesa_unpack_etc2_format(uint8_t *dst_row,
1172 unsigned dst_stride,
1173 const uint8_t *src_row,
1174 unsigned src_stride,
1175 unsigned src_width,
1176 unsigned src_height,
1177 mesa_format format)
1178 {
1179 if (format == MESA_FORMAT_ETC2_RGB8)
1180 etc2_unpack_rgb8(dst_row, dst_stride,
1181 src_row, src_stride,
1182 src_width, src_height);
1183 else if (format == MESA_FORMAT_ETC2_SRGB8)
1184 etc2_unpack_srgb8(dst_row, dst_stride,
1185 src_row, src_stride,
1186 src_width, src_height);
1187 else if (format == MESA_FORMAT_ETC2_RGBA8_EAC)
1188 etc2_unpack_rgba8(dst_row, dst_stride,
1189 src_row, src_stride,
1190 src_width, src_height);
1191 else if (format == MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC)
1192 etc2_unpack_srgb8_alpha8(dst_row, dst_stride,
1193 src_row, src_stride,
1194 src_width, src_height);
1195 else if (format == MESA_FORMAT_ETC2_R11_EAC)
1196 etc2_unpack_r11(dst_row, dst_stride,
1197 src_row, src_stride,
1198 src_width, src_height);
1199 else if (format == MESA_FORMAT_ETC2_RG11_EAC)
1200 etc2_unpack_rg11(dst_row, dst_stride,
1201 src_row, src_stride,
1202 src_width, src_height);
1203 else if (format == MESA_FORMAT_ETC2_SIGNED_R11_EAC)
1204 etc2_unpack_signed_r11(dst_row, dst_stride,
1205 src_row, src_stride,
1206 src_width, src_height);
1207 else if (format == MESA_FORMAT_ETC2_SIGNED_RG11_EAC)
1208 etc2_unpack_signed_rg11(dst_row, dst_stride,
1209 src_row, src_stride,
1210 src_width, src_height);
1211 else if (format == MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1)
1212 etc2_unpack_rgb8_punchthrough_alpha1(dst_row, dst_stride,
1213 src_row, src_stride,
1214 src_width, src_height);
1215 else if (format == MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1)
1216 etc2_unpack_srgb8_punchthrough_alpha1(dst_row, dst_stride,
1217 src_row, src_stride,
1218 src_width, src_height);
1219 }
1220
1221
1222
1223 static void
1224 fetch_etc1_rgb8(const GLubyte *map,
1225 GLint rowStride, GLint i, GLint j,
1226 GLfloat *texel)
1227 {
1228 struct etc1_block block;
1229 GLubyte dst[3];
1230 const GLubyte *src;
1231
1232 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1233
1234 etc1_parse_block(&block, src);
1235 etc1_fetch_texel(&block, i % 4, j % 4, dst);
1236
1237 texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
1238 texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
1239 texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
1240 texel[ACOMP] = 1.0f;
1241 }
1242
1243
1244 static void
1245 fetch_etc2_rgb8(const GLubyte *map,
1246 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1247 {
1248 struct etc2_block block;
1249 uint8_t dst[3];
1250 const uint8_t *src;
1251
1252 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1253
1254 etc2_rgb8_parse_block(&block, src,
1255 false /* punchthrough_alpha */);
1256 etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst,
1257 false /* punchthrough_alpha */);
1258
1259 texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
1260 texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
1261 texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
1262 texel[ACOMP] = 1.0f;
1263 }
1264
1265 static void
1266 fetch_etc2_srgb8(const GLubyte *map,
1267 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1268 {
1269 struct etc2_block block;
1270 uint8_t dst[3];
1271 const uint8_t *src;
1272
1273 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1274
1275 etc2_rgb8_parse_block(&block, src,
1276 false /* punchthrough_alpha */);
1277 etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst,
1278 false /* punchthrough_alpha */);
1279
1280 texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
1281 texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
1282 texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
1283 texel[ACOMP] = 1.0f;
1284 }
1285
1286 static void
1287 fetch_etc2_rgba8_eac(const GLubyte *map,
1288 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1289 {
1290 struct etc2_block block;
1291 uint8_t dst[4];
1292 const uint8_t *src;
1293
1294 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;
1295
1296 etc2_rgba8_parse_block(&block, src);
1297 etc2_rgba8_fetch_texel(&block, i % 4, j % 4, dst);
1298
1299 texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
1300 texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
1301 texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
1302 texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
1303 }
1304
1305 static void
1306 fetch_etc2_srgb8_alpha8_eac(const GLubyte *map,
1307 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1308 {
1309 struct etc2_block block;
1310 uint8_t dst[4];
1311 const uint8_t *src;
1312
1313 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;
1314
1315 etc2_rgba8_parse_block(&block, src);
1316 etc2_rgba8_fetch_texel(&block, i % 4, j % 4, dst);
1317
1318 texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
1319 texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
1320 texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
1321 texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
1322 }
1323
1324 static void
1325 fetch_etc2_r11_eac(const GLubyte *map,
1326 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1327 {
1328 struct etc2_block block;
1329 GLushort dst;
1330 const uint8_t *src;
1331
1332 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1333
1334 etc2_r11_parse_block(&block, src);
1335 etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)&dst);
1336
1337 texel[RCOMP] = USHORT_TO_FLOAT(dst);
1338 texel[GCOMP] = 0.0f;
1339 texel[BCOMP] = 0.0f;
1340 texel[ACOMP] = 1.0f;
1341 }
1342
1343 static void
1344 fetch_etc2_rg11_eac(const GLubyte *map,
1345 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1346 {
1347 struct etc2_block block;
1348 GLushort dst[2];
1349 const uint8_t *src;
1350
1351 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;
1352
1353 /* red component */
1354 etc2_r11_parse_block(&block, src);
1355 etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)dst);
1356
1357 /* green component */
1358 etc2_r11_parse_block(&block, src + 8);
1359 etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)(dst + 1));
1360
1361 texel[RCOMP] = USHORT_TO_FLOAT(dst[0]);
1362 texel[GCOMP] = USHORT_TO_FLOAT(dst[1]);
1363 texel[BCOMP] = 0.0f;
1364 texel[ACOMP] = 1.0f;
1365 }
1366
1367 static void
1368 fetch_etc2_signed_r11_eac(const GLubyte *map,
1369 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1370 {
1371 struct etc2_block block;
1372 GLushort dst;
1373 const uint8_t *src;
1374
1375 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1376
1377 etc2_r11_parse_block(&block, src);
1378 etc2_signed_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)&dst);
1379
1380 texel[RCOMP] = SHORT_TO_FLOAT(dst);
1381 texel[GCOMP] = 0.0f;
1382 texel[BCOMP] = 0.0f;
1383 texel[ACOMP] = 1.0f;
1384 }
1385
1386 static void
1387 fetch_etc2_signed_rg11_eac(const GLubyte *map,
1388 GLint rowStride, GLint i, GLint j, GLfloat *texel)
1389 {
1390 struct etc2_block block;
1391 GLushort dst[2];
1392 const uint8_t *src;
1393
1394 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;
1395
1396 /* red component */
1397 etc2_r11_parse_block(&block, src);
1398 etc2_signed_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)dst);
1399
1400 /* green component */
1401 etc2_r11_parse_block(&block, src + 8);
1402 etc2_signed_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)(dst + 1));
1403
1404 texel[RCOMP] = SHORT_TO_FLOAT(dst[0]);
1405 texel[GCOMP] = SHORT_TO_FLOAT(dst[1]);
1406 texel[BCOMP] = 0.0f;
1407 texel[ACOMP] = 1.0f;
1408 }
1409
1410 static void
1411 fetch_etc2_rgb8_punchthrough_alpha1(const GLubyte *map,
1412 GLint rowStride, GLint i, GLint j,
1413 GLfloat *texel)
1414 {
1415 struct etc2_block block;
1416 uint8_t dst[4];
1417 const uint8_t *src;
1418
1419 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1420
1421 etc2_rgb8_parse_block(&block, src,
1422 true /* punchthrough alpha */);
1423 etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst,
1424 true /* punchthrough alpha */);
1425 texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
1426 texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
1427 texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
1428 texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
1429 }
1430
1431 static void
1432 fetch_etc2_srgb8_punchthrough_alpha1(const GLubyte *map,
1433 GLint rowStride,
1434 GLint i, GLint j, GLfloat *texel)
1435 {
1436 struct etc2_block block;
1437 uint8_t dst[4];
1438 const uint8_t *src;
1439
1440 src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8;
1441
1442 etc2_rgb8_parse_block(&block, src,
1443 true /* punchthrough alpha */);
1444 etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst,
1445 true /* punchthrough alpha */);
1446 texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
1447 texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
1448 texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
1449 texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
1450 }
1451
1452
1453 compressed_fetch_func
1454 _mesa_get_etc_fetch_func(mesa_format format)
1455 {
1456 switch (format) {
1457 case MESA_FORMAT_ETC1_RGB8:
1458 return fetch_etc1_rgb8;
1459 case MESA_FORMAT_ETC2_RGB8:
1460 return fetch_etc2_rgb8;
1461 case MESA_FORMAT_ETC2_SRGB8:
1462 return fetch_etc2_srgb8;
1463 case MESA_FORMAT_ETC2_RGBA8_EAC:
1464 return fetch_etc2_rgba8_eac;
1465 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
1466 return fetch_etc2_srgb8_alpha8_eac;
1467 case MESA_FORMAT_ETC2_R11_EAC:
1468 return fetch_etc2_r11_eac;
1469 case MESA_FORMAT_ETC2_RG11_EAC:
1470 return fetch_etc2_rg11_eac;
1471 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
1472 return fetch_etc2_signed_r11_eac;
1473 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
1474 return fetch_etc2_signed_rg11_eac;
1475 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
1476 return fetch_etc2_rgb8_punchthrough_alpha1;
1477 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
1478 return fetch_etc2_srgb8_punchthrough_alpha1;
1479 default:
1480 return NULL;
1481 }
1482 }