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