Debian Codegraph @

src/imagination/vulkan/pvr_arch_job_common.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 <stdbool.h>
25 #include <stdint.h>
26
27 #include "hwdef/rogue_hw_defs.h"
28 #include "hwdef/rogue_hw_utils.h"
29 #include "pvr_csb_enum_helpers.h"
30 #include "pvr_device_info.h"
31 #include "pvr_formats.h"
32 #include "pvr_job_common.h"
33 #include "util/macros.h"
34 #include "util/u_math.h"
35 #include "vk_alloc.h"
36 #include "vk_format.h"
37 #include "vk_object.h"
38
39 void pvr_arch_pbe_get_src_format_and_gamma(VkFormat vk_format,
40 enum pvr_pbe_gamma default_gamma,
41 bool with_packed_usc_channel,
42 uint32_t *const src_format_out,
43 enum pvr_pbe_gamma *const gamma_out)
44 {
45 const struct util_format_description *desc =
46 vk_format_description(vk_format);
47 uint32_t chan_0_width = desc->channel[0].size;
48
49 *gamma_out = default_gamma;
50
51 if (pvr_vk_format_has_32bit_component(vk_format) ||
52 vk_format_is_int(vk_format)) {
53 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
54 } else if (vk_format_is_float(vk_format)) {
55 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_F16_PER_CHANNEL;
56 } else if (vk_format_is_srgb(vk_format)) {
57 *gamma_out = PVR_PBE_GAMMA_ENABLED;
58
59 /* F16 source for gamma'd formats. */
60 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_F16_PER_CHANNEL;
61 } else if (vk_format_has_depth(vk_format) &&
62 vk_format_get_component_bits(vk_format,
63 UTIL_FORMAT_COLORSPACE_ZS,
64 0) > 16) {
65 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
66 } else if (vk_format_has_stencil(vk_format) &&
67 vk_format_get_component_bits(vk_format,
68 UTIL_FORMAT_COLORSPACE_ZS,
69 1) > 0) {
70 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
71 } else if (chan_0_width > 16) {
72 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
73 } else if (chan_0_width > 8) {
74 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_F16_PER_CHANNEL;
75 } else if (!with_packed_usc_channel) {
76 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_F16_PER_CHANNEL;
77 } else {
78 *src_format_out = ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
79 }
80 }
81
82 void pvr_arch_pbe_pack_state(
83 const struct pvr_device_info *dev_info,
84 const struct pvr_pbe_surf_params *surface_params,
85 const struct pvr_pbe_render_params *render_params,
86 uint32_t pbe_cs_words[static const ROGUE_NUM_PBESTATE_STATE_WORDS],
87 uint64_t pbe_reg_words[static const ROGUE_NUM_PBESTATE_REG_WORDS])
88 {
89 /* This function needs updating if the value of
90 * ROGUE_NUM_PBESTATE_STATE_WORDS changes, so check that it's the expected
91 * value.
92 */
93 STATIC_ASSERT(ROGUE_NUM_PBESTATE_STATE_WORDS == 2);
94
95 /* This function needs updating if the value of ROGUE_NUM_PBESTATE_REG_WORDS
96 * changes, so check that it's the expected value.
97 */
98 STATIC_ASSERT(ROGUE_NUM_PBESTATE_REG_WORDS == 3);
99
100 pbe_reg_words[2] = 0;
101
102 if (surface_params->z_only_render) {
103 pbe_cs_words[0] = 0;
104
105 pvr_csb_pack (&pbe_cs_words[1], PBESTATE_STATE_WORD1, state) {
106 state.emptytile = true;
107 }
108
109 pbe_reg_words[0] = 0;
110 pbe_reg_words[1] = 0;
111
112 return;
113 }
114
115 pvr_csb_pack (&pbe_cs_words[0], PBESTATE_STATE_WORD0, state) {
116 state.address_low = surface_params->addr;
117 }
118
119 pvr_csb_pack (&pbe_cs_words[1], PBESTATE_STATE_WORD1, state) {
120 state.address_high = surface_params->addr;
121
122 state.source_format = surface_params->source_format;
123
124 state.source_pos = pvr_pbestate_source_pos(render_params->source_start);
125 if (PVR_HAS_FEATURE(dev_info, eight_output_registers)) {
126 state.source_pos_offset_128 = render_params->source_start >=
127 PVR_PBE_STARTPOS_BIT128;
128 } else {
129 assert(render_params->source_start < PVR_PBE_STARTPOS_BIT128);
130 }
131
132 /* MRT index (Use 0 for a single render target)/ */
133 state.mrt_index = render_params->mrt_index;
134
135 /* Normalization flag based on output format. */
136 state.norm = surface_params->is_normalized;
137
138 state.packmode = surface_params->pbe_packmode;
139 }
140
141 pvr_csb_pack (&pbe_reg_words[0], PBESTATE_REG_WORD0, reg) {
142 reg.tilerelative = true;
143
144 switch (surface_params->mem_layout) {
145 case PVR_MEMLAYOUT_TWIDDLED:
146 reg.memlayout = ROGUE_PBESTATE_MEMLAYOUT_TWIDDLE_2D;
147 break;
148
149 case PVR_MEMLAYOUT_3DTWIDDLED:
150 reg.memlayout = ROGUE_PBESTATE_MEMLAYOUT_TWIDDLE_3D;
151 break;
152
153 case PVR_MEMLAYOUT_LINEAR:
154 default:
155 reg.memlayout = ROGUE_PBESTATE_MEMLAYOUT_LINEAR;
156 break;
157 }
158
159 /* FIXME: Remove rotation and y_flip hardcoding if needed. */
160 reg.rotation = ROGUE_PBESTATE_ROTATION_TYPE_0_DEG;
161 reg.y_flip = false;
162
163 /* Note: Due to gamma being overridden above, anything other than
164 * ENABLED/NONE is ignored.
165 */
166 if (surface_params->gamma == PVR_PBE_GAMMA_ENABLED) {
167 reg.gamma = true;
168
169 if (surface_params->nr_components == 2)
170 reg.twocomp_gamma =
171 ROGUE_PBESTATE_TWOCOMP_GAMMA_GAMMA_BOTH_CHANNELS;
172 }
173
174 reg.linestride = (surface_params->stride - 1) /
175 ROGUE_PBESTATE_REG_WORD0_LINESTRIDE_UNIT_SIZE;
176 reg.minclip_x = render_params->min_x_clip;
177
178 /* r, y or depth*/
179 switch (surface_params->swizzle[0]) {
180 case PIPE_SWIZZLE_X:
181 reg.swiz_chan0 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN0;
182 break;
183 case PIPE_SWIZZLE_Y:
184 reg.swiz_chan1 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN0;
185 break;
186 case PIPE_SWIZZLE_Z:
187 reg.swiz_chan2 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN0;
188 break;
189 case PIPE_SWIZZLE_W:
190 reg.swiz_chan3 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN0;
191 break;
192 case PIPE_SWIZZLE_0:
193 case PIPE_SWIZZLE_NONE:
194 reg.swiz_chan0 = ROGUE_PBESTATE_SWIZ_ZERO;
195 break;
196 case PIPE_SWIZZLE_1:
197 reg.swiz_chan0 = ROGUE_PBESTATE_SWIZ_ONE;
198 break;
199 default:
200 UNREACHABLE("Unknown enum pipe_swizzle");
201 break;
202 }
203 /* g, u or stencil*/
204 switch (surface_params->swizzle[1]) {
205 case PIPE_SWIZZLE_X:
206 reg.swiz_chan0 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN1;
207 break;
208 case PIPE_SWIZZLE_Y:
209 reg.swiz_chan1 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN1;
210 break;
211 case PIPE_SWIZZLE_Z:
212 reg.swiz_chan2 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN1;
213 break;
214 case PIPE_SWIZZLE_W:
215 reg.swiz_chan3 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN1;
216 break;
217 case PIPE_SWIZZLE_0:
218 case PIPE_SWIZZLE_NONE:
219 reg.swiz_chan1 = ROGUE_PBESTATE_SWIZ_ZERO;
220 break;
221 case PIPE_SWIZZLE_1:
222 reg.swiz_chan1 = ROGUE_PBESTATE_SWIZ_ONE;
223 break;
224 default:
225 UNREACHABLE("Unknown enum pipe_swizzle");
226 break;
227 }
228 /* b or v*/
229 switch (surface_params->swizzle[2]) {
230 case PIPE_SWIZZLE_X:
231 reg.swiz_chan0 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN2;
232 break;
233 case PIPE_SWIZZLE_Y:
234 reg.swiz_chan1 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN2;
235 break;
236 case PIPE_SWIZZLE_Z:
237 reg.swiz_chan2 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN2;
238 break;
239 case PIPE_SWIZZLE_W:
240 reg.swiz_chan3 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN2;
241 break;
242 case PIPE_SWIZZLE_0:
243 case PIPE_SWIZZLE_NONE:
244 reg.swiz_chan2 = ROGUE_PBESTATE_SWIZ_ZERO;
245 break;
246 case PIPE_SWIZZLE_1:
247 reg.swiz_chan2 = ROGUE_PBESTATE_SWIZ_ONE;
248 break;
249 default:
250 UNREACHABLE("Unknown enum pipe_swizzle");
251 break;
252 }
253 /* a */
254 switch (surface_params->swizzle[3]) {
255 case PIPE_SWIZZLE_X:
256 reg.swiz_chan0 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN3;
257 break;
258 case PIPE_SWIZZLE_Y:
259 reg.swiz_chan1 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN3;
260 break;
261 case PIPE_SWIZZLE_Z:
262 reg.swiz_chan2 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN3;
263 break;
264 case PIPE_SWIZZLE_W:
265 reg.swiz_chan3 = ROGUE_PBESTATE_SWIZ_SOURCE_CHAN3;
266 break;
267 case PIPE_SWIZZLE_0:
268 case PIPE_SWIZZLE_NONE:
269 reg.swiz_chan3 = ROGUE_PBESTATE_SWIZ_ZERO;
270 break;
271 case PIPE_SWIZZLE_1:
272 reg.swiz_chan3 = ROGUE_PBESTATE_SWIZ_ONE;
273 break;
274 default:
275 UNREACHABLE("Unknown enum pipe_swizzle");
276 break;
277 }
278
279 if (surface_params->mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)
280 reg.size_z = util_logbase2_ceil(surface_params->depth);
281
282 reg.downscale = surface_params->down_scale;
283 }
284
285 pvr_csb_pack (&pbe_reg_words[1], PBESTATE_REG_WORD1, reg) {
286 if (surface_params->mem_layout == PVR_MEMLAYOUT_TWIDDLED ||
287 surface_params->mem_layout == PVR_MEMLAYOUT_3DTWIDDLED) {
288 reg.size_x = util_logbase2_ceil(surface_params->width);
289 reg.size_y = util_logbase2_ceil(surface_params->height);
290 }
291
292 reg.minclip_y = render_params->min_y_clip;
293 reg.maxclip_x = render_params->max_x_clip;
294 reg.zslice = render_params->slice;
295 reg.maxclip_y = render_params->max_y_clip;
296 }
297 }
298
299 /* TODO: Split this into smaller functions to make it easier to follow. When
300 * doing this, it would be nice to have a function that returns
301 * total_tiles_in_flight so that CR_ISP_CTL can be fully packed in
302 * pvr_render_job_ws_fragment_state_init().
303 */
304 void pvr_arch_setup_tiles_in_flight(
305 const struct pvr_device_info *dev_info,
306 const struct pvr_device_runtime_info *dev_runtime_info,
307 uint32_t msaa_mode,
308 uint32_t pixel_width,
309 bool paired_tiles,
310 uint32_t max_tiles_in_flight,
311 uint32_t *const isp_ctl_out,
312 uint32_t *const pixel_ctl_out)
313 {
314 uint32_t total_tiles_in_flight = 0;
315 uint32_t usable_partition_size;
316 uint32_t partitions_available;
317 uint32_t usc_min_output_regs;
318 uint32_t max_partitions;
319 uint32_t partition_size;
320 uint32_t max_phantoms;
321 uint32_t tile_size_x;
322 uint32_t tile_size_y;
323 uint32_t isp_samples;
324
325 /* Round up the pixel width to the next allocation granularity. */
326 usc_min_output_regs =
327 PVR_GET_FEATURE_VALUE(dev_info, usc_min_output_registers_per_pix, 0);
328 pixel_width = MAX2(pixel_width, usc_min_output_regs);
329 pixel_width = util_next_power_of_two(pixel_width);
330
331 assert(pixel_width <= rogue_get_max_output_regs_per_pixel(dev_info));
332
333 partition_size = pixel_width;
334
335 isp_samples = PVR_GET_FEATURE_VALUE(dev_info, isp_samples_per_pixel, 1);
336 if (isp_samples == 2) {
337 if (msaa_mode != ROGUE_CR_ISP_AA_MODE_TYPE_AA_NONE)
338 partition_size *= 2U;
339 } else if (isp_samples == 4) {
340 if (msaa_mode == ROGUE_CR_ISP_AA_MODE_TYPE_AA_4X ||
341 msaa_mode == ROGUE_CR_ISP_AA_MODE_TYPE_AA_8X)
342 partition_size *= 4U;
343 else if (msaa_mode == ROGUE_CR_ISP_AA_MODE_TYPE_AA_2X)
344 partition_size *= 2U;
345 }
346
347 /* Cores with a tile size of 16x16 don't have quadrant affinity. Hence the
348 * partition size is the same as for a 32x32 tile quadrant (with no MSAA).
349 * When MSAA is enabled, the USC has to process half the tile (16x8 pixels).
350 */
351 tile_size_x = PVR_GET_FEATURE_VALUE(dev_info, tile_size_x, 0);
352 tile_size_y = PVR_GET_FEATURE_VALUE(dev_info, tile_size_y, 0);
353
354 /* We only support square tiles. */
355 assert(tile_size_x == tile_size_y);
356
357 if (tile_size_x == 16U) {
358 /* Cores with 16x16 tiles does not use tile quadrants. */
359 partition_size *= tile_size_x * tile_size_y;
360 } else {
361 /* Size of a tile quadrant (in dwords). */
362 partition_size *= (tile_size_x * tile_size_y / 4U);
363 }
364
365 /* Maximum available partition space for partitions of this size. */
366 max_partitions = PVR_GET_FEATURE_VALUE(dev_info, max_partitions, 0);
367 usable_partition_size = MIN2(dev_runtime_info->total_reserved_partition_size,
368 partition_size * max_partitions);
369
370 if (PVR_GET_FEATURE_VALUE(dev_info, common_store_size_in_dwords, 0) <
371 (1024 * 4 * 4)) {
372 /* Do not apply the limit for cores with 16x16 tile size (no quadrant
373 * affinity). */
374 if (tile_size_x != 16) {
375 /* This is to counter the extremely limited CS size on some cores.
376 */
377 /* Available partition space is limited to 8 tile quadrants. */
378 usable_partition_size =
379 MIN2((tile_size_x * tile_size_y / 4U) * 8U, usable_partition_size);
380 }
381 }
382
383 /* Ensure that maximum number of partitions in use is not greater
384 * than the total number of partitions available.
385 */
386 partitions_available =
387 MIN2(max_partitions, usable_partition_size / partition_size);
388
389 if (PVR_HAS_FEATURE(dev_info, xt_top_infrastructure))
390 max_phantoms = dev_runtime_info->num_phantoms;
391 else if (PVR_HAS_FEATURE(dev_info, roguexe))
392 max_phantoms = PVR_GET_FEATURE_VALUE(dev_info, num_raster_pipes, 0);
393 else
394 max_phantoms = 1;
395
396 for (uint32_t i = 0; i < max_phantoms; i++) {
397 uint32_t usc_tiles_in_flight = partitions_available;
398 uint32_t isp_tiles_in_flight;
399
400 /* Cores with tiles size other than 16x16 use tile quadrants. */
401 if (tile_size_x != 16) {
402 uint32_t num_clusters =
403 PVR_GET_FEATURE_VALUE(dev_info, num_clusters, 0U);
404 usc_tiles_in_flight =
405 (usc_tiles_in_flight * MIN2(4U, num_clusters - (4U * i))) / 4U;
406 }
407
408 assert(usc_tiles_in_flight > 0);
409
410 isp_tiles_in_flight =
411 PVR_GET_FEATURE_VALUE(dev_info, isp_max_tiles_in_flight, 0);
412 /* Ensure that maximum number of ISP tiles in flight is not greater
413 * than the maximum number of USC tiles in flight.
414 */
415 if (!PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format) ||
416 PVR_GET_FEATURE_VALUE(dev_info, simple_parameter_format_version, 0) !=
417 2) {
418 isp_tiles_in_flight /= dev_runtime_info->num_phantoms;
419 }
420
421 isp_tiles_in_flight = MIN2(usc_tiles_in_flight, isp_tiles_in_flight);
422
423 /* Limit the number of tiles in flight if the shaders have
424 * requested a large allocation of local memory.
425 */
426 if (max_tiles_in_flight > 0U) {
427 isp_tiles_in_flight = MIN2(usc_tiles_in_flight, max_tiles_in_flight);
428
429 if (PVR_HAS_FEATURE(dev_info, roguexe)) {
430 if (tile_size_x == 16) {
431 /* The FW infers the tiles in flight value from the
432 * partitions setting.
433 */
434 /* Partitions per tile. */
435 partitions_available = isp_tiles_in_flight;
436 } else {
437 /* Partitions per tile quadrant. */
438 partitions_available = isp_tiles_in_flight * 4U;
439 }
440 }
441 }
442
443 /* Due to limitations of ISP_CTL_PIPE there can only be a difference of
444 * 1 between Phantoms.
445 */
446 if (total_tiles_in_flight > (isp_tiles_in_flight + 1U))
447 total_tiles_in_flight = isp_tiles_in_flight + 1U;
448
449 total_tiles_in_flight += isp_tiles_in_flight;
450 }
451
452 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format) &&
453 PVR_GET_FEATURE_VALUE(dev_info, simple_parameter_format_version, 0) ==
454 2) {
455 /* Limit the ISP tiles in flight to fit into the available USC partition
456 * store.
457 */
458 total_tiles_in_flight = MIN2(total_tiles_in_flight, partitions_available);
459 }
460
461 if (PVR_HAS_FEATURE(dev_info, paired_tiles) && paired_tiles) {
462 total_tiles_in_flight =
463 MIN2(total_tiles_in_flight, partitions_available / 2);
464 }
465
466 pvr_csb_pack (pixel_ctl_out, CR_USC_PIXEL_OUTPUT_CTRL, reg) {
467 if (pixel_width == 1 && usc_min_output_regs == 1) {
468 reg.width = ROGUE_CR_PIXEL_WIDTH_1REGISTER;
469 } else if (pixel_width == 2) {
470 reg.width = ROGUE_CR_PIXEL_WIDTH_2REGISTERS;
471 } else if (pixel_width == 4) {
472 reg.width = ROGUE_CR_PIXEL_WIDTH_4REGISTERS;
473 } else if (pixel_width == 8 &&
474 PVR_HAS_FEATURE(dev_info, eight_output_registers)) {
475 reg.width = ROGUE_CR_PIXEL_WIDTH_8REGISTERS;
476 } else if (usc_min_output_regs == 1) {
477 reg.width = ROGUE_CR_PIXEL_WIDTH_1REGISTER;
478 } else {
479 reg.width = ROGUE_CR_PIXEL_WIDTH_2REGISTERS;
480 }
481
482 if (PVR_HAS_FEATURE(dev_info, usc_pixel_partition_mask)) {
483 /* Setup the partition mask based on the maximum number of
484 * partitions available.
485 */
486 reg.partition_mask = (1 << max_partitions) - 1;
487 } else {
488 reg.enable_4th_partition = true;
489
490 /* Setup the partition mask based on the number of partitions
491 * available.
492 */
493 reg.partition_mask = (1U << partitions_available) - 1U;
494 }
495 }
496
497 pvr_csb_pack (isp_ctl_out, CR_ISP_CTL, reg) {
498 if (PVR_HAS_FEATURE(dev_info, xt_top_infrastructure))
499 reg.pipe_enable = (2 * total_tiles_in_flight) - 1;
500 else
501 reg.pipe_enable = total_tiles_in_flight - 1;
502 }
503 }