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_Z24X8 0x2c
49 /* This format is only used for depth/stencil reload */
50 #define LIMA_TEXEL_FORMAT_Z24S8_RLD 0x32
51
52 #define LIMA_PIXEL_FORMAT_B5G6R5 0x00
53 #define LIMA_PIXEL_FORMAT_B5G5R5A1 0x01
54 #define LIMA_PIXEL_FORMAT_B4G4R4A4 0x02
55 #define LIMA_PIXEL_FORMAT_B8G8R8A8 0x03
56 #define LIMA_PIXEL_FORMAT_B8 0x04
57 #define LIMA_PIXEL_FORMAT_G8B8 0x05
58 #define LIMA_PIXEL_FORMAT_Z16 0x0e
59 #define LIMA_PIXEL_FORMAT_Z24S8 0x0f
60
61 struct lima_format {
62 bool present;
63 int format;
64 bool swap_r_b;
65 union {
66 uint32_t channel_layout;
67 uint8_t swizzle[4];
68 };
69 };
70
71 #define SWIZ(x,y,z,w) { \
72 PIPE_SWIZZLE_##x, \
73 PIPE_SWIZZLE_##y, \
74 PIPE_SWIZZLE_##z, \
75 PIPE_SWIZZLE_##w \
76 }
77
78 #define LIMA_TEXEL_FORMAT(pipe, tex, swap, swiz) \
79 [PIPE_FORMAT_##pipe] = { \
80 .present = true, .format = LIMA_TEXEL_FORMAT_##tex, \
81 .swap_r_b = swap, .swizzle = swiz \
82 }
83
84 #define LIMA_PIXEL_FORMAT(pipe, pix, swap, ch_layout) \
85 [PIPE_FORMAT_##pipe] = { \
86 .present = true, .format = LIMA_PIXEL_FORMAT_##pix, \
87 .swap_r_b = swap, .channel_layout = ch_layout \
88 }
89
90 static const struct lima_format lima_texel_formats[] = {
91 LIMA_TEXEL_FORMAT(R8G8B8A8_UNORM, RGBA_8888, true, SWIZ(X, Y, Z, W)),
92 LIMA_TEXEL_FORMAT(B8G8R8A8_UNORM, RGBA_8888, false, SWIZ(X, Y, Z, W)),
93 LIMA_TEXEL_FORMAT(R8G8B8A8_SRGB, RGBA_8888, true, SWIZ(X, Y, Z, W)),
94 LIMA_TEXEL_FORMAT(B8G8R8A8_SRGB, RGBA_8888, false, SWIZ(X, Y, Z, W)),
95 LIMA_TEXEL_FORMAT(R8G8B8X8_UNORM, RGBX_8888, true, SWIZ(X, Y, Z, W)),
96 LIMA_TEXEL_FORMAT(B8G8R8X8_UNORM, RGBX_8888, false, SWIZ(X, Y, Z, W)),
97 LIMA_TEXEL_FORMAT(B5G6R5_UNORM, BGR_565, false, SWIZ(X, Y, Z, W)),
98 LIMA_TEXEL_FORMAT(B5G5R5A1_UNORM, BGRA_5551, false, SWIZ(X, Y, Z, W)),
99 LIMA_TEXEL_FORMAT(B4G4R4A4_UNORM, BGRA_4444, false, SWIZ(X, Y, Z, W)),
100 LIMA_TEXEL_FORMAT(Z24_UNORM_S8_UINT, Z24X8, false, SWIZ(X, Y, Z, W)),
101 LIMA_TEXEL_FORMAT(Z24X8_UNORM, Z24X8, false, SWIZ(X, Y, Z, W)),
102 LIMA_TEXEL_FORMAT(L16_UNORM, L16, false, SWIZ(X, Y, Z, W)),
103 LIMA_TEXEL_FORMAT(L8_UNORM, L8, false, SWIZ(X, Y, Z, W)),
104 LIMA_TEXEL_FORMAT(A16_UNORM, A16, false, SWIZ(X, Y, Z, W)),
105 LIMA_TEXEL_FORMAT(A8_UNORM, A8, false, SWIZ(X, Y, Z, W)),
106 LIMA_TEXEL_FORMAT(I16_UNORM, I16, false, SWIZ(X, Y, Z, W)),
107 LIMA_TEXEL_FORMAT(I8_UNORM, I8, false, SWIZ(X, Y, Z, W)),
108 LIMA_TEXEL_FORMAT(L8A8_UNORM, L8A8, false, SWIZ(X, Y, Z, W)),
109 LIMA_TEXEL_FORMAT(ETC1_RGB8, ETC1_RGB8, false, SWIZ(X, Y, Z, W)),
110 LIMA_TEXEL_FORMAT(R8_UNORM, L8, false, SWIZ(X, 0, 0, 1)),
111 LIMA_TEXEL_FORMAT(R8G8_UNORM, L8A8, false, SWIZ(X, W, 0, 1)),
112 };
113
114 static const struct lima_format lima_pixel_formats[] = {
115 LIMA_PIXEL_FORMAT(R8G8B8A8_UNORM, B8G8R8A8, true, 0x8888),
116 LIMA_PIXEL_FORMAT(B8G8R8A8_UNORM, B8G8R8A8, false, 0x8888),
117 LIMA_PIXEL_FORMAT(R8G8B8A8_SRGB, B8G8R8A8, true, 0x8888),
118 LIMA_PIXEL_FORMAT(B8G8R8A8_SRGB, B8G8R8A8, false, 0x8888),
119 LIMA_PIXEL_FORMAT(R8G8B8X8_UNORM, B8G8R8A8, true, 0x8888),
120 LIMA_PIXEL_FORMAT(B8G8R8X8_UNORM, B8G8R8A8, false, 0x8888),
121 LIMA_PIXEL_FORMAT(B5G6R5_UNORM, B5G6R5, false, 0x8565),
122 LIMA_PIXEL_FORMAT(B5G5R5A1_UNORM, B5G5R5A1, false, 0x8565),
123 LIMA_PIXEL_FORMAT(B4G4R4A4_UNORM, B4G4R4A4, false, 0x8444),
124 /* FIXME: reload doesn't work for these formats for some reason */
125 //LIMA_PIXEL_FORMAT(R8_UNORM, B8, true, 0x8888),
126 //LIMA_PIXEL_FORMAT(R8G8_UNORM, G8B8, true, 0x8888),
127 LIMA_PIXEL_FORMAT(Z24_UNORM_S8_UINT, Z24S8, false, 0x0000),
128 LIMA_PIXEL_FORMAT(Z24X8_UNORM, Z24S8, false, 0x0000),
129 };
130
131 static const struct lima_format *
get_texel_format(enum pipe_format f)132 get_texel_format(enum pipe_format f)
133 {
134 if (f >= ARRAY_SIZE(lima_texel_formats) ||
135 !lima_texel_formats[f].present)
136 return NULL;
137
138 return lima_texel_formats + f;
139 }
140
141 static const struct lima_format *
get_pixel_format(enum pipe_format f)142 get_pixel_format(enum pipe_format f)
143 {
144 if (f >= ARRAY_SIZE(lima_pixel_formats) ||
145 !lima_pixel_formats[f].present)
146 return NULL;
147
148 return lima_pixel_formats + f;
149 }
150
151 bool
lima_format_texel_supported(enum pipe_format f)152 lima_format_texel_supported(enum pipe_format f)
153 {
154 const struct lima_format *lf = get_texel_format(f);
155
156 if (!lf)
157 return false;
158
159 return true;
160 }
161
162 bool
lima_format_pixel_supported(enum pipe_format f)163 lima_format_pixel_supported(enum pipe_format f)
164 {
165 const struct lima_format *lf = get_pixel_format(f);
166
167 if (!lf)
168 return false;
169
170 return true;
171 }
172
173 int
lima_format_get_texel(enum pipe_format f)174 lima_format_get_texel(enum pipe_format f)
175 {
176 return lima_texel_formats[f].format;
177 }
178
179 int
lima_format_get_texel_reload(enum pipe_format f)180 lima_format_get_texel_reload(enum pipe_format f)
181 {
182 switch (f) {
183 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
184 case PIPE_FORMAT_Z24X8_UNORM:
185 return LIMA_TEXEL_FORMAT_Z24S8_RLD;
186 default:
187 return lima_format_get_texel(f);
188 }
189 }
190
191 int
lima_format_get_pixel(enum pipe_format f)192 lima_format_get_pixel(enum pipe_format f)
193 {
194 return lima_pixel_formats[f].format;
195 }
196
197 bool
lima_format_get_texel_swap_rb(enum pipe_format f)198 lima_format_get_texel_swap_rb(enum pipe_format f)
199 {
200 return lima_texel_formats[f].swap_r_b;
201 }
202
203 bool
lima_format_get_pixel_swap_rb(enum pipe_format f)204 lima_format_get_pixel_swap_rb(enum pipe_format f)
205 {
206 return lima_pixel_formats[f].swap_r_b;
207 }
208
209 const uint8_t *
lima_format_get_texel_swizzle(enum pipe_format f)210 lima_format_get_texel_swizzle(enum pipe_format f)
211 {
212 return lima_texel_formats[f].swizzle;
213 }
214
215 uint32_t
lima_format_get_channel_layout(enum pipe_format f)216 lima_format_get_channel_layout(enum pipe_format f)
217 {
218 return lima_pixel_formats[f].channel_layout;
219 }
220