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