1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 * SPDX-License-Identifier: MIT
5 *
6 * based in part on anv driver which is:
7 * Copyright © 2015 Intel Corporation
8 */
9
10 #include "tu_cmd_buffer.h"
11
12 #include "vk_render_pass.h"
13 #include "vk_util.h"
14
15 #include "tu_clear_blit.h"
16 #include "tu_cs.h"
17 #include "tu_image.h"
18 #include "tu_tracepoints.h"
19
20 void
tu6_emit_event_write(struct tu_cmd_buffer * cmd,struct tu_cs * cs,enum vgt_event_type event)21 tu6_emit_event_write(struct tu_cmd_buffer *cmd,
22 struct tu_cs *cs,
23 enum vgt_event_type event)
24 {
25 bool need_seqno = false;
26 switch (event) {
27 case CACHE_FLUSH_TS:
28 case WT_DONE_TS:
29 case RB_DONE_TS:
30 case PC_CCU_FLUSH_DEPTH_TS:
31 case PC_CCU_FLUSH_COLOR_TS:
32 case PC_CCU_RESOLVE_TS:
33 need_seqno = true;
34 break;
35 default:
36 break;
37 }
38
39 tu_cs_emit_pkt7(cs, CP_EVENT_WRITE, need_seqno ? 4 : 1);
40 tu_cs_emit(cs, CP_EVENT_WRITE_0_EVENT(event));
41 if (need_seqno) {
42 tu_cs_emit_qw(cs, global_iova(cmd, seqno_dummy));
43 tu_cs_emit(cs, 0);
44 }
45 }
46
47 /* Emits the tessfactor address to the top-level CS if it hasn't been already.
48 * Updating this register requires a WFI if outstanding drawing is using it, but
49 * tu6_init_hardware() will have WFIed before we started and no other draws
50 * could be using the tessfactor address yet since we only emit one per cmdbuf.
51 */
52 static void
tu6_lazy_emit_tessfactor_addr(struct tu_cmd_buffer * cmd)53 tu6_lazy_emit_tessfactor_addr(struct tu_cmd_buffer *cmd)
54 {
55 if (cmd->state.tessfactor_addr_set)
56 return;
57
58 tu_cs_emit_regs(&cmd->cs, A6XX_PC_TESSFACTOR_ADDR(.qword = cmd->device->tess_bo->iova));
59 /* Updating PC_TESSFACTOR_ADDR could race with the next draw which uses it. */
60 cmd->state.cache.flush_bits |= TU_CMD_FLAG_WAIT_FOR_IDLE;
61 cmd->state.tessfactor_addr_set = true;
62 }
63
64 static void
tu6_emit_flushes(struct tu_cmd_buffer * cmd_buffer,struct tu_cs * cs,enum tu_cmd_flush_bits flushes)65 tu6_emit_flushes(struct tu_cmd_buffer *cmd_buffer,
66 struct tu_cs *cs,
67 enum tu_cmd_flush_bits flushes)
68 {
69 if (unlikely(cmd_buffer->device->physical_device->instance->debug_flags & TU_DEBUG_FLUSHALL))
70 flushes |= TU_CMD_FLAG_ALL_FLUSH | TU_CMD_FLAG_ALL_INVALIDATE;
71
72 if (unlikely(cmd_buffer->device->physical_device->instance->debug_flags & TU_DEBUG_SYNCDRAW))
73 flushes |= TU_CMD_FLAG_WAIT_MEM_WRITES |
74 TU_CMD_FLAG_WAIT_FOR_IDLE |
75 TU_CMD_FLAG_WAIT_FOR_ME;
76
77 /* Experiments show that invalidating CCU while it still has data in it
78 * doesn't work, so make sure to always flush before invalidating in case
79 * any data remains that hasn't yet been made available through a barrier.
80 * However it does seem to work for UCHE.
81 */
82 if (flushes & (TU_CMD_FLAG_CCU_FLUSH_COLOR |
83 TU_CMD_FLAG_CCU_INVALIDATE_COLOR))
84 tu6_emit_event_write(cmd_buffer, cs, PC_CCU_FLUSH_COLOR_TS);
85 if (flushes & (TU_CMD_FLAG_CCU_FLUSH_DEPTH |
86 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH))
87 tu6_emit_event_write(cmd_buffer, cs, PC_CCU_FLUSH_DEPTH_TS);
88 if (flushes & TU_CMD_FLAG_CCU_INVALIDATE_COLOR)
89 tu6_emit_event_write(cmd_buffer, cs, PC_CCU_INVALIDATE_COLOR);
90 if (flushes & TU_CMD_FLAG_CCU_INVALIDATE_DEPTH)
91 tu6_emit_event_write(cmd_buffer, cs, PC_CCU_INVALIDATE_DEPTH);
92 if (flushes & TU_CMD_FLAG_CACHE_FLUSH)
93 tu6_emit_event_write(cmd_buffer, cs, CACHE_FLUSH_TS);
94 if (flushes & TU_CMD_FLAG_CACHE_INVALIDATE)
95 tu6_emit_event_write(cmd_buffer, cs, CACHE_INVALIDATE);
96 if (flushes & TU_CMD_FLAG_WAIT_MEM_WRITES)
97 tu_cs_emit_pkt7(cs, CP_WAIT_MEM_WRITES, 0);
98 if ((flushes & TU_CMD_FLAG_WAIT_FOR_IDLE) ||
99 (cmd_buffer->device->physical_device->info->a6xx.has_ccu_flush_bug &&
100 (flushes & (TU_CMD_FLAG_CCU_FLUSH_COLOR | TU_CMD_FLAG_CCU_FLUSH_DEPTH))))
101 tu_cs_emit_wfi(cs);
102 if (flushes & TU_CMD_FLAG_WAIT_FOR_ME)
103 tu_cs_emit_pkt7(cs, CP_WAIT_FOR_ME, 0);
104 }
105
106 /* "Normal" cache flushes, that don't require any special handling */
107
108 static void
tu_emit_cache_flush(struct tu_cmd_buffer * cmd_buffer,struct tu_cs * cs)109 tu_emit_cache_flush(struct tu_cmd_buffer *cmd_buffer,
110 struct tu_cs *cs)
111 {
112 tu6_emit_flushes(cmd_buffer, cs, cmd_buffer->state.cache.flush_bits);
113 cmd_buffer->state.cache.flush_bits = 0;
114 }
115
116 /* Renderpass cache flushes */
117
118 void
tu_emit_cache_flush_renderpass(struct tu_cmd_buffer * cmd_buffer,struct tu_cs * cs)119 tu_emit_cache_flush_renderpass(struct tu_cmd_buffer *cmd_buffer,
120 struct tu_cs *cs)
121 {
122 if (!cmd_buffer->state.renderpass_cache.flush_bits &&
123 likely(!cmd_buffer->device->physical_device->instance->debug_flags))
124 return;
125 tu6_emit_flushes(cmd_buffer, cs, cmd_buffer->state.renderpass_cache.flush_bits);
126 cmd_buffer->state.renderpass_cache.flush_bits = 0;
127 }
128
129 /* Cache flushes for things that use the color/depth read/write path (i.e.
130 * blits and draws). This deals with changing CCU state as well as the usual
131 * cache flushing.
132 */
133
134 void
tu_emit_cache_flush_ccu(struct tu_cmd_buffer * cmd_buffer,struct tu_cs * cs,enum tu_cmd_ccu_state ccu_state)135 tu_emit_cache_flush_ccu(struct tu_cmd_buffer *cmd_buffer,
136 struct tu_cs *cs,
137 enum tu_cmd_ccu_state ccu_state)
138 {
139 enum tu_cmd_flush_bits flushes = cmd_buffer->state.cache.flush_bits;
140
141 assert(ccu_state != TU_CMD_CCU_UNKNOWN);
142 /* It's unsafe to flush inside condition because we clear flush_bits */
143 assert(!cs->cond_stack_depth);
144
145 /* Changing CCU state must involve invalidating the CCU. In sysmem mode,
146 * the CCU may also contain data that we haven't flushed out yet, so we
147 * also need to flush. Also, in order to program RB_CCU_CNTL, we need to
148 * emit a WFI as it isn't pipelined.
149 */
150 if (ccu_state != cmd_buffer->state.ccu_state) {
151 if (cmd_buffer->state.ccu_state != TU_CMD_CCU_GMEM) {
152 flushes |=
153 TU_CMD_FLAG_CCU_FLUSH_COLOR |
154 TU_CMD_FLAG_CCU_FLUSH_DEPTH;
155 cmd_buffer->state.cache.pending_flush_bits &= ~(
156 TU_CMD_FLAG_CCU_FLUSH_COLOR |
157 TU_CMD_FLAG_CCU_FLUSH_DEPTH);
158 }
159 flushes |=
160 TU_CMD_FLAG_CCU_INVALIDATE_COLOR |
161 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH |
162 TU_CMD_FLAG_WAIT_FOR_IDLE;
163 cmd_buffer->state.cache.pending_flush_bits &= ~(
164 TU_CMD_FLAG_CCU_INVALIDATE_COLOR |
165 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH |
166 TU_CMD_FLAG_WAIT_FOR_IDLE);
167 }
168
169 tu6_emit_flushes(cmd_buffer, cs, flushes);
170 cmd_buffer->state.cache.flush_bits = 0;
171
172 if (ccu_state != cmd_buffer->state.ccu_state) {
173 struct tu_physical_device *phys_dev = cmd_buffer->device->physical_device;
174 tu_cs_emit_regs(cs,
175 A6XX_RB_CCU_CNTL(.color_offset =
176 ccu_state == TU_CMD_CCU_GMEM ?
177 phys_dev->ccu_offset_gmem :
178 phys_dev->ccu_offset_bypass,
179 .gmem = ccu_state == TU_CMD_CCU_GMEM));
180 cmd_buffer->state.ccu_state = ccu_state;
181 }
182 }
183
184 static void
tu6_emit_zs(struct tu_cmd_buffer * cmd,const struct tu_subpass * subpass,struct tu_cs * cs)185 tu6_emit_zs(struct tu_cmd_buffer *cmd,
186 const struct tu_subpass *subpass,
187 struct tu_cs *cs)
188 {
189 const uint32_t a = subpass->depth_stencil_attachment.attachment;
190 if (a == VK_ATTACHMENT_UNUSED) {
191 tu_cs_emit_regs(cs,
192 A6XX_RB_DEPTH_BUFFER_INFO(.depth_format = DEPTH6_NONE),
193 A6XX_RB_DEPTH_BUFFER_PITCH(0),
194 A6XX_RB_DEPTH_BUFFER_ARRAY_PITCH(0),
195 A6XX_RB_DEPTH_BUFFER_BASE(0),
196 A6XX_RB_DEPTH_BUFFER_BASE_GMEM(0));
197
198 tu_cs_emit_regs(cs,
199 A6XX_GRAS_SU_DEPTH_BUFFER_INFO(.depth_format = DEPTH6_NONE));
200
201 tu_cs_emit_regs(cs, A6XX_RB_STENCIL_INFO(0));
202
203 return;
204 }
205
206 const struct tu_image_view *iview = cmd->state.attachments[a];
207 const struct tu_render_pass_attachment *attachment =
208 &cmd->state.pass->attachments[a];
209 enum a6xx_depth_format fmt = tu6_pipe2depth(attachment->format);
210
211 tu_cs_emit_pkt4(cs, REG_A6XX_RB_DEPTH_BUFFER_INFO, 6);
212 tu_cs_emit(cs, A6XX_RB_DEPTH_BUFFER_INFO(.depth_format = fmt).value);
213 if (attachment->format == VK_FORMAT_D32_SFLOAT_S8_UINT)
214 tu_cs_image_depth_ref(cs, iview, 0);
215 else
216 tu_cs_image_ref(cs, &iview->view, 0);
217 tu_cs_emit(cs, tu_attachment_gmem_offset(cmd, attachment));
218
219 tu_cs_emit_regs(cs,
220 A6XX_GRAS_SU_DEPTH_BUFFER_INFO(.depth_format = fmt));
221
222 tu_cs_emit_pkt4(cs, REG_A6XX_RB_DEPTH_FLAG_BUFFER_BASE, 3);
223 tu_cs_image_flag_ref(cs, &iview->view, 0);
224
225 if (attachment->format == VK_FORMAT_D32_SFLOAT_S8_UINT ||
226 attachment->format == VK_FORMAT_S8_UINT) {
227
228 tu_cs_emit_pkt4(cs, REG_A6XX_RB_STENCIL_INFO, 6);
229 tu_cs_emit(cs, A6XX_RB_STENCIL_INFO(.separate_stencil = true).value);
230 if (attachment->format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
231 tu_cs_image_stencil_ref(cs, iview, 0);
232 tu_cs_emit(cs, tu_attachment_gmem_offset_stencil(cmd, attachment));
233 } else {
234 tu_cs_image_ref(cs, &iview->view, 0);
235 tu_cs_emit(cs, tu_attachment_gmem_offset(cmd, attachment));
236 }
237 } else {
238 tu_cs_emit_regs(cs,
239 A6XX_RB_STENCIL_INFO(0));
240 }
241 }
242
243 static void
tu6_emit_mrt(struct tu_cmd_buffer * cmd,const struct tu_subpass * subpass,struct tu_cs * cs)244 tu6_emit_mrt(struct tu_cmd_buffer *cmd,
245 const struct tu_subpass *subpass,
246 struct tu_cs *cs)
247 {
248 const struct tu_framebuffer *fb = cmd->state.framebuffer;
249
250 enum a6xx_format mrt0_format = 0;
251
252 for (uint32_t i = 0; i < subpass->color_count; ++i) {
253 uint32_t a = subpass->color_attachments[i].attachment;
254 if (a == VK_ATTACHMENT_UNUSED) {
255 /* From the VkPipelineRenderingCreateInfo definition:
256 *
257 * Valid formats indicate that an attachment can be used - but it
258 * is still valid to set the attachment to NULL when beginning
259 * rendering.
260 *
261 * This means that with dynamic rendering, pipelines may write to
262 * some attachments that are UNUSED here. Setting the format to 0
263 * here should prevent them from writing to anything.
264 */
265 tu_cs_emit_pkt4(cs, REG_A6XX_RB_MRT_BUF_INFO(i), 6);
266 for (unsigned i = 0; i < 6; i++)
267 tu_cs_emit(cs, 0);
268 continue;
269 }
270
271 const struct tu_image_view *iview = cmd->state.attachments[a];
272
273 tu_cs_emit_pkt4(cs, REG_A6XX_RB_MRT_BUF_INFO(i), 6);
274 tu_cs_emit(cs, iview->view.RB_MRT_BUF_INFO);
275 tu_cs_image_ref(cs, &iview->view, 0);
276 tu_cs_emit(cs, tu_attachment_gmem_offset(cmd, &cmd->state.pass->attachments[a]));
277
278 tu_cs_emit_regs(cs,
279 A6XX_SP_FS_MRT_REG(i, .dword = iview->view.SP_FS_MRT_REG));
280
281 tu_cs_emit_pkt4(cs, REG_A6XX_RB_MRT_FLAG_BUFFER_ADDR(i), 3);
282 tu_cs_image_flag_ref(cs, &iview->view, 0);
283
284 if (i == 0)
285 mrt0_format = iview->view.SP_FS_MRT_REG & 0xff;
286 }
287
288 tu_cs_emit_regs(cs, A6XX_GRAS_LRZ_MRT_BUF_INFO_0(.color_format = mrt0_format));
289
290 tu_cs_emit_regs(cs,
291 A6XX_RB_SRGB_CNTL(.dword = subpass->srgb_cntl));
292 tu_cs_emit_regs(cs,
293 A6XX_SP_SRGB_CNTL(.dword = subpass->srgb_cntl));
294
295 unsigned layers = MAX2(fb->layers, util_logbase2(subpass->multiview_mask) + 1);
296 tu_cs_emit_regs(cs, A6XX_GRAS_MAX_LAYER_INDEX(layers - 1));
297 }
298
299 void
tu6_emit_msaa(struct tu_cs * cs,VkSampleCountFlagBits vk_samples,enum a5xx_line_mode line_mode)300 tu6_emit_msaa(struct tu_cs *cs, VkSampleCountFlagBits vk_samples,
301 enum a5xx_line_mode line_mode)
302 {
303 const enum a3xx_msaa_samples samples = tu_msaa_samples(vk_samples);
304 bool msaa_disable = (samples == MSAA_ONE) || (line_mode == BRESENHAM);
305
306 tu_cs_emit_regs(cs,
307 A6XX_SP_TP_RAS_MSAA_CNTL(samples),
308 A6XX_SP_TP_DEST_MSAA_CNTL(.samples = samples,
309 .msaa_disable = msaa_disable));
310
311 tu_cs_emit_regs(cs,
312 A6XX_GRAS_RAS_MSAA_CNTL(samples),
313 A6XX_GRAS_DEST_MSAA_CNTL(.samples = samples,
314 .msaa_disable = msaa_disable));
315
316 tu_cs_emit_regs(cs,
317 A6XX_RB_RAS_MSAA_CNTL(samples),
318 A6XX_RB_DEST_MSAA_CNTL(.samples = samples,
319 .msaa_disable = msaa_disable));
320
321 tu_cs_emit_regs(cs,
322 A6XX_RB_MSAA_CNTL(samples));
323 }
324
325 static void
tu6_emit_bin_size(struct tu_cs * cs,uint32_t bin_w,uint32_t bin_h,uint32_t flags)326 tu6_emit_bin_size(struct tu_cs *cs,
327 uint32_t bin_w, uint32_t bin_h, uint32_t flags)
328 {
329 tu_cs_emit_regs(cs,
330 A6XX_GRAS_BIN_CONTROL(.binw = bin_w,
331 .binh = bin_h,
332 .dword = flags));
333
334 tu_cs_emit_regs(cs,
335 A6XX_RB_BIN_CONTROL(.binw = bin_w,
336 .binh = bin_h,
337 .dword = flags));
338
339 /* no flag for RB_BIN_CONTROL2... */
340 tu_cs_emit_regs(cs,
341 A6XX_RB_BIN_CONTROL2(.binw = bin_w,
342 .binh = bin_h));
343 }
344
345 static void
tu6_emit_render_cntl(struct tu_cmd_buffer * cmd,const struct tu_subpass * subpass,struct tu_cs * cs,bool binning)346 tu6_emit_render_cntl(struct tu_cmd_buffer *cmd,
347 const struct tu_subpass *subpass,
348 struct tu_cs *cs,
349 bool binning)
350 {
351 /* doesn't RB_RENDER_CNTL set differently for binning pass: */
352 bool no_track = !cmd->device->physical_device->info->a6xx.has_cp_reg_write;
353 uint32_t cntl = 0;
354 cntl |= A6XX_RB_RENDER_CNTL_CCUSINGLECACHELINESIZE(2);
355 if (binning) {
356 if (no_track)
357 return;
358 cntl |= A6XX_RB_RENDER_CNTL_BINNING;
359 } else {
360 uint32_t mrts_ubwc_enable = 0;
361 for (uint32_t i = 0; i < subpass->color_count; ++i) {
362 uint32_t a = subpass->color_attachments[i].attachment;
363 if (a == VK_ATTACHMENT_UNUSED)
364 continue;
365
366 const struct tu_image_view *iview = cmd->state.attachments[a];
367 if (iview->view.ubwc_enabled)
368 mrts_ubwc_enable |= 1 << i;
369 }
370
371 cntl |= A6XX_RB_RENDER_CNTL_FLAG_MRTS(mrts_ubwc_enable);
372
373 const uint32_t a = subpass->depth_stencil_attachment.attachment;
374 if (a != VK_ATTACHMENT_UNUSED) {
375 const struct tu_image_view *iview = cmd->state.attachments[a];
376 if (iview->view.ubwc_enabled)
377 cntl |= A6XX_RB_RENDER_CNTL_FLAG_DEPTH;
378 }
379
380 if (no_track) {
381 tu_cs_emit_pkt4(cs, REG_A6XX_RB_RENDER_CNTL, 1);
382 tu_cs_emit(cs, cntl);
383 return;
384 }
385
386 /* In the !binning case, we need to set RB_RENDER_CNTL in the draw_cs
387 * in order to set it correctly for the different subpasses. However,
388 * that means the packets we're emitting also happen during binning. So
389 * we need to guard the write on !BINNING at CP execution time.
390 */
391 tu_cs_reserve(cs, 3 + 4);
392 tu_cs_emit_pkt7(cs, CP_COND_REG_EXEC, 2);
393 tu_cs_emit(cs, CP_COND_REG_EXEC_0_MODE(RENDER_MODE) |
394 CP_COND_REG_EXEC_0_GMEM | CP_COND_REG_EXEC_0_SYSMEM);
395 tu_cs_emit(cs, CP_COND_REG_EXEC_1_DWORDS(4));
396 }
397
398 tu_cs_emit_pkt7(cs, CP_REG_WRITE, 3);
399 tu_cs_emit(cs, CP_REG_WRITE_0_TRACKER(TRACK_RENDER_CNTL));
400 tu_cs_emit(cs, REG_A6XX_RB_RENDER_CNTL);
401 tu_cs_emit(cs, cntl);
402 }
403
404 static void
tu6_emit_blit_scissor(struct tu_cmd_buffer * cmd,struct tu_cs * cs,bool align)405 tu6_emit_blit_scissor(struct tu_cmd_buffer *cmd, struct tu_cs *cs, bool align)
406 {
407 struct tu_physical_device *phys_dev = cmd->device->physical_device;
408 const VkRect2D *render_area = &cmd->state.render_area;
409
410 /* Avoid assertion fails with an empty render area at (0, 0) where the
411 * subtraction below wraps around. Empty render areas should be forced to
412 * the sysmem path by use_sysmem_rendering(). It's not even clear whether
413 * an empty scissor here works, and the blob seems to force sysmem too as
414 * it sets something wrong (non-empty) for the scissor.
415 */
416 if (render_area->extent.width == 0 ||
417 render_area->extent.height == 0)
418 return;
419
420 uint32_t x1 = render_area->offset.x;
421 uint32_t y1 = render_area->offset.y;
422 uint32_t x2 = x1 + render_area->extent.width - 1;
423 uint32_t y2 = y1 + render_area->extent.height - 1;
424
425 if (align) {
426 x1 = x1 & ~(phys_dev->info->gmem_align_w - 1);
427 y1 = y1 & ~(phys_dev->info->gmem_align_h - 1);
428 x2 = ALIGN_POT(x2 + 1, phys_dev->info->gmem_align_w) - 1;
429 y2 = ALIGN_POT(y2 + 1, phys_dev->info->gmem_align_h) - 1;
430 }
431
432 tu_cs_emit_regs(cs,
433 A6XX_RB_BLIT_SCISSOR_TL(.x = x1, .y = y1),
434 A6XX_RB_BLIT_SCISSOR_BR(.x = x2, .y = y2));
435 }
436
437 void
tu6_emit_window_scissor(struct tu_cs * cs,uint32_t x1,uint32_t y1,uint32_t x2,uint32_t y2)438 tu6_emit_window_scissor(struct tu_cs *cs,
439 uint32_t x1,
440 uint32_t y1,
441 uint32_t x2,
442 uint32_t y2)
443 {
444 tu_cs_emit_regs(cs,
445 A6XX_GRAS_SC_WINDOW_SCISSOR_TL(.x = x1, .y = y1),
446 A6XX_GRAS_SC_WINDOW_SCISSOR_BR(.x = x2, .y = y2));
447
448 tu_cs_emit_regs(cs,
449 A6XX_GRAS_2D_RESOLVE_CNTL_1(.x = x1, .y = y1),
450 A6XX_GRAS_2D_RESOLVE_CNTL_2(.x = x2, .y = y2));
451 }
452
453 void
tu6_emit_window_offset(struct tu_cs * cs,uint32_t x1,uint32_t y1)454 tu6_emit_window_offset(struct tu_cs *cs, uint32_t x1, uint32_t y1)
455 {
456 tu_cs_emit_regs(cs,
457 A6XX_RB_WINDOW_OFFSET(.x = x1, .y = y1));
458
459 tu_cs_emit_regs(cs,
460 A6XX_RB_WINDOW_OFFSET2(.x = x1, .y = y1));
461
462 tu_cs_emit_regs(cs,
463 A6XX_SP_WINDOW_OFFSET(.x = x1, .y = y1));
464
465 tu_cs_emit_regs(cs,
466 A6XX_SP_TP_WINDOW_OFFSET(.x = x1, .y = y1));
467 }
468
469 void
tu6_apply_depth_bounds_workaround(struct tu_device * device,uint32_t * rb_depth_cntl)470 tu6_apply_depth_bounds_workaround(struct tu_device *device,
471 uint32_t *rb_depth_cntl)
472 {
473 if (!device->physical_device->info->a6xx.depth_bounds_require_depth_test_quirk)
474 return;
475
476 /* On some GPUs it is necessary to enable z test for depth bounds test when
477 * UBWC is enabled. Otherwise, the GPU would hang. FUNC_ALWAYS is required to
478 * pass z test. Relevant tests:
479 * dEQP-VK.pipeline.extended_dynamic_state.two_draws_dynamic.depth_bounds_test_disable
480 * dEQP-VK.dynamic_state.ds_state.depth_bounds_1
481 */
482 *rb_depth_cntl |= A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE |
483 A6XX_RB_DEPTH_CNTL_ZFUNC(FUNC_ALWAYS);
484 }
485
486 static void
tu_cs_emit_draw_state(struct tu_cs * cs,uint32_t id,struct tu_draw_state state)487 tu_cs_emit_draw_state(struct tu_cs *cs, uint32_t id, struct tu_draw_state state)
488 {
489 uint32_t enable_mask;
490 switch (id) {
491 case TU_DRAW_STATE_PROGRAM:
492 case TU_DRAW_STATE_VI:
493 /* The blob seems to not enable this (DESC_SETS_LOAD) for binning, even
494 * when resources would actually be used in the binning shader.
495 * Presumably the overhead of prefetching the resources isn't
496 * worth it.
497 */
498 case TU_DRAW_STATE_DESC_SETS_LOAD:
499 enable_mask = CP_SET_DRAW_STATE__0_GMEM |
500 CP_SET_DRAW_STATE__0_SYSMEM;
501 break;
502 case TU_DRAW_STATE_PROGRAM_BINNING:
503 case TU_DRAW_STATE_VI_BINNING:
504 enable_mask = CP_SET_DRAW_STATE__0_BINNING;
505 break;
506 case TU_DRAW_STATE_INPUT_ATTACHMENTS_GMEM:
507 case TU_DRAW_STATE_PRIM_MODE_GMEM:
508 enable_mask = CP_SET_DRAW_STATE__0_GMEM;
509 break;
510 case TU_DRAW_STATE_INPUT_ATTACHMENTS_SYSMEM:
511 case TU_DRAW_STATE_PRIM_MODE_SYSMEM:
512 enable_mask = CP_SET_DRAW_STATE__0_SYSMEM;
513 break;
514 default:
515 enable_mask = CP_SET_DRAW_STATE__0_GMEM |
516 CP_SET_DRAW_STATE__0_SYSMEM |
517 CP_SET_DRAW_STATE__0_BINNING;
518 break;
519 }
520
521 STATIC_ASSERT(TU_DRAW_STATE_COUNT <= 32);
522
523 /* We need to reload the descriptors every time the descriptor sets
524 * change. However, the commands we send only depend on the pipeline
525 * because the whole point is to cache descriptors which are used by the
526 * pipeline. There's a problem here, in that the firmware has an
527 * "optimization" which skips executing groups that are set to the same
528 * value as the last draw. This means that if the descriptor sets change
529 * but not the pipeline, we'd try to re-execute the same buffer which
530 * the firmware would ignore and we wouldn't pre-load the new
531 * descriptors. Set the DIRTY bit to avoid this optimization
532 */
533 if (id == TU_DRAW_STATE_DESC_SETS_LOAD)
534 enable_mask |= CP_SET_DRAW_STATE__0_DIRTY;
535
536 tu_cs_emit(cs, CP_SET_DRAW_STATE__0_COUNT(state.size) |
537 enable_mask |
538 CP_SET_DRAW_STATE__0_GROUP_ID(id) |
539 COND(!state.size, CP_SET_DRAW_STATE__0_DISABLE));
540 tu_cs_emit_qw(cs, state.iova);
541 }
542
543 static bool
use_hw_binning(struct tu_cmd_buffer * cmd)544 use_hw_binning(struct tu_cmd_buffer *cmd)
545 {
546 const struct tu_framebuffer *fb = cmd->state.framebuffer;
547 const struct tu_tiling_config *tiling = &fb->tiling[cmd->state.gmem_layout];
548
549 /* XFB commands are emitted for BINNING || SYSMEM, which makes it
550 * incompatible with non-hw binning GMEM rendering. this is required because
551 * some of the XFB commands need to only be executed once.
552 * use_sysmem_rendering() should have made sure we only ended up here if no
553 * XFB was used.
554 */
555 if (cmd->state.rp.xfb_used) {
556 assert(tiling->binning_possible);
557 return true;
558 }
559
560 /* VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT emulates GL_PRIMITIVES_GENERATED,
561 * which wasn't designed to care about tilers and expects the result not to
562 * be multiplied by tile count.
563 * See https://gitlab.khronos.org/vulkan/vulkan/-/issues/3131
564 */
565 if (cmd->state.rp.has_prim_generated_query_in_rp ||
566 cmd->state.prim_generated_query_running_before_rp) {
567 assert(tiling->binning_possible);
568 return true;
569 }
570
571 return tiling->binning;
572 }
573
574 static bool
use_sysmem_rendering(struct tu_cmd_buffer * cmd,struct tu_renderpass_result ** autotune_result)575 use_sysmem_rendering(struct tu_cmd_buffer *cmd,
576 struct tu_renderpass_result **autotune_result)
577 {
578 if (unlikely(cmd->device->physical_device->instance->debug_flags & TU_DEBUG_SYSMEM))
579 return true;
580
581 /* can't fit attachments into gmem */
582 if (!cmd->state.pass->gmem_pixels[cmd->state.gmem_layout])
583 return true;
584
585 if (cmd->state.framebuffer->layers > 1)
586 return true;
587
588 /* Use sysmem for empty render areas */
589 if (cmd->state.render_area.extent.width == 0 ||
590 cmd->state.render_area.extent.height == 0)
591 return true;
592
593 if (cmd->state.rp.has_tess)
594 return true;
595
596 if (cmd->state.rp.disable_gmem)
597 return true;
598
599 /* XFB is incompatible with non-hw binning GMEM rendering, see use_hw_binning */
600 if (cmd->state.rp.xfb_used && !cmd->state.tiling->binning_possible)
601 return true;
602
603 /* QUERY_TYPE_PRIMITIVES_GENERATED is incompatible with non-hw binning
604 * GMEM rendering, see use_hw_binning.
605 */
606 if ((cmd->state.rp.has_prim_generated_query_in_rp ||
607 cmd->state.prim_generated_query_running_before_rp) &&
608 !cmd->state.tiling->binning_possible)
609 return true;
610
611 if (unlikely(cmd->device->physical_device->instance->debug_flags & TU_DEBUG_GMEM))
612 return false;
613
614 bool use_sysmem = tu_autotune_use_bypass(&cmd->device->autotune,
615 cmd, autotune_result);
616 if (*autotune_result) {
617 list_addtail(&(*autotune_result)->node, &cmd->renderpass_autotune_results);
618 }
619
620 return use_sysmem;
621 }
622
623 /* Optimization: there is no reason to load gmem if there is no
624 * geometry to process. COND_REG_EXEC predicate is set here,
625 * but the actual skip happens in tu6_emit_tile_load() and tile_store_cs,
626 * for each blit separately.
627 */
628 static void
tu6_emit_cond_for_load_stores(struct tu_cmd_buffer * cmd,struct tu_cs * cs,uint32_t pipe,uint32_t slot,bool wfm)629 tu6_emit_cond_for_load_stores(struct tu_cmd_buffer *cmd, struct tu_cs *cs,
630 uint32_t pipe, uint32_t slot, bool wfm)
631 {
632 if (cmd->state.tiling->binning_possible) {
633 tu_cs_emit_pkt7(cs, CP_REG_TEST, 1);
634 tu_cs_emit(cs, A6XX_CP_REG_TEST_0_REG(REG_A6XX_VSC_STATE_REG(pipe)) |
635 A6XX_CP_REG_TEST_0_BIT(slot) |
636 COND(wfm, A6XX_CP_REG_TEST_0_WAIT_FOR_ME));
637 } else {
638 /* COND_REG_EXECs are not emitted in non-binning case */
639 }
640 }
641
642 static void
tu6_emit_tile_select(struct tu_cmd_buffer * cmd,struct tu_cs * cs,uint32_t tx,uint32_t ty,uint32_t pipe,uint32_t slot)643 tu6_emit_tile_select(struct tu_cmd_buffer *cmd,
644 struct tu_cs *cs,
645 uint32_t tx, uint32_t ty, uint32_t pipe, uint32_t slot)
646 {
647 const struct tu_tiling_config *tiling = cmd->state.tiling;
648
649 tu_cs_emit_pkt7(cs, CP_SET_MARKER, 1);
650 tu_cs_emit(cs, A6XX_CP_SET_MARKER_0_MODE(RM6_GMEM));
651
652 const uint32_t x1 = tiling->tile0.width * tx;
653 const uint32_t y1 = tiling->tile0.height * ty;
654 const uint32_t x2 = MIN2(x1 + tiling->tile0.width - 1, MAX_VIEWPORT_SIZE - 1);
655 const uint32_t y2 = MIN2(y1 + tiling->tile0.height - 1, MAX_VIEWPORT_SIZE - 1);
656 tu6_emit_window_scissor(cs, x1, y1, x2, y2);
657 tu6_emit_window_offset(cs, x1, y1);
658
659 bool hw_binning = use_hw_binning(cmd);
660
661 if (hw_binning) {
662 tu_cs_emit_pkt7(cs, CP_WAIT_FOR_ME, 0);
663
664 tu_cs_emit_pkt7(cs, CP_SET_MODE, 1);
665 tu_cs_emit(cs, 0x0);
666
667 tu_cs_emit_pkt7(cs, CP_SET_BIN_DATA5_OFFSET, 4);
668 tu_cs_emit(cs, tiling->pipe_sizes[pipe] |
669 CP_SET_BIN_DATA5_0_VSC_N(slot));
670 tu_cs_emit(cs, pipe * cmd->vsc_draw_strm_pitch);
671 tu_cs_emit(cs, pipe * 4);
672 tu_cs_emit(cs, pipe * cmd->vsc_prim_strm_pitch);
673 }
674
675 tu6_emit_cond_for_load_stores(cmd, cs, pipe, slot, hw_binning);
676
677 tu_cs_emit_pkt7(cs, CP_SET_VISIBILITY_OVERRIDE, 1);
678 tu_cs_emit(cs, !hw_binning);
679
680 tu_cs_emit_pkt7(cs, CP_SET_MODE, 1);
681 tu_cs_emit(cs, 0x0);
682 }
683
684 static void
tu6_emit_sysmem_resolve(struct tu_cmd_buffer * cmd,struct tu_cs * cs,uint32_t layer_mask,uint32_t a,uint32_t gmem_a)685 tu6_emit_sysmem_resolve(struct tu_cmd_buffer *cmd,
686 struct tu_cs *cs,
687 uint32_t layer_mask,
688 uint32_t a,
689 uint32_t gmem_a)
690 {
691 const struct tu_framebuffer *fb = cmd->state.framebuffer;
692 const struct tu_image_view *dst = cmd->state.attachments[a];
693 const struct tu_image_view *src = cmd->state.attachments[gmem_a];
694
695 tu_resolve_sysmem(cmd, cs, src, dst, layer_mask, fb->layers, &cmd->state.render_area);
696 }
697
698 static void
tu6_emit_sysmem_resolves(struct tu_cmd_buffer * cmd,struct tu_cs * cs,const struct tu_subpass * subpass)699 tu6_emit_sysmem_resolves(struct tu_cmd_buffer *cmd,
700 struct tu_cs *cs,
701 const struct tu_subpass *subpass)
702 {
703 if (subpass->resolve_attachments) {
704 /* From the documentation for vkCmdNextSubpass, section 7.4 "Render Pass
705 * Commands":
706 *
707 * End-of-subpass multisample resolves are treated as color
708 * attachment writes for the purposes of synchronization.
709 * This applies to resolve operations for both color and
710 * depth/stencil attachments. That is, they are considered to
711 * execute in the VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
712 * pipeline stage and their writes are synchronized with
713 * VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT. Synchronization between
714 * rendering within a subpass and any resolve operations at the end
715 * of the subpass occurs automatically, without need for explicit
716 * dependencies or pipeline barriers. However, if the resolve
717 * attachment is also used in a different subpass, an explicit
718 * dependency is needed.
719 *
720 * We use the CP_BLIT path for sysmem resolves, which is really a
721 * transfer command, so we have to manually flush similar to the gmem
722 * resolve case. However, a flush afterwards isn't needed because of the
723 * last sentence and the fact that we're in sysmem mode.
724 */
725 tu6_emit_event_write(cmd, cs, PC_CCU_FLUSH_COLOR_TS);
726 if (subpass->resolve_depth_stencil)
727 tu6_emit_event_write(cmd, cs, PC_CCU_FLUSH_DEPTH_TS);
728
729 tu6_emit_event_write(cmd, cs, CACHE_INVALIDATE);
730
731 /* Wait for the flushes to land before using the 2D engine */
732 tu_cs_emit_wfi(cs);
733
734 for (unsigned i = 0; i < subpass->resolve_count; i++) {
735 uint32_t a = subpass->resolve_attachments[i].attachment;
736 if (a == VK_ATTACHMENT_UNUSED)
737 continue;
738
739 uint32_t gmem_a = tu_subpass_get_attachment_to_resolve(subpass, i);
740
741 tu6_emit_sysmem_resolve(cmd, cs, subpass->multiview_mask, a, gmem_a);
742 }
743 }
744 }
745
746 static void
tu6_emit_tile_load(struct tu_cmd_buffer * cmd,struct tu_cs * cs)747 tu6_emit_tile_load(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
748 {
749 tu6_emit_blit_scissor(cmd, cs, true);
750
751 for (uint32_t i = 0; i < cmd->state.pass->attachment_count; ++i)
752 tu_load_gmem_attachment(cmd, cs, i, cmd->state.tiling->binning, false);
753 }
754
755 static void
tu6_emit_tile_store(struct tu_cmd_buffer * cmd,struct tu_cs * cs)756 tu6_emit_tile_store(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
757 {
758 const struct tu_render_pass *pass = cmd->state.pass;
759 const struct tu_subpass *subpass = &pass->subpasses[pass->subpass_count-1];
760
761 tu_cs_emit_pkt7(cs, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
762 tu_cs_emit(cs, 0x0);
763
764 tu_cs_emit_pkt7(cs, CP_SET_MARKER, 1);
765 tu_cs_emit(cs, A6XX_CP_SET_MARKER_0_MODE(RM6_RESOLVE));
766
767 tu6_emit_blit_scissor(cmd, cs, true);
768
769 for (uint32_t a = 0; a < pass->attachment_count; ++a) {
770 if (pass->attachments[a].gmem)
771 tu_store_gmem_attachment(cmd, cs, a, a, cmd->state.tiling->binning_possible);
772 }
773
774 if (subpass->resolve_attachments) {
775 for (unsigned i = 0; i < subpass->resolve_count; i++) {
776 uint32_t a = subpass->resolve_attachments[i].attachment;
777 if (a != VK_ATTACHMENT_UNUSED) {
778 uint32_t gmem_a = tu_subpass_get_attachment_to_resolve(subpass, i);
779 tu_store_gmem_attachment(cmd, cs, a, gmem_a, false);
780 }
781 }
782 }
783 }
784
785 void
tu_disable_draw_states(struct tu_cmd_buffer * cmd,struct tu_cs * cs)786 tu_disable_draw_states(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
787 {
788 tu_cs_emit_pkt7(cs, CP_SET_DRAW_STATE, 3);
789 tu_cs_emit(cs, CP_SET_DRAW_STATE__0_COUNT(0) |
790 CP_SET_DRAW_STATE__0_DISABLE_ALL_GROUPS |
791 CP_SET_DRAW_STATE__0_GROUP_ID(0));
792 tu_cs_emit(cs, CP_SET_DRAW_STATE__1_ADDR_LO(0));
793 tu_cs_emit(cs, CP_SET_DRAW_STATE__2_ADDR_HI(0));
794
795 cmd->state.dirty |= TU_CMD_DIRTY_DRAW_STATE;
796 }
797
798 static void
tu6_init_hw(struct tu_cmd_buffer * cmd,struct tu_cs * cs)799 tu6_init_hw(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
800 {
801 struct tu_device *dev = cmd->device;
802 const struct tu_physical_device *phys_dev = dev->physical_device;
803
804 tu6_emit_event_write(cmd, cs, CACHE_INVALIDATE);
805
806 tu_cs_emit_regs(cs, A6XX_HLSQ_INVALIDATE_CMD(
807 .vs_state = true,
808 .hs_state = true,
809 .ds_state = true,
810 .gs_state = true,
811 .fs_state = true,
812 .cs_state = true,
813 .gfx_ibo = true,
814 .cs_ibo = true,
815 .gfx_shared_const = true,
816 .cs_shared_const = true,
817 .gfx_bindless = 0x1f,
818 .cs_bindless = 0x1f));
819
820 tu_cs_emit_wfi(cs);
821
822 cmd->state.cache.pending_flush_bits &=
823 ~(TU_CMD_FLAG_WAIT_FOR_IDLE | TU_CMD_FLAG_CACHE_INVALIDATE);
824
825 tu_cs_emit_regs(cs,
826 A6XX_RB_CCU_CNTL(.color_offset = phys_dev->ccu_offset_bypass));
827 cmd->state.ccu_state = TU_CMD_CCU_SYSMEM;
828 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_8E04, 0x00100000);
829 tu_cs_emit_write_reg(cs, REG_A6XX_SP_FLOAT_CNTL, 0);
830 tu_cs_emit_write_reg(cs, REG_A6XX_SP_UNKNOWN_AE00, 0);
831 tu_cs_emit_write_reg(cs, REG_A6XX_SP_PERFCTR_ENABLE, 0x3f);
832 tu_cs_emit_write_reg(cs, REG_A6XX_TPL1_UNKNOWN_B605, 0x44);
833 tu_cs_emit_write_reg(cs, REG_A6XX_TPL1_DBG_ECO_CNTL,
834 phys_dev->info->a6xx.magic.TPL1_DBG_ECO_CNTL);
835 tu_cs_emit_write_reg(cs, REG_A6XX_HLSQ_UNKNOWN_BE00, 0x80);
836 tu_cs_emit_write_reg(cs, REG_A6XX_HLSQ_UNKNOWN_BE01, 0);
837
838 tu_cs_emit_write_reg(cs, REG_A6XX_VPC_UNKNOWN_9600, 0);
839 tu_cs_emit_write_reg(cs, REG_A6XX_GRAS_DBG_ECO_CNTL, 0x880);
840 tu_cs_emit_write_reg(cs, REG_A6XX_HLSQ_UNKNOWN_BE04, 0);
841 tu_cs_emit_write_reg(cs, REG_A6XX_SP_CHICKEN_BITS, 0x00000410);
842 tu_cs_emit_write_reg(cs, REG_A6XX_SP_IBO_COUNT, 0);
843 tu_cs_emit_write_reg(cs, REG_A6XX_SP_UNKNOWN_B182, 0);
844 tu_cs_emit_regs(cs, A6XX_HLSQ_SHARED_CONSTS(.enable = false));
845 tu_cs_emit_write_reg(cs, REG_A6XX_UCHE_UNKNOWN_0E12, 0x3200000);
846 tu_cs_emit_write_reg(cs, REG_A6XX_UCHE_CLIENT_PF, 4);
847 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_8E01, 0x0);
848 tu_cs_emit_write_reg(cs, REG_A6XX_SP_UNKNOWN_A9A8, 0);
849 tu_cs_emit_regs(cs, A6XX_SP_MODE_CONTROL(.constant_demotion_enable = true,
850 .isammode = ISAMMODE_GL,
851 .shared_consts_enable = false));
852
853 /* TODO: set A6XX_VFD_ADD_OFFSET_INSTANCE and fix ir3 to avoid adding base instance */
854 tu_cs_emit_write_reg(cs, REG_A6XX_VFD_ADD_OFFSET, A6XX_VFD_ADD_OFFSET_VERTEX);
855 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_8811, 0x00000010);
856 tu_cs_emit_write_reg(cs, REG_A6XX_PC_MODE_CNTL, 0x1f);
857
858 tu_cs_emit_write_reg(cs, REG_A6XX_GRAS_UNKNOWN_8110, 0);
859
860 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_8818, 0);
861 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_8819, 0);
862 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_881A, 0);
863 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_881B, 0);
864 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_881C, 0);
865 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_881D, 0);
866 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_881E, 0);
867 tu_cs_emit_write_reg(cs, REG_A6XX_RB_UNKNOWN_88F0, 0);
868
869 tu_cs_emit_regs(cs, A6XX_VPC_POINT_COORD_INVERT(false));
870 tu_cs_emit_write_reg(cs, REG_A6XX_VPC_UNKNOWN_9300, 0);
871
872 tu_cs_emit_regs(cs, A6XX_VPC_SO_DISABLE(true));
873
874 tu_cs_emit_write_reg(cs, REG_A6XX_SP_UNKNOWN_B183, 0);
875
876 tu_cs_emit_write_reg(cs, REG_A6XX_GRAS_SU_CONSERVATIVE_RAS_CNTL, 0);
877 tu_cs_emit_write_reg(cs, REG_A6XX_GRAS_UNKNOWN_80AF, 0);
878 tu_cs_emit_write_reg(cs, REG_A6XX_VPC_UNKNOWN_9210, 0);
879 tu_cs_emit_write_reg(cs, REG_A6XX_VPC_UNKNOWN_9211, 0);
880 tu_cs_emit_write_reg(cs, REG_A6XX_VPC_UNKNOWN_9602, 0);
881 tu_cs_emit_write_reg(cs, REG_A6XX_PC_UNKNOWN_9E72, 0);
882 tu_cs_emit_write_reg(cs, REG_A6XX_SP_TP_MODE_CNTL,
883 0x000000a0 |
884 A6XX_SP_TP_MODE_CNTL_ISAMMODE(ISAMMODE_GL));
885 tu_cs_emit_write_reg(cs, REG_A6XX_HLSQ_CONTROL_5_REG, 0xfc);
886
887 tu_cs_emit_write_reg(cs, REG_A6XX_VFD_MODE_CNTL, 0x00000000);
888
889 tu_cs_emit_write_reg(cs, REG_A6XX_PC_MODE_CNTL, 0x0000001f);
890
891 tu_cs_emit_regs(cs, A6XX_RB_ALPHA_CONTROL()); /* always disable alpha test */
892 tu_cs_emit_regs(cs, A6XX_RB_DITHER_CNTL()); /* always disable dithering */
893
894 tu_disable_draw_states(cmd, cs);
895
896 tu_cs_emit_regs(cs,
897 A6XX_SP_TP_BORDER_COLOR_BASE_ADDR(.bo = dev->global_bo,
898 .bo_offset = gb_offset(bcolor_builtin)));
899 tu_cs_emit_regs(cs,
900 A6XX_SP_PS_TP_BORDER_COLOR_BASE_ADDR(.bo = dev->global_bo,
901 .bo_offset = gb_offset(bcolor_builtin)));
902
903 /* VSC buffers:
904 * use vsc pitches from the largest values used so far with this device
905 * if there hasn't been overflow, there will already be a scratch bo
906 * allocated for these sizes
907 *
908 * if overflow is detected, the stream size is increased by 2x
909 */
910 mtx_lock(&dev->mutex);
911
912 struct tu6_global *global = dev->global_bo->map;
913
914 uint32_t vsc_draw_overflow = global->vsc_draw_overflow;
915 uint32_t vsc_prim_overflow = global->vsc_prim_overflow;
916
917 if (vsc_draw_overflow >= dev->vsc_draw_strm_pitch)
918 dev->vsc_draw_strm_pitch = (dev->vsc_draw_strm_pitch - VSC_PAD) * 2 + VSC_PAD;
919
920 if (vsc_prim_overflow >= dev->vsc_prim_strm_pitch)
921 dev->vsc_prim_strm_pitch = (dev->vsc_prim_strm_pitch - VSC_PAD) * 2 + VSC_PAD;
922
923 cmd->vsc_prim_strm_pitch = dev->vsc_prim_strm_pitch;
924 cmd->vsc_draw_strm_pitch = dev->vsc_draw_strm_pitch;
925
926 mtx_unlock(&dev->mutex);
927
928 struct tu_bo *vsc_bo;
929 uint32_t size0 = cmd->vsc_prim_strm_pitch * MAX_VSC_PIPES +
930 cmd->vsc_draw_strm_pitch * MAX_VSC_PIPES;
931
932 tu_get_scratch_bo(dev, size0 + MAX_VSC_PIPES * 4, &vsc_bo);
933
934 tu_cs_emit_regs(cs,
935 A6XX_VSC_DRAW_STRM_SIZE_ADDRESS(.bo = vsc_bo, .bo_offset = size0));
936 tu_cs_emit_regs(cs,
937 A6XX_VSC_PRIM_STRM_ADDRESS(.bo = vsc_bo));
938 tu_cs_emit_regs(cs,
939 A6XX_VSC_DRAW_STRM_ADDRESS(.bo = vsc_bo,
940 .bo_offset = cmd->vsc_prim_strm_pitch * MAX_VSC_PIPES));
941
942 tu_cs_sanity_check(cs);
943 }
944
945 static void
update_vsc_pipe(struct tu_cmd_buffer * cmd,struct tu_cs * cs)946 update_vsc_pipe(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
947 {
948 const struct tu_tiling_config *tiling = cmd->state.tiling;
949
950 tu_cs_emit_regs(cs,
951 A6XX_VSC_BIN_SIZE(.width = tiling->tile0.width,
952 .height = tiling->tile0.height));
953
954 tu_cs_emit_regs(cs,
955 A6XX_VSC_BIN_COUNT(.nx = tiling->tile_count.width,
956 .ny = tiling->tile_count.height));
957
958 tu_cs_emit_pkt4(cs, REG_A6XX_VSC_PIPE_CONFIG_REG(0), 32);
959 tu_cs_emit_array(cs, tiling->pipe_config, 32);
960
961 tu_cs_emit_regs(cs,
962 A6XX_VSC_PRIM_STRM_PITCH(cmd->vsc_prim_strm_pitch),
963 A6XX_VSC_PRIM_STRM_LIMIT(cmd->vsc_prim_strm_pitch - VSC_PAD));
964
965 tu_cs_emit_regs(cs,
966 A6XX_VSC_DRAW_STRM_PITCH(cmd->vsc_draw_strm_pitch),
967 A6XX_VSC_DRAW_STRM_LIMIT(cmd->vsc_draw_strm_pitch - VSC_PAD));
968 }
969
970 static void
emit_vsc_overflow_test(struct tu_cmd_buffer * cmd,struct tu_cs * cs)971 emit_vsc_overflow_test(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
972 {
973 const struct tu_tiling_config *tiling = cmd->state.tiling;
974 const uint32_t used_pipe_count =
975 tiling->pipe_count.width * tiling->pipe_count.height;
976
977 for (int i = 0; i < used_pipe_count; i++) {
978 tu_cs_emit_pkt7(cs, CP_COND_WRITE5, 8);
979 tu_cs_emit(cs, CP_COND_WRITE5_0_FUNCTION(WRITE_GE) |
980 CP_COND_WRITE5_0_WRITE_MEMORY);
981 tu_cs_emit(cs, CP_COND_WRITE5_1_POLL_ADDR_LO(REG_A6XX_VSC_DRAW_STRM_SIZE_REG(i)));
982 tu_cs_emit(cs, CP_COND_WRITE5_2_POLL_ADDR_HI(0));
983 tu_cs_emit(cs, CP_COND_WRITE5_3_REF(cmd->vsc_draw_strm_pitch - VSC_PAD));
984 tu_cs_emit(cs, CP_COND_WRITE5_4_MASK(~0));
985 tu_cs_emit_qw(cs, global_iova(cmd, vsc_draw_overflow));
986 tu_cs_emit(cs, CP_COND_WRITE5_7_WRITE_DATA(cmd->vsc_draw_strm_pitch));
987
988 tu_cs_emit_pkt7(cs, CP_COND_WRITE5, 8);
989 tu_cs_emit(cs, CP_COND_WRITE5_0_FUNCTION(WRITE_GE) |
990 CP_COND_WRITE5_0_WRITE_MEMORY);
991 tu_cs_emit(cs, CP_COND_WRITE5_1_POLL_ADDR_LO(REG_A6XX_VSC_PRIM_STRM_SIZE_REG(i)));
992 tu_cs_emit(cs, CP_COND_WRITE5_2_POLL_ADDR_HI(0));
993 tu_cs_emit(cs, CP_COND_WRITE5_3_REF(cmd->vsc_prim_strm_pitch - VSC_PAD));
994 tu_cs_emit(cs, CP_COND_WRITE5_4_MASK(~0));
995 tu_cs_emit_qw(cs, global_iova(cmd, vsc_prim_overflow));
996 tu_cs_emit(cs, CP_COND_WRITE5_7_WRITE_DATA(cmd->vsc_prim_strm_pitch));
997 }
998
999 tu_cs_emit_pkt7(cs, CP_WAIT_MEM_WRITES, 0);
1000 }
1001
1002 static void
tu6_emit_binning_pass(struct tu_cmd_buffer * cmd,struct tu_cs * cs)1003 tu6_emit_binning_pass(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
1004 {
1005 struct tu_physical_device *phys_dev = cmd->device->physical_device;
1006 const struct tu_framebuffer *fb = cmd->state.framebuffer;
1007
1008 tu6_emit_window_scissor(cs, 0, 0, fb->width - 1, fb->height - 1);
1009
1010 tu_cs_emit_pkt7(cs, CP_SET_MARKER, 1);
1011 tu_cs_emit(cs, A6XX_CP_SET_MARKER_0_MODE(RM6_BINNING));
1012
1013 tu_cs_emit_pkt7(cs, CP_SET_VISIBILITY_OVERRIDE, 1);
1014 tu_cs_emit(cs, 0x1);
1015
1016 tu_cs_emit_pkt7(cs, CP_SET_MODE, 1);
1017 tu_cs_emit(cs, 0x1);
1018
1019 tu_cs_emit_wfi(cs);
1020
1021 tu_cs_emit_regs(cs,
1022 A6XX_VFD_MODE_CNTL(.render_mode = BINNING_PASS));
1023
1024 update_vsc_pipe(cmd, cs);
1025
1026 tu_cs_emit_regs(cs,
1027 A6XX_PC_POWER_CNTL(phys_dev->info->a6xx.magic.PC_POWER_CNTL));
1028
1029 tu_cs_emit_regs(cs,
1030 A6XX_VFD_POWER_CNTL(phys_dev->info->a6xx.magic.PC_POWER_CNTL));
1031
1032 tu_cs_emit_pkt7(cs, CP_EVENT_WRITE, 1);
1033 tu_cs_emit(cs, UNK_2C);
1034
1035 tu_cs_emit_regs(cs,
1036 A6XX_RB_WINDOW_OFFSET(.x = 0, .y = 0));
1037
1038 tu_cs_emit_regs(cs,
1039 A6XX_SP_TP_WINDOW_OFFSET(.x = 0, .y = 0));
1040
1041 trace_start_binning_ib(&cmd->trace, cs);
1042
1043 /* emit IB to binning drawcmds: */
1044 tu_cs_emit_call(cs, &cmd->draw_cs);
1045
1046 trace_end_binning_ib(&cmd->trace, cs);
1047
1048 /* switching from binning pass to GMEM pass will cause a switch from
1049 * PROGRAM_BINNING to PROGRAM, which invalidates const state (XS_CONST states)
1050 * so make sure these states are re-emitted
1051 * (eventually these states shouldn't exist at all with shader prologue)
1052 * only VS and GS are invalidated, as FS isn't emitted in binning pass,
1053 * and we don't use HW binning when tesselation is used
1054 */
1055 tu_cs_emit_pkt7(cs, CP_SET_DRAW_STATE, 3);
1056 tu_cs_emit(cs, CP_SET_DRAW_STATE__0_COUNT(0) |
1057 CP_SET_DRAW_STATE__0_DISABLE |
1058 CP_SET_DRAW_STATE__0_GROUP_ID(TU_DRAW_STATE_CONST));
1059 tu_cs_emit(cs, CP_SET_DRAW_STATE__1_ADDR_LO(0));
1060 tu_cs_emit(cs, CP_SET_DRAW_STATE__2_ADDR_HI(0));
1061
1062 tu_cs_emit_pkt7(cs, CP_EVENT_WRITE, 1);
1063 tu_cs_emit(cs, UNK_2D);
1064
1065 /* This flush is probably required because the VSC, which produces the
1066 * visibility stream, is a client of UCHE, whereas the CP needs to read the
1067 * visibility stream (without caching) to do draw skipping. The
1068 * WFI+WAIT_FOR_ME combination guarantees that the binning commands
1069 * submitted are finished before reading the VSC regs (in
1070 * emit_vsc_overflow_test) or the VSC_DATA buffer directly (implicitly as
1071 * part of draws).
1072 */
1073 tu6_emit_event_write(cmd, cs, CACHE_FLUSH_TS);
1074
1075 tu_cs_emit_wfi(cs);
1076
1077 tu_cs_emit_pkt7(cs, CP_WAIT_FOR_ME, 0);
1078
1079 emit_vsc_overflow_test(cmd, cs);
1080
1081 tu_cs_emit_pkt7(cs, CP_SET_VISIBILITY_OVERRIDE, 1);
1082 tu_cs_emit(cs, 0x0);
1083
1084 tu_cs_emit_pkt7(cs, CP_SET_MODE, 1);
1085 tu_cs_emit(cs, 0x0);
1086 }
1087
1088 static struct tu_draw_state
tu_emit_input_attachments(struct tu_cmd_buffer * cmd,const struct tu_subpass * subpass,bool gmem)1089 tu_emit_input_attachments(struct tu_cmd_buffer *cmd,
1090 const struct tu_subpass *subpass,
1091 bool gmem)
1092 {
1093 const struct tu_tiling_config *tiling = cmd->state.tiling;
1094
1095 /* note: we can probably emit input attachments just once for the whole
1096 * renderpass, this would avoid emitting both sysmem/gmem versions
1097 *
1098 * emit two texture descriptors for each input, as a workaround for
1099 * d24s8/d32s8, which can be sampled as both float (depth) and integer (stencil)
1100 * tu_shader lowers uint input attachment loads to use the 2nd descriptor
1101 * in the pair
1102 * TODO: a smarter workaround
1103 */
1104
1105 if (!subpass->input_count)
1106 return (struct tu_draw_state) {};
1107
1108 struct tu_cs_memory texture;
1109 VkResult result = tu_cs_alloc(&cmd->sub_cs, subpass->input_count * 2,
1110 A6XX_TEX_CONST_DWORDS, &texture);
1111 if (result != VK_SUCCESS) {
1112 cmd->record_result = result;
1113 return (struct tu_draw_state) {};
1114 }
1115
1116 for (unsigned i = 0; i < subpass->input_count * 2; i++) {
1117 uint32_t a = subpass->input_attachments[i / 2].attachment;
1118 if (a == VK_ATTACHMENT_UNUSED)
1119 continue;
1120
1121 const struct tu_image_view *iview = cmd->state.attachments[a];
1122 const struct tu_render_pass_attachment *att =
1123 &cmd->state.pass->attachments[a];
1124 uint32_t *dst = &texture.map[A6XX_TEX_CONST_DWORDS * i];
1125 uint32_t gmem_offset = tu_attachment_gmem_offset(cmd, att);
1126 uint32_t cpp = att->cpp;
1127
1128 memcpy(dst, iview->view.descriptor, A6XX_TEX_CONST_DWORDS * 4);
1129
1130 /* Cube descriptors require a different sampling instruction in shader,
1131 * however we don't know whether image is a cube or not until the start
1132 * of a renderpass. We have to patch the descriptor to make it compatible
1133 * with how it is sampled in shader.
1134 */
1135 enum a6xx_tex_type tex_type = (dst[2] & A6XX_TEX_CONST_2_TYPE__MASK) >>
1136 A6XX_TEX_CONST_2_TYPE__SHIFT;
1137 if (tex_type == A6XX_TEX_CUBE) {
1138 dst[2] &= ~A6XX_TEX_CONST_2_TYPE__MASK;
1139 dst[2] |= A6XX_TEX_CONST_2_TYPE(A6XX_TEX_2D);
1140
1141 uint32_t depth = (dst[5] & A6XX_TEX_CONST_5_DEPTH__MASK) >>
1142 A6XX_TEX_CONST_5_DEPTH__SHIFT;
1143 dst[5] &= ~A6XX_TEX_CONST_5_DEPTH__MASK;
1144 dst[5] |= A6XX_TEX_CONST_5_DEPTH(depth * 6);
1145 }
1146
1147 if (i % 2 == 1 && att->format == VK_FORMAT_D24_UNORM_S8_UINT) {
1148 /* note this works because spec says fb and input attachments
1149 * must use identity swizzle
1150 *
1151 * Also we clear swap to WZYX. This is because the view might have
1152 * picked XYZW to work better with border colors.
1153 */
1154 dst[0] &= ~(A6XX_TEX_CONST_0_FMT__MASK |
1155 A6XX_TEX_CONST_0_SWAP__MASK |
1156 A6XX_TEX_CONST_0_SWIZ_X__MASK | A6XX_TEX_CONST_0_SWIZ_Y__MASK |
1157 A6XX_TEX_CONST_0_SWIZ_Z__MASK | A6XX_TEX_CONST_0_SWIZ_W__MASK);
1158 if (!cmd->device->physical_device->info->a6xx.has_z24uint_s8uint) {
1159 dst[0] |= A6XX_TEX_CONST_0_FMT(FMT6_8_8_8_8_UINT) |
1160 A6XX_TEX_CONST_0_SWIZ_X(A6XX_TEX_W) |
1161 A6XX_TEX_CONST_0_SWIZ_Y(A6XX_TEX_ZERO) |
1162 A6XX_TEX_CONST_0_SWIZ_Z(A6XX_TEX_ZERO) |
1163 A6XX_TEX_CONST_0_SWIZ_W(A6XX_TEX_ONE);
1164 } else {
1165 dst[0] |= A6XX_TEX_CONST_0_FMT(FMT6_Z24_UINT_S8_UINT) |
1166 A6XX_TEX_CONST_0_SWIZ_X(A6XX_TEX_Y) |
1167 A6XX_TEX_CONST_0_SWIZ_Y(A6XX_TEX_ZERO) |
1168 A6XX_TEX_CONST_0_SWIZ_Z(A6XX_TEX_ZERO) |
1169 A6XX_TEX_CONST_0_SWIZ_W(A6XX_TEX_ONE);
1170 }
1171 }
1172
1173 if (i % 2 == 1 && att->format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
1174 dst[0] &= ~A6XX_TEX_CONST_0_FMT__MASK;
1175 dst[0] |= A6XX_TEX_CONST_0_FMT(FMT6_8_UINT);
1176 dst[2] &= ~(A6XX_TEX_CONST_2_PITCHALIGN__MASK | A6XX_TEX_CONST_2_PITCH__MASK);
1177 dst[2] |= A6XX_TEX_CONST_2_PITCH(iview->stencil_PITCH << 6);
1178 dst[3] = 0;
1179 dst[4] = iview->stencil_base_addr;
1180 dst[5] = (dst[5] & 0xffff) | iview->stencil_base_addr >> 32;
1181
1182 cpp = att->samples;
1183 gmem_offset = att->gmem_offset_stencil[cmd->state.gmem_layout];
1184 }
1185
1186 if (!gmem || !subpass->input_attachments[i / 2].patch_input_gmem)
1187 continue;
1188
1189 /* patched for gmem */
1190 dst[0] &= ~(A6XX_TEX_CONST_0_SWAP__MASK | A6XX_TEX_CONST_0_TILE_MODE__MASK);
1191 dst[0] |= A6XX_TEX_CONST_0_TILE_MODE(TILE6_2);
1192 dst[2] =
1193 A6XX_TEX_CONST_2_TYPE(A6XX_TEX_2D) |
1194 A6XX_TEX_CONST_2_PITCH(tiling->tile0.width * cpp);
1195 dst[3] = 0;
1196 dst[4] = cmd->device->physical_device->gmem_base + gmem_offset;
1197 dst[5] = A6XX_TEX_CONST_5_DEPTH(1);
1198 for (unsigned i = 6; i < A6XX_TEX_CONST_DWORDS; i++)
1199 dst[i] = 0;
1200 }
1201
1202 struct tu_cs cs;
1203 struct tu_draw_state ds = tu_cs_draw_state(&cmd->sub_cs, &cs, 9);
1204
1205 tu_cs_emit_pkt7(&cs, CP_LOAD_STATE6_FRAG, 3);
1206 tu_cs_emit(&cs, CP_LOAD_STATE6_0_DST_OFF(0) |
1207 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
1208 CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
1209 CP_LOAD_STATE6_0_STATE_BLOCK(SB6_FS_TEX) |
1210 CP_LOAD_STATE6_0_NUM_UNIT(subpass->input_count * 2));
1211 tu_cs_emit_qw(&cs, texture.iova);
1212
1213 tu_cs_emit_regs(&cs, A6XX_SP_FS_TEX_CONST(.qword = texture.iova));
1214
1215 tu_cs_emit_regs(&cs, A6XX_SP_FS_TEX_COUNT(subpass->input_count * 2));
1216
1217 assert(cs.cur == cs.end); /* validate draw state size */
1218
1219 return ds;
1220 }
1221
1222 static void
tu_set_input_attachments(struct tu_cmd_buffer * cmd,const struct tu_subpass * subpass)1223 tu_set_input_attachments(struct tu_cmd_buffer *cmd, const struct tu_subpass *subpass)
1224 {
1225 struct tu_cs *cs = &cmd->draw_cs;
1226
1227 tu_cs_emit_pkt7(cs, CP_SET_DRAW_STATE, 6);
1228 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_INPUT_ATTACHMENTS_GMEM,
1229 tu_emit_input_attachments(cmd, subpass, true));
1230 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_INPUT_ATTACHMENTS_SYSMEM,
1231 tu_emit_input_attachments(cmd, subpass, false));
1232 }
1233
1234
1235 static void
tu_emit_renderpass_begin(struct tu_cmd_buffer * cmd,const VkClearValue * clear_values)1236 tu_emit_renderpass_begin(struct tu_cmd_buffer *cmd,
1237 const VkClearValue *clear_values)
1238 {
1239 struct tu_cs *cs = &cmd->draw_cs;
1240
1241 tu_cond_exec_start(cs, CP_COND_EXEC_0_RENDER_MODE_GMEM);
1242
1243 tu6_emit_tile_load(cmd, cs);
1244
1245 tu6_emit_blit_scissor(cmd, cs, false);
1246
1247 for (uint32_t i = 0; i < cmd->state.pass->attachment_count; ++i)
1248 tu_clear_gmem_attachment(cmd, cs, i, &clear_values[i]);
1249
1250 tu_cond_exec_end(cs);
1251
1252 tu_cond_exec_start(cs, CP_COND_EXEC_0_RENDER_MODE_SYSMEM);
1253
1254 for (uint32_t i = 0; i < cmd->state.pass->attachment_count; ++i)
1255 tu_clear_sysmem_attachment(cmd, cs, i, &clear_values[i]);
1256
1257 tu_cond_exec_end(cs);
1258 }
1259
1260 static void
tu6_sysmem_render_begin(struct tu_cmd_buffer * cmd,struct tu_cs * cs,struct tu_renderpass_result * autotune_result)1261 tu6_sysmem_render_begin(struct tu_cmd_buffer *cmd, struct tu_cs *cs,
1262 struct tu_renderpass_result *autotune_result)
1263 {
1264 const struct tu_framebuffer *fb = cmd->state.framebuffer;
1265
1266 tu_lrz_sysmem_begin(cmd, cs);
1267
1268 assert(fb->width > 0 && fb->height > 0);
1269 tu6_emit_window_scissor(cs, 0, 0, fb->width - 1, fb->height - 1);
1270 tu6_emit_window_offset(cs, 0, 0);
1271
1272 tu6_emit_bin_size(cs, 0, 0,
1273 A6XX_RB_BIN_CONTROL_BUFFERS_LOCATION(BUFFERS_IN_SYSMEM) |
1274 A6XX_RB_BIN_CONTROL_FORCE_LRZ_WRITE_DIS);
1275
1276 tu_cs_emit_pkt7(cs, CP_SET_MARKER, 1);
1277 tu_cs_emit(cs, A6XX_CP_SET_MARKER_0_MODE(RM6_BYPASS));
1278
1279 tu_cs_emit_pkt7(cs, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
1280 tu_cs_emit(cs, 0x0);
1281
1282 tu_emit_cache_flush_ccu(cmd, cs, TU_CMD_CCU_SYSMEM);
1283
1284 tu_cs_emit_pkt7(cs, CP_SET_VISIBILITY_OVERRIDE, 1);
1285 tu_cs_emit(cs, 0x1);
1286
1287 tu_cs_emit_pkt7(cs, CP_SET_MODE, 1);
1288 tu_cs_emit(cs, 0x0);
1289
1290 tu_autotune_begin_renderpass(cmd, cs, autotune_result);
1291
1292 tu_cs_sanity_check(cs);
1293 }
1294
1295 static void
tu6_sysmem_render_end(struct tu_cmd_buffer * cmd,struct tu_cs * cs,struct tu_renderpass_result * autotune_result)1296 tu6_sysmem_render_end(struct tu_cmd_buffer *cmd, struct tu_cs *cs,
1297 struct tu_renderpass_result *autotune_result)
1298 {
1299 tu_autotune_end_renderpass(cmd, cs, autotune_result);
1300
1301 /* Do any resolves of the last subpass. These are handled in the
1302 * tile_store_cs in the gmem path.
1303 */
1304 tu6_emit_sysmem_resolves(cmd, cs, cmd->state.subpass);
1305
1306 tu_cs_emit_call(cs, &cmd->draw_epilogue_cs);
1307
1308 tu_cs_emit_pkt7(cs, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
1309 tu_cs_emit(cs, 0x0);
1310
1311 tu_lrz_sysmem_end(cmd, cs);
1312
1313 tu_cs_sanity_check(cs);
1314 }
1315
1316 static void
tu6_tile_render_begin(struct tu_cmd_buffer * cmd,struct tu_cs * cs,struct tu_renderpass_result * autotune_result)1317 tu6_tile_render_begin(struct tu_cmd_buffer *cmd, struct tu_cs *cs,
1318 struct tu_renderpass_result *autotune_result)
1319 {
1320 struct tu_physical_device *phys_dev = cmd->device->physical_device;
1321 const struct tu_tiling_config *tiling = cmd->state.tiling;
1322 tu_lrz_tiling_begin(cmd, cs);
1323
1324 tu_cs_emit_pkt7(cs, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
1325 tu_cs_emit(cs, 0x0);
1326
1327 tu_emit_cache_flush_ccu(cmd, cs, TU_CMD_CCU_GMEM);
1328
1329 if (use_hw_binning(cmd)) {
1330 tu6_emit_bin_size(cs, tiling->tile0.width, tiling->tile0.height,
1331 A6XX_RB_BIN_CONTROL_RENDER_MODE(BINNING_PASS) |
1332 A6XX_RB_BIN_CONTROL_LRZ_FEEDBACK_ZMODE_MASK(0x6));
1333
1334 tu6_emit_render_cntl(cmd, cmd->state.subpass, cs, true);
1335
1336 tu6_emit_binning_pass(cmd, cs);
1337
1338 tu6_emit_bin_size(cs, tiling->tile0.width, tiling->tile0.height,
1339 A6XX_RB_BIN_CONTROL_FORCE_LRZ_WRITE_DIS |
1340 A6XX_RB_BIN_CONTROL_LRZ_FEEDBACK_ZMODE_MASK(0x6));
1341
1342 tu_cs_emit_regs(cs,
1343 A6XX_VFD_MODE_CNTL(0));
1344
1345 tu_cs_emit_regs(cs,
1346 A6XX_PC_POWER_CNTL(phys_dev->info->a6xx.magic.PC_POWER_CNTL));
1347
1348 tu_cs_emit_regs(cs,
1349 A6XX_VFD_POWER_CNTL(phys_dev->info->a6xx.magic.PC_POWER_CNTL));
1350
1351 tu_cs_emit_pkt7(cs, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
1352 tu_cs_emit(cs, 0x1);
1353 } else {
1354 tu6_emit_bin_size(cs, tiling->tile0.width, tiling->tile0.height,
1355 A6XX_RB_BIN_CONTROL_LRZ_FEEDBACK_ZMODE_MASK(0x6));
1356
1357 if (tiling->binning_possible) {
1358 /* Mark all tiles as visible for tu6_emit_cond_for_load_stores(), since
1359 * the actual binner didn't run.
1360 */
1361 int pipe_count = tiling->pipe_count.width * tiling->pipe_count.height;
1362 tu_cs_emit_pkt4(cs, REG_A6XX_VSC_STATE_REG(0), pipe_count);
1363 for (int i = 0; i < pipe_count; i++)
1364 tu_cs_emit(cs, ~0);
1365 }
1366 }
1367
1368 tu_autotune_begin_renderpass(cmd, cs, autotune_result);
1369
1370 tu_cs_sanity_check(cs);
1371 }
1372
1373 static void
tu6_render_tile(struct tu_cmd_buffer * cmd,struct tu_cs * cs,uint32_t tx,uint32_t ty,uint32_t pipe,uint32_t slot)1374 tu6_render_tile(struct tu_cmd_buffer *cmd, struct tu_cs *cs,
1375 uint32_t tx, uint32_t ty, uint32_t pipe, uint32_t slot)
1376 {
1377 tu6_emit_tile_select(cmd, &cmd->cs, tx, ty, pipe, slot);
1378
1379 trace_start_draw_ib_gmem(&cmd->trace, &cmd->cs);
1380
1381 /* Primitives that passed all tests are still counted in in each
1382 * tile even with HW binning beforehand. Do not permit it.
1383 */
1384 if (cmd->state.prim_generated_query_running_before_rp)
1385 tu6_emit_event_write(cmd, cs, STOP_PRIMITIVE_CTRS);
1386
1387 tu_cs_emit_call(cs, &cmd->draw_cs);
1388
1389 if (cmd->state.prim_generated_query_running_before_rp)
1390 tu6_emit_event_write(cmd, cs, START_PRIMITIVE_CTRS);
1391
1392 if (use_hw_binning(cmd)) {
1393 tu_cs_emit_pkt7(cs, CP_SET_MARKER, 1);
1394 tu_cs_emit(cs, A6XX_CP_SET_MARKER_0_MODE(RM6_ENDVIS));
1395 }
1396
1397 /* Predicate is changed in draw_cs so we have to re-emit it */
1398 if (cmd->state.rp.draw_cs_writes_to_cond_pred)
1399 tu6_emit_cond_for_load_stores(cmd, cs, pipe, slot, false);
1400
1401 tu_cs_emit_call(cs, &cmd->tile_store_cs);
1402
1403 if (!u_trace_iterator_equal(cmd->trace_renderpass_start, cmd->trace_renderpass_end)) {
1404 tu_cs_emit_wfi(cs);
1405 tu_cs_emit_pkt7(&cmd->cs, CP_WAIT_FOR_ME, 0);
1406 u_trace_clone_append(cmd->trace_renderpass_start,
1407 cmd->trace_renderpass_end,
1408 &cmd->trace,
1409 cs, tu_copy_timestamp_buffer);
1410 }
1411
1412 tu_cs_sanity_check(cs);
1413
1414 trace_end_draw_ib_gmem(&cmd->trace, &cmd->cs);
1415 }
1416
1417 static void
tu6_tile_render_end(struct tu_cmd_buffer * cmd,struct tu_cs * cs,struct tu_renderpass_result * autotune_result)1418 tu6_tile_render_end(struct tu_cmd_buffer *cmd, struct tu_cs *cs,
1419 struct tu_renderpass_result *autotune_result)
1420 {
1421 tu_autotune_end_renderpass(cmd, cs, autotune_result);
1422
1423 tu_cs_emit_call(cs, &cmd->draw_epilogue_cs);
1424
1425 tu_lrz_tiling_end(cmd, cs);
1426
1427 tu6_emit_event_write(cmd, cs, PC_CCU_RESOLVE_TS);
1428
1429 tu_cs_sanity_check(cs);
1430 }
1431
1432 static void
tu_cmd_render_tiles(struct tu_cmd_buffer * cmd,struct tu_renderpass_result * autotune_result)1433 tu_cmd_render_tiles(struct tu_cmd_buffer *cmd,
1434 struct tu_renderpass_result *autotune_result)
1435 {
1436 const struct tu_framebuffer *fb = cmd->state.framebuffer;
1437 const struct tu_tiling_config *tiling = cmd->state.tiling;
1438
1439 /* Create gmem stores now (at EndRenderPass time)) because they needed to
1440 * know whether to allow their conditional execution, which was tied to a
1441 * state that was known only at the end of the renderpass. They will be
1442 * called from tu6_render_tile().
1443 */
1444 tu_cs_begin(&cmd->tile_store_cs);
1445 tu6_emit_tile_store(cmd, &cmd->tile_store_cs);
1446 tu_cs_end(&cmd->tile_store_cs);
1447
1448 tu6_tile_render_begin(cmd, &cmd->cs, autotune_result);
1449
1450 /* Note: we reverse the order of walking the pipes and tiles on every
1451 * other row, to improve texture cache locality compared to raster order.
1452 */
1453 for (uint32_t py = 0; py < tiling->pipe_count.height; py++) {
1454 uint32_t pipe_row = py * tiling->pipe_count.width;
1455 for (uint32_t pipe_row_i = 0; pipe_row_i < tiling->pipe_count.width; pipe_row_i++) {
1456 uint32_t px;
1457 if (py & 1)
1458 px = tiling->pipe_count.width - 1 - pipe_row_i;
1459 else
1460 px = pipe_row_i;
1461 uint32_t pipe = pipe_row + px;
1462 uint32_t tx1 = px * tiling->pipe0.width;
1463 uint32_t ty1 = py * tiling->pipe0.height;
1464 uint32_t tx2 = MIN2(tx1 + tiling->pipe0.width, tiling->tile_count.width);
1465 uint32_t ty2 = MIN2(ty1 + tiling->pipe0.height, tiling->tile_count.height);
1466 uint32_t tile_row_stride = tx2 - tx1;
1467 uint32_t slot_row = 0;
1468 for (uint32_t ty = ty1; ty < ty2; ty++) {
1469 for (uint32_t tile_row_i = 0; tile_row_i < tile_row_stride; tile_row_i++) {
1470 uint32_t tx;
1471 if (ty & 1)
1472 tx = tile_row_stride - 1 - tile_row_i;
1473 else
1474 tx = tile_row_i;
1475 uint32_t slot = slot_row + tx;
1476 tu6_render_tile(cmd, &cmd->cs, tx1 + tx, ty, pipe, slot);
1477 }
1478 slot_row += tile_row_stride;
1479 }
1480 }
1481 }
1482
1483 tu6_tile_render_end(cmd, &cmd->cs, autotune_result);
1484
1485 trace_end_render_pass(&cmd->trace, &cmd->cs, fb, tiling);
1486
1487 if (!u_trace_iterator_equal(cmd->trace_renderpass_start, cmd->trace_renderpass_end))
1488 u_trace_disable_event_range(cmd->trace_renderpass_start,
1489 cmd->trace_renderpass_end);
1490
1491 /* Reset the gmem store CS entry lists so that the next render pass
1492 * does its own stores.
1493 */
1494 tu_cs_discard_entries(&cmd->tile_store_cs);
1495 }
1496
1497 static void
tu_cmd_render_sysmem(struct tu_cmd_buffer * cmd,struct tu_renderpass_result * autotune_result)1498 tu_cmd_render_sysmem(struct tu_cmd_buffer *cmd,
1499 struct tu_renderpass_result *autotune_result)
1500 {
1501 tu6_sysmem_render_begin(cmd, &cmd->cs, autotune_result);
1502
1503 trace_start_draw_ib_sysmem(&cmd->trace, &cmd->cs);
1504
1505 tu_cs_emit_call(&cmd->cs, &cmd->draw_cs);
1506
1507 trace_end_draw_ib_sysmem(&cmd->trace, &cmd->cs);
1508
1509 tu6_sysmem_render_end(cmd, &cmd->cs, autotune_result);
1510
1511 trace_end_render_pass(&cmd->trace, &cmd->cs, cmd->state.framebuffer, cmd->state.tiling);
1512 }
1513
1514 void
tu_cmd_render(struct tu_cmd_buffer * cmd_buffer)1515 tu_cmd_render(struct tu_cmd_buffer *cmd_buffer)
1516 {
1517 if (cmd_buffer->state.rp.has_tess)
1518 tu6_lazy_emit_tessfactor_addr(cmd_buffer);
1519
1520 struct tu_renderpass_result *autotune_result = NULL;
1521 if (use_sysmem_rendering(cmd_buffer, &autotune_result))
1522 tu_cmd_render_sysmem(cmd_buffer, autotune_result);
1523 else
1524 tu_cmd_render_tiles(cmd_buffer, autotune_result);
1525
1526 /* Outside of renderpasses we assume all draw states are disabled. We do
1527 * this outside the draw CS for the normal case where 3d gmem stores aren't
1528 * used.
1529 */
1530 tu_disable_draw_states(cmd_buffer, &cmd_buffer->cs);
1531
1532 }
1533
tu_reset_render_pass(struct tu_cmd_buffer * cmd_buffer)1534 static void tu_reset_render_pass(struct tu_cmd_buffer *cmd_buffer)
1535 {
1536 /* discard draw_cs and draw_epilogue_cs entries now that the tiles are
1537 rendered */
1538 tu_cs_discard_entries(&cmd_buffer->draw_cs);
1539 tu_cs_begin(&cmd_buffer->draw_cs);
1540 tu_cs_discard_entries(&cmd_buffer->draw_epilogue_cs);
1541 tu_cs_begin(&cmd_buffer->draw_epilogue_cs);
1542
1543 cmd_buffer->state.pass = NULL;
1544 cmd_buffer->state.subpass = NULL;
1545 cmd_buffer->state.framebuffer = NULL;
1546 cmd_buffer->state.attachments = NULL;
1547 cmd_buffer->state.gmem_layout = TU_GMEM_LAYOUT_COUNT; /* invalid value to prevent looking up gmem offsets */
1548 memset(&cmd_buffer->state.rp, 0, sizeof(cmd_buffer->state.rp));
1549
1550 /* LRZ is not valid next time we use it */
1551 cmd_buffer->state.lrz.valid = false;
1552 cmd_buffer->state.dirty |= TU_CMD_DIRTY_LRZ;
1553 }
1554
1555 static VkResult
tu_create_cmd_buffer(struct tu_device * device,struct tu_cmd_pool * pool,VkCommandBufferLevel level,VkCommandBuffer * pCommandBuffer)1556 tu_create_cmd_buffer(struct tu_device *device,
1557 struct tu_cmd_pool *pool,
1558 VkCommandBufferLevel level,
1559 VkCommandBuffer *pCommandBuffer)
1560 {
1561 struct tu_cmd_buffer *cmd_buffer;
1562
1563 cmd_buffer = vk_zalloc2(&device->vk.alloc, NULL, sizeof(*cmd_buffer), 8,
1564 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1565
1566 if (cmd_buffer == NULL)
1567 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1568
1569 VkResult result = vk_command_buffer_init(&cmd_buffer->vk, &pool->vk, level);
1570 if (result != VK_SUCCESS) {
1571 vk_free2(&device->vk.alloc, NULL, cmd_buffer);
1572 return result;
1573 }
1574
1575 cmd_buffer->device = device;
1576 cmd_buffer->pool = pool;
1577
1578 if (pool) {
1579 list_addtail(&cmd_buffer->pool_link, &pool->cmd_buffers);
1580 cmd_buffer->queue_family_index = pool->vk.queue_family_index;
1581
1582 } else {
1583 /* Init the pool_link so we can safely call list_del when we destroy
1584 * the command buffer
1585 */
1586 list_inithead(&cmd_buffer->pool_link);
1587 cmd_buffer->queue_family_index = TU_QUEUE_GENERAL;
1588 }
1589
1590
1591 u_trace_init(&cmd_buffer->trace, &device->trace_context);
1592 list_inithead(&cmd_buffer->renderpass_autotune_results);
1593
1594 tu_cs_init(&cmd_buffer->cs, device, TU_CS_MODE_GROW, 4096);
1595 tu_cs_init(&cmd_buffer->draw_cs, device, TU_CS_MODE_GROW, 4096);
1596 tu_cs_init(&cmd_buffer->tile_store_cs, device, TU_CS_MODE_GROW, 2048);
1597 tu_cs_init(&cmd_buffer->draw_epilogue_cs, device, TU_CS_MODE_GROW, 4096);
1598 tu_cs_init(&cmd_buffer->sub_cs, device, TU_CS_MODE_SUB_STREAM, 2048);
1599 tu_cs_init(&cmd_buffer->pre_chain.draw_cs, device, TU_CS_MODE_GROW, 4096);
1600 tu_cs_init(&cmd_buffer->pre_chain.draw_epilogue_cs, device, TU_CS_MODE_GROW, 4096);
1601
1602 *pCommandBuffer = tu_cmd_buffer_to_handle(cmd_buffer);
1603
1604 return VK_SUCCESS;
1605 }
1606
1607 static void
tu_cmd_buffer_destroy(struct tu_cmd_buffer * cmd_buffer)1608 tu_cmd_buffer_destroy(struct tu_cmd_buffer *cmd_buffer)
1609 {
1610 list_del(&cmd_buffer->pool_link);
1611
1612 tu_cs_finish(&cmd_buffer->cs);
1613 tu_cs_finish(&cmd_buffer->draw_cs);
1614 tu_cs_finish(&cmd_buffer->tile_store_cs);
1615 tu_cs_finish(&cmd_buffer->draw_epilogue_cs);
1616 tu_cs_finish(&cmd_buffer->sub_cs);
1617 tu_cs_finish(&cmd_buffer->pre_chain.draw_cs);
1618 tu_cs_finish(&cmd_buffer->pre_chain.draw_epilogue_cs);
1619
1620 u_trace_fini(&cmd_buffer->trace);
1621
1622 tu_autotune_free_results(cmd_buffer->device, &cmd_buffer->renderpass_autotune_results);
1623
1624 for (unsigned i = 0; i < MAX_BIND_POINTS; i++) {
1625 if (cmd_buffer->descriptors[i].push_set.layout)
1626 tu_descriptor_set_layout_unref(cmd_buffer->device,
1627 cmd_buffer->descriptors[i].push_set.layout);
1628 }
1629
1630 vk_command_buffer_finish(&cmd_buffer->vk);
1631 vk_free2(&cmd_buffer->device->vk.alloc, &cmd_buffer->pool->vk.alloc,
1632 cmd_buffer);
1633 }
1634
1635 static VkResult
tu_reset_cmd_buffer(struct tu_cmd_buffer * cmd_buffer)1636 tu_reset_cmd_buffer(struct tu_cmd_buffer *cmd_buffer)
1637 {
1638 vk_command_buffer_reset(&cmd_buffer->vk);
1639
1640 cmd_buffer->record_result = VK_SUCCESS;
1641
1642 tu_cs_reset(&cmd_buffer->cs);
1643 tu_cs_reset(&cmd_buffer->draw_cs);
1644 tu_cs_reset(&cmd_buffer->tile_store_cs);
1645 tu_cs_reset(&cmd_buffer->draw_epilogue_cs);
1646 tu_cs_reset(&cmd_buffer->sub_cs);
1647 tu_cs_reset(&cmd_buffer->pre_chain.draw_cs);
1648 tu_cs_reset(&cmd_buffer->pre_chain.draw_epilogue_cs);
1649
1650 tu_autotune_free_results(cmd_buffer->device, &cmd_buffer->renderpass_autotune_results);
1651
1652 for (unsigned i = 0; i < MAX_BIND_POINTS; i++) {
1653 memset(&cmd_buffer->descriptors[i].sets, 0, sizeof(cmd_buffer->descriptors[i].sets));
1654 if (cmd_buffer->descriptors[i].push_set.layout)
1655 tu_descriptor_set_layout_unref(cmd_buffer->device,
1656 cmd_buffer->descriptors[i].push_set.layout);
1657 memset(&cmd_buffer->descriptors[i].push_set, 0, sizeof(cmd_buffer->descriptors[i].push_set));
1658 cmd_buffer->descriptors[i].push_set.base.type = VK_OBJECT_TYPE_DESCRIPTOR_SET;
1659 }
1660
1661 u_trace_fini(&cmd_buffer->trace);
1662 u_trace_init(&cmd_buffer->trace, &cmd_buffer->device->trace_context);
1663
1664 cmd_buffer->status = TU_CMD_BUFFER_STATUS_INITIAL;
1665
1666 return cmd_buffer->record_result;
1667 }
1668
1669 VKAPI_ATTR VkResult VKAPI_CALL
tu_AllocateCommandBuffers(VkDevice _device,const VkCommandBufferAllocateInfo * pAllocateInfo,VkCommandBuffer * pCommandBuffers)1670 tu_AllocateCommandBuffers(VkDevice _device,
1671 const VkCommandBufferAllocateInfo *pAllocateInfo,
1672 VkCommandBuffer *pCommandBuffers)
1673 {
1674 TU_FROM_HANDLE(tu_device, device, _device);
1675 TU_FROM_HANDLE(tu_cmd_pool, pool, pAllocateInfo->commandPool);
1676
1677 VkResult result = VK_SUCCESS;
1678 uint32_t i;
1679
1680 for (i = 0; i < pAllocateInfo->commandBufferCount; i++) {
1681
1682 if (!list_is_empty(&pool->free_cmd_buffers)) {
1683 struct tu_cmd_buffer *cmd_buffer = list_first_entry(
1684 &pool->free_cmd_buffers, struct tu_cmd_buffer, pool_link);
1685
1686 list_del(&cmd_buffer->pool_link);
1687 list_addtail(&cmd_buffer->pool_link, &pool->cmd_buffers);
1688
1689 result = tu_reset_cmd_buffer(cmd_buffer);
1690 vk_command_buffer_finish(&cmd_buffer->vk);
1691 VkResult init_result =
1692 vk_command_buffer_init(&cmd_buffer->vk, &pool->vk, pAllocateInfo->level);
1693 if (init_result != VK_SUCCESS)
1694 result = init_result;
1695
1696 pCommandBuffers[i] = tu_cmd_buffer_to_handle(cmd_buffer);
1697 } else {
1698 result = tu_create_cmd_buffer(device, pool, pAllocateInfo->level,
1699 &pCommandBuffers[i]);
1700 }
1701 if (result != VK_SUCCESS)
1702 break;
1703 }
1704
1705 if (result != VK_SUCCESS) {
1706 tu_FreeCommandBuffers(_device, pAllocateInfo->commandPool, i,
1707 pCommandBuffers);
1708
1709 /* From the Vulkan 1.0.66 spec:
1710 *
1711 * "vkAllocateCommandBuffers can be used to create multiple
1712 * command buffers. If the creation of any of those command
1713 * buffers fails, the implementation must destroy all
1714 * successfully created command buffer objects from this
1715 * command, set all entries of the pCommandBuffers array to
1716 * NULL and return the error."
1717 */
1718 memset(pCommandBuffers, 0,
1719 sizeof(*pCommandBuffers) * pAllocateInfo->commandBufferCount);
1720 }
1721
1722 return result;
1723 }
1724
1725 VKAPI_ATTR void VKAPI_CALL
tu_FreeCommandBuffers(VkDevice device,VkCommandPool commandPool,uint32_t commandBufferCount,const VkCommandBuffer * pCommandBuffers)1726 tu_FreeCommandBuffers(VkDevice device,
1727 VkCommandPool commandPool,
1728 uint32_t commandBufferCount,
1729 const VkCommandBuffer *pCommandBuffers)
1730 {
1731 for (uint32_t i = 0; i < commandBufferCount; i++) {
1732 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, pCommandBuffers[i]);
1733
1734 if (cmd_buffer) {
1735 if (cmd_buffer->pool) {
1736 list_del(&cmd_buffer->pool_link);
1737 list_addtail(&cmd_buffer->pool_link,
1738 &cmd_buffer->pool->free_cmd_buffers);
1739 } else
1740 tu_cmd_buffer_destroy(cmd_buffer);
1741 }
1742 }
1743 }
1744
1745 VKAPI_ATTR VkResult VKAPI_CALL
tu_ResetCommandBuffer(VkCommandBuffer commandBuffer,VkCommandBufferResetFlags flags)1746 tu_ResetCommandBuffer(VkCommandBuffer commandBuffer,
1747 VkCommandBufferResetFlags flags)
1748 {
1749 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
1750 return tu_reset_cmd_buffer(cmd_buffer);
1751 }
1752
1753 /* Initialize the cache, assuming all necessary flushes have happened but *not*
1754 * invalidations.
1755 */
1756 static void
tu_cache_init(struct tu_cache_state * cache)1757 tu_cache_init(struct tu_cache_state *cache)
1758 {
1759 cache->flush_bits = 0;
1760 cache->pending_flush_bits = TU_CMD_FLAG_ALL_INVALIDATE;
1761 }
1762
1763 /* Unlike the public entrypoint, this doesn't handle cache tracking, and
1764 * tracking the CCU state. It's used for the driver to insert its own command
1765 * buffer in the middle of a submit.
1766 */
1767 VkResult
tu_cmd_buffer_begin(struct tu_cmd_buffer * cmd_buffer,VkCommandBufferUsageFlags usage_flags)1768 tu_cmd_buffer_begin(struct tu_cmd_buffer *cmd_buffer,
1769 VkCommandBufferUsageFlags usage_flags)
1770 {
1771 VkResult result = VK_SUCCESS;
1772 if (cmd_buffer->status != TU_CMD_BUFFER_STATUS_INITIAL) {
1773 /* If the command buffer has already been resetted with
1774 * vkResetCommandBuffer, no need to do it again.
1775 */
1776 result = tu_reset_cmd_buffer(cmd_buffer);
1777 if (result != VK_SUCCESS)
1778 return result;
1779 }
1780
1781 memset(&cmd_buffer->state, 0, sizeof(cmd_buffer->state));
1782 cmd_buffer->state.index_size = 0xff; /* dirty restart index */
1783 cmd_buffer->state.line_mode = RECTANGULAR;
1784 cmd_buffer->state.gmem_layout = TU_GMEM_LAYOUT_COUNT; /* dirty value */
1785
1786 tu_cache_init(&cmd_buffer->state.cache);
1787 tu_cache_init(&cmd_buffer->state.renderpass_cache);
1788 cmd_buffer->usage_flags = usage_flags;
1789
1790 tu_cs_begin(&cmd_buffer->cs);
1791 tu_cs_begin(&cmd_buffer->draw_cs);
1792 tu_cs_begin(&cmd_buffer->draw_epilogue_cs);
1793
1794 cmd_buffer->status = TU_CMD_BUFFER_STATUS_RECORDING;
1795 return VK_SUCCESS;
1796 }
1797
1798 VKAPI_ATTR VkResult VKAPI_CALL
tu_BeginCommandBuffer(VkCommandBuffer commandBuffer,const VkCommandBufferBeginInfo * pBeginInfo)1799 tu_BeginCommandBuffer(VkCommandBuffer commandBuffer,
1800 const VkCommandBufferBeginInfo *pBeginInfo)
1801 {
1802 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
1803 VkResult result = tu_cmd_buffer_begin(cmd_buffer, pBeginInfo->flags);
1804 if (result != VK_SUCCESS)
1805 return result;
1806
1807 /* setup initial configuration into command buffer */
1808 if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
1809 switch (cmd_buffer->queue_family_index) {
1810 case TU_QUEUE_GENERAL:
1811 tu6_init_hw(cmd_buffer, &cmd_buffer->cs);
1812 break;
1813 default:
1814 break;
1815 }
1816 } else if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
1817 assert(pBeginInfo->pInheritanceInfo);
1818
1819 cmd_buffer->inherited_pipeline_statistics =
1820 pBeginInfo->pInheritanceInfo->pipelineStatistics;
1821
1822 vk_foreach_struct_const(ext, pBeginInfo->pInheritanceInfo) {
1823 switch (ext->sType) {
1824 case VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT: {
1825 const VkCommandBufferInheritanceConditionalRenderingInfoEXT *cond_rend = (void *) ext;
1826 cmd_buffer->state.predication_active = cond_rend->conditionalRenderingEnable;
1827 break;
1828 default:
1829 break;
1830 }
1831 }
1832 }
1833
1834 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1835 const VkCommandBufferInheritanceRenderingInfo *rendering_info =
1836 vk_find_struct_const(pBeginInfo->pInheritanceInfo->pNext,
1837 COMMAND_BUFFER_INHERITANCE_RENDERING_INFO);
1838
1839 if (unlikely(cmd_buffer->device->instance->debug_flags & TU_DEBUG_DYNAMIC)) {
1840 rendering_info =
1841 vk_get_command_buffer_inheritance_rendering_info(cmd_buffer->vk.level,
1842 pBeginInfo);
1843 }
1844
1845 if (rendering_info) {
1846 tu_setup_dynamic_inheritance(cmd_buffer, rendering_info);
1847 cmd_buffer->state.pass = &cmd_buffer->dynamic_pass;
1848 cmd_buffer->state.subpass = &cmd_buffer->dynamic_subpass;
1849 } else {
1850 cmd_buffer->state.pass = tu_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
1851 cmd_buffer->state.subpass =
1852 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
1853 }
1854
1855 /* We can't set the gmem layout here, because the state.pass only has
1856 * to be compatible (same formats/sample counts) with the primary's
1857 * renderpass, rather than exactly equal.
1858 */
1859
1860 tu_lrz_begin_secondary_cmdbuf(cmd_buffer);
1861 } else {
1862 /* When executing in the middle of another command buffer, the CCU
1863 * state is unknown.
1864 */
1865 cmd_buffer->state.ccu_state = TU_CMD_CCU_UNKNOWN;
1866 }
1867 }
1868
1869 return VK_SUCCESS;
1870 }
1871
1872 VKAPI_ATTR void VKAPI_CALL
tu_CmdBindVertexBuffers2EXT(VkCommandBuffer commandBuffer,uint32_t firstBinding,uint32_t bindingCount,const VkBuffer * pBuffers,const VkDeviceSize * pOffsets,const VkDeviceSize * pSizes,const VkDeviceSize * pStrides)1873 tu_CmdBindVertexBuffers2EXT(VkCommandBuffer commandBuffer,
1874 uint32_t firstBinding,
1875 uint32_t bindingCount,
1876 const VkBuffer* pBuffers,
1877 const VkDeviceSize* pOffsets,
1878 const VkDeviceSize* pSizes,
1879 const VkDeviceSize* pStrides)
1880 {
1881 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
1882 struct tu_cs cs;
1883 /* TODO: track a "max_vb" value for the cmdbuf to save a bit of memory */
1884 cmd->state.vertex_buffers.iova = tu_cs_draw_state(&cmd->sub_cs, &cs, 4 * MAX_VBS).iova;
1885
1886 for (uint32_t i = 0; i < bindingCount; i++) {
1887 if (pBuffers[i] == VK_NULL_HANDLE) {
1888 cmd->state.vb[firstBinding + i].base = 0;
1889 cmd->state.vb[firstBinding + i].size = 0;
1890 } else {
1891 struct tu_buffer *buf = tu_buffer_from_handle(pBuffers[i]);
1892 cmd->state.vb[firstBinding + i].base = buf->iova + pOffsets[i];
1893 cmd->state.vb[firstBinding + i].size = pSizes ? pSizes[i] : (buf->size - pOffsets[i]);
1894 }
1895
1896 if (pStrides)
1897 cmd->state.vb[firstBinding + i].stride = pStrides[i];
1898 }
1899
1900 for (uint32_t i = 0; i < MAX_VBS; i++) {
1901 tu_cs_emit_regs(&cs,
1902 A6XX_VFD_FETCH_BASE(i, .qword = cmd->state.vb[i].base),
1903 A6XX_VFD_FETCH_SIZE(i, cmd->state.vb[i].size));
1904 }
1905
1906 cmd->state.dirty |= TU_CMD_DIRTY_VERTEX_BUFFERS;
1907
1908 if (pStrides) {
1909 cmd->state.dynamic_state[TU_DYNAMIC_STATE_VB_STRIDE].iova =
1910 tu_cs_draw_state(&cmd->sub_cs, &cs, 2 * MAX_VBS).iova;
1911
1912 for (uint32_t i = 0; i < MAX_VBS; i++)
1913 tu_cs_emit_regs(&cs, A6XX_VFD_FETCH_STRIDE(i, cmd->state.vb[i].stride));
1914
1915 cmd->state.dirty |= TU_CMD_DIRTY_VB_STRIDE;
1916 }
1917 }
1918
1919 VKAPI_ATTR void VKAPI_CALL
tu_CmdBindIndexBuffer(VkCommandBuffer commandBuffer,VkBuffer buffer,VkDeviceSize offset,VkIndexType indexType)1920 tu_CmdBindIndexBuffer(VkCommandBuffer commandBuffer,
1921 VkBuffer buffer,
1922 VkDeviceSize offset,
1923 VkIndexType indexType)
1924 {
1925 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
1926 TU_FROM_HANDLE(tu_buffer, buf, buffer);
1927
1928
1929
1930 uint32_t index_size, index_shift, restart_index;
1931
1932 switch (indexType) {
1933 case VK_INDEX_TYPE_UINT16:
1934 index_size = INDEX4_SIZE_16_BIT;
1935 index_shift = 1;
1936 restart_index = 0xffff;
1937 break;
1938 case VK_INDEX_TYPE_UINT32:
1939 index_size = INDEX4_SIZE_32_BIT;
1940 index_shift = 2;
1941 restart_index = 0xffffffff;
1942 break;
1943 case VK_INDEX_TYPE_UINT8_EXT:
1944 index_size = INDEX4_SIZE_8_BIT;
1945 index_shift = 0;
1946 restart_index = 0xff;
1947 break;
1948 default:
1949 unreachable("invalid VkIndexType");
1950 }
1951
1952 /* initialize/update the restart index */
1953 if (cmd->state.index_size != index_size)
1954 tu_cs_emit_regs(&cmd->draw_cs, A6XX_PC_RESTART_INDEX(restart_index));
1955
1956 assert(buf->size >= offset);
1957
1958 cmd->state.index_va = buf->iova + offset;
1959 cmd->state.max_index_count = (buf->size - offset) >> index_shift;
1960 cmd->state.index_size = index_size;
1961 }
1962
1963 VKAPI_ATTR void VKAPI_CALL
tu_CmdBindDescriptorSets(VkCommandBuffer commandBuffer,VkPipelineBindPoint pipelineBindPoint,VkPipelineLayout _layout,uint32_t firstSet,uint32_t descriptorSetCount,const VkDescriptorSet * pDescriptorSets,uint32_t dynamicOffsetCount,const uint32_t * pDynamicOffsets)1964 tu_CmdBindDescriptorSets(VkCommandBuffer commandBuffer,
1965 VkPipelineBindPoint pipelineBindPoint,
1966 VkPipelineLayout _layout,
1967 uint32_t firstSet,
1968 uint32_t descriptorSetCount,
1969 const VkDescriptorSet *pDescriptorSets,
1970 uint32_t dynamicOffsetCount,
1971 const uint32_t *pDynamicOffsets)
1972 {
1973 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
1974 TU_FROM_HANDLE(tu_pipeline_layout, layout, _layout);
1975 unsigned dyn_idx = 0;
1976
1977 struct tu_descriptor_state *descriptors_state =
1978 tu_get_descriptors_state(cmd, pipelineBindPoint);
1979
1980 for (unsigned i = 0; i < descriptorSetCount; ++i) {
1981 unsigned idx = i + firstSet;
1982 TU_FROM_HANDLE(tu_descriptor_set, set, pDescriptorSets[i]);
1983
1984 descriptors_state->sets[idx] = set;
1985
1986 if (!set->layout->dynamic_offset_size)
1987 continue;
1988
1989 uint32_t *src = set->dynamic_descriptors;
1990 uint32_t *dst = descriptors_state->dynamic_descriptors +
1991 layout->set[idx].dynamic_offset_start / 4;
1992 for (unsigned j = 0; j < set->layout->binding_count; j++) {
1993 struct tu_descriptor_set_binding_layout *binding =
1994 &set->layout->binding[j];
1995 if (binding->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
1996 binding->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1997 for (unsigned k = 0; k < binding->array_size; k++, dyn_idx++) {
1998 assert(dyn_idx < dynamicOffsetCount);
1999 uint32_t offset = pDynamicOffsets[dyn_idx];
2000 memcpy(dst, src, binding->size);
2001
2002 if (binding->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2003 /* Note: we can assume here that the addition won't roll
2004 * over and change the SIZE field.
2005 */
2006 uint64_t va = src[0] | ((uint64_t)src[1] << 32);
2007 va += offset;
2008 dst[0] = va;
2009 dst[1] = va >> 32;
2010 } else {
2011 uint32_t *dst_desc = dst;
2012 for (unsigned i = 0;
2013 i < binding->size / (4 * A6XX_TEX_CONST_DWORDS);
2014 i++, dst_desc += A6XX_TEX_CONST_DWORDS) {
2015 /* Note: A6XX_TEX_CONST_5_DEPTH is always 0 */
2016 uint64_t va = dst_desc[4] | ((uint64_t)dst_desc[5] << 32);
2017 va += offset;
2018 dst_desc[4] = va;
2019 dst_desc[5] = va >> 32;
2020 }
2021 }
2022
2023 dst += binding->size / 4;
2024 src += binding->size / 4;
2025 }
2026 }
2027 }
2028 }
2029 assert(dyn_idx == dynamicOffsetCount);
2030
2031 uint32_t sp_bindless_base_reg, hlsq_bindless_base_reg, hlsq_invalidate_value;
2032 uint64_t addr[MAX_SETS + 1] = {};
2033 struct tu_cs *cs, state_cs;
2034
2035 for (uint32_t i = 0; i < MAX_SETS; i++) {
2036 struct tu_descriptor_set *set = descriptors_state->sets[i];
2037 if (set)
2038 addr[i] = set->va | 3;
2039 }
2040
2041 if (layout->dynamic_offset_size) {
2042 /* allocate and fill out dynamic descriptor set */
2043 struct tu_cs_memory dynamic_desc_set;
2044 VkResult result = tu_cs_alloc(&cmd->sub_cs,
2045 layout->dynamic_offset_size / (4 * A6XX_TEX_CONST_DWORDS),
2046 A6XX_TEX_CONST_DWORDS, &dynamic_desc_set);
2047 if (result != VK_SUCCESS) {
2048 cmd->record_result = result;
2049 return;
2050 }
2051
2052 memcpy(dynamic_desc_set.map, descriptors_state->dynamic_descriptors,
2053 layout->dynamic_offset_size);
2054 addr[MAX_SETS] = dynamic_desc_set.iova | 3;
2055 }
2056
2057 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
2058 sp_bindless_base_reg = REG_A6XX_SP_BINDLESS_BASE(0);
2059 hlsq_bindless_base_reg = REG_A6XX_HLSQ_BINDLESS_BASE(0);
2060 hlsq_invalidate_value = A6XX_HLSQ_INVALIDATE_CMD_GFX_BINDLESS(0x1f);
2061
2062 cmd->state.desc_sets = tu_cs_draw_state(&cmd->sub_cs, &state_cs, 24);
2063 cmd->state.dirty |= TU_CMD_DIRTY_DESC_SETS_LOAD;
2064 cs = &state_cs;
2065 } else {
2066 assert(pipelineBindPoint == VK_PIPELINE_BIND_POINT_COMPUTE);
2067
2068 sp_bindless_base_reg = REG_A6XX_SP_CS_BINDLESS_BASE(0);
2069 hlsq_bindless_base_reg = REG_A6XX_HLSQ_CS_BINDLESS_BASE(0);
2070 hlsq_invalidate_value = A6XX_HLSQ_INVALIDATE_CMD_CS_BINDLESS(0x1f);
2071
2072 cmd->state.dirty |= TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD;
2073 cs = &cmd->cs;
2074 }
2075
2076 tu_cs_emit_pkt4(cs, sp_bindless_base_reg, 10);
2077 tu_cs_emit_array(cs, (const uint32_t*) addr, 10);
2078 tu_cs_emit_pkt4(cs, hlsq_bindless_base_reg, 10);
2079 tu_cs_emit_array(cs, (const uint32_t*) addr, 10);
2080 tu_cs_emit_regs(cs, A6XX_HLSQ_INVALIDATE_CMD(.dword = hlsq_invalidate_value));
2081
2082 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
2083 assert(cs->cur == cs->end); /* validate draw state size */
2084 /* note: this also avoids emitting draw states before renderpass clears,
2085 * which may use the 3D clear path (for MSAA cases)
2086 */
2087 if (!(cmd->state.dirty & TU_CMD_DIRTY_DRAW_STATE)) {
2088 tu_cs_emit_pkt7(&cmd->draw_cs, CP_SET_DRAW_STATE, 3);
2089 tu_cs_emit_draw_state(&cmd->draw_cs, TU_DRAW_STATE_DESC_SETS, cmd->state.desc_sets);
2090 }
2091 }
2092 }
2093
2094 VKAPI_ATTR void VKAPI_CALL
tu_CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer,VkPipelineBindPoint pipelineBindPoint,VkPipelineLayout _layout,uint32_t _set,uint32_t descriptorWriteCount,const VkWriteDescriptorSet * pDescriptorWrites)2095 tu_CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer,
2096 VkPipelineBindPoint pipelineBindPoint,
2097 VkPipelineLayout _layout,
2098 uint32_t _set,
2099 uint32_t descriptorWriteCount,
2100 const VkWriteDescriptorSet *pDescriptorWrites)
2101 {
2102 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2103 TU_FROM_HANDLE(tu_pipeline_layout, pipe_layout, _layout);
2104 struct tu_descriptor_set_layout *layout = pipe_layout->set[_set].layout;
2105 struct tu_descriptor_set *set =
2106 &tu_get_descriptors_state(cmd, pipelineBindPoint)->push_set;
2107
2108 struct tu_cs_memory set_mem;
2109 VkResult result = tu_cs_alloc(&cmd->sub_cs,
2110 DIV_ROUND_UP(layout->size, A6XX_TEX_CONST_DWORDS * 4),
2111 A6XX_TEX_CONST_DWORDS, &set_mem);
2112 if (result != VK_SUCCESS) {
2113 cmd->record_result = result;
2114 return;
2115 }
2116
2117 /* preserve previous content if the layout is the same: */
2118 if (set->layout == layout)
2119 memcpy(set_mem.map, set->mapped_ptr, layout->size);
2120
2121 if (set->layout != layout) {
2122 if (set->layout)
2123 tu_descriptor_set_layout_unref(cmd->device, set->layout);
2124 tu_descriptor_set_layout_ref(layout);
2125 set->layout = layout;
2126 }
2127
2128 set->mapped_ptr = set_mem.map;
2129 set->va = set_mem.iova;
2130
2131 tu_update_descriptor_sets(cmd->device, tu_descriptor_set_to_handle(set),
2132 descriptorWriteCount, pDescriptorWrites, 0, NULL);
2133
2134 tu_CmdBindDescriptorSets(commandBuffer, pipelineBindPoint, _layout, _set,
2135 1, (VkDescriptorSet[]) { tu_descriptor_set_to_handle(set) },
2136 0, NULL);
2137 }
2138
2139 VKAPI_ATTR void VKAPI_CALL
tu_CmdPushDescriptorSetWithTemplateKHR(VkCommandBuffer commandBuffer,VkDescriptorUpdateTemplate descriptorUpdateTemplate,VkPipelineLayout _layout,uint32_t _set,const void * pData)2140 tu_CmdPushDescriptorSetWithTemplateKHR(VkCommandBuffer commandBuffer,
2141 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
2142 VkPipelineLayout _layout,
2143 uint32_t _set,
2144 const void* pData)
2145 {
2146 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2147 TU_FROM_HANDLE(tu_pipeline_layout, pipe_layout, _layout);
2148 TU_FROM_HANDLE(tu_descriptor_update_template, templ, descriptorUpdateTemplate);
2149 struct tu_descriptor_set_layout *layout = pipe_layout->set[_set].layout;
2150 struct tu_descriptor_set *set =
2151 &tu_get_descriptors_state(cmd, templ->bind_point)->push_set;
2152
2153 struct tu_cs_memory set_mem;
2154 VkResult result = tu_cs_alloc(&cmd->sub_cs,
2155 DIV_ROUND_UP(layout->size, A6XX_TEX_CONST_DWORDS * 4),
2156 A6XX_TEX_CONST_DWORDS, &set_mem);
2157 if (result != VK_SUCCESS) {
2158 cmd->record_result = result;
2159 return;
2160 }
2161
2162 /* preserve previous content if the layout is the same: */
2163 if (set->layout == layout)
2164 memcpy(set_mem.map, set->mapped_ptr, layout->size);
2165
2166 if (set->layout != layout) {
2167 if (set->layout)
2168 tu_descriptor_set_layout_unref(cmd->device, set->layout);
2169 tu_descriptor_set_layout_ref(layout);
2170 set->layout = layout;
2171 }
2172
2173 set->mapped_ptr = set_mem.map;
2174 set->va = set_mem.iova;
2175
2176 tu_update_descriptor_set_with_template(cmd->device, set, descriptorUpdateTemplate, pData);
2177
2178 tu_CmdBindDescriptorSets(commandBuffer, templ->bind_point, _layout, _set,
2179 1, (VkDescriptorSet[]) { tu_descriptor_set_to_handle(set) },
2180 0, NULL);
2181 }
2182
2183 VKAPI_ATTR void VKAPI_CALL
tu_CmdBindTransformFeedbackBuffersEXT(VkCommandBuffer commandBuffer,uint32_t firstBinding,uint32_t bindingCount,const VkBuffer * pBuffers,const VkDeviceSize * pOffsets,const VkDeviceSize * pSizes)2184 tu_CmdBindTransformFeedbackBuffersEXT(VkCommandBuffer commandBuffer,
2185 uint32_t firstBinding,
2186 uint32_t bindingCount,
2187 const VkBuffer *pBuffers,
2188 const VkDeviceSize *pOffsets,
2189 const VkDeviceSize *pSizes)
2190 {
2191 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2192 struct tu_cs *cs = &cmd->draw_cs;
2193
2194 /* using COND_REG_EXEC for xfb commands matches the blob behavior
2195 * presumably there isn't any benefit using a draw state when the
2196 * condition is (SYSMEM | BINNING)
2197 */
2198 tu_cond_exec_start(cs, CP_COND_REG_EXEC_0_MODE(RENDER_MODE) |
2199 CP_COND_REG_EXEC_0_SYSMEM |
2200 CP_COND_REG_EXEC_0_BINNING);
2201
2202 for (uint32_t i = 0; i < bindingCount; i++) {
2203 TU_FROM_HANDLE(tu_buffer, buf, pBuffers[i]);
2204 uint64_t iova = buf->iova + pOffsets[i];
2205 uint32_t size = buf->bo->size - (iova - buf->bo->iova);
2206 uint32_t idx = i + firstBinding;
2207
2208 if (pSizes && pSizes[i] != VK_WHOLE_SIZE)
2209 size = pSizes[i];
2210
2211 /* BUFFER_BASE is 32-byte aligned, add remaining offset to BUFFER_OFFSET */
2212 uint32_t offset = iova & 0x1f;
2213 iova &= ~(uint64_t) 0x1f;
2214
2215 tu_cs_emit_pkt4(cs, REG_A6XX_VPC_SO_BUFFER_BASE(idx), 3);
2216 tu_cs_emit_qw(cs, iova);
2217 tu_cs_emit(cs, size + offset);
2218
2219 cmd->state.streamout_offset[idx] = offset;
2220 }
2221
2222 tu_cond_exec_end(cs);
2223 }
2224
2225 VKAPI_ATTR void VKAPI_CALL
tu_CmdBeginTransformFeedbackEXT(VkCommandBuffer commandBuffer,uint32_t firstCounterBuffer,uint32_t counterBufferCount,const VkBuffer * pCounterBuffers,const VkDeviceSize * pCounterBufferOffsets)2226 tu_CmdBeginTransformFeedbackEXT(VkCommandBuffer commandBuffer,
2227 uint32_t firstCounterBuffer,
2228 uint32_t counterBufferCount,
2229 const VkBuffer *pCounterBuffers,
2230 const VkDeviceSize *pCounterBufferOffsets)
2231 {
2232 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2233 struct tu_cs *cs = &cmd->draw_cs;
2234
2235 tu_cond_exec_start(cs, CP_COND_REG_EXEC_0_MODE(RENDER_MODE) |
2236 CP_COND_REG_EXEC_0_SYSMEM |
2237 CP_COND_REG_EXEC_0_BINNING);
2238
2239 tu_cs_emit_regs(cs, A6XX_VPC_SO_DISABLE(false));
2240
2241 /* TODO: only update offset for active buffers */
2242 for (uint32_t i = 0; i < IR3_MAX_SO_BUFFERS; i++)
2243 tu_cs_emit_regs(cs, A6XX_VPC_SO_BUFFER_OFFSET(i, cmd->state.streamout_offset[i]));
2244
2245 for (uint32_t i = 0; i < (pCounterBuffers ? counterBufferCount : 0); i++) {
2246 uint32_t idx = firstCounterBuffer + i;
2247 uint32_t offset = cmd->state.streamout_offset[idx];
2248 uint64_t counter_buffer_offset = pCounterBufferOffsets ? pCounterBufferOffsets[i] : 0u;
2249
2250 if (!pCounterBuffers[i])
2251 continue;
2252
2253 TU_FROM_HANDLE(tu_buffer, buf, pCounterBuffers[i]);
2254
2255 tu_cs_emit_pkt7(cs, CP_MEM_TO_REG, 3);
2256 tu_cs_emit(cs, CP_MEM_TO_REG_0_REG(REG_A6XX_VPC_SO_BUFFER_OFFSET(idx)) |
2257 CP_MEM_TO_REG_0_UNK31 |
2258 CP_MEM_TO_REG_0_CNT(1));
2259 tu_cs_emit_qw(cs, buf->iova + counter_buffer_offset);
2260
2261 if (offset) {
2262 tu_cs_emit_pkt7(cs, CP_REG_RMW, 3);
2263 tu_cs_emit(cs, CP_REG_RMW_0_DST_REG(REG_A6XX_VPC_SO_BUFFER_OFFSET(idx)) |
2264 CP_REG_RMW_0_SRC1_ADD);
2265 tu_cs_emit(cs, 0xffffffff);
2266 tu_cs_emit(cs, offset);
2267 }
2268 }
2269
2270 tu_cond_exec_end(cs);
2271 }
2272
2273 VKAPI_ATTR void VKAPI_CALL
tu_CmdEndTransformFeedbackEXT(VkCommandBuffer commandBuffer,uint32_t firstCounterBuffer,uint32_t counterBufferCount,const VkBuffer * pCounterBuffers,const VkDeviceSize * pCounterBufferOffsets)2274 tu_CmdEndTransformFeedbackEXT(VkCommandBuffer commandBuffer,
2275 uint32_t firstCounterBuffer,
2276 uint32_t counterBufferCount,
2277 const VkBuffer *pCounterBuffers,
2278 const VkDeviceSize *pCounterBufferOffsets)
2279 {
2280 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2281 struct tu_cs *cs = &cmd->draw_cs;
2282
2283 tu_cond_exec_start(cs, CP_COND_REG_EXEC_0_MODE(RENDER_MODE) |
2284 CP_COND_REG_EXEC_0_SYSMEM |
2285 CP_COND_REG_EXEC_0_BINNING);
2286
2287 tu_cs_emit_regs(cs, A6XX_VPC_SO_DISABLE(true));
2288
2289 /* TODO: only flush buffers that need to be flushed */
2290 for (uint32_t i = 0; i < IR3_MAX_SO_BUFFERS; i++) {
2291 /* note: FLUSH_BASE is always the same, so it could go in init_hw()? */
2292 tu_cs_emit_pkt4(cs, REG_A6XX_VPC_SO_FLUSH_BASE(i), 2);
2293 tu_cs_emit_qw(cs, global_iova(cmd, flush_base[i]));
2294 tu6_emit_event_write(cmd, cs, FLUSH_SO_0 + i);
2295 }
2296
2297 for (uint32_t i = 0; i < (pCounterBuffers ? counterBufferCount : 0); i++) {
2298 uint32_t idx = firstCounterBuffer + i;
2299 uint32_t offset = cmd->state.streamout_offset[idx];
2300 uint64_t counter_buffer_offset = pCounterBufferOffsets ? pCounterBufferOffsets[i] : 0u;
2301
2302 if (!pCounterBuffers[i])
2303 continue;
2304
2305 TU_FROM_HANDLE(tu_buffer, buf, pCounterBuffers[i]);
2306
2307 /* VPC_SO_FLUSH_BASE has dwords counter, but counter should be in bytes */
2308 tu_cs_emit_pkt7(cs, CP_MEM_TO_REG, 3);
2309 tu_cs_emit(cs, CP_MEM_TO_REG_0_REG(REG_A6XX_CP_SCRATCH_REG(0)) |
2310 CP_MEM_TO_REG_0_SHIFT_BY_2 |
2311 0x40000 | /* ??? */
2312 CP_MEM_TO_REG_0_UNK31 |
2313 CP_MEM_TO_REG_0_CNT(1));
2314 tu_cs_emit_qw(cs, global_iova(cmd, flush_base[idx]));
2315
2316 if (offset) {
2317 tu_cs_emit_pkt7(cs, CP_REG_RMW, 3);
2318 tu_cs_emit(cs, CP_REG_RMW_0_DST_REG(REG_A6XX_CP_SCRATCH_REG(0)) |
2319 CP_REG_RMW_0_SRC1_ADD);
2320 tu_cs_emit(cs, 0xffffffff);
2321 tu_cs_emit(cs, -offset);
2322 }
2323
2324 tu_cs_emit_pkt7(cs, CP_REG_TO_MEM, 3);
2325 tu_cs_emit(cs, CP_REG_TO_MEM_0_REG(REG_A6XX_CP_SCRATCH_REG(0)) |
2326 CP_REG_TO_MEM_0_CNT(1));
2327 tu_cs_emit_qw(cs, buf->iova + counter_buffer_offset);
2328 }
2329
2330 tu_cond_exec_end(cs);
2331
2332 cmd->state.rp.xfb_used = true;
2333 }
2334
2335 VKAPI_ATTR void VKAPI_CALL
tu_CmdPushConstants(VkCommandBuffer commandBuffer,VkPipelineLayout layout,VkShaderStageFlags stageFlags,uint32_t offset,uint32_t size,const void * pValues)2336 tu_CmdPushConstants(VkCommandBuffer commandBuffer,
2337 VkPipelineLayout layout,
2338 VkShaderStageFlags stageFlags,
2339 uint32_t offset,
2340 uint32_t size,
2341 const void *pValues)
2342 {
2343 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2344 memcpy((void*) cmd->push_constants + offset, pValues, size);
2345 cmd->state.dirty |= TU_CMD_DIRTY_SHADER_CONSTS;
2346 }
2347
2348 /* Flush everything which has been made available but we haven't actually
2349 * flushed yet.
2350 */
2351 static void
tu_flush_all_pending(struct tu_cache_state * cache)2352 tu_flush_all_pending(struct tu_cache_state *cache)
2353 {
2354 cache->flush_bits |= cache->pending_flush_bits & TU_CMD_FLAG_ALL_FLUSH;
2355 cache->pending_flush_bits &= ~TU_CMD_FLAG_ALL_FLUSH;
2356 }
2357
2358 VKAPI_ATTR VkResult VKAPI_CALL
tu_EndCommandBuffer(VkCommandBuffer commandBuffer)2359 tu_EndCommandBuffer(VkCommandBuffer commandBuffer)
2360 {
2361 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
2362
2363 /* We currently flush CCU at the end of the command buffer, like
2364 * what the blob does. There's implicit synchronization around every
2365 * vkQueueSubmit, but the kernel only flushes the UCHE, and we don't
2366 * know yet if this command buffer will be the last in the submit so we
2367 * have to defensively flush everything else.
2368 *
2369 * TODO: We could definitely do better than this, since these flushes
2370 * aren't required by Vulkan, but we'd need kernel support to do that.
2371 * Ideally, we'd like the kernel to flush everything afterwards, so that we
2372 * wouldn't have to do any flushes here, and when submitting multiple
2373 * command buffers there wouldn't be any unnecessary flushes in between.
2374 */
2375 if (cmd_buffer->state.pass) {
2376 tu_flush_all_pending(&cmd_buffer->state.renderpass_cache);
2377 tu_emit_cache_flush_renderpass(cmd_buffer, &cmd_buffer->draw_cs);
2378 } else {
2379 tu_flush_all_pending(&cmd_buffer->state.cache);
2380 cmd_buffer->state.cache.flush_bits |=
2381 TU_CMD_FLAG_CCU_FLUSH_COLOR |
2382 TU_CMD_FLAG_CCU_FLUSH_DEPTH;
2383 tu_emit_cache_flush(cmd_buffer, &cmd_buffer->cs);
2384 }
2385
2386 tu_cs_end(&cmd_buffer->cs);
2387 tu_cs_end(&cmd_buffer->draw_cs);
2388 tu_cs_end(&cmd_buffer->draw_epilogue_cs);
2389
2390 cmd_buffer->status = TU_CMD_BUFFER_STATUS_EXECUTABLE;
2391
2392 return cmd_buffer->record_result;
2393 }
2394
2395 static struct tu_cs
tu_cmd_dynamic_state(struct tu_cmd_buffer * cmd,uint32_t id,uint32_t size)2396 tu_cmd_dynamic_state(struct tu_cmd_buffer *cmd, uint32_t id, uint32_t size)
2397 {
2398 struct tu_cs cs;
2399
2400 assert(id < ARRAY_SIZE(cmd->state.dynamic_state));
2401 cmd->state.dynamic_state[id] = tu_cs_draw_state(&cmd->sub_cs, &cs, size);
2402
2403 /* note: this also avoids emitting draw states before renderpass clears,
2404 * which may use the 3D clear path (for MSAA cases)
2405 */
2406 if (cmd->state.dirty & TU_CMD_DIRTY_DRAW_STATE)
2407 return cs;
2408
2409 tu_cs_emit_pkt7(&cmd->draw_cs, CP_SET_DRAW_STATE, 3);
2410 tu_cs_emit_draw_state(&cmd->draw_cs, TU_DRAW_STATE_DYNAMIC + id, cmd->state.dynamic_state[id]);
2411
2412 return cs;
2413 }
2414
2415 VKAPI_ATTR void VKAPI_CALL
tu_CmdBindPipeline(VkCommandBuffer commandBuffer,VkPipelineBindPoint pipelineBindPoint,VkPipeline _pipeline)2416 tu_CmdBindPipeline(VkCommandBuffer commandBuffer,
2417 VkPipelineBindPoint pipelineBindPoint,
2418 VkPipeline _pipeline)
2419 {
2420 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2421 TU_FROM_HANDLE(tu_pipeline, pipeline, _pipeline);
2422
2423 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_COMPUTE) {
2424 cmd->state.compute_pipeline = pipeline;
2425 tu_cs_emit_state_ib(&cmd->cs, pipeline->program.state);
2426 return;
2427 }
2428
2429 assert(pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS);
2430
2431 cmd->state.pipeline = pipeline;
2432 cmd->state.dirty |= TU_CMD_DIRTY_DESC_SETS_LOAD | TU_CMD_DIRTY_SHADER_CONSTS |
2433 TU_CMD_DIRTY_LRZ | TU_CMD_DIRTY_VS_PARAMS;
2434
2435 struct tu_cs *cs = &cmd->draw_cs;
2436
2437 /* note: this also avoids emitting draw states before renderpass clears,
2438 * which may use the 3D clear path (for MSAA cases)
2439 */
2440 if (!(cmd->state.dirty & TU_CMD_DIRTY_DRAW_STATE)) {
2441 uint32_t mask = ~pipeline->dynamic_state_mask & BITFIELD_MASK(TU_DYNAMIC_STATE_COUNT);
2442
2443 tu_cs_emit_pkt7(cs, CP_SET_DRAW_STATE, 3 * (8 + util_bitcount(mask)));
2444 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PROGRAM_CONFIG, pipeline->program.config_state);
2445 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PROGRAM, pipeline->program.state);
2446 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PROGRAM_BINNING, pipeline->program.binning_state);
2447 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VI, pipeline->vi.state);
2448 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VI_BINNING, pipeline->vi.binning_state);
2449 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_RAST, pipeline->rast_state);
2450 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PRIM_MODE_SYSMEM, pipeline->prim_order_state_sysmem);
2451 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PRIM_MODE_GMEM, pipeline->prim_order_state_gmem);
2452
2453 u_foreach_bit(i, mask)
2454 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DYNAMIC + i, pipeline->dynamic_state[i]);
2455 }
2456
2457 if (pipeline->active_stages & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
2458 cmd->state.rp.has_tess = true;
2459
2460 /* maximum number of patches that can fit in tess factor/param buffers */
2461 uint32_t subdraw_size = MIN2(TU_TESS_FACTOR_SIZE / ir3_tess_factor_stride(pipeline->tess.patch_type),
2462 TU_TESS_PARAM_SIZE / pipeline->tess.param_stride);
2463 /* convert from # of patches to draw count */
2464 subdraw_size *= (pipeline->ia.primtype - DI_PT_PATCHES0);
2465
2466 /* TODO: Move this packet to pipeline state, since it's constant based on the pipeline. */
2467 tu_cs_emit_pkt7(cs, CP_SET_SUBDRAW_SIZE, 1);
2468 tu_cs_emit(cs, subdraw_size);
2469 }
2470
2471 if (cmd->state.line_mode != pipeline->line_mode) {
2472 cmd->state.line_mode = pipeline->line_mode;
2473
2474 /* We have to disable MSAA when bresenham lines are used, this is
2475 * a hardware limitation and spec allows it:
2476 *
2477 * When Bresenham lines are being rasterized, sample locations may
2478 * all be treated as being at the pixel center (this may affect
2479 * attribute and depth interpolation).
2480 */
2481 if (cmd->state.subpass && cmd->state.subpass->samples) {
2482 tu6_emit_msaa(cs, cmd->state.subpass->samples, cmd->state.line_mode);
2483 }
2484 }
2485
2486 if ((pipeline->dynamic_state_mask & BIT(VK_DYNAMIC_STATE_VIEWPORT)) &&
2487 (pipeline->z_negative_one_to_one != cmd->state.z_negative_one_to_one)) {
2488 cmd->state.z_negative_one_to_one = pipeline->z_negative_one_to_one;
2489 cmd->state.dirty |= TU_CMD_DIRTY_VIEWPORTS;
2490 }
2491
2492 /* the vertex_buffers draw state always contains all the currently
2493 * bound vertex buffers. update its size to only emit the vbs which
2494 * are actually used by the pipeline
2495 * note there is a HW optimization which makes it so the draw state
2496 * is not re-executed completely when only the size changes
2497 */
2498 if (cmd->state.vertex_buffers.size != pipeline->num_vbs * 4) {
2499 cmd->state.vertex_buffers.size = pipeline->num_vbs * 4;
2500 cmd->state.dirty |= TU_CMD_DIRTY_VERTEX_BUFFERS;
2501 }
2502
2503 if ((pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_VB_STRIDE)) &&
2504 cmd->state.dynamic_state[TU_DYNAMIC_STATE_VB_STRIDE].size != pipeline->num_vbs * 2) {
2505 cmd->state.dynamic_state[TU_DYNAMIC_STATE_VB_STRIDE].size = pipeline->num_vbs * 2;
2506 cmd->state.dirty |= TU_CMD_DIRTY_VB_STRIDE;
2507 }
2508
2509 #define UPDATE_REG(X, Y) { \
2510 /* note: would be better to have pipeline bits already masked */ \
2511 uint32_t pipeline_bits = pipeline->X & pipeline->X##_mask; \
2512 if ((cmd->state.X & pipeline->X##_mask) != pipeline_bits) { \
2513 cmd->state.X &= ~pipeline->X##_mask; \
2514 cmd->state.X |= pipeline_bits; \
2515 cmd->state.dirty |= TU_CMD_DIRTY_##Y; \
2516 } \
2517 if (!(pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_##Y))) \
2518 cmd->state.dirty &= ~TU_CMD_DIRTY_##Y; \
2519 }
2520
2521 /* these registers can have bits set from both pipeline and dynamic state
2522 * this updates the bits set by the pipeline
2523 * if the pipeline doesn't use a dynamic state for the register, then
2524 * the relevant dirty bit is cleared to avoid overriding the non-dynamic
2525 * state with a dynamic state the next draw.
2526 */
2527 UPDATE_REG(gras_su_cntl, GRAS_SU_CNTL);
2528 UPDATE_REG(rb_depth_cntl, RB_DEPTH_CNTL);
2529 UPDATE_REG(rb_stencil_cntl, RB_STENCIL_CNTL);
2530 UPDATE_REG(pc_raster_cntl, RASTERIZER_DISCARD);
2531 UPDATE_REG(vpc_unknown_9107, RASTERIZER_DISCARD);
2532 UPDATE_REG(sp_blend_cntl, BLEND);
2533 UPDATE_REG(rb_blend_cntl, BLEND);
2534
2535 for (unsigned i = 0; i < pipeline->num_rts; i++) {
2536 if ((cmd->state.rb_mrt_control[i] & pipeline->rb_mrt_control_mask) !=
2537 pipeline->rb_mrt_control[i]) {
2538 cmd->state.rb_mrt_control[i] &= ~pipeline->rb_mrt_control_mask;
2539 cmd->state.rb_mrt_control[i] |= pipeline->rb_mrt_control[i];
2540 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2541 }
2542
2543 if (cmd->state.rb_mrt_blend_control[i] != pipeline->rb_mrt_blend_control[i]) {
2544 cmd->state.rb_mrt_blend_control[i] = pipeline->rb_mrt_blend_control[i];
2545 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2546 }
2547 }
2548 #undef UPDATE_REG
2549
2550 if (cmd->state.pipeline_color_write_enable != pipeline->color_write_enable) {
2551 cmd->state.pipeline_color_write_enable = pipeline->color_write_enable;
2552 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2553 }
2554 if (cmd->state.pipeline_blend_enable != pipeline->blend_enable) {
2555 cmd->state.pipeline_blend_enable = pipeline->blend_enable;
2556 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2557 }
2558 if (cmd->state.logic_op_enabled != pipeline->logic_op_enabled) {
2559 cmd->state.logic_op_enabled = pipeline->logic_op_enabled;
2560 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2561 }
2562 if (!(pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_LOGIC_OP)) &&
2563 cmd->state.rop_reads_dst != pipeline->rop_reads_dst) {
2564 cmd->state.rop_reads_dst = pipeline->rop_reads_dst;
2565 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2566 }
2567 if (cmd->state.dynamic_state[TU_DYNAMIC_STATE_BLEND].size != pipeline->num_rts * 3 + 4) {
2568 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2569 }
2570 if (!(pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_BLEND))) {
2571 cmd->state.dirty &= ~TU_CMD_DIRTY_BLEND;
2572 }
2573
2574 if (pipeline->rb_depth_cntl_disable)
2575 cmd->state.dirty |= TU_CMD_DIRTY_RB_DEPTH_CNTL;
2576 }
2577
2578 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetViewport(VkCommandBuffer commandBuffer,uint32_t firstViewport,uint32_t viewportCount,const VkViewport * pViewports)2579 tu_CmdSetViewport(VkCommandBuffer commandBuffer,
2580 uint32_t firstViewport,
2581 uint32_t viewportCount,
2582 const VkViewport *pViewports)
2583 {
2584 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2585
2586 memcpy(&cmd->state.viewport[firstViewport], pViewports, viewportCount * sizeof(*pViewports));
2587 cmd->state.max_viewport = MAX2(cmd->state.max_viewport, firstViewport + viewportCount);
2588
2589 /* With VK_EXT_depth_clip_control we have to take into account
2590 * negativeOneToOne property of the pipeline, so the viewport calculations
2591 * are deferred until it is known.
2592 */
2593 cmd->state.dirty |= TU_CMD_DIRTY_VIEWPORTS;
2594 }
2595
2596 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetScissor(VkCommandBuffer commandBuffer,uint32_t firstScissor,uint32_t scissorCount,const VkRect2D * pScissors)2597 tu_CmdSetScissor(VkCommandBuffer commandBuffer,
2598 uint32_t firstScissor,
2599 uint32_t scissorCount,
2600 const VkRect2D *pScissors)
2601 {
2602 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2603 struct tu_cs cs;
2604
2605 memcpy(&cmd->state.scissor[firstScissor], pScissors, scissorCount * sizeof(*pScissors));
2606 cmd->state.max_scissor = MAX2(cmd->state.max_scissor, firstScissor + scissorCount);
2607
2608 cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_SCISSOR, 1 + 2 * cmd->state.max_scissor);
2609 tu6_emit_scissor(&cs, cmd->state.scissor, cmd->state.max_scissor);
2610 }
2611
2612 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetLineWidth(VkCommandBuffer commandBuffer,float lineWidth)2613 tu_CmdSetLineWidth(VkCommandBuffer commandBuffer, float lineWidth)
2614 {
2615 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2616
2617 cmd->state.gras_su_cntl &= ~A6XX_GRAS_SU_CNTL_LINEHALFWIDTH__MASK;
2618 cmd->state.gras_su_cntl |= A6XX_GRAS_SU_CNTL_LINEHALFWIDTH(lineWidth / 2.0f);
2619
2620 cmd->state.dirty |= TU_CMD_DIRTY_GRAS_SU_CNTL;
2621 }
2622
2623 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthBias(VkCommandBuffer commandBuffer,float depthBiasConstantFactor,float depthBiasClamp,float depthBiasSlopeFactor)2624 tu_CmdSetDepthBias(VkCommandBuffer commandBuffer,
2625 float depthBiasConstantFactor,
2626 float depthBiasClamp,
2627 float depthBiasSlopeFactor)
2628 {
2629 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2630 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_DEPTH_BIAS, 4);
2631
2632 tu6_emit_depth_bias(&cs, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
2633 }
2634
2635 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetBlendConstants(VkCommandBuffer commandBuffer,const float blendConstants[4])2636 tu_CmdSetBlendConstants(VkCommandBuffer commandBuffer,
2637 const float blendConstants[4])
2638 {
2639 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2640 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_BLEND_CONSTANTS, 5);
2641
2642 tu_cs_emit_pkt4(&cs, REG_A6XX_RB_BLEND_RED_F32, 4);
2643 tu_cs_emit_array(&cs, (const uint32_t *) blendConstants, 4);
2644 }
2645
2646 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthBounds(VkCommandBuffer commandBuffer,float minDepthBounds,float maxDepthBounds)2647 tu_CmdSetDepthBounds(VkCommandBuffer commandBuffer,
2648 float minDepthBounds,
2649 float maxDepthBounds)
2650 {
2651 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2652 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_DEPTH_BOUNDS, 3);
2653
2654 tu_cs_emit_regs(&cs,
2655 A6XX_RB_Z_BOUNDS_MIN(minDepthBounds),
2656 A6XX_RB_Z_BOUNDS_MAX(maxDepthBounds));
2657 }
2658
2659 void
update_stencil_mask(uint32_t * value,VkStencilFaceFlags face,uint32_t mask)2660 update_stencil_mask(uint32_t *value, VkStencilFaceFlags face, uint32_t mask)
2661 {
2662 if (face & VK_STENCIL_FACE_FRONT_BIT)
2663 *value = (*value & 0xff00) | (mask & 0xff);
2664 if (face & VK_STENCIL_FACE_BACK_BIT)
2665 *value = (*value & 0xff) | (mask & 0xff) << 8;
2666 }
2667
2668 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetStencilCompareMask(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,uint32_t compareMask)2669 tu_CmdSetStencilCompareMask(VkCommandBuffer commandBuffer,
2670 VkStencilFaceFlags faceMask,
2671 uint32_t compareMask)
2672 {
2673 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2674 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK, 2);
2675
2676 update_stencil_mask(&cmd->state.dynamic_stencil_mask, faceMask, compareMask);
2677
2678 tu_cs_emit_regs(&cs, A6XX_RB_STENCILMASK(.dword = cmd->state.dynamic_stencil_mask));
2679 }
2680
2681 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetStencilWriteMask(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,uint32_t writeMask)2682 tu_CmdSetStencilWriteMask(VkCommandBuffer commandBuffer,
2683 VkStencilFaceFlags faceMask,
2684 uint32_t writeMask)
2685 {
2686 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2687 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_STENCIL_WRITE_MASK, 2);
2688
2689 update_stencil_mask(&cmd->state.dynamic_stencil_wrmask, faceMask, writeMask);
2690
2691 tu_cs_emit_regs(&cs, A6XX_RB_STENCILWRMASK(.dword = cmd->state.dynamic_stencil_wrmask));
2692
2693 cmd->state.dirty |= TU_CMD_DIRTY_LRZ;
2694 }
2695
2696 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetStencilReference(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,uint32_t reference)2697 tu_CmdSetStencilReference(VkCommandBuffer commandBuffer,
2698 VkStencilFaceFlags faceMask,
2699 uint32_t reference)
2700 {
2701 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2702 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_STENCIL_REFERENCE, 2);
2703
2704 update_stencil_mask(&cmd->state.dynamic_stencil_ref, faceMask, reference);
2705
2706 tu_cs_emit_regs(&cs, A6XX_RB_STENCILREF(.dword = cmd->state.dynamic_stencil_ref));
2707 }
2708
2709 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetSampleLocationsEXT(VkCommandBuffer commandBuffer,const VkSampleLocationsInfoEXT * pSampleLocationsInfo)2710 tu_CmdSetSampleLocationsEXT(VkCommandBuffer commandBuffer,
2711 const VkSampleLocationsInfoEXT* pSampleLocationsInfo)
2712 {
2713 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2714 struct tu_cs cs = tu_cmd_dynamic_state(cmd, TU_DYNAMIC_STATE_SAMPLE_LOCATIONS, 9);
2715
2716 assert(pSampleLocationsInfo);
2717
2718 tu6_emit_sample_locations(&cs, pSampleLocationsInfo);
2719 }
2720
2721 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetCullModeEXT(VkCommandBuffer commandBuffer,VkCullModeFlags cullMode)2722 tu_CmdSetCullModeEXT(VkCommandBuffer commandBuffer, VkCullModeFlags cullMode)
2723 {
2724 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2725
2726 cmd->state.gras_su_cntl &=
2727 ~(A6XX_GRAS_SU_CNTL_CULL_FRONT | A6XX_GRAS_SU_CNTL_CULL_BACK);
2728
2729 if (cullMode & VK_CULL_MODE_FRONT_BIT)
2730 cmd->state.gras_su_cntl |= A6XX_GRAS_SU_CNTL_CULL_FRONT;
2731 if (cullMode & VK_CULL_MODE_BACK_BIT)
2732 cmd->state.gras_su_cntl |= A6XX_GRAS_SU_CNTL_CULL_BACK;
2733
2734 cmd->state.dirty |= TU_CMD_DIRTY_GRAS_SU_CNTL;
2735 }
2736
2737 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetFrontFaceEXT(VkCommandBuffer commandBuffer,VkFrontFace frontFace)2738 tu_CmdSetFrontFaceEXT(VkCommandBuffer commandBuffer, VkFrontFace frontFace)
2739 {
2740 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2741
2742 cmd->state.gras_su_cntl &= ~A6XX_GRAS_SU_CNTL_FRONT_CW;
2743
2744 if (frontFace == VK_FRONT_FACE_CLOCKWISE)
2745 cmd->state.gras_su_cntl |= A6XX_GRAS_SU_CNTL_FRONT_CW;
2746
2747 cmd->state.dirty |= TU_CMD_DIRTY_GRAS_SU_CNTL;
2748 }
2749
2750 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetPrimitiveTopologyEXT(VkCommandBuffer commandBuffer,VkPrimitiveTopology primitiveTopology)2751 tu_CmdSetPrimitiveTopologyEXT(VkCommandBuffer commandBuffer,
2752 VkPrimitiveTopology primitiveTopology)
2753 {
2754 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2755
2756 cmd->state.primtype = tu6_primtype(primitiveTopology);
2757 }
2758
2759 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetViewportWithCountEXT(VkCommandBuffer commandBuffer,uint32_t viewportCount,const VkViewport * pViewports)2760 tu_CmdSetViewportWithCountEXT(VkCommandBuffer commandBuffer,
2761 uint32_t viewportCount,
2762 const VkViewport* pViewports)
2763 {
2764 tu_CmdSetViewport(commandBuffer, 0, viewportCount, pViewports);
2765 }
2766
2767 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetScissorWithCountEXT(VkCommandBuffer commandBuffer,uint32_t scissorCount,const VkRect2D * pScissors)2768 tu_CmdSetScissorWithCountEXT(VkCommandBuffer commandBuffer,
2769 uint32_t scissorCount,
2770 const VkRect2D* pScissors)
2771 {
2772 tu_CmdSetScissor(commandBuffer, 0, scissorCount, pScissors);
2773 }
2774
2775 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthTestEnableEXT(VkCommandBuffer commandBuffer,VkBool32 depthTestEnable)2776 tu_CmdSetDepthTestEnableEXT(VkCommandBuffer commandBuffer,
2777 VkBool32 depthTestEnable)
2778 {
2779 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2780
2781 cmd->state.rb_depth_cntl &= ~A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE;
2782
2783 if (depthTestEnable)
2784 cmd->state.rb_depth_cntl |= A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE;
2785
2786 cmd->state.dirty |= TU_CMD_DIRTY_RB_DEPTH_CNTL;
2787 }
2788
2789 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthWriteEnableEXT(VkCommandBuffer commandBuffer,VkBool32 depthWriteEnable)2790 tu_CmdSetDepthWriteEnableEXT(VkCommandBuffer commandBuffer,
2791 VkBool32 depthWriteEnable)
2792 {
2793 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2794
2795 cmd->state.rb_depth_cntl &= ~A6XX_RB_DEPTH_CNTL_Z_WRITE_ENABLE;
2796
2797 if (depthWriteEnable)
2798 cmd->state.rb_depth_cntl |= A6XX_RB_DEPTH_CNTL_Z_WRITE_ENABLE;
2799
2800 cmd->state.dirty |= TU_CMD_DIRTY_RB_DEPTH_CNTL;
2801 }
2802
2803 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthCompareOpEXT(VkCommandBuffer commandBuffer,VkCompareOp depthCompareOp)2804 tu_CmdSetDepthCompareOpEXT(VkCommandBuffer commandBuffer,
2805 VkCompareOp depthCompareOp)
2806 {
2807 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2808
2809 cmd->state.rb_depth_cntl &= ~A6XX_RB_DEPTH_CNTL_ZFUNC__MASK;
2810
2811 cmd->state.rb_depth_cntl |=
2812 A6XX_RB_DEPTH_CNTL_ZFUNC(tu6_compare_func(depthCompareOp));
2813
2814 cmd->state.dirty |= TU_CMD_DIRTY_RB_DEPTH_CNTL;
2815 }
2816
2817 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthBoundsTestEnableEXT(VkCommandBuffer commandBuffer,VkBool32 depthBoundsTestEnable)2818 tu_CmdSetDepthBoundsTestEnableEXT(VkCommandBuffer commandBuffer,
2819 VkBool32 depthBoundsTestEnable)
2820 {
2821 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2822
2823 cmd->state.rb_depth_cntl &= ~A6XX_RB_DEPTH_CNTL_Z_BOUNDS_ENABLE;
2824
2825 if (depthBoundsTestEnable)
2826 cmd->state.rb_depth_cntl |= A6XX_RB_DEPTH_CNTL_Z_BOUNDS_ENABLE;
2827
2828 cmd->state.dirty |= TU_CMD_DIRTY_RB_DEPTH_CNTL;
2829 }
2830
2831 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetStencilTestEnableEXT(VkCommandBuffer commandBuffer,VkBool32 stencilTestEnable)2832 tu_CmdSetStencilTestEnableEXT(VkCommandBuffer commandBuffer,
2833 VkBool32 stencilTestEnable)
2834 {
2835 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2836
2837 cmd->state.rb_stencil_cntl &= ~(
2838 A6XX_RB_STENCIL_CONTROL_STENCIL_ENABLE |
2839 A6XX_RB_STENCIL_CONTROL_STENCIL_ENABLE_BF |
2840 A6XX_RB_STENCIL_CONTROL_STENCIL_READ);
2841
2842 if (stencilTestEnable) {
2843 cmd->state.rb_stencil_cntl |=
2844 A6XX_RB_STENCIL_CONTROL_STENCIL_ENABLE |
2845 A6XX_RB_STENCIL_CONTROL_STENCIL_ENABLE_BF |
2846 A6XX_RB_STENCIL_CONTROL_STENCIL_READ;
2847 }
2848
2849 cmd->state.dirty |= TU_CMD_DIRTY_RB_STENCIL_CNTL;
2850 }
2851
2852 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetStencilOpEXT(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,VkStencilOp failOp,VkStencilOp passOp,VkStencilOp depthFailOp,VkCompareOp compareOp)2853 tu_CmdSetStencilOpEXT(VkCommandBuffer commandBuffer,
2854 VkStencilFaceFlags faceMask,
2855 VkStencilOp failOp,
2856 VkStencilOp passOp,
2857 VkStencilOp depthFailOp,
2858 VkCompareOp compareOp)
2859 {
2860 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2861
2862 if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
2863 cmd->state.rb_stencil_cntl &= ~(
2864 A6XX_RB_STENCIL_CONTROL_FUNC__MASK |
2865 A6XX_RB_STENCIL_CONTROL_FAIL__MASK |
2866 A6XX_RB_STENCIL_CONTROL_ZPASS__MASK |
2867 A6XX_RB_STENCIL_CONTROL_ZFAIL__MASK);
2868
2869 cmd->state.rb_stencil_cntl |=
2870 A6XX_RB_STENCIL_CONTROL_FUNC(tu6_compare_func(compareOp)) |
2871 A6XX_RB_STENCIL_CONTROL_FAIL(tu6_stencil_op(failOp)) |
2872 A6XX_RB_STENCIL_CONTROL_ZPASS(tu6_stencil_op(passOp)) |
2873 A6XX_RB_STENCIL_CONTROL_ZFAIL(tu6_stencil_op(depthFailOp));
2874 }
2875
2876 if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
2877 cmd->state.rb_stencil_cntl &= ~(
2878 A6XX_RB_STENCIL_CONTROL_FUNC_BF__MASK |
2879 A6XX_RB_STENCIL_CONTROL_FAIL_BF__MASK |
2880 A6XX_RB_STENCIL_CONTROL_ZPASS_BF__MASK |
2881 A6XX_RB_STENCIL_CONTROL_ZFAIL_BF__MASK);
2882
2883 cmd->state.rb_stencil_cntl |=
2884 A6XX_RB_STENCIL_CONTROL_FUNC_BF(tu6_compare_func(compareOp)) |
2885 A6XX_RB_STENCIL_CONTROL_FAIL_BF(tu6_stencil_op(failOp)) |
2886 A6XX_RB_STENCIL_CONTROL_ZPASS_BF(tu6_stencil_op(passOp)) |
2887 A6XX_RB_STENCIL_CONTROL_ZFAIL_BF(tu6_stencil_op(depthFailOp));
2888 }
2889
2890 cmd->state.dirty |= TU_CMD_DIRTY_RB_STENCIL_CNTL;
2891 }
2892
2893 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDepthBiasEnableEXT(VkCommandBuffer commandBuffer,VkBool32 depthBiasEnable)2894 tu_CmdSetDepthBiasEnableEXT(VkCommandBuffer commandBuffer,
2895 VkBool32 depthBiasEnable)
2896 {
2897 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2898
2899 cmd->state.gras_su_cntl &= ~A6XX_GRAS_SU_CNTL_POLY_OFFSET;
2900 if (depthBiasEnable)
2901 cmd->state.gras_su_cntl |= A6XX_GRAS_SU_CNTL_POLY_OFFSET;
2902
2903 cmd->state.dirty |= TU_CMD_DIRTY_GRAS_SU_CNTL;
2904 }
2905
2906 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetPrimitiveRestartEnableEXT(VkCommandBuffer commandBuffer,VkBool32 primitiveRestartEnable)2907 tu_CmdSetPrimitiveRestartEnableEXT(VkCommandBuffer commandBuffer,
2908 VkBool32 primitiveRestartEnable)
2909 {
2910 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2911
2912 cmd->state.primitive_restart_enable = primitiveRestartEnable;
2913 }
2914
2915 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetRasterizerDiscardEnableEXT(VkCommandBuffer commandBuffer,VkBool32 rasterizerDiscardEnable)2916 tu_CmdSetRasterizerDiscardEnableEXT(VkCommandBuffer commandBuffer,
2917 VkBool32 rasterizerDiscardEnable)
2918 {
2919 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2920
2921 cmd->state.pc_raster_cntl &= ~A6XX_PC_RASTER_CNTL_DISCARD;
2922 cmd->state.vpc_unknown_9107 &= ~A6XX_VPC_UNKNOWN_9107_RASTER_DISCARD;
2923 if (rasterizerDiscardEnable) {
2924 cmd->state.pc_raster_cntl |= A6XX_PC_RASTER_CNTL_DISCARD;
2925 cmd->state.vpc_unknown_9107 |= A6XX_VPC_UNKNOWN_9107_RASTER_DISCARD;
2926 }
2927
2928 cmd->state.dirty |= TU_CMD_DIRTY_RASTERIZER_DISCARD;
2929 }
2930
2931 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetLogicOpEXT(VkCommandBuffer commandBuffer,VkLogicOp logicOp)2932 tu_CmdSetLogicOpEXT(VkCommandBuffer commandBuffer,
2933 VkLogicOp logicOp)
2934 {
2935 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2936
2937 cmd->state.rb_mrt_control_rop =
2938 tu6_rb_mrt_control_rop(logicOp, &cmd->state.rop_reads_dst);
2939
2940 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2941 }
2942
2943 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetPatchControlPointsEXT(VkCommandBuffer commandBuffer,uint32_t patchControlPoints)2944 tu_CmdSetPatchControlPointsEXT(VkCommandBuffer commandBuffer,
2945 uint32_t patchControlPoints)
2946 {
2947 tu_stub();
2948 }
2949
2950 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetLineStippleEXT(VkCommandBuffer commandBuffer,uint32_t lineStippleFactor,uint16_t lineStipplePattern)2951 tu_CmdSetLineStippleEXT(VkCommandBuffer commandBuffer,
2952 uint32_t lineStippleFactor,
2953 uint16_t lineStipplePattern)
2954 {
2955 tu_stub();
2956 }
2957
2958 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetColorWriteEnableEXT(VkCommandBuffer commandBuffer,uint32_t attachmentCount,const VkBool32 * pColorWriteEnables)2959 tu_CmdSetColorWriteEnableEXT(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
2960 const VkBool32 *pColorWriteEnables)
2961 {
2962 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
2963 uint32_t color_write_enable = 0;
2964
2965 for (unsigned i = 0; i < attachmentCount; i++) {
2966 if (pColorWriteEnables[i])
2967 color_write_enable |= BIT(i);
2968 }
2969
2970 cmd->state.color_write_enable = color_write_enable;
2971 cmd->state.dirty |= TU_CMD_DIRTY_BLEND;
2972 }
2973
2974 static void
tu_flush_for_access(struct tu_cache_state * cache,enum tu_cmd_access_mask src_mask,enum tu_cmd_access_mask dst_mask)2975 tu_flush_for_access(struct tu_cache_state *cache,
2976 enum tu_cmd_access_mask src_mask,
2977 enum tu_cmd_access_mask dst_mask)
2978 {
2979 enum tu_cmd_flush_bits flush_bits = 0;
2980
2981 if (src_mask & TU_ACCESS_SYSMEM_WRITE) {
2982 cache->pending_flush_bits |= TU_CMD_FLAG_ALL_INVALIDATE;
2983 }
2984
2985 if (src_mask & TU_ACCESS_CP_WRITE) {
2986 /* Flush the CP write queue.
2987 */
2988 cache->pending_flush_bits |=
2989 TU_CMD_FLAG_WAIT_MEM_WRITES |
2990 TU_CMD_FLAG_ALL_INVALIDATE;
2991 }
2992
2993 #define SRC_FLUSH(domain, flush, invalidate) \
2994 if (src_mask & TU_ACCESS_##domain##_WRITE) { \
2995 cache->pending_flush_bits |= TU_CMD_FLAG_##flush | \
2996 (TU_CMD_FLAG_ALL_INVALIDATE & ~TU_CMD_FLAG_##invalidate); \
2997 }
2998
2999 SRC_FLUSH(UCHE, CACHE_FLUSH, CACHE_INVALIDATE)
3000 SRC_FLUSH(CCU_COLOR, CCU_FLUSH_COLOR, CCU_INVALIDATE_COLOR)
3001 SRC_FLUSH(CCU_DEPTH, CCU_FLUSH_DEPTH, CCU_INVALIDATE_DEPTH)
3002
3003 #undef SRC_FLUSH
3004
3005 #define SRC_INCOHERENT_FLUSH(domain, flush, invalidate) \
3006 if (src_mask & TU_ACCESS_##domain##_INCOHERENT_WRITE) { \
3007 flush_bits |= TU_CMD_FLAG_##flush; \
3008 cache->pending_flush_bits |= \
3009 (TU_CMD_FLAG_ALL_INVALIDATE & ~TU_CMD_FLAG_##invalidate); \
3010 }
3011
3012 SRC_INCOHERENT_FLUSH(CCU_COLOR, CCU_FLUSH_COLOR, CCU_INVALIDATE_COLOR)
3013 SRC_INCOHERENT_FLUSH(CCU_DEPTH, CCU_FLUSH_DEPTH, CCU_INVALIDATE_DEPTH)
3014
3015 #undef SRC_INCOHERENT_FLUSH
3016
3017 /* Treat host & sysmem write accesses the same, since the kernel implicitly
3018 * drains the queue before signalling completion to the host.
3019 */
3020 if (dst_mask & (TU_ACCESS_SYSMEM_READ | TU_ACCESS_SYSMEM_WRITE)) {
3021 flush_bits |= cache->pending_flush_bits & TU_CMD_FLAG_ALL_FLUSH;
3022 }
3023
3024 #define DST_FLUSH(domain, flush, invalidate) \
3025 if (dst_mask & (TU_ACCESS_##domain##_READ | \
3026 TU_ACCESS_##domain##_WRITE)) { \
3027 flush_bits |= cache->pending_flush_bits & \
3028 (TU_CMD_FLAG_##invalidate | \
3029 (TU_CMD_FLAG_ALL_FLUSH & ~TU_CMD_FLAG_##flush)); \
3030 }
3031
3032 DST_FLUSH(UCHE, CACHE_FLUSH, CACHE_INVALIDATE)
3033 DST_FLUSH(CCU_COLOR, CCU_FLUSH_COLOR, CCU_INVALIDATE_COLOR)
3034 DST_FLUSH(CCU_DEPTH, CCU_FLUSH_DEPTH, CCU_INVALIDATE_DEPTH)
3035
3036 #undef DST_FLUSH
3037
3038 #define DST_INCOHERENT_FLUSH(domain, flush, invalidate) \
3039 if (dst_mask & (TU_ACCESS_##domain##_INCOHERENT_READ | \
3040 TU_ACCESS_##domain##_INCOHERENT_WRITE)) { \
3041 flush_bits |= TU_CMD_FLAG_##invalidate | \
3042 (cache->pending_flush_bits & \
3043 (TU_CMD_FLAG_ALL_FLUSH & ~TU_CMD_FLAG_##flush)); \
3044 }
3045
3046 DST_INCOHERENT_FLUSH(CCU_COLOR, CCU_FLUSH_COLOR, CCU_INVALIDATE_COLOR)
3047 DST_INCOHERENT_FLUSH(CCU_DEPTH, CCU_FLUSH_DEPTH, CCU_INVALIDATE_DEPTH)
3048
3049 #undef DST_INCOHERENT_FLUSH
3050
3051 cache->flush_bits |= flush_bits;
3052 cache->pending_flush_bits &= ~flush_bits;
3053 }
3054
3055 /* When translating Vulkan access flags to which cache is accessed
3056 * (CCU/UCHE/sysmem), we should take into account both the access flags and
3057 * the stage so that accesses with MEMORY_READ_BIT/MEMORY_WRITE_BIT + a
3058 * specific stage return something sensible. The specification for
3059 * VK_KHR_synchronization2 says that we should do this:
3060 *
3061 * Additionally, scoping the pipeline stages into the barrier structs
3062 * allows the use of the MEMORY_READ and MEMORY_WRITE flags without
3063 * sacrificing precision. The per-stage access flags should be used to
3064 * disambiguate specific accesses in a given stage or set of stages - for
3065 * instance, between uniform reads and sampling operations.
3066 *
3067 * Note that while in all known cases the stage is actually enough, we should
3068 * still narrow things down based on the access flags to handle "old-style"
3069 * barriers that may specify a wider range of stages but more precise access
3070 * flags. These helpers allow us to do both.
3071 */
3072
3073 static bool
filter_read_access(VkAccessFlags2 flags,VkPipelineStageFlags2 stages,VkAccessFlags2 tu_flags,VkPipelineStageFlags2 tu_stages)3074 filter_read_access(VkAccessFlags2 flags, VkPipelineStageFlags2 stages,
3075 VkAccessFlags2 tu_flags, VkPipelineStageFlags2 tu_stages)
3076 {
3077 return (flags & (tu_flags | VK_ACCESS_2_MEMORY_READ_BIT)) &&
3078 (stages & (tu_stages | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT));
3079 }
3080
3081 static bool
filter_write_access(VkAccessFlags2 flags,VkPipelineStageFlags2 stages,VkAccessFlags2 tu_flags,VkPipelineStageFlags2 tu_stages)3082 filter_write_access(VkAccessFlags2 flags, VkPipelineStageFlags2 stages,
3083 VkAccessFlags2 tu_flags, VkPipelineStageFlags2 tu_stages)
3084 {
3085 return (flags & (tu_flags | VK_ACCESS_2_MEMORY_WRITE_BIT)) &&
3086 (stages & (tu_stages | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT));
3087 }
3088
3089 static bool
gfx_read_access(VkAccessFlags2 flags,VkPipelineStageFlags2 stages,VkAccessFlags2 tu_flags,VkPipelineStageFlags2 tu_stages)3090 gfx_read_access(VkAccessFlags2 flags, VkPipelineStageFlags2 stages,
3091 VkAccessFlags2 tu_flags, VkPipelineStageFlags2 tu_stages)
3092 {
3093 return filter_read_access(flags, stages, tu_flags,
3094 tu_stages | VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT);
3095 }
3096
3097 static bool
gfx_write_access(VkAccessFlags2 flags,VkPipelineStageFlags2 stages,VkAccessFlags2 tu_flags,VkPipelineStageFlags2 tu_stages)3098 gfx_write_access(VkAccessFlags2 flags, VkPipelineStageFlags2 stages,
3099 VkAccessFlags2 tu_flags, VkPipelineStageFlags2 tu_stages)
3100 {
3101 return filter_write_access(flags, stages, tu_flags,
3102 tu_stages | VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT);
3103 }
3104 static enum tu_cmd_access_mask
vk2tu_access(VkAccessFlags2 flags,VkPipelineStageFlags2 stages,bool image_only,bool gmem)3105 vk2tu_access(VkAccessFlags2 flags, VkPipelineStageFlags2 stages, bool image_only, bool gmem)
3106 {
3107 enum tu_cmd_access_mask mask = 0;
3108
3109 if (gfx_read_access(flags, stages,
3110 VK_ACCESS_2_INDIRECT_COMMAND_READ_BIT |
3111 VK_ACCESS_2_CONDITIONAL_RENDERING_READ_BIT_EXT |
3112 VK_ACCESS_2_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT |
3113 VK_ACCESS_2_HOST_READ_BIT,
3114 VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT |
3115 VK_PIPELINE_STAGE_2_CONDITIONAL_RENDERING_BIT_EXT |
3116 VK_PIPELINE_STAGE_2_TRANSFORM_FEEDBACK_BIT_EXT |
3117 VK_PIPELINE_STAGE_2_HOST_BIT))
3118 mask |= TU_ACCESS_SYSMEM_READ;
3119
3120 if (gfx_write_access(flags, stages,
3121 VK_ACCESS_2_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT,
3122 VK_PIPELINE_STAGE_2_TRANSFORM_FEEDBACK_BIT_EXT))
3123 mask |= TU_ACCESS_CP_WRITE;
3124
3125 if (gfx_write_access(flags, stages,
3126 VK_ACCESS_2_HOST_WRITE_BIT,
3127 VK_PIPELINE_STAGE_2_HOST_BIT))
3128 mask |= TU_ACCESS_SYSMEM_WRITE;
3129
3130 #define SHADER_STAGES \
3131 (VK_PIPELINE_STAGE_2_VERTEX_SHADER_BIT | \
3132 VK_PIPELINE_STAGE_2_TESSELLATION_CONTROL_SHADER_BIT | \
3133 VK_PIPELINE_STAGE_2_TESSELLATION_EVALUATION_SHADER_BIT | \
3134 VK_PIPELINE_STAGE_2_GEOMETRY_SHADER_BIT | \
3135 VK_PIPELINE_STAGE_2_PRE_RASTERIZATION_SHADERS_BIT | \
3136 VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | \
3137 VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT)
3138
3139
3140 if (gfx_read_access(flags, stages,
3141 VK_ACCESS_2_INDEX_READ_BIT |
3142 VK_ACCESS_2_VERTEX_ATTRIBUTE_READ_BIT |
3143 VK_ACCESS_2_UNIFORM_READ_BIT |
3144 VK_ACCESS_2_INPUT_ATTACHMENT_READ_BIT |
3145 VK_ACCESS_2_SHADER_READ_BIT,
3146 VK_PIPELINE_STAGE_2_INDEX_INPUT_BIT |
3147 VK_PIPELINE_STAGE_2_VERTEX_INPUT_BIT |
3148 VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT |
3149 SHADER_STAGES))
3150 mask |= TU_ACCESS_UCHE_READ;
3151
3152 if (gfx_write_access(flags, stages,
3153 VK_ACCESS_2_SHADER_WRITE_BIT |
3154 VK_ACCESS_2_TRANSFORM_FEEDBACK_WRITE_BIT_EXT,
3155 VK_PIPELINE_STAGE_2_TRANSFORM_FEEDBACK_BIT_EXT |
3156 SHADER_STAGES))
3157 mask |= TU_ACCESS_UCHE_WRITE;
3158
3159 /* When using GMEM, the CCU is always flushed automatically to GMEM, and
3160 * then GMEM is flushed to sysmem. Furthermore, we already had to flush any
3161 * previous writes in sysmem mode when transitioning to GMEM. Therefore we
3162 * can ignore CCU and pretend that color attachments and transfers use
3163 * sysmem directly.
3164 */
3165
3166 if (gfx_read_access(flags, stages,
3167 VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT |
3168 VK_ACCESS_2_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT,
3169 VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT)) {
3170 if (gmem)
3171 mask |= TU_ACCESS_SYSMEM_READ;
3172 else
3173 mask |= TU_ACCESS_CCU_COLOR_INCOHERENT_READ;
3174 }
3175
3176 if (gfx_read_access(flags, stages,
3177 VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
3178 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT |
3179 VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT)) {
3180 if (gmem)
3181 mask |= TU_ACCESS_SYSMEM_READ;
3182 else
3183 mask |= TU_ACCESS_CCU_DEPTH_INCOHERENT_READ;
3184 }
3185
3186 if (gfx_write_access(flags, stages,
3187 VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
3188 VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT)) {
3189 if (gmem) {
3190 mask |= TU_ACCESS_SYSMEM_WRITE;
3191 } else {
3192 mask |= TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE;
3193 }
3194 }
3195
3196 if (gfx_write_access(flags, stages,
3197 VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
3198 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT |
3199 VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT)) {
3200 if (gmem) {
3201 mask |= TU_ACCESS_SYSMEM_WRITE;
3202 } else {
3203 mask |= TU_ACCESS_CCU_DEPTH_INCOHERENT_WRITE;
3204 }
3205 }
3206
3207 if (filter_write_access(flags, stages,
3208 VK_ACCESS_2_TRANSFER_WRITE_BIT,
3209 VK_PIPELINE_STAGE_2_COPY_BIT |
3210 VK_PIPELINE_STAGE_2_BLIT_BIT |
3211 VK_PIPELINE_STAGE_2_CLEAR_BIT |
3212 VK_PIPELINE_STAGE_2_RESOLVE_BIT |
3213 VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT)) {
3214 if (gmem) {
3215 mask |= TU_ACCESS_SYSMEM_WRITE;
3216 } else if (image_only) {
3217 /* Because we always split up blits/copies of images involving
3218 * multiple layers, we always access each layer in the same way, with
3219 * the same base address, same format, etc. This means we can avoid
3220 * flushing between multiple writes to the same image. This elides
3221 * flushes between e.g. multiple blits to the same image.
3222 */
3223 mask |= TU_ACCESS_CCU_COLOR_WRITE;
3224 } else {
3225 mask |= TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE;
3226 }
3227 }
3228
3229 if (filter_read_access(flags, stages,
3230 VK_ACCESS_2_TRANSFER_READ_BIT,
3231 VK_PIPELINE_STAGE_2_COPY_BIT |
3232 VK_PIPELINE_STAGE_2_BLIT_BIT |
3233 VK_PIPELINE_STAGE_2_RESOLVE_BIT |
3234 VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT)) {
3235 mask |= TU_ACCESS_UCHE_READ;
3236 }
3237
3238 return mask;
3239 }
3240
3241 /* These helpers deal with legacy BOTTOM_OF_PIPE/TOP_OF_PIPE stages.
3242 */
3243
3244 static VkPipelineStageFlags2
sanitize_src_stage(VkPipelineStageFlags2 stage_mask)3245 sanitize_src_stage(VkPipelineStageFlags2 stage_mask)
3246 {
3247 /* From the Vulkan spec:
3248 *
3249 * VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT is ... equivalent to
3250 * VK_PIPELINE_STAGE_2_NONE in the first scope.
3251 *
3252 * VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT is equivalent to
3253 * VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT with VkAccessFlags2 set to 0
3254 * when specified in the first synchronization scope, ...
3255 */
3256 if (stage_mask & VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT)
3257 return VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
3258
3259 return stage_mask & ~VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT;
3260 }
3261
3262 static VkPipelineStageFlags2
sanitize_dst_stage(VkPipelineStageFlags2 stage_mask)3263 sanitize_dst_stage(VkPipelineStageFlags2 stage_mask)
3264 {
3265 /* From the Vulkan spec:
3266 *
3267 * VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT is equivalent to
3268 * VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT with VkAccessFlags2 set to 0
3269 * when specified in the second synchronization scope, ...
3270 *
3271 * VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT is ... equivalent to
3272 * VK_PIPELINE_STAGE_2_NONE in the second scope.
3273 *
3274 */
3275 if (stage_mask & VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT)
3276 return VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
3277
3278 return stage_mask & ~VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT;
3279 }
3280
3281 static enum tu_stage
vk2tu_single_stage(VkPipelineStageFlags2 vk_stage,bool dst)3282 vk2tu_single_stage(VkPipelineStageFlags2 vk_stage, bool dst)
3283 {
3284 if (vk_stage == VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT ||
3285 vk_stage == VK_PIPELINE_STAGE_2_CONDITIONAL_RENDERING_BIT_EXT)
3286 return TU_STAGE_CP;
3287
3288 if (vk_stage == VK_PIPELINE_STAGE_2_VERTEX_INPUT_BIT ||
3289 vk_stage == VK_PIPELINE_STAGE_2_INDEX_INPUT_BIT ||
3290 vk_stage == VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT)
3291 return TU_STAGE_FE;
3292
3293 if (vk_stage == VK_PIPELINE_STAGE_2_VERTEX_SHADER_BIT ||
3294 vk_stage == VK_PIPELINE_STAGE_2_TESSELLATION_CONTROL_SHADER_BIT ||
3295 vk_stage == VK_PIPELINE_STAGE_2_TESSELLATION_EVALUATION_SHADER_BIT ||
3296 vk_stage == VK_PIPELINE_STAGE_2_GEOMETRY_SHADER_BIT ||
3297 vk_stage == VK_PIPELINE_STAGE_2_PRE_RASTERIZATION_SHADERS_BIT)
3298 return TU_STAGE_SP_VS;
3299
3300 if (vk_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT ||
3301 vk_stage == VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT)
3302 return TU_STAGE_SP_PS;
3303
3304 if (vk_stage == VK_PIPELINE_STAGE_2_TRANSFORM_FEEDBACK_BIT_EXT || /* Yes, really */
3305 /* See comment in TU_STAGE_GRAS about early fragment tests */
3306 vk_stage == VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT ||
3307 vk_stage == VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT ||
3308 vk_stage == VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT)
3309
3310 return TU_STAGE_PS;
3311
3312 if (vk_stage == VK_PIPELINE_STAGE_2_COPY_BIT ||
3313 vk_stage == VK_PIPELINE_STAGE_2_BLIT_BIT ||
3314 vk_stage == VK_PIPELINE_STAGE_2_RESOLVE_BIT ||
3315 vk_stage == VK_PIPELINE_STAGE_2_CLEAR_BIT ||
3316 vk_stage == VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT)
3317 /* Blits read in SP_PS and write in PS, in both 2d and 3d cases */
3318 return dst ? TU_STAGE_SP_PS : TU_STAGE_PS;
3319
3320 if (vk_stage == VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT ||
3321 vk_stage == VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT)
3322 /* Be conservative */
3323 return dst ? TU_STAGE_CP : TU_STAGE_PS;
3324
3325 if (vk_stage == VK_PIPELINE_STAGE_2_HOST_BIT)
3326 return dst ? TU_STAGE_PS : TU_STAGE_CP;
3327
3328 unreachable("unknown pipeline stage");
3329 }
3330
3331 static enum tu_stage
vk2tu_src_stage(VkPipelineStageFlags vk_stages)3332 vk2tu_src_stage(VkPipelineStageFlags vk_stages)
3333 {
3334 enum tu_stage stage = TU_STAGE_CP;
3335 u_foreach_bit (bit, vk_stages) {
3336 enum tu_stage new_stage = vk2tu_single_stage(1ull << bit, false);
3337 stage = MAX2(stage, new_stage);
3338 }
3339
3340 return stage;
3341 }
3342
3343 static enum tu_stage
vk2tu_dst_stage(VkPipelineStageFlags vk_stages)3344 vk2tu_dst_stage(VkPipelineStageFlags vk_stages)
3345 {
3346 enum tu_stage stage = TU_STAGE_PS;
3347 u_foreach_bit (bit, vk_stages) {
3348 enum tu_stage new_stage = vk2tu_single_stage(1ull << bit, true);
3349 stage = MIN2(stage, new_stage);
3350 }
3351
3352 return stage;
3353 }
3354
3355 static void
tu_flush_for_stage(struct tu_cache_state * cache,enum tu_stage src_stage,enum tu_stage dst_stage)3356 tu_flush_for_stage(struct tu_cache_state *cache,
3357 enum tu_stage src_stage, enum tu_stage dst_stage)
3358 {
3359 /* As far as we know, flushes take place in the last stage so if there are
3360 * any pending flushes then we have to move down the source stage, because
3361 * the data only becomes available when the flush finishes. In particular
3362 * this can matter when the CP writes something and we need to invalidate
3363 * UCHE to read it.
3364 */
3365 if (cache->flush_bits & (TU_CMD_FLAG_ALL_FLUSH | TU_CMD_FLAG_ALL_INVALIDATE))
3366 src_stage = TU_STAGE_PS;
3367
3368 /* Note: if the destination stage is the CP, then the CP also has to wait
3369 * for any WFI's to finish. This is already done for draw calls, including
3370 * before indirect param reads, for the most part, so we just need to WFI.
3371 *
3372 * However, some indirect draw opcodes, depending on firmware, don't have
3373 * implicit CP_WAIT_FOR_ME so we have to handle it manually.
3374 *
3375 * Transform feedback counters are read via CP_MEM_TO_REG, which implicitly
3376 * does CP_WAIT_FOR_ME, but we still need a WFI if the GPU writes it.
3377 *
3378 * Currently we read the draw predicate using CP_MEM_TO_MEM, which
3379 * also implicitly does CP_WAIT_FOR_ME. However CP_DRAW_PRED_SET does *not*
3380 * implicitly do CP_WAIT_FOR_ME, it seems to only wait for counters to
3381 * complete since it's written for DX11 where you can only predicate on the
3382 * result of a query object. So if we implement 64-bit comparisons in the
3383 * future, or if CP_DRAW_PRED_SET grows the capability to do 32-bit
3384 * comparisons, then this will have to be dealt with.
3385 */
3386 if (src_stage > dst_stage) {
3387 cache->flush_bits |= TU_CMD_FLAG_WAIT_FOR_IDLE;
3388 if (dst_stage == TU_STAGE_CP)
3389 cache->pending_flush_bits |= TU_CMD_FLAG_WAIT_FOR_ME;
3390 }
3391 }
3392
3393 void
tu_render_pass_state_merge(struct tu_render_pass_state * dst,const struct tu_render_pass_state * src)3394 tu_render_pass_state_merge(struct tu_render_pass_state *dst,
3395 const struct tu_render_pass_state *src)
3396 {
3397 dst->xfb_used |= src->xfb_used;
3398 dst->has_tess |= src->has_tess;
3399 dst->has_prim_generated_query_in_rp |= src->has_prim_generated_query_in_rp;
3400 dst->disable_gmem |= src->disable_gmem;
3401 dst->draw_cs_writes_to_cond_pred |= src->draw_cs_writes_to_cond_pred;
3402
3403 dst->drawcall_count += src->drawcall_count;
3404 dst->drawcall_bandwidth_per_sample_sum +=
3405 src->drawcall_bandwidth_per_sample_sum;
3406 }
3407
3408 void
tu_restore_suspended_pass(struct tu_cmd_buffer * cmd,struct tu_cmd_buffer * suspended)3409 tu_restore_suspended_pass(struct tu_cmd_buffer *cmd,
3410 struct tu_cmd_buffer *suspended)
3411 {
3412 cmd->state.pass = suspended->state.suspended_pass.pass;
3413 cmd->state.subpass = suspended->state.suspended_pass.subpass;
3414 cmd->state.framebuffer = suspended->state.suspended_pass.framebuffer;
3415 cmd->state.attachments = suspended->state.suspended_pass.attachments;
3416 cmd->state.render_area = suspended->state.suspended_pass.render_area;
3417 cmd->state.gmem_layout = suspended->state.suspended_pass.gmem_layout;
3418 cmd->state.tiling = &cmd->state.framebuffer->tiling[cmd->state.gmem_layout];
3419 cmd->state.lrz = suspended->state.suspended_pass.lrz;
3420 }
3421
3422 /* Take the saved pre-chain in "secondary" and copy its commands to "cmd",
3423 * appending it after any saved-up commands in "cmd".
3424 */
3425 void
tu_append_pre_chain(struct tu_cmd_buffer * cmd,struct tu_cmd_buffer * secondary)3426 tu_append_pre_chain(struct tu_cmd_buffer *cmd,
3427 struct tu_cmd_buffer *secondary)
3428 {
3429 tu_cs_add_entries(&cmd->draw_cs, &secondary->pre_chain.draw_cs);
3430 tu_cs_add_entries(&cmd->draw_epilogue_cs,
3431 &secondary->pre_chain.draw_epilogue_cs);
3432 tu_render_pass_state_merge(&cmd->state.rp,
3433 &secondary->pre_chain.state);
3434 if (!u_trace_iterator_equal(secondary->pre_chain.trace_renderpass_start,
3435 secondary->pre_chain.trace_renderpass_end)) {
3436 tu_cs_emit_wfi(&cmd->draw_cs);
3437 tu_cs_emit_pkt7(&cmd->draw_cs, CP_WAIT_FOR_ME, 0);
3438 u_trace_clone_append(secondary->pre_chain.trace_renderpass_start,
3439 secondary->pre_chain.trace_renderpass_end,
3440 &cmd->trace, &cmd->draw_cs,
3441 tu_copy_timestamp_buffer);
3442 }
3443 }
3444
3445 /* Take the saved post-chain in "secondary" and copy it to "cmd".
3446 */
3447 void
tu_append_post_chain(struct tu_cmd_buffer * cmd,struct tu_cmd_buffer * secondary)3448 tu_append_post_chain(struct tu_cmd_buffer *cmd,
3449 struct tu_cmd_buffer *secondary)
3450 {
3451 tu_cs_add_entries(&cmd->draw_cs, &secondary->draw_cs);
3452 tu_cs_add_entries(&cmd->draw_epilogue_cs, &secondary->draw_epilogue_cs);
3453 if (!u_trace_iterator_equal(secondary->trace_renderpass_start,
3454 secondary->trace_renderpass_end)) {
3455 tu_cs_emit_wfi(&cmd->draw_cs);
3456 tu_cs_emit_pkt7(&cmd->draw_cs, CP_WAIT_FOR_ME, 0);
3457 u_trace_clone_append(secondary->trace_renderpass_start,
3458 secondary->trace_renderpass_end,
3459 &cmd->trace, &cmd->draw_cs,
3460 tu_copy_timestamp_buffer);
3461 }
3462 cmd->state.rp = secondary->state.rp;
3463 }
3464
3465 /* Assuming "secondary" is just a sequence of suspended and resuming passes,
3466 * copy its state to "cmd". This also works instead of tu_append_post_chain(),
3467 * but it's a bit slower because we don't assume that the chain begins in
3468 * "secondary" and therefore have to care about the command buffer's
3469 * renderpass state.
3470 */
3471 void
tu_append_pre_post_chain(struct tu_cmd_buffer * cmd,struct tu_cmd_buffer * secondary)3472 tu_append_pre_post_chain(struct tu_cmd_buffer *cmd,
3473 struct tu_cmd_buffer *secondary)
3474 {
3475 tu_cs_add_entries(&cmd->draw_cs, &secondary->draw_cs);
3476 tu_cs_add_entries(&cmd->draw_epilogue_cs, &secondary->draw_epilogue_cs);
3477 if (!u_trace_iterator_equal(secondary->trace_renderpass_start,
3478 secondary->trace_renderpass_end)) {
3479 tu_cs_emit_wfi(&cmd->draw_cs);
3480 tu_cs_emit_pkt7(&cmd->draw_cs, CP_WAIT_FOR_ME, 0);
3481 u_trace_clone_append(secondary->trace_renderpass_start,
3482 secondary->trace_renderpass_end,
3483 &cmd->trace, &cmd->draw_cs,
3484 tu_copy_timestamp_buffer);
3485 }
3486 tu_render_pass_state_merge(&cmd->state.rp,
3487 &secondary->state.rp);
3488 }
3489
3490 /* Take the current render pass state and save it to "pre_chain" to be
3491 * combined later.
3492 */
3493 static void
tu_save_pre_chain(struct tu_cmd_buffer * cmd)3494 tu_save_pre_chain(struct tu_cmd_buffer *cmd)
3495 {
3496 tu_cs_add_entries(&cmd->pre_chain.draw_cs,
3497 &cmd->draw_cs);
3498 tu_cs_add_entries(&cmd->pre_chain.draw_epilogue_cs,
3499 &cmd->draw_epilogue_cs);
3500 cmd->pre_chain.trace_renderpass_start =
3501 cmd->trace_renderpass_start;
3502 cmd->pre_chain.trace_renderpass_end =
3503 cmd->trace_renderpass_end;
3504 cmd->pre_chain.state = cmd->state.rp;
3505 }
3506
3507 VKAPI_ATTR void VKAPI_CALL
tu_CmdExecuteCommands(VkCommandBuffer commandBuffer,uint32_t commandBufferCount,const VkCommandBuffer * pCmdBuffers)3508 tu_CmdExecuteCommands(VkCommandBuffer commandBuffer,
3509 uint32_t commandBufferCount,
3510 const VkCommandBuffer *pCmdBuffers)
3511 {
3512 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
3513 VkResult result;
3514
3515 assert(commandBufferCount > 0);
3516
3517 /* Emit any pending flushes. */
3518 if (cmd->state.pass) {
3519 tu_flush_all_pending(&cmd->state.renderpass_cache);
3520 tu_emit_cache_flush_renderpass(cmd, &cmd->draw_cs);
3521 } else {
3522 tu_flush_all_pending(&cmd->state.cache);
3523 tu_emit_cache_flush(cmd, &cmd->cs);
3524 }
3525
3526 for (uint32_t i = 0; i < commandBufferCount; i++) {
3527 TU_FROM_HANDLE(tu_cmd_buffer, secondary, pCmdBuffers[i]);
3528
3529 if (secondary->usage_flags &
3530 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
3531 assert(tu_cs_is_empty(&secondary->cs));
3532
3533 result = tu_cs_add_entries(&cmd->draw_cs, &secondary->draw_cs);
3534 if (result != VK_SUCCESS) {
3535 cmd->record_result = result;
3536 break;
3537 }
3538
3539 result = tu_cs_add_entries(&cmd->draw_epilogue_cs,
3540 &secondary->draw_epilogue_cs);
3541 if (result != VK_SUCCESS) {
3542 cmd->record_result = result;
3543 break;
3544 }
3545
3546 /* If LRZ was made invalid in secondary - we should disable
3547 * LRZ retroactively for the whole renderpass.
3548 */
3549 if (!secondary->state.lrz.valid)
3550 cmd->state.lrz.valid = false;
3551
3552 tu_render_pass_state_merge(&cmd->state.rp, &secondary->state.rp);
3553 } else {
3554 switch (secondary->state.suspend_resume) {
3555 case SR_NONE:
3556 assert(tu_cs_is_empty(&secondary->draw_cs));
3557 assert(tu_cs_is_empty(&secondary->draw_epilogue_cs));
3558 tu_cs_add_entries(&cmd->cs, &secondary->cs);
3559 break;
3560
3561 case SR_IN_PRE_CHAIN:
3562 /* cmd may be empty, which means that the chain begins before cmd
3563 * in which case we have to update its state.
3564 */
3565 if (cmd->state.suspend_resume == SR_NONE) {
3566 cmd->state.suspend_resume = SR_IN_PRE_CHAIN;
3567 cmd->trace_renderpass_start = u_trace_end_iterator(&cmd->trace);
3568 }
3569
3570 /* The secondary is just a continuous suspend/resume chain so we
3571 * just have to append it to the the command buffer.
3572 */
3573 assert(tu_cs_is_empty(&secondary->cs));
3574 tu_append_pre_post_chain(cmd, secondary);
3575 break;
3576
3577 case SR_AFTER_PRE_CHAIN:
3578 case SR_IN_CHAIN:
3579 case SR_IN_CHAIN_AFTER_PRE_CHAIN:
3580 if (secondary->state.suspend_resume == SR_AFTER_PRE_CHAIN ||
3581 secondary->state.suspend_resume == SR_IN_CHAIN_AFTER_PRE_CHAIN) {
3582 /* In thse cases there is a `pre_chain` in the secondary which
3583 * ends that we need to append to the primary.
3584 */
3585
3586 if (cmd->state.suspend_resume == SR_NONE)
3587 cmd->trace_renderpass_start = u_trace_end_iterator(&cmd->trace);
3588
3589 tu_append_pre_chain(cmd, secondary);
3590 cmd->trace_renderpass_end = u_trace_end_iterator(&cmd->trace);
3591
3592 /* We're about to render, so we need to end the command stream
3593 * in case there were any extra commands generated by copying
3594 * the trace.
3595 */
3596 tu_cs_end(&cmd->draw_cs);
3597 tu_cs_end(&cmd->draw_epilogue_cs);
3598
3599 switch (cmd->state.suspend_resume) {
3600 case SR_NONE:
3601 case SR_IN_PRE_CHAIN:
3602 /* The renderpass chain ends in the secondary but isn't
3603 * started in the primary, so we have to move the state to
3604 * `pre_chain`.
3605 */
3606 tu_save_pre_chain(cmd);
3607 cmd->state.suspend_resume = SR_AFTER_PRE_CHAIN;
3608 break;
3609 case SR_IN_CHAIN:
3610 case SR_IN_CHAIN_AFTER_PRE_CHAIN:
3611 /* The renderpass ends in the secondary and starts somewhere
3612 * earlier in this primary. Since the last render pass in
3613 * the chain is in the secondary, we are technically outside
3614 * of a render pass. Fix that here by reusing the dynamic
3615 * render pass that was setup for the last suspended render
3616 * pass before the secondary.
3617 */
3618 tu_restore_suspended_pass(cmd, cmd);
3619
3620 tu_cmd_render(cmd);
3621 if (cmd->state.suspend_resume == SR_IN_CHAIN)
3622 cmd->state.suspend_resume = SR_NONE;
3623 else
3624 cmd->state.suspend_resume = SR_AFTER_PRE_CHAIN;
3625 break;
3626 case SR_AFTER_PRE_CHAIN:
3627 unreachable("resuming render pass is not preceded by suspending one");
3628 }
3629
3630 tu_reset_render_pass(cmd);
3631 }
3632
3633 tu_cs_add_entries(&cmd->cs, &secondary->cs);
3634
3635 if (secondary->state.suspend_resume == SR_IN_CHAIN_AFTER_PRE_CHAIN ||
3636 secondary->state.suspend_resume == SR_IN_CHAIN) {
3637 /* The secondary ends in a "post-chain" (the opposite of a
3638 * pre-chain) that we need to copy into the current command
3639 * buffer.
3640 */
3641 cmd->trace_renderpass_start = u_trace_end_iterator(&cmd->trace);
3642 tu_append_post_chain(cmd, secondary);
3643 cmd->trace_renderpass_end = u_trace_end_iterator(&cmd->trace);
3644 cmd->state.suspended_pass = secondary->state.suspended_pass;
3645
3646 switch (cmd->state.suspend_resume) {
3647 case SR_NONE:
3648 cmd->state.suspend_resume = SR_IN_CHAIN;
3649 break;
3650 case SR_AFTER_PRE_CHAIN:
3651 cmd->state.suspend_resume = SR_IN_CHAIN_AFTER_PRE_CHAIN;
3652 break;
3653 default:
3654 unreachable("suspending render pass is followed by a not resuming one");
3655 }
3656 }
3657 }
3658 }
3659
3660 cmd->state.index_size = secondary->state.index_size; /* for restart index update */
3661 }
3662 cmd->state.dirty = ~0u; /* TODO: set dirty only what needs to be */
3663
3664 if (!cmd->state.lrz.gpu_dir_tracking && cmd->state.pass) {
3665 /* After a secondary command buffer is executed, LRZ is not valid
3666 * until it is cleared again.
3667 */
3668 cmd->state.lrz.valid = false;
3669 }
3670
3671 /* After executing secondary command buffers, there may have been arbitrary
3672 * flushes executed, so when we encounter a pipeline barrier with a
3673 * srcMask, we have to assume that we need to invalidate. Therefore we need
3674 * to re-initialize the cache with all pending invalidate bits set.
3675 */
3676 if (cmd->state.pass) {
3677 tu_cache_init(&cmd->state.renderpass_cache);
3678 } else {
3679 tu_cache_init(&cmd->state.cache);
3680 }
3681 }
3682
3683 VKAPI_ATTR VkResult VKAPI_CALL
tu_CreateCommandPool(VkDevice _device,const VkCommandPoolCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkCommandPool * pCmdPool)3684 tu_CreateCommandPool(VkDevice _device,
3685 const VkCommandPoolCreateInfo *pCreateInfo,
3686 const VkAllocationCallbacks *pAllocator,
3687 VkCommandPool *pCmdPool)
3688 {
3689 TU_FROM_HANDLE(tu_device, device, _device);
3690 struct tu_cmd_pool *pool;
3691
3692 pool = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*pool), 8,
3693 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3694 if (pool == NULL)
3695 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
3696
3697 VkResult result = vk_command_pool_init(&pool->vk, &device->vk,
3698 pCreateInfo, pAllocator);
3699 if (result != VK_SUCCESS) {
3700 vk_free2(&device->vk.alloc, pAllocator, pool);
3701 return result;
3702 }
3703
3704 list_inithead(&pool->cmd_buffers);
3705 list_inithead(&pool->free_cmd_buffers);
3706
3707 *pCmdPool = tu_cmd_pool_to_handle(pool);
3708
3709 return VK_SUCCESS;
3710 }
3711
3712 VKAPI_ATTR void VKAPI_CALL
tu_DestroyCommandPool(VkDevice _device,VkCommandPool commandPool,const VkAllocationCallbacks * pAllocator)3713 tu_DestroyCommandPool(VkDevice _device,
3714 VkCommandPool commandPool,
3715 const VkAllocationCallbacks *pAllocator)
3716 {
3717 TU_FROM_HANDLE(tu_device, device, _device);
3718 TU_FROM_HANDLE(tu_cmd_pool, pool, commandPool);
3719
3720 if (!pool)
3721 return;
3722
3723 list_for_each_entry_safe(struct tu_cmd_buffer, cmd_buffer,
3724 &pool->cmd_buffers, pool_link)
3725 {
3726 tu_cmd_buffer_destroy(cmd_buffer);
3727 }
3728
3729 list_for_each_entry_safe(struct tu_cmd_buffer, cmd_buffer,
3730 &pool->free_cmd_buffers, pool_link)
3731 {
3732 tu_cmd_buffer_destroy(cmd_buffer);
3733 }
3734
3735 vk_command_pool_finish(&pool->vk);
3736 vk_free2(&device->vk.alloc, pAllocator, pool);
3737 }
3738
3739 VKAPI_ATTR VkResult VKAPI_CALL
tu_ResetCommandPool(VkDevice device,VkCommandPool commandPool,VkCommandPoolResetFlags flags)3740 tu_ResetCommandPool(VkDevice device,
3741 VkCommandPool commandPool,
3742 VkCommandPoolResetFlags flags)
3743 {
3744 TU_FROM_HANDLE(tu_cmd_pool, pool, commandPool);
3745 VkResult result;
3746
3747 list_for_each_entry(struct tu_cmd_buffer, cmd_buffer, &pool->cmd_buffers,
3748 pool_link)
3749 {
3750 result = tu_reset_cmd_buffer(cmd_buffer);
3751 if (result != VK_SUCCESS)
3752 return result;
3753 }
3754
3755 return VK_SUCCESS;
3756 }
3757
3758 VKAPI_ATTR void VKAPI_CALL
tu_TrimCommandPool(VkDevice device,VkCommandPool commandPool,VkCommandPoolTrimFlags flags)3759 tu_TrimCommandPool(VkDevice device,
3760 VkCommandPool commandPool,
3761 VkCommandPoolTrimFlags flags)
3762 {
3763 TU_FROM_HANDLE(tu_cmd_pool, pool, commandPool);
3764
3765 if (!pool)
3766 return;
3767
3768 list_for_each_entry_safe(struct tu_cmd_buffer, cmd_buffer,
3769 &pool->free_cmd_buffers, pool_link)
3770 {
3771 tu_cmd_buffer_destroy(cmd_buffer);
3772 }
3773 }
3774
3775 static void
tu_subpass_barrier(struct tu_cmd_buffer * cmd_buffer,const struct tu_subpass_barrier * barrier,bool external)3776 tu_subpass_barrier(struct tu_cmd_buffer *cmd_buffer,
3777 const struct tu_subpass_barrier *barrier,
3778 bool external)
3779 {
3780 /* Note: we don't know until the end of the subpass whether we'll use
3781 * sysmem, so assume sysmem here to be safe.
3782 */
3783 struct tu_cache_state *cache =
3784 external ? &cmd_buffer->state.cache : &cmd_buffer->state.renderpass_cache;
3785 VkPipelineStageFlags2 src_stage_vk =
3786 sanitize_src_stage(barrier->src_stage_mask);
3787 VkPipelineStageFlags2 dst_stage_vk =
3788 sanitize_dst_stage(barrier->dst_stage_mask);
3789 enum tu_cmd_access_mask src_flags =
3790 vk2tu_access(barrier->src_access_mask, src_stage_vk, false, false);
3791 enum tu_cmd_access_mask dst_flags =
3792 vk2tu_access(barrier->dst_access_mask, dst_stage_vk, false, false);
3793
3794 if (barrier->incoherent_ccu_color)
3795 src_flags |= TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE;
3796 if (barrier->incoherent_ccu_depth)
3797 src_flags |= TU_ACCESS_CCU_DEPTH_INCOHERENT_WRITE;
3798
3799 tu_flush_for_access(cache, src_flags, dst_flags);
3800
3801 enum tu_stage src_stage = vk2tu_src_stage(src_stage_vk);
3802 enum tu_stage dst_stage = vk2tu_dst_stage(dst_stage_vk);
3803 tu_flush_for_stage(cache, src_stage, dst_stage);
3804 }
3805
3806 /* emit mrt/zs/msaa/ubwc state for the subpass that is starting (either at
3807 * vkCmdBeginRenderPass2() or vkCmdNextSubpass2())
3808 */
3809 static void
tu_emit_subpass_begin(struct tu_cmd_buffer * cmd)3810 tu_emit_subpass_begin(struct tu_cmd_buffer *cmd)
3811 {
3812 tu6_emit_zs(cmd, cmd->state.subpass, &cmd->draw_cs);
3813 tu6_emit_mrt(cmd, cmd->state.subpass, &cmd->draw_cs);
3814 if (cmd->state.subpass->samples)
3815 tu6_emit_msaa(&cmd->draw_cs, cmd->state.subpass->samples, cmd->state.line_mode);
3816 tu6_emit_render_cntl(cmd, cmd->state.subpass, &cmd->draw_cs, false);
3817
3818 tu_set_input_attachments(cmd, cmd->state.subpass);
3819 }
3820
3821 VKAPI_ATTR void VKAPI_CALL
tu_CmdBeginRenderPass2(VkCommandBuffer commandBuffer,const VkRenderPassBeginInfo * pRenderPassBegin,const VkSubpassBeginInfo * pSubpassBeginInfo)3822 tu_CmdBeginRenderPass2(VkCommandBuffer commandBuffer,
3823 const VkRenderPassBeginInfo *pRenderPassBegin,
3824 const VkSubpassBeginInfo *pSubpassBeginInfo)
3825 {
3826 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
3827
3828 if (unlikely(cmd->device->instance->debug_flags & TU_DEBUG_DYNAMIC)) {
3829 vk_common_CmdBeginRenderPass2(commandBuffer, pRenderPassBegin,
3830 pSubpassBeginInfo);
3831 return;
3832 }
3833
3834 TU_FROM_HANDLE(tu_render_pass, pass, pRenderPassBegin->renderPass);
3835 TU_FROM_HANDLE(tu_framebuffer, fb, pRenderPassBegin->framebuffer);
3836
3837 const struct VkRenderPassAttachmentBeginInfo *pAttachmentInfo =
3838 vk_find_struct_const(pRenderPassBegin->pNext,
3839 RENDER_PASS_ATTACHMENT_BEGIN_INFO);
3840
3841 cmd->state.pass = pass;
3842 cmd->state.subpass = pass->subpasses;
3843 cmd->state.framebuffer = fb;
3844 cmd->state.render_area = pRenderPassBegin->renderArea;
3845
3846 cmd->state.attachments =
3847 vk_alloc(&cmd->pool->vk.alloc, pass->attachment_count *
3848 sizeof(cmd->state.attachments[0]), 8,
3849 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3850
3851 if (!cmd->state.attachments) {
3852 cmd->record_result = VK_ERROR_OUT_OF_HOST_MEMORY;
3853 return;
3854 }
3855
3856 for (unsigned i = 0; i < pass->attachment_count; i++) {
3857 cmd->state.attachments[i] = pAttachmentInfo ?
3858 tu_image_view_from_handle(pAttachmentInfo->pAttachments[i]) :
3859 cmd->state.framebuffer->attachments[i].attachment;
3860 }
3861 tu_choose_gmem_layout(cmd);
3862
3863 trace_start_render_pass(&cmd->trace, &cmd->cs);
3864
3865 /* Note: because this is external, any flushes will happen before draw_cs
3866 * gets called. However deferred flushes could have to happen later as part
3867 * of the subpass.
3868 */
3869 tu_subpass_barrier(cmd, &pass->subpasses[0].start_barrier, true);
3870 cmd->state.renderpass_cache.pending_flush_bits =
3871 cmd->state.cache.pending_flush_bits;
3872 cmd->state.renderpass_cache.flush_bits = 0;
3873
3874 if (pass->subpasses[0].feedback_invalidate)
3875 cmd->state.renderpass_cache.flush_bits |= TU_CMD_FLAG_CACHE_INVALIDATE;
3876
3877 tu_lrz_begin_renderpass(cmd, pRenderPassBegin->pClearValues);
3878
3879 cmd->trace_renderpass_start = u_trace_end_iterator(&cmd->trace);
3880
3881 tu_emit_renderpass_begin(cmd, pRenderPassBegin->pClearValues);
3882 tu_emit_subpass_begin(cmd);
3883 }
3884
3885 VKAPI_ATTR void VKAPI_CALL
tu_CmdBeginRendering(VkCommandBuffer commandBuffer,const VkRenderingInfo * pRenderingInfo)3886 tu_CmdBeginRendering(VkCommandBuffer commandBuffer,
3887 const VkRenderingInfo *pRenderingInfo)
3888 {
3889 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
3890 VkClearValue clear_values[2 * (MAX_RTS + 1)];
3891
3892 tu_setup_dynamic_render_pass(cmd, pRenderingInfo);
3893 tu_setup_dynamic_framebuffer(cmd, pRenderingInfo);
3894
3895 cmd->state.pass = &cmd->dynamic_pass;
3896 cmd->state.subpass = &cmd->dynamic_subpass;
3897 cmd->state.framebuffer = &cmd->dynamic_framebuffer;
3898 cmd->state.render_area = pRenderingInfo->renderArea;
3899
3900 cmd->state.attachments = cmd->dynamic_attachments;
3901
3902 for (unsigned i = 0; i < pRenderingInfo->colorAttachmentCount; i++) {
3903 uint32_t a = cmd->dynamic_subpass.color_attachments[i].attachment;
3904 if (!pRenderingInfo->pColorAttachments[i].imageView)
3905 continue;
3906
3907 TU_FROM_HANDLE(tu_image_view, view,
3908 pRenderingInfo->pColorAttachments[i].imageView);
3909 cmd->state.attachments[a] = view;
3910 clear_values[a] = pRenderingInfo->pColorAttachments[i].clearValue;
3911
3912 a = cmd->dynamic_subpass.resolve_attachments[i].attachment;
3913 if (a != VK_ATTACHMENT_UNUSED) {
3914 TU_FROM_HANDLE(tu_image_view, resolve_view,
3915 pRenderingInfo->pColorAttachments[i].resolveImageView);
3916 cmd->state.attachments[a] = resolve_view;
3917 }
3918 }
3919
3920 uint32_t a = cmd->dynamic_subpass.depth_stencil_attachment.attachment;
3921 if (pRenderingInfo->pDepthAttachment || pRenderingInfo->pStencilAttachment) {
3922 const struct VkRenderingAttachmentInfo *common_info =
3923 (pRenderingInfo->pDepthAttachment &&
3924 pRenderingInfo->pDepthAttachment->imageView != VK_NULL_HANDLE) ?
3925 pRenderingInfo->pDepthAttachment :
3926 pRenderingInfo->pStencilAttachment;
3927 if (common_info && common_info->imageView != VK_NULL_HANDLE) {
3928 TU_FROM_HANDLE(tu_image_view, view, common_info->imageView);
3929 cmd->state.attachments[a] = view;
3930 if (pRenderingInfo->pDepthAttachment) {
3931 clear_values[a].depthStencil.depth =
3932 pRenderingInfo->pDepthAttachment->clearValue.depthStencil.depth;
3933 }
3934
3935 if (pRenderingInfo->pStencilAttachment) {
3936 clear_values[a].depthStencil.stencil =
3937 pRenderingInfo->pStencilAttachment->clearValue.depthStencil.stencil;
3938 }
3939
3940 if (cmd->dynamic_subpass.resolve_count >
3941 cmd->dynamic_subpass.color_count) {
3942 TU_FROM_HANDLE(tu_image_view, resolve_view,
3943 common_info->resolveImageView);
3944 a = cmd->dynamic_subpass.resolve_attachments[cmd->dynamic_subpass.color_count].attachment;
3945 cmd->state.attachments[a] = resolve_view;
3946 }
3947 }
3948 }
3949
3950 if (unlikely(cmd->device->instance->debug_flags & TU_DEBUG_DYNAMIC)) {
3951 const VkRenderingSelfDependencyInfoMESA *self_dependency =
3952 vk_find_struct_const(pRenderingInfo->pNext, RENDERING_SELF_DEPENDENCY_INFO_MESA);
3953 if (self_dependency &&
3954 (self_dependency->colorSelfDependencies ||
3955 self_dependency->depthSelfDependency ||
3956 self_dependency->stencilSelfDependency)) {
3957 /* Mesa's renderpass emulation requires us to use normal attachments
3958 * for input attachments, and currently doesn't try to keep track of
3959 * which color/depth attachment an input attachment corresponds to.
3960 * So when there's a self-dependency, we have to use sysmem.
3961 */
3962 cmd->state.rp.disable_gmem = true;
3963 }
3964 }
3965
3966 tu_choose_gmem_layout(cmd);
3967
3968 cmd->state.renderpass_cache.pending_flush_bits =
3969 cmd->state.cache.pending_flush_bits;
3970 cmd->state.renderpass_cache.flush_bits = 0;
3971
3972 bool resuming = pRenderingInfo->flags & VK_RENDERING_RESUMING_BIT;
3973 bool suspending = pRenderingInfo->flags & VK_RENDERING_SUSPENDING_BIT;
3974 cmd->state.suspending = suspending;
3975 cmd->state.resuming = resuming;
3976
3977 /* We can't track LRZ across command buffer boundaries, so we have to
3978 * disable LRZ when resuming/suspending unless we can track on the GPU.
3979 */
3980 if ((resuming || suspending) &&
3981 !cmd->device->physical_device->info->a6xx.has_lrz_dir_tracking) {
3982 cmd->state.lrz.valid = false;
3983 } else {
3984 if (resuming)
3985 tu_lrz_begin_resumed_renderpass(cmd, clear_values);
3986 else
3987 tu_lrz_begin_renderpass(cmd, clear_values);
3988 }
3989
3990
3991 if (suspending) {
3992 cmd->state.suspended_pass.pass = cmd->state.pass;
3993 cmd->state.suspended_pass.subpass = cmd->state.subpass;
3994 cmd->state.suspended_pass.framebuffer = cmd->state.framebuffer;
3995 cmd->state.suspended_pass.render_area = cmd->state.render_area;
3996 cmd->state.suspended_pass.attachments = cmd->state.attachments;
3997 cmd->state.suspended_pass.gmem_layout = cmd->state.gmem_layout;
3998 }
3999
4000 if (!resuming) {
4001 trace_start_render_pass(&cmd->trace, &cmd->cs);
4002 }
4003
4004 if (!resuming || cmd->state.suspend_resume == SR_NONE) {
4005 cmd->trace_renderpass_start = u_trace_end_iterator(&cmd->trace);
4006 }
4007
4008 if (!resuming) {
4009 tu_emit_renderpass_begin(cmd, clear_values);
4010 tu_emit_subpass_begin(cmd);
4011 }
4012
4013 if (suspending && !resuming) {
4014 /* entering a chain */
4015 switch (cmd->state.suspend_resume) {
4016 case SR_NONE:
4017 cmd->state.suspend_resume = SR_IN_CHAIN;
4018 break;
4019 case SR_AFTER_PRE_CHAIN:
4020 cmd->state.suspend_resume = SR_IN_CHAIN_AFTER_PRE_CHAIN;
4021 break;
4022 case SR_IN_PRE_CHAIN:
4023 case SR_IN_CHAIN:
4024 case SR_IN_CHAIN_AFTER_PRE_CHAIN:
4025 unreachable("suspending render pass not followed by resuming pass");
4026 break;
4027 }
4028 }
4029
4030 if (resuming && cmd->state.suspend_resume == SR_NONE)
4031 cmd->state.suspend_resume = SR_IN_PRE_CHAIN;
4032 }
4033
4034 VKAPI_ATTR void VKAPI_CALL
tu_CmdNextSubpass2(VkCommandBuffer commandBuffer,const VkSubpassBeginInfo * pSubpassBeginInfo,const VkSubpassEndInfo * pSubpassEndInfo)4035 tu_CmdNextSubpass2(VkCommandBuffer commandBuffer,
4036 const VkSubpassBeginInfo *pSubpassBeginInfo,
4037 const VkSubpassEndInfo *pSubpassEndInfo)
4038 {
4039 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4040
4041 if (unlikely(cmd->device->instance->debug_flags & TU_DEBUG_DYNAMIC)) {
4042 vk_common_CmdNextSubpass2(commandBuffer, pSubpassBeginInfo,
4043 pSubpassEndInfo);
4044 return;
4045 }
4046
4047 const struct tu_render_pass *pass = cmd->state.pass;
4048 struct tu_cs *cs = &cmd->draw_cs;
4049 const struct tu_subpass *last_subpass = cmd->state.subpass;
4050
4051 const struct tu_subpass *subpass = cmd->state.subpass++;
4052
4053 /* Track LRZ valid state
4054 *
4055 * TODO: Improve this tracking for keeping the state of the past depth/stencil images,
4056 * so if they become active again, we reuse its old state.
4057 */
4058 if (last_subpass->depth_stencil_attachment.attachment != subpass->depth_stencil_attachment.attachment) {
4059 cmd->state.lrz.valid = false;
4060 cmd->state.dirty |= TU_CMD_DIRTY_LRZ;
4061 }
4062
4063 tu_cond_exec_start(cs, CP_COND_EXEC_0_RENDER_MODE_GMEM);
4064
4065 if (subpass->resolve_attachments) {
4066 tu6_emit_blit_scissor(cmd, cs, true);
4067
4068 for (unsigned i = 0; i < subpass->resolve_count; i++) {
4069 uint32_t a = subpass->resolve_attachments[i].attachment;
4070 if (a == VK_ATTACHMENT_UNUSED)
4071 continue;
4072
4073 uint32_t gmem_a = tu_subpass_get_attachment_to_resolve(subpass, i);
4074
4075 tu_store_gmem_attachment(cmd, cs, a, gmem_a, false);
4076
4077 if (!pass->attachments[a].gmem)
4078 continue;
4079
4080 /* check if the resolved attachment is needed by later subpasses,
4081 * if it is, should be doing a GMEM->GMEM resolve instead of GMEM->MEM->GMEM..
4082 */
4083 perf_debug(cmd->device, "TODO: missing GMEM->GMEM resolve path\n");
4084 tu_load_gmem_attachment(cmd, cs, a, false, true);
4085 }
4086 }
4087
4088 tu_cond_exec_end(cs);
4089
4090 tu_cond_exec_start(cs, CP_COND_EXEC_0_RENDER_MODE_SYSMEM);
4091
4092 tu6_emit_sysmem_resolves(cmd, cs, subpass);
4093
4094 tu_cond_exec_end(cs);
4095
4096 /* Handle dependencies for the next subpass */
4097 tu_subpass_barrier(cmd, &cmd->state.subpass->start_barrier, false);
4098
4099 if (cmd->state.subpass->feedback_invalidate)
4100 cmd->state.renderpass_cache.flush_bits |= TU_CMD_FLAG_CACHE_INVALIDATE;
4101
4102 tu_emit_subpass_begin(cmd);
4103 }
4104
4105 static uint32_t
tu6_user_consts_size(const struct tu_pipeline * pipeline,gl_shader_stage type)4106 tu6_user_consts_size(const struct tu_pipeline *pipeline,
4107 gl_shader_stage type)
4108 {
4109 const struct tu_program_descriptor_linkage *link =
4110 &pipeline->program.link[type];
4111 uint32_t dwords = 0;
4112
4113 if (link->push_consts.dwords > 0) {
4114 unsigned num_units = link->push_consts.dwords;
4115 dwords += 4 + num_units;
4116 }
4117
4118 return dwords;
4119 }
4120
4121 static void
tu6_emit_user_consts(struct tu_cs * cs,const struct tu_pipeline * pipeline,gl_shader_stage type,uint32_t * push_constants)4122 tu6_emit_user_consts(struct tu_cs *cs,
4123 const struct tu_pipeline *pipeline,
4124 gl_shader_stage type,
4125 uint32_t *push_constants)
4126 {
4127 const struct tu_program_descriptor_linkage *link =
4128 &pipeline->program.link[type];
4129
4130 if (link->push_consts.dwords > 0) {
4131 unsigned num_units = link->push_consts.dwords;
4132 unsigned offset = link->push_consts.lo;
4133
4134 /* DST_OFF and NUM_UNIT requires vec4 units */
4135 tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 3 + num_units);
4136 tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset / 4) |
4137 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
4138 CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
4139 CP_LOAD_STATE6_0_STATE_BLOCK(tu6_stage2shadersb(type)) |
4140 CP_LOAD_STATE6_0_NUM_UNIT(num_units / 4));
4141 tu_cs_emit(cs, 0);
4142 tu_cs_emit(cs, 0);
4143 for (unsigned i = 0; i < num_units; i++)
4144 tu_cs_emit(cs, push_constants[i + offset]);
4145 }
4146 }
4147
4148 static void
tu6_emit_shared_consts(struct tu_cs * cs,const struct tu_pipeline * pipeline,uint32_t * push_constants,bool compute)4149 tu6_emit_shared_consts(struct tu_cs *cs,
4150 const struct tu_pipeline *pipeline,
4151 uint32_t *push_constants,
4152 bool compute)
4153 {
4154 if (pipeline->shared_consts.dwords > 0) {
4155 /* Offset and num_units for shared consts are in units of dwords. */
4156 unsigned num_units = pipeline->shared_consts.dwords;
4157 unsigned offset = pipeline->shared_consts.lo;
4158
4159 enum a6xx_state_type st = compute ? ST6_UBO : ST6_CONSTANTS;
4160 uint32_t cp_load_state = compute ? CP_LOAD_STATE6_FRAG : CP_LOAD_STATE6;
4161
4162 tu_cs_emit_pkt7(cs, cp_load_state, 3 + num_units);
4163 tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset) |
4164 CP_LOAD_STATE6_0_STATE_TYPE(st) |
4165 CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
4166 CP_LOAD_STATE6_0_STATE_BLOCK(SB6_IBO) |
4167 CP_LOAD_STATE6_0_NUM_UNIT(num_units));
4168 tu_cs_emit(cs, 0);
4169 tu_cs_emit(cs, 0);
4170
4171 for (unsigned i = 0; i < num_units; i++)
4172 tu_cs_emit(cs, push_constants[i + offset]);
4173 }
4174 }
4175
4176 static uint32_t
tu6_const_size(struct tu_cmd_buffer * cmd,const struct tu_pipeline * pipeline,bool compute)4177 tu6_const_size(struct tu_cmd_buffer *cmd,
4178 const struct tu_pipeline *pipeline,
4179 bool compute)
4180 {
4181 uint32_t dwords = 0;
4182
4183 if (pipeline->shared_consts.dwords > 0) {
4184 dwords = pipeline->shared_consts.dwords + 4;
4185 } else {
4186 if (compute) {
4187 dwords = tu6_user_consts_size(pipeline, MESA_SHADER_COMPUTE);
4188 } else {
4189 for (uint32_t type = MESA_SHADER_VERTEX; type <= MESA_SHADER_FRAGMENT; type++)
4190 dwords += tu6_user_consts_size(pipeline, type);
4191 }
4192 }
4193
4194 return dwords;
4195 }
4196
4197 static struct tu_draw_state
tu6_emit_consts(struct tu_cmd_buffer * cmd,const struct tu_pipeline * pipeline,bool compute)4198 tu6_emit_consts(struct tu_cmd_buffer *cmd,
4199 const struct tu_pipeline *pipeline,
4200 bool compute)
4201 {
4202 uint32_t dwords = 0;
4203
4204 dwords = tu6_const_size(cmd, pipeline, compute);
4205
4206 if (dwords == 0)
4207 return (struct tu_draw_state) {};
4208
4209 struct tu_cs cs;
4210 tu_cs_begin_sub_stream(&cmd->sub_cs, dwords, &cs);
4211
4212 if (pipeline->shared_consts.dwords > 0) {
4213 tu6_emit_shared_consts(&cs, pipeline, cmd->push_constants, compute);
4214
4215 for (uint32_t i = 0; i < ARRAY_SIZE(pipeline->program.link); i++) {
4216 const struct tu_program_descriptor_linkage *link =
4217 &pipeline->program.link[i];
4218 assert(!link->push_consts.dwords);
4219 }
4220 } else {
4221 if (compute) {
4222 tu6_emit_user_consts(&cs, pipeline, MESA_SHADER_COMPUTE, cmd->push_constants);
4223 } else {
4224 for (uint32_t type = MESA_SHADER_VERTEX; type <= MESA_SHADER_FRAGMENT; type++)
4225 tu6_emit_user_consts(&cs, pipeline, type, cmd->push_constants);
4226 }
4227 }
4228
4229 return tu_cs_end_draw_state(&cmd->sub_cs, &cs);
4230 }
4231
4232 static bool
tu6_writes_depth(struct tu_cmd_buffer * cmd,bool depth_test_enable)4233 tu6_writes_depth(struct tu_cmd_buffer *cmd, bool depth_test_enable)
4234 {
4235 bool depth_write_enable =
4236 cmd->state.rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_WRITE_ENABLE;
4237
4238 VkCompareOp depth_compare_op =
4239 (cmd->state.rb_depth_cntl & A6XX_RB_DEPTH_CNTL_ZFUNC__MASK) >> A6XX_RB_DEPTH_CNTL_ZFUNC__SHIFT;
4240
4241 bool depth_compare_op_writes = depth_compare_op != VK_COMPARE_OP_NEVER;
4242
4243 return depth_test_enable && depth_write_enable && depth_compare_op_writes;
4244 }
4245
4246 static bool
tu6_writes_stencil(struct tu_cmd_buffer * cmd)4247 tu6_writes_stencil(struct tu_cmd_buffer *cmd)
4248 {
4249 bool stencil_test_enable =
4250 cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_STENCIL_ENABLE;
4251
4252 bool stencil_front_writemask =
4253 (cmd->state.pipeline->dynamic_state_mask & BIT(VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) ?
4254 (cmd->state.dynamic_stencil_wrmask & 0xff) :
4255 (cmd->state.pipeline->stencil_wrmask & 0xff);
4256
4257 bool stencil_back_writemask =
4258 (cmd->state.pipeline->dynamic_state_mask & BIT(VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) ?
4259 ((cmd->state.dynamic_stencil_wrmask & 0xff00) >> 8) :
4260 (cmd->state.pipeline->stencil_wrmask & 0xff00) >> 8;
4261
4262 VkStencilOp front_fail_op =
4263 (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_FAIL__MASK) >> A6XX_RB_STENCIL_CONTROL_FAIL__SHIFT;
4264 VkStencilOp front_pass_op =
4265 (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_ZPASS__MASK) >> A6XX_RB_STENCIL_CONTROL_ZPASS__SHIFT;
4266 VkStencilOp front_depth_fail_op =
4267 (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_ZFAIL__MASK) >> A6XX_RB_STENCIL_CONTROL_ZFAIL__SHIFT;
4268 VkStencilOp back_fail_op =
4269 (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_FAIL_BF__MASK) >> A6XX_RB_STENCIL_CONTROL_FAIL_BF__SHIFT;
4270 VkStencilOp back_pass_op =
4271 (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_ZPASS_BF__MASK) >> A6XX_RB_STENCIL_CONTROL_ZPASS_BF__SHIFT;
4272 VkStencilOp back_depth_fail_op =
4273 (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_ZFAIL_BF__MASK) >> A6XX_RB_STENCIL_CONTROL_ZFAIL_BF__SHIFT;
4274
4275 bool stencil_front_op_writes =
4276 front_pass_op != VK_STENCIL_OP_KEEP ||
4277 front_fail_op != VK_STENCIL_OP_KEEP ||
4278 front_depth_fail_op != VK_STENCIL_OP_KEEP;
4279
4280 bool stencil_back_op_writes =
4281 back_pass_op != VK_STENCIL_OP_KEEP ||
4282 back_fail_op != VK_STENCIL_OP_KEEP ||
4283 back_depth_fail_op != VK_STENCIL_OP_KEEP;
4284
4285 return stencil_test_enable &&
4286 ((stencil_front_writemask && stencil_front_op_writes) ||
4287 (stencil_back_writemask && stencil_back_op_writes));
4288 }
4289
4290 static void
tu6_build_depth_plane_z_mode(struct tu_cmd_buffer * cmd,struct tu_cs * cs)4291 tu6_build_depth_plane_z_mode(struct tu_cmd_buffer *cmd, struct tu_cs *cs)
4292 {
4293 enum a6xx_ztest_mode zmode = A6XX_EARLY_Z;
4294 bool depth_test_enable = cmd->state.rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE;
4295 bool depth_write = tu6_writes_depth(cmd, depth_test_enable);
4296 bool stencil_write = tu6_writes_stencil(cmd);
4297
4298 if ((cmd->state.pipeline->lrz.fs_has_kill ||
4299 cmd->state.pipeline->subpass_feedback_loop_ds) &&
4300 (depth_write || stencil_write)) {
4301 zmode = (cmd->state.lrz.valid && cmd->state.lrz.enabled)
4302 ? A6XX_EARLY_LRZ_LATE_Z
4303 : A6XX_LATE_Z;
4304 }
4305
4306 if (cmd->state.pipeline->lrz.force_late_z || !depth_test_enable)
4307 zmode = A6XX_LATE_Z;
4308
4309 /* User defined early tests take precedence above all else */
4310 if (cmd->state.pipeline->lrz.early_fragment_tests)
4311 zmode = A6XX_EARLY_Z;
4312
4313 tu_cs_emit_pkt4(cs, REG_A6XX_GRAS_SU_DEPTH_PLANE_CNTL, 1);
4314 tu_cs_emit(cs, A6XX_GRAS_SU_DEPTH_PLANE_CNTL_Z_MODE(zmode));
4315
4316 tu_cs_emit_pkt4(cs, REG_A6XX_RB_DEPTH_PLANE_CNTL, 1);
4317 tu_cs_emit(cs, A6XX_RB_DEPTH_PLANE_CNTL_Z_MODE(zmode));
4318 }
4319
4320 static void
tu6_emit_blend(struct tu_cs * cs,struct tu_cmd_buffer * cmd)4321 tu6_emit_blend(struct tu_cs *cs, struct tu_cmd_buffer *cmd)
4322 {
4323 struct tu_pipeline *pipeline = cmd->state.pipeline;
4324 uint32_t color_write_enable = cmd->state.pipeline_color_write_enable;
4325
4326 if (pipeline->dynamic_state_mask &
4327 BIT(TU_DYNAMIC_STATE_COLOR_WRITE_ENABLE))
4328 color_write_enable &= cmd->state.color_write_enable;
4329
4330 for (unsigned i = 0; i < pipeline->num_rts; i++) {
4331 tu_cs_emit_pkt4(cs, REG_A6XX_RB_MRT_CONTROL(i), 2);
4332 if (color_write_enable & BIT(i)) {
4333 tu_cs_emit(cs, cmd->state.rb_mrt_control[i] |
4334 ((cmd->state.logic_op_enabled ?
4335 cmd->state.rb_mrt_control_rop : 0) &
4336 ~pipeline->rb_mrt_control_mask));
4337 tu_cs_emit(cs, cmd->state.rb_mrt_blend_control[i]);
4338 } else {
4339 tu_cs_emit(cs, 0);
4340 tu_cs_emit(cs, 0);
4341 }
4342 }
4343
4344 uint32_t blend_enable_mask = color_write_enable;
4345 if (!(cmd->state.logic_op_enabled && cmd->state.rop_reads_dst))
4346 blend_enable_mask &= cmd->state.pipeline_blend_enable;
4347
4348 tu_cs_emit_pkt4(cs, REG_A6XX_SP_BLEND_CNTL, 1);
4349 tu_cs_emit(cs, cmd->state.sp_blend_cntl |
4350 (A6XX_SP_BLEND_CNTL_ENABLE_BLEND(blend_enable_mask) &
4351 ~pipeline->sp_blend_cntl_mask));
4352
4353 tu_cs_emit_pkt4(cs, REG_A6XX_RB_BLEND_CNTL, 1);
4354 tu_cs_emit(cs, cmd->state.rb_blend_cntl |
4355 (A6XX_RB_BLEND_CNTL_ENABLE_BLEND(blend_enable_mask) &
4356 ~pipeline->rb_blend_cntl_mask));
4357 }
4358
4359 static VkResult
tu6_draw_common(struct tu_cmd_buffer * cmd,struct tu_cs * cs,bool indexed,uint32_t draw_count)4360 tu6_draw_common(struct tu_cmd_buffer *cmd,
4361 struct tu_cs *cs,
4362 bool indexed,
4363 /* note: draw_count is 0 for indirect */
4364 uint32_t draw_count)
4365 {
4366 const struct tu_pipeline *pipeline = cmd->state.pipeline;
4367
4368 /* Fill draw stats for autotuner */
4369 cmd->state.rp.drawcall_count++;
4370
4371 cmd->state.rp.drawcall_bandwidth_per_sample_sum +=
4372 cmd->state.pipeline->color_bandwidth_per_sample;
4373
4374 /* add depth memory bandwidth cost */
4375 const uint32_t depth_bandwidth = cmd->state.pipeline->depth_cpp_per_sample;
4376 if (cmd->state.rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_WRITE_ENABLE)
4377 cmd->state.rp.drawcall_bandwidth_per_sample_sum += depth_bandwidth;
4378 if (cmd->state.rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE)
4379 cmd->state.rp.drawcall_bandwidth_per_sample_sum += depth_bandwidth;
4380
4381 /* add stencil memory bandwidth cost */
4382 const uint32_t stencil_bandwidth =
4383 cmd->state.pipeline->stencil_cpp_per_sample;
4384 if (cmd->state.rb_stencil_cntl & A6XX_RB_STENCIL_CONTROL_STENCIL_ENABLE)
4385 cmd->state.rp.drawcall_bandwidth_per_sample_sum += stencil_bandwidth * 2;
4386
4387 tu_emit_cache_flush_renderpass(cmd, cs);
4388
4389 bool primitive_restart_enabled = pipeline->ia.primitive_restart;
4390 if (pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE))
4391 primitive_restart_enabled = cmd->state.primitive_restart_enable;
4392
4393 tu_cs_emit_regs(cs, A6XX_PC_PRIMITIVE_CNTL_0(
4394 .primitive_restart =
4395 primitive_restart_enabled && indexed,
4396 .provoking_vtx_last = pipeline->provoking_vertex_last,
4397 .tess_upper_left_domain_origin =
4398 pipeline->tess.upper_left_domain_origin));
4399
4400 /* Early exit if there is nothing to emit, saves CPU cycles */
4401 if (!(cmd->state.dirty & ~TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD))
4402 return VK_SUCCESS;
4403
4404 bool dirty_lrz =
4405 cmd->state.dirty & (TU_CMD_DIRTY_LRZ | TU_CMD_DIRTY_RB_DEPTH_CNTL |
4406 TU_CMD_DIRTY_RB_STENCIL_CNTL | TU_CMD_DIRTY_BLEND);
4407
4408 if (dirty_lrz) {
4409 struct tu_cs cs;
4410 uint32_t size = cmd->device->physical_device->info->a6xx.lrz_track_quirk ? 10 : 8;
4411
4412 cmd->state.lrz_and_depth_plane_state =
4413 tu_cs_draw_state(&cmd->sub_cs, &cs, size);
4414 tu6_emit_lrz(cmd, &cs);
4415 tu6_build_depth_plane_z_mode(cmd, &cs);
4416 }
4417
4418 if (cmd->state.dirty & TU_CMD_DIRTY_RASTERIZER_DISCARD) {
4419 struct tu_cs cs = tu_cmd_dynamic_state(cmd, TU_DYNAMIC_STATE_RASTERIZER_DISCARD, 4);
4420 tu_cs_emit_regs(&cs, A6XX_PC_RASTER_CNTL(.dword = cmd->state.pc_raster_cntl));
4421 tu_cs_emit_regs(&cs, A6XX_VPC_UNKNOWN_9107(.dword = cmd->state.vpc_unknown_9107));
4422 }
4423
4424 if (cmd->state.dirty & TU_CMD_DIRTY_GRAS_SU_CNTL) {
4425 struct tu_cs cs = tu_cmd_dynamic_state(cmd, TU_DYNAMIC_STATE_GRAS_SU_CNTL, 2);
4426 tu_cs_emit_regs(&cs, A6XX_GRAS_SU_CNTL(.dword = cmd->state.gras_su_cntl));
4427 }
4428
4429 if (cmd->state.dirty & TU_CMD_DIRTY_RB_DEPTH_CNTL) {
4430 struct tu_cs cs = tu_cmd_dynamic_state(cmd, TU_DYNAMIC_STATE_RB_DEPTH_CNTL, 2);
4431 uint32_t rb_depth_cntl = cmd->state.rb_depth_cntl;
4432
4433 if ((rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE) ||
4434 (rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_BOUNDS_ENABLE))
4435 rb_depth_cntl |= A6XX_RB_DEPTH_CNTL_Z_READ_ENABLE;
4436
4437 if ((rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_BOUNDS_ENABLE) &&
4438 !(rb_depth_cntl & A6XX_RB_DEPTH_CNTL_Z_TEST_ENABLE))
4439 tu6_apply_depth_bounds_workaround(cmd->device, &rb_depth_cntl);
4440
4441 if (pipeline->rb_depth_cntl_disable)
4442 rb_depth_cntl = 0;
4443
4444 tu_cs_emit_regs(&cs, A6XX_RB_DEPTH_CNTL(.dword = rb_depth_cntl));
4445 }
4446
4447 if (cmd->state.dirty & TU_CMD_DIRTY_RB_STENCIL_CNTL) {
4448 struct tu_cs cs = tu_cmd_dynamic_state(cmd, TU_DYNAMIC_STATE_RB_STENCIL_CNTL, 2);
4449 tu_cs_emit_regs(&cs, A6XX_RB_STENCIL_CONTROL(.dword = cmd->state.rb_stencil_cntl));
4450 }
4451
4452 if (cmd->state.dirty & TU_CMD_DIRTY_SHADER_CONSTS)
4453 cmd->state.shader_const = tu6_emit_consts(cmd, pipeline, false);
4454
4455 if (cmd->state.dirty & TU_CMD_DIRTY_VIEWPORTS) {
4456 struct tu_cs cs = tu_cmd_dynamic_state(cmd, VK_DYNAMIC_STATE_VIEWPORT, 8 + 10 * cmd->state.max_viewport);
4457 tu6_emit_viewport(&cs, cmd->state.viewport, cmd->state.max_viewport,
4458 pipeline->z_negative_one_to_one);
4459 }
4460
4461 if (cmd->state.dirty & TU_CMD_DIRTY_BLEND) {
4462 struct tu_cs cs = tu_cmd_dynamic_state(cmd, TU_DYNAMIC_STATE_BLEND,
4463 4 + 3 * cmd->state.pipeline->num_rts);
4464 tu6_emit_blend(&cs, cmd);
4465 }
4466
4467 /* for the first draw in a renderpass, re-emit all the draw states
4468 *
4469 * and if a draw-state disabling path (CmdClearAttachments 3D fallback) was
4470 * used, then draw states must be re-emitted. note however this only happens
4471 * in the sysmem path, so this can be skipped this for the gmem path (TODO)
4472 *
4473 * the two input attachment states are excluded because secondary command
4474 * buffer doesn't have a state ib to restore it, and not re-emitting them
4475 * is OK since CmdClearAttachments won't disable/overwrite them
4476 */
4477 if (cmd->state.dirty & TU_CMD_DIRTY_DRAW_STATE) {
4478 tu_cs_emit_pkt7(cs, CP_SET_DRAW_STATE, 3 * (TU_DRAW_STATE_COUNT - 2));
4479
4480 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PROGRAM_CONFIG, pipeline->program.config_state);
4481 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PROGRAM, pipeline->program.state);
4482 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PROGRAM_BINNING, pipeline->program.binning_state);
4483 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VI, pipeline->vi.state);
4484 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VI_BINNING, pipeline->vi.binning_state);
4485 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_RAST, pipeline->rast_state);
4486 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PRIM_MODE_SYSMEM, pipeline->prim_order_state_sysmem);
4487 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_PRIM_MODE_GMEM, pipeline->prim_order_state_gmem);
4488 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_CONST, cmd->state.shader_const);
4489 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DESC_SETS, cmd->state.desc_sets);
4490 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DESC_SETS_LOAD, pipeline->load_state);
4491 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VB, cmd->state.vertex_buffers);
4492 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VS_PARAMS, cmd->state.vs_params);
4493 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_LRZ_AND_DEPTH_PLANE, cmd->state.lrz_and_depth_plane_state);
4494
4495 for (uint32_t i = 0; i < ARRAY_SIZE(cmd->state.dynamic_state); i++) {
4496 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DYNAMIC + i,
4497 ((pipeline->dynamic_state_mask & BIT(i)) ?
4498 cmd->state.dynamic_state[i] :
4499 pipeline->dynamic_state[i]));
4500 }
4501 } else {
4502 /* emit draw states that were just updated
4503 * note we eventually don't want to have to emit anything here
4504 */
4505 bool emit_binding_stride = false, emit_blend = false;
4506 uint32_t draw_state_count =
4507 ((cmd->state.dirty & TU_CMD_DIRTY_SHADER_CONSTS) ? 1 : 0) +
4508 ((cmd->state.dirty & TU_CMD_DIRTY_DESC_SETS_LOAD) ? 1 : 0) +
4509 ((cmd->state.dirty & TU_CMD_DIRTY_VERTEX_BUFFERS) ? 1 : 0) +
4510 ((cmd->state.dirty & TU_CMD_DIRTY_VS_PARAMS) ? 1 : 0) +
4511 (dirty_lrz ? 1 : 0);
4512
4513 if ((cmd->state.dirty & TU_CMD_DIRTY_VB_STRIDE) &&
4514 (pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_VB_STRIDE))) {
4515 emit_binding_stride = true;
4516 draw_state_count += 1;
4517 }
4518
4519 if ((cmd->state.dirty & TU_CMD_DIRTY_BLEND) &&
4520 (pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_BLEND))) {
4521 emit_blend = true;
4522 draw_state_count += 1;
4523 }
4524
4525 if (draw_state_count > 0)
4526 tu_cs_emit_pkt7(cs, CP_SET_DRAW_STATE, 3 * draw_state_count);
4527
4528 if (cmd->state.dirty & TU_CMD_DIRTY_SHADER_CONSTS)
4529 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_CONST, cmd->state.shader_const);
4530 if (cmd->state.dirty & TU_CMD_DIRTY_DESC_SETS_LOAD)
4531 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DESC_SETS_LOAD, pipeline->load_state);
4532 if (cmd->state.dirty & TU_CMD_DIRTY_VERTEX_BUFFERS)
4533 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VB, cmd->state.vertex_buffers);
4534 if (emit_binding_stride) {
4535 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DYNAMIC + TU_DYNAMIC_STATE_VB_STRIDE,
4536 cmd->state.dynamic_state[TU_DYNAMIC_STATE_VB_STRIDE]);
4537 }
4538 if (emit_blend) {
4539 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_DYNAMIC + TU_DYNAMIC_STATE_BLEND,
4540 cmd->state.dynamic_state[TU_DYNAMIC_STATE_BLEND]);
4541 }
4542 if (cmd->state.dirty & TU_CMD_DIRTY_VS_PARAMS)
4543 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_VS_PARAMS, cmd->state.vs_params);
4544
4545 if (dirty_lrz) {
4546 tu_cs_emit_draw_state(cs, TU_DRAW_STATE_LRZ_AND_DEPTH_PLANE, cmd->state.lrz_and_depth_plane_state);
4547 }
4548 }
4549
4550 tu_cs_sanity_check(cs);
4551
4552 /* There are too many graphics dirty bits to list here, so just list the
4553 * bits to preserve instead. The only things not emitted here are
4554 * compute-related state.
4555 */
4556 cmd->state.dirty &= TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD;
4557 return VK_SUCCESS;
4558 }
4559
4560 static uint32_t
tu_draw_initiator(struct tu_cmd_buffer * cmd,enum pc_di_src_sel src_sel)4561 tu_draw_initiator(struct tu_cmd_buffer *cmd, enum pc_di_src_sel src_sel)
4562 {
4563 const struct tu_pipeline *pipeline = cmd->state.pipeline;
4564 enum pc_di_primtype primtype = pipeline->ia.primtype;
4565
4566 if (pipeline->dynamic_state_mask & BIT(TU_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY)) {
4567 if (primtype < DI_PT_PATCHES0) {
4568 /* If tesselation used, only VK_PRIMITIVE_TOPOLOGY_PATCH_LIST can be
4569 * set via vkCmdSetPrimitiveTopology, but primtype is already
4570 * calculated at the pipeline creation based on control points
4571 * for each patch.
4572 *
4573 * Just use the primtype as is for the case.
4574 */
4575 primtype = cmd->state.primtype;
4576 }
4577 }
4578
4579 uint32_t initiator =
4580 CP_DRAW_INDX_OFFSET_0_PRIM_TYPE(primtype) |
4581 CP_DRAW_INDX_OFFSET_0_SOURCE_SELECT(src_sel) |
4582 CP_DRAW_INDX_OFFSET_0_INDEX_SIZE(cmd->state.index_size) |
4583 CP_DRAW_INDX_OFFSET_0_VIS_CULL(USE_VISIBILITY);
4584
4585 if (pipeline->active_stages & VK_SHADER_STAGE_GEOMETRY_BIT)
4586 initiator |= CP_DRAW_INDX_OFFSET_0_GS_ENABLE;
4587
4588 switch (pipeline->tess.patch_type) {
4589 case IR3_TESS_TRIANGLES:
4590 initiator |= CP_DRAW_INDX_OFFSET_0_PATCH_TYPE(TESS_TRIANGLES) |
4591 CP_DRAW_INDX_OFFSET_0_TESS_ENABLE;
4592 break;
4593 case IR3_TESS_ISOLINES:
4594 initiator |= CP_DRAW_INDX_OFFSET_0_PATCH_TYPE(TESS_ISOLINES) |
4595 CP_DRAW_INDX_OFFSET_0_TESS_ENABLE;
4596 break;
4597 case IR3_TESS_NONE:
4598 initiator |= CP_DRAW_INDX_OFFSET_0_PATCH_TYPE(TESS_QUADS);
4599 break;
4600 case IR3_TESS_QUADS:
4601 initiator |= CP_DRAW_INDX_OFFSET_0_PATCH_TYPE(TESS_QUADS) |
4602 CP_DRAW_INDX_OFFSET_0_TESS_ENABLE;
4603 break;
4604 }
4605 return initiator;
4606 }
4607
4608
4609 static uint32_t
vs_params_offset(struct tu_cmd_buffer * cmd)4610 vs_params_offset(struct tu_cmd_buffer *cmd)
4611 {
4612 const struct tu_program_descriptor_linkage *link =
4613 &cmd->state.pipeline->program.link[MESA_SHADER_VERTEX];
4614 const struct ir3_const_state *const_state = &link->const_state;
4615
4616 if (const_state->offsets.driver_param >= link->constlen)
4617 return 0;
4618
4619 /* this layout is required by CP_DRAW_INDIRECT_MULTI */
4620 STATIC_ASSERT(IR3_DP_DRAWID == 0);
4621 STATIC_ASSERT(IR3_DP_VTXID_BASE == 1);
4622 STATIC_ASSERT(IR3_DP_INSTID_BASE == 2);
4623
4624 /* 0 means disabled for CP_DRAW_INDIRECT_MULTI */
4625 assert(const_state->offsets.driver_param != 0);
4626
4627 return const_state->offsets.driver_param;
4628 }
4629
4630 static void
tu6_emit_empty_vs_params(struct tu_cmd_buffer * cmd)4631 tu6_emit_empty_vs_params(struct tu_cmd_buffer *cmd)
4632 {
4633 if (cmd->state.vs_params.iova) {
4634 cmd->state.vs_params = (struct tu_draw_state) {};
4635 cmd->state.dirty |= TU_CMD_DIRTY_VS_PARAMS;
4636 }
4637 }
4638
4639 static void
tu6_emit_vs_params(struct tu_cmd_buffer * cmd,uint32_t vertex_offset,uint32_t first_instance)4640 tu6_emit_vs_params(struct tu_cmd_buffer *cmd,
4641 uint32_t vertex_offset,
4642 uint32_t first_instance)
4643 {
4644 /* Beside re-emitting params when they are changed, we should re-emit
4645 * them after constants are invalidated via HLSQ_INVALIDATE_CMD.
4646 */
4647 if (!(cmd->state.dirty & (TU_CMD_DIRTY_DRAW_STATE | TU_CMD_DIRTY_VS_PARAMS)) &&
4648 vertex_offset == cmd->state.last_vs_params.vertex_offset &&
4649 first_instance == cmd->state.last_vs_params.first_instance) {
4650 return;
4651 }
4652
4653 uint32_t offset = vs_params_offset(cmd);
4654
4655 struct tu_cs cs;
4656 VkResult result = tu_cs_begin_sub_stream(&cmd->sub_cs, 3 + (offset ? 8 : 0), &cs);
4657 if (result != VK_SUCCESS) {
4658 cmd->record_result = result;
4659 return;
4660 }
4661
4662 tu_cs_emit_regs(&cs,
4663 A6XX_VFD_INDEX_OFFSET(vertex_offset),
4664 A6XX_VFD_INSTANCE_START_OFFSET(first_instance));
4665
4666 if (offset) {
4667 tu_cs_emit_pkt7(&cs, CP_LOAD_STATE6_GEOM, 3 + 4);
4668 tu_cs_emit(&cs, CP_LOAD_STATE6_0_DST_OFF(offset) |
4669 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
4670 CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
4671 CP_LOAD_STATE6_0_STATE_BLOCK(SB6_VS_SHADER) |
4672 CP_LOAD_STATE6_0_NUM_UNIT(1));
4673 tu_cs_emit(&cs, 0);
4674 tu_cs_emit(&cs, 0);
4675
4676 tu_cs_emit(&cs, 0);
4677 tu_cs_emit(&cs, vertex_offset);
4678 tu_cs_emit(&cs, first_instance);
4679 tu_cs_emit(&cs, 0);
4680 }
4681
4682 cmd->state.last_vs_params.vertex_offset = vertex_offset;
4683 cmd->state.last_vs_params.first_instance = first_instance;
4684
4685 struct tu_cs_entry entry = tu_cs_end_sub_stream(&cmd->sub_cs, &cs);
4686 cmd->state.vs_params = (struct tu_draw_state) {entry.bo->iova + entry.offset, entry.size / 4};
4687
4688 cmd->state.dirty |= TU_CMD_DIRTY_VS_PARAMS;
4689 }
4690
4691 VKAPI_ATTR void VKAPI_CALL
tu_CmdDraw(VkCommandBuffer commandBuffer,uint32_t vertexCount,uint32_t instanceCount,uint32_t firstVertex,uint32_t firstInstance)4692 tu_CmdDraw(VkCommandBuffer commandBuffer,
4693 uint32_t vertexCount,
4694 uint32_t instanceCount,
4695 uint32_t firstVertex,
4696 uint32_t firstInstance)
4697 {
4698 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4699 struct tu_cs *cs = &cmd->draw_cs;
4700
4701 tu6_emit_vs_params(cmd, firstVertex, firstInstance);
4702
4703 tu6_draw_common(cmd, cs, false, vertexCount);
4704
4705 tu_cs_emit_pkt7(cs, CP_DRAW_INDX_OFFSET, 3);
4706 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_AUTO_INDEX));
4707 tu_cs_emit(cs, instanceCount);
4708 tu_cs_emit(cs, vertexCount);
4709 }
4710
4711 VKAPI_ATTR void VKAPI_CALL
tu_CmdDrawIndexed(VkCommandBuffer commandBuffer,uint32_t indexCount,uint32_t instanceCount,uint32_t firstIndex,int32_t vertexOffset,uint32_t firstInstance)4712 tu_CmdDrawIndexed(VkCommandBuffer commandBuffer,
4713 uint32_t indexCount,
4714 uint32_t instanceCount,
4715 uint32_t firstIndex,
4716 int32_t vertexOffset,
4717 uint32_t firstInstance)
4718 {
4719 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4720 struct tu_cs *cs = &cmd->draw_cs;
4721
4722 tu6_emit_vs_params(cmd, vertexOffset, firstInstance);
4723
4724 tu6_draw_common(cmd, cs, true, indexCount);
4725
4726 tu_cs_emit_pkt7(cs, CP_DRAW_INDX_OFFSET, 7);
4727 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_DMA));
4728 tu_cs_emit(cs, instanceCount);
4729 tu_cs_emit(cs, indexCount);
4730 tu_cs_emit(cs, firstIndex);
4731 tu_cs_emit_qw(cs, cmd->state.index_va);
4732 tu_cs_emit(cs, cmd->state.max_index_count);
4733 }
4734
4735 /* Various firmware bugs/inconsistencies mean that some indirect draw opcodes
4736 * do not wait for WFI's to complete before executing. Add a WAIT_FOR_ME if
4737 * pending for these opcodes. This may result in a few extra WAIT_FOR_ME's
4738 * with these opcodes, but the alternative would add unnecessary WAIT_FOR_ME's
4739 * before draw opcodes that don't need it.
4740 */
4741 static void
draw_wfm(struct tu_cmd_buffer * cmd)4742 draw_wfm(struct tu_cmd_buffer *cmd)
4743 {
4744 cmd->state.renderpass_cache.flush_bits |=
4745 cmd->state.renderpass_cache.pending_flush_bits & TU_CMD_FLAG_WAIT_FOR_ME;
4746 cmd->state.renderpass_cache.pending_flush_bits &= ~TU_CMD_FLAG_WAIT_FOR_ME;
4747 }
4748
4749 VKAPI_ATTR void VKAPI_CALL
tu_CmdDrawIndirect(VkCommandBuffer commandBuffer,VkBuffer _buffer,VkDeviceSize offset,uint32_t drawCount,uint32_t stride)4750 tu_CmdDrawIndirect(VkCommandBuffer commandBuffer,
4751 VkBuffer _buffer,
4752 VkDeviceSize offset,
4753 uint32_t drawCount,
4754 uint32_t stride)
4755 {
4756 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4757 TU_FROM_HANDLE(tu_buffer, buf, _buffer);
4758 struct tu_cs *cs = &cmd->draw_cs;
4759
4760 tu6_emit_empty_vs_params(cmd);
4761
4762 if (cmd->device->physical_device->info->a6xx.indirect_draw_wfm_quirk)
4763 draw_wfm(cmd);
4764
4765 tu6_draw_common(cmd, cs, false, 0);
4766
4767 tu_cs_emit_pkt7(cs, CP_DRAW_INDIRECT_MULTI, 6);
4768 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_AUTO_INDEX));
4769 tu_cs_emit(cs, A6XX_CP_DRAW_INDIRECT_MULTI_1_OPCODE(INDIRECT_OP_NORMAL) |
4770 A6XX_CP_DRAW_INDIRECT_MULTI_1_DST_OFF(vs_params_offset(cmd)));
4771 tu_cs_emit(cs, drawCount);
4772 tu_cs_emit_qw(cs, buf->iova + offset);
4773 tu_cs_emit(cs, stride);
4774 }
4775
4776 VKAPI_ATTR void VKAPI_CALL
tu_CmdDrawIndexedIndirect(VkCommandBuffer commandBuffer,VkBuffer _buffer,VkDeviceSize offset,uint32_t drawCount,uint32_t stride)4777 tu_CmdDrawIndexedIndirect(VkCommandBuffer commandBuffer,
4778 VkBuffer _buffer,
4779 VkDeviceSize offset,
4780 uint32_t drawCount,
4781 uint32_t stride)
4782 {
4783 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4784 TU_FROM_HANDLE(tu_buffer, buf, _buffer);
4785 struct tu_cs *cs = &cmd->draw_cs;
4786
4787 tu6_emit_empty_vs_params(cmd);
4788
4789 if (cmd->device->physical_device->info->a6xx.indirect_draw_wfm_quirk)
4790 draw_wfm(cmd);
4791
4792 tu6_draw_common(cmd, cs, true, 0);
4793
4794 tu_cs_emit_pkt7(cs, CP_DRAW_INDIRECT_MULTI, 9);
4795 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_DMA));
4796 tu_cs_emit(cs, A6XX_CP_DRAW_INDIRECT_MULTI_1_OPCODE(INDIRECT_OP_INDEXED) |
4797 A6XX_CP_DRAW_INDIRECT_MULTI_1_DST_OFF(vs_params_offset(cmd)));
4798 tu_cs_emit(cs, drawCount);
4799 tu_cs_emit_qw(cs, cmd->state.index_va);
4800 tu_cs_emit(cs, cmd->state.max_index_count);
4801 tu_cs_emit_qw(cs, buf->iova + offset);
4802 tu_cs_emit(cs, stride);
4803 }
4804
4805 VKAPI_ATTR void VKAPI_CALL
tu_CmdDrawIndirectCount(VkCommandBuffer commandBuffer,VkBuffer _buffer,VkDeviceSize offset,VkBuffer countBuffer,VkDeviceSize countBufferOffset,uint32_t drawCount,uint32_t stride)4806 tu_CmdDrawIndirectCount(VkCommandBuffer commandBuffer,
4807 VkBuffer _buffer,
4808 VkDeviceSize offset,
4809 VkBuffer countBuffer,
4810 VkDeviceSize countBufferOffset,
4811 uint32_t drawCount,
4812 uint32_t stride)
4813 {
4814 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4815 TU_FROM_HANDLE(tu_buffer, buf, _buffer);
4816 TU_FROM_HANDLE(tu_buffer, count_buf, countBuffer);
4817 struct tu_cs *cs = &cmd->draw_cs;
4818
4819 tu6_emit_empty_vs_params(cmd);
4820
4821 /* It turns out that the firmware we have for a650 only partially fixed the
4822 * problem with CP_DRAW_INDIRECT_MULTI not waiting for WFI's to complete
4823 * before reading indirect parameters. It waits for WFI's before reading
4824 * the draw parameters, but after reading the indirect count :(.
4825 */
4826 draw_wfm(cmd);
4827
4828 tu6_draw_common(cmd, cs, false, 0);
4829
4830 tu_cs_emit_pkt7(cs, CP_DRAW_INDIRECT_MULTI, 8);
4831 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_AUTO_INDEX));
4832 tu_cs_emit(cs, A6XX_CP_DRAW_INDIRECT_MULTI_1_OPCODE(INDIRECT_OP_INDIRECT_COUNT) |
4833 A6XX_CP_DRAW_INDIRECT_MULTI_1_DST_OFF(vs_params_offset(cmd)));
4834 tu_cs_emit(cs, drawCount);
4835 tu_cs_emit_qw(cs, buf->iova + offset);
4836 tu_cs_emit_qw(cs, count_buf->iova + countBufferOffset);
4837 tu_cs_emit(cs, stride);
4838 }
4839
4840 VKAPI_ATTR void VKAPI_CALL
tu_CmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer,VkBuffer _buffer,VkDeviceSize offset,VkBuffer countBuffer,VkDeviceSize countBufferOffset,uint32_t drawCount,uint32_t stride)4841 tu_CmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer,
4842 VkBuffer _buffer,
4843 VkDeviceSize offset,
4844 VkBuffer countBuffer,
4845 VkDeviceSize countBufferOffset,
4846 uint32_t drawCount,
4847 uint32_t stride)
4848 {
4849 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4850 TU_FROM_HANDLE(tu_buffer, buf, _buffer);
4851 TU_FROM_HANDLE(tu_buffer, count_buf, countBuffer);
4852 struct tu_cs *cs = &cmd->draw_cs;
4853
4854 tu6_emit_empty_vs_params(cmd);
4855
4856 draw_wfm(cmd);
4857
4858 tu6_draw_common(cmd, cs, true, 0);
4859
4860 tu_cs_emit_pkt7(cs, CP_DRAW_INDIRECT_MULTI, 11);
4861 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_DMA));
4862 tu_cs_emit(cs, A6XX_CP_DRAW_INDIRECT_MULTI_1_OPCODE(INDIRECT_OP_INDIRECT_COUNT_INDEXED) |
4863 A6XX_CP_DRAW_INDIRECT_MULTI_1_DST_OFF(vs_params_offset(cmd)));
4864 tu_cs_emit(cs, drawCount);
4865 tu_cs_emit_qw(cs, cmd->state.index_va);
4866 tu_cs_emit(cs, cmd->state.max_index_count);
4867 tu_cs_emit_qw(cs, buf->iova + offset);
4868 tu_cs_emit_qw(cs, count_buf->iova + countBufferOffset);
4869 tu_cs_emit(cs, stride);
4870 }
4871
4872 VKAPI_ATTR void VKAPI_CALL
tu_CmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer,uint32_t instanceCount,uint32_t firstInstance,VkBuffer _counterBuffer,VkDeviceSize counterBufferOffset,uint32_t counterOffset,uint32_t vertexStride)4873 tu_CmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer,
4874 uint32_t instanceCount,
4875 uint32_t firstInstance,
4876 VkBuffer _counterBuffer,
4877 VkDeviceSize counterBufferOffset,
4878 uint32_t counterOffset,
4879 uint32_t vertexStride)
4880 {
4881 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
4882 TU_FROM_HANDLE(tu_buffer, buf, _counterBuffer);
4883 struct tu_cs *cs = &cmd->draw_cs;
4884
4885 /* All known firmware versions do not wait for WFI's with CP_DRAW_AUTO.
4886 * Plus, for the common case where the counter buffer is written by
4887 * vkCmdEndTransformFeedback, we need to wait for the CP_WAIT_MEM_WRITES to
4888 * complete which means we need a WAIT_FOR_ME anyway.
4889 */
4890 draw_wfm(cmd);
4891
4892 tu6_emit_vs_params(cmd, 0, firstInstance);
4893
4894 tu6_draw_common(cmd, cs, false, 0);
4895
4896 tu_cs_emit_pkt7(cs, CP_DRAW_AUTO, 6);
4897 tu_cs_emit(cs, tu_draw_initiator(cmd, DI_SRC_SEL_AUTO_XFB));
4898 tu_cs_emit(cs, instanceCount);
4899 tu_cs_emit_qw(cs, buf->iova + counterBufferOffset);
4900 tu_cs_emit(cs, counterOffset);
4901 tu_cs_emit(cs, vertexStride);
4902 }
4903
4904 struct tu_dispatch_info
4905 {
4906 /**
4907 * Determine the layout of the grid (in block units) to be used.
4908 */
4909 uint32_t blocks[3];
4910
4911 /**
4912 * A starting offset for the grid. If unaligned is set, the offset
4913 * must still be aligned.
4914 */
4915 uint32_t offsets[3];
4916 /**
4917 * Whether it's an unaligned compute dispatch.
4918 */
4919 bool unaligned;
4920
4921 /**
4922 * Indirect compute parameters resource.
4923 */
4924 struct tu_buffer *indirect;
4925 uint64_t indirect_offset;
4926 };
4927
4928 static void
tu_emit_compute_driver_params(struct tu_cmd_buffer * cmd,struct tu_cs * cs,struct tu_pipeline * pipeline,const struct tu_dispatch_info * info)4929 tu_emit_compute_driver_params(struct tu_cmd_buffer *cmd,
4930 struct tu_cs *cs, struct tu_pipeline *pipeline,
4931 const struct tu_dispatch_info *info)
4932 {
4933 gl_shader_stage type = MESA_SHADER_COMPUTE;
4934 const struct tu_program_descriptor_linkage *link =
4935 &pipeline->program.link[type];
4936 const struct ir3_const_state *const_state = &link->const_state;
4937 uint32_t offset = const_state->offsets.driver_param;
4938 unsigned subgroup_size = pipeline->compute.subgroup_size;
4939 unsigned subgroup_shift = util_logbase2(subgroup_size);
4940
4941 if (link->constlen <= offset)
4942 return;
4943
4944 uint32_t num_consts = MIN2(const_state->num_driver_params,
4945 (link->constlen - offset) * 4);
4946
4947 if (!info->indirect) {
4948 uint32_t driver_params[12] = {
4949 [IR3_DP_NUM_WORK_GROUPS_X] = info->blocks[0],
4950 [IR3_DP_NUM_WORK_GROUPS_Y] = info->blocks[1],
4951 [IR3_DP_NUM_WORK_GROUPS_Z] = info->blocks[2],
4952 [IR3_DP_BASE_GROUP_X] = info->offsets[0],
4953 [IR3_DP_BASE_GROUP_Y] = info->offsets[1],
4954 [IR3_DP_BASE_GROUP_Z] = info->offsets[2],
4955 [IR3_DP_CS_SUBGROUP_SIZE] = subgroup_size,
4956 [IR3_DP_SUBGROUP_ID_SHIFT] = subgroup_shift,
4957 };
4958
4959 assert(num_consts <= ARRAY_SIZE(driver_params));
4960
4961 /* push constants */
4962 tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 3 + num_consts);
4963 tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset) |
4964 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
4965 CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
4966 CP_LOAD_STATE6_0_STATE_BLOCK(tu6_stage2shadersb(type)) |
4967 CP_LOAD_STATE6_0_NUM_UNIT(num_consts / 4));
4968 tu_cs_emit(cs, 0);
4969 tu_cs_emit(cs, 0);
4970 uint32_t i;
4971 for (i = 0; i < num_consts; i++)
4972 tu_cs_emit(cs, driver_params[i]);
4973 } else if (!(info->indirect_offset & 0xf)) {
4974 tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 3);
4975 tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset) |
4976 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
4977 CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
4978 CP_LOAD_STATE6_0_STATE_BLOCK(tu6_stage2shadersb(type)) |
4979 CP_LOAD_STATE6_0_NUM_UNIT(1));
4980 tu_cs_emit_qw(cs, info->indirect->iova + info->indirect_offset);
4981 } else {
4982 /* Vulkan guarantees only 4 byte alignment for indirect_offset.
4983 * However, CP_LOAD_STATE.EXT_SRC_ADDR needs 16 byte alignment.
4984 */
4985
4986 uint64_t indirect_iova = info->indirect->iova + info->indirect_offset;
4987
4988 for (uint32_t i = 0; i < 3; i++) {
4989 tu_cs_emit_pkt7(cs, CP_MEM_TO_MEM, 5);
4990 tu_cs_emit(cs, 0);
4991 tu_cs_emit_qw(cs, global_iova(cmd, cs_indirect_xyz[i]));
4992 tu_cs_emit_qw(cs, indirect_iova + i * 4);
4993 }
4994
4995 tu_cs_emit_pkt7(cs, CP_WAIT_MEM_WRITES, 0);
4996 tu6_emit_event_write(cmd, cs, CACHE_INVALIDATE);
4997
4998 tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 3);
4999 tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset) |
5000 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
5001 CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
5002 CP_LOAD_STATE6_0_STATE_BLOCK(tu6_stage2shadersb(type)) |
5003 CP_LOAD_STATE6_0_NUM_UNIT(1));
5004 tu_cs_emit_qw(cs, global_iova(cmd, cs_indirect_xyz[0]));
5005 }
5006
5007 /* Fill out IR3_DP_CS_SUBGROUP_SIZE and IR3_DP_SUBGROUP_ID_SHIFT for
5008 * indirect dispatch.
5009 */
5010 if (info->indirect && num_consts > IR3_DP_BASE_GROUP_X) {
5011 tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 7);
5012 tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset + (IR3_DP_BASE_GROUP_X / 4)) |
5013 CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
5014 CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
5015 CP_LOAD_STATE6_0_STATE_BLOCK(tu6_stage2shadersb(type)) |
5016 CP_LOAD_STATE6_0_NUM_UNIT((num_consts - IR3_DP_BASE_GROUP_X) / 4));
5017 tu_cs_emit_qw(cs, 0);
5018 tu_cs_emit(cs, 0); /* BASE_GROUP_X */
5019 tu_cs_emit(cs, 0); /* BASE_GROUP_Y */
5020 tu_cs_emit(cs, 0); /* BASE_GROUP_Z */
5021 tu_cs_emit(cs, subgroup_size);
5022 if (num_consts > IR3_DP_LOCAL_GROUP_SIZE_X) {
5023 assert(num_consts == align(IR3_DP_SUBGROUP_ID_SHIFT, 4));
5024 tu_cs_emit(cs, 0); /* LOCAL_GROUP_SIZE_X */
5025 tu_cs_emit(cs, 0); /* LOCAL_GROUP_SIZE_Y */
5026 tu_cs_emit(cs, 0); /* LOCAL_GROUP_SIZE_Z */
5027 tu_cs_emit(cs, subgroup_shift);
5028 }
5029 }
5030 }
5031
5032 static void
tu_dispatch(struct tu_cmd_buffer * cmd,const struct tu_dispatch_info * info)5033 tu_dispatch(struct tu_cmd_buffer *cmd,
5034 const struct tu_dispatch_info *info)
5035 {
5036 if (!info->indirect &&
5037 (info->blocks[0] == 0 || info->blocks[1] == 0 || info->blocks[2] == 0))
5038 return;
5039
5040 struct tu_cs *cs = &cmd->cs;
5041 struct tu_pipeline *pipeline = cmd->state.compute_pipeline;
5042
5043 /* TODO: We could probably flush less if we add a compute_flush_bits
5044 * bitfield.
5045 */
5046 tu_emit_cache_flush(cmd, cs);
5047
5048 /* note: no reason to have this in a separate IB */
5049 tu_cs_emit_state_ib(cs, tu6_emit_consts(cmd, pipeline, true));
5050
5051 tu_emit_compute_driver_params(cmd, cs, pipeline, info);
5052
5053 if (cmd->state.dirty & TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD)
5054 tu_cs_emit_state_ib(cs, pipeline->load_state);
5055
5056 cmd->state.dirty &= ~TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD;
5057
5058 tu_cs_emit_pkt7(cs, CP_SET_MARKER, 1);
5059 tu_cs_emit(cs, A6XX_CP_SET_MARKER_0_MODE(RM6_COMPUTE));
5060
5061 const uint32_t *local_size = pipeline->compute.local_size;
5062 const uint32_t *num_groups = info->blocks;
5063 tu_cs_emit_regs(cs,
5064 A6XX_HLSQ_CS_NDRANGE_0(.kerneldim = 3,
5065 .localsizex = local_size[0] - 1,
5066 .localsizey = local_size[1] - 1,
5067 .localsizez = local_size[2] - 1),
5068 A6XX_HLSQ_CS_NDRANGE_1(.globalsize_x = local_size[0] * num_groups[0]),
5069 A6XX_HLSQ_CS_NDRANGE_2(.globaloff_x = 0),
5070 A6XX_HLSQ_CS_NDRANGE_3(.globalsize_y = local_size[1] * num_groups[1]),
5071 A6XX_HLSQ_CS_NDRANGE_4(.globaloff_y = 0),
5072 A6XX_HLSQ_CS_NDRANGE_5(.globalsize_z = local_size[2] * num_groups[2]),
5073 A6XX_HLSQ_CS_NDRANGE_6(.globaloff_z = 0));
5074
5075 tu_cs_emit_regs(cs,
5076 A6XX_HLSQ_CS_KERNEL_GROUP_X(1),
5077 A6XX_HLSQ_CS_KERNEL_GROUP_Y(1),
5078 A6XX_HLSQ_CS_KERNEL_GROUP_Z(1));
5079
5080 trace_start_compute(&cmd->trace, cs);
5081
5082 if (info->indirect) {
5083 uint64_t iova = info->indirect->iova + info->indirect_offset;
5084
5085 tu_cs_emit_pkt7(cs, CP_EXEC_CS_INDIRECT, 4);
5086 tu_cs_emit(cs, 0x00000000);
5087 tu_cs_emit_qw(cs, iova);
5088 tu_cs_emit(cs,
5089 A5XX_CP_EXEC_CS_INDIRECT_3_LOCALSIZEX(local_size[0] - 1) |
5090 A5XX_CP_EXEC_CS_INDIRECT_3_LOCALSIZEY(local_size[1] - 1) |
5091 A5XX_CP_EXEC_CS_INDIRECT_3_LOCALSIZEZ(local_size[2] - 1));
5092 } else {
5093 tu_cs_emit_pkt7(cs, CP_EXEC_CS, 4);
5094 tu_cs_emit(cs, 0x00000000);
5095 tu_cs_emit(cs, CP_EXEC_CS_1_NGROUPS_X(info->blocks[0]));
5096 tu_cs_emit(cs, CP_EXEC_CS_2_NGROUPS_Y(info->blocks[1]));
5097 tu_cs_emit(cs, CP_EXEC_CS_3_NGROUPS_Z(info->blocks[2]));
5098 }
5099
5100 trace_end_compute(&cmd->trace, cs,
5101 info->indirect != NULL,
5102 local_size[0], local_size[1], local_size[2],
5103 info->blocks[0], info->blocks[1], info->blocks[2]);
5104
5105 tu_cs_emit_wfi(cs);
5106 }
5107
5108 VKAPI_ATTR void VKAPI_CALL
tu_CmdDispatchBase(VkCommandBuffer commandBuffer,uint32_t base_x,uint32_t base_y,uint32_t base_z,uint32_t x,uint32_t y,uint32_t z)5109 tu_CmdDispatchBase(VkCommandBuffer commandBuffer,
5110 uint32_t base_x,
5111 uint32_t base_y,
5112 uint32_t base_z,
5113 uint32_t x,
5114 uint32_t y,
5115 uint32_t z)
5116 {
5117 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
5118 struct tu_dispatch_info info = {};
5119
5120 info.blocks[0] = x;
5121 info.blocks[1] = y;
5122 info.blocks[2] = z;
5123
5124 info.offsets[0] = base_x;
5125 info.offsets[1] = base_y;
5126 info.offsets[2] = base_z;
5127 tu_dispatch(cmd_buffer, &info);
5128 }
5129
5130 VKAPI_ATTR void VKAPI_CALL
tu_CmdDispatch(VkCommandBuffer commandBuffer,uint32_t x,uint32_t y,uint32_t z)5131 tu_CmdDispatch(VkCommandBuffer commandBuffer,
5132 uint32_t x,
5133 uint32_t y,
5134 uint32_t z)
5135 {
5136 tu_CmdDispatchBase(commandBuffer, 0, 0, 0, x, y, z);
5137 }
5138
5139 VKAPI_ATTR void VKAPI_CALL
tu_CmdDispatchIndirect(VkCommandBuffer commandBuffer,VkBuffer _buffer,VkDeviceSize offset)5140 tu_CmdDispatchIndirect(VkCommandBuffer commandBuffer,
5141 VkBuffer _buffer,
5142 VkDeviceSize offset)
5143 {
5144 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
5145 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
5146 struct tu_dispatch_info info = {};
5147
5148 info.indirect = buffer;
5149 info.indirect_offset = offset;
5150
5151 tu_dispatch(cmd_buffer, &info);
5152 }
5153
5154 VKAPI_ATTR void VKAPI_CALL
tu_CmdEndRenderPass2(VkCommandBuffer commandBuffer,const VkSubpassEndInfo * pSubpassEndInfo)5155 tu_CmdEndRenderPass2(VkCommandBuffer commandBuffer,
5156 const VkSubpassEndInfo *pSubpassEndInfo)
5157 {
5158 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
5159
5160 if (unlikely(cmd_buffer->device->instance->debug_flags & TU_DEBUG_DYNAMIC)) {
5161 vk_common_CmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
5162 return;
5163 }
5164
5165 cmd_buffer->trace_renderpass_end = u_trace_end_iterator(&cmd_buffer->trace);
5166
5167 tu_cs_end(&cmd_buffer->draw_cs);
5168 tu_cs_end(&cmd_buffer->draw_epilogue_cs);
5169 tu_cmd_render(cmd_buffer);
5170
5171 cmd_buffer->state.cache.pending_flush_bits |=
5172 cmd_buffer->state.renderpass_cache.pending_flush_bits;
5173 tu_subpass_barrier(cmd_buffer, &cmd_buffer->state.pass->end_barrier, true);
5174
5175 vk_free(&cmd_buffer->pool->vk.alloc, cmd_buffer->state.attachments);
5176
5177 tu_reset_render_pass(cmd_buffer);
5178 }
5179
5180 VKAPI_ATTR void VKAPI_CALL
tu_CmdEndRendering(VkCommandBuffer commandBuffer)5181 tu_CmdEndRendering(VkCommandBuffer commandBuffer)
5182 {
5183 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
5184
5185 cmd_buffer->trace_renderpass_end = u_trace_end_iterator(&cmd_buffer->trace);
5186
5187 if (cmd_buffer->state.suspending)
5188 cmd_buffer->state.suspended_pass.lrz = cmd_buffer->state.lrz;
5189
5190 if (!cmd_buffer->state.suspending) {
5191 tu_cs_end(&cmd_buffer->draw_cs);
5192 tu_cs_end(&cmd_buffer->draw_epilogue_cs);
5193
5194 if (cmd_buffer->state.suspend_resume == SR_IN_PRE_CHAIN) {
5195 tu_save_pre_chain(cmd_buffer);
5196 } else {
5197 tu_cmd_render(cmd_buffer);
5198 }
5199
5200 tu_reset_render_pass(cmd_buffer);
5201 }
5202
5203 if (cmd_buffer->state.resuming && !cmd_buffer->state.suspending) {
5204 /* exiting suspend/resume chain */
5205 switch (cmd_buffer->state.suspend_resume) {
5206 case SR_IN_CHAIN:
5207 cmd_buffer->state.suspend_resume = SR_NONE;
5208 break;
5209 case SR_IN_PRE_CHAIN:
5210 case SR_IN_CHAIN_AFTER_PRE_CHAIN:
5211 cmd_buffer->state.suspend_resume = SR_AFTER_PRE_CHAIN;
5212 break;
5213 default:
5214 unreachable("suspending render pass not followed by resuming pass");
5215 }
5216 }
5217 }
5218
5219 static void
tu_barrier(struct tu_cmd_buffer * cmd,const VkDependencyInfo * dep_info)5220 tu_barrier(struct tu_cmd_buffer *cmd,
5221 const VkDependencyInfo *dep_info)
5222 {
5223 VkPipelineStageFlags2 srcStage = 0;
5224 VkPipelineStageFlags2 dstStage = 0;
5225 enum tu_cmd_access_mask src_flags = 0;
5226 enum tu_cmd_access_mask dst_flags = 0;
5227
5228 /* Inside a renderpass, we don't know yet whether we'll be using sysmem
5229 * so we have to use the sysmem flushes.
5230 */
5231 bool gmem = cmd->state.ccu_state == TU_CMD_CCU_GMEM &&
5232 !cmd->state.pass;
5233
5234
5235 for (uint32_t i = 0; i < dep_info->memoryBarrierCount; i++) {
5236 VkPipelineStageFlags2 sanitized_src_stage =
5237 sanitize_src_stage(dep_info->pMemoryBarriers[i].srcStageMask);
5238 VkPipelineStageFlags2 sanitized_dst_stage =
5239 sanitize_dst_stage(dep_info->pMemoryBarriers[i].dstStageMask);
5240 src_flags |= vk2tu_access(dep_info->pMemoryBarriers[i].srcAccessMask,
5241 sanitized_src_stage, false, gmem);
5242 dst_flags |= vk2tu_access(dep_info->pMemoryBarriers[i].dstAccessMask,
5243 sanitized_dst_stage, false, gmem);
5244 srcStage |= sanitized_src_stage;
5245 dstStage |= sanitized_dst_stage;
5246 }
5247
5248 for (uint32_t i = 0; i < dep_info->bufferMemoryBarrierCount; i++) {
5249 VkPipelineStageFlags2 sanitized_src_stage =
5250 sanitize_src_stage(dep_info->pBufferMemoryBarriers[i].srcStageMask);
5251 VkPipelineStageFlags2 sanitized_dst_stage =
5252 sanitize_dst_stage(dep_info->pBufferMemoryBarriers[i].dstStageMask);
5253 src_flags |= vk2tu_access(dep_info->pBufferMemoryBarriers[i].srcAccessMask,
5254 sanitized_src_stage, false, gmem);
5255 dst_flags |= vk2tu_access(dep_info->pBufferMemoryBarriers[i].dstAccessMask,
5256 sanitized_dst_stage, false, gmem);
5257 srcStage |= sanitized_src_stage;
5258 dstStage |= sanitized_dst_stage;
5259 }
5260
5261 for (uint32_t i = 0; i < dep_info->imageMemoryBarrierCount; i++) {
5262 VkImageLayout old_layout = dep_info->pImageMemoryBarriers[i].oldLayout;
5263 if (old_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
5264 /* The underlying memory for this image may have been used earlier
5265 * within the same queue submission for a different image, which
5266 * means that there may be old, stale cache entries which are in the
5267 * "wrong" location, which could cause problems later after writing
5268 * to the image. We don't want these entries being flushed later and
5269 * overwriting the actual image, so we need to flush the CCU.
5270 */
5271 src_flags |= TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE;
5272 }
5273 VkPipelineStageFlags2 sanitized_src_stage =
5274 sanitize_src_stage(dep_info->pImageMemoryBarriers[i].srcStageMask);
5275 VkPipelineStageFlags2 sanitized_dst_stage =
5276 sanitize_dst_stage(dep_info->pImageMemoryBarriers[i].dstStageMask);
5277 src_flags |= vk2tu_access(dep_info->pImageMemoryBarriers[i].srcAccessMask,
5278 sanitized_src_stage, true, gmem);
5279 dst_flags |= vk2tu_access(dep_info->pImageMemoryBarriers[i].dstAccessMask,
5280 sanitized_dst_stage, true, gmem);
5281 srcStage |= sanitized_src_stage;
5282 dstStage |= sanitized_dst_stage;
5283 }
5284
5285 if (cmd->state.pass) {
5286 const VkPipelineStageFlags framebuffer_space_stages =
5287 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
5288 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
5289 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT |
5290 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
5291
5292 /* We cannot have non-by-region "fb-space to fb-space" barriers.
5293 *
5294 * From the Vulkan 1.2.185 spec, section 7.6.1 "Subpass Self-dependency":
5295 *
5296 * If the source and destination stage masks both include
5297 * framebuffer-space stages, then dependencyFlags must include
5298 * VK_DEPENDENCY_BY_REGION_BIT.
5299 * [...]
5300 * Each of the synchronization scopes and access scopes of a
5301 * vkCmdPipelineBarrier2 or vkCmdPipelineBarrier command inside
5302 * a render pass instance must be a subset of the scopes of one of
5303 * the self-dependencies for the current subpass.
5304 *
5305 * If the self-dependency has VK_DEPENDENCY_BY_REGION_BIT or
5306 * VK_DEPENDENCY_VIEW_LOCAL_BIT set, then so must the pipeline barrier.
5307 *
5308 * By-region barriers are ok for gmem. All other barriers would involve
5309 * vtx stages which are NOT ok for gmem rendering.
5310 * See dep_invalid_for_gmem().
5311 */
5312 if ((srcStage & ~framebuffer_space_stages) ||
5313 (dstStage & ~framebuffer_space_stages)) {
5314 cmd->state.rp.disable_gmem = true;
5315 }
5316 }
5317
5318 struct tu_cache_state *cache =
5319 cmd->state.pass ? &cmd->state.renderpass_cache : &cmd->state.cache;
5320 tu_flush_for_access(cache, src_flags, dst_flags);
5321
5322 enum tu_stage src_stage = vk2tu_src_stage(srcStage);
5323 enum tu_stage dst_stage = vk2tu_dst_stage(dstStage);
5324 tu_flush_for_stage(cache, src_stage, dst_stage);
5325 }
5326
5327 VKAPI_ATTR void VKAPI_CALL
tu_CmdPipelineBarrier2(VkCommandBuffer commandBuffer,const VkDependencyInfo * pDependencyInfo)5328 tu_CmdPipelineBarrier2(VkCommandBuffer commandBuffer,
5329 const VkDependencyInfo *pDependencyInfo)
5330 {
5331 TU_FROM_HANDLE(tu_cmd_buffer, cmd_buffer, commandBuffer);
5332
5333 tu_barrier(cmd_buffer, pDependencyInfo);
5334 }
5335
5336 static void
write_event(struct tu_cmd_buffer * cmd,struct tu_event * event,VkPipelineStageFlags2 stageMask,unsigned value)5337 write_event(struct tu_cmd_buffer *cmd, struct tu_event *event,
5338 VkPipelineStageFlags2 stageMask, unsigned value)
5339 {
5340 struct tu_cs *cs = &cmd->cs;
5341
5342 /* vkCmdSetEvent/vkCmdResetEvent cannot be called inside a render pass */
5343 assert(!cmd->state.pass);
5344
5345 tu_emit_cache_flush(cmd, cs);
5346
5347 /* Flags that only require a top-of-pipe event. DrawIndirect parameters are
5348 * read by the CP, so the draw indirect stage counts as top-of-pipe too.
5349 */
5350 VkPipelineStageFlags2 top_of_pipe_flags =
5351 VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT |
5352 VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT;
5353
5354 if (!(stageMask & ~top_of_pipe_flags)) {
5355 tu_cs_emit_pkt7(cs, CP_MEM_WRITE, 3);
5356 tu_cs_emit_qw(cs, event->bo->iova); /* ADDR_LO/HI */
5357 tu_cs_emit(cs, value);
5358 } else {
5359 /* Use a RB_DONE_TS event to wait for everything to complete. */
5360 tu_cs_emit_pkt7(cs, CP_EVENT_WRITE, 4);
5361 tu_cs_emit(cs, CP_EVENT_WRITE_0_EVENT(RB_DONE_TS));
5362 tu_cs_emit_qw(cs, event->bo->iova);
5363 tu_cs_emit(cs, value);
5364 }
5365 }
5366
5367 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetEvent2(VkCommandBuffer commandBuffer,VkEvent _event,const VkDependencyInfo * pDependencyInfo)5368 tu_CmdSetEvent2(VkCommandBuffer commandBuffer,
5369 VkEvent _event,
5370 const VkDependencyInfo *pDependencyInfo)
5371 {
5372 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
5373 TU_FROM_HANDLE(tu_event, event, _event);
5374 VkPipelineStageFlags2 src_stage_mask = 0;
5375
5376 for (uint32_t i = 0; i < pDependencyInfo->memoryBarrierCount; i++)
5377 src_stage_mask |= pDependencyInfo->pMemoryBarriers[i].srcStageMask;
5378 for (uint32_t i = 0; i < pDependencyInfo->bufferMemoryBarrierCount; i++)
5379 src_stage_mask |= pDependencyInfo->pBufferMemoryBarriers[i].srcStageMask;
5380 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; i++)
5381 src_stage_mask |= pDependencyInfo->pImageMemoryBarriers[i].srcStageMask;
5382
5383 write_event(cmd, event, src_stage_mask, 1);
5384 }
5385
5386 VKAPI_ATTR void VKAPI_CALL
tu_CmdResetEvent2(VkCommandBuffer commandBuffer,VkEvent _event,VkPipelineStageFlags2 stageMask)5387 tu_CmdResetEvent2(VkCommandBuffer commandBuffer,
5388 VkEvent _event,
5389 VkPipelineStageFlags2 stageMask)
5390 {
5391 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
5392 TU_FROM_HANDLE(tu_event, event, _event);
5393
5394 write_event(cmd, event, stageMask, 0);
5395 }
5396
5397 VKAPI_ATTR void VKAPI_CALL
tu_CmdWaitEvents2(VkCommandBuffer commandBuffer,uint32_t eventCount,const VkEvent * pEvents,const VkDependencyInfo * pDependencyInfos)5398 tu_CmdWaitEvents2(VkCommandBuffer commandBuffer,
5399 uint32_t eventCount,
5400 const VkEvent *pEvents,
5401 const VkDependencyInfo* pDependencyInfos)
5402 {
5403 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
5404 struct tu_cs *cs = cmd->state.pass ? &cmd->draw_cs : &cmd->cs;
5405
5406 for (uint32_t i = 0; i < eventCount; i++) {
5407 TU_FROM_HANDLE(tu_event, event, pEvents[i]);
5408
5409 tu_cs_emit_pkt7(cs, CP_WAIT_REG_MEM, 6);
5410 tu_cs_emit(cs, CP_WAIT_REG_MEM_0_FUNCTION(WRITE_EQ) |
5411 CP_WAIT_REG_MEM_0_POLL_MEMORY);
5412 tu_cs_emit_qw(cs, event->bo->iova); /* POLL_ADDR_LO/HI */
5413 tu_cs_emit(cs, CP_WAIT_REG_MEM_3_REF(1));
5414 tu_cs_emit(cs, CP_WAIT_REG_MEM_4_MASK(~0u));
5415 tu_cs_emit(cs, CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(20));
5416 }
5417
5418 tu_barrier(cmd, pDependencyInfos);
5419 }
5420
5421 VKAPI_ATTR void VKAPI_CALL
tu_CmdSetDeviceMask(VkCommandBuffer commandBuffer,uint32_t deviceMask)5422 tu_CmdSetDeviceMask(VkCommandBuffer commandBuffer, uint32_t deviceMask)
5423 {
5424 /* No-op */
5425 }
5426
5427
5428 VKAPI_ATTR void VKAPI_CALL
tu_CmdBeginConditionalRenderingEXT(VkCommandBuffer commandBuffer,const VkConditionalRenderingBeginInfoEXT * pConditionalRenderingBegin)5429 tu_CmdBeginConditionalRenderingEXT(VkCommandBuffer commandBuffer,
5430 const VkConditionalRenderingBeginInfoEXT *pConditionalRenderingBegin)
5431 {
5432 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
5433
5434 cmd->state.predication_active = true;
5435
5436 struct tu_cs *cs = cmd->state.pass ? &cmd->draw_cs : &cmd->cs;
5437
5438 tu_cs_emit_pkt7(cs, CP_DRAW_PRED_ENABLE_GLOBAL, 1);
5439 tu_cs_emit(cs, 1);
5440
5441 /* Wait for any writes to the predicate to land */
5442 if (cmd->state.pass)
5443 tu_emit_cache_flush_renderpass(cmd, cs);
5444 else
5445 tu_emit_cache_flush(cmd, cs);
5446
5447 TU_FROM_HANDLE(tu_buffer, buf, pConditionalRenderingBegin->buffer);
5448 uint64_t iova = buf->iova + pConditionalRenderingBegin->offset;
5449
5450 /* qcom doesn't support 32-bit reference values, only 64-bit, but Vulkan
5451 * mandates 32-bit comparisons. Our workaround is to copy the the reference
5452 * value to the low 32-bits of a location where the high 32 bits are known
5453 * to be 0 and then compare that.
5454 */
5455 tu_cs_emit_pkt7(cs, CP_MEM_TO_MEM, 5);
5456 tu_cs_emit(cs, 0);
5457 tu_cs_emit_qw(cs, global_iova(cmd, predicate));
5458 tu_cs_emit_qw(cs, iova);
5459
5460 tu_cs_emit_pkt7(cs, CP_WAIT_MEM_WRITES, 0);
5461 tu_cs_emit_pkt7(cs, CP_WAIT_FOR_ME, 0);
5462
5463 bool inv = pConditionalRenderingBegin->flags & VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT;
5464 tu_cs_emit_pkt7(cs, CP_DRAW_PRED_SET, 3);
5465 tu_cs_emit(cs, CP_DRAW_PRED_SET_0_SRC(PRED_SRC_MEM) |
5466 CP_DRAW_PRED_SET_0_TEST(inv ? EQ_0_PASS : NE_0_PASS));
5467 tu_cs_emit_qw(cs, global_iova(cmd, predicate));
5468 }
5469
5470 VKAPI_ATTR void VKAPI_CALL
tu_CmdEndConditionalRenderingEXT(VkCommandBuffer commandBuffer)5471 tu_CmdEndConditionalRenderingEXT(VkCommandBuffer commandBuffer)
5472 {
5473 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
5474
5475 cmd->state.predication_active = false;
5476
5477 struct tu_cs *cs = cmd->state.pass ? &cmd->draw_cs : &cmd->cs;
5478
5479 tu_cs_emit_pkt7(cs, CP_DRAW_PRED_ENABLE_GLOBAL, 1);
5480 tu_cs_emit(cs, 0);
5481 }
5482
5483 void
tu_CmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer,VkPipelineStageFlagBits2 pipelineStage,VkBuffer dstBuffer,VkDeviceSize dstOffset,uint32_t marker)5484 tu_CmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer,
5485 VkPipelineStageFlagBits2 pipelineStage,
5486 VkBuffer dstBuffer,
5487 VkDeviceSize dstOffset,
5488 uint32_t marker)
5489 {
5490 /* Almost the same as write_event, but also allowed in renderpass */
5491 TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
5492 TU_FROM_HANDLE(tu_buffer, buffer, dstBuffer);
5493
5494 uint64_t va = buffer->bo->iova + dstOffset;
5495
5496 struct tu_cs *cs = cmd->state.pass ? &cmd->draw_cs : &cmd->cs;
5497 struct tu_cache_state *cache =
5498 cmd->state.pass ? &cmd->state.renderpass_cache : &cmd->state.cache;
5499
5500 /* From the Vulkan 1.2.203 spec:
5501 *
5502 * The access scope for buffer marker writes falls under
5503 * the VK_ACCESS_TRANSFER_WRITE_BIT, and the pipeline stages for
5504 * identifying the synchronization scope must include both pipelineStage
5505 * and VK_PIPELINE_STAGE_TRANSFER_BIT.
5506 *
5507 * Transfer operations use CCU however here we write via CP.
5508 * Flush CCU in order to make the results of previous transfer
5509 * operation visible to CP.
5510 */
5511 tu_flush_for_access(cache, 0, TU_ACCESS_SYSMEM_WRITE);
5512
5513 /* Flags that only require a top-of-pipe event. DrawIndirect parameters are
5514 * read by the CP, so the draw indirect stage counts as top-of-pipe too.
5515 */
5516 VkPipelineStageFlags2 top_of_pipe_flags =
5517 VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT |
5518 VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT;
5519
5520 bool is_top_of_pipe = !(pipelineStage & ~top_of_pipe_flags);
5521
5522 /* We have to WFI only if we flushed CCU here and are using CP_MEM_WRITE.
5523 * Otherwise:
5524 * - We do CP_EVENT_WRITE(RB_DONE_TS) which should wait for flushes;
5525 * - There was a barrier to synchronize other writes with WriteBufferMarkerAMD
5526 * and they had to include our pipelineStage which forces the WFI.
5527 */
5528 if (cache->flush_bits != 0 && is_top_of_pipe) {
5529 cache->flush_bits |= TU_CMD_FLAG_WAIT_FOR_IDLE;
5530 }
5531
5532 if (cmd->state.pass) {
5533 tu_emit_cache_flush_renderpass(cmd, cs);
5534 } else {
5535 tu_emit_cache_flush(cmd, cs);
5536 }
5537
5538 if (is_top_of_pipe) {
5539 tu_cs_emit_pkt7(cs, CP_MEM_WRITE, 3);
5540 tu_cs_emit_qw(cs, va); /* ADDR_LO/HI */
5541 tu_cs_emit(cs, marker);
5542 } else {
5543 /* Use a RB_DONE_TS event to wait for everything to complete. */
5544 tu_cs_emit_pkt7(cs, CP_EVENT_WRITE, 4);
5545 tu_cs_emit(cs, CP_EVENT_WRITE_0_EVENT(RB_DONE_TS));
5546 tu_cs_emit_qw(cs, va);
5547 tu_cs_emit(cs, marker);
5548 }
5549
5550 /* Make sure the result of this write is visible to others. */
5551 tu_flush_for_access(cache, TU_ACCESS_CP_WRITE, 0);
5552 }
5553