Debian Codegraph @

src/panfrost/vulkan/panvk_cmd_draw.h

raw
1 /*
2 * Copyright © 2024 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef PANVK_CMD_DRAW_H
7 #define PANVK_CMD_DRAW_H
8
9 #ifndef PAN_ARCH
10 #error "PAN_ARCH must be defined"
11 #endif
12
13 #include "panvk_blend.h"
14 #include "panvk_cmd_desc_state.h"
15 #include "panvk_cmd_query.h"
16 #include "panvk_entrypoints.h"
17 #include "panvk_image.h"
18 #include "panvk_image_view.h"
19 #include "panvk_physical_device.h"
20 #include "panvk_shader.h"
21
22 #include "vk_command_buffer.h"
23 #include "vk_format.h"
24 #include "util/u_tristate.h"
25
26 #include "pan_fb.h"
27 #include "pan_props.h"
28
29 #define MAX_VBS 16
30
31 struct panvk_cmd_buffer;
32
33 struct panvk_attrib_buf {
34 uint64_t address;
35 unsigned size;
36 };
37
38 struct panvk_resolve_attachment {
39 VkResolveModeFlagBits mode;
40 struct panvk_image_view *dst_iview;
41 };
42
43 struct panvk_rendering_state {
44 VkRenderingFlags flags;
45 uint32_t layer_count;
46 uint32_t view_mask;
47 enum u_tristate first_provoking_vertex;
48
49 enum vk_rp_attachment_flags bound_attachments;
50 struct {
51 struct panvk_image_view *iviews[MAX_RTS];
52 /* If non-null, preload_iviews[i] overrides iviews[i] for preloads. */
53 struct panvk_image_view *preload_iviews[MAX_RTS];
54 VkFormat fmts[MAX_RTS];
55 uint8_t samples[MAX_RTS];
56 struct panvk_resolve_attachment resolve[MAX_RTS];
57 } color_attachments;
58
59 struct {
60 struct panvk_image_view *iview;
61 /* If non-null, preload_iview overrides iview for preloads. */
62 struct panvk_image_view *preload_iview;
63 VkFormat fmt;
64 struct panvk_resolve_attachment resolve;
65 } z_attachment, s_attachment;
66
67 /* Used for separate Z/S images */
68 struct {
69 struct pan_image_view load, spill, store, resolve;
70 } z_pview, s_pview;
71
72 struct {
73 struct pan_fb_layout layout;
74 struct pan_fb_load load;
75 struct pan_fb_resolve resolve;
76 struct pan_fb_store store;
77 struct {
78 struct pan_fb_load load;
79 struct pan_fb_store store;
80 } spill;
81
82 /* nr_samples to be used before framebuffer / tiler descriptor are emitted */
83 uint32_t nr_samples;
84
85 #if PAN_ARCH < 9
86 uint32_t bo_count;
87 struct pan_kmod_bo *bos[(MAX_RTS * PANVK_MAX_PLANES) + 2];
88 bool needs_load;
89 bool needs_store;
90 #endif
91 } fb;
92
93 #if PAN_ARCH >= 10
94 struct pan_ptr fbds;
95 uint64_t tiler;
96
97 /* When a secondary command buffer has to flush draws, it disturbs the
98 * inherited context, and the primary command buffer needs to know. */
99 bool invalidate_inherited_ctx;
100
101 /* True if the last render pass was suspended. */
102 bool suspended;
103
104 /* Blocks that can patch to flip the provoking vertex mode if we need to
105 * emit FBDs/TDs before we know which mode the application is using */
106 struct cs_maybe *maybe_set_tds_provoking_vertex;
107 struct cs_maybe *maybe_set_fbds_provoking_vertex;
108
109 struct {
110 /* != 0 if the render pass contains one or more occlusion queries to
111 * signal. */
112 uint64_t chain;
113
114 /* Point to the syncobj of the last occlusion query that was passed
115 * to a draw. */
116 uint64_t last;
117 } oq;
118
119 struct {
120 uint64_t fbds[3];
121 } ir;
122 #endif
123 };
124
125 enum panvk_cmd_graphics_dirty_state {
126 PANVK_CMD_GRAPHICS_DIRTY_VS,
127 PANVK_CMD_GRAPHICS_DIRTY_FS,
128 PANVK_CMD_GRAPHICS_DIRTY_VB,
129 PANVK_CMD_GRAPHICS_DIRTY_IB,
130 PANVK_CMD_GRAPHICS_DIRTY_OQ,
131 PANVK_CMD_GRAPHICS_DIRTY_DESC_STATE,
132 PANVK_CMD_GRAPHICS_DIRTY_RENDER_STATE,
133 PANVK_CMD_GRAPHICS_DIRTY_VS_PUSH_UNIFORMS,
134 PANVK_CMD_GRAPHICS_DIRTY_FS_PUSH_UNIFORMS,
135 PANVK_CMD_GRAPHICS_DIRTY_STATE_COUNT,
136 };
137
138 struct panvk_cmd_graphics_state {
139 struct panvk_descriptor_state desc_state;
140
141 struct {
142 struct vk_vertex_input_state vi;
143 struct vk_sample_locations_state sl;
144 } dynamic;
145
146 struct panvk_occlusion_query_state occlusion_query;
147 #if PAN_ARCH >= 10
148 struct panvk_prims_generated_query_state prims_generated_query;
149 #endif
150 struct panvk_graphics_sysvals sysvals;
151
152 #if PAN_ARCH < 9
153 struct panvk_shader_link link;
154 #endif
155
156 struct {
157 const struct panvk_shader *shader;
158 struct panvk_shader_desc_state desc;
159 uint64_t blend_descs[MAX_RTS];
160 uint64_t push_uniforms;
161 bool required;
162 #if PAN_ARCH < 9
163 uint64_t rsd;
164 #endif
165 } fs;
166
167 struct {
168 const struct panvk_shader *shader;
169 struct panvk_shader_desc_state desc;
170 uint64_t push_uniforms;
171 #if PAN_ARCH < 9
172 uint64_t attribs;
173 uint64_t attrib_bufs;
174 uint64_t indirect_attribs_infos;
175 uint64_t indirect_attrib_bufs_infos;
176 uint64_t indirect_varying_bufs_infos;
177 bool previous_draw_was_indirect;
178 #endif
179 } vs;
180
181 struct {
182 struct panvk_attrib_buf bufs[MAX_VBS];
183 unsigned count;
184 } vb;
185
186 #if PAN_ARCH >= 10
187 struct {
188 uint32_t attribs_changing_on_base_instance;
189 } vi;
190 #endif
191
192 /* Index buffer */
193 struct {
194 uint64_t dev_addr;
195 uint64_t size;
196 uint8_t index_size;
197 } ib;
198
199 struct {
200 struct panvk_blend_info info;
201 } cb;
202
203 struct panvk_rendering_state render;
204
205 bool vk_meta;
206
207 #if PAN_ARCH < 9
208 uint64_t vpd;
209 #endif
210
211 #if PAN_ARCH >= 10
212 uint64_t tsd;
213 #endif
214
215 BITSET_DECLARE(dirty, PANVK_CMD_GRAPHICS_DIRTY_STATE_COUNT);
216 };
217
218 #define dyn_gfx_state_dirty(__cmdbuf, __name) \
219 BITSET_TEST((__cmdbuf)->vk.dynamic_graphics_state.dirty, \
220 MESA_VK_DYNAMIC_##__name)
221
222 #define gfx_state_dirty(__cmdbuf, __name) \
223 BITSET_TEST((__cmdbuf)->state.gfx.dirty, PANVK_CMD_GRAPHICS_DIRTY_##__name)
224
225 #define gfx_state_set_dirty(__cmdbuf, __name) \
226 BITSET_SET((__cmdbuf)->state.gfx.dirty, PANVK_CMD_GRAPHICS_DIRTY_##__name)
227
228 #define gfx_state_clear_all_dirty(__cmdbuf) \
229 BITSET_ZERO((__cmdbuf)->state.gfx.dirty)
230
231 #define gfx_state_set_all_dirty(__cmdbuf) \
232 BITSET_ONES((__cmdbuf)->state.gfx.dirty)
233
234 #define set_gfx_sysval(__cmdbuf, __dirty, __name, __val) \
235 do { \
236 struct panvk_graphics_sysvals __new_sysval; \
237 __new_sysval.__name = __val; \
238 if (memcmp(&(__cmdbuf)->state.gfx.sysvals.__name, &__new_sysval.__name, \
239 sizeof(__new_sysval.__name))) { \
240 (__cmdbuf)->state.gfx.sysvals.__name = __new_sysval.__name; \
241 BITSET_SET_RANGE(__dirty, sysval_fau_start(graphics, __name), \
242 sysval_fau_end(graphics, __name)); \
243 } \
244 } while (0)
245
246 #if PAN_ARCH >= 10
247 struct panvk_device_draw_context {
248 struct panvk_priv_bo *fns_bo;
249 uint64_t fn_set_fbds_provoking_vertex_stride;
250 };
251 #endif
252
253 static inline void
254 panvk_depth_range(const struct panvk_cmd_graphics_state *state,
255 const struct vk_viewport_state *vp,
256 float *z_min, float *z_max)
257 {
258 if (vp->depth_clamp_mode == VK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT) {
259 *z_min = vp->depth_clamp_range.minDepthClamp;
260 *z_max = vp->depth_clamp_range.maxDepthClamp;
261 return;
262 }
263
264 float a = vp->depth_clip_negative_one_to_one ?
265 state->sysvals.viewport.offset.z - state->sysvals.viewport.scale.z :
266 state->sysvals.viewport.offset.z;
267 float b = state->sysvals.viewport.offset.z + state->sysvals.viewport.scale.z;
268 *z_min = MIN2(a, b);
269 *z_max = MAX2(a, b);
270 }
271
272 static inline uint32_t
273 panvk_select_tiler_hierarchy_mask(const struct panvk_physical_device *phys_dev,
274 const struct panvk_cmd_graphics_state *state,
275 unsigned bin_ptr_mem_budget)
276 {
277 struct pan_tiler_features tiler_features =
278 pan_query_tiler_features(&phys_dev->kmod.dev->props);
279
280 uint32_t hierarchy_mask = GENX(pan_select_tiler_hierarchy_mask)(
281 state->render.fb.layout.width_px, state->render.fb.layout.height_px,
282 tiler_features.max_levels, state->render.fb.layout.tile_size_px,
283 bin_ptr_mem_budget);
284
285 return hierarchy_mask;
286 }
287
288 static inline bool
289 fs_required(const struct panvk_cmd_graphics_state *state,
290 const struct vk_dynamic_graphics_state *dyn_state)
291 {
292 const struct panvk_shader_variant *fs =
293 panvk_shader_only_variant(state->fs.shader);
294 const struct pan_shader_info *fs_info = fs ? &fs->info : NULL;
295 const struct vk_color_blend_state *cb = &dyn_state->cb;
296 const struct vk_rasterization_state *rs = &dyn_state->rs;
297
298 if (rs->rasterizer_discard_enable || !fs_info)
299 return false;
300
301 /* If we generally have side effects */
302 if (fs_info->fs.sidefx)
303 return true;
304
305 /* If colour is written we need to execute */
306 for (unsigned i = 0; i < cb->attachment_count; ++i) {
307 if ((cb->color_write_enables & BITFIELD_BIT(i)) &&
308 cb->attachments[i].write_mask)
309 return true;
310 }
311
312 /* If alpha-to-coverage is enabled, we need to run the fragment shader even
313 * if we don't have a color attachment, so depth/stencil updates can be
314 * discarded if alpha, and thus coverage, is 0. */
315 if (dyn_state->ms.alpha_to_coverage_enable)
316 return true;
317
318 /* If the sample mask is updated, we need to run the fragment shader,
319 * otherwise the fixed-function depth/stencil results will apply to all
320 * samples. */
321 if (fs_info->outputs_written & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK))
322 return true;
323
324 /* If depth is written and not implied we need to execute.
325 * TODO: Predicate on Z/S writes being enabled */
326 return (fs_info->fs.writes_depth || fs_info->fs.writes_stencil);
327 }
328
329 static inline bool
330 cached_fs_required(ASSERTED const struct panvk_cmd_graphics_state *state,
331 ASSERTED const struct vk_dynamic_graphics_state *dyn_state,
332 bool cached_value)
333 {
334 /* Make sure the cached value was properly initialized. */
335 assert(fs_required(state, dyn_state) == cached_value);
336 return cached_value;
337 }
338
339 #define get_fs(__cmdbuf) \
340 (cached_fs_required(&(__cmdbuf)->state.gfx, \
341 &(__cmdbuf)->vk.dynamic_graphics_state, \
342 (__cmdbuf)->state.gfx.fs.required) \
343 ? (__cmdbuf)->state.gfx.fs.shader \
344 : NULL)
345
346 /* Anything that might change the value returned by get_fs() makes users of the
347 * fragment shader dirty, because not using the fragment shader (when
348 * fs_required() returns false) impacts various other things, like VS -> FS
349 * linking in the JM backend, or the update of the fragment shader pointer in
350 * the CSF backend. Call gfx_state_dirty(cmdbuf, FS) if you only care about
351 * fragment shader updates. */
352
353 #define fs_user_dirty(__cmdbuf) \
354 (gfx_state_dirty(cmdbuf, FS) || \
355 dyn_gfx_state_dirty(cmdbuf, RS_RASTERIZER_DISCARD_ENABLE) || \
356 dyn_gfx_state_dirty(cmdbuf, CB_ATTACHMENT_COUNT) || \
357 dyn_gfx_state_dirty(cmdbuf, CB_COLOR_WRITE_ENABLES) || \
358 dyn_gfx_state_dirty(cmdbuf, CB_WRITE_MASKS) || \
359 dyn_gfx_state_dirty(cmdbuf, MS_ALPHA_TO_COVERAGE_ENABLE))
360
361 /* After a draw, all dirty flags are cleared except the FS dirty flag which
362 * needs to be set again if the draw didn't use the fragment shader. */
363
364 #define clear_dirty_after_draw(__cmdbuf) \
365 do { \
366 bool __set_fs_dirty = \
367 (__cmdbuf)->state.gfx.fs.shader != get_fs(__cmdbuf); \
368 bool __set_fs_push_dirty = \
369 __set_fs_dirty && gfx_state_dirty(__cmdbuf, FS_PUSH_UNIFORMS); \
370 vk_dynamic_graphics_state_clear_dirty( \
371 &(__cmdbuf)->vk.dynamic_graphics_state); \
372 gfx_state_clear_all_dirty(__cmdbuf); \
373 if (__set_fs_dirty) \
374 gfx_state_set_dirty(__cmdbuf, FS); \
375 if (__set_fs_push_dirty) \
376 gfx_state_set_dirty(__cmdbuf, FS_PUSH_UNIFORMS); \
377 } while (0)
378
379
380 #if PAN_ARCH >= 10
381 VkResult
382 panvk_per_arch(device_draw_context_init)(struct panvk_device *dev);
383
384 void
385 panvk_per_arch(device_draw_context_cleanup)(struct panvk_device *dev);
386 #endif
387
388 void
389 panvk_per_arch(cmd_init_render_state)(struct panvk_cmd_buffer *cmdbuf,
390 const VkRenderingInfo *pRenderingInfo);
391
392 void panvk_per_arch(cmd_select_tile_size)(struct panvk_cmd_buffer *cmdbuf);
393
394 struct panvk_draw_info {
395 struct {
396 uint32_t size;
397 uint32_t offset;
398 } index;
399
400 struct {
401 #if PAN_ARCH < 9
402 int32_t raw_offset;
403 #endif
404 int32_t base;
405 uint32_t count;
406 } vertex;
407
408 struct {
409 int32_t base;
410 uint32_t count;
411 } instance;
412
413 struct {
414 uint64_t buffer_dev_addr;
415 uint64_t count_buffer_dev_addr;
416 uint32_t draw_count;
417 uint32_t stride;
418 } indirect;
419
420 #if PAN_ARCH < 9
421 uint32_t layer_id;
422 #endif
423 };
424
425 void
426 panvk_per_arch(cmd_prepare_draw_sysvals)(struct panvk_cmd_buffer *cmdbuf,
427 const struct panvk_draw_info *info);
428
429 static inline uint32_t
430 color_attachment_written_mask(
431 const struct panvk_shader_variant *fs,
432 const struct vk_color_attachment_location_state *cal)
433 {
434 uint32_t written_by_shader =
435 (fs->info.outputs_written >> FRAG_RESULT_DATA0) & BITFIELD_MASK(8);
436 uint32_t catt_written_mask = 0;
437
438 for (uint32_t i = 0; i < MAX_RTS; i++) {
439 if (cal->color_map[i] == MESA_VK_ATTACHMENT_UNUSED)
440 continue;
441
442 uint32_t shader_rt = cal->color_map[i];
443
444 if (written_by_shader & BITFIELD_BIT(shader_rt))
445 catt_written_mask |= BITFIELD_BIT(i);
446 }
447
448 return catt_written_mask;
449 }
450
451 static inline uint32_t
452 color_attachment_read_mask(const struct panvk_shader_variant *fs,
453 const struct vk_input_attachment_location_state *ial,
454 uint8_t color_attachment_mask)
455 {
456 uint32_t color_attachment_count =
457 ial->color_attachment_count == MESA_VK_COLOR_ATTACHMENT_COUNT_UNKNOWN
458 ? util_last_bit(color_attachment_mask)
459 : ial->color_attachment_count;
460 uint32_t catt_read_mask = 0;
461
462 for (uint32_t i = 0; i < color_attachment_count; i++) {
463 if (ial->color_map[i] == MESA_VK_ATTACHMENT_UNUSED)
464 continue;
465
466 uint32_t catt_idx = ial->color_map[i] + 1;
467 if (fs->fs.input_attachment_read & BITFIELD_BIT(catt_idx)) {
468 assert(color_attachment_mask & BITFIELD_BIT(i));
469 catt_read_mask |= BITFIELD_BIT(i);
470 }
471 }
472
473 return catt_read_mask;
474 }
475
476 static inline bool
477 z_attachment_read(const struct panvk_shader_variant *fs,
478 const struct vk_input_attachment_location_state *ial)
479 {
480 uint32_t depth_mask = ial->depth_att == MESA_VK_ATTACHMENT_NO_INDEX
481 ? BITFIELD_BIT(0)
482 : ial->depth_att != MESA_VK_ATTACHMENT_UNUSED
483 ? BITFIELD_BIT(ial->depth_att + 1)
484 : 0;
485 return depth_mask & fs->fs.input_attachment_read;
486 }
487
488 static inline bool
489 s_attachment_read(const struct panvk_shader_variant *fs,
490 const struct vk_input_attachment_location_state *ial)
491 {
492 uint32_t stencil_mask = ial->stencil_att == MESA_VK_ATTACHMENT_NO_INDEX
493 ? BITFIELD_BIT(0)
494 : ial->stencil_att != MESA_VK_ATTACHMENT_UNUSED
495 ? BITFIELD_BIT(ial->stencil_att + 1)
496 : 0;
497
498 return stencil_mask & fs->fs.input_attachment_read;
499 }
500
501 #endif