1 /*
2  * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3  * Copyright © 2018 Google, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *    Rob Clark <robclark@freedesktop.org>
26  */
27 
28 #include "pipe/p_defines.h"
29 #include "util/format/u_format.h"
30 
31 #include "fd6_format.h"
32 #include "freedreno_resource.h"
33 
34 enum a6xx_tex_swiz
fd6_pipe2swiz(unsigned swiz)35 fd6_pipe2swiz(unsigned swiz)
36 {
37    switch (swiz) {
38    default:
39    case PIPE_SWIZZLE_X:
40       return A6XX_TEX_X;
41    case PIPE_SWIZZLE_Y:
42       return A6XX_TEX_Y;
43    case PIPE_SWIZZLE_Z:
44       return A6XX_TEX_Z;
45    case PIPE_SWIZZLE_W:
46       return A6XX_TEX_W;
47    case PIPE_SWIZZLE_0:
48       return A6XX_TEX_ZERO;
49    case PIPE_SWIZZLE_1:
50       return A6XX_TEX_ONE;
51    }
52 }
53 
54 void
fd6_tex_swiz(enum pipe_format format,enum a6xx_tile_mode tile_mode,unsigned char * swiz,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)55 fd6_tex_swiz(enum pipe_format format, enum a6xx_tile_mode tile_mode, unsigned char *swiz, unsigned swizzle_r,
56              unsigned swizzle_g, unsigned swizzle_b, unsigned swizzle_a)
57 {
58    const struct util_format_description *desc = util_format_description(format);
59    const unsigned char uswiz[4] = {swizzle_r, swizzle_g, swizzle_b, swizzle_a};
60 
61    /* Gallium expects stencil sampler to return (s,s,s,s), so massage
62     * the swizzle to do so.
63     */
64    if (format == PIPE_FORMAT_X24S8_UINT) {
65       const unsigned char stencil_swiz[4] = {PIPE_SWIZZLE_W, PIPE_SWIZZLE_W,
66                                              PIPE_SWIZZLE_W, PIPE_SWIZZLE_W};
67       util_format_compose_swizzles(stencil_swiz, uswiz, swiz);
68    } else if (format == PIPE_FORMAT_R8G8_R8B8_UNORM || format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
69       unsigned char fswiz[4] = {PIPE_SWIZZLE_Z, PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y, PIPE_SWIZZLE_1};
70       util_format_compose_swizzles(fswiz, uswiz, swiz);
71    } else if (fd6_texture_swap(format, TILE6_LINEAR) != WZYX || format == PIPE_FORMAT_A1R5G5B5_UNORM) {
72       /* Formats with a non-pass-through swap are permutations of RGBA
73        * formats. We program the permutation using the swap and don't
74        * need to compose the format swizzle with the user swizzle.
75        */
76       memcpy(swiz, uswiz, sizeof(uswiz));
77    } else {
78       /* Otherwise, it's an unswapped RGBA format or a format like L8 where
79        * we need the XXX1 swizzle from the gallium format description.
80        */
81       util_format_compose_swizzles(desc->swizzle, uswiz, swiz);
82    }
83 }
84 
85 /* Compute the TEX_CONST_0 value for texture state, including SWIZ/SWAP/etc: */
86 uint32_t
fd6_tex_const_0(struct pipe_resource * prsc,unsigned level,enum pipe_format format,unsigned swizzle_r,unsigned swizzle_g,unsigned swizzle_b,unsigned swizzle_a)87 fd6_tex_const_0(struct pipe_resource *prsc, unsigned level,
88                 enum pipe_format format, unsigned swizzle_r, unsigned swizzle_g,
89                 unsigned swizzle_b, unsigned swizzle_a)
90 {
91    struct fd_resource *rsc = fd_resource(prsc);
92    unsigned char swiz[4];
93 
94    fd6_tex_swiz(format, rsc->layout.tile_mode, swiz, swizzle_r, swizzle_g, swizzle_b, swizzle_a);
95 
96    return A6XX_TEX_CONST_0_FMT(fd6_texture_format(format, rsc->layout.tile_mode)) |
97           A6XX_TEX_CONST_0_SAMPLES(fd_msaa_samples(prsc->nr_samples)) |
98           A6XX_TEX_CONST_0_SWAP(fd6_texture_swap(format, rsc->layout.tile_mode)) |
99           A6XX_TEX_CONST_0_TILE_MODE(fd_resource_tile_mode(prsc, level)) |
100           COND(util_format_is_srgb(format), A6XX_TEX_CONST_0_SRGB) |
101           A6XX_TEX_CONST_0_SWIZ_X(fd6_pipe2swiz(swiz[0])) |
102           A6XX_TEX_CONST_0_SWIZ_Y(fd6_pipe2swiz(swiz[1])) |
103           A6XX_TEX_CONST_0_SWIZ_Z(fd6_pipe2swiz(swiz[2])) |
104           A6XX_TEX_CONST_0_SWIZ_W(fd6_pipe2swiz(swiz[3]));
105 }
106