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