xref: /aosp_15_r20/external/mesa3d/src/panfrost/lib/pan_util.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 
27 /* Translate a PIPE swizzle quad to a 12-bit Mali swizzle code. PIPE
28  * swizzles line up with Mali swizzles for the XYZW01, but PIPE swizzles have
29  * an additional "NONE" field that we have to mask out to zero. Additionally,
30  * PIPE swizzles are sparse but Mali swizzles are packed */
31 
32 unsigned
panfrost_translate_swizzle_4(const unsigned char swizzle[4])33 panfrost_translate_swizzle_4(const unsigned char swizzle[4])
34 {
35    unsigned out = 0;
36 
37    for (unsigned i = 0; i < 4; ++i) {
38       assert(swizzle[i] <= PIPE_SWIZZLE_1);
39       out |= (swizzle[i] << (3 * i));
40    }
41 
42    return out;
43 }
44 
45 void
panfrost_invert_swizzle(const unsigned char * in,unsigned char * out)46 panfrost_invert_swizzle(const unsigned char *in, unsigned char *out)
47 {
48    /* First, default to all zeroes, both to prevent uninitialized junk
49       and to provide a known baseline so we can tell when components
50       have been modified
51     */
52 
53    for (unsigned c = 0; c < 4; ++c)
54       out[c] = PIPE_SWIZZLE_0;
55 
56    /* Now "do" what the swizzle says */
57 
58    for (unsigned c = 0; c < 4; ++c) {
59       unsigned char i = in[c];
60 
61       /* Who cares? */
62       assert(PIPE_SWIZZLE_X == 0);
63       if (i > PIPE_SWIZZLE_W)
64          continue;
65 
66       /* Invert (only if we haven't already applied) */
67       unsigned idx = i - PIPE_SWIZZLE_X;
68       if (out[idx] == PIPE_SWIZZLE_0)
69          out[idx] = PIPE_SWIZZLE_X + c;
70    }
71 }
72