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