1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2019 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdio.h>
25bf215546Sopenharmony_ci#include "pan_texture.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/* Translate a PIPE swizzle quad to a 12-bit Mali swizzle code. PIPE
28bf215546Sopenharmony_ci * swizzles line up with Mali swizzles for the XYZW01, but PIPE swizzles have
29bf215546Sopenharmony_ci * an additional "NONE" field that we have to mask out to zero. Additionally,
30bf215546Sopenharmony_ci * PIPE swizzles are sparse but Mali swizzles are packed */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ciunsigned
33bf215546Sopenharmony_cipanfrost_translate_swizzle_4(const unsigned char swizzle[4])
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci        unsigned out = 0;
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci        for (unsigned i = 0; i < 4; ++i) {
38bf215546Sopenharmony_ci                unsigned translated = (swizzle[i] > PIPE_SWIZZLE_1) ? PIPE_SWIZZLE_0 : swizzle[i];
39bf215546Sopenharmony_ci                out |= (translated << (3*i));
40bf215546Sopenharmony_ci        }
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci        return out;
43bf215546Sopenharmony_ci}
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_civoid
46bf215546Sopenharmony_cipanfrost_invert_swizzle(const unsigned char *in, unsigned char *out)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci        /* First, default to all zeroes to prevent uninitialized junk */
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci        for (unsigned c = 0; c < 4; ++c)
51bf215546Sopenharmony_ci                out[c] = PIPE_SWIZZLE_0;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci        /* Now "do" what the swizzle says */
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci        for (unsigned c = 0; c < 4; ++c) {
56bf215546Sopenharmony_ci                unsigned char i = in[c];
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci                /* Who cares? */
59bf215546Sopenharmony_ci                assert(PIPE_SWIZZLE_X == 0);
60bf215546Sopenharmony_ci                if (i > PIPE_SWIZZLE_W)
61bf215546Sopenharmony_ci                        continue;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci                /* Invert */
64bf215546Sopenharmony_ci                unsigned idx = i - PIPE_SWIZZLE_X;
65bf215546Sopenharmony_ci                out[idx] = PIPE_SWIZZLE_X + c;
66bf215546Sopenharmony_ci        }
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci/* Formats requiring blend shaders are stored raw in the tilebuffer and will
70bf215546Sopenharmony_ci * have 0 as their pixel format. Assumes dithering is set, I don't know of a
71bf215546Sopenharmony_ci * case when it makes sense to turn off dithering. */
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ciunsigned
74bf215546Sopenharmony_cipanfrost_format_to_bifrost_blend(const struct panfrost_device *dev,
75bf215546Sopenharmony_ci                                 enum pipe_format format,
76bf215546Sopenharmony_ci                                 bool dithered)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci        mali_pixel_format pixfmt = (dev->arch >= 7) ?
79bf215546Sopenharmony_ci                panfrost_blendable_formats_v7[format].bifrost[dithered] :
80bf215546Sopenharmony_ci                panfrost_blendable_formats_v6[format].bifrost[dithered];
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci        return pixfmt ?: dev->formats[format].hw;
83bf215546Sopenharmony_ci}
84