Debian Codegraph @

src/imagination/vulkan/pvr_arch_job_transfer.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 <assert.h>
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <vulkan/vulkan.h>
30
31 #include "pvr_cmd_buffer.h"
32 #include "pvr_csb.h"
33 #include "pvr_csb_enum_helpers.h"
34 #include "pvr_device.h"
35 #include "pvr_formats.h"
36 #include "pvr_job_common.h"
37 #include "pvr_job_context.h"
38 #include "pvr_job_transfer.h"
39 #include "pvr_physical_device.h"
40 #include "pvr_tex_state.h"
41 #include "pvr_transfer_frag_store.h"
42 #include "pvr_types.h"
43 #include "pvr_usc.h"
44 #include "pvr_util.h"
45 #include "pvr_winsys.h"
46 #include "util/bitscan.h"
47 #include "util/list.h"
48 #include "util/macros.h"
49 #include "util/u_math.h"
50 #define XXH_INLINE_ALL
51 #include "util/xxhash.h"
52 #include "vk_format.h"
53 #include "vk_log.h"
54 #include "vk_sync.h"
55
56 #define PVR_TRANSFER_MAX_PASSES 10U
57 #define PVR_TRANSFER_MAX_CLIP_RECTS 4U
58 #define PVR_TRANSFER_MAX_PREPARES_PER_SUBMIT 16U
59 #define PVR_TRANSFER_MAX_CUSTOM_RECTS 3U
60
61 /* Number of triangles sent to the TSP per raster. */
62 #define PVR_TRANSFER_NUM_LAYERS 1U
63
64 #define PVR_MAX_WIDTH 16384
65 #define PVR_MAX_HEIGHT 16384
66
67 #define PVR_MAX_CLIP_SIZE(dev_info) \
68 (PVR_HAS_FEATURE(dev_info, screen_size8K) ? 8192U : 16384U)
69
70 enum pvr_paired_tiles {
71 PVR_PAIRED_TILES_NONE,
72 PVR_PAIRED_TILES_X,
73 PVR_PAIRED_TILES_Y
74 };
75
76 struct pvr_transfer_wa_source {
77 uint32_t src_offset;
78 uint32_t mapping_count;
79 struct pvr_rect_mapping mappings[PVR_TRANSFER_MAX_CUSTOM_MAPPINGS];
80 bool extend_height;
81 };
82
83 struct pvr_transfer_pass {
84 uint32_t dst_offset;
85
86 uint32_t source_count;
87 struct pvr_transfer_wa_source sources[PVR_TRANSFER_MAX_SOURCES];
88
89 uint32_t clip_rects_count;
90 VkRect2D clip_rects[PVR_TRANSFER_MAX_CLIP_RECTS];
91 };
92
93 /* Structure representing a layer iteration. */
94 struct pvr_transfer_custom_mapping {
95 bool double_stride;
96 uint32_t texel_unwind_src;
97 uint32_t texel_unwind_dst;
98 uint32_t texel_extend_src;
99 uint32_t texel_extend_dst;
100 uint32_t pass_count;
101 struct pvr_transfer_pass passes[PVR_TRANSFER_MAX_PASSES];
102 uint32_t max_clip_rects;
103 int32_t max_clip_size;
104 };
105
106 struct pvr_transfer_3d_iteration {
107 uint32_t texture_coords[12];
108 };
109
110 struct pvr_transfer_3d_state {
111 struct pvr_winsys_transfer_regs regs;
112
113 bool empty_dst;
114 bool down_scale;
115 /* Write all channels present in the dst from the USC even if those are
116 * constants.
117 */
118 bool dont_force_pbe;
119
120 /* The rate of the shader. */
121 uint32_t msaa_multiplier;
122 /* Top left corner of the render in ISP tiles. */
123 uint32_t origin_x_in_tiles;
124 /* Top left corner of the render in ISP tiles. */
125 uint32_t origin_y_in_tiles;
126 /* Width of the render in ISP tiles. */
127 uint32_t width_in_tiles;
128 /* Height of the render in ISP tiles. */
129 uint32_t height_in_tiles;
130
131 /* Width of a sample in registers (pixel partition width). */
132 uint32_t usc_pixel_width;
133
134 /* Properties of the USC shader. */
135 struct pvr_tq_shader_properties shader_props;
136
137 /* TODO: Use pvr_dev_addr_t of an offset type for these. */
138 uint32_t pds_shader_task_offset;
139 uint32_t tex_state_data_offset;
140 uint32_t uni_tex_code_offset;
141
142 uint32_t uniform_data_size;
143 uint32_t tex_state_data_size;
144 uint32_t usc_coeff_regs;
145
146 /* Pointer into the common store. */
147 uint32_t common_ptr;
148 /* Pointer into the dynamic constant reg buffer. */
149 uint32_t dynamic_const_reg_ptr;
150 /* Pointer into the USC constant reg buffer. */
151 uint32_t usc_const_reg_ptr;
152
153 uint32_t pds_coeff_task_offset;
154 uint32_t coeff_data_size;
155
156 /* Number of temporary 32bit registers used by PDS. */
157 uint32_t pds_temps;
158
159 struct pvr_transfer_custom_mapping custom_mapping;
160 uint32_t pass_idx;
161
162 enum pvr_filter filter[PVR_TRANSFER_MAX_SOURCES];
163 bool custom_filter;
164
165 enum pvr_paired_tiles pair_tiles;
166 };
167
168 struct pvr_transfer_prep_data {
169 struct pvr_winsys_transfer_cmd_flags flags;
170 struct pvr_transfer_3d_state state;
171 };
172
173 struct pvr_transfer_submit {
174 uint32_t prep_count;
175 struct pvr_transfer_prep_data
176 prep_array[PVR_TRANSFER_MAX_PREPARES_PER_SUBMIT];
177 };
178
179 static enum pvr_transfer_pbe_pixel_src pvr_pbe_src_format_raw(VkFormat format)
180 {
181 uint32_t bpp = vk_format_get_blocksizebits(format);
182
183 if (bpp <= 32U)
184 return PVR_TRANSFER_PBE_PIXEL_SRC_RAW32;
185 else if (bpp <= 64U)
186 return PVR_TRANSFER_PBE_PIXEL_SRC_RAW64;
187
188 return PVR_TRANSFER_PBE_PIXEL_SRC_RAW128;
189 }
190
191 static VkResult pvr_pbe_src_format_pick_depth(
192 const VkFormat src_format,
193 const VkFormat dst_format,
194 enum pvr_transfer_pbe_pixel_src *const src_format_out)
195 {
196 switch (dst_format) {
197 case VK_FORMAT_D24_UNORM_S8_UINT:
198 switch (src_format) {
199 case VK_FORMAT_D24_UNORM_S8_UINT:
200 case VK_FORMAT_X8_D24_UNORM_PACK32:
201 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D24S8_D24S8;
202 break;
203
204 case VK_FORMAT_D32_SFLOAT:
205 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32_D24S8;
206 break;
207
208 default:
209 return VK_ERROR_FORMAT_NOT_SUPPORTED;
210 }
211 break;
212
213 case VK_FORMAT_D32_SFLOAT_S8_UINT:
214 switch (src_format) {
215 case VK_FORMAT_D32_SFLOAT:
216 case VK_FORMAT_D32_SFLOAT_S8_UINT:
217 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32S8_D32S8;
218 break;
219
220 default:
221 return VK_ERROR_FORMAT_NOT_SUPPORTED;
222 }
223 break;
224
225 default:
226 return VK_ERROR_FORMAT_NOT_SUPPORTED;
227 }
228
229 return VK_SUCCESS;
230 }
231
232 static VkResult pvr_pbe_src_format_pick_stencil(
233 const VkFormat src_format,
234 const VkFormat dst_format,
235 enum pvr_transfer_pbe_pixel_src *const src_format_out)
236 {
237 switch (dst_format) {
238 case VK_FORMAT_D24_UNORM_S8_UINT:
239 switch (src_format) {
240 case VK_FORMAT_D24_UNORM_S8_UINT:
241 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D24S8;
242 break;
243
244 case VK_FORMAT_S8_UINT:
245 case VK_FORMAT_D32_SFLOAT_S8_UINT:
246 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D24S8;
247 break;
248
249 default:
250 return VK_ERROR_FORMAT_NOT_SUPPORTED;
251 }
252 break;
253
254 case VK_FORMAT_D32_SFLOAT_S8_UINT:
255 switch (src_format) {
256 case VK_FORMAT_S8_UINT:
257 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D32S8;
258 break;
259
260 case VK_FORMAT_D24_UNORM_S8_UINT:
261 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D32S8;
262 break;
263
264 case VK_FORMAT_D32_SFLOAT_S8_UINT:
265 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D32S8_D32S8;
266 break;
267
268 default:
269 return VK_ERROR_FORMAT_NOT_SUPPORTED;
270 }
271 break;
272
273 default:
274 return VK_ERROR_FORMAT_NOT_SUPPORTED;
275 }
276
277 return VK_SUCCESS;
278 }
279
280 static inline unsigned vk_format_depth_width(VkFormat vk_format)
281 {
282 const struct util_format_description *desc =
283 vk_format_description(vk_format);
284 assert(desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
285
286 unsigned depth_channel = desc->swizzle[0];
287 if (depth_channel == PIPE_SWIZZLE_NONE)
288 return 0;
289
290 return desc->channel[depth_channel].size;
291 }
292
293 static VkResult
294 pvr_pbe_src_format_ds(const struct pvr_transfer_cmd_surface *src,
295 const enum pvr_filter filter,
296 const VkFormat dst_format,
297 const uint32_t flags,
298 const bool down_scale,
299 enum pvr_transfer_pbe_pixel_src *src_format_out)
300 {
301 const VkFormat src_format = src->vk_format;
302
303 const bool src_depth = vk_format_has_depth(src_format);
304 const bool dst_depth = vk_format_has_depth(dst_format);
305 const bool src_stencil = vk_format_has_stencil(src_format);
306 const bool dst_stencil = vk_format_has_stencil(dst_format);
307 const unsigned src_depth_width = vk_format_depth_width(src_format);
308 const unsigned dst_depth_width = vk_format_depth_width(dst_format);
309
310 if (flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE) {
311 /* Merging, so destination should always have both. */
312 if (!dst_depth || !dst_stencil)
313 return VK_ERROR_FORMAT_NOT_SUPPORTED;
314
315 if (flags & PVR_TRANSFER_CMD_FLAGS_PICKD) {
316 return pvr_pbe_src_format_pick_depth(src_format,
317 dst_format,
318 src_format_out);
319 } else {
320 return pvr_pbe_src_format_pick_stencil(src_format,
321 dst_format,
322 src_format_out);
323 }
324 }
325
326 /* We can't invent channels out of nowhere. */
327 if ((dst_depth && !src_depth) || (dst_stencil && !src_stencil))
328 return VK_ERROR_FORMAT_NOT_SUPPORTED;
329
330 if (src_depth_width == 24) {
331 switch (dst_depth_width) {
332 case 0:
333 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SWAP_LMSB;
334 break;
335
336 case 24:
337 if (src_format == VK_FORMAT_X8_D24_UNORM_PACK32 &&
338 dst_format == VK_FORMAT_D24_UNORM_S8_UINT) {
339 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_CONV_S8D24_D24S8;
340 } else if (filter == PVR_FILTER_LINEAR) {
341 assert(src_format == dst_format);
342 if (src_format == VK_FORMAT_X8_D24_UNORM_PACK32) {
343 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_S8D24;
344 } else {
345 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_D24S8;
346 }
347 } else {
348 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_RAW32;
349 }
350 break;
351
352 case 32:
353 if (dst_stencil)
354 return VK_ERROR_FORMAT_NOT_SUPPORTED;
355
356 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_CONV_D24_D32;
357 break;
358
359 default:
360 return VK_ERROR_FORMAT_NOT_SUPPORTED;
361 }
362 } else if (src_depth_width == 32 && dst_depth_width == 24) {
363 if (src_format != VK_FORMAT_D32_SFLOAT)
364 return VK_ERROR_FORMAT_NOT_SUPPORTED;
365
366 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_CONV_D32_D24S8;
367 } else if (dst_depth_width == 16) {
368 if (down_scale && dst_format == VK_FORMAT_D16_UNORM)
369 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_U16NORM;
370 else if (src_format == dst_format)
371 *src_format_out = pvr_pbe_src_format_raw(dst_format);
372 else
373 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_U16NORM;
374 } else if (src_depth_width == 16 && dst_depth_width == 24) {
375 /* D16_UNORM results in a 0.0->1.0 float from the TPU, the same as D32 */
376 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_CONV_D32_D24S8;
377 } else {
378 if (dst_depth && dst_stencil) {
379 if (filter == PVR_FILTER_LINEAR) {
380 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_D32S8;
381 } else {
382 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_RAW64;
383 }
384 } else {
385 if (dst_depth_width == 32) {
386 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_F32;
387 } else {
388 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_RAW32;
389 }
390 }
391 }
392
393 return VK_SUCCESS;
394 }
395
396 /**
397 * How the PBE expects the output buffer for an RGBA space conversion.
398 */
399 static VkResult
400 pvr_pbe_src_format_normal(VkFormat src_format,
401 VkFormat dst_format,
402 bool down_scale,
403 bool dont_force_pbe,
404 enum pvr_transfer_pbe_pixel_src *src_format_out)
405 {
406 bool dst_signed = vk_format_is_sint(dst_format) ||
407 vk_format_is_snorm(dst_format);
408
409 if (vk_format_is_int(dst_format)) {
410 uint32_t red_width;
411 bool src_signed;
412 uint32_t count;
413
414 if (!vk_format_is_int(src_format))
415 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
416
417 src_signed = vk_format_is_sint(src_format);
418
419 red_width = vk_format_get_component_bits(dst_format,
420 UTIL_FORMAT_COLORSPACE_RGB,
421 0);
422
423 switch (red_width) {
424 case 8:
425 if (!src_signed && !dst_signed)
426 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_UU8888;
427 else if (src_signed && !dst_signed)
428 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SU8888;
429 else if (!src_signed && dst_signed)
430 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_US8888;
431 else
432 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SS8888;
433
434 break;
435
436 case 10:
437 switch (dst_format) {
438 case VK_FORMAT_A2B10G10R10_UINT_PACK32:
439 *src_format_out = src_signed ? PVR_TRANSFER_PBE_PIXEL_SRC_SU1010102
440 : PVR_TRANSFER_PBE_PIXEL_SRC_UU1010102;
441 break;
442
443 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
444 *src_format_out = src_signed
445 ? PVR_TRANSFER_PBE_PIXEL_SRC_RBSWAP_SU1010102
446 : PVR_TRANSFER_PBE_PIXEL_SRC_RBSWAP_UU1010102;
447 break;
448
449 default:
450 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
451 }
452 break;
453
454 case 16:
455 if (!src_signed && !dst_signed)
456 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_UU16U16;
457 else if (src_signed && !dst_signed)
458 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SU16U16;
459 else if (!src_signed && dst_signed)
460 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_US16S16;
461 else
462 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_SS16S16;
463
464 break;
465
466 case 32:
467 if (dont_force_pbe) {
468 count = vk_format_get_blocksizebits(dst_format) / 32U;
469 } else {
470 count = pvr_vk_format_get_common_color_channel_count(src_format,
471 dst_format);
472 }
473
474 if (!src_signed && !dst_signed) {
475 *src_format_out = (count > 2U) ? PVR_TRANSFER_PBE_PIXEL_SRC_RAW128
476 : PVR_TRANSFER_PBE_PIXEL_SRC_RAW64;
477 } else if (src_signed && !dst_signed) {
478 *src_format_out = (count > 2U) ? PVR_TRANSFER_PBE_PIXEL_SRC_S4XU32
479 : PVR_TRANSFER_PBE_PIXEL_SRC_SU32U32;
480 } else if (!src_signed && dst_signed) {
481 *src_format_out = (count > 2U) ? PVR_TRANSFER_PBE_PIXEL_SRC_U4XS32
482 : PVR_TRANSFER_PBE_PIXEL_SRC_US32S32;
483 } else {
484 *src_format_out = (count > 2U) ? PVR_TRANSFER_PBE_PIXEL_SRC_RAW128
485 : PVR_TRANSFER_PBE_PIXEL_SRC_RAW64;
486 }
487 break;
488
489 default:
490 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
491 }
492
493 } else if (vk_format_is_float(dst_format) ||
494 pvr_vk_format_is_fully_normalized(dst_format)) {
495 bool is_float = true;
496
497 if (!vk_format_is_float(src_format) &&
498 !pvr_vk_format_is_fully_normalized(src_format) &&
499 !vk_format_is_block_compressed(src_format)) {
500 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
501 }
502
503 if (pvr_vk_format_is_fully_normalized(dst_format)) {
504 uint32_t chan_width;
505
506 is_float = false;
507
508 /* Alpha only. */
509 switch (dst_format) {
510 case VK_FORMAT_D16_UNORM:
511 chan_width = 16;
512 break;
513
514 default:
515 chan_width =
516 vk_format_get_component_bits(dst_format,
517 UTIL_FORMAT_COLORSPACE_RGB,
518 0U);
519 break;
520 }
521
522 if (src_format == dst_format) {
523 switch (chan_width) {
524 case 16U:
525 if (down_scale) {
526 *src_format_out = dst_signed
527 ? PVR_TRANSFER_PBE_PIXEL_SRC_S16NORM
528 : PVR_TRANSFER_PBE_PIXEL_SRC_U16NORM;
529 } else {
530 *src_format_out = dst_signed
531 ? PVR_TRANSFER_PBE_PIXEL_SRC_SS16S16
532 : PVR_TRANSFER_PBE_PIXEL_SRC_UU16U16;
533 }
534 break;
535
536 case 32U:
537 *src_format_out = pvr_pbe_src_format_raw(dst_format);
538 break;
539 default:
540 is_float = true;
541 break;
542 }
543 } else {
544 switch (chan_width) {
545 case 16U:
546 *src_format_out = dst_signed
547 ? PVR_TRANSFER_PBE_PIXEL_SRC_S16NORM
548 : PVR_TRANSFER_PBE_PIXEL_SRC_U16NORM;
549 break;
550 default:
551 is_float = true;
552 break;
553 }
554 }
555 }
556
557 if (is_float) {
558 if (pvr_vk_format_has_32bit_component(dst_format)) {
559 uint32_t count;
560
561 if (dont_force_pbe) {
562 count = vk_format_get_blocksizebits(dst_format) / 32U;
563 } else {
564 count = pvr_vk_format_get_common_color_channel_count(src_format,
565 dst_format);
566 }
567
568 switch (count) {
569 case 1U:
570 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_F32;
571 break;
572 case 2U:
573 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_F32X2;
574 break;
575 default:
576 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_F32X4;
577 break;
578 }
579 } else {
580 if (dst_format == VK_FORMAT_B8G8R8A8_UNORM ||
581 dst_format == VK_FORMAT_R8G8B8A8_UNORM ||
582 dst_format == VK_FORMAT_A8B8G8R8_UNORM_PACK32) {
583 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_F16_U8;
584 } else {
585 *src_format_out = PVR_TRANSFER_PBE_PIXEL_SRC_F16F16;
586 }
587 }
588 }
589 } else {
590 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
591 }
592
593 return VK_SUCCESS;
594 }
595
596 static inline uint32_t
597 pvr_get_blit_flags(const struct pvr_transfer_cmd *transfer_cmd)
598 {
599 return transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_FAST2D
600 ? 0
601 : transfer_cmd->flags;
602 }
603
604 static VkResult pvr_pbe_src_format(struct pvr_transfer_cmd *transfer_cmd,
605 struct pvr_transfer_3d_state *state,
606 struct pvr_tq_shader_properties *prop)
607 {
608 struct pvr_tq_layer_properties *layer = &prop->layer_props;
609 const enum pvr_filter filter = transfer_cmd->source_count
610 ? transfer_cmd->sources[0].filter
611 : PVR_FILTER_POINT;
612 const uint32_t flags = transfer_cmd->flags;
613 VkFormat dst_format = transfer_cmd->dst.vk_format;
614 const struct pvr_transfer_cmd_surface *src;
615 VkFormat src_format;
616 bool down_scale;
617
618 if (transfer_cmd->source_count > 0) {
619 src = &transfer_cmd->sources[0].surface;
620 down_scale = transfer_cmd->sources[0].resolve_op == PVR_RESOLVE_BLEND &&
621 transfer_cmd->sources[0].surface.sample_count > 1U &&
622 transfer_cmd->dst.sample_count <= 1U;
623 } else {
624 src = &transfer_cmd->dst;
625 down_scale = false;
626 }
627
628 src_format = src->vk_format;
629
630 /* This has to come before the rest as S8 for instance is integer and
631 * signedness check fails on D24S8.
632 */
633 if (vk_format_is_depth_or_stencil(src_format) ||
634 vk_format_is_depth_or_stencil(dst_format) ||
635 flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE) {
636 return pvr_pbe_src_format_ds(src,
637 filter,
638 dst_format,
639 flags,
640 down_scale,
641 &layer->pbe_format);
642 }
643
644 return pvr_pbe_src_format_normal(src_format,
645 dst_format,
646 down_scale,
647 state->dont_force_pbe,
648 &layer->pbe_format);
649 }
650
651 static inline void pvr_setup_hwbg_object(const struct pvr_device_info *dev_info,
652 struct pvr_transfer_3d_state *state)
653 {
654 struct pvr_winsys_transfer_regs *regs = &state->regs;
655
656 pvr_csb_pack (&regs->pds_bgnd0_base, CR_PDS_BGRND0_BASE, reg) {
657 reg.shader_addr = PVR_DEV_ADDR(state->pds_shader_task_offset);
658 assert(pvr_dev_addr_is_aligned(
659 reg.shader_addr,
660 ROGUE_CR_PDS_BGRND0_BASE_SHADER_ADDR_ALIGNMENT));
661 reg.texunicode_addr = PVR_DEV_ADDR(state->uni_tex_code_offset);
662 assert(pvr_dev_addr_is_aligned(
663 reg.texunicode_addr,
664 ROGUE_CR_PDS_BGRND0_BASE_TEXUNICODE_ADDR_ALIGNMENT));
665 }
666
667 pvr_csb_pack (&regs->pds_bgnd1_base, CR_PDS_BGRND1_BASE, reg) {
668 reg.texturedata_addr = PVR_DEV_ADDR(state->tex_state_data_offset);
669 assert(pvr_dev_addr_is_aligned(
670 reg.texturedata_addr,
671 ROGUE_CR_PDS_BGRND1_BASE_TEXTUREDATA_ADDR_ALIGNMENT));
672 }
673
674 /* BGRND 2 not needed, background object PDS doesn't use uniform program. */
675
676 pvr_csb_pack (&regs->pds_bgnd3_sizeinfo, CR_PDS_BGRND3_SIZEINFO, reg) {
677 reg.usc_sharedsize =
678 DIV_ROUND_UP(state->common_ptr,
679 ROGUE_CR_PDS_BGRND3_SIZEINFO_USC_SHAREDSIZE_UNIT_SIZE);
680
681 assert(!(state->uniform_data_size &
682 (ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_UNIFORMSIZE_UNIT_SIZE - 1)));
683 reg.pds_uniformsize =
684 state->uniform_data_size /
685 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_UNIFORMSIZE_UNIT_SIZE;
686
687 assert(
688 !(state->tex_state_data_size &
689 (ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEXTURESTATESIZE_UNIT_SIZE - 1)));
690 reg.pds_texturestatesize =
691 state->tex_state_data_size /
692 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEXTURESTATESIZE_UNIT_SIZE;
693
694 reg.pds_tempsize =
695 DIV_ROUND_UP(state->pds_temps,
696 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEMPSIZE_UNIT_SIZE);
697 }
698 }
699
700 static inline bool
701 pvr_is_surface_aligned(pvr_dev_addr_t dev_addr, bool is_input, uint32_t bpp)
702 {
703 /* 96 bpp is 32 bit granular. */
704 if (bpp == 64U || bpp == 128U) {
705 uint64_t mask = (uint64_t)((bpp >> 3U) - 1U);
706
707 if ((dev_addr.addr & mask) != 0ULL)
708 return false;
709 }
710
711 if (is_input) {
712 if ((dev_addr.addr &
713 (ROGUE_TEXSTATE_STRIDE_IMAGE_WORD1_TEXADDR_ALIGNMENT - 1U)) !=
714 0ULL) {
715 return false;
716 }
717 } else {
718 if ((dev_addr.addr &
719 (ROGUE_PBESTATE_STATE_WORD0_ADDRESS_LOW_ALIGNMENT - 1U)) != 0ULL) {
720 return false;
721 }
722 }
723
724 return true;
725 }
726
727 static inline VkResult
728 pvr_mem_layout_spec(const struct pvr_transfer_cmd_surface *surface,
729 uint32_t load,
730 bool is_input,
731 uint32_t *width_out,
732 uint32_t *height_out,
733 uint32_t *stride_out,
734 enum pvr_memlayout *mem_layout_out,
735 pvr_dev_addr_t *dev_addr_out)
736 {
737 const uint32_t bpp = vk_format_get_blocksizebits(surface->vk_format);
738 uint32_t unsigned_stride;
739
740 *mem_layout_out = surface->mem_layout;
741 *height_out = surface->height;
742 *width_out = surface->width;
743 *stride_out = surface->stride;
744 *dev_addr_out = surface->dev_addr;
745
746 if (surface->mem_layout != PVR_MEMLAYOUT_LINEAR &&
747 !pvr_is_surface_aligned(*dev_addr_out, is_input, bpp)) {
748 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
749 }
750
751 switch (surface->mem_layout) {
752 case PVR_MEMLAYOUT_LINEAR:
753 if (surface->stride == 0U)
754 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
755
756 unsigned_stride = *stride_out;
757
758 if (!pvr_is_surface_aligned(*dev_addr_out, is_input, bpp))
759 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
760
761 if (unsigned_stride < *width_out)
762 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
763
764 if (!is_input) {
765 if (unsigned_stride == 1U) {
766 /* Change the setup to twiddling as that doesn't hit the stride
767 * limit and twiddled == strided when 1px stride.
768 */
769 *mem_layout_out = PVR_MEMLAYOUT_TWIDDLED;
770 }
771 }
772
773 *stride_out = unsigned_stride;
774 break;
775
776 case PVR_MEMLAYOUT_TWIDDLED:
777 case PVR_MEMLAYOUT_3DTWIDDLED:
778 /* Ignoring stride value for twiddled/tiled surface. */
779 *stride_out = *width_out;
780 break;
781
782 default:
783 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
784 }
785
786 return VK_SUCCESS;
787 }
788
789 static VkResult
790 pvr_pbe_setup_codegen_defaults(const struct pvr_device_info *dev_info,
791 const struct pvr_transfer_cmd *transfer_cmd,
792 struct pvr_transfer_3d_state *state,
793 struct pvr_pbe_surf_params *surface_params,
794 struct pvr_pbe_render_params *render_params)
795 {
796 const struct pvr_transfer_cmd_surface *dst = &transfer_cmd->dst;
797 const uint8_t *swizzle;
798 VkFormat format;
799 VkResult result;
800
801 switch (dst->vk_format) {
802 case VK_FORMAT_D24_UNORM_S8_UINT:
803 case VK_FORMAT_X8_D24_UNORM_PACK32:
804 format = VK_FORMAT_R32_UINT;
805 break;
806
807 case VK_FORMAT_D32_SFLOAT_S8_UINT:
808 format = VK_FORMAT_R32G32_UINT;
809 break;
810
811 default:
812 format = dst->vk_format;
813 break;
814 }
815
816 swizzle = pvr_get_format_swizzle(format);
817 memcpy(surface_params->swizzle, swizzle, sizeof(surface_params->swizzle));
818
819 pvr_arch_pbe_get_src_format_and_gamma(format,
820 PVR_PBE_GAMMA_NONE,
821 false,
822 &surface_params->source_format,
823 &surface_params->gamma);
824
825 surface_params->is_normalized = pvr_vk_format_is_fully_normalized(format);
826 surface_params->pbe_packmode = pvr_arch_get_pbe_packmode(format);
827 surface_params->nr_components = vk_format_get_nr_components(format);
828
829 result = pvr_mem_layout_spec(dst,
830 0U,
831 false,
832 &surface_params->width,
833 &surface_params->height,
834 &surface_params->stride,
835 &surface_params->mem_layout,
836 &surface_params->addr);
837 if (result != VK_SUCCESS)
838 return result;
839
840 surface_params->z_only_render = false;
841 surface_params->depth = dst->depth;
842 surface_params->down_scale = state->down_scale;
843
844 if (surface_params->mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)
845 render_params->slice = (uint32_t)MAX2(dst->z_position, 0.0f);
846 else
847 render_params->slice = 0U;
848
849 uint32_t tile_size_x = PVR_GET_FEATURE_VALUE(dev_info, tile_size_x, 0U);
850 uint32_t tile_size_y = PVR_GET_FEATURE_VALUE(dev_info, tile_size_y, 0U);
851
852 /* If the rectangle happens to be empty / off-screen we clip away
853 * everything.
854 */
855 if (state->empty_dst) {
856 render_params->min_x_clip = 2U * tile_size_x;
857 render_params->max_x_clip = 3U * tile_size_x;
858 render_params->min_y_clip = 2U * tile_size_y;
859 render_params->max_y_clip = 3U * tile_size_y;
860 state->origin_x_in_tiles = 0U;
861 state->origin_y_in_tiles = 0U;
862 state->height_in_tiles = 1U;
863 state->width_in_tiles = 1U;
864 } else {
865 const VkRect2D *scissor = &transfer_cmd->scissor;
866
867 /* Clamp */
868 render_params->min_x_clip =
869 MAX2(MIN2(scissor->offset.x, (int32_t)surface_params->width), 0U);
870 render_params->max_x_clip =
871 MAX2(MIN2(scissor->offset.x + scissor->extent.width,
872 (int32_t)surface_params->width),
873 0U) -
874 1U;
875
876 render_params->min_y_clip =
877 MAX2(MIN2(scissor->offset.y, surface_params->height), 0U);
878 render_params->max_y_clip =
879 MAX2(MIN2(scissor->offset.y + scissor->extent.height,
880 surface_params->height),
881 0U) -
882 1U;
883
884 if (state->custom_mapping.pass_count > 0U) {
885 struct pvr_transfer_pass *pass =
886 &state->custom_mapping.passes[state->pass_idx];
887
888 render_params->min_x_clip = (uint32_t)pass->clip_rects[0U].offset.x;
889 render_params->max_x_clip =
890 (uint32_t)(pass->clip_rects[0U].offset.x +
891 pass->clip_rects[0U].extent.width) -
892 1U;
893 render_params->min_y_clip = (uint32_t)pass->clip_rects[0U].offset.y;
894 render_params->max_y_clip =
895 (uint32_t)(pass->clip_rects[0U].offset.y +
896 pass->clip_rects[0U].extent.height) -
897 1U;
898 }
899
900 state->origin_x_in_tiles = render_params->min_x_clip / tile_size_x;
901 state->origin_y_in_tiles = render_params->min_y_clip / tile_size_y;
902 state->width_in_tiles =
903 (render_params->max_x_clip + tile_size_x) / tile_size_x;
904 state->height_in_tiles =
905 (render_params->max_y_clip + tile_size_y) / tile_size_y;
906
907 /* Be careful here as this isn't the same as ((max_x_clip -
908 * min_x_clip) + tile_size_x) >> tile_size_x.
909 */
910 state->width_in_tiles -= state->origin_x_in_tiles;
911 state->height_in_tiles -= state->origin_y_in_tiles;
912 }
913
914 render_params->source_start = PVR_PBE_STARTPOS_BIT0;
915 render_params->mrt_index = 0U;
916
917 return VK_SUCCESS;
918 }
919
920 static VkResult
921 pvr_pbe_setup_modify_defaults(const struct pvr_transfer_cmd_surface *dst,
922 struct pvr_transfer_3d_state *state,
923 uint32_t rt_idx,
924 struct pvr_pbe_surf_params *surf_params,
925 struct pvr_pbe_render_params *render_params)
926 {
927 struct pvr_transfer_pass *pass;
928 VkRect2D *clip_rect;
929
930 render_params->mrt_index = rt_idx;
931
932 assert(rt_idx > 0 && rt_idx <= PVR_TRANSFER_MAX_RENDER_TARGETS);
933
934 if (state->custom_mapping.pass_count == 0)
935 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
936
937 pass = &state->custom_mapping.passes[state->pass_idx];
938
939 assert(rt_idx < PVR_TRANSFER_MAX_CUSTOM_RECTS);
940
941 clip_rect = &pass->clip_rects[rt_idx];
942
943 render_params->min_x_clip = (uint32_t)clip_rect->offset.x;
944 render_params->max_x_clip =
945 (uint32_t)clip_rect->offset.x + clip_rect->extent.width - 1U;
946 render_params->min_y_clip = (uint32_t)clip_rect->offset.y;
947 render_params->max_y_clip =
948 (uint32_t)clip_rect->offset.y + clip_rect->extent.height - 1U;
949
950 return VK_SUCCESS;
951 }
952
953 static uint32_t
954 pvr_pbe_get_pixel_size(enum pvr_transfer_pbe_pixel_src pixel_format)
955 {
956 switch (pixel_format) {
957 case PVR_TRANSFER_PBE_PIXEL_SRC_CONV_D24_D32:
958 case PVR_TRANSFER_PBE_PIXEL_SRC_CONV_D32_D24S8:
959 case PVR_TRANSFER_PBE_PIXEL_SRC_CONV_S8D24_D24S8:
960 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D24S8_D24S8:
961 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32_D24S8:
962 case PVR_TRANSFER_PBE_PIXEL_SRC_F16_U8:
963 case PVR_TRANSFER_PBE_PIXEL_SRC_F32:
964 case PVR_TRANSFER_PBE_PIXEL_SRC_RAW32:
965 case PVR_TRANSFER_PBE_PIXEL_SRC_RBSWAP_SU1010102:
966 case PVR_TRANSFER_PBE_PIXEL_SRC_RBSWAP_UU1010102:
967 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D24S8:
968 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D24S8:
969 case PVR_TRANSFER_PBE_PIXEL_SRC_SS8888:
970 case PVR_TRANSFER_PBE_PIXEL_SRC_SU1010102:
971 case PVR_TRANSFER_PBE_PIXEL_SRC_SU8888:
972 case PVR_TRANSFER_PBE_PIXEL_SRC_SWAP_LMSB:
973 case PVR_TRANSFER_PBE_PIXEL_SRC_US8888:
974 case PVR_TRANSFER_PBE_PIXEL_SRC_UU1010102:
975 case PVR_TRANSFER_PBE_PIXEL_SRC_UU8888:
976 return 1U;
977
978 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32S8_D32S8:
979 case PVR_TRANSFER_PBE_PIXEL_SRC_F16F16:
980 case PVR_TRANSFER_PBE_PIXEL_SRC_F32X2:
981 case PVR_TRANSFER_PBE_PIXEL_SRC_MOV_BY45:
982 case PVR_TRANSFER_PBE_PIXEL_SRC_RAW64:
983 case PVR_TRANSFER_PBE_PIXEL_SRC_S16NORM:
984 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D32S8:
985 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D32S8_D32S8:
986 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D32S8:
987 case PVR_TRANSFER_PBE_PIXEL_SRC_SS16S16:
988 case PVR_TRANSFER_PBE_PIXEL_SRC_SU16U16:
989 case PVR_TRANSFER_PBE_PIXEL_SRC_SU32U32:
990 case PVR_TRANSFER_PBE_PIXEL_SRC_U16NORM:
991 case PVR_TRANSFER_PBE_PIXEL_SRC_US16S16:
992 case PVR_TRANSFER_PBE_PIXEL_SRC_US32S32:
993 case PVR_TRANSFER_PBE_PIXEL_SRC_UU16U16:
994 return 2U;
995
996 case PVR_TRANSFER_PBE_PIXEL_SRC_F32X4:
997 case PVR_TRANSFER_PBE_PIXEL_SRC_RAW128:
998 case PVR_TRANSFER_PBE_PIXEL_SRC_S4XU32:
999 case PVR_TRANSFER_PBE_PIXEL_SRC_U4XS32:
1000 return 4U;
1001
1002 case PVR_TRANSFER_PBE_PIXEL_SRC_NUM:
1003 default:
1004 break;
1005 }
1006
1007 return 0U;
1008 }
1009
1010 static void pvr_pbe_setup_swizzle(const struct pvr_transfer_cmd *transfer_cmd,
1011 struct pvr_transfer_3d_state *state,
1012 struct pvr_pbe_surf_params *surf_params)
1013 {
1014 bool color_fill = !!(transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_FILL);
1015 const struct pvr_transfer_cmd_surface *dst = &transfer_cmd->dst;
1016
1017 const uint32_t pixel_size =
1018 pvr_pbe_get_pixel_size(state->shader_props.layer_props.pbe_format);
1019
1020 state->usc_pixel_width = MAX2(pixel_size, 1U);
1021
1022 switch (dst->vk_format) {
1023 case VK_FORMAT_X8_D24_UNORM_PACK32:
1024 case VK_FORMAT_D24_UNORM_S8_UINT:
1025 case VK_FORMAT_S8_UINT:
1026 surf_params->swizzle[0U] = PIPE_SWIZZLE_X;
1027 surf_params->swizzle[1U] = PIPE_SWIZZLE_0;
1028 surf_params->swizzle[2U] = PIPE_SWIZZLE_0;
1029 surf_params->swizzle[3U] = PIPE_SWIZZLE_0;
1030 break;
1031
1032 case VK_FORMAT_D32_SFLOAT_S8_UINT:
1033 surf_params->swizzle[0U] = PIPE_SWIZZLE_X;
1034 surf_params->swizzle[1U] = PIPE_SWIZZLE_Y;
1035 surf_params->swizzle[2U] = PIPE_SWIZZLE_0;
1036 surf_params->swizzle[3U] = PIPE_SWIZZLE_0;
1037 break;
1038
1039 default: {
1040 const uint32_t red_width =
1041 vk_format_get_component_bits(dst->vk_format,
1042 UTIL_FORMAT_COLORSPACE_RGB,
1043 0U);
1044
1045 if (transfer_cmd->source_count > 0 &&
1046 vk_format_is_alpha(dst->vk_format)) {
1047 if (vk_format_has_alpha(transfer_cmd->sources[0].surface.vk_format)) {
1048 /* Modify the destination format swizzle to always source from
1049 * src0.
1050 */
1051 surf_params->swizzle[0U] = PIPE_SWIZZLE_X;
1052 surf_params->swizzle[1U] = PIPE_SWIZZLE_0;
1053 surf_params->swizzle[2U] = PIPE_SWIZZLE_0;
1054 surf_params->swizzle[3U] = PIPE_SWIZZLE_1;
1055 break;
1056 }
1057
1058 /* Source format having no alpha channel still allocates 4 output
1059 * buffer registers.
1060 */
1061 }
1062
1063 if (pvr_vk_format_is_fully_normalized(dst->vk_format)) {
1064 if (color_fill &&
1065 (dst->vk_format == VK_FORMAT_B8G8R8A8_UNORM ||
1066 dst->vk_format == VK_FORMAT_R8G8B8A8_UNORM ||
1067 dst->vk_format == VK_FORMAT_A8B8G8R8_UNORM_PACK32)) {
1068 surf_params->source_format =
1069 ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
1070 } else if (state->shader_props.layer_props.pbe_format ==
1071 PVR_TRANSFER_PBE_PIXEL_SRC_F16_U8) {
1072 surf_params->source_format =
1073 ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL;
1074 } else if (red_width <= 8U) {
1075 surf_params->source_format =
1076 ROGUE_PBESTATE_SOURCE_FORMAT_F16_PER_CHANNEL;
1077 }
1078 } else if (red_width == 32U && !state->dont_force_pbe) {
1079 uint32_t count = 0U;
1080
1081 for (uint32_t i = 0; i < transfer_cmd->source_count; i++) {
1082 VkFormat src_format = transfer_cmd->sources[i].surface.vk_format;
1083 uint32_t tmp;
1084
1085 tmp = pvr_vk_format_get_common_color_channel_count(src_format,
1086 dst->vk_format);
1087
1088 count = MAX2(count, tmp);
1089 }
1090
1091 switch (count) {
1092 case 1U:
1093 surf_params->swizzle[1U] = PIPE_SWIZZLE_0;
1094 FALLTHROUGH;
1095 case 2U:
1096 surf_params->swizzle[2U] = PIPE_SWIZZLE_0;
1097 FALLTHROUGH;
1098 case 3U:
1099 surf_params->swizzle[3U] = PIPE_SWIZZLE_1;
1100 break;
1101
1102 case 4U:
1103 default:
1104 break;
1105 }
1106 }
1107 break;
1108 }
1109 }
1110 }
1111
1112 /**
1113 * Calculates the required PBE byte mask based on the incoming transfer command.
1114 *
1115 * @param transfer_cmd the transfer command
1116 * @return the bytemask (active high disable mask)
1117 */
1118
1119 static uint64_t pvr_pbe_byte_mask(const struct pvr_device_info *dev_info,
1120 const struct pvr_transfer_cmd *transfer_cmd)
1121 {
1122 uint32_t flags = pvr_get_blit_flags(transfer_cmd);
1123
1124 assert(PVR_HAS_ENHANCEMENT(dev_info, 42064));
1125
1126 if (flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE) {
1127 uint32_t mask = 0U;
1128
1129 switch (transfer_cmd->dst.vk_format) {
1130 case VK_FORMAT_D32_SFLOAT_S8_UINT:
1131 mask = 0xF0F0F0F0U;
1132 break;
1133 case VK_FORMAT_D24_UNORM_S8_UINT:
1134 mask = 0x88888888U;
1135 break;
1136 default:
1137 break;
1138 }
1139
1140 if ((flags & PVR_TRANSFER_CMD_FLAGS_PICKD) == 0U)
1141 mask = ~mask;
1142
1143 return mask;
1144 }
1145
1146 /* The mask is as it was inactive on cores without the ERN. This keeps the
1147 * firmware agnostic to the feature.
1148 */
1149 return 0U;
1150 }
1151
1152 static VkResult pvr_pbe_setup_emit(const struct pvr_transfer_cmd *transfer_cmd,
1153 struct pvr_transfer_ctx *ctx,
1154 struct pvr_transfer_3d_state *state,
1155 uint32_t rt_count,
1156 uint32_t *pbe_setup_words)
1157 {
1158 struct pvr_device *const device = ctx->device;
1159 const struct pvr_device_info *const dev_info = &device->pdevice->dev_info;
1160
1161 struct pvr_winsys_transfer_regs *regs = &state->regs;
1162 struct pvr_pds_event_program program = {
1163 .emit_words = pbe_setup_words,
1164 .num_emit_word_pairs = rt_count,
1165 };
1166 struct pvr_pds_upload pds_upload;
1167 uint32_t staging_buffer_size;
1168 uint32_t *staging_buffer;
1169 pvr_dev_addr_t addr;
1170 VkResult result;
1171
1172 /* Precondition, make sure to use a valid index for ctx->usc_eot_bos. */
1173 assert(rt_count <= ARRAY_SIZE(ctx->usc_eot_bos));
1174 assert(rt_count > 0U);
1175
1176 addr.addr = ctx->usc_eot_bos[rt_count - 1U]->dev_addr.addr -
1177 device->heaps.usc_heap->base_addr.addr;
1178
1179 pvr_pds_setup_doutu(&program.task_control,
1180 addr.addr,
1181 ctx->usc_eot_usc_temps[rt_count - 1U],
1182 ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
1183 false);
1184
1185 pvr_pds_set_sizes_pixel_event(&program, dev_info);
1186
1187 staging_buffer_size = PVR_DW_TO_BYTES(program.code_size + program.data_size);
1188
1189 staging_buffer = vk_alloc(&device->vk.alloc,
1190 staging_buffer_size,
1191 8U,
1192 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1193 if (!staging_buffer)
1194 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1195
1196 pvr_pds_generate_pixel_event_data_segment(&program,
1197 staging_buffer,
1198 dev_info);
1199
1200 /* TODO: We can save some memory by generating a code segment for each
1201 * rt_count, which at the time of writing is a maximum of 3, in
1202 * pvr_setup_transfer_eot_shaders() when we setup the corresponding EOT
1203 * USC programs.
1204 */
1205 pvr_pds_generate_pixel_event_code_segment(&program,
1206 staging_buffer + program.data_size,
1207 dev_info);
1208
1209 result = pvr_arch_cmd_buffer_upload_pds(
1210 transfer_cmd->cmd_buffer,
1211 staging_buffer,
1212 program.data_size,
1213 ROGUE_CR_EVENT_PIXEL_PDS_DATA_ADDR_ALIGNMENT,
1214 staging_buffer + program.data_size,
1215 program.code_size,
1216 ROGUE_CR_EVENT_PIXEL_PDS_CODE_ADDR_ALIGNMENT,
1217 ROGUE_CR_EVENT_PIXEL_PDS_DATA_ADDR_ALIGNMENT,
1218 &pds_upload);
1219 vk_free(&device->vk.alloc, staging_buffer);
1220 if (result != VK_SUCCESS)
1221 return result;
1222
1223 pvr_csb_pack (&regs->event_pixel_pds_info, CR_EVENT_PIXEL_PDS_INFO, reg) {
1224 reg.temp_stride = 0U;
1225 reg.const_size =
1226 DIV_ROUND_UP(program.data_size,
1227 ROGUE_CR_EVENT_PIXEL_PDS_INFO_CONST_SIZE_UNIT_SIZE);
1228 reg.usc_sr_size =
1229 DIV_ROUND_UP(rt_count * PVR_STATE_PBE_DWORDS,
1230 ROGUE_CR_EVENT_PIXEL_PDS_INFO_USC_SR_SIZE_UNIT_SIZE);
1231 }
1232
1233 pvr_csb_pack (&regs->event_pixel_pds_data, CR_EVENT_PIXEL_PDS_DATA, reg) {
1234 reg.addr = PVR_DEV_ADDR(pds_upload.data_offset);
1235 }
1236
1237 pvr_csb_pack (&regs->event_pixel_pds_code, CR_EVENT_PIXEL_PDS_CODE, reg) {
1238 reg.addr = PVR_DEV_ADDR(pds_upload.code_offset);
1239 }
1240
1241 return VK_SUCCESS;
1242 }
1243
1244 static VkResult pvr_pbe_setup(const struct pvr_transfer_cmd *transfer_cmd,
1245 struct pvr_transfer_ctx *ctx,
1246 struct pvr_transfer_3d_state *state)
1247 {
1248 struct pvr_device *const device = ctx->device;
1249 const struct pvr_device_info *const dev_info = &device->pdevice->dev_info;
1250
1251 const struct pvr_transfer_cmd_surface *dst = &transfer_cmd->dst;
1252 uint32_t num_rts = vk_format_get_plane_count(dst->vk_format);
1253 uint32_t pbe_setup_words[PVR_TRANSFER_MAX_RENDER_TARGETS *
1254 ROGUE_NUM_PBESTATE_STATE_WORDS];
1255 struct pvr_pbe_render_params render_params;
1256 struct pvr_pbe_surf_params surf_params;
1257 VkResult result;
1258
1259 if (state->custom_mapping.pass_count > 0U)
1260 num_rts = state->custom_mapping.passes[state->pass_idx].clip_rects_count;
1261
1262 if (PVR_HAS_FEATURE(dev_info, paired_tiles))
1263 state->pair_tiles = PVR_PAIRED_TILES_NONE;
1264
1265 for (uint32_t i = 0U; i < num_rts; i++) {
1266 uint64_t *pbe_regs;
1267 uint32_t *pbe_words;
1268
1269 /* Ensure the access into the pbe_wordx_mrty is made within its bounds. */
1270 assert(i * ROGUE_NUM_PBESTATE_REG_WORDS_FOR_TRANSFER <
1271 ARRAY_SIZE(state->regs.pbe_wordx_mrty));
1272 /* Ensure the access into pbe_setup_words is made within its bounds. */
1273 assert(i * ROGUE_NUM_PBESTATE_STATE_WORDS < ARRAY_SIZE(pbe_setup_words));
1274
1275 pbe_regs =
1276 &state->regs
1277 .pbe_wordx_mrty[i * ROGUE_NUM_PBESTATE_REG_WORDS_FOR_TRANSFER];
1278 pbe_words = &pbe_setup_words[i * ROGUE_NUM_PBESTATE_STATE_WORDS];
1279
1280 if (PVR_HAS_ENHANCEMENT(dev_info, 42064))
1281 pbe_regs[2U] = 0UL;
1282
1283 if (i == 0U) {
1284 result = pvr_pbe_setup_codegen_defaults(dev_info,
1285 transfer_cmd,
1286 state,
1287 &surf_params,
1288 &render_params);
1289 if (result != VK_SUCCESS)
1290 return result;
1291 } else {
1292 result = pvr_pbe_setup_modify_defaults(dst,
1293 state,
1294 i,
1295 &surf_params,
1296 &render_params);
1297 if (result != VK_SUCCESS)
1298 return result;
1299 }
1300
1301 pvr_pbe_setup_swizzle(transfer_cmd, state, &surf_params);
1302
1303 pvr_arch_pbe_pack_state(dev_info,
1304 &surf_params,
1305 &render_params,
1306 pbe_words,
1307 pbe_regs);
1308
1309 if (PVR_HAS_ENHANCEMENT(dev_info, 42064)) {
1310 uint64_t temp_reg;
1311
1312 pvr_csb_pack (&temp_reg, PBESTATE_REG_WORD2, reg) {
1313 reg.sw_bytemask = pvr_pbe_byte_mask(dev_info, transfer_cmd);
1314 }
1315
1316 pbe_regs[2U] |= temp_reg;
1317 }
1318
1319 if (PVR_HAS_FEATURE(dev_info, paired_tiles)) {
1320 if (pbe_regs[2U] &
1321 (1ULL << ROGUE_PBESTATE_REG_WORD2_PAIR_TILES_SHIFT)) {
1322 if (transfer_cmd->dst.mem_layout == PVR_MEMLAYOUT_TWIDDLED)
1323 state->pair_tiles = PVR_PAIRED_TILES_Y;
1324 else
1325 state->pair_tiles = PVR_PAIRED_TILES_X;
1326 }
1327 }
1328 }
1329
1330 result =
1331 pvr_pbe_setup_emit(transfer_cmd, ctx, state, num_rts, pbe_setup_words);
1332 if (result != VK_SUCCESS)
1333 return result;
1334
1335 /* Adjust tile origin and width to include all emits. */
1336 if (state->custom_mapping.pass_count > 0U) {
1337 const uint32_t tile_size_x =
1338 PVR_GET_FEATURE_VALUE(dev_info, tile_size_x, 0U);
1339 const uint32_t tile_size_y =
1340 PVR_GET_FEATURE_VALUE(dev_info, tile_size_y, 0U);
1341 struct pvr_transfer_pass *pass =
1342 &state->custom_mapping.passes[state->pass_idx];
1343 VkOffset2D offset = { 0U, 0U };
1344 VkOffset2D end = { 0U, 0U };
1345
1346 for (uint32_t i = 0U; i < pass->clip_rects_count; i++) {
1347 VkRect2D *rect = &pass->clip_rects[i];
1348
1349 offset.x = MIN2(offset.x, rect->offset.x);
1350 offset.y = MIN2(offset.y, rect->offset.y);
1351 end.x = MAX2(end.x, rect->offset.x + rect->extent.width);
1352 end.y = MAX2(end.y, rect->offset.y + rect->extent.height);
1353 }
1354
1355 state->origin_x_in_tiles = (uint32_t)offset.x / tile_size_x;
1356 state->origin_y_in_tiles = (uint32_t)offset.y / tile_size_y;
1357 state->width_in_tiles =
1358 DIV_ROUND_UP((uint32_t)end.x, tile_size_x) - state->origin_x_in_tiles;
1359 state->height_in_tiles =
1360 DIV_ROUND_UP((uint32_t)end.y, tile_size_y) - state->origin_y_in_tiles;
1361 }
1362
1363 return VK_SUCCESS;
1364 }
1365
1366 /**
1367 * Writes the ISP tile registers according to the MSAA state. Sets up the USC
1368 * pixel partition allocations and the number of tiles in flight.
1369 */
1370 static VkResult pvr_isp_tiles(const struct pvr_device *device,
1371 struct pvr_transfer_3d_state *state)
1372 {
1373 const struct pvr_device_runtime_info *dev_runtime_info =
1374 &device->pdevice->dev_runtime_info;
1375 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
1376 const uint32_t isp_samples =
1377 PVR_GET_FEATURE_VALUE(dev_info, isp_samples_per_pixel, 1U);
1378 uint32_t origin_x = state->origin_x_in_tiles;
1379 uint32_t origin_y = state->origin_y_in_tiles;
1380 uint32_t width = state->width_in_tiles;
1381 uint32_t height = state->height_in_tiles;
1382 uint32_t isp_tiles_in_flight;
1383
1384 /* msaa_multiplier is calculated by sample_count & ~1U. Given sample
1385 * count is always in powers of two, we can get the sample count from
1386 * msaa_multiplier using the following logic.
1387 */
1388 const uint32_t samples = MAX2(state->msaa_multiplier, 1U);
1389
1390 /* isp_samples_per_pixel feature is also know as "2x/4x for free", when
1391 * this is present SAMPLES_PER_PIXEL is 2/4, otherwise 1. The following
1392 * logic should end up with these numbers:
1393 *
1394 * |---------------------------------|
1395 * | 4 SAMPLES / ISP PIXEL |
1396 * |-----------------------+----+----|
1397 * | MSAA | X* | Y* |
1398 * | 2X | 1 | 1 |
1399 * | 4X | 1 | 1 |
1400 * |---------------------------------|
1401 * | 2 SAMPLES / ISP PIXEL |
1402 * |-----------------------+----+----|
1403 * | MSAA | X* | Y* |
1404 * | 2X | 1 | 1 |
1405 * | 4X | 1 | 2 |
1406 * | 8X | 2 | 2 |
1407 * |-----------------------+----+----|
1408 * | 1 SAMPLE / ISP PIXEL |
1409 * |-----------------------+----+----|
1410 * | MSAA | X* | Y* |
1411 * | 2X | 1 | 2 |
1412 * | 4X | 2 | 2 |
1413 * |-----------------------+----+----|
1414 */
1415
1416 origin_x <<= (state->msaa_multiplier >> (isp_samples + 1U)) & 1U;
1417 origin_y <<= ((state->msaa_multiplier >> (isp_samples + 1U)) |
1418 (state->msaa_multiplier >> isp_samples)) &
1419 1U;
1420 width <<= (state->msaa_multiplier >> (isp_samples + 1U)) & 1U;
1421 height <<= ((state->msaa_multiplier >> (isp_samples + 1U)) |
1422 (state->msaa_multiplier >> isp_samples)) &
1423 1U;
1424
1425 if (PVR_HAS_FEATURE(dev_info, paired_tiles) &&
1426 state->pair_tiles != PVR_PAIRED_TILES_NONE) {
1427 width = ALIGN_POT(width, 2U);
1428 height = ALIGN_POT(height, 2U);
1429 }
1430
1431 pvr_csb_pack (&state->regs.isp_mtile_size, CR_ISP_MTILE_SIZE, reg) {
1432 reg.x = width;
1433 reg.y = height;
1434 }
1435
1436 pvr_csb_pack (&state->regs.isp_render_origin, CR_ISP_RENDER_ORIGIN, reg) {
1437 reg.x = origin_x;
1438 reg.y = origin_y;
1439 }
1440
1441 pvr_arch_setup_tiles_in_flight(dev_info,
1442 dev_runtime_info,
1443 pvr_cr_isp_aa_mode_type(samples),
1444 state->usc_pixel_width,
1445 state->pair_tiles != PVR_PAIRED_TILES_NONE,
1446 0,
1447 &isp_tiles_in_flight,
1448 &state->regs.usc_pixel_output_ctrl);
1449
1450 pvr_csb_pack (&state->regs.isp_ctl, CR_ISP_CTL, reg) {
1451 reg.process_empty_tiles = true;
1452
1453 if (PVR_HAS_FEATURE(dev_info, paired_tiles)) {
1454 if (state->pair_tiles == PVR_PAIRED_TILES_X) {
1455 reg.pair_tiles = true;
1456 } else if (state->pair_tiles == PVR_PAIRED_TILES_Y) {
1457 reg.pair_tiles = true;
1458 reg.pair_tiles_vert = true;
1459 }
1460 }
1461 }
1462
1463 state->regs.isp_ctl |= isp_tiles_in_flight;
1464
1465 return VK_SUCCESS;
1466 }
1467
1468 static bool
1469 pvr_int_pbe_pixel_changes_dst_rate(const struct pvr_device_info *dev_info,
1470 enum pvr_transfer_pbe_pixel_src pbe_format)
1471 {
1472 /* We don't emulate rate change from the USC with the pbe_yuv feature. */
1473 if (!PVR_HAS_FEATURE(dev_info, pbe_yuv) &&
1474 (pbe_format == PVR_TRANSFER_PBE_PIXEL_SRC_Y_UV_INTERLEAVED ||
1475 pbe_format == PVR_TRANSFER_PBE_PIXEL_SRC_Y_U_V)) {
1476 return true;
1477 }
1478
1479 return false;
1480 }
1481
1482 /**
1483 * Number of DWORDs from the unified store that floating texture coefficients
1484 * take up.
1485 */
1486 static void pvr_uv_space(const struct pvr_device_info *dev_info,
1487 const struct pvr_transfer_cmd *transfer_cmd,
1488 struct pvr_transfer_3d_state *state)
1489 {
1490 const struct pvr_transfer_cmd_surface *dst = &transfer_cmd->dst;
1491 const VkRect2D *dst_rect = &transfer_cmd->scissor;
1492
1493 /* This also avoids division by 0 in pvr_dma_texture_floats(). */
1494 if (state->custom_mapping.pass_count == 0U &&
1495 (dst_rect->extent.width == 0U || dst_rect->extent.height == 0U ||
1496 MAX2(dst_rect->offset.x, dst_rect->offset.x + dst_rect->extent.width) <
1497 0U ||
1498 MIN2(dst_rect->offset.x, dst_rect->offset.x + dst_rect->extent.width) >
1499 (int32_t)dst->width ||
1500 MAX2(dst_rect->offset.y, dst_rect->offset.y + dst_rect->extent.height) <
1501 0U ||
1502 MIN2(dst_rect->offset.y, dst_rect->offset.y + dst_rect->extent.height) >
1503 (int32_t)dst->height)) {
1504 state->empty_dst = true;
1505 } else {
1506 state->empty_dst = false;
1507
1508 if (transfer_cmd->source_count > 0) {
1509 struct pvr_tq_layer_properties *layer =
1510 &state->shader_props.layer_props;
1511
1512 const VkRect2D *src_rect =
1513 &transfer_cmd->sources[0U].mappings[0U].src_rect;
1514 const VkRect2D *dst_rect =
1515 &transfer_cmd->sources[0U].mappings[0U].dst_rect;
1516 int32_t dst_x1 = dst_rect->offset.x + dst_rect->extent.width;
1517 int32_t dst_y1 = dst_rect->offset.y + dst_rect->extent.height;
1518 int32_t src_x1 = src_rect->offset.x + src_rect->extent.width;
1519 int32_t src_y1 = src_rect->offset.y + src_rect->extent.height;
1520
1521 assert(transfer_cmd->source_count == 1);
1522
1523 if (state->filter[0U] > PVR_FILTER_POINT) {
1524 layer->layer_floats = PVR_INT_COORD_SET_FLOATS_4;
1525 } else if (src_rect->extent.width == 0U ||
1526 src_rect->extent.height == 0U) {
1527 layer->layer_floats = PVR_INT_COORD_SET_FLOATS_0;
1528 } else if ((src_rect->offset.x * dst_x1 !=
1529 src_x1 * dst_rect->offset.x) ||
1530 (src_rect->offset.y * dst_y1 !=
1531 src_y1 * dst_rect->offset.y) ||
1532 (src_rect->extent.width != dst_rect->extent.width) ||
1533 (src_rect->extent.height != dst_rect->extent.height) ||
1534 transfer_cmd->sources[0U].mappings[0U].flip_x ||
1535 transfer_cmd->sources[0U].mappings[0U].flip_y) {
1536 layer->layer_floats = PVR_INT_COORD_SET_FLOATS_4;
1537 } else {
1538 layer->layer_floats = PVR_INT_COORD_SET_FLOATS_0;
1539 }
1540
1541 /* We have to adjust the rate. */
1542 if (layer->layer_floats != PVR_INT_COORD_SET_FLOATS_0 &&
1543 pvr_int_pbe_pixel_changes_dst_rate(dev_info, layer->pbe_format)) {
1544 layer->layer_floats = PVR_INT_COORD_SET_FLOATS_6;
1545 }
1546 }
1547 }
1548 }
1549
1550 static uint32_t pvr_int_pbe_pixel_num_sampler_and_image_states(
1551 enum pvr_transfer_pbe_pixel_src pbe_format)
1552 {
1553 switch (pbe_format) {
1554 case PVR_TRANSFER_PBE_PIXEL_SRC_Y_UV_INTERLEAVED:
1555 case PVR_TRANSFER_PBE_PIXEL_SRC_Y_U_V:
1556 return 1U;
1557 default:
1558 return pvr_pbe_pixel_num_loads(pbe_format);
1559 }
1560 }
1561
1562 static VkResult pvr_sampler_state_for_surface(
1563 const struct pvr_device_info *dev_info,
1564 const struct pvr_transfer_cmd_surface *surface,
1565 enum pvr_filter filter,
1566 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
1567 uint32_t sampler,
1568 bool nn_coords,
1569 uint32_t *mem_ptr)
1570 {
1571 uint64_t sampler_state[2U] = { 0UL, 0UL };
1572
1573 pvr_csb_pack (&sampler_state[0U], TEXSTATE_SAMPLER_WORD0, reg) {
1574 reg.anisoctl = ROGUE_TEXSTATE_ANISOCTL_DISABLED;
1575 reg.minlod = ROGUE_TEXSTATE_CLAMP_MIN;
1576 reg.maxlod = ROGUE_TEXSTATE_CLAMP_MIN;
1577 reg.dadjust = ROGUE_TEXSTATE_DADJUST_MIN_UINT;
1578
1579 if (filter == PVR_FILTER_DONTCARE || filter == PVR_FILTER_POINT) {
1580 reg.minfilter = ROGUE_TEXSTATE_FILTER_POINT;
1581 reg.magfilter = ROGUE_TEXSTATE_FILTER_POINT;
1582 } else if (filter == PVR_FILTER_LINEAR) {
1583 reg.minfilter = ROGUE_TEXSTATE_FILTER_LINEAR;
1584 reg.magfilter = ROGUE_TEXSTATE_FILTER_LINEAR;
1585 } else {
1586 assert(PVR_HAS_FEATURE(dev_info, tf_bicubic_filter));
1587 reg.minfilter = ROGUE_TEXSTATE_FILTER_BICUBIC;
1588 reg.magfilter = ROGUE_TEXSTATE_FILTER_BICUBIC;
1589 }
1590
1591 reg.non_normalized_coords = nn_coords;
1592
1593 reg.addrmode_u = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
1594 reg.addrmode_v = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
1595
1596 if (surface->mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)
1597 reg.addrmode_w = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
1598 }
1599
1600 assert(sampler < PVR_TRANSFER_MAX_IMAGES);
1601
1602 assert(sampler <= sh_reg_layout->combined_image_samplers.count);
1603 mem_ptr += sh_reg_layout->combined_image_samplers.offsets[sampler].sampler;
1604
1605 memcpy(mem_ptr, sampler_state, sizeof(sampler_state));
1606
1607 return VK_SUCCESS;
1608 }
1609
1610 static inline VkResult pvr_image_state_set_codegen_defaults(
1611 struct pvr_device *device,
1612 struct pvr_transfer_3d_state *state,
1613 const struct pvr_transfer_cmd_surface *surface,
1614 VkFormat dst_vk_format,
1615 uint32_t load,
1616 uint64_t *mem_ptr)
1617 {
1618 struct pvr_tq_layer_properties *layer = &state->shader_props.layer_props;
1619 struct pvr_texture_state_info info = { 0U };
1620 struct pvr_image_descriptor image_state;
1621 VkResult result;
1622
1623 switch (surface->vk_format) {
1624 /* ERN 46863 */
1625 case VK_FORMAT_D32_SFLOAT_S8_UINT:
1626 switch (layer->pbe_format) {
1627 case PVR_TRANSFER_PBE_PIXEL_SRC_RAW32:
1628 case PVR_TRANSFER_PBE_PIXEL_SRC_RAW64:
1629 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D24S8:
1630 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D32S8_D32S8:
1631 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32S8_D32S8:
1632 case PVR_TRANSFER_PBE_PIXEL_SRC_CONV_D32_D24S8:
1633 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32_D24S8:
1634 case PVR_TRANSFER_PBE_PIXEL_SRC_D32S8:
1635 info.format = VK_FORMAT_R32G32_UINT;
1636 break;
1637 default:
1638 info.format = surface->vk_format;
1639 break;
1640 }
1641 break;
1642
1643 case VK_FORMAT_D24_UNORM_S8_UINT:
1644 case VK_FORMAT_X8_D24_UNORM_PACK32:
1645 info.format = VK_FORMAT_R32_UINT;
1646 break;
1647
1648 default:
1649 info.format = surface->vk_format;
1650 break;
1651 }
1652
1653 info.flags = 0U;
1654 info.base_level = 0U;
1655 info.mip_levels = 1U;
1656 info.mipmaps_present = false;
1657 info.sample_count = MAX2(surface->sample_count, 1U);
1658
1659 if (surface->mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)
1660 info.extent.depth = surface->depth;
1661 else
1662 info.extent.depth = 0U;
1663
1664 if (PVR_HAS_FEATURE(&device->pdevice->dev_info, tpu_array_textures))
1665 info.array_size = 0U;
1666
1667 result = pvr_mem_layout_spec(surface,
1668 load,
1669 true,
1670 &info.extent.width,
1671 &info.extent.height,
1672 &info.stride,
1673 &info.mem_layout,
1674 &info.addr);
1675 if (result != VK_SUCCESS)
1676 return result;
1677
1678 if (state->custom_mapping.texel_extend_dst > 1U) {
1679 info.extent.width /= state->custom_mapping.texel_extend_dst;
1680 info.stride /= state->custom_mapping.texel_extend_dst;
1681 }
1682
1683 info.tex_state_type = PVR_TEXTURE_STATE_SAMPLE;
1684
1685 if (surface->vk_format == VK_FORMAT_S8_UINT &&
1686 (dst_vk_format == VK_FORMAT_D24_UNORM_S8_UINT ||
1687 dst_vk_format == VK_FORMAT_S8_UINT)) {
1688 info.swizzle[0U] = PIPE_SWIZZLE_X;
1689 info.swizzle[1U] = PIPE_SWIZZLE_0;
1690 info.swizzle[2U] = PIPE_SWIZZLE_0;
1691 info.swizzle[3U] = PIPE_SWIZZLE_0;
1692 } else if (surface->vk_format == VK_FORMAT_S8_UINT &&
1693 dst_vk_format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
1694 info.swizzle[0U] = PIPE_SWIZZLE_X;
1695 info.swizzle[1U] = PIPE_SWIZZLE_0;
1696 info.swizzle[2U] = PIPE_SWIZZLE_0;
1697 info.swizzle[3U] = PIPE_SWIZZLE_1;
1698 } else if (surface->vk_format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
1699 switch (dst_vk_format) {
1700 case VK_FORMAT_S8_UINT:
1701 info.swizzle[0U] = PIPE_SWIZZLE_Y;
1702 info.swizzle[1U] = PIPE_SWIZZLE_0;
1703 info.swizzle[2U] = PIPE_SWIZZLE_0;
1704 info.swizzle[3U] = PIPE_SWIZZLE_0;
1705 break;
1706
1707 case VK_FORMAT_D24_UNORM_S8_UINT:
1708 info.swizzle[0U] = PIPE_SWIZZLE_Y;
1709 info.swizzle[1U] = PIPE_SWIZZLE_X;
1710 info.swizzle[2U] = PIPE_SWIZZLE_0;
1711 info.swizzle[3U] = PIPE_SWIZZLE_0;
1712 break;
1713
1714 case VK_FORMAT_D32_SFLOAT_S8_UINT:
1715 info.swizzle[0U] = PIPE_SWIZZLE_X;
1716 info.swizzle[1U] = PIPE_SWIZZLE_Y;
1717 info.swizzle[2U] = PIPE_SWIZZLE_0;
1718 info.swizzle[3U] = PIPE_SWIZZLE_0;
1719 break;
1720
1721 default:
1722 memcpy(info.swizzle,
1723 pvr_get_format_swizzle(info.format),
1724 sizeof(info.swizzle));
1725 break;
1726 }
1727 } else {
1728 memcpy(info.swizzle,
1729 pvr_get_format_swizzle(info.format),
1730 sizeof(info.swizzle));
1731 }
1732
1733 if (info.extent.depth > 0U)
1734 info.type = VK_IMAGE_VIEW_TYPE_3D;
1735 else if (info.extent.height > 1U)
1736 info.type = VK_IMAGE_VIEW_TYPE_2D;
1737 else
1738 info.type = VK_IMAGE_VIEW_TYPE_1D;
1739
1740 result = pvr_arch_pack_tex_state(device, &info, &image_state);
1741 if (result != VK_SUCCESS)
1742 return result;
1743
1744 memcpy(mem_ptr, &image_state, sizeof(image_state) - (4 * sizeof(uint32_t)));
1745
1746 return VK_SUCCESS;
1747 }
1748
1749 static VkResult pvr_image_state_for_surface(
1750 const struct pvr_transfer_ctx *ctx,
1751 const struct pvr_transfer_cmd *transfer_cmd,
1752 const struct pvr_transfer_cmd_surface *surface,
1753 uint32_t load,
1754 uint32_t source,
1755 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
1756 struct pvr_transfer_3d_state *state,
1757 uint32_t uf_image,
1758 uint32_t *mem_ptr)
1759 {
1760 uint32_t tex_state[ROGUE_MAXIMUM_IMAGE_STATE_SIZE] = { 0U };
1761 VkResult result;
1762 uint8_t offset;
1763
1764 result = pvr_image_state_set_codegen_defaults(ctx->device,
1765 state,
1766 surface,
1767 transfer_cmd->dst.vk_format,
1768 load,
1769 (uint64_t *)tex_state);
1770 if (result != VK_SUCCESS)
1771 return result;
1772
1773 assert(uf_image < PVR_TRANSFER_MAX_IMAGES);
1774
1775 /* Offset of the shared registers containing the hardware image state. */
1776 assert(uf_image < sh_reg_layout->combined_image_samplers.count);
1777 offset = sh_reg_layout->combined_image_samplers.offsets[uf_image].image;
1778
1779 /* Copy the image state to the buffer which is loaded into the shared
1780 * registers.
1781 */
1782 memcpy(mem_ptr + offset, tex_state, sizeof(tex_state));
1783
1784 return VK_SUCCESS;
1785 }
1786
1787 /* Writes the texture state/sampler state into DMAed memory. */
1788 static VkResult
1789 pvr_sampler_image_state(struct pvr_transfer_ctx *ctx,
1790 const struct pvr_transfer_cmd *transfer_cmd,
1791 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
1792 struct pvr_transfer_3d_state *state,
1793 uint32_t *mem_ptr)
1794 {
1795 if (!state->empty_dst) {
1796 uint32_t uf_sampler = 0U;
1797 uint32_t uf_image = 0U;
1798
1799 for (uint32_t source = 0; source < transfer_cmd->source_count; source++) {
1800 struct pvr_tq_layer_properties *layer =
1801 &state->shader_props.layer_props;
1802 uint32_t max_load = pvr_pbe_pixel_num_loads(layer->pbe_format);
1803
1804 for (uint32_t load = 0U; load < max_load; load++) {
1805 const struct pvr_transfer_cmd_surface *surface;
1806 enum pvr_filter filter;
1807 VkResult result;
1808
1809 switch (layer->pbe_format) {
1810 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D32S8:
1811 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D32S8:
1812 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D32S8_D32S8:
1813 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32S8_D32S8:
1814 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_S8_D24S8:
1815 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D24S8:
1816 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D24S8_D24S8:
1817 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D32_D24S8:
1818 case PVR_TRANSFER_PBE_PIXEL_SRC_F16F16:
1819 case PVR_TRANSFER_PBE_PIXEL_SRC_F16_U8:
1820 if (load > 0U) {
1821 surface = &transfer_cmd->dst;
1822 filter = transfer_cmd->sources[source].filter;
1823 } else {
1824 surface = &transfer_cmd->sources[source].surface;
1825 filter = state->filter[source];
1826 }
1827 break;
1828
1829 case PVR_TRANSFER_PBE_PIXEL_SRC_Y_UV_INTERLEAVED:
1830 case PVR_TRANSFER_PBE_PIXEL_SRC_Y_U_V:
1831 surface = &transfer_cmd->sources[source].surface;
1832 filter = state->filter[source];
1833 break;
1834
1835 default:
1836 surface = &transfer_cmd->sources[source + load].surface;
1837 filter = state->filter[source + load];
1838 break;
1839 }
1840
1841 if (load < pvr_int_pbe_pixel_num_sampler_and_image_states(
1842 layer->pbe_format)) {
1843 const struct pvr_device_info *dev_info =
1844 &transfer_cmd->cmd_buffer->device->pdevice->dev_info;
1845 const bool nn_coords = state->shader_props.layer_props.linear ||
1846 !state->shader_props.iterated;
1847
1848 result = pvr_sampler_state_for_surface(dev_info,
1849 surface,
1850 filter,
1851 sh_reg_layout,
1852 uf_sampler,
1853 nn_coords,
1854 mem_ptr);
1855 if (result != VK_SUCCESS)
1856 return result;
1857
1858 uf_sampler++;
1859
1860 result = pvr_image_state_for_surface(ctx,
1861 transfer_cmd,
1862 surface,
1863 load,
1864 source,
1865 sh_reg_layout,
1866 state,
1867 uf_image,
1868 mem_ptr);
1869 if (result != VK_SUCCESS)
1870 return result;
1871
1872 uf_image++;
1873 }
1874 }
1875 }
1876 }
1877
1878 return VK_SUCCESS;
1879 }
1880
1881 /* The returned offset is in dwords. */
1882 static inline uint32_t pvr_dynamic_const_reg_advance(
1883 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
1884 struct pvr_transfer_3d_state *state)
1885 {
1886 const uint32_t offset = sh_reg_layout->dynamic_consts.offset;
1887
1888 assert(state->dynamic_const_reg_ptr < sh_reg_layout->dynamic_consts.count);
1889
1890 return offset + state->dynamic_const_reg_ptr++;
1891 }
1892
1893 /** Scales coefficients for sampling. (non normalized). */
1894 static inline void
1895 pvr_dma_texture_floats(const struct pvr_transfer_cmd *transfer_cmd,
1896 struct pvr_transfer_3d_state *state,
1897 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
1898 uint32_t *mem_ptr)
1899
1900 {
1901 if (transfer_cmd->source_count > 0) {
1902 struct pvr_tq_layer_properties *layer = &state->shader_props.layer_props;
1903 const struct pvr_rect_mapping *mapping =
1904 &transfer_cmd->sources[0].mappings[0U];
1905 VkRect2D src_rect = mapping->src_rect;
1906 VkRect2D dst_rect = mapping->dst_rect;
1907
1908 switch (layer->layer_floats) {
1909 case PVR_INT_COORD_SET_FLOATS_0:
1910 break;
1911
1912 case PVR_INT_COORD_SET_FLOATS_6:
1913 case PVR_INT_COORD_SET_FLOATS_4: {
1914 int32_t consts[2U] = { 0U, 0U };
1915 int32_t denom[2U] = { 0U, 0U };
1916 int32_t nums[2U] = { 0U, 0U };
1917 int32_t src_x, dst_x;
1918 int32_t src_y, dst_y;
1919 float offset = 0.0f;
1920 float tmp;
1921
1922 dst_x = mapping->flip_x ? -(int32_t)dst_rect.extent.width
1923 : dst_rect.extent.width;
1924 dst_y = mapping->flip_y ? -(int32_t)dst_rect.extent.height
1925 : dst_rect.extent.height;
1926 src_x = src_rect.extent.width;
1927 src_y = src_rect.extent.height;
1928
1929 nums[0U] = src_x;
1930 denom[0U] = dst_x;
1931 consts[0U] =
1932 mapping->flip_x
1933 ? src_rect.offset.x * dst_x -
1934 src_x * (dst_rect.offset.x + dst_rect.extent.width)
1935 : src_rect.offset.x * dst_x - src_x * dst_rect.offset.x;
1936 nums[1U] = src_y;
1937 denom[1U] = dst_y;
1938 consts[1U] =
1939 mapping->flip_y
1940 ? src_rect.offset.y * dst_y -
1941 src_y * (dst_rect.offset.y + dst_rect.extent.height)
1942 : src_rect.offset.y * dst_y - src_y * dst_rect.offset.y;
1943
1944 for (uint32_t i = 0U; i < 2U; i++) {
1945 tmp = (float)(nums[i]) / (float)(denom[i]);
1946 mem_ptr[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
1947 fui(tmp);
1948
1949 tmp = ((float)(consts[i]) + (i == 1U ? offset : 0.0f)) /
1950 (float)(denom[i]);
1951 mem_ptr[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
1952 fui(tmp);
1953 }
1954
1955 if (layer->layer_floats == PVR_INT_COORD_SET_FLOATS_6) {
1956 tmp = (float)MIN2(dst_rect.offset.x, dst_rect.offset.x + dst_x);
1957 mem_ptr[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
1958 fui(tmp);
1959
1960 tmp = (float)MIN2(dst_rect.offset.y, dst_rect.offset.y + dst_y);
1961 mem_ptr[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
1962 fui(tmp);
1963 }
1964 break;
1965 }
1966
1967 default:
1968 UNREACHABLE("Unknown COORD_SET_FLOATS.");
1969 break;
1970 }
1971 }
1972 }
1973
1974 static bool pvr_int_pbe_pixel_requires_usc_filter(
1975 const struct pvr_device_info *dev_info,
1976 enum pvr_transfer_pbe_pixel_src pixel_format)
1977 {
1978 switch (pixel_format) {
1979 case PVR_TRANSFER_PBE_PIXEL_SRC_SMRG_D24S8_D24S8:
1980 case PVR_TRANSFER_PBE_PIXEL_SRC_DMRG_D24S8_D24S8:
1981 case PVR_TRANSFER_PBE_PIXEL_SRC_U16NORM:
1982 case PVR_TRANSFER_PBE_PIXEL_SRC_S16NORM:
1983 case PVR_TRANSFER_PBE_PIXEL_SRC_F32:
1984 case PVR_TRANSFER_PBE_PIXEL_SRC_F32X2:
1985 case PVR_TRANSFER_PBE_PIXEL_SRC_F32X4:
1986 return true;
1987 case PVR_TRANSFER_PBE_PIXEL_SRC_F16F16:
1988 return !PVR_HAS_FEATURE(dev_info, pbe_filterable_f16);
1989 default:
1990 return false;
1991 }
1992 }
1993
1994 /**
1995 * Sets up the MSAA related bits in the operation
1996 *
1997 * TPU sample count is read directly from transfer_cmd in the TPU code. An MSAA
1998 * src can be read from sample rate or instance rate shaders as long as the
1999 * sample count is set on the TPU. If a layer is single sample we expect the
2000 * same sample replicated in full rate shaders. If the layer is multi sample,
2001 * instance rate shaders are used to emulate the filter or to select the
2002 * specified sample. The sample number is static in the programs.
2003 */
2004 static VkResult pvr_msaa_state(const struct pvr_device_info *dev_info,
2005 const struct pvr_transfer_cmd *transfer_cmd,
2006 struct pvr_transfer_3d_state *state,
2007 uint32_t source)
2008 {
2009 struct pvr_tq_shader_properties *shader_props = &state->shader_props;
2010 struct pvr_tq_layer_properties *layer = &shader_props->layer_props;
2011 struct pvr_winsys_transfer_regs *const regs = &state->regs;
2012 uint32_t src_sample_count =
2013 transfer_cmd->sources[source].surface.sample_count & ~1U;
2014 uint32_t dst_sample_count = transfer_cmd->dst.sample_count & ~1U;
2015 uint32_t bsample_count = 0U;
2016
2017 shader_props->full_rate = false;
2018 state->msaa_multiplier = 1U;
2019 state->down_scale = false;
2020
2021 /* clang-format off */
2022 pvr_csb_pack (&regs->isp_aa, CR_ISP_AA, reg);
2023 /* clang-format on */
2024
2025 layer->sample_count = 1U;
2026 layer->resolve_op = PVR_RESOLVE_BLEND;
2027
2028 bsample_count |= src_sample_count | dst_sample_count;
2029
2030 if (bsample_count > PVR_GET_FEATURE_VALUE(dev_info, max_multisample, 0U))
2031 return vk_error(transfer_cmd->cmd_buffer, VK_ERROR_FORMAT_NOT_SUPPORTED);
2032
2033 /* Shouldn't get two distinct bits set (implies different sample counts).
2034 * The reason being the rate at which the shader runs has to match.
2035 */
2036 if ((bsample_count & (bsample_count - 1U)) != 0U)
2037 return vk_error(transfer_cmd->cmd_buffer, VK_ERROR_FORMAT_NOT_SUPPORTED);
2038
2039 if (src_sample_count == 0U && dst_sample_count == 0U) {
2040 /* S -> S (no MSAA involved). */
2041 layer->msaa = false;
2042 } else if (src_sample_count != 0U && dst_sample_count == 0U) {
2043 /* M -> S (resolve). */
2044 layer->resolve_op = transfer_cmd->sources[source].resolve_op;
2045
2046 if ((uint32_t)layer->resolve_op >=
2047 (src_sample_count + (uint32_t)PVR_RESOLVE_SAMPLE0)) {
2048 return vk_error(transfer_cmd->cmd_buffer,
2049 VK_ERROR_FORMAT_NOT_SUPPORTED);
2050 }
2051
2052 layer->msaa = true;
2053
2054 switch (layer->resolve_op) {
2055 case PVR_RESOLVE_MIN:
2056 case PVR_RESOLVE_MAX:
2057 switch (transfer_cmd->sources[source].surface.vk_format) {
2058 case VK_FORMAT_D32_SFLOAT:
2059 case VK_FORMAT_D16_UNORM:
2060 case VK_FORMAT_S8_UINT:
2061 case VK_FORMAT_D24_UNORM_S8_UINT:
2062 case VK_FORMAT_X8_D24_UNORM_PACK32:
2063 if (transfer_cmd->sources[source].surface.vk_format !=
2064 transfer_cmd->dst.vk_format) {
2065 return vk_error(transfer_cmd->cmd_buffer,
2066 VK_ERROR_FORMAT_NOT_SUPPORTED);
2067 }
2068 break;
2069
2070 default:
2071 return vk_error(transfer_cmd->cmd_buffer,
2072 VK_ERROR_FORMAT_NOT_SUPPORTED);
2073 }
2074
2075 /* Instance rate. */
2076 layer->sample_count = src_sample_count;
2077 state->shader_props.full_rate = false;
2078 break;
2079
2080 case PVR_RESOLVE_BLEND:
2081 if (pvr_int_pbe_pixel_requires_usc_filter(dev_info,
2082 layer->pbe_format)) {
2083 /* Instance rate. */
2084 layer->sample_count = src_sample_count;
2085 state->shader_props.full_rate = false;
2086 } else {
2087 /* Sample rate. */
2088 state->shader_props.full_rate = true;
2089 state->msaa_multiplier = src_sample_count;
2090 state->down_scale = true;
2091
2092 pvr_csb_pack (&regs->isp_aa, CR_ISP_AA, reg) {
2093 reg.mode = pvr_cr_isp_aa_mode_type(src_sample_count);
2094 }
2095 }
2096 break;
2097
2098 default:
2099 /* Shader doesn't have to know the number of samples. It's enough
2100 * if the TPU knows, and the shader sets the right sno (given to the
2101 * shader in resolve_op).
2102 */
2103 state->shader_props.full_rate = false;
2104 break;
2105 }
2106 } else {
2107 state->msaa_multiplier = dst_sample_count;
2108
2109 pvr_csb_pack (&regs->isp_aa, CR_ISP_AA, reg) {
2110 reg.mode = pvr_cr_isp_aa_mode_type(dst_sample_count);
2111 }
2112
2113 if (src_sample_count == 0U && dst_sample_count != 0U) {
2114 /* S -> M (replicate samples) */
2115 layer->msaa = false;
2116 state->shader_props.full_rate = !state->shader_props.iterated;
2117 } else {
2118 /* M -> M (sample to sample) */
2119 layer->msaa = true;
2120 state->shader_props.full_rate = true;
2121 }
2122 }
2123
2124 return VK_SUCCESS;
2125 }
2126
2127 static bool pvr_requires_usc_linear_filter(VkFormat format)
2128 {
2129 switch (format) {
2130 case VK_FORMAT_R32_SFLOAT:
2131 case VK_FORMAT_R32G32_SFLOAT:
2132 case VK_FORMAT_R32G32B32_SFLOAT:
2133 case VK_FORMAT_R32G32B32A32_SFLOAT:
2134 case VK_FORMAT_D32_SFLOAT:
2135 case VK_FORMAT_D24_UNORM_S8_UINT:
2136 case VK_FORMAT_X8_D24_UNORM_PACK32:
2137 case VK_FORMAT_D32_SFLOAT_S8_UINT:
2138 return true;
2139 default:
2140 return false;
2141 }
2142 }
2143
2144 static inline bool
2145 pvr_int_pbe_usc_linear_filter(enum pvr_transfer_pbe_pixel_src pbe_format,
2146 bool sample,
2147 bool msaa,
2148 bool full_rate)
2149 {
2150 if (sample || msaa || full_rate)
2151 return false;
2152
2153 switch (pbe_format) {
2154 case PVR_TRANSFER_PBE_PIXEL_SRC_D24S8:
2155 case PVR_TRANSFER_PBE_PIXEL_SRC_S8D24:
2156 case PVR_TRANSFER_PBE_PIXEL_SRC_D32S8:
2157 case PVR_TRANSFER_PBE_PIXEL_SRC_F32:
2158 case PVR_TRANSFER_PBE_PIXEL_SRC_F32X2:
2159 case PVR_TRANSFER_PBE_PIXEL_SRC_F32X4:
2160 return true;
2161 default:
2162 return false;
2163 }
2164 }
2165
2166 static inline bool pvr_pick_component_needed(
2167 const struct pvr_transfer_custom_mapping *custom_mapping)
2168 {
2169 return custom_mapping->pass_count > 0U &&
2170 custom_mapping->texel_extend_dst > 1U &&
2171 custom_mapping->texel_extend_src <= 1U;
2172 }
2173
2174 /** Writes the shader related constants into the DMA space. */
2175 static void
2176 pvr_write_usc_constants(const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
2177 uint32_t *dma_space)
2178 {
2179 const uint32_t reg = sh_reg_layout->driver_total;
2180 const uint32_t consts_count =
2181 sh_reg_layout->compiler_out.usc_constants.count;
2182
2183 /* If not we likely need to write more consts. */
2184 assert(consts_count == sh_reg_layout->compiler_out_total);
2185
2186 /* Append the usc consts after the driver allocated regs. */
2187 for (uint32_t i = 0U; i < consts_count; i++)
2188 dma_space[reg + i] = sh_reg_layout->compiler_out.usc_constants.values[i];
2189 }
2190
2191 static inline void
2192 pvr_dma_texel_unwind(struct pvr_transfer_3d_state *state,
2193 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout,
2194 uint32_t *mem_ptr)
2195
2196 {
2197 const uint32_t coord_sample_mask =
2198 state->custom_mapping.texel_extend_dst - 1U;
2199
2200 mem_ptr[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
2201 coord_sample_mask;
2202 mem_ptr[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
2203 state->custom_mapping.texel_unwind_dst;
2204 }
2205
2206 /** Writes the Uniform/Texture state data segments + the UniTex code. */
2207 static inline VkResult
2208 pvr_pds_unitex(const struct pvr_device_info *dev_info,
2209 struct pvr_transfer_ctx *ctx,
2210 const struct pvr_transfer_cmd *transfer_cmd,
2211 struct pvr_pds_pixel_shader_sa_program *program,
2212 struct pvr_transfer_prep_data *prep_data)
2213 {
2214 struct pvr_pds_upload *unitex_code =
2215 &ctx->pds_unitex_code[program->num_texture_dma_kicks]
2216 [program->num_uniform_dma_kicks];
2217 struct pvr_transfer_3d_state *state = &prep_data->state;
2218 struct pvr_suballoc_bo *pvr_bo;
2219 VkResult result;
2220 void *map;
2221
2222 /* Uniform program is not used. */
2223 assert(program->num_uniform_dma_kicks == 0U);
2224
2225 if (program->num_texture_dma_kicks == 0U) {
2226 state->uniform_data_size = 0U;
2227 state->tex_state_data_size = 0U;
2228 state->tex_state_data_offset = 0U;
2229 state->uni_tex_code_offset = 0U;
2230
2231 return VK_SUCCESS;
2232 }
2233
2234 pvr_pds_set_sizes_pixel_shader_sa_uniform_data(program, dev_info);
2235 assert(program->data_size == 0U);
2236 state->uniform_data_size = 0U;
2237
2238 pvr_pds_set_sizes_pixel_shader_sa_texture_data(program, dev_info);
2239 state->tex_state_data_size =
2240 ALIGN_POT(program->data_size,
2241 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEXTURESTATESIZE_UNIT_SIZE);
2242
2243 result =
2244 pvr_arch_cmd_buffer_alloc_mem(transfer_cmd->cmd_buffer,
2245 ctx->device->heaps.pds_heap,
2246 PVR_DW_TO_BYTES(state->tex_state_data_size),
2247 &pvr_bo);
2248 if (result != VK_SUCCESS)
2249 return result;
2250
2251 state->tex_state_data_offset =
2252 pvr_bo->dev_addr.addr - ctx->device->heaps.pds_heap->base_addr.addr;
2253
2254 map = pvr_bo_suballoc_get_map_addr(pvr_bo);
2255 pvr_pds_generate_pixel_shader_sa_texture_state_data(program, map, dev_info);
2256
2257 /* Save the dev_addr and size in the 3D state. */
2258 state->uni_tex_code_offset = unitex_code->code_offset;
2259 state->pds_temps = program->temps_used;
2260
2261 return VK_SUCCESS;
2262 }
2263
2264 /** Converts a float in range 0 to 1 to an N-bit fixed-point integer. */
2265 static uint32_t pvr_float_to_ufixed(float value, uint32_t bits)
2266 {
2267 uint32_t max = (1U << bits) - 1U;
2268
2269 /* NaN and Inf and overflow. */
2270 if (util_is_inf_or_nan(value) || value >= 1.0f)
2271 return max;
2272 else if (value < 0.0f)
2273 return 0U;
2274
2275 /* Normalise. */
2276 value = value * (float)max;
2277
2278 /* Cast to double so that we can accurately represent the sum for N > 23. */
2279 return (uint32_t)floor((double)value + 0.5f);
2280 }
2281
2282 /** Converts a float in range -1 to 1 to a signed N-bit fixed-point integer. */
2283 static uint32_t pvr_float_to_sfixed(float value, uint32_t N)
2284 {
2285 int32_t max = (1 << (N - 1)) - 1;
2286 int32_t min = 0 - (1 << (N - 1));
2287 union fi x;
2288
2289 /* NaN and Inf and overflow. */
2290 if (util_is_inf_or_nan(value) || value >= 1.0f)
2291 return (uint32_t)max;
2292 else if (value == 0.0f)
2293 return 0U;
2294 else if (value <= -1.0f)
2295 return (uint32_t)min;
2296
2297 /* Normalise. */
2298 value *= (float)max;
2299
2300 /* Cast to double so that we can accurately represent the sum for N > 23. */
2301 if (value > 0.0f)
2302 x.i = (int32_t)floor((double)value + 0.5f);
2303 else
2304 x.i = (int32_t)floor((double)value - 0.5f);
2305
2306 return x.ui;
2307 }
2308
2309 /** Convert a value in IEEE single precision format to 16-bit floating point
2310 * format.
2311 */
2312 /* TODO: See if we can use _mesa_float_to_float16_rtz_slow() instead. */
2313 static uint16_t pvr_float_to_f16(float value, bool round_to_even)
2314 {
2315 uint32_t input_value;
2316 uint32_t exponent;
2317 uint32_t mantissa;
2318 uint16_t output;
2319
2320 /* 0.0f can be exactly expressed in binary using IEEE float format. */
2321 if (value == 0.0f)
2322 return 0U;
2323
2324 if (value < 0U) {
2325 output = 0x8000;
2326 value = -value;
2327 } else {
2328 output = 0U;
2329 }
2330
2331 /* 2^16 * (2 - 1/1024) = highest f16 representable value. */
2332 value = MIN2(value, 131008);
2333 input_value = fui(value);
2334
2335 /* Extract the exponent and mantissa. */
2336 exponent = util_get_float32_exponent(value) + 15;
2337 mantissa = input_value & ((1 << 23) - 1);
2338
2339 /* If the exponent is outside the supported range then denormalise the
2340 * mantissa.
2341 */
2342 if ((int32_t)exponent <= 0) {
2343 uint32_t shift;
2344
2345 mantissa |= (1 << 23);
2346 exponent = input_value >> 23;
2347 shift = -14 + 127 - exponent;
2348
2349 if (shift < 24)
2350 mantissa >>= shift;
2351 else
2352 mantissa = 0;
2353 } else {
2354 output = (uint16_t)(output | ((exponent << 10) & 0x7C00));
2355 }
2356
2357 output = (uint16_t)(output | (((mantissa >> 13) << 0) & 0x03FF));
2358
2359 if (round_to_even) {
2360 /* Round to nearest even. */
2361 if ((((int)value) % 2 != 0) && (((1 << 13) - 1) & mantissa))
2362 output++;
2363 } else {
2364 /* Round to nearest. */
2365 if (mantissa & (1 << 12))
2366 output++;
2367 }
2368
2369 return output;
2370 }
2371
2372 static VkResult pvr_pack_clear_color(VkFormat format,
2373 const union fi color[static 4],
2374 uint32_t pkd_color[static 4])
2375 {
2376 const uint32_t red_width =
2377 vk_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0U);
2378 uint32_t pbe_pack_mode = pvr_arch_get_pbe_packmode(format);
2379 const bool pbe_norm = pvr_vk_format_is_fully_normalized(format);
2380
2381 /* TODO: Use PBE Accum format NOT PBE pack format! */
2382 if (vk_format_is_srgb(format))
2383 pbe_pack_mode = ROGUE_PBESTATE_PACKMODE_F16F16F16F16;
2384
2385 if (pbe_pack_mode == ROGUE_PBESTATE_PACKMODE_INVALID)
2386 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
2387
2388 /* Set packed color based on PBE pack mode and PBE norm. */
2389 switch (pbe_pack_mode) {
2390 case ROGUE_PBESTATE_PACKMODE_U8U8U8U8:
2391 case ROGUE_PBESTATE_PACKMODE_A8R3G3B2:
2392 if (pbe_norm) {
2393 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 8) & 0xFFU;
2394 pkd_color[0] |= (pvr_float_to_ufixed(color[1].f, 8) & 0xFFU) << 8;
2395 pkd_color[0] |= (pvr_float_to_ufixed(color[2].f, 8) & 0xFFU) << 16;
2396 pkd_color[0] |= (pvr_float_to_ufixed(color[3].f, 8) & 0xFFU) << 24;
2397 } else {
2398 pkd_color[0] = color[0].ui & 0xFFU;
2399 pkd_color[0] |= (color[1].ui & 0xFFU) << 8;
2400 pkd_color[0] |= (color[2].ui & 0xFFU) << 16;
2401 pkd_color[0] |= (color[3].ui & 0xFFU) << 24;
2402 }
2403 break;
2404
2405 case ROGUE_PBESTATE_PACKMODE_S8S8S8S8:
2406 case ROGUE_PBESTATE_PACKMODE_X8U8S8S8:
2407 case ROGUE_PBESTATE_PACKMODE_X8S8S8U8:
2408 if (pbe_norm) {
2409 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, false);
2410 pkd_color[0] |= (uint32_t)pvr_float_to_f16(color[1].f, false) << 16;
2411 pkd_color[1] = (uint32_t)pvr_float_to_f16(color[2].f, false);
2412 pkd_color[1] |= (uint32_t)pvr_float_to_f16(color[3].f, false) << 16;
2413 } else {
2414 pkd_color[0] = color[0].ui & 0xFFU;
2415 pkd_color[0] |= (color[1].ui & 0xFFU) << 8;
2416 pkd_color[0] |= (color[2].ui & 0xFFU) << 16;
2417 pkd_color[0] |= (color[3].ui & 0xFFU) << 24;
2418 }
2419 break;
2420
2421 case ROGUE_PBESTATE_PACKMODE_U16U16U16U16:
2422 if (pbe_norm) {
2423 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 16) & 0xFFFFU;
2424 pkd_color[0] |= (pvr_float_to_ufixed(color[1].f, 16) & 0xFFFFU) << 16;
2425 pkd_color[1] = pvr_float_to_ufixed(color[2].f, 16) & 0xFFFFU;
2426 pkd_color[1] |= (pvr_float_to_ufixed(color[3].f, 16) & 0xFFFFU) << 16;
2427 } else {
2428 pkd_color[0] = color[0].ui & 0xFFFFU;
2429 pkd_color[0] |= (color[1].ui & 0xFFFFU) << 16;
2430 pkd_color[1] = color[2].ui & 0xFFFFU;
2431 pkd_color[1] |= (color[3].ui & 0xFFFFU) << 16;
2432 }
2433 break;
2434
2435 case ROGUE_PBESTATE_PACKMODE_S16S16S16S16:
2436 if (pbe_norm) {
2437 pkd_color[0] = pvr_float_to_sfixed(color[0].f, 16) & 0xFFFFU;
2438 pkd_color[0] |= (pvr_float_to_sfixed(color[1].f, 16) & 0xFFFFU) << 16;
2439 pkd_color[1] = (pvr_float_to_sfixed(color[2].f, 16) & 0xFFFFU);
2440 pkd_color[1] |= (pvr_float_to_sfixed(color[3].f, 16) & 0xFFFFU) << 16;
2441 } else {
2442 pkd_color[0] = color[0].ui & 0xFFFFU;
2443 pkd_color[0] |= (color[1].ui & 0xFFFFU) << 16;
2444 pkd_color[1] = color[2].ui & 0xFFFFU;
2445 pkd_color[1] |= (color[3].ui & 0xFFFFU) << 16;
2446 }
2447 break;
2448
2449 case ROGUE_PBESTATE_PACKMODE_A2_XRBIAS_U10U10U10:
2450 case ROGUE_PBESTATE_PACKMODE_ARGBV16_XR10:
2451 case ROGUE_PBESTATE_PACKMODE_F16F16F16F16:
2452 case ROGUE_PBESTATE_PACKMODE_A2R10B10G10:
2453 case ROGUE_PBESTATE_PACKMODE_A4R4G4B4:
2454 case ROGUE_PBESTATE_PACKMODE_A1R5G5B5:
2455 case ROGUE_PBESTATE_PACKMODE_R5G5B5A1:
2456 case ROGUE_PBESTATE_PACKMODE_R5G6B5:
2457 if (red_width > 0) {
2458 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, false);
2459 pkd_color[0] |= (uint32_t)pvr_float_to_f16(color[1].f, false) << 16;
2460 pkd_color[1] = (uint32_t)pvr_float_to_f16(color[2].f, false);
2461 pkd_color[1] |= (uint32_t)pvr_float_to_f16(color[3].f, false) << 16;
2462 } else {
2463 /* Swizzle only uses first channel for alpha formats. */
2464 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[3].f, false);
2465 }
2466 break;
2467
2468 case ROGUE_PBESTATE_PACKMODE_U32U32U32U32:
2469 pkd_color[0] = color[0].ui;
2470 pkd_color[1] = color[1].ui;
2471 pkd_color[2] = color[2].ui;
2472 pkd_color[3] = color[3].ui;
2473 break;
2474
2475 case ROGUE_PBESTATE_PACKMODE_S32S32S32S32:
2476 pkd_color[0] = (uint32_t)color[0].i;
2477 pkd_color[1] = (uint32_t)color[1].i;
2478 pkd_color[2] = (uint32_t)color[2].i;
2479 pkd_color[3] = (uint32_t)color[3].i;
2480 break;
2481
2482 case ROGUE_PBESTATE_PACKMODE_F32F32F32F32:
2483 memcpy(pkd_color, &color[0].f, 4U * sizeof(float));
2484 break;
2485
2486 case ROGUE_PBESTATE_PACKMODE_R10B10G10A2:
2487 if (pbe_norm) {
2488 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 10) & 0xFFU;
2489 pkd_color[0] |= (pvr_float_to_ufixed(color[1].f, 10) & 0xFFU) << 10;
2490 pkd_color[0] |= (pvr_float_to_ufixed(color[2].f, 10) & 0xFFU) << 20;
2491 pkd_color[0] |= (pvr_float_to_ufixed(color[3].f, 2) & 0xFFU) << 30;
2492 } else if (format == VK_FORMAT_A2R10G10B10_UINT_PACK32) {
2493 pkd_color[0] = color[2].ui & 0x3FFU;
2494 pkd_color[0] |= (color[1].ui & 0x3FFU) << 10;
2495 pkd_color[0] |= (color[0].ui & 0x3FFU) << 20;
2496 pkd_color[0] |= (color[3].ui & 0x3U) << 30;
2497 } else {
2498 pkd_color[0] = color[0].ui & 0x3FFU;
2499 pkd_color[0] |= (color[1].ui & 0x3FFU) << 10;
2500 pkd_color[0] |= (color[2].ui & 0x3FFU) << 20;
2501 pkd_color[0] |= (color[3].ui & 0x3U) << 30;
2502 }
2503
2504 break;
2505
2506 case ROGUE_PBESTATE_PACKMODE_A2F10F10F10:
2507 case ROGUE_PBESTATE_PACKMODE_F10F10F10A2:
2508 pkd_color[0] = pvr_float_to_sfixed(color[0].f, 10) & 0xFFU;
2509 pkd_color[0] |= (pvr_float_to_sfixed(color[1].f, 10) & 0xFFU) << 10;
2510 pkd_color[0] |= (pvr_float_to_sfixed(color[2].f, 10) & 0xFFU) << 20;
2511 pkd_color[0] |= (pvr_float_to_sfixed(color[3].f, 2) & 0xFFU) << 30;
2512 break;
2513
2514 case ROGUE_PBESTATE_PACKMODE_U8U8U8:
2515 case ROGUE_PBESTATE_PACKMODE_R5SG5SB6:
2516 if (pbe_norm) {
2517 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 8) & 0xFFU;
2518 pkd_color[0] |= (pvr_float_to_ufixed(color[1].f, 8) & 0xFFU) << 8;
2519 pkd_color[0] |= (pvr_float_to_ufixed(color[2].f, 8) & 0xFFU) << 16;
2520 } else {
2521 pkd_color[0] = color[0].ui & 0xFFU;
2522 pkd_color[0] |= (color[1].ui & 0xFFU) << 8;
2523 pkd_color[0] |= (color[2].ui & 0xFFU) << 16;
2524 }
2525 break;
2526
2527 case ROGUE_PBESTATE_PACKMODE_S8S8S8:
2528 case ROGUE_PBESTATE_PACKMODE_B6G5SR5S:
2529 if (pbe_norm) {
2530 pkd_color[0] = pvr_float_to_sfixed(color[0].f, 8) & 0xFFU;
2531 pkd_color[0] |= (pvr_float_to_sfixed(color[1].f, 8) & 0xFFU) << 8;
2532 pkd_color[0] |= (pvr_float_to_sfixed(color[2].f, 8) & 0xFFU) << 16;
2533 } else {
2534 pkd_color[0] = color[0].ui & 0xFFU;
2535 pkd_color[0] |= (color[1].ui & 0xFFU) << 8;
2536 pkd_color[0] |= (color[2].ui & 0xFFU) << 16;
2537 }
2538 break;
2539
2540 case ROGUE_PBESTATE_PACKMODE_U16U16U16:
2541 if (pbe_norm) {
2542 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 16) & 0xFFFFU;
2543 pkd_color[0] |= (pvr_float_to_ufixed(color[1].f, 16) & 0xFFFFU) << 16;
2544 pkd_color[1] = (pvr_float_to_ufixed(color[2].f, 16) & 0xFFFFU);
2545 } else {
2546 pkd_color[0] = color[0].ui & 0xFFFFU;
2547 pkd_color[0] |= (color[1].ui & 0xFFFFU) << 16;
2548 pkd_color[1] = color[2].ui & 0xFFFFU;
2549 }
2550 break;
2551
2552 case ROGUE_PBESTATE_PACKMODE_S16S16S16:
2553 if (pbe_norm) {
2554 pkd_color[0] = pvr_float_to_sfixed(color[0].f, 16) & 0xFFFFU;
2555 pkd_color[0] |= (pvr_float_to_sfixed(color[1].f, 16) & 0xFFFFU) << 16;
2556 pkd_color[1] = pvr_float_to_sfixed(color[2].f, 16) & 0xFFFFU;
2557 } else {
2558 pkd_color[0] = color[0].ui & 0xFFFFU;
2559 pkd_color[0] |= (color[1].ui & 0xFFFFU) << 16;
2560 pkd_color[1] = color[2].ui & 0xFFFFU;
2561 }
2562 break;
2563
2564 case ROGUE_PBESTATE_PACKMODE_F16F16F16:
2565 case ROGUE_PBESTATE_PACKMODE_F11F11F10:
2566 case ROGUE_PBESTATE_PACKMODE_F10F11F11:
2567 case ROGUE_PBESTATE_PACKMODE_SE9995:
2568 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, true);
2569 pkd_color[0] |= (uint32_t)pvr_float_to_f16(color[1].f, true) << 16;
2570 pkd_color[1] = (uint32_t)pvr_float_to_f16(color[2].f, true);
2571 break;
2572
2573 case ROGUE_PBESTATE_PACKMODE_U32U32U32:
2574 pkd_color[0] = color[0].ui;
2575 pkd_color[1] = color[1].ui;
2576 pkd_color[2] = color[2].ui;
2577 break;
2578
2579 case ROGUE_PBESTATE_PACKMODE_S32S32S32:
2580 pkd_color[0] = (uint32_t)color[0].i;
2581 pkd_color[1] = (uint32_t)color[1].i;
2582 pkd_color[2] = (uint32_t)color[2].i;
2583 break;
2584
2585 case ROGUE_PBESTATE_PACKMODE_X24G8X32:
2586 case ROGUE_PBESTATE_PACKMODE_U8X24:
2587 pkd_color[1] = (color[1].ui & 0xFFU) << 24;
2588 break;
2589
2590 case ROGUE_PBESTATE_PACKMODE_F32F32F32:
2591 memcpy(pkd_color, &color[0].f, 3U * sizeof(float));
2592 break;
2593
2594 case ROGUE_PBESTATE_PACKMODE_U8U8:
2595 if (pbe_norm) {
2596 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, false);
2597 pkd_color[0] |= (uint32_t)pvr_float_to_f16(color[1].f, false) << 16;
2598 } else {
2599 pkd_color[0] = color[0].ui & 0xFFU;
2600 pkd_color[0] |= (color[1].ui & 0xFFU) << 8;
2601 }
2602 break;
2603
2604 case ROGUE_PBESTATE_PACKMODE_S8S8:
2605 if (pbe_norm) {
2606 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, false);
2607 pkd_color[0] |= (uint32_t)pvr_float_to_f16(color[1].f, false) << 16;
2608 } else {
2609 pkd_color[0] = color[0].ui & 0xFFU;
2610 pkd_color[0] |= (color[1].ui & 0xFFU) << 8;
2611 pkd_color[0] |= (color[2].ui & 0xFFU) << 16;
2612 pkd_color[0] |= (color[3].ui & 0xFFU) << 24;
2613 }
2614 break;
2615
2616 case ROGUE_PBESTATE_PACKMODE_U16U16:
2617 if (pbe_norm) {
2618 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 16) & 0xFFFFU;
2619 pkd_color[0] |= (pvr_float_to_ufixed(color[1].f, 16) & 0xFFFFU) << 16;
2620 } else {
2621 pkd_color[0] = color[0].ui & 0xFFFFU;
2622 pkd_color[0] |= (color[1].ui & 0xFFFFU) << 16;
2623 }
2624 break;
2625
2626 case ROGUE_PBESTATE_PACKMODE_S16S16:
2627 if (pbe_norm) {
2628 pkd_color[0] = pvr_float_to_sfixed(color[0].f, 16) & 0xFFFFU;
2629 pkd_color[0] |= (pvr_float_to_sfixed(color[1].f, 16) & 0xFFFFU) << 16;
2630 } else {
2631 pkd_color[0] = color[0].ui & 0xFFFFU;
2632 pkd_color[0] |= (color[1].ui & 0xFFFFU) << 16;
2633 }
2634 break;
2635
2636 case ROGUE_PBESTATE_PACKMODE_F16F16:
2637 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, true);
2638 pkd_color[0] |= (uint32_t)pvr_float_to_f16(color[1].f, true) << 16;
2639 break;
2640
2641 case ROGUE_PBESTATE_PACKMODE_U32U32:
2642 pkd_color[0] = color[0].ui;
2643 pkd_color[1] = color[1].ui;
2644 break;
2645
2646 case ROGUE_PBESTATE_PACKMODE_S32S32:
2647 pkd_color[0] = (uint32_t)color[0].i;
2648 pkd_color[1] = (uint32_t)color[1].i;
2649 break;
2650
2651 case ROGUE_PBESTATE_PACKMODE_X24U8F32:
2652 case ROGUE_PBESTATE_PACKMODE_X24X8F32:
2653 memcpy(pkd_color, &color[0].f, 1U * sizeof(float));
2654 pkd_color[1] = color[1].ui & 0xFFU;
2655 break;
2656
2657 case ROGUE_PBESTATE_PACKMODE_F32F32:
2658 memcpy(pkd_color, &color[0].f, 2U * sizeof(float));
2659 break;
2660
2661 case ROGUE_PBESTATE_PACKMODE_ST8U24:
2662 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 24) & 0xFFFFFFU;
2663 pkd_color[0] |= color[1].ui << 24;
2664 break;
2665
2666 case ROGUE_PBESTATE_PACKMODE_U8:
2667 if (format == VK_FORMAT_S8_UINT)
2668 pkd_color[0] = color[1].ui & 0xFFU;
2669 else if (pbe_norm)
2670 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, false);
2671 else
2672 pkd_color[0] = color[0].ui & 0xFFU;
2673
2674 break;
2675
2676 case ROGUE_PBESTATE_PACKMODE_S8:
2677 if (pbe_norm)
2678 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, false);
2679 else
2680 pkd_color[0] = color[0].ui & 0xFFU;
2681 break;
2682
2683 case ROGUE_PBESTATE_PACKMODE_U16:
2684 if (pbe_norm)
2685 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 16) & 0xFFFFU;
2686 else
2687 pkd_color[0] = color[0].ui & 0xFFFFU;
2688 break;
2689
2690 case ROGUE_PBESTATE_PACKMODE_S16:
2691 if (pbe_norm)
2692 pkd_color[0] = pvr_float_to_sfixed(color[0].f, 16) & 0xFFFFU;
2693 else
2694 pkd_color[0] = color[0].ui & 0xFFFFU;
2695 break;
2696
2697 case ROGUE_PBESTATE_PACKMODE_F16:
2698 pkd_color[0] = (uint32_t)pvr_float_to_f16(color[0].f, true);
2699 break;
2700
2701 /* U32 */
2702 case ROGUE_PBESTATE_PACKMODE_U32:
2703 if (format == VK_FORMAT_X8_D24_UNORM_PACK32) {
2704 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 24) & 0xFFFFFFU;
2705 } else if (format == VK_FORMAT_D24_UNORM_S8_UINT) {
2706 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 24) & 0xFFFFFFU;
2707 pkd_color[0] |= (color[1].ui & 0xFFU) << 24;
2708 } else if (format == VK_FORMAT_A2B10G10R10_UINT_PACK32) {
2709 pkd_color[0] = color[0].ui & 0x3FFU;
2710 pkd_color[0] |= (color[1].ui & 0x3FFU) << 10;
2711 pkd_color[0] |= (color[2].ui & 0x3FFU) << 20;
2712 pkd_color[0] |= (color[3].ui & 0x3U) << 30;
2713 } else {
2714 pkd_color[0] = color[0].ui;
2715 }
2716 break;
2717
2718 /* U24ST8 */
2719 case ROGUE_PBESTATE_PACKMODE_U24ST8:
2720 pkd_color[1] = (color[1].ui & 0xFFU) << 24;
2721 pkd_color[1] |= pvr_float_to_ufixed(color[0].f, 24) & 0xFFFFFFU;
2722 break;
2723
2724 /* S32 */
2725 case ROGUE_PBESTATE_PACKMODE_S32:
2726 pkd_color[0] = (uint32_t)color[0].i;
2727 break;
2728
2729 /* F32 */
2730 case ROGUE_PBESTATE_PACKMODE_F32:
2731 memcpy(pkd_color, &color[0].f, sizeof(float));
2732 break;
2733
2734 /* X8U24 */
2735 case ROGUE_PBESTATE_PACKMODE_X8U24:
2736 pkd_color[0] = pvr_float_to_ufixed(color[0].f, 24) & 0xFFFFFFU;
2737 break;
2738
2739 default:
2740 break;
2741 }
2742
2743 return VK_SUCCESS;
2744 }
2745
2746 static VkResult
2747 pvr_isp_scan_direction(struct pvr_transfer_cmd *transfer_cmd,
2748 bool custom_mapping,
2749 enum ROGUE_CR_DIR_TYPE *const dir_type_out)
2750 {
2751 pvr_dev_addr_t dst_dev_addr = transfer_cmd->dst.dev_addr;
2752 bool backwards_in_x = false;
2753 bool backwards_in_y = false;
2754 bool done_dest_rect = false;
2755 VkRect2D dst_rect;
2756 int32_t dst_x1;
2757 int32_t dst_y1;
2758
2759 for (uint32_t i = 0; i < transfer_cmd->source_count; i++) {
2760 struct pvr_transfer_cmd_source *src = &transfer_cmd->sources[i];
2761 pvr_dev_addr_t src_dev_addr = src->surface.dev_addr;
2762
2763 if (src_dev_addr.addr == dst_dev_addr.addr && !custom_mapping) {
2764 VkRect2D *src_rect = &src->mappings[0].src_rect;
2765 int32_t src_x1 = src_rect->offset.x + src_rect->extent.width;
2766 int32_t src_y1 = src_rect->offset.y + src_rect->extent.height;
2767
2768 if (!done_dest_rect) {
2769 dst_rect = src->mappings[0].dst_rect;
2770
2771 dst_x1 = dst_rect.offset.x + dst_rect.extent.width;
2772 dst_y1 = dst_rect.offset.y + dst_rect.extent.height;
2773
2774 done_dest_rect = true;
2775 }
2776
2777 if ((dst_rect.offset.x < src_x1 && dst_x1 > src_rect->offset.x) &&
2778 (dst_rect.offset.y < src_y1 && dst_y1 > src_rect->offset.y)) {
2779 if (src_rect->extent.width != dst_rect.extent.width ||
2780 src_rect->extent.height != dst_rect.extent.height) {
2781 /* Scaling is not possible. */
2782 return vk_error(NULL, VK_ERROR_FORMAT_NOT_SUPPORTED);
2783 }
2784
2785 /* Direction is to the right. */
2786 backwards_in_x = dst_rect.offset.x > src_rect->offset.x;
2787
2788 /* Direction is to the bottom. */
2789 backwards_in_y = dst_rect.offset.y > src_rect->offset.y;
2790 }
2791 }
2792 }
2793
2794 if (backwards_in_x) {
2795 if (backwards_in_y)
2796 *dir_type_out = ROGUE_CR_DIR_TYPE_BR2TL;
2797 else
2798 *dir_type_out = ROGUE_CR_DIR_TYPE_TR2BL;
2799 } else {
2800 if (backwards_in_y)
2801 *dir_type_out = ROGUE_CR_DIR_TYPE_BL2TR;
2802 else
2803 *dir_type_out = ROGUE_CR_DIR_TYPE_TL2BR;
2804 }
2805
2806 return VK_SUCCESS;
2807 }
2808
2809 static VkResult pvr_3d_copy_blit_core(struct pvr_transfer_ctx *ctx,
2810 struct pvr_transfer_cmd *transfer_cmd,
2811 struct pvr_transfer_prep_data *prep_data,
2812 uint32_t pass_idx,
2813 bool *finished_out)
2814 {
2815 struct pvr_transfer_3d_state *const state = &prep_data->state;
2816 struct pvr_winsys_transfer_regs *const regs = &state->regs;
2817 struct pvr_device *const device = ctx->device;
2818 const struct pvr_device_info *const dev_info = &device->pdevice->dev_info;
2819
2820 VkResult result;
2821
2822 *finished_out = true;
2823
2824 state->common_ptr = 0U;
2825 state->dynamic_const_reg_ptr = 0U;
2826 state->usc_const_reg_ptr = 0U;
2827
2828 if ((transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_FILL) != 0U) {
2829 uint32_t packed_color[4U] = { 0U };
2830
2831 if (transfer_cmd->source_count != 0U)
2832 return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
2833
2834 if (vk_format_is_compressed(transfer_cmd->dst.vk_format))
2835 return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
2836
2837 /* No shader. */
2838 state->pds_temps = 0U;
2839 state->uniform_data_size = 0U;
2840 state->tex_state_data_size = 0U;
2841
2842 /* No background enabled. */
2843 /* clang-format off */
2844 pvr_csb_pack (&regs->isp_bgobjvals, CR_ISP_BGOBJVALS, reg);
2845 /* clang-format on */
2846 pvr_csb_pack (&regs->isp_aa, CR_ISP_AA, reg) {
2847 reg.mode = pvr_cr_isp_aa_mode_type(transfer_cmd->dst.sample_count);
2848 }
2849
2850 result = pvr_pack_clear_color(transfer_cmd->dst.vk_format,
2851 transfer_cmd->clear_color,
2852 packed_color);
2853 if (result != VK_SUCCESS)
2854 return result;
2855
2856 pvr_csb_pack (&regs->usc_clear_register0, CR_USC_CLEAR_REGISTER, reg) {
2857 reg.val = packed_color[0U];
2858 }
2859
2860 pvr_csb_pack (&regs->usc_clear_register1, CR_USC_CLEAR_REGISTER, reg) {
2861 reg.val = packed_color[1U];
2862 }
2863
2864 pvr_csb_pack (&regs->usc_clear_register2, CR_USC_CLEAR_REGISTER, reg) {
2865 reg.val = packed_color[2U];
2866 }
2867
2868 pvr_csb_pack (&regs->usc_clear_register3, CR_USC_CLEAR_REGISTER, reg) {
2869 reg.val = packed_color[3U];
2870 }
2871
2872 state->msaa_multiplier = transfer_cmd->dst.sample_count & ~1U;
2873 state->pds_shader_task_offset = 0U;
2874 state->uni_tex_code_offset = 0U;
2875 state->tex_state_data_offset = 0U;
2876 } else if (transfer_cmd->source_count > 0U) {
2877 const struct pvr_tq_frag_sh_reg_layout nop_sh_reg_layout = {
2878 /* TODO: Setting this to 1 so that we don't try to pvr_bo_alloc() with
2879 * zero size. The device will ignore the PDS program if USC_SHAREDSIZE
2880 * is zero and in the case of the nop shader we're expecting it to be
2881 * zero. See if we can safely pass PVR_DEV_ADDR_INVALID for the unitex
2882 * program.
2883 */
2884 .driver_total = 1,
2885 };
2886 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout;
2887 struct pvr_pds_pixel_shader_sa_program unitex_prog = { 0U };
2888 uint32_t tex_state_dma_size_dw;
2889 struct pvr_suballoc_bo *pvr_bo;
2890 uint32_t *dma_space;
2891
2892 result = pvr_pbe_src_format(transfer_cmd, state, &state->shader_props);
2893 if (result != VK_SUCCESS)
2894 return result;
2895
2896 pvr_uv_space(dev_info, transfer_cmd, state);
2897
2898 state->shader_props.iterated = false;
2899
2900 state->shader_props.layer_props.sample =
2901 transfer_cmd->sources[0].surface.mem_layout ==
2902 PVR_MEMLAYOUT_3DTWIDDLED;
2903
2904 result = pvr_msaa_state(dev_info, transfer_cmd, state, 0);
2905 if (result != VK_SUCCESS)
2906 return result;
2907
2908 state->shader_props.pick_component =
2909 pvr_pick_component_needed(&state->custom_mapping);
2910
2911 if (state->filter[0] == PVR_FILTER_LINEAR &&
2912 pvr_requires_usc_linear_filter(
2913 transfer_cmd->sources[0].surface.vk_format)) {
2914 if (pvr_int_pbe_usc_linear_filter(
2915 state->shader_props.layer_props.pbe_format,
2916 state->shader_props.layer_props.sample,
2917 state->shader_props.layer_props.msaa,
2918 state->shader_props.full_rate)) {
2919 state->shader_props.layer_props.linear = true;
2920 } else {
2921 mesa_logw("Transfer: F32 linear filter not supported.");
2922 }
2923 }
2924
2925 if (state->empty_dst) {
2926 sh_reg_layout = &nop_sh_reg_layout;
2927 state->pds_shader_task_offset = device->nop_program.pds.data_offset;
2928 } else {
2929 pvr_dev_addr_t kick_usc_pds_dev_addr;
2930
2931 result =
2932 pvr_transfer_frag_store_get_shader_info(device,
2933 &ctx->frag_store,
2934 &state->shader_props,
2935 &kick_usc_pds_dev_addr,
2936 &sh_reg_layout);
2937 if (result != VK_SUCCESS)
2938 return result;
2939
2940 assert(kick_usc_pds_dev_addr.addr <= UINT32_MAX);
2941 state->pds_shader_task_offset = (uint32_t)kick_usc_pds_dev_addr.addr;
2942 }
2943
2944 unitex_prog.kick_usc = false;
2945 unitex_prog.clear = false;
2946
2947 tex_state_dma_size_dw =
2948 sh_reg_layout->driver_total + sh_reg_layout->compiler_out_total;
2949
2950 unitex_prog.num_texture_dma_kicks = 1U;
2951 unitex_prog.num_uniform_dma_kicks = 0U;
2952
2953 result =
2954 pvr_arch_cmd_buffer_alloc_mem(transfer_cmd->cmd_buffer,
2955 device->heaps.general_heap,
2956 PVR_DW_TO_BYTES(tex_state_dma_size_dw),
2957 &pvr_bo);
2958 if (result != VK_SUCCESS)
2959 return result;
2960
2961 dma_space = (uint32_t *)pvr_bo_suballoc_get_map_addr(pvr_bo);
2962
2963 result = pvr_sampler_image_state(ctx,
2964 transfer_cmd,
2965 sh_reg_layout,
2966 state,
2967 dma_space);
2968 if (result != VK_SUCCESS)
2969 return result;
2970
2971 pvr_dma_texture_floats(transfer_cmd, state, sh_reg_layout, dma_space);
2972
2973 if (transfer_cmd->sources[0].surface.mem_layout ==
2974 PVR_MEMLAYOUT_3DTWIDDLED) {
2975 dma_space[pvr_dynamic_const_reg_advance(sh_reg_layout, state)] =
2976 fui(transfer_cmd->sources[0].surface.z_position);
2977 }
2978
2979 pvr_write_usc_constants(sh_reg_layout, dma_space);
2980
2981 if (pvr_pick_component_needed(&state->custom_mapping))
2982 pvr_dma_texel_unwind(state, sh_reg_layout, dma_space);
2983
2984 pvr_pds_encode_dma_burst(unitex_prog.texture_dma_control,
2985 unitex_prog.texture_dma_address,
2986 state->common_ptr,
2987 tex_state_dma_size_dw,
2988 pvr_bo->dev_addr.addr,
2989 true,
2990 dev_info);
2991
2992 state->common_ptr += tex_state_dma_size_dw;
2993
2994 result =
2995 pvr_pds_unitex(dev_info, ctx, transfer_cmd, &unitex_prog, prep_data);
2996 if (result != VK_SUCCESS)
2997 return result;
2998
2999 pvr_csb_pack (&regs->isp_bgobjvals, CR_ISP_BGOBJVALS, reg) {
3000 reg.enablebgtag = true;
3001 }
3002 } else {
3003 /* No shader. */
3004 state->pds_temps = 0U;
3005 state->uniform_data_size = 0U;
3006 state->tex_state_data_size = 0U;
3007
3008 /* No background enabled. */
3009 /* clang-format off */
3010 pvr_csb_pack (&regs->isp_bgobjvals, CR_ISP_BGOBJVALS, reg);
3011 /* clang-format on */
3012 pvr_csb_pack (&regs->isp_aa, CR_ISP_AA, reg) {
3013 reg.mode = pvr_cr_isp_aa_mode_type(transfer_cmd->dst.sample_count);
3014 }
3015 state->msaa_multiplier = transfer_cmd->dst.sample_count & ~1U;
3016 state->pds_shader_task_offset = 0U;
3017 state->uni_tex_code_offset = 0U;
3018 state->tex_state_data_offset = 0U;
3019
3020 result = pvr_pbe_src_format(transfer_cmd, state, &state->shader_props);
3021 if (result != VK_SUCCESS)
3022 return result;
3023 }
3024
3025 pvr_setup_hwbg_object(dev_info, state);
3026
3027 pvr_csb_pack (&regs->isp_render, CR_ISP_RENDER, reg) {
3028 reg.mode_type = ROGUE_CR_ISP_RENDER_MODE_TYPE_FAST_SCALE;
3029
3030 result = pvr_isp_scan_direction(transfer_cmd,
3031 state->custom_mapping.pass_count,
3032 &reg.dir_type);
3033 if (result != VK_SUCCESS)
3034 return result;
3035 }
3036
3037 /* Set up pixel event handling. */
3038 result = pvr_pbe_setup(transfer_cmd, ctx, state);
3039 if (result != VK_SUCCESS)
3040 return result;
3041
3042 result = pvr_isp_tiles(device, state);
3043 if (result != VK_SUCCESS)
3044 return result;
3045
3046 if (PVR_HAS_FEATURE(&device->pdevice->dev_info, gpu_multicore_support)) {
3047 pvr_csb_pack (&regs->frag_screen, CR_FRAG_SCREEN, reg) {
3048 reg.xmax = transfer_cmd->dst.width - 1;
3049 reg.ymax = transfer_cmd->dst.height - 1;
3050 }
3051 }
3052
3053 if ((pass_idx + 1U) < state->custom_mapping.pass_count)
3054 *finished_out = false;
3055
3056 return VK_SUCCESS;
3057 }
3058
3059 static VkResult
3060 pvr_pbe_src_format_f2d(uint32_t merge_flags,
3061 struct pvr_transfer_cmd_source *src,
3062 VkFormat dst_format,
3063 bool down_scale,
3064 bool dont_force_pbe,
3065 enum pvr_transfer_pbe_pixel_src *pixel_format_out)
3066 {
3067 VkFormat src_format = src->surface.vk_format;
3068
3069 /* This has to come before the rest as S8 for instance is integer and
3070 * signedsess check fails on D24S8.
3071 */
3072 if (vk_format_is_depth_or_stencil(src_format) ||
3073 vk_format_is_depth_or_stencil(dst_format) ||
3074 merge_flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE) {
3075 return pvr_pbe_src_format_ds(&src->surface,
3076 src->filter,
3077 dst_format,
3078 merge_flags,
3079 down_scale,
3080 pixel_format_out);
3081 }
3082
3083 return pvr_pbe_src_format_normal(src_format,
3084 dst_format,
3085 down_scale,
3086 dont_force_pbe,
3087 pixel_format_out);
3088 }
3089
3090 /** Writes the coefficient loading PDS task. */
3091 static inline VkResult
3092 pvr_pds_coeff_task(struct pvr_transfer_ctx *ctx,
3093 const struct pvr_transfer_cmd *transfer_cmd,
3094 const bool sample_3d,
3095 struct pvr_transfer_prep_data *prep_data)
3096 {
3097 struct pvr_transfer_3d_state *state = &prep_data->state;
3098 struct pvr_pds_coeff_loading_program program = { 0U };
3099 struct pvr_suballoc_bo *pvr_bo;
3100 VkResult result;
3101
3102 program.num_fpu_iterators = 1U;
3103
3104 pvr_csb_pack (&program.FPU_iterators[0U],
3105 PDSINST_DOUT_FIELDS_DOUTI_SRC,
3106 reg) {
3107 if (sample_3d)
3108 reg.size = ROGUE_PDSINST_DOUTI_SIZE_3D;
3109 else
3110 reg.size = ROGUE_PDSINST_DOUTI_SIZE_2D;
3111
3112 reg.perspective = false;
3113
3114 /* Varying wrap on the TSP means that the TSP chooses the shorter path
3115 * out of the normal and the wrapping path i.e. chooses between u0->u1
3116 * and u1->1.0 == 0.0 -> u0. We don't need this behavior.
3117 */
3118 /*
3119 * if RHW ever needed offset SRC_F32 to the first U in 16 bit units
3120 * l0 U <= offs 0
3121 * l0 V
3122 * l1 U <= offs 4
3123 * ...
3124 */
3125 reg.shademodel = ROGUE_PDSINST_DOUTI_SHADEMODEL_GOURUAD;
3126 reg.f32_offset = 0U;
3127 }
3128
3129 if (sample_3d)
3130 state->usc_coeff_regs = 12U;
3131 else
3132 state->usc_coeff_regs = 8U;
3133
3134 pvr_pds_set_sizes_coeff_loading(&program);
3135
3136 result = pvr_arch_cmd_buffer_alloc_mem(
3137 transfer_cmd->cmd_buffer,
3138 ctx->device->heaps.pds_heap,
3139 PVR_DW_TO_BYTES(program.data_size + program.code_size),
3140 &pvr_bo);
3141 if (result != VK_SUCCESS)
3142 return result;
3143
3144 state->pds_coeff_task_offset =
3145 pvr_bo->dev_addr.addr - ctx->device->heaps.pds_heap->base_addr.addr;
3146
3147 pvr_pds_generate_coeff_loading_program(&program,
3148 pvr_bo_suballoc_get_map_addr(pvr_bo));
3149
3150 state->coeff_data_size = program.data_size;
3151 state->pds_temps = program.temps_used;
3152
3153 return VK_SUCCESS;
3154 }
3155
3156 #define X 0U
3157 #define Y 1U
3158 #define Z 2U
3159
3160 static void pvr_tsp_floats(const struct pvr_device_info *dev_info,
3161 VkRect2D *rect,
3162 const float recips[3U],
3163 bool custom_filter,
3164 bool z_present,
3165 float z_value,
3166 struct pvr_transfer_3d_iteration *layer)
3167 {
3168 #define U0 0U
3169 #define U1 1U
3170 #define V0 2U
3171 #define V1 3U
3172
3173 const uint32_t indices[8U] = { U0, V0, U0, V1, U1, V1, U1, V0 };
3174 float delta[2U] = { 0.0f, 0.0f };
3175 int32_t non_normalized[4U];
3176 uint32_t src_flipped[2U];
3177 uint32_t normalized[4U];
3178 int32_t src_span[2U];
3179
3180 non_normalized[U0] = rect->offset.x;
3181 non_normalized[U1] = rect->offset.x + rect->extent.width;
3182 non_normalized[V0] = rect->offset.y;
3183 non_normalized[V1] = rect->offset.y + rect->extent.height;
3184
3185 /* Filter adjust. */
3186 src_span[X] = rect->extent.width;
3187 src_flipped[X] = src_span[X] > 0U ? 0U : 1U;
3188 src_span[Y] = rect->extent.height;
3189 src_flipped[Y] = src_span[Y] > 0U ? 0U : 1U;
3190 /*
3191 * | X | Y | srcFlipX | srcFlipY |
3192 * +----+----+----------+----------|
3193 * | X | Y | 0 | 0 |
3194 * | -X | Y | 1 | 0 |
3195 * | X | -Y | 0 | 1 |
3196 * | -X | -Y | 1 | 1 |
3197 */
3198 for (uint32_t i = X; i <= Y; i++) {
3199 if (custom_filter) {
3200 if (src_flipped[i] != 0U)
3201 delta[i] += 0.25;
3202 else
3203 delta[i] -= 0.25;
3204 }
3205 }
3206
3207 /* Normalize. */
3208 for (uint32_t i = 0U; i < ARRAY_SIZE(normalized); i++) {
3209 uint32_t tmp;
3210 float ftmp;
3211
3212 ftmp = (float)non_normalized[i] + delta[i >> 1U];
3213 ftmp *= recips[i >> 1U];
3214
3215 tmp = fui(ftmp);
3216 if (!PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format))
3217 tmp = XXH_rotl32(tmp, 1U);
3218
3219 normalized[i] = tmp;
3220 }
3221
3222 /* Apply indices. */
3223 for (uint32_t i = 0U; i < 8U; i++)
3224 layer->texture_coords[i] = normalized[indices[i]];
3225
3226 if (z_present) {
3227 uint32_t tmp = fui(z_value * recips[2U]);
3228
3229 if (!PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format))
3230 tmp = XXH_rotl32(tmp, 1U);
3231
3232 for (uint32_t i = 8U; i < 12U; i++)
3233 layer->texture_coords[i] = tmp;
3234 }
3235
3236 #undef U0
3237 #undef U1
3238 #undef V0
3239 #undef V1
3240 }
3241
3242 static void
3243 pvr_isp_prim_block_tsp_vertex_block(const struct pvr_device_info *dev_info,
3244 const struct pvr_transfer_cmd_source *src,
3245 struct pvr_rect_mapping *mappings,
3246 bool custom_filter,
3247 uint32_t num_mappings,
3248 uint32_t mapping_offset,
3249 uint32_t tsp_comp_format_in_dw,
3250 uint32_t **const cs_ptr_out)
3251 {
3252 struct pvr_transfer_3d_iteration layer;
3253 uint32_t *cs_ptr = *cs_ptr_out;
3254
3255 /* |<-32b->|
3256 * +-------+-----
3257 * | RHW | | X num_isp_vertices
3258 * +-------+-- |
3259 * | U | | |
3260 * | V | | X PVR_TRANSFER_NUM_LAYERS
3261 * +-------+-----
3262 *
3263 * RHW is not there any more in the Transfer. The comment still explains
3264 * where it should go if ever needed.
3265 */
3266 for (uint32_t i = mapping_offset; i < mapping_offset + num_mappings; i++) {
3267 bool z_present = src->surface.mem_layout == PVR_MEMLAYOUT_3DTWIDDLED;
3268 const float recips[3U] = {
3269 [X] = 1.0f / (float)src->surface.width,
3270 [Y] = 1.0f / (float)src->surface.height,
3271 [Z] = z_present ? 1.0f / (float)src->surface.depth : 0.0f,
3272 };
3273 float z_pos = (src->filter < PVR_FILTER_LINEAR)
3274 ? floor(src->surface.z_position) + 0.5f
3275 : src->surface.z_position;
3276
3277 pvr_tsp_floats(dev_info,
3278 &mappings[i].src_rect,
3279 recips,
3280 custom_filter,
3281 z_present,
3282 z_pos,
3283 &layer);
3284
3285 /* We request UVs from TSP for ISP triangle:
3286 * 0 u 1
3287 * +---,
3288 * v| /|
3289 * | / |
3290 * 2'/--'3
3291 */
3292 for (uint32_t j = 0U; j < PVR_TRANSFER_NUM_LAYERS; j++) {
3293 *cs_ptr++ = layer.texture_coords[0U];
3294 *cs_ptr++ = layer.texture_coords[1U];
3295 }
3296
3297 if (z_present) {
3298 *cs_ptr++ = layer.texture_coords[8U];
3299 *cs_ptr++ = 0U;
3300 }
3301
3302 for (uint32_t j = 0U; j < PVR_TRANSFER_NUM_LAYERS; j++) {
3303 *cs_ptr++ = layer.texture_coords[6U];
3304 *cs_ptr++ = layer.texture_coords[7U];
3305 }
3306
3307 if (z_present) {
3308 *cs_ptr++ = layer.texture_coords[11U];
3309 *cs_ptr++ = 0U;
3310 }
3311
3312 for (uint32_t j = 0U; j < PVR_TRANSFER_NUM_LAYERS; j++) {
3313 *cs_ptr++ = layer.texture_coords[2U];
3314 *cs_ptr++ = layer.texture_coords[3U];
3315 }
3316
3317 if (z_present) {
3318 *cs_ptr++ = layer.texture_coords[9U];
3319 *cs_ptr++ = 0U;
3320 }
3321
3322 for (uint32_t j = 0U; j < PVR_TRANSFER_NUM_LAYERS; j++) {
3323 *cs_ptr++ = layer.texture_coords[4U];
3324 *cs_ptr++ = layer.texture_coords[5U];
3325 }
3326
3327 if (z_present) {
3328 *cs_ptr++ = layer.texture_coords[10U];
3329 *cs_ptr++ = 0U;
3330 }
3331 }
3332
3333 if (!PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3334 /* Skipped optional primitive id. */
3335 for (uint32_t i = 0U; i < tsp_comp_format_in_dw; i++)
3336 *cs_ptr++ = 0x88888888U;
3337 } else {
3338 /* Align back to 64 bits. */
3339 if (((uintptr_t)cs_ptr & 7U) != 0U)
3340 cs_ptr++;
3341 }
3342
3343 *cs_ptr_out = cs_ptr;
3344 }
3345
3346 #undef X
3347 #undef Y
3348 #undef Z
3349
3350 static void pvr_isp_prim_block_pds_state(const struct pvr_device_info *dev_info,
3351 struct pvr_transfer_ctx *ctx,
3352 struct pvr_transfer_3d_state *state,
3353 uint32_t **const cs_ptr_out)
3354 {
3355 uint32_t *cs_ptr = *cs_ptr_out;
3356
3357 pvr_csb_pack (cs_ptr, TA_STATE_PDS_SHADERBASE, shader_base) {
3358 shader_base.addr = PVR_DEV_ADDR(state->pds_shader_task_offset);
3359 }
3360 cs_ptr++;
3361
3362 pvr_csb_pack (cs_ptr, TA_STATE_PDS_TEXUNICODEBASE, tex_base) {
3363 tex_base.addr = PVR_DEV_ADDR(state->uni_tex_code_offset);
3364 }
3365 cs_ptr++;
3366
3367 pvr_csb_pack (cs_ptr, TA_STATE_PDS_SIZEINFO1, info1) {
3368 info1.pds_uniformsize =
3369 state->uniform_data_size /
3370 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_UNIFORMSIZE_UNIT_SIZE;
3371
3372 info1.pds_texturestatesize =
3373 state->tex_state_data_size /
3374 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEXTURESTATESIZE_UNIT_SIZE;
3375
3376 info1.pds_varyingsize =
3377 state->coeff_data_size /
3378 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_VARYINGSIZE_UNIT_SIZE;
3379
3380 info1.usc_varyingsize =
3381 ALIGN_POT(state->usc_coeff_regs,
3382 ROGUE_TA_STATE_PDS_SIZEINFO1_USC_VARYINGSIZE_UNIT_SIZE) /
3383 ROGUE_TA_STATE_PDS_SIZEINFO1_USC_VARYINGSIZE_UNIT_SIZE;
3384
3385 info1.pds_tempsize =
3386 ALIGN_POT(state->pds_temps,
3387 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEMPSIZE_UNIT_SIZE) /
3388 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEMPSIZE_UNIT_SIZE;
3389 }
3390 cs_ptr++;
3391
3392 pvr_csb_pack (cs_ptr, TA_STATE_PDS_VARYINGBASE, base) {
3393 base.addr = PVR_DEV_ADDR(state->pds_coeff_task_offset);
3394 }
3395 cs_ptr++;
3396
3397 pvr_csb_pack (cs_ptr, TA_STATE_PDS_TEXTUREDATABASE, base) {
3398 base.addr = PVR_DEV_ADDR(state->tex_state_data_offset);
3399 }
3400 cs_ptr++;
3401
3402 /* PDS uniform program not used. */
3403 pvr_csb_pack (cs_ptr, TA_STATE_PDS_UNIFORMDATABASE, base) {
3404 base.addr = PVR_DEV_ADDR(0U);
3405 }
3406 cs_ptr++;
3407
3408 pvr_csb_pack (cs_ptr, TA_STATE_PDS_SIZEINFO2, info) {
3409 info.usc_sharedsize =
3410 ALIGN_POT(state->common_ptr,
3411 ROGUE_TA_STATE_PDS_SIZEINFO2_USC_SHAREDSIZE_UNIT_SIZE) /
3412 ROGUE_TA_STATE_PDS_SIZEINFO2_USC_SHAREDSIZE_UNIT_SIZE;
3413 info.pds_tri_merge_disable = !PVR_HAS_ENHANCEMENT(dev_info, 42307);
3414 info.pds_batchnum = 0U;
3415 }
3416 cs_ptr++;
3417
3418 /* Get back to 64 bits boundary. */
3419 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format))
3420 cs_ptr++;
3421
3422 *cs_ptr_out = cs_ptr;
3423 }
3424
3425 static void pvr_isp_prim_block_isp_state(const struct pvr_device_info *dev_info,
3426 UNUSED uint32_t tsp_comp_format_in_dw,
3427 uint32_t tsp_data_size_in_bytes,
3428 uint32_t num_isp_vertices,
3429 bool read_bgnd,
3430 uint32_t **const cs_ptr_out)
3431 {
3432 const bool has_simple_internal_parameter_format_v2 =
3433 PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format_v2);
3434 uint32_t *cs_ptr = *cs_ptr_out;
3435
3436 if (has_simple_internal_parameter_format_v2) {
3437 const uint32_t tsp_data_per_vrx_in_bytes =
3438 tsp_data_size_in_bytes / num_isp_vertices;
3439
3440 pvr_csb_pack ((uint64_t *)cs_ptr,
3441 IPF_VERTEX_FORMAT_WORD_SIPF2,
3442 vert_fmt) {
3443 vert_fmt.vf_isp_state_size =
3444 pvr_cmd_length(TA_STATE_ISPCTL) + pvr_cmd_length(TA_STATE_ISPA);
3445
3446 vert_fmt.vf_tsp_vtx_raw = true;
3447 vert_fmt.vf_isp_vtx_raw = true;
3448
3449 vert_fmt.vf_varying_vertex_bits = tsp_data_per_vrx_in_bytes * 8U;
3450 vert_fmt.vf_primitive_total = (num_isp_vertices / 2U) - 1U;
3451 vert_fmt.vf_vertex_total = num_isp_vertices - 1U;
3452 }
3453 cs_ptr += pvr_cmd_length(IPF_VERTEX_FORMAT_WORD_SIPF2);
3454 }
3455
3456 /* ISP state words. */
3457
3458 /* clang-format off */
3459 pvr_csb_pack (cs_ptr, TA_STATE_ISPCTL, ispctl);
3460 /* clang-format on */
3461 cs_ptr += pvr_cmd_length(TA_STATE_ISPCTL);
3462
3463 pvr_csb_pack (cs_ptr, TA_STATE_ISPA, ispa) {
3464 ispa.objtype = ROGUE_TA_OBJTYPE_TRIANGLE;
3465 ispa.passtype = read_bgnd ? ROGUE_TA_PASSTYPE_TRANSLUCENT
3466 : ROGUE_TA_PASSTYPE_OPAQUE;
3467 ispa.dcmpmode = ROGUE_TA_CMPMODE_ALWAYS;
3468 ispa.dwritedisable = true;
3469 }
3470 cs_ptr += pvr_cmd_length(TA_STATE_ISPA);
3471
3472 if (has_simple_internal_parameter_format_v2) {
3473 *cs_ptr_out = cs_ptr;
3474 return;
3475 }
3476
3477 /* How many bytes the TSP compression format needs? */
3478 pvr_csb_pack (cs_ptr, IPF_COMPRESSION_SIZE_WORD, word) {
3479 word.cs_isp_comp_table_size = 0U;
3480 word.cs_tsp_comp_format_size = tsp_comp_format_in_dw;
3481 word.cs_tsp_comp_table_size = 0U;
3482 word.cs_tsp_comp_vertex_size = tsp_data_size_in_bytes / num_isp_vertices;
3483 }
3484 cs_ptr += pvr_cmd_length(IPF_COMPRESSION_SIZE_WORD);
3485
3486 /* ISP vertex compression. */
3487 pvr_csb_pack (cs_ptr, IPF_ISP_COMPRESSION_WORD_0, word0) {
3488 word0.cf_isp_comp_fmt_x0 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3489 word0.cf_isp_comp_fmt_x1 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3490 word0.cf_isp_comp_fmt_x2 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3491 word0.cf_isp_comp_fmt_y0 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3492 word0.cf_isp_comp_fmt_y1 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3493 word0.cf_isp_comp_fmt_y2 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3494 word0.cf_isp_comp_fmt_z0 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3495 word0.cf_isp_comp_fmt_z1 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3496 }
3497 cs_ptr += pvr_cmd_length(IPF_ISP_COMPRESSION_WORD_0);
3498
3499 pvr_csb_pack (cs_ptr, IPF_ISP_COMPRESSION_WORD_1, word1) {
3500 word1.vf_prim_msaa = 0U;
3501 word1.vf_prim_id_pres = 0U;
3502 word1.vf_vertex_clipped = 0U;
3503 word1.vf_vertex_total = num_isp_vertices - 1U;
3504 word1.cf_isp_comp_fmt_z3 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3505 word1.cf_isp_comp_fmt_z2 = ROGUE_IPF_COMPRESSION_FORMAT_RAW_BYTE;
3506 }
3507 cs_ptr += pvr_cmd_length(IPF_ISP_COMPRESSION_WORD_1);
3508
3509 *cs_ptr_out = cs_ptr;
3510 }
3511
3512 static void
3513 pvr_isp_prim_block_index_block(const struct pvr_device_info *dev_info,
3514 uint32_t num_mappings,
3515 uint32_t **const cs_ptr_out)
3516 {
3517 uint32_t *cs_ptr = *cs_ptr_out;
3518
3519 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3520 for (uint32_t i = 0U; i < DIV_ROUND_UP(num_mappings, 2U); i++) {
3521 const uint32_t idx = i * 8U;
3522
3523 pvr_csb_pack ((uint64_t *)cs_ptr,
3524 IPF_INDEX_DATA_WORDS_SIPF,
3525 idx_data_word) {
3526 idx_data_word.ix_triangle3_index_2 = idx + 5U;
3527 idx_data_word.ix_triangle3_index_1 = idx + 6U;
3528 idx_data_word.ix_triangle3_index_0 = idx + 7U;
3529
3530 idx_data_word.ix_triangle2_index_2 = idx + 6U;
3531 idx_data_word.ix_triangle2_index_1 = idx + 5U;
3532 idx_data_word.ix_triangle2_index_0 = idx + 4U;
3533
3534 idx_data_word.ix_triangle1_index_2 = idx + 1U;
3535 idx_data_word.ix_triangle1_index_1 = idx + 2U;
3536 idx_data_word.ix_triangle1_index_0 = idx + 3U;
3537
3538 idx_data_word.ix_triangle0_index_2 = idx + 2U;
3539 idx_data_word.ix_triangle0_index_1 = idx + 1U;
3540 idx_data_word.ix_triangle0_index_0 = idx + 0U;
3541 }
3542 cs_ptr += pvr_cmd_length(IPF_INDEX_DATA_WORDS_SIPF);
3543 }
3544
3545 *cs_ptr_out = cs_ptr;
3546 return;
3547 }
3548
3549 for (uint32_t i = 0U, j = 0U; i < num_mappings; i++, j += 4U) {
3550 if ((i & 1U) == 0U) {
3551 pvr_csb_pack (cs_ptr, IPF_INDEX_DATA, word) {
3552 word.ix_index0_0 = j;
3553 word.ix_index0_1 = j + 1U;
3554 word.ix_index0_2 = j + 2U;
3555 word.ix_index1_0 = j + 3U;
3556 }
3557 cs_ptr += pvr_cmd_length(IPF_INDEX_DATA);
3558
3559 /* Don't increment cs_ptr here. IPF_INDEX_DATA is patched in the
3560 * else part and then cs_ptr is incremented.
3561 */
3562 pvr_csb_pack (cs_ptr, IPF_INDEX_DATA, word) {
3563 word.ix_index0_0 = j + 2U;
3564 word.ix_index0_1 = j + 1U;
3565 }
3566 } else {
3567 uint32_t tmp;
3568
3569 pvr_csb_pack (&tmp, IPF_INDEX_DATA, word) {
3570 word.ix_index0_2 = j;
3571 word.ix_index1_0 = j + 1U;
3572 }
3573 *cs_ptr |= tmp;
3574 cs_ptr += pvr_cmd_length(IPF_INDEX_DATA);
3575
3576 pvr_csb_pack (cs_ptr, IPF_INDEX_DATA, word) {
3577 word.ix_index0_0 = j + 2U;
3578 word.ix_index0_1 = j + 3U;
3579 word.ix_index0_2 = j + 2U;
3580 word.ix_index1_0 = j + 1U;
3581 }
3582 cs_ptr += pvr_cmd_length(IPF_INDEX_DATA);
3583 }
3584 }
3585
3586 /* The last pass didn't ++. */
3587 if ((num_mappings & 1U) != 0U)
3588 cs_ptr++;
3589
3590 *cs_ptr_out = cs_ptr;
3591 }
3592
3593 /* Calculates a 24 bit fixed point (biased) representation of a signed integer.
3594 */
3595 static inline VkResult
3596 pvr_int32_to_isp_xy_vtx(const struct pvr_device_info *dev_info,
3597 int32_t val,
3598 bool bias,
3599 uint32_t *word_out)
3600 {
3601 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3602 const uint32_t max_fractional = ROGUE_IPF_ISP_VERTEX_XY_SIPF_FRAC_MAX_VAL;
3603 const uint32_t max_integer = ROGUE_IPF_ISP_VERTEX_XY_SIPF_INTEGER_MAX_VAL;
3604
3605 uint32_t fractional;
3606 uint32_t integer;
3607
3608 if (bias)
3609 val += ROGUE_IPF_ISP_VERTEX_XY_BIAS_VALUE_SIPF;
3610
3611 if (val < 0 || val > max_integer + 1) {
3612 mesa_loge("ISP vertex xy value out of range.");
3613 return vk_error(NULL, VK_ERROR_UNKNOWN);
3614 }
3615
3616 if (val <= max_integer) {
3617 integer = val;
3618 fractional = 0;
3619 } else if (val == max_integer + 1) {
3620 /* The integer field is 13 bits long so the max value is
3621 * 2 ^ 13 - 1 = 8191. For 8k support we need to handle 8192 so we set
3622 * all fractional bits to get as close as possible. The best we can do
3623 * is: 0x1FFF.F = 8191.9375 ≈ 8192 .
3624 */
3625 integer = max_integer;
3626 fractional = max_fractional;
3627 }
3628
3629 pvr_csb_pack (word_out, IPF_ISP_VERTEX_XY_SIPF, word) {
3630 word.integer = integer;
3631 word.frac = fractional;
3632 }
3633
3634 return VK_SUCCESS;
3635 }
3636
3637 val += ROGUE_IPF_ISP_VERTEX_XY_BIAS_VALUE;
3638
3639 if (((uint32_t)val & 0x7fff8000U) != 0U)
3640 return vk_error(NULL, VK_ERROR_UNKNOWN);
3641
3642 pvr_csb_pack (word_out, IPF_ISP_VERTEX_XY, word) {
3643 word.sign = val < 0;
3644 word.integer = val;
3645 }
3646
3647 return VK_SUCCESS;
3648 }
3649
3650 static VkResult
3651 pvr_isp_prim_block_isp_vertices(const struct pvr_device_info *dev_info,
3652 struct pvr_transfer_3d_state *state,
3653 struct pvr_rect_mapping *mappings,
3654 uint32_t num_mappings,
3655 uint32_t mapping_offset,
3656 uint32_t **const cs_ptr_out)
3657 {
3658 uint32_t *cs_ptr = *cs_ptr_out;
3659 bool bias = true;
3660 uint32_t i;
3661
3662 if (PVR_HAS_FEATURE(dev_info, screen_size8K))
3663 bias = state->width_in_tiles <= 256U && state->height_in_tiles <= 256U;
3664
3665 for (i = mapping_offset; i < mapping_offset + num_mappings; i++) {
3666 uint32_t bottom = 0U;
3667 uint32_t right = 0U;
3668 uint32_t left = 0U;
3669 uint32_t top = 0U;
3670 VkResult result;
3671
3672 /* ISP vertex data (X, Y, Z). */
3673 result = pvr_int32_to_isp_xy_vtx(dev_info,
3674 mappings[i].dst_rect.offset.y,
3675 bias,
3676 &top);
3677 if (result != VK_SUCCESS)
3678 return result;
3679
3680 result = pvr_int32_to_isp_xy_vtx(dev_info,
3681 mappings[i].dst_rect.offset.y +
3682 mappings[i].dst_rect.extent.height,
3683 bias,
3684 &bottom);
3685 if (result != VK_SUCCESS)
3686 return result;
3687
3688 result = pvr_int32_to_isp_xy_vtx(dev_info,
3689 mappings[i].dst_rect.offset.x,
3690 bias,
3691 &left);
3692 if (result != VK_SUCCESS)
3693 return result;
3694
3695 result = pvr_int32_to_isp_xy_vtx(dev_info,
3696 mappings[i].dst_rect.offset.x +
3697 mappings[i].dst_rect.extent.width,
3698 bias,
3699 &right);
3700 if (result != VK_SUCCESS)
3701 return result;
3702
3703 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3704 pvr_csb_pack ((uint64_t *)cs_ptr, IPF_ISP_VERTEX_WORD_SIPF, word) {
3705 word.y = top;
3706 word.x = left;
3707 }
3708 cs_ptr += pvr_cmd_length(IPF_ISP_VERTEX_WORD_SIPF);
3709
3710 pvr_csb_pack ((uint64_t *)cs_ptr, IPF_ISP_VERTEX_WORD_SIPF, word) {
3711 word.y = top;
3712 word.x = right;
3713 }
3714 cs_ptr += pvr_cmd_length(IPF_ISP_VERTEX_WORD_SIPF);
3715
3716 pvr_csb_pack ((uint64_t *)cs_ptr, IPF_ISP_VERTEX_WORD_SIPF, word) {
3717 word.y = bottom;
3718 word.x = left;
3719 }
3720 cs_ptr += pvr_cmd_length(IPF_ISP_VERTEX_WORD_SIPF);
3721
3722 pvr_csb_pack ((uint64_t *)cs_ptr, IPF_ISP_VERTEX_WORD_SIPF, word) {
3723 word.y = bottom;
3724 word.x = right;
3725 }
3726 cs_ptr += pvr_cmd_length(IPF_ISP_VERTEX_WORD_SIPF);
3727
3728 continue;
3729 }
3730
3731 /* ISP vertices 0 and 1. */
3732 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_0, word0) {
3733 word0.x0 = left;
3734 word0.y0 = top & 0xFF;
3735 }
3736 cs_ptr++;
3737
3738 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_1, word1) {
3739 word1.y0 = top >> ROGUE_IPF_ISP_VERTEX_WORD_1_Y0_SHIFT;
3740 }
3741 cs_ptr++;
3742
3743 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_2, word2) {
3744 word2.x1 = right & 0xFFFF;
3745 word2.z0 = 0U;
3746 }
3747 cs_ptr++;
3748
3749 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_3, word3) {
3750 word3.x1 = right >> ROGUE_IPF_ISP_VERTEX_WORD_3_X1_SHIFT;
3751 word3.y1 = top;
3752 }
3753 cs_ptr++;
3754
3755 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_4, word4) {
3756 word4.z1 = 0U;
3757 }
3758 cs_ptr++;
3759
3760 /* ISP vertices 2 and 3. */
3761 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_0, word0) {
3762 word0.x0 = left;
3763 word0.y0 = bottom & 0xFF;
3764 }
3765 cs_ptr++;
3766
3767 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_1, word1) {
3768 word1.y0 = bottom >> ROGUE_IPF_ISP_VERTEX_WORD_1_Y0_SHIFT;
3769 }
3770 cs_ptr++;
3771
3772 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_2, word2) {
3773 word2.x1 = right & 0xFFFF;
3774 word2.z0 = 0U;
3775 }
3776 cs_ptr++;
3777
3778 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_3, word3) {
3779 word3.x1 = right >> ROGUE_IPF_ISP_VERTEX_WORD_3_X1_SHIFT;
3780 word3.y1 = bottom;
3781 }
3782 cs_ptr++;
3783
3784 pvr_csb_pack (cs_ptr, IPF_ISP_VERTEX_WORD_4, word4) {
3785 word4.z1 = 0U;
3786 }
3787 cs_ptr++;
3788 }
3789 *cs_ptr_out = cs_ptr;
3790
3791 return VK_SUCCESS;
3792 }
3793
3794 static uint32_t
3795 pvr_isp_primitive_block_size(const struct pvr_device_info *dev_info,
3796 const struct pvr_transfer_cmd_source *src,
3797 uint32_t num_mappings)
3798 {
3799 uint32_t num_isp_vertices = num_mappings * 4U;
3800 uint32_t num_tsp_vertices_per_isp_vertex;
3801 uint32_t isp_vertex_data_size_dw;
3802 bool color_fill = (src == NULL);
3803 uint32_t tsp_comp_format_dw;
3804 uint32_t isp_state_size_dw;
3805 uint32_t pds_state_size_dw;
3806 uint32_t idx_data_size_dw;
3807 uint32_t tsp_data_size;
3808 uint32_t stream_size;
3809
3810 if (color_fill) {
3811 num_tsp_vertices_per_isp_vertex = 0U;
3812 } else {
3813 num_tsp_vertices_per_isp_vertex =
3814 src->surface.mem_layout == PVR_MEMLAYOUT_3DTWIDDLED ? 4U : 2U;
3815 }
3816
3817 tsp_data_size = PVR_DW_TO_BYTES(num_isp_vertices * PVR_TRANSFER_NUM_LAYERS *
3818 num_tsp_vertices_per_isp_vertex);
3819
3820 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3821 /* An XYZ vertex is 16/16/32 bits => 8 bytes. */
3822 isp_vertex_data_size_dw = num_isp_vertices * 2U;
3823
3824 /* Round to even for 64 bit boundary. */
3825 idx_data_size_dw = ALIGN_POT(num_mappings, 2U);
3826 tsp_comp_format_dw = 0U;
3827 isp_state_size_dw = 4U;
3828 pds_state_size_dw = 8U;
3829 } else {
3830 tsp_comp_format_dw = color_fill ? 0U : PVR_TRANSFER_NUM_LAYERS;
3831
3832 if (!color_fill) {
3833 if (src->surface.mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)
3834 tsp_comp_format_dw *= 2U;
3835 }
3836
3837 /* An XYZ vertex is 24/24/32 bits => 10 bytes with last padded to 4 byte
3838 * burst align.
3839 */
3840 isp_vertex_data_size_dw = DIV_ROUND_UP(num_isp_vertices * 10U, 4U);
3841
3842 /* 4 triangles fit in 3 dw: t0t0t0t1_t1t1t2t2_t2t3t3t3. */
3843 idx_data_size_dw = num_mappings + DIV_ROUND_UP(num_mappings, 2U);
3844 isp_state_size_dw = 5U;
3845 pds_state_size_dw = 7U;
3846 }
3847
3848 stream_size =
3849 tsp_data_size + PVR_DW_TO_BYTES(idx_data_size_dw + tsp_comp_format_dw +
3850 isp_vertex_data_size_dw +
3851 isp_state_size_dw + pds_state_size_dw);
3852
3853 return stream_size;
3854 }
3855
3856 static VkResult
3857 pvr_isp_primitive_block(const struct pvr_device_info *dev_info,
3858 struct pvr_transfer_ctx *ctx,
3859 const struct pvr_transfer_cmd *transfer_cmd,
3860 struct pvr_transfer_prep_data *prep_data,
3861 const struct pvr_transfer_cmd_source *src,
3862 bool custom_filter,
3863 struct pvr_rect_mapping *mappings,
3864 uint32_t num_mappings,
3865 uint32_t mapping_offset,
3866 bool read_bgnd,
3867 uint32_t *cs_start_offset,
3868 uint32_t **cs_ptr_out)
3869 {
3870 struct pvr_transfer_3d_state *state = &prep_data->state;
3871 uint32_t num_isp_vertices = num_mappings * 4U;
3872 uint32_t num_tsp_vertices_per_isp_vert;
3873 uint32_t tsp_data_size_in_bytes;
3874 uint32_t tsp_comp_format_in_dw;
3875 bool color_fill = src == NULL;
3876 uint32_t stream_size_in_bytes;
3877 uint32_t *cs_ptr_start;
3878 VkResult result;
3879
3880 if (color_fill) {
3881 num_tsp_vertices_per_isp_vert = 0U;
3882 } else {
3883 num_tsp_vertices_per_isp_vert =
3884 src->surface.mem_layout == PVR_MEMLAYOUT_3DTWIDDLED ? 4U : 2U;
3885 }
3886
3887 tsp_data_size_in_bytes =
3888 PVR_DW_TO_BYTES(num_isp_vertices * PVR_TRANSFER_NUM_LAYERS *
3889 num_tsp_vertices_per_isp_vert);
3890
3891 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3892 tsp_comp_format_in_dw = 0U;
3893 } else {
3894 tsp_comp_format_in_dw = color_fill ? 0U : PVR_TRANSFER_NUM_LAYERS;
3895
3896 if (!color_fill && src->surface.mem_layout == PVR_MEMLAYOUT_3DTWIDDLED)
3897 tsp_comp_format_in_dw *= 2U;
3898 }
3899
3900 stream_size_in_bytes =
3901 pvr_isp_primitive_block_size(dev_info, src, num_mappings);
3902
3903 cs_ptr_start = *cs_ptr_out;
3904
3905 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
3906 /* This includes:
3907 * Vertex formats.
3908 * ISP state words.
3909 */
3910 pvr_isp_prim_block_isp_state(dev_info,
3911 tsp_comp_format_in_dw,
3912 tsp_data_size_in_bytes,
3913 num_isp_vertices,
3914 read_bgnd,
3915 cs_ptr_out);
3916
3917 /* This include:
3918 * Index data / point pitch.
3919 */
3920 pvr_isp_prim_block_index_block(dev_info, num_mappings, cs_ptr_out);
3921
3922 result = pvr_isp_prim_block_isp_vertices(dev_info,
3923 state,
3924 mappings,
3925 num_mappings,
3926 mapping_offset,
3927 cs_ptr_out);
3928 if (result != VK_SUCCESS)
3929 return result;
3930
3931 pvr_isp_prim_block_pds_state(dev_info, ctx, state, cs_ptr_out);
3932
3933 if (!color_fill) {
3934 /* This includes:
3935 * TSP vertex formats.
3936 */
3937 pvr_isp_prim_block_tsp_vertex_block(dev_info,
3938 src,
3939 mappings,
3940 custom_filter,
3941 num_mappings,
3942 mapping_offset,
3943 tsp_comp_format_in_dw,
3944 cs_ptr_out);
3945 }
3946
3947 *cs_start_offset = 0;
3948 } else {
3949 if (!color_fill) {
3950 /* This includes:
3951 * Compressed TSP vertex data & tables.
3952 * Primitive id.
3953 * TSP compression formats.
3954 */
3955 pvr_isp_prim_block_tsp_vertex_block(dev_info,
3956 src,
3957 mappings,
3958 custom_filter,
3959 num_mappings,
3960 mapping_offset,
3961 tsp_comp_format_in_dw,
3962 cs_ptr_out);
3963 }
3964
3965 pvr_isp_prim_block_pds_state(dev_info, ctx, state, cs_ptr_out);
3966
3967 /* Point the CS_PRIM_BASE here. */
3968 *cs_start_offset = (*cs_ptr_out - cs_ptr_start) * sizeof(cs_ptr_start[0]);
3969
3970 /* This includes:
3971 * ISP state words.
3972 * Compression size word.
3973 * ISP compression and vertex formats.
3974 */
3975 pvr_isp_prim_block_isp_state(dev_info,
3976 tsp_comp_format_in_dw,
3977 tsp_data_size_in_bytes,
3978 num_isp_vertices,
3979 read_bgnd,
3980 cs_ptr_out);
3981
3982 pvr_isp_prim_block_index_block(dev_info, num_mappings, cs_ptr_out);
3983
3984 result = pvr_isp_prim_block_isp_vertices(dev_info,
3985 state,
3986 mappings,
3987 num_mappings,
3988 mapping_offset,
3989 cs_ptr_out);
3990 if (result != VK_SUCCESS)
3991 return result;
3992 }
3993
3994 assert((*cs_ptr_out - cs_ptr_start) * sizeof(cs_ptr_start[0]) ==
3995 stream_size_in_bytes);
3996
3997 return VK_SUCCESS;
3998 }
3999
4000 static inline uint32_t
4001 pvr_transfer_prim_blocks_per_alloc(const struct pvr_device_info *dev_info)
4002 {
4003 uint32_t ret = PVR_DW_TO_BYTES(ROGUE_IPF_CONTROL_STREAM_SIZE_DWORDS);
4004
4005 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format))
4006 return ret / sizeof(uint64_t) / 2U;
4007
4008 return ret / sizeof(uint32_t) / 2U - 1U;
4009 }
4010
4011 static inline uint32_t
4012 pvr_transfer_max_quads_per_pb(const struct pvr_device_info *dev_info)
4013 {
4014 return PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format) ? 4U
4015 : 16U;
4016 }
4017
4018 static inline uint8_t *pvr_isp_ctrl_stream_sipf_write_aligned(uint8_t *stream,
4019 uint32_t data,
4020 uint32_t size)
4021 {
4022 const uint32_t offset = (uintptr_t)stream & 0x3U;
4023 uint32_t *aligned_stream = (uint32_t *)(stream - offset);
4024 const uint32_t current_data = *aligned_stream & ((1U << (offset * 8U)) - 1U);
4025
4026 assert(size > 0 && size <= 4U);
4027
4028 *aligned_stream = current_data | data << (offset * 8U);
4029
4030 if (offset + size > 4U) {
4031 aligned_stream++;
4032 *aligned_stream = data >> ((4U - offset) * 8);
4033 }
4034
4035 return stream + size;
4036 }
4037
4038 /**
4039 * Writes ISP ctrl stream.
4040 *
4041 * We change sampler/texture state when we process a new TQ source. The
4042 * primitive block contains the shader pointers, but we supply the primitive
4043 * blocks with shaders from here.
4044 */
4045 static VkResult pvr_isp_ctrl_stream(const struct pvr_device_info *dev_info,
4046 struct pvr_transfer_ctx *ctx,
4047 struct pvr_transfer_cmd *transfer_cmd,
4048 struct pvr_transfer_prep_data *prep_data)
4049 {
4050 const uint32_t max_mappings_per_pb = pvr_transfer_max_quads_per_pb(dev_info);
4051 bool fill_blit = (transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_FILL) != 0U;
4052 uint32_t free_ctrl_stream_words = ROGUE_IPF_CONTROL_STREAM_SIZE_DWORDS;
4053 struct pvr_transfer_3d_state *const state = &prep_data->state;
4054 struct pvr_winsys_transfer_regs *const regs = &state->regs;
4055 struct pvr_transfer_pass *pass = NULL;
4056 uint32_t flags = transfer_cmd->flags;
4057 struct pvr_suballoc_bo *pvr_cs_bo;
4058 pvr_dev_addr_t stream_base_vaddr;
4059 uint32_t num_prim_blks = 0U;
4060 uint32_t prim_blk_size = 0U;
4061 uint32_t region_arrays_size;
4062 uint32_t num_region_arrays;
4063 uint32_t total_stream_size;
4064 bool was_linked = false;
4065 uint32_t rem_mappings;
4066 uint32_t num_sources;
4067 uint32_t *blk_cs_ptr;
4068 uint32_t *cs_ptr;
4069 uint32_t source;
4070 VkResult result;
4071
4072 if (state->custom_mapping.pass_count > 0U) {
4073 pass = &state->custom_mapping.passes[state->pass_idx];
4074
4075 num_sources = pass->source_count;
4076
4077 for (source = 0; source < num_sources; source++) {
4078 uint32_t num_mappings = pass->sources[source].mapping_count;
4079
4080 while (num_mappings > 0U) {
4081 if (fill_blit) {
4082 prim_blk_size += pvr_isp_primitive_block_size(
4083 dev_info,
4084 NULL,
4085 MIN2(max_mappings_per_pb, num_mappings));
4086 }
4087
4088 if (transfer_cmd->source_count > 0) {
4089 prim_blk_size += pvr_isp_primitive_block_size(
4090 dev_info,
4091 &transfer_cmd->sources[source],
4092 MIN2(max_mappings_per_pb, num_mappings));
4093 }
4094
4095 num_mappings -= MIN2(max_mappings_per_pb, num_mappings);
4096 num_prim_blks++;
4097 }
4098 }
4099 } else {
4100 num_sources = fill_blit ? 1U : transfer_cmd->source_count;
4101
4102 if (fill_blit) {
4103 num_prim_blks = 1U;
4104 prim_blk_size +=
4105 pvr_isp_primitive_block_size(dev_info,
4106 NULL,
4107 MIN2(max_mappings_per_pb, 1U));
4108
4109 /* Fill blits can also have a source; fallthrough to handle. */
4110 }
4111
4112 for (source = 0; source < transfer_cmd->source_count; source++) {
4113 uint32_t num_mappings = transfer_cmd->sources[source].mapping_count;
4114
4115 while (num_mappings > 0U) {
4116 prim_blk_size += pvr_isp_primitive_block_size(
4117 dev_info,
4118 &transfer_cmd->sources[source],
4119 MIN2(max_mappings_per_pb, num_mappings));
4120
4121 num_mappings -= MIN2(max_mappings_per_pb, num_mappings);
4122 num_prim_blks++;
4123 }
4124 }
4125 }
4126
4127 num_region_arrays =
4128 (num_prim_blks + (pvr_transfer_prim_blocks_per_alloc(dev_info) - 1U)) /
4129 pvr_transfer_prim_blocks_per_alloc(dev_info);
4130 region_arrays_size = ROGUE_IPF_CONTROL_STREAM_SIZE_DWORDS *
4131 sizeof(uint32_t) * num_region_arrays;
4132 total_stream_size = region_arrays_size + prim_blk_size;
4133
4134 /* Allocate space for IPF control stream. */
4135 result = pvr_arch_cmd_buffer_alloc_mem(transfer_cmd->cmd_buffer,
4136 ctx->device->heaps.transfer_frag_heap,
4137 total_stream_size,
4138 &pvr_cs_bo);
4139 if (result != VK_SUCCESS)
4140 return result;
4141
4142 stream_base_vaddr =
4143 PVR_DEV_ADDR(pvr_cs_bo->dev_addr.addr -
4144 ctx->device->heaps.transfer_frag_heap->base_addr.addr);
4145
4146 cs_ptr = pvr_bo_suballoc_get_map_addr(pvr_cs_bo);
4147 blk_cs_ptr = cs_ptr + region_arrays_size / sizeof(uint32_t);
4148
4149 source = 0;
4150 while (source < num_sources) {
4151 if (fill_blit)
4152 rem_mappings = pass ? pass->sources[source].mapping_count : 1U;
4153 else
4154 rem_mappings = transfer_cmd->sources[source].mapping_count;
4155
4156 if ((transfer_cmd->source_count > 0 || fill_blit) && rem_mappings != 0U) {
4157 struct pvr_pds_pixel_shader_sa_program unitex_pds_prog = { 0U };
4158 struct pvr_transfer_cmd_source *src = &transfer_cmd->sources[source];
4159 struct pvr_rect_mapping fill_mapping;
4160 uint32_t mapping_offset = 0U;
4161 bool read_bgnd = false;
4162
4163 if (fill_blit) {
4164 uint32_t packed_color[4U] = { 0U };
4165
4166 if (vk_format_is_compressed(transfer_cmd->dst.vk_format)) {
4167 return vk_error(transfer_cmd->cmd_buffer,
4168 VK_ERROR_FORMAT_NOT_SUPPORTED);
4169 }
4170
4171 state->pds_shader_task_offset = 0U;
4172 state->uni_tex_code_offset = 0U;
4173 state->tex_state_data_offset = 0U;
4174 state->common_ptr = 0U;
4175
4176 result = pvr_pack_clear_color(transfer_cmd->dst.vk_format,
4177 transfer_cmd->clear_color,
4178 packed_color);
4179 if (result != VK_SUCCESS)
4180 return result;
4181
4182 fill_mapping.dst_rect = transfer_cmd->scissor;
4183
4184 pvr_csb_pack (&regs->usc_clear_register0,
4185 CR_USC_CLEAR_REGISTER,
4186 reg) {
4187 reg.val = packed_color[0U];
4188 }
4189
4190 pvr_csb_pack (&regs->usc_clear_register1,
4191 CR_USC_CLEAR_REGISTER,
4192 reg) {
4193 reg.val = packed_color[1U];
4194 }
4195
4196 pvr_csb_pack (&regs->usc_clear_register2,
4197 CR_USC_CLEAR_REGISTER,
4198 reg) {
4199 reg.val = packed_color[2U];
4200 }
4201
4202 pvr_csb_pack (&regs->usc_clear_register3,
4203 CR_USC_CLEAR_REGISTER,
4204 reg) {
4205 reg.val = packed_color[3U];
4206 }
4207
4208 state->pds_shader_task_offset =
4209 transfer_cmd->cmd_buffer->device->nop_program.pds.data_offset;
4210
4211 unitex_pds_prog.kick_usc = false;
4212 unitex_pds_prog.clear = false;
4213 } else {
4214 const bool down_scale = transfer_cmd->sources[source].resolve_op ==
4215 PVR_RESOLVE_BLEND &&
4216 src->surface.sample_count > 1U &&
4217 transfer_cmd->dst.sample_count <= 1U;
4218 struct pvr_tq_shader_properties *shader_props =
4219 &state->shader_props;
4220 struct pvr_tq_layer_properties *layer = &shader_props->layer_props;
4221 const struct pvr_tq_frag_sh_reg_layout *sh_reg_layout;
4222 enum pvr_transfer_pbe_pixel_src pbe_src_format;
4223 struct pvr_suballoc_bo *pvr_bo;
4224 uint32_t tex_state_dma_size;
4225 pvr_dev_addr_t dev_offset;
4226
4227 /* Reset the shared register bank ptrs each src implies new texture
4228 * state (Note that we don't change texture state per prim block).
4229 */
4230 state->common_ptr = 0U;
4231 state->usc_const_reg_ptr = 0U;
4232 /* We don't use state->dynamic_const_reg_ptr here. */
4233
4234 if (flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE)
4235 read_bgnd = true;
4236
4237 result = pvr_pbe_src_format_f2d(flags,
4238 src,
4239 transfer_cmd->dst.vk_format,
4240 down_scale,
4241 state->dont_force_pbe,
4242 &pbe_src_format);
4243 if (result != VK_SUCCESS)
4244 return result;
4245
4246 memset(shader_props, 0U, sizeof(*shader_props));
4247
4248 layer->pbe_format = pbe_src_format;
4249 layer->sample =
4250 (src->surface.mem_layout == PVR_MEMLAYOUT_3DTWIDDLED);
4251 shader_props->iterated = true;
4252
4253 shader_props->pick_component =
4254 pvr_pick_component_needed(&state->custom_mapping);
4255
4256 result = pvr_msaa_state(dev_info, transfer_cmd, state, source);
4257 if (result != VK_SUCCESS)
4258 return result;
4259
4260 if (state->filter[source] == PVR_FILTER_LINEAR &&
4261 pvr_requires_usc_linear_filter(src->surface.vk_format)) {
4262 if (pvr_int_pbe_usc_linear_filter(layer->pbe_format,
4263 layer->sample,
4264 layer->msaa,
4265 shader_props->full_rate)) {
4266 layer->linear = true;
4267 } else {
4268 mesa_logw("Transfer: F32 linear filter not supported.");
4269 }
4270 }
4271
4272 result = pvr_transfer_frag_store_get_shader_info(
4273 transfer_cmd->cmd_buffer->device,
4274 &ctx->frag_store,
4275 shader_props,
4276 &dev_offset,
4277 &sh_reg_layout);
4278 if (result != VK_SUCCESS)
4279 return result;
4280
4281 assert(dev_offset.addr <= UINT32_MAX);
4282 prep_data->state.pds_shader_task_offset = (uint32_t)dev_offset.addr;
4283
4284 result =
4285 pvr_pds_coeff_task(ctx, transfer_cmd, layer->sample, prep_data);
4286 if (result != VK_SUCCESS)
4287 return result;
4288
4289 unitex_pds_prog.kick_usc = false;
4290 unitex_pds_prog.clear = false;
4291
4292 tex_state_dma_size =
4293 sh_reg_layout->driver_total + sh_reg_layout->compiler_out_total;
4294
4295 unitex_pds_prog.num_texture_dma_kicks = 1U;
4296 unitex_pds_prog.num_uniform_dma_kicks = 0U;
4297
4298 /* Allocate memory for DMA. */
4299 result =
4300 pvr_arch_cmd_buffer_alloc_mem(transfer_cmd->cmd_buffer,
4301 ctx->device->heaps.general_heap,
4302 tex_state_dma_size << 2U,
4303 &pvr_bo);
4304 if (result != VK_SUCCESS)
4305 return result;
4306
4307 result = pvr_sampler_state_for_surface(
4308 dev_info,
4309 &transfer_cmd->sources[source].surface,
4310 state->filter[source],
4311 sh_reg_layout,
4312 0U,
4313 layer->linear,
4314 pvr_bo_suballoc_get_map_addr(pvr_bo));
4315 if (result != VK_SUCCESS)
4316 return result;
4317
4318 result = pvr_image_state_for_surface(
4319 ctx,
4320 transfer_cmd,
4321 &transfer_cmd->sources[source].surface,
4322 0U,
4323 source,
4324 sh_reg_layout,
4325 state,
4326 0U,
4327 pvr_bo_suballoc_get_map_addr(pvr_bo));
4328 if (result != VK_SUCCESS)
4329 return result;
4330
4331 pvr_pds_encode_dma_burst(unitex_pds_prog.texture_dma_control,
4332 unitex_pds_prog.texture_dma_address,
4333 state->common_ptr,
4334 tex_state_dma_size,
4335 pvr_bo->dev_addr.addr,
4336 true,
4337 dev_info);
4338
4339 state->common_ptr += tex_state_dma_size;
4340
4341 pvr_write_usc_constants(sh_reg_layout,
4342 pvr_bo_suballoc_get_map_addr(pvr_bo));
4343
4344 if (pvr_pick_component_needed(&state->custom_mapping)) {
4345 pvr_dma_texel_unwind(state,
4346 sh_reg_layout,
4347 pvr_bo_suballoc_get_map_addr(pvr_bo));
4348 }
4349 }
4350
4351 result = pvr_pds_unitex(dev_info,
4352 ctx,
4353 transfer_cmd,
4354 &unitex_pds_prog,
4355 prep_data);
4356 if (result != VK_SUCCESS)
4357 return result;
4358
4359 while (rem_mappings > 0U) {
4360 const uint32_t min_free_ctrl_stream_words =
4361 PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format) ? 2
4362 : 3;
4363 const uint32_t num_mappings =
4364 MIN2(max_mappings_per_pb, rem_mappings);
4365 struct pvr_rect_mapping *mappings = NULL;
4366 uint32_t stream_start_offset = 0U;
4367 pvr_dev_addr_t prim_blk_addr;
4368
4369 if (free_ctrl_stream_words < min_free_ctrl_stream_words) {
4370 pvr_dev_addr_t next_region_array_vaddr = stream_base_vaddr;
4371
4372 num_region_arrays++;
4373 next_region_array_vaddr.addr +=
4374 num_region_arrays *
4375 PVR_DW_TO_BYTES(ROGUE_IPF_CONTROL_STREAM_SIZE_DWORDS);
4376
4377 if (PVR_HAS_FEATURE(dev_info,
4378 simple_internal_parameter_format_v2)) {
4379 uint32_t link_addr;
4380
4381 pvr_csb_pack (&link_addr,
4382 IPF_CONTROL_STREAM_LINK_SIPF2,
4383 control_stream) {
4384 control_stream.cs_ctrl_type =
4385 ROGUE_IPF_CS_CTRL_TYPE_SIPF2_LINK;
4386 control_stream.cs_link.addr = next_region_array_vaddr.addr;
4387 }
4388
4389 pvr_isp_ctrl_stream_sipf_write_aligned(
4390 (uint8_t *)cs_ptr,
4391 link_addr,
4392 PVR_DW_TO_BYTES(
4393 pvr_cmd_length(IPF_CONTROL_STREAM_LINK_SIPF2)));
4394 } else {
4395 pvr_csb_pack (cs_ptr, IPF_CONTROL_STREAM, control_stream) {
4396 control_stream.cs_type = ROGUE_IPF_CS_TYPE_LINK;
4397 control_stream.cs_link.addr = next_region_array_vaddr.addr;
4398 }
4399 }
4400
4401 cs_ptr =
4402 (uint32_t *)pvr_bo_suballoc_get_map_addr(pvr_cs_bo) +
4403 num_region_arrays * ROGUE_IPF_CONTROL_STREAM_SIZE_DWORDS;
4404 free_ctrl_stream_words = ROGUE_IPF_CONTROL_STREAM_SIZE_DWORDS;
4405
4406 was_linked = PVR_HAS_FEATURE(dev_info, ipf_creq_pf);
4407 }
4408
4409 if (fill_blit)
4410 mappings = pass ? pass->sources[source].mappings : &fill_mapping;
4411 else
4412 mappings = transfer_cmd->sources[source].mappings;
4413
4414 prim_blk_addr = stream_base_vaddr;
4415 prim_blk_addr.addr +=
4416 (uintptr_t)blk_cs_ptr -
4417 (uintptr_t)pvr_bo_suballoc_get_map_addr(pvr_cs_bo);
4418
4419 result = pvr_isp_primitive_block(dev_info,
4420 ctx,
4421 transfer_cmd,
4422 prep_data,
4423 fill_blit ? NULL : src,
4424 state->custom_filter,
4425 mappings,
4426 num_mappings,
4427 mapping_offset,
4428 read_bgnd,
4429 &stream_start_offset,
4430 &blk_cs_ptr);
4431 if (result != VK_SUCCESS)
4432 return result;
4433
4434 prim_blk_addr.addr += stream_start_offset;
4435
4436 if (PVR_HAS_FEATURE(dev_info,
4437 simple_internal_parameter_format_v2)) {
4438 uint8_t *cs_byte_ptr = (uint8_t *)cs_ptr;
4439 uint32_t tmp;
4440
4441 /* This part of the control stream is byte granular. */
4442
4443 pvr_csb_pack (&tmp, IPF_PRIMITIVE_HEADER_SIPF2, prim_header) {
4444 prim_header.cs_prim_base_size = 1;
4445 prim_header.cs_mask_num_bytes = 1;
4446 prim_header.cs_valid_tile0 = true;
4447 }
4448 cs_byte_ptr =
4449 pvr_isp_ctrl_stream_sipf_write_aligned(cs_byte_ptr, tmp, 1);
4450
4451 pvr_csb_pack (&tmp, IPF_PRIMITIVE_BASE_SIPF2, word) {
4452 word.cs_prim_base = prim_blk_addr;
4453 }
4454 cs_byte_ptr =
4455 pvr_isp_ctrl_stream_sipf_write_aligned(cs_byte_ptr, tmp, 4);
4456
4457 /* IPF_BYTE_BASED_MASK_ONE_BYTE_WORD_0_SIPF2 since
4458 * IPF_PRIMITIVE_HEADER_SIPF2.cs_mask_num_bytes == 1.
4459 */
4460 pvr_csb_pack (&tmp,
4461 IPF_BYTE_BASED_MASK_ONE_BYTE_WORD_0_SIPF2,
4462 mask) {
4463 switch (num_mappings) {
4464 case 4:
4465 mask.cs_mask_one_byte_tile0_7 = true;
4466 mask.cs_mask_one_byte_tile0_6 = true;
4467 FALLTHROUGH;
4468 case 3:
4469 mask.cs_mask_one_byte_tile0_5 = true;
4470 mask.cs_mask_one_byte_tile0_4 = true;
4471 FALLTHROUGH;
4472 case 2:
4473 mask.cs_mask_one_byte_tile0_3 = true;
4474 mask.cs_mask_one_byte_tile0_2 = true;
4475 FALLTHROUGH;
4476 case 1:
4477 mask.cs_mask_one_byte_tile0_1 = true;
4478 mask.cs_mask_one_byte_tile0_0 = true;
4479 break;
4480 default:
4481 /* Unreachable since we clamped the value earlier so
4482 * reaching this is an implementation error.
4483 */
4484 UNREACHABLE("num_mapping exceeded max_mappings_per_pb");
4485 break;
4486 }
4487 }
4488 /* Only 1 byte since there's only 1 valid tile within the single
4489 * IPF_BYTE_BASED_MASK_ONE_BYTE_WORD_0_SIPF2 mask.
4490 * ROGUE_IPF_PRIMITIVE_HEADER_SIPF2.cs_valid_tile0 == true.
4491 */
4492 cs_byte_ptr =
4493 pvr_isp_ctrl_stream_sipf_write_aligned(cs_byte_ptr, tmp, 1);
4494
4495 cs_ptr = (uint32_t *)cs_byte_ptr;
4496
4497 free_ctrl_stream_words -= 2;
4498 } else {
4499 pvr_csb_pack (cs_ptr, IPF_PRIMITIVE_FORMAT, word) {
4500 word.cs_type = ROGUE_IPF_CS_TYPE_PRIM;
4501 word.cs_isp_state_read = true;
4502 word.cs_isp_state_size = 2U;
4503 word.cs_prim_total = 2U * num_mappings - 1U;
4504 word.cs_mask_fmt = ROGUE_IPF_CS_MASK_FMT_FULL;
4505 word.cs_prim_base_pres = true;
4506 }
4507 cs_ptr += pvr_cmd_length(IPF_PRIMITIVE_FORMAT);
4508
4509 pvr_csb_pack (cs_ptr, IPF_PRIMITIVE_BASE, word) {
4510 word.cs_prim_base = prim_blk_addr;
4511 }
4512 cs_ptr += pvr_cmd_length(IPF_PRIMITIVE_BASE);
4513
4514 free_ctrl_stream_words -= 2;
4515 }
4516
4517 rem_mappings -= num_mappings;
4518 mapping_offset += num_mappings;
4519 }
4520 }
4521
4522 source++;
4523
4524 /* A fill blit may also have sources for normal blits. */
4525 if (fill_blit && transfer_cmd->source_count > 0) {
4526 /* Fill blit count for custom mapping equals source blit count. While
4527 * normal blits use only one fill blit.
4528 */
4529 if (state->custom_mapping.pass_count == 0 || source >= num_sources) {
4530 fill_blit = false;
4531 source = 0;
4532 }
4533 }
4534 }
4535
4536 if (PVR_HAS_FEATURE(dev_info, ipf_creq_pf))
4537 assert((num_region_arrays > 1) == was_linked);
4538
4539 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format_v2)) {
4540 uint8_t *cs_byte_ptr = (uint8_t *)cs_ptr;
4541 uint32_t tmp;
4542
4543 /* clang-format off */
4544 pvr_csb_pack (&tmp, IPF_CONTROL_STREAM_TERMINATE_SIPF2, term);
4545 /* clang-format on */
4546
4547 cs_byte_ptr = pvr_isp_ctrl_stream_sipf_write_aligned(cs_byte_ptr, tmp, 1);
4548
4549 cs_ptr = (uint32_t *)cs_byte_ptr;
4550 } else {
4551 pvr_csb_pack (cs_ptr, IPF_CONTROL_STREAM, word) {
4552 word.cs_type = ROGUE_IPF_CS_TYPE_TERM;
4553 }
4554 cs_ptr += pvr_cmd_length(IPF_CONTROL_STREAM);
4555 }
4556
4557 pvr_csb_pack (&regs->isp_mtile_base, CR_ISP_MTILE_BASE, reg) {
4558 reg.addr =
4559 PVR_DEV_ADDR(pvr_cs_bo->dev_addr.addr -
4560 ctx->device->heaps.transfer_frag_heap->base_addr.addr);
4561 }
4562
4563 pvr_csb_pack (&regs->isp_render, CR_ISP_RENDER, reg) {
4564 reg.mode_type = ROGUE_CR_ISP_RENDER_MODE_TYPE_FAST_2D;
4565 }
4566
4567 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format_v2) &&
4568 PVR_HAS_FEATURE(dev_info, ipf_creq_pf)) {
4569 pvr_csb_pack (&regs->isp_rgn, CR_ISP_RGN_SIPF, isp_rgn) {
4570 /* Bit 0 in CR_ISP_RGN.cs_size_ipf_creq_pf is used to indicate the
4571 * presence of a link.
4572 */
4573 isp_rgn.cs_size_ipf_creq_pf = was_linked;
4574 }
4575 } else {
4576 /* clang-format off */
4577 pvr_csb_pack(&regs->isp_rgn, CR_ISP_RGN, isp_rgn);
4578 /* clang-format on */
4579 }
4580
4581 return VK_SUCCESS;
4582 }
4583
4584 static void pvr_transfer_set_filter(struct pvr_transfer_cmd *transfer_cmd,
4585 struct pvr_transfer_3d_state *state)
4586 {
4587 for (uint32_t i = 0; i < transfer_cmd->source_count; i++) {
4588 VkRect2D *src = &transfer_cmd->sources[i].mappings[0U].src_rect;
4589 VkRect2D *dst = &transfer_cmd->sources[i].mappings[0U].dst_rect;
4590
4591 /* If no scaling is applied to the copy region, we can use point
4592 * filtering.
4593 */
4594 if (!state->custom_filter && (src->extent.width == dst->extent.width) &&
4595 (src->extent.height == dst->extent.height))
4596 state->filter[i] = PVR_FILTER_POINT;
4597 else
4598 state->filter[i] = transfer_cmd->sources[i].filter;
4599 }
4600 }
4601
4602 /** Generates hw resources to kick a 3D clip blit. */
4603 static VkResult pvr_3d_clip_blit(struct pvr_transfer_ctx *ctx,
4604 struct pvr_transfer_cmd *transfer_cmd,
4605 struct pvr_transfer_prep_data *prep_data,
4606 uint32_t pass_idx,
4607 bool *finished_out)
4608 {
4609 struct pvr_transfer_3d_state *state = &prep_data->state;
4610 uint32_t texel_unwind_src = state->custom_mapping.texel_unwind_src;
4611 struct pvr_transfer_cmd bg_cmd = { 0U };
4612 uint32_t control_reg;
4613 VkResult result;
4614
4615 state->dont_force_pbe = false;
4616 bg_cmd.scissor = transfer_cmd->scissor;
4617 bg_cmd.cmd_buffer = transfer_cmd->cmd_buffer;
4618 bg_cmd.flags = transfer_cmd->flags;
4619 bg_cmd.flags &=
4620 ~(PVR_TRANSFER_CMD_FLAGS_FAST2D | PVR_TRANSFER_CMD_FLAGS_FILL |
4621 PVR_TRANSFER_CMD_FLAGS_DSMERGE | PVR_TRANSFER_CMD_FLAGS_PICKD);
4622
4623 bg_cmd.source_count = state->custom_mapping.pass_count > 0U ? 0 : 1;
4624 if (bg_cmd.source_count > 0) {
4625 struct pvr_transfer_cmd_source *src = &bg_cmd.sources[0];
4626
4627 src->mappings[0U].src_rect = transfer_cmd->scissor;
4628 src->mappings[0U].dst_rect = transfer_cmd->scissor;
4629 src->resolve_op = PVR_RESOLVE_BLEND;
4630 src->surface = transfer_cmd->dst;
4631 }
4632
4633 state->filter[0] = PVR_FILTER_DONTCARE;
4634 bg_cmd.dst = transfer_cmd->dst;
4635 state->custom_mapping.texel_unwind_src =
4636 state->custom_mapping.texel_unwind_dst;
4637
4638 result =
4639 pvr_3d_copy_blit_core(ctx, &bg_cmd, prep_data, pass_idx, finished_out);
4640 if (result != VK_SUCCESS)
4641 return result;
4642
4643 /* If the destination has 4 channels and the source has at most 2, we still
4644 * need all 4 channels from the USC into the PBE.
4645 */
4646 state->dont_force_pbe = true;
4647 state->custom_mapping.texel_unwind_src = texel_unwind_src;
4648
4649 /* We need the viewport mask, otherwise all pixels would be disabled. */
4650 pvr_csb_pack (&control_reg, CR_ISP_BGOBJVALS, reg) {
4651 reg.mask = true;
4652 }
4653 state->regs.isp_bgobjvals |= control_reg;
4654
4655 pvr_transfer_set_filter(transfer_cmd, state);
4656 result = pvr_isp_ctrl_stream(&ctx->device->pdevice->dev_info,
4657 ctx,
4658 transfer_cmd,
4659 prep_data);
4660 if (result != VK_SUCCESS)
4661 return result;
4662
4663 /* In case of resolve M -> S, the accumulation is read from and written to a
4664 * single sampled surface. Make sure that we are resolving and we have the
4665 * right number of tiles.
4666 */
4667 if (state->down_scale) {
4668 uint64_t tmp;
4669
4670 pvr_csb_pack (&tmp, CR_PBE_WORD0_MRT0, reg) {
4671 reg.downscale = true;
4672 }
4673 state->regs.pbe_wordx_mrty[0U] |= tmp;
4674
4675 result = pvr_isp_tiles(ctx->device, state);
4676 if (result != VK_SUCCESS)
4677 return result;
4678 }
4679
4680 return VK_SUCCESS;
4681 }
4682
4683 static bool pvr_texel_unwind(uint32_t bpp,
4684 pvr_dev_addr_t dev_addr,
4685 bool is_input,
4686 uint32_t texel_extend,
4687 uint32_t *texel_unwind_out)
4688 {
4689 uint32_t texel_unwind = 0U;
4690
4691 for (uint32_t i = 0U; i < 16U; i++) {
4692 if (pvr_is_surface_aligned(dev_addr, is_input, bpp)) {
4693 break;
4694 } else {
4695 if (i == 15U) {
4696 return false;
4697 } else {
4698 dev_addr.addr -= (bpp / texel_extend) / 8U;
4699 texel_unwind++;
4700 }
4701 }
4702 }
4703
4704 *texel_unwind_out = texel_unwind;
4705
4706 return true;
4707 }
4708
4709 static bool pvr_is_identity_mapping(const struct pvr_rect_mapping *mapping)
4710 {
4711 return (mapping->src_rect.offset.x == mapping->dst_rect.offset.x &&
4712 mapping->src_rect.offset.y == mapping->dst_rect.offset.y &&
4713 mapping->src_rect.extent.width == mapping->dst_rect.extent.width &&
4714 mapping->src_rect.extent.height == mapping->dst_rect.extent.height);
4715 }
4716
4717 static inline bool pvr_is_pbe_stride_aligned(const uint32_t stride)
4718 {
4719 if (stride == 1U)
4720 return true;
4721
4722 return ((stride & (ROGUE_PBESTATE_REG_WORD0_LINESTRIDE_UNIT_SIZE - 1U)) ==
4723 0x0U);
4724 }
4725
4726 static struct pvr_transfer_pass *
4727 pvr_create_pass(struct pvr_transfer_custom_mapping *custom_mapping,
4728 uint32_t dst_offset)
4729 {
4730 struct pvr_transfer_pass *pass;
4731
4732 assert(custom_mapping->pass_count < PVR_TRANSFER_MAX_PASSES);
4733
4734 pass = &custom_mapping->passes[custom_mapping->pass_count];
4735 pass->clip_rects_count = 0U;
4736 pass->dst_offset = dst_offset;
4737 pass->source_count = 0U;
4738
4739 custom_mapping->pass_count++;
4740
4741 return pass;
4742 }
4743
4744 /* Acquire pass with given offset. If one doesn't exist, create new. */
4745 static struct pvr_transfer_pass *
4746 pvr_acquire_pass(struct pvr_transfer_custom_mapping *custom_mapping,
4747 uint32_t dst_offset)
4748 {
4749 for (uint32_t i = 0U; i < custom_mapping->pass_count; i++) {
4750 if (custom_mapping->passes[i].dst_offset == dst_offset)
4751 return &custom_mapping->passes[i];
4752 }
4753
4754 return pvr_create_pass(custom_mapping, dst_offset);
4755 }
4756
4757 static struct pvr_transfer_wa_source *
4758 pvr_create_source(struct pvr_transfer_pass *pass,
4759 uint32_t src_offset,
4760 bool extend_height)
4761 {
4762 struct pvr_transfer_wa_source *src;
4763
4764 assert(pass->source_count < ARRAY_SIZE(pass->sources));
4765
4766 src = &pass->sources[pass->source_count];
4767 src->mapping_count = 0U;
4768 src->extend_height = extend_height;
4769
4770 pass->source_count++;
4771
4772 return src;
4773 }
4774
4775 /* Acquire source with given offset. If one doesn't exist, create new. */
4776 static struct pvr_transfer_wa_source *
4777 pvr_acquire_source(struct pvr_transfer_pass *pass,
4778 uint32_t src_offset,
4779 bool extend_height)
4780 {
4781 for (uint32_t i = 0U; i < pass->source_count; i++) {
4782 if (pass->sources[i].src_offset == src_offset &&
4783 pass->sources[i].extend_height == extend_height)
4784 return &pass->sources[i];
4785 }
4786
4787 return pvr_create_source(pass, src_offset, extend_height);
4788 }
4789
4790 static void pvr_remove_source(struct pvr_transfer_pass *pass, uint32_t idx)
4791 {
4792 assert(idx < pass->source_count);
4793
4794 for (uint32_t i = idx; i < (pass->source_count - 1U); i++)
4795 pass->sources[i] = pass->sources[i + 1U];
4796
4797 pass->source_count--;
4798 }
4799
4800 static void pvr_remove_mapping(struct pvr_transfer_wa_source *src, uint32_t idx)
4801 {
4802 assert(idx < src->mapping_count);
4803
4804 for (uint32_t i = idx; i < (src->mapping_count - 1U); i++)
4805 src->mappings[i] = src->mappings[i + 1U];
4806
4807 src->mapping_count--;
4808 }
4809
4810 static struct pvr_rect_mapping *
4811 pvr_create_mapping(struct pvr_transfer_wa_source *src)
4812 {
4813 assert(src->mapping_count < ARRAY_SIZE(src->mappings));
4814
4815 return &src->mappings[src->mapping_count++];
4816 }
4817
4818 /**
4819 * If PBE can't write to surfaces with odd stride, the stride of
4820 * destination surface is doubled to make it even. Height of the surface is
4821 * halved. The source surface is not resized. Each half of the modified
4822 * destination surface samples every second row from the source surface. This
4823 * only works with nearest filtering.
4824 */
4825 static bool pvr_double_stride(struct pvr_transfer_pass *pass, uint32_t stride)
4826 {
4827 struct pvr_rect_mapping *mappings = pass->sources[0].mappings;
4828 uint32_t new_mapping = 0;
4829
4830 if (stride == 1U)
4831 return false;
4832
4833 if (mappings[0U].dst_rect.extent.height == 1U &&
4834 pass->sources[0].mapping_count == 1U) {
4835 /* Only one mapping required if height is 1. */
4836 if ((mappings[0U].dst_rect.offset.y & 1U) != 0U) {
4837 mappings[0U].dst_rect.offset.x += (int32_t)stride;
4838 mappings[0U].dst_rect.offset.y /= 2U;
4839 mappings[0U].dst_rect.extent.height =
4840 (mappings[0U].dst_rect.extent.height + 1U) / 2U;
4841 } else {
4842 mappings[0U].dst_rect.extent.height =
4843 (mappings[0U].dst_rect.offset.y +
4844 mappings[0U].dst_rect.extent.height + 1U) /
4845 2U -
4846 mappings[0U].dst_rect.offset.y;
4847 mappings[0U].dst_rect.offset.y /= 2U;
4848 }
4849
4850 return true;
4851 }
4852
4853 for (uint32_t i = 0; i < pass->sources[0].mapping_count; i++) {
4854 struct pvr_rect_mapping *mapping_a = &mappings[i];
4855 struct pvr_rect_mapping *mapping_b =
4856 &mappings[pass->sources[0].mapping_count + new_mapping];
4857
4858 int32_t a_src_rect_y0 = mapping_a->src_rect.offset.y;
4859 int32_t a_src_rect_y1 = a_src_rect_y0 + mapping_a->src_rect.extent.height;
4860 int32_t a_dst_rect_y0 = mapping_a->dst_rect.offset.y;
4861 int32_t a_dst_rect_y1 = a_dst_rect_y0 + mapping_a->dst_rect.extent.height;
4862
4863 int32_t b_src_rect_y0 = a_src_rect_y0;
4864 int32_t b_src_rect_y1 = a_src_rect_y1;
4865 int32_t b_dst_rect_y0 = a_dst_rect_y0;
4866 int32_t b_dst_rect_y1 = a_dst_rect_y1;
4867
4868 const bool dst_starts_odd_row = !!(a_dst_rect_y0 & 1);
4869 const bool dst_ends_odd_row = !!(a_dst_rect_y1 & 1);
4870 const bool src_starts_odd_row = !!(a_src_rect_y0 & 1);
4871 const bool src_ends_odd_row = !!(a_src_rect_y1 & 1);
4872
4873 assert(pass->sources[0].mapping_count + new_mapping <
4874 ARRAY_SIZE(pass->sources[0].mappings));
4875 *mapping_b = *mapping_a;
4876
4877 a_src_rect_y0 = ALIGN_POT(a_src_rect_y0, 2);
4878 if (dst_starts_odd_row && !src_starts_odd_row)
4879 a_src_rect_y0++;
4880 else if (!dst_starts_odd_row && src_starts_odd_row)
4881 a_src_rect_y0--;
4882
4883 a_src_rect_y1 = ALIGN_POT(a_src_rect_y1, 2);
4884 if (dst_ends_odd_row && !src_ends_odd_row)
4885 a_src_rect_y1++;
4886 else if (!dst_ends_odd_row && src_ends_odd_row)
4887 a_src_rect_y1--;
4888
4889 b_src_rect_y0 = ALIGN_POT(b_src_rect_y0, 2);
4890 if (dst_starts_odd_row && src_starts_odd_row)
4891 b_src_rect_y0--;
4892 else if (!dst_starts_odd_row && !src_starts_odd_row)
4893 b_src_rect_y0++;
4894
4895 b_src_rect_y1 = ALIGN_POT(b_src_rect_y1, 2);
4896 if (dst_ends_odd_row && src_ends_odd_row)
4897 b_src_rect_y1--;
4898 else if (!dst_ends_odd_row && !src_ends_odd_row)
4899 b_src_rect_y1++;
4900
4901 /* Destination rectangles. */
4902 a_dst_rect_y0 /= 2;
4903 if (dst_starts_odd_row)
4904 a_dst_rect_y0++;
4905
4906 a_dst_rect_y1 = (a_dst_rect_y1 + 1) / 2;
4907
4908 mapping_b->dst_rect.offset.x += stride;
4909 b_dst_rect_y0 /= 2;
4910 b_dst_rect_y1 /= 2;
4911
4912 mapping_a->src_rect.offset.y = a_src_rect_y0;
4913 mapping_a->src_rect.extent.height = a_src_rect_y1 - a_src_rect_y0;
4914
4915 mapping_a->dst_rect.offset.y = a_dst_rect_y0;
4916 mapping_a->dst_rect.extent.height = a_dst_rect_y1 - a_dst_rect_y0;
4917
4918 mapping_b->src_rect.offset.y = b_src_rect_y0;
4919 mapping_b->src_rect.extent.height = b_src_rect_y1 - b_src_rect_y0;
4920
4921 mapping_b->dst_rect.offset.y = b_dst_rect_y0;
4922 mapping_b->dst_rect.extent.height = b_dst_rect_y1 - b_dst_rect_y0;
4923
4924 if (!mapping_a->src_rect.extent.width ||
4925 !mapping_a->src_rect.extent.height) {
4926 *mapping_a = *mapping_b;
4927 } else if (mapping_b->src_rect.extent.width &&
4928 mapping_b->src_rect.extent.height) {
4929 new_mapping++;
4930 }
4931 }
4932
4933 pass->sources[0].mapping_count++;
4934
4935 return true;
4936 }
4937
4938 static void pvr_split_rect(uint32_t stride,
4939 uint32_t height,
4940 uint32_t texel_unwind,
4941 VkRect2D *rect_a,
4942 VkRect2D *rect_b)
4943 {
4944 rect_a->offset.x = 0;
4945 rect_a->extent.width = stride - texel_unwind;
4946 rect_a->offset.y = 0;
4947 rect_a->extent.height = height;
4948
4949 rect_b->offset.x = (int32_t)stride - texel_unwind;
4950 rect_b->extent.width = texel_unwind;
4951 rect_b->offset.y = 0;
4952 rect_b->extent.height = height;
4953 }
4954
4955 static bool pvr_rect_width_covered_by(const VkRect2D *rect_a,
4956 const VkRect2D *rect_b)
4957 {
4958 return (rect_b->offset.x <= rect_a->offset.x &&
4959 (rect_b->offset.x + rect_b->extent.width) >=
4960 (rect_a->offset.x + rect_a->extent.width));
4961 }
4962
4963 static void pvr_unwind_rects(uint32_t width,
4964 uint32_t height,
4965 uint32_t texel_unwind,
4966 bool input,
4967 struct pvr_transfer_pass *pass)
4968 {
4969 struct pvr_transfer_wa_source *const source = &pass->sources[0];
4970 struct pvr_rect_mapping *const mappings = source->mappings;
4971 const uint32_t num_mappings = source->mapping_count;
4972 VkRect2D rect_a, rect_b;
4973
4974 if (texel_unwind == 0)
4975 return;
4976
4977 pvr_split_rect(width, height, texel_unwind, &rect_a, &rect_b);
4978
4979 for (uint32_t i = 0; i < num_mappings; i++) {
4980 VkRect2D *const old_rect = input ? &mappings[i].src_rect
4981 : &mappings[i].dst_rect;
4982
4983 if (height == 1) {
4984 old_rect->offset.x += texel_unwind;
4985 } else if (width == 1) {
4986 old_rect->offset.y += texel_unwind;
4987 } else if (pvr_rect_width_covered_by(old_rect, &rect_a)) {
4988 old_rect->offset.x += texel_unwind;
4989 } else if (pvr_rect_width_covered_by(old_rect, &rect_b)) {
4990 old_rect->offset.x = texel_unwind - width + old_rect->offset.x;
4991 old_rect->offset.y++;
4992 } else {
4993 /* Mapping requires split. */
4994 const uint32_t new_mapping = source->mapping_count++;
4995
4996 VkRect2D *const new_rect = input ? &mappings[new_mapping].src_rect
4997 : &mappings[new_mapping].dst_rect;
4998
4999 VkRect2D *const new_rect_opp = input ? &mappings[new_mapping].dst_rect
5000 : &mappings[new_mapping].src_rect;
5001 VkRect2D *const old_rect_opp = input ? &mappings[i].dst_rect
5002 : &mappings[i].src_rect;
5003
5004 const uint32_t split_point = width - texel_unwind;
5005 const uint32_t split_width =
5006 old_rect->offset.x + old_rect->extent.width - split_point;
5007
5008 assert(new_mapping < ARRAY_SIZE(source->mappings));
5009 mappings[new_mapping] = mappings[i];
5010
5011 old_rect_opp->extent.width -= split_width;
5012 new_rect_opp->extent.width = split_width;
5013 new_rect_opp->offset.x =
5014 old_rect_opp->offset.x + old_rect_opp->extent.width;
5015
5016 old_rect->offset.x += texel_unwind;
5017 old_rect->extent.width = width - old_rect->offset.x;
5018
5019 new_rect->offset.x = 0;
5020 new_rect->offset.y++;
5021 new_rect->extent.width = split_width;
5022 }
5023 }
5024 }
5025
5026 /**
5027 * Assign clip rects to rectangle mappings. TDM can only do two PBE clip
5028 * rects per screen.
5029 */
5030 static void
5031 pvr_map_clip_rects(struct pvr_transfer_custom_mapping *custom_mapping)
5032 {
5033 for (uint32_t i = 0U; i < custom_mapping->pass_count; i++) {
5034 struct pvr_transfer_pass *pass = &custom_mapping->passes[i];
5035
5036 pass->clip_rects_count = 0U;
5037
5038 for (uint32_t s = 0U; s < pass->source_count; s++) {
5039 struct pvr_transfer_wa_source *src = &pass->sources[s];
5040
5041 for (uint32_t j = 0U; j < src->mapping_count; j++) {
5042 struct pvr_rect_mapping *mappings = src->mappings;
5043 VkRect2D *clip_rects = pass->clip_rects;
5044 bool merged = false;
5045
5046 /* Try merge adjacent clip rects. */
5047 for (uint32_t k = 0U; k < pass->clip_rects_count; k++) {
5048 if (clip_rects[k].offset.y == mappings[j].dst_rect.offset.y &&
5049 clip_rects[k].extent.height ==
5050 mappings[j].dst_rect.extent.height &&
5051 clip_rects[k].offset.x + clip_rects[k].extent.width ==
5052 mappings[j].dst_rect.offset.x) {
5053 clip_rects[k].extent.width +=
5054 mappings[j].dst_rect.extent.width;
5055 merged = true;
5056 break;
5057 }
5058
5059 if (clip_rects[k].offset.y == mappings[j].dst_rect.offset.y &&
5060 clip_rects[k].extent.height ==
5061 mappings[j].dst_rect.extent.height &&
5062 clip_rects[k].offset.x ==
5063 mappings[j].dst_rect.offset.x +
5064 mappings[j].dst_rect.extent.width) {
5065 clip_rects[k].offset.x = mappings[j].dst_rect.offset.x;
5066 clip_rects[k].extent.width +=
5067 mappings[j].dst_rect.extent.width;
5068 merged = true;
5069 break;
5070 }
5071
5072 if (clip_rects[k].offset.x == mappings[j].dst_rect.offset.x &&
5073 clip_rects[k].extent.width ==
5074 mappings[j].dst_rect.extent.width &&
5075 clip_rects[k].offset.y + clip_rects[k].extent.height ==
5076 mappings[j].dst_rect.offset.y) {
5077 clip_rects[k].extent.height +=
5078 mappings[j].dst_rect.extent.height;
5079 merged = true;
5080 break;
5081 }
5082
5083 if (clip_rects[k].offset.x == mappings[j].dst_rect.offset.x &&
5084 clip_rects[k].extent.width ==
5085 mappings[j].dst_rect.extent.width &&
5086 clip_rects[k].offset.y ==
5087 mappings[j].dst_rect.offset.y +
5088 mappings[j].dst_rect.extent.height) {
5089 clip_rects[k].extent.height +=
5090 mappings[j].dst_rect.extent.height;
5091 clip_rects[k].offset.y = mappings[j].dst_rect.offset.y;
5092 merged = true;
5093 break;
5094 }
5095 }
5096
5097 if (merged)
5098 continue;
5099
5100 /* Create new pass if needed, TDM can only have 2 clip rects. */
5101 if (pass->clip_rects_count >= custom_mapping->max_clip_rects) {
5102 struct pvr_transfer_pass *new_pass =
5103 pvr_create_pass(custom_mapping, pass->dst_offset);
5104 struct pvr_transfer_wa_source *new_source =
5105 pvr_create_source(new_pass,
5106 src->src_offset,
5107 src->extend_height);
5108 struct pvr_rect_mapping *new_mapping =
5109 pvr_create_mapping(new_source);
5110
5111 new_pass->clip_rects_count = 1U;
5112 *new_mapping = src->mappings[j];
5113
5114 pvr_remove_mapping(src, j);
5115
5116 if (src->mapping_count == 0) {
5117 pvr_remove_source(pass, s);
5118 s--;
5119 } else {
5120 /* Redo - mapping was replaced. */
5121 j--;
5122 }
5123 } else {
5124 pass->clip_rects[pass->clip_rects_count] =
5125 src->mappings[j].dst_rect;
5126
5127 pass->clip_rects_count++;
5128
5129 assert(pass->clip_rects_count <= ARRAY_SIZE(pass->clip_rects));
5130 }
5131 }
5132 }
5133 }
5134 }
5135
5136 static bool pvr_extend_height(const VkRect2D *rect,
5137 const uint32_t height,
5138 const uint32_t unwind_src)
5139 {
5140 if (rect->offset.x >= (int32_t)unwind_src)
5141 return false;
5142
5143 return (rect->offset.y > (int32_t)height) ||
5144 ((rect->offset.y + rect->extent.height) > (int32_t)height);
5145 }
5146
5147 static void
5148 pvr_generate_custom_mapping(uint32_t src_stride,
5149 uint32_t src_width,
5150 uint32_t src_height,
5151 uint32_t dst_stride,
5152 uint32_t dst_width,
5153 uint32_t dst_height,
5154 enum pvr_memlayout dst_mem_layout,
5155 struct pvr_transfer_custom_mapping *custom_mapping)
5156 {
5157 src_stride *= custom_mapping->texel_extend_src;
5158 src_width *= custom_mapping->texel_extend_src;
5159 dst_stride *= custom_mapping->texel_extend_dst;
5160 dst_width *= custom_mapping->texel_extend_dst;
5161
5162 if (custom_mapping->texel_unwind_src > 0U) {
5163 pvr_unwind_rects(src_stride,
5164 src_height,
5165 custom_mapping->texel_unwind_src,
5166 true,
5167 &custom_mapping->passes[0U]);
5168 }
5169
5170 if (custom_mapping->double_stride) {
5171 custom_mapping->double_stride =
5172 pvr_double_stride(&custom_mapping->passes[0U], dst_stride);
5173
5174 dst_stride *= 2U;
5175 }
5176
5177 pvr_unwind_rects(dst_stride,
5178 dst_height,
5179 custom_mapping->texel_unwind_dst,
5180 false,
5181 &custom_mapping->passes[0U]);
5182
5183 pvr_map_clip_rects(custom_mapping);
5184
5185 /* If the last row of the source mapping is sampled, height of the surface
5186 * can only be increased if the new area contains a valid region. Some blits
5187 * are split to two sources.
5188 */
5189 if (custom_mapping->texel_unwind_src > 0U) {
5190 for (uint32_t i = 0; i < custom_mapping->pass_count; i++) {
5191 struct pvr_transfer_pass *pass = &custom_mapping->passes[i];
5192
5193 for (uint32_t j = 0; j < pass->source_count; j++) {
5194 struct pvr_transfer_wa_source *src = &pass->sources[j];
5195
5196 for (uint32_t k = 0; k < src->mapping_count; k++) {
5197 VkRect2D *src_rect = &src->mappings[k].src_rect;
5198 bool extend_height =
5199 pvr_extend_height(src_rect,
5200 src_height,
5201 custom_mapping->texel_unwind_src);
5202
5203 if (src->mapping_count == 1) {
5204 src->extend_height = extend_height;
5205 } else if (!src->extend_height && extend_height) {
5206 struct pvr_transfer_wa_source *new_src =
5207 pvr_acquire_source(pass, src->src_offset, extend_height);
5208
5209 new_src->mappings[new_src->mapping_count] = src->mappings[k];
5210 new_src->src_offset = src->src_offset;
5211
5212 for (uint32_t l = k + 1; l < src->mapping_count; l++)
5213 src->mappings[l - 1] = src->mappings[l];
5214
5215 new_src->mapping_count++;
5216 src->mapping_count--;
5217 k--;
5218 }
5219 }
5220 }
5221 }
5222 }
5223 }
5224
5225 static bool
5226 pvr_get_custom_mapping(const struct pvr_device_info *dev_info,
5227 const struct pvr_transfer_cmd *transfer_cmd,
5228 uint32_t max_clip_rects,
5229 struct pvr_transfer_custom_mapping *custom_mapping)
5230 {
5231 const uint32_t dst_bpp =
5232 vk_format_get_blocksizebits(transfer_cmd->dst.vk_format);
5233 const struct pvr_transfer_cmd_source *src = NULL;
5234 struct pvr_transfer_pass *pass;
5235 bool ret;
5236
5237 custom_mapping->max_clip_rects = max_clip_rects;
5238 custom_mapping->texel_unwind_src = 0U;
5239 custom_mapping->texel_unwind_dst = 0U;
5240 custom_mapping->texel_extend_src = 1U;
5241 custom_mapping->texel_extend_dst = 1U;
5242 custom_mapping->pass_count = 0U;
5243
5244 if (transfer_cmd->source_count > 1)
5245 return false;
5246
5247 custom_mapping->max_clip_size = PVR_MAX_CLIP_SIZE(dev_info);
5248
5249 ret = pvr_texel_unwind(dst_bpp,
5250 transfer_cmd->dst.dev_addr,
5251 false,
5252 1U,
5253 &custom_mapping->texel_unwind_dst);
5254 if (!ret) {
5255 custom_mapping->texel_extend_dst = dst_bpp / 8U;
5256 if (transfer_cmd->source_count > 0) {
5257 if (transfer_cmd->sources[0].surface.mem_layout ==
5258 PVR_MEMLAYOUT_LINEAR) {
5259 custom_mapping->texel_extend_src = custom_mapping->texel_extend_dst;
5260 } else if (transfer_cmd->sources[0].surface.mem_layout ==
5261 PVR_MEMLAYOUT_TWIDDLED &&
5262 transfer_cmd->sources[0].surface.height == 1U) {
5263 custom_mapping->texel_extend_src = custom_mapping->texel_extend_dst;
5264 }
5265 }
5266
5267 ret = pvr_texel_unwind(dst_bpp,
5268 transfer_cmd->dst.dev_addr,
5269 false,
5270 custom_mapping->texel_extend_dst,
5271 &custom_mapping->texel_unwind_dst);
5272 if (!ret)
5273 return false;
5274 }
5275
5276 if (transfer_cmd->source_count > 0) {
5277 src = &transfer_cmd->sources[0];
5278 const uint32_t src_bpp =
5279 vk_format_get_blocksizebits(src->surface.vk_format);
5280
5281 ret = pvr_is_surface_aligned(src->surface.dev_addr, true, src_bpp);
5282
5283 if (!ret && (src->surface.mem_layout == PVR_MEMLAYOUT_LINEAR ||
5284 src->surface.height == 1U)) {
5285 ret = pvr_texel_unwind(src_bpp,
5286 src->surface.dev_addr,
5287 true,
5288 custom_mapping->texel_extend_src,
5289 &custom_mapping->texel_unwind_src);
5290 }
5291
5292 if (!ret) {
5293 custom_mapping->texel_extend_src = dst_bpp / 8U;
5294 custom_mapping->texel_extend_dst = custom_mapping->texel_extend_src;
5295
5296 ret = pvr_texel_unwind(src_bpp,
5297 src->surface.dev_addr,
5298 true,
5299 custom_mapping->texel_extend_src,
5300 &custom_mapping->texel_unwind_src);
5301 }
5302
5303 if (!ret)
5304 return false;
5305 }
5306
5307 VkRect2D rect = transfer_cmd->scissor;
5308 assert(
5309 (rect.offset.x + rect.extent.width) <= custom_mapping->max_clip_size &&
5310 (rect.offset.y + rect.extent.height) <= custom_mapping->max_clip_size);
5311
5312 /* Texel extend only works with strided memory layout, because pixel width is
5313 * changed. Texel unwind only works with strided memory layout. 1D blits are
5314 * allowed.
5315 */
5316 if (src && src->surface.height > 1U &&
5317 (custom_mapping->texel_extend_src > 1U ||
5318 custom_mapping->texel_unwind_src > 0U) &&
5319 src->surface.mem_layout != PVR_MEMLAYOUT_LINEAR) {
5320 return false;
5321 }
5322
5323 /* Texel extend only works with strided memory layout, because pixel width is
5324 * changed. Texel unwind only works with strided memory layout. 1D blits are
5325 * allowed.
5326 */
5327 if ((custom_mapping->texel_extend_dst > 1U ||
5328 custom_mapping->texel_unwind_dst > 0U) &&
5329 transfer_cmd->dst.mem_layout != PVR_MEMLAYOUT_LINEAR &&
5330 transfer_cmd->dst.height > 1U) {
5331 return false;
5332 }
5333
5334 if (transfer_cmd->dst.mem_layout == PVR_MEMLAYOUT_LINEAR) {
5335 custom_mapping->double_stride = !pvr_is_pbe_stride_aligned(
5336 transfer_cmd->dst.stride * custom_mapping->texel_extend_dst);
5337 }
5338
5339 if (custom_mapping->texel_unwind_src > 0U ||
5340 custom_mapping->texel_unwind_dst > 0U || custom_mapping->double_stride) {
5341 struct pvr_transfer_wa_source *wa_src;
5342 struct pvr_rect_mapping *mapping;
5343
5344 pass = pvr_acquire_pass(custom_mapping, 0U);
5345 wa_src = pvr_create_source(pass, 0U, false);
5346 mapping = pvr_create_mapping(wa_src);
5347
5348 if (transfer_cmd->source_count > 0) {
5349 *mapping = src->mappings[0U];
5350 } else {
5351 mapping->src_rect = transfer_cmd->scissor;
5352 mapping->dst_rect = transfer_cmd->scissor;
5353 }
5354 } else {
5355 return false;
5356 }
5357
5358 if (custom_mapping->texel_extend_src > 1U ||
5359 custom_mapping->texel_extend_dst > 1U) {
5360 pass->sources[0].mappings[0U].src_rect.offset.x *=
5361 (int32_t)custom_mapping->texel_extend_dst;
5362 pass->sources[0].mappings[0U].src_rect.extent.width *=
5363 (int32_t)custom_mapping->texel_extend_dst;
5364 pass->sources[0].mappings[0U].dst_rect.offset.x *=
5365 (int32_t)custom_mapping->texel_extend_dst;
5366 pass->sources[0].mappings[0U].dst_rect.extent.width *=
5367 (int32_t)custom_mapping->texel_extend_dst;
5368 }
5369
5370 if (transfer_cmd->source_count > 0) {
5371 pvr_generate_custom_mapping(transfer_cmd->sources[0].surface.stride,
5372 transfer_cmd->sources[0].surface.width,
5373 transfer_cmd->sources[0].surface.height,
5374 transfer_cmd->dst.stride,
5375 transfer_cmd->dst.width,
5376 transfer_cmd->dst.height,
5377 transfer_cmd->dst.mem_layout,
5378 custom_mapping);
5379 } else {
5380 pvr_generate_custom_mapping(0U,
5381 0U,
5382 0U,
5383 transfer_cmd->dst.stride,
5384 transfer_cmd->dst.width,
5385 transfer_cmd->dst.height,
5386 transfer_cmd->dst.mem_layout,
5387 custom_mapping);
5388 }
5389
5390 return true;
5391 }
5392
5393 static void pvr_pbe_extend_rect(uint32_t texel_extend, VkRect2D *rect)
5394 {
5395 rect->offset.x *= texel_extend;
5396 rect->extent.width *= texel_extend;
5397 }
5398
5399 static void pvr_pbe_rect_intersect(VkRect2D *rect_a, VkRect2D *rect_b)
5400 {
5401 rect_a->extent.width = MIN2(rect_a->offset.x + rect_a->extent.width,
5402 rect_b->offset.x + rect_b->extent.width) -
5403 MAX2(rect_a->offset.x, rect_b->offset.x);
5404 rect_a->offset.x = MAX2(rect_a->offset.x, rect_b->offset.x);
5405 rect_a->extent.height = MIN2(rect_a->offset.y + rect_a->extent.height,
5406 rect_b->offset.y + rect_b->extent.height) -
5407 MAX2(rect_a->offset.y, rect_b->offset.y);
5408 rect_a->offset.y = MAX2(rect_a->offset.y, rect_b->offset.y);
5409 }
5410
5411 static VkFormat pvr_texel_extend_src_format(VkFormat vk_format)
5412 {
5413 uint32_t bpp = vk_format_get_blocksizebits(vk_format);
5414 VkFormat ext_format;
5415
5416 switch (bpp) {
5417 case 16:
5418 ext_format = VK_FORMAT_R8G8_UINT;
5419 break;
5420 case 32:
5421 ext_format = VK_FORMAT_R8G8B8A8_UINT;
5422 break;
5423 case 48:
5424 ext_format = VK_FORMAT_R16G16B16_UINT;
5425 break;
5426 default:
5427 ext_format = VK_FORMAT_R8_UINT;
5428 break;
5429 }
5430
5431 return ext_format;
5432 }
5433
5434 static void
5435 pvr_modify_command(struct pvr_transfer_custom_mapping *custom_mapping,
5436 uint32_t pass_idx,
5437 struct pvr_transfer_cmd *transfer_cmd)
5438 {
5439 struct pvr_transfer_pass *pass = &custom_mapping->passes[pass_idx];
5440 uint32_t bpp;
5441
5442 if (custom_mapping->texel_extend_src > 1U) {
5443 struct pvr_rect_mapping *mapping = &transfer_cmd->sources[0].mappings[0];
5444
5445 pvr_pbe_extend_rect(custom_mapping->texel_extend_src, &mapping->dst_rect);
5446 pvr_pbe_extend_rect(custom_mapping->texel_extend_src, &mapping->src_rect);
5447
5448 transfer_cmd->dst.vk_format = VK_FORMAT_R8_UINT;
5449 transfer_cmd->dst.width *= custom_mapping->texel_extend_src;
5450 transfer_cmd->dst.stride *= custom_mapping->texel_extend_src;
5451 transfer_cmd->sources[0].surface.vk_format = VK_FORMAT_R8_UINT;
5452 transfer_cmd->sources[0].surface.width *=
5453 custom_mapping->texel_extend_src;
5454 transfer_cmd->sources[0].surface.stride *=
5455 custom_mapping->texel_extend_src;
5456 } else if (custom_mapping->texel_extend_dst > 1U) {
5457 VkRect2D max_clip = {
5458 .offset = { 0, 0 },
5459 .extent = { custom_mapping->max_clip_size,
5460 custom_mapping->max_clip_size },
5461 };
5462
5463 pvr_pbe_extend_rect(custom_mapping->texel_extend_dst,
5464 &transfer_cmd->scissor);
5465
5466 pvr_pbe_rect_intersect(&transfer_cmd->scissor, &max_clip);
5467
5468 if (transfer_cmd->source_count > 0) {
5469 transfer_cmd->sources[0].surface.width *=
5470 custom_mapping->texel_extend_dst;
5471 transfer_cmd->sources[0].surface.stride *=
5472 custom_mapping->texel_extend_dst;
5473
5474 transfer_cmd->sources[0].surface.vk_format =
5475 pvr_texel_extend_src_format(
5476 transfer_cmd->sources[0].surface.vk_format);
5477 }
5478
5479 transfer_cmd->dst.vk_format = VK_FORMAT_R8_UINT;
5480 transfer_cmd->dst.width *= custom_mapping->texel_extend_dst;
5481 transfer_cmd->dst.stride *= custom_mapping->texel_extend_dst;
5482 }
5483
5484 if (custom_mapping->double_stride) {
5485 transfer_cmd->dst.width *= 2U;
5486 transfer_cmd->dst.stride *= 2U;
5487 }
5488
5489 if (custom_mapping->texel_unwind_src > 0U) {
5490 if (transfer_cmd->sources[0].surface.height == 1U) {
5491 transfer_cmd->sources[0].surface.width +=
5492 custom_mapping->texel_unwind_src;
5493 transfer_cmd->sources[0].surface.stride +=
5494 custom_mapping->texel_unwind_src;
5495 } else if (transfer_cmd->sources[0].surface.stride == 1U) {
5496 transfer_cmd->sources[0].surface.height +=
5497 custom_mapping->texel_unwind_src;
5498 } else {
5499 /* Increase source width by texel unwind. If texel unwind is less than
5500 * the distance between width and stride. The blit can be done with one
5501 * rectangle mapping, but the width of the surface needs be to
5502 * increased in case we sample from the area between width and stride.
5503 */
5504 transfer_cmd->sources[0].surface.width =
5505 MIN2(transfer_cmd->sources[0].surface.width +
5506 custom_mapping->texel_unwind_src,
5507 transfer_cmd->sources[0].surface.stride);
5508 }
5509 }
5510
5511 for (uint32_t i = 0U; i < pass->source_count; i++) {
5512 struct pvr_transfer_wa_source *src = &pass->sources[i];
5513
5514 if (i > 0)
5515 transfer_cmd->sources[i] = transfer_cmd->sources[0];
5516
5517 transfer_cmd->sources[i].mapping_count = src->mapping_count;
5518 for (uint32_t j = 0U; j < transfer_cmd->sources[i].mapping_count; j++)
5519 transfer_cmd->sources[i].mappings[j] = src->mappings[j];
5520
5521 if (src->extend_height)
5522 transfer_cmd->sources[i].surface.height += 1U;
5523
5524 transfer_cmd->sources[i].surface.width =
5525 MIN2(PVR_MAX_WIDTH, transfer_cmd->sources[i].surface.width);
5526 transfer_cmd->sources[i].surface.height =
5527 MIN2(PVR_MAX_WIDTH, transfer_cmd->sources[i].surface.height);
5528 transfer_cmd->sources[i].surface.stride =
5529 MIN2(PVR_MAX_WIDTH, transfer_cmd->sources[i].surface.stride);
5530 }
5531
5532 if (transfer_cmd->dst.height == 1U) {
5533 transfer_cmd->dst.width =
5534 transfer_cmd->dst.stride + custom_mapping->texel_unwind_dst;
5535 transfer_cmd->dst.mem_layout = PVR_MEMLAYOUT_TWIDDLED;
5536 }
5537
5538 if (transfer_cmd->dst.mem_layout == PVR_MEMLAYOUT_TWIDDLED) {
5539 transfer_cmd->dst.width =
5540 MIN2((uint32_t)custom_mapping->max_clip_size, transfer_cmd->dst.width);
5541 transfer_cmd->dst.height = MIN2((uint32_t)custom_mapping->max_clip_size,
5542 transfer_cmd->dst.height);
5543 } else {
5544 transfer_cmd->dst.width = MIN2(PVR_MAX_WIDTH, transfer_cmd->dst.width);
5545 }
5546
5547 if (transfer_cmd->source_count > 0) {
5548 for (uint32_t i = 0; i < pass->source_count; i++) {
5549 struct pvr_transfer_cmd_source *src = &transfer_cmd->sources[i];
5550
5551 bpp = vk_format_get_blocksizebits(src->surface.vk_format);
5552
5553 src->surface.dev_addr.addr -=
5554 custom_mapping->texel_unwind_src * bpp / 8U;
5555 src->surface.dev_addr.addr += MAX2(src->surface.sample_count, 1U) *
5556 pass->sources[i].src_offset * bpp / 8U;
5557 }
5558 }
5559
5560 bpp = vk_format_get_blocksizebits(transfer_cmd->dst.vk_format);
5561 transfer_cmd->dst.dev_addr.addr -=
5562 custom_mapping->texel_unwind_dst * bpp / 8U;
5563 transfer_cmd->dst.dev_addr.addr +=
5564 MAX2(transfer_cmd->dst.sample_count, 1U) * pass->dst_offset * bpp / 8U;
5565
5566 if (transfer_cmd->source_count > 0)
5567 transfer_cmd->source_count = pass->source_count;
5568 }
5569
5570 /* Route a copy_blit (FastScale HW) to a clip_blit (Fast2D HW).
5571 * Destination rectangle can be specified in dst_rect, or NULL to use existing.
5572 */
5573 static VkResult pvr_reroute_to_clip(struct pvr_transfer_ctx *ctx,
5574 const struct pvr_transfer_cmd *transfer_cmd,
5575 const struct VkRect2D *dst_rect,
5576 struct pvr_transfer_prep_data *prep_data,
5577 uint32_t pass_idx,
5578 bool *finished_out)
5579 {
5580 struct pvr_transfer_cmd clip_transfer_cmd;
5581
5582 clip_transfer_cmd = *transfer_cmd;
5583 clip_transfer_cmd.flags |= PVR_TRANSFER_CMD_FLAGS_FAST2D;
5584
5585 if (transfer_cmd->source_count <= 1U) {
5586 if (dst_rect)
5587 clip_transfer_cmd.scissor = *dst_rect;
5588
5589 return pvr_3d_clip_blit(ctx,
5590 &clip_transfer_cmd,
5591 prep_data,
5592 pass_idx,
5593 finished_out);
5594 }
5595
5596 return vk_error(ctx->device, VK_ERROR_FORMAT_NOT_SUPPORTED);
5597 }
5598
5599 static VkResult pvr_3d_copy_blit(struct pvr_transfer_ctx *ctx,
5600 struct pvr_transfer_cmd *transfer_cmd,
5601 struct pvr_transfer_prep_data *prep_data,
5602 uint32_t pass_idx,
5603 bool *finished_out)
5604 {
5605 const struct pvr_device_info *const dev_info =
5606 &ctx->device->pdevice->dev_info;
5607
5608 struct pvr_transfer_3d_state *state = &prep_data->state;
5609 struct pvr_transfer_cmd *active_cmd = transfer_cmd;
5610 struct pvr_transfer_cmd int_cmd;
5611 VkResult result;
5612
5613 state->dont_force_pbe = false;
5614 state->pass_idx = pass_idx;
5615
5616 pvr_transfer_set_filter(transfer_cmd, state);
5617
5618 if (transfer_cmd->source_count == 1U) {
5619 struct pvr_transfer_cmd_source *src = &transfer_cmd->sources[0];
5620
5621 /* Try to work out a condition to map pixel formats to RAW. That is only
5622 * possible if we don't perform any kind of 2D operation on the blit as we
5623 * don't know the actual pixel values - i.e. it has to be point sampled -
5624 * scaling doesn't matter as long as point sampled.
5625 */
5626 if (src->surface.vk_format == transfer_cmd->dst.vk_format &&
5627 state->filter[0] == PVR_FILTER_POINT &&
5628 src->surface.sample_count <= transfer_cmd->dst.sample_count &&
5629 (transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE) == 0U) {
5630 uint32_t bpp;
5631
5632 int_cmd = *transfer_cmd;
5633 active_cmd = &int_cmd;
5634 bpp = vk_format_get_blocksizebits(int_cmd.dst.vk_format);
5635
5636 if (bpp > 0U) {
5637 switch (bpp) {
5638 case 8U:
5639 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R8_UINT;
5640 break;
5641 case 16U:
5642 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R8G8_UINT;
5643 break;
5644 case 24U:
5645 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R8G8B8_UINT;
5646 break;
5647 case 32U:
5648 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R32_UINT;
5649 break;
5650 case 48U:
5651 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R16G16B16_UINT;
5652 break;
5653 case 64U:
5654 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R32G32_UINT;
5655 break;
5656 case 96U:
5657 int_cmd.sources[0].surface.vk_format = VK_FORMAT_R32G32B32_UINT;
5658 break;
5659 case 128U:
5660 int_cmd.sources[0].surface.vk_format =
5661 VK_FORMAT_R32G32B32A32_UINT;
5662 break;
5663 default:
5664 active_cmd = transfer_cmd;
5665 break;
5666 }
5667 }
5668
5669 int_cmd.dst.vk_format = int_cmd.sources[0].surface.vk_format;
5670 }
5671 }
5672
5673 if (pass_idx == 0U) {
5674 pvr_get_custom_mapping(dev_info, active_cmd, 3U, &state->custom_mapping);
5675
5676 if (state->custom_mapping.texel_extend_src > 1U)
5677 state->custom_mapping.texel_extend_dst = 1U;
5678 }
5679
5680 if (state->custom_mapping.pass_count > 0U) {
5681 struct pvr_transfer_pass *pass = &state->custom_mapping.passes[pass_idx];
5682
5683 if (active_cmd != &int_cmd) {
5684 int_cmd = *active_cmd;
5685 active_cmd = &int_cmd;
5686 }
5687
5688 state->custom_filter = true;
5689
5690 pvr_modify_command(&state->custom_mapping, pass_idx, active_cmd);
5691
5692 if (state->custom_mapping.double_stride ||
5693 pass->sources[0].mapping_count > 1U || pass->source_count > 1U) {
5694 result =
5695 pvr_3d_clip_blit(ctx, active_cmd, prep_data, pass_idx, finished_out);
5696 } else {
5697 struct pvr_rect_mapping *mappings = &pass->sources[0].mappings[0U];
5698
5699 mappings[0U].src_rect.offset.x /=
5700 MAX2(1U, state->custom_mapping.texel_extend_dst);
5701 mappings[0U].src_rect.extent.width /=
5702 MAX2(1U, state->custom_mapping.texel_extend_dst);
5703
5704 if (int_cmd.source_count > 0) {
5705 for (uint32_t i = 0U; i < pass->sources[0].mapping_count; i++)
5706 active_cmd->sources[0].mappings[i] = mappings[i];
5707 }
5708
5709 active_cmd->scissor = mappings[0U].dst_rect;
5710
5711 result = pvr_3d_copy_blit_core(ctx,
5712 active_cmd,
5713 prep_data,
5714 pass_idx,
5715 finished_out);
5716 }
5717
5718 return result;
5719 }
5720
5721 /* Route DS merge blits to Clip blit. Background object is used to preserve
5722 * the unmerged channel.
5723 */
5724 if ((transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_DSMERGE) != 0U) {
5725 /* PBE byte mask could be used for DS merge with FastScale. Clearing the
5726 * other channel on a DS merge requires Clip blit.
5727 */
5728 if (!PVR_HAS_ENHANCEMENT(dev_info, 42064) ||
5729 ((transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_FILL) != 0U)) {
5730 return pvr_reroute_to_clip(ctx,
5731 active_cmd,
5732 &active_cmd->scissor,
5733 prep_data,
5734 pass_idx,
5735 finished_out);
5736 }
5737 }
5738
5739 return pvr_3d_copy_blit_core(ctx,
5740 active_cmd,
5741 prep_data,
5742 pass_idx,
5743 finished_out);
5744 }
5745
5746 /* TODO: This should be generated in csbgen. */
5747 #define TEXSTATE_STRIDE_IMAGE_WORD1_TEXADDR_MASK \
5748 BITFIELD64_RANGE(2, (53 - 16) + 1)
5749
5750 static bool pvr_validate_source_addr(pvr_dev_addr_t addr)
5751 {
5752 if (!pvr_dev_addr_is_aligned(
5753 addr,
5754 ROGUE_TEXSTATE_STRIDE_IMAGE_WORD1_TEXADDR_ALIGNMENT)) {
5755 return false;
5756 }
5757
5758 if (addr.addr & ~TEXSTATE_STRIDE_IMAGE_WORD1_TEXADDR_MASK)
5759 return false;
5760
5761 return true;
5762 }
5763
5764 static bool pvr_supports_texel_unwind(struct pvr_transfer_cmd *transfer_cmd)
5765 {
5766 struct pvr_transfer_cmd_surface *dst = &transfer_cmd->dst;
5767
5768 if (transfer_cmd->source_count > 1)
5769 return false;
5770
5771 if (transfer_cmd->source_count) {
5772 struct pvr_transfer_cmd_surface *src = &transfer_cmd->sources[0].surface;
5773
5774 if (src->height == 1) {
5775 if (src->mem_layout != PVR_MEMLAYOUT_LINEAR &&
5776 src->mem_layout != PVR_MEMLAYOUT_TWIDDLED &&
5777 src->mem_layout != PVR_MEMLAYOUT_3DTWIDDLED) {
5778 return false;
5779 }
5780 } else if (src->mem_layout == PVR_MEMLAYOUT_TWIDDLED ||
5781 src->mem_layout == PVR_MEMLAYOUT_3DTWIDDLED) {
5782 if (!pvr_validate_source_addr(src->dev_addr))
5783 return false;
5784 } else {
5785 if (src->mem_layout != PVR_MEMLAYOUT_LINEAR)
5786 return false;
5787 }
5788 }
5789
5790 if (dst->mem_layout != PVR_MEMLAYOUT_LINEAR &&
5791 dst->mem_layout != PVR_MEMLAYOUT_TWIDDLED) {
5792 return false;
5793 }
5794
5795 return true;
5796 }
5797
5798 static bool pvr_3d_validate_addr(struct pvr_transfer_cmd *transfer_cmd)
5799 {
5800 if (!pvr_supports_texel_unwind(transfer_cmd)) {
5801 return pvr_dev_addr_is_aligned(
5802 transfer_cmd->dst.dev_addr,
5803 ROGUE_PBESTATE_STATE_WORD0_ADDRESS_LOW_ALIGNMENT);
5804 }
5805
5806 return true;
5807 }
5808
5809 static void
5810 pvr_submit_info_stream_init(struct pvr_transfer_ctx *ctx,
5811 struct pvr_transfer_prep_data *prep_data,
5812 struct pvr_winsys_transfer_cmd *cmd)
5813 {
5814 const struct pvr_winsys_transfer_regs *const regs = &prep_data->state.regs;
5815 const struct pvr_physical_device *const pdevice = ctx->device->pdevice;
5816 const struct pvr_device_info *const dev_info = &pdevice->dev_info;
5817
5818 uint32_t *stream_ptr = (uint32_t *)cmd->fw_stream;
5819 uint32_t *stream_len_ptr = stream_ptr;
5820
5821 /* Leave space for stream header. */
5822 stream_ptr += pvr_cmd_length(KMD_STREAM_HDR);
5823
5824 memcpy(stream_ptr, &regs->pds_bgnd0_base, sizeof(regs->pds_bgnd0_base));
5825 stream_ptr += pvr_cmd_length(CR_PDS_BGRND0_BASE);
5826
5827 memcpy(stream_ptr, &regs->pds_bgnd1_base, sizeof(regs->pds_bgnd1_base));
5828 stream_ptr += pvr_cmd_length(CR_PDS_BGRND1_BASE);
5829
5830 memcpy(stream_ptr,
5831 &regs->pds_bgnd3_sizeinfo,
5832 sizeof(regs->pds_bgnd3_sizeinfo));
5833 stream_ptr += pvr_cmd_length(CR_PDS_BGRND3_SIZEINFO);
5834
5835 memcpy(stream_ptr, &regs->isp_mtile_base, sizeof(regs->isp_mtile_base));
5836 stream_ptr += pvr_cmd_length(CR_ISP_MTILE_BASE);
5837
5838 STATIC_ASSERT(ARRAY_SIZE(regs->pbe_wordx_mrty) == 9U);
5839 STATIC_ASSERT(sizeof(regs->pbe_wordx_mrty[0]) == sizeof(uint64_t));
5840 memcpy(stream_ptr, regs->pbe_wordx_mrty, sizeof(regs->pbe_wordx_mrty));
5841 stream_ptr += 9U * 2U;
5842
5843 *stream_ptr = regs->isp_bgobjvals;
5844 stream_ptr += pvr_cmd_length(CR_ISP_BGOBJVALS);
5845
5846 *stream_ptr = regs->usc_pixel_output_ctrl;
5847 stream_ptr += pvr_cmd_length(CR_USC_PIXEL_OUTPUT_CTRL);
5848
5849 *stream_ptr = regs->usc_clear_register0;
5850 stream_ptr += pvr_cmd_length(CR_USC_CLEAR_REGISTER);
5851
5852 *stream_ptr = regs->usc_clear_register1;
5853 stream_ptr += pvr_cmd_length(CR_USC_CLEAR_REGISTER);
5854
5855 *stream_ptr = regs->usc_clear_register2;
5856 stream_ptr += pvr_cmd_length(CR_USC_CLEAR_REGISTER);
5857
5858 *stream_ptr = regs->usc_clear_register3;
5859 stream_ptr += pvr_cmd_length(CR_USC_CLEAR_REGISTER);
5860
5861 *stream_ptr = regs->isp_mtile_size;
5862 stream_ptr += pvr_cmd_length(CR_ISP_MTILE_SIZE);
5863
5864 *stream_ptr = regs->isp_render_origin;
5865 stream_ptr += pvr_cmd_length(CR_ISP_RENDER_ORIGIN);
5866
5867 *stream_ptr = regs->isp_ctl;
5868 stream_ptr += pvr_cmd_length(CR_ISP_CTL);
5869
5870 *stream_ptr = regs->isp_aa;
5871 stream_ptr += pvr_cmd_length(CR_ISP_AA);
5872
5873 *stream_ptr = regs->event_pixel_pds_info;
5874 stream_ptr += pvr_cmd_length(CR_EVENT_PIXEL_PDS_INFO);
5875
5876 *stream_ptr = regs->event_pixel_pds_code;
5877 stream_ptr += pvr_cmd_length(CR_EVENT_PIXEL_PDS_CODE);
5878
5879 *stream_ptr = regs->event_pixel_pds_data;
5880 stream_ptr += pvr_cmd_length(CR_EVENT_PIXEL_PDS_DATA);
5881
5882 *stream_ptr = regs->isp_render;
5883 stream_ptr += pvr_cmd_length(CR_ISP_RENDER);
5884
5885 *stream_ptr = regs->isp_rgn;
5886 stream_ptr++;
5887
5888 if (PVR_HAS_FEATURE(dev_info, gpu_multicore_support)) {
5889 *stream_ptr = regs->frag_screen;
5890 stream_ptr++;
5891 }
5892
5893 cmd->fw_stream_len = (uint8_t *)stream_ptr - (uint8_t *)cmd->fw_stream;
5894 assert(cmd->fw_stream_len <= ARRAY_SIZE(cmd->fw_stream));
5895
5896 pvr_csb_pack ((uint64_t *)stream_len_ptr, KMD_STREAM_HDR, value) {
5897 value.length = cmd->fw_stream_len;
5898 }
5899 }
5900
5901 static void
5902 pvr_submit_info_flags_init(const struct pvr_device_info *const dev_info,
5903 const struct pvr_transfer_prep_data *const prep_data,
5904 struct pvr_winsys_transfer_cmd_flags *flags)
5905 {
5906 *flags = prep_data->flags;
5907 flags->use_single_core = PVR_HAS_FEATURE(dev_info, gpu_multicore_support);
5908 }
5909
5910 static void pvr_transfer_job_ws_submit_info_init(
5911 struct pvr_transfer_ctx *ctx,
5912 struct pvr_transfer_submit *submit,
5913 struct vk_sync *wait,
5914 struct pvr_winsys_transfer_submit_info *submit_info)
5915 {
5916 const struct pvr_device *const device = ctx->device;
5917 const struct pvr_device_info *const dev_info = &device->pdevice->dev_info;
5918
5919 submit_info->frame_num = device->global_queue_present_count;
5920 submit_info->job_num = device->global_cmd_buffer_submit_count;
5921 submit_info->wait = wait;
5922 submit_info->cmd_count = submit->prep_count;
5923
5924 for (uint32_t i = 0U; i < submit->prep_count; i++) {
5925 struct pvr_winsys_transfer_cmd *const cmd = &submit_info->cmds[i];
5926 struct pvr_transfer_prep_data *prep_data = &submit->prep_array[i];
5927
5928 pvr_submit_info_stream_init(ctx, prep_data, cmd);
5929 pvr_submit_info_flags_init(dev_info, prep_data, &cmd->flags);
5930 }
5931 }
5932
5933 static VkResult pvr_submit_transfer(struct pvr_transfer_ctx *ctx,
5934 struct pvr_transfer_submit *submit,
5935 struct vk_sync *wait,
5936 struct vk_sync *signal_sync)
5937 {
5938 struct pvr_winsys_transfer_submit_info submit_info;
5939
5940 pvr_transfer_job_ws_submit_info_init(ctx, submit, wait, &submit_info);
5941
5942 return ctx->device->ws->ops->transfer_submit(ctx->ws_ctx,
5943 &submit_info,
5944 &ctx->device->pdevice->dev_info,
5945 signal_sync);
5946 }
5947
5948 static VkResult pvr_queue_transfer(struct pvr_transfer_ctx *ctx,
5949 struct pvr_transfer_cmd *transfer_cmd,
5950 struct vk_sync *wait,
5951 struct vk_sync *signal_sync)
5952 {
5953 struct pvr_transfer_prep_data *prep_data = NULL;
5954 struct pvr_transfer_prep_data *prev_prep_data;
5955 struct pvr_transfer_submit submit = { 0U };
5956 bool finished = false;
5957 uint32_t pass = 0U;
5958 VkResult result;
5959
5960 /* Transfer queue might decide to do a blit in multiple passes. When the
5961 * prepare doesn't set the finished flag this code will keep calling the
5962 * prepare with increasing pass. If queued transfers are submitted from
5963 * here we submit them straight away. That's why we only need a single
5964 * prepare for the blit rather then one for each pass. Otherwise we insert
5965 * each prepare into the prepare array. When the client does blit batching
5966 * and we split the blit into multiple passes each pass in each queued
5967 * transfer adds one more prepare. Thus the prepare array after 2
5968 * pvr_queue_transfer calls might look like:
5969 *
5970 * +------+------++-------+-------+-------+
5971 * |B0/P0 |B0/P1 || B1/P0 | B1/P1 | B1/P2 |
5972 * +------+------++-------+-------+-------+
5973 * F S/U F S/U
5974 *
5975 * Bn/Pm : nth blit (queue transfer call) / mth prepare
5976 * F : fence point
5977 * S/U : update / server sync update point
5978 */
5979
5980 while (!finished) {
5981 prev_prep_data = prep_data;
5982 prep_data = &submit.prep_array[submit.prep_count++];
5983
5984 /* Clear down the memory before we write to this prep. */
5985 memset(prep_data, 0U, sizeof(*prep_data));
5986
5987 if (pass == 0U) {
5988 if (!pvr_3d_validate_addr(transfer_cmd))
5989 return vk_error(ctx->device, VK_ERROR_FEATURE_NOT_PRESENT);
5990 } else {
5991 /* Transfer queue workarounds could use more than one pass with 3D
5992 * path.
5993 */
5994 prep_data->state = prev_prep_data->state;
5995 }
5996
5997 if (transfer_cmd->flags & PVR_TRANSFER_CMD_FLAGS_FAST2D) {
5998 result =
5999 pvr_3d_clip_blit(ctx, transfer_cmd, prep_data, pass, &finished);
6000 } else {
6001 result =
6002 pvr_3d_copy_blit(ctx, transfer_cmd, prep_data, pass, &finished);
6003 }
6004 if (result != VK_SUCCESS)
6005 return result;
6006
6007 /* Submit if we have finished the blit or if we are out of prepares. */
6008 if (finished || submit.prep_count == ARRAY_SIZE(submit.prep_array)) {
6009 result = pvr_submit_transfer(ctx,
6010 &submit,
6011 wait,
6012 finished ? signal_sync : NULL);
6013 if (result != VK_SUCCESS)
6014 return result;
6015
6016 /* Check if we need to reset prep_count. */
6017 if (submit.prep_count == ARRAY_SIZE(submit.prep_array))
6018 submit.prep_count = 0U;
6019 }
6020
6021 pass++;
6022 }
6023
6024 return VK_SUCCESS;
6025 }
6026
6027 VkResult pvr_arch_transfer_job_submit(struct pvr_transfer_ctx *ctx,
6028 struct pvr_sub_cmd_transfer *sub_cmd,
6029 struct vk_sync *wait_sync,
6030 struct vk_sync *signal_sync)
6031 {
6032 list_for_each_entry_safe (struct pvr_transfer_cmd,
6033 transfer_cmd,
6034 sub_cmd->transfer_cmds,
6035 link) {
6036 /* The fw guarantees that any kick on the same context will be
6037 * synchronized in submission order. This means only the first kick must
6038 * wait, and only the last kick need signal.
6039 */
6040 struct vk_sync *first_cmd_wait_sync = NULL;
6041 struct vk_sync *last_cmd_signal_sync = NULL;
6042 VkResult result;
6043
6044 if (list_first_entry(sub_cmd->transfer_cmds,
6045 struct pvr_transfer_cmd,
6046 link) == transfer_cmd) {
6047 first_cmd_wait_sync = wait_sync;
6048 }
6049
6050 if (list_last_entry(sub_cmd->transfer_cmds,
6051 struct pvr_transfer_cmd,
6052 link) == transfer_cmd) {
6053 last_cmd_signal_sync = signal_sync;
6054 }
6055
6056 result = pvr_queue_transfer(ctx,
6057 transfer_cmd,
6058 first_cmd_wait_sync,
6059 last_cmd_signal_sync);
6060 if (result != VK_SUCCESS)
6061 return result;
6062 }
6063
6064 return VK_SUCCESS;
6065 }