Debian Codegraph @

src/imagination/vulkan/pvr_arch_spm.c

raw
1 /*
2 * Copyright © 2023 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include <stdint.h>
8 #include <string.h>
9 #include <vulkan/vulkan_core.h>
10
11 #include "hwdef/rogue_hw_utils.h"
12 #include "pvr_bo.h"
13 #include "pvr_csb.h"
14 #include "pvr_csb_enum_helpers.h"
15 #include "pvr_device.h"
16 #include "pvr_device_info.h"
17 #include "pvr_formats.h"
18 #include "pvr_framebuffer.h"
19 #include "pvr_hw_pass.h"
20 #include "pvr_job_common.h"
21 #include "pvr_macros.h"
22 #include "pvr_pass.h"
23 #include "pvr_pds.h"
24 #include "pvr_physical_device.h"
25 #include "pvr_spm.h"
26 #include "pvr_tex_state.h"
27 #include "pvr_types.h"
28 #include "pvr_usc.h"
29 #include "util/macros.h"
30 #include "vk_alloc.h"
31 #include "vk_log.h"
32
33 struct pvr_spm_scratch_buffer {
34 uint32_t ref_count;
35 struct pvr_bo *bo;
36 uint64_t size;
37 };
38
39 uint64_t pvr_arch_spm_scratch_buffer_calc_required_size(
40 const struct pvr_renderpass_hwsetup_render *renders,
41 uint32_t render_count,
42 uint32_t sample_count,
43 uint32_t framebuffer_width,
44 uint32_t framebuffer_height)
45 {
46 uint64_t dwords_per_pixel;
47 uint64_t buffer_size;
48
49 uint32_t max_used_tile_buffers = 0;
50
51 /* The maximum number of output registers / tile buffer dwords it ever
52 * encountered when allocating space in the output registers and any tile
53 * buffers.
54 */
55 uint32_t max_used_dwords = 1;
56
57 for (uint32_t i = 0; i < render_count; i++) {
58 const struct pvr_renderpass_hwsetup_render *hw_render = &renders[i];
59
60 max_used_tile_buffers = MAX2(max_used_tile_buffers, hw_render->tile_buffers_count);
61 max_used_dwords = MAX2(max_used_dwords, hw_render->output_regs_count);
62 }
63
64 /* Add one to the number of tile buffers to account for the output registers,
65 * which are always used
66 */
67 dwords_per_pixel = (uint64_t)sample_count * max_used_dwords * (1 + max_used_tile_buffers);
68
69 buffer_size = ALIGN_POT((uint64_t)framebuffer_width,
70 ROGUE_CR_PBE_WORD0_MRT0_LINESTRIDE_ALIGNMENT);
71 buffer_size *=
72 (uint64_t)framebuffer_height * PVR_DW_TO_BYTES(dwords_per_pixel);
73
74 return buffer_size;
75 }
76
77 VkResult pvr_arch_device_init_spm_load_state(struct pvr_device *device)
78 {
79 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
80 uint32_t pds_texture_aligned_offsets[PVR_NUM_SPM_LOAD_SHADERS];
81 uint32_t pds_kick_aligned_offsets[PVR_NUM_SPM_LOAD_SHADERS];
82 uint32_t usc_aligned_offsets[PVR_NUM_SPM_LOAD_SHADERS];
83 pco_shader *shaders[PVR_NUM_SPM_LOAD_SHADERS];
84 uint32_t pds_allocation_size = 0;
85 uint32_t usc_allocation_size = 0;
86 struct pvr_suballoc_bo *pds_bo;
87 struct pvr_suballoc_bo *usc_bo;
88 uint8_t *mem_ptr;
89 VkResult result;
90
91 /* TODO: We don't need to upload all the programs since the set contains
92 * programs for devices with 8 output regs as well. We can save some memory
93 * by not uploading them on devices without the feature.
94 * It's likely that once the compiler is hooked up we'll be using the shader
95 * cache and generate the shaders as needed so this todo will be unnecessary.
96 */
97
98 /* Build and upload USC shaders. */
99
100 struct pvr_spm_load_props props;
101
102 for (unsigned is_multisampled = 0; is_multisampled <= 1; ++is_multisampled) {
103 for (unsigned output_reg_count_log2 = 0; output_reg_count_log2 <= 2;
104 ++output_reg_count_log2) {
105 unsigned output_reg_count = 1 << output_reg_count_log2;
106
107 props = (struct pvr_spm_load_props){
108 .output_reg_count = output_reg_count,
109 .tile_buffer_count = 0,
110 .is_multisampled = is_multisampled,
111 };
112
113 unsigned u = pvr_uscgen_spm_load_index(&props);
114 shaders[u] = pvr_uscgen_spm_load(device->pdevice->pco_ctx, &props);
115 usc_allocation_size += pco_shader_binary_size(shaders[u]);
116
117 if (output_reg_count != 4)
118 continue;
119
120 for (unsigned tile_buffer_count = 1; tile_buffer_count <= 7;
121 ++tile_buffer_count) {
122 props.tile_buffer_count = tile_buffer_count;
123
124 u = pvr_uscgen_spm_load_index(&props);
125 shaders[u] = pvr_uscgen_spm_load(device->pdevice->pco_ctx, &props);
126 usc_allocation_size += pco_shader_binary_size(shaders[u]);
127 }
128 }
129 }
130
131 result = pvr_bo_suballoc(&device->suballoc_usc,
132 usc_allocation_size,
133 4,
134 false,
135 &usc_bo);
136 if (result != VK_SUCCESS)
137 return result;
138
139 mem_ptr = (uint8_t *)pvr_bo_suballoc_get_map_addr(usc_bo);
140
141 unsigned offset = 0;
142 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u) {
143 unsigned shader_size = pco_shader_binary_size(shaders[u]);
144
145 usc_aligned_offsets[u] = offset;
146 memcpy(&mem_ptr[offset], pco_shader_binary_data(shaders[u]), shader_size);
147
148 offset += shader_size;
149 }
150
151 /* Upload PDS programs. */
152
153 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u) {
154 struct pvr_pds_pixel_shader_sa_program pds_texture_program = {
155 /* DMA for clear colors and tile buffer address parts. */
156 .num_texture_dma_kicks = 1,
157 };
158 struct pvr_pds_kickusc_program pds_kick_program = { 0 };
159
160 /* TODO: This looks a bit odd and isn't consistent with other code where
161 * we're getting the size of the PDS program. Can we improve this?
162 */
163 pvr_pds_set_sizes_pixel_shader_uniform_texture_code(&pds_texture_program);
164 pvr_pds_set_sizes_pixel_shader_sa_texture_data(&pds_texture_program,
165 dev_info);
166
167 /* TODO: Looking at the pvr_pds_generate_...() functions and the run-time
168 * behavior the data size is always the same here. Should we try saving
169 * some memory by adjusting things based on that?
170 */
171 device->spm_load_state.load_program[u].pds_texture_program_data_size =
172 pds_texture_program.data_size;
173
174 pds_texture_aligned_offsets[u] = pds_allocation_size;
175 /* FIXME: Figure out the define for alignment of 16. */
176 pds_allocation_size +=
177 ALIGN_POT(PVR_DW_TO_BYTES(pds_texture_program.code_size), 16);
178
179 pvr_pds_set_sizes_pixel_shader(&pds_kick_program);
180
181 pds_kick_aligned_offsets[u] = pds_allocation_size;
182 /* FIXME: Figure out the define for alignment of 16. */
183 pds_allocation_size +=
184 ALIGN_POT(PVR_DW_TO_BYTES(pds_kick_program.code_size +
185 pds_kick_program.data_size),
186 16);
187 }
188
189 /* FIXME: Figure out the define for alignment of 16. */
190 result = pvr_bo_suballoc(&device->suballoc_pds,
191 pds_allocation_size,
192 16,
193 false,
194 &pds_bo);
195 if (result != VK_SUCCESS) {
196 pvr_bo_suballoc_free(usc_bo);
197 return result;
198 }
199
200 mem_ptr = (uint8_t *)pvr_bo_suballoc_get_map_addr(pds_bo);
201
202 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u) {
203 struct pvr_pds_pixel_shader_sa_program pds_texture_program = {
204 /* DMA for clear colors and tile buffer address parts. */
205 .num_texture_dma_kicks = 1,
206 };
207 const pvr_dev_addr_t usc_program_dev_addr =
208 PVR_DEV_ADDR_OFFSET(usc_bo->dev_addr, usc_aligned_offsets[u]);
209 struct pvr_pds_kickusc_program pds_kick_program = { 0 };
210
211 pco_data *shader_data = pco_shader_data(shaders[u]);
212
213 pvr_pds_generate_pixel_shader_sa_code_segment(
214 &pds_texture_program,
215 (uint32_t *)(mem_ptr + pds_texture_aligned_offsets[u]));
216
217 pvr_pds_setup_doutu(&pds_kick_program.usc_task_control,
218 usc_program_dev_addr.addr,
219 shader_data->common.temps,
220 shader_data->fs.uses.sample_shading
221 ? ROGUE_PDSINST_DOUTU_SAMPLE_RATE_FULL
222 : ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
223 false);
224
225 /* Generated both code and data. */
226 pvr_pds_generate_pixel_shader_program(
227 &pds_kick_program,
228 (uint32_t *)(mem_ptr + pds_kick_aligned_offsets[u]));
229
230 device->spm_load_state.load_program[u].pds_pixel_program_offset =
231 PVR_DEV_ADDR_OFFSET(pds_bo->dev_addr, pds_kick_aligned_offsets[u]);
232 device->spm_load_state.load_program[u].pds_uniform_program_offset =
233 PVR_DEV_ADDR_OFFSET(pds_bo->dev_addr, pds_texture_aligned_offsets[u]);
234
235 /* TODO: From looking at the pvr_pds_generate_...() functions, it seems
236 * like temps_used is always 1. Should we remove this and hard code it
237 * with a define in the PDS code?
238 */
239 device->spm_load_state.load_program[u].pds_texture_program_temps_count =
240 pds_texture_program.temps_used;
241 }
242
243 device->spm_load_state.usc_programs = usc_bo;
244 device->spm_load_state.pds_programs = pds_bo;
245
246 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u)
247 ralloc_free(shaders[u]);
248
249 return VK_SUCCESS;
250 }
251
252 void pvr_arch_device_finish_spm_load_state(struct pvr_device *device)
253 {
254 pvr_bo_suballoc_free(device->spm_load_state.pds_programs);
255 pvr_bo_suballoc_free(device->spm_load_state.usc_programs);
256 }
257
258 static inline enum ROGUE_PBESTATE_PACKMODE
259 pvr_spm_get_pbe_packmode(uint32_t dword_count)
260 {
261 switch (dword_count) {
262 case 1:
263 return ROGUE_PBESTATE_PACKMODE_U32;
264 case 2:
265 return ROGUE_PBESTATE_PACKMODE_U32U32;
266 case 3:
267 return ROGUE_PBESTATE_PACKMODE_U32U32U32;
268 case 4:
269 return ROGUE_PBESTATE_PACKMODE_U32U32U32U32;
270 default:
271 UNREACHABLE("Unsupported dword_count");
272 }
273 }
274
275 /**
276 * \brief Sets up PBE registers and state values per a single render output.
277 *
278 * On a PR we want to store tile data to the scratch buffer so we need to
279 * setup the Pixel Back End (PBE) to write the data to the scratch buffer. This
280 * function sets up the PBE state and register values required to do so, for a
281 * single resource whether it be a tile buffer or the output register set.
282 *
283 * \return Size of the data saved into the scratch buffer in bytes.
284 */
285 static uint64_t pvr_spm_setup_pbe_state(
286 const struct pvr_device_info *dev_info,
287 const VkExtent2D *framebuffer_size,
288 uint32_t dword_count,
289 enum pvr_pbe_source_start_pos source_start,
290 uint32_t sample_count,
291 pvr_dev_addr_t scratch_buffer_addr,
292 uint32_t pbe_state_words_out[static const ROGUE_NUM_PBESTATE_STATE_WORDS],
293 uint64_t pbe_reg_words_out[static const ROGUE_NUM_PBESTATE_REG_WORDS])
294 {
295 const uint32_t stride =
296 ALIGN_POT(framebuffer_size->width,
297 ROGUE_PBESTATE_REG_WORD0_LINESTRIDE_UNIT_SIZE);
298
299 const struct pvr_pbe_surf_params surface_params = {
300 .swizzle = {
301 [0] = PIPE_SWIZZLE_X,
302 [1] = PIPE_SWIZZLE_Y,
303 [2] = PIPE_SWIZZLE_Z,
304 [3] = PIPE_SWIZZLE_W,
305 },
306 .pbe_packmode = pvr_spm_get_pbe_packmode(dword_count),
307 .source_format = ROGUE_PBESTATE_SOURCE_FORMAT_8_PER_CHANNEL,
308 .addr = scratch_buffer_addr,
309 .mem_layout = PVR_MEMLAYOUT_LINEAR,
310 .stride = stride,
311 };
312 const struct pvr_pbe_render_params render_params = {
313 .max_x_clip = framebuffer_size->width - 1,
314 .max_y_clip = framebuffer_size->height - 1,
315 .source_start = source_start,
316 };
317
318 pvr_arch_pbe_pack_state(dev_info,
319 &surface_params,
320 &render_params,
321 pbe_state_words_out,
322 pbe_reg_words_out);
323
324 return (uint64_t)stride * framebuffer_size->height * sample_count *
325 PVR_DW_TO_BYTES(dword_count);
326 }
327
328 static inline void pvr_set_pbe_all_valid_mask(struct usc_mrt_desc *desc)
329 {
330 for (uint32_t i = 0; i < ARRAY_SIZE(desc->valid_mask); i++)
331 desc->valid_mask[i] = ~0;
332 }
333
334 /**
335 * \brief Sets up PBE registers, PBE state values and MRT data per a single
336 * render output requiring 8 dwords to be written.
337 *
338 * On a PR we want to store tile data to the scratch buffer so we need to
339 * setup the Pixel Back End (PBE) to write the data to the scratch buffer, as
340 * well as setup the Multiple Render Target (MRT) info so the compiler knows
341 * what data needs to be stored (output regs or tile buffers) and generate the
342 * appropriate EOT shader.
343 *
344 * This function is only available for devices with the eight_output_registers
345 * feature thus requiring 8 dwords to be stored.
346 *
347 * \return Size of the data saved into the scratch buffer in bytes.
348 */
349 static uint64_t pvr_spm_setup_pbe_eight_dword_write(
350 const struct pvr_device_info *dev_info,
351 const VkExtent2D *framebuffer_size,
352 uint32_t sample_count,
353 enum usc_mrt_resource_type source_type,
354 uint32_t tile_buffer_idx,
355 pvr_dev_addr_t scratch_buffer_addr,
356 uint32_t pbe_state_word_0_out[static const ROGUE_NUM_PBESTATE_STATE_WORDS],
357 uint32_t pbe_state_word_1_out[static const ROGUE_NUM_PBESTATE_STATE_WORDS],
358 uint64_t pbe_reg_word_0_out[static const ROGUE_NUM_PBESTATE_REG_WORDS],
359 uint64_t pbe_reg_word_1_out[static const ROGUE_NUM_PBESTATE_REG_WORDS],
360 uint32_t *render_target_used_out)
361 {
362 const uint32_t max_pbe_write_size_dw = 4;
363 uint32_t render_target_used = 0;
364 uint64_t mem_stored;
365
366 assert(PVR_HAS_FEATURE(dev_info, eight_output_registers));
367 assert(source_type != USC_MRT_RESOURCE_TYPE_INVALID);
368
369 /* To store 8 dwords we need to split this into two
370 * ROGUE_PBESTATE_PACKMODE_U32U32U32U32 stores with the second one using
371 * PVR_PBE_STARTPOS_BIT128 as the source offset to store the last 4 dwords.
372 */
373
374 mem_stored = pvr_spm_setup_pbe_state(dev_info,
375 framebuffer_size,
376 max_pbe_write_size_dw,
377 PVR_PBE_STARTPOS_BIT0,
378 sample_count,
379 scratch_buffer_addr,
380 pbe_state_word_0_out,
381 pbe_reg_word_0_out);
382
383 scratch_buffer_addr = PVR_DEV_ADDR_OFFSET(scratch_buffer_addr, mem_stored);
384
385 render_target_used++;
386
387 mem_stored += pvr_spm_setup_pbe_state(dev_info,
388 framebuffer_size,
389 max_pbe_write_size_dw,
390 PVR_PBE_STARTPOS_BIT128,
391 sample_count,
392 scratch_buffer_addr,
393 pbe_state_word_1_out,
394 pbe_reg_word_1_out);
395
396 scratch_buffer_addr = PVR_DEV_ADDR_OFFSET(scratch_buffer_addr, mem_stored);
397
398 render_target_used++;
399 *render_target_used_out = render_target_used;
400
401 return mem_stored;
402 }
403
404 /**
405 * \brief Create and upload the EOT PDS program.
406 *
407 * Essentially DOUTU the USC EOT shader.
408 */
409 /* TODO: See if we can dedup this with
410 * pvr_sub_cmd_gfx_per_job_fragment_programs_create_and_upload().
411 */
412 static VkResult pvr_pds_pixel_event_program_create_and_upload(
413 struct pvr_device *device,
414 const struct pvr_suballoc_bo *usc_eot_program,
415 uint32_t usc_temp_count,
416 struct pvr_pds_upload *const pds_upload_out)
417 {
418 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
419 struct pvr_pds_event_program program = { 0 };
420 uint32_t *staging_buffer;
421 VkResult result;
422
423 pvr_pds_setup_doutu(&program.task_control,
424 usc_eot_program->dev_addr.addr,
425 usc_temp_count,
426 ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
427 false);
428
429 staging_buffer =
430 vk_alloc(&device->vk.alloc,
431 PVR_DW_TO_BYTES(device->pixel_event_data_size_in_dwords),
432 8,
433 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
434 if (!staging_buffer)
435 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
436
437 pvr_pds_generate_pixel_event_data_segment(&program,
438 staging_buffer,
439 dev_info);
440
441 result = pvr_gpu_upload_pds(device,
442 staging_buffer,
443 device->pixel_event_data_size_in_dwords,
444 4,
445 NULL,
446 0,
447 0,
448 4,
449 pds_upload_out);
450 vk_free(&device->vk.alloc, staging_buffer);
451 return result;
452 }
453
454 /**
455 * \brief Sets up the End of Tile (EOT) program for SPM.
456 *
457 * This sets up an EOT program to store the render pass'es on-chip and
458 * off-chip tile data to the SPM scratch buffer on the EOT event.
459 */
460 VkResult pvr_arch_spm_init_eot_state(
461 struct pvr_device *device,
462 struct pvr_spm_eot_state *spm_eot_state,
463 const struct pvr_render_state *rstate,
464 const struct pvr_renderpass_hwsetup_render *hw_render)
465 {
466 const VkExtent2D framebuffer_size = {
467 .width = rstate->width,
468 .height = rstate->height,
469 };
470 uint32_t pbe_state_words[PVR_MAX_COLOR_ATTACHMENTS]
471 [ROGUE_NUM_PBESTATE_STATE_WORDS];
472 uint64_t tile_buffer_addrs[PVR_MAX_COLOR_ATTACHMENTS] = {0};
473 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
474 uint32_t total_render_target_used = 0;
475 struct pvr_pds_upload pds_eot_program;
476 struct pvr_eot_props props;
477 uint32_t usc_temp_count;
478 pco_shader *eot;
479 VkResult result;
480
481 pvr_dev_addr_t next_scratch_buffer_addr =
482 rstate->scratch_buffer->bo->vma->dev_addr;
483 uint64_t mem_stored;
484
485 /* TODO: See if instead of having a separate path for devices with 8 output
486 * regs we can instead do this in a loop and dedup some stuff.
487 */
488 assert(util_is_power_of_two_or_zero(hw_render->output_regs_count) &&
489 hw_render->output_regs_count <= 8);
490 if (hw_render->output_regs_count == 8) {
491 uint32_t render_targets_used;
492
493 /* Store on-chip tile data (i.e. output regs). */
494
495 mem_stored = pvr_spm_setup_pbe_eight_dword_write(
496 dev_info,
497 &framebuffer_size,
498 hw_render->sample_count,
499 USC_MRT_RESOURCE_TYPE_OUTPUT_REG,
500 0,
501 next_scratch_buffer_addr,
502 pbe_state_words[total_render_target_used],
503 pbe_state_words[total_render_target_used + 1],
504 spm_eot_state->pbe_reg_words[total_render_target_used],
505 spm_eot_state->pbe_reg_words[total_render_target_used + 1],
506 &render_targets_used);
507
508 next_scratch_buffer_addr =
509 PVR_DEV_ADDR_OFFSET(next_scratch_buffer_addr, mem_stored);
510 total_render_target_used += render_targets_used;
511
512 /* Store off-chip tile data (i.e. tile buffers). */
513
514 for (uint32_t i = 0; i < hw_render->tile_buffers_count; i++) {
515 assert(!"Add support for tile buffers in EOT");
516 pvr_finishme("Add support for tile buffers in EOT");
517
518 /* `+ 1` since we have 2 emits per tile buffer. */
519 assert(total_render_target_used + 1 < PVR_MAX_COLOR_ATTACHMENTS);
520
521 mem_stored = pvr_spm_setup_pbe_eight_dword_write(
522 dev_info,
523 &framebuffer_size,
524 hw_render->sample_count,
525 USC_MRT_RESOURCE_TYPE_MEMORY,
526 i,
527 next_scratch_buffer_addr,
528 pbe_state_words[total_render_target_used],
529 pbe_state_words[total_render_target_used + 1],
530 spm_eot_state->pbe_reg_words[total_render_target_used],
531 spm_eot_state->pbe_reg_words[total_render_target_used + 1],
532 &render_targets_used);
533
534 next_scratch_buffer_addr =
535 PVR_DEV_ADDR_OFFSET(next_scratch_buffer_addr, mem_stored);
536 total_render_target_used += render_targets_used;
537 }
538 } else {
539 /* Store on-chip tile data (i.e. output regs). */
540
541 mem_stored = pvr_spm_setup_pbe_state(
542 dev_info,
543 &framebuffer_size,
544 hw_render->output_regs_count,
545 PVR_PBE_STARTPOS_BIT0,
546 hw_render->sample_count,
547 next_scratch_buffer_addr,
548 pbe_state_words[total_render_target_used],
549 spm_eot_state->pbe_reg_words[total_render_target_used]);
550
551 next_scratch_buffer_addr =
552 PVR_DEV_ADDR_OFFSET(next_scratch_buffer_addr, mem_stored);
553
554 total_render_target_used++;
555
556 /* Store off-chip tile data (i.e. tile buffers). */
557 const struct pvr_device_tile_buffer_state *tile_buffer_state =
558 &device->tile_buffer_state;
559
560 for (uint32_t i = 0; i < hw_render->tile_buffers_count; i++) {
561 assert(total_render_target_used < PVR_MAX_COLOR_ATTACHMENTS);
562 assert(i < tile_buffer_state->buffer_count);
563
564 mem_stored = pvr_spm_setup_pbe_state(
565 dev_info,
566 &framebuffer_size,
567 hw_render->output_regs_count,
568 PVR_PBE_STARTPOS_BIT0,
569 hw_render->sample_count,
570 next_scratch_buffer_addr,
571 pbe_state_words[total_render_target_used],
572 spm_eot_state->pbe_reg_words[total_render_target_used]);
573
574 tile_buffer_addrs[total_render_target_used] = tile_buffer_state->buffers[i]->vma->dev_addr.addr;
575
576 next_scratch_buffer_addr =
577 PVR_DEV_ADDR_OFFSET(next_scratch_buffer_addr, mem_stored);
578
579 total_render_target_used++;
580 }
581 }
582
583 props = (struct pvr_eot_props){
584 .emit_count = total_render_target_used,
585 .shared_words = false,
586 .state_words = pbe_state_words[0],
587 .num_output_regs = hw_render->output_regs_count,
588 .msaa_samples = hw_render->sample_count,
589 };
590
591 memcpy(props.tile_buffer_addrs, tile_buffer_addrs, sizeof(tile_buffer_addrs));
592
593 eot = pvr_usc_eot(device->pdevice->pco_ctx, &props, dev_info);
594 usc_temp_count = pco_shader_data(eot)->common.temps;
595
596 /* TODO: Create a #define in the compiler code to replace the 16. */
597 result = pvr_gpu_upload_usc(device,
598 pco_shader_binary_data(eot),
599 pco_shader_binary_size(eot),
600 16,
601 &spm_eot_state->usc_eot_program);
602
603 ralloc_free(eot);
604
605 if (result != VK_SUCCESS)
606 return result;
607
608 result = pvr_pds_pixel_event_program_create_and_upload(
609 device,
610 spm_eot_state->usc_eot_program,
611 usc_temp_count,
612 &pds_eot_program);
613 if (result != VK_SUCCESS) {
614 pvr_bo_suballoc_free(spm_eot_state->usc_eot_program);
615 return result;
616 }
617
618 spm_eot_state->pixel_event_program_data_upload = pds_eot_program.pvr_bo;
619 spm_eot_state->pixel_event_program_data_offset = pds_eot_program.data_offset;
620
621 return VK_SUCCESS;
622 }
623
624 static VkFormat pvr_get_format_from_dword_count(uint32_t dword_count)
625 {
626 switch (dword_count) {
627 case 1:
628 return VK_FORMAT_R32_UINT;
629 case 2:
630 return VK_FORMAT_R32G32_UINT;
631 case 4:
632 return VK_FORMAT_R32G32B32A32_UINT;
633 default:
634 UNREACHABLE("Invalid dword_count");
635 }
636 }
637
638 static VkResult
639 pvr_spm_setup_texture_state_words(struct pvr_device *device,
640 uint32_t dword_count,
641 const VkExtent2D framebuffer_size,
642 uint32_t sample_count,
643 pvr_dev_addr_t scratch_buffer_addr,
644 void *image_state_ptr,
645 uint64_t *mem_used_out)
646 {
647 const uint64_t aligned_fb_width =
648 ALIGN_POT(framebuffer_size.width,
649 ROGUE_CR_PBE_WORD0_MRT0_LINESTRIDE_ALIGNMENT);
650
651 /* We can ignore the framebuffer's layer count since we only support
652 * writing to layer 0.
653 */
654 struct pvr_texture_state_info info = {
655 .format = pvr_get_format_from_dword_count(dword_count),
656 .mem_layout = PVR_MEMLAYOUT_LINEAR,
657
658 .type = VK_IMAGE_VIEW_TYPE_2D,
659 .tex_state_type = PVR_TEXTURE_STATE_STORAGE,
660 .extent = {
661 .width = framebuffer_size.width,
662 .height = framebuffer_size.height,
663 },
664
665 .mip_levels = 1,
666
667 .sample_count = sample_count,
668 .stride = aligned_fb_width,
669
670 .addr = scratch_buffer_addr,
671 };
672 const uint64_t fb_area = aligned_fb_width * framebuffer_size.height;
673 struct pvr_image_descriptor image_descriptor;
674 const uint8_t *format_swizzle;
675 VkResult result;
676
677 format_swizzle = pvr_get_format_swizzle(info.format);
678 memcpy(info.swizzle, format_swizzle, sizeof(info.swizzle));
679
680 result = pvr_arch_pack_tex_state(device, &info, &image_descriptor);
681 if (result != VK_SUCCESS)
682 return result;
683
684 memcpy(image_state_ptr,
685 image_descriptor.words,
686 sizeof(image_descriptor.words));
687
688 *mem_used_out = fb_area * PVR_DW_TO_BYTES(dword_count) * sample_count;
689
690 return VK_SUCCESS;
691 }
692
693 /* FIXME: Can we dedup this with pvr_load_op_pds_data_create_and_upload() ? */
694 static VkResult pvr_pds_bgnd_program_create_and_upload(
695 struct pvr_device *device,
696 uint32_t texture_program_data_size_in_dwords,
697 const struct pvr_bo *consts_buffer,
698 uint32_t const_shared_regs,
699 struct pvr_pds_upload *pds_upload_out)
700 {
701 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
702 struct pvr_pds_pixel_shader_sa_program texture_program = { 0 };
703 uint32_t staging_buffer_size;
704 uint32_t *staging_buffer;
705 VkResult result;
706
707 pvr_csb_pack (&texture_program.texture_dma_address[0],
708 PDSINST_DOUT_FIELDS_DOUTD_SRC0,
709 doutd_src0) {
710 doutd_src0.sbase = consts_buffer->vma->dev_addr;
711 }
712
713 pvr_csb_pack (&texture_program.texture_dma_control[0],
714 PDSINST_DOUT_FIELDS_DOUTD_SRC1,
715 doutd_src1) {
716 doutd_src1.dest = ROGUE_PDSINST_DOUTD_DEST_COMMON_STORE;
717 doutd_src1.bsize = const_shared_regs;
718 }
719
720 texture_program.num_texture_dma_kicks += 1;
721
722 #if MESA_DEBUG
723 pvr_pds_set_sizes_pixel_shader_sa_texture_data(&texture_program, dev_info);
724 assert(texture_program_data_size_in_dwords == texture_program.data_size);
725 #endif
726
727 staging_buffer_size = PVR_DW_TO_BYTES(texture_program_data_size_in_dwords);
728
729 staging_buffer = vk_alloc(&device->vk.alloc,
730 staging_buffer_size,
731 8,
732 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
733 if (!staging_buffer)
734 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
735
736 pvr_pds_generate_pixel_shader_sa_texture_state_data(&texture_program,
737 staging_buffer,
738 dev_info);
739
740 /* FIXME: Figure out the define for alignment of 16. */
741 result = pvr_gpu_upload_pds(device,
742 &staging_buffer[0],
743 texture_program_data_size_in_dwords,
744 16,
745 NULL,
746 0,
747 0,
748 16,
749 pds_upload_out);
750 if (result != VK_SUCCESS) {
751 vk_free(&device->vk.alloc, staging_buffer);
752 return result;
753 }
754
755 vk_free(&device->vk.alloc, staging_buffer);
756
757 return VK_SUCCESS;
758 }
759
760 VkResult pvr_arch_spm_init_bgobj_state(
761 struct pvr_device *device,
762 struct pvr_spm_bgobj_state *spm_bgobj_state,
763 const struct pvr_render_state *rstate,
764 const struct pvr_renderpass_hwsetup_render *hw_render)
765 {
766 const VkExtent2D framebuffer_size = {
767 .width = rstate->width,
768 .height = rstate->height,
769 };
770 pvr_dev_addr_t next_scratch_buffer_addr =
771 rstate->scratch_buffer->bo->vma->dev_addr;
772 struct pvr_spm_per_load_program_state *load_program_state;
773 struct pvr_pds_upload pds_texture_data_upload;
774 struct pvr_sampler_descriptor *descriptor;
775 uint64_t consts_buffer_size;
776 uint32_t dword_count;
777 uint32_t *mem_ptr;
778 VkResult result;
779
780 /* Even if we might have 8 output regs we can only pack and write 4 dwords
781 * using R32G32B32A32_UINT.
782 */
783 if (hw_render->tile_buffers_count > 0)
784 dword_count = 4;
785 else
786 dword_count = MIN2(hw_render->output_regs_count, 4);
787
788 struct pvr_spm_load_props props = {
789 .output_reg_count = dword_count,
790 .tile_buffer_count = hw_render->tile_buffers_count,
791 .is_multisampled = hw_render->sample_count > 1,
792 };
793
794 const uint32_t spm_load_program_idx = pvr_uscgen_spm_load_index(&props);
795
796 consts_buffer_size = PVR_DW_TO_BYTES(pvr_uscgen_spm_load_data_size(&props));
797
798 result = pvr_bo_alloc(device,
799 device->heaps.general_heap,
800 consts_buffer_size,
801 sizeof(uint32_t),
802 PVR_BO_ALLOC_FLAG_CPU_MAPPED,
803 &spm_bgobj_state->consts_buffer);
804 if (result != VK_SUCCESS)
805 return result;
806
807 mem_ptr = spm_bgobj_state->consts_buffer->bo->map;
808
809 for (unsigned u = 0; u < hw_render->tile_buffers_count; ++u) {
810 unsigned tile_buffer_addr_location = pvr_uscgen_spm_buffer_data(u, true);
811 pvr_dev_addr_t tile_buffer_addr =
812 device->tile_buffer_state.buffers[u]->vma->dev_addr;
813
814 mem_ptr[tile_buffer_addr_location] = tile_buffer_addr.addr & 0xffffffff;
815 mem_ptr[tile_buffer_addr_location + 1] = tile_buffer_addr.addr >> 32;
816 }
817
818 descriptor =
819 (struct pvr_sampler_descriptor *)&mem_ptr[PVR_SPM_LOAD_DATA_SMP];
820 pvr_csb_pack (&descriptor->words[0], TEXSTATE_SAMPLER_WORD0, sampler) {
821 sampler.non_normalized_coords = true;
822 sampler.addrmode_v = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
823 sampler.addrmode_u = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
824 sampler.minfilter = ROGUE_TEXSTATE_FILTER_POINT;
825 sampler.magfilter = ROGUE_TEXSTATE_FILTER_POINT;
826 sampler.maxlod = ROGUE_TEXSTATE_CLAMP_MIN;
827 sampler.minlod = ROGUE_TEXSTATE_CLAMP_MIN;
828 sampler.dadjust = ROGUE_TEXSTATE_DADJUST_ZERO_UINT;
829 }
830
831 pvr_csb_pack (&descriptor->words[1], TEXSTATE_SAMPLER_WORD1, sampler) {}
832
833 uint64_t mem_used = 0;
834 /* Setup image descriptor for reg output. */
835 result =
836 pvr_spm_setup_texture_state_words(device,
837 dword_count,
838 framebuffer_size,
839 hw_render->sample_count,
840 next_scratch_buffer_addr,
841 &mem_ptr[PVR_SPM_LOAD_DATA_REG_TEX],
842 &mem_used);
843 if (result != VK_SUCCESS)
844 goto err_free_consts_buffer;
845
846 next_scratch_buffer_addr =
847 PVR_DEV_ADDR_OFFSET(next_scratch_buffer_addr, mem_used);
848
849 /* Setup image descriptors for tile buffer outputs. */
850 for (unsigned u = 0; u < hw_render->tile_buffers_count; ++u) {
851 unsigned tile_buffer_tex_state_location =
852 pvr_uscgen_spm_buffer_data(u, false);
853
854 result = pvr_spm_setup_texture_state_words(
855 device,
856 dword_count,
857 framebuffer_size,
858 hw_render->sample_count,
859 next_scratch_buffer_addr,
860 &mem_ptr[tile_buffer_tex_state_location],
861 &mem_used);
862 if (result != VK_SUCCESS)
863 goto err_free_consts_buffer;
864
865 next_scratch_buffer_addr =
866 PVR_DEV_ADDR_OFFSET(next_scratch_buffer_addr, mem_used);
867 }
868
869 load_program_state =
870 &device->spm_load_state.load_program[spm_load_program_idx];
871
872 result = pvr_pds_bgnd_program_create_and_upload(
873 device,
874 load_program_state->pds_texture_program_data_size,
875 spm_bgobj_state->consts_buffer,
876 consts_buffer_size,
877 &pds_texture_data_upload);
878 if (result != VK_SUCCESS)
879 goto err_free_consts_buffer;
880
881 spm_bgobj_state->pds_texture_data_upload = pds_texture_data_upload.pvr_bo;
882
883 /* TODO: Is it worth to dedup this with pvr_pds_bgnd_pack_state() ? */
884
885 /* clang-format off */
886 pvr_csb_pack (&spm_bgobj_state->pds_reg_values[0],
887 CR_PDS_BGRND0_BASE,
888 value) {
889 /* clang-format on */
890 value.shader_addr = load_program_state->pds_pixel_program_offset;
891 value.texunicode_addr = load_program_state->pds_uniform_program_offset;
892 }
893
894 /* clang-format off */
895 pvr_csb_pack (&spm_bgobj_state->pds_reg_values[1],
896 CR_PDS_BGRND1_BASE,
897 value) {
898 /* clang-format on */
899 value.texturedata_addr =
900 PVR_DEV_ADDR(pds_texture_data_upload.data_offset);
901 }
902
903 /* clang-format off */
904 pvr_csb_pack (&spm_bgobj_state->pds_reg_values[2],
905 CR_PDS_BGRND3_SIZEINFO,
906 value) {
907 /* clang-format on */
908 value.usc_sharedsize =
909 DIV_ROUND_UP(consts_buffer_size,
910 ROGUE_CR_PDS_BGRND3_SIZEINFO_USC_SHAREDSIZE_UNIT_SIZE);
911 value.pds_texturestatesize = DIV_ROUND_UP(
912 pds_texture_data_upload.data_size,
913 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEXTURESTATESIZE_UNIT_SIZE);
914 value.pds_tempsize =
915 DIV_ROUND_UP(load_program_state->pds_texture_program_temps_count,
916 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEMPSIZE_UNIT_SIZE);
917 }
918
919 return VK_SUCCESS;
920
921 err_free_consts_buffer:
922 pvr_bo_free(device, spm_bgobj_state->consts_buffer);
923
924 return result;
925 }