1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Broadcom
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
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci/**
25bf215546Sopenharmony_ci * @file vc4_formats.c
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci * Contains the table and accessors for VC4 texture and render target format
28bf215546Sopenharmony_ci * support.
29bf215546Sopenharmony_ci *
30bf215546Sopenharmony_ci * The hardware has limited support for texture formats, and extremely limited
31bf215546Sopenharmony_ci * support for render target formats.  As a result, we emulate other formats
32bf215546Sopenharmony_ci * in our shader code, and this stores the table for doing so.
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "util/format/u_format.h"
36bf215546Sopenharmony_ci#include "util/macros.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "vc4_context.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#define RT_NO        0
41bf215546Sopenharmony_ci#define RT_RGBA8888  1
42bf215546Sopenharmony_ci#define RT_RGB565    2
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistruct vc4_format {
45bf215546Sopenharmony_ci        /** Set if the pipe format is defined in the table. */
46bf215546Sopenharmony_ci        bool present;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci        /** Set to 0 if unsupported, 1 if RGBA8888, 2 if rgb565. */
49bf215546Sopenharmony_ci        uint8_t rt_type;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci        /** One of VC4_TEXTURE_TYPE_*. */
52bf215546Sopenharmony_ci        uint8_t tex_type;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci        /**
55bf215546Sopenharmony_ci         * Swizzle to apply to the RGBA shader output for storing to the tile
56bf215546Sopenharmony_ci         * buffer, to the RGBA tile buffer to produce shader input (for
57bf215546Sopenharmony_ci         * blending), and for turning the rgba8888 texture sampler return
58bf215546Sopenharmony_ci         * value into shader rgba values.
59bf215546Sopenharmony_ci         */
60bf215546Sopenharmony_ci        uint8_t swizzle[4];
61bf215546Sopenharmony_ci};
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci#define SWIZ(x,y,z,w) {          \
64bf215546Sopenharmony_ci        PIPE_SWIZZLE_##x, \
65bf215546Sopenharmony_ci        PIPE_SWIZZLE_##y, \
66bf215546Sopenharmony_ci        PIPE_SWIZZLE_##z, \
67bf215546Sopenharmony_ci        PIPE_SWIZZLE_##w  \
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci#define FORMAT(pipe, rt, tex, swiz)                                     \
71bf215546Sopenharmony_ci        [PIPE_FORMAT_##pipe] = { true, RT_##rt, VC4_TEXTURE_TYPE_##tex, swiz }
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cistatic const struct vc4_format vc4_format_table[] = {
74bf215546Sopenharmony_ci        FORMAT(R8G8B8A8_UNORM, RGBA8888, RGBA8888, SWIZ(X, Y, Z, W)),
75bf215546Sopenharmony_ci        FORMAT(R8G8B8X8_UNORM, RGBA8888, RGBA8888, SWIZ(X, Y, Z, 1)),
76bf215546Sopenharmony_ci        FORMAT(R8G8B8A8_SRGB,  RGBA8888, RGBA8888, SWIZ(X, Y, Z, W)),
77bf215546Sopenharmony_ci        FORMAT(R8G8B8X8_SRGB,  RGBA8888, RGBA8888, SWIZ(X, Y, Z, 1)),
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci        FORMAT(B8G8R8A8_UNORM, RGBA8888, RGBA8888, SWIZ(Z, Y, X, W)),
80bf215546Sopenharmony_ci        FORMAT(B8G8R8X8_UNORM, RGBA8888, RGBA8888, SWIZ(Z, Y, X, 1)),
81bf215546Sopenharmony_ci        FORMAT(B8G8R8A8_SRGB, RGBA8888, RGBA8888, SWIZ(Z, Y, X, W)),
82bf215546Sopenharmony_ci        FORMAT(B8G8R8X8_SRGB, RGBA8888, RGBA8888, SWIZ(Z, Y, X, 1)),
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci        FORMAT(B5G6R5_UNORM, RGB565, RGB565, SWIZ(X, Y, Z, 1)),
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci        FORMAT(ETC1_RGB8, NO, ETC1, SWIZ(X, Y, Z, 1)),
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci        /* Depth sampling will be handled by doing nearest filtering and not
89bf215546Sopenharmony_ci         * unpacking the RGBA value.
90bf215546Sopenharmony_ci         */
91bf215546Sopenharmony_ci        FORMAT(S8_UINT_Z24_UNORM, NO, RGBA8888, SWIZ(X, Y, Z, W)),
92bf215546Sopenharmony_ci        FORMAT(X8Z24_UNORM,       NO, RGBA8888, SWIZ(X, Y, Z, W)),
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci        FORMAT(B4G4R4A4_UNORM, NO, RGBA4444, SWIZ(Y, Z, W, X)),
95bf215546Sopenharmony_ci        FORMAT(B4G4R4X4_UNORM, NO, RGBA4444, SWIZ(Y, Z, W, 1)),
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci        FORMAT(A1B5G5R5_UNORM, NO, RGBA5551, SWIZ(X, Y, Z, W)),
98bf215546Sopenharmony_ci        FORMAT(X1B5G5R5_UNORM, NO, RGBA5551, SWIZ(X, Y, Z, 1)),
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci        FORMAT(A8_UNORM, NO, ALPHA, SWIZ(0, 0, 0, W)),
101bf215546Sopenharmony_ci        FORMAT(L8_UNORM, NO, ALPHA, SWIZ(W, W, W, 1)),
102bf215546Sopenharmony_ci        FORMAT(I8_UNORM, NO, ALPHA, SWIZ(W, W, W, W)),
103bf215546Sopenharmony_ci        FORMAT(R8_UNORM, NO, ALPHA, SWIZ(W, 0, 0, 1)),
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci        FORMAT(L8A8_UNORM, NO, LUMALPHA, SWIZ(X, X, X, W)),
106bf215546Sopenharmony_ci        FORMAT(R8G8_UNORM, NO, LUMALPHA, SWIZ(X, W, 0, 1)),
107bf215546Sopenharmony_ci};
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cistatic const struct vc4_format *
110bf215546Sopenharmony_ciget_format(enum pipe_format f)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci        if (f >= ARRAY_SIZE(vc4_format_table) ||
113bf215546Sopenharmony_ci            !vc4_format_table[f].present)
114bf215546Sopenharmony_ci                return NULL;
115bf215546Sopenharmony_ci        else
116bf215546Sopenharmony_ci                return &vc4_format_table[f];
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_cibool
120bf215546Sopenharmony_civc4_rt_format_supported(enum pipe_format f)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci        const struct vc4_format *vf = get_format(f);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci        if (!vf)
125bf215546Sopenharmony_ci                return false;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci        return vf->rt_type != RT_NO;
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_cibool
131bf215546Sopenharmony_civc4_rt_format_is_565(enum pipe_format f)
132bf215546Sopenharmony_ci{
133bf215546Sopenharmony_ci        const struct vc4_format *vf = get_format(f);
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci        if (!vf)
136bf215546Sopenharmony_ci                return false;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci        return vf->rt_type == RT_RGB565;
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_cibool
142bf215546Sopenharmony_civc4_tex_format_supported(enum pipe_format f)
143bf215546Sopenharmony_ci{
144bf215546Sopenharmony_ci        const struct vc4_format *vf = get_format(f);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci        return vf != NULL;
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ciuint8_t
150bf215546Sopenharmony_civc4_get_tex_format(enum pipe_format f)
151bf215546Sopenharmony_ci{
152bf215546Sopenharmony_ci        const struct vc4_format *vf = get_format(f);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci        if (!vf)
155bf215546Sopenharmony_ci                return 0;
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci        return vf->tex_type;
158bf215546Sopenharmony_ci}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ciconst uint8_t *
161bf215546Sopenharmony_civc4_get_format_swizzle(enum pipe_format f)
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci        const struct vc4_format *vf = get_format(f);
164bf215546Sopenharmony_ci        static const uint8_t fallback[] = {0, 1, 2, 3};
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci        if (!vf)
167bf215546Sopenharmony_ci                return fallback;
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci        return vf->swizzle;
170bf215546Sopenharmony_ci}
171