1 /*
2 * Copyright (c) 2019 Etnaviv Project
3 * Copyright (C) 2019 Zodiac Inflight Innovations
4 *
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:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
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.
23 *
24 * Authors:
25 * Christian Gmeiner <[email protected]>
26 */
27
28 #include "etnaviv_etc2.h"
29 #include "etnaviv_resource.h"
30 #include "etnaviv_screen.h"
31 #include "util/format/u_format.h"
32
33 bool
etna_etc2_needs_patching(const struct pipe_resource * prsc)34 etna_etc2_needs_patching(const struct pipe_resource *prsc)
35 {
36 const struct etna_screen *screen = etna_screen(prsc->screen);
37
38 if (!util_format_is_etc(prsc->format))
39 return false;
40
41 if (VIV_FEATURE(screen, ETNA_FEATURE_HALTI1))
42 return false;
43
44 switch (prsc->format) {
45 case PIPE_FORMAT_ETC2_RGB8:
46 case PIPE_FORMAT_ETC2_SRGB8:
47 case PIPE_FORMAT_ETC2_RGB8A1:
48 case PIPE_FORMAT_ETC2_SRGB8A1:
49 case PIPE_FORMAT_ETC2_RGBA8:
50 case PIPE_FORMAT_ETC2_SRGBA8:
51 return true;
52 break;
53
54 default:
55 return false;
56 }
57 }
58
59 static inline bool
needs_patching(uint8_t * buffer,bool punchthrough_alpha)60 needs_patching(uint8_t *buffer, bool punchthrough_alpha)
61 {
62 /* punchthrough_alpha or etc2 individual mode? */
63 if (!punchthrough_alpha && !(buffer[3] & 0x2))
64 return false;
65
66 /* etc2 t-mode? */
67 static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 };
68 const int R_plus_dR = (buffer[0] >> 3) + lookup[buffer[0] & 0x7];
69
70 if (R_plus_dR < 0 || R_plus_dR > 31)
71 return true;
72
73 return false;
74 }
75
76 void
etna_etc2_calculate_blocks(uint8_t * buffer,unsigned stride,unsigned width,unsigned height,enum pipe_format format,struct util_dynarray * offsets)77 etna_etc2_calculate_blocks(uint8_t *buffer, unsigned stride,
78 unsigned width, unsigned height,
79 enum pipe_format format,
80 struct util_dynarray *offsets)
81 {
82 const unsigned bw = util_format_get_blockwidth(format);
83 const unsigned bh = util_format_get_blockheight(format);
84 const unsigned bs = util_format_get_blocksize(format);
85 bool punchthrough_alpha = false;
86 unsigned offset = 0;
87 const uint8_t *base = buffer;
88
89 if (format == PIPE_FORMAT_ETC2_RGB8A1 ||
90 format == PIPE_FORMAT_ETC2_SRGB8A1)
91 punchthrough_alpha = true;
92
93 if (format == PIPE_FORMAT_ETC2_RGBA8 ||
94 format == PIPE_FORMAT_ETC2_SRGBA8 ||
95 format == PIPE_FORMAT_ETC2_SRGB8A1)
96 offset = 8;
97
98 for (unsigned y = 0; y < height; y += bh) {
99 uint8_t *src = buffer;
100
101 for (unsigned x = 0; x < width; x += bw) {
102 if (needs_patching(src + offset, punchthrough_alpha))
103 util_dynarray_append(offsets, unsigned, src + offset - base);
104
105 src += bs;
106 }
107
108 buffer += stride;
109 }
110 }
111
112 static inline void
swap_colors(uint8_t * buffer)113 swap_colors(uint8_t *buffer)
114 {
115 const uint8_t r1a = (buffer[0] & 0x18) >> 3;
116 const uint8_t r1b = (buffer[0] & 0x3);
117 const uint8_t r1 = (r1a << 2) | r1b;
118 const uint8_t g1 = (buffer[1] & 0xf0) >> 4;
119 const uint8_t b1 = (buffer[1] & 0x0f);
120 const uint8_t r2 = (buffer[2] & 0xf0) >> 4;
121 const uint8_t g2 = buffer[2] & 0x0f;
122 const uint8_t b2 = (buffer[3] & 0xf0) >> 4;
123 const uint8_t rest = (buffer[3] & 0x0f);
124
125 /* fixup R and dR used for t-mode detection */
126 static const uint8_t fixup[16] = {
127 0x04, 0x04, 0x04, 0x04,
128 0x04, 0x04, 0x04, 0xe0,
129 0x04, 0x04, 0xe0, 0xe0,
130 0x04, 0xe0, 0xe0, 0xe0
131 };
132
133 /* swap colors */
134 buffer[0] = fixup[r2] | ((r2 & 0x0c) << 1) | (r2 & 0x03);
135 buffer[1] = (g2 << 4) | b2;
136 buffer[2] = (r1 << 4) | g1;
137 buffer[3] = (b1 << 4) | rest;
138 }
139
140 void
etna_etc2_patch(uint8_t * buffer,const struct util_dynarray * offsets)141 etna_etc2_patch(uint8_t *buffer, const struct util_dynarray *offsets)
142 {
143 util_dynarray_foreach(offsets, unsigned, offset)
144 swap_colors(buffer + *offset);
145 }
146