util: Move gallium's PIPE_FORMAT utils to /util/format/
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_etc2.c
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 <christian.gmeiner@gmail.com>
26 */
27
28 #include "etnaviv_etc2.h"
29 #include "etnaviv_resource.h"
30 #include "etnaviv_screen.h"
31 #include "hw/common.xml.h"
32 #include "util/format/u_format.h"
33
34 bool
35 etna_etc2_needs_patching(const struct pipe_resource *prsc)
36 {
37 const struct etna_screen *screen = etna_screen(prsc->screen);
38
39 if (!util_format_is_etc(prsc->format))
40 return false;
41
42 if (VIV_FEATURE(screen, chipMinorFeatures2, HALTI1))
43 return false;
44
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:
52 return true;
53 break;
54
55 default:
56 return false;
57 }
58 }
59
60 static inline bool
61 needs_patching(uint8_t *buffer, bool punchthrough_alpha)
62 {
63 /* punchthrough_alpha or etc2 individual mode? */
64 if (!punchthrough_alpha && !(buffer[3] & 0x2))
65 return false;
66
67 /* etc2 t-mode? */
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];
70
71 if (R_plus_dR < 0 || R_plus_dR > 31)
72 return true;
73
74 return false;
75 }
76
77 void
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)
82 {
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;
87 unsigned offset = 0;
88 const uint8_t *base = buffer;
89
90 if (format == PIPE_FORMAT_ETC2_RGB8A1 ||
91 format == PIPE_FORMAT_ETC2_SRGB8A1)
92 punchthrough_alpha = true;
93
94 if (format == PIPE_FORMAT_ETC2_RGBA8 ||
95 format == PIPE_FORMAT_ETC2_SRGBA8 ||
96 format == PIPE_FORMAT_ETC2_SRGB8A1)
97 offset = 8;
98
99 for (unsigned y = 0; y < height; y += bh) {
100 uint8_t *src = buffer;
101
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);
105
106 src += bs;
107 }
108
109 buffer += stride;
110 }
111 }
112
113 static inline void
114 swap_colors(uint8_t *buffer)
115 {
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);
125
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
132 };
133
134 /* swap colors */
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;
139 }
140
141 void
142 etna_etc2_patch(uint8_t *buffer, const struct util_dynarray *offsets)
143 {
144 util_dynarray_foreach(offsets, unsigned, offset)
145 swap_colors(buffer + *offset);
146 }