anv: Move the RT BTI flush workaround to begin_subpass
[mesa.git] / src / intel / vulkan / genX_cmd_buffer.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26
27 #include "anv_private.h"
28 #include "vk_format_info.h"
29 #include "vk_util.h"
30 #include "util/fast_idiv_by_const.h"
31
32 #include "common/gen_aux_map.h"
33 #include "common/gen_l3_config.h"
34 #include "genxml/gen_macros.h"
35 #include "genxml/genX_pack.h"
36
37 /* We reserve GPR 14 and 15 for conditional rendering */
38 #define GEN_MI_BUILDER_NUM_ALLOC_GPRS 14
39 #define __gen_get_batch_dwords anv_batch_emit_dwords
40 #define __gen_address_offset anv_address_add
41 #include "common/gen_mi_builder.h"
42
43 static void
44 emit_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
45 {
46 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
47 lri.RegisterOffset = reg;
48 lri.DataDWord = imm;
49 }
50 }
51
52 void
53 genX(cmd_buffer_emit_state_base_address)(struct anv_cmd_buffer *cmd_buffer)
54 {
55 struct anv_device *device = cmd_buffer->device;
56
57 /* If we are emitting a new state base address we probably need to re-emit
58 * binding tables.
59 */
60 cmd_buffer->state.descriptors_dirty |= ~0;
61
62 /* Emit a render target cache flush.
63 *
64 * This isn't documented anywhere in the PRM. However, it seems to be
65 * necessary prior to changing the surface state base adress. Without
66 * this, we get GPU hangs when using multi-level command buffers which
67 * clear depth, reset state base address, and then go render stuff.
68 */
69 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
70 pc.DCFlushEnable = true;
71 pc.RenderTargetCacheFlushEnable = true;
72 pc.CommandStreamerStallEnable = true;
73 #if GEN_GEN >= 12
74 pc.TileCacheFlushEnable = true;
75 #endif
76 }
77
78 anv_batch_emit(&cmd_buffer->batch, GENX(STATE_BASE_ADDRESS), sba) {
79 sba.GeneralStateBaseAddress = (struct anv_address) { NULL, 0 };
80 sba.GeneralStateMOCS = GENX(MOCS);
81 sba.GeneralStateBaseAddressModifyEnable = true;
82
83 sba.StatelessDataPortAccessMOCS = GENX(MOCS);
84
85 sba.SurfaceStateBaseAddress =
86 anv_cmd_buffer_surface_base_address(cmd_buffer);
87 sba.SurfaceStateMOCS = GENX(MOCS);
88 sba.SurfaceStateBaseAddressModifyEnable = true;
89
90 sba.DynamicStateBaseAddress =
91 (struct anv_address) { device->dynamic_state_pool.block_pool.bo, 0 };
92 sba.DynamicStateMOCS = GENX(MOCS);
93 sba.DynamicStateBaseAddressModifyEnable = true;
94
95 sba.IndirectObjectBaseAddress = (struct anv_address) { NULL, 0 };
96 sba.IndirectObjectMOCS = GENX(MOCS);
97 sba.IndirectObjectBaseAddressModifyEnable = true;
98
99 sba.InstructionBaseAddress =
100 (struct anv_address) { device->instruction_state_pool.block_pool.bo, 0 };
101 sba.InstructionMOCS = GENX(MOCS);
102 sba.InstructionBaseAddressModifyEnable = true;
103
104 # if (GEN_GEN >= 8)
105 /* Broadwell requires that we specify a buffer size for a bunch of
106 * these fields. However, since we will be growing the BO's live, we
107 * just set them all to the maximum.
108 */
109 sba.GeneralStateBufferSize = 0xfffff;
110 sba.GeneralStateBufferSizeModifyEnable = true;
111 sba.DynamicStateBufferSize = 0xfffff;
112 sba.DynamicStateBufferSizeModifyEnable = true;
113 sba.IndirectObjectBufferSize = 0xfffff;
114 sba.IndirectObjectBufferSizeModifyEnable = true;
115 sba.InstructionBufferSize = 0xfffff;
116 sba.InstructionBuffersizeModifyEnable = true;
117 # else
118 /* On gen7, we have upper bounds instead. According to the docs,
119 * setting an upper bound of zero means that no bounds checking is
120 * performed so, in theory, we should be able to leave them zero.
121 * However, border color is broken and the GPU bounds-checks anyway.
122 * To avoid this and other potential problems, we may as well set it
123 * for everything.
124 */
125 sba.GeneralStateAccessUpperBound =
126 (struct anv_address) { .bo = NULL, .offset = 0xfffff000 };
127 sba.GeneralStateAccessUpperBoundModifyEnable = true;
128 sba.DynamicStateAccessUpperBound =
129 (struct anv_address) { .bo = NULL, .offset = 0xfffff000 };
130 sba.DynamicStateAccessUpperBoundModifyEnable = true;
131 sba.InstructionAccessUpperBound =
132 (struct anv_address) { .bo = NULL, .offset = 0xfffff000 };
133 sba.InstructionAccessUpperBoundModifyEnable = true;
134 # endif
135 # if (GEN_GEN >= 9)
136 if (cmd_buffer->device->instance->physicalDevice.use_softpin) {
137 sba.BindlessSurfaceStateBaseAddress = (struct anv_address) {
138 .bo = device->surface_state_pool.block_pool.bo,
139 .offset = 0,
140 };
141 sba.BindlessSurfaceStateSize = (1 << 20) - 1;
142 } else {
143 sba.BindlessSurfaceStateBaseAddress = ANV_NULL_ADDRESS;
144 sba.BindlessSurfaceStateSize = 0;
145 }
146 sba.BindlessSurfaceStateMOCS = GENX(MOCS);
147 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
148 # endif
149 # if (GEN_GEN >= 10)
150 sba.BindlessSamplerStateBaseAddress = (struct anv_address) { NULL, 0 };
151 sba.BindlessSamplerStateMOCS = GENX(MOCS);
152 sba.BindlessSamplerStateBaseAddressModifyEnable = true;
153 sba.BindlessSamplerStateBufferSize = 0;
154 # endif
155 }
156
157 /* After re-setting the surface state base address, we have to do some
158 * cache flusing so that the sampler engine will pick up the new
159 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
160 * Shared Function > 3D Sampler > State > State Caching (page 96):
161 *
162 * Coherency with system memory in the state cache, like the texture
163 * cache is handled partially by software. It is expected that the
164 * command stream or shader will issue Cache Flush operation or
165 * Cache_Flush sampler message to ensure that the L1 cache remains
166 * coherent with system memory.
167 *
168 * [...]
169 *
170 * Whenever the value of the Dynamic_State_Base_Addr,
171 * Surface_State_Base_Addr are altered, the L1 state cache must be
172 * invalidated to ensure the new surface or sampler state is fetched
173 * from system memory.
174 *
175 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
176 * which, according the PIPE_CONTROL instruction documentation in the
177 * Broadwell PRM:
178 *
179 * Setting this bit is independent of any other bit in this packet.
180 * This bit controls the invalidation of the L1 and L2 state caches
181 * at the top of the pipe i.e. at the parsing time.
182 *
183 * Unfortunately, experimentation seems to indicate that state cache
184 * invalidation through a PIPE_CONTROL does nothing whatsoever in
185 * regards to surface state and binding tables. In stead, it seems that
186 * invalidating the texture cache is what is actually needed.
187 *
188 * XXX: As far as we have been able to determine through
189 * experimentation, shows that flush the texture cache appears to be
190 * sufficient. The theory here is that all of the sampling/rendering
191 * units cache the binding table in the texture cache. However, we have
192 * yet to be able to actually confirm this.
193 */
194 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
195 pc.TextureCacheInvalidationEnable = true;
196 pc.ConstantCacheInvalidationEnable = true;
197 pc.StateCacheInvalidationEnable = true;
198 }
199 }
200
201 static void
202 add_surface_reloc(struct anv_cmd_buffer *cmd_buffer,
203 struct anv_state state, struct anv_address addr)
204 {
205 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
206
207 VkResult result =
208 anv_reloc_list_add(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc,
209 state.offset + isl_dev->ss.addr_offset,
210 addr.bo, addr.offset, NULL);
211 if (result != VK_SUCCESS)
212 anv_batch_set_error(&cmd_buffer->batch, result);
213 }
214
215 static void
216 add_surface_state_relocs(struct anv_cmd_buffer *cmd_buffer,
217 struct anv_surface_state state)
218 {
219 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
220
221 assert(!anv_address_is_null(state.address));
222 add_surface_reloc(cmd_buffer, state.state, state.address);
223
224 if (!anv_address_is_null(state.aux_address)) {
225 VkResult result =
226 anv_reloc_list_add(&cmd_buffer->surface_relocs,
227 &cmd_buffer->pool->alloc,
228 state.state.offset + isl_dev->ss.aux_addr_offset,
229 state.aux_address.bo,
230 state.aux_address.offset,
231 NULL);
232 if (result != VK_SUCCESS)
233 anv_batch_set_error(&cmd_buffer->batch, result);
234 }
235
236 if (!anv_address_is_null(state.clear_address)) {
237 VkResult result =
238 anv_reloc_list_add(&cmd_buffer->surface_relocs,
239 &cmd_buffer->pool->alloc,
240 state.state.offset +
241 isl_dev->ss.clear_color_state_offset,
242 state.clear_address.bo,
243 state.clear_address.offset,
244 NULL);
245 if (result != VK_SUCCESS)
246 anv_batch_set_error(&cmd_buffer->batch, result);
247 }
248 }
249
250 static void
251 color_attachment_compute_aux_usage(struct anv_device * device,
252 struct anv_cmd_state * cmd_state,
253 uint32_t att, VkRect2D render_area,
254 union isl_color_value *fast_clear_color)
255 {
256 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
257 struct anv_image_view *iview = cmd_state->attachments[att].image_view;
258
259 assert(iview->n_planes == 1);
260
261 if (iview->planes[0].isl.base_array_layer >=
262 anv_image_aux_layers(iview->image, VK_IMAGE_ASPECT_COLOR_BIT,
263 iview->planes[0].isl.base_level)) {
264 /* There is no aux buffer which corresponds to the level and layer(s)
265 * being accessed.
266 */
267 att_state->aux_usage = ISL_AUX_USAGE_NONE;
268 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
269 att_state->fast_clear = false;
270 return;
271 }
272
273 att_state->aux_usage =
274 anv_layout_to_aux_usage(&device->info, iview->image,
275 VK_IMAGE_ASPECT_COLOR_BIT,
276 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
277
278 /* If we don't have aux, then we should have returned early in the layer
279 * check above. If we got here, we must have something.
280 */
281 assert(att_state->aux_usage != ISL_AUX_USAGE_NONE);
282
283 if (att_state->aux_usage == ISL_AUX_USAGE_CCS_E ||
284 att_state->aux_usage == ISL_AUX_USAGE_MCS) {
285 att_state->input_aux_usage = att_state->aux_usage;
286 } else {
287 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
288 *
289 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
290 * setting is only allowed if Surface Format supported for Fast
291 * Clear. In addition, if the surface is bound to the sampling
292 * engine, Surface Format must be supported for Render Target
293 * Compression for surfaces bound to the sampling engine."
294 *
295 * In other words, we can only sample from a fast-cleared image if it
296 * also supports color compression.
297 */
298 if (isl_format_supports_ccs_e(&device->info, iview->planes[0].isl.format) &&
299 isl_format_supports_ccs_d(&device->info, iview->planes[0].isl.format)) {
300 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_D;
301
302 /* While fast-clear resolves and partial resolves are fairly cheap in the
303 * case where you render to most of the pixels, full resolves are not
304 * because they potentially involve reading and writing the entire
305 * framebuffer. If we can't texture with CCS_E, we should leave it off and
306 * limit ourselves to fast clears.
307 */
308 if (cmd_state->pass->attachments[att].first_subpass_layout ==
309 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
310 anv_perf_warn(device->instance, iview->image,
311 "Not temporarily enabling CCS_E.");
312 }
313 } else {
314 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
315 }
316 }
317
318 assert(iview->image->planes[0].aux_surface.isl.usage &
319 (ISL_SURF_USAGE_CCS_BIT | ISL_SURF_USAGE_MCS_BIT));
320
321 union isl_color_value clear_color = {};
322 anv_clear_color_from_att_state(&clear_color, att_state, iview);
323
324 att_state->clear_color_is_zero_one =
325 isl_color_value_is_zero_one(clear_color, iview->planes[0].isl.format);
326 att_state->clear_color_is_zero =
327 isl_color_value_is_zero(clear_color, iview->planes[0].isl.format);
328
329 if (att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
330 /* Start by getting the fast clear type. We use the first subpass
331 * layout here because we don't want to fast-clear if the first subpass
332 * to use the attachment can't handle fast-clears.
333 */
334 enum anv_fast_clear_type fast_clear_type =
335 anv_layout_to_fast_clear_type(&device->info, iview->image,
336 VK_IMAGE_ASPECT_COLOR_BIT,
337 cmd_state->pass->attachments[att].first_subpass_layout);
338 switch (fast_clear_type) {
339 case ANV_FAST_CLEAR_NONE:
340 att_state->fast_clear = false;
341 break;
342 case ANV_FAST_CLEAR_DEFAULT_VALUE:
343 att_state->fast_clear = att_state->clear_color_is_zero;
344 break;
345 case ANV_FAST_CLEAR_ANY:
346 att_state->fast_clear = true;
347 break;
348 }
349
350 /* Potentially, we could do partial fast-clears but doing so has crazy
351 * alignment restrictions. It's easier to just restrict to full size
352 * fast clears for now.
353 */
354 if (render_area.offset.x != 0 ||
355 render_area.offset.y != 0 ||
356 render_area.extent.width != iview->extent.width ||
357 render_area.extent.height != iview->extent.height)
358 att_state->fast_clear = false;
359
360 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
361 if (GEN_GEN <= 8 && !att_state->clear_color_is_zero_one)
362 att_state->fast_clear = false;
363
364 /* We only allow fast clears to the first slice of an image (level 0,
365 * layer 0) and only for the entire slice. This guarantees us that, at
366 * any given time, there is only one clear color on any given image at
367 * any given time. At the time of our testing (Jan 17, 2018), there
368 * were no known applications which would benefit from fast-clearing
369 * more than just the first slice.
370 */
371 if (att_state->fast_clear &&
372 (iview->planes[0].isl.base_level > 0 ||
373 iview->planes[0].isl.base_array_layer > 0)) {
374 anv_perf_warn(device->instance, iview->image,
375 "Rendering with multi-lod or multi-layer framebuffer "
376 "with LOAD_OP_LOAD and baseMipLevel > 0 or "
377 "baseArrayLayer > 0. Not fast clearing.");
378 att_state->fast_clear = false;
379 } else if (att_state->fast_clear && cmd_state->framebuffer->layers > 1) {
380 anv_perf_warn(device->instance, iview->image,
381 "Rendering to a multi-layer framebuffer with "
382 "LOAD_OP_CLEAR. Only fast-clearing the first slice");
383 }
384
385 if (att_state->fast_clear)
386 *fast_clear_color = clear_color;
387 } else {
388 att_state->fast_clear = false;
389 }
390 }
391
392 static void
393 depth_stencil_attachment_compute_aux_usage(struct anv_device *device,
394 struct anv_cmd_state *cmd_state,
395 uint32_t att, VkRect2D render_area)
396 {
397 struct anv_render_pass_attachment *pass_att =
398 &cmd_state->pass->attachments[att];
399 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
400 struct anv_image_view *iview = cmd_state->attachments[att].image_view;
401
402 /* These will be initialized after the first subpass transition. */
403 att_state->aux_usage = ISL_AUX_USAGE_NONE;
404 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
405
406 if (GEN_GEN == 7) {
407 /* We don't do any HiZ or depth fast-clears on gen7 yet */
408 att_state->fast_clear = false;
409 return;
410 }
411
412 if (!(att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
413 /* If we're just clearing stencil, we can always HiZ clear */
414 att_state->fast_clear = true;
415 return;
416 }
417
418 /* Default to false for now */
419 att_state->fast_clear = false;
420
421 /* We must have depth in order to have HiZ */
422 if (!(iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
423 return;
424
425 const enum isl_aux_usage first_subpass_aux_usage =
426 anv_layout_to_aux_usage(&device->info, iview->image,
427 VK_IMAGE_ASPECT_DEPTH_BIT,
428 pass_att->first_subpass_layout);
429 if (!blorp_can_hiz_clear_depth(&device->info,
430 &iview->image->planes[0].surface.isl,
431 first_subpass_aux_usage,
432 iview->planes[0].isl.base_level,
433 iview->planes[0].isl.base_array_layer,
434 render_area.offset.x,
435 render_area.offset.y,
436 render_area.offset.x +
437 render_area.extent.width,
438 render_area.offset.y +
439 render_area.extent.height))
440 return;
441
442 if (att_state->clear_value.depthStencil.depth != ANV_HZ_FC_VAL)
443 return;
444
445 if (GEN_GEN == 8 && anv_can_sample_with_hiz(&device->info, iview->image)) {
446 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
447 * fast-cleared portion of a HiZ buffer. Testing has revealed that Gen8
448 * only supports returning 0.0f. Gens prior to gen8 do not support this
449 * feature at all.
450 */
451 return;
452 }
453
454 /* If we got here, then we can fast clear */
455 att_state->fast_clear = true;
456 }
457
458 static bool
459 need_input_attachment_state(const struct anv_render_pass_attachment *att)
460 {
461 if (!(att->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
462 return false;
463
464 /* We only allocate input attachment states for color surfaces. Compression
465 * is not yet enabled for depth textures and stencil doesn't allow
466 * compression so we can just use the texture surface state from the view.
467 */
468 return vk_format_is_color(att->format);
469 }
470
471 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
472 * the initial layout is undefined, the HiZ buffer and depth buffer will
473 * represent the same data at the end of this operation.
474 */
475 static void
476 transition_depth_buffer(struct anv_cmd_buffer *cmd_buffer,
477 const struct anv_image *image,
478 VkImageLayout initial_layout,
479 VkImageLayout final_layout)
480 {
481 const bool hiz_enabled = ISL_AUX_USAGE_HIZ ==
482 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
483 VK_IMAGE_ASPECT_DEPTH_BIT, initial_layout);
484 const bool enable_hiz = ISL_AUX_USAGE_HIZ ==
485 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
486 VK_IMAGE_ASPECT_DEPTH_BIT, final_layout);
487
488 enum isl_aux_op hiz_op;
489 if (hiz_enabled && !enable_hiz) {
490 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
491 } else if (!hiz_enabled && enable_hiz) {
492 hiz_op = ISL_AUX_OP_AMBIGUATE;
493 } else {
494 assert(hiz_enabled == enable_hiz);
495 /* If the same buffer will be used, no resolves are necessary. */
496 hiz_op = ISL_AUX_OP_NONE;
497 }
498
499 if (hiz_op != ISL_AUX_OP_NONE)
500 anv_image_hiz_op(cmd_buffer, image, VK_IMAGE_ASPECT_DEPTH_BIT,
501 0, 0, 1, hiz_op);
502 }
503
504 static inline bool
505 vk_image_layout_stencil_write_optimal(VkImageLayout layout)
506 {
507 return layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
508 layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL;
509 }
510
511 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
512 * the initial layout is undefined, the HiZ buffer and depth buffer will
513 * represent the same data at the end of this operation.
514 */
515 static void
516 transition_stencil_buffer(struct anv_cmd_buffer *cmd_buffer,
517 const struct anv_image *image,
518 uint32_t base_level, uint32_t level_count,
519 uint32_t base_layer, uint32_t layer_count,
520 VkImageLayout initial_layout,
521 VkImageLayout final_layout)
522 {
523 #if GEN_GEN == 7
524 uint32_t plane = anv_image_aspect_to_plane(image->aspects,
525 VK_IMAGE_ASPECT_STENCIL_BIT);
526
527 /* On gen7, we have to store a texturable version of the stencil buffer in
528 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
529 * forth at strategic points. Stencil writes are only allowed in three
530 * layouts:
531 *
532 * - VK_IMAGE_LAYOUT_GENERAL
533 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
534 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
535 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
536 *
537 * For general, we have no nice opportunity to transition so we do the copy
538 * to the shadow unconditionally at the end of the subpass. For transfer
539 * destinations, we can update it as part of the transfer op. For the
540 * other two, we delay the copy until a transition into some other layout.
541 */
542 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
543 vk_image_layout_stencil_write_optimal(initial_layout) &&
544 !vk_image_layout_stencil_write_optimal(final_layout)) {
545 anv_image_copy_to_shadow(cmd_buffer, image,
546 VK_IMAGE_ASPECT_STENCIL_BIT,
547 base_level, level_count,
548 base_layer, layer_count);
549 }
550 #endif /* GEN_GEN == 7 */
551 }
552
553 #define MI_PREDICATE_SRC0 0x2400
554 #define MI_PREDICATE_SRC1 0x2408
555 #define MI_PREDICATE_RESULT 0x2418
556
557 static void
558 set_image_compressed_bit(struct anv_cmd_buffer *cmd_buffer,
559 const struct anv_image *image,
560 VkImageAspectFlagBits aspect,
561 uint32_t level,
562 uint32_t base_layer, uint32_t layer_count,
563 bool compressed)
564 {
565 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
566
567 /* We only have compression tracking for CCS_E */
568 if (image->planes[plane].aux_usage != ISL_AUX_USAGE_CCS_E)
569 return;
570
571 for (uint32_t a = 0; a < layer_count; a++) {
572 uint32_t layer = base_layer + a;
573 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
574 sdi.Address = anv_image_get_compression_state_addr(cmd_buffer->device,
575 image, aspect,
576 level, layer);
577 sdi.ImmediateData = compressed ? UINT32_MAX : 0;
578 }
579 }
580 }
581
582 static void
583 set_image_fast_clear_state(struct anv_cmd_buffer *cmd_buffer,
584 const struct anv_image *image,
585 VkImageAspectFlagBits aspect,
586 enum anv_fast_clear_type fast_clear)
587 {
588 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
589 sdi.Address = anv_image_get_fast_clear_type_addr(cmd_buffer->device,
590 image, aspect);
591 sdi.ImmediateData = fast_clear;
592 }
593
594 /* Whenever we have fast-clear, we consider that slice to be compressed.
595 * This makes building predicates much easier.
596 */
597 if (fast_clear != ANV_FAST_CLEAR_NONE)
598 set_image_compressed_bit(cmd_buffer, image, aspect, 0, 0, 1, true);
599 }
600
601 /* This is only really practical on haswell and above because it requires
602 * MI math in order to get it correct.
603 */
604 #if GEN_GEN >= 8 || GEN_IS_HASWELL
605 static void
606 anv_cmd_compute_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
607 const struct anv_image *image,
608 VkImageAspectFlagBits aspect,
609 uint32_t level, uint32_t array_layer,
610 enum isl_aux_op resolve_op,
611 enum anv_fast_clear_type fast_clear_supported)
612 {
613 struct gen_mi_builder b;
614 gen_mi_builder_init(&b, &cmd_buffer->batch);
615
616 const struct gen_mi_value fast_clear_type =
617 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer->device,
618 image, aspect));
619
620 if (resolve_op == ISL_AUX_OP_FULL_RESOLVE) {
621 /* In this case, we're doing a full resolve which means we want the
622 * resolve to happen if any compression (including fast-clears) is
623 * present.
624 *
625 * In order to simplify the logic a bit, we make the assumption that,
626 * if the first slice has been fast-cleared, it is also marked as
627 * compressed. See also set_image_fast_clear_state.
628 */
629 const struct gen_mi_value compression_state =
630 gen_mi_mem32(anv_image_get_compression_state_addr(cmd_buffer->device,
631 image, aspect,
632 level, array_layer));
633 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
634 compression_state);
635 gen_mi_store(&b, compression_state, gen_mi_imm(0));
636
637 if (level == 0 && array_layer == 0) {
638 /* If the predicate is true, we want to write 0 to the fast clear type
639 * and, if it's false, leave it alone. We can do this by writing
640 *
641 * clear_type = clear_type & ~predicate;
642 */
643 struct gen_mi_value new_fast_clear_type =
644 gen_mi_iand(&b, fast_clear_type,
645 gen_mi_inot(&b, gen_mi_reg64(MI_PREDICATE_SRC0)));
646 gen_mi_store(&b, fast_clear_type, new_fast_clear_type);
647 }
648 } else if (level == 0 && array_layer == 0) {
649 /* In this case, we are doing a partial resolve to get rid of fast-clear
650 * colors. We don't care about the compression state but we do care
651 * about how much fast clear is allowed by the final layout.
652 */
653 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
654 assert(fast_clear_supported < ANV_FAST_CLEAR_ANY);
655
656 /* We need to compute (fast_clear_supported < image->fast_clear) */
657 struct gen_mi_value pred =
658 gen_mi_ult(&b, gen_mi_imm(fast_clear_supported), fast_clear_type);
659 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
660 gen_mi_value_ref(&b, pred));
661
662 /* If the predicate is true, we want to write 0 to the fast clear type
663 * and, if it's false, leave it alone. We can do this by writing
664 *
665 * clear_type = clear_type & ~predicate;
666 */
667 struct gen_mi_value new_fast_clear_type =
668 gen_mi_iand(&b, fast_clear_type, gen_mi_inot(&b, pred));
669 gen_mi_store(&b, fast_clear_type, new_fast_clear_type);
670 } else {
671 /* In this case, we're trying to do a partial resolve on a slice that
672 * doesn't have clear color. There's nothing to do.
673 */
674 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
675 return;
676 }
677
678 /* Set src1 to 0 and use a != condition */
679 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
680
681 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
682 mip.LoadOperation = LOAD_LOADINV;
683 mip.CombineOperation = COMBINE_SET;
684 mip.CompareOperation = COMPARE_SRCS_EQUAL;
685 }
686 }
687 #endif /* GEN_GEN >= 8 || GEN_IS_HASWELL */
688
689 #if GEN_GEN <= 8
690 static void
691 anv_cmd_simple_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
692 const struct anv_image *image,
693 VkImageAspectFlagBits aspect,
694 uint32_t level, uint32_t array_layer,
695 enum isl_aux_op resolve_op,
696 enum anv_fast_clear_type fast_clear_supported)
697 {
698 struct gen_mi_builder b;
699 gen_mi_builder_init(&b, &cmd_buffer->batch);
700
701 struct gen_mi_value fast_clear_type_mem =
702 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer->device,
703 image, aspect));
704
705 /* This only works for partial resolves and only when the clear color is
706 * all or nothing. On the upside, this emits less command streamer code
707 * and works on Ivybridge and Bay Trail.
708 */
709 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
710 assert(fast_clear_supported != ANV_FAST_CLEAR_ANY);
711
712 /* We don't support fast clears on anything other than the first slice. */
713 if (level > 0 || array_layer > 0)
714 return;
715
716 /* On gen8, we don't have a concept of default clear colors because we
717 * can't sample from CCS surfaces. It's enough to just load the fast clear
718 * state into the predicate register.
719 */
720 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), fast_clear_type_mem);
721 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
722 gen_mi_store(&b, fast_clear_type_mem, gen_mi_imm(0));
723
724 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
725 mip.LoadOperation = LOAD_LOADINV;
726 mip.CombineOperation = COMBINE_SET;
727 mip.CompareOperation = COMPARE_SRCS_EQUAL;
728 }
729 }
730 #endif /* GEN_GEN <= 8 */
731
732 static void
733 anv_cmd_predicated_ccs_resolve(struct anv_cmd_buffer *cmd_buffer,
734 const struct anv_image *image,
735 enum isl_format format,
736 VkImageAspectFlagBits aspect,
737 uint32_t level, uint32_t array_layer,
738 enum isl_aux_op resolve_op,
739 enum anv_fast_clear_type fast_clear_supported)
740 {
741 const uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
742
743 #if GEN_GEN >= 9
744 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
745 aspect, level, array_layer,
746 resolve_op, fast_clear_supported);
747 #else /* GEN_GEN <= 8 */
748 anv_cmd_simple_resolve_predicate(cmd_buffer, image,
749 aspect, level, array_layer,
750 resolve_op, fast_clear_supported);
751 #endif
752
753 /* CCS_D only supports full resolves and BLORP will assert on us if we try
754 * to do a partial resolve on a CCS_D surface.
755 */
756 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
757 image->planes[plane].aux_usage == ISL_AUX_USAGE_NONE)
758 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
759
760 anv_image_ccs_op(cmd_buffer, image, format, aspect, level,
761 array_layer, 1, resolve_op, NULL, true);
762 }
763
764 static void
765 anv_cmd_predicated_mcs_resolve(struct anv_cmd_buffer *cmd_buffer,
766 const struct anv_image *image,
767 enum isl_format format,
768 VkImageAspectFlagBits aspect,
769 uint32_t array_layer,
770 enum isl_aux_op resolve_op,
771 enum anv_fast_clear_type fast_clear_supported)
772 {
773 assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
774 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
775
776 #if GEN_GEN >= 8 || GEN_IS_HASWELL
777 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
778 aspect, 0, array_layer,
779 resolve_op, fast_clear_supported);
780
781 anv_image_mcs_op(cmd_buffer, image, format, aspect,
782 array_layer, 1, resolve_op, NULL, true);
783 #else
784 unreachable("MCS resolves are unsupported on Ivybridge and Bay Trail");
785 #endif
786 }
787
788 void
789 genX(cmd_buffer_mark_image_written)(struct anv_cmd_buffer *cmd_buffer,
790 const struct anv_image *image,
791 VkImageAspectFlagBits aspect,
792 enum isl_aux_usage aux_usage,
793 uint32_t level,
794 uint32_t base_layer,
795 uint32_t layer_count)
796 {
797 /* The aspect must be exactly one of the image aspects. */
798 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
799
800 /* The only compression types with more than just fast-clears are MCS,
801 * CCS_E, and HiZ. With HiZ we just trust the layout and don't actually
802 * track the current fast-clear and compression state. This leaves us
803 * with just MCS and CCS_E.
804 */
805 if (aux_usage != ISL_AUX_USAGE_CCS_E &&
806 aux_usage != ISL_AUX_USAGE_MCS)
807 return;
808
809 set_image_compressed_bit(cmd_buffer, image, aspect,
810 level, base_layer, layer_count, true);
811 }
812
813 static void
814 init_fast_clear_color(struct anv_cmd_buffer *cmd_buffer,
815 const struct anv_image *image,
816 VkImageAspectFlagBits aspect)
817 {
818 assert(cmd_buffer && image);
819 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
820
821 set_image_fast_clear_state(cmd_buffer, image, aspect,
822 ANV_FAST_CLEAR_NONE);
823
824 /* Initialize the struct fields that are accessed for fast-clears so that
825 * the HW restrictions on the field values are satisfied.
826 */
827 struct anv_address addr =
828 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
829
830 if (GEN_GEN >= 9) {
831 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
832 const unsigned num_dwords = GEN_GEN >= 10 ?
833 isl_dev->ss.clear_color_state_size / 4 :
834 isl_dev->ss.clear_value_size / 4;
835 for (unsigned i = 0; i < num_dwords; i++) {
836 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
837 sdi.Address = addr;
838 sdi.Address.offset += i * 4;
839 sdi.ImmediateData = 0;
840 }
841 }
842 } else {
843 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
844 sdi.Address = addr;
845 if (GEN_GEN >= 8 || GEN_IS_HASWELL) {
846 /* Pre-SKL, the dword containing the clear values also contains
847 * other fields, so we need to initialize those fields to match the
848 * values that would be in a color attachment.
849 */
850 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
851 ISL_CHANNEL_SELECT_GREEN << 22 |
852 ISL_CHANNEL_SELECT_BLUE << 19 |
853 ISL_CHANNEL_SELECT_ALPHA << 16;
854 } else if (GEN_GEN == 7) {
855 /* On IVB, the dword containing the clear values also contains
856 * other fields that must be zero or can be zero.
857 */
858 sdi.ImmediateData = 0;
859 }
860 }
861 }
862 }
863
864 /* Copy the fast-clear value dword(s) between a surface state object and an
865 * image's fast clear state buffer.
866 */
867 static void
868 genX(copy_fast_clear_dwords)(struct anv_cmd_buffer *cmd_buffer,
869 struct anv_state surface_state,
870 const struct anv_image *image,
871 VkImageAspectFlagBits aspect,
872 bool copy_from_surface_state)
873 {
874 assert(cmd_buffer && image);
875 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
876
877 struct anv_address ss_clear_addr = {
878 .bo = cmd_buffer->device->surface_state_pool.block_pool.bo,
879 .offset = surface_state.offset +
880 cmd_buffer->device->isl_dev.ss.clear_value_offset,
881 };
882 const struct anv_address entry_addr =
883 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
884 unsigned copy_size = cmd_buffer->device->isl_dev.ss.clear_value_size;
885
886 #if GEN_GEN == 7
887 /* On gen7, the combination of commands used here(MI_LOAD_REGISTER_MEM
888 * and MI_STORE_REGISTER_MEM) can cause GPU hangs if any rendering is
889 * in-flight when they are issued even if the memory touched is not
890 * currently active for rendering. The weird bit is that it is not the
891 * MI_LOAD/STORE_REGISTER_MEM commands which hang but rather the in-flight
892 * rendering hangs such that the next stalling command after the
893 * MI_LOAD/STORE_REGISTER_MEM commands will catch the hang.
894 *
895 * It is unclear exactly why this hang occurs. Both MI commands come with
896 * warnings about the 3D pipeline but that doesn't seem to fully explain
897 * it. My (Jason's) best theory is that it has something to do with the
898 * fact that we're using a GPU state register as our temporary and that
899 * something with reading/writing it is causing problems.
900 *
901 * In order to work around this issue, we emit a PIPE_CONTROL with the
902 * command streamer stall bit set.
903 */
904 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
905 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
906 #endif
907
908 struct gen_mi_builder b;
909 gen_mi_builder_init(&b, &cmd_buffer->batch);
910
911 if (copy_from_surface_state) {
912 gen_mi_memcpy(&b, entry_addr, ss_clear_addr, copy_size);
913 } else {
914 gen_mi_memcpy(&b, ss_clear_addr, entry_addr, copy_size);
915
916 /* Updating a surface state object may require that the state cache be
917 * invalidated. From the SKL PRM, Shared Functions -> State -> State
918 * Caching:
919 *
920 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
921 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
922 * modified [...], the L1 state cache must be invalidated to ensure
923 * the new surface or sampler state is fetched from system memory.
924 *
925 * In testing, SKL doesn't actually seem to need this, but HSW does.
926 */
927 cmd_buffer->state.pending_pipe_bits |=
928 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
929 }
930 }
931
932 /**
933 * @brief Transitions a color buffer from one layout to another.
934 *
935 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
936 * more information.
937 *
938 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
939 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
940 * this represents the maximum layers to transition at each
941 * specified miplevel.
942 */
943 static void
944 transition_color_buffer(struct anv_cmd_buffer *cmd_buffer,
945 const struct anv_image *image,
946 VkImageAspectFlagBits aspect,
947 const uint32_t base_level, uint32_t level_count,
948 uint32_t base_layer, uint32_t layer_count,
949 VkImageLayout initial_layout,
950 VkImageLayout final_layout)
951 {
952 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
953 /* Validate the inputs. */
954 assert(cmd_buffer);
955 assert(image && image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
956 /* These values aren't supported for simplicity's sake. */
957 assert(level_count != VK_REMAINING_MIP_LEVELS &&
958 layer_count != VK_REMAINING_ARRAY_LAYERS);
959 /* Ensure the subresource range is valid. */
960 UNUSED uint64_t last_level_num = base_level + level_count;
961 const uint32_t max_depth = anv_minify(image->extent.depth, base_level);
962 UNUSED const uint32_t image_layers = MAX2(image->array_size, max_depth);
963 assert((uint64_t)base_layer + layer_count <= image_layers);
964 assert(last_level_num <= image->levels);
965 /* The spec disallows these final layouts. */
966 assert(final_layout != VK_IMAGE_LAYOUT_UNDEFINED &&
967 final_layout != VK_IMAGE_LAYOUT_PREINITIALIZED);
968
969 /* No work is necessary if the layout stays the same or if this subresource
970 * range lacks auxiliary data.
971 */
972 if (initial_layout == final_layout)
973 return;
974
975 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
976
977 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
978 final_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
979 /* This surface is a linear compressed image with a tiled shadow surface
980 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
981 * we need to ensure the shadow copy is up-to-date.
982 */
983 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
984 assert(image->planes[plane].surface.isl.tiling == ISL_TILING_LINEAR);
985 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
986 assert(isl_format_is_compressed(image->planes[plane].surface.isl.format));
987 assert(plane == 0);
988 anv_image_copy_to_shadow(cmd_buffer, image,
989 VK_IMAGE_ASPECT_COLOR_BIT,
990 base_level, level_count,
991 base_layer, layer_count);
992 }
993
994 if (base_layer >= anv_image_aux_layers(image, aspect, base_level))
995 return;
996
997 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
998
999 if (initial_layout == VK_IMAGE_LAYOUT_UNDEFINED ||
1000 initial_layout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
1001 /* A subresource in the undefined layout may have been aliased and
1002 * populated with any arrangement of bits. Therefore, we must initialize
1003 * the related aux buffer and clear buffer entry with desirable values.
1004 * An initial layout of PREINITIALIZED is the same as UNDEFINED for
1005 * images with VK_IMAGE_TILING_OPTIMAL.
1006 *
1007 * Initialize the relevant clear buffer entries.
1008 */
1009 if (base_level == 0 && base_layer == 0)
1010 init_fast_clear_color(cmd_buffer, image, aspect);
1011
1012 /* Initialize the aux buffers to enable correct rendering. In order to
1013 * ensure that things such as storage images work correctly, aux buffers
1014 * need to be initialized to valid data.
1015 *
1016 * Having an aux buffer with invalid data is a problem for two reasons:
1017 *
1018 * 1) Having an invalid value in the buffer can confuse the hardware.
1019 * For instance, with CCS_E on SKL, a two-bit CCS value of 2 is
1020 * invalid and leads to the hardware doing strange things. It
1021 * doesn't hang as far as we can tell but rendering corruption can
1022 * occur.
1023 *
1024 * 2) If this transition is into the GENERAL layout and we then use the
1025 * image as a storage image, then we must have the aux buffer in the
1026 * pass-through state so that, if we then go to texture from the
1027 * image, we get the results of our storage image writes and not the
1028 * fast clear color or other random data.
1029 *
1030 * For CCS both of the problems above are real demonstrable issues. In
1031 * that case, the only thing we can do is to perform an ambiguate to
1032 * transition the aux surface into the pass-through state.
1033 *
1034 * For MCS, (2) is never an issue because we don't support multisampled
1035 * storage images. In theory, issue (1) is a problem with MCS but we've
1036 * never seen it in the wild. For 4x and 16x, all bit patters could, in
1037 * theory, be interpreted as something but we don't know that all bit
1038 * patterns are actually valid. For 2x and 8x, you could easily end up
1039 * with the MCS referring to an invalid plane because not all bits of
1040 * the MCS value are actually used. Even though we've never seen issues
1041 * in the wild, it's best to play it safe and initialize the MCS. We
1042 * can use a fast-clear for MCS because we only ever touch from render
1043 * and texture (no image load store).
1044 */
1045 if (image->samples == 1) {
1046 for (uint32_t l = 0; l < level_count; l++) {
1047 const uint32_t level = base_level + l;
1048
1049 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1050 if (base_layer >= aux_layers)
1051 break; /* We will only get fewer layers as level increases */
1052 uint32_t level_layer_count =
1053 MIN2(layer_count, aux_layers - base_layer);
1054
1055 anv_image_ccs_op(cmd_buffer, image,
1056 image->planes[plane].surface.isl.format,
1057 aspect, level, base_layer, level_layer_count,
1058 ISL_AUX_OP_AMBIGUATE, NULL, false);
1059
1060 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E) {
1061 set_image_compressed_bit(cmd_buffer, image, aspect,
1062 level, base_layer, level_layer_count,
1063 false);
1064 }
1065 }
1066 } else {
1067 if (image->samples == 4 || image->samples == 16) {
1068 anv_perf_warn(cmd_buffer->device->instance, image,
1069 "Doing a potentially unnecessary fast-clear to "
1070 "define an MCS buffer.");
1071 }
1072
1073 assert(base_level == 0 && level_count == 1);
1074 anv_image_mcs_op(cmd_buffer, image,
1075 image->planes[plane].surface.isl.format,
1076 aspect, base_layer, layer_count,
1077 ISL_AUX_OP_FAST_CLEAR, NULL, false);
1078 }
1079 return;
1080 }
1081
1082 const enum isl_aux_usage initial_aux_usage =
1083 anv_layout_to_aux_usage(devinfo, image, aspect, initial_layout);
1084 const enum isl_aux_usage final_aux_usage =
1085 anv_layout_to_aux_usage(devinfo, image, aspect, final_layout);
1086
1087 /* The current code assumes that there is no mixing of CCS_E and CCS_D.
1088 * We can handle transitions between CCS_D/E to and from NONE. What we
1089 * don't yet handle is switching between CCS_E and CCS_D within a given
1090 * image. Doing so in a performant way requires more detailed aux state
1091 * tracking such as what is done in i965. For now, just assume that we
1092 * only have one type of compression.
1093 */
1094 assert(initial_aux_usage == ISL_AUX_USAGE_NONE ||
1095 final_aux_usage == ISL_AUX_USAGE_NONE ||
1096 initial_aux_usage == final_aux_usage);
1097
1098 /* If initial aux usage is NONE, there is nothing to resolve */
1099 if (initial_aux_usage == ISL_AUX_USAGE_NONE)
1100 return;
1101
1102 enum isl_aux_op resolve_op = ISL_AUX_OP_NONE;
1103
1104 /* If the initial layout supports more fast clear than the final layout
1105 * then we need at least a partial resolve.
1106 */
1107 const enum anv_fast_clear_type initial_fast_clear =
1108 anv_layout_to_fast_clear_type(devinfo, image, aspect, initial_layout);
1109 const enum anv_fast_clear_type final_fast_clear =
1110 anv_layout_to_fast_clear_type(devinfo, image, aspect, final_layout);
1111 if (final_fast_clear < initial_fast_clear)
1112 resolve_op = ISL_AUX_OP_PARTIAL_RESOLVE;
1113
1114 if (initial_aux_usage == ISL_AUX_USAGE_CCS_E &&
1115 final_aux_usage != ISL_AUX_USAGE_CCS_E)
1116 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
1117
1118 if (resolve_op == ISL_AUX_OP_NONE)
1119 return;
1120
1121 /* Perform a resolve to synchronize data between the main and aux buffer.
1122 * Before we begin, we must satisfy the cache flushing requirement specified
1123 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
1124 *
1125 * Any transition from any value in {Clear, Render, Resolve} to a
1126 * different value in {Clear, Render, Resolve} requires end of pipe
1127 * synchronization.
1128 *
1129 * We perform a flush of the write cache before and after the clear and
1130 * resolve operations to meet this requirement.
1131 *
1132 * Unlike other drawing, fast clear operations are not properly
1133 * synchronized. The first PIPE_CONTROL here likely ensures that the
1134 * contents of the previous render or clear hit the render target before we
1135 * resolve and the second likely ensures that the resolve is complete before
1136 * we do any more rendering or clearing.
1137 */
1138 cmd_buffer->state.pending_pipe_bits |=
1139 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
1140
1141 for (uint32_t l = 0; l < level_count; l++) {
1142 uint32_t level = base_level + l;
1143
1144 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1145 if (base_layer >= aux_layers)
1146 break; /* We will only get fewer layers as level increases */
1147 uint32_t level_layer_count =
1148 MIN2(layer_count, aux_layers - base_layer);
1149
1150 for (uint32_t a = 0; a < level_layer_count; a++) {
1151 uint32_t array_layer = base_layer + a;
1152 if (image->samples == 1) {
1153 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
1154 image->planes[plane].surface.isl.format,
1155 aspect, level, array_layer, resolve_op,
1156 final_fast_clear);
1157 } else {
1158 /* We only support fast-clear on the first layer so partial
1159 * resolves should not be used on other layers as they will use
1160 * the clear color stored in memory that is only valid for layer0.
1161 */
1162 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
1163 array_layer != 0)
1164 continue;
1165
1166 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
1167 image->planes[plane].surface.isl.format,
1168 aspect, array_layer, resolve_op,
1169 final_fast_clear);
1170 }
1171 }
1172 }
1173
1174 cmd_buffer->state.pending_pipe_bits |=
1175 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
1176 }
1177
1178 /**
1179 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
1180 */
1181 static VkResult
1182 genX(cmd_buffer_setup_attachments)(struct anv_cmd_buffer *cmd_buffer,
1183 struct anv_render_pass *pass,
1184 const VkRenderPassBeginInfo *begin)
1185 {
1186 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
1187 struct anv_cmd_state *state = &cmd_buffer->state;
1188 struct anv_framebuffer *framebuffer = cmd_buffer->state.framebuffer;
1189
1190 vk_free(&cmd_buffer->pool->alloc, state->attachments);
1191
1192 if (pass->attachment_count > 0) {
1193 state->attachments = vk_alloc(&cmd_buffer->pool->alloc,
1194 pass->attachment_count *
1195 sizeof(state->attachments[0]),
1196 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1197 if (state->attachments == NULL) {
1198 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
1199 return anv_batch_set_error(&cmd_buffer->batch,
1200 VK_ERROR_OUT_OF_HOST_MEMORY);
1201 }
1202 } else {
1203 state->attachments = NULL;
1204 }
1205
1206 /* Reserve one for the NULL state. */
1207 unsigned num_states = 1;
1208 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1209 if (vk_format_is_color(pass->attachments[i].format))
1210 num_states++;
1211
1212 if (need_input_attachment_state(&pass->attachments[i]))
1213 num_states++;
1214 }
1215
1216 const uint32_t ss_stride = align_u32(isl_dev->ss.size, isl_dev->ss.align);
1217 state->render_pass_states =
1218 anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
1219 num_states * ss_stride, isl_dev->ss.align);
1220
1221 struct anv_state next_state = state->render_pass_states;
1222 next_state.alloc_size = isl_dev->ss.size;
1223
1224 state->null_surface_state = next_state;
1225 next_state.offset += ss_stride;
1226 next_state.map += ss_stride;
1227
1228 const VkRenderPassAttachmentBeginInfoKHR *begin_attachment =
1229 vk_find_struct_const(begin, RENDER_PASS_ATTACHMENT_BEGIN_INFO_KHR);
1230
1231 if (begin && !begin_attachment)
1232 assert(pass->attachment_count == framebuffer->attachment_count);
1233
1234 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1235 if (vk_format_is_color(pass->attachments[i].format)) {
1236 state->attachments[i].color.state = next_state;
1237 next_state.offset += ss_stride;
1238 next_state.map += ss_stride;
1239 }
1240
1241 if (need_input_attachment_state(&pass->attachments[i])) {
1242 state->attachments[i].input.state = next_state;
1243 next_state.offset += ss_stride;
1244 next_state.map += ss_stride;
1245 }
1246
1247 if (begin_attachment && begin_attachment->attachmentCount != 0) {
1248 assert(begin_attachment->attachmentCount == pass->attachment_count);
1249 ANV_FROM_HANDLE(anv_image_view, iview, begin_attachment->pAttachments[i]);
1250 cmd_buffer->state.attachments[i].image_view = iview;
1251 } else if (framebuffer && i < framebuffer->attachment_count) {
1252 cmd_buffer->state.attachments[i].image_view = framebuffer->attachments[i];
1253 }
1254 }
1255 assert(next_state.offset == state->render_pass_states.offset +
1256 state->render_pass_states.alloc_size);
1257
1258 if (begin) {
1259 isl_null_fill_state(isl_dev, state->null_surface_state.map,
1260 isl_extent3d(framebuffer->width,
1261 framebuffer->height,
1262 framebuffer->layers));
1263
1264 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1265 struct anv_render_pass_attachment *att = &pass->attachments[i];
1266 VkImageAspectFlags att_aspects = vk_format_aspects(att->format);
1267 VkImageAspectFlags clear_aspects = 0;
1268 VkImageAspectFlags load_aspects = 0;
1269
1270 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1271 /* color attachment */
1272 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1273 clear_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1274 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1275 load_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1276 }
1277 } else {
1278 /* depthstencil attachment */
1279 if (att_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1280 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1281 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1282 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1283 load_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1284 }
1285 }
1286 if (att_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
1287 if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1288 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1289 } else if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1290 load_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1291 }
1292 }
1293 }
1294
1295 state->attachments[i].current_layout = att->initial_layout;
1296 state->attachments[i].pending_clear_aspects = clear_aspects;
1297 state->attachments[i].pending_load_aspects = load_aspects;
1298 if (clear_aspects)
1299 state->attachments[i].clear_value = begin->pClearValues[i];
1300
1301 struct anv_image_view *iview = cmd_buffer->state.attachments[i].image_view;
1302 anv_assert(iview->vk_format == att->format);
1303
1304 const uint32_t num_layers = iview->planes[0].isl.array_len;
1305 state->attachments[i].pending_clear_views = (1 << num_layers) - 1;
1306
1307 union isl_color_value clear_color = { .u32 = { 0, } };
1308 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1309 anv_assert(iview->n_planes == 1);
1310 assert(att_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1311 color_attachment_compute_aux_usage(cmd_buffer->device,
1312 state, i, begin->renderArea,
1313 &clear_color);
1314
1315 anv_image_fill_surface_state(cmd_buffer->device,
1316 iview->image,
1317 VK_IMAGE_ASPECT_COLOR_BIT,
1318 &iview->planes[0].isl,
1319 ISL_SURF_USAGE_RENDER_TARGET_BIT,
1320 state->attachments[i].aux_usage,
1321 &clear_color,
1322 0,
1323 &state->attachments[i].color,
1324 NULL);
1325
1326 add_surface_state_relocs(cmd_buffer, state->attachments[i].color);
1327 } else {
1328 depth_stencil_attachment_compute_aux_usage(cmd_buffer->device,
1329 state, i,
1330 begin->renderArea);
1331 }
1332
1333 if (need_input_attachment_state(&pass->attachments[i])) {
1334 anv_image_fill_surface_state(cmd_buffer->device,
1335 iview->image,
1336 VK_IMAGE_ASPECT_COLOR_BIT,
1337 &iview->planes[0].isl,
1338 ISL_SURF_USAGE_TEXTURE_BIT,
1339 state->attachments[i].input_aux_usage,
1340 &clear_color,
1341 0,
1342 &state->attachments[i].input,
1343 NULL);
1344
1345 add_surface_state_relocs(cmd_buffer, state->attachments[i].input);
1346 }
1347 }
1348 }
1349
1350 return VK_SUCCESS;
1351 }
1352
1353 VkResult
1354 genX(BeginCommandBuffer)(
1355 VkCommandBuffer commandBuffer,
1356 const VkCommandBufferBeginInfo* pBeginInfo)
1357 {
1358 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1359
1360 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1361 * command buffer's state. Otherwise, we must *reset* its state. In both
1362 * cases we reset it.
1363 *
1364 * From the Vulkan 1.0 spec:
1365 *
1366 * If a command buffer is in the executable state and the command buffer
1367 * was allocated from a command pool with the
1368 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1369 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1370 * as if vkResetCommandBuffer had been called with
1371 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1372 * the command buffer in the recording state.
1373 */
1374 anv_cmd_buffer_reset(cmd_buffer);
1375
1376 cmd_buffer->usage_flags = pBeginInfo->flags;
1377
1378 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY ||
1379 !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT));
1380
1381 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1382
1383 /* We sometimes store vertex data in the dynamic state buffer for blorp
1384 * operations and our dynamic state stream may re-use data from previous
1385 * command buffers. In order to prevent stale cache data, we flush the VF
1386 * cache. We could do this on every blorp call but that's not really
1387 * needed as all of the data will get written by the CPU prior to the GPU
1388 * executing anything. The chances are fairly high that they will use
1389 * blorp at least once per primary command buffer so it shouldn't be
1390 * wasted.
1391 */
1392 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY)
1393 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1394
1395 /* We send an "Indirect State Pointers Disable" packet at
1396 * EndCommandBuffer, so all push contant packets are ignored during a
1397 * context restore. Documentation says after that command, we need to
1398 * emit push constants again before any rendering operation. So we
1399 * flag them dirty here to make sure they get emitted.
1400 */
1401 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1402
1403 VkResult result = VK_SUCCESS;
1404 if (cmd_buffer->usage_flags &
1405 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1406 assert(pBeginInfo->pInheritanceInfo);
1407 cmd_buffer->state.pass =
1408 anv_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
1409 cmd_buffer->state.subpass =
1410 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
1411
1412 /* This is optional in the inheritance info. */
1413 cmd_buffer->state.framebuffer =
1414 anv_framebuffer_from_handle(pBeginInfo->pInheritanceInfo->framebuffer);
1415
1416 result = genX(cmd_buffer_setup_attachments)(cmd_buffer,
1417 cmd_buffer->state.pass, NULL);
1418
1419 /* Record that HiZ is enabled if we can. */
1420 if (cmd_buffer->state.framebuffer) {
1421 const struct anv_image_view * const iview =
1422 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
1423
1424 if (iview) {
1425 VkImageLayout layout =
1426 cmd_buffer->state.subpass->depth_stencil_attachment->layout;
1427
1428 enum isl_aux_usage aux_usage =
1429 anv_layout_to_aux_usage(&cmd_buffer->device->info, iview->image,
1430 VK_IMAGE_ASPECT_DEPTH_BIT, layout);
1431
1432 cmd_buffer->state.hiz_enabled = aux_usage == ISL_AUX_USAGE_HIZ;
1433 }
1434 }
1435
1436 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
1437 }
1438
1439 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1440 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
1441 const VkCommandBufferInheritanceConditionalRenderingInfoEXT *conditional_rendering_info =
1442 vk_find_struct_const(pBeginInfo->pInheritanceInfo->pNext, COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT);
1443
1444 /* If secondary buffer supports conditional rendering
1445 * we should emit commands as if conditional rendering is enabled.
1446 */
1447 cmd_buffer->state.conditional_render_enabled =
1448 conditional_rendering_info && conditional_rendering_info->conditionalRenderingEnable;
1449 }
1450 #endif
1451
1452 return result;
1453 }
1454
1455 /* From the PRM, Volume 2a:
1456 *
1457 * "Indirect State Pointers Disable
1458 *
1459 * At the completion of the post-sync operation associated with this pipe
1460 * control packet, the indirect state pointers in the hardware are
1461 * considered invalid; the indirect pointers are not saved in the context.
1462 * If any new indirect state commands are executed in the command stream
1463 * while the pipe control is pending, the new indirect state commands are
1464 * preserved.
1465 *
1466 * [DevIVB+]: Using Invalidate State Pointer (ISP) only inhibits context
1467 * restoring of Push Constant (3DSTATE_CONSTANT_*) commands. Push Constant
1468 * commands are only considered as Indirect State Pointers. Once ISP is
1469 * issued in a context, SW must initialize by programming push constant
1470 * commands for all the shaders (at least to zero length) before attempting
1471 * any rendering operation for the same context."
1472 *
1473 * 3DSTATE_CONSTANT_* packets are restored during a context restore,
1474 * even though they point to a BO that has been already unreferenced at
1475 * the end of the previous batch buffer. This has been fine so far since
1476 * we are protected by these scratch page (every address not covered by
1477 * a BO should be pointing to the scratch page). But on CNL, it is
1478 * causing a GPU hang during context restore at the 3DSTATE_CONSTANT_*
1479 * instruction.
1480 *
1481 * The flag "Indirect State Pointers Disable" in PIPE_CONTROL tells the
1482 * hardware to ignore previous 3DSTATE_CONSTANT_* packets during a
1483 * context restore, so the mentioned hang doesn't happen. However,
1484 * software must program push constant commands for all stages prior to
1485 * rendering anything. So we flag them dirty in BeginCommandBuffer.
1486 *
1487 * Finally, we also make sure to stall at pixel scoreboard to make sure the
1488 * constants have been loaded into the EUs prior to disable the push constants
1489 * so that it doesn't hang a previous 3DPRIMITIVE.
1490 */
1491 static void
1492 emit_isp_disable(struct anv_cmd_buffer *cmd_buffer)
1493 {
1494 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1495 pc.StallAtPixelScoreboard = true;
1496 pc.CommandStreamerStallEnable = true;
1497 }
1498 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1499 pc.IndirectStatePointersDisable = true;
1500 pc.CommandStreamerStallEnable = true;
1501 }
1502 }
1503
1504 VkResult
1505 genX(EndCommandBuffer)(
1506 VkCommandBuffer commandBuffer)
1507 {
1508 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1509
1510 if (anv_batch_has_error(&cmd_buffer->batch))
1511 return cmd_buffer->batch.status;
1512
1513 /* We want every command buffer to start with the PMA fix in a known state,
1514 * so we disable it at the end of the command buffer.
1515 */
1516 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
1517
1518 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1519
1520 emit_isp_disable(cmd_buffer);
1521
1522 anv_cmd_buffer_end_batch_buffer(cmd_buffer);
1523
1524 return VK_SUCCESS;
1525 }
1526
1527 void
1528 genX(CmdExecuteCommands)(
1529 VkCommandBuffer commandBuffer,
1530 uint32_t commandBufferCount,
1531 const VkCommandBuffer* pCmdBuffers)
1532 {
1533 ANV_FROM_HANDLE(anv_cmd_buffer, primary, commandBuffer);
1534
1535 assert(primary->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1536
1537 if (anv_batch_has_error(&primary->batch))
1538 return;
1539
1540 /* The secondary command buffers will assume that the PMA fix is disabled
1541 * when they begin executing. Make sure this is true.
1542 */
1543 genX(cmd_buffer_enable_pma_fix)(primary, false);
1544
1545 /* The secondary command buffer doesn't know which textures etc. have been
1546 * flushed prior to their execution. Apply those flushes now.
1547 */
1548 genX(cmd_buffer_apply_pipe_flushes)(primary);
1549
1550 for (uint32_t i = 0; i < commandBufferCount; i++) {
1551 ANV_FROM_HANDLE(anv_cmd_buffer, secondary, pCmdBuffers[i]);
1552
1553 assert(secondary->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
1554 assert(!anv_batch_has_error(&secondary->batch));
1555
1556 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1557 if (secondary->state.conditional_render_enabled) {
1558 if (!primary->state.conditional_render_enabled) {
1559 /* Secondary buffer is constructed as if it will be executed
1560 * with conditional rendering, we should satisfy this dependency
1561 * regardless of conditional rendering being enabled in primary.
1562 */
1563 struct gen_mi_builder b;
1564 gen_mi_builder_init(&b, &primary->batch);
1565 gen_mi_store(&b, gen_mi_reg64(ANV_PREDICATE_RESULT_REG),
1566 gen_mi_imm(UINT64_MAX));
1567 }
1568 }
1569 #endif
1570
1571 if (secondary->usage_flags &
1572 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1573 /* If we're continuing a render pass from the primary, we need to
1574 * copy the surface states for the current subpass into the storage
1575 * we allocated for them in BeginCommandBuffer.
1576 */
1577 struct anv_bo *ss_bo =
1578 primary->device->surface_state_pool.block_pool.bo;
1579 struct anv_state src_state = primary->state.render_pass_states;
1580 struct anv_state dst_state = secondary->state.render_pass_states;
1581 assert(src_state.alloc_size == dst_state.alloc_size);
1582
1583 genX(cmd_buffer_so_memcpy)(primary,
1584 (struct anv_address) {
1585 .bo = ss_bo,
1586 .offset = dst_state.offset,
1587 },
1588 (struct anv_address) {
1589 .bo = ss_bo,
1590 .offset = src_state.offset,
1591 },
1592 src_state.alloc_size);
1593 }
1594
1595 anv_cmd_buffer_add_secondary(primary, secondary);
1596 }
1597
1598 /* The secondary may have selected a different pipeline (3D or compute) and
1599 * may have changed the current L3$ configuration. Reset our tracking
1600 * variables to invalid values to ensure that we re-emit these in the case
1601 * where we do any draws or compute dispatches from the primary after the
1602 * secondary has returned.
1603 */
1604 primary->state.current_pipeline = UINT32_MAX;
1605 primary->state.current_l3_config = NULL;
1606 primary->state.current_hash_scale = 0;
1607
1608 /* Each of the secondary command buffers will use its own state base
1609 * address. We need to re-emit state base address for the primary after
1610 * all of the secondaries are done.
1611 *
1612 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1613 * address calls?
1614 */
1615 genX(cmd_buffer_emit_state_base_address)(primary);
1616 }
1617
1618 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1619 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1620 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1621
1622 /**
1623 * Program the hardware to use the specified L3 configuration.
1624 */
1625 void
1626 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
1627 const struct gen_l3_config *cfg)
1628 {
1629 assert(cfg);
1630 if (cfg == cmd_buffer->state.current_l3_config)
1631 return;
1632
1633 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
1634 intel_logd("L3 config transition: ");
1635 gen_dump_l3_config(cfg, stderr);
1636 }
1637
1638 UNUSED const bool has_slm = cfg->n[GEN_L3P_SLM];
1639
1640 /* According to the hardware docs, the L3 partitioning can only be changed
1641 * while the pipeline is completely drained and the caches are flushed,
1642 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1643 */
1644 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1645 pc.DCFlushEnable = true;
1646 pc.PostSyncOperation = NoWrite;
1647 pc.CommandStreamerStallEnable = true;
1648 }
1649
1650 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1651 * invalidation of the relevant caches. Note that because RO invalidation
1652 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1653 * command is processed by the CS) we cannot combine it with the previous
1654 * stalling flush as the hardware documentation suggests, because that
1655 * would cause the CS to stall on previous rendering *after* RO
1656 * invalidation and wouldn't prevent the RO caches from being polluted by
1657 * concurrent rendering before the stall completes. This intentionally
1658 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1659 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1660 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1661 * already guarantee that there is no concurrent GPGPU kernel execution
1662 * (see SKL HSD 2132585).
1663 */
1664 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1665 pc.TextureCacheInvalidationEnable = true;
1666 pc.ConstantCacheInvalidationEnable = true;
1667 pc.InstructionCacheInvalidateEnable = true;
1668 pc.StateCacheInvalidationEnable = true;
1669 pc.PostSyncOperation = NoWrite;
1670 }
1671
1672 /* Now send a third stalling flush to make sure that invalidation is
1673 * complete when the L3 configuration registers are modified.
1674 */
1675 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1676 pc.DCFlushEnable = true;
1677 pc.PostSyncOperation = NoWrite;
1678 pc.CommandStreamerStallEnable = true;
1679 }
1680
1681 #if GEN_GEN >= 8
1682
1683 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
1684
1685 #if GEN_GEN >= 12
1686 #define L3_ALLOCATION_REG GENX(L3ALLOC)
1687 #define L3_ALLOCATION_REG_num GENX(L3ALLOC_num)
1688 #else
1689 #define L3_ALLOCATION_REG GENX(L3CNTLREG)
1690 #define L3_ALLOCATION_REG_num GENX(L3CNTLREG_num)
1691 #endif
1692
1693 uint32_t l3cr;
1694 anv_pack_struct(&l3cr, L3_ALLOCATION_REG,
1695 #if GEN_GEN < 12
1696 .SLMEnable = has_slm,
1697 #endif
1698 #if GEN_GEN == 11
1699 /* WA_1406697149: Bit 9 "Error Detection Behavior Control" must be set
1700 * in L3CNTLREG register. The default setting of the bit is not the
1701 * desirable behavior.
1702 */
1703 .ErrorDetectionBehaviorControl = true,
1704 .UseFullWays = true,
1705 #endif
1706 .URBAllocation = cfg->n[GEN_L3P_URB],
1707 .ROAllocation = cfg->n[GEN_L3P_RO],
1708 .DCAllocation = cfg->n[GEN_L3P_DC],
1709 .AllAllocation = cfg->n[GEN_L3P_ALL]);
1710
1711 /* Set up the L3 partitioning. */
1712 emit_lri(&cmd_buffer->batch, L3_ALLOCATION_REG_num, l3cr);
1713
1714 #else
1715
1716 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
1717 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
1718 cfg->n[GEN_L3P_ALL];
1719 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
1720 cfg->n[GEN_L3P_ALL];
1721 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
1722 cfg->n[GEN_L3P_ALL];
1723
1724 assert(!cfg->n[GEN_L3P_ALL]);
1725
1726 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1727 * the matching space on the remaining banks has to be allocated to a
1728 * client (URB for all validated configurations) set to the
1729 * lower-bandwidth 2-bank address hashing mode.
1730 */
1731 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
1732 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
1733 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
1734
1735 /* Minimum number of ways that can be allocated to the URB. */
1736 const unsigned n0_urb = devinfo->is_baytrail ? 32 : 0;
1737 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
1738
1739 uint32_t l3sqcr1, l3cr2, l3cr3;
1740 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
1741 .ConvertDC_UC = !has_dc,
1742 .ConvertIS_UC = !has_is,
1743 .ConvertC_UC = !has_c,
1744 .ConvertT_UC = !has_t);
1745 l3sqcr1 |=
1746 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
1747 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
1748 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
1749
1750 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
1751 .SLMEnable = has_slm,
1752 .URBLowBandwidth = urb_low_bw,
1753 .URBAllocation = cfg->n[GEN_L3P_URB] - n0_urb,
1754 #if !GEN_IS_HASWELL
1755 .ALLAllocation = cfg->n[GEN_L3P_ALL],
1756 #endif
1757 .ROAllocation = cfg->n[GEN_L3P_RO],
1758 .DCAllocation = cfg->n[GEN_L3P_DC]);
1759
1760 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
1761 .ISAllocation = cfg->n[GEN_L3P_IS],
1762 .ISLowBandwidth = 0,
1763 .CAllocation = cfg->n[GEN_L3P_C],
1764 .CLowBandwidth = 0,
1765 .TAllocation = cfg->n[GEN_L3P_T],
1766 .TLowBandwidth = 0);
1767
1768 /* Set up the L3 partitioning. */
1769 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
1770 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
1771 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
1772
1773 #if GEN_IS_HASWELL
1774 if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
1775 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1776 * them disabled to avoid crashing the system hard.
1777 */
1778 uint32_t scratch1, chicken3;
1779 anv_pack_struct(&scratch1, GENX(SCRATCH1),
1780 .L3AtomicDisable = !has_dc);
1781 anv_pack_struct(&chicken3, GENX(CHICKEN3),
1782 .L3AtomicDisableMask = true,
1783 .L3AtomicDisable = !has_dc);
1784 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
1785 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
1786 }
1787 #endif
1788
1789 #endif
1790
1791 cmd_buffer->state.current_l3_config = cfg;
1792 }
1793
1794 void
1795 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
1796 {
1797 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
1798
1799 /* Flushes are pipelined while invalidations are handled immediately.
1800 * Therefore, if we're flushing anything then we need to schedule a stall
1801 * before any invalidations can happen.
1802 */
1803 if (bits & ANV_PIPE_FLUSH_BITS)
1804 bits |= ANV_PIPE_NEEDS_CS_STALL_BIT;
1805
1806 /* If we're going to do an invalidate and we have a pending CS stall that
1807 * has yet to be resolved, we do the CS stall now.
1808 */
1809 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
1810 (bits & ANV_PIPE_NEEDS_CS_STALL_BIT)) {
1811 bits |= ANV_PIPE_CS_STALL_BIT;
1812 bits &= ~ANV_PIPE_NEEDS_CS_STALL_BIT;
1813 }
1814
1815 if (GEN_GEN >= 12 &&
1816 ((bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT) ||
1817 (bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT))) {
1818 /* From the PIPE_CONTROL instruction table, bit 28 (Tile Cache Flush
1819 * Enable):
1820 *
1821 * Unified Cache (Tile Cache Disabled):
1822 *
1823 * When the Color and Depth (Z) streams are enabled to be cached in
1824 * the DC space of L2, Software must use "Render Target Cache Flush
1825 * Enable" and "Depth Cache Flush Enable" along with "Tile Cache
1826 * Flush" for getting the color and depth (Z) write data to be
1827 * globally observable. In this mode of operation it is not required
1828 * to set "CS Stall" upon setting "Tile Cache Flush" bit.
1829 */
1830 bits |= ANV_PIPE_TILE_CACHE_FLUSH_BIT;
1831 }
1832
1833 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT)) {
1834 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1835 #if GEN_GEN >= 12
1836 pipe.TileCacheFlushEnable = bits & ANV_PIPE_TILE_CACHE_FLUSH_BIT;
1837 #endif
1838 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1839 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1840 pipe.RenderTargetCacheFlushEnable =
1841 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1842
1843 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
1844 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
1845 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
1846
1847 /*
1848 * According to the Broadwell documentation, any PIPE_CONTROL with the
1849 * "Command Streamer Stall" bit set must also have another bit set,
1850 * with five different options:
1851 *
1852 * - Render Target Cache Flush
1853 * - Depth Cache Flush
1854 * - Stall at Pixel Scoreboard
1855 * - Post-Sync Operation
1856 * - Depth Stall
1857 * - DC Flush Enable
1858 *
1859 * I chose "Stall at Pixel Scoreboard" since that's what we use in
1860 * mesa and it seems to work fine. The choice is fairly arbitrary.
1861 */
1862 if ((bits & ANV_PIPE_CS_STALL_BIT) &&
1863 !(bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_DEPTH_STALL_BIT |
1864 ANV_PIPE_STALL_AT_SCOREBOARD_BIT)))
1865 pipe.StallAtPixelScoreboard = true;
1866 }
1867
1868 /* If a render target flush was emitted, then we can toggle off the bit
1869 * saying that render target writes are ongoing.
1870 */
1871 if (bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1872 bits &= ~(ANV_PIPE_RENDER_TARGET_BUFFER_WRITES);
1873
1874 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT);
1875 }
1876
1877 if (bits & ANV_PIPE_INVALIDATE_BITS) {
1878 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
1879 *
1880 * "If the VF Cache Invalidation Enable is set to a 1 in a
1881 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields sets to
1882 * 0, with the VF Cache Invalidation Enable set to 0 needs to be sent
1883 * prior to the PIPE_CONTROL with VF Cache Invalidation Enable set to
1884 * a 1."
1885 *
1886 * This appears to hang Broadwell, so we restrict it to just gen9.
1887 */
1888 if (GEN_GEN == 9 && (bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT))
1889 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe);
1890
1891 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1892 pipe.StateCacheInvalidationEnable =
1893 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
1894 pipe.ConstantCacheInvalidationEnable =
1895 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1896 pipe.VFCacheInvalidationEnable =
1897 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1898 pipe.TextureCacheInvalidationEnable =
1899 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1900 pipe.InstructionCacheInvalidateEnable =
1901 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
1902
1903 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
1904 *
1905 * "When VF Cache Invalidate is set “Post Sync Operation” must be
1906 * enabled to “Write Immediate Data” or “Write PS Depth Count” or
1907 * “Write Timestamp”.
1908 */
1909 if (GEN_GEN == 9 && pipe.VFCacheInvalidationEnable) {
1910 pipe.PostSyncOperation = WriteImmediateData;
1911 pipe.Address =
1912 (struct anv_address) { cmd_buffer->device->workaround_bo, 0 };
1913 }
1914 }
1915
1916 bits &= ~ANV_PIPE_INVALIDATE_BITS;
1917 }
1918
1919 cmd_buffer->state.pending_pipe_bits = bits;
1920 }
1921
1922 void genX(CmdPipelineBarrier)(
1923 VkCommandBuffer commandBuffer,
1924 VkPipelineStageFlags srcStageMask,
1925 VkPipelineStageFlags destStageMask,
1926 VkBool32 byRegion,
1927 uint32_t memoryBarrierCount,
1928 const VkMemoryBarrier* pMemoryBarriers,
1929 uint32_t bufferMemoryBarrierCount,
1930 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1931 uint32_t imageMemoryBarrierCount,
1932 const VkImageMemoryBarrier* pImageMemoryBarriers)
1933 {
1934 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1935
1936 /* XXX: Right now, we're really dumb and just flush whatever categories
1937 * the app asks for. One of these days we may make this a bit better
1938 * but right now that's all the hardware allows for in most areas.
1939 */
1940 VkAccessFlags src_flags = 0;
1941 VkAccessFlags dst_flags = 0;
1942
1943 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
1944 src_flags |= pMemoryBarriers[i].srcAccessMask;
1945 dst_flags |= pMemoryBarriers[i].dstAccessMask;
1946 }
1947
1948 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
1949 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
1950 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
1951 }
1952
1953 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1954 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
1955 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
1956 ANV_FROM_HANDLE(anv_image, image, pImageMemoryBarriers[i].image);
1957 const VkImageSubresourceRange *range =
1958 &pImageMemoryBarriers[i].subresourceRange;
1959
1960 uint32_t base_layer, layer_count;
1961 if (image->type == VK_IMAGE_TYPE_3D) {
1962 base_layer = 0;
1963 layer_count = anv_minify(image->extent.depth, range->baseMipLevel);
1964 } else {
1965 base_layer = range->baseArrayLayer;
1966 layer_count = anv_get_layerCount(image, range);
1967 }
1968
1969 if (range->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1970 transition_depth_buffer(cmd_buffer, image,
1971 pImageMemoryBarriers[i].oldLayout,
1972 pImageMemoryBarriers[i].newLayout);
1973 }
1974
1975 if (range->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1976 transition_stencil_buffer(cmd_buffer, image,
1977 range->baseMipLevel,
1978 anv_get_levelCount(image, range),
1979 base_layer, layer_count,
1980 pImageMemoryBarriers[i].oldLayout,
1981 pImageMemoryBarriers[i].newLayout);
1982 }
1983
1984 if (range->aspectMask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1985 VkImageAspectFlags color_aspects =
1986 anv_image_expand_aspects(image, range->aspectMask);
1987 uint32_t aspect_bit;
1988 anv_foreach_image_aspect_bit(aspect_bit, image, color_aspects) {
1989 transition_color_buffer(cmd_buffer, image, 1UL << aspect_bit,
1990 range->baseMipLevel,
1991 anv_get_levelCount(image, range),
1992 base_layer, layer_count,
1993 pImageMemoryBarriers[i].oldLayout,
1994 pImageMemoryBarriers[i].newLayout);
1995 }
1996 }
1997 }
1998
1999 cmd_buffer->state.pending_pipe_bits |=
2000 anv_pipe_flush_bits_for_access_flags(src_flags) |
2001 anv_pipe_invalidate_bits_for_access_flags(dst_flags);
2002 }
2003
2004 static void
2005 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
2006 {
2007 VkShaderStageFlags stages =
2008 cmd_buffer->state.gfx.base.pipeline->active_stages;
2009
2010 /* In order to avoid thrash, we assume that vertex and fragment stages
2011 * always exist. In the rare case where one is missing *and* the other
2012 * uses push concstants, this may be suboptimal. However, avoiding stalls
2013 * seems more important.
2014 */
2015 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
2016
2017 if (stages == cmd_buffer->state.push_constant_stages)
2018 return;
2019
2020 #if GEN_GEN >= 8
2021 const unsigned push_constant_kb = 32;
2022 #elif GEN_IS_HASWELL
2023 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
2024 #else
2025 const unsigned push_constant_kb = 16;
2026 #endif
2027
2028 const unsigned num_stages =
2029 util_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
2030 unsigned size_per_stage = push_constant_kb / num_stages;
2031
2032 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
2033 * units of 2KB. Incidentally, these are the same platforms that have
2034 * 32KB worth of push constant space.
2035 */
2036 if (push_constant_kb == 32)
2037 size_per_stage &= ~1u;
2038
2039 uint32_t kb_used = 0;
2040 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
2041 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
2042 anv_batch_emit(&cmd_buffer->batch,
2043 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
2044 alloc._3DCommandSubOpcode = 18 + i;
2045 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
2046 alloc.ConstantBufferSize = push_size;
2047 }
2048 kb_used += push_size;
2049 }
2050
2051 anv_batch_emit(&cmd_buffer->batch,
2052 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
2053 alloc.ConstantBufferOffset = kb_used;
2054 alloc.ConstantBufferSize = push_constant_kb - kb_used;
2055 }
2056
2057 cmd_buffer->state.push_constant_stages = stages;
2058
2059 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
2060 *
2061 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
2062 * the next 3DPRIMITIVE command after programming the
2063 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
2064 *
2065 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
2066 * pipeline setup, we need to dirty push constants.
2067 */
2068 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
2069 }
2070
2071 static const struct anv_descriptor *
2072 anv_descriptor_for_binding(const struct anv_cmd_pipeline_state *pipe_state,
2073 const struct anv_pipeline_binding *binding)
2074 {
2075 assert(binding->set < MAX_SETS);
2076 const struct anv_descriptor_set *set =
2077 pipe_state->descriptors[binding->set];
2078 const uint32_t offset =
2079 set->layout->binding[binding->binding].descriptor_index;
2080 return &set->descriptors[offset + binding->index];
2081 }
2082
2083 static uint32_t
2084 dynamic_offset_for_binding(const struct anv_cmd_pipeline_state *pipe_state,
2085 const struct anv_pipeline_binding *binding)
2086 {
2087 assert(binding->set < MAX_SETS);
2088 const struct anv_descriptor_set *set =
2089 pipe_state->descriptors[binding->set];
2090
2091 uint32_t dynamic_offset_idx =
2092 pipe_state->layout->set[binding->set].dynamic_offset_start +
2093 set->layout->binding[binding->binding].dynamic_offset_index +
2094 binding->index;
2095
2096 return pipe_state->dynamic_offsets[dynamic_offset_idx];
2097 }
2098
2099 static struct anv_address
2100 anv_descriptor_set_address(struct anv_cmd_buffer *cmd_buffer,
2101 struct anv_descriptor_set *set)
2102 {
2103 if (set->pool) {
2104 /* This is a normal descriptor set */
2105 return (struct anv_address) {
2106 .bo = set->pool->bo,
2107 .offset = set->desc_mem.offset,
2108 };
2109 } else {
2110 /* This is a push descriptor set. We have to flag it as used on the GPU
2111 * so that the next time we push descriptors, we grab a new memory.
2112 */
2113 struct anv_push_descriptor_set *push_set =
2114 (struct anv_push_descriptor_set *)set;
2115 push_set->set_used_on_gpu = true;
2116
2117 return (struct anv_address) {
2118 .bo = cmd_buffer->dynamic_state_stream.state_pool->block_pool.bo,
2119 .offset = set->desc_mem.offset,
2120 };
2121 }
2122 }
2123
2124 static VkResult
2125 emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
2126 gl_shader_stage stage,
2127 struct anv_state *bt_state)
2128 {
2129 struct anv_subpass *subpass = cmd_buffer->state.subpass;
2130 struct anv_cmd_pipeline_state *pipe_state;
2131 struct anv_pipeline *pipeline;
2132 uint32_t state_offset;
2133
2134 switch (stage) {
2135 case MESA_SHADER_COMPUTE:
2136 pipe_state = &cmd_buffer->state.compute.base;
2137 break;
2138 default:
2139 pipe_state = &cmd_buffer->state.gfx.base;
2140 break;
2141 }
2142 pipeline = pipe_state->pipeline;
2143
2144 if (!anv_pipeline_has_stage(pipeline, stage)) {
2145 *bt_state = (struct anv_state) { 0, };
2146 return VK_SUCCESS;
2147 }
2148
2149 struct anv_shader_bin *bin = pipeline->shaders[stage];
2150 struct anv_pipeline_bind_map *map = &bin->bind_map;
2151 if (map->surface_count == 0) {
2152 *bt_state = (struct anv_state) { 0, };
2153 return VK_SUCCESS;
2154 }
2155
2156 *bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer,
2157 map->surface_count,
2158 &state_offset);
2159 uint32_t *bt_map = bt_state->map;
2160
2161 if (bt_state->map == NULL)
2162 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2163
2164 /* We only need to emit relocs if we're not using softpin. If we are using
2165 * softpin then we always keep all user-allocated memory objects resident.
2166 */
2167 const bool need_client_mem_relocs =
2168 !cmd_buffer->device->instance->physicalDevice.use_softpin;
2169
2170 for (uint32_t s = 0; s < map->surface_count; s++) {
2171 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[s];
2172
2173 struct anv_state surface_state;
2174
2175 if (binding->set == ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS) {
2176 /* Color attachment binding */
2177 assert(stage == MESA_SHADER_FRAGMENT);
2178 assert(binding->binding == 0);
2179 if (binding->index < subpass->color_count) {
2180 const unsigned att =
2181 subpass->color_attachments[binding->index].attachment;
2182
2183 /* From the Vulkan 1.0.46 spec:
2184 *
2185 * "If any color or depth/stencil attachments are
2186 * VK_ATTACHMENT_UNUSED, then no writes occur for those
2187 * attachments."
2188 */
2189 if (att == VK_ATTACHMENT_UNUSED) {
2190 surface_state = cmd_buffer->state.null_surface_state;
2191 } else {
2192 surface_state = cmd_buffer->state.attachments[att].color.state;
2193 }
2194 } else {
2195 surface_state = cmd_buffer->state.null_surface_state;
2196 }
2197
2198 bt_map[s] = surface_state.offset + state_offset;
2199 continue;
2200 } else if (binding->set == ANV_DESCRIPTOR_SET_SHADER_CONSTANTS) {
2201 struct anv_state surface_state =
2202 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
2203
2204 struct anv_address constant_data = {
2205 .bo = pipeline->device->dynamic_state_pool.block_pool.bo,
2206 .offset = pipeline->shaders[stage]->constant_data.offset,
2207 };
2208 unsigned constant_data_size =
2209 pipeline->shaders[stage]->constant_data_size;
2210
2211 const enum isl_format format =
2212 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
2213 anv_fill_buffer_surface_state(cmd_buffer->device,
2214 surface_state, format,
2215 constant_data, constant_data_size, 1);
2216
2217 bt_map[s] = surface_state.offset + state_offset;
2218 add_surface_reloc(cmd_buffer, surface_state, constant_data);
2219 continue;
2220 } else if (binding->set == ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS) {
2221 /* This is always the first binding for compute shaders */
2222 assert(stage == MESA_SHADER_COMPUTE && s == 0);
2223 if (!get_cs_prog_data(pipeline)->uses_num_work_groups)
2224 continue;
2225
2226 struct anv_state surface_state =
2227 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
2228
2229 const enum isl_format format =
2230 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2231 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2232 format,
2233 cmd_buffer->state.compute.num_workgroups,
2234 12, 1);
2235 bt_map[s] = surface_state.offset + state_offset;
2236 if (need_client_mem_relocs) {
2237 add_surface_reloc(cmd_buffer, surface_state,
2238 cmd_buffer->state.compute.num_workgroups);
2239 }
2240 continue;
2241 } else if (binding->set == ANV_DESCRIPTOR_SET_DESCRIPTORS) {
2242 /* This is a descriptor set buffer so the set index is actually
2243 * given by binding->binding. (Yes, that's confusing.)
2244 */
2245 struct anv_descriptor_set *set =
2246 pipe_state->descriptors[binding->binding];
2247 assert(set->desc_mem.alloc_size);
2248 assert(set->desc_surface_state.alloc_size);
2249 bt_map[s] = set->desc_surface_state.offset + state_offset;
2250 add_surface_reloc(cmd_buffer, set->desc_surface_state,
2251 anv_descriptor_set_address(cmd_buffer, set));
2252 continue;
2253 }
2254
2255 const struct anv_descriptor *desc =
2256 anv_descriptor_for_binding(pipe_state, binding);
2257
2258 switch (desc->type) {
2259 case VK_DESCRIPTOR_TYPE_SAMPLER:
2260 /* Nothing for us to do here */
2261 continue;
2262
2263 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2264 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
2265 struct anv_surface_state sstate =
2266 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2267 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2268 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2269 surface_state = sstate.state;
2270 assert(surface_state.alloc_size);
2271 if (need_client_mem_relocs)
2272 add_surface_state_relocs(cmd_buffer, sstate);
2273 break;
2274 }
2275 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2276 assert(stage == MESA_SHADER_FRAGMENT);
2277 if ((desc->image_view->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0) {
2278 /* For depth and stencil input attachments, we treat it like any
2279 * old texture that a user may have bound.
2280 */
2281 struct anv_surface_state sstate =
2282 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2283 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2284 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2285 surface_state = sstate.state;
2286 assert(surface_state.alloc_size);
2287 if (need_client_mem_relocs)
2288 add_surface_state_relocs(cmd_buffer, sstate);
2289 } else {
2290 /* For color input attachments, we create the surface state at
2291 * vkBeginRenderPass time so that we can include aux and clear
2292 * color information.
2293 */
2294 assert(binding->input_attachment_index < subpass->input_count);
2295 const unsigned subpass_att = binding->input_attachment_index;
2296 const unsigned att = subpass->input_attachments[subpass_att].attachment;
2297 surface_state = cmd_buffer->state.attachments[att].input.state;
2298 }
2299 break;
2300
2301 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2302 struct anv_surface_state sstate = (binding->write_only)
2303 ? desc->image_view->planes[binding->plane].writeonly_storage_surface_state
2304 : desc->image_view->planes[binding->plane].storage_surface_state;
2305 surface_state = sstate.state;
2306 assert(surface_state.alloc_size);
2307 if (need_client_mem_relocs)
2308 add_surface_state_relocs(cmd_buffer, sstate);
2309 break;
2310 }
2311
2312 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2313 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2314 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2315 surface_state = desc->buffer_view->surface_state;
2316 assert(surface_state.alloc_size);
2317 if (need_client_mem_relocs) {
2318 add_surface_reloc(cmd_buffer, surface_state,
2319 desc->buffer_view->address);
2320 }
2321 break;
2322
2323 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2324 /* If the shader never does any UBO pulls (this is a fairly common
2325 * case) then we don't need to fill out those binding table entries.
2326 * The real cost savings here is that we don't have to build the
2327 * surface state for them which is surprisingly expensive when it's
2328 * on the hot-path.
2329 */
2330 if (!bin->prog_data->has_ubo_pull)
2331 continue;
2332 /* Fall through */
2333
2334 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2335 /* Compute the offset within the buffer */
2336 uint32_t dynamic_offset =
2337 dynamic_offset_for_binding(pipe_state, binding);
2338 uint64_t offset = desc->offset + dynamic_offset;
2339 /* Clamp to the buffer size */
2340 offset = MIN2(offset, desc->buffer->size);
2341 /* Clamp the range to the buffer size */
2342 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
2343
2344 struct anv_address address =
2345 anv_address_add(desc->buffer->address, offset);
2346
2347 surface_state =
2348 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
2349 enum isl_format format =
2350 anv_isl_format_for_descriptor_type(desc->type);
2351
2352 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2353 format, address, range, 1);
2354 if (need_client_mem_relocs)
2355 add_surface_reloc(cmd_buffer, surface_state, address);
2356 break;
2357 }
2358
2359 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2360 surface_state = (binding->write_only)
2361 ? desc->buffer_view->writeonly_storage_surface_state
2362 : desc->buffer_view->storage_surface_state;
2363 assert(surface_state.alloc_size);
2364 if (need_client_mem_relocs) {
2365 add_surface_reloc(cmd_buffer, surface_state,
2366 desc->buffer_view->address);
2367 }
2368 break;
2369
2370 default:
2371 assert(!"Invalid descriptor type");
2372 continue;
2373 }
2374
2375 bt_map[s] = surface_state.offset + state_offset;
2376 }
2377
2378 return VK_SUCCESS;
2379 }
2380
2381 static VkResult
2382 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
2383 gl_shader_stage stage,
2384 struct anv_state *state)
2385 {
2386 struct anv_cmd_pipeline_state *pipe_state =
2387 stage == MESA_SHADER_COMPUTE ? &cmd_buffer->state.compute.base :
2388 &cmd_buffer->state.gfx.base;
2389 struct anv_pipeline *pipeline = pipe_state->pipeline;
2390
2391 if (!anv_pipeline_has_stage(pipeline, stage)) {
2392 *state = (struct anv_state) { 0, };
2393 return VK_SUCCESS;
2394 }
2395
2396 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
2397 if (map->sampler_count == 0) {
2398 *state = (struct anv_state) { 0, };
2399 return VK_SUCCESS;
2400 }
2401
2402 uint32_t size = map->sampler_count * 16;
2403 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
2404
2405 if (state->map == NULL)
2406 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2407
2408 for (uint32_t s = 0; s < map->sampler_count; s++) {
2409 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
2410 const struct anv_descriptor *desc =
2411 anv_descriptor_for_binding(pipe_state, binding);
2412
2413 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
2414 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2415 continue;
2416
2417 struct anv_sampler *sampler = desc->sampler;
2418
2419 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2420 * happens to be zero.
2421 */
2422 if (sampler == NULL)
2423 continue;
2424
2425 memcpy(state->map + (s * 16),
2426 sampler->state[binding->plane], sizeof(sampler->state[0]));
2427 }
2428
2429 return VK_SUCCESS;
2430 }
2431
2432 static uint32_t
2433 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2434 {
2435 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2436
2437 VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty &
2438 pipeline->active_stages;
2439
2440 VkResult result = VK_SUCCESS;
2441 anv_foreach_stage(s, dirty) {
2442 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2443 if (result != VK_SUCCESS)
2444 break;
2445 result = emit_binding_table(cmd_buffer, s,
2446 &cmd_buffer->state.binding_tables[s]);
2447 if (result != VK_SUCCESS)
2448 break;
2449 }
2450
2451 if (result != VK_SUCCESS) {
2452 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2453
2454 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2455 if (result != VK_SUCCESS)
2456 return 0;
2457
2458 /* Re-emit state base addresses so we get the new surface state base
2459 * address before we start emitting binding tables etc.
2460 */
2461 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2462
2463 /* Re-emit all active binding tables */
2464 dirty |= pipeline->active_stages;
2465 anv_foreach_stage(s, dirty) {
2466 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2467 if (result != VK_SUCCESS) {
2468 anv_batch_set_error(&cmd_buffer->batch, result);
2469 return 0;
2470 }
2471 result = emit_binding_table(cmd_buffer, s,
2472 &cmd_buffer->state.binding_tables[s]);
2473 if (result != VK_SUCCESS) {
2474 anv_batch_set_error(&cmd_buffer->batch, result);
2475 return 0;
2476 }
2477 }
2478 }
2479
2480 cmd_buffer->state.descriptors_dirty &= ~dirty;
2481
2482 return dirty;
2483 }
2484
2485 static void
2486 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
2487 uint32_t stages)
2488 {
2489 static const uint32_t sampler_state_opcodes[] = {
2490 [MESA_SHADER_VERTEX] = 43,
2491 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
2492 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
2493 [MESA_SHADER_GEOMETRY] = 46,
2494 [MESA_SHADER_FRAGMENT] = 47,
2495 [MESA_SHADER_COMPUTE] = 0,
2496 };
2497
2498 static const uint32_t binding_table_opcodes[] = {
2499 [MESA_SHADER_VERTEX] = 38,
2500 [MESA_SHADER_TESS_CTRL] = 39,
2501 [MESA_SHADER_TESS_EVAL] = 40,
2502 [MESA_SHADER_GEOMETRY] = 41,
2503 [MESA_SHADER_FRAGMENT] = 42,
2504 [MESA_SHADER_COMPUTE] = 0,
2505 };
2506
2507 anv_foreach_stage(s, stages) {
2508 assert(s < ARRAY_SIZE(binding_table_opcodes));
2509 assert(binding_table_opcodes[s] > 0);
2510
2511 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
2512 anv_batch_emit(&cmd_buffer->batch,
2513 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
2514 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
2515 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
2516 }
2517 }
2518
2519 /* Always emit binding table pointers if we're asked to, since on SKL
2520 * this is what flushes push constants. */
2521 anv_batch_emit(&cmd_buffer->batch,
2522 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
2523 btp._3DCommandSubOpcode = binding_table_opcodes[s];
2524 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
2525 }
2526 }
2527 }
2528
2529 static void
2530 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer,
2531 VkShaderStageFlags dirty_stages)
2532 {
2533 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
2534 const struct anv_pipeline *pipeline = gfx_state->base.pipeline;
2535
2536 static const uint32_t push_constant_opcodes[] = {
2537 [MESA_SHADER_VERTEX] = 21,
2538 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2539 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2540 [MESA_SHADER_GEOMETRY] = 22,
2541 [MESA_SHADER_FRAGMENT] = 23,
2542 [MESA_SHADER_COMPUTE] = 0,
2543 };
2544
2545 VkShaderStageFlags flushed = 0;
2546
2547 anv_foreach_stage(stage, dirty_stages) {
2548 assert(stage < ARRAY_SIZE(push_constant_opcodes));
2549 assert(push_constant_opcodes[stage] > 0);
2550
2551 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
2552 c._3DCommandSubOpcode = push_constant_opcodes[stage];
2553
2554 if (anv_pipeline_has_stage(pipeline, stage)) {
2555 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2556 const struct brw_stage_prog_data *prog_data =
2557 pipeline->shaders[stage]->prog_data;
2558 const struct anv_pipeline_bind_map *bind_map =
2559 &pipeline->shaders[stage]->bind_map;
2560
2561 /* The Skylake PRM contains the following restriction:
2562 *
2563 * "The driver must ensure The following case does not occur
2564 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2565 * buffer 3 read length equal to zero committed followed by a
2566 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2567 * zero committed."
2568 *
2569 * To avoid this, we program the buffers in the highest slots.
2570 * This way, slot 0 is only used if slot 3 is also used.
2571 */
2572 int n = 3;
2573
2574 for (int i = 3; i >= 0; i--) {
2575 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2576 if (range->length == 0)
2577 continue;
2578
2579 const unsigned surface =
2580 prog_data->binding_table.ubo_start + range->block;
2581
2582 assert(surface <= bind_map->surface_count);
2583 const struct anv_pipeline_binding *binding =
2584 &bind_map->surface_to_descriptor[surface];
2585
2586 struct anv_address read_addr;
2587 uint32_t read_len;
2588 if (binding->set == ANV_DESCRIPTOR_SET_SHADER_CONSTANTS) {
2589 struct anv_address constant_data = {
2590 .bo = pipeline->device->dynamic_state_pool.block_pool.bo,
2591 .offset = pipeline->shaders[stage]->constant_data.offset,
2592 };
2593 unsigned constant_data_size =
2594 pipeline->shaders[stage]->constant_data_size;
2595
2596 read_len = MIN2(range->length,
2597 DIV_ROUND_UP(constant_data_size, 32) - range->start);
2598 read_addr = anv_address_add(constant_data,
2599 range->start * 32);
2600 } else if (binding->set == ANV_DESCRIPTOR_SET_DESCRIPTORS) {
2601 /* This is a descriptor set buffer so the set index is
2602 * actually given by binding->binding. (Yes, that's
2603 * confusing.)
2604 */
2605 struct anv_descriptor_set *set =
2606 gfx_state->base.descriptors[binding->binding];
2607 struct anv_address desc_buffer_addr =
2608 anv_descriptor_set_address(cmd_buffer, set);
2609 const unsigned desc_buffer_size = set->desc_mem.alloc_size;
2610
2611 read_len = MIN2(range->length,
2612 DIV_ROUND_UP(desc_buffer_size, 32) - range->start);
2613 read_addr = anv_address_add(desc_buffer_addr,
2614 range->start * 32);
2615 } else {
2616 const struct anv_descriptor *desc =
2617 anv_descriptor_for_binding(&gfx_state->base, binding);
2618
2619 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
2620 read_len = MIN2(range->length,
2621 DIV_ROUND_UP(desc->buffer_view->range, 32) - range->start);
2622 read_addr = anv_address_add(desc->buffer_view->address,
2623 range->start * 32);
2624 } else {
2625 assert(desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
2626
2627 uint32_t dynamic_offset =
2628 dynamic_offset_for_binding(&gfx_state->base, binding);
2629 uint32_t buf_offset =
2630 MIN2(desc->offset + dynamic_offset, desc->buffer->size);
2631 uint32_t buf_range =
2632 MIN2(desc->range, desc->buffer->size - buf_offset);
2633
2634 read_len = MIN2(range->length,
2635 DIV_ROUND_UP(buf_range, 32) - range->start);
2636 read_addr = anv_address_add(desc->buffer->address,
2637 buf_offset + range->start * 32);
2638 }
2639 }
2640
2641 if (read_len > 0) {
2642 c.ConstantBody.Buffer[n] = read_addr;
2643 c.ConstantBody.ReadLength[n] = read_len;
2644 n--;
2645 }
2646 }
2647
2648 struct anv_state state =
2649 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2650
2651 if (state.alloc_size > 0) {
2652 c.ConstantBody.Buffer[n] = (struct anv_address) {
2653 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2654 .offset = state.offset,
2655 };
2656 c.ConstantBody.ReadLength[n] =
2657 DIV_ROUND_UP(state.alloc_size, 32);
2658 }
2659 #else
2660 /* For Ivy Bridge, the push constants packets have a different
2661 * rule that would require us to iterate in the other direction
2662 * and possibly mess around with dynamic state base address.
2663 * Don't bother; just emit regular push constants at n = 0.
2664 */
2665 struct anv_state state =
2666 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2667
2668 if (state.alloc_size > 0) {
2669 c.ConstantBody.Buffer[0].offset = state.offset,
2670 c.ConstantBody.ReadLength[0] =
2671 DIV_ROUND_UP(state.alloc_size, 32);
2672 }
2673 #endif
2674 }
2675 }
2676
2677 flushed |= mesa_to_vk_shader_stage(stage);
2678 }
2679
2680 cmd_buffer->state.push_constants_dirty &= ~flushed;
2681 }
2682
2683 #if GEN_GEN >= 12
2684 void
2685 genX(cmd_buffer_aux_map_state)(struct anv_cmd_buffer *cmd_buffer)
2686 {
2687 void *aux_map_ctx = cmd_buffer->device->aux_map_ctx;
2688 if (!aux_map_ctx)
2689 return;
2690 uint32_t aux_map_state_num = gen_aux_map_get_state_num(aux_map_ctx);
2691 if (cmd_buffer->state.last_aux_map_state != aux_map_state_num) {
2692 /* If the aux-map state number increased, then we need to rewrite the
2693 * register. Rewriting the register is used to both set the aux-map
2694 * translation table address, and also to invalidate any previously
2695 * cached translations.
2696 */
2697 uint64_t base_addr = gen_aux_map_get_base(aux_map_ctx);
2698 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
2699 lri.RegisterOffset = GENX(GFX_AUX_TABLE_BASE_ADDR_num);
2700 lri.DataDWord = base_addr & 0xffffffff;
2701 }
2702 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
2703 lri.RegisterOffset = GENX(GFX_AUX_TABLE_BASE_ADDR_num) + 4;
2704 lri.DataDWord = base_addr >> 32;
2705 }
2706 cmd_buffer->state.last_aux_map_state = aux_map_state_num;
2707 }
2708 }
2709 #endif
2710
2711 void
2712 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
2713 {
2714 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2715 uint32_t *p;
2716
2717 uint32_t vb_emit = cmd_buffer->state.gfx.vb_dirty & pipeline->vb_used;
2718 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE)
2719 vb_emit |= pipeline->vb_used;
2720
2721 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
2722
2723 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
2724
2725 genX(cmd_buffer_emit_hashing_mode)(cmd_buffer, UINT_MAX, UINT_MAX, 1);
2726
2727 genX(flush_pipeline_select_3d)(cmd_buffer);
2728
2729 #if GEN_GEN >= 12
2730 genX(cmd_buffer_aux_map_state)(cmd_buffer);
2731 #endif
2732
2733 if (vb_emit) {
2734 const uint32_t num_buffers = __builtin_popcount(vb_emit);
2735 const uint32_t num_dwords = 1 + num_buffers * 4;
2736
2737 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2738 GENX(3DSTATE_VERTEX_BUFFERS));
2739 uint32_t vb, i = 0;
2740 for_each_bit(vb, vb_emit) {
2741 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
2742 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
2743
2744 struct GENX(VERTEX_BUFFER_STATE) state = {
2745 .VertexBufferIndex = vb,
2746
2747 .MOCS = anv_mocs_for_bo(cmd_buffer->device, buffer->address.bo),
2748 #if GEN_GEN <= 7
2749 .BufferAccessType = pipeline->vb[vb].instanced ? INSTANCEDATA : VERTEXDATA,
2750 .InstanceDataStepRate = pipeline->vb[vb].instance_divisor,
2751 #endif
2752
2753 .AddressModifyEnable = true,
2754 .BufferPitch = pipeline->vb[vb].stride,
2755 .BufferStartingAddress = anv_address_add(buffer->address, offset),
2756
2757 #if GEN_GEN >= 8
2758 .BufferSize = buffer->size - offset
2759 #else
2760 .EndAddress = anv_address_add(buffer->address, buffer->size - 1),
2761 #endif
2762 };
2763
2764 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
2765 i++;
2766 }
2767 }
2768
2769 cmd_buffer->state.gfx.vb_dirty &= ~vb_emit;
2770
2771 #if GEN_GEN >= 8
2772 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_XFB_ENABLE) {
2773 /* We don't need any per-buffer dirty tracking because you're not
2774 * allowed to bind different XFB buffers while XFB is enabled.
2775 */
2776 for (unsigned idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
2777 struct anv_xfb_binding *xfb = &cmd_buffer->state.xfb_bindings[idx];
2778 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_SO_BUFFER), sob) {
2779 #if GEN_GEN < 12
2780 sob.SOBufferIndex = idx;
2781 #else
2782 sob._3DCommandOpcode = 0;
2783 sob._3DCommandSubOpcode = SO_BUFFER_INDEX_0_CMD + idx;
2784 #endif
2785
2786 if (cmd_buffer->state.xfb_enabled && xfb->buffer && xfb->size != 0) {
2787 sob.SOBufferEnable = true;
2788 sob.MOCS = cmd_buffer->device->default_mocs,
2789 sob.StreamOffsetWriteEnable = false;
2790 sob.SurfaceBaseAddress = anv_address_add(xfb->buffer->address,
2791 xfb->offset);
2792 /* Size is in DWords - 1 */
2793 sob.SurfaceSize = xfb->size / 4 - 1;
2794 }
2795 }
2796 }
2797
2798 /* CNL and later require a CS stall after 3DSTATE_SO_BUFFER */
2799 if (GEN_GEN >= 10)
2800 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
2801 }
2802 #endif
2803
2804 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE) {
2805 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2806
2807 /* The exact descriptor layout is pulled from the pipeline, so we need
2808 * to re-emit binding tables on every pipeline change.
2809 */
2810 cmd_buffer->state.descriptors_dirty |= pipeline->active_stages;
2811
2812 /* If the pipeline changed, we may need to re-allocate push constant
2813 * space in the URB.
2814 */
2815 cmd_buffer_alloc_push_constants(cmd_buffer);
2816 }
2817
2818 #if GEN_GEN <= 7
2819 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
2820 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
2821 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
2822 *
2823 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
2824 * stall needs to be sent just prior to any 3DSTATE_VS,
2825 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
2826 * 3DSTATE_BINDING_TABLE_POINTER_VS,
2827 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
2828 * PIPE_CONTROL needs to be sent before any combination of VS
2829 * associated 3DSTATE."
2830 */
2831 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2832 pc.DepthStallEnable = true;
2833 pc.PostSyncOperation = WriteImmediateData;
2834 pc.Address =
2835 (struct anv_address) { cmd_buffer->device->workaround_bo, 0 };
2836 }
2837 }
2838 #endif
2839
2840 /* Render targets live in the same binding table as fragment descriptors */
2841 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
2842 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
2843
2844 /* We emit the binding tables and sampler tables first, then emit push
2845 * constants and then finally emit binding table and sampler table
2846 * pointers. It has to happen in this order, since emitting the binding
2847 * tables may change the push constants (in case of storage images). After
2848 * emitting push constants, on SKL+ we have to emit the corresponding
2849 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
2850 */
2851 uint32_t dirty = 0;
2852 if (cmd_buffer->state.descriptors_dirty)
2853 dirty = flush_descriptor_sets(cmd_buffer);
2854
2855 if (dirty || cmd_buffer->state.push_constants_dirty) {
2856 /* Because we're pushing UBOs, we have to push whenever either
2857 * descriptors or push constants is dirty.
2858 */
2859 dirty |= cmd_buffer->state.push_constants_dirty;
2860 dirty &= ANV_STAGE_MASK & VK_SHADER_STAGE_ALL_GRAPHICS;
2861 cmd_buffer_flush_push_constants(cmd_buffer, dirty);
2862 }
2863
2864 if (dirty)
2865 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
2866
2867 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
2868 gen8_cmd_buffer_emit_viewport(cmd_buffer);
2869
2870 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
2871 ANV_CMD_DIRTY_PIPELINE)) {
2872 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
2873 pipeline->depth_clamp_enable);
2874 }
2875
2876 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_SCISSOR |
2877 ANV_CMD_DIRTY_RENDER_TARGETS))
2878 gen7_cmd_buffer_emit_scissor(cmd_buffer);
2879
2880 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
2881
2882 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2883 }
2884
2885 static void
2886 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
2887 struct anv_address addr,
2888 uint32_t size, uint32_t index)
2889 {
2890 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
2891 GENX(3DSTATE_VERTEX_BUFFERS));
2892
2893 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
2894 &(struct GENX(VERTEX_BUFFER_STATE)) {
2895 .VertexBufferIndex = index,
2896 .AddressModifyEnable = true,
2897 .BufferPitch = 0,
2898 .MOCS = anv_mocs_for_bo(cmd_buffer->device, addr.bo),
2899 #if (GEN_GEN >= 8)
2900 .BufferStartingAddress = addr,
2901 .BufferSize = size
2902 #else
2903 .BufferStartingAddress = addr,
2904 .EndAddress = anv_address_add(addr, size),
2905 #endif
2906 });
2907 }
2908
2909 static void
2910 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
2911 struct anv_address addr)
2912 {
2913 emit_vertex_bo(cmd_buffer, addr, 8, ANV_SVGS_VB_INDEX);
2914 }
2915
2916 static void
2917 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
2918 uint32_t base_vertex, uint32_t base_instance)
2919 {
2920 struct anv_state id_state =
2921 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
2922
2923 ((uint32_t *)id_state.map)[0] = base_vertex;
2924 ((uint32_t *)id_state.map)[1] = base_instance;
2925
2926 struct anv_address addr = {
2927 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2928 .offset = id_state.offset,
2929 };
2930
2931 emit_base_vertex_instance_bo(cmd_buffer, addr);
2932 }
2933
2934 static void
2935 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
2936 {
2937 struct anv_state state =
2938 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
2939
2940 ((uint32_t *)state.map)[0] = draw_index;
2941
2942 struct anv_address addr = {
2943 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2944 .offset = state.offset,
2945 };
2946
2947 emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
2948 }
2949
2950 void genX(CmdDraw)(
2951 VkCommandBuffer commandBuffer,
2952 uint32_t vertexCount,
2953 uint32_t instanceCount,
2954 uint32_t firstVertex,
2955 uint32_t firstInstance)
2956 {
2957 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2958 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2959 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2960
2961 if (anv_batch_has_error(&cmd_buffer->batch))
2962 return;
2963
2964 genX(cmd_buffer_flush_state)(cmd_buffer);
2965
2966 if (cmd_buffer->state.conditional_render_enabled)
2967 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
2968
2969 if (vs_prog_data->uses_firstvertex ||
2970 vs_prog_data->uses_baseinstance)
2971 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
2972 if (vs_prog_data->uses_drawid)
2973 emit_draw_index(cmd_buffer, 0);
2974
2975 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2976 * different views. We need to multiply instanceCount by the view count.
2977 */
2978 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2979
2980 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2981 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
2982 prim.VertexAccessType = SEQUENTIAL;
2983 prim.PrimitiveTopologyType = pipeline->topology;
2984 prim.VertexCountPerInstance = vertexCount;
2985 prim.StartVertexLocation = firstVertex;
2986 prim.InstanceCount = instanceCount;
2987 prim.StartInstanceLocation = firstInstance;
2988 prim.BaseVertexLocation = 0;
2989 }
2990 }
2991
2992 void genX(CmdDrawIndexed)(
2993 VkCommandBuffer commandBuffer,
2994 uint32_t indexCount,
2995 uint32_t instanceCount,
2996 uint32_t firstIndex,
2997 int32_t vertexOffset,
2998 uint32_t firstInstance)
2999 {
3000 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3001 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3002 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3003
3004 if (anv_batch_has_error(&cmd_buffer->batch))
3005 return;
3006
3007 genX(cmd_buffer_flush_state)(cmd_buffer);
3008
3009 if (cmd_buffer->state.conditional_render_enabled)
3010 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3011
3012 if (vs_prog_data->uses_firstvertex ||
3013 vs_prog_data->uses_baseinstance)
3014 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
3015 if (vs_prog_data->uses_drawid)
3016 emit_draw_index(cmd_buffer, 0);
3017
3018 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3019 * different views. We need to multiply instanceCount by the view count.
3020 */
3021 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
3022
3023 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3024 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3025 prim.VertexAccessType = RANDOM;
3026 prim.PrimitiveTopologyType = pipeline->topology;
3027 prim.VertexCountPerInstance = indexCount;
3028 prim.StartVertexLocation = firstIndex;
3029 prim.InstanceCount = instanceCount;
3030 prim.StartInstanceLocation = firstInstance;
3031 prim.BaseVertexLocation = vertexOffset;
3032 }
3033 }
3034
3035 /* Auto-Draw / Indirect Registers */
3036 #define GEN7_3DPRIM_END_OFFSET 0x2420
3037 #define GEN7_3DPRIM_START_VERTEX 0x2430
3038 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3039 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3040 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3041 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3042
3043 void genX(CmdDrawIndirectByteCountEXT)(
3044 VkCommandBuffer commandBuffer,
3045 uint32_t instanceCount,
3046 uint32_t firstInstance,
3047 VkBuffer counterBuffer,
3048 VkDeviceSize counterBufferOffset,
3049 uint32_t counterOffset,
3050 uint32_t vertexStride)
3051 {
3052 #if GEN_IS_HASWELL || GEN_GEN >= 8
3053 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3054 ANV_FROM_HANDLE(anv_buffer, counter_buffer, counterBuffer);
3055 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3056 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3057
3058 /* firstVertex is always zero for this draw function */
3059 const uint32_t firstVertex = 0;
3060
3061 if (anv_batch_has_error(&cmd_buffer->batch))
3062 return;
3063
3064 genX(cmd_buffer_flush_state)(cmd_buffer);
3065
3066 if (vs_prog_data->uses_firstvertex ||
3067 vs_prog_data->uses_baseinstance)
3068 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
3069 if (vs_prog_data->uses_drawid)
3070 emit_draw_index(cmd_buffer, 0);
3071
3072 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3073 * different views. We need to multiply instanceCount by the view count.
3074 */
3075 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
3076
3077 struct gen_mi_builder b;
3078 gen_mi_builder_init(&b, &cmd_buffer->batch);
3079 struct gen_mi_value count =
3080 gen_mi_mem32(anv_address_add(counter_buffer->address,
3081 counterBufferOffset));
3082 if (counterOffset)
3083 count = gen_mi_isub(&b, count, gen_mi_imm(counterOffset));
3084 count = gen_mi_udiv32_imm(&b, count, vertexStride);
3085 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT), count);
3086
3087 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX),
3088 gen_mi_imm(firstVertex));
3089 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT),
3090 gen_mi_imm(instanceCount));
3091 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3092 gen_mi_imm(firstInstance));
3093 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX), gen_mi_imm(0));
3094
3095 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3096 prim.IndirectParameterEnable = true;
3097 prim.VertexAccessType = SEQUENTIAL;
3098 prim.PrimitiveTopologyType = pipeline->topology;
3099 }
3100 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
3101 }
3102
3103 static void
3104 load_indirect_parameters(struct anv_cmd_buffer *cmd_buffer,
3105 struct anv_address addr,
3106 bool indexed)
3107 {
3108 struct gen_mi_builder b;
3109 gen_mi_builder_init(&b, &cmd_buffer->batch);
3110
3111 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT),
3112 gen_mi_mem32(anv_address_add(addr, 0)));
3113
3114 struct gen_mi_value instance_count = gen_mi_mem32(anv_address_add(addr, 4));
3115 unsigned view_count = anv_subpass_view_count(cmd_buffer->state.subpass);
3116 if (view_count > 1) {
3117 #if GEN_IS_HASWELL || GEN_GEN >= 8
3118 instance_count = gen_mi_imul_imm(&b, instance_count, view_count);
3119 #else
3120 anv_finishme("Multiview + indirect draw requires MI_MATH; "
3121 "MI_MATH is not supported on Ivy Bridge");
3122 #endif
3123 }
3124 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT), instance_count);
3125
3126 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX),
3127 gen_mi_mem32(anv_address_add(addr, 8)));
3128
3129 if (indexed) {
3130 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX),
3131 gen_mi_mem32(anv_address_add(addr, 12)));
3132 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3133 gen_mi_mem32(anv_address_add(addr, 16)));
3134 } else {
3135 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3136 gen_mi_mem32(anv_address_add(addr, 12)));
3137 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX), gen_mi_imm(0));
3138 }
3139 }
3140
3141 void genX(CmdDrawIndirect)(
3142 VkCommandBuffer commandBuffer,
3143 VkBuffer _buffer,
3144 VkDeviceSize offset,
3145 uint32_t drawCount,
3146 uint32_t stride)
3147 {
3148 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3149 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3150 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3151 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3152
3153 if (anv_batch_has_error(&cmd_buffer->batch))
3154 return;
3155
3156 genX(cmd_buffer_flush_state)(cmd_buffer);
3157
3158 if (cmd_buffer->state.conditional_render_enabled)
3159 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3160
3161 for (uint32_t i = 0; i < drawCount; i++) {
3162 struct anv_address draw = anv_address_add(buffer->address, offset);
3163
3164 if (vs_prog_data->uses_firstvertex ||
3165 vs_prog_data->uses_baseinstance)
3166 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3167 if (vs_prog_data->uses_drawid)
3168 emit_draw_index(cmd_buffer, i);
3169
3170 load_indirect_parameters(cmd_buffer, draw, false);
3171
3172 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3173 prim.IndirectParameterEnable = true;
3174 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3175 prim.VertexAccessType = SEQUENTIAL;
3176 prim.PrimitiveTopologyType = pipeline->topology;
3177 }
3178
3179 offset += stride;
3180 }
3181 }
3182
3183 void genX(CmdDrawIndexedIndirect)(
3184 VkCommandBuffer commandBuffer,
3185 VkBuffer _buffer,
3186 VkDeviceSize offset,
3187 uint32_t drawCount,
3188 uint32_t stride)
3189 {
3190 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3191 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3192 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3193 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3194
3195 if (anv_batch_has_error(&cmd_buffer->batch))
3196 return;
3197
3198 genX(cmd_buffer_flush_state)(cmd_buffer);
3199
3200 if (cmd_buffer->state.conditional_render_enabled)
3201 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3202
3203 for (uint32_t i = 0; i < drawCount; i++) {
3204 struct anv_address draw = anv_address_add(buffer->address, offset);
3205
3206 /* TODO: We need to stomp base vertex to 0 somehow */
3207 if (vs_prog_data->uses_firstvertex ||
3208 vs_prog_data->uses_baseinstance)
3209 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3210 if (vs_prog_data->uses_drawid)
3211 emit_draw_index(cmd_buffer, i);
3212
3213 load_indirect_parameters(cmd_buffer, draw, true);
3214
3215 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3216 prim.IndirectParameterEnable = true;
3217 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3218 prim.VertexAccessType = RANDOM;
3219 prim.PrimitiveTopologyType = pipeline->topology;
3220 }
3221
3222 offset += stride;
3223 }
3224 }
3225
3226 #define TMP_DRAW_COUNT_REG 0x2670 /* MI_ALU_REG14 */
3227
3228 static void
3229 prepare_for_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3230 struct anv_address count_address,
3231 const bool conditional_render_enabled)
3232 {
3233 struct gen_mi_builder b;
3234 gen_mi_builder_init(&b, &cmd_buffer->batch);
3235
3236 if (conditional_render_enabled) {
3237 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3238 gen_mi_store(&b, gen_mi_reg64(TMP_DRAW_COUNT_REG),
3239 gen_mi_mem32(count_address));
3240 #endif
3241 } else {
3242 /* Upload the current draw count from the draw parameters buffer to
3243 * MI_PREDICATE_SRC0.
3244 */
3245 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
3246 gen_mi_mem32(count_address));
3247
3248 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC1 + 4), gen_mi_imm(0));
3249 }
3250 }
3251
3252 static void
3253 emit_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3254 uint32_t draw_index)
3255 {
3256 struct gen_mi_builder b;
3257 gen_mi_builder_init(&b, &cmd_buffer->batch);
3258
3259 /* Upload the index of the current primitive to MI_PREDICATE_SRC1. */
3260 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC1), gen_mi_imm(draw_index));
3261
3262 if (draw_index == 0) {
3263 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3264 mip.LoadOperation = LOAD_LOADINV;
3265 mip.CombineOperation = COMBINE_SET;
3266 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3267 }
3268 } else {
3269 /* While draw_index < draw_count the predicate's result will be
3270 * (draw_index == draw_count) ^ TRUE = TRUE
3271 * When draw_index == draw_count the result is
3272 * (TRUE) ^ TRUE = FALSE
3273 * After this all results will be:
3274 * (FALSE) ^ FALSE = FALSE
3275 */
3276 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3277 mip.LoadOperation = LOAD_LOAD;
3278 mip.CombineOperation = COMBINE_XOR;
3279 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3280 }
3281 }
3282 }
3283
3284 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3285 static void
3286 emit_draw_count_predicate_with_conditional_render(
3287 struct anv_cmd_buffer *cmd_buffer,
3288 uint32_t draw_index)
3289 {
3290 struct gen_mi_builder b;
3291 gen_mi_builder_init(&b, &cmd_buffer->batch);
3292
3293 struct gen_mi_value pred = gen_mi_ult(&b, gen_mi_imm(draw_index),
3294 gen_mi_reg64(TMP_DRAW_COUNT_REG));
3295 pred = gen_mi_iand(&b, pred, gen_mi_reg64(ANV_PREDICATE_RESULT_REG));
3296
3297 #if GEN_GEN >= 8
3298 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_RESULT), pred);
3299 #else
3300 /* MI_PREDICATE_RESULT is not whitelisted in i915 command parser
3301 * so we emit MI_PREDICATE to set it.
3302 */
3303
3304 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), pred);
3305 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
3306
3307 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3308 mip.LoadOperation = LOAD_LOADINV;
3309 mip.CombineOperation = COMBINE_SET;
3310 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3311 }
3312 #endif
3313 }
3314 #endif
3315
3316 void genX(CmdDrawIndirectCountKHR)(
3317 VkCommandBuffer commandBuffer,
3318 VkBuffer _buffer,
3319 VkDeviceSize offset,
3320 VkBuffer _countBuffer,
3321 VkDeviceSize countBufferOffset,
3322 uint32_t maxDrawCount,
3323 uint32_t stride)
3324 {
3325 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3326 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3327 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3328 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3329 struct anv_pipeline *pipeline = cmd_state->gfx.base.pipeline;
3330 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3331
3332 if (anv_batch_has_error(&cmd_buffer->batch))
3333 return;
3334
3335 genX(cmd_buffer_flush_state)(cmd_buffer);
3336
3337 struct anv_address count_address =
3338 anv_address_add(count_buffer->address, countBufferOffset);
3339
3340 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3341 cmd_state->conditional_render_enabled);
3342
3343 for (uint32_t i = 0; i < maxDrawCount; i++) {
3344 struct anv_address draw = anv_address_add(buffer->address, offset);
3345
3346 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3347 if (cmd_state->conditional_render_enabled) {
3348 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3349 } else {
3350 emit_draw_count_predicate(cmd_buffer, i);
3351 }
3352 #else
3353 emit_draw_count_predicate(cmd_buffer, i);
3354 #endif
3355
3356 if (vs_prog_data->uses_firstvertex ||
3357 vs_prog_data->uses_baseinstance)
3358 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3359 if (vs_prog_data->uses_drawid)
3360 emit_draw_index(cmd_buffer, i);
3361
3362 load_indirect_parameters(cmd_buffer, draw, false);
3363
3364 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3365 prim.IndirectParameterEnable = true;
3366 prim.PredicateEnable = true;
3367 prim.VertexAccessType = SEQUENTIAL;
3368 prim.PrimitiveTopologyType = pipeline->topology;
3369 }
3370
3371 offset += stride;
3372 }
3373 }
3374
3375 void genX(CmdDrawIndexedIndirectCountKHR)(
3376 VkCommandBuffer commandBuffer,
3377 VkBuffer _buffer,
3378 VkDeviceSize offset,
3379 VkBuffer _countBuffer,
3380 VkDeviceSize countBufferOffset,
3381 uint32_t maxDrawCount,
3382 uint32_t stride)
3383 {
3384 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3385 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3386 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3387 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3388 struct anv_pipeline *pipeline = cmd_state->gfx.base.pipeline;
3389 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3390
3391 if (anv_batch_has_error(&cmd_buffer->batch))
3392 return;
3393
3394 genX(cmd_buffer_flush_state)(cmd_buffer);
3395
3396 struct anv_address count_address =
3397 anv_address_add(count_buffer->address, countBufferOffset);
3398
3399 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3400 cmd_state->conditional_render_enabled);
3401
3402 for (uint32_t i = 0; i < maxDrawCount; i++) {
3403 struct anv_address draw = anv_address_add(buffer->address, offset);
3404
3405 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3406 if (cmd_state->conditional_render_enabled) {
3407 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3408 } else {
3409 emit_draw_count_predicate(cmd_buffer, i);
3410 }
3411 #else
3412 emit_draw_count_predicate(cmd_buffer, i);
3413 #endif
3414
3415 /* TODO: We need to stomp base vertex to 0 somehow */
3416 if (vs_prog_data->uses_firstvertex ||
3417 vs_prog_data->uses_baseinstance)
3418 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3419 if (vs_prog_data->uses_drawid)
3420 emit_draw_index(cmd_buffer, i);
3421
3422 load_indirect_parameters(cmd_buffer, draw, true);
3423
3424 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3425 prim.IndirectParameterEnable = true;
3426 prim.PredicateEnable = true;
3427 prim.VertexAccessType = RANDOM;
3428 prim.PrimitiveTopologyType = pipeline->topology;
3429 }
3430
3431 offset += stride;
3432 }
3433 }
3434
3435 void genX(CmdBeginTransformFeedbackEXT)(
3436 VkCommandBuffer commandBuffer,
3437 uint32_t firstCounterBuffer,
3438 uint32_t counterBufferCount,
3439 const VkBuffer* pCounterBuffers,
3440 const VkDeviceSize* pCounterBufferOffsets)
3441 {
3442 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3443
3444 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
3445 assert(counterBufferCount <= MAX_XFB_BUFFERS);
3446 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
3447
3448 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3449 *
3450 * "Ssoftware must ensure that no HW stream output operations can be in
3451 * process or otherwise pending at the point that the MI_LOAD/STORE
3452 * commands are processed. This will likely require a pipeline flush."
3453 */
3454 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3455 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3456
3457 for (uint32_t idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
3458 /* If we have a counter buffer, this is a resume so we need to load the
3459 * value into the streamout offset register. Otherwise, this is a begin
3460 * and we need to reset it to zero.
3461 */
3462 if (pCounterBuffers &&
3463 idx >= firstCounterBuffer &&
3464 idx - firstCounterBuffer < counterBufferCount &&
3465 pCounterBuffers[idx - firstCounterBuffer] != VK_NULL_HANDLE) {
3466 uint32_t cb_idx = idx - firstCounterBuffer;
3467 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
3468 uint64_t offset = pCounterBufferOffsets ?
3469 pCounterBufferOffsets[cb_idx] : 0;
3470
3471 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3472 lrm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3473 lrm.MemoryAddress = anv_address_add(counter_buffer->address,
3474 offset);
3475 }
3476 } else {
3477 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
3478 lri.RegisterOffset = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3479 lri.DataDWord = 0;
3480 }
3481 }
3482 }
3483
3484 cmd_buffer->state.xfb_enabled = true;
3485 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
3486 }
3487
3488 void genX(CmdEndTransformFeedbackEXT)(
3489 VkCommandBuffer commandBuffer,
3490 uint32_t firstCounterBuffer,
3491 uint32_t counterBufferCount,
3492 const VkBuffer* pCounterBuffers,
3493 const VkDeviceSize* pCounterBufferOffsets)
3494 {
3495 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3496
3497 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
3498 assert(counterBufferCount <= MAX_XFB_BUFFERS);
3499 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
3500
3501 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3502 *
3503 * "Ssoftware must ensure that no HW stream output operations can be in
3504 * process or otherwise pending at the point that the MI_LOAD/STORE
3505 * commands are processed. This will likely require a pipeline flush."
3506 */
3507 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3508 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3509
3510 for (uint32_t cb_idx = 0; cb_idx < counterBufferCount; cb_idx++) {
3511 unsigned idx = firstCounterBuffer + cb_idx;
3512
3513 /* If we have a counter buffer, this is a resume so we need to load the
3514 * value into the streamout offset register. Otherwise, this is a begin
3515 * and we need to reset it to zero.
3516 */
3517 if (pCounterBuffers &&
3518 cb_idx < counterBufferCount &&
3519 pCounterBuffers[cb_idx] != VK_NULL_HANDLE) {
3520 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
3521 uint64_t offset = pCounterBufferOffsets ?
3522 pCounterBufferOffsets[cb_idx] : 0;
3523
3524 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
3525 srm.MemoryAddress = anv_address_add(counter_buffer->address,
3526 offset);
3527 srm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3528 }
3529 }
3530 }
3531
3532 cmd_buffer->state.xfb_enabled = false;
3533 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
3534 }
3535
3536 static VkResult
3537 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
3538 {
3539 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3540 struct anv_state surfaces = { 0, }, samplers = { 0, };
3541 VkResult result;
3542
3543 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
3544 if (result != VK_SUCCESS) {
3545 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
3546
3547 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
3548 if (result != VK_SUCCESS)
3549 return result;
3550
3551 /* Re-emit state base addresses so we get the new surface state base
3552 * address before we start emitting binding tables etc.
3553 */
3554 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
3555
3556 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
3557 if (result != VK_SUCCESS) {
3558 anv_batch_set_error(&cmd_buffer->batch, result);
3559 return result;
3560 }
3561 }
3562
3563 result = emit_samplers(cmd_buffer, MESA_SHADER_COMPUTE, &samplers);
3564 if (result != VK_SUCCESS) {
3565 anv_batch_set_error(&cmd_buffer->batch, result);
3566 return result;
3567 }
3568
3569 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
3570 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
3571 .BindingTablePointer = surfaces.offset,
3572 .SamplerStatePointer = samplers.offset,
3573 };
3574 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
3575
3576 struct anv_state state =
3577 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
3578 pipeline->interface_descriptor_data,
3579 GENX(INTERFACE_DESCRIPTOR_DATA_length),
3580 64);
3581
3582 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
3583 anv_batch_emit(&cmd_buffer->batch,
3584 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
3585 mid.InterfaceDescriptorTotalLength = size;
3586 mid.InterfaceDescriptorDataStartAddress = state.offset;
3587 }
3588
3589 return VK_SUCCESS;
3590 }
3591
3592 void
3593 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
3594 {
3595 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3596 VkResult result;
3597
3598 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
3599
3600 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
3601
3602 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
3603
3604 #if GEN_GEN >= 12
3605 genX(cmd_buffer_aux_map_state)(cmd_buffer);
3606 #endif
3607
3608 if (cmd_buffer->state.compute.pipeline_dirty) {
3609 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
3610 *
3611 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
3612 * the only bits that are changed are scoreboard related: Scoreboard
3613 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
3614 * these scoreboard related states, a MEDIA_STATE_FLUSH is
3615 * sufficient."
3616 */
3617 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3618 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3619
3620 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
3621 }
3622
3623 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
3624 cmd_buffer->state.compute.pipeline_dirty) {
3625 /* FIXME: figure out descriptors for gen7 */
3626 result = flush_compute_descriptor_set(cmd_buffer);
3627 if (result != VK_SUCCESS)
3628 return;
3629
3630 cmd_buffer->state.descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
3631 }
3632
3633 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
3634 struct anv_state push_state =
3635 anv_cmd_buffer_cs_push_constants(cmd_buffer);
3636
3637 if (push_state.alloc_size) {
3638 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
3639 curbe.CURBETotalDataLength = push_state.alloc_size;
3640 curbe.CURBEDataStartAddress = push_state.offset;
3641 }
3642 }
3643
3644 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
3645 }
3646
3647 cmd_buffer->state.compute.pipeline_dirty = false;
3648
3649 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3650 }
3651
3652 #if GEN_GEN == 7
3653
3654 static VkResult
3655 verify_cmd_parser(const struct anv_device *device,
3656 int required_version,
3657 const char *function)
3658 {
3659 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
3660 return vk_errorf(device->instance, device->instance,
3661 VK_ERROR_FEATURE_NOT_PRESENT,
3662 "cmd parser version %d is required for %s",
3663 required_version, function);
3664 } else {
3665 return VK_SUCCESS;
3666 }
3667 }
3668
3669 #endif
3670
3671 static void
3672 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer *cmd_buffer,
3673 uint32_t baseGroupX,
3674 uint32_t baseGroupY,
3675 uint32_t baseGroupZ)
3676 {
3677 if (anv_batch_has_error(&cmd_buffer->batch))
3678 return;
3679
3680 struct anv_push_constants *push =
3681 &cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
3682 if (push->base_work_group_id[0] != baseGroupX ||
3683 push->base_work_group_id[1] != baseGroupY ||
3684 push->base_work_group_id[2] != baseGroupZ) {
3685 push->base_work_group_id[0] = baseGroupX;
3686 push->base_work_group_id[1] = baseGroupY;
3687 push->base_work_group_id[2] = baseGroupZ;
3688
3689 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
3690 }
3691 }
3692
3693 void genX(CmdDispatch)(
3694 VkCommandBuffer commandBuffer,
3695 uint32_t x,
3696 uint32_t y,
3697 uint32_t z)
3698 {
3699 genX(CmdDispatchBase)(commandBuffer, 0, 0, 0, x, y, z);
3700 }
3701
3702 void genX(CmdDispatchBase)(
3703 VkCommandBuffer commandBuffer,
3704 uint32_t baseGroupX,
3705 uint32_t baseGroupY,
3706 uint32_t baseGroupZ,
3707 uint32_t groupCountX,
3708 uint32_t groupCountY,
3709 uint32_t groupCountZ)
3710 {
3711 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3712 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3713 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3714
3715 anv_cmd_buffer_push_base_group_id(cmd_buffer, baseGroupX,
3716 baseGroupY, baseGroupZ);
3717
3718 if (anv_batch_has_error(&cmd_buffer->batch))
3719 return;
3720
3721 if (prog_data->uses_num_work_groups) {
3722 struct anv_state state =
3723 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
3724 uint32_t *sizes = state.map;
3725 sizes[0] = groupCountX;
3726 sizes[1] = groupCountY;
3727 sizes[2] = groupCountZ;
3728 cmd_buffer->state.compute.num_workgroups = (struct anv_address) {
3729 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
3730 .offset = state.offset,
3731 };
3732 }
3733
3734 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3735
3736 if (cmd_buffer->state.conditional_render_enabled)
3737 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3738
3739 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
3740 ggw.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3741 ggw.SIMDSize = prog_data->simd_size / 16;
3742 ggw.ThreadDepthCounterMaximum = 0;
3743 ggw.ThreadHeightCounterMaximum = 0;
3744 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3745 ggw.ThreadGroupIDXDimension = groupCountX;
3746 ggw.ThreadGroupIDYDimension = groupCountY;
3747 ggw.ThreadGroupIDZDimension = groupCountZ;
3748 ggw.RightExecutionMask = pipeline->cs_right_mask;
3749 ggw.BottomExecutionMask = 0xffffffff;
3750 }
3751
3752 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
3753 }
3754
3755 #define GPGPU_DISPATCHDIMX 0x2500
3756 #define GPGPU_DISPATCHDIMY 0x2504
3757 #define GPGPU_DISPATCHDIMZ 0x2508
3758
3759 void genX(CmdDispatchIndirect)(
3760 VkCommandBuffer commandBuffer,
3761 VkBuffer _buffer,
3762 VkDeviceSize offset)
3763 {
3764 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3765 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3766 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3767 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3768 struct anv_address addr = anv_address_add(buffer->address, offset);
3769 struct anv_batch *batch = &cmd_buffer->batch;
3770
3771 anv_cmd_buffer_push_base_group_id(cmd_buffer, 0, 0, 0);
3772
3773 #if GEN_GEN == 7
3774 /* Linux 4.4 added command parser version 5 which allows the GPGPU
3775 * indirect dispatch registers to be written.
3776 */
3777 if (verify_cmd_parser(cmd_buffer->device, 5,
3778 "vkCmdDispatchIndirect") != VK_SUCCESS)
3779 return;
3780 #endif
3781
3782 if (prog_data->uses_num_work_groups)
3783 cmd_buffer->state.compute.num_workgroups = addr;
3784
3785 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3786
3787 struct gen_mi_builder b;
3788 gen_mi_builder_init(&b, &cmd_buffer->batch);
3789
3790 struct gen_mi_value size_x = gen_mi_mem32(anv_address_add(addr, 0));
3791 struct gen_mi_value size_y = gen_mi_mem32(anv_address_add(addr, 4));
3792 struct gen_mi_value size_z = gen_mi_mem32(anv_address_add(addr, 8));
3793
3794 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMX), size_x);
3795 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMY), size_y);
3796 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMZ), size_z);
3797
3798 #if GEN_GEN <= 7
3799 /* predicate = (compute_dispatch_indirect_x_size == 0); */
3800 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), size_x);
3801 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
3802 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3803 mip.LoadOperation = LOAD_LOAD;
3804 mip.CombineOperation = COMBINE_SET;
3805 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3806 }
3807
3808 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
3809 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0), size_y);
3810 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3811 mip.LoadOperation = LOAD_LOAD;
3812 mip.CombineOperation = COMBINE_OR;
3813 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3814 }
3815
3816 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
3817 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0), size_z);
3818 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3819 mip.LoadOperation = LOAD_LOAD;
3820 mip.CombineOperation = COMBINE_OR;
3821 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3822 }
3823
3824 /* predicate = !predicate; */
3825 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3826 mip.LoadOperation = LOAD_LOADINV;
3827 mip.CombineOperation = COMBINE_OR;
3828 mip.CompareOperation = COMPARE_FALSE;
3829 }
3830
3831 #if GEN_IS_HASWELL
3832 if (cmd_buffer->state.conditional_render_enabled) {
3833 /* predicate &= !(conditional_rendering_predicate == 0); */
3834 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0),
3835 gen_mi_reg32(ANV_PREDICATE_RESULT_REG));
3836 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3837 mip.LoadOperation = LOAD_LOADINV;
3838 mip.CombineOperation = COMBINE_AND;
3839 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3840 }
3841 }
3842 #endif
3843
3844 #else /* GEN_GEN > 7 */
3845 if (cmd_buffer->state.conditional_render_enabled)
3846 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3847 #endif
3848
3849 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
3850 ggw.IndirectParameterEnable = true;
3851 ggw.PredicateEnable = GEN_GEN <= 7 ||
3852 cmd_buffer->state.conditional_render_enabled;
3853 ggw.SIMDSize = prog_data->simd_size / 16;
3854 ggw.ThreadDepthCounterMaximum = 0;
3855 ggw.ThreadHeightCounterMaximum = 0;
3856 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3857 ggw.RightExecutionMask = pipeline->cs_right_mask;
3858 ggw.BottomExecutionMask = 0xffffffff;
3859 }
3860
3861 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
3862 }
3863
3864 static void
3865 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
3866 uint32_t pipeline)
3867 {
3868 UNUSED const struct gen_device_info *devinfo = &cmd_buffer->device->info;
3869
3870 if (cmd_buffer->state.current_pipeline == pipeline)
3871 return;
3872
3873 #if GEN_GEN >= 8 && GEN_GEN < 10
3874 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
3875 *
3876 * Software must clear the COLOR_CALC_STATE Valid field in
3877 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
3878 * with Pipeline Select set to GPGPU.
3879 *
3880 * The internal hardware docs recommend the same workaround for Gen9
3881 * hardware too.
3882 */
3883 if (pipeline == GPGPU)
3884 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
3885 #endif
3886
3887 #if GEN_GEN == 9
3888 if (pipeline == _3D) {
3889 /* There is a mid-object preemption workaround which requires you to
3890 * re-emit MEDIA_VFE_STATE after switching from GPGPU to 3D. However,
3891 * even without preemption, we have issues with geometry flickering when
3892 * GPGPU and 3D are back-to-back and this seems to fix it. We don't
3893 * really know why.
3894 */
3895 const uint32_t subslices =
3896 MAX2(cmd_buffer->device->instance->physicalDevice.subslice_total, 1);
3897 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_VFE_STATE), vfe) {
3898 vfe.MaximumNumberofThreads =
3899 devinfo->max_cs_threads * subslices - 1;
3900 vfe.NumberofURBEntries = 2;
3901 vfe.URBEntryAllocationSize = 2;
3902 }
3903 }
3904 #endif
3905
3906 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
3907 * PIPELINE_SELECT [DevBWR+]":
3908 *
3909 * Project: DEVSNB+
3910 *
3911 * Software must ensure all the write caches are flushed through a
3912 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
3913 * command to invalidate read only caches prior to programming
3914 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
3915 */
3916 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3917 pc.RenderTargetCacheFlushEnable = true;
3918 pc.DepthCacheFlushEnable = true;
3919 pc.DCFlushEnable = true;
3920 pc.PostSyncOperation = NoWrite;
3921 pc.CommandStreamerStallEnable = true;
3922 #if GEN_GEN >= 12
3923 pc.TileCacheFlushEnable = true;
3924 #endif
3925 }
3926
3927 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3928 pc.TextureCacheInvalidationEnable = true;
3929 pc.ConstantCacheInvalidationEnable = true;
3930 pc.StateCacheInvalidationEnable = true;
3931 pc.InstructionCacheInvalidateEnable = true;
3932 pc.PostSyncOperation = NoWrite;
3933 #if GEN_GEN >= 12
3934 pc.TileCacheFlushEnable = true;
3935 #endif
3936 }
3937
3938 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
3939 #if GEN_GEN >= 9
3940 ps.MaskBits = 3;
3941 #endif
3942 ps.PipelineSelection = pipeline;
3943 }
3944
3945 #if GEN_GEN == 9
3946 if (devinfo->is_geminilake) {
3947 /* Project: DevGLK
3948 *
3949 * "This chicken bit works around a hardware issue with barrier logic
3950 * encountered when switching between GPGPU and 3D pipelines. To
3951 * workaround the issue, this mode bit should be set after a pipeline
3952 * is selected."
3953 */
3954 uint32_t scec;
3955 anv_pack_struct(&scec, GENX(SLICE_COMMON_ECO_CHICKEN1),
3956 .GLKBarrierMode =
3957 pipeline == GPGPU ? GLK_BARRIER_MODE_GPGPU
3958 : GLK_BARRIER_MODE_3D_HULL,
3959 .GLKBarrierModeMask = 1);
3960 emit_lri(&cmd_buffer->batch, GENX(SLICE_COMMON_ECO_CHICKEN1_num), scec);
3961 }
3962 #endif
3963
3964 cmd_buffer->state.current_pipeline = pipeline;
3965 }
3966
3967 void
3968 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
3969 {
3970 genX(flush_pipeline_select)(cmd_buffer, _3D);
3971 }
3972
3973 void
3974 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
3975 {
3976 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
3977 }
3978
3979 void
3980 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
3981 {
3982 if (GEN_GEN >= 8)
3983 return;
3984
3985 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
3986 *
3987 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
3988 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
3989 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
3990 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
3991 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
3992 * Depth Flush Bit set, followed by another pipelined depth stall
3993 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
3994 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
3995 * via a preceding MI_FLUSH)."
3996 */
3997 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3998 pipe.DepthStallEnable = true;
3999 }
4000 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
4001 pipe.DepthCacheFlushEnable = true;
4002 #if GEN_GEN >= 12
4003 pipe.TileCacheFlushEnable = true;
4004 #endif
4005 }
4006 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
4007 pipe.DepthStallEnable = true;
4008 }
4009 }
4010
4011 /**
4012 * Update the pixel hashing modes that determine the balancing of PS threads
4013 * across subslices and slices.
4014 *
4015 * \param width Width bound of the rendering area (already scaled down if \p
4016 * scale is greater than 1).
4017 * \param height Height bound of the rendering area (already scaled down if \p
4018 * scale is greater than 1).
4019 * \param scale The number of framebuffer samples that could potentially be
4020 * affected by an individual channel of the PS thread. This is
4021 * typically one for single-sampled rendering, but for operations
4022 * like CCS resolves and fast clears a single PS invocation may
4023 * update a huge number of pixels, in which case a finer
4024 * balancing is desirable in order to maximally utilize the
4025 * bandwidth available. UINT_MAX can be used as shorthand for
4026 * "finest hashing mode available".
4027 */
4028 void
4029 genX(cmd_buffer_emit_hashing_mode)(struct anv_cmd_buffer *cmd_buffer,
4030 unsigned width, unsigned height,
4031 unsigned scale)
4032 {
4033 #if GEN_GEN == 9
4034 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
4035 const unsigned slice_hashing[] = {
4036 /* Because all Gen9 platforms with more than one slice require
4037 * three-way subslice hashing, a single "normal" 16x16 slice hashing
4038 * block is guaranteed to suffer from substantial imbalance, with one
4039 * subslice receiving twice as much work as the other two in the
4040 * slice.
4041 *
4042 * The performance impact of that would be particularly severe when
4043 * three-way hashing is also in use for slice balancing (which is the
4044 * case for all Gen9 GT4 platforms), because one of the slices
4045 * receives one every three 16x16 blocks in either direction, which
4046 * is roughly the periodicity of the underlying subslice imbalance
4047 * pattern ("roughly" because in reality the hardware's
4048 * implementation of three-way hashing doesn't do exact modulo 3
4049 * arithmetic, which somewhat decreases the magnitude of this effect
4050 * in practice). This leads to a systematic subslice imbalance
4051 * within that slice regardless of the size of the primitive. The
4052 * 32x32 hashing mode guarantees that the subslice imbalance within a
4053 * single slice hashing block is minimal, largely eliminating this
4054 * effect.
4055 */
4056 _32x32,
4057 /* Finest slice hashing mode available. */
4058 NORMAL
4059 };
4060 const unsigned subslice_hashing[] = {
4061 /* 16x16 would provide a slight cache locality benefit especially
4062 * visible in the sampler L1 cache efficiency of low-bandwidth
4063 * non-LLC platforms, but it comes at the cost of greater subslice
4064 * imbalance for primitives of dimensions approximately intermediate
4065 * between 16x4 and 16x16.
4066 */
4067 _16x4,
4068 /* Finest subslice hashing mode available. */
4069 _8x4
4070 };
4071 /* Dimensions of the smallest hashing block of a given hashing mode. If
4072 * the rendering area is smaller than this there can't possibly be any
4073 * benefit from switching to this mode, so we optimize out the
4074 * transition.
4075 */
4076 const unsigned min_size[][2] = {
4077 { 16, 4 },
4078 { 8, 4 }
4079 };
4080 const unsigned idx = scale > 1;
4081
4082 if (cmd_buffer->state.current_hash_scale != scale &&
4083 (width > min_size[idx][0] || height > min_size[idx][1])) {
4084 uint32_t gt_mode;
4085
4086 anv_pack_struct(&gt_mode, GENX(GT_MODE),
4087 .SliceHashing = (devinfo->num_slices > 1 ? slice_hashing[idx] : 0),
4088 .SliceHashingMask = (devinfo->num_slices > 1 ? -1 : 0),
4089 .SubsliceHashing = subslice_hashing[idx],
4090 .SubsliceHashingMask = -1);
4091
4092 cmd_buffer->state.pending_pipe_bits |=
4093 ANV_PIPE_CS_STALL_BIT | ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
4094 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4095
4096 emit_lri(&cmd_buffer->batch, GENX(GT_MODE_num), gt_mode);
4097
4098 cmd_buffer->state.current_hash_scale = scale;
4099 }
4100 #endif
4101 }
4102
4103 static void
4104 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
4105 {
4106 struct anv_device *device = cmd_buffer->device;
4107 const struct anv_image_view *iview =
4108 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
4109 const struct anv_image *image = iview ? iview->image : NULL;
4110
4111 /* FIXME: Width and Height are wrong */
4112
4113 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
4114
4115 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
4116 device->isl_dev.ds.size / 4);
4117 if (dw == NULL)
4118 return;
4119
4120 struct isl_depth_stencil_hiz_emit_info info = { };
4121
4122 if (iview)
4123 info.view = &iview->planes[0].isl;
4124
4125 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
4126 uint32_t depth_plane =
4127 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_DEPTH_BIT);
4128 const struct anv_surface *surface = &image->planes[depth_plane].surface;
4129
4130 info.depth_surf = &surface->isl;
4131
4132 info.depth_address =
4133 anv_batch_emit_reloc(&cmd_buffer->batch,
4134 dw + device->isl_dev.ds.depth_offset / 4,
4135 image->planes[depth_plane].address.bo,
4136 image->planes[depth_plane].address.offset +
4137 surface->offset);
4138 info.mocs =
4139 anv_mocs_for_bo(device, image->planes[depth_plane].address.bo);
4140
4141 const uint32_t ds =
4142 cmd_buffer->state.subpass->depth_stencil_attachment->attachment;
4143 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
4144 if (info.hiz_usage == ISL_AUX_USAGE_HIZ) {
4145 info.hiz_surf = &image->planes[depth_plane].aux_surface.isl;
4146
4147 info.hiz_address =
4148 anv_batch_emit_reloc(&cmd_buffer->batch,
4149 dw + device->isl_dev.ds.hiz_offset / 4,
4150 image->planes[depth_plane].address.bo,
4151 image->planes[depth_plane].address.offset +
4152 image->planes[depth_plane].aux_surface.offset);
4153
4154 info.depth_clear_value = ANV_HZ_FC_VAL;
4155 }
4156 }
4157
4158 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
4159 uint32_t stencil_plane =
4160 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_STENCIL_BIT);
4161 const struct anv_surface *surface = &image->planes[stencil_plane].surface;
4162
4163 info.stencil_surf = &surface->isl;
4164
4165 info.stencil_address =
4166 anv_batch_emit_reloc(&cmd_buffer->batch,
4167 dw + device->isl_dev.ds.stencil_offset / 4,
4168 image->planes[stencil_plane].address.bo,
4169 image->planes[stencil_plane].address.offset +
4170 surface->offset);
4171 info.mocs =
4172 anv_mocs_for_bo(device, image->planes[stencil_plane].address.bo);
4173 }
4174
4175 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
4176
4177 cmd_buffer->state.hiz_enabled = info.hiz_usage == ISL_AUX_USAGE_HIZ;
4178 }
4179
4180 /**
4181 * This ANDs the view mask of the current subpass with the pending clear
4182 * views in the attachment to get the mask of views active in the subpass
4183 * that still need to be cleared.
4184 */
4185 static inline uint32_t
4186 get_multiview_subpass_clear_mask(const struct anv_cmd_state *cmd_state,
4187 const struct anv_attachment_state *att_state)
4188 {
4189 return cmd_state->subpass->view_mask & att_state->pending_clear_views;
4190 }
4191
4192 static inline bool
4193 do_first_layer_clear(const struct anv_cmd_state *cmd_state,
4194 const struct anv_attachment_state *att_state)
4195 {
4196 if (!cmd_state->subpass->view_mask)
4197 return true;
4198
4199 uint32_t pending_clear_mask =
4200 get_multiview_subpass_clear_mask(cmd_state, att_state);
4201
4202 return pending_clear_mask & 1;
4203 }
4204
4205 static inline bool
4206 current_subpass_is_last_for_attachment(const struct anv_cmd_state *cmd_state,
4207 uint32_t att_idx)
4208 {
4209 const uint32_t last_subpass_idx =
4210 cmd_state->pass->attachments[att_idx].last_subpass_idx;
4211 const struct anv_subpass *last_subpass =
4212 &cmd_state->pass->subpasses[last_subpass_idx];
4213 return last_subpass == cmd_state->subpass;
4214 }
4215
4216 static void
4217 cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
4218 uint32_t subpass_id)
4219 {
4220 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4221 struct anv_subpass *subpass = &cmd_state->pass->subpasses[subpass_id];
4222 cmd_state->subpass = subpass;
4223
4224 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
4225
4226 /* Our implementation of VK_KHR_multiview uses instancing to draw the
4227 * different views. If the client asks for instancing, we need to use the
4228 * Instance Data Step Rate to ensure that we repeat the client's
4229 * per-instance data once for each view. Since this bit is in
4230 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
4231 * of each subpass.
4232 */
4233 if (GEN_GEN == 7)
4234 cmd_buffer->state.gfx.vb_dirty |= ~0;
4235
4236 /* It is possible to start a render pass with an old pipeline. Because the
4237 * render pass and subpass index are both baked into the pipeline, this is
4238 * highly unlikely. In order to do so, it requires that you have a render
4239 * pass with a single subpass and that you use that render pass twice
4240 * back-to-back and use the same pipeline at the start of the second render
4241 * pass as at the end of the first. In order to avoid unpredictable issues
4242 * with this edge case, we just dirty the pipeline at the start of every
4243 * subpass.
4244 */
4245 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_PIPELINE;
4246
4247 /* Accumulate any subpass flushes that need to happen before the subpass */
4248 cmd_buffer->state.pending_pipe_bits |=
4249 cmd_buffer->state.pass->subpass_flushes[subpass_id];
4250
4251 VkRect2D render_area = cmd_buffer->state.render_area;
4252 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4253
4254 bool is_multiview = subpass->view_mask != 0;
4255
4256 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4257 const uint32_t a = subpass->attachments[i].attachment;
4258 if (a == VK_ATTACHMENT_UNUSED)
4259 continue;
4260
4261 assert(a < cmd_state->pass->attachment_count);
4262 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4263
4264 struct anv_image_view *iview = cmd_state->attachments[a].image_view;
4265 const struct anv_image *image = iview->image;
4266
4267 /* A resolve is necessary before use as an input attachment if the clear
4268 * color or auxiliary buffer usage isn't supported by the sampler.
4269 */
4270 const bool input_needs_resolve =
4271 (att_state->fast_clear && !att_state->clear_color_is_zero_one) ||
4272 att_state->input_aux_usage != att_state->aux_usage;
4273
4274 VkImageLayout target_layout;
4275 if (iview->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV &&
4276 !input_needs_resolve) {
4277 /* Layout transitions before the final only help to enable sampling
4278 * as an input attachment. If the input attachment supports sampling
4279 * using the auxiliary surface, we can skip such transitions by
4280 * making the target layout one that is CCS-aware.
4281 */
4282 target_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
4283 } else {
4284 target_layout = subpass->attachments[i].layout;
4285 }
4286
4287 uint32_t base_layer, layer_count;
4288 if (image->type == VK_IMAGE_TYPE_3D) {
4289 base_layer = 0;
4290 layer_count = anv_minify(iview->image->extent.depth,
4291 iview->planes[0].isl.base_level);
4292 } else {
4293 base_layer = iview->planes[0].isl.base_array_layer;
4294 layer_count = fb->layers;
4295 }
4296
4297 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
4298 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4299 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4300 iview->planes[0].isl.base_level, 1,
4301 base_layer, layer_count,
4302 att_state->current_layout, target_layout);
4303 }
4304
4305 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4306 transition_depth_buffer(cmd_buffer, image,
4307 att_state->current_layout, target_layout);
4308 att_state->aux_usage =
4309 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
4310 VK_IMAGE_ASPECT_DEPTH_BIT, target_layout);
4311 }
4312
4313 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
4314 transition_stencil_buffer(cmd_buffer, image,
4315 iview->planes[0].isl.base_level, 1,
4316 base_layer, layer_count,
4317 att_state->current_layout, target_layout);
4318 }
4319 att_state->current_layout = target_layout;
4320
4321 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
4322 assert(att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4323
4324 /* Multi-planar images are not supported as attachments */
4325 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4326 assert(image->n_planes == 1);
4327
4328 uint32_t base_clear_layer = iview->planes[0].isl.base_array_layer;
4329 uint32_t clear_layer_count = fb->layers;
4330
4331 if (att_state->fast_clear &&
4332 do_first_layer_clear(cmd_state, att_state)) {
4333 /* We only support fast-clears on the first layer */
4334 assert(iview->planes[0].isl.base_level == 0);
4335 assert(iview->planes[0].isl.base_array_layer == 0);
4336
4337 union isl_color_value clear_color = {};
4338 anv_clear_color_from_att_state(&clear_color, att_state, iview);
4339 if (iview->image->samples == 1) {
4340 anv_image_ccs_op(cmd_buffer, image,
4341 iview->planes[0].isl.format,
4342 VK_IMAGE_ASPECT_COLOR_BIT,
4343 0, 0, 1, ISL_AUX_OP_FAST_CLEAR,
4344 &clear_color,
4345 false);
4346 } else {
4347 anv_image_mcs_op(cmd_buffer, image,
4348 iview->planes[0].isl.format,
4349 VK_IMAGE_ASPECT_COLOR_BIT,
4350 0, 1, ISL_AUX_OP_FAST_CLEAR,
4351 &clear_color,
4352 false);
4353 }
4354 base_clear_layer++;
4355 clear_layer_count--;
4356 if (is_multiview)
4357 att_state->pending_clear_views &= ~1;
4358
4359 if (att_state->clear_color_is_zero) {
4360 /* This image has the auxiliary buffer enabled. We can mark the
4361 * subresource as not needing a resolve because the clear color
4362 * will match what's in every RENDER_SURFACE_STATE object when
4363 * it's being used for sampling.
4364 */
4365 set_image_fast_clear_state(cmd_buffer, iview->image,
4366 VK_IMAGE_ASPECT_COLOR_BIT,
4367 ANV_FAST_CLEAR_DEFAULT_VALUE);
4368 } else {
4369 set_image_fast_clear_state(cmd_buffer, iview->image,
4370 VK_IMAGE_ASPECT_COLOR_BIT,
4371 ANV_FAST_CLEAR_ANY);
4372 }
4373 }
4374
4375 /* From the VkFramebufferCreateInfo spec:
4376 *
4377 * "If the render pass uses multiview, then layers must be one and each
4378 * attachment requires a number of layers that is greater than the
4379 * maximum bit index set in the view mask in the subpasses in which it
4380 * is used."
4381 *
4382 * So if multiview is active we ignore the number of layers in the
4383 * framebuffer and instead we honor the view mask from the subpass.
4384 */
4385 if (is_multiview) {
4386 assert(image->n_planes == 1);
4387 uint32_t pending_clear_mask =
4388 get_multiview_subpass_clear_mask(cmd_state, att_state);
4389
4390 uint32_t layer_idx;
4391 for_each_bit(layer_idx, pending_clear_mask) {
4392 uint32_t layer =
4393 iview->planes[0].isl.base_array_layer + layer_idx;
4394
4395 anv_image_clear_color(cmd_buffer, image,
4396 VK_IMAGE_ASPECT_COLOR_BIT,
4397 att_state->aux_usage,
4398 iview->planes[0].isl.format,
4399 iview->planes[0].isl.swizzle,
4400 iview->planes[0].isl.base_level,
4401 layer, 1,
4402 render_area,
4403 vk_to_isl_color(att_state->clear_value.color));
4404 }
4405
4406 att_state->pending_clear_views &= ~pending_clear_mask;
4407 } else if (clear_layer_count > 0) {
4408 assert(image->n_planes == 1);
4409 anv_image_clear_color(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4410 att_state->aux_usage,
4411 iview->planes[0].isl.format,
4412 iview->planes[0].isl.swizzle,
4413 iview->planes[0].isl.base_level,
4414 base_clear_layer, clear_layer_count,
4415 render_area,
4416 vk_to_isl_color(att_state->clear_value.color));
4417 }
4418 } else if (att_state->pending_clear_aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
4419 VK_IMAGE_ASPECT_STENCIL_BIT)) {
4420 if (att_state->fast_clear && !is_multiview) {
4421 /* We currently only support HiZ for single-layer images */
4422 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4423 assert(iview->image->planes[0].aux_usage == ISL_AUX_USAGE_HIZ);
4424 assert(iview->planes[0].isl.base_level == 0);
4425 assert(iview->planes[0].isl.base_array_layer == 0);
4426 assert(fb->layers == 1);
4427 }
4428
4429 anv_image_hiz_clear(cmd_buffer, image,
4430 att_state->pending_clear_aspects,
4431 iview->planes[0].isl.base_level,
4432 iview->planes[0].isl.base_array_layer,
4433 fb->layers, render_area,
4434 att_state->clear_value.depthStencil.stencil);
4435 } else if (is_multiview) {
4436 uint32_t pending_clear_mask =
4437 get_multiview_subpass_clear_mask(cmd_state, att_state);
4438
4439 uint32_t layer_idx;
4440 for_each_bit(layer_idx, pending_clear_mask) {
4441 uint32_t layer =
4442 iview->planes[0].isl.base_array_layer + layer_idx;
4443
4444 anv_image_clear_depth_stencil(cmd_buffer, image,
4445 att_state->pending_clear_aspects,
4446 att_state->aux_usage,
4447 iview->planes[0].isl.base_level,
4448 layer, 1,
4449 render_area,
4450 att_state->clear_value.depthStencil.depth,
4451 att_state->clear_value.depthStencil.stencil);
4452 }
4453
4454 att_state->pending_clear_views &= ~pending_clear_mask;
4455 } else {
4456 anv_image_clear_depth_stencil(cmd_buffer, image,
4457 att_state->pending_clear_aspects,
4458 att_state->aux_usage,
4459 iview->planes[0].isl.base_level,
4460 iview->planes[0].isl.base_array_layer,
4461 fb->layers, render_area,
4462 att_state->clear_value.depthStencil.depth,
4463 att_state->clear_value.depthStencil.stencil);
4464 }
4465 } else {
4466 assert(att_state->pending_clear_aspects == 0);
4467 }
4468
4469 if (GEN_GEN < 10 &&
4470 (att_state->pending_load_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
4471 image->planes[0].aux_surface.isl.size_B > 0 &&
4472 iview->planes[0].isl.base_level == 0 &&
4473 iview->planes[0].isl.base_array_layer == 0) {
4474 if (att_state->aux_usage != ISL_AUX_USAGE_NONE) {
4475 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color.state,
4476 image, VK_IMAGE_ASPECT_COLOR_BIT,
4477 false /* copy to ss */);
4478 }
4479
4480 if (need_input_attachment_state(&cmd_state->pass->attachments[a]) &&
4481 att_state->input_aux_usage != ISL_AUX_USAGE_NONE) {
4482 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->input.state,
4483 image, VK_IMAGE_ASPECT_COLOR_BIT,
4484 false /* copy to ss */);
4485 }
4486 }
4487
4488 if (subpass->attachments[i].usage ==
4489 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
4490 /* We assume that if we're starting a subpass, we're going to do some
4491 * rendering so we may end up with compressed data.
4492 */
4493 genX(cmd_buffer_mark_image_written)(cmd_buffer, iview->image,
4494 VK_IMAGE_ASPECT_COLOR_BIT,
4495 att_state->aux_usage,
4496 iview->planes[0].isl.base_level,
4497 iview->planes[0].isl.base_array_layer,
4498 fb->layers);
4499 } else if (subpass->attachments[i].usage ==
4500 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
4501 /* We may be writing depth or stencil so we need to mark the surface.
4502 * Unfortunately, there's no way to know at this point whether the
4503 * depth or stencil tests used will actually write to the surface.
4504 *
4505 * Even though stencil may be plane 1, it always shares a base_level
4506 * with depth.
4507 */
4508 const struct isl_view *ds_view = &iview->planes[0].isl;
4509 if (iview->aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
4510 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
4511 VK_IMAGE_ASPECT_DEPTH_BIT,
4512 att_state->aux_usage,
4513 ds_view->base_level,
4514 ds_view->base_array_layer,
4515 fb->layers);
4516 }
4517 if (iview->aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
4518 /* Even though stencil may be plane 1, it always shares a
4519 * base_level with depth.
4520 */
4521 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
4522 VK_IMAGE_ASPECT_STENCIL_BIT,
4523 ISL_AUX_USAGE_NONE,
4524 ds_view->base_level,
4525 ds_view->base_array_layer,
4526 fb->layers);
4527 }
4528 }
4529
4530 /* If multiview is enabled, then we are only done clearing when we no
4531 * longer have pending layers to clear, or when we have processed the
4532 * last subpass that uses this attachment.
4533 */
4534 if (!is_multiview ||
4535 att_state->pending_clear_views == 0 ||
4536 current_subpass_is_last_for_attachment(cmd_state, a)) {
4537 att_state->pending_clear_aspects = 0;
4538 }
4539
4540 att_state->pending_load_aspects = 0;
4541 }
4542
4543 cmd_buffer_emit_depth_stencil(cmd_buffer);
4544
4545 #if GEN_GEN >= 11
4546 /* The PIPE_CONTROL command description says:
4547 *
4548 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
4549 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
4550 * Target Cache Flush by enabling this bit. When render target flush
4551 * is set due to new association of BTI, PS Scoreboard Stall bit must
4552 * be set in this packet."
4553 */
4554 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
4555 pc.RenderTargetCacheFlushEnable = true;
4556 pc.StallAtPixelScoreboard = true;
4557 #if GEN_GEN >= 12
4558 pc.TileCacheFlushEnable = true;
4559 #endif
4560 }
4561 #endif
4562 }
4563
4564 static enum blorp_filter
4565 vk_to_blorp_resolve_mode(VkResolveModeFlagBitsKHR vk_mode)
4566 {
4567 switch (vk_mode) {
4568 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR:
4569 return BLORP_FILTER_SAMPLE_0;
4570 case VK_RESOLVE_MODE_AVERAGE_BIT_KHR:
4571 return BLORP_FILTER_AVERAGE;
4572 case VK_RESOLVE_MODE_MIN_BIT_KHR:
4573 return BLORP_FILTER_MIN_SAMPLE;
4574 case VK_RESOLVE_MODE_MAX_BIT_KHR:
4575 return BLORP_FILTER_MAX_SAMPLE;
4576 default:
4577 return BLORP_FILTER_NONE;
4578 }
4579 }
4580
4581 static void
4582 cmd_buffer_end_subpass(struct anv_cmd_buffer *cmd_buffer)
4583 {
4584 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4585 struct anv_subpass *subpass = cmd_state->subpass;
4586 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
4587 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4588
4589 if (subpass->has_color_resolve) {
4590 /* We are about to do some MSAA resolves. We need to flush so that the
4591 * result of writes to the MSAA color attachments show up in the sampler
4592 * when we blit to the single-sampled resolve target.
4593 */
4594 cmd_buffer->state.pending_pipe_bits |=
4595 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
4596 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
4597
4598 for (uint32_t i = 0; i < subpass->color_count; ++i) {
4599 uint32_t src_att = subpass->color_attachments[i].attachment;
4600 uint32_t dst_att = subpass->resolve_attachments[i].attachment;
4601
4602 if (dst_att == VK_ATTACHMENT_UNUSED)
4603 continue;
4604
4605 assert(src_att < cmd_buffer->state.pass->attachment_count);
4606 assert(dst_att < cmd_buffer->state.pass->attachment_count);
4607
4608 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
4609 /* From the Vulkan 1.0 spec:
4610 *
4611 * If the first use of an attachment in a render pass is as a
4612 * resolve attachment, then the loadOp is effectively ignored
4613 * as the resolve is guaranteed to overwrite all pixels in the
4614 * render area.
4615 */
4616 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
4617 }
4618
4619 struct anv_image_view *src_iview = cmd_state->attachments[src_att].image_view;
4620 struct anv_image_view *dst_iview = cmd_state->attachments[dst_att].image_view;
4621
4622 const VkRect2D render_area = cmd_buffer->state.render_area;
4623
4624 enum isl_aux_usage src_aux_usage =
4625 cmd_buffer->state.attachments[src_att].aux_usage;
4626 enum isl_aux_usage dst_aux_usage =
4627 cmd_buffer->state.attachments[dst_att].aux_usage;
4628
4629 assert(src_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT &&
4630 dst_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT);
4631
4632 anv_image_msaa_resolve(cmd_buffer,
4633 src_iview->image, src_aux_usage,
4634 src_iview->planes[0].isl.base_level,
4635 src_iview->planes[0].isl.base_array_layer,
4636 dst_iview->image, dst_aux_usage,
4637 dst_iview->planes[0].isl.base_level,
4638 dst_iview->planes[0].isl.base_array_layer,
4639 VK_IMAGE_ASPECT_COLOR_BIT,
4640 render_area.offset.x, render_area.offset.y,
4641 render_area.offset.x, render_area.offset.y,
4642 render_area.extent.width,
4643 render_area.extent.height,
4644 fb->layers, BLORP_FILTER_NONE);
4645 }
4646 }
4647
4648 if (subpass->ds_resolve_attachment) {
4649 /* We are about to do some MSAA resolves. We need to flush so that the
4650 * result of writes to the MSAA depth attachments show up in the sampler
4651 * when we blit to the single-sampled resolve target.
4652 */
4653 cmd_buffer->state.pending_pipe_bits |=
4654 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
4655 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
4656
4657 uint32_t src_att = subpass->depth_stencil_attachment->attachment;
4658 uint32_t dst_att = subpass->ds_resolve_attachment->attachment;
4659
4660 assert(src_att < cmd_buffer->state.pass->attachment_count);
4661 assert(dst_att < cmd_buffer->state.pass->attachment_count);
4662
4663 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
4664 /* From the Vulkan 1.0 spec:
4665 *
4666 * If the first use of an attachment in a render pass is as a
4667 * resolve attachment, then the loadOp is effectively ignored
4668 * as the resolve is guaranteed to overwrite all pixels in the
4669 * render area.
4670 */
4671 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
4672 }
4673
4674 struct anv_image_view *src_iview = cmd_state->attachments[src_att].image_view;
4675 struct anv_image_view *dst_iview = cmd_state->attachments[dst_att].image_view;
4676
4677 const VkRect2D render_area = cmd_buffer->state.render_area;
4678
4679 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
4680 subpass->depth_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
4681
4682 struct anv_attachment_state *src_state =
4683 &cmd_state->attachments[src_att];
4684 struct anv_attachment_state *dst_state =
4685 &cmd_state->attachments[dst_att];
4686
4687 /* MSAA resolves sample from the source attachment. Transition the
4688 * depth attachment first to get rid of any HiZ that we may not be
4689 * able to handle.
4690 */
4691 transition_depth_buffer(cmd_buffer, src_iview->image,
4692 src_state->current_layout,
4693 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
4694 src_state->aux_usage =
4695 anv_layout_to_aux_usage(&cmd_buffer->device->info, src_iview->image,
4696 VK_IMAGE_ASPECT_DEPTH_BIT,
4697 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
4698 src_state->current_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
4699
4700 /* MSAA resolves write to the resolve attachment as if it were any
4701 * other transfer op. Transition the resolve attachment accordingly.
4702 */
4703 VkImageLayout dst_initial_layout = dst_state->current_layout;
4704
4705 /* If our render area is the entire size of the image, we're going to
4706 * blow it all away so we can claim the initial layout is UNDEFINED
4707 * and we'll get a HiZ ambiguate instead of a resolve.
4708 */
4709 if (dst_iview->image->type != VK_IMAGE_TYPE_3D &&
4710 render_area.offset.x == 0 && render_area.offset.y == 0 &&
4711 render_area.extent.width == dst_iview->extent.width &&
4712 render_area.extent.height == dst_iview->extent.height)
4713 dst_initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
4714
4715 transition_depth_buffer(cmd_buffer, dst_iview->image,
4716 dst_initial_layout,
4717 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
4718 dst_state->aux_usage =
4719 anv_layout_to_aux_usage(&cmd_buffer->device->info, dst_iview->image,
4720 VK_IMAGE_ASPECT_DEPTH_BIT,
4721 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
4722 dst_state->current_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
4723
4724 enum blorp_filter filter =
4725 vk_to_blorp_resolve_mode(subpass->depth_resolve_mode);
4726
4727 anv_image_msaa_resolve(cmd_buffer,
4728 src_iview->image, src_state->aux_usage,
4729 src_iview->planes[0].isl.base_level,
4730 src_iview->planes[0].isl.base_array_layer,
4731 dst_iview->image, dst_state->aux_usage,
4732 dst_iview->planes[0].isl.base_level,
4733 dst_iview->planes[0].isl.base_array_layer,
4734 VK_IMAGE_ASPECT_DEPTH_BIT,
4735 render_area.offset.x, render_area.offset.y,
4736 render_area.offset.x, render_area.offset.y,
4737 render_area.extent.width,
4738 render_area.extent.height,
4739 fb->layers, filter);
4740 }
4741
4742 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
4743 subpass->stencil_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
4744
4745 enum isl_aux_usage src_aux_usage = ISL_AUX_USAGE_NONE;
4746 enum isl_aux_usage dst_aux_usage = ISL_AUX_USAGE_NONE;
4747
4748 enum blorp_filter filter =
4749 vk_to_blorp_resolve_mode(subpass->stencil_resolve_mode);
4750
4751 anv_image_msaa_resolve(cmd_buffer,
4752 src_iview->image, src_aux_usage,
4753 src_iview->planes[0].isl.base_level,
4754 src_iview->planes[0].isl.base_array_layer,
4755 dst_iview->image, dst_aux_usage,
4756 dst_iview->planes[0].isl.base_level,
4757 dst_iview->planes[0].isl.base_array_layer,
4758 VK_IMAGE_ASPECT_STENCIL_BIT,
4759 render_area.offset.x, render_area.offset.y,
4760 render_area.offset.x, render_area.offset.y,
4761 render_area.extent.width,
4762 render_area.extent.height,
4763 fb->layers, filter);
4764 }
4765 }
4766
4767 #if GEN_GEN == 7
4768 /* On gen7, we have to store a texturable version of the stencil buffer in
4769 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
4770 * forth at strategic points. Stencil writes are only allowed in three
4771 * layouts:
4772 *
4773 * - VK_IMAGE_LAYOUT_GENERAL
4774 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
4775 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
4776 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
4777 *
4778 * For general, we have no nice opportunity to transition so we do the copy
4779 * to the shadow unconditionally at the end of the subpass. For transfer
4780 * destinations, we can update it as part of the transfer op. For the
4781 * other two, we delay the copy until a transition into some other layout.
4782 */
4783 if (subpass->depth_stencil_attachment) {
4784 uint32_t a = subpass->depth_stencil_attachment->attachment;
4785 assert(a != VK_ATTACHMENT_UNUSED);
4786
4787 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4788 struct anv_image_view *iview = cmd_state->attachments[a].image_view;;
4789 const struct anv_image *image = iview->image;
4790
4791 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
4792 uint32_t plane = anv_image_aspect_to_plane(image->aspects,
4793 VK_IMAGE_ASPECT_STENCIL_BIT);
4794
4795 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
4796 att_state->current_layout == VK_IMAGE_LAYOUT_GENERAL) {
4797 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
4798 anv_image_copy_to_shadow(cmd_buffer, image,
4799 VK_IMAGE_ASPECT_STENCIL_BIT,
4800 iview->planes[plane].isl.base_level, 1,
4801 iview->planes[plane].isl.base_array_layer,
4802 fb->layers);
4803 }
4804 }
4805 }
4806 #endif /* GEN_GEN == 7 */
4807
4808 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4809 const uint32_t a = subpass->attachments[i].attachment;
4810 if (a == VK_ATTACHMENT_UNUSED)
4811 continue;
4812
4813 if (cmd_state->pass->attachments[a].last_subpass_idx != subpass_id)
4814 continue;
4815
4816 assert(a < cmd_state->pass->attachment_count);
4817 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4818 struct anv_image_view *iview = cmd_state->attachments[a].image_view;
4819 const struct anv_image *image = iview->image;
4820
4821 if ((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
4822 image->vk_format != iview->vk_format) {
4823 enum anv_fast_clear_type fast_clear_type =
4824 anv_layout_to_fast_clear_type(&cmd_buffer->device->info,
4825 image, VK_IMAGE_ASPECT_COLOR_BIT,
4826 att_state->current_layout);
4827
4828 /* If any clear color was used, flush it down the aux surfaces. If we
4829 * don't do it now using the view's format we might use the clear
4830 * color incorrectly in the following resolves (for example with an
4831 * SRGB view & a UNORM image).
4832 */
4833 if (fast_clear_type != ANV_FAST_CLEAR_NONE) {
4834 anv_perf_warn(cmd_buffer->device->instance, iview,
4835 "Doing a partial resolve to get rid of clear color at the "
4836 "end of a renderpass due to an image/view format mismatch");
4837
4838 uint32_t base_layer, layer_count;
4839 if (image->type == VK_IMAGE_TYPE_3D) {
4840 base_layer = 0;
4841 layer_count = anv_minify(iview->image->extent.depth,
4842 iview->planes[0].isl.base_level);
4843 } else {
4844 base_layer = iview->planes[0].isl.base_array_layer;
4845 layer_count = fb->layers;
4846 }
4847
4848 for (uint32_t a = 0; a < layer_count; a++) {
4849 uint32_t array_layer = base_layer + a;
4850 if (image->samples == 1) {
4851 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
4852 iview->planes[0].isl.format,
4853 VK_IMAGE_ASPECT_COLOR_BIT,
4854 iview->planes[0].isl.base_level,
4855 array_layer,
4856 ISL_AUX_OP_PARTIAL_RESOLVE,
4857 ANV_FAST_CLEAR_NONE);
4858 } else {
4859 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
4860 iview->planes[0].isl.format,
4861 VK_IMAGE_ASPECT_COLOR_BIT,
4862 base_layer,
4863 ISL_AUX_OP_PARTIAL_RESOLVE,
4864 ANV_FAST_CLEAR_NONE);
4865 }
4866 }
4867 }
4868 }
4869
4870 /* Transition the image into the final layout for this render pass */
4871 VkImageLayout target_layout =
4872 cmd_state->pass->attachments[a].final_layout;
4873
4874 uint32_t base_layer, layer_count;
4875 if (image->type == VK_IMAGE_TYPE_3D) {
4876 base_layer = 0;
4877 layer_count = anv_minify(iview->image->extent.depth,
4878 iview->planes[0].isl.base_level);
4879 } else {
4880 base_layer = iview->planes[0].isl.base_array_layer;
4881 layer_count = fb->layers;
4882 }
4883
4884 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
4885 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4886 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4887 iview->planes[0].isl.base_level, 1,
4888 base_layer, layer_count,
4889 att_state->current_layout, target_layout);
4890 }
4891
4892 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4893 transition_depth_buffer(cmd_buffer, image,
4894 att_state->current_layout, target_layout);
4895 }
4896
4897 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
4898 transition_stencil_buffer(cmd_buffer, image,
4899 iview->planes[0].isl.base_level, 1,
4900 base_layer, layer_count,
4901 att_state->current_layout, target_layout);
4902 }
4903 }
4904
4905 /* Accumulate any subpass flushes that need to happen after the subpass.
4906 * Yes, they do get accumulated twice in the NextSubpass case but since
4907 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
4908 * ORing the bits in twice so it's harmless.
4909 */
4910 cmd_buffer->state.pending_pipe_bits |=
4911 cmd_buffer->state.pass->subpass_flushes[subpass_id + 1];
4912 }
4913
4914 void genX(CmdBeginRenderPass)(
4915 VkCommandBuffer commandBuffer,
4916 const VkRenderPassBeginInfo* pRenderPassBegin,
4917 VkSubpassContents contents)
4918 {
4919 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4920 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
4921 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
4922
4923 cmd_buffer->state.framebuffer = framebuffer;
4924 cmd_buffer->state.pass = pass;
4925 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
4926 VkResult result =
4927 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
4928
4929 /* If we failed to setup the attachments we should not try to go further */
4930 if (result != VK_SUCCESS) {
4931 assert(anv_batch_has_error(&cmd_buffer->batch));
4932 return;
4933 }
4934
4935 genX(flush_pipeline_select_3d)(cmd_buffer);
4936
4937 cmd_buffer_begin_subpass(cmd_buffer, 0);
4938 }
4939
4940 void genX(CmdBeginRenderPass2KHR)(
4941 VkCommandBuffer commandBuffer,
4942 const VkRenderPassBeginInfo* pRenderPassBeginInfo,
4943 const VkSubpassBeginInfoKHR* pSubpassBeginInfo)
4944 {
4945 genX(CmdBeginRenderPass)(commandBuffer, pRenderPassBeginInfo,
4946 pSubpassBeginInfo->contents);
4947 }
4948
4949 void genX(CmdNextSubpass)(
4950 VkCommandBuffer commandBuffer,
4951 VkSubpassContents contents)
4952 {
4953 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4954
4955 if (anv_batch_has_error(&cmd_buffer->batch))
4956 return;
4957
4958 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
4959
4960 uint32_t prev_subpass = anv_get_subpass_id(&cmd_buffer->state);
4961 cmd_buffer_end_subpass(cmd_buffer);
4962 cmd_buffer_begin_subpass(cmd_buffer, prev_subpass + 1);
4963 }
4964
4965 void genX(CmdNextSubpass2KHR)(
4966 VkCommandBuffer commandBuffer,
4967 const VkSubpassBeginInfoKHR* pSubpassBeginInfo,
4968 const VkSubpassEndInfoKHR* pSubpassEndInfo)
4969 {
4970 genX(CmdNextSubpass)(commandBuffer, pSubpassBeginInfo->contents);
4971 }
4972
4973 void genX(CmdEndRenderPass)(
4974 VkCommandBuffer commandBuffer)
4975 {
4976 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4977
4978 if (anv_batch_has_error(&cmd_buffer->batch))
4979 return;
4980
4981 cmd_buffer_end_subpass(cmd_buffer);
4982
4983 cmd_buffer->state.hiz_enabled = false;
4984
4985 #ifndef NDEBUG
4986 anv_dump_add_attachments(cmd_buffer);
4987 #endif
4988
4989 /* Remove references to render pass specific state. This enables us to
4990 * detect whether or not we're in a renderpass.
4991 */
4992 cmd_buffer->state.framebuffer = NULL;
4993 cmd_buffer->state.pass = NULL;
4994 cmd_buffer->state.subpass = NULL;
4995 }
4996
4997 void genX(CmdEndRenderPass2KHR)(
4998 VkCommandBuffer commandBuffer,
4999 const VkSubpassEndInfoKHR* pSubpassEndInfo)
5000 {
5001 genX(CmdEndRenderPass)(commandBuffer);
5002 }
5003
5004 void
5005 genX(cmd_emit_conditional_render_predicate)(struct anv_cmd_buffer *cmd_buffer)
5006 {
5007 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5008 struct gen_mi_builder b;
5009 gen_mi_builder_init(&b, &cmd_buffer->batch);
5010
5011 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
5012 gen_mi_reg32(ANV_PREDICATE_RESULT_REG));
5013 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
5014
5015 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
5016 mip.LoadOperation = LOAD_LOADINV;
5017 mip.CombineOperation = COMBINE_SET;
5018 mip.CompareOperation = COMPARE_SRCS_EQUAL;
5019 }
5020 #endif
5021 }
5022
5023 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5024 void genX(CmdBeginConditionalRenderingEXT)(
5025 VkCommandBuffer commandBuffer,
5026 const VkConditionalRenderingBeginInfoEXT* pConditionalRenderingBegin)
5027 {
5028 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5029 ANV_FROM_HANDLE(anv_buffer, buffer, pConditionalRenderingBegin->buffer);
5030 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
5031 struct anv_address value_address =
5032 anv_address_add(buffer->address, pConditionalRenderingBegin->offset);
5033
5034 const bool isInverted = pConditionalRenderingBegin->flags &
5035 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT;
5036
5037 cmd_state->conditional_render_enabled = true;
5038
5039 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
5040
5041 struct gen_mi_builder b;
5042 gen_mi_builder_init(&b, &cmd_buffer->batch);
5043
5044 /* Section 19.4 of the Vulkan 1.1.85 spec says:
5045 *
5046 * If the value of the predicate in buffer memory changes
5047 * while conditional rendering is active, the rendering commands
5048 * may be discarded in an implementation-dependent way.
5049 * Some implementations may latch the value of the predicate
5050 * upon beginning conditional rendering while others
5051 * may read it before every rendering command.
5052 *
5053 * So it's perfectly fine to read a value from the buffer once.
5054 */
5055 struct gen_mi_value value = gen_mi_mem32(value_address);
5056
5057 /* Precompute predicate result, it is necessary to support secondary
5058 * command buffers since it is unknown if conditional rendering is
5059 * inverted when populating them.
5060 */
5061 gen_mi_store(&b, gen_mi_reg64(ANV_PREDICATE_RESULT_REG),
5062 isInverted ? gen_mi_uge(&b, gen_mi_imm(0), value) :
5063 gen_mi_ult(&b, gen_mi_imm(0), value));
5064 }
5065
5066 void genX(CmdEndConditionalRenderingEXT)(
5067 VkCommandBuffer commandBuffer)
5068 {
5069 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5070 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
5071
5072 cmd_state->conditional_render_enabled = false;
5073 }
5074 #endif
5075
5076 /* Set of stage bits for which are pipelined, i.e. they get queued by the
5077 * command streamer for later execution.
5078 */
5079 #define ANV_PIPELINE_STAGE_PIPELINED_BITS \
5080 (VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | \
5081 VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | \
5082 VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT | \
5083 VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT | \
5084 VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | \
5085 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | \
5086 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | \
5087 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | \
5088 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | \
5089 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | \
5090 VK_PIPELINE_STAGE_TRANSFER_BIT | \
5091 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT | \
5092 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT | \
5093 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT)
5094
5095 void genX(CmdSetEvent)(
5096 VkCommandBuffer commandBuffer,
5097 VkEvent _event,
5098 VkPipelineStageFlags stageMask)
5099 {
5100 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5101 ANV_FROM_HANDLE(anv_event, event, _event);
5102
5103 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
5104 if (stageMask & ANV_PIPELINE_STAGE_PIPELINED_BITS) {
5105 pc.StallAtPixelScoreboard = true;
5106 pc.CommandStreamerStallEnable = true;
5107 }
5108
5109 pc.DestinationAddressType = DAT_PPGTT,
5110 pc.PostSyncOperation = WriteImmediateData,
5111 pc.Address = (struct anv_address) {
5112 cmd_buffer->device->dynamic_state_pool.block_pool.bo,
5113 event->state.offset
5114 };
5115 pc.ImmediateData = VK_EVENT_SET;
5116 }
5117 }
5118
5119 void genX(CmdResetEvent)(
5120 VkCommandBuffer commandBuffer,
5121 VkEvent _event,
5122 VkPipelineStageFlags stageMask)
5123 {
5124 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5125 ANV_FROM_HANDLE(anv_event, event, _event);
5126
5127 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
5128 if (stageMask & ANV_PIPELINE_STAGE_PIPELINED_BITS) {
5129 pc.StallAtPixelScoreboard = true;
5130 pc.CommandStreamerStallEnable = true;
5131 }
5132
5133 pc.DestinationAddressType = DAT_PPGTT;
5134 pc.PostSyncOperation = WriteImmediateData;
5135 pc.Address = (struct anv_address) {
5136 cmd_buffer->device->dynamic_state_pool.block_pool.bo,
5137 event->state.offset
5138 };
5139 pc.ImmediateData = VK_EVENT_RESET;
5140 }
5141 }
5142
5143 void genX(CmdWaitEvents)(
5144 VkCommandBuffer commandBuffer,
5145 uint32_t eventCount,
5146 const VkEvent* pEvents,
5147 VkPipelineStageFlags srcStageMask,
5148 VkPipelineStageFlags destStageMask,
5149 uint32_t memoryBarrierCount,
5150 const VkMemoryBarrier* pMemoryBarriers,
5151 uint32_t bufferMemoryBarrierCount,
5152 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
5153 uint32_t imageMemoryBarrierCount,
5154 const VkImageMemoryBarrier* pImageMemoryBarriers)
5155 {
5156 #if GEN_GEN >= 8
5157 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5158
5159 for (uint32_t i = 0; i < eventCount; i++) {
5160 ANV_FROM_HANDLE(anv_event, event, pEvents[i]);
5161
5162 anv_batch_emit(&cmd_buffer->batch, GENX(MI_SEMAPHORE_WAIT), sem) {
5163 sem.WaitMode = PollingMode,
5164 sem.CompareOperation = COMPARE_SAD_EQUAL_SDD,
5165 sem.SemaphoreDataDword = VK_EVENT_SET,
5166 sem.SemaphoreAddress = (struct anv_address) {
5167 cmd_buffer->device->dynamic_state_pool.block_pool.bo,
5168 event->state.offset
5169 };
5170 }
5171 }
5172 #else
5173 anv_finishme("Implement events on gen7");
5174 #endif
5175
5176 genX(CmdPipelineBarrier)(commandBuffer, srcStageMask, destStageMask,
5177 false, /* byRegion */
5178 memoryBarrierCount, pMemoryBarriers,
5179 bufferMemoryBarrierCount, pBufferMemoryBarriers,
5180 imageMemoryBarrierCount, pImageMemoryBarriers);
5181 }
5182
5183 VkResult genX(CmdSetPerformanceOverrideINTEL)(
5184 VkCommandBuffer commandBuffer,
5185 const VkPerformanceOverrideInfoINTEL* pOverrideInfo)
5186 {
5187 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5188
5189 switch (pOverrideInfo->type) {
5190 case VK_PERFORMANCE_OVERRIDE_TYPE_NULL_HARDWARE_INTEL: {
5191 uint32_t dw;
5192
5193 #if GEN_GEN >= 9
5194 anv_pack_struct(&dw, GENX(CS_DEBUG_MODE2),
5195 ._3DRenderingInstructionDisable = pOverrideInfo->enable,
5196 .MediaInstructionDisable = pOverrideInfo->enable,
5197 ._3DRenderingInstructionDisableMask = true,
5198 .MediaInstructionDisableMask = true);
5199 emit_lri(&cmd_buffer->batch, GENX(CS_DEBUG_MODE2_num), dw);
5200 #else
5201 anv_pack_struct(&dw, GENX(INSTPM),
5202 ._3DRenderingInstructionDisable = pOverrideInfo->enable,
5203 .MediaInstructionDisable = pOverrideInfo->enable,
5204 ._3DRenderingInstructionDisableMask = true,
5205 .MediaInstructionDisableMask = true);
5206 emit_lri(&cmd_buffer->batch, GENX(INSTPM_num), dw);
5207 #endif
5208 break;
5209 }
5210
5211 case VK_PERFORMANCE_OVERRIDE_TYPE_FLUSH_GPU_CACHES_INTEL:
5212 if (pOverrideInfo->enable) {
5213 /* FLUSH ALL THE THINGS! As requested by the MDAPI team. */
5214 cmd_buffer->state.pending_pipe_bits |=
5215 ANV_PIPE_FLUSH_BITS |
5216 ANV_PIPE_INVALIDATE_BITS;
5217 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
5218 }
5219 break;
5220
5221 default:
5222 unreachable("Invalid override");
5223 }
5224
5225 return VK_SUCCESS;
5226 }
5227
5228 VkResult genX(CmdSetPerformanceStreamMarkerINTEL)(
5229 VkCommandBuffer commandBuffer,
5230 const VkPerformanceStreamMarkerInfoINTEL* pMarkerInfo)
5231 {
5232 /* TODO: Waiting on the register to write, might depend on generation. */
5233
5234 return VK_SUCCESS;
5235 }