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/format/u_format.h"
30 #include <util/macros.h>
31
32 #include "lima_format.h"
33
34 #define LIMA_TEXEL_FORMAT_L8 0x09
35 #define LIMA_TEXEL_FORMAT_A8 0x0a
36 #define LIMA_TEXEL_FORMAT_I8 0x0b
37 #define LIMA_TEXEL_FORMAT_BGR_565 0x0e
38 #define LIMA_TEXEL_FORMAT_BGRA_5551 0x0f
39 #define LIMA_TEXEL_FORMAT_BGRA_4444 0x10
40 #define LIMA_TEXEL_FORMAT_L8A8 0x11
41 #define LIMA_TEXEL_FORMAT_L16 0x12
42 #define LIMA_TEXEL_FORMAT_A16 0x13
43 #define LIMA_TEXEL_FORMAT_I16 0x14
44 #define LIMA_TEXEL_FORMAT_RGB_888 0x15
45 #define LIMA_TEXEL_FORMAT_RGBA_8888 0x16
46 #define LIMA_TEXEL_FORMAT_RGBX_8888 0x17
47 #define LIMA_TEXEL_FORMAT_ETC1_RGB8 0x20
48 #define LIMA_TEXEL_FORMAT_L16_FLOAT 0x22
49 #define LIMA_TEXEL_FORMAT_A16_FLOAT 0x23
50 #define LIMA_TEXEL_FORMAT_I16_FLOAT 0x24
51 #define LIMA_TEXEL_FORMAT_L16A16_FLOAT 0x25
52 #define LIMA_TEXEL_FORMAT_R16G16B16A16_FLOAT 0x26
53 #define LIMA_TEXEL_FORMAT_R16G16B16_FLOAT 0x2f
54 #define LIMA_TEXEL_FORMAT_Z24X8 0x2c
55 /* This format is only used for depth/stencil reload */
56 #define LIMA_TEXEL_FORMAT_Z24S8_RLD 0x32
57
58 #define LIMA_PIXEL_FORMAT_B5G6R5 0x00
59 #define LIMA_PIXEL_FORMAT_B5G5R5A1 0x01
60 #define LIMA_PIXEL_FORMAT_B4G4R4A4 0x02
61 #define LIMA_PIXEL_FORMAT_B8G8R8A8 0x03
62 #define LIMA_PIXEL_FORMAT_B8 0x04
63 #define LIMA_PIXEL_FORMAT_G8B8 0x05
64 #define LIMA_PIXEL_FORMAT_B16G16R16A16_FLOAT 0x06
65 #define LIMA_PIXEL_FORMAT_B16_FLOAT 0x07
66 #define LIMA_PIXEL_FORMAT_G16B16_FLOAT 0x08
67 #define LIMA_PIXEL_FORMAT_Z16 0x0e
68 #define LIMA_PIXEL_FORMAT_Z24S8 0x0f
69
70 struct lima_format {
71 bool present;
72 int format;
73 bool swap_r_b;
74 union {
75 uint32_t channel_layout;
76 uint8_t swizzle[4];
77 };
78 };
79
80 #define SWIZ(x,y,z,w) { \
81 PIPE_SWIZZLE_##x, \
82 PIPE_SWIZZLE_##y, \
83 PIPE_SWIZZLE_##z, \
84 PIPE_SWIZZLE_##w \
85 }
86
87 #define LIMA_TEXEL_FORMAT(pipe, tex, swap, swiz) \
88 [PIPE_FORMAT_##pipe] = { \
89 .present = true, .format = LIMA_TEXEL_FORMAT_##tex, \
90 .swap_r_b = swap, .swizzle = swiz \
91 }
92
93 #define LIMA_PIXEL_FORMAT(pipe, pix, swap, ch_layout) \
94 [PIPE_FORMAT_##pipe] = { \
95 .present = true, .format = LIMA_PIXEL_FORMAT_##pix, \
96 .swap_r_b = swap, .channel_layout = ch_layout \
97 }
98
99 static const struct lima_format lima_texel_formats[] = {
100 LIMA_TEXEL_FORMAT(R8G8B8A8_UNORM, RGBA_8888, true, SWIZ(X, Y, Z, W)),
101 LIMA_TEXEL_FORMAT(B8G8R8A8_UNORM, RGBA_8888, false, SWIZ(X, Y, Z, W)),
102 LIMA_TEXEL_FORMAT(R8G8B8A8_SRGB, RGBA_8888, true, SWIZ(X, Y, Z, W)),
103 LIMA_TEXEL_FORMAT(B8G8R8A8_SRGB, RGBA_8888, false, SWIZ(X, Y, Z, W)),
104 LIMA_TEXEL_FORMAT(R8G8B8X8_UNORM, RGBX_8888, true, SWIZ(X, Y, Z, W)),
105 LIMA_TEXEL_FORMAT(B8G8R8X8_UNORM, RGBX_8888, false, SWIZ(X, Y, Z, W)),
106 LIMA_TEXEL_FORMAT(B5G6R5_UNORM, BGR_565, false, SWIZ(X, Y, Z, W)),
107 LIMA_TEXEL_FORMAT(B5G5R5A1_UNORM, BGRA_5551, false, SWIZ(X, Y, Z, W)),
108 LIMA_TEXEL_FORMAT(B4G4R4A4_UNORM, BGRA_4444, false, SWIZ(X, Y, Z, W)),
109 LIMA_TEXEL_FORMAT(Z24_UNORM_S8_UINT, Z24X8, false, SWIZ(X, Y, Z, W)),
110 LIMA_TEXEL_FORMAT(Z24X8_UNORM, Z24X8, false, SWIZ(X, Y, Z, W)),
111 LIMA_TEXEL_FORMAT(Z16_UNORM, L16, false, SWIZ(X, Y, Z, W)),
112 LIMA_TEXEL_FORMAT(L16_UNORM, L16, false, SWIZ(X, Y, Z, W)),
113 LIMA_TEXEL_FORMAT(L8_UNORM, L8, false, SWIZ(X, Y, Z, W)),
114 LIMA_TEXEL_FORMAT(A16_UNORM, A16, false, SWIZ(X, Y, Z, W)),
115 LIMA_TEXEL_FORMAT(A8_UNORM, A8, false, SWIZ(X, Y, Z, W)),
116 LIMA_TEXEL_FORMAT(I16_UNORM, I16, false, SWIZ(X, Y, Z, W)),
117 LIMA_TEXEL_FORMAT(I8_UNORM, I8, false, SWIZ(X, Y, Z, W)),
118 LIMA_TEXEL_FORMAT(L8A8_UNORM, L8A8, false, SWIZ(X, Y, Z, W)),
119 LIMA_TEXEL_FORMAT(ETC1_RGB8, ETC1_RGB8, false, SWIZ(X, Y, Z, W)),
120 LIMA_TEXEL_FORMAT(R8_UNORM, L8, false, SWIZ(X, 0, 0, 1)),
121 LIMA_TEXEL_FORMAT(R8G8_UNORM, L8A8, false, SWIZ(X, W, 0, 1)),
122 LIMA_TEXEL_FORMAT(R16G16B16_FLOAT, R16G16B16_FLOAT, true, SWIZ(X, Y, Z, W)),
123 LIMA_TEXEL_FORMAT(R16G16B16A16_FLOAT, R16G16B16A16_FLOAT, true, SWIZ(X, Y, Z, W)),
124 LIMA_TEXEL_FORMAT(L16_FLOAT, L16_FLOAT, false, SWIZ(X, Y, Z, W)),
125 LIMA_TEXEL_FORMAT(A16_FLOAT, A16_FLOAT, false, SWIZ(X, Y, Z, W)),
126 LIMA_TEXEL_FORMAT(I16_FLOAT, I16_FLOAT, false, SWIZ(X, Y, Z, W)),
127 LIMA_TEXEL_FORMAT(L16A16_FLOAT, L16A16_FLOAT, false, SWIZ(X, Y, Z, W)),
128 };
129
130 static const struct lima_format lima_pixel_formats[] = {
131 LIMA_PIXEL_FORMAT(R8G8B8A8_UNORM, B8G8R8A8, true, 0x8888),
132 LIMA_PIXEL_FORMAT(B8G8R8A8_UNORM, B8G8R8A8, false, 0x8888),
133 LIMA_PIXEL_FORMAT(R8G8B8A8_SRGB, B8G8R8A8, true, 0x8888),
134 LIMA_PIXEL_FORMAT(B8G8R8A8_SRGB, B8G8R8A8, false, 0x8888),
135 LIMA_PIXEL_FORMAT(R8G8B8X8_UNORM, B8G8R8A8, true, 0x8888),
136 LIMA_PIXEL_FORMAT(B8G8R8X8_UNORM, B8G8R8A8, false, 0x8888),
137 LIMA_PIXEL_FORMAT(B5G6R5_UNORM, B5G6R5, false, 0x8565),
138 LIMA_PIXEL_FORMAT(B5G5R5A1_UNORM, B5G5R5A1, false, 0x8565),
139 LIMA_PIXEL_FORMAT(B4G4R4A4_UNORM, B4G4R4A4, false, 0x8444),
140 LIMA_PIXEL_FORMAT(R8_UNORM, B8, true, 0x8888),
141 LIMA_PIXEL_FORMAT(R8G8_UNORM, G8B8, true, 0x8888),
142 LIMA_PIXEL_FORMAT(Z16_UNORM, Z16, false, 0x0000),
143 LIMA_PIXEL_FORMAT(Z24_UNORM_S8_UINT, Z24S8, false, 0x0000),
144 LIMA_PIXEL_FORMAT(Z24X8_UNORM, Z24S8, false, 0x0000),
145 LIMA_PIXEL_FORMAT(R16G16B16A16_FLOAT, B16G16R16A16_FLOAT, true, 0x0000),
146 LIMA_PIXEL_FORMAT(R16_FLOAT, B16_FLOAT, true, 0x0000),
147 LIMA_PIXEL_FORMAT(R16G16_FLOAT, G16B16_FLOAT, true, 0x0000),
148 };
149
150 static const struct lima_format *
get_texel_format(enum pipe_format f)151 get_texel_format(enum pipe_format f)
152 {
153 if (f >= ARRAY_SIZE(lima_texel_formats) ||
154 !lima_texel_formats[f].present)
155 return NULL;
156
157 return lima_texel_formats + f;
158 }
159
160 static const struct lima_format *
get_pixel_format(enum pipe_format f)161 get_pixel_format(enum pipe_format f)
162 {
163 if (f >= ARRAY_SIZE(lima_pixel_formats) ||
164 !lima_pixel_formats[f].present)
165 return NULL;
166
167 return lima_pixel_formats + f;
168 }
169
170 bool
lima_format_texel_supported(enum pipe_format f)171 lima_format_texel_supported(enum pipe_format f)
172 {
173 const struct lima_format *lf = get_texel_format(f);
174
175 if (!lf)
176 return false;
177
178 return true;
179 }
180
181 bool
lima_format_pixel_supported(enum pipe_format f)182 lima_format_pixel_supported(enum pipe_format f)
183 {
184 const struct lima_format *lf = get_pixel_format(f);
185
186 if (!lf)
187 return false;
188
189 return true;
190 }
191
192 int
lima_format_get_texel(enum pipe_format f)193 lima_format_get_texel(enum pipe_format f)
194 {
195 return lima_texel_formats[f].format;
196 }
197
198 int
lima_format_get_texel_reload(enum pipe_format f)199 lima_format_get_texel_reload(enum pipe_format f)
200 {
201 switch (f) {
202 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
203 case PIPE_FORMAT_Z24X8_UNORM:
204 return LIMA_TEXEL_FORMAT_Z24S8_RLD;
205 default:
206 return lima_format_get_texel(f);
207 }
208 }
209
210 int
lima_format_get_pixel(enum pipe_format f)211 lima_format_get_pixel(enum pipe_format f)
212 {
213 return lima_pixel_formats[f].format;
214 }
215
216 bool
lima_format_get_texel_swap_rb(enum pipe_format f)217 lima_format_get_texel_swap_rb(enum pipe_format f)
218 {
219 return lima_texel_formats[f].swap_r_b;
220 }
221
222 bool
lima_format_get_pixel_swap_rb(enum pipe_format f)223 lima_format_get_pixel_swap_rb(enum pipe_format f)
224 {
225 return lima_pixel_formats[f].swap_r_b;
226 }
227
228 const uint8_t *
lima_format_get_texel_swizzle(enum pipe_format f)229 lima_format_get_texel_swizzle(enum pipe_format f)
230 {
231 return lima_texel_formats[f].swizzle;
232 }
233
234 uint32_t
lima_format_get_channel_layout(enum pipe_format f)235 lima_format_get_channel_layout(enum pipe_format f)
236 {
237 return lima_pixel_formats[f].channel_layout;
238 }
239