Debian Codegraph @

src/imagination/vulkan/rogue/pvr_clear.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdint.h>
25 #include <vulkan/vulkan_core.h>
26
27 #include "hwdef/rogue_hw_utils.h"
28 #include "pco/pco_data.h"
29 #include "pco_uscgen_programs.h"
30 #include "pvr_clear.h"
31 #include "pvr_cmd_buffer.h"
32 #include "pvr_device.h"
33 #include "pvr_pds.h"
34 #include "pvr_physical_device.h"
35 #include "pvr_types.h"
36 #include "pvr_usc.h"
37 #include "vk_alloc.h"
38 #include "vk_log.h"
39
40 #define PVR_STATIC_CLEAR_PDS_STATE_COUNT \
41 (pvr_cmd_length(TA_STATE_PDS_SHADERBASE) + \
42 pvr_cmd_length(TA_STATE_PDS_TEXUNICODEBASE) + \
43 pvr_cmd_length(TA_STATE_PDS_SIZEINFO1) + \
44 pvr_cmd_length(TA_STATE_PDS_SIZEINFO2) + \
45 pvr_cmd_length(TA_STATE_PDS_VARYINGBASE) + \
46 pvr_cmd_length(TA_STATE_PDS_TEXTUREDATABASE))
47
48 static_assert(PVR_PDS_STATE_LENGTH == PVR_STATIC_CLEAR_PDS_STATE_COUNT,
49 "pvr_static_clear_ppp_pds_state_type might require fixing.");
50
51 static void pvr_device_setup_graphics_static_clear_ppp_base(
52 struct pvr_static_clear_ppp_base *const base)
53 {
54 pvr_csb_pack (&base->wclamp, TA_WCLAMP, wclamp) {
55 wclamp.val = fui(1.0e-13f);
56 }
57
58 /* clang-format off */
59 pvr_csb_pack (&base->varying_word[0], TA_STATE_VARYING0, varying0);
60 pvr_csb_pack (&base->varying_word[1], TA_STATE_VARYING1, varying1);
61 pvr_csb_pack (&base->varying_word[2], TA_STATE_VARYING2, varying2);
62 /* clang-format on */
63
64 pvr_csb_pack (&base->ppp_ctrl, TA_STATE_PPP_CTRL, ppp_ctrl) {
65 ppp_ctrl.pretransform = true;
66 ppp_ctrl.cullmode = ROGUE_TA_CULLMODE_NO_CULLING;
67 }
68
69 /* clang-format off */
70 pvr_csb_pack (&base->stream_out0, TA_STATE_STREAM_OUT0, stream_out0);
71 /* clang-format on */
72 }
73
74 static void pvr_device_setup_graphics_static_clear_ppp_templates(
75 struct pvr_static_clear_ppp_template
76 templates[static PVR_STATIC_CLEAR_VARIANT_COUNT])
77 {
78 for (uint32_t i = 0; i < PVR_STATIC_CLEAR_VARIANT_COUNT; i++) {
79 const bool has_color = !!(i & VK_IMAGE_ASPECT_COLOR_BIT);
80 const bool has_depth = !!(i & VK_IMAGE_ASPECT_DEPTH_BIT);
81 const bool has_stencil = !!(i & VK_IMAGE_ASPECT_STENCIL_BIT);
82
83 struct pvr_static_clear_ppp_template *const template = &templates[i];
84
85 template->requires_pds_state = has_color;
86
87 pvr_csb_pack (&template->header, TA_STATE_HEADER, header) {
88 header.pres_stream_out_size = true;
89 header.pres_ppp_ctrl = true;
90 header.pres_varying_word2 = true;
91 header.pres_varying_word1 = true;
92 header.pres_varying_word0 = true;
93 header.pres_outselects = true;
94 header.pres_wclamp = true;
95 header.pres_region_clip = true;
96 header.pres_pds_state_ptr2 = template->requires_pds_state;
97 header.pres_pds_state_ptr1 = template->requires_pds_state;
98 header.pres_pds_state_ptr0 = template->requires_pds_state;
99 header.pres_ispctl_fb = true;
100 header.pres_ispctl_fa = true;
101 header.pres_ispctl = true;
102 }
103
104 #define CS_HEADER(cs) \
105 (struct ROGUE_##cs) \
106 { \
107 pvr_cmd_header(cs) \
108 }
109
110 template->config.ispctl = CS_HEADER(TA_STATE_ISPCTL);
111 template->config.ispctl.tagwritedisable = !has_color;
112 template->config.ispctl.bpres = true;
113
114 template->config.ispa = CS_HEADER(TA_STATE_ISPA);
115 template->config.ispa.objtype = ROGUE_TA_OBJTYPE_TRIANGLE;
116 template->config.ispa.passtype = ROGUE_TA_PASSTYPE_TRANSLUCENT;
117 template->config.ispa.dwritedisable = !has_depth;
118 template->config.ispa.dcmpmode = (i == 0) ? ROGUE_TA_CMPMODE_NEVER
119 : ROGUE_TA_CMPMODE_ALWAYS;
120 template->config.ispa.sref =
121 has_stencil ? ROGUE_TA_STATE_ISPA_SREF_SIZE_MAX : 0;
122
123 pvr_csb_pack (&template->ispb, TA_STATE_ISPB, ispb) {
124 ispb.scmpmode = ROGUE_TA_CMPMODE_ALWAYS;
125 ispb.sop1 = ROGUE_TA_ISPB_STENCILOP_KEEP;
126 ispb.sop2 = ROGUE_TA_ISPB_STENCILOP_KEEP;
127
128 ispb.sop3 = has_stencil ? ROGUE_TA_ISPB_STENCILOP_REPLACE
129 : ROGUE_TA_ISPB_STENCILOP_KEEP;
130
131 ispb.swmask = has_stencil ? 0xFF : 0;
132 }
133
134 template->config.pds_state = NULL;
135
136 template->config.region_clip0 = CS_HEADER(TA_REGION_CLIP0);
137 template->config.region_clip0.mode = ROGUE_TA_REGION_CLIP_MODE_OUTSIDE;
138 template->config.region_clip0.left = 0;
139 template->config.region_clip0.right = ROGUE_TA_REGION_CLIP_MAX;
140
141 template->config.region_clip1 = CS_HEADER(TA_REGION_CLIP1);
142 template->config.region_clip1.top = 0;
143 template->config.region_clip1.bottom = ROGUE_TA_REGION_CLIP_MAX;
144
145 template->config.output_sel = CS_HEADER(TA_OUTPUT_SEL);
146 template->config.output_sel.vtxsize = 4;
147 template->config.output_sel.rhw_pres = true;
148
149 #undef CS_HEADER
150 }
151 }
152
153 /**
154 * \brief Emit geom state from a configurable template.
155 *
156 * Note that the state is emitted by joining the template with a base so the
157 * base must have been setup before calling this.
158 *
159 * \param[in] csb Control stream to emit to.
160 * \param[in] template The configured template.
161 * \param[out] pvr_bo_out Uploaded state's pvr_bo object.
162 *
163 * \return VK_SUCCESS if the state was successfully uploaded.
164 */
165 VkResult pvr_emit_ppp_from_template(
166 struct pvr_csb *const csb,
167 const struct pvr_static_clear_ppp_template *const template,
168 struct pvr_suballoc_bo **const pvr_bo_out)
169 {
170 const uint32_t dword_count =
171 pvr_cmd_length(TA_STATE_HEADER) + pvr_cmd_length(TA_STATE_ISPCTL) +
172 pvr_cmd_length(TA_STATE_ISPA) + pvr_cmd_length(TA_STATE_ISPB) +
173 (template->requires_pds_state ? PVR_STATIC_CLEAR_PDS_STATE_COUNT : 0) +
174 pvr_cmd_length(TA_REGION_CLIP0) + pvr_cmd_length(TA_REGION_CLIP1) +
175 pvr_cmd_length(TA_WCLAMP) + pvr_cmd_length(TA_OUTPUT_SEL) +
176 pvr_cmd_length(TA_STATE_VARYING0) + pvr_cmd_length(TA_STATE_VARYING1) +
177 pvr_cmd_length(TA_STATE_VARYING2) + pvr_cmd_length(TA_STATE_PPP_CTRL) +
178 pvr_cmd_length(TA_STATE_STREAM_OUT0);
179
180 struct pvr_device *const device = csb->device;
181 const uint32_t cache_line_size =
182 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
183 const struct pvr_static_clear_ppp_base *const base =
184 &device->static_clear_state->ppp_base;
185 struct pvr_suballoc_bo *pvr_bo;
186 uint32_t *stream;
187 VkResult result;
188
189 result = pvr_bo_suballoc(&device->suballoc_general,
190 PVR_DW_TO_BYTES(dword_count),
191 cache_line_size,
192 false,
193 &pvr_bo);
194 if (result != VK_SUCCESS) {
195 *pvr_bo_out = NULL;
196 return result;
197 }
198
199 stream = (uint32_t *)pvr_bo_suballoc_get_map_addr(pvr_bo);
200
201 pvr_csb_write_value(stream, TA_STATE_HEADER, template->header);
202 pvr_csb_write_struct(stream, TA_STATE_ISPCTL, &template->config.ispctl);
203 pvr_csb_write_struct(stream, TA_STATE_ISPA, &template->config.ispa);
204 pvr_csb_write_value(stream, TA_STATE_ISPB, template->ispb);
205
206 if (template->requires_pds_state) {
207 static_assert(sizeof(*stream) == sizeof((*template->config.pds_state)[0]),
208 "Size mismatch");
209 for (uint32_t i = 0; i < PVR_STATIC_CLEAR_PDS_STATE_COUNT; i++)
210 *stream++ = (*template->config.pds_state)[i];
211 }
212
213 pvr_csb_write_struct(stream,
214 TA_REGION_CLIP0,
215 &template->config.region_clip0);
216 pvr_csb_write_struct(stream,
217 TA_REGION_CLIP1,
218 &template->config.region_clip1);
219 pvr_csb_write_value(stream, TA_WCLAMP, base->wclamp);
220 pvr_csb_write_struct(stream, TA_OUTPUT_SEL, &template->config.output_sel);
221 pvr_csb_write_value(stream, TA_STATE_VARYING0, base->varying_word[0]);
222 pvr_csb_write_value(stream, TA_STATE_VARYING1, base->varying_word[1]);
223 pvr_csb_write_value(stream, TA_STATE_VARYING2, base->varying_word[2]);
224 pvr_csb_write_value(stream, TA_STATE_PPP_CTRL, base->ppp_ctrl);
225 pvr_csb_write_value(stream, TA_STATE_STREAM_OUT0, base->stream_out0);
226
227 assert((uint64_t)(stream - (uint32_t *)pvr_bo_suballoc_get_map_addr(
228 pvr_bo)) == dword_count);
229
230 stream = NULL;
231
232 pvr_csb_set_relocation_mark(csb);
233
234 pvr_csb_emit (csb, VDMCTRL_PPP_STATE0, state) {
235 state.word_count = dword_count;
236 state.addrmsb = pvr_bo->dev_addr;
237 }
238
239 pvr_csb_emit (csb, VDMCTRL_PPP_STATE1, state) {
240 state.addrlsb = pvr_bo->dev_addr;
241 }
242
243 pvr_csb_clear_relocation_mark(csb);
244
245 *pvr_bo_out = pvr_bo;
246
247 return VK_SUCCESS;
248 }
249
250 static VkResult
251 pvr_device_init_clear_attachment_programs(struct pvr_device *device)
252 {
253 const uint32_t pds_prog_alignment =
254 MAX2(ROGUE_TA_STATE_PDS_TEXUNICODEBASE_ADDR_ALIGNMENT,
255 ROGUE_TA_STATE_PDS_SHADERBASE_ADDR_ALIGNMENT);
256 struct pvr_device_static_clear_state *clear_state =
257 device->static_clear_state;
258 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
259 uint32_t pds_texture_program_offsets[PVR_NUM_CLEAR_ATTACH_SHADERS];
260 uint32_t pds_pixel_program_offsets[PVR_NUM_CLEAR_ATTACH_SHADERS];
261 uint32_t usc_program_offsets[PVR_NUM_CLEAR_ATTACH_SHADERS];
262 pco_shader *shaders[PVR_NUM_CLEAR_ATTACH_SHADERS];
263 struct pvr_clear_attach_props props;
264 uint64_t usc_upload_offset;
265 uint64_t pds_upload_offset;
266 uint32_t alloc_size = 0;
267 VkResult result;
268 uint8_t *ptr;
269
270 /* Build and upload USC fragment shaders. */
271 for (unsigned dword_count = 1; dword_count <= 4; ++dword_count) {
272 for (unsigned offset = 0; offset <= 3; ++offset) {
273 for (unsigned uses_tile_buffer = 0; uses_tile_buffer <= 1;
274 ++uses_tile_buffer) {
275 if (dword_count + offset > 4)
276 continue;
277
278 props.dword_count = dword_count;
279 props.offset = offset;
280 props.uses_tile_buffer = uses_tile_buffer;
281
282 unsigned u = pvr_uscgen_clear_attach_index(&props);
283 shaders[u] =
284 pvr_uscgen_clear_attach(device->pdevice->pco_ctx, &props);
285 alloc_size += pco_shader_binary_size(shaders[u]);
286 }
287 }
288 }
289
290 result = pvr_bo_suballoc(&device->suballoc_usc,
291 alloc_size,
292 4,
293 false,
294 &clear_state->usc_clear_attachment_programs);
295 if (result != VK_SUCCESS)
296 return result;
297
298 usc_upload_offset =
299 clear_state->usc_clear_attachment_programs->dev_addr.addr -
300 device->heaps.usc_heap->base_addr.addr;
301
302 ptr = (uint8_t *)pvr_bo_suballoc_get_map_addr(
303 clear_state->usc_clear_attachment_programs);
304
305 unsigned offset = 0;
306 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u) {
307 unsigned shader_size = pco_shader_binary_size(shaders[u]);
308
309 usc_program_offsets[u] = offset;
310 memcpy(&ptr[offset], pco_shader_binary_data(shaders[u]), shader_size);
311
312 offset += shader_size;
313 }
314
315 /* Upload PDS programs. */
316
317 alloc_size = 0;
318
319 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u) {
320 struct pvr_pds_pixel_shader_sa_program texture_pds_program;
321 struct pvr_pds_kickusc_program pixel_shader_pds_program;
322 uint32_t program_size;
323
324 /* Texture program to load colors. */
325 texture_pds_program = (struct pvr_pds_pixel_shader_sa_program){
326 .num_texture_dma_kicks = 1,
327 };
328
329 pvr_pds_set_sizes_pixel_shader_uniform_texture_code(&texture_pds_program);
330
331 pds_texture_program_offsets[u] = alloc_size;
332 alloc_size += ALIGN_POT(PVR_DW_TO_BYTES(texture_pds_program.code_size),
333 pds_prog_alignment);
334
335 /* Pixel program to load fragment shader. */
336 pixel_shader_pds_program = (struct pvr_pds_kickusc_program){ 0 };
337
338 pvr_pds_setup_doutu(&pixel_shader_pds_program.usc_task_control,
339 usc_upload_offset + usc_program_offsets[u],
340 pco_shader_data(shaders[u])->common.temps,
341 ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
342 false);
343
344 pvr_pds_set_sizes_pixel_shader(&pixel_shader_pds_program);
345
346 program_size = pixel_shader_pds_program.code_size +
347 pixel_shader_pds_program.data_size;
348 program_size = PVR_DW_TO_BYTES(program_size);
349
350 pds_pixel_program_offsets[u] = alloc_size;
351 alloc_size += ALIGN_POT(program_size, pds_prog_alignment);
352 }
353
354 result = pvr_bo_suballoc(&device->suballoc_pds,
355 alloc_size,
356 pds_prog_alignment,
357 false,
358 &clear_state->pds_clear_attachment_programs);
359 if (result != VK_SUCCESS) {
360 pvr_bo_suballoc_free(clear_state->usc_clear_attachment_programs);
361 return result;
362 }
363
364 pds_upload_offset =
365 clear_state->pds_clear_attachment_programs->dev_addr.addr -
366 device->heaps.pds_heap->base_addr.addr;
367
368 ptr =
369 pvr_bo_suballoc_get_map_addr(clear_state->pds_clear_attachment_programs);
370
371 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u) {
372 struct pvr_pds_pixel_shader_sa_program texture_pds_program;
373 struct pvr_pds_kickusc_program pixel_shader_pds_program;
374
375 /* Texture program to load colors. */
376 texture_pds_program = (struct pvr_pds_pixel_shader_sa_program){
377 .num_texture_dma_kicks = 1,
378 };
379
380 pvr_pds_generate_pixel_shader_sa_code_segment(
381 &texture_pds_program,
382 (uint32_t *)(ptr + pds_texture_program_offsets[u]));
383
384 /* Pixel program to load fragment shader. */
385 pixel_shader_pds_program = (struct pvr_pds_kickusc_program){ 0 };
386
387 pvr_pds_setup_doutu(&pixel_shader_pds_program.usc_task_control,
388 usc_upload_offset + usc_program_offsets[u],
389 pco_shader_data(shaders[u])->common.temps,
390 ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
391 false);
392
393 pvr_pds_generate_pixel_shader_program(
394 &pixel_shader_pds_program,
395 (uint32_t *)(ptr + pds_pixel_program_offsets[u]));
396
397 /* Setup the PDS program info. */
398 pvr_pds_set_sizes_pixel_shader_sa_texture_data(&texture_pds_program,
399 dev_info);
400
401 clear_state->pds_clear_attachment_program_info[u] =
402 (struct pvr_pds_clear_attachment_program_info){
403 .texture_program_offset =
404 PVR_DEV_ADDR(pds_upload_offset + pds_texture_program_offsets[u]),
405 .pixel_program_offset =
406 PVR_DEV_ADDR(pds_upload_offset + pds_pixel_program_offsets[u]),
407
408 .texture_program_pds_temps_count = texture_pds_program.temps_used,
409 .texture_program_data_size = texture_pds_program.data_size,
410 };
411 }
412
413 for (unsigned u = 0; u < ARRAY_SIZE(shaders); ++u)
414 ralloc_free(shaders[u]);
415
416 return VK_SUCCESS;
417 }
418
419 static void
420 pvr_device_finish_clear_attachment_programs(struct pvr_device *device)
421 {
422 struct pvr_device_static_clear_state *clear_state =
423 device->static_clear_state;
424
425 pvr_bo_suballoc_free(clear_state->usc_clear_attachment_programs);
426 pvr_bo_suballoc_free(clear_state->pds_clear_attachment_programs);
427 }
428
429 /**
430 * \brief Generate and uploads vertices required to clear the rect area.
431 *
432 * We use the triangle strip topology for clears so this functions generates 4
433 * vertices to represent the rect. Note that the coordinates are in screen space
434 * and not NDC.
435 *
436 * \param[in] device Device to upload to.
437 * \param[in] rect Area to clear.
438 * \param[in] depth Depth (i.e. Z coordinate) of the area to clear.
439 * \param[out] pvr_bo_out BO upload object.
440 * \return VK_SUCCESS if the upload succeeded.
441 */
442 VkResult pvr_clear_vertices_upload(struct pvr_device *device,
443 const VkRect2D *rect,
444 float depth,
445 struct pvr_suballoc_bo **const pvr_bo_out)
446 {
447 const float y1 = (float)(rect->offset.y + rect->extent.height);
448 const float x1 = (float)(rect->offset.x + rect->extent.width);
449 const float y0 = (float)rect->offset.y;
450 const float x0 = (float)rect->offset.x;
451
452 const float vertices[PVR_CLEAR_VERTEX_COUNT][PVR_CLEAR_VERTEX_COORDINATES] = {
453 [0] = { [0] = x0, [1] = y0, [2] = depth },
454 [1] = { [0] = x0, [1] = y1, [2] = depth },
455 [2] = { [0] = x1, [1] = y0, [2] = depth },
456 [3] = { [0] = x1, [1] = y1, [2] = depth }
457 };
458
459 return pvr_gpu_upload(device,
460 device->heaps.general_heap,
461 vertices,
462 sizeof(vertices),
463 4,
464 pvr_bo_out);
465 }
466
467 VkResult pvr_device_init_graphics_static_clear_state(struct pvr_device *device)
468 {
469 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
470 const VkRect2D vf_rect = {
471 .offset = { .x = 0, .y = 0 },
472 .extent = { .width = rogue_get_param_vf_max_x(dev_info),
473 .height = rogue_get_param_vf_max_y(dev_info) }
474 };
475
476 const uint32_t vdm_state_size_in_dw =
477 pvr_clear_vdm_state_get_size_in_dw(dev_info, 1);
478
479 struct pvr_device_static_clear_state *state = device->static_clear_state =
480 vk_zalloc(&device->vk.alloc,
481 sizeof(struct pvr_device_static_clear_state),
482 8,
483 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
484 if (!state)
485 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
486
487 const uint32_t cache_line_size = pvr_get_slc_cache_line_size(dev_info);
488 struct pvr_pds_vertex_shader_program pds_program;
489 const pco_precomp_data *precomp_data;
490 uint32_t *state_buffer;
491 VkResult result;
492
493 if (PVR_HAS_FEATURE(dev_info, gs_rta_support)) {
494 precomp_data =
495 (pco_precomp_data *)pco_usclib_common[VS_PASSTHROUGH_RTA_COMMON];
496
497 result = pvr_gpu_upload_usc(device,
498 precomp_data->binary,
499 precomp_data->size_dwords * sizeof(uint32_t),
500 cache_line_size,
501 &state->usc_multi_layer_vertex_shader_bo);
502 if (result != VK_SUCCESS)
503 goto err_free_static_clear_state;
504 } else {
505 state->usc_multi_layer_vertex_shader_bo = NULL;
506 }
507
508 precomp_data = (pco_precomp_data *)pco_usclib_common[VS_PASSTHROUGH_COMMON];
509 result = pvr_gpu_upload_usc(device,
510 precomp_data->binary,
511 precomp_data->size_dwords * sizeof(uint32_t),
512 cache_line_size,
513 &state->usc_vertex_shader_bo);
514 if (result != VK_SUCCESS)
515 goto err_free_usc_multi_layer_shader;
516
517 result =
518 pvr_clear_vertices_upload(device, &vf_rect, 0.0f, &state->vertices_bo);
519 if (result != VK_SUCCESS)
520 goto err_free_usc_shader;
521
522 pvr_pds_clear_vertex_shader_program_init_base(&pds_program,
523 state->usc_vertex_shader_bo);
524
525 result =
526 pvr_pds_clear_vertex_shader_program_create_and_upload(&pds_program,
527 device,
528 state->vertices_bo,
529 &state->pds);
530 if (result != VK_SUCCESS)
531 goto err_free_vertices_buffer;
532
533 pvr_device_setup_graphics_static_clear_ppp_base(&state->ppp_base);
534 pvr_device_setup_graphics_static_clear_ppp_templates(state->ppp_templates);
535
536 assert(pds_program.code_size <= state->pds.code_size);
537
538 state_buffer = vk_alloc(&device->vk.alloc,
539 PVR_DW_TO_BYTES(vdm_state_size_in_dw * 2),
540 8,
541 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
542 if (state_buffer == NULL) {
543 result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
544 goto err_free_pds_program;
545 }
546
547 /* TODO: The difference between the large and normal words is only the last
548 * word. The value is 3 or 4 depending on the amount of indices. Should we
549 * dedup this?
550 */
551
552 /* The large clear state words cover the max framebuffer. The normal clear
553 * state words cover only half (since 3 indices are passed, forming a single
554 * triangle, instead of 4) and are used when the render area fits within a
555 * quarter of the max framebuffer, i.e. fit within the single triangle.
556 */
557 /* 4 * sizeof(uint32_t) because of the 4 pixel output regs. */
558 /* TODO: Replace 4 * sizeof(uint32_t) with a defines from the compiler or
559 * hook up the value directly to it using some compiler info.
560 */
561 pvr_pack_clear_vdm_state(&device->pdevice->dev_info,
562 &state->pds,
563 pds_program.temps_used,
564 3,
565 4 * sizeof(uint32_t),
566 1,
567 state_buffer);
568 state->vdm_words = state_buffer;
569 state_buffer += vdm_state_size_in_dw;
570
571 pvr_pack_clear_vdm_state(&device->pdevice->dev_info,
572 &state->pds,
573 pds_program.temps_used,
574 4,
575 4 * sizeof(uint32_t),
576 1,
577 state_buffer);
578 state->large_clear_vdm_words = state_buffer;
579
580 result = pvr_device_init_clear_attachment_programs(device);
581 if (result != VK_SUCCESS)
582 goto err_free_vdm_state;
583
584 return VK_SUCCESS;
585
586 err_free_vdm_state:
587 /* Cast away the const :( */
588 vk_free(&device->vk.alloc, (void *)state->vdm_words);
589
590 err_free_pds_program:
591 pvr_bo_suballoc_free(state->pds.pvr_bo);
592
593 err_free_vertices_buffer:
594 pvr_bo_suballoc_free(state->vertices_bo);
595
596 err_free_usc_shader:
597 pvr_bo_suballoc_free(state->usc_vertex_shader_bo);
598
599 err_free_usc_multi_layer_shader:
600 pvr_bo_suballoc_free(state->usc_multi_layer_vertex_shader_bo);
601
602 err_free_static_clear_state:
603 vk_free(&device->vk.alloc, state);
604
605 return result;
606 }
607
608 void pvr_device_finish_graphics_static_clear_state(struct pvr_device *device)
609 {
610 struct pvr_device_static_clear_state *state = device->static_clear_state;
611
612 pvr_device_finish_clear_attachment_programs(device);
613
614 /* Don't free `large_clear_vdm_words` since it was allocated together with
615 * `vdm_words`.
616 */
617 /* Cast away the const :( */
618 vk_free(&device->vk.alloc, (void *)state->vdm_words);
619
620 pvr_bo_suballoc_free(state->pds.pvr_bo);
621 pvr_bo_suballoc_free(state->vertices_bo);
622 pvr_bo_suballoc_free(state->usc_vertex_shader_bo);
623 pvr_bo_suballoc_free(state->usc_multi_layer_vertex_shader_bo);
624
625 vk_free(&device->vk.alloc, state);
626 }
627
628 void pvr_pds_clear_vertex_shader_program_init_base(
629 struct pvr_pds_vertex_shader_program *program,
630 const struct pvr_suballoc_bo *usc_shader_bo)
631 {
632 *program = (struct pvr_pds_vertex_shader_program){
633 .num_streams = 1,
634 .streams = {
635 [0] = {
636 /* We'll get this from this interface's client when generating the
637 * data segment. This will be the address of the vertex buffer.
638 */
639 .address = 0,
640 .stride = PVR_CLEAR_VERTEX_COORDINATES * sizeof(uint32_t),
641 .num_elements = 1,
642 .elements = {
643 [0] = {
644 .size = PVR_CLEAR_VERTEX_COUNT * PVR_CLEAR_VERTEX_COORDINATES,
645 },
646 },
647 },
648 },
649 };
650
651 pvr_pds_setup_doutu(&program->usc_task_control,
652 usc_shader_bo->dev_addr.addr,
653 0,
654 ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
655 false);
656 }
657
658 VkResult pvr_pds_clear_vertex_shader_program_create_and_upload(
659 struct pvr_pds_vertex_shader_program *program,
660 struct pvr_device *device,
661 const struct pvr_suballoc_bo *vertices_bo,
662 struct pvr_pds_upload *const upload_out)
663 {
664 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
665 uint32_t staging_buffer_size;
666 uint32_t *staging_buffer;
667 VkResult result;
668
669 program->streams[0].address = vertices_bo->dev_addr.addr;
670
671 pvr_pds_vertex_shader(program, NULL, PDS_GENERATE_SIZES, dev_info);
672
673 staging_buffer_size =
674 PVR_DW_TO_BYTES(program->code_size + program->data_size);
675
676 staging_buffer = vk_alloc(&device->vk.alloc,
677 staging_buffer_size,
678 8,
679 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
680 if (!staging_buffer) {
681 result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
682 goto err_exit;
683 }
684
685 pvr_pds_vertex_shader(program,
686 staging_buffer,
687 PDS_GENERATE_DATA_SEGMENT,
688 dev_info);
689 pvr_pds_vertex_shader(program,
690 &staging_buffer[program->data_size],
691 PDS_GENERATE_CODE_SEGMENT,
692 dev_info);
693
694 /* FIXME: Figure out the define for alignment of 16. */
695 result = pvr_gpu_upload_pds(device,
696 &staging_buffer[0],
697 program->data_size,
698 16,
699 &staging_buffer[program->data_size],
700 program->code_size,
701 16,
702 16,
703 upload_out);
704 if (result != VK_SUCCESS)
705 goto err_free_staging_buffer;
706
707 vk_free(&device->vk.alloc, staging_buffer);
708 return VK_SUCCESS;
709
710 err_free_staging_buffer:
711 vk_free(&device->vk.alloc, staging_buffer);
712
713 err_exit:
714 *upload_out = (struct pvr_pds_upload){ 0 };
715 return result;
716 }
717
718 VkResult pvr_pds_clear_vertex_shader_program_create_and_upload_data(
719 struct pvr_pds_vertex_shader_program *program,
720 struct pvr_cmd_buffer *cmd_buffer,
721 struct pvr_suballoc_bo *vertices_bo,
722 struct pvr_pds_upload *const pds_upload_out)
723 {
724 struct pvr_device_info *dev_info = &cmd_buffer->device->pdevice->dev_info;
725 uint32_t staging_buffer_size;
726 uint32_t *staging_buffer;
727 VkResult result;
728
729 program->streams[0].address = vertices_bo->dev_addr.addr;
730
731 pvr_pds_vertex_shader(program, NULL, PDS_GENERATE_SIZES, dev_info);
732
733 staging_buffer_size = PVR_DW_TO_BYTES(program->data_size);
734
735 staging_buffer = vk_alloc(&cmd_buffer->device->vk.alloc,
736 staging_buffer_size,
737 8,
738 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
739 if (!staging_buffer) {
740 *pds_upload_out = (struct pvr_pds_upload){ 0 };
741
742 return vk_command_buffer_set_error(&cmd_buffer->vk,
743 VK_ERROR_OUT_OF_HOST_MEMORY);
744 }
745
746 pvr_pds_vertex_shader(program,
747 staging_buffer,
748 PDS_GENERATE_DATA_SEGMENT,
749 dev_info);
750
751 result = pvr_arch_cmd_buffer_upload_pds(cmd_buffer,
752 staging_buffer,
753 program->data_size,
754 4,
755 NULL,
756 0,
757 0,
758 4,
759 pds_upload_out);
760 if (result != VK_SUCCESS) {
761 vk_free(&cmd_buffer->device->vk.alloc, staging_buffer);
762
763 *pds_upload_out = (struct pvr_pds_upload){ 0 };
764
765 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
766 }
767
768 vk_free(&cmd_buffer->device->vk.alloc, staging_buffer);
769
770 return VK_SUCCESS;
771 }
772
773 void pvr_pds_clear_rta_vertex_shader_program_init_base(
774 struct pvr_pds_vertex_shader_program *program,
775 const struct pvr_suballoc_bo *usc_shader_bo)
776 {
777 pvr_pds_clear_vertex_shader_program_init_base(program, usc_shader_bo);
778
779 /* We'll set the render target index to be the instance id + base array
780 * layer. Since the base array layer can change in between clear rects, we
781 * don't set it here and ask for it when generating the code and data
782 * section.
783 */
784 /* This is 3 because the instance id register will follow the xyz coordinate
785 * registers in the register file.
786 * TODO: Maybe we want this to be hooked up to the compiler?
787 */
788 program->iterate_instance_id = true;
789 program->instance_id_register = 3;
790 }
791
792 VkResult pvr_pds_clear_rta_vertex_shader_program_create_and_upload_code(
793 struct pvr_pds_vertex_shader_program *program,
794 struct pvr_cmd_buffer *cmd_buffer,
795 uint32_t base_array_layer,
796 struct pvr_pds_upload *const pds_upload_out)
797 {
798 struct pvr_device_info *dev_info = &cmd_buffer->device->pdevice->dev_info;
799 uint32_t staging_buffer_size;
800 uint32_t *staging_buffer;
801 VkResult result;
802
803 program->instance_id_modifier = base_array_layer;
804
805 pvr_pds_vertex_shader(program, NULL, PDS_GENERATE_SIZES, dev_info);
806
807 staging_buffer_size = PVR_DW_TO_BYTES(program->code_size);
808
809 staging_buffer = vk_alloc(&cmd_buffer->device->vk.alloc,
810 staging_buffer_size,
811 8,
812 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
813 if (!staging_buffer) {
814 *pds_upload_out = (struct pvr_pds_upload){ 0 };
815
816 return vk_command_buffer_set_error(&cmd_buffer->vk,
817 VK_ERROR_OUT_OF_HOST_MEMORY);
818 }
819
820 pvr_pds_vertex_shader(program,
821 staging_buffer,
822 PDS_GENERATE_CODE_SEGMENT,
823 dev_info);
824
825 result = pvr_arch_cmd_buffer_upload_pds(cmd_buffer,
826 NULL,
827 0,
828 0,
829 staging_buffer,
830 program->code_size,
831 4,
832 4,
833 pds_upload_out);
834 if (result != VK_SUCCESS) {
835 vk_free(&cmd_buffer->device->vk.alloc, staging_buffer);
836
837 *pds_upload_out = (struct pvr_pds_upload){ 0 };
838
839 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
840 }
841
842 vk_free(&cmd_buffer->device->vk.alloc, staging_buffer);
843
844 return VK_SUCCESS;
845 }
846
847 /**
848 * Pack VDM control stream words for clear.
849 *
850 * The size of the `state_buffer` provided is expected to point to a buffer of
851 * size equal to what is returned by `pvr_clear_vdm_state_get_size_in_dw()`.
852 */
853 void pvr_pack_clear_vdm_state(const struct pvr_device_info *const dev_info,
854 const struct pvr_pds_upload *const program,
855 uint32_t temps,
856 uint32_t index_count,
857 uint32_t vs_output_size_in_bytes,
858 uint32_t layer_count,
859 uint32_t *const state_buffer)
860 {
861 const uint32_t vs_output_size =
862 DIV_ROUND_UP(vs_output_size_in_bytes,
863 ROGUE_VDMCTRL_VDM_STATE4_VS_OUTPUT_SIZE_UNIT_SIZE);
864 uint32_t *stream = state_buffer;
865 bool needs_instance_count;
866 uint32_t max_instances;
867 uint32_t cam_size;
868
869 if (PVR_HAS_FEATURE(dev_info, gs_rta_support)) {
870 needs_instance_count = layer_count > 1;
871
872 /* The layer count should at least be 1. For vkCmdClearAttachment() the
873 * spec. guarantees that the layer count is not 0.
874 */
875 assert(layer_count);
876 } else {
877 needs_instance_count = false;
878 /* FIXME: CTS dynamic rendering tests need RTA emulation for multi-layered
879 * clears.
880 */
881 }
882
883 pvr_arch_calculate_vertex_cam_size(dev_info,
884 vs_output_size,
885 true,
886 &cam_size,
887 &max_instances);
888
889 pvr_csb_pack (stream, VDMCTRL_VDM_STATE0, state0) {
890 state0.vs_data_addr_present = true;
891 state0.vs_other_present = true;
892 state0.cam_size = cam_size;
893 state0.uvs_scratch_size_select =
894 ROGUE_VDMCTRL_UVS_SCRATCH_SIZE_SELECT_FIVE;
895 state0.flatshade_control = ROGUE_VDMCTRL_FLATSHADE_CONTROL_VERTEX_0;
896 }
897 stream += pvr_cmd_length(VDMCTRL_VDM_STATE0);
898
899 pvr_csb_pack (stream, VDMCTRL_VDM_STATE2, state2) {
900 state2.vs_pds_data_base_addr = PVR_DEV_ADDR(program->data_offset);
901 }
902 stream += pvr_cmd_length(VDMCTRL_VDM_STATE2);
903
904 pvr_csb_pack (stream, VDMCTRL_VDM_STATE3, state3) {
905 state3.vs_pds_code_base_addr = PVR_DEV_ADDR(program->code_offset);
906 }
907 stream += pvr_cmd_length(VDMCTRL_VDM_STATE3);
908
909 pvr_csb_pack (stream, VDMCTRL_VDM_STATE4, state4) {
910 state4.vs_output_size = vs_output_size;
911 }
912 stream += pvr_cmd_length(VDMCTRL_VDM_STATE4);
913
914 pvr_csb_pack (stream, VDMCTRL_VDM_STATE5, state5) {
915 state5.vs_max_instances = max_instances;
916 /* This is the size of the input vertex. The hw manages the USC
917 * temporaries separately so we don't need to include them here.
918 */
919 state5.vs_usc_unified_size =
920 DIV_ROUND_UP(PVR_CLEAR_VERTEX_COORDINATES * sizeof(uint32_t),
921 ROGUE_VDMCTRL_VDM_STATE5_VS_USC_UNIFIED_SIZE_UNIT_SIZE);
922 state5.vs_pds_temp_size =
923 DIV_ROUND_UP(temps,
924 ROGUE_VDMCTRL_VDM_STATE5_VS_PDS_TEMP_SIZE_UNIT_SIZE);
925 state5.vs_pds_data_size =
926 DIV_ROUND_UP(PVR_DW_TO_BYTES(program->data_size),
927 ROGUE_VDMCTRL_VDM_STATE5_VS_PDS_DATA_SIZE_UNIT_SIZE);
928 }
929 stream += pvr_cmd_length(VDMCTRL_VDM_STATE5);
930
931 /* TODO: Here we're doing another state update. If emitting directly to the
932 * control stream, we don't mark them as separate state updates by setting
933 * the relocation mark so we might be wasting a little bit of memory. See if
934 * it's worth changing the code to use the relocation mark.
935 */
936
937 pvr_csb_pack (stream, VDMCTRL_INDEX_LIST0, index_list0) {
938 index_list0.index_count_present = true;
939 index_list0.index_instance_count_present = needs_instance_count;
940 index_list0.primitive_topology =
941 ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_STRIP;
942 }
943 stream += pvr_cmd_length(VDMCTRL_INDEX_LIST0);
944
945 pvr_csb_pack (stream, VDMCTRL_INDEX_LIST2, index_list3) {
946 index_list3.index_count = index_count;
947 }
948 stream += pvr_cmd_length(VDMCTRL_INDEX_LIST2);
949
950 if (needs_instance_count) {
951 pvr_csb_pack (stream, VDMCTRL_INDEX_LIST3, index_list3) {
952 index_list3.instance_count = layer_count - 1;
953 }
954 stream += pvr_cmd_length(VDMCTRL_INDEX_LIST3);
955 }
956
957 assert((uint64_t)(stream - state_buffer) ==
958 pvr_clear_vdm_state_get_size_in_dw(dev_info, layer_count));
959 }