Debian Codegraph @

src/imagination/vulkan/pvr_arch_tex_state.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdint.h>
25 #include <vulkan/vulkan.h>
26
27 #include "hwdef/rogue_hw_defs.h"
28 #include "pco/pco_common.h"
29 #include "pvr_csb.h"
30 #include "pvr_device.h"
31 #include "pvr_device_info.h"
32 #include "pvr_formats.h"
33 #include "pvr_physical_device.h"
34 #include "pvr_tex_state.h"
35 #include "util/macros.h"
36 #include "util/u_math.h"
37 #include "vk_format.h"
38 #include "vk_log.h"
39
40 static enum ROGUE_TEXSTATE_SWIZ pvr_get_hw_swizzle(VkComponentSwizzle comp,
41 enum pipe_swizzle swz)
42 {
43 switch (swz) {
44 case PIPE_SWIZZLE_0:
45 return ROGUE_TEXSTATE_SWIZ_SRC_ZERO;
46 case PIPE_SWIZZLE_1:
47 return ROGUE_TEXSTATE_SWIZ_SRC_ONE;
48 case PIPE_SWIZZLE_X:
49 return ROGUE_TEXSTATE_SWIZ_SRCCHAN_0;
50 case PIPE_SWIZZLE_Y:
51 return ROGUE_TEXSTATE_SWIZ_SRCCHAN_1;
52 case PIPE_SWIZZLE_Z:
53 return ROGUE_TEXSTATE_SWIZ_SRCCHAN_2;
54 case PIPE_SWIZZLE_W:
55 return ROGUE_TEXSTATE_SWIZ_SRCCHAN_3;
56 case PIPE_SWIZZLE_NONE:
57 if (comp == VK_COMPONENT_SWIZZLE_A)
58 return ROGUE_TEXSTATE_SWIZ_SRC_ONE;
59 else
60 return ROGUE_TEXSTATE_SWIZ_SRC_ZERO;
61 default:
62 UNREACHABLE("Unknown enum pipe_swizzle");
63 };
64 }
65
66 static enum ROGUE_TEXSTATE_FORMAT
67 pvr_chroma_swap_format(enum ROGUE_TEXSTATE_FORMAT format)
68 {
69 switch (format) {
70 case ROGUE_TEXSTATE_FORMAT_YUV420_2PLANE:
71 return ROGUE_TEXSTATE_FORMAT_YVU420_2PLANE;
72 case ROGUE_TEXSTATE_FORMAT_YUV420_3PLANE:
73 return ROGUE_TEXSTATE_FORMAT_YVU420_3PLANE;
74 case ROGUE_TEXSTATE_FORMAT_YVU420_2PLANE:
75 return ROGUE_TEXSTATE_FORMAT_YUV420_2PLANE;
76 case ROGUE_TEXSTATE_FORMAT_YVU420_3PLANE:
77 return ROGUE_TEXSTATE_FORMAT_YUV420_3PLANE;
78 default:
79 UNREACHABLE("Unsupported format");
80 return ROGUE_TEXSTATE_FORMAT_INVALID;
81 }
82 }
83
84 static uint32_t setup_pck_info(VkFormat vk_format)
85 {
86 enum pipe_format format = vk_format_to_pipe_format(vk_format);
87 bool scale = false;
88 bool roundzero = false;
89 bool split = false;
90 enum pco_pck_format pck_format =
91 pco_pipe_to_pck_format(format, &scale, &roundzero, &split);
92
93 /* Invalidate the format bits, but clear the rest so they can still be set.
94 */
95 if (pck_format == ~0)
96 pck_format = PVR_PCK_FORMAT_INVALID;
97
98 uint32_t pck_info = pck_format;
99 if (split)
100 pck_info |= PVR_PCK_INFO_SPLIT_BIT;
101
102 if (scale)
103 pck_info |= PVR_PCK_INFO_SCALE_BIT;
104
105 if (roundzero)
106 pck_info |= PVR_PCK_INFO_ROUNDZERO_BIT;
107
108 if (util_format_is_unorm(format))
109 pck_info |= PVR_PCK_INFO_UNORM_BIT;
110
111 return pck_info;
112 }
113
114 VkResult pvr_arch_pack_tex_state(struct pvr_device *device,
115 const struct pvr_texture_state_info *info,
116 struct pvr_image_descriptor *state)
117 {
118 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
119 const struct vk_format_ycbcr_info *ycbcr =
120 vk_format_get_ycbcr_info(info->format);
121 enum pvr_memlayout mem_layout;
122 VkImageViewType iview_type;
123
124 if (info->type == VK_IMAGE_VIEW_TYPE_1D &&
125 info->mem_layout == PVR_MEMLAYOUT_LINEAR) {
126 /* Change the memory layout to twiddled as there isn't a TEXSTATE_TEXTYPE
127 * for 1D linear and 1D twiddled is equivalent.
128 */
129 mem_layout = PVR_MEMLAYOUT_TWIDDLED;
130 } else {
131 mem_layout = info->mem_layout;
132 }
133
134 if (info->is_cube && info->tex_state_type != PVR_TEXTURE_STATE_SAMPLE)
135 iview_type = VK_IMAGE_VIEW_TYPE_2D;
136 else
137 iview_type = info->type;
138
139 pvr_csb_pack (&state->words[0], TEXSTATE_IMAGE_WORD0, word0) {
140 if (mem_layout == PVR_MEMLAYOUT_LINEAR) {
141 switch (iview_type) {
142 case VK_IMAGE_VIEW_TYPE_2D:
143 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
144 word0.textype = ROGUE_TEXSTATE_TEXTYPE_STRIDE;
145 break;
146
147 default:
148 return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
149 }
150 } else if (mem_layout == PVR_MEMLAYOUT_TWIDDLED) {
151 switch (iview_type) {
152 case VK_IMAGE_VIEW_TYPE_1D:
153 case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
154 word0.textype = ROGUE_TEXSTATE_TEXTYPE_1D;
155 break;
156
157 case VK_IMAGE_VIEW_TYPE_2D:
158 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
159 word0.textype = ROGUE_TEXSTATE_TEXTYPE_2D;
160 break;
161
162 case VK_IMAGE_VIEW_TYPE_CUBE:
163 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
164 word0.textype = ROGUE_TEXSTATE_TEXTYPE_CUBE;
165 break;
166
167 default:
168 return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
169 }
170 } else if (mem_layout == PVR_MEMLAYOUT_3DTWIDDLED) {
171 switch (iview_type) {
172 case VK_IMAGE_VIEW_TYPE_2D:
173 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
174 case VK_IMAGE_VIEW_TYPE_3D:
175 word0.textype = ROGUE_TEXSTATE_TEXTYPE_3D;
176 break;
177
178 default:
179 return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
180 }
181 } else {
182 UNREACHABLE("Unknown memory layout");
183 }
184
185 /* When sampling from a combined D/S image, the TPU will default to only
186 * the depth aspect.
187 * The driver must select the correct single aspect format when sampling
188 * to avoid this.
189 */
190 word0.texformat =
191 pvr_arch_get_tex_format_aspect(info->format, info->aspect_mask);
192
193 if (info->swap_chroma) {
194 word0.texformat = pvr_chroma_swap_format(word0.texformat);
195 }
196
197 word0.smpcnt = util_logbase2(info->sample_count);
198 word0.swiz0 =
199 pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_R, info->swizzle[0]);
200 word0.swiz1 =
201 pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_G, info->swizzle[1]);
202 word0.swiz2 =
203 pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_B, info->swizzle[2]);
204 word0.swiz3 =
205 pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_A, info->swizzle[3]);
206
207 /* Gamma */
208 if (vk_format_is_srgb(info->format)) {
209 /* Gamma for 2 Component Formats has to be handled differently. */
210 if (vk_format_get_nr_components(info->format) == 2) {
211 /* Enable Gamma only for Channel 0 if Channel 1 is an Alpha
212 * Channel.
213 */
214 if (vk_format_has_alpha(info->format)) {
215 word0.twocomp_gamma = ROGUE_TEXSTATE_TWOCOMP_GAMMA_R;
216 } else {
217 /* Otherwise Enable Gamma for both the Channels. */
218 word0.twocomp_gamma = ROGUE_TEXSTATE_TWOCOMP_GAMMA_RG;
219
220 /* If Channel 0 happens to be the Alpha Channel, the
221 * ALPHA_MSB bit would not be set thereby disabling Gamma
222 * for Channel 0.
223 */
224 }
225 } else {
226 word0.gamma = ROGUE_TEXSTATE_GAMMA_ON;
227 }
228 }
229
230 word0.width = info->extent.width - 1;
231 if (iview_type != VK_IMAGE_VIEW_TYPE_1D &&
232 iview_type != VK_IMAGE_VIEW_TYPE_1D_ARRAY)
233 word0.height = info->extent.height - 1;
234 }
235
236 if (ycbcr) {
237 pvr_csb_pack (&state->words[1], TEXSTATE_YUV_IMAGE_WORD1, word1) {
238 word1.chroma_interpolation_v = 0;
239 word1.chroma_interpolation_u = 0;
240 word1.stride = info->extent.width - 1;
241 word1.texaddr = PVR_DEV_ADDR_OFFSET(info->addr, info->offset);
242 word1.csc_coeff_index = info->csc_coeff_index;
243 }
244 } else if (mem_layout == PVR_MEMLAYOUT_LINEAR) {
245 pvr_csb_pack (&state->words[1], TEXSTATE_STRIDE_IMAGE_WORD1, word1) {
246 assert(info->stride > 0U);
247 word1.stride = info->stride - 1U;
248 word1.num_mip_levels = info->mip_levels;
249 word1.mipmaps_present = info->mipmaps_present;
250
251 word1.texaddr = PVR_DEV_ADDR_OFFSET(info->addr, info->offset);
252
253 if (vk_format_is_alpha_on_msb(info->format))
254 word1.alpha_msb = true;
255
256 if (!PVR_HAS_FEATURE(dev_info, tpu_extended_integer_lookup) &&
257 !PVR_HAS_FEATURE(dev_info, tpu_image_state_v2)) {
258 if (info->flags & PVR_TEXFLAGS_INDEX_LOOKUP ||
259 info->flags & PVR_TEXFLAGS_BUFFER)
260 word1.index_lookup = true;
261 }
262
263 if (info->flags & PVR_TEXFLAGS_BUFFER)
264 word1.mipmaps_present = false;
265
266 if (PVR_HAS_FEATURE(dev_info, tpu_image_state_v2) &&
267 vk_format_is_compressed(info->format))
268 word1.tpu_image_state_v2_compression_mode =
269 ROGUE_TEXSTATE_COMPRESSION_MODE_TPU;
270 }
271 } else {
272 pvr_csb_pack (&state->words[1], TEXSTATE_IMAGE_WORD1, word1) {
273 word1.num_mip_levels = info->mip_levels;
274 word1.mipmaps_present = info->mipmaps_present;
275 word1.baselevel = info->base_level;
276
277 if (iview_type == VK_IMAGE_VIEW_TYPE_3D ||
278 /* 2d view of 3d */
279 (iview_type == VK_IMAGE_VIEW_TYPE_2D &&
280 mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)) {
281 if (info->extent.depth > 0)
282 word1.depth = info->extent.depth - 1;
283 } else {
284 uint32_t array_layers = info->array_size;
285
286 if (iview_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
287 array_layers /= 6;
288
289 if (array_layers > 0)
290 word1.depth = array_layers - 1;
291 }
292
293 word1.texaddr = PVR_DEV_ADDR_OFFSET(info->addr, info->offset);
294
295 if (!PVR_HAS_FEATURE(dev_info, tpu_extended_integer_lookup) &&
296 !PVR_HAS_FEATURE(dev_info, tpu_image_state_v2)) {
297 if (info->flags & PVR_TEXFLAGS_INDEX_LOOKUP ||
298 info->flags & PVR_TEXFLAGS_BUFFER)
299 word1.index_lookup = true;
300 }
301
302 if (info->flags & PVR_TEXFLAGS_BUFFER)
303 word1.mipmaps_present = false;
304
305 if (info->flags & PVR_TEXFLAGS_BORDER)
306 word1.border = true;
307
308 if (vk_format_is_alpha_on_msb(info->format))
309 word1.alpha_msb = true;
310
311 if (PVR_HAS_FEATURE(dev_info, tpu_image_state_v2) &&
312 vk_format_is_compressed(info->format))
313 word1.tpu_image_state_v2_compression_mode =
314 ROGUE_TEXSTATE_COMPRESSION_MODE_TPU;
315 }
316 }
317
318 state->meta[PCO_IMAGE_META_LAYER_SIZE] = info->layer_size;
319 state->meta[PCO_IMAGE_META_BUFFER_ELEMS] = info->buffer_elems;
320 state->meta[PCO_IMAGE_META_Z_SLICE] = info->z_slice;
321 state->meta[PCO_IMAGE_META_PCK_INFO] = setup_pck_info(info->format);
322
323 return VK_SUCCESS;
324 }