2 * Copyright (c) 2019 Etnaviv Project
3 * Copyright (C) 2019 Zodiac Inflight Innovations
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Christian Gmeiner <christian.gmeiner@gmail.com>
28 #include "etnaviv_etc2.h"
29 #include "etnaviv_resource.h"
30 #include "etnaviv_screen.h"
31 #include "hw/common.xml.h"
32 #include "util/u_format.h"
35 etna_etc2_needs_patching(const struct pipe_resource
*prsc
)
37 const struct etna_screen
*screen
= etna_screen(prsc
->screen
);
39 if (!util_format_is_etc(prsc
->format
))
42 if (VIV_FEATURE(screen
, chipMinorFeatures2
, HALTI1
))
45 switch (prsc
->format
) {
46 case PIPE_FORMAT_ETC2_RGB8
:
47 case PIPE_FORMAT_ETC2_SRGB8
:
48 case PIPE_FORMAT_ETC2_RGB8A1
:
49 case PIPE_FORMAT_ETC2_SRGB8A1
:
50 case PIPE_FORMAT_ETC2_RGBA8
:
51 case PIPE_FORMAT_ETC2_SRGBA8
:
61 needs_patching(uint8_t *buffer
, bool punchthrough_alpha
)
63 /* punchthrough_alpha or etc2 individual mode? */
64 if (!punchthrough_alpha
&& !(buffer
[3] & 0x2))
68 static const int lookup
[8] = { 0, 1, 2, 3, -4, -3, -2, -1 };
69 const int R_plus_dR
= (buffer
[0] >> 3) + lookup
[buffer
[0] & 0x7];
71 if (R_plus_dR
< 0 || R_plus_dR
> 31)
78 etna_etc2_calculate_blocks(uint8_t *buffer
, unsigned stride
,
79 unsigned width
, unsigned height
,
80 enum pipe_format format
,
81 struct util_dynarray
*offsets
)
83 const unsigned bw
= util_format_get_blockwidth(format
);
84 const unsigned bh
= util_format_get_blockheight(format
);
85 const unsigned bs
= util_format_get_blocksize(format
);
86 bool punchthrough_alpha
= false;
88 const uint8_t *base
= buffer
;
90 if (format
== PIPE_FORMAT_ETC2_RGB8A1
||
91 format
== PIPE_FORMAT_ETC2_SRGB8A1
)
92 punchthrough_alpha
= true;
94 if (format
== PIPE_FORMAT_ETC2_RGBA8
||
95 format
== PIPE_FORMAT_ETC2_SRGBA8
||
96 format
== PIPE_FORMAT_ETC2_SRGB8A1
)
99 for (unsigned y
= 0; y
< height
; y
+= bh
) {
100 uint8_t *src
= buffer
;
102 for (unsigned x
= 0; x
< width
; x
+= bw
) {
103 if (needs_patching(src
+ offset
, punchthrough_alpha
))
104 util_dynarray_append(offsets
, unsigned, src
+ offset
- base
);
114 swap_colors(uint8_t *buffer
)
116 const uint8_t r1a
= (buffer
[0] & 0x18) >> 3;
117 const uint8_t r1b
= (buffer
[0] & 0x3);
118 const uint8_t r1
= (r1a
<< 2) | r1b
;
119 const uint8_t g1
= (buffer
[1] & 0xf0) >> 4;
120 const uint8_t b1
= (buffer
[1] & 0x0f);
121 const uint8_t r2
= (buffer
[2] & 0xf0) >> 4;
122 const uint8_t g2
= buffer
[2] & 0x0f;
123 const uint8_t b2
= (buffer
[3] & 0xf0) >> 4;
124 const uint8_t rest
= (buffer
[3] & 0x0f);
126 /* fixup R and dR used for t-mode detection */
127 static const uint8_t fixup
[16] = {
128 0x04, 0x04, 0x04, 0x04,
129 0x04, 0x04, 0x04, 0xe0,
130 0x04, 0x04, 0xe0, 0xe0,
131 0x04, 0xe0, 0xe0, 0xe0
135 buffer
[0] = fixup
[r2
] | ((r2
& 0x0c) << 1) | (r2
& 0x03);
136 buffer
[1] = (g2
<< 4) | b2
;
137 buffer
[2] = (r1
<< 4) | g1
;
138 buffer
[3] = (b1
<< 4) | rest
;
142 etna_etc2_patch(uint8_t *buffer
, const struct util_dynarray
*offsets
)
144 util_dynarray_foreach(offsets
, unsigned, offset
)
145 swap_colors(buffer
+ *offset
);