summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/vc4/vc4_formats.c
blob: 6d4a6249fbfa146c2c92a4c9cb3b590cee75ffcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright © 2014 Broadcom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * @file vc4_formats.c
 *
 * Contains the table and accessors for VC4 texture and render target format
 * support.
 *
 * The hardware has limited support for texture formats, and extremely limited
 * support for render target formats.  As a result, we emulate other formats
 * in our shader code, and this stores the table for doing so.
 */

#include "util/u_format.h"
#include "util/macros.h"

#include "vc4_context.h"

#define RT_NO        0
#define RT_RGBA8888  1
#define RT_RGB565    2

struct vc4_format {
        /** Set if the pipe format is defined in the table. */
        bool present;

        /** Set to 0 if unsupported, 1 if RGBA8888, 2 if rgb565. */
        uint8_t rt_type;

        /** One of VC4_TEXTURE_TYPE_*. */
        uint8_t tex_type;

        /**
         * Swizzle to apply to the RGBA shader output for storing to the tile
         * buffer, to the RGBA tile buffer to produce shader input (for
         * blending), and for turning the rgba8888 texture sampler return
         * value into shader rgba values.
         */
        uint8_t swizzle[4];
};

#define SWIZ(x,y,z,w) {          \
        PIPE_SWIZZLE_##x, \
        PIPE_SWIZZLE_##y, \
        PIPE_SWIZZLE_##z, \
        PIPE_SWIZZLE_##w  \
}

#define FORMAT(pipe, rt, tex, swiz)                                     \
        [PIPE_FORMAT_##pipe] = { true, RT_##rt, VC4_TEXTURE_TYPE_##tex, swiz }

static const struct vc4_format vc4_format_table[] = {
        FORMAT(R8G8B8A8_UNORM, RGBA8888, RGBA8888, SWIZ(X, Y, Z, W)),
        FORMAT(R8G8B8X8_UNORM, RGBA8888, RGBA8888, SWIZ(X, Y, Z, 1)),
        FORMAT(R8G8B8A8_SRGB,  RGBA8888, RGBA8888, SWIZ(X, Y, Z, W)),
        FORMAT(R8G8B8X8_SRGB,  RGBA8888, RGBA8888, SWIZ(X, Y, Z, 1)),

        FORMAT(B8G8R8A8_UNORM, RGBA8888, RGBA8888, SWIZ(Z, Y, X, W)),
        FORMAT(B8G8R8X8_UNORM, RGBA8888, RGBA8888, SWIZ(Z, Y, X, 1)),
        FORMAT(B8G8R8A8_SRGB, RGBA8888, RGBA8888, SWIZ(Z, Y, X, W)),
        FORMAT(B8G8R8X8_SRGB, RGBA8888, RGBA8888, SWIZ(Z, Y, X, 1)),

        FORMAT(B5G6R5_UNORM, RGB565, RGB565, SWIZ(X, Y, Z, 1)),

        FORMAT(ETC1_RGB8, NO, ETC1, SWIZ(X, Y, Z, 1)),

        /* Depth sampling will be handled by doing nearest filtering and not
         * unpacking the RGBA value.
         */
        FORMAT(S8_UINT_Z24_UNORM, NO, RGBA8888, SWIZ(X, Y, Z, W)),
        FORMAT(X8Z24_UNORM,       NO, RGBA8888, SWIZ(X, Y, Z, W)),

        FORMAT(B4G4R4A4_UNORM, NO, RGBA4444, SWIZ(Y, Z, W, X)),
        FORMAT(B4G4R4X4_UNORM, NO, RGBA4444, SWIZ(Y, Z, W, 1)),

        FORMAT(A1B5G5R5_UNORM, NO, RGBA5551, SWIZ(X, Y, Z, W)),
        FORMAT(X1B5G5R5_UNORM, NO, RGBA5551, SWIZ(X, Y, Z, 1)),

        FORMAT(A8_UNORM, NO, ALPHA, SWIZ(0, 0, 0, W)),
        FORMAT(L8_UNORM, NO, ALPHA, SWIZ(W, W, W, 1)),
        FORMAT(I8_UNORM, NO, ALPHA, SWIZ(W, W, W, W)),
        FORMAT(R8_UNORM, NO, ALPHA, SWIZ(W, 0, 0, 1)),

        FORMAT(L8A8_UNORM, NO, LUMALPHA, SWIZ(X, X, X, W)),
        FORMAT(R8G8_UNORM, NO, LUMALPHA, SWIZ(X, W, 0, 1)),
};

static const struct vc4_format *
get_format(enum pipe_format f)
{
        if (f >= ARRAY_SIZE(vc4_format_table) ||
            !vc4_format_table[f].present)
                return NULL;
        else
                return &vc4_format_table[f];
}

bool
vc4_rt_format_supported(enum pipe_format f)
{
        const struct vc4_format *vf = get_format(f);

        if (!vf)
                return false;

        return vf->rt_type != RT_NO;
}

bool
vc4_rt_format_is_565(enum pipe_format f)
{
        const struct vc4_format *vf = get_format(f);

        if (!vf)
                return false;

        return vf->rt_type == RT_RGB565;
}

bool
vc4_tex_format_supported(enum pipe_format f)
{
        const struct vc4_format *vf = get_format(f);

        return vf != NULL;
}

uint8_t
vc4_get_tex_format(enum pipe_format f)
{
        const struct vc4_format *vf = get_format(f);

        if (!vf)
                return 0;

        return vf->tex_type;
}

const uint8_t *
vc4_get_format_swizzle(enum pipe_format f)
{
        const struct vc4_format *vf = get_format(f);
        static const uint8_t fallback[] = {0, 1, 2, 3};

        if (!vf)
                return fallback;

        return vf->swizzle;
}