126a9a5c59c85e3972250ce2cb8e83670f79a17c
[mesa.git] / src / gallium / drivers / lima / lima_format.c
1 /*
2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2018-2019 Lima Project
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, sub license,
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
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 NON-INFRINGEMENT. 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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include <stdint.h>
27 #include <stdlib.h>
28
29 #include <util/macros.h>
30
31 #include "lima_format.h"
32
33 #define LIMA_TEXEL_FORMAT_L8 0x09
34 #define LIMA_TEXEL_FORMAT_A8 0x0a
35 #define LIMA_TEXEL_FORMAT_I8 0x0b
36 #define LIMA_TEXEL_FORMAT_BGR_565 0x0e
37 #define LIMA_TEXEL_FORMAT_BGRA_5551 0x0f
38 #define LIMA_TEXEL_FORMAT_BGRA_4444 0x10
39 #define LIMA_TEXEL_FORMAT_L8A8 0x11
40 #define LIMA_TEXEL_FORMAT_L16 0x12
41 #define LIMA_TEXEL_FORMAT_A16 0x13
42 #define LIMA_TEXEL_FORMAT_I16 0x14
43 #define LIMA_TEXEL_FORMAT_RGB_888 0x15
44 #define LIMA_TEXEL_FORMAT_RGBA_8888 0x16
45 #define LIMA_TEXEL_FORMAT_RGBX_8888 0x17
46 #define LIMA_TEXEL_FORMAT_ETC1_RGB8 0x20
47 #define LIMA_TEXEL_FORMAT_Z24S8 0x2c
48 #define LIMA_TEXEL_FORMAT_NONE -1
49
50 #define LIMA_PIXEL_FORMAT_B5G6R5 0x00
51 #define LIMA_PIXEL_FORMAT_B5G5R5A1 0x01
52 #define LIMA_PIXEL_FORMAT_B4G4R4A4 0x02
53 #define LIMA_PIXEL_FORMAT_B8G8R8A8 0x03
54 #define LIMA_PIXEL_FORMAT_Z16 0x0e
55 #define LIMA_PIXEL_FORMAT_Z24S8 0x0f
56 #define LIMA_PIXEL_FORMAT_NONE -1
57
58 struct lima_format {
59 bool present;
60 int texel;
61 int pixel;
62 bool swap_r_b;
63 uint32_t channel_layout;
64 };
65
66 #define LIMA_FORMAT(pipe, tex, pix, swap, ch_layout) \
67 [PIPE_FORMAT_##pipe] = { \
68 .present = true, .texel = LIMA_TEXEL_FORMAT_##tex, \
69 .pixel = LIMA_PIXEL_FORMAT_##pix, .swap_r_b = swap, \
70 .channel_layout = ch_layout, \
71 }
72
73 static const struct lima_format lima_format_table[] = {
74 LIMA_FORMAT(R8G8B8A8_UNORM, RGBA_8888, B8G8R8A8, true, 0x8888),
75 LIMA_FORMAT(B8G8R8A8_UNORM, RGBA_8888, B8G8R8A8, false, 0x8888),
76 LIMA_FORMAT(R8G8B8A8_SRGB, RGBA_8888, B8G8R8A8, true, 0x8888),
77 LIMA_FORMAT(B8G8R8A8_SRGB, RGBA_8888, B8G8R8A8, false, 0x8888),
78 LIMA_FORMAT(R8G8B8X8_UNORM, RGBX_8888, B8G8R8A8, true, 0x8888),
79 LIMA_FORMAT(B8G8R8X8_UNORM, RGBX_8888, B8G8R8A8, false, 0x8888),
80 LIMA_FORMAT(B5G6R5_UNORM, BGR_565, B5G6R5, false, 0x8565),
81 /* BGRA_5551 seems to need channel layout 0x8565, it's not a typo */
82 LIMA_FORMAT(B5G5R5A1_UNORM, BGRA_5551, B5G5R5A1, false, 0x8565),
83 LIMA_FORMAT(B4G4R4A4_UNORM, BGRA_4444, B4G4R4A4, false, 0x8444),
84 LIMA_FORMAT(Z24_UNORM_S8_UINT, Z24S8, Z24S8, false, 0x0000),
85 LIMA_FORMAT(Z24X8_UNORM, Z24S8, Z24S8, false, 0x0000),
86 /* Blob uses L16 for Z16 */
87 LIMA_FORMAT(Z16_UNORM, L16, Z16, false, 0x0000),
88 LIMA_FORMAT(L16_UNORM, L16, NONE, false, 0x0000),
89 LIMA_FORMAT(L8_UNORM, L8, NONE, false, 0x0000),
90 LIMA_FORMAT(A16_UNORM, A16, NONE, false, 0x0000),
91 LIMA_FORMAT(A8_UNORM, A8, NONE, false, 0x0000),
92 LIMA_FORMAT(I16_UNORM, I16, NONE, false, 0x0000),
93 LIMA_FORMAT(I8_UNORM, I8, NONE, false, 0x0000),
94 LIMA_FORMAT(L8A8_UNORM, L8A8, NONE, false, 0x0000),
95 LIMA_FORMAT(ETC1_RGB8, ETC1_RGB8, NONE, false, 0x8888),
96 };
97
98 static const struct lima_format *
99 get_format(enum pipe_format f)
100 {
101 if (f >= ARRAY_SIZE(lima_format_table) ||
102 !lima_format_table[f].present)
103 return NULL;
104
105 return lima_format_table + f;
106 }
107
108 bool
109 lima_format_texel_supported(enum pipe_format f)
110 {
111 const struct lima_format *lf = get_format(f);
112
113 if (!lf)
114 return false;
115
116 return lf->texel != LIMA_TEXEL_FORMAT_NONE;
117 }
118
119 bool
120 lima_format_pixel_supported(enum pipe_format f)
121 {
122 const struct lima_format *lf = get_format(f);
123
124 if (!lf)
125 return false;
126
127 return lf->pixel != LIMA_PIXEL_FORMAT_NONE;
128 }
129
130 int
131 lima_format_get_texel(enum pipe_format f)
132 {
133 return lima_format_table[f].texel;
134 }
135
136 int
137 lima_format_get_pixel(enum pipe_format f)
138 {
139 return lima_format_table[f].pixel;
140 }
141
142 bool
143 lima_format_get_swap_rb(enum pipe_format f)
144 {
145 return lima_format_table[f].swap_r_b;
146 }
147
148 uint32_t
149 lima_format_get_channel_layout(enum pipe_format f)
150 {
151 return lima_format_table[f].channel_layout;
152 }